blob: 4f7094aecb30ac4e6b2aa36ce8bde6e0c612970c [file] [log] [blame]
Eric Andersen27f64e12002-06-23 04:24:25 +00001/* vi: set sw=4 ts=4: */
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00002/*
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4 */
5
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00006#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +00007#include <syslog.h>
Eric Andersen27f64e12002-06-23 04:24:25 +00008
Eric Andersen27f64e12002-06-23 04:24:25 +00009
Denis Vlasenkoab24e182006-11-30 16:41:15 +000010static void nuke_str(char *str)
Eric Andersen27f64e12002-06-23 04:24:25 +000011{
Denis Vlasenkoab24e182006-11-30 16:41:15 +000012 if (str) memset(str, 0, strlen(str));
Eric Andersen27f64e12002-06-23 04:24:25 +000013}
14
Denis Vlasenkobecd8c52006-12-01 21:34:20 +000015static char* new_password(const struct passwd *pw, uid_t myuid, int algo)
Eric Andersen27f64e12002-06-23 04:24:25 +000016{
Denis Vlasenkoab24e182006-11-30 16:41:15 +000017 char salt[sizeof("$N$XXXXXXXX")]; /* "$N$XXXXXXXX" or "XX" */
Denis Vlasenko06c0a712007-01-29 22:51:44 +000018 char *orig = (char*)"";
Denis Vlasenkoab24e182006-11-30 16:41:15 +000019 char *newp = NULL;
20 char *cipher = NULL;
21 char *cp = NULL;
22 char *ret = NULL; /* failure so far */
Eric Andersen27f64e12002-06-23 04:24:25 +000023
Denis Vlasenkobecd8c52006-12-01 21:34:20 +000024 if (myuid && pw->pw_passwd[0]) {
Denis Vlasenkoab24e182006-11-30 16:41:15 +000025 orig = bb_askpass(0, "Old password:"); /* returns ptr to static */
26 if (!orig)
27 goto err_ret;
Denis Vlasenkobecd8c52006-12-01 21:34:20 +000028 cipher = pw_encrypt(orig, pw->pw_passwd); /* returns ptr to static */
29 if (strcmp(cipher, pw->pw_passwd) != 0) {
Denis Vlasenkod78b4332006-09-23 12:30:03 +000030 syslog(LOG_WARNING, "incorrect password for '%s'",
Denis Vlasenkoab24e182006-11-30 16:41:15 +000031 pw->pw_name);
Rob Landley84cb7672006-01-06 20:59:09 +000032 bb_do_delay(FAIL_DELAY);
Denis Vlasenkod78b4332006-09-23 12:30:03 +000033 puts("Incorrect password");
Denis Vlasenkoab24e182006-11-30 16:41:15 +000034 goto err_ret;
Eric Andersen27f64e12002-06-23 04:24:25 +000035 }
Eric Andersen27f64e12002-06-23 04:24:25 +000036 }
Denis Vlasenkoab24e182006-11-30 16:41:15 +000037 orig = xstrdup(orig); /* or else bb_askpass() will destroy it */
Denis Vlasenkobecd8c52006-12-01 21:34:20 +000038 newp = bb_askpass(0, "New password:"); /* returns ptr to static */
Denis Vlasenkoab24e182006-11-30 16:41:15 +000039 if (!newp)
40 goto err_ret;
41 newp = xstrdup(newp); /* we are going to bb_askpass() again, so save it */
Denis Vlasenko8eb3b392006-12-19 00:33:53 +000042 if (ENABLE_FEATURE_PASSWD_WEAK_CHECK
43 && obscure(orig, newp, pw) && myuid)
Denis Vlasenko16c2c702006-12-12 18:11:58 +000044 goto err_ret; /* non-root is not allowed to have weak passwd */
Eric Andersen27f64e12002-06-23 04:24:25 +000045
Denis Vlasenkobecd8c52006-12-01 21:34:20 +000046 cp = bb_askpass(0, "Retype password:");
Denis Vlasenkoab24e182006-11-30 16:41:15 +000047 if (!cp)
48 goto err_ret;
49 if (strcmp(cp, newp)) {
Denis Vlasenkobecd8c52006-12-01 21:34:20 +000050 puts("Passwords don't match");
Denis Vlasenkoab24e182006-11-30 16:41:15 +000051 goto err_ret;
52 }
53
Denis Vlasenko21d10142007-07-20 21:28:41 +000054 crypt_make_salt(salt, 1, 0); /* des */
Denis Vlasenko00252642006-11-30 20:41:28 +000055 if (algo) { /* MD5 */
Ned Ludd79197642006-04-21 00:40:35 +000056 strcpy(salt, "$1$");
Denis Vlasenko21d10142007-07-20 21:28:41 +000057 crypt_make_salt(salt + 3, 4, 0);
Ned Ludd79197642006-04-21 00:40:35 +000058 }
Denis Vlasenko3734b942007-07-27 11:20:10 +000059 /* pw_encrypt returns ptr to static */
60 ret = xstrdup(pw_encrypt(newp, salt));
Denis Vlasenkoab24e182006-11-30 16:41:15 +000061 /* whee, success! */
Ned Ludd79197642006-04-21 00:40:35 +000062
Denis Vlasenkoab24e182006-11-30 16:41:15 +000063 err_ret:
64 nuke_str(orig);
65 if (ENABLE_FEATURE_CLEAN_UP) free(orig);
66 nuke_str(newp);
67 if (ENABLE_FEATURE_CLEAN_UP) free(newp);
68 nuke_str(cipher);
69 nuke_str(cp);
70 return ret;
Eric Andersen27f64e12002-06-23 04:24:25 +000071}
72
Denis Vlasenko06af2162007-02-03 17:28:39 +000073int passwd_main(int argc, char **argv);
Denis Vlasenkoab24e182006-11-30 16:41:15 +000074int passwd_main(int argc, char **argv)
75{
76 enum {
77 OPT_algo = 0x1, /* -a - password algorithm */
78 OPT_lock = 0x2, /* -l - lock account */
79 OPT_unlock = 0x4, /* -u - unlock account */
80 OPT_delete = 0x8, /* -d - delete password */
81 OPT_lud = 0xe,
82 STATE_ALGO_md5 = 0x10,
Denis Vlasenko3734b942007-07-27 11:20:10 +000083 //STATE_ALGO_des = 0x20, not needed yet
Denis Vlasenkoab24e182006-11-30 16:41:15 +000084 };
85 unsigned opt;
Denis Vlasenko21d10142007-07-20 21:28:41 +000086 int rc;
Denis Vlasenko06c0a712007-01-29 22:51:44 +000087 const char *opt_a = "";
Denis Vlasenkoab24e182006-11-30 16:41:15 +000088 const char *filename;
89 char *myname;
90 char *name;
Denis Vlasenkobecd8c52006-12-01 21:34:20 +000091 char *newp;
92 struct passwd *pw;
Denis Vlasenkoab24e182006-11-30 16:41:15 +000093 uid_t myuid;
Denis Vlasenkobecd8c52006-12-01 21:34:20 +000094 struct rlimit rlimit_fsize;
95 char c;
Denis Vlasenkoab24e182006-11-30 16:41:15 +000096
Denis Vlasenko5df955f2007-03-13 13:01:14 +000097#if ENABLE_FEATURE_SHADOWPASSWDS
98 /* Using _r function to avoid pulling in static buffers */
99 struct spwd spw;
100 struct spwd *result;
101 char buffer[256];
102#endif
103
Denis Vlasenkobecd8c52006-12-01 21:34:20 +0000104 logmode = LOGMODE_BOTH;
105 openlog(applet_name, LOG_NOWAIT, LOG_AUTH);
Denis Vlasenkoab24e182006-11-30 16:41:15 +0000106 opt = getopt32(argc, argv, "a:lud", &opt_a);
Denis Vlasenko3734b942007-07-27 11:20:10 +0000107 //argc -= optind;
Denis Vlasenkoab24e182006-11-30 16:41:15 +0000108 argv += optind;
109
Denis Vlasenko4c87d4f2006-11-30 23:13:59 +0000110 if (strcasecmp(opt_a, "des") != 0) /* -a */
Denis Vlasenkoab24e182006-11-30 16:41:15 +0000111 opt |= STATE_ALGO_md5;
Denis Vlasenko4c87d4f2006-11-30 23:13:59 +0000112 //else
113 // opt |= STATE_ALGO_des;
Denis Vlasenkoab24e182006-11-30 16:41:15 +0000114 myuid = getuid();
Denis Vlasenko3734b942007-07-27 11:20:10 +0000115 /* -l, -u, -d require root priv and username argument */
116 if ((opt & OPT_lud) && (myuid || !argv[0]))
Denis Vlasenkoab24e182006-11-30 16:41:15 +0000117 bb_show_usage();
118
Denis Vlasenko3734b942007-07-27 11:20:10 +0000119 /* Will complain and die if username not found */
120 myname = xstrdup(bb_getpwuid(NULL, -1, myuid));
121 name = argv[0] ? argv[0] : myname;
Denis Vlasenkoab24e182006-11-30 16:41:15 +0000122
123 pw = getpwnam(name);
124 if (!pw) bb_error_msg_and_die("unknown user %s", name);
125 if (myuid && pw->pw_uid != myuid) {
Denis Vlasenkobecd8c52006-12-01 21:34:20 +0000126 /* LOGMODE_BOTH */
127 bb_error_msg_and_die("%s can't change password for %s", myname, name);
Denis Vlasenkoab24e182006-11-30 16:41:15 +0000128 }
129
Denis Vlasenkob5a122b2006-12-30 14:46:51 +0000130#if ENABLE_FEATURE_SHADOWPASSWDS
Denis Vlasenko1d10aaf2007-07-27 11:22:34 +0000131 /* getspnam_r() can lie! Even if user isn't in shadow, it can
132 * return success (pwd field was seen set to "!" in this case) */
133 if (getspnam_r(pw->pw_name, &spw, buffer, sizeof(buffer), &result)
134 || LONE_CHAR(spw.sp_pwdp, '!')) {
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000135 /* LOGMODE_BOTH */
136 bb_error_msg("no record of %s in %s, using %s",
137 name, bb_path_shadow_file,
138 bb_path_passwd_file);
139 } else {
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000140 pw->pw_passwd = spw.sp_pwdp;
Denis Vlasenkoab24e182006-11-30 16:41:15 +0000141 }
Denis Vlasenkob5a122b2006-12-30 14:46:51 +0000142#endif
Denis Vlasenkoab24e182006-11-30 16:41:15 +0000143
144 /* Decide what the new password will be */
Denis Vlasenkobecd8c52006-12-01 21:34:20 +0000145 newp = NULL;
146 c = pw->pw_passwd[0] - '!';
Denis Vlasenkoab24e182006-11-30 16:41:15 +0000147 if (!(opt & OPT_lud)) {
Denis Vlasenkobecd8c52006-12-01 21:34:20 +0000148 if (myuid && !c) { /* passwd starts with '!' */
149 /* LOGMODE_BOTH */
150 bb_error_msg_and_die("cannot change "
151 "locked password for %s", name);
Denis Vlasenkoab24e182006-11-30 16:41:15 +0000152 }
153 printf("Changing password for %s\n", name);
Denis Vlasenkobecd8c52006-12-01 21:34:20 +0000154 newp = new_password(pw, myuid, opt & STATE_ALGO_md5);
Denis Vlasenkoab24e182006-11-30 16:41:15 +0000155 if (!newp) {
Denis Vlasenkobecd8c52006-12-01 21:34:20 +0000156 logmode = LOGMODE_STDIO;
157 bb_error_msg_and_die("password for %s is unchanged", name);
Denis Vlasenkoab24e182006-11-30 16:41:15 +0000158 }
159 } else if (opt & OPT_lock) {
Denis Vlasenkobecd8c52006-12-01 21:34:20 +0000160 if (!c) goto skip; /* passwd starts with '!' */
161 newp = xasprintf("!%s", pw->pw_passwd);
Denis Vlasenkoab24e182006-11-30 16:41:15 +0000162 } else if (opt & OPT_unlock) {
Denis Vlasenkobecd8c52006-12-01 21:34:20 +0000163 if (c) goto skip; /* not '!' */
Denis Vlasenko3734b942007-07-27 11:20:10 +0000164 /* pw->pw_passwd pints to static storage,
165 * strdup'ing to avoid nasty surprizes */
Denis Vlasenkobecd8c52006-12-01 21:34:20 +0000166 newp = xstrdup(&pw->pw_passwd[1]);
Denis Vlasenkoab24e182006-11-30 16:41:15 +0000167 } else if (opt & OPT_delete) {
Denis Vlasenko3734b942007-07-27 11:20:10 +0000168 //newp = xstrdup("");
169 newp = (char*)"";
Denis Vlasenkoab24e182006-11-30 16:41:15 +0000170 }
171
Denis Vlasenkobecd8c52006-12-01 21:34:20 +0000172 rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * 30000;
173 setrlimit(RLIMIT_FSIZE, &rlimit_fsize);
Denis Vlasenkoab24e182006-11-30 16:41:15 +0000174 signal(SIGHUP, SIG_IGN);
175 signal(SIGINT, SIG_IGN);
176 signal(SIGQUIT, SIG_IGN);
177 umask(077);
178 xsetuid(0);
Denis Vlasenko1d10aaf2007-07-27 11:22:34 +0000179
180#if ENABLE_FEATURE_SHADOWPASSWDS
181 filename = bb_path_shadow_file;
182 rc = update_passwd(bb_path_shadow_file, name, newp);
183 if (rc == 0) /* no lines updated, no errors detected */
184#endif
185 {
186 filename = bb_path_passwd_file;
187 rc = update_passwd(bb_path_passwd_file, name, newp);
188 }
189 /* LOGMODE_BOTH */
Denis Vlasenko21d10142007-07-20 21:28:41 +0000190 if (rc < 0)
Denis Vlasenkobecd8c52006-12-01 21:34:20 +0000191 bb_error_msg_and_die("cannot update password file %s",
192 filename);
Denis Vlasenkobecd8c52006-12-01 21:34:20 +0000193 bb_info_msg("Password for %s changed by %s", name, myname);
Denis Vlasenkoab24e182006-11-30 16:41:15 +0000194
Denis Vlasenko3734b942007-07-27 11:20:10 +0000195 //if (ENABLE_FEATURE_CLEAN_UP) free(newp);
196 skip:
Denis Vlasenkobecd8c52006-12-01 21:34:20 +0000197 if (!newp) {
198 bb_error_msg_and_die("password for %s is already %slocked",
199 name, (opt & OPT_unlock) ? "un" : "");
200 }
Denis Vlasenkoab24e182006-11-30 16:41:15 +0000201 if (ENABLE_FEATURE_CLEAN_UP) free(myname);
202 return 0;
203}