]> granicus.if.org Git - shadow/blob - src/groupmems.c
* src/chage.c, src/chfn.c, src/chgpasswd.c, src/chpasswd.c,
[shadow] / src / groupmems.c
1 /*
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 - 2011, Nicolas François
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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.
31  */
32
33 #include <config.h>
34
35 #include <fcntl.h>
36 #include <getopt.h>
37 #include <grp.h>
38 #include <stdio.h>
39 #include <sys/types.h>
40 #ifdef USE_PAM
41 #include "pam_defs.h"
42 #endif                          /* USE_PAM */
43 #include <pwd.h>
44 #include "defines.h"
45 #include "prototypes.h"
46 #include "groupio.h"
47 #ifdef SHADOWGRP
48 #include "sgroupio.h"
49 #endif
50
51 /* Exit Status Values */
52 /*@-exitarg@*/
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 */
63
64 /*
65  * Global variables
66  */
67 const char *Prog;
68
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;
76 #ifdef SHADOWGRP
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;
81 #endif
82
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 /*@noreturn@*/void usage (int status);
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)
96
97 static char *whoami (void)
98 {
99         /* local, no need for xgetgrgid */
100         struct group *grp = getgrgid (getgid ());
101         /* local, no need for xgetpwuid */
102         struct passwd *usr = getpwuid (getuid ());
103
104         if (   (NULL != usr)
105             && (NULL != grp)
106             && (0 == strcmp (usr->pw_name, grp->gr_name))) {
107                 return xstrdup (usr->pw_name);
108         } else {
109                 return NULL;
110         }
111 }
112
113 /*
114  * add_user - Add an user to the specified group
115  */
116 static void add_user (const char *user,
117                       const struct group *grp)
118 {
119         struct group *newgrp;
120
121         /* Make sure the user is not already part of the group */
122         if (is_on_list (grp->gr_mem, user)) {
123                 fprintf (stderr,
124                          _("%s: user '%s' is already a member of '%s'\n"),
125                          Prog, user, grp->gr_name);
126                 fail_exit (EXIT_MEMBER_EXISTS);
127         }
128
129         newgrp = __gr_dup(grp);
130         if (NULL == newgrp) {
131                 fprintf (stderr,
132                          _("%s: Out of memory. Cannot update %s.\n"),
133                          Prog, gr_dbname ());
134                 fail_exit (13);
135         }
136
137         /* Add the user to the /etc/group group */
138         newgrp->gr_mem = add_list (newgrp->gr_mem, user);
139
140 #ifdef SHADOWGRP
141         if (is_shadowgrp) {
142                 const struct sgrp *sg = sgr_locate (newgrp->gr_name);
143                 struct sgrp *newsg;
144
145                 if (NULL == sg) {
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;
155                         } else
156 #endif
157                         {
158                                 sgrent.sg_adm[0] = NULL;
159                         }
160
161                         /* Move any password to gshadow */
162                         sgrent.sg_passwd = newgrp->gr_passwd;
163                         newgrp->gr_passwd = SHADOW_PASSWD_STRING;
164
165                         newsg = &sgrent;
166                 } else {
167                         newsg = __sgr_dup (sg);
168                         if (NULL == newsg) {
169                                 fprintf (stderr,
170                                          _("%s: Out of memory. Cannot update %s.\n"),
171                                          Prog, sgr_dbname ());
172                                 fail_exit (13);
173                         }
174                         /* Add the user to the members */
175                         newsg->sg_mem = add_list (newsg->sg_mem, user);
176                         /* Do not touch the administrators */
177                 }
178
179                 if (sgr_update (newsg) == 0) {
180                         fprintf (stderr,
181                                  _("%s: failed to prepare the new %s entry '%s'\n"),
182                                  Prog, sgr_dbname (), newsg->sg_name);
183                         fail_exit (13);
184                 }
185         }
186 #endif
187
188         if (gr_update (newgrp) == 0) {
189                 fprintf (stderr,
190                          _("%s: failed to prepare the new %s entry '%s'\n"),
191                          Prog, gr_dbname (), newgrp->gr_name);
192                 fail_exit (13);
193         }
194 }
195
196 /*
197  * remove_user - Remove an user from a given group
198  */
199 static void remove_user (const char *user, 
200                          const struct group *grp)
201 {
202         struct group *newgrp;
203
204         /* Check if the user is a member of the specified group */
205         if (!is_on_list (grp->gr_mem, user)) {
206                 fprintf (stderr,
207                          _("%s: user '%s' is not a member of '%s'\n"),
208                          Prog, user, grp->gr_name);
209                 fail_exit (EXIT_NOT_MEMBER);
210         }
211
212         newgrp = __gr_dup (grp);
213         if (NULL == newgrp) {
214                 fprintf (stderr,
215                          _("%s: Out of memory. Cannot update %s.\n"),
216                          Prog, gr_dbname ());
217                 fail_exit (13);
218         }
219
220         /* Remove the user from the /etc/group group */
221         newgrp->gr_mem = del_list (newgrp->gr_mem, user);
222
223 #ifdef SHADOWGRP
224         if (is_shadowgrp) {
225                 const struct sgrp *sg = sgr_locate (newgrp->gr_name);
226                 struct sgrp *newsg;
227
228                 if (NULL == sg) {
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;
238                         } else
239 #endif
240                         {
241                                 sgrent.sg_adm[0] = NULL;
242                         }
243
244                         /* Move any password to gshadow */
245                         sgrent.sg_passwd = newgrp->gr_passwd;
246                         newgrp->gr_passwd = SHADOW_PASSWD_STRING;
247
248                         newsg = &sgrent;
249                 } else {
250                         newsg = __sgr_dup (sg);
251                         if (NULL == newsg) {
252                                 fprintf (stderr,
253                                          _("%s: Out of memory. Cannot update %s.\n"),
254                                          Prog, sgr_dbname ());
255                                 fail_exit (13);
256                         }
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);
261                 }
262
263                 if (sgr_update (newsg) == 0) {
264                         fprintf (stderr,
265                                  _("%s: failed to prepare the new %s entry '%s'\n"),
266                                  Prog, sgr_dbname (), newsg->sg_name);
267                         fail_exit (13);
268                 }
269         }
270 #endif
271
272         if (gr_update (newgrp) == 0) {
273                 fprintf (stderr,
274                          _("%s: failed to prepare the new %s entry '%s'\n"),
275                          Prog, gr_dbname (), newgrp->gr_name);
276                 fail_exit (13);
277         }
278 }
279
280 /*
281  * purge_members - Rmeove every members of the specified group
282  */
283 static void purge_members (const struct group *grp)
284 {
285         struct group *newgrp = __gr_dup (grp);
286
287         if (NULL == newgrp) {
288                 fprintf (stderr,
289                          _("%s: Out of memory. Cannot update %s.\n"),
290                          Prog, gr_dbname ());
291                 fail_exit (13);
292         }
293
294         /* Remove all the members of the /etc/group group */
295         newgrp->gr_mem[0] = NULL;
296
297 #ifdef SHADOWGRP
298         if (is_shadowgrp) {
299                 const struct sgrp *sg = sgr_locate (newgrp->gr_name);
300                 struct sgrp *newsg;
301
302                 if (NULL == sg) {
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;
310
311                         /* Move any password to gshadow */
312                         sgrent.sg_passwd = newgrp->gr_passwd;
313                         newgrp->gr_passwd = xstrdup(SHADOW_PASSWD_STRING);
314
315                         newsg = &sgrent;
316                 } else {
317                         newsg = __sgr_dup (sg);
318                         if (NULL == newsg) {
319                                 fprintf (stderr,
320                                          _("%s: Out of memory. Cannot update %s.\n"),
321                                          Prog, sgr_dbname ());
322                                 fail_exit (13);
323                         }
324                         /* Remove all the members of the /etc/gshadow
325                          * group */
326                         newsg->sg_mem[0] = NULL;
327                         /* Remove all the administrators of the
328                          * /etc/gshadow group */
329                         newsg->sg_adm[0] = NULL;
330                 }
331
332                 if (sgr_update (newsg) == 0) {
333                         fprintf (stderr,
334                                  _("%s: failed to prepare the new %s entry '%s'\n"),
335                                  Prog, sgr_dbname (), newsg->sg_name);
336                         fail_exit (13);
337                 }
338         }
339 #endif
340
341         if (gr_update (newgrp) == 0) {
342                 fprintf (stderr,
343                          _("%s: failed to prepare the new %s entry '%s'\n"),
344                          Prog, gr_dbname (), newgrp->gr_name);
345                 fail_exit (13);
346         }
347 }
348
349 static void display_members (const char *const *members)
350 {
351         int i;
352
353         for (i = 0; NULL != members[i]; i++) {
354                 printf ("%s ", members[i]);
355
356                 if (NULL == members[i + 1]) {
357                         printf ("\n");
358                 } else {
359                         printf (" ");
360                 }
361         }
362 }
363
364 static /*@noreturn@*/void usage (int status)
365 {
366         FILE *usageout = (EXIT_SUCCESS != status) ? stderr : stdout;
367         (void) fprintf (usageout,
368                         _("Usage: %s [options] [action]\n"
369                           "\n"
370                           "Options:\n"),
371                         Prog);
372         (void) fputs (_("  -g, --group groupname         change groupname instead of the user's group\n"
373                         "                                (root only)\n"), usageout);
374         (void) fputs (_("  -R, --root CHROOT_DIR         directory to chroot into\n"), usageout);
375         (void) fputs (_("\n"), usageout);
376         (void) fputs (_("Actions:\n"), usageout);
377         (void) fputs (_("  -a, --add username            add username to the members of the group\n"), usageout);
378         (void) fputs (_("  -d, --delete username         remove username from the members of the group\n"), usageout);
379         (void) fputs (_("  -h, --help                    display this help message and exit\n"), usageout);
380         (void) fputs (_("  -p, --purge                   purge all members from the group\n"), usageout);
381         (void) fputs (_("  -l, --list                    list the members of the group\n"), usageout);
382         exit (status);
383 }
384
385 /*
386  * process_flags - perform command line argument setting
387  */
388 static void process_flags (int argc, char **argv)
389 {
390         int c;
391         static struct option long_options[] = {
392                 {"add",    required_argument, NULL, 'a'},
393                 {"delete", required_argument, NULL, 'd'},
394                 {"group",  required_argument, NULL, 'g'},
395                 {"help",   no_argument,       NULL, 'h'},
396                 {"list",   no_argument,       NULL, 'l'},
397                 {"purge",  no_argument,       NULL, 'p'},
398                 {"root",   required_argument, NULL, 'R'},
399                 {NULL, 0, NULL, '\0'}
400         };
401
402         while ((c = getopt_long (argc, argv, "a:d:g:hlpR:",
403                                  long_options, NULL)) != EOF) {
404                 switch (c) {
405                 case 'a':
406                         adduser = xstrdup (optarg);
407                         ++exclusive;
408                         break;
409                 case 'd':
410                         deluser = xstrdup (optarg);
411                         ++exclusive;
412                         break;
413                 case 'g':
414                         thisgroup = xstrdup (optarg);
415                         break;
416                 case 'h':
417                         usage (EXIT_SUCCESS);
418                         /*@notreached@*/break;
419                 case 'l':
420                         list = true;
421                         ++exclusive;
422                         break;
423                 case 'p':
424                         purge = true;
425                         ++exclusive;
426                         break;
427                 case 'R': /* no-op, handled in process_root_flag () */
428                         break;
429                 default:
430                         usage (EXIT_USAGE);
431                 }
432         }
433
434         if ((exclusive > 1) || (optind < argc)) {
435                 usage (EXIT_USAGE);
436         }
437
438         /* local, no need for xgetpwnam */
439         if (   (NULL != adduser)
440             && (getpwnam (adduser) == NULL)) {
441                 fprintf (stderr, _("%s: user '%s' does not exist\n"),
442                          Prog, adduser);
443                 fail_exit (EXIT_INVALID_USER);
444         }
445
446 }
447
448 static void check_perms (void)
449 {
450         if (!list) {
451 #ifdef USE_PAM
452                 pam_handle_t *pamh = NULL;
453                 int retval;
454                 struct passwd *pampw;
455
456                 pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
457                 if (NULL == pampw) {
458                         fprintf (stderr,
459                                  _("%s: Cannot determine your user name.\n"),
460                                  Prog);
461                         fail_exit (1);
462                 }
463
464                 retval = pam_start ("groupmems", pampw->pw_name, &conv, &pamh);
465
466                 if (PAM_SUCCESS == retval) {
467                         retval = pam_authenticate (pamh, 0);
468                 }
469
470                 if (PAM_SUCCESS == retval) {
471                         retval = pam_acct_mgmt (pamh, 0);
472                 }
473
474                 if (PAM_SUCCESS != retval) {
475                         fprintf (stderr, _("%s: PAM: %s\n"),
476                                  Prog, pam_strerror (pamh, retval));
477                         SYSLOG((LOG_ERR, "%s", pam_strerror (pamh, retval)));
478                         if (NULL != pamh) {
479                                 (void) pam_end (pamh, retval);
480                         }
481                         fail_exit (1);
482                 }
483                 (void) pam_end (pamh, retval);
484 #endif
485         }
486 }
487
488 static void fail_exit (int code)
489 {
490         if (gr_locked) {
491                 if (gr_unlock () == 0) {
492                         fprintf (stderr,
493                                  _("%s: failed to unlock %s\n"),
494                                  Prog, gr_dbname ());
495                         SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
496                         /* continue */
497                 }
498         }
499
500 #ifdef SHADOWGRP
501         if (sgr_locked) {
502                 if (sgr_unlock () == 0) {
503                         fprintf (stderr,
504                                  _("%s: failed to unlock %s\n"),
505                                  Prog, sgr_dbname ());
506                         SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
507                         /* continue */
508                 }
509         }
510 #endif
511
512         exit (code);
513 }
514
515 static void open_files (void)
516 {
517         if (!list) {
518                 if (gr_lock () == 0) {
519                         fprintf (stderr,
520                                  _("%s: cannot lock %s; try again later.\n"),
521                                  Prog, gr_dbname ());
522                         fail_exit (EXIT_GROUP_FILE);
523                 }
524                 gr_locked = true;
525
526 #ifdef SHADOWGRP
527                 if (is_shadowgrp) {
528                         if (sgr_lock () == 0) {
529                                 fprintf (stderr,
530                                          _("%s: cannot lock %s; try again later.\n"),
531                                          Prog, sgr_dbname ());
532                                 fail_exit (EXIT_GROUP_FILE);
533                         }
534                         sgr_locked = true;
535                 }
536 #endif
537         }
538
539         if (gr_open (list ? O_RDONLY : O_RDWR) == 0) {
540                 fprintf (stderr, _("%s: cannot open %s\n"), Prog, gr_dbname ());
541                 fail_exit (EXIT_GROUP_FILE);
542         }
543
544 #ifdef SHADOWGRP
545         if (is_shadowgrp) {
546                 if (sgr_open (list ? O_RDONLY : O_RDWR) == 0) {
547                         fprintf (stderr, _("%s: cannot open %s\n"), Prog, sgr_dbname ());
548                         fail_exit (EXIT_GROUP_FILE);
549                 }
550         }
551 #endif
552 }
553
554 static void close_files (void)
555 {
556         if ((gr_close () == 0) && !list) {
557                 fprintf (stderr, _("%s: failure while writing changes to %s\n"), Prog, gr_dbname ());
558                 SYSLOG ((LOG_ERR, "failure while writing changes to %s", gr_dbname ()));
559                 fail_exit (EXIT_GROUP_FILE);
560         }
561         if (gr_locked) {
562                 if (gr_unlock () == 0) {
563                         fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
564                         SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
565                         /* continue */
566                 }
567                 gr_locked = false;
568         }
569
570 #ifdef SHADOWGRP
571         if (is_shadowgrp) {
572                 if ((sgr_close () == 0) && !list) {
573                         fprintf (stderr, _("%s: failure while writing changes to %s\n"), Prog, sgr_dbname ());
574                         SYSLOG ((LOG_ERR, "failure while writing changes to %s", sgr_dbname ()));
575                         fail_exit (EXIT_GROUP_FILE);
576                 }
577                 if (sgr_locked) {
578                         if (sgr_unlock () == 0) {
579                                 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
580                                 SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
581                                 /* continue */
582                         }
583                         sgr_locked = false;
584                 }
585         }
586 #endif
587 }
588
589 int main (int argc, char **argv) 
590 {
591         char *name;
592         const struct group *grp;
593
594         /*
595          * Get my name so that I can use it to report errors.
596          */
597         Prog = Basename (argv[0]);
598
599         (void) setlocale (LC_ALL, "");
600         (void) bindtextdomain (PACKAGE, LOCALEDIR);
601         (void) textdomain (PACKAGE);
602
603         process_root_flag ("-R", argc, argv);
604
605         OPENLOG ("groupmems");
606
607 #ifdef SHADOWGRP
608         is_shadowgrp = sgr_file_present ();
609 #endif
610
611         process_flags (argc, argv);
612
613         if (NULL == thisgroup) {
614                 name = whoami ();
615                 if (!list && (NULL == name)) {
616                         fprintf (stderr, _("%s: your groupname does not match your username\n"), Prog);
617                         fail_exit (EXIT_NOT_PRIMARY);
618                 }
619         } else {
620                 name = thisgroup;
621                 if (!list && !isroot ()) {
622                         fprintf (stderr, _("%s: only root can use the -g/--group option\n"), Prog);
623                         fail_exit (EXIT_NOT_ROOT);
624                 }
625         }
626
627         check_perms ();
628
629         open_files ();
630
631         grp = gr_locate (name);
632         if (NULL == grp) {
633                 fprintf (stderr, _("%s: group '%s' does not exist in %s\n"),
634                          Prog, name, gr_dbname ());
635                 fail_exit (EXIT_INVALID_GROUP);
636         }
637
638         if (list) {
639                 display_members ((const char *const *)grp->gr_mem);
640         } else if (NULL != adduser) {
641                 add_user (adduser, grp);
642         } else if (NULL != deluser) {
643                 remove_user (deluser, grp);
644         } else if (purge) {
645                 purge_members (grp);
646         }
647
648         close_files ();
649
650         exit (EXIT_SUCCESS);
651 }
652