]> granicus.if.org Git - shadow/blob - src/chage.c
Updated copyright dates.
[shadow] / src / chage.c
1 /*
2  * Copyright (c) 1989 - 1994, Julianne Frances Haugh
3  * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4  * Copyright (c) 2000 - 2006, Tomasz Kłoczko
5  * Copyright (c) 2007 - 2009, Nicolas François
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the copyright holders or contributors may not be used to
17  *    endorse or promote products derived from this software without
18  *    specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <config.h>
34
35 #ident "$Id$"
36
37 #include <ctype.h>
38 #include <fcntl.h>
39 #include <getopt.h>
40 #include <signal.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <sys/types.h>
44 #include <time.h>
45 #ifdef ACCT_TOOLS_SETUID
46 #ifdef USE_PAM
47 #include "pam_defs.h"
48 #endif                          /* USE_PAM */
49 #endif                          /* ACCT_TOOLS_SETUID */
50 #include <pwd.h>
51 #ifdef WITH_SELINUX
52 #include <selinux/selinux.h>
53 #include <selinux/av_permissions.h>
54 #endif
55 #include "exitcodes.h"
56 #include "prototypes.h"
57 #include "defines.h"
58 #include "pwio.h"
59 #include "shadowio.h"
60 /*
61  * Global variables
62  */
63 char *Prog;
64
65 static bool
66     dflg = false,               /* set last password change date */
67     Eflg = false,               /* set account expiration date */
68     Iflg = false,               /* set password inactive after expiration */
69     lflg = false,               /* show account aging information */
70     mflg = false,               /* set minimum number of days before password change */
71     Mflg = false,               /* set maximum number of days before password change */
72     Wflg = false;               /* set expiration warning days */
73 static bool amroot = false;
74
75 static bool pw_locked  = false; /* Indicate if the password file is locked */
76 static bool spw_locked = false; /* Indicate if the shadow file is locked */
77 /* The name and UID of the user being worked on */
78 static char user_name[BUFSIZ] = "";
79 static uid_t user_uid = -1;
80
81 static long mindays;
82 static long maxdays;
83 static long lastday;
84 static long warndays;
85 static long inactdays;
86 static long expdays;
87
88 #define EPOCH           "1969-12-31"
89
90 /* local function prototypes */
91 static bool isnum (const char *s);
92 static void usage (void);
93 static void date_to_str (char *buf, size_t maxsize, time_t date);
94 static int new_fields (void);
95 static void print_date (time_t date);
96 static void list_fields (void);
97 static void process_flags (int argc, char **argv);
98 static void check_flags (int argc, int opt_index);
99 static void check_perms (void);
100 static void open_files (bool readonly);
101 static void close_files (void);
102 static void fail_exit (int code);
103
104 /*
105  * fail_exit - do some cleanup and exit with the given error code
106  */
107 static void fail_exit (int code)
108 {
109         if (spw_locked) {
110                 if (spw_unlock () == 0) {
111                         fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, spw_dbname ());
112                         SYSLOG ((LOG_ERR, "failed to unlock %s", spw_dbname ()));
113                         /* continue */
114                 }
115         }
116         if (pw_locked) {
117                 if (pw_unlock () == 0) {
118                         fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, pw_dbname ());
119                         SYSLOG ((LOG_ERR, "failed to unlock %s", pw_dbname ()));
120                         /* continue */
121                 }
122         }
123         closelog ();
124
125 #ifdef WITH_AUDIT
126         if (E_SUCCESS != code) {
127                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
128                               "change age",
129                               user_name, (unsigned int) user_uid, 0);
130         }
131 #endif
132
133         exit (code);
134 }
135
136 /*
137  * isnum - determine whether or not a string is a number
138  */
139 static bool isnum (const char *s)
140 {
141         while ('\0' != *s) {
142                 if (!isdigit (*s)) {
143                         return false;
144                 }
145                 s++;
146         }
147         return true;
148 }
149
150 /*
151  * usage - print command line syntax and exit
152  */
153 static void usage (void)
154 {
155         fputs (_("Usage: chage [options] [LOGIN]\n"
156                  "\n"
157                  "Options:\n"
158                  "  -d, --lastday LAST_DAY        set last password change to LAST_DAY\n"
159                  "  -E, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE\n"
160                  "  -h, --help                    display this help message and exit\n"
161                  "  -I, --inactive INACTIVE       set password inactive after expiration\n"
162                  "                                to INACTIVE\n"
163                  "  -l, --list                    show account aging information\n"
164                  "  -m, --mindays MIN_DAYS        set minimum number of days before password\n"
165                  "                                change to MIN_DAYS\n"
166                  "  -M, --maxdays MAX_DAYS        set maximim number of days before password\n"
167                  "                                change to MAX_DAYS\n"
168                  "  -W, --warndays WARN_DAYS      set expiration warning days to WARN_DAYS\n"
169                  "\n"), stderr);
170         exit (E_USAGE);
171 }
172
173 static void date_to_str (char *buf, size_t maxsize, time_t date)
174 {
175         struct tm *tp;
176
177         tp = gmtime (&date);
178 #ifdef HAVE_STRFTIME
179         (void) strftime (buf, maxsize, "%Y-%m-%d", tp);
180 #else
181         (void) snprintf (buf, maxsize, "%04d-%02d-%02d",
182                          tp->tm_year + 1900, tp->tm_mon + 1, tp->tm_mday);
183 #endif                          /* HAVE_STRFTIME */
184 }
185
186 /*
187  * new_fields - change the user's password aging information interactively.
188  *
189  * prompt the user for all of the password age values. set the fields
190  * from the user's response, or leave alone if nothing was entered. The
191  * value (-1) is used to indicate the field should be removed if possible.
192  * any other negative value is an error. very large positive values will
193  * be handled elsewhere.
194  */
195 static int new_fields (void)
196 {
197         char buf[200];
198         char *cp;
199
200         (void) puts (_("Enter the new value, or press ENTER for the default"));
201         (void) puts ("");
202
203         snprintf (buf, sizeof buf, "%ld", mindays);
204         change_field (buf, sizeof buf, _("Minimum Password Age"));
205         mindays = strtol (buf, &cp, 10);
206         if (   ((0 == mindays) && ('\0' != *cp))
207             || (mindays < -1)) {
208                 return 0;
209         }
210
211         snprintf (buf, sizeof buf, "%ld", maxdays);
212         change_field (buf, sizeof buf, _("Maximum Password Age"));
213         maxdays = strtol (buf, &cp, 10);
214         if (   ((0 == maxdays) && ('\0' != *cp))
215             || (maxdays < -1)) {
216                 return 0;
217         }
218
219         date_to_str (buf, sizeof buf, lastday * SCALE);
220
221         change_field (buf, sizeof buf, _("Last Password Change (YYYY-MM-DD)"));
222
223         if (strcmp (buf, EPOCH) == 0) {
224                 lastday = -1;
225         } else {
226                 lastday = strtoday (buf);
227                 if (lastday == -1) {
228                         return 0;
229                 }
230         }
231
232         snprintf (buf, sizeof buf, "%ld", warndays);
233         change_field (buf, sizeof buf, _("Password Expiration Warning"));
234         warndays = strtol (buf, &cp, 10);
235         if (   ((warndays == 0) && ('\0' != *cp))
236             || (warndays < -1)) {
237                 return 0;
238         }
239
240         snprintf (buf, sizeof buf, "%ld", inactdays);
241         change_field (buf, sizeof buf, _("Password Inactive"));
242         inactdays = strtol (buf, &cp, 10);
243         if (   ((inactdays == 0) && ('\0' != *cp))
244             || (inactdays < -1)) {
245                 return 0;
246         }
247
248         date_to_str (buf, sizeof buf, expdays * SCALE);
249
250         change_field (buf, sizeof buf,
251                       _("Account Expiration Date (YYYY-MM-DD)"));
252
253         if (strcmp (buf, EPOCH) == 0) {
254                 expdays = -1;
255         } else {
256                 expdays = strtoday (buf);
257                 if (expdays == -1) {
258                         return 0;
259                 }
260         }
261
262         return 1;
263 }
264
265 static void print_date (time_t date)
266 {
267 #ifdef HAVE_STRFTIME
268         struct tm *tp;
269         char buf[80];
270
271         tp = gmtime (&date);
272         if (NULL == tp) {
273                 (void) printf ("time_t: %lu\n", date);
274         } else {
275                 (void) strftime (buf, sizeof buf, "%b %d, %Y", tp);
276                 (void) puts (buf);
277         }
278 #else
279         struct tm *tp;
280         char *cp = NULL;
281
282         tp = gmtime (&date);
283         if (NULL != tp) {
284                 cp = asctime (tp);
285         }
286         if (NULL != cp) {
287                 (void) printf ("%6.6s, %4.4s\n", cp + 4, cp + 20);
288         } else {
289                 (void) printf ("time_t: %lu\n", date);
290         }
291 #endif
292 }
293
294 /*
295  * list_fields - display the current values of the expiration fields
296  *
297  * display the password age information from the password fields. Date
298  * values will be displayed as a calendar date, or the word "never" if
299  * the date is 1/1/70, which is day number 0.
300  */
301 static void list_fields (void)
302 {
303         long changed = 0;
304         long expires;
305
306         /*
307          * The "last change" date is either "never" or the date the password
308          * was last modified. The date is the number of days since 1/1/1970.
309          */
310         (void) fputs (_("Last password change\t\t\t\t\t: "), stdout);
311         if (lastday < 0) {
312                 (void) puts (_("never"));
313         } else if (lastday == 0) {
314                 (void) puts (_("password must be changed"));
315         } else {
316                 changed = lastday * SCALE;
317                 print_date ((time_t) changed);
318         }
319
320         /*
321          * The password expiration date is determined from the last change
322          * date plus the number of days the password is valid for.
323          */
324         (void) fputs (_("Password expires\t\t\t\t\t: "), stdout);
325         if (lastday == 0) {
326                 (void) puts (_("password must be changed"));
327         } else if (   (lastday < 0)
328                    || (maxdays >= (10000 * (DAY / SCALE)))
329                    || (maxdays < 0)) {
330                 (void) puts (_("never"));
331         } else {
332                 expires = changed + maxdays * SCALE;
333                 print_date ((time_t) expires);
334         }
335
336         /*
337          * The account becomes inactive if the password is expired for more
338          * than "inactdays". The expiration date is calculated and the
339          * number of inactive days is added. The resulting date is when the
340          * active will be disabled.
341          */
342         (void) fputs (_("Password inactive\t\t\t\t\t: "), stdout);
343         if (lastday == 0) {
344                 (void) puts (_("password must be changed"));
345         } else if (   (lastday < 0)
346                    || (inactdays < 0)
347                    || (maxdays >= (10000 * (DAY / SCALE)))
348                    || (maxdays < 0)) {
349                 (void) puts (_("never"));
350         } else {
351                 expires = changed + (maxdays + inactdays) * SCALE;
352                 print_date ((time_t) expires);
353         }
354
355         /*
356          * The account will expire on the given date regardless of the
357          * password expiring or not.
358          */
359         (void) fputs (_("Account expires\t\t\t\t\t\t: "), stdout);
360         if (expdays < 0) {
361                 (void) puts (_("never"));
362         } else {
363                 expires = expdays * SCALE;
364                 print_date ((time_t) expires);
365         }
366
367         /*
368          * Start with the easy numbers - the number of days before the
369          * password can be changed, the number of days after which the
370          * password must be chaged, the number of days before the password
371          * expires that the user is told, and the number of days after the
372          * password expires that the account becomes unusable.
373          */
374         printf (_("Minimum number of days between password change\t\t: %ld\n"),
375                 mindays);
376         printf (_("Maximum number of days between password change\t\t: %ld\n"),
377                 maxdays);
378         printf (_("Number of days of warning before password expires\t: %ld\n"),
379                 warndays);
380 }
381
382 /*
383  * process_flags - parse the command line options
384  *
385  *      It will not return if an error is encountered.
386  */
387 static void process_flags (int argc, char **argv)
388 {
389         /*
390          * Parse the command line options.
391          */
392         int option_index = 0;
393         int c;
394         static struct option long_options[] = {
395                 {"lastday", required_argument, NULL, 'd'},
396                 {"expiredate", required_argument, NULL, 'E'},
397                 {"help", no_argument, NULL, 'h'},
398                 {"inactive", required_argument, NULL, 'I'},
399                 {"list", no_argument, NULL, 'l'},
400                 {"mindays", required_argument, NULL, 'm'},
401                 {"maxdays", required_argument, NULL, 'M'},
402                 {"warndays", required_argument, NULL, 'W'},
403                 {NULL, 0, NULL, '\0'}
404         };
405
406         while ((c =
407                 getopt_long (argc, argv, "d:E:hI:lm:M:W:", long_options,
408                              &option_index)) != -1) {
409                 switch (c) {
410                 case 'd':
411                         dflg = true;
412                         if (!isnum (optarg)) {
413                                 lastday = strtoday (optarg);
414                         } else {
415                                 lastday = strtol (optarg, 0, 10);
416                         }
417                         break;
418                 case 'E':
419                         Eflg = true;
420                         if (!isnum (optarg)) {
421                                 expdays = strtoday (optarg);
422                         } else {
423                                 expdays = strtol (optarg, 0, 10);
424                         }
425                         break;
426                 case 'h':
427                         usage ();
428                         break;
429                 case 'I':
430                         Iflg = true;
431                         inactdays = strtol (optarg, 0, 10);
432                         break;
433                 case 'l':
434                         lflg = true;
435                         break;
436                 case 'm':
437                         mflg = true;
438                         mindays = strtol (optarg, 0, 10);
439                         break;
440                 case 'M':
441                         Mflg = true;
442                         maxdays = strtol (optarg, 0, 10);
443                         break;
444                 case 'W':
445                         Wflg = true;
446                         warndays = strtol (optarg, 0, 10);
447                         break;
448                 default:
449                         usage ();
450                 }
451         }
452
453         check_flags (argc, optind);
454 }
455
456 /*
457  * check_flags - check flags and parameters consistency
458  *
459  *      It will not return if an error is encountered.
460  */
461 static void check_flags (int argc, int opt_index)
462 {
463         /*
464          * Make certain the flags do not conflict and that there is a user
465          * name on the command line.
466          */
467
468         if (argc != opt_index + 1) {
469                 usage ();
470         }
471
472         if (lflg && (mflg || Mflg || dflg || Wflg || Iflg || Eflg)) {
473                 fprintf (stderr,
474                          _("%s: do not include \"l\" with other flags\n"),
475                          Prog);
476                 usage ();
477         }
478 }
479
480 /*
481  * check_perms - check if the caller is allowed to add a group
482  *
483  *      Non-root users are only allowed to display their aging information.
484  *      (we will later make sure that the user is only listing her aging
485  *      information)
486  *
487  *      With PAM support, the setuid bit can be set on chage to allow
488  *      non-root users to groups.
489  *      Without PAM support, only users who can write in the group databases
490  *      can add groups.
491  *
492  *      It will not return if the user is not allowed.
493  */
494 static void check_perms (void)
495 {
496 #ifdef ACCT_TOOLS_SETUID
497 #ifdef USE_PAM
498         pam_handle_t *pamh = NULL;
499         struct passwd *pampw;
500         int retval;
501 #endif                          /* USE_PAM */
502 #endif                          /* ACCT_TOOLS_SETUID */
503
504         /*
505          * An unprivileged user can ask for their own aging information, but
506          * only root can change it, or list another user's aging
507          * information.
508          */
509
510         if (!amroot && !lflg) {
511                 fprintf (stderr, _("%s: Permission denied.\n"), Prog);
512                 fail_exit (E_NOPERM);
513         }
514
515 #ifdef ACCT_TOOLS_SETUID
516 #ifdef USE_PAM
517         pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
518         if (NULL == pampw) {
519                 fprintf (stderr,
520                          _("%s: Cannot determine your user name.\n"),
521                          Prog);
522                 exit (E_NOPERM);
523         }
524
525         retval = pam_start ("chage", pampw->pw_name, &conv, &pamh);
526
527         if (PAM_SUCCESS == retval) {
528                 retval = pam_authenticate (pamh, 0);
529         }
530
531         if (PAM_SUCCESS == retval) {
532                 retval = pam_acct_mgmt (pamh, 0);
533         }
534
535         if (NULL != pamh) {
536                 (void) pam_end (pamh, retval);
537         }
538         if (PAM_SUCCESS != retval) {
539                 fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
540                 fail_exit (E_NOPERM);
541         }
542 #endif                          /* USE_PAM */
543 #endif                          /* ACCT_TOOLS_SETUID */
544 }
545
546 /*
547  * open_files - open the shadow database
548  *
549  *      In read-only mode, the databases are not locked and are opened
550  *      only for reading.
551  */
552 static void open_files (bool readonly)
553 {
554         /*
555          * Lock and open the password file. This loads all of the password
556          * file entries into memory. Then we get a pointer to the password
557          * file entry for the requested user.
558          */
559         if (!readonly) {
560                 if (pw_lock () == 0) {
561                         fprintf (stderr,
562                                  _("%s: cannot lock %s; try again later.\n"),
563                                  Prog, pw_dbname ());
564                         fail_exit (E_NOPERM);
565                 }
566                 pw_locked = true;
567         }
568         if (pw_open (readonly ? O_RDONLY: O_RDWR) == 0) {
569                 fprintf (stderr, _("%s: cannot open %s\n"), Prog, pw_dbname ());
570                 SYSLOG ((LOG_WARN, "cannot open %s", pw_dbname ()));
571                 fail_exit (E_NOPERM);
572         }
573
574         /*
575          * For shadow password files we have to lock the file and read in
576          * the entries as was done for the password file. The user entries
577          * does not have to exist in this case; a new entry will be created
578          * for this user if one does not exist already.
579          */
580         if (!readonly) {
581                 if (spw_lock () == 0) {
582                         fprintf (stderr,
583                                  _("%s: cannot lock %s; try again later.\n"),
584                                  Prog, spw_dbname ());
585                         fail_exit (E_NOPERM);
586                 }
587                 spw_locked = true;
588         }
589         if (spw_open (readonly ? O_RDONLY: O_RDWR) == 0) {
590                 fprintf (stderr,
591                          _("%s: cannot open %s\n"), Prog, spw_dbname ());
592                 SYSLOG ((LOG_WARN, "cannot open %s", spw_dbname ()));
593                 fail_exit (E_NOPERM);
594         }
595 }
596
597 /*
598  * close_files - close and unlock the password/shadow databases
599  */
600 static void close_files (void)
601 {
602         /*
603          * Now close the shadow password file, which will cause all of the
604          * entries to be re-written.
605          */
606         if (spw_close () == 0) {
607                 fprintf (stderr,
608                          _("%s: failure while writing changes to %s\n"), Prog, spw_dbname ());
609                 SYSLOG ((LOG_ERR, "failure while writing changes to %s", spw_dbname ()));
610                 fail_exit (E_NOPERM);
611         }
612
613         /*
614          * Close the password file. If any entries were modified, the file
615          * will be re-written.
616          */
617         if (pw_close () == 0) {
618                 fprintf (stderr, _("%s: failure while writing changes to %s\n"), Prog, pw_dbname ());
619                 SYSLOG ((LOG_ERR, "failure while writing changes to %s", pw_dbname ()));
620                 fail_exit (E_NOPERM);
621         }
622         if (spw_unlock () == 0) {
623                 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, spw_dbname ());
624                 SYSLOG ((LOG_ERR, "failed to unlock %s", spw_dbname ()));
625                 /* continue */
626         }
627         spw_locked = false;
628         if (pw_unlock () == 0) {
629                 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, pw_dbname ());
630                 SYSLOG ((LOG_ERR, "failed to unlock %s", pw_dbname ()));
631                 /* continue */
632         }
633         pw_locked = false;
634 }
635
636 /*
637  * update_age - update the aging information in the database
638  *
639  *      It will not return in case of error
640  */
641 static void update_age (const struct spwd *sp, const struct passwd *pw)
642 {
643         struct spwd spwent;
644
645         /*
646          * There was no shadow entry. The new entry will have the encrypted
647          * password transferred from the normal password file along with the
648          * aging information.
649          */
650         if (NULL == sp) {
651                 struct passwd pwent = *pw;
652
653                 memzero (&spwent, sizeof spwent);
654                 spwent.sp_namp = xstrdup (pw->pw_name);
655                 spwent.sp_pwdp = xstrdup (pw->pw_passwd);
656                 spwent.sp_flag = SHADOW_SP_FLAG_UNSET;
657
658                 pwent.pw_passwd = SHADOW_PASSWD_STRING; /* XXX warning: const */
659                 if (pw_update (&pwent) == 0) {
660                         fprintf (stderr,
661                                  _("%s: failed to prepare the new %s entry '%s'\n"), Prog, pw_dbname (), pwent.pw_name);
662                         fail_exit (E_NOPERM);
663                 }
664         } else {
665                 spwent.sp_namp = xstrdup (sp->sp_namp);
666                 spwent.sp_pwdp = xstrdup (sp->sp_pwdp);
667                 spwent.sp_flag = sp->sp_flag;
668         }
669
670         /*
671          * Copy the fields back to the shadow file entry and write the
672          * modified entry back to the shadow file. Closing the shadow and
673          * password files will commit any changes that have been made.
674          */
675         spwent.sp_max = maxdays;
676         spwent.sp_min = mindays;
677         spwent.sp_lstchg = lastday;
678         spwent.sp_warn = warndays;
679         spwent.sp_inact = inactdays;
680         spwent.sp_expire = expdays;
681
682         if (spw_update (&spwent) == 0) {
683                 fprintf (stderr,
684                          _("%s: failed to prepare the new %s entry '%s'\n"), Prog, spw_dbname (), spwent.sp_namp);
685                 fail_exit (E_NOPERM);
686         }
687
688 }
689
690 /*
691  * get_defaults - get the value of the fields not set from the command line
692  */
693 static void get_defaults (const struct spwd *sp)
694 {
695         /*
696          * Set the fields that aren't being set from the command line from
697          * the password file.
698          */
699         if (NULL != sp) {
700                 if (!Mflg) {
701                         maxdays = sp->sp_max;
702                 }
703                 if (!mflg) {
704                         mindays = sp->sp_min;
705                 }
706                 if (!dflg) {
707                         lastday = sp->sp_lstchg;
708                 }
709                 if (!Wflg) {
710                         warndays = sp->sp_warn;
711                 }
712                 if (!Iflg) {
713                         inactdays = sp->sp_inact;
714                 }
715                 if (!Eflg) {
716                         expdays = sp->sp_expire;
717                 }
718         } else {
719                 /*
720                  * Use default values that will not change the behavior of the
721                  * account.
722                  */
723                 if (!Mflg) {
724                         maxdays = -1;
725                 }
726                 if (!mflg) {
727                         mindays = -1;
728                 }
729                 if (!dflg) {
730                         lastday = -1;
731                 }
732                 if (!Wflg) {
733                         warndays = -1;
734                 }
735                 if (!Iflg) {
736                         inactdays = -1;
737                 }
738                 if (!Eflg) {
739                         expdays = -1;
740                 }
741         }
742 }
743
744 /*
745  * chage - change a user's password aging information
746  *
747  *      This command controls the password aging information.
748  *
749  *      The valid options are
750  *
751  *      -d      set last password change date (*)
752  *      -E      set account expiration date (*)
753  *      -I      set password inactive after expiration (*)
754  *      -l      show account aging information
755  *      -M      set maximim number of days before password change (*)
756  *      -m      set minimum number of days before password change (*)
757  *      -W      set expiration warning days (*)
758  *
759  *      (*) requires root permission to execute.
760  *
761  *      All of the time fields are entered in the internal format which is
762  *      either seconds or days.
763  */
764
765 int main (int argc, char **argv)
766 {
767         const struct spwd *sp;
768         uid_t ruid;
769         gid_t rgid;
770         const struct passwd *pw;
771
772 #ifdef WITH_AUDIT
773         audit_help_open ();
774 #endif
775         sanitize_env ();
776         (void) setlocale (LC_ALL, "");
777         (void) bindtextdomain (PACKAGE, LOCALEDIR);
778         (void) textdomain (PACKAGE);
779
780         ruid = getuid ();
781         rgid = getgid ();
782         amroot = (ruid == 0);
783 #ifdef WITH_SELINUX
784         if (amroot && (is_selinux_enabled () > 0)) {
785                 amroot = (selinux_check_passwd_access (PASSWD__ROOTOK) == 0);
786         }
787 #endif
788
789         /*
790          * Get the program name so that error messages can use it.
791          */
792         Prog = Basename (argv[0]);
793
794         process_flags (argc, argv);
795
796         OPENLOG ("chage");
797
798         check_perms ();
799
800         if (!spw_file_present ()) {
801                 fprintf (stderr,
802                          _("%s: the shadow password file is not present\n"),
803                          Prog);
804                 SYSLOG ((LOG_WARN, "can't find the shadow password file"));
805                 closelog ();
806                 exit (E_SHADOW_NOTFOUND);
807         }
808
809         open_files (lflg);
810         /* Drop privileges */
811         if (lflg && (   (setregid (rgid, rgid) != 0)
812                      || (setreuid (ruid, ruid) != 0))) {
813                 fprintf (stderr, _("%s: failed to drop privileges (%s)\n"),
814                          Prog, strerror (errno));
815                 fail_exit (E_NOPERM);
816         }
817
818         pw = pw_locate (argv[optind]);
819         if (NULL == pw) {
820                 fprintf (stderr, _("%s: user '%s' does not exist in %s\n"),
821                          Prog, argv[optind], pw_dbname ());
822                 closelog ();
823                 exit (E_NOPERM);
824         }
825
826         STRFCPY (user_name, pw->pw_name);
827         user_uid = pw->pw_uid;
828
829         sp = spw_locate (argv[optind]);
830         get_defaults(sp);
831
832         /*
833          * Print out the expiration fields if the user has requested the
834          * list option.
835          */
836         if (lflg) {
837                 if (!amroot && (ruid != user_uid)) {
838                         fprintf (stderr, _("%s: Permission denied.\n"), Prog);
839                         fail_exit (E_NOPERM);
840                 }
841 #ifdef WITH_AUDIT
842                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
843                               "display aging info",
844                               user_name, (unsigned int) user_uid, 1);
845 #endif
846                 list_fields ();
847                 fail_exit (E_SUCCESS);
848         }
849
850         /*
851          * If none of the fields were changed from the command line, let the
852          * user interactively change them.
853          */
854         if (!mflg && !Mflg && !dflg && !Wflg && !Iflg && !Eflg) {
855                 printf (_("Changing the aging information for %s\n"),
856                         user_name);
857                 if (new_fields () == 0) {
858                         fprintf (stderr, _("%s: error changing fields\n"),
859                                  Prog);
860                         fail_exit (E_NOPERM);
861                 }
862 #ifdef WITH_AUDIT
863                 else {
864                         audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
865                                       "change all aging information",
866                                       user_name, (unsigned int) user_uid, 1);
867                 }
868 #endif
869         } else {
870 #ifdef WITH_AUDIT
871                 if (Mflg) {
872                         audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
873                                       "change max age",
874                                       user_name, (unsigned int) user_uid, 1);
875                 }
876                 if (mflg) {
877                         audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
878                                       "change min age",
879                                       user_name, (unsigned int) user_uid, 1);
880                 }
881                 if (dflg) {
882                         audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
883                                       "change last change date",
884                                       user_name, (unsigned int) user_uid, 1);
885                 }
886                 if (Wflg) {
887                         audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
888                                       "change passwd warning",
889                                       user_name, (unsigned int) user_uid, 1);
890                 }
891                 if (Iflg) {
892                         audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
893                                       "change inactive days",
894                                       user_name, (unsigned int) user_uid, 1);
895                 }
896                 if (Eflg) {
897                         audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
898                                       "change passwd expiration",
899                                       user_name, (unsigned int) user_uid, 1);
900                 }
901 #endif
902         }
903
904         update_age (sp, pw);
905
906         close_files ();
907
908         SYSLOG ((LOG_INFO, "changed password expiry for %s", user_name));
909
910         closelog ();
911         exit (E_SUCCESS);
912 }
913