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 (void) 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 (void) 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);
587 printf (_("no matching group file entry in %s\n"),
589 printf (_("add group '%s' in %s? "),
590 grp->gr_name, sgr_file);
592 if (yes_or_no (read_only)) {
595 static char *empty = NULL;
597 sg.sg_name = grp->gr_name;
598 sg.sg_passwd = grp->gr_passwd;
600 sg.sg_mem = grp->gr_mem;
602 "add group '%s' to '%s'",
603 grp->gr_name, sgr_file));
606 if (sgr_update (&sg) == 0) {
608 _("%s: failed to prepare the new %s entry '%s'\n"),
609 Prog, sgr_dbname (), sg.sg_name);
610 fail_exit (E_CANT_UPDATE);
612 /* remove password from /etc/group */
614 gr.gr_passwd = SHADOW_PASSWD_STRING; /* XXX warning: const */
615 if (gr_update (&gr) == 0) {
617 _("%s: failed to prepare the new %s entry '%s'\n"),
618 Prog, gr_dbname (), gr.gr_name);
619 fail_exit (E_CANT_UPDATE);
624 * Verify that all the members defined in /etc/group are also
625 * present in /etc/gshadow.
627 compare_members_lists (grp->gr_name,
628 grp->gr_mem, sgr->sg_mem,
631 /* The group entry has a gshadow counterpart.
632 * Make sure no passwords are in group.
634 if (strcmp (grp->gr_passwd, SHADOW_PASSWD_STRING) != 0) {
635 printf (_("group %s has an entry in %s, but its password field in %s is not set to 'x'\n"),
636 grp->gr_name, sgr_file, grp_file);
648 * check_sgr_file - check the content of the shadowed group file (gshadow)
650 static void check_sgr_file (int *errors, bool *changed)
653 struct commonio_entry *sge, *tsge;
657 * Loop through the entire shadow group file.
659 for (sge = __sgr_get_head (); NULL != sge; sge = sge->next) {
662 * Start with the entries that are completely corrupt. They
663 * have no (struct sgrp) entry because they couldn't be
666 if (NULL == sge->eptr) {
669 * Tell the user this entire line is bogus and ask
672 (void) puts (_("invalid shadow group file entry"));
673 printf (_("delete line '%s'? "), sge->line);
677 * prompt the user to delete the entry or not
679 if (!yes_or_no (read_only)) {
684 * All shadow group file deletions wind up here.
685 * This code removes the current entry from the
686 * linked list. When done, it skips back to the top
687 * of the loop to try out the next list element.
690 SYSLOG ((LOG_INFO, "delete shadow line '%s'",
694 __sgr_del_entry (sge);
699 * Shadow group structure is good, start using it.
704 * Make sure this entry has a unique name.
706 for (tsge = __sgr_get_head (); NULL != tsge; tsge = tsge->next) {
708 const struct sgrp *ent = tsge->eptr;
711 * Don't check this entry
718 * Don't check invalid entries.
724 if (strcmp (sgr->sg_name, ent->sg_name) != 0) {
729 * Tell the user this entry is a duplicate of
730 * another and ask them to delete it.
732 (void) puts (_("duplicate shadow group entry"));
733 printf (_("delete line '%s'? "), sge->line);
737 * prompt the user to delete the entry or not
739 if (yes_or_no (read_only)) {
745 * Make sure this entry exists in the /etc/group file.
747 grp = (struct group *) gr_locate (sgr->sg_name);
749 printf (_("no matching group file entry in %s\n"),
751 printf (_("delete line '%s'? "), sge->line);
753 if (yes_or_no (read_only)) {
758 * Verify that the all members defined in /etc/gshadow are also
759 * present in /etc/group.
761 compare_members_lists (sgr->sg_name,
762 sgr->sg_mem, grp->gr_mem,
767 * Make sure each administrator exists
769 if (check_members (sgr->sg_name, sgr->sg_adm,
770 _("shadow group %s: no administrative user %s\n"),
771 _("delete administrative member '%s'? "),
772 "delete admin '%s' from shadow group '%s'",
776 __sgr_set_changed ();
780 * Make sure each member exists
782 if (check_members (sgr->sg_name, sgr->sg_mem,
783 _("shadow group %s: no user %s\n"),
784 _("delete member '%s'? "),
785 "delete member '%s' from shadow group '%s'",
789 __sgr_set_changed ();
793 #endif /* SHADOWGRP */
796 * grpck - verify group file integrity
798 int main (int argc, char **argv)
801 bool changed = false;
804 * Get my name so that I can use it to report errors.
806 Prog = Basename (argv[0]);
808 (void) setlocale (LC_ALL, "");
809 (void) bindtextdomain (PACKAGE, LOCALEDIR);
810 (void) textdomain (PACKAGE);
814 /* Parse the command line arguments */
815 process_flags (argc, argv);
828 check_grp_file (&errors, &changed);
831 check_sgr_file (&errors, &changed);
836 /* Commit the change in the database if needed */
837 close_files (changed);
839 nscd_flush_cache ("group");
842 * Tell the user what we did and exit.
846 printf (_("%s: the files have been updated\n"), Prog);
848 printf (_("%s: no changes\n"), Prog);
852 return ((0 != errors) ? E_BAD_ENTRY : E_OKAY);