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