All of lore.kernel.org
 help / color / mirror / Atom feed
From: ebiederm-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org (Eric W. Biederman)
To: "Nicolas François"
	<nicolas.francois-Fa7rcPG4DJn7nK0/Xc0eeg@public.gmane.org>
Cc: Linux Containers
	<containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org>,
	Pkg-shadow-devel-XbBxUvOt3X2LieD7tvxI8l/i77bcL1HB@public.gmane.org,
	"Michael Kerrisk (man-pages)"
	<mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Subject: [PATCH 07/11] useradd: Add support for subordinate user identifiers
Date: Tue, 22 Jan 2013 01:17:30 -0800	[thread overview]
Message-ID: <8738xtr2z9.fsf@xmission.com> (raw)
In-Reply-To: <87d2wxshu0.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org> (Eric W. Biederman's message of "Tue, 22 Jan 2013 01:11:19 -0800")


Signed-off-by: "Eric W. Biederman" <ebiederm-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
---
 src/useradd.c |  141 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 140 insertions(+), 1 deletions(-)

diff --git a/src/useradd.c b/src/useradd.c
index bfddfc6..87ba98d 100644
--- a/src/useradd.c
+++ b/src/useradd.c
@@ -65,6 +65,7 @@
 #include "sgroupio.h"
 #endif
 #include "shadowio.h"
+#include "subordinateio.h"
 #ifdef WITH_TCB
 #include "tcbfuncs.h"
 #endif
@@ -121,12 +122,20 @@ static bool is_shadow_pwd;
 static bool is_shadow_grp;
 static bool sgr_locked = false;
 #endif
+static bool is_sub_uid = false;
+static bool is_sub_gid = false;
 static bool pw_locked = false;
 static bool gr_locked = false;
 static bool spw_locked = false;
+static bool sub_uid_locked = false;
+static bool sub_gid_locked = false;
 static char **user_groups;	/* NULL-terminated list */
 static long sys_ngroups;
 static bool do_grp_update = false;	/* group files need to be updated */
+static uid_t sub_uid_start;	/* New subordinate uid range */
+static unsigned long sub_uid_count;
+static gid_t sub_gid_start;	/* New subordinate gid range */
+static unsigned long sub_gid_count;
 
 static bool
     bflg = false,		/* new default root of home directory */
@@ -168,6 +177,8 @@ static bool home_added = false;
 #define E_GRP_UPDATE	10	/* can't update group file */
 #define E_HOMEDIR	12	/* can't create home directory */
 #define E_SE_UPDATE	14	/* can't update SELinux user mapping */
+#define E_SUB_UID_UPDATE 16	/* can't update the subordinate uid file */
+#define E_SUB_GID_UPDATE 18	/* can't update the subordinate gid file */
 
 #define DGROUP			"GROUP="
 #define DHOME			"HOME="
@@ -268,6 +279,32 @@ static void fail_exit (int code)
 		}
 	}
 #endif
+	if (sub_uid_locked) {
+		if (sub_uid_unlock () == 0) {
+			fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sub_uid_dbname ());
+			SYSLOG ((LOG_ERR, "failed to unlock %s", sub_uid_dbname ()));
+#ifdef WITH_AUDIT
+			audit_logger (AUDIT_ADD_USER, Prog,
+			              "unlocking subodinate user file",
+			              user_name, AUDIT_NO_ID,
+			              SHADOW_AUDIT_FAILURE);
+#endif
+			/* continue */
+		}
+	}
+	if (sub_gid_locked) {
+		if (sub_gid_unlock () == 0) {
+			fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sub_gid_dbname ());
+			SYSLOG ((LOG_ERR, "failed to unlock %s", sub_gid_dbname ()));
+#ifdef WITH_AUDIT
+			audit_logger (AUDIT_ADD_USER, Prog,
+			              "unlocking subodinate group file",
+			              user_name, AUDIT_NO_ID,
+			              SHADOW_AUDIT_FAILURE);
+#endif
+			/* continue */
+		}
+	}
 
 #ifdef WITH_AUDIT
 	audit_logger (AUDIT_ADD_USER, Prog,
@@ -1378,6 +1415,18 @@ static void close_files (void)
 		}
 #endif
 	}
+	if (is_sub_uid  && (sub_uid_close () == 0)) {
+		fprintf (stderr,
+		         _("%s: failure while writing changes to %s\n"), Prog, sub_uid_dbname ());
+		SYSLOG ((LOG_ERR, "failure while writing changes to %s", sub_uid_dbname ()));
+		fail_exit (E_SUB_UID_UPDATE);
+	}
+	if (is_sub_gid  && (sub_gid_close () == 0)) {
+		fprintf (stderr,
+		         _("%s: failure while writing changes to %s\n"), Prog, sub_gid_dbname ());
+		SYSLOG ((LOG_ERR, "failure while writing changes to %s", sub_gid_dbname ()));
+		fail_exit (E_SUB_GID_UPDATE);
+	}
 	if (is_shadow_pwd) {
 		if (spw_unlock () == 0) {
 			fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, spw_dbname ());
@@ -1432,6 +1481,34 @@ static void close_files (void)
 		sgr_locked = false;
 	}
 #endif
