- }
- pw = getpwnam(name);
- if (!pw) {
- bb_error_msg_and_die("Unknown user %s\n", name);
- }
- if (!amroot && pw->pw_uid != getuid()) {
- syslog(LOG_WARNING, "can't change pwd for `%s'", name);
- bb_error_msg_and_die("Permission denied.\n");
- }
-#ifdef CONFIG_FEATURE_SHADOWPASSWDS
- sp = getspnam(name);
- if (!sp) {
- sp = (struct spwd *) pwd_to_spwd(pw);
- }
- cp = sp->sp_pwdp;
- np = sp->sp_namp;
-#else
- cp = pw->pw_passwd;
- np = name;
-#endif /* CONFIG_FEATURE_SHADOWPASSWDS */
-
- safe_strncpy(crypt_passwd, cp, sizeof(crypt_passwd));
- if (!(dflg || lflg || uflg)) {
- if (!amroot) {
- if (cp[0] == '!') {
- syslog(LOG_WARNING, "password locked for `%s'", np);
- bb_error_msg_and_die( "The password for `%s' cannot be changed.\n", np);
- }
- }
- printf("Changing password for %s\n", name);
- if (new_password(pw, amroot, algo)) {
- bb_error_msg_and_die( "The password for %s is unchanged.\n", name);
- }
- } else if (lflg) {
- if (crypt_passwd[0] != '!') {
- memmove(&crypt_passwd[1], crypt_passwd,
- sizeof crypt_passwd - 1);
- crypt_passwd[sizeof crypt_passwd - 1] = '\0';
- crypt_passwd[0] = '!';
- }
- } else if (uflg) {
- if (crypt_passwd[0] == '!') {
- memmove(crypt_passwd, &crypt_passwd[1],
- sizeof crypt_passwd - 1);
- }
- } else if (dflg) {
- crypt_passwd[0] = '\0';
- }
- set_filesize_limit(30000);
- signal(SIGHUP, SIG_IGN);
- signal(SIGINT, SIG_IGN);
- signal(SIGQUIT, SIG_IGN);
- umask(077);
- if (setuid(0)) {
- syslog(LOG_ERR, "can't setuid(0)");
- bb_error_msg_and_die( "Cannot change ID to root.\n");
- }
- if (!update_passwd(pw, crypt_passwd)) {
- syslog(LOG_INFO, "password for `%s' changed by user `%s'", name,
- myname);
- printf("Password changed.\n");
- } else {
- syslog(LOG_WARNING, "an error occurred updating the password file");
- bb_error_msg_and_die("An error occurred updating the password file.\n");
- }
- return (0);
-}
-