2 * Copyright 1989 - 1994, Julianne Frances Haugh
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
40 #include <sys/types.h>
47 #include <selinux/selinux.h>
48 #include <selinux/av_permissions.h>
50 #include "exitcodes.h"
51 #include "prototypes.h"
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;
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;
80 static long inactdays;
84 static pam_handle_t *pamh = NULL;
87 #define EPOCH "1969-12-31"
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);
103 * fail_exit - do some cleanup and exit with the given error code
105 static void fail_exit (int code)
116 if (E_SUCCESS != code) {
117 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "change age",
118 user_name, user_uid, 0);
124 /* If there is a PAM error, pam_end will be called by the
126 * We always end the pam transaction with PAM_SUCCESS here.
128 pam_end (pamh, PAM_SUCCESS);
136 * isnum - determine whether or not a string is a number
138 int isnum (const char *s)
150 * usage - print command line syntax and exit
152 static void usage (void)
154 fprintf (stderr, _("Usage: chage [options] [LOGIN]\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"
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"
172 static void date_to_str (char *buf, size_t maxsize, time_t date)
178 strftime (buf, maxsize, "%Y-%m-%d", tp);
180 snprintf (buf, maxsize, "%04d-%02d-%02d",
181 tp->tm_year + 1900, tp->tm_mon + 1, tp->tm_mday);
182 #endif /* HAVE_STRFTIME */
186 * new_fields - change the user's password aging information interactively.
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.
194 static int new_fields (void)
199 printf (_("Enter the new value, or press ENTER for the default\n"));
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))
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))
218 date_to_str (buf, sizeof buf, lastday * SCALE);
220 change_field (buf, sizeof buf, _("Last Password Change (YYYY-MM-DD)"));
222 if (strcmp (buf, EPOCH) == 0) {
225 lastday = strtoday (buf);
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)) {
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)) {
247 date_to_str (buf, sizeof buf, expdays * SCALE);
249 change_field (buf, sizeof buf,
250 _("Account Expiration Date (YYYY-MM-DD)"));
252 if (strcmp (buf, EPOCH) == 0) {
255 expdays = strtoday (buf);
264 static void print_date (time_t date)
271 strftime (buf, sizeof buf, "%b %d, %Y", tp);
279 printf ("%6.6s, %4.4s\n", cp + 4, cp + 20);
284 * list_fields - display the current values of the expiration fields
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.
290 static void list_fields (void)
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.
299 printf (_("Last password change\t\t\t\t\t: "));
301 printf (_("never\n"));
302 } else if (lastday == 0) {
303 printf (_("password must be changed\n"));
305 changed = lastday * SCALE;
306 print_date (changed);
310 * The password expiration date is determined from the last change
311 * date plus the number of days the password is valid for.
313 printf (_("Password expires\t\t\t\t\t: "));
314 if ((lastday <= 0) || (maxdays >= (10000 * (DAY / SCALE)))
316 printf (_("never\n"));
318 expires = changed + maxdays * SCALE;
319 print_date (expires);
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.
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"));
333 expires = changed + (maxdays + inactdays) * SCALE;
334 print_date (expires);
338 * The account will expire on the given date regardless of the
339 * password expiring or not.
341 printf (_("Account expires\t\t\t\t\t\t: "));
343 printf (_("never\n"));
345 expires = expdays * SCALE;
346 print_date (expires);
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.
356 printf (_("Minimum number of days between password change\t\t: %ld\n"),
358 printf (_("Maximum number of days between password change\t\t: %ld\n"),
360 printf (_("Number of days of warning before password expires\t: %ld\n"),
365 * process_flags - parse the command line options
367 * It will not return if an error is encountered.
369 static void process_flags (int argc, char **argv)
372 * Parse the command line options.
374 int option_index = 0;
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'}
389 getopt_long (argc, argv, "d:E:hI:lm:M:W:", long_options,
390 &option_index)) != -1) {
394 if (!isnum (optarg)) {
395 lastday = strtoday (optarg);
397 lastday = strtol (optarg, 0, 10);
402 if (!isnum (optarg)) {
403 expdays = strtoday (optarg);
405 expdays = strtol (optarg, 0, 10);
413 inactdays = strtol (optarg, 0, 10);
420 mindays = strtol (optarg, 0, 10);
424 maxdays = strtol (optarg, 0, 10);
428 warndays = strtol (optarg, 0, 10);
435 check_flags (argc, optind);
439 * check_flags - check flags and parameters consistency
441 * It will not return if an error is encountered.
443 static void check_flags (int argc, int opt_index)
446 * Make certain the flags do not conflict and that there is a user
447 * name on the command line.
450 if (argc != opt_index + 1) {
454 if (lflg && (mflg || Mflg || dflg || Wflg || Iflg || Eflg)) {
456 _("%s: do not include \"l\" with other flags\n"),
463 * check_perms - check if the caller is allowed to add a group
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
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
474 * It will not return if the user is not allowed.
476 static void check_perms (void)
479 struct passwd *pampw;
484 * An unprivileged user can ask for their own aging information, but
485 * only root can change it, or list another user's aging
489 if (!amroot && !lflg) {
490 fprintf (stderr, _("%s: Permission denied.\n"), Prog);
491 fail_exit (E_NOPERM);
495 retval = PAM_SUCCESS;
497 pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
499 retval = PAM_USER_UNKNOWN;
502 if (retval == PAM_SUCCESS) {
503 retval = pam_start ("chage", pampw->pw_name, &conv, &pamh);
506 if (retval == PAM_SUCCESS) {
507 retval = pam_authenticate (pamh, 0);
508 if (retval != PAM_SUCCESS) {
509 pam_end (pamh, retval);
513 if (retval == PAM_SUCCESS) {
514 retval = pam_acct_mgmt (pamh, 0);
515 if (retval != PAM_SUCCESS) {
516 pam_end (pamh, retval);
520 if (retval != PAM_SUCCESS) {
521 fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
523 fail_exit (E_NOPERM);
529 * open_files - open the shadow database
531 * In read-only mode, the databases are not locked and are opened
534 static void open_files (int readonly)
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.
541 if (!readonly && (pw_lock () == 0)) {
543 _("%s: can't lock password file\n"), Prog);
544 SYSLOG ((LOG_ERR, "failed locking %s", PASSWD_FILE));
545 fail_exit (E_NOPERM);
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);
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.
562 if (!readonly && (spw_lock () == 0)) {
564 _("%s: can't lock shadow password file\n"), Prog);
565 SYSLOG ((LOG_ERR, "failed locking %s", SHADOW_FILE));
566 fail_exit (E_NOPERM);
571 if (spw_open (readonly ? O_RDONLY: O_RDWR) == 0) {
573 _("%s: can't open shadow password file\n"), Prog);
574 SYSLOG ((LOG_ERR, "failed opening %s", SHADOW_FILE));
575 fail_exit (E_NOPERM);
580 * close_files - close and unlock the password/shadow databases
582 static void close_files (void)
585 * Now close the shadow password file, which will cause all of the
586 * entries to be re-written.
588 if (spw_close () == 0) {
590 _("%s: can't rewrite shadow password file\n"), Prog);
591 SYSLOG ((LOG_ERR, "failed rewriting %s", SHADOW_FILE));
592 fail_exit (E_NOPERM);
596 * Close the password file. If any entries were modified, the file
597 * will be re-written.
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);
611 * update_age - update the aging information in the database
613 * It will not return in case of error
615 static void update_age (const struct spwd *sp, const struct passwd *pw)
620 * There was no shadow entry. The new entry will have the encrypted
621 * password transferred from the normal password file along with the
625 struct passwd pwent = *pw;
627 memzero (&spwent, sizeof spwent);
628 spwent.sp_namp = xstrdup (pw->pw_name);
629 spwent.sp_pwdp = xstrdup (pw->pw_passwd);
632 pwent.pw_passwd = SHADOW_PASSWD_STRING; /* XXX warning: const */
633 if (pw_update (&pwent) == 0) {
635 _("%s: can't update password file\n"), Prog);
636 SYSLOG ((LOG_ERR, "failed updating %s", PASSWD_FILE));
637 fail_exit (E_NOPERM);
640 spwent.sp_namp = xstrdup (sp->sp_namp);
641 spwent.sp_pwdp = xstrdup (sp->sp_pwdp);
642 spwent.sp_flag = sp->sp_flag;
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.
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;
657 if (spw_update (&spwent) == 0) {
659 _("%s: can't update shadow password file\n"), Prog);
660 SYSLOG ((LOG_ERR, "failed updating %s", SHADOW_FILE));
661 fail_exit (E_NOPERM);
667 * get_defaults - get the value of the fields not set from the command line
669 static void get_defaults (const struct spwd *sp)
672 * Set the fields that aren't being set from the command line from
677 maxdays = sp->sp_max;
680 mindays = sp->sp_min;
683 lastday = sp->sp_lstchg;
686 warndays = sp->sp_warn;
689 inactdays = sp->sp_inact;
692 expdays = sp->sp_expire;
696 * Use default values that will not change the behavior of the
721 * chage - change a user's password aging information
723 * This command controls the password aging information.
725 * The valid options are
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 (*)
735 * (*) requires root permission to execute.
737 * All of the time fields are entered in the internal format which is
738 * either seconds or days.
741 int main (int argc, char **argv)
743 const struct spwd *sp;
746 const struct passwd *pw;
752 setlocale (LC_ALL, "");
753 bindtextdomain (PACKAGE, LOCALEDIR);
754 textdomain (PACKAGE);
758 amroot = (ruid == 0);
760 if (amroot && (is_selinux_enabled () > 0)) {
761 amroot = (selinux_check_passwd_access (PASSWD__ROOTOK) == 0);
766 * Get the program name so that error messages can use it.
768 Prog = Basename (argv[0]);
770 process_flags (argc, argv);
776 if (!spw_file_present ()) {
778 _("%s: the shadow password file is not present\n"),
780 SYSLOG ((LOG_ERR, "can't find the shadow password file"));
782 exit (E_SHADOW_NOTFOUND);
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);
793 pw = pw_locate (argv[optind]);
795 fprintf (stderr, _("%s: unknown user %s\n"), Prog,
801 STRFCPY (user_name, pw->pw_name);
802 user_uid = pw->pw_uid;
804 sp = spw_locate (argv[optind]);
808 * Print out the expiration fields if the user has requested the
812 if (!amroot && (ruid != user_uid)) {
813 fprintf (stderr, _("%s: Permission denied.\n"), Prog);
814 fail_exit (E_NOPERM);
817 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "display aging info",
818 user_name, user_uid, 1);
821 fail_exit (E_SUCCESS);
825 * If none of the fields were changed from the command line, let the
826 * user interactively change them.
828 if (!mflg && !Mflg && !dflg && !Wflg && !Iflg && !Eflg) {
829 printf (_("Changing the aging information for %s\n"),
831 if (new_fields () == 0) {
832 fprintf (stderr, _("%s: error changing fields\n"),
834 fail_exit (E_NOPERM);
838 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
839 "change all aging information",
840 user_name, user_uid, 1);
846 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
847 "change max age", user_name,
851 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
852 "change min age", user_name,
856 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
857 "change last change date", user_name,
861 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
862 "change passwd warning", user_name,
866 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
867 "change inactive days", user_name,
871 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
872 "change passwd expiration", user_name,
882 SYSLOG ((LOG_INFO, "changed password expiry for %s", user_name));
885 pam_end (pamh, PAM_SUCCESS);