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 - 2008, Nicolas François
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
47 #include "prototypes.h"
62 #define E_CANT_UPDATE 5
68 static const char *grp_file = GROUP_FILE;
69 static bool use_system_grp_file = true;
72 static const char *sgr_file = SGROUP_FILE;
73 static bool use_system_sgr_file = true;
74 static bool is_shadow = false;
77 static bool read_only = false;
78 static bool sort_mode = false;
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 (bool changed);
86 static int check_members (const char *groupname,
89 const char *fmt_prompt,
90 const char *fmt_syslog,
92 static void check_grp_file (int *errors, bool *changed);
94 static void compare_members_lists (const char *groupname,
98 const char *other_file);
99 static void check_sgr_file (int *errors, bool *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 = false;
190 if ((optind + 2) == argc) {
191 sgr_file = argv[optind + 1];
194 use_system_sgr_file = false;
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 (bool 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; NULL != members[i]; i++) {
333 /* local, no need for xgetpwnam */
334 if (getpwnam (members[i]) != NULL) {
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;
359 * compare_members_lists - make sure the list of members is contained in
362 * compare_members_lists() checks that all the members of members are
363 * also in other_members.
364 * file and other_file are used for logging.
366 * TODO: No changes are performed on the lists.
368 static void compare_members_lists (const char *groupname,
370 char **other_members,
372 const char *other_file)
374 char **pmem, **other_pmem;
376 for (pmem = members; NULL != *pmem; pmem++) {
377 for (other_pmem = other_members; NULL != *other_pmem; other_pmem++) {
378 if (strcmp (*pmem, *other_pmem) == 0) {
382 if (*other_pmem == NULL) {
384 ("'%s' is a member of the '%s' group in %s but not in %s\n",
385 *pmem, groupname, file, other_file);
389 #endif /* SHADOWGRP */
392 * check_grp_file - check the content of the group file
394 static void check_grp_file (int *errors, bool *changed)
396 struct commonio_entry *gre, *tgre;
403 * Loop through the entire group file.
405 for (gre = __gr_get_head (); NULL != gre; gre = gre->next) {
407 * Skip all NIS entries.
410 if ((gre->line[0] == '+') || (gre->line[0] == '-')) {
415 * Start with the entries that are completely corrupt. They
416 * have no (struct group) entry because they couldn't be
419 if (NULL == gre->eptr) {
422 * Tell the user this entire line is bogus and ask
425 puts (_("invalid group file entry"));
426 printf (_("delete line '%s'? "), gre->line);
430 * prompt the user to delete the entry or not
432 if (!yes_or_no (read_only)) {
437 * All group file deletions wind up here. This code
438 * removes the current entry from the linked list.
439 * When done, it skips back to the top of the loop
440 * to try out the next list element.
443 SYSLOG ((LOG_INFO, "delete group line `%s'",
447 __gr_del_entry (gre);
452 * Group structure is good, start using it.
457 * Make sure this entry has a unique name.
459 for (tgre = __gr_get_head (); NULL != tgre; tgre = tgre->next) {
461 const struct group *ent = tgre->eptr;
464 * Don't check this entry
471 * Don't check invalid entries.
477 if (strcmp (grp->gr_name, ent->gr_name) != 0) {
482 * Tell the user this entry is a duplicate of
483 * another and ask them to delete it.
485 puts (_("duplicate group entry"));
486 printf (_("delete line '%s'? "), gre->line);
490 * prompt the user to delete the entry or not
492 if (yes_or_no (read_only)) {
498 * Check for invalid group names. --marekm
500 if (!is_valid_group_name (grp->gr_name)) {
502 printf (_("invalid group name '%s'\n"), grp->gr_name);
506 * Workaround for a NYS libc 5.3.12 bug on RedHat 4.2 -
507 * groups with no members are returned as groups with one
508 * member "", causing grpck to fail. --marekm
510 if ( (NULL != grp->gr_mem[0])
511 && (NULL == grp->gr_mem[1])
512 && ('\0' == grp->gr_mem[0][0])) {
513 grp->gr_mem[0] = NULL;
516 if (check_members (grp->gr_name, grp->gr_mem,
517 _("group %s: no user %s\n"),
518 _("delete member '%s'? "),
519 "delete member `%s' from group `%s'",
528 * Make sure this entry exists in the /etc/gshadow file.
532 sgr = (struct sgrp *) sgr_locate (grp->gr_name);
535 ("no matching group file entry in %s\n"),
537 printf (_("add group '%s' in %s ?"),
538 grp->gr_name, sgr_file);
540 if (yes_or_no (read_only)) {
543 static char *empty = NULL;
545 sg.sg_name = grp->gr_name;
546 sg.sg_passwd = grp->gr_passwd;
548 sg.sg_mem = grp->gr_mem;
550 "add group `%s' to `%s'",
551 grp->gr_name, sgr_file));
554 if (sgr_update (&sg) == 0) {
557 ("%s: can't update shadow entry for %s\n"),
559 exit (E_CANT_UPDATE);
561 /* remove password from /etc/group */
563 gr.gr_passwd = SHADOW_PASSWD_STRING; /* XXX warning: const */
564 if (gr_update (&gr) == 0) {
567 ("%s: can't update entry for group %s\n"),
569 exit (E_CANT_UPDATE);
574 * Verify that all the members defined in /etc/group are also
575 * present in /etc/gshadow.
577 compare_members_lists (grp->gr_name,
578 grp->gr_mem, sgr->sg_mem,
589 * check_sgr_file - check the content of the shadowed group file (gshadow)
591 static void check_sgr_file (int *errors, bool *changed)
594 struct commonio_entry *sge, *tsge;
598 * Loop through the entire shadow group file.
600 for (sge = __sgr_get_head (); NULL != sge; sge = sge->next) {
603 * Start with the entries that are completely corrupt. They
604 * have no (struct sgrp) entry because they couldn't be
607 if (NULL == sge->eptr) {
610 * Tell the user this entire line is bogus and ask
613 puts (_("invalid shadow group file entry"));
614 printf (_("delete line '%s'? "), sge->line);
618 * prompt the user to delete the entry or not
620 if (!yes_or_no (read_only)) {
625 * All shadow group file deletions wind up here.
626 * This code removes the current entry from the
627 * linked list. When done, it skips back to the top
628 * of the loop to try out the next list element.
631 SYSLOG ((LOG_INFO, "delete shadow line `%s'",
635 __sgr_del_entry (sge);
640 * Shadow group structure is good, start using it.
645 * Make sure this entry has a unique name.
647 for (tsge = __sgr_get_head (); NULL != tsge; tsge = tsge->next) {
649 const struct sgrp *ent = tsge->eptr;
652 * Don't check this entry
659 * Don't check invalid entries.
665 if (strcmp (sgr->sg_name, ent->sg_name) != 0) {
670 * Tell the user this entry is a duplicate of
671 * another and ask them to delete it.
673 puts (_("duplicate shadow group entry"));
674 printf (_("delete line '%s'? "), sge->line);
678 * prompt the user to delete the entry or not
680 if (yes_or_no (read_only)) {
686 * Make sure this entry exists in the /etc/group file.
688 grp = (struct group *) gr_locate (sgr->sg_name);
690 printf (_("no matching group file entry in %s\n"),
692 printf (_("delete line '%s'? "), sge->line);
694 if (yes_or_no (read_only)) {
699 * Verify that the all members defined in /etc/gshadow are also
700 * present in /etc/group.
702 compare_members_lists (sgr->sg_name,
703 sgr->sg_mem, grp->gr_mem,
708 * Make sure each administrator exists
710 if (check_members (sgr->sg_name, sgr->sg_adm,
711 _("shadow group %s: no administrative user %s\n"),
712 _("delete administrative member '%s'? "),
713 "delete admin `%s' from shadow group `%s'",
717 __sgr_set_changed ();
721 * Make sure each member exists
723 if (check_members (sgr->sg_name, sgr->sg_mem,
724 _("shadow group %s: no user %s\n"),
725 _("delete member '%s'? "),
726 "delete member `%s' from shadow group `%s'",
730 __sgr_set_changed ();
734 #endif /* SHADOWGRP */
737 * grpck - verify group file integrity
739 int main (int argc, char **argv)
742 bool changed = false;
745 * Get my name so that I can use it to report errors.
747 Prog = Basename (argv[0]);
749 (void) setlocale (LC_ALL, "");
750 (void) bindtextdomain (PACKAGE, LOCALEDIR);
751 (void) textdomain (PACKAGE);
755 /* Parse the command line arguments */
756 process_flags (argc, argv);
769 check_grp_file (&errors, &changed);
772 check_sgr_file (&errors, &changed);
777 /* Commit the change in the database if needed */
778 close_files (changed);
780 nscd_flush_cache ("group");
783 * Tell the user what we did and exit.
787 _("%s: the files have been updated\n") :
788 _("%s: no changes\n"), Prog);
791 exit ((0 != errors) ? E_BAD_ENTRY : E_OKAY);