[ PATCH] was Re: How to add an existing user to an existing group

Tito farmatito at tiscali.it
Tue Sep 18 13:15:21 UTC 2012


On Tuesday 18 September 2012 14:27:16 Tito wrote:
> On Tuesday 18 September 2012 09:24:58 Jan Pohanka wrote:
> > Hello,
> > 
> > I can't found proper command to add an existing user to an existing group.  
> > I have tried following:
> > 
> > user daemon exists as well as group mygroup
> > 
> > # adduser -D -H -G mygroup daemon
> > adduser: user 'daemon' in use
> > 
> > Tested on Busybox v1.20.0
> > 
> > regards
> > Jan
> > 
> > 
> 
> Hi,
> the command is:
> 
> addgroup user group
> 
> You need to enable:
> config FEATURE_ADDUSER_TO_GROUP
> 	bool "Support for adding users to groups"
> 	default y
> 	depends on ADDGROUP
> 	help
> 	  If  called  with two non-option arguments,
> 	  addgroup will add an existing user to an
> 	  existing group.
> 
> This seems to be somewhat non standard, but at least on debian systems
> where addgroup is a link to adduser it works:
> 
> addgroup prova
> Adding group `prova' (GID 1005) ...
> Done.
> debian:/home/tito# addgroup tito prova
> Adding user `tito' to group `prova' ...
> Adding user tito to group prova
> Done.
> debian:/home/tito# adduser tito prova
> The user `tito' is already a member of `prova'.
> debian:/home/tito# which adduser
> /usr/sbin/adduser
> debian:/home/tito# ls -la /usr/sbin/addgroup
> lrwxrwxrwx 1 root root 7 Jul 30  2011 /usr/sbin/addgroup -> adduser
> debian:/home/tito# 
> 
> In busybox it was implemented this way because
> addgroup has already the needed functions to parse the group files.
> Eventually if there is a strong request adduser could be
> modified to run:
> 
> static void addgroup_wrapper(struct passwd *p, const char *group_name)
> 
> when called with 2 non-option arguments as it already does when creating
> a new user that needs to be added to an existing group.
> 
> Ciao,
> Tito

Hi,
attached you will find a patch that allows the adduser applet to add an existing
user to an existing group if called with two non-option arguments.
That way this task can be accomplished in busybox with:

adduser user group
addgroup user group

According to the man page:

ADDUSER(8)                                                                                ADDUSER(8)

NAME
       adduser, addgroup - add a user or group to the system


to correct way seems to be:

adduser user group

nonetheless on debian: 

addgroup user group

also works as addgroup is a link to adduser.
The patch was not tested extensively and as this is sensitive stuff needs more
care and review form the list members.
Eventually a formally more correct approach could be to remove
the "adding (existing) user to existing group"  functionality from addgroup
and move it to adduser.

Ciao,
Tito

Allow adduser user group

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

--- loginutils/adduser.c.original	2012-07-31 21:23:24.000000000 +0200
+++ loginutils/adduser.c	2012-09-18 14:54:07.000000000 +0200
@@ -9,9 +9,9 @@
  */
 
 //usage:#define adduser_trivial_usage
-//usage:       "[OPTIONS] USER"
+//usage:       "[OPTIONS] USER or USER GROUP"
 //usage:#define adduser_full_usage "\n\n"
-//usage:       "Add a user\n"
+//usage:       "Add a user or add a existing user to a existing group\n"
 //usage:     "\n	-h DIR		Home directory"
 //usage:     "\n	-g GECOS	GECOS field"
 //usage:     "\n	-s SHELL	Login shell"
@@ -80,7 +80,7 @@
 	}
 }
 
-static void addgroup_wrapper(struct passwd *p, const char *group_name)
+static int addgroup_wrapper(struct passwd *p, const char *group_name)
 {
 	char *argv[6];
 
@@ -110,7 +110,7 @@
 		argv[5] = NULL;
 	}
 
-	spawn_and_wait(argv);
+	return spawn_and_wait(argv);
 }
 
 static void passwd_wrapper(const char *login_name) NORETURN;
@@ -162,9 +162,9 @@
 	pw.pw_shell = (char *)get_shell_name();
 	pw.pw_dir = NULL;
 
-	/* exactly one non-option arg */
+	/* at most two non-option args */
 	/* disable interactive passwd for system accounts */
-	opt_complementary = "=1:SD:u+";
+	opt_complementary = "?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 {
@@ -176,6 +176,13 @@
 	}
 	argv += optind;
 
+	if (!opts && argv[1]) {
+		/*If called with two non-option arguments, adduser
+		  will add an existing  user  to  an  existing group. */
+		pw.pw_name = argv[0];
+		return addgroup_wrapper(&pw, argv[1]);
+	}
+
 	/* fill in the passwd struct */
 	pw.pw_name = argv[0];
 	die_if_bad_username(pw.pw_name);

-------------- next part --------------
A non-text attachment was scrubbed...
Name: adduser.patch
Type: text/x-patch
Size: 1766 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20120918/7f796300/attachment.bin>


More information about the busybox mailing list