2 * Copyright (c) 2000 , International Business Machines
3 * George Kraft IV, gk4@us.ibm.com, 03/23/2000
4 * Copyright (c) 2000 - 2006, Tomasz Kłoczko
5 * Copyright (c) 2007 - 2008, Nicolas François
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the copyright holders or contributors may not be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/types.h>
45 #include "prototypes.h"
51 /* Exit Status Values */
53 #define EXIT_SUCCESS 0 /* success */
54 #define EXIT_USAGE 1 /* invalid command syntax */
55 #define EXIT_GROUP_FILE 2 /* group file access problems */
56 #define EXIT_NOT_ROOT 3 /* not superuser */
57 #define EXIT_NOT_EROOT 4 /* not effective superuser */
58 #define EXIT_NOT_PRIMARY 5 /* not primary owner of group */
59 #define EXIT_NOT_MEMBER 6 /* member of group does not exist */
60 #define EXIT_MEMBER_EXISTS 7 /* member of group already exists */
61 #define EXIT_INVALID_USER 8 /* specified user does not exist */
62 #define EXIT_INVALID_GROUP 9 /* specified group does not exist */
69 static char *adduser = NULL;
70 static char *deluser = NULL;
71 static char *thisgroup = NULL;
72 static bool purge = false;
73 static bool list = false;
74 static int exclusive = 0;
75 static bool gr_locked = false;
77 /* Indicate if shadow groups are enabled on the system
78 * (/etc/gshadow present) */
79 static bool is_shadowgrp;
80 static bool sgr_locked = false;
83 /* local function prototypes */
84 static char *whoami (void);
85 static void add_user (const char *user,
86 const struct group *grp);
87 static void remove_user (const char *user,
88 const struct group *grp);
89 static void purge_members (const struct group *grp);
90 static void display_members (const char *const *members);
91 static void usage (void);
92 static void process_flags (int argc, char **argv);
93 static void check_perms (void);
94 static void fail_exit (int code);
95 #define isroot() (getuid () == 0)
97 static char *whoami (void)
99 /* local, no need for xgetgrgid */
100 struct group *grp = getgrgid (getgid ());
101 /* local, no need for xgetpwuid */
102 struct passwd *usr = getpwuid (getuid ());
106 && (0 == strcmp (usr->pw_name, grp->gr_name))) {
107 return xstrdup (usr->pw_name);
114 * add_user - Add an user to the specified group
116 static void add_user (const char *user,
117 const struct group *grp)
119 struct group *newgrp;
121 /* Make sure the user is not already part of the group */
122 if (is_on_list (grp->gr_mem, user)) {
124 _("%s: user '%s' is already a member of '%s'\n"),
125 Prog, user, grp->gr_name);
126 fail_exit (EXIT_MEMBER_EXISTS);
129 newgrp = __gr_dup(grp);
130 if (NULL == newgrp) {
132 _("%s: Out of memory. Cannot update %s.\n"),
137 /* Add the user to the /etc/group group */
138 newgrp->gr_mem = add_list (newgrp->gr_mem, user);
142 const struct sgrp *sg = sgr_locate (newgrp->gr_name);
146 /* Create a shadow group based on this group */
147 static struct sgrp sgrent;
148 sgrent.sg_name = xstrdup (newgrp->gr_name);
149 sgrent.sg_mem = dup_list (newgrp->gr_mem);
150 sgrent.sg_adm = (char **) xmalloc (sizeof (char *));
151 #ifdef FIRST_MEMBER_IS_ADMIN
152 if (sgrent.sg_mem[0]) {
153 sgrent.sg_adm[0] = xstrdup (sgrent.sg_mem[0]);
154 sgrent.sg_adm[1] = NULL;
158 sgrent.sg_adm[0] = NULL;
161 /* Move any password to gshadow */
162 sgrent.sg_passwd = newgrp->gr_passwd;
163 newgrp->gr_passwd = SHADOW_PASSWD_STRING;
167 newsg = __sgr_dup (sg);
170 _("%s: Out of memory. Cannot update %s.\n"),
171 Prog, sgr_dbname ());
174 /* Add the user to the members */
175 newsg->sg_mem = add_list (newsg->sg_mem, user);
176 /* Do not touch the administrators */
179 if (sgr_update (newsg) == 0) {
181 _("%s: failed to prepare the new %s entry '%s'\n"),
182 Prog, sgr_dbname (), newsg->sg_name);
188 if (gr_update (newgrp) == 0) {
190 _("%s: failed to prepare the new %s entry '%s'\n"),
191 Prog, gr_dbname (), newgrp->gr_name);
197 * remove_user - Remove an user from a given group
199 static void remove_user (const char *user,
200 const struct group *grp)
202 struct group *newgrp;
204 /* Check if the user is a member of the specified group */
205 if (!is_on_list (grp->gr_mem, user)) {
207 _("%s: user '%s' is not a member of '%s'\n"),
208 Prog, user, grp->gr_name);
209 fail_exit (EXIT_NOT_MEMBER);
212 newgrp = __gr_dup (grp);
213 if (NULL == newgrp) {
215 _("%s: Out of memory. Cannot update %s.\n"),
220 /* Remove the user from the /etc/group group */
221 newgrp->gr_mem = del_list (newgrp->gr_mem, user);
225 const struct sgrp *sg = sgr_locate (newgrp->gr_name);
229 /* Create a shadow group based on this group */
230 static struct sgrp sgrent;
231 sgrent.sg_name = xstrdup (newgrp->gr_name);
232 sgrent.sg_mem = dup_list (newgrp->gr_mem);
233 sgrent.sg_adm = (char **) xmalloc (sizeof (char *));
234 #ifdef FIRST_MEMBER_IS_ADMIN
235 if (sgrent.sg_mem[0]) {
236 sgrent.sg_adm[0] = xstrdup (sgrent.sg_mem[0]);
237 sgrent.sg_adm[1] = NULL;
241 sgrent.sg_adm[0] = NULL;
244 /* Move any password to gshadow */
245 sgrent.sg_passwd = newgrp->gr_passwd;
246 newgrp->gr_passwd = SHADOW_PASSWD_STRING;
250 newsg = __sgr_dup (sg);
253 _("%s: Out of memory. Cannot update %s.\n"),
254 Prog, sgr_dbname ());
257 /* Remove the user from the members */
258 newsg->sg_mem = del_list (newsg->sg_mem, user);
259 /* Remove the user from the administrators */
260 newsg->sg_adm = del_list (newsg->sg_adm, user);
263 if (sgr_update (newsg) == 0) {
265 _("%s: failed to prepare the new %s entry '%s'\n"),
266 Prog, sgr_dbname (), newsg->sg_name);
272 if (gr_update (newgrp) == 0) {
274 _("%s: failed to prepare the new %s entry '%s'\n"),
275 Prog, gr_dbname (), newgrp->gr_name);
281 * purge_members - Rmeove every members of the specified group
283 static void purge_members (const struct group *grp)
285 struct group *newgrp = __gr_dup (grp);
287 if (NULL == newgrp) {
289 _("%s: Out of memory. Cannot update %s.\n"),
294 /* Remove all the members of the /etc/group group */
295 newgrp->gr_mem[0] = NULL;
299 const struct sgrp *sg = sgr_locate (newgrp->gr_name);
303 /* Create a shadow group based on this group */
304 static struct sgrp sgrent;
305 sgrent.sg_name = xstrdup (newgrp->gr_name);
306 sgrent.sg_mem = (char **) xmalloc (sizeof (char *));
307 sgrent.sg_mem[0] = NULL;
308 sgrent.sg_adm = (char **) xmalloc (sizeof (char *));
309 sgrent.sg_adm[0] = NULL;
311 /* Move any password to gshadow */
312 sgrent.sg_passwd = newgrp->gr_passwd;
313 newgrp->gr_passwd = xstrdup(SHADOW_PASSWD_STRING);
317 newsg = __sgr_dup (sg);
320 _("%s: Out of memory. Cannot update %s.\n"),
321 Prog, sgr_dbname ());
324 /* Remove all the members of the /etc/gshadow
326 newsg->sg_mem[0] = NULL;
327 /* Remove all the administrators of the
328 * /etc/gshadow group */
329 newsg->sg_adm[0] = NULL;
332 if (sgr_update (newsg) == 0) {
334 _("%s: failed to prepare the new %s entry '%s'\n"),
335 Prog, sgr_dbname (), newsg->sg_name);
341 if (gr_update (newgrp) == 0) {
343 _("%s: failed to prepare the new %s entry '%s'\n"),
344 Prog, gr_dbname (), newgrp->gr_name);
349 static void display_members (const char *const *members)
353 for (i = 0; NULL != members[i]; i++) {
354 printf ("%s ", members[i]);
356 if (NULL == members[i + 1]) {
364 static void usage (void)
366 (void) fputs (_("Usage: groupmems [options] [action]\n"
369 " -g, --group groupname change groupname instead of the user's group\n"
373 " -a, --add username add username to the members of the group\n"
374 " -d, --delete username remove username from the members of the group\n"
375 " -p, --purge purge all members from the group\n"
376 " -l, --list list the members of the group\n"
378 fail_exit (EXIT_USAGE);
382 * process_flags - perform command line argument setting
384 static void process_flags (int argc, char **argv)
387 int option_index = 0;
388 static struct option long_options[] = {
389 {"add", required_argument, NULL, 'a'},
390 {"delete", required_argument, NULL, 'd'},
391 {"group", required_argument, NULL, 'g'},
392 {"list", no_argument, NULL, 'l'},
393 {"purge", no_argument, NULL, 'p'},
394 {NULL, 0, NULL, '\0'}
397 while ((arg = getopt_long (argc, argv, "a:d:g:lp", long_options,
398 &option_index)) != EOF) {
401 adduser = xstrdup (optarg);
405 deluser = xstrdup (optarg);
409 thisgroup = xstrdup (optarg);
424 if ((exclusive > 1) || (optind < argc)) {
428 /* local, no need for xgetpwnam */
429 if ( (NULL != adduser)
430 && (getpwnam (adduser) == NULL)) {
431 fprintf (stderr, _("%s: user '%s' does not exist\n"),
433 fail_exit (EXIT_INVALID_USER);
438 static void check_perms (void)
442 pam_handle_t *pamh = NULL;
444 struct passwd *pampw;
446 pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
449 _("%s: Cannot determine your user name.\n"),
454 retval = pam_start ("groupmems", pampw->pw_name, &conv, &pamh);
456 if (PAM_SUCCESS == retval) {
457 retval = pam_authenticate (pamh, 0);
460 if (PAM_SUCCESS == retval) {
461 retval = pam_acct_mgmt (pamh, 0);
465 (void) pam_end (pamh, retval);
467 if (PAM_SUCCESS != retval) {
468 fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
475 static void fail_exit (int code)
478 if (gr_unlock () == 0) {
480 _("%s: failed to unlock %s\n"),
482 SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
489 if (sgr_unlock () == 0) {
491 _("%s: failed to unlock %s\n"),
492 Prog, sgr_dbname ());
493 SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
502 static void open_files (void)
505 if (gr_lock () == 0) {
507 _("%s: cannot lock %s; try again later.\n"),
509 fail_exit (EXIT_GROUP_FILE);
515 if (sgr_lock () == 0) {
517 _("%s: cannot lock %s; try again later.\n"),
518 Prog, sgr_dbname ());
519 fail_exit (EXIT_GROUP_FILE);
526 if (gr_open (list ? O_RDONLY : O_RDWR) == 0) {
527 fprintf (stderr, _("%s: cannot open %s\n"), Prog, gr_dbname ());
528 fail_exit (EXIT_GROUP_FILE);
533 if (sgr_open (list ? O_RDONLY : O_RDWR) == 0) {
534 fprintf (stderr, _("%s: cannot open %s\n"), Prog, sgr_dbname ());
535 fail_exit (EXIT_GROUP_FILE);
541 static void close_files (void)
543 if ((gr_close () == 0) && !list) {
544 fprintf (stderr, _("%s: failure while writing changes to %s\n"), Prog, gr_dbname ());
545 SYSLOG ((LOG_ERR, "failure while writing changes to %s", gr_dbname ()));
546 fail_exit (EXIT_GROUP_FILE);
549 if (gr_unlock () == 0) {
550 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
551 SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
559 if ((sgr_close () == 0) && !list) {
560 fprintf (stderr, _("%s: failure while writing changes to %s\n"), Prog, sgr_dbname ());
561 SYSLOG ((LOG_ERR, "failure while writing changes to %s", sgr_dbname ()));
562 fail_exit (EXIT_GROUP_FILE);
565 if (sgr_unlock () == 0) {
566 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
567 SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
576 int main (int argc, char **argv)
579 const struct group *grp;
582 * Get my name so that I can use it to report errors.
584 Prog = Basename (argv[0]);
586 OPENLOG ("groupmems");
588 (void) setlocale (LC_ALL, "");
589 (void) bindtextdomain (PACKAGE, LOCALEDIR);
590 (void) textdomain (PACKAGE);
593 is_shadowgrp = sgr_file_present ();
596 process_flags (argc, argv);
598 if (NULL == thisgroup) {
600 if (!list && (NULL == name)) {
601 fprintf (stderr, _("%s: your groupname does not match your username\n"), Prog);
602 fail_exit (EXIT_NOT_PRIMARY);
606 if (!list && !isroot ()) {
607 fprintf (stderr, _("%s: only root can use the -g/--group option\n"), Prog);
608 fail_exit (EXIT_NOT_ROOT);
616 grp = gr_locate (name);
618 fprintf (stderr, _("%s: group '%s' does not exist in %s\n"),
619 Prog, name, gr_dbname ());
620 fail_exit (EXIT_INVALID_GROUP);
624 display_members ((const char *const *)grp->gr_mem);
625 } else if (NULL != adduser) {
626 add_user (adduser, grp);
627 } else if (NULL != deluser) {
628 remove_user (deluser, grp);