1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 static void nuke_str(char *str
)
10 if (str
) memset(str
, 0, strlen(str
));
13 static char* new_password(const struct passwd
*pw
, uid_t myuid
, int algo
)
15 char salt
[sizeof("$N$XXXXXXXX")]; /* "$N$XXXXXXXX" or "XX" */
16 char *orig
= (char*)"";
19 char *ret
= NULL
; /* failure so far */
21 if (myuid
&& pw
->pw_passwd
[0]) {
24 orig
= bb_ask_stdin("Old password:"); /* returns ptr to static */
27 encrypted
= pw_encrypt(orig
, pw
->pw_passwd
, 1); /* returns malloced str */
28 if (strcmp(encrypted
, pw
->pw_passwd
) != 0) {
29 syslog(LOG_WARNING
, "incorrect password for %s",
31 bb_do_delay(FAIL_DELAY
);
32 puts("Incorrect password");
35 if (ENABLE_FEATURE_CLEAN_UP
) free(encrypted
);
37 orig
= xstrdup(orig
); /* or else bb_ask_stdin() will destroy it */
38 newp
= bb_ask_stdin("New password:"); /* returns ptr to static */
41 newp
= xstrdup(newp
); /* we are going to bb_ask_stdin() again, so save it */
42 if (ENABLE_FEATURE_PASSWD_WEAK_CHECK
43 && obscure(orig
, newp
, pw
) && myuid
)
44 goto err_ret
; /* non-root is not allowed to have weak passwd */
46 cp
= bb_ask_stdin("Retype password:");
49 if (strcmp(cp
, newp
)) {
50 puts("Passwords don't match");
54 crypt_make_salt(salt
, 1, 0); /* des */
57 crypt_make_salt(salt
+ 3, 4, 0);
59 /* pw_encrypt returns malloced str */
60 ret
= pw_encrypt(newp
, salt
, 1);
65 if (ENABLE_FEATURE_CLEAN_UP
) free(orig
);
67 if (ENABLE_FEATURE_CLEAN_UP
) free(newp
);
72 int passwd_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
73 int passwd_main(int argc UNUSED_PARAM
, char **argv
)
76 OPT_algo
= 0x1, /* -a - password algorithm */
77 OPT_lock
= 0x2, /* -l - lock account */
78 OPT_unlock
= 0x4, /* -u - unlock account */
79 OPT_delete
= 0x8, /* -d - delete password */
81 STATE_ALGO_md5
= 0x10,
82 //STATE_ALGO_des = 0x20, not needed yet
86 const char *opt_a
= "";
93 struct rlimit rlimit_fsize
;
95 #if ENABLE_FEATURE_SHADOWPASSWDS
96 /* Using _r function to avoid pulling in static buffers */
101 logmode
= LOGMODE_BOTH
;
102 openlog(applet_name
, 0, LOG_AUTH
);
103 opt
= getopt32(argv
, "a:lud", &opt_a
);
107 if (strcasecmp(opt_a
, "des") != 0) /* -a */
108 opt
|= STATE_ALGO_md5
;
110 // opt |= STATE_ALGO_des;
112 /* -l, -u, -d require root priv and username argument */
113 if ((opt
& OPT_lud
) && (myuid
|| !argv
[0]))
116 /* Will complain and die if username not found */
117 myname
= xstrdup(xuid2uname(myuid
));
118 name
= argv
[0] ? argv
[0] : myname
;
120 pw
= xgetpwnam(name
);
121 if (myuid
&& pw
->pw_uid
!= myuid
) {
123 bb_error_msg_and_die("%s can't change password for %s", myname
, name
);
126 #if ENABLE_FEATURE_SHADOWPASSWDS
128 /* getspnam_r may return 0 yet set result to NULL.
129 * At least glibc 2.4 does this. Be extra paranoid here. */
130 struct spwd
*result
= NULL
;
131 if (getspnam_r(pw
->pw_name
, &spw
, buffer
, sizeof(buffer
), &result
)
132 || !result
|| strcmp(result
->sp_namp
, pw
->pw_name
) != 0) {
134 bb_error_msg("no record of %s in %s, using %s",
135 name
, bb_path_shadow_file
,
136 bb_path_passwd_file
);
138 pw
->pw_passwd
= result
->sp_pwdp
;
143 /* Decide what the new password will be */
145 c
= pw
->pw_passwd
[0] - '!';
146 if (!(opt
& OPT_lud
)) {
147 if (myuid
&& !c
) { /* passwd starts with '!' */
149 bb_error_msg_and_die("cannot change "
150 "locked password for %s", name
);
152 printf("Changing password for %s\n", name
);
153 newp
= new_password(pw
, myuid
, opt
& STATE_ALGO_md5
);
155 logmode
= LOGMODE_STDIO
;
156 bb_error_msg_and_die("password for %s is unchanged", name
);
158 } else if (opt
& OPT_lock
) {
159 if (!c
) goto skip
; /* passwd starts with '!' */
160 newp
= xasprintf("!%s", pw
->pw_passwd
);
161 } else if (opt
& OPT_unlock
) {
162 if (c
) goto skip
; /* not '!' */
163 /* pw->pw_passwd points to static storage,
164 * strdup'ing to avoid nasty surprizes */
165 newp
= xstrdup(&pw
->pw_passwd
[1]);
166 } else if (opt
& OPT_delete
) {
167 //newp = xstrdup("");
171 rlimit_fsize
.rlim_cur
= rlimit_fsize
.rlim_max
= 512L * 30000;
172 setrlimit(RLIMIT_FSIZE
, &rlimit_fsize
);
181 #if ENABLE_FEATURE_SHADOWPASSWDS
182 filename
= bb_path_shadow_file
;
183 rc
= update_passwd(bb_path_shadow_file
, name
, newp
, NULL
);
184 if (rc
== 0) /* no lines updated, no errors detected */
187 filename
= bb_path_passwd_file
;
188 rc
= update_passwd(bb_path_passwd_file
, name
, newp
, NULL
);
192 bb_error_msg_and_die("cannot update password file %s",
194 bb_info_msg("Password for %s changed by %s", name
, myname
);
196 //if (ENABLE_FEATURE_CLEAN_UP) free(newp);
199 bb_error_msg_and_die("password for %s is already %slocked",
200 name
, (opt
& OPT_unlock
) ? "un" : "");
202 if (ENABLE_FEATURE_CLEAN_UP
) free(myname
);