1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 #include <sys/times.h> /* times() */
11 static void nuke_str(char *str)
13 if (str) memset(str, 0, strlen(str));
17 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 #if !defined(__GLIBC__)
35 #define TIMES times(&t)
37 /* glibc allows for times(NULL) a-la time() */
38 #define TIMES times(NULL)
40 unsigned long x = x; /* it's pointless to initialize it anyway :) */
44 /* clock() and times() variability is different between systems */
45 /* hopefully at least one is good enough */
46 x += time(NULL) + clock() + TIMES;
47 *p++ = i64c(((x >> 18) ^ (x >> 6)) & 0x3f);
48 *p++ = i64c(((x >> 12) ^ x) & 0x3f);
49 usleep(100); /* or else time() etc won't change */
55 static char* new_password(const struct passwd *pw, uid_t myuid, int algo)
57 char salt[sizeof("$N$XXXXXXXX")]; /* "$N$XXXXXXXX" or "XX" */
62 char *ret = NULL; /* failure so far */
64 if (myuid && pw->pw_passwd[0]) {
65 orig = bb_askpass(0, "Old password:"); /* returns ptr to static */
68 cipher = pw_encrypt(orig, pw->pw_passwd); /* returns ptr to static */
69 if (strcmp(cipher, pw->pw_passwd) != 0) {
70 syslog(LOG_WARNING, "incorrect password for '%s'",
72 bb_do_delay(FAIL_DELAY);
73 puts("Incorrect password");
77 orig = xstrdup(orig); /* or else bb_askpass() will destroy it */
78 newp = bb_askpass(0, "New password:"); /* returns ptr to static */
81 newp = xstrdup(newp); /* we are going to bb_askpass() again, so save it */
82 if (obscure(orig, newp, pw) && myuid) {
83 goto err_ret; /* non-root is not allowed to have weak passwd */
86 cp = bb_askpass(0, "Retype password:");
89 if (strcmp(cp, newp)) {
90 puts("Passwords don't match");
94 /*memset(salt, 0, sizeof(salt)); - why?*/
95 crypt_make_salt(salt, 1); /* des */
98 crypt_make_salt(salt + 3, 4);
100 ret = xstrdup(pw_encrypt(newp, salt)); /* returns ptr to static */
105 if (ENABLE_FEATURE_CLEAN_UP) free(orig);
107 if (ENABLE_FEATURE_CLEAN_UP) free(newp);
115 static int get_algo(char *a)
119 if (strcasecmp(a, "des") == 0)
126 static int update_passwd(const char *filename, const char *username,
139 int ret = 1; /* failure */
141 logmode = LOGMODE_STDIO;
142 /* New passwd file, "/etc/passwd+" for now */
143 new_name = xasprintf("%s+", filename);
144 last_char = &new_name[strlen(new_name)-1];
145 username = xasprintf("%s:", username);
146 user_len = strlen(username);
148 old_fp = fopen(filename, "r+");
151 old_fd = fileno(old_fp);
153 /* Try to create "/etc/passwd+". Wait if it exists. */
156 // FIXME: on last iteration try w/o O_EXCL but with O_TRUNC?
157 new_fd = open(new_name, O_WRONLY|O_CREAT|O_EXCL,0600);
158 if (new_fd >= 0) goto created;
159 if (errno != EEXIST) break;
160 usleep(100000); /* 0.1 sec */
162 bb_perror_msg("cannot create '%s'", new_name);
165 if (!fstat(old_fd, &sb)) {
166 fchmod(new_fd, sb.st_mode & 0777); /* ignore errors */
167 fchown(new_fd, sb.st_uid, sb.st_gid);
169 new_fp = fdopen(new_fd, "w");
175 /* Backup file is "/etc/passwd-" */
177 /* Delete old one, create new as a hardlink to current */
178 i = (unlink(new_name) && errno != ENOENT);
179 if (i || link(filename, new_name))
180 bb_perror_msg("warning: cannot create backup copy '%s'", new_name);
183 /* Lock the password file before updating */
184 lock.l_type = F_WRLCK;
185 lock.l_whence = SEEK_SET;
188 if (fcntl(old_fd, F_SETLK, &lock) < 0)
189 bb_perror_msg("warning: cannot lock '%s'", filename);
190 lock.l_type = F_UNLCK;
192 /* Read current password file, write updated one */
194 char *line = xmalloc_fgets(old_fp);
195 if (!line) break; /* EOF/error */
196 if (strncmp(username, line, user_len) == 0) {
197 /* we have a match with "username:"... */
198 const char *cp = line + user_len;
199 /* now cp -> old passwd, skip it: */
200 cp = strchr(cp, ':');
202 /* now cp -> ':' after old passwd or -> "" */
203 fprintf(new_fp, "%s%s%s", username, new_pw, cp);
204 /* Erase password in memory */
209 fcntl(old_fd, F_SETLK, &lock);
211 /* We do want all of them to execute, thus | instead of || */
212 if ((ferror(old_fp) | fflush(new_fp) | fsync(new_fd) | fclose(new_fp))
213 || rename(new_name, filename)
215 /* At least one of those failed */
218 ret = 0; /* whee, success! */
221 if (ret) unlink(new_name);
227 if (ENABLE_FEATURE_CLEAN_UP) free(new_name);
228 if (ENABLE_FEATURE_CLEAN_UP) free((char*)username);
229 logmode = LOGMODE_BOTH;
234 int passwd_main(int argc, char **argv)
237 OPT_algo = 0x1, /* -a - password algorithm */
238 OPT_lock = 0x2, /* -l - lock account */
239 OPT_unlock = 0x4, /* -u - unlock account */
240 OPT_delete = 0x8, /* -d - delete password */
242 STATE_ALGO_md5 = 0x10,
243 /*STATE_ALGO_des = 0x20, not needed yet */
247 const char *filename;
253 struct rlimit rlimit_fsize;
256 logmode = LOGMODE_BOTH;
257 openlog(applet_name, LOG_NOWAIT, LOG_AUTH);
258 opt = getopt32(argc, argv, "a:lud", &opt_a);
262 if (strcasecmp(opt_a, "des") != 0) /* -a */
263 opt |= STATE_ALGO_md5;
265 // opt |= STATE_ALGO_des;
267 if ((opt & OPT_lud) && (!argc || myuid))
270 myname = xstrdup(bb_getpwuid(NULL, myuid, -1));
271 name = argc ? argv[0] : myname;
274 if (!pw) bb_error_msg_and_die("unknown user %s", name);
275 if (myuid && pw->pw_uid != myuid) {
277 bb_error_msg_and_die("%s can't change password for %s", myname, name);
280 filename = bb_path_passwd_file;
281 if (ENABLE_FEATURE_SHADOWPASSWDS) {
282 struct spwd *sp = getspnam(name);
285 bb_error_msg("no record of %s in %s, using %s",
286 name, bb_path_shadow_file,
287 bb_path_passwd_file);
289 filename = bb_path_shadow_file;
290 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);