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 syslog(LOG_ERR, "can't setuid(0)");
232 bb_error_msg_and_die( "Cannot change ID to root.\n");
234 if (!update_passwd(pw, crypt_passwd)) {
235 syslog(LOG_INFO, "password for `%s' changed by user `%s'", name,
237 printf("Password changed.\n");
239 syslog(LOG_WARNING, "an error occurred updating the password file");
240 bb_error_msg_and_die("An error occurred updating the password file.\n");
242 if (ENABLE_FEATURE_CLEAN_UP) free(myname);
248 static int create_backup(const char *backup, FILE * fp)
255 if (fstat(fileno(fp), &sb))
260 bkfp = fopen(backup, "w");
266 /* TODO: faster copy, not one-char-at-a-time. --marekm */
268 while ((c = getc(fp)) != EOF) {
269 if (putc(c, bkfp) == EOF)
272 if (c != EOF || fflush(bkfp)) {
281 ub.actime = sb.st_atime;
282 ub.modtime = sb.st_mtime;
287 static int i64c(int i)
293 if (i >= 2 && i < 12)
294 return ('0' - 2 + i);
295 if (i >= 12 && i < 38)
296 return ('A' - 12 + i);
297 if (i >= 38 && i < 63)
298 return ('a' - 38 + i);
302 static char *crypt_make_salt(void)
305 static unsigned long x;
306 static char result[3];
309 x += now + getpid() + clock();
310 result[0] = i64c(((x >> 18) ^ (x >> 6)) & 077);
311 result[1] = i64c(((x >> 12) ^ x) & 077);
317 static int new_password(const struct passwd *pw, int amroot, int algo)
322 char salt[12]; /* "$N$XXXXXXXX" or "XX" */
326 if (!amroot && crypt_passwd[0]) {
327 if (!(clear = bb_askpass(0, "Old password:"))) {
331 cipher = pw_encrypt(clear, crypt_passwd);
332 if (strcmp(cipher, crypt_passwd) != 0) {
333 syslog(LOG_WARNING, "incorrect password for `%s'",
335 bb_do_delay(FAIL_DELAY);
336 fprintf(stderr, "Incorrect password.\n");
340 safe_strncpy(orig, clear, sizeof(orig));
341 memset(clear, 0, strlen(clear));
342 memset(cipher, 0, strlen(cipher));
346 if (! (cp=bb_askpass(0, "Enter the new password (minimum of 5, maximum of 8 characters)\n"
347 "Please use a combination of upper and lower case letters and numbers.\n"
348 "Enter new password: ")))
350 memset(orig, 0, sizeof orig);
354 safe_strncpy(pass, cp, sizeof(pass));
355 memset(cp, 0, strlen(cp));
356 /* if (!obscure(orig, pass, pw)) { */
357 if (obscure(orig, pass, pw)) {
359 printf("\nWarning: weak password (continuing).\n");
365 if (!(cp = bb_askpass(0, "Re-enter new password: "))) {
366 memset(orig, 0, sizeof orig);
370 if (strcmp(cp, pass)) {
371 fprintf(stderr, "Passwords do not match.\n");
375 memset(cp, 0, strlen(cp));
376 memset(orig, 0, sizeof(orig));
377 memset(salt, 0, sizeof(salt));
381 strcat(salt, crypt_make_salt());
382 strcat(salt, crypt_make_salt());
383 strcat(salt, crypt_make_salt());
386 strcat(salt, crypt_make_salt());
387 cp = pw_encrypt(pass, salt);
389 memset(pass, 0, sizeof pass);
390 safe_strncpy(crypt_passwd, cp, sizeof(crypt_passwd));
394 static void set_filesize_limit(int blocks)
396 struct rlimit rlimit_fsize;
398 rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * blocks;
399 setrlimit(RLIMIT_FSIZE, &rlimit_fsize);