Home | History | Annotate | Line # | Download | only in passwd
yp_passwd.c revision 1.34
      1 /*	$NetBSD: yp_passwd.c,v 1.34 2010/09/08 13:44:44 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 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. 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.
     18  *
     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.
     30  */
     31 
     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.34 2010/09/08 13:44:44 christos Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #ifdef	YP
     42 
     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>
     55 
     56 #include <rpc/rpc.h>
     57 #include <rpcsvc/yp_prot.h>
     58 #include <rpcsvc/ypclnt.h>
     59 
     60 #include "extern.h"
     61 
     62 #define passwd yp_passwd_rec
     63 #include <rpcsvc/yppasswd.h>
     64 #undef passwd
     65 
     66 #ifndef _PASSWORD_LEN
     67 #define _PASSWORD_LEN PASS_MAX
     68 #endif
     69 
     70 static uid_t uid;
     71 static char *domain;
     72 
     73 static void
     74 pwerror(const char *name, int show_err, int eval)
     75 {
     76 
     77 	if (show_err)
     78 		warn("%s", name);
     79 	errx(eval, "NIS passwd database unchanged");
     80 }
     81 
     82 static char *
     83 getnewpasswd(struct passwd *pw, char **old_pass)
     84 {
     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;
     91 
     92 	(void)printf("Changing NIS password for %s.\n", pw->pw_name);
     93 
     94 	if (old_pass) {
     95 		*old_pass = NULL;
     96 
     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);
    102 			}
    103 		} else {
    104 			p = "";
    105 		}
    106 
    107 		*old_pass = strdup(p);
    108 		if (!*old_pass) {
    109 			(void)printf("not enough core.\n");
    110 			pwerror(NULL, 0, 1);
    111 		}
    112 	}
    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);
    118 		}
    119 		if (strlen(p) <= 5 && ++tries < 2) {
    120 			(void)printf("Please enter a longer password.\n");
    121 			continue;
    122 		}
    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;
    130 		}
    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");
    135 	}
    136 
    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);
    143 	}
    144 	result = strdup(crypt(buf, salt));
    145 	if (!result) {
    146 		(void)printf("not enough core.\n");
    147 		pwerror(NULL, 0, 0);
    148 	}
    149 	return (result);
    150 }
    151 
    152 static void
    153 makeypp(struct yppasswd *ypp, struct passwd *pw)
    154 {
    155 	/* prompt for new password */
    156 	ypp->newpw.pw_passwd = getnewpasswd(pw, &ypp->oldpass);
    157 
    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);
    165 }
    166 
    167 static int
    168 ypgetpwnam(const char *nam, struct passwd *pwd)
    169 {
    170 	char *val;
    171 	int reason, vallen, namlen = (int)strlen(nam);
    172 	int flags = 0;
    173 	int ok = 0;
    174 
    175 	val = NULL;
    176 	reason = yp_match(domain, "master.passwd.byname", nam, namlen,
    177                           &val, &vallen);
    178 	if (reason == YPERR_MAP) {
    179 		reason = yp_match(domain, "passwd.byname", nam, namlen,
    180 				  &val, &vallen);
    181 		flags = _PASSWORD_OLDFMT;
    182 	}
    183 	if (reason != 0)
    184 		goto out;
    185 
    186 	if (pw_scan(val, pwd, &flags) == 0)
    187 		goto out;
    188 
    189 	ok = 1;
    190 	val = NULL;	/* Don't free the memory, it is still in use */
    191 out:
    192 	if (val)
    193 		free(val);
    194 	return ok;
    195 }
    196 
    197 #ifdef USE_PAM
    198 
    199 void
    200 pwyp_usage(const char *prefix)
    201 {
    202 
    203 	(void) fprintf(stderr, "%s %s [-d nis | -y] [user]\n",
    204 	    prefix, getprogname());
    205 }
    206 
    207 void
    208 pwyp_argv0_usage(const char *prefix)
    209 {
    210 
    211 	(void) fprintf(stderr, "%s %s [user]\n",
    212 	    prefix, getprogname());
    213 }
    214 
    215 void
    216 pwyp_process(const char *username, int argc, char **argv)
    217 {
    218 	char *master;
    219 	int ch, r, rpcport, status;
    220 	struct yppasswd ypp;
    221 	struct passwd pwb, pwb2, *pw;
    222 	char pwbuf[1024];
    223 	struct timeval tv;
    224 	CLIENT *client;
    225 
    226 	while ((ch = getopt(argc, argv, "y")) != -1) {
    227 		switch (ch) {
    228 		case 'y':
    229 			/*
    230 			 * Abosrb the -y that may have gotten us here.
    231 			 */
    232 			break;
    233 
    234 		default:
    235 			usage();
    236 			/* NOTREACHED */
    237 		}
    238 	}
    239 
    240 	argc -= optind;
    241 	argv += optind;
    242 
    243 	switch (argc) {
    244 	case 0:
    245 		/* username already provided */
    246 		break;
    247 	case 1:
    248 		username = argv[0];
    249 		break;
    250 	default:
    251 		usage();
    252 		/* NOTREACHED */
    253 	}
    254 
    255 	if (_yp_check(NULL) == 0) {
    256 		/* can't use YP. */
    257 		errx(1, "NIS not in use.");
    258 	}
    259 
    260 	uid = getuid();
    261 
    262 	/*
    263 	 * Get local domain
    264 	 */
    265 	if ((r = yp_get_default_domain(&domain)) != 0)
    266 		errx(1, "can't get local NIS domain.  Reason: %s",
    267 		    yperr_string(r));
    268 
    269 	/*
    270 	 * Find the host for the passwd map; it should be running
    271 	 * the daemon.
    272 	 */
    273 	if ((r = yp_master(domain, "passwd.byname", &master)) != 0)
    274 		errx(1, "can't find the master NIS server. Reason: %s",
    275 		    yperr_string(r));
    276 
    277 	/*
    278 	 * Ask the portmapper for the port of the daemon.
    279 	 */
    280 	if ((rpcport = getrpcport(master, YPPASSWDPROG,
    281 	    YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0)
    282 		errx(1, "master NIS server not running yppasswd daemon.\n\t%s\n",
    283 		    "Can't change NIS password.");
    284 
    285 	/*
    286 	 * Be sure the port is privileged
    287 	 */
    288 	if (rpcport >= IPPORT_RESERVED)
    289 		errx(1, "yppasswd daemon is on an invalid port.");
    290 
    291 	/* Bail out if this is a local (non-yp) user, */
    292 	/* then get user's login identity */
    293 	if (!ypgetpwnam(username, &pwb) ||
    294 	    getpwnam_r(username, &pwb2, pwbuf, sizeof(pwbuf), &pw) ||
    295 	    pw == NULL)
    296 		errx(1, "NIS unknown user %s", username);
    297 
    298 	if (uid && uid != pwb.pw_uid)
    299 		errx(1, "you may only change your own password: %s",
    300 		    strerror(EACCES));
    301 
    302 	makeypp(&ypp, &pwb);
    303 
    304 	client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
    305 	if (client == NULL)
    306 		errx(1, "cannot contact yppasswdd on %s:  Reason: %s",
    307 		    master, yperr_string(YPERR_YPBIND));
    308 
    309 	client->cl_auth = authunix_create_default();
    310 	tv.tv_sec = 2;
    311 	tv.tv_usec = 0;
    312 	r = clnt_call(client, YPPASSWDPROC_UPDATE,
    313 	    xdr_yppasswd, &ypp, xdr_int, &status, tv);
    314 	if (r)
    315 		errx(1, "rpc to yppasswdd failed.");
    316 	else if (status)
    317 		printf("Couldn't change NIS password.\n");
    318 	else
    319 		printf("The NIS password has been changed on %s, %s\n",
    320 		    master, "the master NIS passwd server.");
    321 }
    322 
    323 #else /* ! USE_PAM */
    324 
    325 static	int yflag;
    326 
    327 int
    328 yp_init(progname)
    329 	const char *progname;
    330 {
    331 	int yppwd;
    332 
    333 	if (strcmp(progname, "yppasswd") == 0) {
    334 		yppwd = 1;
    335 	} else
    336 		yppwd = 0;
    337 	yflag = 0;
    338 	if (_yp_check(NULL) == 0) {
    339 		/* can't use YP. */
    340 		if (yppwd)
    341 			errx(1, "NIS not in use.");
    342 		return(-1);
    343 	}
    344 	return (0);
    345 }
    346 
    347 int
    348 yp_arg(ch, arg)
    349 	char ch;
    350 	const char *arg;
    351 {
    352 	switch (ch) {
    353 	case 'y':
    354 		yflag = 1;
    355 		break;
    356 	default:
    357 		return(0);
    358 	}
    359 	return(1);
    360 }
    361 
    362 int
    363 yp_arg_end()
    364 {
    365 	if (yflag)
    366 		return (PW_USE_FORCE);
    367 	return (PW_USE);
    368 }
    369 
    370 void
    371 yp_end()
    372 {
    373 	/* NOOP */
    374 }
    375 
    376 int
    377 yp_chpw(username)
    378 	const char *username;
    379 {
    380 	char *master;
    381 	int r, rpcport, status;
    382 	struct yppasswd ypp;
    383 	struct passwd *pw, pwb;
    384 	char pwbuf[1024];
    385 	struct timeval tv;
    386 	CLIENT *client;
    387 
    388 	uid = getuid();
    389 
    390 	/*
    391 	 * Get local domain
    392 	 */
    393 	if ((r = yp_get_default_domain(&domain)) != 0)
    394 		errx(1, "can't get local NIS domain.  Reason: %s",
    395 		    yperr_string(r));
    396 
    397 	/*
    398 	 * Find the host for the passwd map; it should be running
    399 	 * the daemon.
    400 	 */
    401 	if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
    402 		warnx("can't find the master NIS server.  Reason: %s",
    403 		    yperr_string(r));
    404 		/* continuation */
    405 		return(-1);
    406 	}
    407 
    408 	/*
    409 	 * Ask the portmapper for the port of the daemon.
    410 	 */
    411 	if ((rpcport = getrpcport(master, YPPASSWDPROG,
    412 	    YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) {
    413 		warnx("master NIS server not running yppasswd daemon.\n\t%s\n",
    414 		    "Can't change NIS password.");
    415 		/* continuation */
    416 		return(-1);
    417 	}
    418 
    419 	/*
    420 	 * Be sure the port is privileged
    421 	 */
    422 	if (rpcport >= IPPORT_RESERVED)
    423 		errx(1, "yppasswd daemon is on an invalid port.");
    424 
    425 	/* Bail out if this is a local (non-yp) user, */
    426 	/* then get user's login identity */
    427 	if (!ypgetpwnam(username, pw = &pwb) ||
    428 	    getpwnam_r(username, &pwb, pwbuf, sizeof(pwbuf), &pw) ||
    429 	    pw == NULL) {
    430 		warnx("NIS unknown user %s", username);
    431 		/* continuation */
    432 		return(-1);
    433 	}
    434 
    435 	if (uid && uid != pw->pw_uid)
    436 		errx(1, "you may only change your own password: %s",
    437 		    strerror(EACCES));
    438 
    439 	makeypp(&ypp, pw);
    440 
    441 	client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
    442 	if (client == NULL) {
    443 		warnx("cannot contact yppasswdd on %s:  Reason: %s",
    444 		    master, yperr_string(YPERR_YPBIND));
    445 		return (YPERR_YPBIND);
    446 	}
    447 
    448 	client->cl_auth = authunix_create_default();
    449 	tv.tv_sec = 2;
    450 	tv.tv_usec = 0;
    451 	r = clnt_call(client, YPPASSWDPROC_UPDATE,
    452 	    xdr_yppasswd, &ypp, xdr_int, &status, tv);
    453 	if (r)
    454 		errx(1, "rpc to yppasswdd failed.");
    455 	else if (status)
    456 		printf("Couldn't change NIS password.\n");
    457 	else
    458 		printf("The NIS password has been changed on %s, %s\n",
    459 		    master, "the master NIS passwd server.");
    460 	return(0);
    461 }
    462 
    463 #endif /* USE_PAM */
    464 
    465 #endif	/* YP */
    466