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 if (((fp = fopen(filename, "r+")) == 0) || (fstat(fileno(fp), &sb))) {
54 /* Lock the password file before updating */
55 lock.l_type = F_WRLCK;
56 lock.l_whence = SEEK_SET;
59 if (fcntl(fileno(fp), F_SETLK, &lock) < 0) {
60 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
63 lock.l_type = F_UNLCK;
65 snprintf(buf, sizeof buf, "%s-", filename);
66 if (create_backup(buf, fp)) {
67 fcntl(fileno(fp), F_SETLK, &lock);
71 snprintf(buf, sizeof buf, "%s+", filename);
73 out_fp = fopen(buf, "w");
75 if ((!out_fp) || (fchmod(fileno(out_fp), sb.st_mode & 0777))
76 || (fchown(fileno(out_fp), sb.st_uid, sb.st_gid))) {
77 fcntl(fileno(fp), F_SETLK, &lock);
84 snprintf(username, sizeof username, "%s:", pw->pw_name);
87 fgets(buffer, sizeof buffer, fp);
88 if (!continued) { /* Check to see if we're updating this line. */
89 if (strncmp(username, buffer, strlen(username)) == 0) {
90 /* we have a match. */
91 pw_rest = strchr(buffer, ':');
93 pw_rest = strchr(pw_rest, ':');
94 fprintf(out_fp, "%s:%s%s", buffer, crypt_pw, pw_rest);
96 fputs(buffer, out_fp);
99 fputs(buffer, out_fp);
101 if (buffer[strlen(buffer) - 1] == '\n') {
106 memset(buffer, 0, sizeof buffer);
109 if (fflush(out_fp) || fsync(fileno(out_fp)) || fclose(out_fp)) {
111 fcntl(fileno(fp), F_SETLK, &lock);
115 if (rename(buf, filename) < 0) {
116 fcntl(fileno(fp), F_SETLK, &lock);
120 fcntl(fileno(fp), F_SETLK, &lock);
127 int passwd_main(int argc, char **argv)
135 int algo = 1; /* -a - password algorithm */
136 int lflg = 0; /* -l - lock account */
137 int uflg = 0; /* -u - unlock account */
138 int dflg = 0; /* -d - delete password */
139 const struct passwd *pw;
141 amroot = (getuid() == 0);
142 openlog("passwd", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
143 while ((flag = getopt(argc, argv, "a:dlu")) != EOF) {
146 algo = get_algo(optarg);
161 myname = (char *) xstrdup(bb_getpwuid(NULL, getuid(), -1));
168 if ((lflg || uflg || dflg) && (optind >= argc || !amroot)) {
173 bb_error_msg_and_die("Unknown user %s\n", name);
175 if (!amroot && pw->pw_uid != getuid()) {
176 syslog(LOG_WARNING, "can't change pwd for `%s'", name);
177 bb_error_msg_and_die("Permission denied.\n");
179 if (ENABLE_FEATURE_SHADOWPASSWDS) {
180 struct spwd *sp = getspnam(name);
181 if (!sp) bb_error_msg_and_die("Unknown user %s", name);
183 } else cp = pw->pw_passwd;
186 safe_strncpy(crypt_passwd, cp, sizeof(crypt_passwd));
187 if (!(dflg || lflg || uflg)) {
190 syslog(LOG_WARNING, "password locked for `%s'", np);
191 bb_error_msg_and_die( "The password for `%s' cannot be changed.\n", np);
194 printf("Changing password for %s\n", name);
195 if (new_password(pw, amroot, algo)) {
196 bb_error_msg_and_die( "The password for %s is unchanged.\n", name);
199 if (crypt_passwd[0] != '!') {
200 memmove(&crypt_passwd[1], crypt_passwd,
201 sizeof crypt_passwd - 1);
202 crypt_passwd[sizeof crypt_passwd - 1] = '\0';
203 crypt_passwd[0] = '!';
206 if (crypt_passwd[0] == '!') {
207 memmove(crypt_passwd, &crypt_passwd[1],
208 sizeof crypt_passwd - 1);
211 crypt_passwd[0] = '\0';
213 set_filesize_limit(30000);
214 signal(SIGHUP, SIG_IGN);
215 signal(SIGINT, SIG_IGN);
216 signal(SIGQUIT, SIG_IGN);
219 if (!update_passwd(pw, crypt_passwd)) {
220 syslog(LOG_INFO, "password for `%s' changed by user `%s'", name,
222 printf("Password changed.\n");
224 syslog(LOG_WARNING, "an error occurred updating the password file");
225 bb_error_msg_and_die("An error occurred updating the password file.\n");
227 if (ENABLE_FEATURE_CLEAN_UP) free(myname);
233 static int create_backup(const char *backup, FILE * fp)
240 if (fstat(fileno(fp), &sb))
245 bkfp = fopen(backup, "w");
251 /* TODO: faster copy, not one-char-at-a-time. --marekm */
253 while ((c = getc(fp)) != EOF) {
254 if (putc(c, bkfp) == EOF)
257 if (c != EOF || fflush(bkfp)) {
266 ub.actime = sb.st_atime;
267 ub.modtime = sb.st_mtime;
272 static int i64c(int i)
278 if (i >= 2 && i < 12)
279 return ('0' - 2 + i);
280 if (i >= 12 && i < 38)
281 return ('A' - 12 + i);
282 if (i >= 38 && i < 63)
283 return ('a' - 38 + i);
287 static char *crypt_make_salt(void)
290 static unsigned long x;
291 static char result[3];
294 x += now + getpid() + clock();
295 result[0] = i64c(((x >> 18) ^ (x >> 6)) & 077);
296 result[1] = i64c(((x >> 12) ^ x) & 077);
302 static int new_password(const struct passwd *pw, int amroot, int algo)
307 char salt[12]; /* "$N$XXXXXXXX" or "XX" */
311 if (!amroot && crypt_passwd[0]) {
312 if (!(clear = bb_askpass(0, "Old password:"))) {
316 cipher = pw_encrypt(clear, crypt_passwd);
317 if (strcmp(cipher, crypt_passwd) != 0) {
318 syslog(LOG_WARNING, "incorrect password for `%s'",
320 bb_do_delay(FAIL_DELAY);
321 fprintf(stderr, "Incorrect password.\n");
325 safe_strncpy(orig, clear, sizeof(orig));
326 memset(clear, 0, strlen(clear));
327 memset(cipher, 0, strlen(cipher));
331 if (! (cp=bb_askpass(0, "Enter the new password (minimum of 5, maximum of 8 characters)\n"
332 "Please use a combination of upper and lower case letters and numbers.\n"
333 "Enter new password: ")))
335 memset(orig, 0, sizeof orig);
339 safe_strncpy(pass, cp, sizeof(pass));
340 memset(cp, 0, strlen(cp));
341 /* if (!obscure(orig, pass, pw)) { */
342 if (obscure(orig, pass, pw)) {
344 printf("\nWarning: weak password (continuing).\n");
350 if (!(cp = bb_askpass(0, "Re-enter new password: "))) {
351 memset(orig, 0, sizeof orig);
355 if (strcmp(cp, pass)) {
356 fprintf(stderr, "Passwords do not match.\n");
360 memset(cp, 0, strlen(cp));
361 memset(orig, 0, sizeof(orig));
362 memset(salt, 0, sizeof(salt));
366 strcat(salt, crypt_make_salt());
367 strcat(salt, crypt_make_salt());
368 strcat(salt, crypt_make_salt());
371 strcat(salt, crypt_make_salt());
372 cp = pw_encrypt(pass, salt);
374 memset(pass, 0, sizeof pass);
375 safe_strncpy(crypt_passwd, cp, sizeof(crypt_passwd));
379 static void set_filesize_limit(int blocks)
381 struct rlimit rlimit_fsize;
383 rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * blocks;
384 setrlimit(RLIMIT_FSIZE, &rlimit_fsize);