Home | History | Annotate | Line # | Download | only in passwd
yp_passwd.c revision 1.9
      1  1.9  thorpej /*	$NetBSD: yp_passwd.c,v 1.9 1996/08/09 09:19:42 thorpej Exp $	*/
      2  1.9  thorpej 
      3  1.1   brezak /*
      4  1.1   brezak  * Copyright (c) 1988 The Regents of the University of California.
      5  1.1   brezak  * All rights reserved.
      6  1.1   brezak  *
      7  1.1   brezak  * Redistribution and use in source and binary forms, with or without
      8  1.1   brezak  * modification, are permitted provided that the following conditions
      9  1.1   brezak  * are met:
     10  1.1   brezak  * 1. Redistributions of source code must retain the above copyright
     11  1.1   brezak  *    notice, this list of conditions and the following disclaimer.
     12  1.1   brezak  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1   brezak  *    notice, this list of conditions and the following disclaimer in the
     14  1.1   brezak  *    documentation and/or other materials provided with the distribution.
     15  1.1   brezak  * 3. All advertising materials mentioning features or use of this software
     16  1.1   brezak  *    must display the following acknowledgement:
     17  1.1   brezak  *	This product includes software developed by the University of
     18  1.1   brezak  *	California, Berkeley and its contributors.
     19  1.1   brezak  * 4. Neither the name of the University nor the names of its contributors
     20  1.1   brezak  *    may be used to endorse or promote products derived from this software
     21  1.1   brezak  *    without specific prior written permission.
     22  1.1   brezak  *
     23  1.1   brezak  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1   brezak  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1   brezak  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1   brezak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1   brezak  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1   brezak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1   brezak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1   brezak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1   brezak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1   brezak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1   brezak  * SUCH DAMAGE.
     34  1.1   brezak  */
     35  1.9  thorpej 
     36  1.1   brezak #ifndef lint
     37  1.9  thorpej #if 0
     38  1.9  thorpej static char sccsid[] = "from: @(#)yp_passwd.c	1.0 2/2/93";
     39  1.9  thorpej #else
     40  1.9  thorpej static char rcsid[] = "$NetBSD: yp_passwd.c,v 1.9 1996/08/09 09:19:42 thorpej Exp $";
     41  1.9  thorpej #endif
     42  1.1   brezak #endif /* not lint */
     43  1.1   brezak 
     44  1.1   brezak #ifdef	YP
     45  1.1   brezak 
     46  1.9  thorpej #include <err.h>
     47  1.1   brezak #include <stdio.h>
     48  1.1   brezak #include <string.h>
     49  1.1   brezak #include <netdb.h>
     50  1.1   brezak #include <time.h>
     51  1.1   brezak #include <pwd.h>
     52  1.1   brezak #include <errno.h>
     53  1.1   brezak #include <rpc/rpc.h>
     54  1.1   brezak #include <rpcsvc/yp_prot.h>
     55  1.1   brezak #include <rpcsvc/ypclnt.h>
     56  1.1   brezak #define passwd yp_passwd_rec
     57  1.1   brezak #include <rpcsvc/yppasswd.h>
     58  1.1   brezak #undef passwd
     59  1.4  deraadt 
     60  1.1   brezak #ifndef _PASSWORD_LEN
     61  1.1   brezak #define _PASSWORD_LEN PASS_MAX
     62  1.1   brezak #endif
     63  1.4  deraadt 
     64  1.9  thorpej extern	char *__progname;		/* from crt0.o */
     65  1.9  thorpej 
     66  1.4  deraadt static char *getnewpasswd();
     67  1.1   brezak static struct passwd *ypgetpwnam();
     68  1.1   brezak 
     69  1.1   brezak static uid_t uid;
     70  1.1   brezak char *domain;
     71  1.1   brezak 
     72  1.4  deraadt static
     73  1.4  deraadt pw_error(name, err, eval)
     74  1.4  deraadt 	char *name;
     75  1.4  deraadt 	int err, eval;
     76  1.4  deraadt {
     77  1.4  deraadt 	int sverrno;
     78  1.4  deraadt 
     79  1.4  deraadt 	if (err) {
     80  1.4  deraadt 		sverrno = errno;
     81  1.9  thorpej 		(void)fprintf(stderr, "%s: ", __progname);
     82  1.4  deraadt 		if (name)
     83  1.4  deraadt 			(void)fprintf(stderr, "%s: ", name);
     84  1.4  deraadt 		(void)fprintf(stderr, "%s\n", strerror(sverrno));
     85  1.4  deraadt 	}
     86  1.9  thorpej 	errx(eval, "YP passwd database unchanged");
     87  1.4  deraadt }
     88  1.4  deraadt 
     89  1.9  thorpej int
     90  1.5  deraadt yp_passwd(username)
     91  1.5  deraadt 	char *username;
     92  1.1   brezak {
     93  1.4  deraadt 	char *master;
     94  1.4  deraadt 	char *pp;
     95  1.4  deraadt 	int r, rpcport, status;
     96  1.4  deraadt 	struct yppasswd yppasswd;
     97  1.4  deraadt 	struct passwd *pw;
     98  1.1   brezak 	struct timeval tv;
     99  1.1   brezak 	CLIENT *client;
    100  1.4  deraadt 
    101  1.4  deraadt 	uid = getuid();
    102  1.4  deraadt 
    103  1.4  deraadt 	/*
    104  1.4  deraadt 	 * Get local domain
    105  1.4  deraadt 	 */
    106  1.9  thorpej 	if (r = yp_get_default_domain(&domain))
    107  1.9  thorpej 		errx(1, "can't get local YP domain.  Reason: %s",
    108  1.9  thorpej 		    yperr_string(r));
    109  1.4  deraadt 
    110  1.4  deraadt 	/*
    111  1.4  deraadt 	 * Find the host for the passwd map; it should be running
    112  1.4  deraadt 	 * the daemon.
    113  1.4  deraadt 	 */
    114  1.9  thorpej 	if ((r = yp_master(domain, "passwd.byname", &master)) != 0)
    115  1.9  thorpej 		errx(1, "can't find the master YP server.  Reason: %s",
    116  1.9  thorpej 		    yperr_string(r));
    117  1.1   brezak 
    118  1.4  deraadt 	/*
    119  1.4  deraadt 	 * Ask the portmapper for the port of the daemon.
    120  1.4  deraadt 	 */
    121  1.4  deraadt 	if ((rpcport = getrpcport(master, YPPASSWDPROG,
    122  1.9  thorpej 	    YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0)
    123  1.9  thorpej 		errx(1, "master YP server not running yppasswd daemon.\n\t%s\n",
    124  1.9  thorpej 		    "Can't change password.");
    125  1.1   brezak 
    126  1.4  deraadt 	/*
    127  1.4  deraadt 	 * Be sure the port is priviledged
    128  1.4  deraadt 	 */
    129  1.9  thorpej 	if (rpcport >= IPPORT_RESERVED)
    130  1.9  thorpej 		errx(1, "yppasswd daemon is on an invalid port.");
    131  1.4  deraadt 
    132  1.4  deraadt 	/* Get user's login identity */
    133  1.9  thorpej 	if (!(pw = ypgetpwnam(username)))
    134  1.9  thorpej 		errx(1, "unknown user %s", username);
    135  1.9  thorpej 
    136  1.9  thorpej 	if (uid && uid != pw->pw_uid)
    137  1.9  thorpej 		errx(1, "you may only change your own password: %s",
    138  1.9  thorpej 		    strerror(EACCES));
    139  1.1   brezak 
    140  1.4  deraadt 	/* prompt for new password */
    141  1.4  deraadt 	yppasswd.newpw.pw_passwd = getnewpasswd(pw, &yppasswd.oldpass);
    142  1.1   brezak 
    143  1.4  deraadt 	/* tell rpc.yppasswdd */
    144  1.4  deraadt 	yppasswd.newpw.pw_name	= pw->pw_name;
    145  1.4  deraadt 	yppasswd.newpw.pw_uid 	= pw->pw_uid;
    146  1.4  deraadt 	yppasswd.newpw.pw_gid	= pw->pw_gid;
    147  1.4  deraadt 	yppasswd.newpw.pw_gecos = pw->pw_gecos;
    148  1.4  deraadt 	yppasswd.newpw.pw_dir	= pw->pw_dir;
    149  1.4  deraadt 	yppasswd.newpw.pw_shell	= pw->pw_shell;
    150  1.4  deraadt 
    151  1.4  deraadt 	client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
    152  1.4  deraadt 	if (client==NULL) {
    153  1.9  thorpej 		warnx("cannot contact yppasswdd on %s:  Reason: %s",
    154  1.5  deraadt 		    master, yperr_string(YPERR_YPBIND));
    155  1.4  deraadt 		return(YPERR_YPBIND);
    156  1.4  deraadt 	}
    157  1.9  thorpej 
    158  1.4  deraadt 	client->cl_auth = authunix_create_default();
    159  1.4  deraadt 	tv.tv_sec = 2;
    160  1.4  deraadt 	tv.tv_usec = 0;
    161  1.4  deraadt 	r = clnt_call(client, YPPASSWDPROC_UPDATE,
    162  1.9  thorpej 	    xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
    163  1.4  deraadt 	if (r)
    164  1.9  thorpej 		errx(1, "rpc to yppasswdd failed.");
    165  1.4  deraadt 	else if (status)
    166  1.4  deraadt 		printf("Couldn't change YP password.\n");
    167  1.4  deraadt 	else
    168  1.9  thorpej 		printf("The YP password has been changed on %s, %s\n",
    169  1.9  thorpej 		    master, "the master YP passwd server.");
    170  1.4  deraadt 	exit(0);
    171  1.1   brezak }
    172  1.1   brezak 
    173  1.1   brezak static char *
    174  1.1   brezak getnewpasswd(pw, old_pass)
    175  1.1   brezak 	register struct passwd *pw;
    176  1.4  deraadt 	char **old_pass;
    177  1.1   brezak {
    178  1.4  deraadt 	static char buf[_PASSWORD_LEN+1];
    179  1.1   brezak 	register char *p, *t;
    180  1.1   brezak 	int tries;
    181  1.1   brezak 	char salt[9], *crypt(), *getpass();
    182  1.4  deraadt 
    183  1.2   brezak 	(void)printf("Changing YP password for %s.\n", pw->pw_name);
    184  1.1   brezak 
    185  1.6  deraadt 	if (old_pass) {
    186  1.4  deraadt 		*old_pass = NULL;
    187  1.4  deraadt 
    188  1.8     phil 		if (pw->pw_passwd[0]) {
    189  1.8     phil 			if (strcmp(crypt(p = getpass("Old password:"),
    190  1.8     phil 					 pw->pw_passwd),  pw->pw_passwd)) {
    191  1.4  deraadt 				   errno = EACCES;
    192  1.4  deraadt 				   pw_error(NULL, 1, 1);
    193  1.8     phil 			}
    194  1.7     phil 		} else {
    195  1.7     phil 			p = "";
    196  1.4  deraadt 		}
    197  1.7     phil 
    198  1.4  deraadt 		*old_pass = strdup(p);
    199  1.4  deraadt 	}
    200  1.1   brezak 	for (buf[0] = '\0', tries = 0;;) {
    201  1.1   brezak 		p = getpass("New password:");
    202  1.1   brezak 		if (!*p) {
    203  1.1   brezak 			(void)printf("Password unchanged.\n");
    204  1.1   brezak 			pw_error(NULL, 0, 0);
    205  1.1   brezak 		}
    206  1.4  deraadt 		if (strlen(p) <= 5 && ++tries < 2) {
    207  1.1   brezak 			(void)printf("Please enter a longer password.\n");
    208  1.1   brezak 			continue;
    209  1.1   brezak 		}
    210  1.1   brezak 		for (t = p; *t && islower(*t); ++t);
    211  1.4  deraadt 		if (!*t && ++tries < 2) {
    212  1.1   brezak 			(void)printf("Please don't use an all-lower case password.\nUnusual capitalization, control characters or digits are suggested.\n");
    213  1.1   brezak 			continue;
    214  1.1   brezak 		}
    215  1.1   brezak 		(void)strcpy(buf, p);
    216  1.1   brezak 		if (!strcmp(buf, getpass("Retype new password:")))
    217  1.1   brezak 			break;
    218  1.1   brezak 		(void)printf("Mismatch; try again, EOF to quit.\n");
    219  1.1   brezak 	}
    220  1.1   brezak 	/* grab a random printable character that isn't a colon */
    221  1.1   brezak 	(void)srandom((int)time((time_t *)NULL));
    222  1.1   brezak #ifdef NEWSALT
    223  1.1   brezak 	salt[0] = _PASSWORD_EFMT1;
    224  1.1   brezak 	to64(&salt[1], (long)(29 * 25), 4);
    225  1.1   brezak 	to64(&salt[5], random(), 4);
    226  1.1   brezak #else
    227  1.1   brezak 	to64(&salt[0], random(), 2);
    228  1.1   brezak #endif
    229  1.1   brezak 	return(strdup(crypt(buf, salt)));
    230  1.1   brezak }
    231  1.1   brezak 
    232  1.1   brezak static char *
    233  1.1   brezak pwskip(register char *p)
    234  1.1   brezak {
    235  1.1   brezak 	while (*p && *p != ':' && *p != '\n')
    236  1.1   brezak 		++p;
    237  1.1   brezak 	if (*p)
    238  1.1   brezak 		*p++ = 0;
    239  1.1   brezak 	return (p);
    240  1.1   brezak }
    241  1.1   brezak 
    242  1.1   brezak struct passwd *
    243  1.1   brezak interpret(struct passwd *pwent, char *line)
    244  1.1   brezak {
    245  1.1   brezak 	register char	*p = line;
    246  1.1   brezak 	register int	c;
    247  1.1   brezak 
    248  1.4  deraadt 	pwent->pw_passwd = "*";
    249  1.4  deraadt 	pwent->pw_uid = 0;
    250  1.4  deraadt 	pwent->pw_gid = 0;
    251  1.4  deraadt 	pwent->pw_gecos = "";
    252  1.4  deraadt 	pwent->pw_dir = "";
    253  1.4  deraadt 	pwent->pw_shell = "";
    254  1.1   brezak 	pwent->pw_change = 0;
    255  1.1   brezak 	pwent->pw_expire = 0;
    256  1.1   brezak 	pwent->pw_class = "";
    257  1.4  deraadt 
    258  1.4  deraadt 	/* line without colon separators is no good, so ignore it */
    259  1.4  deraadt 	if(!strchr(p,':'))
    260  1.4  deraadt 		return(NULL);
    261  1.1   brezak 
    262  1.1   brezak 	pwent->pw_name = p;
    263  1.1   brezak 	p = pwskip(p);
    264  1.1   brezak 	pwent->pw_passwd = p;
    265  1.1   brezak 	p = pwskip(p);
    266  1.1   brezak 	pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
    267  1.1   brezak 	p = pwskip(p);
    268  1.1   brezak 	pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
    269  1.1   brezak 	p = pwskip(p);
    270  1.1   brezak 	pwent->pw_gecos = p;
    271  1.1   brezak 	p = pwskip(p);
    272  1.1   brezak 	pwent->pw_dir = p;
    273  1.1   brezak 	p = pwskip(p);
    274  1.1   brezak 	pwent->pw_shell = p;
    275  1.1   brezak 	while (*p && *p != '\n')
    276  1.1   brezak 		p++;
    277  1.1   brezak 	*p = '\0';
    278  1.1   brezak 	return (pwent);
    279  1.1   brezak }
    280  1.1   brezak 
    281  1.1   brezak static struct passwd *
    282  1.1   brezak ypgetpwnam(nam)
    283  1.4  deraadt 	char *nam;
    284  1.1   brezak {
    285  1.4  deraadt 	static struct passwd pwent;
    286  1.4  deraadt 	static char line[1024];
    287  1.4  deraadt 	char *val;
    288  1.4  deraadt 	int reason, vallen;
    289  1.4  deraadt 
    290  1.4  deraadt 	reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
    291  1.4  deraadt 			  &val, &vallen);
    292  1.4  deraadt 	switch(reason) {
    293  1.4  deraadt 	case 0:
    294  1.4  deraadt 		break;
    295  1.4  deraadt 	default:
    296  1.4  deraadt 		return (NULL);
    297  1.4  deraadt 		break;
    298  1.4  deraadt 	}
    299  1.4  deraadt 	val[vallen] = '\0';
    300  1.4  deraadt 	strcpy(line, val);
    301  1.4  deraadt 	free(val);
    302  1.1   brezak 
    303  1.4  deraadt 	return(interpret(&pwent, line));
    304  1.1   brezak }
    305  1.1   brezak 
    306  1.1   brezak #endif	/* YP */
    307