]> granicus.if.org Git - shadow/blob - src/grpck.c
Re-indent.
[shadow] / src / grpck.c
1 /*
2  * Copyright (c) 1992 - 1994, Julianne Frances Haugh
3  * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4  * Copyright (c) 2001       , Michał Moskal
5  * Copyright (c) 2001 - 2006, Tomasz Kłoczko
6  * Copyright (c) 2007 - 2011, Nicolas François
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the copyright holders or contributors may not be used to
18  *    endorse or promote products derived from this software without
19  *    specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
25  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <config.h>
35
36 #ident "$Id$"
37
38 #include <fcntl.h>
39 #include <grp.h>
40 #include <pwd.h>
41 #include <stdio.h>
42 #include <getopt.h>
43 #include "chkname.h"
44 #include "commonio.h"
45 #include "defines.h"
46 #include "groupio.h"
47 #include "nscd.h"
48 #include "prototypes.h"
49
50 #ifdef SHADOWGRP
51 #include "sgroupio.h"
52 #endif
53
54 /*
55  * Exit codes
56  */
57 /*@-exitarg@*/
58 #define E_OKAY          0
59 #define E_SUCCESS       0
60 #define E_USAGE         1
61 #define E_BAD_ENTRY     2
62 #define E_CANT_OPEN     3
63 #define E_CANT_LOCK     4
64 #define E_CANT_UPDATE   5
65
66 /*
67  * Global variables
68  */
69 const char *Prog;
70
71 static const char *grp_file = GROUP_FILE;
72 static bool use_system_grp_file = true;
73
74 #ifdef  SHADOWGRP
75 static const char *sgr_file = SGROUP_FILE;
76 static bool use_system_sgr_file = true;
77 static bool is_shadow = false;
78 static bool sgr_locked = false;
79 #endif
80 static bool gr_locked = false;
81 /* Options */
82 static bool read_only = false;
83 static bool sort_mode = false;
84
85 /* local function prototypes */
86 static void fail_exit (int status);
87 static /*@noreturn@*/void usage (int status);
88 static void delete_member (char **, const char *);
89 static void process_flags (int argc, char **argv);
90 static void open_files (void);
91 static void close_files (bool changed);
92 static int check_members (const char *groupname,
93                           char **members,
94                           const char *fmt_info,
95                           const char *fmt_prompt,
96                           const char *fmt_syslog,
97                           int *errors);
98 static void check_grp_file (int *errors, bool *changed);
99 #ifdef SHADOWGRP
100 static void compare_members_lists (const char *groupname,
101                                    char **members,
102                                    char **other_members,
103                                    const char *file,
104                                    const char *other_file);
105 static void check_sgr_file (int *errors, bool *changed);
106 #endif
107
108 /*
109  * fail_exit - exit with an error code after unlocking files
110  */
111 static void fail_exit (int status)
112 {
113         if (gr_locked) {
114                 if (gr_unlock () == 0) {
115                         fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
116                         SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
117                         /* continue */
118                 }
119         }
120
121 #ifdef  SHADOWGRP
122         if (sgr_locked) {
123                 if (sgr_unlock () == 0) {
124                         fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
125                         SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
126                         /* continue */
127                 }
128         }
129 #endif
130
131         closelog ();
132
133         exit (status);
134 }
135
136 /*
137  * usage - print syntax message and exit
138  */
139 static /*@noreturn@*/void usage (int status)
140 {
141         FILE *usageout = (E_SUCCESS != status) ? stderr : stdout;
142 #ifdef  SHADOWGRP
143         (void) fprintf (usageout,
144                         _("Usage: %s [options] [group [gshadow]]\n"
145                           "\n"
146                           "Options:\n"),
147                         Prog);
148 #else                           /* !SHADOWGRP */
149         (void) fprintf (usageout,
150                         _("Usage: %s [options] [group]\n"
151                           "\n"
152                           "Options:\n"),
153                         Prog);
154 #endif                          /* !SHADOWGRP */
155         (void) fputs (_("  -h, --help                    display this help message and exit\n"), usageout);
156         (void) fputs (_("  -r, --read-only               display errors and warnings\n"
157                         "                                but do not change files\n"), usageout);
158         (void) fputs (_("  -s, --sort                    sort entries by UID\n"), usageout);
159         (void) fputs ("\n", usageout);
160         exit (status);
161 }
162
163 /*
164  * delete_member - delete an entry in a list of members
165  *
166  * It only deletes the first entry with the given name.
167  * The member is defined by its address, no string comparison are
168  * performed.
169  */
170 static void delete_member (char **list, const char *member)
171 {
172         int i;
173
174         for (i = 0; NULL != list[i]; i++) {
175                 if (list[i] == member) {
176                         break;
177                 }
178         }
179
180         for (; NULL != list[i]; i++) {
181                 list[i] = list[i + 1];
182         }
183 }
184
185 /*
186  * process_flags - parse the command line options
187  *
188  *      It will not return if an error is encountered.
189  */
190 static void process_flags (int argc, char **argv)
191 {
192         int c;
193         static struct option long_options[] = {
194                 {"help",      no_argument, NULL, 'h'},
195                 {"quiet",     no_argument, NULL, 'q'},
196                 {"read-only", no_argument, NULL, 'r'},
197                 {"sort",      no_argument, NULL, 's'},
198                 {NULL, 0, NULL, '\0'}
199         };
200
201         /*
202          * Parse the command line arguments
203          */
204         while ((c = getopt_long (argc, argv, "hqrs",
205                                  long_options, NULL)) != -1) {
206                 switch (c) {
207                 case 'h':
208                         usage (E_SUCCESS);
209                         /*@notreached@*/break;
210                 case 'q':
211                         /* quiet - ignored for now */
212                         break;
213                 case 'r':
214                         read_only = true;
215                         break;
216                 case 's':
217                         sort_mode = true;
218                         break;
219                 default:
220                         usage (E_USAGE);
221                 }
222         }
223
224         if (sort_mode && read_only) {
225                 fprintf (stderr, _("%s: -s and -r are incompatible\n"), Prog);
226                 exit (E_USAGE);
227         }
228
229         /*
230          * Make certain we have the right number of arguments
231          */
232 #ifdef  SHADOWGRP
233         if (argc > (optind + 2))
234 #else
235         if (argc > (optind + 1))
236 #endif
237         {
238                 usage (E_USAGE);
239         }
240
241         /*
242          * If there are two left over filenames, use those as the group and
243          * group password filenames.
244          */
245         if (optind != argc) {
246                 grp_file = argv[optind];
247                 gr_setdbname (grp_file);
248                 use_system_grp_file = false;
249         }
250 #ifdef  SHADOWGRP
251         if ((optind + 2) == argc) {
252                 sgr_file = argv[optind + 1];
253                 sgr_setdbname (sgr_file);
254                 is_shadow = true;
255                 use_system_sgr_file = false;
256         } else if (optind == argc) {
257                 is_shadow = sgr_file_present ();
258         }
259 #endif
260 }
261
262 /*
263  * open_files - open the shadow database
264  *
265  *      In read-only mode, the databases are not locked and are opened
266  *      only for reading.
267  */
268 static void open_files (void)
269 {
270         /*
271          * Lock the files if we aren't in "read-only" mode
272          */
273         if (!read_only) {
274                 if (gr_lock () == 0) {
275                         fprintf (stderr,
276                                  _("%s: cannot lock %s; try again later.\n"),
277                                  Prog, grp_file);
278                         fail_exit (E_CANT_LOCK);
279                 }
280                 gr_locked = true;
281 #ifdef  SHADOWGRP
282                 if (is_shadow) {
283                         if (sgr_lock () == 0) {
284                                 fprintf (stderr,
285                                          _("%s: cannot lock %s; try again later.\n"),
286                                          Prog, sgr_file);
287                                 fail_exit (E_CANT_LOCK);
288                         }
289                         sgr_locked = true;
290                 }
291 #endif
292         }
293
294         /*
295          * Open the files. Use O_RDONLY if we are in read_only mode,
296          * O_RDWR otherwise.
297          */
298         if (gr_open (read_only ? O_RDONLY : O_RDWR) == 0) {
299                 fprintf (stderr, _("%s: cannot open %s\n"), Prog,
300                          grp_file);
301                 if (use_system_grp_file) {
302                         SYSLOG ((LOG_WARN, "cannot open %s", grp_file));
303                 }
304                 fail_exit (E_CANT_OPEN);
305         }
306 #ifdef  SHADOWGRP
307         if (is_shadow && (sgr_open (read_only ? O_RDONLY : O_RDWR) == 0)) {
308                 fprintf (stderr, _("%s: cannot open %s\n"), Prog,
309                          sgr_file);
310                 if (use_system_sgr_file) {
311                         SYSLOG ((LOG_WARN, "cannot open %s", sgr_file));
312                 }
313                 fail_exit (E_CANT_OPEN);
314         }
315 #endif
316 }
317
318 /*
319  * close_files - close and unlock the group/gshadow databases
320  *
321  *      If changed is not set, the databases are not closed, and no
322  *      changes are committed in the databases. The databases are
323  *      unlocked anyway.
324  */
325 static void close_files (bool changed)
326 {
327         /*
328          * All done. If there were no change we can just abandon any
329          * changes to the files.
330          */
331         if (changed) {
332                 if (gr_close () == 0) {
333                         fprintf (stderr, _("%s: failure while writing changes to %s\n"),
334                                  Prog, grp_file);
335                         fail_exit (E_CANT_UPDATE);
336                 }
337 #ifdef  SHADOWGRP
338                 if (is_shadow && (sgr_close () == 0)) {
339                         fprintf (stderr, _("%s: failure while writing changes to %s\n"),
340                                  Prog, sgr_file);
341                         fail_exit (E_CANT_UPDATE);
342                 }
343 #endif
344         }
345
346         /*
347          * Don't be anti-social - unlock the files when you're done.
348          */
349 #ifdef  SHADOWGRP
350         if (sgr_locked) {
351                 if (sgr_unlock () == 0) {
352                         fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
353                         SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
354                         /* continue */
355                 }
356                 sgr_locked = false;
357         }
358 #endif
359         if (gr_locked) {
360                 if (gr_unlock () == 0) {
361                         fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
362                         SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
363                         /* continue */
364                 }
365                 gr_locked = false;
366         }
367 }
368
369 /*
370  * check_members - check that every members of a group exist
371  *
372  *      If an error is detected, *errors is incremented.
373  *
374  *      The user will be prompted for the removal of the non-existent
375  *      user.
376  *
377  *      If any changes are performed, the return value will be 1,
378  *      otherwise check_members() returns 0.
379  *
380  *      fmt_info, fmt_prompt, and fmt_syslog are used for logging.
381  *        * fmt_info must contain two string flags (%s): for the group's
382  *          name and the missing member.
383  *        * fmt_prompt must contain one string flags (%s): the missing
384  *          member.
385  *        * fmt_syslog must contain two string flags (%s): for the
386  *          group's name and the missing member.
387  */
388 static int check_members (const char *groupname,
389                           char **members,
390                           const char *fmt_info,
391                           const char *fmt_prompt,
392                           const char *fmt_syslog,
393                           int *errors)
394 {
395         int i;
396         int members_changed = 0;
397
398         /*
399          * Make sure each member exists
400          */
401         for (i = 0; NULL != members[i]; i++) {
402                 /* local, no need for xgetpwnam */
403                 if (getpwnam (members[i]) != NULL) {
404                         continue;
405                 }
406                 /*
407                  * Can't find this user. Remove them
408                  * from the list.
409                  */
410                 *errors += 1;
411                 printf (fmt_info, groupname, members[i]);
412                 printf (fmt_prompt, members[i]);
413
414                 if (!yes_or_no (read_only)) {
415                         continue;
416                 }
417
418                 SYSLOG ((LOG_INFO, fmt_syslog, members[i], groupname));
419                 members_changed = 1;
420                 delete_member (members, members[i]);
421
422                 /* Rewind in case of removal */
423                 i--;
424         }
425
426         return members_changed;
427 }
428
429 #ifdef SHADOWGRP
430 /*
431  * compare_members_lists - make sure the list of members is contained in
432  *                         another list.
433  *
434  *      compare_members_lists() checks that all the members of members are
435  *      also in other_members.
436  *      file and other_file are used for logging.
437  *
438  *      TODO: No changes are performed on the lists.
439  */
440 static void compare_members_lists (const char *groupname,
441                                    char **members,
442                                    char **other_members,
443                                    const char *file,
444                                    const char *other_file)
445 {
446         char **pmem, **other_pmem;
447
448         for (pmem = members; NULL != *pmem; pmem++) {
449                 for (other_pmem = other_members; NULL != *other_pmem; other_pmem++) {
450                         if (strcmp (*pmem, *other_pmem) == 0) {
451                                 break;
452                         }
453                 }
454                 if (*other_pmem == NULL) {
455                         printf
456                             ("'%s' is a member of the '%s' group in %s but not in %s\n",
457                              *pmem, groupname, file, other_file);
458                 }
459         }
460 }
461 #endif                          /* SHADOWGRP */
462
463 /*
464  * check_grp_file - check the content of the group file
465  */
466 static void check_grp_file (int *errors, bool *changed)
467 {
468         struct commonio_entry *gre, *tgre;
469         struct group *grp;
470 #ifdef SHADOWGRP
471         struct sgrp *sgr;
472 #endif
473
474         /*
475          * Loop through the entire group file.
476          */
477         for (gre = __gr_get_head (); NULL != gre; gre = gre->next) {
478                 /*
479                  * Skip all NIS entries.
480                  */
481
482                 if ((gre->line[0] == '+') || (gre->line[0] == '-')) {
483                         continue;
484                 }
485
486                 /*
487                  * Start with the entries that are completely corrupt. They
488                  * have no (struct group) entry because they couldn't be
489                  * parsed properly.
490                  */
491                 if (NULL == gre->eptr) {
492
493                         /*
494                          * Tell the user this entire line is bogus and ask
495                          * them to delete it.
496                          */
497                         (void) puts (_("invalid group file entry"));
498                         printf (_("delete line '%s'? "), gre->line);
499                         *errors += 1;
500
501                         /*
502                          * prompt the user to delete the entry or not
503                          */
504                         if (!yes_or_no (read_only)) {
505                                 continue;
506                         }
507
508                         /*
509                          * All group file deletions wind up here. This code
510                          * removes the current entry from the linked list.
511                          * When done, it skips back to the top of the loop
512                          * to try out the next list element.
513                          */
514                       delete_gr:
515                         SYSLOG ((LOG_INFO, "delete group line '%s'",
516                                  gre->line));
517                         *changed = true;
518
519                         __gr_del_entry (gre);
520                         continue;
521                 }
522
523                 /*
524                  * Group structure is good, start using it.
525                  */
526                 grp = gre->eptr;
527
528                 /*
529                  * Make sure this entry has a unique name.
530                  */
531                 for (tgre = __gr_get_head (); NULL != tgre; tgre = tgre->next) {
532
533                         const struct group *ent = tgre->eptr;
534
535                         /*
536                          * Don't check this entry
537                          */
538                         if (tgre == gre) {
539                                 continue;
540                         }
541
542                         /*
543                          * Don't check invalid entries.
544                          */
545                         if (NULL == ent) {
546                                 continue;
547                         }
548
549                         if (strcmp (grp->gr_name, ent->gr_name) != 0) {
550                                 continue;
551                         }
552
553                         /*
554                          * Tell the user this entry is a duplicate of
555                          * another and ask them to delete it.
556                          */
557                         (void) puts (_("duplicate group entry"));
558                         printf (_("delete line '%s'? "), gre->line);
559                         *errors += 1;
560
561                         /*
562                          * prompt the user to delete the entry or not
563                          */
564                         if (yes_or_no (read_only)) {
565                                 goto delete_gr;
566                         }
567                 }
568
569                 /*
570                  * Check for invalid group names.  --marekm
571                  */
572                 if (!is_valid_group_name (grp->gr_name)) {
573                         *errors += 1;
574                         printf (_("invalid group name '%s'\n"), grp->gr_name);
575                 }
576
577                 /*
578                  * Check for invalid group ID.
579                  */
580                 if (grp->gr_gid == (gid_t)-1) {
581                         printf (_("invalid group ID '%lu'\n"), (long unsigned int)grp->gr_gid);
582                         *errors += 1;
583                 }
584
585                 /*
586                  * Workaround for a NYS libc 5.3.12 bug on RedHat 4.2 -
587                  * groups with no members are returned as groups with one
588                  * member "", causing grpck to fail.  --marekm
589                  */
590                 if (   (NULL != grp->gr_mem[0])
591                     && (NULL == grp->gr_mem[1])
592                     && ('\0' == grp->gr_mem[0][0])) {
593                         grp->gr_mem[0] = NULL;
594                 }
595
596                 if (check_members (grp->gr_name, grp->gr_mem,
597                                    _("group %s: no user %s\n"),
598                                    _("delete member '%s'? "),
599                                    "delete member '%s' from group '%s'",
600                                    errors) == 1) {
601                         *changed = true;
602                         gre->changed = true;
603                         __gr_set_changed ();
604                 }
605
606 #ifdef  SHADOWGRP
607                 /*
608                  * Make sure this entry exists in the /etc/gshadow file.
609                  */
610
611                 if (is_shadow) {
612                         sgr = (struct sgrp *) sgr_locate (grp->gr_name);
613                         if (sgr == NULL) {
614                                 printf (_("no matching group file entry in %s\n"),
615                                         sgr_file);
616                                 printf (_("add group '%s' in %s? "),
617                                         grp->gr_name, sgr_file);
618                                 *errors += 1;
619                                 if (yes_or_no (read_only)) {
620                                         struct sgrp sg;
621                                         struct group gr;
622                                         static char *empty = NULL;
623
624                                         sg.sg_name = grp->gr_name;
625                                         sg.sg_passwd = grp->gr_passwd;
626                                         sg.sg_adm = &empty;
627                                         sg.sg_mem = grp->gr_mem;
628                                         SYSLOG ((LOG_INFO,
629                                                  "add group '%s' to '%s'",
630                                                  grp->gr_name, sgr_file));
631                                         *changed = true;
632
633                                         if (sgr_update (&sg) == 0) {
634                                                 fprintf (stderr,
635                                                          _("%s: failed to prepare the new %s entry '%s'\n"),
636                                                          Prog, sgr_dbname (), sg.sg_name);
637                                                 fail_exit (E_CANT_UPDATE);
638                                         }
639                                         /* remove password from /etc/group */
640                                         gr = *grp;
641                                         gr.gr_passwd = SHADOW_PASSWD_STRING;    /* XXX warning: const */
642                                         if (gr_update (&gr) == 0) {
643                                                 fprintf (stderr,
644                                                          _("%s: failed to prepare the new %s entry '%s'\n"),
645                                                          Prog, gr_dbname (), gr.gr_name);
646                                                 fail_exit (E_CANT_UPDATE);
647                                         }
648                                 }
649                         } else {
650                                 /**
651                                  * Verify that all the members defined in /etc/group are also
652                                  * present in /etc/gshadow.
653                                  */
654                                 compare_members_lists (grp->gr_name,
655                                                        grp->gr_mem, sgr->sg_mem,
656                                                        grp_file, sgr_file);
657
658                                 /* The group entry has a gshadow counterpart.
659                                  * Make sure no passwords are in group.
660                                  */
661                                 if (strcmp (grp->gr_passwd, SHADOW_PASSWD_STRING) != 0) {
662                                         printf (_("group %s has an entry in %s, but its password field in %s is not set to 'x'\n"),
663                                                 grp->gr_name, sgr_file, grp_file);
664                                         *errors += 1;
665                                 }
666                         }
667                 }
668 #endif
669
670         }
671 }
672
673 #ifdef SHADOWGRP
674 /*
675  * check_sgr_file - check the content of the shadowed group file (gshadow)
676  */
677 static void check_sgr_file (int *errors, bool *changed)
678 {
679         struct group *grp;
680         struct commonio_entry *sge, *tsge;
681         struct sgrp *sgr;
682
683         /*
684          * Loop through the entire shadow group file.
685          */
686         for (sge = __sgr_get_head (); NULL != sge; sge = sge->next) {
687
688                 /*
689                  * Start with the entries that are completely corrupt. They
690                  * have no (struct sgrp) entry because they couldn't be
691                  * parsed properly.
692                  */
693                 if (NULL == sge->eptr) {
694
695                         /*
696                          * Tell the user this entire line is bogus and ask
697                          * them to delete it.
698                          */
699                         (void) puts (_("invalid shadow group file entry"));
700                         printf (_("delete line '%s'? "), sge->line);
701                         *errors += 1;
702
703                         /*
704                          * prompt the user to delete the entry or not
705                          */
706                         if (!yes_or_no (read_only)) {
707                                 continue;
708                         }
709
710                         /*
711                          * All shadow group file deletions wind up here. 
712                          * This code removes the current entry from the
713                          * linked list. When done, it skips back to the top
714                          * of the loop to try out the next list element.
715                          */
716                       delete_sg:
717                         SYSLOG ((LOG_INFO, "delete shadow line '%s'",
718                                  sge->line));
719                         *changed = true;
720
721                         __sgr_del_entry (sge);
722                         continue;
723                 }
724
725                 /*
726                  * Shadow group structure is good, start using it.
727                  */
728                 sgr = sge->eptr;
729
730                 /*
731                  * Make sure this entry has a unique name.
732                  */
733                 for (tsge = __sgr_get_head (); NULL != tsge; tsge = tsge->next) {
734
735                         const struct sgrp *ent = tsge->eptr;
736
737                         /*
738                          * Don't check this entry
739                          */
740                         if (tsge == sge) {
741                                 continue;
742                         }
743
744                         /*
745                          * Don't check invalid entries.
746                          */
747                         if (NULL == ent) {
748                                 continue;
749                         }
750
751                         if (strcmp (sgr->sg_name, ent->sg_name) != 0) {
752                                 continue;
753                         }
754
755                         /*
756                          * Tell the user this entry is a duplicate of
757                          * another and ask them to delete it.
758                          */
759                         (void) puts (_("duplicate shadow group entry"));
760                         printf (_("delete line '%s'? "), sge->line);
761                         *errors += 1;
762
763                         /*
764                          * prompt the user to delete the entry or not
765                          */
766                         if (yes_or_no (read_only)) {
767                                 goto delete_sg;
768                         }
769                 }
770
771                 /*
772                  * Make sure this entry exists in the /etc/group file.
773                  */
774                 grp = (struct group *) gr_locate (sgr->sg_name);
775                 if (grp == NULL) {
776                         printf (_("no matching group file entry in %s\n"),
777                                 grp_file);
778                         printf (_("delete line '%s'? "), sge->line);
779                         *errors += 1;
780                         if (yes_or_no (read_only)) {
781                                 goto delete_sg;
782                         }
783                 } else {
784                         /**
785                          * Verify that the all members defined in /etc/gshadow are also
786                          * present in /etc/group.
787                          */
788                         compare_members_lists (sgr->sg_name,
789                                                sgr->sg_mem, grp->gr_mem,
790                                                sgr_file, grp_file);
791                 }
792
793                 /*
794                  * Make sure each administrator exists
795                  */
796                 if (check_members (sgr->sg_name, sgr->sg_adm,
797                                    _("shadow group %s: no administrative user %s\n"),
798                                    _("delete administrative member '%s'? "),
799                                    "delete admin '%s' from shadow group '%s'",
800                                    errors) == 1) {
801                         *changed = true;
802                         sge->changed = true;
803                         __sgr_set_changed ();
804                 }
805
806                 /*
807                  * Make sure each member exists
808                  */
809                 if (check_members (sgr->sg_name, sgr->sg_mem,
810                                    _("shadow group %s: no user %s\n"),
811                                    _("delete member '%s'? "),
812                                    "delete member '%s' from shadow group '%s'",
813                                    errors) == 1) {
814                         *changed = true;
815                         sge->changed = true;
816                         __sgr_set_changed ();
817                 }
818         }
819 }
820 #endif                          /* SHADOWGRP */
821
822 /*
823  * grpck - verify group file integrity
824  */
825 int main (int argc, char **argv)
826 {
827         int errors = 0;
828         bool changed = false;
829
830         /*
831          * Get my name so that I can use it to report errors.
832          */
833         Prog = Basename (argv[0]);
834
835         (void) setlocale (LC_ALL, "");
836         (void) bindtextdomain (PACKAGE, LOCALEDIR);
837         (void) textdomain (PACKAGE);
838
839         OPENLOG ("grpck");
840
841         /* Parse the command line arguments */
842         process_flags (argc, argv);
843
844         open_files ();
845
846         if (sort_mode) {
847                 gr_sort ();
848 #ifdef  SHADOWGRP
849                 if (is_shadow) {
850                         sgr_sort ();
851                 }
852                 changed = true;
853 #endif
854         } else {
855                 check_grp_file (&errors, &changed);
856 #ifdef  SHADOWGRP
857                 if (is_shadow) {
858                         check_sgr_file (&errors, &changed);
859                 }
860 #endif
861         }
862
863         /* Commit the change in the database if needed */
864         close_files (changed);
865
866         nscd_flush_cache ("group");
867
868         /*
869          * Tell the user what we did and exit.
870          */
871         if (0 != errors) {
872                 if (changed) {
873                         printf (_("%s: the files have been updated\n"), Prog);
874                 } else {
875                         printf (_("%s: no changes\n"), Prog);
876                 }
877         }
878
879         return ((0 != errors) ? E_BAD_ENTRY : E_OKAY);
880 }
881