Annotation of src/usr.bin/passwd/local_passwd.c, Revision 1.45
1.45 ! ajacouto 1: /* $OpenBSD: local_passwd.c,v 1.44 2014/11/21 05:13:44 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.1 deraadt 47:
1.27 millert 48: #define UNCHANGED_MSG "Password unchanged.\n"
49:
1.7 weingart 50: static uid_t uid;
1.33 millert 51: extern int pwd_check(login_cap_t *, char *);
52: extern int pwd_gettries(login_cap_t *);
1.14 millert 53:
1.39 djm 54: int local_passwd(char *, int);
1.25 millert 55: char *getnewpasswd(struct passwd *, login_cap_t *, int);
56: void kbintr(int);
1.1 deraadt 57:
1.7 weingart 58: int
1.26 deraadt 59: local_passwd(char *uname, int authenticated)
1.1 deraadt 60: {
1.32 millert 61: struct passwd *pw, *opw;
1.14 millert 62: login_cap_t *lc;
1.15 millert 63: sigset_t fullset;
64: time_t period;
1.21 millert 65: int i, pfd, tfd = -1;
1.20 millert 66: int pwflags = _PASSWORD_OMITV7;
1.1 deraadt 67:
68: if (!(pw = getpwnam(uname))) {
69: #ifdef YP
70: extern int use_yp;
71: if (!use_yp)
72: #endif
1.7 weingart 73: warnx("unknown user %s.", uname);
1.1 deraadt 74: return(1);
75: }
1.32 millert 76: if ((opw = pw_dup(pw)) == NULL) {
77: warn(NULL);
78: return(1);
79: }
1.14 millert 80: if ((lc = login_getclass(pw->pw_class)) == NULL) {
81: warnx("unable to get login class for user %s.", uname);
1.36 moritz 82: free(opw);
1.14 millert 83: return(1);
84: }
1.1 deraadt 85:
1.13 millert 86: uid = authenticated ? pw->pw_uid : getuid();
1.1 deraadt 87: if (uid && uid != pw->pw_uid) {
1.8 millert 88: warnx("login/uid mismatch, username argument required.");
1.36 moritz 89: free(opw);
1.1 deraadt 90: return(1);
91: }
92:
1.14 millert 93: /* Get the new password. */
94: pw->pw_passwd = getnewpasswd(pw, lc, authenticated);
95:
96: /* Reset password change time based on login.conf. */
1.30 deraadt 97: period = (time_t)login_getcaptime(lc, "passwordtime",
98: (quad_t)0, (quad_t)0);
1.19 millert 99: if (period > 0) {
1.14 millert 100: pw->pw_change = time(NULL) + period;
1.19 millert 101: } else {
102: /*
103: * If the pw change time is the same we only need
104: * to update the spwd.db file.
105: */
106: if (pw->pw_change != 0)
107: pw->pw_change = 0;
108: else
1.20 millert 109: pwflags = _PASSWORD_SECUREONLY;
1.19 millert 110: }
1.14 millert 111:
1.15 millert 112: /* Drop user's real uid and block all signals to avoid a DoS. */
113: setuid(0);
114: sigfillset(&fullset);
115: sigdelset(&fullset, SIGINT);
116: sigprocmask(SIG_BLOCK, &fullset, NULL);
117:
118: /* Get a lock on the passwd file and open it. */
119: pw_init();
1.21 millert 120: for (i = 1; (tfd = pw_lock(0)) == -1; i++) {
121: if (i == 4)
1.38 jsing 122: (void)fputs("Attempting to lock password file, "
1.21 millert 123: "please wait or press ^C to abort", stderr);
124: (void)signal(SIGINT, kbintr);
125: if (i % 16 == 0)
1.15 millert 126: fputc('.', stderr);
1.21 millert 127: usleep(250000);
1.15 millert 128: (void)signal(SIGINT, SIG_IGN);
129: }
1.21 millert 130: if (i >= 4)
131: fputc('\n', stderr);
1.41 guenther 132: pfd = open(_PATH_MASTERPASSWD, O_RDONLY | O_CLOEXEC, 0);
133: if (pfd < 0)
1.15 millert 134: pw_error(_PATH_MASTERPASSWD, 1, 1);
135:
1.18 millert 136: /* Update master.passwd file and rebuild spwd.db. */
1.32 millert 137: pw_copy(pfd, tfd, pw, opw);
138: free(opw);
1.20 millert 139: if (pw_mkdb(uname, pwflags) < 0)
1.19 millert 140: pw_error(NULL, 0, 1);
1.14 millert 141:
1.1 deraadt 142: return(0);
143: }
144:
145: char *
1.26 deraadt 146: getnewpasswd(struct passwd *pw, login_cap_t *lc, int authenticated)
1.1 deraadt 147: {
1.43 tedu 148: static char hash[_PASSWORD_LEN];
1.44 tedu 149: char *p, *pref;
1.11 provos 150: int tries, pwd_tries;
1.43 tedu 151: char buf[1024];
1.24 millert 152: sig_t saveint, savequit;
1.1 deraadt 153:
1.24 millert 154: saveint = signal(SIGINT, kbintr);
155: savequit = signal(SIGQUIT, kbintr);
1.23 millert 156:
1.13 millert 157: if (!authenticated) {
158: (void)printf("Changing local password for %s.\n", pw->pw_name);
1.27 millert 159: if (uid != 0 && pw->pw_passwd[0] != '\0') {
160: p = getpass("Old password:");
1.31 millert 161: if (p == NULL || *p == '\0') {
1.27 millert 162: (void)printf(UNCHANGED_MSG);
163: pw_abort();
1.31 millert 164: exit(p == NULL ? 1 : 0);
1.27 millert 165: }
1.42 tedu 166: if (crypt_checkpass(p, pw->pw_passwd) != 0) {
1.27 millert 167: errno = EACCES;
168: pw_error(NULL, 1, 1);
169: }
1.13 millert 170: }
1.1 deraadt 171: }
1.26 deraadt 172:
1.33 millert 173: pwd_tries = pwd_gettries(lc);
1.1 deraadt 174:
175: for (buf[0] = '\0', tries = 0;;) {
176: p = getpass("New password:");
1.31 millert 177: if (p == NULL || *p == '\0') {
1.27 millert 178: (void)printf(UNCHANGED_MSG);
179: pw_abort();
1.31 millert 180: exit(p == NULL ? 1 : 0);
1.9 deraadt 181: }
182: if (strcmp(p, "s/key") == 0) {
183: printf("That password collides with a system feature. Choose another.\n");
184: continue;
1.1 deraadt 185: }
1.26 deraadt 186:
1.37 deraadt 187: if ((tries++ < pwd_tries || pwd_tries == 0) &&
188: pwd_check(lc, p) == 0)
1.1 deraadt 189: continue;
1.13 millert 190: strlcpy(buf, p, sizeof(buf));
1.31 millert 191: p = getpass("Retype new password:");
192: if (p != NULL && strcmp(buf, p) == 0)
1.1 deraadt 193: break;
194: (void)printf("Mismatch; try again, EOF to quit.\n");
195: }
1.43 tedu 196:
1.24 millert 197: (void)signal(SIGINT, saveint);
198: (void)signal(SIGQUIT, savequit);
1.23 millert 199:
1.44 tedu 200: pref = login_getcapstr(lc, "localcipher", NULL, NULL);
201: if (crypt_newhash(buf, pref, hash, sizeof(hash)) != 0) {
1.43 tedu 202: (void)printf("Couldn't generate hash.\n");
203: pw_error(NULL, 0, 0);
204: }
1.44 tedu 205: free(pref);
1.43 tedu 206: return hash;
1.15 millert 207: }
208:
1.35 deraadt 209: /* ARGSUSED */
1.15 millert 210: void
1.26 deraadt 211: kbintr(int signo)
1.15 millert 212: {
1.34 millert 213: write(STDOUT_FILENO, "\n", 1);
1.27 millert 214: write(STDOUT_FILENO, UNCHANGED_MSG, sizeof(UNCHANGED_MSG) - 1);
215: _exit(0);
1.1 deraadt 216: }