Annotation of src/usr.bin/passwd/local_passwd.c, Revision 1.53
1.53 ! millert 1: /* $OpenBSD: local_passwd.c,v 1.52 2016/09/02 18:06:43 tedu Exp $ */
1.3 deraadt 2:
1.1 deraadt 3: /*-
4: * Copyright (c) 1990 The Regents of the University of California.
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
1.29 millert 15: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 16: * may be used to endorse or promote products derived from this software
17: * without specific prior written permission.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: */
31:
32: #include <sys/types.h>
1.2 deraadt 33: #include <sys/stat.h>
1.21 millert 34: #include <sys/uio.h>
35:
1.7 weingart 36: #include <err.h>
1.1 deraadt 37: #include <errno.h>
1.21 millert 38: #include <fcntl.h>
39: #include <pwd.h>
1.1 deraadt 40: #include <stdio.h>
1.28 deraadt 41: #include <stdlib.h>
1.21 millert 42: #include <signal.h>
1.1 deraadt 43: #include <string.h>
1.2 deraadt 44: #include <unistd.h>
45: #include <util.h>
1.14 millert 46: #include <login_cap.h>
1.50 tedu 47: #include <readpassphrase.h>
1.1 deraadt 48:
1.49 tedu 49: #define UNCHANGED_MSG "Password unchanged."
1.27 millert 50:
1.7 weingart 51: static uid_t uid;
1.33 millert 52: extern int pwd_check(login_cap_t *, char *);
53: extern int pwd_gettries(login_cap_t *);
1.14 millert 54:
1.39 djm 55: int local_passwd(char *, int);
1.25 millert 56: char *getnewpasswd(struct passwd *, login_cap_t *, int);
57: void kbintr(int);
1.1 deraadt 58:
1.7 weingart 59: int
1.26 deraadt 60: local_passwd(char *uname, int authenticated)
1.1 deraadt 61: {
1.32 millert 62: struct passwd *pw, *opw;
1.14 millert 63: login_cap_t *lc;
1.15 millert 64: sigset_t fullset;
65: time_t period;
1.21 millert 66: int i, pfd, tfd = -1;
1.20 millert 67: int pwflags = _PASSWORD_OMITV7;
1.1 deraadt 68:
1.47 tim 69: if (!(pw = getpwnam_shadow(uname))) {
1.7 weingart 70: warnx("unknown user %s.", uname);
1.1 deraadt 71: return(1);
72: }
1.46 deraadt 73:
74: if (pledge("stdio rpath wpath cpath getpw tty id proc exec", NULL) == -1)
75: err(1, "pledge");
76:
1.32 millert 77: if ((opw = pw_dup(pw)) == NULL) {
78: warn(NULL);
79: return(1);
80: }
1.14 millert 81: if ((lc = login_getclass(pw->pw_class)) == NULL) {
82: warnx("unable to get login class for user %s.", uname);
1.36 moritz 83: free(opw);
1.14 millert 84: return(1);
85: }
1.1 deraadt 86:
1.13 millert 87: uid = authenticated ? pw->pw_uid : getuid();
1.1 deraadt 88: if (uid && uid != pw->pw_uid) {
1.8 millert 89: warnx("login/uid mismatch, username argument required.");
1.36 moritz 90: free(opw);
1.1 deraadt 91: return(1);
92: }
93:
1.14 millert 94: /* Get the new password. */
95: pw->pw_passwd = getnewpasswd(pw, lc, authenticated);
96:
1.46 deraadt 97: if (pledge("stdio rpath wpath cpath getpw id proc exec", NULL) == -1)
98: err(1, "pledge");
99:
1.14 millert 100: /* Reset password change time based on login.conf. */
1.48 guenther 101: period = (time_t)login_getcaptime(lc, "passwordtime", 0, 0);
1.19 millert 102: if (period > 0) {
1.14 millert 103: pw->pw_change = time(NULL) + period;
1.19 millert 104: } else {
105: /*
106: * If the pw change time is the same we only need
107: * to update the spwd.db file.
108: */
109: if (pw->pw_change != 0)
110: pw->pw_change = 0;
111: else
1.20 millert 112: pwflags = _PASSWORD_SECUREONLY;
1.19 millert 113: }
1.14 millert 114:
1.15 millert 115: /* Drop user's real uid and block all signals to avoid a DoS. */
116: setuid(0);
117: sigfillset(&fullset);
118: sigdelset(&fullset, SIGINT);
119: sigprocmask(SIG_BLOCK, &fullset, NULL);
120:
1.46 deraadt 121: if (pledge("stdio rpath wpath cpath proc exec", NULL) == -1)
122: err(1, "pledge");
123:
1.15 millert 124: /* Get a lock on the passwd file and open it. */
125: pw_init();
1.21 millert 126: for (i = 1; (tfd = pw_lock(0)) == -1; i++) {
127: if (i == 4)
1.38 jsing 128: (void)fputs("Attempting to lock password file, "
1.21 millert 129: "please wait or press ^C to abort", stderr);
130: (void)signal(SIGINT, kbintr);
131: if (i % 16 == 0)
1.15 millert 132: fputc('.', stderr);
1.21 millert 133: usleep(250000);
1.15 millert 134: (void)signal(SIGINT, SIG_IGN);
135: }
1.21 millert 136: if (i >= 4)
137: fputc('\n', stderr);
1.41 guenther 138: pfd = open(_PATH_MASTERPASSWD, O_RDONLY | O_CLOEXEC, 0);
139: if (pfd < 0)
1.15 millert 140: pw_error(_PATH_MASTERPASSWD, 1, 1);
141:
1.18 millert 142: /* Update master.passwd file and rebuild spwd.db. */
1.32 millert 143: pw_copy(pfd, tfd, pw, opw);
144: free(opw);
1.20 millert 145: if (pw_mkdb(uname, pwflags) < 0)
1.19 millert 146: pw_error(NULL, 0, 1);
1.14 millert 147:
1.1 deraadt 148: return(0);
149: }
150:
151: char *
1.26 deraadt 152: getnewpasswd(struct passwd *pw, login_cap_t *lc, int authenticated)
1.1 deraadt 153: {
1.43 tedu 154: static char hash[_PASSWORD_LEN];
1.52 tedu 155: char newpass[1024];
1.44 tedu 156: char *p, *pref;
1.11 provos 157: int tries, pwd_tries;
1.24 millert 158: sig_t saveint, savequit;
1.1 deraadt 159:
1.24 millert 160: saveint = signal(SIGINT, kbintr);
161: savequit = signal(SIGQUIT, kbintr);
1.23 millert 162:
1.13 millert 163: if (!authenticated) {
1.46 deraadt 164: (void)printf("Changing password for %s.\n", pw->pw_name);
1.27 millert 165: if (uid != 0 && pw->pw_passwd[0] != '\0') {
1.52 tedu 166: char oldpass[1024];
1.50 tedu 167:
168: p = readpassphrase("Old password:", oldpass,
169: sizeof(oldpass), RPP_ECHO_OFF);
1.31 millert 170: if (p == NULL || *p == '\0') {
1.49 tedu 171: (void)printf("%s\n", UNCHANGED_MSG);
1.27 millert 172: pw_abort();
1.31 millert 173: exit(p == NULL ? 1 : 0);
1.27 millert 174: }
1.42 tedu 175: if (crypt_checkpass(p, pw->pw_passwd) != 0) {
1.27 millert 176: errno = EACCES;
1.51 gsoares 177: explicit_bzero(oldpass, sizeof(oldpass));
1.27 millert 178: pw_error(NULL, 1, 1);
179: }
1.51 gsoares 180: explicit_bzero(oldpass, sizeof(oldpass));
1.13 millert 181: }
1.1 deraadt 182: }
1.26 deraadt 183:
1.33 millert 184: pwd_tries = pwd_gettries(lc);
1.1 deraadt 185:
1.50 tedu 186: for (newpass[0] = '\0', tries = 0;;) {
1.52 tedu 187: char repeat[1024];
1.50 tedu 188:
189: p = readpassphrase("New password:", newpass, sizeof(newpass),
190: RPP_ECHO_OFF);
1.31 millert 191: if (p == NULL || *p == '\0') {
1.49 tedu 192: (void)printf("%s\n", UNCHANGED_MSG);
1.27 millert 193: pw_abort();
1.31 millert 194: exit(p == NULL ? 1 : 0);
1.9 deraadt 195: }
196: if (strcmp(p, "s/key") == 0) {
197: printf("That password collides with a system feature. Choose another.\n");
198: continue;
1.1 deraadt 199: }
1.26 deraadt 200:
1.37 deraadt 201: if ((tries++ < pwd_tries || pwd_tries == 0) &&
202: pwd_check(lc, p) == 0)
1.1 deraadt 203: continue;
1.50 tedu 204: p = readpassphrase("Retype new password:", repeat, sizeof(repeat),
205: RPP_ECHO_OFF);
1.53 ! millert 206: if (p != NULL && strcmp(newpass, p) == 0) {
! 207: explicit_bzero(repeat, sizeof(repeat));
1.1 deraadt 208: break;
1.53 ! millert 209: }
1.1 deraadt 210: (void)printf("Mismatch; try again, EOF to quit.\n");
1.53 ! millert 211: explicit_bzero(repeat, sizeof(repeat));
1.51 gsoares 212: explicit_bzero(newpass, sizeof(newpass));
1.1 deraadt 213: }
1.43 tedu 214:
1.24 millert 215: (void)signal(SIGINT, saveint);
216: (void)signal(SIGQUIT, savequit);
1.23 millert 217:
1.44 tedu 218: pref = login_getcapstr(lc, "localcipher", NULL, NULL);
1.50 tedu 219: if (crypt_newhash(newpass, pref, hash, sizeof(hash)) != 0) {
1.43 tedu 220: (void)printf("Couldn't generate hash.\n");
1.51 gsoares 221: explicit_bzero(newpass, sizeof(newpass));
1.43 tedu 222: pw_error(NULL, 0, 0);
223: }
1.51 gsoares 224: explicit_bzero(newpass, sizeof(newpass));
1.44 tedu 225: free(pref);
1.43 tedu 226: return hash;
1.15 millert 227: }
228:
229: void
1.26 deraadt 230: kbintr(int signo)
1.15 millert 231: {
1.49 tedu 232: dprintf(STDOUT_FILENO, "\n%s\n", UNCHANGED_MSG);
1.27 millert 233: _exit(0);
1.1 deraadt 234: }