yp_passwd.c revision 1.32 1 1.32 christos /* $NetBSD: yp_passwd.c,v 1.32 2008/01/25 19:36:12 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.32 christos __RCSID("$NetBSD: yp_passwd.c,v 1.32 2008/01/25 19:36:12 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.1 brezak static uid_t uid;
71 1.31 thorpej static char *domain;
72 1.1 brezak
73 1.15 lukem static void
74 1.31 thorpej pwerror(char *name, int err, int eval)
75 1.4 deraadt {
76 1.17 mrg
77 1.15 lukem if (err)
78 1.18 mrg warn("%s", name);
79 1.31 thorpej errx(eval, "NIS passwd database unchanged");
80 1.31 thorpej }
81 1.31 thorpej
82 1.31 thorpej static char *
83 1.31 thorpej getnewpasswd(struct passwd *pw, char **old_pass)
84 1.31 thorpej {
85 1.31 thorpej int tries;
86 1.31 thorpej char *p, *t;
87 1.31 thorpej static char buf[_PASSWORD_LEN+1];
88 1.31 thorpej char salt[_PASSWORD_LEN+1];
89 1.31 thorpej char option[LINE_MAX], *key, *opt;
90 1.31 thorpej
91 1.31 thorpej (void)printf("Changing NIS password for %s.\n", pw->pw_name);
92 1.31 thorpej
93 1.31 thorpej if (old_pass) {
94 1.31 thorpej *old_pass = NULL;
95 1.31 thorpej
96 1.31 thorpej if (pw->pw_passwd[0]) {
97 1.31 thorpej if (strcmp(crypt(p = getpass("Old password:"),
98 1.31 thorpej pw->pw_passwd), pw->pw_passwd)) {
99 1.31 thorpej (void)printf("Sorry.\n");
100 1.31 thorpej pwerror(NULL, 0, 1);
101 1.31 thorpej }
102 1.31 thorpej } else {
103 1.31 thorpej p = "";
104 1.31 thorpej }
105 1.31 thorpej
106 1.31 thorpej *old_pass = strdup(p);
107 1.31 thorpej if (!*old_pass) {
108 1.31 thorpej (void)printf("not enough core.\n");
109 1.31 thorpej pwerror(NULL, 0, 1);
110 1.31 thorpej }
111 1.31 thorpej }
112 1.31 thorpej for (buf[0] = '\0', tries = 0;;) {
113 1.31 thorpej p = getpass("New password:");
114 1.31 thorpej if (!*p) {
115 1.31 thorpej (void)printf("Password unchanged.\n");
116 1.31 thorpej pwerror(NULL, 0, 0);
117 1.31 thorpej }
118 1.31 thorpej if (strlen(p) <= 5 && ++tries < 2) {
119 1.31 thorpej (void)printf("Please enter a longer password.\n");
120 1.31 thorpej continue;
121 1.31 thorpej }
122 1.31 thorpej for (t = p; *t && islower((unsigned char)*t); ++t);
123 1.31 thorpej if (!*t && ++tries < 2) {
124 1.31 thorpej (void)printf("Please don't use an all-lower case "
125 1.31 thorpej "password.\nUnusual capitalization, "
126 1.31 thorpej "control characters or digits are "
127 1.31 thorpej "suggested.\n");
128 1.31 thorpej continue;
129 1.31 thorpej }
130 1.31 thorpej (void)strlcpy(buf, p, sizeof(buf));
131 1.31 thorpej if (!strcmp(buf, getpass("Retype new password:")))
132 1.31 thorpej break;
133 1.31 thorpej (void)printf("Mismatch; try again, EOF to quit.\n");
134 1.31 thorpej }
135 1.31 thorpej
136 1.31 thorpej pw_getpwconf(option, sizeof(option), pw, "ypcipher");
137 1.31 thorpej opt = option;
138 1.31 thorpej key = strsep(&opt, ",");
139 1.31 thorpej if (pw_gensalt(salt, _PASSWORD_LEN, key, opt) == -1) {
140 1.31 thorpej warn("Couldn't generate salt");
141 1.31 thorpej pwerror(NULL, 0, 0);
142 1.31 thorpej }
143 1.31 thorpej p = strdup(crypt(buf, salt));
144 1.31 thorpej if (!p) {
145 1.31 thorpej (void)printf("not enough core.\n");
146 1.31 thorpej pwerror(NULL, 0, 0);
147 1.31 thorpej }
148 1.31 thorpej return (p);
149 1.31 thorpej }
150 1.31 thorpej
151 1.32 christos static void
152 1.32 christos makeypp(struct yppasswd *ypp, struct passwd *pw)
153 1.32 christos {
154 1.32 christos /* prompt for new password */
155 1.32 christos ypp->newpw.pw_passwd = getnewpasswd(pw, &ypp->oldpass);
156 1.32 christos
157 1.32 christos /* tell rpc.yppasswdd */
158 1.32 christos ypp->newpw.pw_name = estrdup(pw->pw_name);
159 1.32 christos ypp->newpw.pw_uid = pw->pw_uid;
160 1.32 christos ypp->newpw.pw_gid = pw->pw_gid;
161 1.32 christos ypp->newpw.pw_gecos = estrdup(pw->pw_gecos);
162 1.32 christos ypp->newpw.pw_dir = estrdup(pw->pw_dir);
163 1.32 christos ypp->newpw.pw_shell = estrdup(pw->pw_shell);
164 1.32 christos }
165 1.32 christos
166 1.31 thorpej static int
167 1.32 christos ypgetpwnam(const char *nam, struct passwd *pwd)
168 1.31 thorpej {
169 1.31 thorpej char *val;
170 1.31 thorpej int reason, vallen;
171 1.32 christos int flags;
172 1.32 christos int ok = 0;
173 1.31 thorpej
174 1.31 thorpej val = NULL;
175 1.32 christos reason = yp_match(domain, "passwd.byname", nam, (int)strlen(nam),
176 1.31 thorpej &val, &vallen);
177 1.32 christos if (reason != 0)
178 1.32 christos goto out;
179 1.32 christos
180 1.32 christos flags = _PASSWORD_OLDFMT;
181 1.32 christos if (pw_scan(val, pwd, &flags) == 0)
182 1.32 christos goto out;
183 1.32 christos
184 1.32 christos ok = 1;
185 1.32 christos out:
186 1.32 christos if (val)
187 1.32 christos free(val);
188 1.32 christos return ok;
189 1.31 thorpej }
190 1.31 thorpej
191 1.31 thorpej #ifdef USE_PAM
192 1.31 thorpej
193 1.31 thorpej void
194 1.31 thorpej pwyp_usage(const char *prefix)
195 1.31 thorpej {
196 1.31 thorpej
197 1.31 thorpej (void) fprintf(stderr, "%s %s [-d nis | -y] [user]\n",
198 1.31 thorpej prefix, getprogname());
199 1.31 thorpej }
200 1.31 thorpej
201 1.31 thorpej void
202 1.31 thorpej pwyp_argv0_usage(const char *prefix)
203 1.31 thorpej {
204 1.31 thorpej
205 1.31 thorpej (void) fprintf(stderr, "%s %s [user]\n",
206 1.31 thorpej prefix, getprogname());
207 1.31 thorpej }
208 1.31 thorpej
209 1.31 thorpej void
210 1.31 thorpej pwyp_process(const char *username, int argc, char **argv)
211 1.31 thorpej {
212 1.31 thorpej char *master;
213 1.31 thorpej int ch, r, rpcport, status;
214 1.32 christos struct yppasswd ypp;
215 1.32 christos struct passwd pwb, *pw;
216 1.32 christos char pwbuf[1024];
217 1.31 thorpej struct timeval tv;
218 1.31 thorpej CLIENT *client;
219 1.31 thorpej
220 1.31 thorpej while ((ch = getopt(argc, argv, "y")) != -1) {
221 1.31 thorpej switch (ch) {
222 1.31 thorpej case 'y':
223 1.31 thorpej /*
224 1.31 thorpej * Abosrb the -y that may have gotten us here.
225 1.31 thorpej */
226 1.31 thorpej break;
227 1.31 thorpej
228 1.31 thorpej default:
229 1.31 thorpej usage();
230 1.31 thorpej /* NOTREACHED */
231 1.31 thorpej }
232 1.31 thorpej }
233 1.31 thorpej
234 1.31 thorpej argc -= optind;
235 1.31 thorpej argv += optind;
236 1.31 thorpej
237 1.31 thorpej switch (argc) {
238 1.31 thorpej case 0:
239 1.31 thorpej /* username already provided */
240 1.31 thorpej break;
241 1.31 thorpej case 1:
242 1.31 thorpej username = argv[0];
243 1.31 thorpej break;
244 1.31 thorpej default:
245 1.31 thorpej usage();
246 1.31 thorpej /* NOTREACHED */
247 1.31 thorpej }
248 1.31 thorpej
249 1.31 thorpej if (_yp_check(NULL) == 0) {
250 1.31 thorpej /* can't use YP. */
251 1.31 thorpej errx(1, "NIS not in use.");
252 1.31 thorpej }
253 1.31 thorpej
254 1.31 thorpej uid = getuid();
255 1.31 thorpej
256 1.31 thorpej /*
257 1.31 thorpej * Get local domain
258 1.31 thorpej */
259 1.31 thorpej if ((r = yp_get_default_domain(&domain)) != 0)
260 1.31 thorpej errx(1, "can't get local NIS domain. Reason: %s",
261 1.31 thorpej yperr_string(r));
262 1.31 thorpej
263 1.31 thorpej /*
264 1.31 thorpej * Find the host for the passwd map; it should be running
265 1.31 thorpej * the daemon.
266 1.31 thorpej */
267 1.31 thorpej if ((r = yp_master(domain, "passwd.byname", &master)) != 0)
268 1.31 thorpej errx(1, "can't find the master NIS server. Reason: %s",
269 1.31 thorpej yperr_string(r));
270 1.31 thorpej
271 1.31 thorpej /*
272 1.31 thorpej * Ask the portmapper for the port of the daemon.
273 1.31 thorpej */
274 1.31 thorpej if ((rpcport = getrpcport(master, YPPASSWDPROG,
275 1.31 thorpej YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0)
276 1.31 thorpej errx(1, "master NIS server not running yppasswd daemon.\n\t%s\n",
277 1.31 thorpej "Can't change NIS password.");
278 1.31 thorpej
279 1.31 thorpej /*
280 1.31 thorpej * Be sure the port is privileged
281 1.31 thorpej */
282 1.31 thorpej if (rpcport >= IPPORT_RESERVED)
283 1.31 thorpej errx(1, "yppasswd daemon is on an invalid port.");
284 1.31 thorpej
285 1.31 thorpej /* Bail out if this is a local (non-yp) user, */
286 1.31 thorpej /* then get user's login identity */
287 1.32 christos if (!ypgetpwnam(username, pw = &pwb) ||
288 1.32 christos getpwnam_r(username, &pwb, pwbuf, sizeof(pwbuf), &pw) ||
289 1.32 christos pw == NULL)
290 1.31 thorpej errx(1, "NIS unknown user %s", username);
291 1.31 thorpej
292 1.31 thorpej if (uid && uid != pw->pw_uid)
293 1.31 thorpej errx(1, "you may only change your own password: %s",
294 1.31 thorpej strerror(EACCES));
295 1.31 thorpej
296 1.32 christos makeypp(&ypp, pw);
297 1.31 thorpej
298 1.31 thorpej client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
299 1.31 thorpej if (client == NULL)
300 1.31 thorpej errx(1, "cannot contact yppasswdd on %s: Reason: %s",
301 1.31 thorpej master, yperr_string(YPERR_YPBIND));
302 1.31 thorpej
303 1.31 thorpej client->cl_auth = authunix_create_default();
304 1.31 thorpej tv.tv_sec = 2;
305 1.31 thorpej tv.tv_usec = 0;
306 1.31 thorpej r = clnt_call(client, YPPASSWDPROC_UPDATE,
307 1.32 christos xdr_yppasswd, &ypp, xdr_int, &status, tv);
308 1.31 thorpej if (r)
309 1.31 thorpej errx(1, "rpc to yppasswdd failed.");
310 1.31 thorpej else if (status)
311 1.31 thorpej printf("Couldn't change NIS password.\n");
312 1.31 thorpej else
313 1.31 thorpej printf("The NIS password has been changed on %s, %s\n",
314 1.31 thorpej master, "the master NIS passwd server.");
315 1.4 deraadt }
316 1.4 deraadt
317 1.31 thorpej #else /* ! USE_PAM */
318 1.31 thorpej
319 1.31 thorpej static int yflag;
320 1.31 thorpej
321 1.22 aidan int
322 1.22 aidan yp_init(progname)
323 1.22 aidan const char *progname;
324 1.22 aidan {
325 1.22 aidan int yppwd;
326 1.22 aidan
327 1.22 aidan if (strcmp(progname, "yppasswd") == 0) {
328 1.22 aidan yppwd = 1;
329 1.22 aidan } else
330 1.22 aidan yppwd = 0;
331 1.22 aidan yflag = 0;
332 1.22 aidan if (_yp_check(NULL) == 0) {
333 1.22 aidan /* can't use YP. */
334 1.22 aidan if (yppwd)
335 1.31 thorpej errx(1, "NIS not in use.");
336 1.22 aidan return(-1);
337 1.22 aidan }
338 1.22 aidan return (0);
339 1.22 aidan }
340 1.22 aidan
341 1.22 aidan int
342 1.22 aidan yp_arg(ch, arg)
343 1.22 aidan char ch;
344 1.22 aidan const char *arg;
345 1.22 aidan {
346 1.22 aidan switch (ch) {
347 1.22 aidan case 'y':
348 1.22 aidan yflag = 1;
349 1.22 aidan break;
350 1.22 aidan default:
351 1.22 aidan return(0);
352 1.22 aidan }
353 1.22 aidan return(1);
354 1.22 aidan }
355 1.22 aidan
356 1.22 aidan int
357 1.22 aidan yp_arg_end()
358 1.16 tv {
359 1.22 aidan if (yflag)
360 1.22 aidan return (PW_USE_FORCE);
361 1.22 aidan return (PW_USE);
362 1.22 aidan }
363 1.17 mrg
364 1.22 aidan void
365 1.22 aidan yp_end()
366 1.22 aidan {
367 1.22 aidan /* NOOP */
368 1.16 tv }
369 1.16 tv
370 1.9 thorpej int
371 1.22 aidan yp_chpw(username)
372 1.22 aidan const char *username;
373 1.1 brezak {
374 1.4 deraadt char *master;
375 1.4 deraadt int r, rpcport, status;
376 1.32 christos struct yppasswd ypp;
377 1.32 christos struct passwd *pw, pwb;
378 1.32 christos char pwbuf[1024];
379 1.1 brezak struct timeval tv;
380 1.1 brezak CLIENT *client;
381 1.21 mjl
382 1.4 deraadt uid = getuid();
383 1.4 deraadt
384 1.4 deraadt /*
385 1.4 deraadt * Get local domain
386 1.4 deraadt */
387 1.22 aidan if ((r = yp_get_default_domain(&domain)) != 0)
388 1.31 thorpej errx(1, "can't get local NIS domain. Reason: %s",
389 1.9 thorpej yperr_string(r));
390 1.4 deraadt
391 1.4 deraadt /*
392 1.4 deraadt * Find the host for the passwd map; it should be running
393 1.4 deraadt * the daemon.
394 1.4 deraadt */
395 1.16 tv if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
396 1.31 thorpej warnx("can't find the master NIS server. Reason: %s",
397 1.9 thorpej yperr_string(r));
398 1.22 aidan /* continuation */
399 1.22 aidan return(-1);
400 1.16 tv }
401 1.1 brezak
402 1.4 deraadt /*
403 1.4 deraadt * Ask the portmapper for the port of the daemon.
404 1.4 deraadt */
405 1.4 deraadt if ((rpcport = getrpcport(master, YPPASSWDPROG,
406 1.10 thorpej YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) {
407 1.31 thorpej warnx("master NIS server not running yppasswd daemon.\n\t%s\n",
408 1.31 thorpej "Can't change NIS password.");
409 1.22 aidan /* continuation */
410 1.22 aidan return(-1);
411 1.10 thorpej }
412 1.1 brezak
413 1.4 deraadt /*
414 1.20 simonb * Be sure the port is privileged
415 1.4 deraadt */
416 1.9 thorpej if (rpcport >= IPPORT_RESERVED)
417 1.9 thorpej errx(1, "yppasswd daemon is on an invalid port.");
418 1.4 deraadt
419 1.21 mjl /* Bail out if this is a local (non-yp) user, */
420 1.21 mjl /* then get user's login identity */
421 1.32 christos if (!ypgetpwnam(username, pw = &pwb) ||
422 1.32 christos getpwnam_r(username, &pwb, pwbuf, sizeof(pwbuf), &pw) ||
423 1.32 christos pw == NULL) {
424 1.31 thorpej warnx("NIS unknown user %s", username);
425 1.22 aidan /* continuation */
426 1.22 aidan return(-1);
427 1.16 tv }
428 1.9 thorpej
429 1.9 thorpej if (uid && uid != pw->pw_uid)
430 1.9 thorpej errx(1, "you may only change your own password: %s",
431 1.9 thorpej strerror(EACCES));
432 1.1 brezak
433 1.32 christos makeypp(&ypp, pw);
434 1.16 tv
435 1.4 deraadt client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
436 1.17 mrg if (client == NULL) {
437 1.9 thorpej warnx("cannot contact yppasswdd on %s: Reason: %s",
438 1.5 deraadt master, yperr_string(YPERR_YPBIND));
439 1.17 mrg return (YPERR_YPBIND);
440 1.4 deraadt }
441 1.9 thorpej
442 1.4 deraadt client->cl_auth = authunix_create_default();
443 1.4 deraadt tv.tv_sec = 2;
444 1.4 deraadt tv.tv_usec = 0;
445 1.4 deraadt r = clnt_call(client, YPPASSWDPROC_UPDATE,
446 1.32 christos xdr_yppasswd, &ypp, xdr_int, &status, tv);
447 1.4 deraadt if (r)
448 1.9 thorpej errx(1, "rpc to yppasswdd failed.");
449 1.4 deraadt else if (status)
450 1.31 thorpej printf("Couldn't change NIS password.\n");
451 1.4 deraadt else
452 1.31 thorpej printf("The NIS password has been changed on %s, %s\n",
453 1.31 thorpej master, "the master NIS passwd server.");
454 1.22 aidan return(0);
455 1.1 brezak }
456 1.1 brezak
457 1.31 thorpej #endif /* USE_PAM */
458 1.1 brezak
459 1.1 brezak #endif /* YP */
460