Annotation of src/usr.bin/passwd/local_passwd.c, Revision 1.26
1.26 ! deraadt 1: /* $OpenBSD: local_passwd.c,v 1.25 2002/02/16 21:27:50 millert 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.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by the University of
18: * California, Berkeley and its contributors.
19: * 4. Neither the name of the University nor the names of its contributors
20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
1.16 millert 37: /*static const char sccsid[] = "from: @(#)local_passwd.c 5.5 (Berkeley) 5/6/91";*/
1.26 ! deraadt 38: static const char rcsid[] = "$OpenBSD: local_passwd.c,v 1.25 2002/02/16 21:27:50 millert Exp $";
1.1 deraadt 39: #endif /* not lint */
40:
41: #include <sys/types.h>
1.2 deraadt 42: #include <sys/stat.h>
1.21 millert 43: #include <sys/uio.h>
44:
1.7 weingart 45: #include <err.h>
1.1 deraadt 46: #include <errno.h>
1.21 millert 47: #include <fcntl.h>
48: #include <pwd.h>
1.1 deraadt 49: #include <stdio.h>
1.21 millert 50: #include <signal.h>
1.1 deraadt 51: #include <string.h>
1.2 deraadt 52: #include <unistd.h>
53: #include <util.h>
1.14 millert 54: #include <login_cap.h>
1.1 deraadt 55:
1.7 weingart 56: static uid_t uid;
1.25 millert 57: extern int pwd_gensalt(char *, int, struct passwd *, login_cap_t *, char);
58: extern int pwd_check(struct passwd *, login_cap_t *, char *);
59: extern int pwd_gettries(struct passwd *, login_cap_t *);
1.14 millert 60:
1.25 millert 61: char *getnewpasswd(struct passwd *, login_cap_t *, int);
62: void kbintr(int);
1.1 deraadt 63:
1.7 weingart 64: int
1.26 ! deraadt 65: local_passwd(char *uname, int authenticated)
1.1 deraadt 66: {
67: struct passwd *pw;
1.14 millert 68: login_cap_t *lc;
1.15 millert 69: sigset_t fullset;
70: time_t period;
1.21 millert 71: int i, pfd, tfd = -1;
1.20 millert 72: int pwflags = _PASSWORD_OMITV7;
1.1 deraadt 73:
74: if (!(pw = getpwnam(uname))) {
75: #ifdef YP
76: extern int use_yp;
77: if (!use_yp)
78: #endif
1.7 weingart 79: warnx("unknown user %s.", uname);
1.1 deraadt 80: return(1);
81: }
1.14 millert 82: if ((lc = login_getclass(pw->pw_class)) == NULL) {
83: warnx("unable to get login class for user %s.", uname);
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.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. */
97: period = login_getcaptime(lc, "passwordtime", 0, 0);
1.19 millert 98: if (period > 0) {
1.14 millert 99: pw->pw_change = time(NULL) + period;
1.19 millert 100: } else {
101: /*
102: * If the pw change time is the same we only need
103: * to update the spwd.db file.
104: */
105: if (pw->pw_change != 0)
106: pw->pw_change = 0;
107: else
1.20 millert 108: pwflags = _PASSWORD_SECUREONLY;
1.19 millert 109: }
1.14 millert 110:
1.15 millert 111: /* Drop user's real uid and block all signals to avoid a DoS. */
112: setuid(0);
113: sigfillset(&fullset);
114: sigdelset(&fullset, SIGINT);
115: sigprocmask(SIG_BLOCK, &fullset, NULL);
116:
117: /* Get a lock on the passwd file and open it. */
118: pw_init();
1.21 millert 119: for (i = 1; (tfd = pw_lock(0)) == -1; i++) {
120: if (i == 4)
121: (void)fputs("Attempting lock password file, "
122: "please wait or press ^C to abort", stderr);
123: (void)signal(SIGINT, kbintr);
124: if (i % 16 == 0)
1.15 millert 125: fputc('.', stderr);
1.21 millert 126: usleep(250000);
1.15 millert 127: (void)signal(SIGINT, SIG_IGN);
128: }
1.21 millert 129: if (i >= 4)
130: fputc('\n', stderr);
1.15 millert 131: pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
132: if (pfd < 0 || fcntl(pfd, F_SETFD, 1) == -1)
133: pw_error(_PATH_MASTERPASSWD, 1, 1);
134:
1.18 millert 135: /* Update master.passwd file and rebuild spwd.db. */
1.1 deraadt 136: pw_copy(pfd, tfd, pw);
1.20 millert 137: if (pw_mkdb(uname, pwflags) < 0)
1.19 millert 138: pw_error(NULL, 0, 1);
1.14 millert 139:
1.1 deraadt 140: return(0);
141: }
142:
143: char *
1.26 ! deraadt 144: getnewpasswd(struct passwd *pw, login_cap_t *lc, int authenticated)
1.1 deraadt 145: {
1.22 mpech 146: char *p;
1.11 provos 147: int tries, pwd_tries;
1.13 millert 148: char buf[_PASSWORD_LEN+1], salt[_PASSWORD_LEN];
1.24 millert 149: sig_t saveint, savequit;
1.1 deraadt 150:
1.24 millert 151: saveint = signal(SIGINT, kbintr);
152: savequit = signal(SIGQUIT, kbintr);
1.23 millert 153:
1.13 millert 154: if (!authenticated) {
155: (void)printf("Changing local password for %s.\n", pw->pw_name);
156: if (uid && pw->pw_passwd[0] &&
157: strcmp(crypt(getpass("Old password:"), pw->pw_passwd),
158: pw->pw_passwd)) {
159: errno = EACCES;
160: pw_error(NULL, 1, 1);
161: }
1.1 deraadt 162: }
1.26 ! deraadt 163:
1.14 millert 164: pwd_tries = pwd_gettries(pw, lc);
1.1 deraadt 165:
166: for (buf[0] = '\0', tries = 0;;) {
167: p = getpass("New password:");
168: if (!*p) {
169: (void)printf("Password unchanged.\n");
170: pw_error(NULL, 0, 0);
1.9 deraadt 171: }
172: if (strcmp(p, "s/key") == 0) {
173: printf("That password collides with a system feature. Choose another.\n");
174: continue;
1.1 deraadt 175: }
1.26 ! deraadt 176:
1.11 provos 177: if ((tries++ < pwd_tries || pwd_tries == 0)
1.14 millert 178: && pwd_check(pw, lc, p) == 0)
1.1 deraadt 179: continue;
1.13 millert 180: strlcpy(buf, p, sizeof(buf));
1.1 deraadt 181: if (!strcmp(buf, getpass("Retype new password:")))
182: break;
183: (void)printf("Mismatch; try again, EOF to quit.\n");
184: }
1.14 millert 185: if (!pwd_gensalt(salt, _PASSWORD_LEN, pw, lc, 'l')) {
1.6 provos 186: (void)printf("Couldn't generate salt.\n");
187: pw_error(NULL, 0, 0);
188: }
1.24 millert 189: (void)signal(SIGINT, saveint);
190: (void)signal(SIGQUIT, savequit);
1.23 millert 191:
1.1 deraadt 192: return(crypt(buf, salt));
1.15 millert 193: }
194:
195: void
1.26 ! deraadt 196: kbintr(int signo)
1.15 millert 197: {
1.21 millert 198: char msg[] = "\nPassword unchanged.\n";
199: struct iovec iv[5];
200: extern char *__progname;
201:
1.24 millert 202: iv[0].iov_base = msg;
1.21 millert 203: iv[0].iov_len = sizeof(msg) - 1;
204: iv[1].iov_base = __progname;
205: iv[1].iov_len = strlen(__progname);
206: iv[2].iov_base = ": ";
207: iv[2].iov_len = 2;
208: iv[3].iov_base = _PATH_MASTERPASSWD;
209: iv[3].iov_len = sizeof(_PATH_MASTERPASSWD) - 1;
210: iv[4].iov_base = " unchanged\n";
211: iv[4].iov_len = 11;
212: writev(STDERR_FILENO, iv, 5);
1.15 millert 213:
214: _exit(1);
1.1 deraadt 215: }