yp_passwd.c revision 1.30 1 1.30 christos /* $NetBSD: yp_passwd.c,v 1.30 2005/01/12 03:34:58 christos Exp $ */
2 1.9 thorpej
3 1.1 brezak /*
4 1.11 tls * Copyright (c) 1988, 1990, 1993, 1994
5 1.11 tls * The Regents of the University of California. All rights reserved.
6 1.1 brezak *
7 1.1 brezak * Redistribution and use in source and binary forms, with or without
8 1.1 brezak * modification, are permitted provided that the following conditions
9 1.1 brezak * are met:
10 1.1 brezak * 1. Redistributions of source code must retain the above copyright
11 1.1 brezak * notice, this list of conditions and the following disclaimer.
12 1.1 brezak * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 brezak * notice, this list of conditions and the following disclaimer in the
14 1.1 brezak * documentation and/or other materials provided with the distribution.
15 1.27 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 brezak * may be used to endorse or promote products derived from this software
17 1.1 brezak * without specific prior written permission.
18 1.1 brezak *
19 1.1 brezak * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 brezak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 brezak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 brezak * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 brezak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 brezak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 brezak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 brezak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 brezak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 brezak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 brezak * SUCH DAMAGE.
30 1.1 brezak */
31 1.9 thorpej
32 1.15 lukem #include <sys/cdefs.h>
33 1.1 brezak #ifndef lint
34 1.9 thorpej #if 0
35 1.11 tls static char sccsid[] = "from: @(#)local_passwd.c 8.3 (Berkeley) 4/2/94";
36 1.9 thorpej #else
37 1.30 christos __RCSID("$NetBSD: yp_passwd.c,v 1.30 2005/01/12 03:34:58 christos Exp $");
38 1.9 thorpej #endif
39 1.1 brezak #endif /* not lint */
40 1.1 brezak
41 1.1 brezak #ifdef YP
42 1.1 brezak
43 1.15 lukem #include <ctype.h>
44 1.9 thorpej #include <err.h>
45 1.15 lukem #include <errno.h>
46 1.15 lukem #include <netdb.h>
47 1.15 lukem #include <pwd.h>
48 1.1 brezak #include <stdio.h>
49 1.15 lukem #include <stdlib.h>
50 1.1 brezak #include <string.h>
51 1.1 brezak #include <time.h>
52 1.15 lukem #include <unistd.h>
53 1.30 christos #include <limits.h>
54 1.30 christos #include <util.h>
55 1.15 lukem
56 1.1 brezak #include <rpc/rpc.h>
57 1.1 brezak #include <rpcsvc/yp_prot.h>
58 1.1 brezak #include <rpcsvc/ypclnt.h>
59 1.15 lukem
60 1.15 lukem #include "extern.h"
61 1.15 lukem
62 1.1 brezak #define passwd yp_passwd_rec
63 1.1 brezak #include <rpcsvc/yppasswd.h>
64 1.1 brezak #undef passwd
65 1.4 deraadt
66 1.1 brezak #ifndef _PASSWORD_LEN
67 1.1 brezak #define _PASSWORD_LEN PASS_MAX
68 1.1 brezak #endif
69 1.9 thorpej
70 1.22 aidan static int yflag;
71 1.10 thorpej
72 1.15 lukem static char *getnewpasswd __P((struct passwd *, char **));
73 1.22 aidan static int ypgetpwnam __P((const char *));
74 1.30 christos static void pwerror __P((char *, int, int));
75 1.1 brezak
76 1.1 brezak static uid_t uid;
77 1.1 brezak char *domain;
78 1.1 brezak
79 1.15 lukem static void
80 1.30 christos pwerror(name, err, eval)
81 1.4 deraadt char *name;
82 1.4 deraadt int err, eval;
83 1.4 deraadt {
84 1.17 mrg
85 1.15 lukem if (err)
86 1.18 mrg warn("%s", name);
87 1.9 thorpej errx(eval, "YP passwd database unchanged");
88 1.4 deraadt }
89 1.4 deraadt
90 1.22 aidan int
91 1.22 aidan yp_init(progname)
92 1.22 aidan const char *progname;
93 1.22 aidan {
94 1.22 aidan int yppwd;
95 1.22 aidan
96 1.22 aidan if (strcmp(progname, "yppasswd") == 0) {
97 1.22 aidan yppwd = 1;
98 1.22 aidan } else
99 1.22 aidan yppwd = 0;
100 1.22 aidan yflag = 0;
101 1.22 aidan if (_yp_check(NULL) == 0) {
102 1.22 aidan /* can't use YP. */
103 1.22 aidan if (yppwd)
104 1.22 aidan errx(1, "YP not in use.");
105 1.22 aidan return(-1);
106 1.22 aidan }
107 1.22 aidan return (0);
108 1.22 aidan }
109 1.22 aidan
110 1.22 aidan int
111 1.22 aidan yp_arg(ch, arg)
112 1.22 aidan char ch;
113 1.22 aidan const char *arg;
114 1.22 aidan {
115 1.22 aidan switch (ch) {
116 1.22 aidan case 'y':
117 1.22 aidan yflag = 1;
118 1.22 aidan break;
119 1.22 aidan default:
120 1.22 aidan return(0);
121 1.22 aidan }
122 1.22 aidan return(1);
123 1.22 aidan }
124 1.22 aidan
125 1.22 aidan int
126 1.22 aidan yp_arg_end()
127 1.16 tv {
128 1.22 aidan if (yflag)
129 1.22 aidan return (PW_USE_FORCE);
130 1.22 aidan return (PW_USE);
131 1.22 aidan }
132 1.17 mrg
133 1.22 aidan void
134 1.22 aidan yp_end()
135 1.22 aidan {
136 1.22 aidan /* NOOP */
137 1.16 tv }
138 1.16 tv
139 1.9 thorpej int
140 1.22 aidan yp_chpw(username)
141 1.22 aidan const char *username;
142 1.1 brezak {
143 1.4 deraadt char *master;
144 1.4 deraadt int r, rpcport, status;
145 1.4 deraadt struct yppasswd yppasswd;
146 1.4 deraadt struct passwd *pw;
147 1.1 brezak struct timeval tv;
148 1.1 brezak CLIENT *client;
149 1.21 mjl
150 1.4 deraadt uid = getuid();
151 1.4 deraadt
152 1.4 deraadt /*
153 1.4 deraadt * Get local domain
154 1.4 deraadt */
155 1.22 aidan if ((r = yp_get_default_domain(&domain)) != 0)
156 1.9 thorpej errx(1, "can't get local YP domain. Reason: %s",
157 1.9 thorpej yperr_string(r));
158 1.4 deraadt
159 1.4 deraadt /*
160 1.4 deraadt * Find the host for the passwd map; it should be running
161 1.4 deraadt * the daemon.
162 1.4 deraadt */
163 1.16 tv if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
164 1.22 aidan warnx("can't find the master YP server. Reason: %s",
165 1.9 thorpej yperr_string(r));
166 1.22 aidan /* continuation */
167 1.22 aidan return(-1);
168 1.16 tv }
169 1.1 brezak
170 1.4 deraadt /*
171 1.4 deraadt * Ask the portmapper for the port of the daemon.
172 1.4 deraadt */
173 1.4 deraadt if ((rpcport = getrpcport(master, YPPASSWDPROG,
174 1.10 thorpej YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) {
175 1.22 aidan warnx("master YP server not running yppasswd daemon.\n\t%s\n",
176 1.22 aidan "Can't change YP password.");
177 1.22 aidan /* continuation */
178 1.22 aidan return(-1);
179 1.10 thorpej }
180 1.1 brezak
181 1.4 deraadt /*
182 1.20 simonb * Be sure the port is privileged
183 1.4 deraadt */
184 1.9 thorpej if (rpcport >= IPPORT_RESERVED)
185 1.9 thorpej errx(1, "yppasswd daemon is on an invalid port.");
186 1.4 deraadt
187 1.21 mjl /* Bail out if this is a local (non-yp) user, */
188 1.21 mjl /* then get user's login identity */
189 1.21 mjl if (!ypgetpwnam(username) ||
190 1.21 mjl !(pw = getpwnam(username))) {
191 1.22 aidan warnx("YP unknown user %s", username);
192 1.22 aidan /* continuation */
193 1.22 aidan return(-1);
194 1.16 tv }
195 1.9 thorpej
196 1.9 thorpej if (uid && uid != pw->pw_uid)
197 1.9 thorpej errx(1, "you may only change your own password: %s",
198 1.9 thorpej strerror(EACCES));
199 1.1 brezak
200 1.4 deraadt /* prompt for new password */
201 1.4 deraadt yppasswd.newpw.pw_passwd = getnewpasswd(pw, &yppasswd.oldpass);
202 1.1 brezak
203 1.4 deraadt /* tell rpc.yppasswdd */
204 1.19 mycroft yppasswd.newpw.pw_name = strdup(pw->pw_name);
205 1.26 itojun if (!yppasswd.newpw.pw_name) {
206 1.26 itojun err(1, "strdup");
207 1.26 itojun /*NOTREACHED*/
208 1.26 itojun }
209 1.4 deraadt yppasswd.newpw.pw_uid = pw->pw_uid;
210 1.4 deraadt yppasswd.newpw.pw_gid = pw->pw_gid;
211 1.19 mycroft yppasswd.newpw.pw_gecos = strdup(pw->pw_gecos);
212 1.26 itojun if (!yppasswd.newpw.pw_gecos) {
213 1.26 itojun err(1, "strdup");
214 1.26 itojun /*NOTREACHED*/
215 1.26 itojun }
216 1.19 mycroft yppasswd.newpw.pw_dir = strdup(pw->pw_dir);
217 1.26 itojun if (!yppasswd.newpw.pw_dir) {
218 1.26 itojun err(1, "strdup");
219 1.26 itojun /*NOTREACHED*/
220 1.26 itojun }
221 1.19 mycroft yppasswd.newpw.pw_shell = strdup(pw->pw_shell);
222 1.26 itojun if (!yppasswd.newpw.pw_shell) {
223 1.26 itojun err(1, "strdup");
224 1.26 itojun /*NOTREACHED*/
225 1.26 itojun }
226 1.16 tv
227 1.4 deraadt client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
228 1.17 mrg if (client == NULL) {
229 1.9 thorpej warnx("cannot contact yppasswdd on %s: Reason: %s",
230 1.5 deraadt master, yperr_string(YPERR_YPBIND));
231 1.17 mrg return (YPERR_YPBIND);
232 1.4 deraadt }
233 1.9 thorpej
234 1.4 deraadt client->cl_auth = authunix_create_default();
235 1.4 deraadt tv.tv_sec = 2;
236 1.4 deraadt tv.tv_usec = 0;
237 1.4 deraadt r = clnt_call(client, YPPASSWDPROC_UPDATE,
238 1.9 thorpej xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
239 1.4 deraadt if (r)
240 1.9 thorpej errx(1, "rpc to yppasswdd failed.");
241 1.4 deraadt else if (status)
242 1.4 deraadt printf("Couldn't change YP password.\n");
243 1.4 deraadt else
244 1.9 thorpej printf("The YP password has been changed on %s, %s\n",
245 1.9 thorpej master, "the master YP passwd server.");
246 1.22 aidan return(0);
247 1.1 brezak }
248 1.1 brezak
249 1.1 brezak static char *
250 1.1 brezak getnewpasswd(pw, old_pass)
251 1.11 tls struct passwd *pw;
252 1.4 deraadt char **old_pass;
253 1.1 brezak {
254 1.11 tls int tries;
255 1.11 tls char *p, *t;
256 1.4 deraadt static char buf[_PASSWORD_LEN+1];
257 1.23 ad char salt[_PASSWORD_LEN+1];
258 1.30 christos char option[LINE_MAX], *key, *opt;
259 1.23 ad
260 1.2 brezak (void)printf("Changing YP password for %s.\n", pw->pw_name);
261 1.1 brezak
262 1.6 deraadt if (old_pass) {
263 1.4 deraadt *old_pass = NULL;
264 1.4 deraadt
265 1.8 phil if (pw->pw_passwd[0]) {
266 1.8 phil if (strcmp(crypt(p = getpass("Old password:"),
267 1.8 phil pw->pw_passwd), pw->pw_passwd)) {
268 1.26 itojun (void)printf("Sorry.\n");
269 1.30 christos pwerror(NULL, 0, 1);
270 1.8 phil }
271 1.7 phil } else {
272 1.7 phil p = "";
273 1.4 deraadt }
274 1.7 phil
275 1.4 deraadt *old_pass = strdup(p);
276 1.26 itojun if (!*old_pass) {
277 1.26 itojun (void)printf("not enough core.\n");
278 1.30 christos pwerror(NULL, 0, 1);
279 1.26 itojun }
280 1.4 deraadt }
281 1.1 brezak for (buf[0] = '\0', tries = 0;;) {
282 1.1 brezak p = getpass("New password:");
283 1.1 brezak if (!*p) {
284 1.1 brezak (void)printf("Password unchanged.\n");
285 1.30 christos pwerror(NULL, 0, 0);
286 1.1 brezak }
287 1.4 deraadt if (strlen(p) <= 5 && ++tries < 2) {
288 1.1 brezak (void)printf("Please enter a longer password.\n");
289 1.1 brezak continue;
290 1.1 brezak }
291 1.28 dsl for (t = p; *t && islower((unsigned char)*t); ++t);
292 1.4 deraadt if (!*t && ++tries < 2) {
293 1.13 thorpej (void)printf("Please don't use an all-lower case "
294 1.13 thorpej "password.\nUnusual capitalization, "
295 1.13 thorpej "control characters or digits are "
296 1.13 thorpej "suggested.\n");
297 1.1 brezak continue;
298 1.1 brezak }
299 1.25 itojun (void)strlcpy(buf, p, sizeof(buf));
300 1.1 brezak if (!strcmp(buf, getpass("Retype new password:")))
301 1.1 brezak break;
302 1.1 brezak (void)printf("Mismatch; try again, EOF to quit.\n");
303 1.1 brezak }
304 1.23 ad
305 1.30 christos pw_getpwconf(option, sizeof(option), pw, "ypcipher");
306 1.30 christos opt = option;
307 1.30 christos key = strsep(&opt, ",");
308 1.30 christos if (pw_gensalt(salt, _PASSWORD_LEN, key, opt) == -1) {
309 1.29 christos warn("Couldn't generate salt");
310 1.30 christos pwerror(NULL, 0, 0);
311 1.26 itojun }
312 1.26 itojun p = strdup(crypt(buf, salt));
313 1.26 itojun if (!p) {
314 1.26 itojun (void)printf("not enough core.\n");
315 1.30 christos pwerror(NULL, 0, 0);
316 1.26 itojun }
317 1.26 itojun return (p);
318 1.1 brezak }
319 1.1 brezak
320 1.21 mjl static int
321 1.1 brezak ypgetpwnam(nam)
322 1.22 aidan const char *nam;
323 1.1 brezak {
324 1.4 deraadt char *val;
325 1.4 deraadt int reason, vallen;
326 1.4 deraadt
327 1.14 lukem val = NULL;
328 1.4 deraadt reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
329 1.4 deraadt &val, &vallen);
330 1.14 lukem if (reason != 0) {
331 1.14 lukem if (val != NULL)
332 1.14 lukem free(val);
333 1.21 mjl return 0;
334 1.4 deraadt }
335 1.4 deraadt free(val);
336 1.21 mjl return 1;
337 1.1 brezak }
338 1.1 brezak
339 1.1 brezak #endif /* YP */
340