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