Home | History | Annotate | Line # | Download | only in passwd
yp_passwd.c revision 1.22
      1 /*	$NetBSD: yp_passwd.c,v 1.22 2000/02/14 04:36:21 aidan 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. 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: yp_passwd.c,v 1.22 2000/02/14 04:36:21 aidan Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #ifdef	YP
     46 
     47 #include <ctype.h>
     48 #include <err.h>
     49 #include <errno.h>
     50 #include <netdb.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 
     58 #include <rpc/rpc.h>
     59 #include <rpcsvc/yp_prot.h>
     60 #include <rpcsvc/ypclnt.h>
     61 
     62 #include "extern.h"
     63 
     64 #define passwd yp_passwd_rec
     65 #include <rpcsvc/yppasswd.h>
     66 #undef passwd
     67 
     68 #ifndef _PASSWORD_LEN
     69 #define _PASSWORD_LEN PASS_MAX
     70 #endif
     71 
     72 extern	char *__progname;		/* from crt0.o */
     73 
     74 static	int yflag;
     75 
     76 static	char		*getnewpasswd __P((struct passwd *, char **));
     77 static	int		 ypgetpwnam __P((const char *));
     78 static	void		 pw_error __P((char *, int, int));
     79 
     80 static uid_t uid;
     81 char *domain;
     82 
     83 static void
     84 pw_error(name, err, eval)
     85 	char *name;
     86 	int err, eval;
     87 {
     88 
     89 	if (err)
     90 		warn("%s", name);
     91 	errx(eval, "YP passwd database unchanged");
     92 }
     93 
     94 int
     95 yp_init(progname)
     96 	const char *progname;
     97 {
     98 	int yppwd;
     99 
    100 	if (strcmp(progname, "yppasswd") == 0) {
    101 		yppwd = 1;
    102 	} else
    103 		yppwd = 0;
    104 	yflag = 0;
    105 	if (_yp_check(NULL) == 0) {
    106 		/* can't use YP. */
    107 		if (yppwd)
    108 			errx(1, "YP not in use.");
    109 		return(-1);
    110 	}
    111 	return (0);
    112 }
    113 
    114 int
    115 yp_arg(ch, arg)
    116 	char ch;
    117 	const char *arg;
    118 {
    119 	switch (ch) {
    120 	case 'y':
    121 		yflag = 1;
    122 		break;
    123 	default:
    124 		return(0);
    125 	}
    126 	return(1);
    127 }
    128 
    129 int
    130 yp_arg_end()
    131 {
    132 	if (yflag)
    133 		return (PW_USE_FORCE);
    134 	return (PW_USE);
    135 }
    136 
    137 void
    138 yp_end()
    139 {
    140 	/* NOOP */
    141 }
    142 
    143 int
    144 yp_chpw(username)
    145 	const char *username;
    146 {
    147 	char *master;
    148 	int r, rpcport, status;
    149 	struct yppasswd yppasswd;
    150 	struct passwd *pw;
    151 	struct timeval tv;
    152 	CLIENT *client;
    153 
    154 	uid = getuid();
    155 
    156 	/*
    157 	 * Get local domain
    158 	 */
    159 	if ((r = yp_get_default_domain(&domain)) != 0)
    160 		errx(1, "can't get local YP domain.  Reason: %s",
    161 		    yperr_string(r));
    162 
    163 	/*
    164 	 * Find the host for the passwd map; it should be running
    165 	 * the daemon.
    166 	 */
    167 	if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
    168 		warnx("can't find the master YP server.  Reason: %s",
    169 		    yperr_string(r));
    170 		/* continuation */
    171 		return(-1);
    172 	}
    173 
    174 	/*
    175 	 * Ask the portmapper for the port of the daemon.
    176 	 */
    177 	if ((rpcport = getrpcport(master, YPPASSWDPROG,
    178 	    YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) {
    179 		warnx("master YP server not running yppasswd daemon.\n\t%s\n",
    180 		    "Can't change YP password.");
    181 		/* continuation */
    182 		return(-1);
    183 	}
    184 
    185 	/*
    186 	 * Be sure the port is privileged
    187 	 */
    188 	if (rpcport >= IPPORT_RESERVED)
    189 		errx(1, "yppasswd daemon is on an invalid port.");
    190 
    191 	/* Bail out if this is a local (non-yp) user, */
    192 	/* then get user's login identity */
    193 	if (!ypgetpwnam(username) ||
    194 	    !(pw = getpwnam(username))) {
    195 		warnx("YP unknown user %s", username);
    196 		/* continuation */
    197 		return(-1);
    198 	}
    199 
    200 	if (uid && uid != pw->pw_uid)
    201 		errx(1, "you may only change your own password: %s",
    202 		    strerror(EACCES));
    203 
    204 	/* prompt for new password */
    205 	yppasswd.newpw.pw_passwd = getnewpasswd(pw, &yppasswd.oldpass);
    206 
    207 	/* tell rpc.yppasswdd */
    208 	yppasswd.newpw.pw_name	= strdup(pw->pw_name);
    209 	yppasswd.newpw.pw_uid 	= pw->pw_uid;
    210 	yppasswd.newpw.pw_gid	= pw->pw_gid;
    211 	yppasswd.newpw.pw_gecos = strdup(pw->pw_gecos);
    212 	yppasswd.newpw.pw_dir	= strdup(pw->pw_dir);
    213 	yppasswd.newpw.pw_shell	= strdup(pw->pw_shell);
    214 
    215 	client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
    216 	if (client == NULL) {
    217 		warnx("cannot contact yppasswdd on %s:  Reason: %s",
    218 		    master, yperr_string(YPERR_YPBIND));
    219 		return (YPERR_YPBIND);
    220 	}
    221 
    222 	client->cl_auth = authunix_create_default();
    223 	tv.tv_sec = 2;
    224 	tv.tv_usec = 0;
    225 	r = clnt_call(client, YPPASSWDPROC_UPDATE,
    226 	    xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
    227 	if (r)
    228 		errx(1, "rpc to yppasswdd failed.");
    229 	else if (status)
    230 		printf("Couldn't change YP password.\n");
    231 	else
    232 		printf("The YP password has been changed on %s, %s\n",
    233 		    master, "the master YP passwd server.");
    234 	return(0);
    235 }
    236 
    237 static char *
    238 getnewpasswd(pw, old_pass)
    239 	struct passwd *pw;
    240 	char **old_pass;
    241 {
    242 	int tries;
    243 	char *p, *t;
    244 	static char buf[_PASSWORD_LEN+1];
    245 	char salt[9];
    246 
    247 	(void)printf("Changing YP password for %s.\n", pw->pw_name);
    248 
    249 	if (old_pass) {
    250 		*old_pass = NULL;
    251 
    252 		if (pw->pw_passwd[0]) {
    253 			if (strcmp(crypt(p = getpass("Old password:"),
    254 					 pw->pw_passwd),  pw->pw_passwd)) {
    255 				   (void)printf("Sorry.\n");
    256 				   pw_error(NULL, 0, 1);
    257 			}
    258 		} else {
    259 			p = "";
    260 		}
    261 
    262 		*old_pass = strdup(p);
    263 	}
    264 	for (buf[0] = '\0', tries = 0;;) {
    265 		p = getpass("New password:");
    266 		if (!*p) {
    267 			(void)printf("Password unchanged.\n");
    268 			pw_error(NULL, 0, 0);
    269 		}
    270 		if (strlen(p) <= 5 && ++tries < 2) {
    271 			(void)printf("Please enter a longer password.\n");
    272 			continue;
    273 		}
    274 		for (t = p; *t && islower(*t); ++t);
    275 		if (!*t && ++tries < 2) {
    276 			(void)printf("Please don't use an all-lower case "
    277 				     "password.\nUnusual capitalization, "
    278 				     "control characters or digits are "
    279 				     "suggested.\n");
    280 			continue;
    281 		}
    282 		(void)strncpy(buf, p, sizeof(buf) - 1);
    283 		if (!strcmp(buf, getpass("Retype new password:")))
    284 			break;
    285 		(void)printf("Mismatch; try again, EOF to quit.\n");
    286 	}
    287 	/* grab a random printable character that isn't a colon */
    288 	(void)srandom((int)time((time_t *)NULL));
    289 #ifdef NEWSALT
    290 	salt[0] = _PASSWORD_EFMT1;
    291 	to64(&salt[1], (long)(29 * 25), 4);
    292 	to64(&salt[5], random(), 4);
    293 #else
    294 	to64(&salt[0], random(), 2);
    295 #endif
    296 	return(strdup(crypt(buf, salt)));
    297 }
    298 
    299 static int
    300 ypgetpwnam(nam)
    301 	const char *nam;
    302 {
    303 	char *val;
    304 	int reason, vallen;
    305 
    306 	val = NULL;
    307 	reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
    308 			  &val, &vallen);
    309 	if (reason != 0) {
    310 		if (val != NULL)
    311 			free(val);
    312 		return 0;
    313 	}
    314 	free(val);
    315 	return 1;
    316 }
    317 
    318 #endif	/* YP */
    319