1 /* vi: set sw=4 ts=4: */
12 #include <sys/resource.h>
17 static char crypt_passwd[128];
19 static int create_backup(const char *backup, FILE * fp);
20 static int new_password(const struct passwd *pw, int amroot, int algo);
21 static void set_filesize_limit(int blocks);
24 static int get_algo(char *a)
26 int x = 1; /* standard: MD5 */
28 if (strcasecmp(a, "des") == 0)
34 static int update_passwd(const struct passwd *pw, const char *crypt_pw)
48 #if ENABLE_FEATURE_SHADOWPASSWDS
49 if (access(bb_path_shadow_file, F_OK) == 0) {
50 snprintf(filename, sizeof filename, "%s", bb_path_shadow_file);
54 snprintf(filename, sizeof filename, "%s", bb_path_passwd_file);
57 if (((fp = fopen(filename, "r+")) == 0) || (fstat(fileno(fp), &sb))) {
62 /* Lock the password file before updating */
63 lock.l_type = F_WRLCK;
64 lock.l_whence = SEEK_SET;
67 if (fcntl(fileno(fp), F_SETLK, &lock) < 0) {
68 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
71 lock.l_type = F_UNLCK;
73 snprintf(buf, sizeof buf, "%s-", filename);
74 if (create_backup(buf, fp)) {
75 fcntl(fileno(fp), F_SETLK, &lock);
79 snprintf(buf, sizeof buf, "%s+", filename);
81 out_fp = fopen(buf, "w");
83 if ((!out_fp) || (fchmod(fileno(out_fp), sb.st_mode & 0777))
84 || (fchown(fileno(out_fp), sb.st_uid, sb.st_gid))) {
85 fcntl(fileno(fp), F_SETLK, &lock);
92 snprintf(username, sizeof username, "%s:", pw->pw_name);
95 fgets(buffer, sizeof buffer, fp);
96 if (!continued) { /* Check to see if we're updating this line. */
97 if (strncmp(username, buffer, strlen(username)) == 0) {
98 /* we have a match. */
99 pw_rest = strchr(buffer, ':');
101 pw_rest = strchr(pw_rest, ':');
102 fprintf(out_fp, "%s:%s%s", buffer, crypt_pw, pw_rest);
104 fputs(buffer, out_fp);
107 fputs(buffer, out_fp);
109 if (buffer[strlen(buffer) - 1] == '\n') {
114 memset(buffer, 0, sizeof buffer);
117 if (fflush(out_fp) || fsync(fileno(out_fp)) || fclose(out_fp)) {
119 fcntl(fileno(fp), F_SETLK, &lock);
123 if (rename(buf, filename) < 0) {
124 fcntl(fileno(fp), F_SETLK, &lock);
128 fcntl(fileno(fp), F_SETLK, &lock);
135 extern int passwd_main(int argc, char **argv)
143 int algo = 1; /* -a - password algorithm */
144 int lflg = 0; /* -l - lock account */
145 int uflg = 0; /* -u - unlock account */
146 int dflg = 0; /* -d - delete password */
147 const struct passwd *pw;
149 #if ENABLE_FEATURE_SHADOWPASSWDS
150 const struct spwd *sp;
152 amroot = (getuid() == 0);
153 openlog("passwd", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
154 while ((flag = getopt(argc, argv, "a:dlu")) != EOF) {
157 algo = get_algo(optarg);
172 myname = (char *) bb_xstrdup(bb_getpwuid(NULL, getuid(), -1));
179 if ((lflg || uflg || dflg) && (optind >= argc || !amroot)) {
184 bb_error_msg_and_die("Unknown user %s\n", name);
186 if (!amroot && pw->pw_uid != getuid()) {
187 syslog(LOG_WARNING, "can't change pwd for `%s'", name);
188 bb_error_msg_and_die("Permission denied.\n");
190 #if ENABLE_FEATURE_SHADOWPASSWDS
193 sp = (struct spwd *) pwd_to_spwd(pw);
202 safe_strncpy(crypt_passwd, cp, sizeof(crypt_passwd));
203 if (!(dflg || lflg || uflg)) {
206 syslog(LOG_WARNING, "password locked for `%s'", np);
207 bb_error_msg_and_die( "The password for `%s' cannot be changed.\n", np);
210 printf("Changing password for %s\n", name);
211 if (new_password(pw, amroot, algo)) {
212 bb_error_msg_and_die( "The password for %s is unchanged.\n", name);
215 if (crypt_passwd[0] != '!') {
216 memmove(&crypt_passwd[1], crypt_passwd,
217 sizeof crypt_passwd - 1);
218 crypt_passwd[sizeof crypt_passwd - 1] = '\0';
219 crypt_passwd[0] = '!';
222 if (crypt_passwd[0] == '!') {
223 memmove(crypt_passwd, &crypt_passwd[1],
224 sizeof crypt_passwd - 1);
227 crypt_passwd[0] = '\0';
229 set_filesize_limit(30000);
230 signal(SIGHUP, SIG_IGN);
231 signal(SIGINT, SIG_IGN);
232 signal(SIGQUIT, SIG_IGN);
235 syslog(LOG_ERR, "can't setuid(0)");
236 bb_error_msg_and_die( "Cannot change ID to root.\n");
238 if (!update_passwd(pw, crypt_passwd)) {
239 syslog(LOG_INFO, "password for `%s' changed by user `%s'", name,
241 printf("Password changed.\n");
243 syslog(LOG_WARNING, "an error occurred updating the password file");
244 bb_error_msg_and_die("An error occurred updating the password file.\n");
251 static int create_backup(const char *backup, FILE * fp)
258 if (fstat(fileno(fp), &sb))
263 bkfp = fopen(backup, "w");
269 /* TODO: faster copy, not one-char-at-a-time. --marekm */
271 while ((c = getc(fp)) != EOF) {
272 if (putc(c, bkfp) == EOF)
275 if (c != EOF || fflush(bkfp)) {
284 ub.actime = sb.st_atime;
285 ub.modtime = sb.st_mtime;
290 static int i64c(int i)
296 if (i >= 2 && i < 12)
297 return ('0' - 2 + i);
298 if (i >= 12 && i < 38)
299 return ('A' - 12 + i);
300 if (i >= 38 && i < 63)
301 return ('a' - 38 + i);
305 static char *crypt_make_salt(void)
308 static unsigned long x;
309 static char result[3];
312 x += now + getpid() + clock();
313 result[0] = i64c(((x >> 18) ^ (x >> 6)) & 077);
314 result[1] = i64c(((x >> 12) ^ x) & 077);
320 static int new_password(const struct passwd *pw, int amroot, int algo)
328 if (!amroot && crypt_passwd[0]) {
329 if (!(clear = bb_askpass(0, "Old password:"))) {
333 cipher = pw_encrypt(clear, crypt_passwd);
334 if (strcmp(cipher, crypt_passwd) != 0) {
335 syslog(LOG_WARNING, "incorrect password for `%s'",
337 bb_do_delay(FAIL_DELAY);
338 fprintf(stderr, "Incorrect password.\n");
342 safe_strncpy(orig, clear, sizeof(orig));
343 memset(clear, 0, strlen(clear));
344 memset(cipher, 0, strlen(cipher));
348 if (! (cp=bb_askpass(0, "Enter the new password (minimum of 5, maximum of 8 characters)\n"
349 "Please use a combination of upper and lower case letters and numbers.\n"
350 "Enter new password: ")))
352 memset(orig, 0, sizeof orig);
356 safe_strncpy(pass, cp, sizeof(pass));
357 memset(cp, 0, strlen(cp));
358 /* if (!obscure(orig, pass, pw)) { */
359 if (obscure(orig, pass, pw)) {
361 printf("\nWarning: weak password (continuing).\n");
367 if (!(cp = bb_askpass(0, "Re-enter new password: "))) {
368 memset(orig, 0, sizeof orig);
372 if (strcmp(cp, pass)) {
373 fprintf(stderr, "Passwords do not match.\n");
377 memset(cp, 0, strlen(cp));
378 memset(orig, 0, sizeof(orig));
381 cp = pw_encrypt(pass, "$1$");
383 cp = pw_encrypt(pass, crypt_make_salt());
384 memset(pass, 0, sizeof pass);
385 safe_strncpy(crypt_passwd, cp, sizeof(crypt_passwd));
389 static void set_filesize_limit(int blocks)
391 struct rlimit rlimit_fsize;
393 rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * blocks;
394 setrlimit(RLIMIT_FSIZE, &rlimit_fsize);