+	if (is_sub_uid) {
+		if (sub_uid_unlock () == 0) {
+			fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sub_uid_dbname ());
+			SYSLOG ((LOG_ERR, "failed to unlock %s", sub_uid_dbname ()));
+#ifdef WITH_AUDIT
+			audit_logger (AUDIT_ADD_USER, Prog,
+				"unlocking subordinate user file",
+				user_name, AUDIT_NO_ID,
+				SHADOW_AUDIT_FAILURE);
+#endif
+			/* continue */
+		}
+		sub_uid_locked = false;
+	}
+	if (is_sub_gid) {
+		if (sub_gid_unlock () == 0) {
+			fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sub_gid_dbname ());
+			SYSLOG ((LOG_ERR, "failed to unlock %s", sub_gid_dbname ()));
+#ifdef WITH_AUDIT
+			audit_logger (AUDIT_ADD_USER, Prog,
+				"unlocking subordinate group file",
+				user_name, AUDIT_NO_ID,
+				SHADOW_AUDIT_FAILURE);
+#endif
+			/* continue */
+		}
+		sub_gid_locked = false;
+	}
 }
 
 /*
@@ -1486,6 +1563,36 @@ static void open_files (void)
 		}
 	}
 #endif
+	if (is_sub_uid) {
+		if (sub_uid_lock () == 0) {
+			fprintf (stderr,
+			         _("%s: cannot lock %s; try again later.\n"),
+			         Prog, sub_uid_dbname ());
+			fail_exit (E_SUB_UID_UPDATE);
+		}
+		sub_uid_locked = true;
+		if (sub_uid_open (O_RDWR) == 0) {
+			fprintf (stderr,
+			         _("%s: cannot open %s\n"),
+			         Prog, sub_uid_dbname ());
+			fail_exit (E_SUB_UID_UPDATE);
+		}
+	}
+	if (is_sub_gid) {
+		if (sub_gid_lock () == 0) {
+			fprintf (stderr,
+			         _("%s: cannot lock %s; try again later.\n"),
+			         Prog, sub_gid_dbname ());
+			fail_exit (E_SUB_GID_UPDATE);
+		}
+		sub_gid_locked = true;
+		if (sub_gid_open (O_RDWR) == 0) {
+			fprintf (stderr,
+			         _("%s: cannot open %s\n"),
+			         Prog, sub_gid_dbname ());
+			fail_exit (E_SUB_GID_UPDATE);
+		}
+	}
 }
 
 static void open_shadow (void)
@@ -1732,13 +1839,27 @@ static void usr_update (void)
 #endif
 		fail_exit (E_PW_UPDATE);
 	}
+	if (is_sub_uid &&
+	    (sub_uid_add(user_name, sub_uid_start, sub_uid_count) == 0)) {
+		fprintf (stderr,
+		         _("%s: failed to prepare the new %s entry\n"),
+		         Prog, sub_uid_dbname ());
+		fail_exit (E_SUB_UID_UPDATE);
+	}
+	if (is_sub_gid &&
+	    (sub_gid_add(user_name, sub_gid_start, sub_gid_count) == 0)) {
+		fprintf (stderr,
+		         _("%s: failed to prepare the new %s entry\n"),
+		         Prog, sub_uid_dbname ());
+		fail_exit (E_SUB_GID_UPDATE);
+	}
+
 #ifdef WITH_AUDIT
 	audit_logger (AUDIT_ADD_USER, Prog,
 	              "adding user",
 	              user_name, (unsigned int) user_id,
 	              SHADOW_AUDIT_SUCCESS);
 #endif
-
 	/*
 	 * Do any group file updates for this user.
 	 */
@@ -1884,6 +2005,8 @@ int main (int argc, char **argv)
 #ifdef SHADOWGRP
 	is_shadow_grp = sgr_file_present ();
 #endif
+	is_sub_uid = sub_uid_file_present ();
+	is_sub_gid = sub_gid_file_present ();
 
 	get_defaults ();
 
@@ -2034,6 +2157,22 @@ int main (int argc, char **argv)
 		grp_add ();
 	}
 
