struct flock lock;
FILE *old_fp;
FILE *new_fp;
- char *new_name;
- char *last_char;
+ char *fnamesfx;
+ char *sfx_char;
unsigned user_len;
int old_fd;
int new_fd;
int i;
int cnt = 0;
- int ret = 1; /* failure */
+ int ret = -1; /* failure */
/* New passwd file, "/etc/passwd+" for now */
- new_name = xasprintf("%s+", filename);
- last_char = &new_name[strlen(new_name)-1];
+ fnamesfx = xasprintf("%s+", filename);
+ sfx_char = &fnamesfx[strlen(fnamesfx)-1];
username = xasprintf("%s:", username);
user_len = strlen(username);
i = 30;
do {
// FIXME: on last iteration try w/o O_EXCL but with O_TRUNC?
- new_fd = open(new_name, O_WRONLY|O_CREAT|O_EXCL,0600);
+ new_fd = open(fnamesfx, O_WRONLY|O_CREAT|O_EXCL, 0600);
if (new_fd >= 0) goto created;
if (errno != EEXIST) break;
usleep(100000); /* 0.1 sec */
} while (--i);
- bb_perror_msg("cannot create '%s'", new_name);
+ bb_perror_msg("cannot create '%s'", fnamesfx);
goto close_old_fp;
created:
}
/* Backup file is "/etc/passwd-" */
- last_char[0] = '-';
- /* Delete old one, create new as a hardlink to current */
- i = (unlink(new_name) && errno != ENOENT);
- if (i || link(filename, new_name))
- bb_perror_msg("warning: cannot create backup copy '%s'", new_name);
- last_char[0] = '+';
+ *sfx_char = '-';
+ /* Delete old backup */
+ i = (unlink(fnamesfx) && errno != ENOENT);
+ /* Create backup as a hardlink to current */
+ if (i || link(filename, fnamesfx))
+ bb_perror_msg("warning: cannot create backup copy '%s'", fnamesfx);
+ *sfx_char = '+';
/* Lock the password file before updating */
lock.l_type = F_WRLCK;
bb_perror_msg("warning: cannot lock '%s'", filename);
lock.l_type = F_UNLCK;
- /* Read current password file, write updated one */
+ /* Read current password file, write updated /etc/passwd+ */
while (1) {
char *line = xmalloc_fgets(old_fp);
if (!line) break; /* EOF/error */
/* we have a match with "username:"... */
const char *cp = line + user_len;
/* now cp -> old passwd, skip it: */
- cp = strchr(cp, ':');
- if (!cp) cp = "";
+ cp = strchrnul(cp, ':');
/* now cp -> ':' after old passwd or -> "" */
fprintf(new_fp, "%s%s%s", username, new_pw, cp);
cnt++;
/* We do want all of them to execute, thus | instead of || */
if ((ferror(old_fp) | fflush(new_fp) | fsync(new_fd) | fclose(new_fp))
- || rename(new_name, filename)
+ || rename(fnamesfx, filename)
) {
/* At least one of those failed */
goto unlink_new;
ret = cnt; /* whee, success! */
unlink_new:
- if (ret) unlink(new_name);
+ if (ret < 0) unlink(fnamesfx);
close_old_fp:
fclose(old_fp);
free_mem:
- if (ENABLE_FEATURE_CLEAN_UP) free(new_name);
- if (ENABLE_FEATURE_CLEAN_UP) free((char*)username);
+ free(fnamesfx);
+ free((char*)username);
return ret;
}
{
char *name, *pass;
char salt[sizeof("$N$XXXXXXXX")];
- int opt, rc;
+ int opt;
int rnd = rnd; /* we *want* it to be non-initialized! */
- const char *pwfile = bb_path_passwd_file;
- if (getuid() != 0)
+ if (getuid())
bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
-#if ENABLE_FEATURE_SHADOWPASSWDS
- if (access(bb_path_shadow_file, F_OK) == 0)
- pwfile = bb_path_shadow_file;
-#endif
-
- opt_complementary = "m--e";
+ opt_complementary = "?m--e:e--m";
USE_GETOPT_LONG(applet_long_options = chpasswd_opts;)
opt = getopt32(argc, argv, "em");
bb_error_msg_and_die("missing new password");
*pass++ = '\0';
- //if (!getpwnam(name))
- // bb_error_msg_and_die("unknown user %s", name);
+ xuname2uid(name); /* dies if there is no such user */
if (!(opt & OPT_ENC)) {
rnd = crypt_make_salt(salt, 1, rnd);
pass = pw_encrypt(pass, salt);
}
- rc = update_passwd(pwfile, name, pass);
/* LOGMODE_BOTH logs to syslog */
logmode = LOGMODE_BOTH;
- if (rc < 0)
- bb_error_msg_and_die("an error occurred updating %s", pwfile);
- if (rc > 0)
+
+ if ((ENABLE_FEATURE_SHADOWPASSWDS
+ && !update_passwd(bb_path_shadow_file, name, pass))
+ || !update_passwd(bb_path_passwd_file, name, pass)
+ ) {
+ bb_error_msg_and_die("an error occurred updating password for %s", name);
+ } else {
bb_info_msg("Password for '%s' changed", name);
- else
- bb_info_msg("User '%s' not found", name);
+ }
logmode = LOGMODE_STDIO;
free(name);
}