Annotation of src/usr.bin/passwd/local_passwd.c, Revision 1.3
1.3 ! deraadt 1: /* $OpenBSD$ */
! 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.3 ! deraadt 38: static char rcsid[] = "$OpenBSD: local_passwd.c,v 1.2 1996/05/22 11:35:27 deraadt 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>
44: #include <errno.h>
45: #include <stdio.h>
46: #include <string.h>
1.2 deraadt 47: #include <unistd.h>
48: #include <fcntl.h>
49: #include <util.h>
1.1 deraadt 50:
51: uid_t uid;
52:
53: char *progname = "passwd";
54: char *tempname;
55:
56: local_passwd(uname)
57: char *uname;
58: {
59: struct passwd *pw;
60: int pfd, tfd;
61: char *getnewpasswd();
62:
63: if (!(pw = getpwnam(uname))) {
64: #ifdef YP
65: extern int use_yp;
66: if (!use_yp)
67: #endif
68: (void)fprintf(stderr, "passwd: unknown user %s.\n", uname);
69: return(1);
70: }
71:
72: uid = getuid();
73: if (uid && uid != pw->pw_uid) {
74: (void)fprintf(stderr, "passwd: %s\n", strerror(EACCES));
75: return(1);
76: }
77:
78: pw_init();
1.2 deraadt 79: tfd = pw_lock(0);
80: if (tfd < 0)
81: errx(1, "the passwd file is busy.");
82: pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
83: if (pfd < 0)
84: pw_error(_PATH_MASTERPASSWD, 1, 1);
1.1 deraadt 85:
86: /*
87: * Get the new password. Reset passwd change time to zero; when
88: * classes are implemented, go and get the "offset" value for this
89: * class and reset the timer.
90: */
91: pw->pw_passwd = getnewpasswd(pw);
92: pw->pw_change = 0;
93: pw_copy(pfd, tfd, pw);
94:
1.2 deraadt 95: if (pw_mkdb() < 0)
1.1 deraadt 96: pw_error((char *)NULL, 0, 1);
97: return(0);
98: }
99:
100: char *
101: getnewpasswd(pw)
102: register struct passwd *pw;
103: {
104: register char *p, *t;
105: int tries;
106: char buf[_PASSWORD_LEN+1], salt[9], *crypt(), *getpass();
107:
108: (void)printf("Changing local password for %s.\n", pw->pw_name);
109:
110: if (uid && pw->pw_passwd[0] &&
111: strcmp(crypt(getpass("Old password:"), pw->pw_passwd),
112: pw->pw_passwd)) {
113: errno = EACCES;
114: pw_error(NULL, 1, 1);
115: }
116:
117: for (buf[0] = '\0', tries = 0;;) {
118: p = getpass("New password:");
119: if (!*p) {
120: (void)printf("Password unchanged.\n");
121: pw_error(NULL, 0, 0);
122: }
123: if (strlen(p) <= 5 && ++tries < 2) {
124: (void)printf("Please enter a longer password.\n");
125: continue;
126: }
127: for (t = p; *t && islower(*t); ++t);
128: if (!*t && ++tries < 2) {
129: (void)printf("Please don't use an all-lower case password.\nUnusual capitalization, control characters or digits are suggested.\n");
130: continue;
131: }
132: (void)strcpy(buf, p);
133: if (!strcmp(buf, getpass("Retype new password:")))
134: break;
135: (void)printf("Mismatch; try again, EOF to quit.\n");
136: }
137: /* grab a random printable character that isn't a colon */
138: (void)srandom((int)time((time_t *)NULL));
139: #ifdef NEWSALT
140: salt[0] = _PASSWORD_EFMT1;
141: to64(&salt[1], (long)(29 * 25), 4);
142: to64(&salt[5], random(), 4);
143: #else
144: to64(&salt[0], random(), 2);
145: #endif
146: return(crypt(buf, salt));
147: }
148:
149: static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */
150: "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
151:
152: to64(s, v, n)
153: register char *s;
154: register long v;
155: register int n;
156: {
157: while (--n >= 0) {
158: *s++ = itoa64[v&0x3f];
159: v >>= 6;
160: }
161: }