return gr;
}
-void gr_free (/*@out@*/ /*@only@*/struct group *grent)
+void gr_free_members (struct group *grent)
{
- free (grent->gr_name);
- if (NULL != grent->gr_passwd) {
- memzero (grent->gr_passwd, strlen (grent->gr_passwd));
- free (grent->gr_passwd);
- }
if (NULL != grent->gr_mem) {
size_t i;
for (i = 0; NULL != grent->gr_mem[i]; i++) {
free (grent->gr_mem[i]);
}
free (grent->gr_mem);
+ grent->gr_mem = NULL;
}
+}
+
+void gr_free (/*@out@*/ /*@only@*/struct group *grent)
+{
+ free (grent->gr_name);
+ if (NULL != grent->gr_passwd) {
+ memzero (grent->gr_passwd, strlen (grent->gr_passwd));
+ free (grent->gr_passwd);
+ }
+ gr_free_members(grent);
free (grent);
}
+bool gr_append_member(struct group *grp, char *member)
+{
+ int i;
+
+ if (NULL == grp->gr_mem || grp->gr_mem[0] == NULL) {
+ grp->gr_mem = (char **)malloc(2 * sizeof(char *));
+ if (!grp->gr_mem) {
+ return false;
+ }
+ grp->gr_mem[0] = strdup(member);
+ if (!grp->gr_mem[0]) {
+ return false;
+ }
+ grp->gr_mem[1] = NULL;
+ return true;
+ }
+
+ for (i = 0; grp->gr_mem[i]; i++) ;
+ grp->gr_mem = realloc(grp->gr_mem, (i + 2) * sizeof(char *));
+ if (NULL == grp->gr_mem) {
+ return false;
+ }
+ grp->gr_mem[i] = strdup(member);
+ if (NULL == grp->gr_mem[i]) {
+ return false;
+ }
+ grp->gr_mem[i + 1] = NULL;
+ return true;
+}
/* groupmem.c */
extern /*@null@*/ /*@only@*/struct group *__gr_dup (const struct group *grent);
+extern void gr_free_members (struct group *grent);
extern void gr_free (/*@out@*/ /*@only@*/struct group *grent);
+extern bool gr_append_member (struct group *grp, char *member);
/* hushed.c */
extern bool hushed (const char *username);
</para>
</listitem>
</varlistentry>
+ <varlistentry>
+ <term>
+ <option>-U</option>, <option>--users</option>
+ </term>
+ <listitem>
+ <para>
+ A list of usernames to add as members of the group.
+ </para>
+ <para>
+ The default behavior (if the <option>-g</option>,
+ <option>-N</option>, and <option>-U</option> options are not
+ specified) is defined by the <option>USERGROUPS_ENAB</option>
+ variable in <filename>/etc/login.defs</filename>.
+ </para>
+ </listitem>
+ </varlistentry>
</variablelist>
</refsect1>
</para>
<variablelist remap='IP'>
<varlistentry>
+ <term>
+ <option>-a</option>, <option>--append</option> <replaceable>GID</replaceable>
+ </term>
+ <listitem>
+ <para>If group members are specified with -U, append them to the existing
+ member list, rather than replacing it.</para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
<term>
<option>-g</option>, <option>--gid</option> <replaceable>GID</replaceable>
</term>
</para>
</listitem>
</varlistentry>
+ <varlistentry>
+ <term>
+ <option>-U</option>, <option>--users</option>
+ </term>
+ <listitem>
+ <para>
+ A list of usernames to add as members of the group.
+ </para>
+ <para>
+ The default behavior (if the <option>-g</option>,
+ <option>-N</option>, and <option>-U</option> options are not
+ specified) is defined by the <option>USERGROUPS_ENAB</option>
+ variable in <filename>/etc/login.defs</filename>.
+ </para>
+ </listitem>
+ </varlistentry>
</variablelist>
</refsect1>
static /*@null@*/char *empty_list = NULL;
static const char *prefix = "";
+static char *user_list;
static bool oflg = false; /* permit non-unique group ID to be specified with -g */
static bool gflg = false; /* ID value for the new group */
(void) fputs (_(" -p, --password PASSWORD use this encrypted password for the new group\n"), usageout);
(void) fputs (_(" -r, --system create a system account\n"), usageout);
(void) fputs (_(" -R, --root CHROOT_DIR directory to chroot into\n"), usageout);
- (void) fputs (_(" -P, --prefix PREFIX_DIR directory prefix\n"), usageout);
+ (void) fputs (_(" -P, --prefix PREFIX_DI directory prefix\n"), usageout);
+ (void) fputs (_(" -U, --users USERS list of user members of this group\n"), usageout);
(void) fputs ("\n", usageout);
exit (status);
}
}
#endif /* SHADOWGRP */
+ if (user_list) {
+ char *token;
+ token = strtok(user_list, ",");
+ while (token) {
+ if (prefix_getpwnam (token) == NULL) {
+ fprintf (stderr, _("Invalid member username %s\n"), token);
+ exit (E_GRP_UPDATE);
+ }
+ grp.gr_mem = add_list(grp.gr_mem, token);
+ token = strtok(NULL, ",");
+ }
+ }
+
/*
* Write out the new group file entry.
*/
{"system", no_argument, NULL, 'r'},
{"root", required_argument, NULL, 'R'},
{"prefix", required_argument, NULL, 'P'},
+ {"users", required_argument, NULL, 'U'},
{NULL, 0, NULL, '\0'}
};
- while ((c = getopt_long (argc, argv, "fg:hK:op:rR:P:",
+ while ((c = getopt_long (argc, argv, "fg:hK:op:rR:P:U:",
long_options, NULL)) != -1) {
switch (c) {
case 'f':
break;
case 'P': /* no-op, handled in process_prefix_flag () */
break;
+ case 'U':
+ user_list = optarg;
+ break;
default:
usage (E_USAGE);
}
static gid_t group_newid;
static const char* prefix = "";
+static char *user_list;
static struct cleanup_info_mod info_passwd;
static struct cleanup_info_mod info_group;
#endif
static bool
+ aflg = false, /* append -U members rather than replace them */
oflg = false, /* permit non-unique group ID to be specified with -g */
gflg = false, /* new ID value for the group */
nflg = false, /* a new name has been specified for the group */
static void close_files (void);
static void update_primary_groups (gid_t ogid, gid_t ngid);
+
/*
* usage - display usage message and exit
*/
"\n"
"Options:\n"),
Prog);
+ (void) fputs (_(" -a, --append append the users mentioned by -U option to the group \n"
+ " without removing existing user members\n"), usageout);
(void) fputs (_(" -g, --gid GID change the group ID to GID\n"), usageout);
(void) fputs (_(" -h, --help display this help message and exit\n"), usageout);
(void) fputs (_(" -n, --new-name NEW_GROUP change the name to NEW_GROUP\n"), usageout);
" PASSWORD\n"), usageout);
(void) fputs (_(" -R, --root CHROOT_DIR directory to chroot into\n"), usageout);
(void) fputs (_(" -P, --prefix PREFIX_DIR prefix directory where are located the /etc/* files\n"), usageout);
+ (void) fputs (_(" -U, --users USERS list of user members of this group\n"), usageout);
(void) fputs ("\n", usageout);
exit (status);
}
update_primary_groups (ogrp->gr_gid, group_newid);
}
+ if (user_list) {
+ char *token;
+
+ if (!aflg) {
+ // requested to replace the existing groups
+ if (NULL != grp.gr_mem[0])
+ gr_free_members(&grp);
+ grp.gr_mem = (char **)xmalloc(sizeof(char *));
+ grp.gr_mem[0] = (char *)0;
+ } else {
+ // append to existing groups
+ if (NULL != grp.gr_mem[0])
+ grp.gr_mem = dup_list (grp.gr_mem);
+ }
+
+ token = strtok(user_list, ",");
+ while (token) {
+ if (prefix_getpwnam (token) == NULL) {
+ fprintf (stderr, _("Invalid member username %s\n"), token);
+ exit (E_GRP_UPDATE);
+ }
+ grp.gr_mem = add_list(grp.gr_mem, token);
+ token = strtok(NULL, ",");
+ }
+ }
+
/*
* Write out the new group file entry.
*/
{
int c;
static struct option long_options[] = {
+ {"append", no_argument, NULL, 'a'},
{"gid", required_argument, NULL, 'g'},
{"help", no_argument, NULL, 'h'},
{"new-name", required_argument, NULL, 'n'},
{"password", required_argument, NULL, 'p'},
{"root", required_argument, NULL, 'R'},
{"prefix", required_argument, NULL, 'P'},
+ {"users", required_argument, NULL, 'U'},
{NULL, 0, NULL, '\0'}
};
- while ((c = getopt_long (argc, argv, "g:hn:op:R:P:",
+ while ((c = getopt_long (argc, argv, "ag:hn:op:R:P:U:",
long_options, NULL)) != -1) {
switch (c) {
+ case 'a':
+ aflg = true;
+ break;
case 'g':
gflg = true;
if ( (get_gid (optarg, &group_newid) == 0)
break;
case 'P': /* no-op, handled in process_prefix_flag () */
break;
+ case 'U':
+ user_list = optarg;
+ break;
default:
usage (E_USAGE);
}