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