Annotation of src/usr.bin/passwd/local_passwd.c, Revision 1.7
1.7 ! weingart 1: /* $OpenBSD: local_passwd.c,v 1.6 1997/02/16 20:08:56 provos 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.7 ! weingart 38: static char rcsid[] = "$OpenBSD: local_passwd.c,v 1.6 1997/02/16 20:08:56 provos 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.1 deraadt 54:
55:
1.7 ! weingart 56: int
1.1 deraadt 57: local_passwd(uname)
58: char *uname;
59: {
60: struct passwd *pw;
61: int pfd, tfd;
62: char *getnewpasswd();
63:
64: if (!(pw = getpwnam(uname))) {
65: #ifdef YP
66: extern int use_yp;
67: if (!use_yp)
68: #endif
1.7 ! weingart 69: warnx("unknown user %s.", uname);
1.1 deraadt 70: return(1);
71: }
72:
73: uid = getuid();
74: if (uid && uid != pw->pw_uid) {
1.7 ! weingart 75: warnx("login != uid: %s", strerror(EACCES));
1.1 deraadt 76: return(1);
77: }
78:
79: pw_init();
1.2 deraadt 80: tfd = pw_lock(0);
1.4 millert 81: if (tfd < 0) {
82: if (errno == EEXIST)
83: errx(1, "the passwd file is busy.");
84: else
85: err(1, "can't open passwd temp file");
86: }
1.2 deraadt 87: pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
88: if (pfd < 0)
89: pw_error(_PATH_MASTERPASSWD, 1, 1);
1.1 deraadt 90:
91: /*
92: * Get the new password. Reset passwd change time to zero; when
93: * classes are implemented, go and get the "offset" value for this
94: * class and reset the timer.
95: */
96: pw->pw_passwd = getnewpasswd(pw);
97: pw->pw_change = 0;
98: pw_copy(pfd, tfd, pw);
99:
1.2 deraadt 100: if (pw_mkdb() < 0)
1.1 deraadt 101: pw_error((char *)NULL, 0, 1);
102: return(0);
103: }
104:
105: char *
106: getnewpasswd(pw)
107: register struct passwd *pw;
108: {
109: register char *p, *t;
110: int tries;
1.5 provos 111: char buf[_PASSWORD_LEN+1], salt[_PASSWORD_LEN], *crypt(), *getpass();
1.6 provos 112: int pwd_gensalt __P(( char *, int, struct passwd *, char));
1.1 deraadt 113:
114: (void)printf("Changing local password for %s.\n", pw->pw_name);
115:
116: if (uid && pw->pw_passwd[0] &&
117: strcmp(crypt(getpass("Old password:"), pw->pw_passwd),
118: pw->pw_passwd)) {
119: errno = EACCES;
120: pw_error(NULL, 1, 1);
121: }
122:
123: for (buf[0] = '\0', tries = 0;;) {
124: p = getpass("New password:");
125: if (!*p) {
126: (void)printf("Password unchanged.\n");
127: pw_error(NULL, 0, 0);
128: }
129: if (strlen(p) <= 5 && ++tries < 2) {
130: (void)printf("Please enter a longer password.\n");
131: continue;
132: }
133: for (t = p; *t && islower(*t); ++t);
134: if (!*t && ++tries < 2) {
135: (void)printf("Please don't use an all-lower case password.\nUnusual capitalization, control characters or digits are suggested.\n");
136: continue;
137: }
138: (void)strcpy(buf, p);
139: if (!strcmp(buf, getpass("Retype new password:")))
140: break;
141: (void)printf("Mismatch; try again, EOF to quit.\n");
142: }
1.6 provos 143: if( !pwd_gensalt( salt, _PASSWORD_LEN, pw, 'l' )) {
144: (void)printf("Couldn't generate salt.\n");
145: pw_error(NULL, 0, 0);
146: }
1.1 deraadt 147: return(crypt(buf, salt));
148: }