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 /*memset(salt, 0, sizeof(salt)); - why?*/
55 crypt_make_salt(salt, 1); /* des */
58 crypt_make_salt(salt + 3, 4);
60 ret = xstrdup(pw_encrypt(newp, salt)); /* returns ptr to static */
65 if (ENABLE_FEATURE_CLEAN_UP) free(orig);
67 if (ENABLE_FEATURE_CLEAN_UP) free(newp);
74 static int update_passwd(const char *filename, const char *username,
87 int ret = 1; /* failure */
89 logmode = LOGMODE_STDIO;
90 /* New passwd file, "/etc/passwd+" for now */
91 new_name = xasprintf("%s+", filename);
92 last_char = &new_name[strlen(new_name)-1];
93 username = xasprintf("%s:", username);
94 user_len = strlen(username);
96 old_fp = fopen(filename, "r+");
99 old_fd = fileno(old_fp);
101 /* Try to create "/etc/passwd+". Wait if it exists. */
104 // FIXME: on last iteration try w/o O_EXCL but with O_TRUNC?
105 new_fd = open(new_name, O_WRONLY|O_CREAT|O_EXCL,0600);
106 if (new_fd >= 0) goto created;
107 if (errno != EEXIST) break;
108 usleep(100000); /* 0.1 sec */
110 bb_perror_msg("cannot create '%s'", new_name);
113 if (!fstat(old_fd, &sb)) {
114 fchmod(new_fd, sb.st_mode & 0777); /* ignore errors */
115 fchown(new_fd, sb.st_uid, sb.st_gid);
117 new_fp = fdopen(new_fd, "w");
123 /* Backup file is "/etc/passwd-" */
125 /* Delete old one, create new as a hardlink to current */
126 i = (unlink(new_name) && errno != ENOENT);
127 if (i || link(filename, new_name))
128 bb_perror_msg("warning: cannot create backup copy '%s'", new_name);
131 /* Lock the password file before updating */
132 lock.l_type = F_WRLCK;
133 lock.l_whence = SEEK_SET;
136 if (fcntl(old_fd, F_SETLK, &lock) < 0)
137 bb_perror_msg("warning: cannot lock '%s'", filename);
138 lock.l_type = F_UNLCK;
140 /* Read current password file, write updated one */
142 char *line = xmalloc_fgets(old_fp);
143 if (!line) break; /* EOF/error */
144 if (strncmp(username, line, user_len) == 0) {
145 /* we have a match with "username:"... */
146 const char *cp = line + user_len;
147 /* now cp -> old passwd, skip it: */
148 cp = strchr(cp, ':');
150 /* now cp -> ':' after old passwd or -> "" */
151 fprintf(new_fp, "%s%s%s", username, new_pw, cp);
152 /* Erase password in memory */
157 fcntl(old_fd, F_SETLK, &lock);
159 /* We do want all of them to execute, thus | instead of || */
160 if ((ferror(old_fp) | fflush(new_fp) | fsync(new_fd) | fclose(new_fp))
161 || rename(new_name, filename)
163 /* At least one of those failed */
166 ret = 0; /* whee, success! */
169 if (ret) unlink(new_name);
175 if (ENABLE_FEATURE_CLEAN_UP) free(new_name);
176 if (ENABLE_FEATURE_CLEAN_UP) free((char*)username);
177 logmode = LOGMODE_BOTH;
182 int passwd_main(int argc, char **argv);
183 int passwd_main(int argc, char **argv)
186 OPT_algo = 0x1, /* -a - password algorithm */
187 OPT_lock = 0x2, /* -l - lock account */
188 OPT_unlock = 0x4, /* -u - unlock account */
189 OPT_delete = 0x8, /* -d - delete password */
191 STATE_ALGO_md5 = 0x10,
192 /*STATE_ALGO_des = 0x20, not needed yet */
195 const char *opt_a = "";
196 const char *filename;
202 struct rlimit rlimit_fsize;
205 #if ENABLE_FEATURE_SHADOWPASSWDS
206 /* Using _r function to avoid pulling in static buffers */
212 logmode = LOGMODE_BOTH;
213 openlog(applet_name, LOG_NOWAIT, LOG_AUTH);
214 opt = getopt32(argc, argv, "a:lud", &opt_a);
218 if (strcasecmp(opt_a, "des") != 0) /* -a */
219 opt |= STATE_ALGO_md5;
221 // opt |= STATE_ALGO_des;
223 if ((opt & OPT_lud) && (!argc || myuid))
226 myname = xstrdup(bb_getpwuid(NULL, myuid, -1));
227 name = argc ? argv[0] : myname;
230 if (!pw) bb_error_msg_and_die("unknown user %s", name);
231 if (myuid && pw->pw_uid != myuid) {
233 bb_error_msg_and_die("%s can't change password for %s", myname, name);
236 filename = bb_path_passwd_file;
237 #if ENABLE_FEATURE_SHADOWPASSWDS
238 if (getspnam_r(pw->pw_name, &spw, buffer, sizeof(buffer), &result)) {
240 bb_error_msg("no record of %s in %s, using %s",
241 name, bb_path_shadow_file,
242 bb_path_passwd_file);
244 filename = bb_path_shadow_file;
245 pw->pw_passwd = spw.sp_pwdp;
249 /* Decide what the new password will be */
251 c = pw->pw_passwd[0] - '!';
252 if (!(opt & OPT_lud)) {
253 if (myuid && !c) { /* passwd starts with '!' */
255 bb_error_msg_and_die("cannot change "
256 "locked password for %s", name);
258 printf("Changing password for %s\n", name);
259 newp = new_password(pw, myuid, opt & STATE_ALGO_md5);
261 logmode = LOGMODE_STDIO;
262 bb_error_msg_and_die("password for %s is unchanged", name);
264 } else if (opt & OPT_lock) {
265 if (!c) goto skip; /* passwd starts with '!' */
266 newp = xasprintf("!%s", pw->pw_passwd);
267 } else if (opt & OPT_unlock) {
268 if (c) goto skip; /* not '!' */
269 newp = xstrdup(&pw->pw_passwd[1]);
270 } else if (opt & OPT_delete) {
274 rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * 30000;
275 setrlimit(RLIMIT_FSIZE, &rlimit_fsize);
276 signal(SIGHUP, SIG_IGN);
277 signal(SIGINT, SIG_IGN);
278 signal(SIGQUIT, SIG_IGN);
281 if (update_passwd(filename, name, newp) != 0) {
283 bb_error_msg_and_die("cannot update password file %s",
287 bb_info_msg("Password for %s changed by %s", name, myname);
289 if (ENABLE_FEATURE_CLEAN_UP) free(newp);
292 bb_error_msg_and_die("password for %s is already %slocked",
293 name, (opt & OPT_unlock) ? "un" : "");
295 if (ENABLE_FEATURE_CLEAN_UP) free(myname);