local_passwd.c revision 1.25 1 /* $NetBSD: local_passwd.c,v 1.25 2002/11/16 04:41:50 itojun 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.25 2002/11/16 04:41:50 itojun 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));
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)
71 struct passwd *pw;
72 int min_pw_len;
73 {
74 int tries;
75 char *p, *t;
76 char buf[_PASSWORD_LEN+1], salt[_PASSWORD_LEN+1];
77
78 (void)printf("Changing local password for %s.\n", pw->pw_name);
79
80 if (uid && pw->pw_passwd[0] &&
81 strcmp(crypt(getpass("Old password:"), pw->pw_passwd),
82 pw->pw_passwd)) {
83 errno = EACCES;
84 pw_error(NULL, 1, 1);
85 }
86
87 for (buf[0] = '\0', tries = 0;;) {
88 p = getpass("New password:");
89 if (!*p) {
90 (void)printf("Password unchanged.\n");
91 pw_error(NULL, 0, 0);
92 }
93 if (min_pw_len > 0 && strlen(p) < min_pw_len) {
94 (void) printf("Password is too short.\n");
95 continue;
96 }
97 if (strlen(p) <= 5 && ++tries < 2) {
98 (void)printf("Please enter a longer password.\n");
99 continue;
100 }
101 for (t = p; *t && islower(*t); ++t);
102 if (!*t && ++tries < 2) {
103 (void)printf("Please don't use an all-lower case "
104 "password.\nUnusual capitalization, "
105 "control characters or digits are "
106 "suggested.\n");
107 continue;
108 }
109 (void)strlcpy(buf, p, sizeof(buf));
110 if (!strcmp(buf, getpass("Retype new password:")))
111 break;
112 (void)printf("Mismatch; try again, EOF to quit.\n");
113 }
114
115 if(!pwd_gensalt(salt, _PASSWORD_LEN, pw, 'l')) {
116 (void)printf("Couldn't generate salt.\n");
117 pw_error(NULL, 0, 0);
118 }
119 return(crypt(buf, salt));
120 }
121
122 int
123 local_init(progname)
124 const char *progname;
125 {
126 force_local = 0;
127 return (0);
128 }
129
130 int
131 local_arg(char arg, const char *optarg)
132 {
133 switch (arg) {
134 case 'l':
135 force_local = 1;
136 break;
137 default:
138 return(0);
139 }
140 return(1);
141 }
142
143 int
144 local_arg_end()
145 {
146 if (force_local)
147 return(PW_USE_FORCE);
148 return(PW_USE);
149 }
150
151 void
152 local_end()
153 {
154 /* NOOP */
155 }
156
157 int
158 local_chpw(uname)
159 const char *uname;
160 {
161 struct passwd *pw;
162 struct passwd old_pw;
163 time_t old_change;
164 int pfd, tfd;
165 int min_pw_len = 0;
166 int pw_expiry = 0;
167 #ifdef LOGIN_CAP
168 login_cap_t *lc;
169 #endif
170
171 if (!(pw = getpwnam(uname))) {
172 warnx("unknown user %s", uname);
173 return (1);
174 }
175
176 uid = getuid();
177 if (uid && uid != pw->pw_uid) {
178 warnx("%s", strerror(EACCES));
179 return (1);
180 }
181
182 /* Save the old pw information for comparing on pw_copy(). */
183 old_pw = *pw;
184
185 /*
186 * Get class restrictions for this user, then get the new password.
187 */
188 #ifdef LOGIN_CAP
189 if((lc = login_getclass(pw->pw_class))) {
190 min_pw_len = (int) login_getcapnum(lc, "minpasswordlen", 0, 0);
191 pw_expiry = (int) login_getcaptime(lc, "passwordtime", 0, 0);
192 login_close(lc);
193 }
194 #endif
195
196 pw->pw_passwd = getnewpasswd(pw, min_pw_len);
197 old_change = pw->pw_change;
198 pw->pw_change = pw_expiry ? pw_expiry + time(NULL) : 0;
199
200 /*
201 * Now that the user has given us a new password, let us
202 * change the database.
203 */
204 pw_init();
205 tfd = pw_lock(0);
206 if (tfd < 0) {
207 warnx ("The passwd file is busy, waiting...");
208 tfd = pw_lock(10);
209 if (tfd < 0)
210 errx(1, "The passwd file is still busy, "
211 "try again later.");
212 }
213
214 pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
215 if (pfd < 0)
216 pw_error(_PATH_MASTERPASSWD, 1, 1);
217
218 pw_copy(pfd, tfd, pw, &old_pw);
219
220 if (pw_mkdb(uname, old_change == pw->pw_change) < 0)
221 pw_error((char *)NULL, 0, 1);
222 return (0);
223 }
224