local_passwd.c revision 1.12 1 1.12 thorpej /* $NetBSD: local_passwd.c,v 1.12 1997/02/22 01:50:46 thorpej Exp $ */
2 1.9 thorpej
3 1.1 cgd /*-
4 1.10 tls * Copyright (c) 1990, 1993, 1994
5 1.10 tls * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #ifndef lint
37 1.9 thorpej #if 0
38 1.10 tls static char sccsid[] = "from: @(#)local_passwd.c 8.3 (Berkeley) 4/2/94";
39 1.9 thorpej #else
40 1.12 thorpej static char rcsid[] = "$NetBSD: local_passwd.c,v 1.12 1997/02/22 01:50:46 thorpej Exp $";
41 1.9 thorpej #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd #include <sys/types.h>
45 1.8 jtc #include <sys/stat.h>
46 1.1 cgd #include <pwd.h>
47 1.1 cgd #include <errno.h>
48 1.1 cgd #include <stdio.h>
49 1.7 cgd #include <string.h>
50 1.8 jtc #include <unistd.h>
51 1.8 jtc #include <fcntl.h>
52 1.8 jtc #include <util.h>
53 1.1 cgd
54 1.10 tls #include "extern.h"
55 1.10 tls
56 1.10 tls static uid_t uid;
57 1.1 cgd
58 1.1 cgd char *tempname;
59 1.1 cgd
60 1.10 tls static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */
61 1.10 tls "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
62 1.10 tls
63 1.10 tls void
64 1.10 tls to64(s, v, n)
65 1.10 tls char *s;
66 1.10 tls long v;
67 1.10 tls int n;
68 1.1 cgd {
69 1.10 tls while (--n >= 0) {
70 1.10 tls *s++ = itoa64[v&0x3f];
71 1.10 tls v >>= 6;
72 1.1 cgd }
73 1.1 cgd }
74 1.1 cgd
75 1.1 cgd char *
76 1.1 cgd getnewpasswd(pw)
77 1.10 tls struct passwd *pw;
78 1.1 cgd {
79 1.1 cgd int tries;
80 1.10 tls char *p, *t;
81 1.1 cgd char buf[_PASSWORD_LEN+1], salt[9], *crypt(), *getpass();
82 1.1 cgd
83 1.1 cgd (void)printf("Changing local password for %s.\n", pw->pw_name);
84 1.1 cgd
85 1.2 proven if (uid && pw->pw_passwd[0] &&
86 1.1 cgd strcmp(crypt(getpass("Old password:"), pw->pw_passwd),
87 1.1 cgd pw->pw_passwd)) {
88 1.1 cgd errno = EACCES;
89 1.1 cgd pw_error(NULL, 1, 1);
90 1.1 cgd }
91 1.1 cgd
92 1.1 cgd for (buf[0] = '\0', tries = 0;;) {
93 1.1 cgd p = getpass("New password:");
94 1.1 cgd if (!*p) {
95 1.1 cgd (void)printf("Password unchanged.\n");
96 1.1 cgd pw_error(NULL, 0, 0);
97 1.1 cgd }
98 1.6 deraadt if (strlen(p) <= 5 && ++tries < 2) {
99 1.1 cgd (void)printf("Please enter a longer password.\n");
100 1.1 cgd continue;
101 1.1 cgd }
102 1.1 cgd for (t = p; *t && islower(*t); ++t);
103 1.6 deraadt if (!*t && ++tries < 2) {
104 1.12 thorpej (void)printf("Please don't use an all-lower case "
105 1.12 thorpej "password.\nUnusual capitalization, "
106 1.12 thorpej "control characters or digits are "
107 1.12 thorpej "suggested.\n");
108 1.1 cgd continue;
109 1.1 cgd }
110 1.11 mrg (void)strncpy(buf, p, sizeof(buf) - 1);
111 1.1 cgd if (!strcmp(buf, getpass("Retype new password:")))
112 1.1 cgd break;
113 1.1 cgd (void)printf("Mismatch; try again, EOF to quit.\n");
114 1.1 cgd }
115 1.1 cgd /* grab a random printable character that isn't a colon */
116 1.1 cgd (void)srandom((int)time((time_t *)NULL));
117 1.1 cgd #ifdef NEWSALT
118 1.1 cgd salt[0] = _PASSWORD_EFMT1;
119 1.1 cgd to64(&salt[1], (long)(29 * 25), 4);
120 1.1 cgd to64(&salt[5], random(), 4);
121 1.1 cgd #else
122 1.1 cgd to64(&salt[0], random(), 2);
123 1.1 cgd #endif
124 1.1 cgd return(crypt(buf, salt));
125 1.1 cgd }
126 1.1 cgd
127 1.10 tls int
128 1.10 tls local_passwd(uname)
129 1.10 tls char *uname;
130 1.10 tls {
131 1.10 tls struct passwd *pw;
132 1.10 tls int pfd, tfd;
133 1.10 tls char *getnewpasswd();
134 1.10 tls
135 1.10 tls if (!(pw = getpwnam(uname))) {
136 1.10 tls warnx("unknown user %s", uname);
137 1.10 tls return (1);
138 1.10 tls }
139 1.1 cgd
140 1.10 tls uid = getuid();
141 1.10 tls if (uid && uid != pw->pw_uid) {
142 1.10 tls warnx("%s", strerror(EACCES));
143 1.10 tls return (1);
144 1.1 cgd }
145 1.10 tls
146 1.10 tls pw_init();
147 1.10 tls tfd = pw_lock(0);
148 1.10 tls if (tfd < 0)
149 1.10 tls errx(1, "the passwd file is busy.");
150 1.10 tls pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
151 1.10 tls if (pfd < 0)
152 1.10 tls pw_error(_PATH_MASTERPASSWD, 1, 1);
153 1.10 tls
154 1.10 tls /*
155 1.10 tls * Get the new password. Reset passwd change time to zero; when
156 1.10 tls * classes are implemented, go and get the "offset" value for this
157 1.10 tls * class and reset the timer.
158 1.10 tls */
159 1.10 tls pw->pw_passwd = getnewpasswd(pw);
160 1.10 tls pw->pw_change = 0;
161 1.10 tls pw_copy(pfd, tfd, pw);
162 1.10 tls
163 1.10 tls if (pw_mkdb() < 0)
164 1.10 tls pw_error((char *)NULL, 0, 1);
165 1.10 tls return (0);
166 1.1 cgd }
167