From patchwork Thu Sep 3 23:33:42 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Zhou X-Patchwork-Id: 514296 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from archives.nicira.com (li376-54.members.linode.com [96.126.127.54]) by ozlabs.org (Postfix) with ESMTP id C6C6714028F for ; Fri, 4 Sep 2015 09:36:51 +1000 (AEST) Received: from archives.nicira.com (localhost [127.0.0.1]) by archives.nicira.com (Postfix) with ESMTP id AD93E10BE9; Thu, 3 Sep 2015 16:34:13 -0700 (PDT) X-Original-To: dev@openvswitch.com Delivered-To: dev@openvswitch.com Received: from mail-pa0-f48.google.com (mail-pa0-f48.google.com [209.85.220.48]) by archives.nicira.com (Postfix) with ESMTPS id B6B3110BE8 for ; Thu, 3 Sep 2015 16:34:11 -0700 (PDT) Received: by pacwi10 with SMTP id wi10so4659336pac.3 for ; Thu, 03 Sep 2015 16:34:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=BiBlaTjXEeIeMJLufojsQ/eULlq6Qxr93/OpgY1wZTk=; b=m8RFRKGCqVikeQudbYDbH/RQqmTdyFcKKOc4BHIj9FAki3nWq/WE4SY2qyXuxtYqap sLTTjKRUA/r55sczaPKjhFOITiCZ5qb5VZsFa1MMHXwZUbv5oCfZDFG2c0kqX/+SMPc6 H4kjjmTo8E6//Cg8ptevhjmvKizYuyJberuW7wpe56neJ708UW/1nEDFmNNqaXUDuTkI qVLXPX61HC8j49KP4ORC/iMpMkN9x9lOJI+CGkymrONdjTIa0rITxwx/cRNUry0gR/BU eT8BqoPizosbvXg7Icahns/dm19Ax1wtktigDEtrmD+zVki5HgdJlnh4aD+lO0/iCbVB YJ/Q== X-Gm-Message-State: ALoCoQkwv5R9/fpvfOB13sCjQ3kXGis0j1HDCmmX/NTOrCnNh/IeHW6sB1D0hzpZSZeKyOMSWTxQ X-Received: by 10.66.186.39 with SMTP id fh7mr1224622pac.48.1441323241232; Thu, 03 Sep 2015 16:34:01 -0700 (PDT) Received: from ubuntu.localdomain ([208.91.1.34]) by smtp.gmail.com with ESMTPSA id vv2sm247828pab.21.2015.09.03.16.34.00 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 03 Sep 2015 16:34:00 -0700 (PDT) From: Andy Zhou To: dev@openvswitch.com Date: Thu, 3 Sep 2015 16:33:42 -0700 Message-Id: <1441323223-11889-2-git-send-email-azhou@nicira.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1441323223-11889-1-git-send-email-azhou@nicira.com> References: <1441323223-11889-1-git-send-email-azhou@nicira.com> Subject: [ovs-dev] [PATCH 2/3] lib: Add --user for daemon X-BeenThere: dev@openvswitch.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dev-bounces@openvswitch.org Sender: "dev" Allow daemon running as root to accept --user option, that accepts "user:group" string as input. Performs sanity check on the input, and store the converted uid and gid. daemon_become_new_user() needs to be called to make the actual switch. Signed-off-by: Andy Zhou --- lib/daemon-unix.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/daemon.h | 27 +++++++++++++++------ 2 files changed, 91 insertions(+), 7 deletions(-) diff --git a/lib/daemon-unix.c b/lib/daemon-unix.c index d52ac2d..44eb800 100644 --- a/lib/daemon-unix.c +++ b/lib/daemon-unix.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -693,6 +694,76 @@ should_service_stop(void) return false; } +void daemon_set_new_user(const char *user_spec) +{ + char *pos = strchr(user_spec, ':'); + + uid = getuid(); + gid = getgid(); + + if (gid || uid) { + VLOG_FATAL("%s: only root can use --user option", pidfile); + } + + user_spec += strspn(user_spec, " \t\r\n"); + int len = pos ? pos - user_spec : strlen(user_spec); + struct passwd pwd, *res; + char buf[4096]; + + if (len) { + user = xzalloc(len + 1); + strncpy(user, user_spec, len); + + if (getpwnam_r(user, &pwd, buf, 4096, &res)) { + VLOG_FATAL("%s: Invalid --user option %s (no such user %s)", + pidfile, user_spec, user); + } + } else { + /* User is not specified, use current user. */ + if (getpwuid_r(uid, &pwd, buf, 4096, &res)) { + VLOG_FATAL("%s: Invalid --user option %s (failed to lookup " + "current user with uid %d)", pidfile, user_spec, uid); + } + user = strdup(pwd.pw_name); + } + + uid = pwd.pw_uid; + gid = pwd.pw_gid; + + if (pos) { + char *grpstr = pos + 1; + grpstr += strspn(grpstr, " \t\r\n"); + + if (*grpstr) { + struct group grp, *res; + + if(getgrnam_r(grpstr, &grp, buf, 4096, &res)) { + VLOG_FATAL("%s: Invalid --user option %s (unknown group %s)", + pidfile, user_spec, grpstr); + } + + if(gid != grp.gr_gid) { + char **mem; + + for(mem = grp.gr_mem; *mem; ++mem) { + if (!strcmp(*mem, user)) { + break; + } + } + + if (!*mem) { + VLOG_FATAL("%s: Invalid --user option %s (user %s is " + "not in group %s)", pidfile, user_spec, + user, grpstr); + } + gid = grp.gr_gid; + } + } + } + + switch_to_new_user = true; +} + void daemon_become_new_user(void) { diff --git a/lib/daemon.h b/lib/daemon.h index fb97cde..4b25f46 100644 --- a/lib/daemon.h +++ b/lib/daemon.h @@ -42,14 +42,16 @@ OPT_NO_CHDIR, \ OPT_OVERWRITE_PIDFILE, \ OPT_PIDFILE, \ - OPT_MONITOR + OPT_MONITOR, \ + OPT_USER_GROUP -#define DAEMON_LONG_OPTIONS \ - {"detach", no_argument, NULL, OPT_DETACH}, \ - {"no-chdir", no_argument, NULL, OPT_NO_CHDIR}, \ - {"pidfile", optional_argument, NULL, OPT_PIDFILE}, \ +#define DAEMON_LONG_OPTIONS \ + {"detach", no_argument, NULL, OPT_DETACH}, \ + {"no-chdir", no_argument, NULL, OPT_NO_CHDIR}, \ + {"pidfile", optional_argument, NULL, OPT_PIDFILE}, \ {"overwrite-pidfile", no_argument, NULL, OPT_OVERWRITE_PIDFILE}, \ - {"monitor", no_argument, NULL, OPT_MONITOR} + {"monitor", no_argument, NULL, OPT_MONITOR}, \ + {"user", required_argument, NULL, OPT_USER_GROUP} #define DAEMON_OPTION_HANDLERS \ case OPT_DETACH: \ @@ -70,6 +72,10 @@ \ case OPT_MONITOR: \ daemon_set_monitor(); \ + break; \ + \ + case OPT_USER_GROUP: \ + daemon_set_new_user(optarg); \ break; void set_detach(void); @@ -77,6 +83,7 @@ void daemon_set_monitor(void); void set_no_chdir(void); void ignore_existing_pidfile(void); void daemon_become_new_user(void); +void daemon_set_new_user(const char *); pid_t read_pidfile(const char *name); #else #define DAEMON_OPTION_ENUMS \ @@ -85,7 +92,7 @@ pid_t read_pidfile(const char *name); OPT_PIDFILE, \ OPT_PIPE_HANDLE, \ OPT_SERVICE, \ - OPT_SERVICE_MONITOR + OPT_SERVICE_MONITOR \ #define DAEMON_LONG_OPTIONS \ {"detach", no_argument, NULL, OPT_DETACH}, \ @@ -120,6 +127,12 @@ void control_handler(DWORD request); void set_pipe_handle(const char *pipe_handle); static inline void +daemon_set_new_user(const char *) +{ + /* Not implemented. */ +} + +static inline void daemon_become_new_user(void) { /* Not implemented. */