]> granicus.if.org Git - shadow/blob - src/gpasswd.c
Avoid implicit brackets.
[shadow] / src / gpasswd.c
1 /*
2  * Copyright 1990 - 1994, Julianne Frances Haugh
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <config.h>
31
32 #ident "$Id$"
33
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <grp.h>
37 #include <pwd.h>
38 #include <signal.h>
39 #include <stdio.h>
40 #include <sys/types.h>
41 #include "defines.h"
42 #include "exitcodes.h"
43 #include "groupio.h"
44 #include "nscd.h"
45 #include "prototypes.h"
46 #ifdef SHADOWGRP
47 #include "sgroupio.h"
48 #endif
49 /*
50  * Global variables
51  */
52 /* The name of this command, as it is invoked */
53 static char *Prog;
54
55 #ifdef SHADOWGRP
56 /* Indicate if shadow groups are enabled on the system
57  * (/etc/gshadow present) */
58 static int is_shadowgrp;
59 #endif
60
61 /* Flags set by options */
62 static int
63  aflg = 0, Aflg = 0, dflg = 0, Mflg = 0, rflg = 0, Rflg = 0;
64 /* The name of the group that is being affected */
65 static char *group = NULL;
66 /* The name of the user being added (-a) or removed (-d) from group */
67 static char *user = NULL;
68 /* The new list of members set with -M */
69 static char *members = NULL;
70 #ifdef SHADOWGRP
71 /* The new list of group administrators set with -A */
72 static char *admins = NULL;
73 #endif
74 /* The name of the caller */
75 static char *myname = NULL;
76 /* The UID of the caller */
77 static unsigned long bywho = -1;
78 /* Indicate if gpasswd was called by root */
79 #define amroot  (0 == bywho)
80
81 /* The number of retries for th user to provide and repeat a new password */
82 #ifndef RETRIES
83 #define RETRIES 3
84 #endif
85
86 /* local function prototypes */
87 static void usage (void);
88 static RETSIGTYPE catch_signals (int killed);
89 static int check_list (const char *users);
90 static void process_flags (int argc, char **argv);
91 static void check_flags (int argc, int opt_index);
92 static void open_files (void);
93 static void close_files (void);
94 #ifdef SHADOWGRP
95 static void get_group (struct group *gr, struct sgrp *sg);
96 static void check_perms (const struct sgrp *sg);
97 static void update_group (struct group *gr, struct sgrp *sg);
98 static void change_passwd (struct group *gr, struct sgrp *sg);
99 #else
100 static void get_group (struct group *gr);
101 static void check_perms (const struct group *gr);
102 static void update_group (struct group *gr);
103 static void change_passwd (struct group *gr);
104 #endif
105
106 /*
107  * usage - display usage message
108  */
109 static void usage (void)
110 {
111         fprintf (stderr, _("Usage: %s [-r|-R] group\n"), Prog);
112         fprintf (stderr, _("       %s [-a user] group\n"), Prog);
113         fprintf (stderr, _("       %s [-d user] group\n"), Prog);
114 #ifdef SHADOWGRP
115         fprintf (stderr,
116                  _("       %s [-A user,...] [-M user,...] group\n"), Prog);
117 #else
118         fprintf (stderr, _("       %s [-M user,...] group\n"), Prog);
119 #endif
120         exit (E_USAGE);
121 }
122
123 /*
124  * catch_signals - set or reset termio modes.
125  *
126  *      catch_signals() is called before processing begins. signal() is then
127  *      called with catch_signals() as the signal handler. If signal later
128  *      calls catch_signals() with a signal number, the terminal modes are
129  *      then reset.
130  */
131 static RETSIGTYPE catch_signals (int killed)
132 {
133         static TERMIO sgtty;
134
135         if (killed) {
136                 STTY (0, &sgtty);
137         } else {
138                 GTTY (0, &sgtty);
139         }
140
141         if (killed) {
142                 putchar ('\n');
143                 fflush (stdout);
144                 exit (killed);
145         }
146 }
147
148 /*
149  * check_list - check a comma-separated list of user names for validity
150  *
151  *      check_list scans a comma-separated list of user names and checks
152  *      that each listed name exists.
153  */
154 static int check_list (const char *users)
155 {
156         const char *start, *end;
157         char username[32];
158         int errors = 0;
159         size_t len;
160
161         for (start = users; start && *start; start = end) {
162                 if ((end = strchr (start, ','))) {
163                         len = end - start;
164                         end++;
165                 } else {
166                         len = strlen (start);
167                 }
168
169                 if (len > sizeof (username) - 1) {
170                         len = sizeof (username) - 1;
171                 }
172                 strncpy (username, start, len);
173                 username[len] = '\0';
174
175                 /*
176                  * This user must exist.
177                  */
178
179                 if (!getpwnam (username)) { /* local, no need for xgetpwnam */
180                         fprintf (stderr, _("%s: unknown user %s\n"),
181                                  Prog, username);
182                         errors++;
183                 }
184         }
185         return errors;
186 }
187
188 static void failure (void)
189 {
190         fprintf (stderr, _("%s: Permission denied.\n"), Prog);
191         exit (1);
192 }
193
194 /*
195  * process_flags - process the command line options and arguments
196  */
197 static void process_flags (int argc, char **argv)
198 {
199         int flag;
200
201         while ((flag = getopt (argc, argv, "a:A:d:gM:rR")) != EOF) {
202                 switch (flag) {
203                 case 'a':       /* add a user */
204                         user = optarg;
205                         /* local, no need for xgetpwnam */
206                         if (!getpwnam (user)) {
207                                 fprintf (stderr,
208                                          _("%s: unknown user %s\n"), Prog,
209                                          user);
210 #ifdef WITH_AUDIT
211                                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
212                                               "adding to group", user, -1, 0);
213 #endif
214                                 exit (1);
215                         }
216                         aflg++;
217                         break;
218 #ifdef SHADOWGRP
219                 case 'A':
220                         if (!amroot) {
221 #ifdef WITH_AUDIT
222                                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
223                                               "Listing administrators", NULL,
224                                               bywho, 0);
225 #endif
226                                 failure ();
227                         }
228                         if (!is_shadowgrp) {
229                                 fprintf (stderr,
230                                          _
231                                          ("%s: shadow group passwords required for -A\n"),
232                                          Prog);
233                                 exit (2);
234                         }
235                         admins = optarg;
236                         if (check_list (admins)) {
237                                 exit (1);
238                         }
239                         Aflg++;
240                         break;
241 #endif
242                 case 'd':       /* delete a user */
243                         dflg++;
244                         user = optarg;
245                         break;
246                 case 'g':       /* no-op from normal password */
247                         break;
248                 case 'M':
249                         if (!amroot) {
250 #ifdef WITH_AUDIT
251                                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
252                                               "listing members", NULL, bywho,
253                                               0);
254 #endif
255                                 failure ();
256                         }
257                         members = optarg;
258                         if (check_list (members)) {
259                                 exit (1);
260                         }
261                         Mflg++;
262                         break;
263                 case 'r':       /* remove group password */
264                         rflg++;
265                         break;
266                 case 'R':       /* restrict group password */
267                         Rflg++;
268                         break;
269                 default:
270                         usage ();
271                 }
272         }
273
274         /* Get the name of the group that is being affected. */
275         group = argv[optind];
276
277         check_flags (argc, optind);
278 }
279
280 /*
281  * check_flags - check the validity of options
282  */
283 static void check_flags (int argc, int opt_index)
284 {
285         /*
286          * Make sure exclusive flags are exclusive
287          */
288         if (aflg + dflg + rflg + Rflg + (Aflg || Mflg) > 1) {
289                 usage ();
290         }
291
292         /*
293          * Make sure one (and only one) group was provided
294          */
295         if ((argc != (opt_index+1)) || (NULL == group)) {
296                 usage ();
297         }
298 }
299
300 /*
301  * open_files - lock and open the group databases
302  *
303  *      It will call exit in case of error.
304  */
305 static void open_files (void)
306 {
307         if (gr_lock () == 0) {
308                 fprintf (stderr, _("%s: can't get lock\n"), Prog);
309                 SYSLOG ((LOG_WARN, "failed to get lock for /etc/group"));
310 #ifdef WITH_AUDIT
311                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
312                               "locking /etc/group", group, -1, 0);
313 #endif
314                 exit (1);
315         }
316 #ifdef  SHADOWGRP
317         if (is_shadowgrp && (sgr_lock () == 0)) {
318                 fprintf (stderr, _("%s: can't get shadow lock\n"), Prog);
319                 SYSLOG ((LOG_WARN, "failed to get lock for /etc/gshadow"));
320 #ifdef WITH_AUDIT
321                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
322                               "locking /etc/gshadow", group, -1, 0);
323 #endif
324                 exit (1);
325         }
326 #endif
327         if (gr_open (O_RDWR) == 0) {
328                 fprintf (stderr, _("%s: can't open file\n"), Prog);
329                 SYSLOG ((LOG_WARN, "cannot open /etc/group"));
330 #ifdef WITH_AUDIT
331                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
332                               "opening /etc/group", group, -1, 0);
333 #endif
334                 exit (1);
335         }
336 #ifdef  SHADOWGRP
337         if (is_shadowgrp && (sgr_open (O_RDWR) == 0)) {
338                 fprintf (stderr, _("%s: can't open shadow file\n"), Prog);
339                 SYSLOG ((LOG_WARN, "cannot open /etc/gshadow"));
340 #ifdef WITH_AUDIT
341                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
342                               "opening /etc/gshadow", group, -1, 0);
343 #endif
344                 exit (1);
345         }
346 #endif
347 }
348
349 /*
350  * close_files - close and unlock the group databases
351  *
352  *      This cause any changes in the databases to be committed.
353  *
354  *      It will call exit in case of error.
355  */
356 static void close_files (void)
357 {
358         if (gr_close () == 0) {
359                 fprintf (stderr, _("%s: can't re-write file\n"), Prog);
360                 SYSLOG ((LOG_WARN, "cannot re-write /etc/group"));
361 #ifdef WITH_AUDIT
362                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
363                               "rewriting /etc/group", group, -1, 0);
364 #endif
365                 exit (1);
366         }
367 #ifdef  SHADOWGRP
368         if (is_shadowgrp && (sgr_close () == 0)) {
369                 fprintf (stderr, _("%s: can't re-write shadow file\n"), Prog);
370                 SYSLOG ((LOG_WARN, "cannot re-write /etc/gshadow"));
371 #ifdef WITH_AUDIT
372                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
373                               "rewriting /etc/gshadow", group, -1, 0);
374 #endif
375                 exit (1);
376         }
377         if (is_shadowgrp) {
378                 /* TODO: same logging as in open_files & for /etc/group */
379                 sgr_unlock ();
380         }
381 #endif
382         if (gr_unlock () == 0) {
383                 fprintf (stderr, _("%s: can't unlock file\n"), Prog);
384 #ifdef WITH_AUDIT
385                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
386                               "unlocking group file", group, -1, 0);
387 #endif
388                 exit (1);
389         }
390 }
391
392 /*
393  * check_perms - check if the user is allowed to change the password of
394  *               the specified group.
395  *
396  *      It only returns if the user is allowed.
397  */
398 #ifdef SHADOWGRP
399 static void check_perms (const struct sgrp *sg)
400 #else
401 static void check_perms (const struct group *gr)
402 #endif
403 {
404 #ifdef SHADOWGRP
405         /*
406          * The policy here for changing a group is that 1) you must be root
407          * or 2). you must be listed as an administrative member.
408          * Administrative members can do anything to a group that the root
409          * user can.
410          */
411         if (!amroot && !is_on_list (sg->sg_adm, myname)) {
412 #ifdef WITH_AUDIT
413                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
414                               "modify group", group, -1, 0);
415 #endif
416                 failure ();
417         }
418 #else                           /* ! SHADOWGRP */
419
420 #ifdef FIRST_MEMBER_IS_ADMIN
421         /*
422          * The policy here for changing a group is that 1) you must bes root
423          * or 2) you must be the first listed member of the group. The
424          * first listed member of a group can do anything to that group that
425          * the root user can. The rationale for this hack is that the FIRST
426          * user is probably the most important user in this entire group.
427          */
428         if (!amroot) {
429                 if (gr->gr_mem[0] == (char *) 0) {
430 #ifdef WITH_AUDIT
431                         audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
432                                       "modifying group", group, -1, 0);
433 #endif
434                         failure ();
435                 }
436
437                 if (strcmp (gr->gr_mem[0], myname) != 0) {
438 #ifdef WITH_AUDIT
439                         audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
440                                       "modifying group", myname, -1, 0);
441 #endif
442                         failure ();
443                 }
444         }
445 #else
446         /*
447          * This feature enabled by default could be a security problem when
448          * installed on existing systems where the first group member might
449          * be just a normal user.  --marekm
450          */
451         if (!amroot) {
452 #ifdef WITH_AUDIT
453                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
454                               "modifying group", group, -1, 0);
455 #endif
456                 failure ();
457         }
458 #endif
459 #endif                          /* SHADOWGRP */
460 }
461
462 /*
463  * update_group - Update the group information in the databases
464  */
465 #ifdef SHADOWGRP
466 static void update_group (struct group *gr, struct sgrp *sg)
467 #else
468 static void update_group (struct group *gr)
469 #endif
470 {
471         if (!gr_update (gr)) {
472                 fprintf (stderr, _("%s: can't update entry\n"), Prog);
473                 SYSLOG ((LOG_WARN, "cannot update /etc/group"));
474 #ifdef WITH_AUDIT
475                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
476                               "updating /etc/group", group, -1, 0);
477 #endif
478                 exit (1);
479         }
480 #ifdef SHADOWGRP
481         if (is_shadowgrp && !sgr_update (sg)) {
482                 fprintf (stderr, _("%s: can't update shadow entry\n"), Prog);
483                 SYSLOG ((LOG_WARN, "cannot update /etc/gshadow"));
484 #ifdef WITH_AUDIT
485                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
486                               "updating /etc/gshadow", group, -1, 0);
487 #endif
488                 exit (1);
489         }
490 #endif
491 }
492
493 /*
494  * get_group - get the current information for the group
495  *
496  *      The information are copied in group structure(s) so that they can be
497  *      modified later.
498  */
499 #ifdef SHADOWGRP
500 static void get_group (struct group *gr, struct sgrp *sg)
501 #else
502 static void get_group (struct group *gr)
503 #endif
504 {
505         struct group const*tmpgr = NULL;
506         struct sgrp const*tmpsg = NULL;
507
508         if (!gr_open (O_RDONLY)) {
509                 fprintf (stderr, _("%s: can't open file\n"), Prog);
510                 SYSLOG ((LOG_WARN, "cannot open /etc/group"));
511 #ifdef WITH_AUDIT
512                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
513                               "opening /etc/group", group, -1, 0);
514 #endif
515                 exit (1);
516         }
517
518         if (!(tmpgr = gr_locate (group))) {
519                 fprintf (stderr, _("unknown group: %s\n"), group);
520 #ifdef WITH_AUDIT
521                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
522                               "group lookup", group, -1, 0);
523 #endif
524                 failure ();
525         }
526
527         *gr = *tmpgr;
528         gr->gr_name = xstrdup (tmpgr->gr_name);
529         gr->gr_passwd = xstrdup (tmpgr->gr_passwd);
530         gr->gr_mem = dup_list (tmpgr->gr_mem);
531
532         if (!gr_close ()) {
533                 fprintf (stderr, _("%s: can't close file\n"), Prog);
534                 SYSLOG ((LOG_WARN, "cannot close /etc/group"));
535 #ifdef WITH_AUDIT
536                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
537                               "closing /etc/group", group, -1, 0);
538 #endif
539                 exit (1);
540         }
541
542 #ifdef SHADOWGRP
543         if (!sgr_open (O_RDONLY)) {
544                 fprintf (stderr, _("%s: can't open shadow file\n"), Prog);
545                 SYSLOG ((LOG_WARN, "cannot open /etc/gshadow"));
546 #ifdef WITH_AUDIT
547                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
548                               "opening /etc/gshadow", group, -1, 0);
549 #endif
550                 exit (1);
551         }
552         if ((tmpsg = sgr_locate (group))) {
553                 *sg = *tmpsg;
554                 sg->sg_name = xstrdup (tmpsg->sg_name);
555                 sg->sg_passwd = xstrdup (tmpsg->sg_passwd);
556
557                 sg->sg_mem = dup_list (tmpsg->sg_mem);
558                 sg->sg_adm = dup_list (tmpsg->sg_adm);
559         } else {
560                 sg->sg_name = xstrdup (group);
561                 sg->sg_passwd = gr->gr_passwd;
562                 gr->gr_passwd = "!";    /* XXX warning: const */
563
564                 sg->sg_mem = dup_list (gr->gr_mem);
565
566                 sg->sg_adm = (char **) xmalloc (sizeof (char *) * 2);
567 #ifdef FIRST_MEMBER_IS_ADMIN
568                 if (sg->sg_mem[0]) {
569                         sg->sg_adm[0] = xstrdup (sg->sg_mem[0]);
570                         sg->sg_adm[1] = 0;
571                 } else
572 #endif
573                 {
574                         sg->sg_adm[0] = 0;
575                 }
576
577         }
578         if (!sgr_close ()) {
579                 fprintf (stderr, _("%s: can't close shadow file\n"), Prog);
580                 SYSLOG ((LOG_WARN, "cannot close /etc/gshadow"));
581 #ifdef WITH_AUDIT
582                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
583                               "closing /etc/gshadow", group, -1, 0);
584 #endif
585                 exit (1);
586         }
587 #endif                          /* SHADOWGRP */
588 }
589
590 /*
591  * change_passwd - change the group's password
592  *
593  *      Get the new password from the user and update the password in the
594  *      group's structure.
595  *
596  *      It will call exit in case of error.
597  */
598 #ifdef SHADOWGRP
599 static void change_passwd (struct group *gr, struct sgrp *sg)
600 #else
601 static void change_passwd (struct group *gr)
602 #endif
603 {
604         char *cp;
605         static char pass[BUFSIZ];
606         int retries;
607
608         /*
609          * A new password is to be entered and it must be encrypted, etc.
610          * The password will be prompted for twice, and both entries must be
611          * identical. There is no need to validate the old password since
612          * the invoker is either the group owner, or root.
613          */
614         printf (_("Changing the password for group %s\n"), group);
615
616         for (retries = 0; retries < RETRIES; retries++) {
617                 if (!(cp = getpass (_("New Password: ")))) {
618                         exit (1);
619                 }
620
621                 STRFCPY (pass, cp);
622                 strzero (cp);
623                 if (!(cp = getpass (_("Re-enter new password: ")))) {
624                         exit (1);
625                 }
626
627                 if (strcmp (pass, cp) == 0) {
628                         strzero (cp);
629                         break;
630                 }
631
632                 strzero (cp);
633                 memzero (pass, sizeof pass);
634
635                 if (retries + 1 < RETRIES) {
636                         puts (_("They don't match; try again"));
637 #ifdef WITH_AUDIT
638                         audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
639                                       "changing password", group, -1, 0);
640 #endif
641                 }
642         }
643
644         if (retries == RETRIES) {
645                 fprintf (stderr, _("%s: Try again later\n"), Prog);
646                 exit (1);
647         }
648
649         cp = pw_encrypt (pass, crypt_make_salt (NULL, NULL));
650         memzero (pass, sizeof pass);
651 #ifdef SHADOWGRP
652         if (is_shadowgrp) {
653                 sg->sg_passwd = cp;
654         } else
655 #endif
656         {
657                 gr->gr_passwd = cp;
658         }
659 #ifdef WITH_AUDIT
660         audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
661                       "changing password", group, -1, 1);
662 #endif
663         SYSLOG ((LOG_INFO, "change the password for group %s by %s", group,
664                 myname));
665 }
666
667 /*
668  * gpasswd - administer the /etc/group file
669  *
670  *      -a user         add user to the named group
671  *      -d user         remove user from the named group
672  *      -r              remove password from the named group
673  *      -R              restrict access to the named group
674  *      -A user,...     make list of users the administrative users
675  *      -M user,...     make list of users the group members
676  */
677 int main (int argc, char **argv)
678 {
679         struct group grent;
680 #ifdef SHADOWGRP
681         struct sgrp sgent;
682 #endif
683         struct passwd *pw = NULL;
684
685 #ifdef WITH_AUDIT
686         audit_help_open ();
687 #endif
688
689         sanitize_env ();
690         setlocale (LC_ALL, "");
691         bindtextdomain (PACKAGE, LOCALEDIR);
692         textdomain (PACKAGE);
693
694         /*
695          * Make a note of whether or not this command was invoked by root.
696          * This will be used to bypass certain checks later on. Also, set
697          * the real user ID to match the effective user ID. This will
698          * prevent the invoker from issuing signals which would interfer
699          * with this command.
700          */
701         bywho = getuid ();
702         Prog = Basename (argv[0]);
703
704         OPENLOG ("gpasswd");
705         setbuf (stdout, NULL);
706         setbuf (stderr, NULL);
707
708 #ifdef SHADOWGRP
709         is_shadowgrp = sgr_file_present ();
710 #endif
711
712         /* Parse the options */
713         process_flags (argc, argv);
714
715         /*
716          * Determine the name of the user that invoked this command. This
717          * is really hit or miss because there are so many ways that command
718          * can be executed and so many ways to trip up the routines that
719          * report the user name.
720          */
721
722         pw = get_my_pwent ();
723         if (!pw) {
724                 fprintf (stderr, _("Who are you?\n"));
725 #ifdef WITH_AUDIT
726                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "user lookup", NULL,
727                               bywho, 0);
728 #endif
729                 failure ();
730         }
731         myname = xstrdup (pw->pw_name);
732
733         /*
734          * Replicate the group so it can be modified later on.
735          */
736 #ifdef SHADOWGRP
737         get_group (&grent, &sgent);
738 #else
739         get_group (&grent);
740 #endif
741
742         /*
743          * Check if the user is allowed to change the password of this group.
744          */
745 #ifdef SHADOWGRP
746         check_perms (&sgent);
747 #else
748         check_perms (&grent);
749 #endif
750
751         /*
752          * Removing a password is straight forward. Just set the password
753          * field to a "".
754          */
755         if (rflg) {
756                 grent.gr_passwd = "";   /* XXX warning: const */
757 #ifdef SHADOWGRP
758                 sgent.sg_passwd = "";   /* XXX warning: const */
759 #endif
760 #ifdef WITH_AUDIT
761                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
762                               "deleting group password", group, -1, 1);
763 #endif
764                 SYSLOG ((LOG_INFO, "remove password from group %s by %s",
765                          group, myname));
766                 goto output;
767         } else if (Rflg) {
768                 /*
769                  * Same thing for restricting the group. Set the password
770                  * field to "!".
771                  */
772                 grent.gr_passwd = "!";  /* XXX warning: const */
773 #ifdef SHADOWGRP
774                 sgent.sg_passwd = "!";  /* XXX warning: const */
775 #endif
776 #ifdef WITH_AUDIT
777                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
778                               "restrict access to group", group, -1, 1);
779 #endif
780                 SYSLOG ((LOG_INFO, "restrict access to group %s by %s",
781                          group, myname));
782                 goto output;
783         }
784
785         /*
786          * Adding a member to a member list is pretty straightforward as
787          * well. Call the appropriate routine and split.
788          */
789         if (aflg) {
790                 printf (_("Adding user %s to group %s\n"), user, group);
791                 grent.gr_mem = add_list (grent.gr_mem, user);
792 #ifdef SHADOWGRP
793                 sgent.sg_mem = add_list (sgent.sg_mem, user);
794 #endif
795 #ifdef WITH_AUDIT
796                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "adding group member",
797                               user, -1, 1);
798 #endif
799                 SYSLOG ((LOG_INFO, "add member %s to group %s by %s", user,
800                          group, myname));
801                 goto output;
802         }
803
804         /*
805          * Removing a member from the member list is the same deal as adding
806          * one, except the routine is different.
807          */
808         if (dflg) {
809                 int removed = 0;
810
811                 printf (_("Removing user %s from group %s\n"), user, group);
812
813                 if (is_on_list (grent.gr_mem, user)) {
814                         removed = 1;
815                         grent.gr_mem = del_list (grent.gr_mem, user);
816                 }
817 #ifdef SHADOWGRP
818                 if (is_on_list (sgent.sg_mem, user)) {
819                         removed = 1;
820                         sgent.sg_mem = del_list (sgent.sg_mem, user);
821                 }
822 #endif
823                 if (!removed) {
824                         fprintf (stderr, _("%s: unknown member %s\n"),
825                                  Prog, user);
826 #ifdef WITH_AUDIT
827                         audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
828                                       "deleting member", user, -1, 0);
829 #endif
830                         exit (1);
831                 }
832 #ifdef WITH_AUDIT
833                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "deleting member",
834                               user, -1, 1);
835 #endif
836                 SYSLOG ((LOG_INFO, "remove member %s from group %s by %s",
837                          user, group, myname));
838                 goto output;
839         }
840 #ifdef SHADOWGRP
841         /*
842          * Replacing the entire list of administators is simple. Check the
843          * list to make sure everyone is a real user. Then slap the new list
844          * in place.
845          */
846         if (Aflg) {
847 #ifdef WITH_AUDIT
848                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "setting group admin",
849                               group, -1, 1);
850 #endif
851                 SYSLOG ((LOG_INFO, "set administrators of %s to %s",
852                          group, admins));
853                 sgent.sg_adm = comma_to_list (admins);
854                 if (!Mflg) {
855                         goto output;
856                 }
857         }
858 #endif
859
860         /*
861          * Replacing the entire list of members is simple. Check the list to
862          * make sure everyone is a real user. Then slap the new list in
863          * place.
864          */
865         if (Mflg) {
866 #ifdef WITH_AUDIT
867                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
868                               "setting group members", group, -1, 1);
869 #endif
870                 SYSLOG ((LOG_INFO, "set members of %s to %s", group, members));
871 #ifdef SHADOWGRP
872                 sgent.sg_mem = comma_to_list (members);
873 #endif
874                 grent.gr_mem = comma_to_list (members);
875                 goto output;
876         }
877
878         /*
879          * If the password is being changed, the input and output must both
880          * be a tty. The typical keyboard signals are caught so the termio
881          * modes can be restored.
882          */
883         if (!isatty (0) || !isatty (1)) {
884                 fprintf (stderr, _("%s: Not a tty\n"), Prog);
885 #ifdef WITH_AUDIT
886                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "changing password",
887                               group, -1, 0);
888 #endif
889                 exit (1);
890         }
891
892         catch_signals (0);      /* save tty modes */
893
894         signal (SIGHUP, catch_signals);
895         signal (SIGINT, catch_signals);
896         signal (SIGQUIT, catch_signals);
897         signal (SIGTERM, catch_signals);
898 #ifdef SIGTSTP
899         signal (SIGTSTP, catch_signals);
900 #endif
901
902         /* Prompt for the new password */
903 #ifdef SHADOWGRP
904         change_passwd (&grent, &sgent);
905 #else
906         change_passwd (&grent);
907 #endif
908
909         /*
910          * This is the common arrival point to output the new group file.
911          * The freshly crafted entry is in allocated space. The group file
912          * will be locked and opened for writing. The new entry will be
913          * output, etc.
914          */
915       output:
916         if (setuid (0)) {
917                 fprintf (stderr, _("Cannot change ID to root.\n"));
918                 SYSLOG ((LOG_ERR, "can't setuid(0)"));
919 #ifdef WITH_AUDIT
920                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "changing id to root",
921                               group, -1, 0);
922 #endif
923                 closelog ();
924                 exit (1);
925         }
926         pwd_init ();
927
928         open_files ();
929
930 #ifdef SHADOWGRP
931         update_group (&grent, &sgent);
932 #else
933         update_group (&grent);
934 #endif
935
936         close_files ();
937
938         nscd_flush_cache ("group");
939
940         exit (E_SUCCESS);
941 }