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
41 #include "prototypes.h"
46 extern void __pw_del_entry (const struct commonio_entry *);
47 extern struct commonio_entry *__pw_get_head (void);
49 extern void __spw_del_entry (const struct commonio_entry *);
50 extern struct commonio_entry *__spw_get_head (void);
61 #define E_CANTUPDATE 5
68 static const char *pwd_file = PASSWD_FILE;
70 static const char *spw_file = SHADOW_FILE;
71 static int read_only = 0;
72 static int quiet = 0; /* don't report warnings, only errors */
74 /* local function prototypes */
75 static void usage (void);
76 static int yes_or_no (void);
79 * usage - print syntax message and exit
81 static void usage (void)
83 fprintf (stderr, _("Usage: %s [-q] [-r] [-s] [passwd [shadow]]\n"),
89 * yes_or_no - get answer to question from the user
91 static int yes_or_no (void)
96 * In read-only mode all questions are answered "no".
105 * Get a line and see what the first character is.
107 if (fgets (buf, sizeof buf, stdin))
108 return buf[0] == 'y' || buf[0] == 'Y';
114 * pwck - verify password file integrity
116 int main (int argc, char **argv)
121 struct commonio_entry *pfe, *tpfe;
125 struct commonio_entry *spe, *tspe;
130 * Get my name so that I can use it to report errors.
132 Prog = Basename (argv[0]);
134 setlocale (LC_ALL, "");
135 bindtextdomain (PACKAGE, LOCALEDIR);
136 textdomain (PACKAGE);
141 * Parse the command line arguments
143 while ((arg = getopt (argc, argv, "eqrs")) != EOF) {
145 case 'e': /* added for Debian shadow-961025-2 compatibility */
160 if (sort_mode && read_only) {
161 fprintf (stderr, _("%s: -s and -r are incompatibile\n"), Prog);
166 * Make certain we have the right number of arguments
168 if (optind != argc && optind + 1 != argc && optind + 2 != argc)
172 * If there are two left over filenames, use those as the password
173 * and shadow password filenames.
175 if (optind != argc) {
176 pwd_file = argv[optind];
179 if (optind + 2 == argc) {
180 spw_file = argv[optind + 1];
183 } else if (optind == argc)
184 is_shadow = spw_file_present ();
187 * Lock the files if we aren't in "read-only" mode
191 fprintf (stderr, _("%s: cannot lock file %s\n"),
194 SYSLOG ((LOG_WARN, "cannot lock %s", pwd_file));
198 if (is_shadow && !spw_lock ()) {
199 fprintf (stderr, _("%s: cannot lock file %s\n"),
202 SYSLOG ((LOG_WARN, "cannot lock %s", spw_file));
209 * Open the files. Use O_RDONLY if we are in read_only mode, O_RDWR
212 if (!pw_open (read_only ? O_RDONLY : O_RDWR)) {
213 fprintf (stderr, _("%s: cannot open file %s\n"),
216 SYSLOG ((LOG_WARN, "cannot open %s", pwd_file));
220 if (is_shadow && !spw_open (read_only ? O_RDONLY : O_RDWR)) {
221 fprintf (stderr, _("%s: cannot open file %s\n"),
224 SYSLOG ((LOG_WARN, "cannot open %s", spw_file));
237 * Loop through the entire password file.
239 for (pfe = __pw_get_head (); pfe; pfe = pfe->next) {
241 * If this is a NIS line, skip it. You can't "know" what NIS
242 * is going to do without directly asking NIS ...
244 if (pfe->line[0] == '+' || pfe->line[0] == '-')
248 * Start with the entries that are completely corrupt. They
249 * have no (struct passwd) entry because they couldn't be
254 * Tell the user this entire line is bogus and ask
257 printf (_("invalid password file entry\n"));
258 printf (_("delete line '%s'? "), pfe->line);
262 * prompt the user to delete the entry or not
268 * All password file deletions wind up here. This
269 * code removes the current entry from the linked
270 * list. When done, it skips back to the top of the
271 * loop to try out the next list element.
274 SYSLOG ((LOG_INFO, "delete passwd line `%s'",
278 __pw_del_entry (pfe);
283 * Password structure is good, start using it.
288 * Make sure this entry has a unique name.
290 for (tpfe = __pw_get_head (); tpfe; tpfe = tpfe->next) {
291 const struct passwd *ent = tpfe->eptr;
294 * Don't check this entry
300 * Don't check invalid entries.
305 if (strcmp (pwd->pw_name, ent->pw_name) != 0)
309 * Tell the user this entry is a duplicate of
310 * another and ask them to delete it.
312 printf (_("duplicate password entry\n"));
313 printf (_("delete line '%s'? "), pfe->line);
317 * prompt the user to delete the entry or not
324 * Check for invalid usernames. --marekm
326 if (!check_user_name (pwd->pw_name)) {
327 printf (_("invalid user name '%s'\n"), pwd->pw_name);
332 * Make sure the primary group exists
334 /* local, no need for xgetgrgid */
335 if (!quiet && !getgrgid (pwd->pw_gid)) {
338 * No primary group, just give a warning
341 printf (_("user %s: no group %u\n"),
342 pwd->pw_name, pwd->pw_gid);
347 * Make sure the home directory exists
349 if (!quiet && access (pwd->pw_dir, F_OK)) {
351 * Home directory doesn't exist, give a warning
354 ("user %s: directory %s does not exist\n"),
355 pwd->pw_name, pwd->pw_dir);
360 * Make sure the login shell is executable
362 if (!quiet && pwd->pw_shell[0]
363 && access (pwd->pw_shell, F_OK)) {
366 * Login shell doesn't exist, give a warning
368 printf (_("user %s: program %s does not exist\n"),
369 pwd->pw_name, pwd->pw_shell);
374 * Make sure this entry exists in the /etc/gshadow file.
378 spw = (struct spwd *) spw_locate (pwd->pw_name);
381 ("no matching password file entry in %s\n"),
383 printf (_("add user '%s' in %s? "),
384 pwd->pw_name, spw_file);
390 sp.sp_namp = pwd->pw_name;
391 sp.sp_pwdp = pwd->pw_passwd;
393 getdef_num ("PASS_MIN_DAYS", -1);
395 getdef_num ("PASS_MAX_DAYS", -1);
397 getdef_num ("PASS_WARN_AGE", -1);
402 time ((time_t *) 0) / (24L * 3600L);
405 if (!spw_update (&sp)) {
408 ("%s: can't update shadow entry for %s\n"),
412 /* remove password from /etc/passwd */
414 pw.pw_passwd = SHADOW_PASSWD_STRING; /* XXX warning: const */
415 if (!pw_update (&pw)) {
418 ("%s: can't update passwd entry for %s\n"),
431 * Loop through the entire shadow password file.
433 for (spe = __spw_get_head (); spe; spe = spe->next) {
435 * Do not treat lines which were missing in gshadow
436 * and were added earlier.
438 if (spe->line == NULL)
442 * If this is a NIS line, skip it. You can't "know" what NIS
443 * is going to do without directly asking NIS ...
445 if (spe->line[0] == '+' || spe->line[0] == '-')
449 * Start with the entries that are completely corrupt. They
450 * have no (struct spwd) entry because they couldn't be
455 * Tell the user this entire line is bogus and ask
458 printf (_("invalid shadow password file entry\n"));
459 printf (_("delete line '%s'? "), spe->line);
463 * prompt the user to delete the entry or not
469 * All shadow file deletions wind up here. This code
470 * removes the current entry from the linked list.
471 * When done, it skips back to the top of the loop
472 * to try out the next list element.
475 SYSLOG ((LOG_INFO, "delete shadow line `%s'",
479 __spw_del_entry (spe);
484 * Shadow password structure is good, start using it.
489 * Make sure this entry has a unique name.
491 for (tspe = __spw_get_head (); tspe; tspe = tspe->next) {
492 const struct spwd *ent = tspe->eptr;
495 * Don't check this entry
501 * Don't check invalid entries.
506 if (strcmp (spw->sp_namp, ent->sp_namp) != 0)
510 * Tell the user this entry is a duplicate of
511 * another and ask them to delete it.
513 printf (_("duplicate shadow password entry\n"));
514 printf (_("delete line '%s'? "), spe->line);
518 * prompt the user to delete the entry or not
525 * Make sure this entry exists in the /etc/passwd
528 if (!pw_locate (spw->sp_namp)) {
530 * Tell the user this entry has no matching
531 * /etc/passwd entry and ask them to delete it.
533 printf (_("no matching password file entry in %s\n"),
535 printf (_("delete line '%s'? "), spe->line);
539 * prompt the user to delete the entry or not
546 * Warn if last password change in the future. --marekm
548 if (!quiet && spw->sp_lstchg > time ((time_t *) 0) / SCALE) {
550 ("user %s: last password change in the future\n"),
559 * All done. If there were no change we can just abandon any
560 * changes to the files.
565 fprintf (stderr, _("%s: cannot update file %s\n"),
567 SYSLOG ((LOG_WARN, "cannot update %s", pwd_file));
571 if (is_shadow && !spw_close ()) {
572 fprintf (stderr, _("%s: cannot update file %s\n"),
574 SYSLOG ((LOG_WARN, "cannot update %s", spw_file));
581 * Don't be anti-social - unlock the files when you're done.
587 nscd_flush_cache ("passwd");
590 * Tell the user what we did and exit.
594 _("%s: the files have been updated\n") :
595 _("%s: no changes\n"), Prog);
598 exit (errors ? E_BADENTRY : E_OKAY);