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));
16 static int i64c(int i)
26 return ('A' - 12 + i);
27 return ('a' - 38 + i);
31 static void crypt_make_salt(char *p, int cnt)
33 unsigned x = x; /* it's pointless to initialize it anyway :) */
35 x += getpid() + time(NULL) + clock();
37 /* x = (x*1664525 + 1013904223) % 2^32 generator is lame
38 * (low-order bit is not "random", etc...),
39 * but for our purposes it is good enough */
40 x = x*1664525 + 1013904223;
41 /* BTW, Park and Miller's "minimal standard generator" is
42 * x = x*16807 % ((2^31)-1)
43 * It has no problem with visibly alternating lowest bit
44 * but is also weak in cryptographic sense + needs div,
45 * which needs more code (and slower) on many CPUs */
53 static char* new_password(const struct passwd *pw, uid_t myuid, int algo)
55 char salt[sizeof("$N$XXXXXXXX")]; /* "$N$XXXXXXXX" or "XX" */
60 char *ret = NULL; /* failure so far */
62 if (myuid && pw->pw_passwd[0]) {
63 orig = bb_askpass(0, "Old password:"); /* returns ptr to static */
66 cipher = pw_encrypt(orig, pw->pw_passwd); /* returns ptr to static */
67 if (strcmp(cipher, pw->pw_passwd) != 0) {
68 syslog(LOG_WARNING, "incorrect password for '%s'",
70 bb_do_delay(FAIL_DELAY);
71 puts("Incorrect password");
75 orig = xstrdup(orig); /* or else bb_askpass() will destroy it */
76 newp = bb_askpass(0, "New password:"); /* returns ptr to static */
79 newp = xstrdup(newp); /* we are going to bb_askpass() again, so save it */
80 if (ENABLE_FEATURE_PASSWD_WEAK_CHECK
81 && obscure(orig, newp, pw) && myuid)
82 goto err_ret; /* non-root is not allowed to have weak passwd */
84 cp = bb_askpass(0, "Retype password:");
87 if (strcmp(cp, newp)) {
88 puts("Passwords don't match");
92 /*memset(salt, 0, sizeof(salt)); - why?*/
93 crypt_make_salt(salt, 1); /* des */
96 crypt_make_salt(salt + 3, 4);
98 ret = xstrdup(pw_encrypt(newp, salt)); /* returns ptr to static */
103 if (ENABLE_FEATURE_CLEAN_UP) free(orig);
105 if (ENABLE_FEATURE_CLEAN_UP) free(newp);
113 static int get_algo(char *a)
117 if (strcasecmp(a, "des") == 0)
124 static int update_passwd(const char *filename, const char *username,
137 int ret = 1; /* failure */
139 logmode = LOGMODE_STDIO;
140 /* New passwd file, "/etc/passwd+" for now */
141 new_name = xasprintf("%s+", filename);
142 last_char = &new_name[strlen(new_name)-1];
143 username = xasprintf("%s:", username);
144 user_len = strlen(username);
146 old_fp = fopen(filename, "r+");
149 old_fd = fileno(old_fp);
151 /* Try to create "/etc/passwd+". Wait if it exists. */
154 // FIXME: on last iteration try w/o O_EXCL but with O_TRUNC?
155 new_fd = open(new_name, O_WRONLY|O_CREAT|O_EXCL,0600);
156 if (new_fd >= 0) goto created;
157 if (errno != EEXIST) break;
158 usleep(100000); /* 0.1 sec */
160 bb_perror_msg("cannot create '%s'", new_name);
163 if (!fstat(old_fd, &sb)) {
164 fchmod(new_fd, sb.st_mode & 0777); /* ignore errors */
165 fchown(new_fd, sb.st_uid, sb.st_gid);
167 new_fp = fdopen(new_fd, "w");
173 /* Backup file is "/etc/passwd-" */
175 /* Delete old one, create new as a hardlink to current */
176 i = (unlink(new_name) && errno != ENOENT);
177 if (i || link(filename, new_name))
178 bb_perror_msg("warning: cannot create backup copy '%s'", new_name);
181 /* Lock the password file before updating */
182 lock.l_type = F_WRLCK;
183 lock.l_whence = SEEK_SET;
186 if (fcntl(old_fd, F_SETLK, &lock) < 0)
187 bb_perror_msg("warning: cannot lock '%s'", filename);
188 lock.l_type = F_UNLCK;
190 /* Read current password file, write updated one */
192 char *line = xmalloc_fgets(old_fp);
193 if (!line) break; /* EOF/error */
194 if (strncmp(username, line, user_len) == 0) {
195 /* we have a match with "username:"... */
196 const char *cp = line + user_len;
197 /* now cp -> old passwd, skip it: */
198 cp = strchr(cp, ':');
200 /* now cp -> ':' after old passwd or -> "" */
201 fprintf(new_fp, "%s%s%s", username, new_pw, cp);
202 /* Erase password in memory */
207 fcntl(old_fd, F_SETLK, &lock);
209 /* We do want all of them to execute, thus | instead of || */
210 if ((ferror(old_fp) | fflush(new_fp) | fsync(new_fd) | fclose(new_fp))
211 || rename(new_name, filename)
213 /* At least one of those failed */
216 ret = 0; /* whee, success! */
219 if (ret) unlink(new_name);
225 if (ENABLE_FEATURE_CLEAN_UP) free(new_name);
226 if (ENABLE_FEATURE_CLEAN_UP) free((char*)username);
227 logmode = LOGMODE_BOTH;
232 int passwd_main(int argc, char **argv)
235 OPT_algo = 0x1, /* -a - password algorithm */
236 OPT_lock = 0x2, /* -l - lock account */
237 OPT_unlock = 0x4, /* -u - unlock account */
238 OPT_delete = 0x8, /* -d - delete password */
240 STATE_ALGO_md5 = 0x10,
241 /*STATE_ALGO_des = 0x20, not needed yet */
245 const char *filename;
251 struct rlimit rlimit_fsize;
254 logmode = LOGMODE_BOTH;
255 openlog(applet_name, LOG_NOWAIT, LOG_AUTH);
256 opt = getopt32(argc, argv, "a:lud", &opt_a);
260 if (strcasecmp(opt_a, "des") != 0) /* -a */
261 opt |= STATE_ALGO_md5;
263 // opt |= STATE_ALGO_des;
265 if ((opt & OPT_lud) && (!argc || myuid))
268 myname = xstrdup(bb_getpwuid(NULL, myuid, -1));
269 name = argc ? argv[0] : myname;
272 if (!pw) bb_error_msg_and_die("unknown user %s", name);
273 if (myuid && pw->pw_uid != myuid) {
275 bb_error_msg_and_die("%s can't change password for %s", myname, name);
278 filename = bb_path_passwd_file;
279 #if ENABLE_FEATURE_SHADOWPASSWDS
281 struct spwd *sp = getspnam(name);
284 bb_error_msg("no record of %s in %s, using %s",
285 name, bb_path_shadow_file,
286 bb_path_passwd_file);
288 filename = bb_path_shadow_file;
289 pw->pw_passwd = sp->sp_pwdp;
294 /* Decide what the new password will be */
296 c = pw->pw_passwd[0] - '!';
297 if (!(opt & OPT_lud)) {
298 if (myuid && !c) { /* passwd starts with '!' */
300 bb_error_msg_and_die("cannot change "
301 "locked password for %s", name);
303 printf("Changing password for %s\n", name);
304 newp = new_password(pw, myuid, opt & STATE_ALGO_md5);
306 logmode = LOGMODE_STDIO;
307 bb_error_msg_and_die("password for %s is unchanged", name);
309 } else if (opt & OPT_lock) {
310 if (!c) goto skip; /* passwd starts with '!' */
311 newp = xasprintf("!%s", pw->pw_passwd);
312 } else if (opt & OPT_unlock) {
313 if (c) goto skip; /* not '!' */
314 newp = xstrdup(&pw->pw_passwd[1]);
315 } else if (opt & OPT_delete) {
319 rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * 30000;
320 setrlimit(RLIMIT_FSIZE, &rlimit_fsize);
321 signal(SIGHUP, SIG_IGN);
322 signal(SIGINT, SIG_IGN);
323 signal(SIGQUIT, SIG_IGN);
326 if (update_passwd(filename, name, newp) != 0) {
328 bb_error_msg_and_die("cannot update password file %s",
332 bb_info_msg("Password for %s changed by %s", name, myname);
334 if (ENABLE_FEATURE_CLEAN_UP) free(newp);
337 bb_error_msg_and_die("password for %s is already %slocked",
338 name, (opt & OPT_unlock) ? "un" : "");
340 if (ENABLE_FEATURE_CLEAN_UP) free(myname);