1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9 static char crypt_passwd[128];
11 static int create_backup(const char *backup, FILE * fp);
12 static int new_password(const struct passwd *pw, int amroot, int algo);
13 static void set_filesize_limit(int blocks);
16 static int get_algo(char *a)
18 int x = 1; /* standard: MD5 */
20 if (strcasecmp(a, "des") == 0)
26 static int update_passwd(const struct passwd *pw, const char *crypt_pw)
40 #if ENABLE_FEATURE_SHADOWPASSWDS
41 if (access(bb_path_shadow_file, F_OK) == 0) {
42 snprintf(filename, sizeof filename, "%s", bb_path_shadow_file);
46 snprintf(filename, sizeof filename, "%s", bb_path_passwd_file);
49 fp = fopen(filename, "r+");
50 if (fp == 0 || fstat(fileno(fp), &sb)) {
55 /* Lock the password file before updating */
56 lock.l_type = F_WRLCK;
57 lock.l_whence = SEEK_SET;
60 if (fcntl(fileno(fp), F_SETLK, &lock) < 0) {
61 bb_perror_msg("%s", filename);
64 lock.l_type = F_UNLCK;
66 snprintf(buf, sizeof buf, "%s-", filename);
67 if (create_backup(buf, fp)) {
68 fcntl(fileno(fp), F_SETLK, &lock);
72 snprintf(buf, sizeof buf, "%s+", filename);
74 out_fp = fopen(buf, "w");
76 if ((!out_fp) || (fchmod(fileno(out_fp), sb.st_mode & 0777))
77 || (fchown(fileno(out_fp), sb.st_uid, sb.st_gid))) {
78 fcntl(fileno(fp), F_SETLK, &lock);
85 snprintf(username, sizeof username, "%s:", pw->pw_name);
88 fgets(buffer, sizeof buffer, fp);
89 if (!continued) { /* Check to see if we're updating this line. */
90 if (strncmp(username, buffer, strlen(username)) == 0) {
91 /* we have a match. */
92 pw_rest = strchr(buffer, ':');
94 pw_rest = strchr(pw_rest, ':');
95 fprintf(out_fp, "%s:%s%s", buffer, crypt_pw, pw_rest);
97 fputs(buffer, out_fp);
100 fputs(buffer, out_fp);
102 if (buffer[strlen(buffer) - 1] == '\n') {
107 memset(buffer, 0, sizeof buffer);
110 if (fflush(out_fp) || fsync(fileno(out_fp)) || fclose(out_fp)) {
112 fcntl(fileno(fp), F_SETLK, &lock);
116 if (rename(buf, filename) < 0) {
117 fcntl(fileno(fp), F_SETLK, &lock);
121 fcntl(fileno(fp), F_SETLK, &lock);
128 int passwd_main(int argc, char **argv)
131 OPT_algo = 0x1, /* -a - password algorithm */
132 OPT_lock = 0x2, /* -l - lock account */
133 OPT_unlock = 0x4, /* -u - unlock account */
134 OPT_delete = 0x8, /* -d - delete password */
145 const struct passwd *pw;
147 amroot = (getuid() == 0);
148 openlog("passwd", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
149 opt = getopt32(argc, argv, "a:lud", &opt_a);
152 if (opt & OPT_algo) algo = get_algo(opt_a); // -a
153 if ((opt & OPT_lud) && (!argc || !amroot))
156 myname = xstrdup(bb_getpwuid(NULL, getuid(), -1));
158 if (argc) name = argv[0];
162 bb_error_msg_and_die("unknown user %s", name);
164 if (!amroot && pw->pw_uid != getuid()) {
165 syslog(LOG_WARNING, "can't change pwd for '%s'", name);
166 bb_error_msg_and_die("permission denied");
168 if (ENABLE_FEATURE_SHADOWPASSWDS) {
169 struct spwd *sp = getspnam(name);
170 if (!sp) bb_error_msg_and_die("unknown user %s", name);
172 } else cp = pw->pw_passwd;
175 safe_strncpy(crypt_passwd, cp, sizeof(crypt_passwd));
176 if (!(opt & OPT_lud)) {
179 syslog(LOG_WARNING, "password locked for '%s'", np);
180 bb_error_msg_and_die("the password for %s cannot be changed", np);
183 printf("Changing password for %s\n", name);
184 if (new_password(pw, amroot, algo)) {
185 bb_error_msg_and_die("the password for %s is unchanged", name);
187 } else if (opt & OPT_lock) {
188 if (crypt_passwd[0] != '!') {
189 memmove(&crypt_passwd[1], crypt_passwd,
190 sizeof crypt_passwd - 1);
191 crypt_passwd[sizeof crypt_passwd - 1] = '\0';
192 crypt_passwd[0] = '!';
194 } else if (opt & OPT_unlock) {
195 if (crypt_passwd[0] == '!') {
196 memmove(crypt_passwd, &crypt_passwd[1],
197 sizeof crypt_passwd - 1);
199 } else if (opt & OPT_delete) {
200 crypt_passwd[0] = '\0';
202 set_filesize_limit(30000);
203 signal(SIGHUP, SIG_IGN);
204 signal(SIGINT, SIG_IGN);
205 signal(SIGQUIT, SIG_IGN);
208 if (!update_passwd(pw, crypt_passwd)) {
209 syslog(LOG_INFO, "password for '%s' changed by user '%s'", name,
211 puts("Password changed");
213 syslog(LOG_WARNING, "cannot update password file");
214 bb_error_msg_and_die("cannot update password file");
216 if (ENABLE_FEATURE_CLEAN_UP) free(myname);
222 static int create_backup(const char *backup, FILE * fp)
229 if (fstat(fileno(fp), &sb))
234 bkfp = fopen(backup, "w");
240 /* TODO: faster copy, not one-char-at-a-time. --marekm */
242 while ((c = getc(fp)) != EOF) {
243 if (putc(c, bkfp) == EOF)
246 if (c != EOF || fflush(bkfp)) {
255 ub.actime = sb.st_atime;
256 ub.modtime = sb.st_mtime;
261 static int i64c(int i)
267 if (i >= 2 && i < 12)
268 return ('0' - 2 + i);
269 if (i >= 12 && i < 38)
270 return ('A' - 12 + i);
271 if (i >= 38 && i < 63)
272 return ('a' - 38 + i);
276 static char *crypt_make_salt(void)
279 static unsigned long x;
280 static char result[3];
283 x += now + getpid() + clock();
284 result[0] = i64c(((x >> 18) ^ (x >> 6)) & 077);
285 result[1] = i64c(((x >> 12) ^ x) & 077);
291 static int new_password(const struct passwd *pw, int amroot, int algo)
296 char salt[12]; /* "$N$XXXXXXXX" or "XX" */
300 if (!amroot && crypt_passwd[0]) {
301 clear = bb_askpass(0, "Old password:");
306 cipher = pw_encrypt(clear, crypt_passwd);
307 if (strcmp(cipher, crypt_passwd) != 0) {
308 syslog(LOG_WARNING, "incorrect password for '%s'",
310 bb_do_delay(FAIL_DELAY);
311 puts("Incorrect password");
315 safe_strncpy(orig, clear, sizeof(orig));
316 memset(clear, 0, strlen(clear));
317 memset(cipher, 0, strlen(cipher));
321 cp = bb_askpass(0, "Enter the new password (minimum of 5 characters).\n"
322 "Please use a combination of upper and lower case letters and numbers.\n"
323 "Enter new password: ");
325 memset(orig, 0, sizeof orig);
329 safe_strncpy(pass, cp, sizeof(pass));
330 memset(cp, 0, strlen(cp));
331 /* if (!obscure(orig, pass, pw)) { */
332 if (obscure(orig, pass, pw)) {
334 puts("\nWarning: weak password (continuing)");
340 cp = bb_askpass(0, "Re-enter new password: ");
342 memset(orig, 0, sizeof orig);
346 if (strcmp(cp, pass)) {
347 puts("Passwords do not match");
351 memset(cp, 0, strlen(cp));
352 memset(orig, 0, sizeof(orig));
353 memset(salt, 0, sizeof(salt));
357 strcat(salt, crypt_make_salt());
358 strcat(salt, crypt_make_salt());
359 strcat(salt, crypt_make_salt());
362 strcat(salt, crypt_make_salt());
363 cp = pw_encrypt(pass, salt);
365 memset(pass, 0, sizeof pass);
366 safe_strncpy(crypt_passwd, cp, sizeof(crypt_passwd));
370 static void set_filesize_limit(int blocks)
372 struct rlimit rlimit_fsize;
374 rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * blocks;
375 setrlimit(RLIMIT_FSIZE, &rlimit_fsize);