[BusyBox-cvs] busybox/loginutils adduser.c,1.6,1.7

Erik Andersen andersen at busybox.net
Sat Jun 21 20:03:11 UTC 2003


Update of /var/cvs/busybox/loginutils
In directory winder:/tmp/cvs-serv31147/loginutils

Modified Files:
	adduser.c 
Log Message:
Based on a tinylogin patch from Philip Blundell, add several
additional options to adduser.
 -Erik


Index: adduser.c
===================================================================
RCS file: /var/cvs/busybox/loginutils/adduser.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- adduser.c	20 Jun 2003 09:01:57 -0000	1.6
+++ adduser.c	21 Jun 2003 20:03:07 -0000	1.7
@@ -21,6 +21,9 @@
  *
  */
 
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
 #include <errno.h>
 #include <fcntl.h>
 #include <stdarg.h>
@@ -29,6 +32,7 @@
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
+#include <getopt.h>
 #include <sys/param.h>
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -89,21 +93,23 @@
 		}
 	}
 
-	/* EDR check for an already existing gid */
-	while (getgrgid(p->pw_uid) != NULL)
-		p->pw_uid++;
+	if (p->pw_gid == 0) {
+		/* EDR check for an already existing gid */
+		while (getgrgid(p->pw_uid) != NULL)
+			p->pw_uid++;
 
-	/* EDR also check for an existing group definition */
-	if (getgrnam(p->pw_name) != NULL)
-		return 3;
+		/* EDR also check for an existing group definition */
+		if (getgrnam(p->pw_name) != NULL)
+			return 3;
+
+		/* EDR create new gid always = uid */
+		p->pw_gid = p->pw_uid;
+	}
 
 	/* EDR bounds check */
 	if ((p->pw_uid > max) || (p->pw_uid < min))
 		return 2;
 
-	/* EDR create new gid always = uid */
-	p->pw_gid = p->pw_uid;
-
 	/* return 1; */
 	return 0;
 }
@@ -127,7 +133,7 @@
 }
 
 /* putpwent(3) remix */
-static int adduser(const char *filename, struct passwd *p)
+static int adduser(const char *filename, struct passwd *p, int makehome, int setpass)
 {
 	FILE *passwd;
 	int r;
@@ -135,6 +141,11 @@
 	FILE *shadow;
 	struct spwd *sp;
 #endif
+	int new_group = 1;
+
+	/* if using a pre-existing group, don't create one */
+	if (p->pw_gid != 0)
+		new_group = 0;
 
 	/* make sure everything is kosher and setup uid && gid */
 	passwd = bb_wfopen(filename, "a");
@@ -181,29 +192,38 @@
 	}
 #endif
 
-	/* add to group */
-	/* addgroup should be responsible for dealing w/ gshadow */
-	addgroup_wrapper(p->pw_name, p->pw_gid);
+	if (new_group) {
+		/* add to group */
+		/* addgroup should be responsible for dealing w/ gshadow */
+		addgroup_wrapper(p->pw_name, p->pw_gid);
+	}
 
 	/* Clear the umask for this process so it doesn't
 	 * * screw up the permissions on the mkdir and chown. */
 	umask(0);
 
-	/* mkdir */
-	if (mkdir(p->pw_dir, 0755)) {
-		bb_perror_msg("%s", p->pw_dir);
-	}
-	/* Set the owner and group so it is owned by the new user. */
-	if (chown(p->pw_dir, p->pw_uid, p->pw_gid)) {
-		bb_perror_msg("%s", p->pw_dir);
+	if (makehome) {
+		/* mkdir */
+		if (mkdir(p->pw_dir, 0755)) {
+			bb_perror_msg("%s", p->pw_dir);
+		}
+		/* Set the owner and group so it is owned by the new user. */
+		if (chown(p->pw_dir, p->pw_uid, p->pw_gid)) {
+			bb_perror_msg("%s", p->pw_dir);
+		}
+		/* Now fix up the permissions to 2755. Can't do it before now
+		 * since chown will clear the setgid bit */
+		if (chmod(p->pw_dir, 02755)) {
+			bb_perror_msg("%s", p->pw_dir);
+		}
 	}
-	/* Now fix up the permissions to 2755. Can't do it before now
-	 * since chown will clear the setgid bit */
-	if (chmod(p->pw_dir, 02755)) {
-		bb_perror_msg("%s", p->pw_dir);
+
+	if (setpass) {
+		/* interactively set passwd */
+		passwd_wrapper(p->pw_name);
 	}
-	/* interactively set passwd */
-	passwd_wrapper(p->pw_name);
+
+	return 0;
 }
 
 
@@ -219,6 +239,9 @@
 	}
 }
 
+#define SETPASS				1
+#define MAKEHOME			4
+
 /*
  * adduser will take a login_name as its first parameter.
  *
@@ -230,19 +253,29 @@
  * ________________________________________________________________________ */
 int adduser_main(int argc, char **argv)
 {
+	struct passwd pw;
 	const char *login;
 	const char *gecos = default_gecos;
 	const char *home = NULL;
 	const char *shell = default_shell;
-
-	struct passwd pw;
+ 	const char *usegroup = NULL;
+	int flags;
+	int setpass = 1;
+	int makehome = 1;
 
 	/* init */
 	if (argc < 2) {
 		bb_show_usage();
 	}
 	/* get args */
-	bb_getopt_ulflags(argc, argv, "h:g:s:", &home, &gecos, &shell);
+	flags = bb_getopt_ulflags(argc, argv, "h:g:s:G:DSH", &home, &gecos, &shell, &usegroup);
+
+	if (flags & SETPASS) {
+		setpass = 0;
+	}
+	if (flags & MAKEHOME) {
+		makehome = 0;
+	}
 
 	/* got root? */
 	if_i_am_not_root();
@@ -271,6 +304,17 @@
 	pw.pw_dir = (char *)home;
 	pw.pw_shell = (char *)shell;
 
+	if (usegroup) {
+		/* Add user to a group that already exists */
+		struct group *g;
+
+		g = getgrnam(usegroup);
+		if (g == NULL)
+			bb_error_msg_and_die("group %s does not exist", usegroup);
+
+		pw.pw_gid = g->gr_gid;
+	}
+
 	/* grand finale */
-	return adduser(bb_path_passwd_file, &pw);
+	return adduser(bb_path_passwd_file, &pw, makehome, setpass);
 }



More information about the busybox-cvs mailing list