[TinyLogin] [PATCH] -u for adduser

Justin Cormack justin at street-vision.com
Mon Nov 17 09:48:32 MST 2003


I needed a -u option on adduser to specify the uid (like current shadow
utils seems to at least in some versions). It involved a few changes so
if you specify a number it will not be changed if it is not available. I
think the patch is correct.



--- adduser-orig.c	2003-11-17 15:21:45.000000000 +0000
+++ adduser.c	2003-11-17 16:32:58.000000000 +0000
@@ -62,21 +62,26 @@
 
 /* remix */
 /* EDR recoded such that the uid may be passed in *p */
+/* JPC recoded so that it is passed in *p */
 static int passwd_study(const char *filename, struct passwd *p)
 {
 	struct passwd *pw;
 	FILE *passwd;
 
-	const int min = 500;
+	const int deflt = 500;
 	const int max = 65000;
+        int unspecified = (p->pw_uid == 0);
+
+	if (unspecified)
+	        p->pw_uid = deflt;
 
 	passwd = wfopen(filename, "r");
 	if (!passwd)
 		return 4;
 
-	/* EDR if uid is out of bounds, set to min */
-	if ((p->pw_uid > max) || (p->pw_uid < min))
-		p->pw_uid = min;
+	/* JPC if uid is out of bounds, return error */
+	if ((p->pw_uid < 0) || (p->pw_uid > max))
+	        return 2;
 
 	/* stuff to do:  
 	 * make sure login isn't taken;
@@ -84,31 +89,33 @@
 	 */
 	while ((pw = fgetpwent(passwd))) {
 		if (strcmp(pw->pw_name, p->pw_name) == 0) {
-			/* return 0; */
 			return 1;
 		}
-		if ((pw->pw_uid >= p->pw_uid) && (pw->pw_uid < max)
-			&& (pw->pw_uid >= min)) {
+		if (unspecified &&
+		    (pw->pw_uid >= p->pw_uid) && (pw->pw_uid < max)) {
 			p->pw_uid = pw->pw_uid + 1;
 		}
+		/* JPC check for existing group */
+		while (unspecified && getgrgid(p->pw_uid) != NULL &&
+		       (pw->pw_uid < max))
+		        p->pw_uid++;
 	}
 
-	/* EDR check for an already existing gid */
-	while (getgrgid(p->pw_uid) != NULL)
-		p->pw_uid++;
+	/* JPC check for an already existing gid */
+	if (getgrgid(p->pw_uid) != NULL)
+	        return 3;
 
 	/* EDR also check for an existing group definition */
 	if (getgrnam(p->pw_name) != NULL)
 		return 3;
 
 	/* EDR bounds check */
-	if ((p->pw_uid > max) || (p->pw_uid < min))
+	if (p->pw_uid > max)
 		return 2;
 
 	/* EDR create new gid always = uid */
 	p->pw_gid = p->pw_uid;
 
-	/* return 1; */
 	return 0;
 }
 
@@ -244,6 +251,7 @@
 	const char *gecos;
 	const char *home = NULL;
 	const char *shell;
+       int uid = 0;
 
 	struct passwd pw;
 
@@ -255,7 +263,7 @@
 	shell = default_shell;
 
 	/* get args */
-	while ((opt = getopt (argc, argv, "h:g:s:")) != -1) {
+	while ((opt = getopt (argc, argv, "h:g:s:u:")) != -1) {
 		switch (opt) {
 			case 'h':
 				home = optarg;
@@ -266,6 +274,9 @@
 			case 's':
 				shell = optarg;
 				break;
+		        case 'u':
+			        uid = atoi(optarg);
+				break;
 			default:
 				show_usage ();
 				break;
@@ -295,8 +306,8 @@
 	/* create a passwd struct */
 	pw.pw_name = (char *)login;
 	pw.pw_passwd = (char *)default_passwd;
-	pw.pw_uid = 0;
-	pw.pw_gid = 0;
+	pw.pw_uid = uid;
+	pw.pw_gid = uid;
 	pw.pw_gecos = (char *)gecos;
 	pw.pw_dir = (char *)home;
 	pw.pw_shell = (char *)shell;



More information about the tinylogin mailing list