1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
6 //config: bool "passwd (22 kb)"
8 //config: select FEATURE_SYSLOG
10 //config: passwd changes passwords for user and group accounts. A normal user
11 //config: may only change the password for his/her own account, the super user
12 //config: may change the password for any account. The administrator of a group
13 //config: may change the password for the group.
15 //config: Note that busybox binary must be setuid root for this applet to
16 //config: work properly.
18 //config:config FEATURE_PASSWD_WEAK_CHECK
19 //config: bool "Check new passwords for weakness"
21 //config: depends on PASSWD
23 //config: With this option passwd will refuse new passwords which are "weak".
25 //applet:/* Needs to be run by root or be suid root - needs to change /etc/{passwd,shadow}: */
26 //applet:IF_PASSWD(APPLET(passwd, BB_DIR_USR_BIN, BB_SUID_REQUIRE))
28 //kbuild:lib-$(CONFIG_PASSWD) += passwd.o
30 //usage:#define passwd_trivial_usage
31 //usage: "[-a ALG] [-dlu] [USER]"
32 //usage:#define passwd_full_usage "\n\n"
33 //usage: "Change USER's password (default: current user)"
35 //usage: "\n -a ALG "CRYPT_METHODS_HELP_STR
36 //usage: "\n -d Set password to ''"
37 //usage: "\n -l Lock (disable) account"
38 //usage: "\n -u Unlock (enable) account"
43 static char* new_password(const struct passwd
*pw
, uid_t myuid
, const char *algo
)
45 char salt
[MAX_PW_SALT_LEN
];
49 char *ret
= NULL
; /* failure so far */
51 if (myuid
!= 0 && pw
->pw_passwd
[0]) {
54 orig
= bb_ask_noecho_stdin("Old password: "); /* returns malloced str */
57 encrypted
= pw_encrypt(orig
, pw
->pw_passwd
, 1); /* returns malloced str */
58 if (strcmp(encrypted
, pw
->pw_passwd
) != 0) {
59 syslog(LOG_WARNING
, "incorrect password for %s", pw
->pw_name
);
60 pause_after_failed_login();
61 puts("Incorrect password");
64 if (ENABLE_FEATURE_CLEAN_UP
)
67 newp
= bb_ask_noecho_stdin("New password: "); /* returns malloced str */
70 if (ENABLE_FEATURE_PASSWD_WEAK_CHECK
71 && obscure(orig
, newp
, pw
) /* NB: passing NULL orig is ok */
74 goto err_ret
; /* non-root is not allowed to have weak passwd */
77 cp
= bb_ask_noecho_stdin("Retype password: ");
80 if (strcmp(cp
, newp
) != 0) {
81 puts("Passwords don't match");
85 crypt_make_pw_salt(salt
, algo
);
87 /* pw_encrypt returns malloced str */
88 ret
= pw_encrypt(newp
, salt
, 1);
93 if (ENABLE_FEATURE_CLEAN_UP
) free(orig
);
96 if (ENABLE_FEATURE_CLEAN_UP
) free(newp
);
99 if (ENABLE_FEATURE_CLEAN_UP
) free(cp
);
103 int passwd_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
104 int passwd_main(int argc UNUSED_PARAM
, char **argv
)
107 OPT_algo
= (1 << 0), /* -a - password algorithm */
108 OPT_lock
= (1 << 1), /* -l - lock account */
109 OPT_unlock
= (1 << 2), /* -u - unlock account */
110 OPT_delete
= (1 << 3), /* -d - delete password */
111 OPT_lud
= OPT_lock
| OPT_unlock
| OPT_delete
,
115 const char *opt_a
= CONFIG_FEATURE_DEFAULT_PASSWD_ALGO
;
116 const char *filename
;
122 struct rlimit rlimit_fsize
;
124 #if ENABLE_FEATURE_SHADOWPASSWDS
125 /* Using _r function to avoid pulling in static buffers */
130 logmode
= LOGMODE_BOTH
;
131 openlog(applet_name
, 0, LOG_AUTH
);
132 opt
= getopt32(argv
, "a:lud", &opt_a
);
137 /* -l, -u, -d require root priv and username argument */
138 if ((opt
& OPT_lud
) && (myuid
!= 0 || !argv
[0]))
141 /* Will complain and die if username not found */
142 myname
= xstrdup(xuid2uname(myuid
));
143 name
= argv
[0] ? argv
[0] : myname
;
145 pw
= xgetpwnam(name
);
146 if (myuid
!= 0 && pw
->pw_uid
!= myuid
) {
148 bb_error_msg_and_die("%s can't change password for %s", myname
, name
);
151 #if ENABLE_FEATURE_SHADOWPASSWDS
153 /* getspnam_r may return 0 yet set result to NULL.
154 * At least glibc 2.4 does this. Be extra paranoid here. */
155 struct spwd
*result
= NULL
;
157 if (getspnam_r(pw
->pw_name
, &spw
, buffer
, sizeof(buffer
), &result
) != 0
158 || !result
/* no error, but no record found either */
159 || strcmp(result
->sp_namp
, pw
->pw_name
) != 0 /* paranoia */
161 if (errno
!= ENOENT
) {
163 bb_perror_msg("no record of %s in %s, using %s",
164 name
, bb_path_shadow_file
,
165 bb_path_passwd_file
);
167 /* else: /etc/shadow does not exist,
168 * apparently we are on a shadow-less system,
169 * no surprise there */
171 pw
->pw_passwd
= result
->sp_pwdp
;
176 /* Decide what the new password will be */
178 c
= pw
->pw_passwd
[0] - '!';
179 if (!(opt
& OPT_lud
)) {
180 if (myuid
!= 0 && !c
) { /* passwd starts with '!' */
182 bb_error_msg_and_die("can't change "
183 "locked password for %s", name
);
185 printf("Changing password for %s\n", name
);
186 newp
= new_password(pw
, myuid
, opt_a
);
188 logmode
= LOGMODE_STDIO
;
189 bb_error_msg_and_die("password for %s is unchanged", name
);
191 } else if (opt
& OPT_lock
) {
193 goto skip
; /* passwd starts with '!' */
194 newp
= xasprintf("!%s", pw
->pw_passwd
);
195 } else if (opt
& OPT_unlock
) {
197 goto skip
; /* not '!' */
198 /* pw->pw_passwd points to static storage,
199 * strdup'ing to avoid nasty surprizes */
200 newp
= xstrdup(&pw
->pw_passwd
[1]);
201 } else if (opt
& OPT_delete
) {
205 rlimit_fsize
.rlim_cur
= rlimit_fsize
.rlim_max
= 512L * 30000;
206 setrlimit(RLIMIT_FSIZE
, &rlimit_fsize
);
215 #if ENABLE_FEATURE_SHADOWPASSWDS
216 filename
= bb_path_shadow_file
;
217 rc
= update_passwd(bb_path_shadow_file
, name
, newp
, NULL
);
219 /* password in /etc/shadow was updated */
222 /* 0 = /etc/shadow missing (not an error), >0 = passwd changed in /etc/shadow */
225 filename
= bb_path_passwd_file
;
226 rc
= update_passwd(bb_path_passwd_file
, name
, newp
, NULL
);
230 bb_error_msg_and_die("can't update password file %s", filename
);
231 bb_info_msg("password for %s changed by %s", name
, myname
);
233 /*if (ENABLE_FEATURE_CLEAN_UP) free(newp); - can't, it may be non-malloced */
236 bb_error_msg_and_die("password for %s is already %slocked",
237 name
, (opt
& OPT_unlock
) ? "un" : "");
240 if (ENABLE_FEATURE_CLEAN_UP
)