local_passwd.c revision 1.21 1 1.21 ad /* $NetBSD: local_passwd.c,v 1.21 2000/09/21 11:11:49 ad 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.14 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.9 thorpej #if 0
39 1.10 tls static char sccsid[] = "from: @(#)local_passwd.c 8.3 (Berkeley) 4/2/94";
40 1.9 thorpej #else
41 1.21 ad __RCSID("$NetBSD: local_passwd.c,v 1.21 2000/09/21 11:11:49 ad Exp $");
42 1.9 thorpej #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/types.h>
46 1.8 jtc #include <sys/stat.h>
47 1.14 lukem #include <ctype.h>
48 1.14 lukem #include <err.h>
49 1.14 lukem #include <errno.h>
50 1.14 lukem #include <fcntl.h>
51 1.1 cgd #include <pwd.h>
52 1.1 cgd #include <stdio.h>
53 1.14 lukem #include <stdlib.h>
54 1.7 cgd #include <string.h>
55 1.15 kleink #include <time.h>
56 1.8 jtc #include <unistd.h>
57 1.8 jtc #include <util.h>
58 1.17 mjl #include <login_cap.h>
59 1.1 cgd
60 1.10 tls #include "extern.h"
61 1.10 tls
62 1.21 ad static char *getnewpasswd __P((struct passwd *, int, int));
63 1.14 lukem
64 1.10 tls static uid_t uid;
65 1.19 aidan static int force_local;
66 1.1 cgd
67 1.1 cgd char *tempname;
68 1.1 cgd
69 1.14 lukem static char *
70 1.21 ad getnewpasswd(pw, min_pw_len, oldexpires)
71 1.10 tls struct passwd *pw;
72 1.17 mjl int min_pw_len;
73 1.21 ad int oldexpires;
74 1.1 cgd {
75 1.1 cgd int tries;
76 1.10 tls char *p, *t;
77 1.20 ad char buf[_PASSWORD_LEN+1], salt[_PASSWORD_LEN+1];
78 1.17 mjl
79 1.1 cgd (void)printf("Changing local password for %s.\n", pw->pw_name);
80 1.1 cgd
81 1.2 proven if (uid && pw->pw_passwd[0] &&
82 1.1 cgd strcmp(crypt(getpass("Old password:"), pw->pw_passwd),
83 1.1 cgd pw->pw_passwd)) {
84 1.1 cgd errno = EACCES;
85 1.1 cgd pw_error(NULL, 1, 1);
86 1.1 cgd }
87 1.1 cgd
88 1.1 cgd for (buf[0] = '\0', tries = 0;;) {
89 1.1 cgd p = getpass("New password:");
90 1.1 cgd if (!*p) {
91 1.1 cgd (void)printf("Password unchanged.\n");
92 1.1 cgd pw_error(NULL, 0, 0);
93 1.1 cgd }
94 1.17 mjl if (min_pw_len > 0 && strlen(p) < min_pw_len) {
95 1.21 ad (void)printf("Password is too short.\n");
96 1.21 ad continue;
97 1.21 ad }
98 1.21 ad if (uid && oldexpires && pw->pw_passwd[0] &&
99 1.21 ad !strcmp(crypt(p, pw->pw_passwd), pw->pw_passwd)) {
100 1.21 ad (void)printf("This password has expired or will"
101 1.21 ad " expire soon. Set a different password.\n");
102 1.17 mjl continue;
103 1.17 mjl }
104 1.6 deraadt if (strlen(p) <= 5 && ++tries < 2) {
105 1.1 cgd (void)printf("Please enter a longer password.\n");
106 1.1 cgd continue;
107 1.1 cgd }
108 1.1 cgd for (t = p; *t && islower(*t); ++t);
109 1.6 deraadt if (!*t && ++tries < 2) {
110 1.12 thorpej (void)printf("Please don't use an all-lower case "
111 1.12 thorpej "password.\nUnusual capitalization, "
112 1.12 thorpej "control characters or digits are "
113 1.12 thorpej "suggested.\n");
114 1.1 cgd continue;
115 1.1 cgd }
116 1.11 mrg (void)strncpy(buf, p, sizeof(buf) - 1);
117 1.16 mrg buf[sizeof(buf) - 1] = '\0';
118 1.1 cgd if (!strcmp(buf, getpass("Retype new password:")))
119 1.1 cgd break;
120 1.1 cgd (void)printf("Mismatch; try again, EOF to quit.\n");
121 1.1 cgd }
122 1.20 ad
123 1.20 ad if(!pwd_gensalt(salt, _PASSWORD_LEN, pw, 'l')) {
124 1.20 ad (void)printf("Couldn't generate salt.\n");
125 1.20 ad pw_error(NULL, 0, 0);
126 1.20 ad }
127 1.1 cgd return(crypt(buf, salt));
128 1.1 cgd }
129 1.1 cgd
130 1.10 tls int
131 1.19 aidan local_init(progname)
132 1.19 aidan const char *progname;
133 1.19 aidan {
134 1.19 aidan force_local = 0;
135 1.19 aidan return (0);
136 1.19 aidan }
137 1.19 aidan
138 1.19 aidan int
139 1.19 aidan local_arg(char arg, const char *optarg)
140 1.19 aidan {
141 1.19 aidan switch (arg) {
142 1.19 aidan case 'l':
143 1.19 aidan force_local = 1;
144 1.19 aidan break;
145 1.19 aidan default:
146 1.19 aidan return(0);
147 1.19 aidan }
148 1.19 aidan return(1);
149 1.19 aidan }
150 1.19 aidan
151 1.19 aidan int
152 1.19 aidan local_arg_end()
153 1.19 aidan {
154 1.19 aidan if (force_local)
155 1.19 aidan return(PW_USE_FORCE);
156 1.19 aidan return(PW_USE);
157 1.19 aidan }
158 1.19 aidan
159 1.19 aidan void
160 1.19 aidan local_end()
161 1.19 aidan {
162 1.19 aidan /* NOOP */
163 1.19 aidan }
164 1.19 aidan
165 1.19 aidan int
166 1.19 aidan local_chpw(uname)
167 1.19 aidan const char *uname;
168 1.10 tls {
169 1.10 tls struct passwd *pw;
170 1.13 phil struct passwd old_pw;
171 1.10 tls int pfd, tfd;
172 1.17 mjl int min_pw_len = 0;
173 1.17 mjl int pw_expiry = 0;
174 1.21 ad int oldexpires = 0;
175 1.21 ad time_t pw_warntime = _PASSWORD_WARNDAYS * 60*60*24;
176 1.18 mjl #ifdef LOGIN_CAP
177 1.17 mjl login_cap_t *lc;
178 1.18 mjl #endif
179 1.21 ad
180 1.10 tls if (!(pw = getpwnam(uname))) {
181 1.10 tls warnx("unknown user %s", uname);
182 1.10 tls return (1);
183 1.10 tls }
184 1.1 cgd
185 1.10 tls uid = getuid();
186 1.10 tls if (uid && uid != pw->pw_uid) {
187 1.10 tls warnx("%s", strerror(EACCES));
188 1.10 tls return (1);
189 1.1 cgd }
190 1.10 tls
191 1.13 phil /* Save the old pw information for comparing on pw_copy(). */
192 1.13 phil old_pw = *pw;
193 1.10 tls
194 1.10 tls /*
195 1.17 mjl * Get class restrictions for this user, then get the new password.
196 1.10 tls */
197 1.18 mjl #ifdef LOGIN_CAP
198 1.17 mjl if((lc = login_getclass(pw->pw_class))) {
199 1.17 mjl min_pw_len = (int) login_getcapnum(lc, "minpasswordlen", 0, 0);
200 1.17 mjl pw_expiry = (int) login_getcaptime(lc, "passwordtime", 0, 0);
201 1.21 ad pw_warntime = login_getcaptime(lc, "password-warn", pw_warntime,
202 1.21 ad pw_warntime);
203 1.17 mjl login_close(lc);
204 1.17 mjl }
205 1.18 mjl #endif
206 1.17 mjl
207 1.21 ad if (pw->pw_change)
208 1.21 ad oldexpires = (pw->pw_change == _PASSWORD_CHGNOW ||
209 1.21 ad pw->pw_change <= time(NULL) + pw_warntime);
210 1.21 ad
211 1.21 ad pw->pw_passwd = getnewpasswd(pw, min_pw_len, oldexpires);
212 1.17 mjl pw->pw_change = pw_expiry ? pw_expiry + time(NULL) : 0;
213 1.13 phil
214 1.16 mrg /*
215 1.16 mrg * Now that the user has given us a new password, let us
216 1.13 phil * change the database.
217 1.13 phil */
218 1.13 phil pw_init();
219 1.13 phil tfd = pw_lock(0);
220 1.13 phil if (tfd < 0) {
221 1.13 phil warnx ("The passwd file is busy, waiting...");
222 1.13 phil tfd = pw_lock(10);
223 1.13 phil if (tfd < 0)
224 1.13 phil errx(1, "The passwd file is still busy, "
225 1.13 phil "try again later.");
226 1.13 phil }
227 1.13 phil
228 1.13 phil pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
229 1.13 phil if (pfd < 0)
230 1.13 phil pw_error(_PATH_MASTERPASSWD, 1, 1);
231 1.13 phil
232 1.13 phil pw_copy(pfd, tfd, pw, &old_pw);
233 1.10 tls
234 1.10 tls if (pw_mkdb() < 0)
235 1.10 tls pw_error((char *)NULL, 0, 1);
236 1.10 tls return (0);
237 1.1 cgd }
238