]> granicus.if.org Git - shadow/blob - src/pwck.c
* src/pwck.c: Use a %lu format and cast group and user IDs to
[shadow] / src / pwck.c
1 /*
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 - 2008, Nicolas François
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
20  *
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.
32  */
33
34 #include <config.h>
35
36 #ident "$Id$"
37
38 #include <fcntl.h>
39 #include <grp.h>
40 #include <pwd.h>
41 #include <stdio.h>
42 #include "chkname.h"
43 #include "commonio.h"
44 #include "defines.h"
45 #include "prototypes.h"
46 #include "pwio.h"
47 #include "shadowio.h"
48 #include "getdef.h"
49 #include "nscd.h"
50
51 /*
52  * Exit codes
53  */
54
55 #define E_OKAY          0
56 #define E_USAGE         1
57 #define E_BADENTRY      2
58 #define E_CANTOPEN      3
59 #define E_CANTLOCK      4
60 #define E_CANTUPDATE    5
61
62 /*
63  * Global variables
64  */
65
66 static char *Prog;
67 static const char *pwd_file = PASSWD_FILE;
68 static bool use_system_pw_file = true;
69 static const char *spw_file = SHADOW_FILE;
70 static bool use_system_spw_file = true;
71
72 static bool is_shadow = false;
73
74 /* Options */
75 static bool read_only = false;
76 static bool sort_mode = false;
77 static bool quiet = false;              /* don't report warnings, only errors */
78
79 /* local function prototypes */
80 static void usage (void);
81 static void process_flags (int argc, char **argv);
82 static void open_files (void);
83 static void close_files (bool changed);
84 static void check_pw_file (int *errors, bool *changed);
85 static void check_spw_file (int *errors, bool *changed);
86
87 /*
88  * usage - print syntax message and exit
89  */
90 static void usage (void)
91 {
92         fprintf (stderr, _("Usage: %s [-q] [-r] [-s] [passwd [shadow]]\n"),
93                  Prog);
94         exit (E_USAGE);
95 }
96
97 /*
98  * process_flags - parse the command line options
99  *
100  *      It will not return if an error is encountered.
101  */
102 static void process_flags (int argc, char **argv)
103 {
104         int arg;
105
106         /*
107          * Parse the command line arguments
108          */
109         while ((arg = getopt (argc, argv, "eqrs")) != EOF) {
110                 switch (arg) {
111                 case 'e':       /* added for Debian shadow-961025-2 compatibility */
112                 case 'q':
113                         quiet = true;
114                         break;
115                 case 'r':
116                         read_only = true;
117                         break;
118                 case 's':
119                         sort_mode = true;
120                         break;
121                 default:
122                         usage ();
123                 }
124         }
125
126         if (sort_mode && read_only) {
127                 fprintf (stderr, _("%s: -s and -r are incompatibile\n"), Prog);
128                 exit (E_USAGE);
129         }
130
131         /*
132          * Make certain we have the right number of arguments
133          */
134         if ((argc < optind) || (argc > (optind + 2))) {
135                 usage ();
136         }
137
138         /*
139          * If there are two left over filenames, use those as the password
140          * and shadow password filenames.
141          */
142         if (optind != argc) {
143                 pwd_file = argv[optind];
144                 pw_name (pwd_file);
145                 use_system_pw_file = false;
146         }
147         if ((optind + 2) == argc) {
148                 spw_file = argv[optind + 1];
149                 spw_name (spw_file);
150                 is_shadow = true;
151                 use_system_spw_file = false;
152         } else if (optind == argc) {
153                 is_shadow = spw_file_present ();
154         }
155 }
156
157 /*
158  * open_files - open the shadow database
159  *
160  *      In read-only mode, the databases are not locked and are opened
161  *      only for reading.
162  */
163 static void open_files (void)
164 {
165         /*
166          * Lock the files if we aren't in "read-only" mode
167          */
168         if (!read_only) {
169                 if (pw_lock () == 0) {
170                         fprintf (stderr, _("%s: cannot lock file %s\n"),
171                                  Prog, pwd_file);
172                         if (use_system_pw_file) {
173                                 SYSLOG ((LOG_WARN, "cannot lock %s", pwd_file));
174                         }
175                         closelog ();
176                         exit (E_CANTLOCK);
177                 }
178                 if (is_shadow && (spw_lock () == 0)) {
179                         fprintf (stderr, _("%s: cannot lock file %s\n"),
180                                  Prog, spw_file);
181                         if (use_system_spw_file) {
182                                 SYSLOG ((LOG_WARN, "cannot lock %s", spw_file));
183                         }
184                         closelog ();
185                         exit (E_CANTLOCK);
186                 }
187         }
188
189         /*
190          * Open the files. Use O_RDONLY if we are in read_only mode, O_RDWR
191          * otherwise.
192          */
193         if (pw_open (read_only ? O_RDONLY : O_RDWR) == 0) {
194                 fprintf (stderr, _("%s: cannot open file %s\n"),
195                          Prog, pwd_file);
196                 if (use_system_pw_file) {
197                         SYSLOG ((LOG_WARN, "cannot open %s", pwd_file));
198                 }
199                 closelog ();
200                 exit (E_CANTOPEN);
201         }
202         if (is_shadow && (spw_open (read_only ? O_RDONLY : O_RDWR) == 0)) {
203                 fprintf (stderr, _("%s: cannot open file %s\n"),
204                          Prog, spw_file);
205                 if (use_system_spw_file) {
206                         SYSLOG ((LOG_WARN, "cannot open %s", spw_file));
207                 }
208                 closelog ();
209                 exit (E_CANTOPEN);
210         }
211 }
212
213 /*
214  * close_files - close and unlock the password/shadow databases
215  *
216  *      If changed is not set, the databases are not closed, and no
217  *      changes are committed in the databases. The databases are
218  *      unlocked anyway.
219  */
220 static void close_files (bool changed)
221 {
222         /*
223          * All done. If there were no change we can just abandon any
224          * changes to the files.
225          */
226         if (changed) {
227                 if (pw_close () == 0) {
228                         fprintf (stderr, _("%s: cannot update file %s\n"),
229                                  Prog, pwd_file);
230                         SYSLOG ((LOG_WARN, "cannot update %s", pwd_file));
231                         closelog ();
232                         exit (E_CANTUPDATE);
233                 }
234                 if (is_shadow && (spw_close () == 0)) {
235                         fprintf (stderr, _("%s: cannot update file %s\n"),
236                                  Prog, spw_file);
237                         SYSLOG ((LOG_WARN, "cannot update %s", spw_file));
238                         closelog ();
239                         exit (E_CANTUPDATE);
240                 }
241         }
242
243         /*
244          * Don't be anti-social - unlock the files when you're done.
245          */
246         if (is_shadow) {
247                 spw_unlock ();
248         }
249         (void) pw_unlock ();
250 }
251
252 /*
253  * check_pw_file - check the content of the passwd file
254  */
255 static void check_pw_file (int *errors, bool *changed)
256 {
257         struct commonio_entry *pfe, *tpfe;
258         struct passwd *pwd;
259         struct spwd *spw;
260
261         /*
262          * Loop through the entire password file.
263          */
264         for (pfe = __pw_get_head (); NULL != pfe; pfe = pfe->next) {
265                 /*
266                  * If this is a NIS line, skip it. You can't "know" what NIS
267                  * is going to do without directly asking NIS ...
268                  */
269                 if (('+' == pfe->line[0]) || ('-' == pfe->line[0])) {
270                         continue;
271                 }
272
273                 /*
274                  * Start with the entries that are completely corrupt.  They
275                  * have no (struct passwd) entry because they couldn't be
276                  * parsed properly.
277                  */
278                 if (NULL == pfe->eptr) {
279                         /*
280                          * Tell the user this entire line is bogus and ask
281                          * them to delete it.
282                          */
283                         puts (_("invalid password file entry"));
284                         printf (_("delete line '%s'? "), pfe->line);
285                         *errors += 1;
286
287                         /*
288                          * prompt the user to delete the entry or not
289                          */
290                         if (!yes_or_no (read_only)) {
291                                 continue;
292                         }
293
294                         /*
295                          * All password file deletions wind up here. This
296                          * code removes the current entry from the linked
297                          * list. When done, it skips back to the top of the
298                          * loop to try out the next list element.
299                          */
300                       delete_pw:
301                         SYSLOG ((LOG_INFO, "delete passwd line `%s'",
302                                  pfe->line));
303                         *changed = true;
304
305                         __pw_del_entry (pfe);
306                         continue;
307                 }
308
309                 /*
310                  * Password structure is good, start using it.
311                  */
312                 pwd = pfe->eptr;
313
314                 /*
315                  * Make sure this entry has a unique name.
316                  */
317                 for (tpfe = __pw_get_head (); NULL != tpfe; tpfe = tpfe->next) {
318                         const struct passwd *ent = tpfe->eptr;
319
320                         /*
321                          * Don't check this entry
322                          */
323                         if (tpfe == pfe) {
324                                 continue;
325                         }
326
327                         /*
328                          * Don't check invalid entries.
329                          */
330                         if (NULL == ent) {
331                                 continue;
332                         }
333
334                         if (strcmp (pwd->pw_name, ent->pw_name) != 0) {
335                                 continue;
336                         }
337
338                         /*
339                          * Tell the user this entry is a duplicate of
340                          * another and ask them to delete it.
341                          */
342                         puts (_("duplicate password entry"));
343                         printf (_("delete line '%s'? "), pfe->line);
344                         *errors += 1;
345
346                         /*
347                          * prompt the user to delete the entry or not
348                          */
349                         if (yes_or_no (read_only)) {
350                                 goto delete_pw;
351                         }
352                 }
353
354                 /*
355                  * Check for invalid usernames.  --marekm
356                  */
357                 if (!is_valid_user_name (pwd->pw_name)) {
358                         printf (_("invalid user name '%s'\n"), pwd->pw_name);
359                         *errors += 1;
360                 }
361
362                 /*
363                  * Make sure the primary group exists
364                  */
365                 /* local, no need for xgetgrgid */
366                 if (!quiet && (NULL == getgrgid (pwd->pw_gid))) {
367
368                         /*
369                          * No primary group, just give a warning
370                          */
371
372                         printf (_("user %s: no group %lu\n"),
373                                 pwd->pw_name, (unsigned long) pwd->pw_gid);
374                         *errors += 1;
375                 }
376
377                 /*
378                  * Make sure the home directory exists
379                  */
380                 if (!quiet && (access (pwd->pw_dir, F_OK) != 0)) {
381                         /*
382                          * Home directory doesn't exist, give a warning
383                          */
384                         printf (_("user %s: directory %s does not exist\n"),
385                                 pwd->pw_name, pwd->pw_dir);
386                         *errors += 1;
387                 }
388
389                 /*
390                  * Make sure the login shell is executable
391                  */
392                 if (   !quiet
393                     && ('\0' != pwd->pw_shell[0])
394                     && (access (pwd->pw_shell, F_OK) != 0)) {
395
396                         /*
397                          * Login shell doesn't exist, give a warning
398                          */
399                         printf (_("user %s: program %s does not exist\n"),
400                                 pwd->pw_name, pwd->pw_shell);
401                         *errors += 1;
402                 }
403
404                 /*
405                  * Make sure this entry exists in the /etc/shadow file.
406                  */
407
408                 if (is_shadow) {
409                         spw = (struct spwd *) spw_locate (pwd->pw_name);
410                         if (NULL == spw) {
411                                 printf (_("no matching password file entry in %s\n"),
412                                         spw_file);
413                                 printf (_("add user '%s' in %s? "),
414                                         pwd->pw_name, spw_file);
415                                 *errors += 1;
416                                 if (yes_or_no (read_only)) {
417                                         struct spwd sp;
418                                         struct passwd pw;
419
420                                         sp.sp_namp   = pwd->pw_name;
421                                         sp.sp_pwdp   = pwd->pw_passwd;
422                                         sp.sp_min    =
423                                             getdef_num ("PASS_MIN_DAYS", -1);
424                                         sp.sp_max    =
425                                             getdef_num ("PASS_MAX_DAYS", -1);
426                                         sp.sp_warn   =
427                                             getdef_num ("PASS_WARN_AGE", -1);
428                                         sp.sp_inact  = -1;
429                                         sp.sp_expire = -1;
430                                         sp.sp_flag   = SHADOW_SP_FLAG_UNSET;
431                                         sp.sp_lstchg = (long) time ((time_t *) 0) / SCALE;
432                                         *changed = true;
433
434                                         if (spw_update (&sp) == 0) {
435                                                 fprintf (stderr,
436                                                          _("%s: can't update shadow entry for %s\n"),
437                                                          Prog, sp.sp_namp);
438                                                 exit (E_CANTUPDATE);
439                                         }
440                                         /* remove password from /etc/passwd */
441                                         pw = *pwd;
442                                         pw.pw_passwd = SHADOW_PASSWD_STRING;    /* XXX warning: const */
443                                         if (pw_update (&pw) == 0) {
444                                                 fprintf (stderr,
445                                                          _("%s: can't update passwd entry for %s\n"),
446                                                          Prog, pw.pw_name);
447                                                 exit (E_CANTUPDATE);
448                                         }
449                                 }
450                         }
451                 }
452         }
453 }
454
455 /*
456  * check_spw_file - check the content of the shadowed password file (shadow)
457  */
458 static void check_spw_file (int *errors, bool *changed)
459 {
460         struct commonio_entry *spe, *tspe;
461         struct spwd *spw;
462
463         /*
464          * Loop through the entire shadow password file.
465          */
466         for (spe = __spw_get_head (); NULL != spe; spe = spe->next) {
467                 /*
468                  * Do not treat lines which were missing in shadow
469                  * and were added earlier.
470                  */
471                 if (NULL == spe->line) {
472                         continue;
473                 }
474
475                 /*
476                  * If this is a NIS line, skip it. You can't "know" what NIS
477                  * is going to do without directly asking NIS ...
478                  */
479                 if (('+' == spe->line[0]) || ('-' == spe->line[0])) {
480                         continue;
481                 }
482
483                 /*
484                  * Start with the entries that are completely corrupt. They
485                  * have no (struct spwd) entry because they couldn't be
486                  * parsed properly.
487                  */
488                 if (NULL == spe->eptr) {
489                         /*
490                          * Tell the user this entire line is bogus and ask
491                          * them to delete it.
492                          */
493                         puts (_("invalid shadow password file entry"));
494                         printf (_("delete line '%s'? "), spe->line);
495                         *errors += 1;
496
497                         /*
498                          * prompt the user to delete the entry or not
499                          */
500                         if (!yes_or_no (read_only)) {
501                                 continue;
502                         }
503
504                         /*
505                          * All shadow file deletions wind up here. This code
506                          * removes the current entry from the linked list.
507                          * When done, it skips back to the top of the loop
508                          * to try out the next list element.
509                          */
510                       delete_spw:
511                         SYSLOG ((LOG_INFO, "delete shadow line `%s'",
512                                  spe->line));
513                         *changed = true;
514
515                         __spw_del_entry (spe);
516                         continue;
517                 }
518
519                 /*
520                  * Shadow password structure is good, start using it.
521                  */
522                 spw = spe->eptr;
523
524                 /*
525                  * Make sure this entry has a unique name.
526                  */
527                 for (tspe = __spw_get_head (); NULL != tspe; tspe = tspe->next) {
528                         const struct spwd *ent = tspe->eptr;
529
530                         /*
531                          * Don't check this entry
532                          */
533                         if (tspe == spe) {
534                                 continue;
535                         }
536
537                         /*
538                          * Don't check invalid entries.
539                          */
540                         if (NULL == ent) {
541                                 continue;
542                         }
543
544                         if (strcmp (spw->sp_namp, ent->sp_namp) != 0) {
545                                 continue;
546                         }
547
548                         /*
549                          * Tell the user this entry is a duplicate of
550                          * another and ask them to delete it.
551                          */
552                         puts (_("duplicate shadow password entry"));
553                         printf (_("delete line '%s'? "), spe->line);
554                         *errors += 1;
555
556                         /*
557                          * prompt the user to delete the entry or not
558                          */
559                         if (yes_or_no (read_only)) {
560                                 goto delete_spw;
561                         }
562                 }
563
564                 /*
565                  * Make sure this entry exists in the /etc/passwd
566                  * file.
567                  */
568                 if (pw_locate (spw->sp_namp) == NULL) {
569                         /*
570                          * Tell the user this entry has no matching
571                          * /etc/passwd entry and ask them to delete it.
572                          */
573                         printf (_("no matching password file entry in %s\n"),
574                                 pwd_file);
575                         printf (_("delete line '%s'? "), spe->line);
576                         *errors += 1;
577
578                         /*
579                          * prompt the user to delete the entry or not
580                          */
581                         if (yes_or_no (read_only)) {
582                                 goto delete_spw;
583                         }
584                 }
585
586                 /*
587                  * Warn if last password change in the future.  --marekm
588                  */
589                 if (   !quiet
590                     && (spw->sp_lstchg > (long) time ((time_t *) 0) / SCALE)) {
591                         printf (_("user %s: last password change in the future\n"),
592                                 spw->sp_namp);
593                         *errors += 1;
594                 }
595         }
596 }
597
598 /*
599  * pwck - verify password file integrity
600  */
601 int main (int argc, char **argv)
602 {
603         int errors = 0;
604         bool changed = false;
605
606         /*
607          * Get my name so that I can use it to report errors.
608          */
609         Prog = Basename (argv[0]);
610
611         (void) setlocale (LC_ALL, "");
612         (void) bindtextdomain (PACKAGE, LOCALEDIR);
613         (void) textdomain (PACKAGE);
614
615         OPENLOG ("pwck");
616
617         /* Parse the command line arguments */
618         process_flags (argc, argv);
619
620         open_files ();
621
622         if (sort_mode) {
623                 pw_sort ();
624                 if (is_shadow) {
625                         spw_sort ();
626                 }
627                 changed = true;
628         } else {
629                 check_pw_file (&errors, &changed);
630
631                 if (is_shadow) {
632                         check_spw_file (&errors, &changed);
633                 }
634         }
635
636         close_files (changed);
637
638         nscd_flush_cache ("passwd");
639
640         /*
641          * Tell the user what we did and exit.
642          */
643         if (0 != errors) {
644                 printf (changed ?
645                         _("%s: the files have been updated\n") :
646                         _("%s: no changes\n"), Prog);
647         }
648
649         closelog ();
650         exit ((0 != errors) ? E_BADENTRY : E_OKAY);
651 }
652