+	if (is_sub_uid) {
+		if (find_new_sub_uids(user_name, &sub_uid_start, &sub_uid_count) < 0) {
+			fprintf (stderr,
+				_("%s: can't find subordinate user range\n"),
+				Prog);
+			fail_exit(E_SUB_UID_UPDATE);
+		}
+	}
+	if (is_sub_gid) {
+		if (find_new_sub_gids(user_name, &sub_gid_start, &sub_gid_count) < 0) {
+			fprintf (stderr,
+				_("%s: can't find subordinate group range\n"),
+				Prog);
+			fail_exit(E_SUB_GID_UPDATE);
+		}
+	}
 	usr_update ();
 
 	if (mflg) {
-- 
1.7.5.4

  parent reply	other threads:[~2013-01-22  9:17 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-22  9:11 [PATCH 00/11] pkg-shadow support subordinate ids with user namespaces Eric W. Biederman
     [not found] ` <87d2wxshu0.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2013-01-22  9:12   ` [PATCH 01/11] Documentation for /etc/subuid and /etc/subgid Eric W. Biederman
2013-01-22  9:12   ` [PATCH 02/11] login.defs.5: Document the new variables in login.defs Eric W. Biederman
2013-01-22  9:13   ` [PATCH 03/11] Implement commonio_append Eric W. Biederman
2013-01-22  9:13   ` Eric W. Biederman
2013-01-22  9:14   ` [PATCH 04/11] Add backend support for suboridnate uids and gids Eric W. Biederman
     [not found]     ` <87liblr344.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2013-01-23 18:22       ` Serge E. Hallyn
     [not found]         ` <20130123182206.GA4468-7LNsyQBKDXoIagZqoN9o3w@public.gmane.org>
2013-01-24 22:42           ` Eric W. Biederman
     [not found]             ` <87a9ryyzh2.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2013-01-24 22:50               ` Serge Hallyn
2013-01-24 22:59                 ` Eric W. Biederman
2013-01-24 23:13                 ` [PATCH] subordinateio: Fix subordinate_parse to have an internal static buffer Eric W. Biederman
2013-01-22  9:15   ` [PATCH 05/11] Implement find_new_sub_uids find_new_sub_gids Eric W. Biederman
2013-01-22  9:16   ` [PATCH 06/11] userdel: Add support for removing subordinate user and group ids Eric W. Biederman
2013-01-22  9:17   ` Eric W. Biederman [this message]
2013-01-22  9:17   ` [PATCH 08/11] Add support for detecting busy subordinate user ids Eric W. Biederman
2013-01-22  9:18   ` [PATCH 09/11] usermod: Add support for subordinate uids and gids Eric W. Biederman
2013-01-22  9:19   ` [PATCH 10/11] newusers: Add support for assiging " Eric W. Biederman
2013-01-22  9:20   ` [PATCH 11/11] newuidmap, newgidmap: New suid helpers for using " Eric W. Biederman
     [not found]     ` <87ehhdpoag.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2013-02-04 18:31       ` [PATCH 11/11] newuidmap,newgidmap: " Serge E. Hallyn
     [not found]         ` <20130204183129.GA27179-7LNsyQBKDXoIagZqoN9o3w@public.gmane.org>
2013-02-05 17:20           ` Serge E. Hallyn
2013-02-06  0:28           ` [PATCH 11/11] newuidmap, newgidmap: " Eric W. Biederman
     [not found]             ` <87sj5ai8us.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2013-02-06  3:00               ` [PATCH 11/11] newuidmap,newgidmap: " Serge E. Hallyn
     [not found]       ` <87fw1tr33a.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2013-06-15  0:15         ` [PATCH 05/11] Implement find_new_sub_uids find_new_sub_gids Seth Arnold
2013-10-25 20:30       ` [PATCH 11/11] newuidmap,newgidmap: New suid helpers for using subordinate uids and gids Serge E. Hallyn
     [not found]         ` <20131025203025.GA2467-7LNsyQBKDXoIagZqoN9o3w@public.gmane.org>
2013-10-26  0:42           ` [PATCH 11/11] newuidmap, newgidmap: " Eric W. Biederman
     [not found]             ` <87zjpw278b.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2013-10-26  2:33               ` Serge Hallyn
2013-10-26 21:50                 ` Eric W. Biederman
     [not found]                   ` <87iowjya4j.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2013-10-27 23:53                     ` Serge E. Hallyn
2013-01-29 18:15   ` [PATCH 00/11] pkg-shadow support subordinate ids with user namespaces Rob Landley
2013-01-29 22:28     ` Eric W. Biederman
2013-01-30  5:35   ` Vasily Kulikov
2013-01-30  6:40     ` Eric W. Biederman
     [not found]       ` <87vcafyy0k.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2013-01-30  7:38         ` Vasily Kulikov
2013-02-22 12:16   ` Glauber Costa
     [not found]     ` <51276189.5040803-bzQdu9zFT3WakBO8gow8eQ@public.gmane.org>
2013-02-22 16:34       ` Eric W. Biederman
     [not found]         ` <87zjyw489z.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2013-02-22 17:09           ` Glauber Costa
     [not found]             ` <5127A657.3010909-bzQdu9zFT3WakBO8gow8eQ@public.gmane.org>
2013-02-25 14:34               ` Serge Hallyn
     [not found]             ` <20130225143451.GE4387@sergelap>
2013-02-25 14:38               ` Glauber Costa
     [not found]                 ` <512B7773.9060704-bzQdu9zFT3WakBO8gow8eQ@public.gmane.org>
2013-02-25 15:15                   ` Serge Hallyn
2013-02-26  1:03                   ` Eric W. Biederman
2013-02-25 14:30           ` Serge Hallyn
2013-03-03 15:37   ` Serge E. Hallyn
2013-03-07 15:23   ` Dwight Engen
2013-07-28 17:14   ` [Pkg-shadow-devel] " Christian PERRIER
     [not found]     ` <20130728171451.GX5670-FvNwPcshoeM/MCprI7ZU+I/wHUNs+SP4HZ5vskTnxNA@public.gmane.org>
2013-07-28 17:58       ` Eric W. Biederman
     [not found]         ` <87r4eilg6y.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2013-07-29  0:33           ` Serge Hallyn
     [not found]         ` <11218395-363e-46cd-b7a1-4488079a4986@email.android.com>
     [not found]           ` <11218395-363e-46cd-b7a1-4488079a4986-2ueSQiBKiTY7tOexoI0I+QC/G2K4zDHf@public.gmane.org>
2013-08-06 14:54             ` Serge E. Hallyn
     [not found]               ` <20130806145403.GA20913-7LNsyQBKDXoIagZqoN9o3w@public.gmane.org>
2013-08-06 22:53                 ` Nicolas François
     [not found]                   ` <20130806225332.GA14109-pDMkYksm/NDBVznEOA0nCqMXiC8k1aZu0e7PPNI6Mm0@public.gmane.org>
2013-08-07 15:33                     ` Serge Hallyn
2013-08-07 18:04                       ` Eric W. Biederman
     [not found]                         ` <87eha5peb8.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2013-08-09 18:40                           ` Nicolas François
     [not found] ` <20130303153726.GA14737@austin.hallyn.com>
     [not found]   ` <20130303153726.GA14737-anj0Drq5vpzx6HRWoRZK3AC/G2K4zDHf@public.gmane.org>
2013-03-04  5:56     ` Christian PERRIER
     [not found]   ` <20130304055654.GE2629@mykerinos.kheops.frmug.org>
     [not found]     ` <20130304055654.GE2629-FvNwPcshoeM/MCprI7ZU+I/wHUNs+SP4HZ5vskTnxNA@public.gmane.org>
2013-03-05 22:05       ` Serge E. Hallyn
     [not found] ` <20130307102352.4a5943cd@oracle.com>
     [not found]   ` <20130307102352.4a5943cd-QHcLZuEGTsvQT0dZR+AlfA@public.gmane.org>
2013-03-07 21:54     ` Serge E. Hallyn
     [not found]       ` <20130307215457.GB9348-anj0Drq5vpzx6HRWoRZK3AC/G2K4zDHf@public.gmane.org>
2013-03-07 22:56         ` Eric W. Biederman
     [not found]           ` <876212rf9b.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2013-03-08  6:50             ` [Pkg-shadow-devel] " Christian PERRIER
     [not found]               ` <20130308065019.GI5885-FvNwPcshoeM/MCprI7ZU+I/wHUNs+SP4HZ5vskTnxNA@public.gmane.org>
2013-06-02 13:48                 ` Serge E. Hallyn
     [not found]                   ` <20130602134823.GB8004-anj0Drq5vpzx6HRWoRZK3AC/G2K4zDHf@public.gmane.org>
2013-06-02 14:04                     ` Christian PERRIER
     [not found]                       ` <20130602140436.GK9152-FvNwPcshoeM/MCprI7ZU+I/wHUNs+SP4HZ5vskTnxNA@public.gmane.org>
2013-06-02 14:33                         ` Serge E. Hallyn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8738xtr2z9.fsf@xmission.com \
    --to=ebiederm-as9lmozglivwk0htik3j/w@public.gmane.org \
    --cc=Pkg-shadow-devel-XbBxUvOt3X2LieD7tvxI8l/i77bcL1HB@public.gmane.org \
    --cc=containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org \
    --cc=mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    --cc=nicolas.francois-Fa7rcPG4DJn7nK0/Xc0eeg@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.