2 * Copyright 1992 - 1994, Julianne Frances Haugh
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
43 #include "prototypes.h"
44 extern void __gr_del_entry (const struct commonio_entry *);
45 extern struct commonio_entry *__gr_get_head (void);
49 extern void __sgr_del_entry (const struct commonio_entry *);
50 extern struct commonio_entry *__sgr_get_head (void);
62 #define E_CANT_UPDATE 5
68 static const char *grp_file = GROUP_FILE;
69 static int use_system_grp_file = 1;
72 static const char *sgr_file = SGROUP_FILE;
73 static int use_system_sgr_file = 1;
74 static int is_shadow = 0;
77 static int read_only = 0;
78 static int sort_mode = 0;
80 /* local function prototypes */
81 static void usage (void);
82 static void delete_member (char **, const char *);
83 static void process_flags (int argc, char **argv);
84 static void open_files (void);
85 static void close_files (int changed);
86 static int check_members (const char *groupname,
89 const char *fmt_prompt,
90 const char *fmt_syslog,
92 static void compare_members_lists (const char *groupname,
96 const char *other_file);
97 static void check_grp_file (int *errors, int *changed);
99 static void check_sgr_file (int *errors, int *changed);
103 * usage - print syntax message and exit
105 static void usage (void)
108 fprintf (stderr, _("Usage: %s [-r] [-s] [group [gshadow]]\n"), Prog);
110 fprintf (stderr, _("Usage: %s [-r] [-s] [group]\n"), Prog);
116 * delete_member - delete an entry in a list of members
118 static void delete_member (char **list, const char *member)
122 for (i = 0; list[i]; i++) {
123 if (list[i] == member) {
129 for (; list[i]; i++) {
130 list[i] = list[i + 1];
136 * process_flags - parse the command line options
138 * It will not return if an error is encountered.
140 static void process_flags (int argc, char **argv)
145 * Parse the command line arguments
147 while ((arg = getopt (argc, argv, "qrs")) != EOF) {
150 /* quiet - ignored for now */
163 if (sort_mode && read_only) {
164 fprintf (stderr, _("%s: -s and -r are incompatibile\n"), Prog);
169 * Make certain we have the right number of arguments
172 if ((argc < optind) || (argc > (optind + 2)))
174 if ((argc < optind) || (argc > (optind + 1)))
181 * If there are two left over filenames, use those as the group and
182 * group password filenames.
184 if (optind != argc) {
185 grp_file = argv[optind];
187 use_system_grp_file = 0;
190 if ((optind + 2) == argc) {
191 sgr_file = argv[optind + 1];
194 use_system_sgr_file = 0;
195 } else if (optind == argc) {
196 is_shadow = sgr_file_present ();
202 * open_files - open the shadow database
204 * In read-only mode, the databases are not locked and are opened
207 static void open_files (void)
210 * Lock the files if we aren't in "read-only" mode
213 if (gr_lock () == 0) {
214 fprintf (stderr, _("%s: cannot lock file %s\n"),
216 if (use_system_grp_file) {
217 SYSLOG ((LOG_WARN, "cannot lock %s", grp_file));
223 if (is_shadow && (sgr_lock () == 0)) {
224 fprintf (stderr, _("%s: cannot lock file %s\n"),
226 if (use_system_sgr_file) {
227 SYSLOG ((LOG_WARN, "cannot lock %s", sgr_file));
236 * Open the files. Use O_RDONLY if we are in read_only mode,
239 if (gr_open (read_only ? O_RDONLY : O_RDWR) == 0) {
240 fprintf (stderr, _("%s: cannot open file %s\n"), Prog,
242 if (use_system_grp_file) {
243 SYSLOG ((LOG_WARN, "cannot open %s", grp_file));
249 if (is_shadow && (sgr_open (read_only ? O_RDONLY : O_RDWR) == 0)) {
250 fprintf (stderr, _("%s: cannot open file %s\n"), Prog,
252 if (use_system_sgr_file) {
253 SYSLOG ((LOG_WARN, "cannot open %s", sgr_file));
262 * close_files - close and unlock the group/gshadow databases
264 * If changed is not set, the databases are not closed, and no
265 * changes are committed in the databases. The databases are
268 static void close_files (int changed)
271 * All done. If there were no change we can just abandon any
272 * changes to the files.
275 if (gr_close () == 0) {
276 fprintf (stderr, _("%s: cannot update file %s\n"),
278 exit (E_CANT_UPDATE);
281 if (is_shadow && (sgr_close () == 0)) {
282 fprintf (stderr, _("%s: cannot update file %s\n"),
284 exit (E_CANT_UPDATE);
290 * Don't be anti-social - unlock the files when you're done.
301 * check_members - check that every members of a group exist
303 * If an error is detected, *errors is incremented.
305 * The user will be prompted for the removal of the non-existent
308 * If any changes are performed, the return value will be 1,
309 * otherwise check_members() returns 0.
311 * fmt_info, fmt_prompt, and fmt_syslog are used for logging.
312 * * fmt_info must contain two string flags (%s): for the group's
313 * name and the missing member.
314 * * fmt_prompt must contain one string flags (%s): the missing
316 * * fmt_syslog must contain two string flags (%s): for the
317 * group's name and the missing member.
319 static int check_members (const char *groupname,
321 const char *fmt_info,
322 const char *fmt_prompt,
323 const char *fmt_syslog,
327 int members_changed = 0;
330 * Make sure each member exists
332 for (i = 0; members[i]; i++) {
333 /* local, no need for xgetpwnam */
334 if (getpwnam (members[i])) {
338 * Can't find this user. Remove them
342 printf (fmt_info, groupname, members[i]);
343 printf (fmt_prompt, members[i]);
345 if (!yes_or_no (read_only)) {
349 SYSLOG ((LOG_INFO, fmt_syslog, members[i], groupname));
351 delete_member (members, members[i]);
354 return members_changed;
358 * compare_members_lists - make sure the list of members is contained in
361 * compare_members_lists() checks that all the members of members are
362 * also in other_members.
363 * file and other_file are used for logging.
365 * TODO: No changes are performed on the lists.
367 static void compare_members_lists (const char *groupname,
369 char **other_members,
371 const char *other_file)
373 char **pmem, **other_pmem;
375 for (pmem = members; *pmem; pmem++) {
376 for (other_pmem = other_members; *other_pmem; other_pmem++) {
377 if (strcmp (*pmem, *other_pmem) == 0) {
381 if (*other_pmem == NULL) {
383 ("'%s' is a member of the '%s' group in %s but not in %s\n",
384 *pmem, groupname, file, other_file);
390 * check_grp_file - check the content of the group file
392 static void check_grp_file (int *errors, int *changed)
394 struct commonio_entry *gre, *tgre;
401 * Loop through the entire group file.
403 for (gre = __gr_get_head (); gre; gre = gre->next) {
405 * Skip all NIS entries.
408 if ((gre->line[0] == '+') || (gre->line[0] == '-')) {
413 * Start with the entries that are completely corrupt. They
414 * have no (struct group) entry because they couldn't be
420 * Tell the user this entire line is bogus and ask
423 printf (_("invalid group file entry\n"));
424 printf (_("delete line '%s'? "), gre->line);
428 * prompt the user to delete the entry or not
430 if (!yes_or_no (read_only)) {
435 * All group file deletions wind up here. This code
436 * removes the current entry from the linked list.
437 * When done, it skips back to the top of the loop
438 * to try out the next list element.
441 SYSLOG ((LOG_INFO, "delete group line `%s'",
445 __gr_del_entry (gre);
450 * Group structure is good, start using it.
455 * Make sure this entry has a unique name.
457 for (tgre = __gr_get_head (); tgre; tgre = tgre->next) {
459 const struct group *ent = tgre->eptr;
462 * Don't check this entry
469 * Don't check invalid entries.
475 if (strcmp (grp->gr_name, ent->gr_name) != 0) {
480 * Tell the user this entry is a duplicate of
481 * another and ask them to delete it.
483 printf (_("duplicate group entry\n"));
484 printf (_("delete line '%s'? "), gre->line);
488 * prompt the user to delete the entry or not
490 if (yes_or_no (read_only)) {
496 * Check for invalid group names. --marekm
498 if (!check_group_name (grp->gr_name)) {
500 printf (_("invalid group name '%s'\n"), grp->gr_name);
504 * Workaround for a NYS libc 5.3.12 bug on RedHat 4.2 -
505 * groups with no members are returned as groups with one
506 * member "", causing grpck to fail. --marekm
508 if (grp->gr_mem[0] && !grp->gr_mem[1]
509 && *(grp->gr_mem[0]) == '\0') {
510 grp->gr_mem[0] = (char *) 0;
513 if (check_members (grp->gr_name, grp->gr_mem,
514 _("group %s: no user %s\n"),
515 _("delete member '%s'? "),
516 "delete member `%s' from group `%s'",
525 * Make sure this entry exists in the /etc/gshadow file.
529 sgr = (struct sgrp *) sgr_locate (grp->gr_name);
532 ("no matching group file entry in %s\n"),
534 printf (_("add group '%s' in %s ?"),
535 grp->gr_name, sgr_file);
537 if (yes_or_no (read_only)) {
540 static char *empty = NULL;
542 sg.sg_name = grp->gr_name;
543 sg.sg_passwd = grp->gr_passwd;
545 sg.sg_mem = grp->gr_mem;
547 "add group `%s' to `%s'",
548 grp->gr_name, sgr_file));
551 if (!sgr_update (&sg)) {
554 ("%s: can't update shadow entry for %s\n"),
556 exit (E_CANT_UPDATE);
558 /* remove password from /etc/group */
560 gr.gr_passwd = SHADOW_PASSWD_STRING; /* XXX warning: const */
561 if (!gr_update (&gr)) {
564 ("%s: can't update entry for group %s\n"),
566 exit (E_CANT_UPDATE);
571 * Verify that all the members defined in /etc/group are also
572 * present in /etc/gshadow.
574 compare_members_lists (grp->gr_name,
575 grp->gr_mem, sgr->sg_mem,
586 * check_sgr_file - check the content of the shadowed group file (gshadow)
588 static void check_sgr_file (int *errors, int *changed)
591 struct commonio_entry *sge, *tsge;
595 * Loop through the entire shadow group file.
597 for (sge = __sgr_get_head (); sge; sge = sge->next) {
600 * Start with the entries that are completely corrupt. They
601 * have no (struct sgrp) entry because they couldn't be
607 * Tell the user this entire line is bogus and ask
610 printf (_("invalid shadow group file entry\n"));
611 printf (_("delete line '%s'? "), sge->line);
615 * prompt the user to delete the entry or not
617 if (!yes_or_no (read_only)) {
622 * All shadow group file deletions wind up here.
623 * This code removes the current entry from the
624 * linked list. When done, it skips back to the top
625 * of the loop to try out the next list element.
628 SYSLOG ((LOG_INFO, "delete shadow line `%s'",
632 __sgr_del_entry (sge);
637 * Shadow group structure is good, start using it.
642 * Make sure this entry has a unique name.
644 for (tsge = __sgr_get_head (); tsge; tsge = tsge->next) {
646 const struct sgrp *ent = tsge->eptr;
649 * Don't check this entry
656 * Don't check invalid entries.
662 if (strcmp (sgr->sg_name, ent->sg_name) != 0) {
667 * Tell the user this entry is a duplicate of
668 * another and ask them to delete it.
670 printf (_("duplicate shadow group entry\n"));
671 printf (_("delete line '%s'? "), sge->line);
675 * prompt the user to delete the entry or not
677 if (yes_or_no (read_only)) {
683 * Make sure this entry exists in the /etc/group file.
685 grp = (struct group *) gr_locate (sgr->sg_name);
687 printf (_("no matching group file entry in %s\n"),
689 printf (_("delete line '%s'? "), sge->line);
691 if (yes_or_no (read_only)) {
696 * Verify that the all members defined in /etc/gshadow are also
697 * present in /etc/group.
699 compare_members_lists (sgr->sg_name,
700 sgr->sg_mem, grp->gr_mem,
705 * Make sure each administrator exists
707 if (check_members (sgr->sg_name, sgr->sg_adm,
708 _("shadow group %s: no administrative user %s\n"),
709 _("delete administrative member '%s'? "),
710 "delete admin `%s' from shadow group `%s'",
714 __sgr_set_changed ();
718 * Make sure each member exists
720 if (check_members (sgr->sg_name, sgr->sg_mem,
721 _("shadow group %s: no user %s\n"),
722 _("delete member '%s'? "),
723 "delete member `%s' from shadow group `%s'",
727 __sgr_set_changed ();
731 #endif /* SHADOWGRP */
734 * grpck - verify group file integrity
736 int main (int argc, char **argv)
742 * Get my name so that I can use it to report errors.
744 Prog = Basename (argv[0]);
746 setlocale (LC_ALL, "");
747 bindtextdomain (PACKAGE, LOCALEDIR);
748 textdomain (PACKAGE);
752 /* Parse the command line arguments */
753 process_flags (argc, argv);
766 check_grp_file (&errors, &changed);
769 check_sgr_file (&errors, &changed);
774 /* Commit the change in the database if needed */
775 close_files (changed);
777 nscd_flush_cache ("group");
780 * Tell the user what we did and exit.
784 _("%s: the files have been updated\n") :
785 _("%s: no changes\n"), Prog);
788 exit (errors ? E_BAD_ENTRY : E_OKAY);