1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 #include <sys/types.h>
16 #include <sys/resource.h>
21 static char crypt_passwd[128];
23 static int create_backup(const char *backup, FILE * fp);
24 static int new_password(const struct passwd *pw, int amroot, int algo);
25 static void set_filesize_limit(int blocks);
28 static int get_algo(char *a)
30 int x = 1; /* standard: MD5 */
32 if (strcasecmp(a, "des") == 0)
38 static int update_passwd(const struct passwd *pw, const char *crypt_pw)
52 #if ENABLE_FEATURE_SHADOWPASSWDS
53 if (access(bb_path_shadow_file, F_OK) == 0) {
54 snprintf(filename, sizeof filename, "%s", bb_path_shadow_file);
58 snprintf(filename, sizeof filename, "%s", bb_path_passwd_file);
61 if (((fp = fopen(filename, "r+")) == 0) || (fstat(fileno(fp), &sb))) {
66 /* Lock the password file before updating */
67 lock.l_type = F_WRLCK;
68 lock.l_whence = SEEK_SET;
71 if (fcntl(fileno(fp), F_SETLK, &lock) < 0) {
72 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
75 lock.l_type = F_UNLCK;
77 snprintf(buf, sizeof buf, "%s-", filename);
78 if (create_backup(buf, fp)) {
79 fcntl(fileno(fp), F_SETLK, &lock);
83 snprintf(buf, sizeof buf, "%s+", filename);
85 out_fp = fopen(buf, "w");
87 if ((!out_fp) || (fchmod(fileno(out_fp), sb.st_mode & 0777))
88 || (fchown(fileno(out_fp), sb.st_uid, sb.st_gid))) {
89 fcntl(fileno(fp), F_SETLK, &lock);
96 snprintf(username, sizeof username, "%s:", pw->pw_name);
99 fgets(buffer, sizeof buffer, fp);
100 if (!continued) { /* Check to see if we're updating this line. */
101 if (strncmp(username, buffer, strlen(username)) == 0) {
102 /* we have a match. */
103 pw_rest = strchr(buffer, ':');
105 pw_rest = strchr(pw_rest, ':');
106 fprintf(out_fp, "%s:%s%s", buffer, crypt_pw, pw_rest);
108 fputs(buffer, out_fp);
111 fputs(buffer, out_fp);
113 if (buffer[strlen(buffer) - 1] == '\n') {
118 memset(buffer, 0, sizeof buffer);
121 if (fflush(out_fp) || fsync(fileno(out_fp)) || fclose(out_fp)) {
123 fcntl(fileno(fp), F_SETLK, &lock);
127 if (rename(buf, filename) < 0) {
128 fcntl(fileno(fp), F_SETLK, &lock);
132 fcntl(fileno(fp), F_SETLK, &lock);
139 int passwd_main(int argc, char **argv)
147 int algo = 1; /* -a - password algorithm */
148 int lflg = 0; /* -l - lock account */
149 int uflg = 0; /* -u - unlock account */
150 int dflg = 0; /* -d - delete password */
151 const struct passwd *pw;
153 amroot = (getuid() == 0);
154 openlog("passwd", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
155 while ((flag = getopt(argc, argv, "a:dlu")) != EOF) {
158 algo = get_algo(optarg);
173 myname = (char *) bb_xstrdup(bb_getpwuid(NULL, getuid(), -1));
180 if ((lflg || uflg || dflg) && (optind >= argc || !amroot)) {
185 bb_error_msg_and_die("Unknown user %s\n", name);
187 if (!amroot && pw->pw_uid != getuid()) {
188 syslog(LOG_WARNING, "can't change pwd for `%s'", name);
189 bb_error_msg_and_die("Permission denied.\n");
191 if (ENABLE_FEATURE_SHADOWPASSWDS) {
192 struct spwd *sp = getspnam(name);
193 if (!sp) bb_error_msg_and_die("Unknown user %s", name);
195 } else cp = pw->pw_passwd;
198 safe_strncpy(crypt_passwd, cp, sizeof(crypt_passwd));
199 if (!(dflg || lflg || uflg)) {
202 syslog(LOG_WARNING, "password locked for `%s'", np);
203 bb_error_msg_and_die( "The password for `%s' cannot be changed.\n", np);
206 printf("Changing password for %s\n", name);
207 if (new_password(pw, amroot, algo)) {
208 bb_error_msg_and_die( "The password for %s is unchanged.\n", name);
211 if (crypt_passwd[0] != '!') {
212 memmove(&crypt_passwd[1], crypt_passwd,
213 sizeof crypt_passwd - 1);
214 crypt_passwd[sizeof crypt_passwd - 1] = '\0';
215 crypt_passwd[0] = '!';
218 if (crypt_passwd[0] == '!') {
219 memmove(crypt_passwd, &crypt_passwd[1],
220 sizeof crypt_passwd - 1);
223 crypt_passwd[0] = '\0';
225 set_filesize_limit(30000);
226 signal(SIGHUP, SIG_IGN);
227 signal(SIGINT, SIG_IGN);
228 signal(SIGQUIT, SIG_IGN);
231 if (!update_passwd(pw, crypt_passwd)) {
232 syslog(LOG_INFO, "password for `%s' changed by user `%s'", name,
234 printf("Password changed.\n");
236 syslog(LOG_WARNING, "an error occurred updating the password file");
237 bb_error_msg_and_die("An error occurred updating the password file.\n");
239 if (ENABLE_FEATURE_CLEAN_UP) free(myname);
245 static int create_backup(const char *backup, FILE * fp)
252 if (fstat(fileno(fp), &sb))
257 bkfp = fopen(backup, "w");
263 /* TODO: faster copy, not one-char-at-a-time. --marekm */
265 while ((c = getc(fp)) != EOF) {
266 if (putc(c, bkfp) == EOF)
269 if (c != EOF || fflush(bkfp)) {
278 ub.actime = sb.st_atime;
279 ub.modtime = sb.st_mtime;
284 static int i64c(int i)
290 if (i >= 2 && i < 12)
291 return ('0' - 2 + i);
292 if (i >= 12 && i < 38)
293 return ('A' - 12 + i);
294 if (i >= 38 && i < 63)
295 return ('a' - 38 + i);
299 static char *crypt_make_salt(void)
302 static unsigned long x;
303 static char result[3];
306 x += now + getpid() + clock();
307 result[0] = i64c(((x >> 18) ^ (x >> 6)) & 077);
308 result[1] = i64c(((x >> 12) ^ x) & 077);
314 static int new_password(const struct passwd *pw, int amroot, int algo)
319 char salt[12]; /* "$N$XXXXXXXX" or "XX" */
323 if (!amroot && crypt_passwd[0]) {
324 if (!(clear = bb_askpass(0, "Old password:"))) {
328 cipher = pw_encrypt(clear, crypt_passwd);
329 if (strcmp(cipher, crypt_passwd) != 0) {
330 syslog(LOG_WARNING, "incorrect password for `%s'",
332 bb_do_delay(FAIL_DELAY);
333 fprintf(stderr, "Incorrect password.\n");
337 safe_strncpy(orig, clear, sizeof(orig));
338 memset(clear, 0, strlen(clear));
339 memset(cipher, 0, strlen(cipher));
343 if (! (cp=bb_askpass(0, "Enter the new password (minimum of 5, maximum of 8 characters)\n"
344 "Please use a combination of upper and lower case letters and numbers.\n"
345 "Enter new password: ")))
347 memset(orig, 0, sizeof orig);
351 safe_strncpy(pass, cp, sizeof(pass));
352 memset(cp, 0, strlen(cp));
353 /* if (!obscure(orig, pass, pw)) { */
354 if (obscure(orig, pass, pw)) {
356 printf("\nWarning: weak password (continuing).\n");
362 if (!(cp = bb_askpass(0, "Re-enter new password: "))) {
363 memset(orig, 0, sizeof orig);
367 if (strcmp(cp, pass)) {
368 fprintf(stderr, "Passwords do not match.\n");
372 memset(cp, 0, strlen(cp));
373 memset(orig, 0, sizeof(orig));
374 memset(salt, 0, sizeof(salt));
378 strcat(salt, crypt_make_salt());
379 strcat(salt, crypt_make_salt());
380 strcat(salt, crypt_make_salt());
383 strcat(salt, crypt_make_salt());
384 cp = pw_encrypt(pass, salt);
386 memset(pass, 0, sizeof pass);
387 safe_strncpy(crypt_passwd, cp, sizeof(crypt_passwd));
391 static void set_filesize_limit(int blocks)
393 struct rlimit rlimit_fsize;
395 rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * blocks;
396 setrlimit(RLIMIT_FSIZE, &rlimit_fsize);