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 ((argc < optind) || (argc > (optind + 2))) {
140 * If there are two left over filenames, use those as the password
141 * and shadow password filenames.
143 if (optind != argc) {
144 pwd_file = argv[optind];
146 use_system_pw_file = 0;
148 if ((optind + 2) == argc) {
149 spw_file = argv[optind + 1];
152 use_system_spw_file = 0;
153 } else if (optind == argc) {
154 is_shadow = spw_file_present ();
159 * open_files - open the shadow database
161 * In read-only mode, the databases are not locked and are opened
164 static void open_files (void)
167 * Lock the files if we aren't in "read-only" mode
170 if (pw_lock () == 0) {
171 fprintf (stderr, _("%s: cannot lock file %s\n"),
173 if (use_system_pw_file) {
174 SYSLOG ((LOG_WARN, "cannot lock %s", pwd_file));
179 if (is_shadow && (spw_lock () == 0)) {
180 fprintf (stderr, _("%s: cannot lock file %s\n"),
182 if (use_system_spw_file) {
183 SYSLOG ((LOG_WARN, "cannot lock %s", spw_file));
191 * Open the files. Use O_RDONLY if we are in read_only mode, O_RDWR
194 if (pw_open (read_only ? O_RDONLY : O_RDWR) == 0) {
195 fprintf (stderr, _("%s: cannot open file %s\n"),
197 if (use_system_pw_file) {
198 SYSLOG ((LOG_WARN, "cannot open %s", pwd_file));
203 if (is_shadow && (spw_open (read_only ? O_RDONLY : O_RDWR) == 0)) {
204 fprintf (stderr, _("%s: cannot open file %s\n"),
206 if (use_system_spw_file) {
207 SYSLOG ((LOG_WARN, "cannot open %s", spw_file));
215 * close_files - close and unlock the password/shadow databases
217 * If changed is not set, the databases are not closed, and no
218 * changes are committed in the databases. The databases are
221 static void close_files (int changed)
224 * All done. If there were no change we can just abandon any
225 * changes to the files.
228 if (pw_close () == 0) {
229 fprintf (stderr, _("%s: cannot update file %s\n"),
231 SYSLOG ((LOG_WARN, "cannot update %s", pwd_file));
235 if (is_shadow && (spw_close () == 0)) {
236 fprintf (stderr, _("%s: cannot update file %s\n"),
238 SYSLOG ((LOG_WARN, "cannot update %s", spw_file));
245 * Don't be anti-social - unlock the files when you're done.
254 * check_pw_file - check the content of the passwd file
256 static void check_pw_file (int *errors, int *changed)
258 struct commonio_entry *pfe, *tpfe;
263 * Loop through the entire password file.
265 for (pfe = __pw_get_head (); NULL != pfe; pfe = pfe->next) {
267 * If this is a NIS line, skip it. You can't "know" what NIS
268 * is going to do without directly asking NIS ...
270 if ((pfe->line[0] == '+') || (pfe->line[0] == '-')) {
275 * Start with the entries that are completely corrupt. They
276 * have no (struct passwd) entry because they couldn't be
279 if (NULL == pfe->eptr) {
281 * Tell the user this entire line is bogus and ask
284 printf (_("invalid password file entry\n"));
285 printf (_("delete line '%s'? "), pfe->line);
289 * prompt the user to delete the entry or not
291 if (!yes_or_no (read_only)) {
296 * All password file deletions wind up here. This
297 * code removes the current entry from the linked
298 * list. When done, it skips back to the top of the
299 * loop to try out the next list element.
302 SYSLOG ((LOG_INFO, "delete passwd line `%s'",
306 __pw_del_entry (pfe);
311 * Password structure is good, start using it.
316 * Make sure this entry has a unique name.
318 for (tpfe = __pw_get_head (); NULL != tpfe; tpfe = tpfe->next) {
319 const struct passwd *ent = tpfe->eptr;
322 * Don't check this entry
329 * Don't check invalid entries.
335 if (strcmp (pwd->pw_name, ent->pw_name) != 0) {
340 * Tell the user this entry is a duplicate of
341 * another and ask them to delete it.
343 printf (_("duplicate password entry\n"));
344 printf (_("delete line '%s'? "), pfe->line);
348 * prompt the user to delete the entry or not
350 if (yes_or_no (read_only)) {
356 * Check for invalid usernames. --marekm
358 if (!check_user_name (pwd->pw_name)) {
359 printf (_("invalid user name '%s'\n"), pwd->pw_name);
364 * Make sure the primary group exists
366 /* local, no need for xgetgrgid */
367 if (!quiet && (NULL == getgrgid (pwd->pw_gid))) {
370 * No primary group, just give a warning
373 printf (_("user %s: no group %u\n"),
374 pwd->pw_name, pwd->pw_gid);
379 * Make sure the home directory exists
381 if (!quiet && (access (pwd->pw_dir, F_OK) != 0)) {
383 * Home directory doesn't exist, give a warning
385 printf (_("user %s: directory %s does not exist\n"),
386 pwd->pw_name, pwd->pw_dir);
391 * Make sure the login shell is executable
394 && ('\0' != pwd->pw_shell[0])
395 && (access (pwd->pw_shell, F_OK) != 0)) {
398 * Login shell doesn't exist, give a warning
400 printf (_("user %s: program %s does not exist\n"),
401 pwd->pw_name, pwd->pw_shell);
406 * Make sure this entry exists in the /etc/shadow file.
410 spw = (struct spwd *) spw_locate (pwd->pw_name);
412 printf (_("no matching password file entry in %s\n"),
414 printf (_("add user '%s' in %s? "),
415 pwd->pw_name, spw_file);
417 if (yes_or_no (read_only)) {
421 sp.sp_namp = pwd->pw_name;
422 sp.sp_pwdp = pwd->pw_passwd;
424 getdef_num ("PASS_MIN_DAYS", -1);
426 getdef_num ("PASS_MAX_DAYS", -1);
428 getdef_num ("PASS_WARN_AGE", -1);
433 time ((time_t *) 0) / (24L * 3600L);
436 if (spw_update (&sp) == 0) {
438 _("%s: can't update shadow entry for %s\n"),
442 /* remove password from /etc/passwd */
444 pw.pw_passwd = SHADOW_PASSWD_STRING; /* XXX warning: const */
445 if (pw_update (&pw) == 0) {
447 _("%s: can't update passwd entry for %s\n"),
458 * check_spw_file - check the content of the shadowed password file (shadow)
460 static void check_spw_file (int *errors, int *changed)
462 struct commonio_entry *spe, *tspe;
466 * Loop through the entire shadow password file.
468 for (spe = __spw_get_head (); NULL != spe; spe = spe->next) {
470 * Do not treat lines which were missing in shadow
471 * and were added earlier.
473 if (spe->line == NULL) {
478 * If this is a NIS line, skip it. You can't "know" what NIS
479 * is going to do without directly asking NIS ...
481 if ((spe->line[0] == '+') || (spe->line[0] == '-')) {
486 * Start with the entries that are completely corrupt. They
487 * have no (struct spwd) entry because they couldn't be
490 if (NULL == spe->eptr) {
492 * Tell the user this entire line is bogus and ask
495 printf (_("invalid shadow password file entry\n"));
496 printf (_("delete line '%s'? "), spe->line);
500 * prompt the user to delete the entry or not
502 if (!yes_or_no (read_only)) {
507 * All shadow file deletions wind up here. This code
508 * removes the current entry from the linked list.
509 * When done, it skips back to the top of the loop
510 * to try out the next list element.
513 SYSLOG ((LOG_INFO, "delete shadow line `%s'",
517 __spw_del_entry (spe);
522 * Shadow password structure is good, start using it.
527 * Make sure this entry has a unique name.
529 for (tspe = __spw_get_head (); NULL != tspe; tspe = tspe->next) {
530 const struct spwd *ent = tspe->eptr;
533 * Don't check this entry
540 * Don't check invalid entries.
546 if (strcmp (spw->sp_namp, ent->sp_namp) != 0) {
551 * Tell the user this entry is a duplicate of
552 * another and ask them to delete it.
554 printf (_("duplicate shadow password entry\n"));
555 printf (_("delete line '%s'? "), spe->line);
559 * prompt the user to delete the entry or not
561 if (yes_or_no (read_only)) {
567 * Make sure this entry exists in the /etc/passwd
570 if (pw_locate (spw->sp_namp) == NULL) {
572 * Tell the user this entry has no matching
573 * /etc/passwd entry and ask them to delete it.
575 printf (_("no matching password file entry in %s\n"),
577 printf (_("delete line '%s'? "), spe->line);
581 * prompt the user to delete the entry or not
583 if (yes_or_no (read_only)) {
589 * Warn if last password change in the future. --marekm
592 && (spw->sp_lstchg > time ((time_t *) 0) / SCALE)) {
593 printf (_("user %s: last password change in the future\n"),
601 * pwck - verify password file integrity
603 int main (int argc, char **argv)
609 * Get my name so that I can use it to report errors.
611 Prog = Basename (argv[0]);
613 setlocale (LC_ALL, "");
614 bindtextdomain (PACKAGE, LOCALEDIR);
615 textdomain (PACKAGE);
619 /* Parse the command line arguments */
620 process_flags (argc, argv);
631 check_pw_file (&errors, &changed);
634 check_spw_file (&errors, &changed);
638 close_files (changed);
640 nscd_flush_cache ("passwd");
643 * Tell the user what we did and exit.
647 _("%s: the files have been updated\n") :
648 _("%s: no changes\n"), Prog);
652 exit (errors ? E_BADENTRY : E_OKAY);