[PATCH] adduser addgroup use LAST_ID (Was: adduser remove some code)

Tito farmatito at tiscali.it
Sat Dec 21 20:36:16 UTC 2013


On Saturday 21 December 2013 17:22:36 you wrote:
> Hi Tito !
> 
> >So I would simplify the code like:
> >
> >	opt_complementary = "-1:?2:SD:u+";
> >	opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir,
> >	&pw.pw_gecos, &pw.pw_shell, &usegroup, &pw.pw_uid);
> 
> IMO you miss one fact. The field pw.pw_uid (especially) is
> different size and type on different systems/libraries
> (sometimes only 16 bit data type). I think getopt32 expects
> pointer to an integer or unsigned data type. Writing to 16 bit
> data type using a pointer to int would write to unknown location.
> 
> Better to use a local variable, and let the compiler do the type
> mangling:
> 
>   unsigned uid;
>   ... getopt32( ..., &uid);
>   pw.pw_uid = uid;
> 
> But nevertheless you are right, the code parts shall be merged.
> 
> --
> Harald
> 

Hi,
could make sense to use the default values used
on Debian in the /etc/login.defs file

#
# Min/max values for automatic uid selection in useradd
#
UID_MIN                  1000
UID_MAX                 60000

Wew could also add a config option as we already have

 (100) First valid system uid or gid for adduser and addgroup
 (999) Last valid system uid or gid for adduser and addgroup
+ (60000) Last valid uid for adduser and addgroup


The attached patch changes adduser.c, addgroup.c
and Config.src to set and use CONFIG_LAST_ID.

Ciao,
Tito

Signed-off-by: Tito Ragusa <farmatito at tiscali.it>

--- loginutils/adduser.c.orig	2013-12-21 12:52:52.000000000 +0100
+++ loginutils/adduser.c	2013-12-21 21:25:01.587583405 +0100
@@ -36,12 +36,11 @@
 #define OPT_DONT_MAKE_HOME (1 << 6)
 #define OPT_UID            (1 << 7)
 
-/* We assume UID_T_MAX == INT_MAX */
 /* remix */
 /* recoded such that the uid may be passed in *p */
 static void passwd_study(struct passwd *p)
 {
-	int max = UINT_MAX;
+	int max = CONFIG_LAST_ID;
 
 	if (getpwnam(p->pw_name)) {
 		bb_error_msg_and_die("%s '%s' in use", "user", p->pw_name);
@@ -54,7 +53,6 @@ static void passwd_study(struct passwd *
 			max = CONFIG_LAST_SYSTEM_ID;
 		} else {
 			p->pw_uid = CONFIG_LAST_SYSTEM_ID + 1;
-			max = 64999;
 		}
 	}
 	/* check for a free uid (and maybe gid) */
@@ -147,6 +145,7 @@ int adduser_main(int argc UNUSED_PARAM,
 	const char *usegroup = NULL;
 	char *p;
 	unsigned opts;
+	char *uid;
 
 #if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS
 	applet_long_options = adduser_longopts;
@@ -164,16 +163,10 @@ int adduser_main(int argc UNUSED_PARAM,
 
 	/* at least one and at most two non-option args */
 	/* disable interactive passwd for system accounts */
-	opt_complementary = "-1:?2:SD:u+";
-	if (sizeof(pw.pw_uid) == sizeof(int)) {
-		opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &pw.pw_uid);
-	} else {
-		unsigned uid;
-		opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &uid);
-		if (opts & OPT_UID) {
-			pw.pw_uid = uid;
-		}
-	}
+	opt_complementary = "-1:?2:SD:u";
+	opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &uid);
+	pw.pw_uid = xatou_range(uid, 0, CONFIG_LAST_ID);
+
 	argv += optind;
 	pw.pw_name = argv[0];
 
--- loginutils/addgroup.c.orig	2013-12-21 19:08:38.000000000 +0100
+++ loginutils/addgroup.c	2013-12-21 21:19:30.810290053 +0100
@@ -26,10 +26,9 @@
 #define OPT_GID                       (1 << 0)
 #define OPT_SYSTEM_ACCOUNT            (1 << 1)
 
-/* We assume GID_T_MAX == INT_MAX */
 static void xgroup_study(struct group *g)
 {
-	unsigned max = INT_MAX;
+	unsigned max = CONFIG_LAST_ID;
 
 	/* Make sure gr_name is unused */
 	if (getgrnam(g->gr_name)) {
@@ -125,7 +124,7 @@ int addgroup_main(int argc, char **argv)
 int addgroup_main(int argc UNUSED_PARAM, char **argv)
 {
 	unsigned opts;
-	unsigned gid = 0;
+	char *gid;
 
 	/* need to be root */
 	if (geteuid()) {
@@ -139,7 +138,7 @@ int addgroup_main(int argc UNUSED_PARAM,
 	 *  addgroup -g num group
 	 *  addgroup user group
 	 * Check for min, max and missing args */
-	opt_complementary = "-1:?2:g+";
+	opt_complementary = "-1:?2:g";
 	opts = getopt32(argv, "g:S", &gid);
 	/* move past the commandline options */
 	argv += optind;
@@ -175,7 +174,7 @@ int addgroup_main(int argc UNUSED_PARAM,
 #endif /* ENABLE_FEATURE_ADDUSER_TO_GROUP */
 	{
 		die_if_bad_username(argv[0]);
-		new_group(argv[0], gid);
+		new_group(argv[0], xatou_range(gid, 0, CONFIG_LAST_ID));
 	}
 	/* Reached only on success */
 	return EXIT_SUCCESS;
--- loginutils/Config.src.orig	2013-06-02 13:56:34.000000000 +0200
+++ loginutils/Config.src	2013-12-21 21:12:32.462974508 +0100
@@ -121,7 +121,7 @@ config FEATURE_CHECK_NAMES
 config FIRST_SYSTEM_ID
 	int "First valid system uid or gid for adduser and addgroup"
 	depends on ADDUSER || ADDGROUP
-	range 0 64900
+	range 100 999
 	default 100
 	help
 	  First valid system uid or gid for adduser and addgroup
@@ -129,11 +129,19 @@ config FIRST_SYSTEM_ID
 config LAST_SYSTEM_ID
 	int "Last valid system uid or gid for adduser and addgroup"
 	depends on ADDUSER || ADDGROUP
-	range 0 64900
+	range 100 999
 	default 999
 	help
 	  Last valid system uid or gid for adduser and addgroup
 
+config LAST_ID
+	int "Last valid uid or gid for adduser and addgroup"
+	depends on ADDUSER || ADDGROUP
+	range 1000 60000
+	default 60000
+	help
+	  Last valid uid or gid for adduser and addgroup
+
 config ADDGROUP
 	bool "addgroup"
 	default y




-------------- next part --------------
A non-text attachment was scrubbed...
Name: adduser3.patch
Type: text/x-patch
Size: 3993 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20131221/340e8713/attachment.bin>


More information about the busybox mailing list