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];
145 use_system_pw_file = 0;
147 if (optind + 2 == argc) {
148 spw_file = argv[optind + 1];
151 use_system_spw_file = 0;
152 } else if (optind == argc)
153 is_shadow = spw_file_present ();
157 * open_files - open the shadow database
159 * In read-only mode, the databases are not locked and are opened
162 static void open_files (void)
165 * Lock the files if we aren't in "read-only" mode
169 fprintf (stderr, _("%s: cannot lock file %s\n"),
171 if (use_system_pw_file)
172 SYSLOG ((LOG_WARN, "cannot lock %s", pwd_file));
176 if (is_shadow && !spw_lock ()) {
177 fprintf (stderr, _("%s: cannot lock file %s\n"),
179 if (use_system_spw_file)
180 SYSLOG ((LOG_WARN, "cannot lock %s", spw_file));
187 * Open the files. Use O_RDONLY if we are in read_only mode, O_RDWR
190 if (!pw_open (read_only ? O_RDONLY : O_RDWR)) {
191 fprintf (stderr, _("%s: cannot open file %s\n"),
193 if (use_system_pw_file)
194 SYSLOG ((LOG_WARN, "cannot open %s", pwd_file));
198 if (is_shadow && !spw_open (read_only ? O_RDONLY : O_RDWR)) {
199 fprintf (stderr, _("%s: cannot open file %s\n"),
201 if (use_system_spw_file)
202 SYSLOG ((LOG_WARN, "cannot open %s", spw_file));
209 * close_files - close and unlock the password/shadow databases
211 * If changed is not set, the databases are not closed, and no
212 * changes are committed in the databases. The databases are
215 static void close_files (int changed)
218 * All done. If there were no change we can just abandon any
219 * changes to the files.
223 fprintf (stderr, _("%s: cannot update file %s\n"),
225 SYSLOG ((LOG_WARN, "cannot update %s", pwd_file));
229 if (is_shadow && !spw_close ()) {
230 fprintf (stderr, _("%s: cannot update file %s\n"),
232 SYSLOG ((LOG_WARN, "cannot update %s", spw_file));
239 * Don't be anti-social - unlock the files when you're done.
247 * check_pw_file - check the content of the passwd file
249 static void check_pw_file (int *errors, int *changed)
251 struct commonio_entry *pfe, *tpfe;
256 * Loop through the entire password file.
258 for (pfe = __pw_get_head (); pfe; pfe = pfe->next) {
260 * If this is a NIS line, skip it. You can't "know" what NIS
261 * is going to do without directly asking NIS ...
263 if (pfe->line[0] == '+' || pfe->line[0] == '-')
267 * Start with the entries that are completely corrupt. They
268 * have no (struct passwd) entry because they couldn't be
273 * Tell the user this entire line is bogus and ask
276 printf (_("invalid password file entry\n"));
277 printf (_("delete line '%s'? "), pfe->line);
281 * prompt the user to delete the entry or not
283 if (!yes_or_no (read_only))
287 * All password file deletions wind up here. This
288 * code removes the current entry from the linked
289 * list. When done, it skips back to the top of the
290 * loop to try out the next list element.
293 SYSLOG ((LOG_INFO, "delete passwd line `%s'",
297 __pw_del_entry (pfe);
302 * Password structure is good, start using it.
307 * Make sure this entry has a unique name.
309 for (tpfe = __pw_get_head (); tpfe; tpfe = tpfe->next) {
310 const struct passwd *ent = tpfe->eptr;
313 * Don't check this entry
319 * Don't check invalid entries.
324 if (strcmp (pwd->pw_name, ent->pw_name) != 0)
328 * Tell the user this entry is a duplicate of
329 * another and ask them to delete it.
331 printf (_("duplicate password entry\n"));
332 printf (_("delete line '%s'? "), pfe->line);
336 * prompt the user to delete the entry or not
338 if (yes_or_no (read_only))
343 * Check for invalid usernames. --marekm
345 if (!check_user_name (pwd->pw_name)) {
346 printf (_("invalid user name '%s'\n"), pwd->pw_name);
351 * Make sure the primary group exists
353 /* local, no need for xgetgrgid */
354 if (!quiet && !getgrgid (pwd->pw_gid)) {
357 * No primary group, just give a warning
360 printf (_("user %s: no group %u\n"),
361 pwd->pw_name, pwd->pw_gid);
366 * Make sure the home directory exists
368 if (!quiet && access (pwd->pw_dir, F_OK)) {
370 * Home directory doesn't exist, give a warning
373 ("user %s: directory %s does not exist\n"),
374 pwd->pw_name, pwd->pw_dir);
379 * Make sure the login shell is executable
381 if (!quiet && pwd->pw_shell[0]
382 && access (pwd->pw_shell, F_OK)) {
385 * Login shell doesn't exist, give a warning
387 printf (_("user %s: program %s does not exist\n"),
388 pwd->pw_name, pwd->pw_shell);
393 * Make sure this entry exists in the /etc/shadow file.
397 spw = (struct spwd *) spw_locate (pwd->pw_name);
400 ("no matching password file entry in %s\n"),
402 printf (_("add user '%s' in %s? "),
403 pwd->pw_name, spw_file);
405 if (yes_or_no (read_only)) {
409 sp.sp_namp = pwd->pw_name;
410 sp.sp_pwdp = pwd->pw_passwd;
412 getdef_num ("PASS_MIN_DAYS", -1);
414 getdef_num ("PASS_MAX_DAYS", -1);
416 getdef_num ("PASS_WARN_AGE", -1);
421 time ((time_t *) 0) / (24L * 3600L);
424 if (!spw_update (&sp)) {
427 ("%s: can't update shadow entry for %s\n"),
431 /* remove password from /etc/passwd */
433 pw.pw_passwd = SHADOW_PASSWD_STRING; /* XXX warning: const */
434 if (!pw_update (&pw)) {
437 ("%s: can't update passwd entry for %s\n"),
448 * check_spw_file - check the content of the shadowed password file (shadow)
450 static void check_spw_file (int *errors, int *changed)
452 struct commonio_entry *spe, *tspe;
456 * Loop through the entire shadow password file.
458 for (spe = __spw_get_head (); spe; spe = spe->next) {
460 * Do not treat lines which were missing in shadow
461 * and were added earlier.
463 if (spe->line == NULL)
467 * If this is a NIS line, skip it. You can't "know" what NIS
468 * is going to do without directly asking NIS ...
470 if (spe->line[0] == '+' || spe->line[0] == '-')
474 * Start with the entries that are completely corrupt. They
475 * have no (struct spwd) entry because they couldn't be
480 * Tell the user this entire line is bogus and ask
483 printf (_("invalid shadow password file entry\n"));
484 printf (_("delete line '%s'? "), spe->line);
488 * prompt the user to delete the entry or not
490 if (!yes_or_no (read_only))
494 * All shadow file deletions wind up here. This code
495 * removes the current entry from the linked list.
496 * When done, it skips back to the top of the loop
497 * to try out the next list element.
500 SYSLOG ((LOG_INFO, "delete shadow line `%s'",
504 __spw_del_entry (spe);
509 * Shadow password structure is good, start using it.
514 * Make sure this entry has a unique name.
516 for (tspe = __spw_get_head (); tspe; tspe = tspe->next) {
517 const struct spwd *ent = tspe->eptr;
520 * Don't check this entry
526 * Don't check invalid entries.
531 if (strcmp (spw->sp_namp, ent->sp_namp) != 0)
535 * Tell the user this entry is a duplicate of
536 * another and ask them to delete it.
538 printf (_("duplicate shadow password entry\n"));
539 printf (_("delete line '%s'? "), spe->line);
543 * prompt the user to delete the entry or not
545 if (yes_or_no (read_only))
550 * Make sure this entry exists in the /etc/passwd
553 if (!pw_locate (spw->sp_namp)) {
555 * Tell the user this entry has no matching
556 * /etc/passwd entry and ask them to delete it.
558 printf (_("no matching password file entry in %s\n"),
560 printf (_("delete line '%s'? "), spe->line);
564 * prompt the user to delete the entry or not
566 if (yes_or_no (read_only))
571 * Warn if last password change in the future. --marekm
573 if (!quiet && spw->sp_lstchg > time ((time_t *) 0) / SCALE) {
575 ("user %s: last password change in the future\n"),
583 * pwck - verify password file integrity
585 int main (int argc, char **argv)
591 * Get my name so that I can use it to report errors.
593 Prog = Basename (argv[0]);
595 setlocale (LC_ALL, "");
596 bindtextdomain (PACKAGE, LOCALEDIR);
597 textdomain (PACKAGE);
601 /* Parse the command line arguments */
602 process_flags (argc, argv);
612 check_pw_file (&errors, &changed);
615 check_spw_file (&errors, &changed);
619 close_files (changed);
621 nscd_flush_cache ("passwd");
624 * Tell the user what we did and exit.
628 _("%s: the files have been updated\n") :
629 _("%s: no changes\n"), Prog);
632 exit (errors ? E_BADENTRY : E_OKAY);