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.
45 #include "prototypes.h"
60 #define E_CANTUPDATE 5
68 static const char *pwd_file = PASSWD_FILE;
69 static bool use_system_pw_file = true;
70 static const char *spw_file = SHADOW_FILE;
71 static bool use_system_spw_file = true;
73 static bool is_shadow = false;
75 static bool pw_locked = false;
76 static bool spw_locked = false;
79 static bool read_only = false;
80 static bool sort_mode = false;
81 static bool quiet = false; /* don't report warnings, only errors */
83 /* local function prototypes */
84 static void fail_exit (int code);
85 static void usage (void);
86 static void process_flags (int argc, char **argv);
87 static void open_files (void);
88 static void close_files (bool changed);
89 static void check_pw_file (int *errors, bool *changed);
90 static void check_spw_file (int *errors, bool *changed);
93 * fail_exit - do some cleanup and exit with the given error code
95 static void fail_exit (int code)
98 if (spw_unlock () == 0) {
99 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, spw_dbname ());
100 SYSLOG ((LOG_ERR, "failed to unlock %s", spw_dbname ()));
106 if (pw_unlock () == 0) {
107 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, pw_dbname ());
108 SYSLOG ((LOG_ERR, "failed to unlock %s", pw_dbname ()));
118 * usage - print syntax message and exit
120 static void usage (void)
122 fprintf (stderr, _("Usage: %s [-q] [-r] [-s] [passwd [shadow]]\n"),
128 * process_flags - parse the command line options
130 * It will not return if an error is encountered.
132 static void process_flags (int argc, char **argv)
137 * Parse the command line arguments
139 while ((arg = getopt (argc, argv, "eqrs")) != EOF) {
141 case 'e': /* added for Debian shadow-961025-2 compatibility */
156 if (sort_mode && read_only) {
157 fprintf (stderr, _("%s: -s and -r are incompatibile\n"), Prog);
162 * Make certain we have the right number of arguments
164 if ((argc < optind) || (argc > (optind + 2))) {
169 * If there are two left over filenames, use those as the password
170 * and shadow password filenames.
172 if (optind != argc) {
173 pwd_file = argv[optind];
174 pw_setdbname (pwd_file);
175 use_system_pw_file = false;
177 if ((optind + 2) == argc) {
178 spw_file = argv[optind + 1];
179 spw_setdbname (spw_file);
181 use_system_spw_file = false;
182 } else if (optind == argc) {
183 is_shadow = spw_file_present ();
188 * open_files - open the shadow database
190 * In read-only mode, the databases are not locked and are opened
193 static void open_files (void)
196 * Lock the files if we aren't in "read-only" mode
199 if (pw_lock () == 0) {
201 _("%s: cannot lock %s; try again later.\n"),
203 fail_exit (E_CANTLOCK);
207 if (spw_lock () == 0) {
209 _("%s: cannot lock %s; try again later.\n"),
211 fail_exit (E_CANTLOCK);
218 * Open the files. Use O_RDONLY if we are in read_only mode, O_RDWR
221 if (pw_open (read_only ? O_RDONLY : O_RDWR) == 0) {
222 fprintf (stderr, _("%s: cannot open %s\n"),
224 if (use_system_pw_file) {
225 SYSLOG ((LOG_WARN, "cannot open %s", pwd_file));
227 fail_exit (E_CANTOPEN);
229 if (is_shadow && (spw_open (read_only ? O_RDONLY : O_RDWR) == 0)) {
230 fprintf (stderr, _("%s: cannot open %s\n"),
232 if (use_system_spw_file) {
233 SYSLOG ((LOG_WARN, "cannot open %s", spw_file));
235 fail_exit (E_CANTOPEN);
240 * close_files - close and unlock the password/shadow databases
242 * If changed is not set, the databases are not closed, and no
243 * changes are committed in the databases. The databases are
246 static void close_files (bool changed)
249 * All done. If there were no change we can just abandon any
250 * changes to the files.
253 if (pw_close () == 0) {
254 fprintf (stderr, _("%s: failure while writing changes to %s\n"),
256 SYSLOG ((LOG_ERR, "failure while writing changes to %s", pwd_file));
257 fail_exit (E_CANTUPDATE);
259 if (is_shadow && (spw_close () == 0)) {
260 fprintf (stderr, _("%s: failure while writing changes to %s\n"),
262 SYSLOG ((LOG_ERR, "failure while writing changes to %s", spw_file));
263 fail_exit (E_CANTUPDATE);
268 * Don't be anti-social - unlock the files when you're done.
271 if (spw_unlock () == 0) {
272 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, spw_dbname ());
273 SYSLOG ((LOG_ERR, "failed to unlock %s", spw_dbname ()));
279 if (pw_unlock () == 0) {
280 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, pw_dbname ());
281 SYSLOG ((LOG_ERR, "failed to unlock %s", pw_dbname ()));
289 * check_pw_file - check the content of the passwd file
291 static void check_pw_file (int *errors, bool *changed)
293 struct commonio_entry *pfe, *tpfe;
298 * Loop through the entire password file.
300 for (pfe = __pw_get_head (); NULL != pfe; pfe = pfe->next) {
302 * If this is a NIS line, skip it. You can't "know" what NIS
303 * is going to do without directly asking NIS ...
305 if (('+' == pfe->line[0]) || ('-' == pfe->line[0])) {
310 * Start with the entries that are completely corrupt. They
311 * have no (struct passwd) entry because they couldn't be
314 if (NULL == pfe->eptr) {
316 * Tell the user this entire line is bogus and ask
319 puts (_("invalid password file entry"));
320 printf (_("delete line '%s'? "), pfe->line);
324 * prompt the user to delete the entry or not
326 if (!yes_or_no (read_only)) {
331 * All password file deletions wind up here. This
332 * code removes the current entry from the linked
333 * list. When done, it skips back to the top of the
334 * loop to try out the next list element.
337 SYSLOG ((LOG_INFO, "delete passwd line '%s'",
341 __pw_del_entry (pfe);
346 * Password structure is good, start using it.
351 * Make sure this entry has a unique name.
353 for (tpfe = __pw_get_head (); NULL != tpfe; tpfe = tpfe->next) {
354 const struct passwd *ent = tpfe->eptr;
357 * Don't check this entry
364 * Don't check invalid entries.
370 if (strcmp (pwd->pw_name, ent->pw_name) != 0) {
375 * Tell the user this entry is a duplicate of
376 * another and ask them to delete it.
378 puts (_("duplicate password entry"));
379 printf (_("delete line '%s'? "), pfe->line);
383 * prompt the user to delete the entry or not
385 if (yes_or_no (read_only)) {
391 * Check for invalid usernames. --marekm
393 if (!is_valid_user_name (pwd->pw_name)) {
394 printf (_("invalid user name '%s'\n"), pwd->pw_name);
399 * Check for invalid user ID.
401 if (pwd->pw_uid == (uid_t)-1) {
402 printf (_("invalid user ID '%lu'\n"), (long unsigned int)pwd->pw_uid);
407 * Make sure the primary group exists
409 /* local, no need for xgetgrgid */
410 if (!quiet && (NULL == getgrgid (pwd->pw_gid))) {
413 * No primary group, just give a warning
416 printf (_("user '%s': no group %lu\n"),
417 pwd->pw_name, (unsigned long) pwd->pw_gid);
422 * Make sure the home directory exists
424 if (!quiet && (access (pwd->pw_dir, F_OK) != 0)) {
426 * Home directory doesn't exist, give a warning
428 printf (_("user '%s': directory '%s' does not exist\n"),
429 pwd->pw_name, pwd->pw_dir);
434 * Make sure the login shell is executable
437 && ('\0' != pwd->pw_shell[0])
438 && (access (pwd->pw_shell, F_OK) != 0)) {
441 * Login shell doesn't exist, give a warning
443 printf (_("user '%s': program '%s' does not exist\n"),
444 pwd->pw_name, pwd->pw_shell);
449 * Make sure this entry exists in the /etc/shadow file.
453 spw = (struct spwd *) spw_locate (pwd->pw_name);
455 printf (_("no matching password file entry in %s\n"),
457 printf (_("add user '%s' in %s? "),
458 pwd->pw_name, spw_file);
460 if (yes_or_no (read_only)) {
464 sp.sp_namp = pwd->pw_name;
465 sp.sp_pwdp = pwd->pw_passwd;
467 getdef_num ("PASS_MIN_DAYS", -1);
469 getdef_num ("PASS_MAX_DAYS", -1);
471 getdef_num ("PASS_WARN_AGE", -1);
474 sp.sp_flag = SHADOW_SP_FLAG_UNSET;
475 sp.sp_lstchg = (long) time ((time_t *) 0) / SCALE;
476 if (0 == sp.sp_lstchg) {
477 /* Better disable aging than
478 * requiring a password change
484 if (spw_update (&sp) == 0) {
486 _("%s: failed to prepare the new %s entry '%s'\n"),
487 Prog, spw_dbname (), sp.sp_namp);
490 /* remove password from /etc/passwd */
492 pw.pw_passwd = SHADOW_PASSWD_STRING; /* XXX warning: const */
493 if (pw_update (&pw) == 0) {
495 _("%s: failed to prepare the new %s entry '%s'\n"),
496 Prog, pw_dbname (), pw.pw_name);
506 * check_spw_file - check the content of the shadowed password file (shadow)
508 static void check_spw_file (int *errors, bool *changed)
510 struct commonio_entry *spe, *tspe;
514 * Loop through the entire shadow password file.
516 for (spe = __spw_get_head (); NULL != spe; spe = spe->next) {
518 * Do not treat lines which were missing in shadow
519 * and were added earlier.
521 if (NULL == spe->line) {
526 * If this is a NIS line, skip it. You can't "know" what NIS
527 * is going to do without directly asking NIS ...
529 if (('+' == spe->line[0]) || ('-' == spe->line[0])) {
534 * Start with the entries that are completely corrupt. They
535 * have no (struct spwd) entry because they couldn't be
538 if (NULL == spe->eptr) {
540 * Tell the user this entire line is bogus and ask
543 puts (_("invalid shadow password file entry"));
544 printf (_("delete line '%s'? "), spe->line);
548 * prompt the user to delete the entry or not
550 if (!yes_or_no (read_only)) {
555 * All shadow file deletions wind up here. This code
556 * removes the current entry from the linked list.
557 * When done, it skips back to the top of the loop
558 * to try out the next list element.
561 SYSLOG ((LOG_INFO, "delete shadow line '%s'",
565 __spw_del_entry (spe);
570 * Shadow password structure is good, start using it.
575 * Make sure this entry has a unique name.
577 for (tspe = __spw_get_head (); NULL != tspe; tspe = tspe->next) {
578 const struct spwd *ent = tspe->eptr;
581 * Don't check this entry
588 * Don't check invalid entries.
594 if (strcmp (spw->sp_namp, ent->sp_namp) != 0) {
599 * Tell the user this entry is a duplicate of
600 * another and ask them to delete it.
602 puts (_("duplicate shadow password entry"));
603 printf (_("delete line '%s'? "), spe->line);
607 * prompt the user to delete the entry or not
609 if (yes_or_no (read_only)) {
615 * Make sure this entry exists in the /etc/passwd
618 if (pw_locate (spw->sp_namp) == NULL) {
620 * Tell the user this entry has no matching
621 * /etc/passwd entry and ask them to delete it.
623 printf (_("no matching password file entry in %s\n"),
625 printf (_("delete line '%s'? "), spe->line);
629 * prompt the user to delete the entry or not
631 if (yes_or_no (read_only)) {
637 * Warn if last password change in the future. --marekm
640 && (spw->sp_lstchg > (long) time ((time_t *) 0) / SCALE)) {
641 printf (_("user %s: last password change in the future\n"),
649 * pwck - verify password file integrity
651 int main (int argc, char **argv)
654 bool changed = false;
657 * Get my name so that I can use it to report errors.
659 Prog = Basename (argv[0]);
661 (void) setlocale (LC_ALL, "");
662 (void) bindtextdomain (PACKAGE, LOCALEDIR);
663 (void) textdomain (PACKAGE);
667 /* Parse the command line arguments */
668 process_flags (argc, argv);
673 if (pw_sort () != 0) {
675 _("%s: cannot sort entries in %s\n"),
677 fail_exit (E_CANTSORT);
680 if (spw_sort () != 0) {
682 _("%s: cannot sort entries in %s\n"),
683 Prog, spw_dbname ());
684 fail_exit (E_CANTSORT);
689 check_pw_file (&errors, &changed);
692 check_spw_file (&errors, &changed);
696 close_files (changed);
698 nscd_flush_cache ("passwd");
701 * Tell the user what we did and exit.
705 _("%s: the files have been updated\n") :
706 _("%s: no changes\n"), Prog);
710 return ((0 != errors) ? E_BADENTRY : E_OKAY);