1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 static void nuke_str(char *str)
12 if (str) memset(str, 0, strlen(str));
15 static char* new_password(const struct passwd *pw, uid_t myuid, int algo)
17 char salt[sizeof("$N$XXXXXXXX")]; /* "$N$XXXXXXXX" or "XX" */
18 char *orig = (char*)"";
22 char *ret = NULL; /* failure so far */
24 if (myuid && pw->pw_passwd[0]) {
25 orig = bb_askpass(0, "Old password:"); /* returns ptr to static */
28 cipher = pw_encrypt(orig, pw->pw_passwd); /* returns ptr to static */
29 if (strcmp(cipher, pw->pw_passwd) != 0) {
30 syslog(LOG_WARNING, "incorrect password for '%s'",
32 bb_do_delay(FAIL_DELAY);
33 puts("Incorrect password");
37 orig = xstrdup(orig); /* or else bb_askpass() will destroy it */
38 newp = bb_askpass(0, "New password:"); /* returns ptr to static */
41 newp = xstrdup(newp); /* we are going to bb_askpass() 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_askpass(0, "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 ptr to static */
60 ret = xstrdup(pw_encrypt(newp, salt));
65 if (ENABLE_FEATURE_CLEAN_UP) free(orig);
67 if (ENABLE_FEATURE_CLEAN_UP) free(newp);
73 int passwd_main(int argc, char **argv);
74 int passwd_main(int argc, char **argv)
77 OPT_algo = 0x1, /* -a - password algorithm */
78 OPT_lock = 0x2, /* -l - lock account */
79 OPT_unlock = 0x4, /* -u - unlock account */
80 OPT_delete = 0x8, /* -d - delete password */
82 STATE_ALGO_md5 = 0x10,
83 //STATE_ALGO_des = 0x20, not needed yet
87 const char *opt_a = "";
94 struct rlimit rlimit_fsize;
97 #if ENABLE_FEATURE_SHADOWPASSWDS
98 /* Using _r function to avoid pulling in static buffers */
104 logmode = LOGMODE_BOTH;
105 openlog(applet_name, LOG_NOWAIT, LOG_AUTH);
106 opt = getopt32(argv, "a:lud", &opt_a);
110 if (strcasecmp(opt_a, "des") != 0) /* -a */
111 opt |= STATE_ALGO_md5;
113 // opt |= STATE_ALGO_des;
115 /* -l, -u, -d require root priv and username argument */
116 if ((opt & OPT_lud) && (myuid || !argv[0]))
119 /* Will complain and die if username not found */
120 myname = xstrdup(bb_getpwuid(NULL, -1, myuid));
121 name = argv[0] ? argv[0] : myname;
124 if (!pw) bb_error_msg_and_die("unknown user %s", name);
125 if (myuid && pw->pw_uid != myuid) {
127 bb_error_msg_and_die("%s can't change password for %s", myname, name);
130 #if ENABLE_FEATURE_SHADOWPASSWDS
131 /* getspnam_r() can lie! Even if user isn't in shadow, it can
132 * return success (pwd field was seen set to "!" in this case) */
133 if (getspnam_r(pw->pw_name, &spw, buffer, sizeof(buffer), &result)
134 || LONE_CHAR(spw.sp_pwdp, '!')) {
136 bb_error_msg("no record of %s in %s, using %s",
137 name, bb_path_shadow_file,
138 bb_path_passwd_file);
140 pw->pw_passwd = spw.sp_pwdp;
144 /* Decide what the new password will be */
146 c = pw->pw_passwd[0] - '!';
147 if (!(opt & OPT_lud)) {
148 if (myuid && !c) { /* passwd starts with '!' */
150 bb_error_msg_and_die("cannot change "
151 "locked password for %s", name);
153 printf("Changing password for %s\n", name);
154 newp = new_password(pw, myuid, opt & STATE_ALGO_md5);
156 logmode = LOGMODE_STDIO;
157 bb_error_msg_and_die("password for %s is unchanged", name);
159 } else if (opt & OPT_lock) {
160 if (!c) goto skip; /* passwd starts with '!' */
161 newp = xasprintf("!%s", pw->pw_passwd);
162 } else if (opt & OPT_unlock) {
163 if (c) goto skip; /* not '!' */
164 /* pw->pw_passwd pints to static storage,
165 * strdup'ing to avoid nasty surprizes */
166 newp = xstrdup(&pw->pw_passwd[1]);
167 } else if (opt & OPT_delete) {
168 //newp = xstrdup("");
172 rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * 30000;
173 setrlimit(RLIMIT_FSIZE, &rlimit_fsize);
174 signal(SIGHUP, SIG_IGN);
175 signal(SIGINT, SIG_IGN);
176 signal(SIGQUIT, SIG_IGN);
180 #if ENABLE_FEATURE_SHADOWPASSWDS
181 filename = bb_path_shadow_file;
182 rc = update_passwd(bb_path_shadow_file, name, newp);
183 if (rc == 0) /* no lines updated, no errors detected */
186 filename = bb_path_passwd_file;
187 rc = update_passwd(bb_path_passwd_file, name, newp);
191 bb_error_msg_and_die("cannot update password file %s",
193 bb_info_msg("Password for %s changed by %s", name, myname);
195 //if (ENABLE_FEATURE_CLEAN_UP) free(newp);
198 bb_error_msg_and_die("password for %s is already %slocked",
199 name, (opt & OPT_unlock) ? "un" : "");
201 if (ENABLE_FEATURE_CLEAN_UP) free(myname);