Home | History | Annotate | Line # | Download | only in passwd
yp_passwd.c revision 1.16
      1 /*	$NetBSD: yp_passwd.c,v 1.16 1997/11/21 20:28:35 tv 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.16 1997/11/21 20:28:35 tv 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 extern	int yflag, yppwd;
     75 
     76 static	char		*getnewpasswd __P((struct passwd *, char **));
     77 static	struct passwd	*interpret __P((struct passwd *, char *));
     78 static	struct passwd	*ypgetpwnam __P((char *));
     79 static	void		 pw_error __P((char *, int, int));
     80 static	void		 test_local __P((char *));
     81 
     82 static uid_t uid;
     83 char *domain;
     84 
     85 static void
     86 pw_error(name, err, eval)
     87 	char *name;
     88 	int err, eval;
     89 {
     90 	if (err)
     91 		warn("%s", name ? name : "");
     92 	errx(eval, "YP passwd database unchanged");
     93 }
     94 
     95 static void
     96 test_local(username)
     97 	char *username;
     98 {
     99 	/*
    100 	 * Something failed recoverably stating that the YP system couldn't
    101 	 * find this user.  Look for a local passwd entry, and change that
    102 	 * if and only if we weren't run as yppasswd or with the -y option.
    103 	 * This function does not return if a local entry is found.
    104 	 */
    105 	if (yppwd == 0 && yflag == 0)
    106 		if ((getpwnam(username) != NULL) && !local_passwd(username))
    107 			exit(0);
    108 }
    109 
    110 int
    111 yp_passwd(username)
    112 	char *username;
    113 {
    114 	char *master;
    115 	int r, rpcport, status;
    116 	struct yppasswd yppasswd;
    117 	struct passwd *pw;
    118 	struct timeval tv;
    119 	CLIENT *client;
    120 
    121 	uid = getuid();
    122 
    123 	/*
    124 	 * Get local domain
    125 	 */
    126 	if ((r = yp_get_default_domain(&domain)) != NULL)
    127 		errx(1, "can't get local YP domain.  Reason: %s",
    128 		    yperr_string(r));
    129 
    130 	/*
    131 	 * Find the host for the passwd map; it should be running
    132 	 * the daemon.
    133 	 */
    134 	if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
    135 		test_local(username);
    136 		errx(1, "can't find the master YP server.  Reason: %s",
    137 		    yperr_string(r));
    138 	}
    139 
    140 	/*
    141 	 * Ask the portmapper for the port of the daemon.
    142 	 */
    143 	if ((rpcport = getrpcport(master, YPPASSWDPROG,
    144 	    YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) {
    145 		test_local(username);
    146 		errx(1, "master YP server not running yppasswd daemon.\n\t%s\n",
    147 		    "Can't change password.");
    148 	}
    149 
    150 	/*
    151 	 * Be sure the port is priviledged
    152 	 */
    153 	if (rpcport >= IPPORT_RESERVED)
    154 		errx(1, "yppasswd daemon is on an invalid port.");
    155 
    156 	/* Get user's login identity */
    157 	if (!(pw = ypgetpwnam(username))) {
    158 		test_local(username);
    159 		errx(1, "unknown user %s", username);
    160 	}
    161 
    162 	if (uid && uid != pw->pw_uid)
    163 		errx(1, "you may only change your own password: %s",
    164 		    strerror(EACCES));
    165 
    166 	/* prompt for new password */
    167 	yppasswd.newpw.pw_passwd = getnewpasswd(pw, &yppasswd.oldpass);
    168 
    169 	/* tell rpc.yppasswdd */
    170 	yppasswd.newpw.pw_name	= pw->pw_name;
    171 	yppasswd.newpw.pw_uid 	= pw->pw_uid;
    172 	yppasswd.newpw.pw_gid	= pw->pw_gid;
    173 	yppasswd.newpw.pw_gecos = pw->pw_gecos;
    174 	yppasswd.newpw.pw_dir	= pw->pw_dir;
    175 	yppasswd.newpw.pw_shell	= pw->pw_shell;
    176 
    177 	client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
    178 	if (client==NULL) {
    179 		warnx("cannot contact yppasswdd on %s:  Reason: %s",
    180 		    master, yperr_string(YPERR_YPBIND));
    181 		return(YPERR_YPBIND);
    182 	}
    183 
    184 	client->cl_auth = authunix_create_default();
    185 	tv.tv_sec = 2;
    186 	tv.tv_usec = 0;
    187 	r = clnt_call(client, YPPASSWDPROC_UPDATE,
    188 	    xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
    189 	if (r)
    190 		errx(1, "rpc to yppasswdd failed.");
    191 	else if (status)
    192 		printf("Couldn't change YP password.\n");
    193 	else
    194 		printf("The YP password has been changed on %s, %s\n",
    195 		    master, "the master YP passwd server.");
    196 	exit(0);
    197 }
    198 
    199 static char *
    200 getnewpasswd(pw, old_pass)
    201 	struct passwd *pw;
    202 	char **old_pass;
    203 {
    204 	int tries;
    205 	char *p, *t;
    206 	static char buf[_PASSWORD_LEN+1];
    207 	char salt[9];
    208 
    209 	(void)printf("Changing YP password for %s.\n", pw->pw_name);
    210 
    211 	if (old_pass) {
    212 		*old_pass = NULL;
    213 
    214 		if (pw->pw_passwd[0]) {
    215 			if (strcmp(crypt(p = getpass("Old password:"),
    216 					 pw->pw_passwd),  pw->pw_passwd)) {
    217 				   errno = EACCES;
    218 				   pw_error(NULL, 1, 1);
    219 			}
    220 		} else {
    221 			p = "";
    222 		}
    223 
    224 		*old_pass = strdup(p);
    225 	}
    226 	for (buf[0] = '\0', tries = 0;;) {
    227 		p = getpass("New password:");
    228 		if (!*p) {
    229 			(void)printf("Password unchanged.\n");
    230 			pw_error(NULL, 0, 0);
    231 		}
    232 		if (strlen(p) <= 5 && ++tries < 2) {
    233 			(void)printf("Please enter a longer password.\n");
    234 			continue;
    235 		}
    236 		for (t = p; *t && islower(*t); ++t);
    237 		if (!*t && ++tries < 2) {
    238 			(void)printf("Please don't use an all-lower case "
    239 				     "password.\nUnusual capitalization, "
    240 				     "control characters or digits are "
    241 				     "suggested.\n");
    242 			continue;
    243 		}
    244 		(void)strncpy(buf, p, sizeof(buf) - 1);
    245 		if (!strcmp(buf, getpass("Retype new password:")))
    246 			break;
    247 		(void)printf("Mismatch; try again, EOF to quit.\n");
    248 	}
    249 	/* grab a random printable character that isn't a colon */
    250 	(void)srandom((int)time((time_t *)NULL));
    251 #ifdef NEWSALT
    252 	salt[0] = _PASSWORD_EFMT1;
    253 	to64(&salt[1], (long)(29 * 25), 4);
    254 	to64(&salt[5], random(), 4);
    255 #else
    256 	to64(&salt[0], random(), 2);
    257 #endif
    258 	return(strdup(crypt(buf, salt)));
    259 }
    260 
    261 static char *
    262 pwskip(char *p)
    263 {
    264 	while (*p && *p != ':' && *p != '\n')
    265 		++p;
    266 	if (*p)
    267 		*p++ = 0;
    268 	return (p);
    269 }
    270 
    271 static struct passwd *
    272 interpret(pwent, line)
    273 	struct passwd *pwent;
    274 	char *line;
    275 {
    276 	char	*p = line;
    277 
    278 	pwent->pw_passwd = "*";
    279 	pwent->pw_uid = 0;
    280 	pwent->pw_gid = 0;
    281 	pwent->pw_gecos = "";
    282 	pwent->pw_dir = "";
    283 	pwent->pw_shell = "";
    284 	pwent->pw_change = 0;
    285 	pwent->pw_expire = 0;
    286 	pwent->pw_class = "";
    287 
    288 	/* line without colon separators is no good, so ignore it */
    289 	if(!strchr(p,':'))
    290 		return(NULL);
    291 
    292 	pwent->pw_name = p;
    293 	p = pwskip(p);
    294 	pwent->pw_passwd = p;
    295 	p = pwskip(p);
    296 	pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
    297 	p = pwskip(p);
    298 	pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
    299 	p = pwskip(p);
    300 	pwent->pw_gecos = p;
    301 	p = pwskip(p);
    302 	pwent->pw_dir = p;
    303 	p = pwskip(p);
    304 	pwent->pw_shell = p;
    305 	while (*p && *p != '\n')
    306 		p++;
    307 	*p = '\0';
    308 	return (pwent);
    309 }
    310 
    311 static struct passwd *
    312 ypgetpwnam(nam)
    313 	char *nam;
    314 {
    315 	static struct passwd pwent;
    316 	static char line[1024];
    317 	char *val;
    318 	int reason, vallen;
    319 
    320 	val = NULL;
    321 	reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
    322 			  &val, &vallen);
    323 	if (reason != 0) {
    324 		if (val != NULL)
    325 			free(val);
    326 		return (NULL);
    327 	}
    328 	val[vallen] = '\0';
    329 	(void)strncpy(line, val, sizeof(line) - 1);
    330 	free(val);
    331 
    332 	return(interpret(&pwent, line));
    333 }
    334 
    335 #endif	/* YP */
    336