2 * Copyright (c) 1989 - 1994, Julianne Frances Haugh
3 * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4 * Copyright (c) 2001 - 2006, Tomasz Kłoczko
5 * Copyright (c) 2007 - 2010, Nicolas François
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
43 #include <sys/types.h>
45 #include <selinux/selinux.h>
46 #include <selinux/flask.h>
47 #include <selinux/av_permissions.h>
48 #include <selinux/context.h>
49 #endif /* WITH_SELINUX */
54 #include "prototypes.h"
63 #define E_SUCCESS 0 /* success */
64 #define E_NOPERM 1 /* permission denied */
65 #define E_USAGE 2 /* invalid combination of options */
66 #define E_FAILURE 3 /* unexpected failure, nothing done */
67 #define E_MISSING 4 /* unexpected failure, passwd file missing */
68 #define E_PWDBUSY 5 /* passwd file busy, try again later */
69 #define E_BAD_ARG 6 /* invalid argument to option */
73 const char *Prog; /* Program name */
75 static char *name; /* The name of user whose password is being changed */
76 static char *myname; /* The current user's name */
77 static bool amroot; /* The caller's real UID was 0 */
80 aflg = false, /* -a - show status for all users */
81 dflg = false, /* -d - delete password */
82 eflg = false, /* -e - force password change */
83 iflg = false, /* -i - set inactive days */
84 kflg = false, /* -k - change only if expired */
85 lflg = false, /* -l - lock the user's password */
86 nflg = false, /* -n - set minimum days */
87 qflg = false, /* -q - quiet mode */
88 Sflg = false, /* -S - show password status */
89 uflg = false, /* -u - unlock the user's password */
90 wflg = false, /* -w - set warning days */
91 xflg = false; /* -x - set maximum days */
94 * set to 1 if there are any flags which require root privileges,
95 * and require username to be specified
97 static bool anyflag = false;
99 static long age_min = 0; /* Minimum days before change */
100 static long age_max = 0; /* Maximum days until change */
101 static long warn = 0; /* Warning days before change */
102 static long inact = 0; /* Days without change before locked */
105 static bool do_update_age = false;
106 #endif /* ! USE_PAM */
108 static bool pw_locked = false;
109 static bool spw_locked = false;
113 * Size of the biggest passwd:
125 static char crypt_passwd[256];
126 static bool do_update_pwd = false;
127 #endif /* !USE_PAM */
130 * External identifiers
133 /* local function prototypes */
134 static /*@noreturn@*/void usage (int);
137 static bool reuse (const char *, const struct passwd *);
138 static int new_password (const struct passwd *);
140 static void check_password (const struct passwd *, const struct spwd *);
141 #endif /* !USE_PAM */
142 static /*@observer@*/const char *date_to_str (time_t);
143 static /*@observer@*/const char *pw_status (const char *);
144 static void print_status (const struct passwd *);
145 static /*@noreturn@*/void fail_exit (int);
146 static /*@noreturn@*/void oom (void);
147 static char *update_crypt_pw (char *);
148 static void update_noshadow (void);
150 static void update_shadow (void);
152 static int check_selinux_access (const char *changed_user,
154 access_vector_t requested_access);
155 #endif /* WITH_SELINUX */
158 * usage - print command usage and exit
160 static /*@noreturn@*/void usage (int status)
163 fputs (_("Usage: passwd [options] [LOGIN]\n"
166 " -a, --all report password status on all accounts\n"
167 " -d, --delete delete the password for the named account\n"
168 " -e, --expire force expire the password for the named account\n"
169 " -h, --help display this help message and exit\n"
170 " -k, --keep-tokens change password only if expired\n"
171 " -i, --inactive INACTIVE set password inactive after expiration\n"
173 " -l, --lock lock the password of the named account\n"
174 " -n, --mindays MIN_DAYS set minimum number of days before password\n"
175 " change to MIN_DAYS\n"
176 " -q, --quiet quiet mode\n"
177 " -r, --repository REPOSITORY change password in REPOSITORY repository\n"
178 " -S, --status report password status on the named account\n"
179 " -u, --unlock unlock the password of the named account\n"
180 " -w, --warndays WARN_DAYS set expiration warning days to WARN_DAYS\n"
181 " -x, --maxdays MAX_DAYS set maximum number of days before password\n"
182 " change to MAX_DAYS\n"
183 "\n"), (E_SUCCESS != status) ? stderr : stdout);
188 static bool reuse (const char *pass, const struct passwd *pw)
190 #ifdef HAVE_LIBCRACK_HIST
193 #ifdef HAVE_LIBCRACK_PW
194 const char *FascistHistoryPw (const char *, const struct passwd *);
196 reason = FascistHistory (pass, pw);
197 #else /* !HAVE_LIBCRACK_PW */
198 const char *FascistHistory (const char *, int);
200 reason = FascistHistory (pass, pw->pw_uid);
201 #endif /* !HAVE_LIBCRACK_PW */
202 if (NULL != reason) {
203 (void) printf (_("Bad password: %s. "), reason);
206 #endif /* HAVE_LIBCRACK_HIST */
211 * new_password - validate old password and replace with new (both old and
212 * new in global "char crypt_passwd[128]")
214 static int new_password (const struct passwd *pw)
216 char *clear; /* Pointer to clear text */
217 char *cipher; /* Pointer to cipher text */
218 char *cp; /* Pointer to getpass() response */
219 char orig[200]; /* Original password */
220 char pass[200]; /* New password */
221 int i; /* Counter for retries */
223 int pass_max_len = -1;
226 #ifdef HAVE_LIBCRACK_HIST
227 int HistUpdate (const char *, const char *);
228 #endif /* HAVE_LIBCRACK_HIST */
231 * Authenticate the user. The user will be prompted for their own
235 if (!amroot && ('\0' != crypt_passwd[0])) {
236 clear = getpass (_("Old password: "));
241 cipher = pw_encrypt (clear, crypt_passwd);
242 if (strcmp (cipher, crypt_passwd) != 0) {
245 SYSLOG ((LOG_WARN, "incorrect password for %s",
248 (void) fprintf (stderr,
249 _("Incorrect password for %s.\n"),
253 STRFCPY (orig, clear);
261 * Get the new password. The user is prompted for the new password
262 * and has five tries to get it right. The password will be tested
263 * for strength, unless it is the root user. This provides an escape
264 * for initial login passwords.
266 method = getdef_str ("ENCRYPT_METHOD");
267 if (NULL == method) {
268 if (!getdef_bool ("MD5_CRYPT_ENAB")) {
269 pass_max_len = getdef_num ("PASS_MAX_LEN", 8);
272 if ( (strcmp (method, "MD5") == 0)
274 || (strcmp (method, "SHA256") == 0)
275 || (strcmp (method, "SHA512") == 0)
276 #endif /* USE_SHA_CRYPT */
280 pass_max_len = getdef_num ("PASS_MAX_LEN", 8);
284 if (pass_max_len == -1) {
286 "Enter the new password (minimum of %d characters)\n"
287 "Please use a combination of upper and lower case letters and numbers.\n"),
288 getdef_num ("PASS_MIN_LEN", 5));
291 "Enter the new password (minimum of %d, maximum of %d characters)\n"
292 "Please use a combination of upper and lower case letters and numbers.\n"),
293 getdef_num ("PASS_MIN_LEN", 5), pass_max_len);
298 for (i = getdef_num ("PASS_CHANGE_TRIES", 5); i > 0; i--) {
299 cp = getpass (_("New password: "));
301 memzero (orig, sizeof orig);
304 if (warned && (strcmp (pass, cp) != 0)) {
310 if (!amroot && (!obscure (orig, pass, pw) || reuse (pass, pw))) {
311 (void) puts (_("Try again."));
316 * If enabled, warn about weak passwords even if you are
317 * root (enter this password again to use it anyway).
320 if (amroot && !warned && getdef_bool ("PASS_ALWAYS_WARN")
321 && (!obscure (orig, pass, pw) || reuse (pass, pw))) {
322 (void) puts (_("\nWarning: weak password (enter it again to use it anyway)."));
326 cp = getpass (_("Re-enter new password: "));
328 memzero (orig, sizeof orig);
331 if (strcmp (cp, pass) != 0) {
332 (void) fputs (_("They don't match; try again.\n"), stderr);
338 memzero (orig, sizeof orig);
341 memzero (pass, sizeof pass);
346 * Encrypt the password, then wipe the cleartext password.
348 cp = pw_encrypt (pass, crypt_make_salt (NULL, NULL));
349 memzero (pass, sizeof pass);
351 #ifdef HAVE_LIBCRACK_HIST
352 HistUpdate (pw->pw_name, crypt_passwd);
353 #endif /* HAVE_LIBCRACK_HIST */
354 STRFCPY (crypt_passwd, cp);
359 * check_password - test a password to see if it can be changed
361 * check_password() sees if the invoker has permission to change the
362 * password for the given user.
364 static void check_password (const struct passwd *pw, const struct spwd *sp)
369 exp_status = isexpired (pw, sp);
372 * If not expired and the "change only if expired" option (idea from
373 * PAM) was specified, do nothing. --marekm
375 if (kflg && (0 == exp_status)) {
380 * Root can change any password any time.
389 * Expired accounts cannot be changed ever. Passwords which are
390 * locked may not be changed. Passwords where min > max may not be
391 * changed. Passwords which have been inactive too long cannot be
394 if ( (sp->sp_pwdp[0] == '!')
396 || ( (sp->sp_max >= 0)
397 && (sp->sp_min > sp->sp_max))) {
398 (void) fprintf (stderr,
399 _("The password for %s cannot be changed.\n"),
401 SYSLOG ((LOG_WARN, "password locked for '%s'", sp->sp_namp));
407 * Passwords may only be changed after sp_min time is up.
409 if (sp->sp_lstchg > 0) {
411 last = sp->sp_lstchg * SCALE;
412 ok = last + (sp->sp_min > 0 ? sp->sp_min * SCALE : 0);
415 (void) fprintf (stderr,
416 _("The password for %s cannot be changed yet.\n"),
418 SYSLOG ((LOG_WARN, "now < minimum age for '%s'", pw->pw_name));
424 #endif /* !USE_PAM */
426 static /*@observer@*/const char *date_to_str (time_t t)
433 (void) strftime (buf, sizeof buf, "%m/%d/%Y", tm);
434 #else /* !HAVE_STRFTIME */
435 (void) snprintf (buf, sizeof buf, "%02d/%02d/%04d",
436 tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900);
437 #endif /* !HAVE_STRFTIME */
441 static /*@observer@*/const char *pw_status (const char *pass)
443 if (*pass == '*' || *pass == '!') {
453 * print_status - print current password status
455 static void print_status (const struct passwd *pw)
459 sp = getspnam (pw->pw_name); /* local, no need for xgetspnam */
461 (void) printf ("%s %s %s %ld %ld %ld %ld\n",
463 pw_status (sp->sp_pwdp),
464 date_to_str (sp->sp_lstchg * SCALE),
465 (sp->sp_min * SCALE) / DAY,
466 (sp->sp_max * SCALE) / DAY,
467 (sp->sp_warn * SCALE) / DAY,
468 (sp->sp_inact * SCALE) / DAY);
470 (void) printf ("%s %s\n",
471 pw->pw_name, pw_status (pw->pw_passwd));
476 static /*@noreturn@*/void fail_exit (int status)
479 if (pw_unlock () == 0) {
480 (void) fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, pw_dbname ());
481 SYSLOG ((LOG_ERR, "failed to unlock %s", pw_dbname ()));
487 if (spw_unlock () == 0) {
488 (void) fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, spw_dbname ());
489 SYSLOG ((LOG_ERR, "failed to unlock %s", spw_dbname ()));
497 static /*@noreturn@*/void oom (void)
499 (void) fprintf (stderr, _("%s: out of memory\n"), Prog);
500 fail_exit (E_FAILURE);
503 static char *update_crypt_pw (char *cp)
507 cp = xstrdup (crypt_passwd);
509 #endif /* !USE_PAM */
515 if (uflg && *cp == '!') {
517 (void) fprintf (stderr,
518 _("%s: unlocking the password would result in a passwordless account.\n"
519 "You should set a password with usermod -p to unlock the password of this account.\n"),
521 fail_exit (E_FAILURE);
527 if (lflg && *cp != '!') {
528 char *newpw = xmalloc (strlen (cp) + 2);
538 static void update_noshadow (void)
540 const struct passwd *pw;
543 if (pw_lock () == 0) {
544 (void) fprintf (stderr,
545 _("%s: cannot lock %s; try again later.\n"),
550 if (pw_open (O_RDWR) == 0) {
551 (void) fprintf (stderr,
552 _("%s: cannot open %s\n"),
554 SYSLOG ((LOG_WARN, "cannot open %s", pw_dbname ()));
555 fail_exit (E_MISSING);
557 pw = pw_locate (name);
559 (void) fprintf (stderr,
560 _("%s: user '%s' does not exist in %s\n"),
561 Prog, name, pw_dbname ());
562 fail_exit (E_NOPERM);
568 npw->pw_passwd = update_crypt_pw (npw->pw_passwd);
569 if (pw_update (npw) == 0) {
570 (void) fprintf (stderr,
571 _("%s: failed to prepare the new %s entry '%s'\n"),
572 Prog, pw_dbname (), npw->pw_name);
573 fail_exit (E_FAILURE);
575 if (pw_close () == 0) {
576 (void) fprintf (stderr,
577 _("%s: failure while writing changes to %s\n"),
579 SYSLOG ((LOG_ERR, "failure while writing changes to %s", pw_dbname ()));
580 fail_exit (E_FAILURE);
582 if (pw_unlock () == 0) {
583 (void) fprintf (stderr,
584 _("%s: failed to unlock %s\n"),
586 SYSLOG ((LOG_ERR, "failed to unlock %s", pw_dbname ()));
592 static void update_shadow (void)
594 const struct spwd *sp;
597 if (spw_lock () == 0) {
598 (void) fprintf (stderr,
599 _("%s: cannot lock %s; try again later.\n"),
600 Prog, spw_dbname ());
604 if (spw_open (O_RDWR) == 0) {
605 (void) fprintf (stderr,
606 _("%s: cannot open %s\n"),
607 Prog, spw_dbname ());
608 SYSLOG ((LOG_WARN, "cannot open %s", spw_dbname ()));
609 fail_exit (E_FAILURE);
611 sp = spw_locate (name);
613 /* Try to update the password in /etc/passwd instead. */
616 if (spw_unlock () == 0) {
617 (void) fprintf (stderr,
618 _("%s: failed to unlock %s\n"),
619 Prog, spw_dbname ());
620 SYSLOG ((LOG_ERR, "failed to unlock %s", spw_dbname ()));
626 nsp = __spw_dup (sp);
630 nsp->sp_pwdp = update_crypt_pw (nsp->sp_pwdp);
632 nsp->sp_max = (age_max * DAY) / SCALE;
635 nsp->sp_min = (age_min * DAY) / SCALE;
638 nsp->sp_warn = (warn * DAY) / SCALE;
641 nsp->sp_inact = (inact * DAY) / SCALE;
645 nsp->sp_lstchg = (long) time ((time_t *) 0) / SCALE;
646 if (0 == nsp->sp_lstchg) {
647 /* Better disable aging than requiring a password
652 #endif /* !USE_PAM */
655 * Force change on next login, like SunOS 4.x passwd -e or Solaris
656 * 2.x passwd -f. Solaris 2.x seems to do the same thing (set
663 if (spw_update (nsp) == 0) {
664 (void) fprintf (stderr,
665 _("%s: failed to prepare the new %s entry '%s'\n"),
666 Prog, spw_dbname (), nsp->sp_namp);
667 fail_exit (E_FAILURE);
669 if (spw_close () == 0) {
670 (void) fprintf (stderr,
671 _("%s: failure while writing changes to %s\n"),
672 Prog, spw_dbname ());
673 SYSLOG ((LOG_ERR, "failure while writing changes to %s", spw_dbname ()));
674 fail_exit (E_FAILURE);
676 if (spw_unlock () == 0) {
677 (void) fprintf (stderr,
678 _("%s: failed to unlock %s\n"),
679 Prog, spw_dbname ());
680 SYSLOG ((LOG_ERR, "failed to unlock %s", spw_dbname ()));
687 static int check_selinux_access (const char *changed_user,
689 access_vector_t requested_access)
692 security_context_t user_context;
696 /* if in permissive mode then allow the operation */
697 if (security_getenforce() == 0) {
701 /* get the context of the process which executed passwd */
702 if (getprevcon(&user_context) != 0) {
706 /* get the "user" portion of the context (the part before the first
708 c = context_new(user_context);
709 user = context_user_get(c);
711 /* if changing a password for an account with UID==0 or for an account
712 where the identity matches then return success */
713 if (changed_uid != 0 && strcmp(changed_user, user) == 0) {
716 struct av_decision avd;
718 retval = security_compute_av(user_context,
724 ((requested_access & avd.allowed) == requested_access)) {
729 freecon(user_context);
733 #endif /* WITH_SELINUX */
736 * passwd - change a user's password file information
738 * This command controls the password file and commands which are used
741 * The valid options are
743 * -d delete the password for the named account (*)
744 * -e expire the password for the named account (*)
745 * -f execute chfn command to interpret flags
746 * -g execute gpasswd command to interpret flags
747 * -i # set sp_inact to # days (*)
748 * -k change password only if expired
749 * -l lock the password of the named account (*)
750 * -n # set sp_min to # days (*)
751 * -r # change password in # repository
752 * -s execute chsh command to interpret flags
753 * -S show password status of named account
754 * -u unlock the password of the named account (*)
755 * -w # set sp_warn to # days (*)
756 * -x # set sp_max to # days (*)
758 * (*) requires root permission to execute.
760 * All of the time fields are entered in days and converted to the
761 * appropriate internal format. For finer resolute the chage
762 * command must be used.
764 int main (int argc, char **argv)
766 const struct passwd *pw; /* Password file entry for user */
769 char *cp; /* Miscellaneous character pointing */
771 const struct spwd *sp; /* Shadow file entry for user */
772 #endif /* !USE_PAM */
774 (void) setlocale (LC_ALL, "");
775 (void) bindtextdomain (PACKAGE, LOCALEDIR);
776 (void) textdomain (PACKAGE);
779 * The program behaves differently when executed by root than when
780 * executed by a normal user.
782 amroot = (getuid () == 0);
785 * Get the program name. The program name is used as a prefix to
786 * most error messages.
788 Prog = Basename (argv[0]);
796 * Parse the command line options.
798 int option_index = 0;
800 static struct option long_options[] = {
801 {"all", no_argument, NULL, 'a'},
802 {"delete", no_argument, NULL, 'd'},
803 {"expire", no_argument, NULL, 'e'},
804 {"help", no_argument, NULL, 'h'},
805 {"inactive", required_argument, NULL, 'i'},
806 {"keep-tokens", no_argument, NULL, 'k'},
807 {"lock", no_argument, NULL, 'l'},
808 {"mindays", required_argument, NULL, 'n'},
809 {"quiet", no_argument, NULL, 'q'},
810 {"repository", required_argument, NULL, 'r'},
811 {"status", no_argument, NULL, 'S'},
812 {"unlock", no_argument, NULL, 'u'},
813 {"warndays", required_argument, NULL, 'w'},
814 {"maxdays", required_argument, NULL, 'x'},
815 {NULL, 0, NULL, '\0'}
818 while ((c = getopt_long (argc, argv, "adehi:kln:qr:Suw:x:",
819 long_options, &option_index)) != -1) {
833 if ( (getlong (optarg, &inact) == 0)
836 _("%s: invalid numeric argument '%s'\n"),
844 /* change only if expired, like Linux-PAM passwd -k. */
845 kflg = true; /* ok for users */
852 if ( (getlong (optarg, &age_min) == 0)
855 _("%s: invalid numeric argument '%s'\n"),
863 qflg = true; /* ok for users */
866 /* -r repository (files|nis|nisplus) */
867 /* only "files" supported for now */
868 if (strcmp (optarg, "files") != 0) {
870 _("%s: repository %s not supported\n"),
876 Sflg = true; /* ok for users */
883 if ( (getlong (optarg, &warn) == 0)
885 (void) fprintf (stderr,
886 _("%s: invalid numeric argument '%s'\n"),
894 if ( (getlong (optarg, &age_max) == 0)
896 (void) fprintf (stderr,
897 _("%s: invalid numeric argument '%s'\n"),
906 /*@notreached@*/break;
914 * Now I have to get the user name. The name will be gotten from the
915 * command line if possible. Otherwise it is figured out from the
918 pw = get_my_pwent ();
920 (void) fprintf (stderr,
921 _("%s: Cannot determine your user name.\n"),
923 SYSLOG ((LOG_WARN, "Cannot determine the user name of the caller (UID %lu)",
924 (unsigned long) getuid ()));
927 myname = xstrdup (pw->pw_name);
935 * Make sure that at most one username was specified.
937 if (argc > (optind+1)) {
942 * The -a flag requires -S, no other flags, no username, and
943 * you must be root. --marekm
946 if (anyflag || !Sflg || (optind < argc)) {
950 (void) fprintf (stderr,
951 _("%s: Permission denied.\n"),
956 while ( (pw = getpwent ()) != NULL ) {
964 * Allow certain users (administrators) to change passwords of
965 * certain users. Not implemented yet. --marekm
967 if (may_change_passwd (myname, name))
972 * If any of the flags were given, a user name must be supplied on
973 * the command line. Only an unadorned command line doesn't require
974 * the user's name be given. Also, -x, -n, -w, -i, -e, -d,
975 * -l, -u may appear with each other. -S, -k must appear alone.
979 * -S now ok for normal users (check status of my own account), and
980 * doesn't require username. --marekm
982 if (anyflag && optind >= argc) {
987 || (anyflag && (Sflg || kflg))) {
991 if (anyflag && !amroot) {
992 (void) fprintf (stderr, _("%s: Permission denied.\n"), Prog);
996 pw = xgetpwnam (name);
998 (void) fprintf (stderr,
999 _("%s: user '%s' does not exist\n"),
1004 /* only do this check when getuid()==0 because it's a pre-condition for
1005 changing a password without entering the old one */
1006 if ((is_selinux_enabled() > 0) && (getuid() == 0) &&
1007 (check_selinux_access (name, pw->pw_uid, PASSWD__PASSWD) != 0)) {
1008 security_context_t user_context = NULL;
1009 const char *user = "Unknown user context";
1010 if (getprevcon (&user_context) == 0) {
1011 user = user_context; /* FIXME: use context_user_get? */
1014 "%s is not authorized to change the password of %s",
1016 (void) fprintf(stderr,
1017 _("%s: %s is not authorized to change the password of %s\n"),
1019 if (NULL != user_context) {
1020 freecon (user_context);
1024 #endif /* WITH_SELINUX */
1027 * If the UID of the user does not match the current real UID,
1028 * check if I'm root.
1030 if (!amroot && (pw->pw_uid != getuid ())) {
1031 (void) fprintf (stderr,
1032 _("%s: You may not view or modify password information for %s.\n"),
1035 "%s: can't view or modify password information for %s",
1047 * The user name is valid, so let's get the shadow file entry.
1049 sp = getspnam (name); /* !USE_PAM, no need for xgetspnam */
1051 sp = pwd_to_spwd (pw);
1057 * If there are no other flags, just change the password.
1060 STRFCPY (crypt_passwd, cp);
1063 * See if the user is permitted to change the password.
1064 * Otherwise, go ahead and set a new password.
1066 check_password (pw, sp);
1069 * Let the user know whose password is being changed.
1072 (void) printf (_("Changing password for %s\n"), name);
1075 if (new_password (pw) != 0) {
1076 (void) fprintf (stderr,
1077 _("The password for %s is unchanged.\n"),
1082 do_update_pwd = true;
1083 do_update_age = true;
1085 #endif /* !USE_PAM */
1087 * Before going any further, raise the ulimit to prevent colliding
1088 * into a lowered ulimit, and set the real UID to root to protect
1089 * against unexpected signals. Any keyboard signals are set to be
1096 * Don't set the real UID for PAM...
1099 do_pam_passwd (name, qflg, kflg);
1102 #endif /* USE_PAM */
1103 if (setuid (0) != 0) {
1104 (void) fputs (_("Cannot change ID to root.\n"), stderr);
1105 SYSLOG ((LOG_ERR, "can't setuid(0)"));
1109 if (spw_file_present ()) {
1115 nscd_flush_cache ("passwd");
1116 nscd_flush_cache ("group");
1118 SYSLOG ((LOG_INFO, "password for '%s' changed by '%s'", name, myname));
1123 (void) printf (_("%s: password changed.\n"), Prog);
1124 #endif /* USE_PAM */
1126 (void) printf (_("%s: password expiry information changed.\n"), Prog);