libexec exec fix
[minix.git] / usr.bin / passwd / yp_passwd.c
blob0d11ff1a308f0ff5d57fa32c2263cf7612bd9eac
1 /* $NetBSD: yp_passwd.c,v 1.35 2010/09/08 13:58:46 christos Exp $ */
3 /*
4 * Copyright (c) 1988, 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "from: @(#)local_passwd.c 8.3 (Berkeley) 4/2/94";
36 #else
37 __RCSID("$NetBSD: yp_passwd.c,v 1.35 2010/09/08 13:58:46 christos Exp $");
38 #endif
39 #endif /* not lint */
41 #ifdef YP
43 #include <ctype.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <netdb.h>
47 #include <pwd.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <time.h>
52 #include <unistd.h>
53 #include <limits.h>
54 #include <util.h>
56 #include <rpc/rpc.h>
57 #include <rpcsvc/yp_prot.h>
58 #include <rpcsvc/ypclnt.h>
60 #include "extern.h"
62 #define passwd yp_passwd_rec
63 #include <rpcsvc/yppasswd.h>
64 #undef passwd
66 #ifndef _PASSWORD_LEN
67 #define _PASSWORD_LEN PASS_MAX
68 #endif
70 static uid_t uid;
71 static char *domain;
73 static void
74 pwerror(const char *name, int show_err, int eval)
77 if (show_err)
78 warn("%s", name);
79 errx(eval, "NIS passwd database unchanged");
82 static char *
83 getnewpasswd(struct passwd *pw, char **old_pass)
85 int tries;
86 const char *p, *t;
87 char *result;
88 static char buf[_PASSWORD_LEN + 1];
89 char salt[_PASSWORD_LEN + 1];
90 char option[LINE_MAX], *key, *opt;
92 (void)printf("Changing NIS password for %s.\n", pw->pw_name);
94 if (old_pass) {
95 *old_pass = NULL;
97 if (pw->pw_passwd[0]) {
98 if (strcmp(crypt(p = getpass("Old password:"),
99 pw->pw_passwd), pw->pw_passwd)) {
100 (void)printf("Sorry.\n");
101 pwerror(NULL, 0, 1);
103 } else {
104 p = "";
107 *old_pass = strdup(p);
108 if (!*old_pass) {
109 (void)printf("not enough core.\n");
110 pwerror(NULL, 0, 1);
113 for (buf[0] = '\0', tries = 0;;) {
114 p = getpass("New password:");
115 if (!*p) {
116 (void)printf("Password unchanged.\n");
117 pwerror(NULL, 0, 0);
119 if (strlen(p) <= 5 && ++tries < 2) {
120 (void)printf("Please enter a longer password.\n");
121 continue;
123 for (t = p; *t && islower((unsigned char)*t); ++t);
124 if (!*t && ++tries < 2) {
125 (void)printf("Please don't use an all-lower case "
126 "password.\nUnusual capitalization, "
127 "control characters or digits are "
128 "suggested.\n");
129 continue;
131 (void)strlcpy(buf, p, sizeof(buf));
132 if (!strcmp(buf, getpass("Retype new password:")))
133 break;
134 (void)printf("Mismatch; try again, EOF to quit.\n");
137 pw_getpwconf(option, sizeof(option), pw, "ypcipher");
138 opt = option;
139 key = strsep(&opt, ",");
140 if (pw_gensalt(salt, _PASSWORD_LEN, key, opt) == -1) {
141 warn("Couldn't generate salt");
142 pwerror(NULL, 0, 0);
144 result = strdup(crypt(buf, salt));
145 if (!result) {
146 (void)printf("not enough core.\n");
147 pwerror(NULL, 0, 0);
149 return result;
152 static void
153 makeypp(struct yppasswd *ypp, struct passwd *pw)
155 /* prompt for new password */
156 ypp->newpw.pw_passwd = getnewpasswd(pw, &ypp->oldpass);
158 /* tell rpc.yppasswdd */
159 ypp->newpw.pw_name = estrdup(pw->pw_name);
160 ypp->newpw.pw_uid = pw->pw_uid;
161 ypp->newpw.pw_gid = pw->pw_gid;
162 ypp->newpw.pw_gecos = estrdup(pw->pw_gecos);
163 ypp->newpw.pw_dir = estrdup(pw->pw_dir);
164 ypp->newpw.pw_shell = estrdup(pw->pw_shell);
167 static int
168 ypgetpwnam(const char *nam, struct passwd *pwd)
170 char *val;
171 int reason, vallen, namlen = (int)strlen(nam);
172 int flags;
173 int ok;
175 flags = ok = 0;
176 val = NULL;
177 reason = yp_match(domain, "master.passwd.byname", nam, namlen,
178 &val, &vallen);
179 if (reason == YPERR_MAP) {
180 reason = yp_match(domain, "passwd.byname", nam, namlen,
181 &val, &vallen);
182 flags = _PASSWORD_OLDFMT;
184 if (reason != 0)
185 goto out;
187 if (pw_scan(val, pwd, &flags) == 0)
188 goto out;
190 ok = 1;
191 val = NULL; /* Don't free the memory, it is still in use */
192 out:
193 if (val)
194 free(val);
195 return ok;
198 #ifdef USE_PAM
200 void
201 pwyp_usage(const char *prefix)
204 (void)fprintf(stderr, "%s %s [-d nis | -y] [user]\n",
205 prefix, getprogname());
208 void
209 pwyp_argv0_usage(const char *prefix)
212 (void)fprintf(stderr, "%s %s [user]\n",
213 prefix, getprogname());
216 void
217 pwyp_process(const char *username, int argc, char **argv)
219 char *master;
220 int ch, r, rpcport, status;
221 enum clnt_stat yr;
222 struct yppasswd ypp;
223 struct passwd pwb, pwb2, *pw;
224 char pwbuf[1024];
225 struct timeval tv;
226 CLIENT *client;
228 while ((ch = getopt(argc, argv, "y")) != -1) {
229 switch (ch) {
230 case 'y':
232 * Abosrb the -y that may have gotten us here.
234 break;
236 default:
237 usage();
238 /* NOTREACHED */
242 argc -= optind;
243 argv += optind;
245 switch (argc) {
246 case 0:
247 /* username already provided */
248 break;
249 case 1:
250 username = argv[0];
251 break;
252 default:
253 usage();
254 /*NOTREACHED*/
257 if (_yp_check(NULL) == 0) {
258 /* can't use YP. */
259 errx(EXIT_FAILURE, "NIS not in use.");
262 uid = getuid();
265 * Get local domain
267 if ((r = yp_get_default_domain(&domain)) != 0)
268 errx(EXIT_FAILURE, "Can't get local NIS domain (%s)",
269 yperr_string(r));
272 * Find the host for the passwd map; it should be running
273 * the daemon.
275 if ((r = yp_master(domain, "passwd.byname", &master)) != 0)
276 errx(EXIT_FAILURE, "Can't find the master NIS server (%s)",
277 yperr_string(r));
280 * Ask the portmapper for the port of the daemon.
282 if ((rpcport = getrpcport(master, YPPASSWDPROG,
283 YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0)
284 errx(EXIT_FAILURE, "Master NIS server not running yppasswd "
285 "daemon");
288 * Be sure the port is privileged
290 if (rpcport >= IPPORT_RESERVED)
291 errx(EXIT_FAILURE, "Yppasswd daemon is on an invalid port");
293 /* Bail out if this is a local (non-yp) user, */
294 /* then get user's login identity */
295 if (!ypgetpwnam(username, &pwb) ||
296 getpwnam_r(username, &pwb2, pwbuf, sizeof(pwbuf), &pw) ||
297 pw == NULL)
298 errx(EXIT_FAILURE, "NIS unknown user %s", username);
300 if (uid && uid != pwb.pw_uid) {
301 errno = EACCES;
302 err(EXIT_FAILURE, "You may only change your own password");
305 makeypp(&ypp, &pwb);
307 client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
308 if (client == NULL)
309 errx(EXIT_FAILURE, "Cannot contact yppasswdd on %s (%s)",
310 master, yperr_string(YPERR_YPBIND));
312 client->cl_auth = authunix_create_default();
313 tv.tv_sec = 2;
314 tv.tv_usec = 0;
315 yr = clnt_call(client, YPPASSWDPROC_UPDATE,
316 xdr_yppasswd, &ypp, xdr_int, &status, tv);
317 if (yr != RPC_SUCCESS)
318 errx(EXIT_FAILURE, "RPC to yppasswdd failed (%s)",
319 clnt_sperrno(yr));
320 else if (status)
321 printf("Couldn't change NIS password.\n");
322 else
323 printf("The NIS password has been changed on %s, %s\n",
324 master, "the master NIS passwd server.");
327 #else /* ! USE_PAM */
329 static int yflag;
332 yp_init(progname)
333 const char *progname;
335 int yppwd;
337 if (strcmp(progname, "yppasswd") == 0) {
338 yppwd = 1;
339 } else
340 yppwd = 0;
341 yflag = 0;
342 if (_yp_check(NULL) == 0) {
343 /* can't use YP. */
344 if (yppwd)
345 errx(EXIT_FAILURE, "NIS not in use");
346 return -1;
348 return 0;
352 yp_arg(char ch, const char *arg)
354 switch (ch) {
355 case 'y':
356 yflag = 1;
357 break;
358 default:
359 return 0;
361 return 1;
365 yp_arg_end(void)
367 if (yflag)
368 return PW_USE_FORCE;
369 return PW_USE;
372 void
373 yp_end(void)
375 /* NOOP */
379 yp_chpw(const char *username)
381 char *master;
382 int r, rpcport, status;
383 enum clnt_stat yr;
384 struct yppasswd ypp;
385 struct passwd *pw, pwb;
386 char pwbuf[1024];
387 struct timeval tv;
388 CLIENT *client;
390 uid = getuid();
393 * Get local domain
395 if ((r = yp_get_default_domain(&domain)) != 0)
396 errx(EXIT_FAILURE, "can't get local NIS domain. Reason: %s",
397 yperr_string(r));
400 * Find the host for the passwd map; it should be running
401 * the daemon.
403 if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
404 warnx("can't find the master NIS server. Reason: %s",
405 yperr_string(r));
406 /* continuation */
407 return -1;
411 * Ask the portmapper for the port of the daemon.
413 if ((rpcport = getrpcport(master, YPPASSWDPROG,
414 YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) {
415 warnx("Master NIS server not running yppasswd daemon");
416 /* continuation */
417 return -1;
421 * Be sure the port is privileged
423 if (rpcport >= IPPORT_RESERVED)
424 errx(EXIT_FAILURE, "Yppasswd daemon is on an invalid port");
426 /* Bail out if this is a local (non-yp) user, */
427 /* then get user's login identity */
428 if (!ypgetpwnam(username, pw = &pwb) ||
429 getpwnam_r(username, &pwb, pwbuf, sizeof(pwbuf), &pw) ||
430 pw == NULL) {
431 warnx("NIS unknown user %s", username);
432 /* continuation */
433 return -1;
436 if (uid && uid != pw->pw_uid) {
437 errno = EACCES;
438 err(EXIT_FAILURE, "You may only change your own password");
441 makeypp(&ypp, pw);
443 client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
444 if (client == NULL) {
445 warnx("Cannot contact yppasswdd on %s (%s)",
446 master, yperr_string(YPERR_YPBIND));
447 return YPERR_YPBIND;
450 client->cl_auth = authunix_create_default();
451 tv.tv_sec = 2;
452 tv.tv_usec = 0;
453 yr = clnt_call(client, YPPASSWDPROC_UPDATE,
454 xdr_yppasswd, &ypp, xdr_int, &status, tv);
455 if (yr != RPC_SUCCESS)
456 errx(EXIT_FAILURE, "RPC to yppasswdd failed (%s)",
457 clnt_sperrno(yr));
458 else if (status)
459 printf("Couldn't change NIS password.\n");
460 else
461 printf("The NIS password has been changed on %s, %s\n",
462 master, "the master NIS passwd server.");
463 return 0;
466 #endif /* USE_PAM */
468 #endif /* YP */