]> granicus.if.org Git - shadow/blob - src/gpasswd.c
gpasswd cleanup
[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 static int
62  aflg = 0, Aflg = 0, dflg = 0, Mflg = 0, rflg = 0, Rflg = 0;
63
64 /* The UID of the caller */
65 unsigned long bywho = -1;
66
67 /* The number of retries for th user to provide and repeat a new password */
68 #ifndef RETRIES
69 #define RETRIES 3
70 #endif
71
72 /* local function prototypes */
73 static void usage (void);
74 static RETSIGTYPE catch_signals (int killed);
75 static int check_list (const char *users);
76
77 /*
78  * usage - display usage message
79  */
80 static void usage (void)
81 {
82         fprintf (stderr, _("Usage: %s [-r|-R] group\n"), Prog);
83         fprintf (stderr, _("       %s [-a user] group\n"), Prog);
84         fprintf (stderr, _("       %s [-d user] group\n"), Prog);
85 #ifdef  SHADOWGRP
86         fprintf (stderr,
87                  _("       %s [-A user,...] [-M user,...] group\n"), Prog);
88 #else
89         fprintf (stderr, _("       %s [-M user,...] group\n"), Prog);
90 #endif
91         exit (E_USAGE);
92 }
93
94 /*
95  * catch_signals - set or reset termio modes.
96  *
97  *      catch_signals() is called before processing begins. signal() is then
98  *      called with catch_signals() as the signal handler. If signal later
99  *      calls catch_signals() with a signal number, the terminal modes are
100  *      then reset.
101  */
102 static RETSIGTYPE catch_signals (int killed)
103 {
104         static TERMIO sgtty;
105
106         if (killed)
107                 STTY (0, &sgtty);
108         else
109                 GTTY (0, &sgtty);
110
111         if (killed) {
112                 putchar ('\n');
113                 fflush (stdout);
114                 exit (killed);
115         }
116 }
117
118 /*
119  * check_list - check a comma-separated list of user names for validity
120  *
121  *      check_list scans a comma-separated list of user names and checks
122  *      that each listed name exists.
123  */
124 static int check_list (const char *users)
125 {
126         const char *start, *end;
127         char username[32];
128         int errors = 0;
129         size_t len;
130
131         for (start = users; start && *start; start = end) {
132                 if ((end = strchr (start, ','))) {
133                         len = end - start;
134                         end++;
135                 } else {
136                         len = strlen (start);
137                 }
138
139                 if (len > sizeof (username) - 1)
140                         len = sizeof (username) - 1;
141                 strncpy (username, start, len);
142                 username[len] = '\0';
143
144                 /*
145                  * This user must exist.
146                  */
147
148                 if (!getpwnam (username)) { /* local, no need for xgetpwnam */
149                         fprintf (stderr, _("%s: unknown user %s\n"),
150                                  Prog, username);
151                         errors++;
152                 }
153         }
154         return errors;
155 }
156
157 static void failure (void)
158 {
159         fprintf (stderr, _("%s: Permission denied.\n"), Prog);
160         exit (1);
161 }
162
163 /*
164  * gpasswd - administer the /etc/group file
165  *
166  *      -a user         add user to the named group
167  *      -d user         remove user from the named group
168  *      -r              remove password from the named group
169  *      -R              restrict access to the named group
170  *      -A user,...     make list of users the administrative users
171  *      -M user,...     make list of users the group members
172  */
173 int main (int argc, char **argv)
174 {
175         int flag;
176         char *cp;
177         int amroot;
178         int retries;
179         struct group const*gr = NULL;
180         struct group grent;
181         static char pass[BUFSIZ];
182
183 #ifdef  SHADOWGRP
184         struct sgrp const*sg = NULL;
185         struct sgrp sgent;
186         char *admins = NULL;
187 #endif
188         struct passwd *pw = NULL;
189         char *myname;
190         char *user = NULL;
191         char *group = NULL;
192         char *members = NULL;
193
194 #ifdef WITH_AUDIT
195         audit_help_open ();
196 #endif
197
198         sanitize_env ();
199         setlocale (LC_ALL, "");
200         bindtextdomain (PACKAGE, LOCALEDIR);
201         textdomain (PACKAGE);
202
203         /*
204          * Make a note of whether or not this command was invoked by root.
205          * This will be used to bypass certain checks later on. Also, set
206          * the real user ID to match the effective user ID. This will
207          * prevent the invoker from issuing signals which would interfer
208          * with this command.
209          */
210         amroot = getuid () == 0;
211         bywho = getuid ();
212         Prog = Basename (argv[0]);
213
214         OPENLOG ("gpasswd");
215         setbuf (stdout, NULL);
216         setbuf (stderr, NULL);
217
218 #ifdef SHADOWGRP
219         is_shadowgrp = sgr_file_present ();
220 #endif
221         while ((flag = getopt (argc, argv, "a:A:d:gM:rR")) != EOF) {
222                 switch (flag) {
223                 case 'a':       /* add a user */
224                         user = optarg;
225                         /* local, no need for xgetpwnam */
226                         if (!getpwnam (user)) {
227                                 fprintf (stderr,
228                                          _("%s: unknown user %s\n"), Prog,
229                                          user);
230 #ifdef WITH_AUDIT
231                                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
232                                               "adding to group", user, -1, 0);
233 #endif
234                                 exit (1);
235                         }
236                         aflg++;
237                         break;
238 #ifdef SHADOWGRP
239                 case 'A':
240                         if (!amroot) {
241 #ifdef WITH_AUDIT
242                                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
243                                               "Listing administrators", NULL,
244                                               bywho, 0);
245 #endif
246                                 failure ();
247                         }
248                         if (!is_shadowgrp) {
249                                 fprintf (stderr,
250                                          _
251                                          ("%s: shadow group passwords required for -A\n"),
252                                          Prog);
253                                 exit (2);
254                         }
255                         admins = optarg;
256                         if (check_list (admins))
257                                 exit (1);
258                         Aflg++;
259                         break;
260 #endif
261                 case 'd':       /* delete a user */
262                         dflg++;
263                         user = optarg;
264                         break;
265                 case 'g':       /* no-op from normal password */
266                         break;
267                 case 'M':
268                         if (!amroot) {
269 #ifdef WITH_AUDIT
270                                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
271                                               "listing members", NULL, bywho,
272                                               0);
273 #endif
274                                 failure ();
275                         }
276                         members = optarg;
277                         if (check_list (members))
278                                 exit (1);
279                         Mflg++;
280                         break;
281                 case 'r':       /* remove group password */
282                         rflg++;
283                         break;
284                 case 'R':       /* restrict group password */
285                         Rflg++;
286                         break;
287                 default:
288                         usage ();
289                 }
290         }
291
292         /*
293          * Make sure exclusive flags are exclusive
294          */
295
296         if (aflg + dflg + rflg + Rflg + (Aflg || Mflg) > 1)
297                 usage ();
298
299         /*
300          * Determine the name of the user that invoked this command. This
301          * is really hit or miss because there are so many ways that command
302          * can be executed and so many ways to trip up the routines that
303          * report the user name.
304          */
305
306         pw = get_my_pwent ();
307         if (!pw) {
308                 fprintf (stderr, _("Who are you?\n"));
309 #ifdef WITH_AUDIT
310                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "user lookup", NULL,
311                               bywho, 0);
312 #endif
313                 failure ();
314         }
315         myname = xstrdup (pw->pw_name);
316
317         /*
318          * Get the name of the group that is being affected. The group entry
319          * will be completely replicated so it may be modified later on.
320          */
321
322         if (!(group = argv[optind]))
323                 usage ();
324
325         if (!gr_open (O_RDONLY)) {
326                 fprintf (stderr, _("%s: can't open file\n"), Prog);
327                 SYSLOG ((LOG_WARN, "cannot open /etc/group"));
328 #ifdef WITH_AUDIT
329                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "opening /etc/group",
330                               group, -1, 0);
331 #endif
332                 exit (1);
333         }
334
335         if (!(gr = gr_locate (group))) {
336                 fprintf (stderr, _("unknown group: %s\n"), group);
337 #ifdef WITH_AUDIT
338                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "group lookup", group,
339                               -1, 0);
340 #endif
341                 failure ();
342         }
343         grent = *gr;
344         grent.gr_name = xstrdup (gr->gr_name);
345         grent.gr_passwd = xstrdup (gr->gr_passwd);
346
347         grent.gr_mem = dup_list (gr->gr_mem);
348         if (!gr_close ()) {
349                 fprintf (stderr, _("%s: can't close file\n"), Prog);
350                 SYSLOG ((LOG_WARN, "cannot close /etc/group"));
351 #ifdef WITH_AUDIT
352                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
353                               "closing /etc/group", group, -1, 0);
354 #endif
355                 exit (1);
356         }
357 #ifdef  SHADOWGRP
358         if (!sgr_open (O_RDONLY)) {
359                 fprintf (stderr, _("%s: can't open shadow file\n"), Prog);
360                 SYSLOG ((LOG_WARN, "cannot open /etc/gshadow"));
361 #ifdef WITH_AUDIT
362                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
363                               "opening /etc/gshadow", group, -1, 0);
364 #endif
365                 exit (1);
366         }
367         if ((sg = sgr_locate (group))) {
368                 sgent = *sg;
369                 sgent.sg_name = xstrdup (sg->sg_name);
370                 sgent.sg_passwd = xstrdup (sg->sg_passwd);
371
372                 sgent.sg_mem = dup_list (sg->sg_mem);
373                 sgent.sg_adm = dup_list (sg->sg_adm);
374         } else {
375                 sgent.sg_name = xstrdup (group);
376                 sgent.sg_passwd = grent.gr_passwd;
377                 grent.gr_passwd = "!";  /* XXX warning: const */
378
379                 sgent.sg_mem = dup_list (grent.gr_mem);
380
381                 sgent.sg_adm = (char **) xmalloc (sizeof (char *) * 2);
382 #ifdef FIRST_MEMBER_IS_ADMIN
383                 if (sgent.sg_mem[0]) {
384                         sgent.sg_adm[0] = xstrdup (sgent.sg_mem[0]);
385                         sgent.sg_adm[1] = 0;
386                 } else
387 #endif
388                         sgent.sg_adm[0] = 0;
389
390                 sg = &sgent;
391         }
392         if (!sgr_close ()) {
393                 fprintf (stderr, _("%s: can't close shadow file\n"), Prog);
394                 SYSLOG ((LOG_WARN, "cannot close /etc/gshadow"));
395 #ifdef WITH_AUDIT
396                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
397                               "closing /etc/gshadow", group, -1, 0);
398 #endif
399                 exit (1);
400         }
401
402         /*
403          * The policy here for changing a group is that 1) you must be root
404          * or 2). you must be listed as an administrative member. 
405          * Administrative members can do anything to a group that the root
406          * user can.
407          */
408         if (!amroot && !is_on_list (sgent.sg_adm, myname)) {
409 #ifdef WITH_AUDIT
410                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "modify group", group,
411                               -1, 0);
412 #endif
413                 failure ();
414         }
415 #else                           /* ! SHADOWGRP */
416
417 #ifdef FIRST_MEMBER_IS_ADMIN
418         /*
419          * The policy here for changing a group is that 1) you must bes root
420          * or 2) you must be the first listed member of the group. The
421          * first listed member of a group can do anything to that group that
422          * the root user can. The rationale for this hack is that the FIRST
423          * user is probably the most important user in this entire group.
424          */
425         if (!amroot) {
426                 if (grent.gr_mem[0] == (char *) 0) {
427 #ifdef WITH_AUDIT
428                         audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
429                                       "modifying group", group, -1, 0);
430 #endif
431                         failure ();
432                 }
433
434                 if (strcmp (grent.gr_mem[0], myname) != 0) {
435 #ifdef WITH_AUDIT
436                         audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
437                                       "modifying group", myname, -1, 0);
438 #endif
439                         failure ();
440                 }
441         }
442 #else
443         /*
444          * This feature enabled by default could be a security problem when
445          * installed on existing systems where the first group member might
446          * be just a normal user.  --marekm
447          */
448         if (!amroot) {
449 #ifdef WITH_AUDIT
450                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "modifying group",
451                               group, -1, 0);
452 #endif
453                 failure ();
454         }
455 #endif
456
457 #endif                          /* SHADOWGRP */
458
459         /*
460          * Removing a password is straight forward. Just set the password
461          * field to a "".
462          */
463         if (rflg) {
464                 grent.gr_passwd = "";   /* XXX warning: const */
465 #ifdef  SHADOWGRP
466                 sgent.sg_passwd = "";   /* XXX warning: const */
467 #endif
468 #ifdef WITH_AUDIT
469                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
470                               "deleting group password", group, -1, 1);
471 #endif
472                 SYSLOG ((LOG_INFO, "remove password from group %s by %s",
473                          group, myname));
474                 goto output;
475         } else if (Rflg) {
476                 /*
477                  * Same thing for restricting the group. Set the password
478                  * field to "!".
479                  */
480                 grent.gr_passwd = "!";  /* XXX warning: const */
481 #ifdef  SHADOWGRP
482                 sgent.sg_passwd = "!";  /* XXX warning: const */
483 #endif
484 #ifdef WITH_AUDIT
485                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
486                               "restrict access to group", group, -1, 1);
487 #endif
488                 SYSLOG ((LOG_INFO, "restrict access to group %s by %s",
489                          group, myname));
490                 goto output;
491         }
492
493         /*
494          * Adding a member to a member list is pretty straightforward as
495          * well. Call the appropriate routine and split.
496          */
497         if (aflg) {
498                 printf (_("Adding user %s to group %s\n"), user, group);
499                 grent.gr_mem = add_list (grent.gr_mem, user);
500 #ifdef  SHADOWGRP
501                 sgent.sg_mem = add_list (sgent.sg_mem, user);
502 #endif
503 #ifdef WITH_AUDIT
504                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "adding group member",
505                               user, -1, 1);
506 #endif
507                 SYSLOG ((LOG_INFO, "add member %s to group %s by %s", user,
508                          group, myname));
509                 goto output;
510         }
511
512         /*
513          * Removing a member from the member list is the same deal as adding
514          * one, except the routine is different.
515          */
516         if (dflg) {
517                 int removed = 0;
518
519                 printf (_("Removing user %s from group %s\n"), user, group);
520
521                 if (is_on_list (grent.gr_mem, user)) {
522                         removed = 1;
523                         grent.gr_mem = del_list (grent.gr_mem, user);
524                 }
525 #ifdef  SHADOWGRP
526                 if (is_on_list (sgent.sg_mem, user)) {
527                         removed = 1;
528                         sgent.sg_mem = del_list (sgent.sg_mem, user);
529                 }
530 #endif
531                 if (!removed) {
532                         fprintf (stderr, _("%s: unknown member %s\n"),
533                                  Prog, user);
534 #ifdef WITH_AUDIT
535                         audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
536                                       "deleting member", user, -1, 0);
537 #endif
538                         exit (1);
539                 }
540 #ifdef WITH_AUDIT
541                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "deleting member",
542                               user, -1, 1);
543 #endif
544                 SYSLOG ((LOG_INFO, "remove member %s from group %s by %s",
545                          user, group, myname));
546                 goto output;
547         }
548 #ifdef  SHADOWGRP
549         /*
550          * Replacing the entire list of administators is simple. Check the
551          * list to make sure everyone is a real user. Then slap the new list
552          * in place.
553          */
554         if (Aflg) {
555 #ifdef WITH_AUDIT
556                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "setting group admin",
557                               group, -1, 1);
558 #endif
559                 SYSLOG ((LOG_INFO, "set administrators of %s to %s",
560                          group, admins));
561                 sgent.sg_adm = comma_to_list (admins);
562                 if (!Mflg)
563                         goto output;
564         }
565 #endif
566
567         /*
568          * Replacing the entire list of members is simple. Check the list to
569          * make sure everyone is a real user. Then slap the new list in
570          * place.
571          */
572         if (Mflg) {
573 #ifdef WITH_AUDIT
574                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
575                               "setting group members", group, -1, 1);
576 #endif
577                 SYSLOG ((LOG_INFO, "set members of %s to %s", group, members));
578 #ifdef SHADOWGRP
579                 sgent.sg_mem = comma_to_list (members);
580 #endif
581                 grent.gr_mem = comma_to_list (members);
582                 goto output;
583         }
584
585         /*
586          * If the password is being changed, the input and output must both
587          * be a tty. The typical keyboard signals are caught so the termio
588          * modes can be restored.
589          */
590         if (!isatty (0) || !isatty (1)) {
591                 fprintf (stderr, _("%s: Not a tty\n"), Prog);
592 #ifdef WITH_AUDIT
593                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "changing password",
594                               group, -1, 0);
595 #endif
596                 exit (1);
597         }
598
599         catch_signals (0);      /* save tty modes */
600
601         signal (SIGHUP, catch_signals);
602         signal (SIGINT, catch_signals);
603         signal (SIGQUIT, catch_signals);
604         signal (SIGTERM, catch_signals);
605 #ifdef  SIGTSTP
606         signal (SIGTSTP, catch_signals);
607 #endif
608
609         /*
610          * A new password is to be entered and it must be encrypted, etc.
611          * The password will be prompted for twice, and both entries must be
612          * identical. There is no need to validate the old password since
613          * the invoker is either the group owner, or root.
614          */
615         printf (_("Changing the password for group %s\n"), group);
616
617         for (retries = 0; retries < RETRIES; retries++) {
618                 if (!(cp = getpass (_("New Password: "))))
619                         exit (1);
620
621                 STRFCPY (pass, cp);
622                 strzero (cp);
623                 if (!(cp = getpass (_("Re-enter new password: "))))
624                         exit (1);
625
626                 if (strcmp (pass, cp) == 0) {
627                         strzero (cp);
628                         break;
629                 }
630
631                 strzero (cp);
632                 memzero (pass, sizeof pass);
633
634                 if (retries + 1 < RETRIES) {
635                         puts (_("They don't match; try again"));
636 #ifdef WITH_AUDIT
637                         audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
638                                       "changing password", group, -1, 0);
639 #endif
640                 }
641         }
642
643         if (retries == RETRIES) {
644                 fprintf (stderr, _("%s: Try again later\n"), Prog);
645                 exit (1);
646         }
647
648         cp = pw_encrypt (pass, crypt_make_salt (NULL, NULL));
649         memzero (pass, sizeof pass);
650 #ifdef SHADOWGRP
651         if (is_shadowgrp)
652                 sgent.sg_passwd = cp;
653         else
654 #endif
655                 grent.gr_passwd = cp;
656 #ifdef WITH_AUDIT
657         audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "changing password", group,
658                       -1, 1);
659 #endif
660         SYSLOG ((LOG_INFO, "change the password for group %s by %s", group,
661                  myname));
662
663         /*
664          * This is the common arrival point to output the new group file.
665          * The freshly crafted entry is in allocated space. The group file
666          * will be locked and opened for writing. The new entry will be
667          * output, etc.
668          */
669       output:
670         if (setuid (0)) {
671                 fprintf (stderr, _("Cannot change ID to root.\n"));
672                 SYSLOG ((LOG_ERR, "can't setuid(0)"));
673 #ifdef WITH_AUDIT
674                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "changing id to root",
675                               group, -1, 0);
676 #endif
677                 closelog ();
678                 exit (1);
679         }
680         pwd_init ();
681
682         if (!gr_lock ()) {
683                 fprintf (stderr, _("%s: can't get lock\n"), Prog);
684                 SYSLOG ((LOG_WARN, "failed to get lock for /etc/group"));
685 #ifdef WITH_AUDIT
686                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "locking /etc/group",
687                               group, -1, 0);
688 #endif
689                 exit (1);
690         }
691 #ifdef  SHADOWGRP
692         if (is_shadowgrp && !sgr_lock ()) {
693                 fprintf (stderr, _("%s: can't get shadow lock\n"), Prog);
694                 SYSLOG ((LOG_WARN, "failed to get lock for /etc/gshadow"));
695 #ifdef WITH_AUDIT
696                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
697                               "locking /etc/gshadow", group, -1, 0);
698 #endif
699                 exit (1);
700         }
701 #endif
702         if (!gr_open (O_RDWR)) {
703                 fprintf (stderr, _("%s: can't open file\n"), Prog);
704                 SYSLOG ((LOG_WARN, "cannot open /etc/group"));
705 #ifdef WITH_AUDIT
706                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "opening /etc/group",
707                               group, -1, 0);
708 #endif
709                 exit (1);
710         }
711 #ifdef  SHADOWGRP
712         if (is_shadowgrp && !sgr_open (O_RDWR)) {
713                 fprintf (stderr, _("%s: can't open shadow file\n"), Prog);
714                 SYSLOG ((LOG_WARN, "cannot open /etc/gshadow"));
715 #ifdef WITH_AUDIT
716                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
717                               "opening /etc/gshadow", group, -1, 0);
718 #endif
719                 exit (1);
720         }
721 #endif
722         if (!gr_update (&grent)) {
723                 fprintf (stderr, _("%s: can't update entry\n"), Prog);
724                 SYSLOG ((LOG_WARN, "cannot update /etc/group"));
725 #ifdef WITH_AUDIT
726                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "updating /etc/group",
727                               group, -1, 0);
728 #endif
729                 exit (1);
730         }
731 #ifdef  SHADOWGRP
732         if (is_shadowgrp && !sgr_update (&sgent)) {
733                 fprintf (stderr, _("%s: can't update shadow entry\n"), Prog);
734                 SYSLOG ((LOG_WARN, "cannot update /etc/gshadow"));
735 #ifdef WITH_AUDIT
736                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
737                               "updating /etc/gshadow", group, -1, 0);
738 #endif
739                 exit (1);
740         }
741 #endif
742         if (!gr_close ()) {
743                 fprintf (stderr, _("%s: can't re-write file\n"), Prog);
744                 SYSLOG ((LOG_WARN, "cannot re-write /etc/group"));
745 #ifdef WITH_AUDIT
746                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
747                               "rewriting /etc/group", group, -1, 0);
748 #endif
749                 exit (1);
750         }
751 #ifdef  SHADOWGRP
752         if (is_shadowgrp && !sgr_close ()) {
753                 fprintf (stderr, _("%s: can't re-write shadow file\n"), Prog);
754                 SYSLOG ((LOG_WARN, "cannot re-write /etc/gshadow"));
755 #ifdef WITH_AUDIT
756                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
757                               "rewriting /etc/gshadow", group, -1, 0);
758 #endif
759                 exit (1);
760         }
761         if (is_shadowgrp)
762                 sgr_unlock ();
763 #endif
764         if (!gr_unlock ()) {
765                 fprintf (stderr, _("%s: can't unlock file\n"), Prog);
766 #ifdef WITH_AUDIT
767                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
768                               "unlocking group file", group, -1, 0);
769 #endif
770                 exit (1);
771         }
772
773         nscd_flush_cache ("group");
774
775         exit (E_SUCCESS);
776 }