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