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