[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
[1765] | 2 | /*
|
---|
[2725] | 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[1765] | 4 | */
|
---|
[3621] | 5 | //config:config PASSWD
|
---|
| 6 | //config: bool "passwd"
|
---|
| 7 | //config: default y
|
---|
| 8 | //config: select FEATURE_SYSLOG
|
---|
| 9 | //config: help
|
---|
| 10 | //config: passwd changes passwords for user and group accounts. A normal user
|
---|
| 11 | //config: may only change the password for his/her own account, the super user
|
---|
| 12 | //config: may change the password for any account. The administrator of a group
|
---|
| 13 | //config: may change the password for the group.
|
---|
| 14 | //config:
|
---|
| 15 | //config: Note that Busybox binary must be setuid root for this applet to
|
---|
| 16 | //config: work properly.
|
---|
| 17 | //config:
|
---|
| 18 | //config:config FEATURE_PASSWD_WEAK_CHECK
|
---|
| 19 | //config: bool "Check new passwords for weakness"
|
---|
| 20 | //config: default y
|
---|
| 21 | //config: depends on PASSWD
|
---|
| 22 | //config: help
|
---|
| 23 | //config: With this option passwd will refuse new passwords which are "weak".
|
---|
[3232] | 24 |
|
---|
[3621] | 25 | //applet:/* Needs to be run by root or be suid root - needs to change /etc/{passwd,shadow}: */
|
---|
| 26 | //applet:IF_PASSWD(APPLET(passwd, BB_DIR_USR_BIN, BB_SUID_REQUIRE))
|
---|
| 27 |
|
---|
| 28 | //kbuild:lib-$(CONFIG_PASSWD) += passwd.o
|
---|
| 29 |
|
---|
[3232] | 30 | //usage:#define passwd_trivial_usage
|
---|
| 31 | //usage: "[OPTIONS] [USER]"
|
---|
| 32 | //usage:#define passwd_full_usage "\n\n"
|
---|
| 33 | //usage: "Change USER's password (default: current user)"
|
---|
| 34 | //usage: "\n"
|
---|
| 35 | //usage: "\n -a ALG Encryption method"
|
---|
| 36 | //usage: "\n -d Set password to ''"
|
---|
| 37 | //usage: "\n -l Lock (disable) account"
|
---|
| 38 | //usage: "\n -u Unlock (enable) account"
|
---|
| 39 |
|
---|
[1765] | 40 | #include "libbb.h"
|
---|
[821] | 41 | #include <syslog.h>
|
---|
[3232] | 42 | #include <sys/resource.h> /* setrlimit */
|
---|
[821] | 43 |
|
---|
[3232] | 44 | static char* new_password(const struct passwd *pw, uid_t myuid, const char *algo)
|
---|
[821] | 45 | {
|
---|
[3232] | 46 | char salt[MAX_PW_SALT_LEN];
|
---|
[1765] | 47 | char *orig = (char*)"";
|
---|
| 48 | char *newp = NULL;
|
---|
| 49 | char *cp = NULL;
|
---|
| 50 | char *ret = NULL; /* failure so far */
|
---|
[821] | 51 |
|
---|
[3232] | 52 | if (myuid != 0 && pw->pw_passwd[0]) {
|
---|
[2725] | 53 | char *encrypted;
|
---|
| 54 |
|
---|
| 55 | orig = bb_ask_stdin("Old password: "); /* returns ptr to static */
|
---|
[1765] | 56 | if (!orig)
|
---|
| 57 | goto err_ret;
|
---|
[2725] | 58 | encrypted = pw_encrypt(orig, pw->pw_passwd, 1); /* returns malloced str */
|
---|
| 59 | if (strcmp(encrypted, pw->pw_passwd) != 0) {
|
---|
[3232] | 60 | syslog(LOG_WARNING, "incorrect password for %s", pw->pw_name);
|
---|
| 61 | bb_do_delay(LOGIN_FAIL_DELAY);
|
---|
[1765] | 62 | puts("Incorrect password");
|
---|
| 63 | goto err_ret;
|
---|
| 64 | }
|
---|
[3232] | 65 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
| 66 | free(encrypted);
|
---|
[821] | 67 | }
|
---|
[2725] | 68 | orig = xstrdup(orig); /* or else bb_ask_stdin() will destroy it */
|
---|
| 69 | newp = bb_ask_stdin("New password: "); /* returns ptr to static */
|
---|
[1765] | 70 | if (!newp)
|
---|
| 71 | goto err_ret;
|
---|
[2725] | 72 | newp = xstrdup(newp); /* we are going to bb_ask_stdin() again, so save it */
|
---|
[1765] | 73 | if (ENABLE_FEATURE_PASSWD_WEAK_CHECK
|
---|
[3232] | 74 | && obscure(orig, newp, pw)
|
---|
| 75 | && myuid != 0
|
---|
| 76 | ) {
|
---|
[1765] | 77 | goto err_ret; /* non-root is not allowed to have weak passwd */
|
---|
[3232] | 78 | }
|
---|
[821] | 79 |
|
---|
[2725] | 80 | cp = bb_ask_stdin("Retype password: ");
|
---|
[1765] | 81 | if (!cp)
|
---|
| 82 | goto err_ret;
|
---|
[3232] | 83 | if (strcmp(cp, newp) != 0) {
|
---|
[1765] | 84 | puts("Passwords don't match");
|
---|
| 85 | goto err_ret;
|
---|
[821] | 86 | }
|
---|
| 87 |
|
---|
[3232] | 88 | crypt_make_pw_salt(salt, algo);
|
---|
| 89 |
|
---|
[2725] | 90 | /* pw_encrypt returns malloced str */
|
---|
| 91 | ret = pw_encrypt(newp, salt, 1);
|
---|
[1765] | 92 | /* whee, success! */
|
---|
[821] | 93 |
|
---|
[1765] | 94 | err_ret:
|
---|
| 95 | nuke_str(orig);
|
---|
| 96 | if (ENABLE_FEATURE_CLEAN_UP) free(orig);
|
---|
[3232] | 97 |
|
---|
[1765] | 98 | nuke_str(newp);
|
---|
| 99 | if (ENABLE_FEATURE_CLEAN_UP) free(newp);
|
---|
[3232] | 100 |
|
---|
[1765] | 101 | nuke_str(cp);
|
---|
| 102 | return ret;
|
---|
[821] | 103 | }
|
---|
| 104 |
|
---|
[2725] | 105 | int passwd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 106 | int passwd_main(int argc UNUSED_PARAM, char **argv)
|
---|
[821] | 107 | {
|
---|
[1765] | 108 | enum {
|
---|
[3232] | 109 | OPT_algo = (1 << 0), /* -a - password algorithm */
|
---|
| 110 | OPT_lock = (1 << 1), /* -l - lock account */
|
---|
| 111 | OPT_unlock = (1 << 2), /* -u - unlock account */
|
---|
| 112 | OPT_delete = (1 << 3), /* -d - delete password */
|
---|
| 113 | OPT_lud = OPT_lock | OPT_unlock | OPT_delete,
|
---|
[1765] | 114 | };
|
---|
| 115 | unsigned opt;
|
---|
| 116 | int rc;
|
---|
[3232] | 117 | const char *opt_a = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO;
|
---|
[1765] | 118 | const char *filename;
|
---|
| 119 | char *myname;
|
---|
[821] | 120 | char *name;
|
---|
[1765] | 121 | char *newp;
|
---|
| 122 | struct passwd *pw;
|
---|
| 123 | uid_t myuid;
|
---|
| 124 | struct rlimit rlimit_fsize;
|
---|
| 125 | char c;
|
---|
[821] | 126 | #if ENABLE_FEATURE_SHADOWPASSWDS
|
---|
[1765] | 127 | /* Using _r function to avoid pulling in static buffers */
|
---|
| 128 | struct spwd spw;
|
---|
| 129 | char buffer[256];
|
---|
[821] | 130 | #endif
|
---|
[1765] | 131 |
|
---|
| 132 | logmode = LOGMODE_BOTH;
|
---|
[2725] | 133 | openlog(applet_name, 0, LOG_AUTH);
|
---|
[1765] | 134 | opt = getopt32(argv, "a:lud", &opt_a);
|
---|
| 135 | //argc -= optind;
|
---|
| 136 | argv += optind;
|
---|
| 137 |
|
---|
| 138 | myuid = getuid();
|
---|
| 139 | /* -l, -u, -d require root priv and username argument */
|
---|
[3232] | 140 | if ((opt & OPT_lud) && (myuid != 0 || !argv[0]))
|
---|
[821] | 141 | bb_show_usage();
|
---|
[1765] | 142 |
|
---|
| 143 | /* Will complain and die if username not found */
|
---|
[2725] | 144 | myname = xstrdup(xuid2uname(myuid));
|
---|
[1765] | 145 | name = argv[0] ? argv[0] : myname;
|
---|
| 146 |
|
---|
[2725] | 147 | pw = xgetpwnam(name);
|
---|
[3232] | 148 | if (myuid != 0 && pw->pw_uid != myuid) {
|
---|
[1765] | 149 | /* LOGMODE_BOTH */
|
---|
| 150 | bb_error_msg_and_die("%s can't change password for %s", myname, name);
|
---|
[821] | 151 | }
|
---|
[1765] | 152 |
|
---|
[821] | 153 | #if ENABLE_FEATURE_SHADOWPASSWDS
|
---|
[2725] | 154 | {
|
---|
| 155 | /* getspnam_r may return 0 yet set result to NULL.
|
---|
| 156 | * At least glibc 2.4 does this. Be extra paranoid here. */
|
---|
| 157 | struct spwd *result = NULL;
|
---|
| 158 | errno = 0;
|
---|
| 159 | if (getspnam_r(pw->pw_name, &spw, buffer, sizeof(buffer), &result) != 0
|
---|
| 160 | || !result /* no error, but no record found either */
|
---|
| 161 | || strcmp(result->sp_namp, pw->pw_name) != 0 /* paranoia */
|
---|
| 162 | ) {
|
---|
| 163 | if (errno != ENOENT) {
|
---|
| 164 | /* LOGMODE_BOTH */
|
---|
| 165 | bb_perror_msg("no record of %s in %s, using %s",
|
---|
| 166 | name, bb_path_shadow_file,
|
---|
| 167 | bb_path_passwd_file);
|
---|
| 168 | }
|
---|
| 169 | /* else: /etc/shadow does not exist,
|
---|
| 170 | * apparently we are on a shadow-less system,
|
---|
| 171 | * no surprise there */
|
---|
| 172 | } else {
|
---|
| 173 | pw->pw_passwd = result->sp_pwdp;
|
---|
| 174 | }
|
---|
[821] | 175 | }
|
---|
| 176 | #endif
|
---|
| 177 |
|
---|
[1765] | 178 | /* Decide what the new password will be */
|
---|
| 179 | newp = NULL;
|
---|
| 180 | c = pw->pw_passwd[0] - '!';
|
---|
| 181 | if (!(opt & OPT_lud)) {
|
---|
[3232] | 182 | if (myuid != 0 && !c) { /* passwd starts with '!' */
|
---|
[1765] | 183 | /* LOGMODE_BOTH */
|
---|
[2725] | 184 | bb_error_msg_and_die("can't change "
|
---|
[1765] | 185 | "locked password for %s", name);
|
---|
[821] | 186 | }
|
---|
| 187 | printf("Changing password for %s\n", name);
|
---|
[3232] | 188 | newp = new_password(pw, myuid, opt_a);
|
---|
[1765] | 189 | if (!newp) {
|
---|
| 190 | logmode = LOGMODE_STDIO;
|
---|
| 191 | bb_error_msg_and_die("password for %s is unchanged", name);
|
---|
[821] | 192 | }
|
---|
[1765] | 193 | } else if (opt & OPT_lock) {
|
---|
[3232] | 194 | if (!c)
|
---|
| 195 | goto skip; /* passwd starts with '!' */
|
---|
[1765] | 196 | newp = xasprintf("!%s", pw->pw_passwd);
|
---|
| 197 | } else if (opt & OPT_unlock) {
|
---|
[3232] | 198 | if (c)
|
---|
| 199 | goto skip; /* not '!' */
|
---|
[2725] | 200 | /* pw->pw_passwd points to static storage,
|
---|
[1765] | 201 | * strdup'ing to avoid nasty surprizes */
|
---|
| 202 | newp = xstrdup(&pw->pw_passwd[1]);
|
---|
| 203 | } else if (opt & OPT_delete) {
|
---|
| 204 | newp = (char*)"";
|
---|
[821] | 205 | }
|
---|
[1765] | 206 |
|
---|
| 207 | rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * 30000;
|
---|
| 208 | setrlimit(RLIMIT_FSIZE, &rlimit_fsize);
|
---|
[2725] | 209 | bb_signals(0
|
---|
| 210 | + (1 << SIGHUP)
|
---|
| 211 | + (1 << SIGINT)
|
---|
| 212 | + (1 << SIGQUIT)
|
---|
| 213 | , SIG_IGN);
|
---|
[821] | 214 | umask(077);
|
---|
| 215 | xsetuid(0);
|
---|
| 216 |
|
---|
[1765] | 217 | #if ENABLE_FEATURE_SHADOWPASSWDS
|
---|
| 218 | filename = bb_path_shadow_file;
|
---|
[2725] | 219 | rc = update_passwd(bb_path_shadow_file, name, newp, NULL);
|
---|
[3232] | 220 | if (rc > 0)
|
---|
| 221 | /* password in /etc/shadow was updated */
|
---|
| 222 | newp = (char*) "x";
|
---|
| 223 | if (rc >= 0)
|
---|
| 224 | /* 0 = /etc/shadow missing (not an error), >0 = passwd changed in /etc/shadow */
|
---|
[1765] | 225 | #endif
|
---|
[821] | 226 | {
|
---|
[1765] | 227 | filename = bb_path_passwd_file;
|
---|
[2725] | 228 | rc = update_passwd(bb_path_passwd_file, name, newp, NULL);
|
---|
[821] | 229 | }
|
---|
[1765] | 230 | /* LOGMODE_BOTH */
|
---|
| 231 | if (rc < 0)
|
---|
[3232] | 232 | bb_error_msg_and_die("can't update password file %s", filename);
|
---|
[3621] | 233 | bb_error_msg("password for %s changed by %s", name, myname);
|
---|
[821] | 234 |
|
---|
[3232] | 235 | /*if (ENABLE_FEATURE_CLEAN_UP) free(newp); - can't, it may be non-malloced */
|
---|
[1765] | 236 | skip:
|
---|
| 237 | if (!newp) {
|
---|
| 238 | bb_error_msg_and_die("password for %s is already %slocked",
|
---|
| 239 | name, (opt & OPT_unlock) ? "un" : "");
|
---|
[821] | 240 | }
|
---|
[3232] | 241 |
|
---|
| 242 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
| 243 | free(myname);
|
---|
[821] | 244 | return 0;
|
---|
| 245 | }
|
---|