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 - 2009, 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
69 static const char *grp_file = GROUP_FILE;
70 static bool use_system_grp_file = true;
73 static const char *sgr_file = SGROUP_FILE;
74 static bool use_system_sgr_file = true;
75 static bool is_shadow = false;
76 static bool sgr_locked = false;
78 static bool gr_locked = false;
80 static bool read_only = false;
81 static bool sort_mode = false;
83 /* local function prototypes */
84 static void fail_exit (int status);
85 static void usage (void);
86 static void delete_member (char **, const char *);
87 static void process_flags (int argc, char **argv);
88 static void open_files (void);
89 static void close_files (bool changed);
90 static int check_members (const char *groupname,
93 const char *fmt_prompt,
94 const char *fmt_syslog,
96 static void check_grp_file (int *errors, bool *changed);
98 static void compare_members_lists (const char *groupname,
100 char **other_members,
102 const char *other_file);
103 static void check_sgr_file (int *errors, bool *changed);
107 * fail_exit - exit with an error code after unlocking files
109 static void fail_exit (int status)
112 if (gr_unlock () == 0) {
113 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
114 SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
121 if (sgr_unlock () == 0) {
122 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
123 SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
135 * usage - print syntax message and exit
137 static void usage (void)
140 fprintf (stderr, _("Usage: %s [-r] [-s] [group [gshadow]]\n"), Prog);
142 fprintf (stderr, _("Usage: %s [-r] [-s] [group]\n"), Prog);
148 * delete_member - delete an entry in a list of members
150 * It only deletes the first entry with the given name.
152 static void delete_member (char **list, const char *member)
156 for (i = 0; list[i]; i++) {
157 if (list[i] == member) {
163 for (; list[i]; i++) {
164 list[i] = list[i + 1];
170 * process_flags - parse the command line options
172 * It will not return if an error is encountered.
174 static void process_flags (int argc, char **argv)
179 * Parse the command line arguments
181 while ((arg = getopt (argc, argv, "qrs")) != EOF) {
184 /* quiet - ignored for now */
197 if (sort_mode && read_only) {
198 fprintf (stderr, _("%s: -s and -r are incompatibile\n"), Prog);
203 * Make certain we have the right number of arguments
206 if ((argc < optind) || (argc > (optind + 2)))
208 if ((argc < optind) || (argc > (optind + 1)))
215 * If there are two left over filenames, use those as the group and
216 * group password filenames.
218 if (optind != argc) {
219 grp_file = argv[optind];
220 gr_setdbname (grp_file);
221 use_system_grp_file = false;
224 if ((optind + 2) == argc) {
225 sgr_file = argv[optind + 1];
226 sgr_setdbname (sgr_file);
228 use_system_sgr_file = false;
229 } else if (optind == argc) {
230 is_shadow = sgr_file_present ();
236 * open_files - open the shadow database
238 * In read-only mode, the databases are not locked and are opened
241 static void open_files (void)
244 * Lock the files if we aren't in "read-only" mode
247 if (gr_lock () == 0) {
249 _("%s: cannot lock %s; try again later.\n"),
251 fail_exit (E_CANT_LOCK);
256 if (sgr_lock () == 0) {
258 _("%s: cannot lock %s; try again later.\n"),
260 fail_exit (E_CANT_LOCK);
268 * Open the files. Use O_RDONLY if we are in read_only mode,
271 if (gr_open (read_only ? O_RDONLY : O_RDWR) == 0) {
272 fprintf (stderr, _("%s: cannot open %s\n"), Prog,
274 if (use_system_grp_file) {
275 SYSLOG ((LOG_WARN, "cannot open %s", grp_file));
277 fail_exit (E_CANT_OPEN);
280 if (is_shadow && (sgr_open (read_only ? O_RDONLY : O_RDWR) == 0)) {
281 fprintf (stderr, _("%s: cannot open %s\n"), Prog,
283 if (use_system_sgr_file) {
284 SYSLOG ((LOG_WARN, "cannot open %s", sgr_file));
286 fail_exit (E_CANT_OPEN);
292 * close_files - close and unlock the group/gshadow databases
294 * If changed is not set, the databases are not closed, and no
295 * changes are committed in the databases. The databases are
298 static void close_files (bool changed)
301 * All done. If there were no change we can just abandon any
302 * changes to the files.
305 if (gr_close () == 0) {
306 fprintf (stderr, _("%s: failure while writing changes to %s\n"),
308 fail_exit (E_CANT_UPDATE);
311 if (is_shadow && (sgr_close () == 0)) {
312 fprintf (stderr, _("%s: failure while writing changes to %s\n"),
314 fail_exit (E_CANT_UPDATE);
320 * Don't be anti-social - unlock the files when you're done.
324 if (sgr_unlock () == 0) {
325 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
326 SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
333 if (gr_unlock () == 0) {
334 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
335 SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
343 * check_members - check that every members of a group exist
345 * If an error is detected, *errors is incremented.
347 * The user will be prompted for the removal of the non-existent
350 * If any changes are performed, the return value will be 1,
351 * otherwise check_members() returns 0.
353 * fmt_info, fmt_prompt, and fmt_syslog are used for logging.
354 * * fmt_info must contain two string flags (%s): for the group's
355 * name and the missing member.
356 * * fmt_prompt must contain one string flags (%s): the missing
358 * * fmt_syslog must contain two string flags (%s): for the
359 * group's name and the missing member.
361 static int check_members (const char *groupname,
363 const char *fmt_info,
364 const char *fmt_prompt,
365 const char *fmt_syslog,
369 int members_changed = 0;
372 * Make sure each member exists
374 for (i = 0; NULL != members[i]; i++) {
375 /* local, no need for xgetpwnam */
376 if (getpwnam (members[i]) != NULL) {
380 * Can't find this user. Remove them
384 printf (fmt_info, groupname, members[i]);
385 printf (fmt_prompt, members[i]);
387 if (!yes_or_no (read_only)) {
391 SYSLOG ((LOG_INFO, fmt_syslog, members[i], groupname));
393 delete_member (members, members[i]);
395 /* Rewind in case of removal */
399 return members_changed;
404 * compare_members_lists - make sure the list of members is contained in
407 * compare_members_lists() checks that all the members of members are
408 * also in other_members.
409 * file and other_file are used for logging.
411 * TODO: No changes are performed on the lists.
413 static void compare_members_lists (const char *groupname,
415 char **other_members,
417 const char *other_file)
419 char **pmem, **other_pmem;
421 for (pmem = members; NULL != *pmem; pmem++) {
422 for (other_pmem = other_members; NULL != *other_pmem; other_pmem++) {
423 if (strcmp (*pmem, *other_pmem) == 0) {
427 if (*other_pmem == NULL) {
429 ("'%s' is a member of the '%s' group in %s but not in %s\n",
430 *pmem, groupname, file, other_file);
434 #endif /* SHADOWGRP */
437 * check_grp_file - check the content of the group file
439 static void check_grp_file (int *errors, bool *changed)
441 struct commonio_entry *gre, *tgre;
448 * Loop through the entire group file.
450 for (gre = __gr_get_head (); NULL != gre; gre = gre->next) {
452 * Skip all NIS entries.
455 if ((gre->line[0] == '+') || (gre->line[0] == '-')) {
460 * Start with the entries that are completely corrupt. They
461 * have no (struct group) entry because they couldn't be
464 if (NULL == gre->eptr) {
467 * Tell the user this entire line is bogus and ask
470 puts (_("invalid group file entry"));
471 printf (_("delete line '%s'? "), gre->line);
475 * prompt the user to delete the entry or not
477 if (!yes_or_no (read_only)) {
482 * All group file deletions wind up here. This code
483 * removes the current entry from the linked list.
484 * When done, it skips back to the top of the loop
485 * to try out the next list element.
488 SYSLOG ((LOG_INFO, "delete group line '%s'",
492 __gr_del_entry (gre);
497 * Group structure is good, start using it.
502 * Make sure this entry has a unique name.
504 for (tgre = __gr_get_head (); NULL != tgre; tgre = tgre->next) {
506 const struct group *ent = tgre->eptr;
509 * Don't check this entry
516 * Don't check invalid entries.
522 if (strcmp (grp->gr_name, ent->gr_name) != 0) {
527 * Tell the user this entry is a duplicate of
528 * another and ask them to delete it.
530 puts (_("duplicate group entry"));
531 printf (_("delete line '%s'? "), gre->line);
535 * prompt the user to delete the entry or not
537 if (yes_or_no (read_only)) {
543 * Check for invalid group names. --marekm
545 if (!is_valid_group_name (grp->gr_name)) {
547 printf (_("invalid group name '%s'\n"), grp->gr_name);
551 * Check for invalid group ID.
553 if (grp->gr_gid == (gid_t)-1) {
554 printf (_("invalid group ID '%lu'\n"), (long unsigned int)grp->gr_gid);
559 * Workaround for a NYS libc 5.3.12 bug on RedHat 4.2 -
560 * groups with no members are returned as groups with one
561 * member "", causing grpck to fail. --marekm
563 if ( (NULL != grp->gr_mem[0])
564 && (NULL == grp->gr_mem[1])
565 && ('\0' == grp->gr_mem[0][0])) {
566 grp->gr_mem[0] = NULL;
569 if (check_members (grp->gr_name, grp->gr_mem,
570 _("group %s: no user %s\n"),
571 _("delete member '%s'? "),
572 "delete member '%s' from group '%s'",
581 * Make sure this entry exists in the /etc/gshadow file.
585 sgr = (struct sgrp *) sgr_locate (grp->gr_name);
588 ("no matching group file entry in %s\n"),
590 printf (_("add group '%s' in %s?"),
591 grp->gr_name, sgr_file);
593 if (yes_or_no (read_only)) {
596 static char *empty = NULL;
598 sg.sg_name = grp->gr_name;
599 sg.sg_passwd = grp->gr_passwd;
601 sg.sg_mem = grp->gr_mem;
603 "add group '%s' to '%s'",
604 grp->gr_name, sgr_file));
607 if (sgr_update (&sg) == 0) {
609 _("%s: failed to prepare the new %s entry '%s'\n"),
610 Prog, sgr_dbname (), sg.sg_name);
611 fail_exit (E_CANT_UPDATE);
613 /* remove password from /etc/group */
615 gr.gr_passwd = SHADOW_PASSWD_STRING; /* XXX warning: const */
616 if (gr_update (&gr) == 0) {
618 _("%s: failed to prepare the new %s entry '%s'\n"),
619 Prog, gr_dbname (), gr.gr_name);
620 fail_exit (E_CANT_UPDATE);
625 * Verify that all the members defined in /etc/group are also
626 * present in /etc/gshadow.
628 compare_members_lists (grp->gr_name,
629 grp->gr_mem, sgr->sg_mem,
640 * check_sgr_file - check the content of the shadowed group file (gshadow)
642 static void check_sgr_file (int *errors, bool *changed)
645 struct commonio_entry *sge, *tsge;
649 * Loop through the entire shadow group file.
651 for (sge = __sgr_get_head (); NULL != sge; sge = sge->next) {
654 * Start with the entries that are completely corrupt. They
655 * have no (struct sgrp) entry because they couldn't be
658 if (NULL == sge->eptr) {
661 * Tell the user this entire line is bogus and ask
664 puts (_("invalid shadow group file entry"));
665 printf (_("delete line '%s'? "), sge->line);
669 * prompt the user to delete the entry or not
671 if (!yes_or_no (read_only)) {
676 * All shadow group file deletions wind up here.
677 * This code removes the current entry from the
678 * linked list. When done, it skips back to the top
679 * of the loop to try out the next list element.
682 SYSLOG ((LOG_INFO, "delete shadow line '%s'",
686 __sgr_del_entry (sge);
691 * Shadow group structure is good, start using it.
696 * Make sure this entry has a unique name.
698 for (tsge = __sgr_get_head (); NULL != tsge; tsge = tsge->next) {
700 const struct sgrp *ent = tsge->eptr;
703 * Don't check this entry
710 * Don't check invalid entries.
716 if (strcmp (sgr->sg_name, ent->sg_name) != 0) {
721 * Tell the user this entry is a duplicate of
722 * another and ask them to delete it.
724 puts (_("duplicate shadow group entry"));
725 printf (_("delete line '%s'? "), sge->line);
729 * prompt the user to delete the entry or not
731 if (yes_or_no (read_only)) {
737 * Make sure this entry exists in the /etc/group file.
739 grp = (struct group *) gr_locate (sgr->sg_name);
741 printf (_("no matching group file entry in %s\n"),
743 printf (_("delete line '%s'? "), sge->line);
745 if (yes_or_no (read_only)) {
750 * Verify that the all members defined in /etc/gshadow are also
751 * present in /etc/group.
753 compare_members_lists (sgr->sg_name,
754 sgr->sg_mem, grp->gr_mem,
759 * Make sure each administrator exists
761 if (check_members (sgr->sg_name, sgr->sg_adm,
762 _("shadow group %s: no administrative user %s\n"),
763 _("delete administrative member '%s'? "),
764 "delete admin '%s' from shadow group '%s'",
768 __sgr_set_changed ();
772 * Make sure each member exists
774 if (check_members (sgr->sg_name, sgr->sg_mem,
775 _("shadow group %s: no user %s\n"),
776 _("delete member '%s'? "),
777 "delete member '%s' from shadow group '%s'",
781 __sgr_set_changed ();
785 #endif /* SHADOWGRP */
788 * grpck - verify group file integrity
790 int main (int argc, char **argv)
793 bool changed = false;
796 * Get my name so that I can use it to report errors.
798 Prog = Basename (argv[0]);
800 (void) setlocale (LC_ALL, "");
801 (void) bindtextdomain (PACKAGE, LOCALEDIR);
802 (void) textdomain (PACKAGE);
806 /* Parse the command line arguments */
807 process_flags (argc, argv);
820 check_grp_file (&errors, &changed);
823 check_sgr_file (&errors, &changed);
828 /* Commit the change in the database if needed */
829 close_files (changed);
831 nscd_flush_cache ("group");
834 * Tell the user what we did and exit.
838 _("%s: the files have been updated\n") :
839 _("%s: no changes\n"), Prog);
842 exit ((0 != errors) ? E_BAD_ENTRY : E_OKAY);