2002-06-23 09:54:25 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-07-12 13:26:04 +05:30
|
|
|
/*
|
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2006-08-03 21:11:12 +05:30
|
|
|
#include <syslog.h>
|
2002-06-23 09:54:25 +05:30
|
|
|
|
2006-11-30 22:11:15 +05:30
|
|
|
static void nuke_str(char *str)
|
|
|
|
{
|
|
|
|
if (str) memset(str, 0, strlen(str));
|
|
|
|
}
|
2002-06-23 09:54:25 +05:30
|
|
|
|
2006-12-02 03:04:20 +05:30
|
|
|
static char* new_password(const struct passwd *pw, uid_t myuid, int algo)
|
2006-11-30 22:11:15 +05:30
|
|
|
{
|
|
|
|
char salt[sizeof("$N$XXXXXXXX")]; /* "$N$XXXXXXXX" or "XX" */
|
2007-01-30 04:21:44 +05:30
|
|
|
char *orig = (char*)"";
|
2006-11-30 22:11:15 +05:30
|
|
|
char *newp = NULL;
|
|
|
|
char *cp = NULL;
|
|
|
|
char *ret = NULL; /* failure so far */
|
|
|
|
|
2006-12-02 03:04:20 +05:30
|
|
|
if (myuid && pw->pw_passwd[0]) {
|
2008-06-12 22:26:52 +05:30
|
|
|
char *encrypted;
|
|
|
|
|
2008-12-04 00:18:39 +05:30
|
|
|
orig = bb_ask_stdin("Old password:"); /* returns ptr to static */
|
2006-11-30 22:11:15 +05:30
|
|
|
if (!orig)
|
|
|
|
goto err_ret;
|
2008-06-12 22:26:52 +05:30
|
|
|
encrypted = pw_encrypt(orig, pw->pw_passwd, 1); /* returns malloced str */
|
|
|
|
if (strcmp(encrypted, pw->pw_passwd) != 0) {
|
2008-07-21 20:11:33 +05:30
|
|
|
syslog(LOG_WARNING, "incorrect password for %s",
|
2006-11-30 22:11:15 +05:30
|
|
|
pw->pw_name);
|
|
|
|
bb_do_delay(FAIL_DELAY);
|
|
|
|
puts("Incorrect password");
|
|
|
|
goto err_ret;
|
|
|
|
}
|
2008-06-12 22:26:52 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) free(encrypted);
|
2006-11-30 22:11:15 +05:30
|
|
|
}
|
2008-12-04 00:18:39 +05:30
|
|
|
orig = xstrdup(orig); /* or else bb_ask_stdin() will destroy it */
|
|
|
|
newp = bb_ask_stdin("New password:"); /* returns ptr to static */
|
2006-11-30 22:11:15 +05:30
|
|
|
if (!newp)
|
|
|
|
goto err_ret;
|
2008-12-04 00:18:39 +05:30
|
|
|
newp = xstrdup(newp); /* we are going to bb_ask_stdin() again, so save it */
|
2006-12-19 06:03:53 +05:30
|
|
|
if (ENABLE_FEATURE_PASSWD_WEAK_CHECK
|
|
|
|
&& obscure(orig, newp, pw) && myuid)
|
2006-12-12 23:41:58 +05:30
|
|
|
goto err_ret; /* non-root is not allowed to have weak passwd */
|
2006-11-30 22:11:15 +05:30
|
|
|
|
2008-12-04 00:18:39 +05:30
|
|
|
cp = bb_ask_stdin("Retype password:");
|
2006-11-30 22:11:15 +05:30
|
|
|
if (!cp)
|
|
|
|
goto err_ret;
|
|
|
|
if (strcmp(cp, newp)) {
|
2006-12-02 03:04:20 +05:30
|
|
|
puts("Passwords don't match");
|
2006-11-30 22:11:15 +05:30
|
|
|
goto err_ret;
|
|
|
|
}
|
|
|
|
|
2007-07-21 02:58:41 +05:30
|
|
|
crypt_make_salt(salt, 1, 0); /* des */
|
2006-12-01 02:11:28 +05:30
|
|
|
if (algo) { /* MD5 */
|
2006-11-30 22:11:15 +05:30
|
|
|
strcpy(salt, "$1$");
|
2007-07-21 02:58:41 +05:30
|
|
|
crypt_make_salt(salt + 3, 4, 0);
|
2006-11-30 22:11:15 +05:30
|
|
|
}
|
2008-06-12 22:26:52 +05:30
|
|
|
/* pw_encrypt returns malloced str */
|
|
|
|
ret = pw_encrypt(newp, salt, 1);
|
2006-11-30 22:11:15 +05:30
|
|
|
/* whee, success! */
|
|
|
|
|
|
|
|
err_ret:
|
|
|
|
nuke_str(orig);
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) free(orig);
|
|
|
|
nuke_str(newp);
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) free(newp);
|
|
|
|
nuke_str(cp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int passwd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int passwd_main(int argc UNUSED_PARAM, char **argv)
|
2002-06-23 09:54:25 +05:30
|
|
|
{
|
2006-09-23 18:00:03 +05:30
|
|
|
enum {
|
|
|
|
OPT_algo = 0x1, /* -a - password algorithm */
|
|
|
|
OPT_lock = 0x2, /* -l - lock account */
|
|
|
|
OPT_unlock = 0x4, /* -u - unlock account */
|
|
|
|
OPT_delete = 0x8, /* -d - delete password */
|
|
|
|
OPT_lud = 0xe,
|
2006-11-30 22:11:15 +05:30
|
|
|
STATE_ALGO_md5 = 0x10,
|
2007-07-27 16:50:10 +05:30
|
|
|
//STATE_ALGO_des = 0x20, not needed yet
|
2006-09-23 18:00:03 +05:30
|
|
|
};
|
2006-10-04 02:30:06 +05:30
|
|
|
unsigned opt;
|
2007-07-21 02:58:41 +05:30
|
|
|
int rc;
|
2007-01-30 04:21:44 +05:30
|
|
|
const char *opt_a = "";
|
2006-11-30 22:11:15 +05:30
|
|
|
const char *filename;
|
2002-06-23 09:54:25 +05:30
|
|
|
char *myname;
|
2006-11-30 22:11:15 +05:30
|
|
|
char *name;
|
2006-12-02 03:04:20 +05:30
|
|
|
char *newp;
|
|
|
|
struct passwd *pw;
|
2006-11-30 22:11:15 +05:30
|
|
|
uid_t myuid;
|
2006-12-02 03:04:20 +05:30
|
|
|
struct rlimit rlimit_fsize;
|
|
|
|
char c;
|
2007-03-13 18:31:14 +05:30
|
|
|
#if ENABLE_FEATURE_SHADOWPASSWDS
|
|
|
|
/* Using _r function to avoid pulling in static buffers */
|
|
|
|
struct spwd spw;
|
|
|
|
char buffer[256];
|
|
|
|
#endif
|
|
|
|
|
2006-12-02 03:04:20 +05:30
|
|
|
logmode = LOGMODE_BOTH;
|
|
|
|
openlog(applet_name, LOG_NOWAIT, LOG_AUTH);
|
2007-08-18 21:02:12 +05:30
|
|
|
opt = getopt32(argv, "a:lud", &opt_a);
|
2007-07-27 16:50:10 +05:30
|
|
|
//argc -= optind;
|
2006-09-23 18:00:03 +05:30
|
|
|
argv += optind;
|
2006-11-30 22:11:15 +05:30
|
|
|
|
2006-12-01 04:43:59 +05:30
|
|
|
if (strcasecmp(opt_a, "des") != 0) /* -a */
|
2006-11-30 22:11:15 +05:30
|
|
|
opt |= STATE_ALGO_md5;
|
2006-12-01 04:43:59 +05:30
|
|
|
//else
|
|
|
|
// opt |= STATE_ALGO_des;
|
2006-11-30 22:11:15 +05:30
|
|
|
myuid = getuid();
|
2007-07-27 16:50:10 +05:30
|
|
|
/* -l, -u, -d require root priv and username argument */
|
|
|
|
if ((opt & OPT_lud) && (myuid || !argv[0]))
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_show_usage();
|
2006-09-23 18:00:03 +05:30
|
|
|
|
2007-07-27 16:50:10 +05:30
|
|
|
/* Will complain and die if username not found */
|
2008-12-03 04:26:59 +05:30
|
|
|
myname = xstrdup(xuid2uname(myuid));
|
2007-07-27 16:50:10 +05:30
|
|
|
name = argv[0] ? argv[0] : myname;
|
2006-09-23 18:00:03 +05:30
|
|
|
|
2008-12-04 00:35:55 +05:30
|
|
|
pw = xgetpwnam(name);
|
2006-11-30 22:11:15 +05:30
|
|
|
if (myuid && pw->pw_uid != myuid) {
|
2006-12-02 03:04:20 +05:30
|
|
|
/* LOGMODE_BOTH */
|
|
|
|
bb_error_msg_and_die("%s can't change password for %s", myname, name);
|
2002-06-23 09:54:25 +05:30
|
|
|
}
|
2006-11-30 22:11:15 +05:30
|
|
|
|
2006-12-30 20:16:51 +05:30
|
|
|
#if ENABLE_FEATURE_SHADOWPASSWDS
|
2007-10-30 00:55:45 +05:30
|
|
|
{
|
|
|
|
/* getspnam_r may return 0 yet set result to NULL.
|
|
|
|
* At least glibc 2.4 does this. Be extra paranoid here. */
|
|
|
|
struct spwd *result = NULL;
|
|
|
|
if (getspnam_r(pw->pw_name, &spw, buffer, sizeof(buffer), &result)
|
|
|
|
|| !result || strcmp(result->sp_namp, pw->pw_name) != 0) {
|
|
|
|
/* LOGMODE_BOTH */
|
|
|
|
bb_error_msg("no record of %s in %s, using %s",
|
|
|
|
name, bb_path_shadow_file,
|
|
|
|
bb_path_passwd_file);
|
|
|
|
} else {
|
|
|
|
pw->pw_passwd = result->sp_pwdp;
|
|
|
|
}
|
2006-11-30 22:11:15 +05:30
|
|
|
}
|
2006-12-30 20:16:51 +05:30
|
|
|
#endif
|
2002-06-23 09:54:25 +05:30
|
|
|
|
2006-11-30 22:11:15 +05:30
|
|
|
/* Decide what the new password will be */
|
2006-12-02 03:04:20 +05:30
|
|
|
newp = NULL;
|
|
|
|
c = pw->pw_passwd[0] - '!';
|
2006-09-23 18:00:03 +05:30
|
|
|
if (!(opt & OPT_lud)) {
|
2006-12-02 03:04:20 +05:30
|
|
|
if (myuid && !c) { /* passwd starts with '!' */
|
|
|
|
/* LOGMODE_BOTH */
|
|
|
|
bb_error_msg_and_die("cannot change "
|
|
|
|
"locked password for %s", name);
|
2002-06-23 09:54:25 +05:30
|
|
|
}
|
|
|
|
printf("Changing password for %s\n", name);
|
2006-12-02 03:04:20 +05:30
|
|
|
newp = new_password(pw, myuid, opt & STATE_ALGO_md5);
|
2006-11-30 22:11:15 +05:30
|
|
|
if (!newp) {
|
2006-12-02 03:04:20 +05:30
|
|
|
logmode = LOGMODE_STDIO;
|
|
|
|
bb_error_msg_and_die("password for %s is unchanged", name);
|
2002-06-23 09:54:25 +05:30
|
|
|
}
|
2006-09-23 18:00:03 +05:30
|
|
|
} else if (opt & OPT_lock) {
|
2006-12-02 03:04:20 +05:30
|
|
|
if (!c) goto skip; /* passwd starts with '!' */
|
|
|
|
newp = xasprintf("!%s", pw->pw_passwd);
|
2006-09-23 18:00:03 +05:30
|
|
|
} else if (opt & OPT_unlock) {
|
2006-12-02 03:04:20 +05:30
|
|
|
if (c) goto skip; /* not '!' */
|
2007-10-30 00:55:45 +05:30
|
|
|
/* pw->pw_passwd points to static storage,
|
2007-07-27 16:50:10 +05:30
|
|
|
* strdup'ing to avoid nasty surprizes */
|
2006-12-02 03:04:20 +05:30
|
|
|
newp = xstrdup(&pw->pw_passwd[1]);
|
2006-09-23 18:00:03 +05:30
|
|
|
} else if (opt & OPT_delete) {
|
2007-07-27 16:50:10 +05:30
|
|
|
//newp = xstrdup("");
|
|
|
|
newp = (char*)"";
|
2002-06-23 09:54:25 +05:30
|
|
|
}
|
2006-11-30 22:11:15 +05:30
|
|
|
|
2006-12-02 03:04:20 +05:30
|
|
|
rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * 30000;
|
|
|
|
setrlimit(RLIMIT_FSIZE, &rlimit_fsize);
|
2008-02-17 04:28:56 +05:30
|
|
|
bb_signals(0
|
|
|
|
+ (1 << SIGHUP)
|
|
|
|
+ (1 << SIGINT)
|
|
|
|
+ (1 << SIGQUIT)
|
|
|
|
, SIG_IGN);
|
2002-06-23 09:54:25 +05:30
|
|
|
umask(077);
|
2006-07-16 13:36:34 +05:30
|
|
|
xsetuid(0);
|
2007-07-27 16:52:34 +05:30
|
|
|
|
|
|
|
#if ENABLE_FEATURE_SHADOWPASSWDS
|
|
|
|
filename = bb_path_shadow_file;
|
|
|
|
rc = update_passwd(bb_path_shadow_file, name, newp);
|
|
|
|
if (rc == 0) /* no lines updated, no errors detected */
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
filename = bb_path_passwd_file;
|
|
|
|
rc = update_passwd(bb_path_passwd_file, name, newp);
|
|
|
|
}
|
|
|
|
/* LOGMODE_BOTH */
|
2007-07-21 02:58:41 +05:30
|
|
|
if (rc < 0)
|
2006-12-02 03:04:20 +05:30
|
|
|
bb_error_msg_and_die("cannot update password file %s",
|
|
|
|
filename);
|
|
|
|
bb_info_msg("Password for %s changed by %s", name, myname);
|
2002-06-23 09:54:25 +05:30
|
|
|
|
2007-07-27 16:50:10 +05:30
|
|
|
//if (ENABLE_FEATURE_CLEAN_UP) free(newp);
|
|
|
|
skip:
|
2006-12-02 03:04:20 +05:30
|
|
|
if (!newp) {
|
|
|
|
bb_error_msg_and_die("password for %s is already %slocked",
|
|
|
|
name, (opt & OPT_unlock) ? "un" : "");
|
|
|
|
}
|
2006-11-30 22:11:15 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) free(myname);
|
2002-06-23 09:54:25 +05:30
|
|
|
return 0;
|
|
|
|
}
|