Annotation of src/usr.bin/passwd/local_passwd.c, Revision 1.13
1.13 ! millert 1: /* $OpenBSD: local_passwd.c,v 1.12 2000/11/26 01:29:43 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
37: /*static char sccsid[] = "from: @(#)local_passwd.c 5.5 (Berkeley) 5/6/91";*/
1.13 ! millert 38: static char rcsid[] = "$OpenBSD: local_passwd.c,v 1.12 2000/11/26 01:29:43 millert Exp $";
1.1 deraadt 39: #endif /* not lint */
40:
41: #include <sys/types.h>
1.2 deraadt 42: #include <sys/stat.h>
1.1 deraadt 43: #include <pwd.h>
1.7 weingart 44: #include <err.h>
1.1 deraadt 45: #include <errno.h>
46: #include <stdio.h>
47: #include <string.h>
1.2 deraadt 48: #include <unistd.h>
1.7 weingart 49: #include <ctype.h>
1.2 deraadt 50: #include <fcntl.h>
51: #include <util.h>
1.1 deraadt 52:
1.7 weingart 53: static uid_t uid;
1.11 provos 54: extern int pwd_gensalt __P(( char *, int, struct passwd *, char));
55: extern int pwd_check __P((struct passwd *, char *));
56: extern int pwd_gettries __P((struct passwd *));
1.1 deraadt 57:
1.7 weingart 58: int
1.13 ! millert 59: local_passwd(uname, authenticated)
1.1 deraadt 60: char *uname;
1.13 ! millert 61: int authenticated;
1.1 deraadt 62: {
63: struct passwd *pw;
64: int pfd, tfd;
65: char *getnewpasswd();
66:
67: if (!(pw = getpwnam(uname))) {
68: #ifdef YP
69: extern int use_yp;
70: if (!use_yp)
71: #endif
1.7 weingart 72: warnx("unknown user %s.", uname);
1.1 deraadt 73: return(1);
74: }
75:
1.13 ! millert 76: uid = authenticated ? pw->pw_uid : getuid();
1.1 deraadt 77: if (uid && uid != pw->pw_uid) {
1.8 millert 78: warnx("login/uid mismatch, username argument required.");
1.1 deraadt 79: return(1);
80: }
81:
82: pw_init();
1.2 deraadt 83: tfd = pw_lock(0);
1.4 millert 84: if (tfd < 0) {
85: if (errno == EEXIST)
86: errx(1, "the passwd file is busy.");
87: else
88: err(1, "can't open passwd temp file");
89: }
1.2 deraadt 90: pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
1.11 provos 91: if (pfd < 0 || fcntl(pfd, F_SETFD, 1) == -1)
1.2 deraadt 92: pw_error(_PATH_MASTERPASSWD, 1, 1);
1.1 deraadt 93:
94: /*
95: * Get the new password. Reset passwd change time to zero; when
96: * classes are implemented, go and get the "offset" value for this
97: * class and reset the timer.
98: */
1.13 ! millert 99: pw->pw_passwd = getnewpasswd(pw, authenticated);
1.1 deraadt 100: pw->pw_change = 0;
101: pw_copy(pfd, tfd, pw);
102:
1.12 millert 103: if (pw_mkdb(uname) < 0)
1.1 deraadt 104: pw_error((char *)NULL, 0, 1);
105: return(0);
106: }
107:
108: char *
1.13 ! millert 109: getnewpasswd(pw, authenticated)
1.1 deraadt 110: register struct passwd *pw;
1.13 ! millert 111: int authenticated;
1.1 deraadt 112: {
1.13 ! millert 113: register char *p;
1.11 provos 114: int tries, pwd_tries;
1.13 ! millert 115: char buf[_PASSWORD_LEN+1], salt[_PASSWORD_LEN];
1.1 deraadt 116:
1.13 ! millert 117: if (!authenticated) {
! 118: (void)printf("Changing local password for %s.\n", pw->pw_name);
! 119: if (uid && pw->pw_passwd[0] &&
! 120: strcmp(crypt(getpass("Old password:"), pw->pw_passwd),
! 121: pw->pw_passwd)) {
! 122: errno = EACCES;
! 123: pw_error(NULL, 1, 1);
! 124: }
1.1 deraadt 125: }
1.11 provos 126:
127: pwd_tries = pwd_gettries(pw);
1.1 deraadt 128:
129: for (buf[0] = '\0', tries = 0;;) {
130: p = getpass("New password:");
131: if (!*p) {
132: (void)printf("Password unchanged.\n");
133: pw_error(NULL, 0, 0);
1.9 deraadt 134: }
135: if (strcmp(p, "s/key") == 0) {
136: printf("That password collides with a system feature. Choose another.\n");
137: continue;
1.1 deraadt 138: }
1.11 provos 139:
140: if ((tries++ < pwd_tries || pwd_tries == 0)
141: && pwd_check(pw, p) == 0)
1.1 deraadt 142: continue;
1.13 ! millert 143: strlcpy(buf, p, sizeof(buf));
1.1 deraadt 144: if (!strcmp(buf, getpass("Retype new password:")))
145: break;
146: (void)printf("Mismatch; try again, EOF to quit.\n");
147: }
1.13 ! millert 148: if(!pwd_gensalt(salt, _PASSWORD_LEN, pw, 'l')) {
1.6 provos 149: (void)printf("Couldn't generate salt.\n");
150: pw_error(NULL, 0, 0);
151: }
1.1 deraadt 152: return(crypt(buf, salt));
153: }