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;
69 static int use_system_pw_file = 1;
70 static const char *spw_file = SHADOW_FILE;
71 static int use_system_spw_file = 1;
73 static int is_shadow = 0;
76 static int read_only = 0;
77 static int sort_mode = 0;
78 static int quiet = 0; /* don't report warnings, only errors */
80 /* local function prototypes */
81 static void usage (void);
82 static void process_flags (int argc, char **argv);
83 static void open_files (void);
84 static void close_files (int changed);
85 static void check_pw_file (int *errors, int *changed);
86 static void check_spw_file (int *errors, int *changed);
89 * usage - print syntax message and exit
91 static void usage (void)
93 fprintf (stderr, _("Usage: %s [-q] [-r] [-s] [passwd [shadow]]\n"),
99 * process_flags - parse the command line options
101 * It will not return if an error is encountered.
103 static void process_flags (int argc, char **argv)
108 * Parse the command line arguments
110 while ((arg = getopt (argc, argv, "eqrs")) != EOF) {
112 case 'e': /* added for Debian shadow-961025-2 compatibility */
127 if (sort_mode && read_only) {
128 fprintf (stderr, _("%s: -s and -r are incompatibile\n"), Prog);
133 * Make certain we have the right number of arguments
135 if (optind != argc && optind + 1 != argc && optind + 2 != argc)
139 * If there are two left over filenames, use those as the password
140 * and shadow password filenames.
142 if (optind != argc) {
143 pwd_file = argv[optind];
146 if (optind + 2 == argc) {
147 spw_file = argv[optind + 1];
150 } else if (optind == argc)
151 is_shadow = spw_file_present ();
155 * open_files - open the shadow database
157 * In read-only mode, the databases are not locked and are opened
160 static void open_files (void)
163 * Lock the files if we aren't in "read-only" mode
167 fprintf (stderr, _("%s: cannot lock file %s\n"),
169 if (use_system_pw_file)
170 SYSLOG ((LOG_WARN, "cannot lock %s", pwd_file));
174 if (is_shadow && !spw_lock ()) {
175 fprintf (stderr, _("%s: cannot lock file %s\n"),
177 if (use_system_spw_file)
178 SYSLOG ((LOG_WARN, "cannot lock %s", spw_file));
185 * Open the files. Use O_RDONLY if we are in read_only mode, O_RDWR
188 if (!pw_open (read_only ? O_RDONLY : O_RDWR)) {
189 fprintf (stderr, _("%s: cannot open file %s\n"),
191 if (use_system_pw_file)
192 SYSLOG ((LOG_WARN, "cannot open %s", pwd_file));
196 if (is_shadow && !spw_open (read_only ? O_RDONLY : O_RDWR)) {
197 fprintf (stderr, _("%s: cannot open file %s\n"),
199 if (use_system_spw_file)
200 SYSLOG ((LOG_WARN, "cannot open %s", spw_file));
207 * close_files - close and unlock the password/shadow databases
209 * If changed is not set, the databases are not closed, and no
210 * changes are committed in the databases. The databases are
213 static void close_files (int changed)
216 * All done. If there were no change we can just abandon any
217 * changes to the files.
221 fprintf (stderr, _("%s: cannot update file %s\n"),
223 SYSLOG ((LOG_WARN, "cannot update %s", pwd_file));
227 if (is_shadow && !spw_close ()) {
228 fprintf (stderr, _("%s: cannot update file %s\n"),
230 SYSLOG ((LOG_WARN, "cannot update %s", spw_file));
237 * Don't be anti-social - unlock the files when you're done.
245 * check_pw_file - check the content of the passwd file
247 static void check_pw_file (int *errors, int *changed)
249 struct commonio_entry *pfe, *tpfe;
254 * Loop through the entire password file.
256 for (pfe = __pw_get_head (); pfe; pfe = pfe->next) {
258 * If this is a NIS line, skip it. You can't "know" what NIS
259 * is going to do without directly asking NIS ...
261 if (pfe->line[0] == '+' || pfe->line[0] == '-')
265 * Start with the entries that are completely corrupt. They
266 * have no (struct passwd) entry because they couldn't be
271 * Tell the user this entire line is bogus and ask
274 printf (_("invalid password file entry\n"));
275 printf (_("delete line '%s'? "), pfe->line);
279 * prompt the user to delete the entry or not
281 if (!yes_or_no (read_only))
285 * All password file deletions wind up here. This
286 * code removes the current entry from the linked
287 * list. When done, it skips back to the top of the
288 * loop to try out the next list element.
291 SYSLOG ((LOG_INFO, "delete passwd line `%s'",
295 __pw_del_entry (pfe);
300 * Password structure is good, start using it.
305 * Make sure this entry has a unique name.
307 for (tpfe = __pw_get_head (); tpfe; tpfe = tpfe->next) {
308 const struct passwd *ent = tpfe->eptr;
311 * Don't check this entry
317 * Don't check invalid entries.
322 if (strcmp (pwd->pw_name, ent->pw_name) != 0)
326 * Tell the user this entry is a duplicate of
327 * another and ask them to delete it.
329 printf (_("duplicate password entry\n"));
330 printf (_("delete line '%s'? "), pfe->line);
334 * prompt the user to delete the entry or not
336 if (yes_or_no (read_only))
341 * Check for invalid usernames. --marekm
343 if (!check_user_name (pwd->pw_name)) {
344 printf (_("invalid user name '%s'\n"), pwd->pw_name);
349 * Make sure the primary group exists
351 /* local, no need for xgetgrgid */
352 if (!quiet && !getgrgid (pwd->pw_gid)) {
355 * No primary group, just give a warning
358 printf (_("user %s: no group %u\n"),
359 pwd->pw_name, pwd->pw_gid);
364 * Make sure the home directory exists
366 if (!quiet && access (pwd->pw_dir, F_OK)) {
368 * Home directory doesn't exist, give a warning
371 ("user %s: directory %s does not exist\n"),
372 pwd->pw_name, pwd->pw_dir);
377 * Make sure the login shell is executable
379 if (!quiet && pwd->pw_shell[0]
380 && access (pwd->pw_shell, F_OK)) {
383 * Login shell doesn't exist, give a warning
385 printf (_("user %s: program %s does not exist\n"),
386 pwd->pw_name, pwd->pw_shell);
391 * Make sure this entry exists in the /etc/gshadow file.
395 spw = (struct spwd *) spw_locate (pwd->pw_name);
398 ("no matching password file entry in %s\n"),
400 printf (_("add user '%s' in %s? "),
401 pwd->pw_name, spw_file);
403 if (yes_or_no (read_only)) {
407 sp.sp_namp = pwd->pw_name;
408 sp.sp_pwdp = pwd->pw_passwd;
410 getdef_num ("PASS_MIN_DAYS", -1);
412 getdef_num ("PASS_MAX_DAYS", -1);
414 getdef_num ("PASS_WARN_AGE", -1);
419 time ((time_t *) 0) / (24L * 3600L);
422 if (!spw_update (&sp)) {
425 ("%s: can't update shadow entry for %s\n"),
429 /* remove password from /etc/passwd */
431 pw.pw_passwd = SHADOW_PASSWD_STRING; /* XXX warning: const */
432 if (!pw_update (&pw)) {
435 ("%s: can't update passwd entry for %s\n"),
446 * check_spw_file - check the content of the shadowed password file (shadow)
448 static void check_spw_file (int *errors, int *changed)
450 struct commonio_entry *spe, *tspe;
454 * Loop through the entire shadow password file.
456 for (spe = __spw_get_head (); spe; spe = spe->next) {
458 * Do not treat lines which were missing in gshadow
459 * and were added earlier.
461 if (spe->line == NULL)
465 * If this is a NIS line, skip it. You can't "know" what NIS
466 * is going to do without directly asking NIS ...
468 if (spe->line[0] == '+' || spe->line[0] == '-')
472 * Start with the entries that are completely corrupt. They
473 * have no (struct spwd) entry because they couldn't be
478 * Tell the user this entire line is bogus and ask
481 printf (_("invalid shadow password file entry\n"));
482 printf (_("delete line '%s'? "), spe->line);
486 * prompt the user to delete the entry or not
488 if (!yes_or_no (read_only))
492 * All shadow file deletions wind up here. This code
493 * removes the current entry from the linked list.
494 * When done, it skips back to the top of the loop
495 * to try out the next list element.
498 SYSLOG ((LOG_INFO, "delete shadow line `%s'",
502 __spw_del_entry (spe);
507 * Shadow password structure is good, start using it.
512 * Make sure this entry has a unique name.
514 for (tspe = __spw_get_head (); tspe; tspe = tspe->next) {
515 const struct spwd *ent = tspe->eptr;
518 * Don't check this entry
524 * Don't check invalid entries.
529 if (strcmp (spw->sp_namp, ent->sp_namp) != 0)
533 * Tell the user this entry is a duplicate of
534 * another and ask them to delete it.
536 printf (_("duplicate shadow password entry\n"));
537 printf (_("delete line '%s'? "), spe->line);
541 * prompt the user to delete the entry or not
543 if (yes_or_no (read_only))
548 * Make sure this entry exists in the /etc/passwd
551 if (!pw_locate (spw->sp_namp)) {
553 * Tell the user this entry has no matching
554 * /etc/passwd entry and ask them to delete it.
556 printf (_("no matching password file entry in %s\n"),
558 printf (_("delete line '%s'? "), spe->line);
562 * prompt the user to delete the entry or not
564 if (yes_or_no (read_only))
569 * Warn if last password change in the future. --marekm
571 if (!quiet && spw->sp_lstchg > time ((time_t *) 0) / SCALE) {
573 ("user %s: last password change in the future\n"),
581 * pwck - verify password file integrity
583 int main (int argc, char **argv)
589 * Get my name so that I can use it to report errors.
591 Prog = Basename (argv[0]);
593 setlocale (LC_ALL, "");
594 bindtextdomain (PACKAGE, LOCALEDIR);
595 textdomain (PACKAGE);
599 /* Parse the command line arguments */
600 process_flags (argc, argv);
612 check_pw_file (&errors, &changed);
617 check_spw_file (&errors, &changed);
622 close_files (changed);
624 nscd_flush_cache ("passwd");
627 * Tell the user what we did and exit.
631 _("%s: the files have been updated\n") :
632 _("%s: no changes\n"), Prog);
635 exit (errors ? E_BADENTRY : E_OKAY);