]> granicus.if.org Git - shadow/blob - src/pwck.c
* src/pwck.c: Use a bool when possible instead of int integers.
[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 (int changed);
84 static void check_pw_file (int *errors, int *changed);
85 static void check_spw_file (int *errors, int *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 (int 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, int *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 = 1;
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 %u\n"),
373                                 pwd->pw_name, 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 (spw == NULL) {
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 = -1;
431                                         sp.sp_lstchg =
432                                             time ((time_t *) 0) / (24L * 3600L);
433                                         *changed = 1;
434
435                                         if (spw_update (&sp) == 0) {
436                                                 fprintf (stderr,
437                                                          _("%s: can't update shadow entry for %s\n"),
438                                                          Prog, sp.sp_namp);
439                                                 exit (E_CANTUPDATE);
440                                         }
441                                         /* remove password from /etc/passwd */
442                                         pw = *pwd;
443                                         pw.pw_passwd = SHADOW_PASSWD_STRING;    /* XXX warning: const */
444                                         if (pw_update (&pw) == 0) {
445                                                 fprintf (stderr,
446                                                          _("%s: can't update passwd entry for %s\n"),
447                                                          Prog, pw.pw_name);
448                                                 exit (E_CANTUPDATE);
449                                         }
450                                 }
451                         }
452                 }
453         }
454 }
455
456 /*
457  * check_spw_file - check the content of the shadowed password file (shadow)
458  */
459 static void check_spw_file (int *errors, int *changed)
460 {
461         struct commonio_entry *spe, *tspe;
462         struct spwd *spw;
463
464         /*
465          * Loop through the entire shadow password file.
466          */
467         for (spe = __spw_get_head (); NULL != spe; spe = spe->next) {
468                 /*
469                  * Do not treat lines which were missing in shadow
470                  * and were added earlier.
471                  */
472                 if (spe->line == NULL) {
473                         continue;
474                 }
475
476                 /*
477                  * If this is a NIS line, skip it. You can't "know" what NIS
478                  * is going to do without directly asking NIS ...
479                  */
480                 if ((spe->line[0] == '+') || (spe->line[0] == '-')) {
481                         continue;
482                 }
483
484                 /*
485                  * Start with the entries that are completely corrupt. They
486                  * have no (struct spwd) entry because they couldn't be
487                  * parsed properly.
488                  */
489                 if (NULL == spe->eptr) {
490                         /*
491                          * Tell the user this entire line is bogus and ask
492                          * them to delete it.
493                          */
494                         puts (_("invalid shadow password file entry"));
495                         printf (_("delete line '%s'? "), spe->line);
496                         *errors += 1;
497
498                         /*
499                          * prompt the user to delete the entry or not
500                          */
501                         if (!yes_or_no (read_only)) {
502                                 continue;
503                         }
504
505                         /*
506                          * All shadow file deletions wind up here. This code
507                          * removes the current entry from the linked list.
508                          * When done, it skips back to the top of the loop
509                          * to try out the next list element.
510                          */
511                       delete_spw:
512                         SYSLOG ((LOG_INFO, "delete shadow line `%s'",
513                                  spe->line));
514                         *changed = 1;
515
516                         __spw_del_entry (spe);
517                         continue;
518                 }
519
520                 /*
521                  * Shadow password structure is good, start using it.
522                  */
523                 spw = spe->eptr;
524
525                 /*
526                  * Make sure this entry has a unique name.
527                  */
528                 for (tspe = __spw_get_head (); NULL != tspe; tspe = tspe->next) {
529                         const struct spwd *ent = tspe->eptr;
530
531                         /*
532                          * Don't check this entry
533                          */
534                         if (tspe == spe) {
535                                 continue;
536                         }
537
538                         /*
539                          * Don't check invalid entries.
540                          */
541                         if (NULL == ent) {
542                                 continue;
543                         }
544
545                         if (strcmp (spw->sp_namp, ent->sp_namp) != 0) {
546                                 continue;
547                         }
548
549                         /*
550                          * Tell the user this entry is a duplicate of
551                          * another and ask them to delete it.
552                          */
553                         puts (_("duplicate shadow password entry"));
554                         printf (_("delete line '%s'? "), spe->line);
555                         *errors += 1;
556
557                         /*
558                          * prompt the user to delete the entry or not
559                          */
560                         if (yes_or_no (read_only)) {
561                                 goto delete_spw;
562                         }
563                 }
564
565                 /*
566                  * Make sure this entry exists in the /etc/passwd
567                  * file.
568                  */
569                 if (pw_locate (spw->sp_namp) == NULL) {
570                         /*
571                          * Tell the user this entry has no matching
572                          * /etc/passwd entry and ask them to delete it.
573                          */
574                         printf (_("no matching password file entry in %s\n"),
575                                 pwd_file);
576                         printf (_("delete line '%s'? "), spe->line);
577                         *errors += 1;
578
579                         /*
580                          * prompt the user to delete the entry or not
581                          */
582                         if (yes_or_no (read_only)) {
583                                 goto delete_spw;
584                         }
585                 }
586
587                 /*
588                  * Warn if last password change in the future.  --marekm
589                  */
590                 if (   !quiet
591                     && (spw->sp_lstchg > time ((time_t *) 0) / SCALE)) {
592                         printf (_("user %s: last password change in the future\n"),
593                                 spw->sp_namp);
594                         *errors += 1;
595                 }
596         }
597 }
598
599 /*
600  * pwck - verify password file integrity
601  */
602 int main (int argc, char **argv)
603 {
604         int errors = 0;
605         int changed = 0;
606
607         /*
608          * Get my name so that I can use it to report errors.
609          */
610         Prog = Basename (argv[0]);
611
612         (void) setlocale (LC_ALL, "");
613         (void) bindtextdomain (PACKAGE, LOCALEDIR);
614         (void) textdomain (PACKAGE);
615
616         OPENLOG ("pwck");
617
618         /* Parse the command line arguments */
619         process_flags (argc, argv);
620
621         open_files ();
622
623         if (sort_mode) {
624                 pw_sort ();
625                 if (is_shadow) {
626                         spw_sort ();
627                 }
628                 changed = 1;
629         } else {
630                 check_pw_file (&errors, &changed);
631
632                 if (is_shadow) {
633                         check_spw_file (&errors, &changed);
634                 }
635         }
636
637         close_files (changed);
638
639         nscd_flush_cache ("passwd");
640
641         /*
642          * Tell the user what we did and exit.
643          */
644         if (errors != 0) {
645                 printf (changed ?
646                         _("%s: the files have been updated\n") :
647                         _("%s: no changes\n"), Prog);
648         }
649
650         closelog ();
651         exit (errors ? E_BADENTRY : E_OKAY);
652 }
653