Home | History | Annotate | Line # | Download | only in chpass
pw_yp.c revision 1.7
      1  1.7  thorpej /*	$NetBSD: pw_yp.c,v 1.7 1996/11/26 23:38:44 thorpej Exp $	*/
      2  1.5    glass 
      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.1   brezak #ifndef lint
     36  1.5    glass #if 0
     37  1.5    glass static char sccsid[] = "@(#)pw_yp.c	1.0 2/2/93";
     38  1.5    glass #else
     39  1.7  thorpej static char rcsid[] = "$NetBSD: pw_yp.c,v 1.7 1996/11/26 23:38:44 thorpej Exp $";
     40  1.5    glass #endif
     41  1.1   brezak #endif /* not lint */
     42  1.1   brezak 
     43  1.1   brezak #ifdef	YP
     44  1.1   brezak 
     45  1.1   brezak #include <stdio.h>
     46  1.1   brezak #include <string.h>
     47  1.1   brezak #include <netdb.h>
     48  1.1   brezak #include <time.h>
     49  1.1   brezak #include <pwd.h>
     50  1.1   brezak #include <errno.h>
     51  1.1   brezak #include <rpc/rpc.h>
     52  1.1   brezak #include <rpcsvc/yp_prot.h>
     53  1.1   brezak #include <rpcsvc/ypclnt.h>
     54  1.1   brezak #define passwd yp_passwd_rec
     55  1.1   brezak #include <rpcsvc/yppasswd.h>
     56  1.1   brezak #undef passwd
     57  1.3  deraadt 
     58  1.1   brezak static char *domain;
     59  1.7  thorpej 
     60  1.7  thorpej /*
     61  1.7  thorpej  * Check if rpc.yppasswdd is running on the master YP server.
     62  1.7  thorpej  * XXX this duplicates some code, but is much less complex
     63  1.7  thorpej  * than the alternative.
     64  1.7  thorpej  */
     65  1.7  thorpej int
     66  1.7  thorpej check_yppasswdd()
     67  1.7  thorpej {
     68  1.7  thorpej 	char *master;
     69  1.7  thorpej 	int rpcport;
     70  1.7  thorpej 
     71  1.7  thorpej 	/*
     72  1.7  thorpej 	 * Get local domain
     73  1.7  thorpej 	 */
     74  1.7  thorpej 	if (!domain && yp_get_default_domain(&domain) != 0)
     75  1.7  thorpej 		return (1);
     76  1.7  thorpej 
     77  1.7  thorpej 	/*
     78  1.7  thorpej 	 * Find the host for the passwd map; it should be running
     79  1.7  thorpej 	 * the daemon.
     80  1.7  thorpej 	 */
     81  1.7  thorpej 	if (yp_master(domain, "passwd.byname", &master) != 0)
     82  1.7  thorpej 		return (1);
     83  1.7  thorpej 
     84  1.7  thorpej 	/*
     85  1.7  thorpej 	 * Ask the portmapper for the port of the daemon.
     86  1.7  thorpej 	 */
     87  1.7  thorpej 	if ((rpcport = getrpcport(master, YPPASSWDPROG, YPPASSWDPROC_UPDATE,
     88  1.7  thorpej 	    IPPROTO_UDP)) == 0)
     89  1.7  thorpej 		return (1);
     90  1.7  thorpej 
     91  1.7  thorpej 	/*
     92  1.7  thorpej 	 * Successful contact with rpc.yppasswdd.
     93  1.7  thorpej 	 */
     94  1.7  thorpej 	return (0);
     95  1.7  thorpej }
     96  1.1   brezak 
     97  1.1   brezak pw_yp(pw, uid)
     98  1.3  deraadt 	struct passwd *pw;
     99  1.3  deraadt 	uid_t uid;
    100  1.1   brezak {
    101  1.3  deraadt 	char *master;
    102  1.3  deraadt 	char *pp;
    103  1.3  deraadt 	int r, rpcport, status;
    104  1.3  deraadt 	struct yppasswd yppasswd;
    105  1.1   brezak 	struct timeval tv;
    106  1.1   brezak 	CLIENT *client;
    107  1.3  deraadt 	extern char *getpass();
    108  1.3  deraadt 
    109  1.3  deraadt 	/*
    110  1.3  deraadt 	 * Get local domain
    111  1.3  deraadt 	 */
    112  1.6  thorpej 	if (!domain && (r = yp_get_default_domain(&domain)))
    113  1.6  thorpej 		errx(1, "can't get local YP domain.  Reason: %s",
    114  1.6  thorpej 		    yperr_string(r));
    115  1.3  deraadt 
    116  1.3  deraadt 	/*
    117  1.3  deraadt 	 * Find the host for the passwd map; it should be running
    118  1.3  deraadt 	 * the daemon.
    119  1.3  deraadt 	 */
    120  1.3  deraadt 	if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
    121  1.6  thorpej 		warnx("can't find the master YP server.  Reason: %s",
    122  1.6  thorpej 		    yperr_string(r));
    123  1.6  thorpej 		return (1);
    124  1.3  deraadt 	}
    125  1.3  deraadt 
    126  1.3  deraadt 	/*
    127  1.3  deraadt 	 * Ask the portmapper for the port of the daemon.
    128  1.3  deraadt 	 */
    129  1.3  deraadt 	if ((rpcport = getrpcport(master, YPPASSWDPROG, YPPASSWDPROC_UPDATE,
    130  1.3  deraadt 	    IPPROTO_UDP)) == 0) {
    131  1.6  thorpej 		warnx("master YP server not running yppasswd daemon.\n\t%s\n",
    132  1.6  thorpej 		    "Can't change password.");
    133  1.6  thorpej 		return (1);
    134  1.3  deraadt 	}
    135  1.3  deraadt 
    136  1.3  deraadt 	/*
    137  1.3  deraadt 	 * Be sure the port is priviledged
    138  1.3  deraadt 	 */
    139  1.3  deraadt 	if (rpcport >= IPPORT_RESERVED) {
    140  1.6  thorpej 		warnx("yppasswd daemon is on an invalid port.");
    141  1.6  thorpej 		return (1);
    142  1.3  deraadt 	}
    143  1.3  deraadt 
    144  1.3  deraadt 	/* prompt for old password */
    145  1.3  deraadt 	bzero(&yppasswd, sizeof yppasswd);
    146  1.3  deraadt 	yppasswd.oldpass = "none";
    147  1.4  deraadt 	yppasswd.oldpass = getpass("Old password:");
    148  1.3  deraadt 	if (!yppasswd.oldpass) {
    149  1.6  thorpej 		warnx("Cancelled.");
    150  1.6  thorpej 		return (1);
    151  1.3  deraadt 	}
    152  1.6  thorpej 
    153  1.3  deraadt 	/* tell rpc.yppasswdd */
    154  1.3  deraadt 	yppasswd.newpw.pw_name	= pw->pw_name;
    155  1.3  deraadt 	yppasswd.newpw.pw_passwd= pw->pw_passwd;
    156  1.3  deraadt 	yppasswd.newpw.pw_uid 	= pw->pw_uid;
    157  1.3  deraadt 	yppasswd.newpw.pw_gid	= pw->pw_gid;
    158  1.3  deraadt 	yppasswd.newpw.pw_gecos = pw->pw_gecos;
    159  1.3  deraadt 	yppasswd.newpw.pw_dir	= pw->pw_dir;
    160  1.3  deraadt 	yppasswd.newpw.pw_shell	= pw->pw_shell;
    161  1.3  deraadt 
    162  1.3  deraadt 	client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
    163  1.3  deraadt 	if (client==NULL) {
    164  1.6  thorpej 		warnx("cannot contact yppasswdd on %s:  Reason: %s",
    165  1.3  deraadt 		    master, yperr_string(YPERR_YPBIND));
    166  1.6  thorpej 		return (1);
    167  1.3  deraadt 	}
    168  1.3  deraadt 	client->cl_auth = authunix_create_default();
    169  1.3  deraadt 	tv.tv_sec = 5;
    170  1.3  deraadt 	tv.tv_usec = 0;
    171  1.3  deraadt 	r = clnt_call(client, YPPASSWDPROC_UPDATE,
    172  1.3  deraadt 	    xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
    173  1.3  deraadt 	if (r) {
    174  1.6  thorpej 		warnx("rpc to yppasswdd failed.");
    175  1.6  thorpej 		return (1);
    176  1.6  thorpej 	} else if (status)
    177  1.6  thorpej 		printf("Couldn't change YP password.\n");
    178  1.6  thorpej 	else
    179  1.6  thorpej 		printf("%s %s, %s\n",
    180  1.6  thorpej 		    "The YP password information has been changed on",
    181  1.6  thorpej 		    master, "the master YP passwd server.");
    182  1.6  thorpej 	return (0);
    183  1.1   brezak }
    184  1.1   brezak 
    185  1.1   brezak static char *
    186  1.3  deraadt pwskip(p)
    187  1.3  deraadt 	register char *p;
    188  1.1   brezak {
    189  1.1   brezak 	while (*p && *p != ':' && *p != '\n')
    190  1.1   brezak 		++p;
    191  1.1   brezak 	if (*p)
    192  1.1   brezak 		*p++ = 0;
    193  1.1   brezak 	return (p);
    194  1.1   brezak }
    195  1.1   brezak 
    196  1.1   brezak static struct passwd *
    197  1.3  deraadt interpret(pwent, line)
    198  1.3  deraadt 	struct passwd *pwent;
    199  1.3  deraadt 	char *line;
    200  1.1   brezak {
    201  1.1   brezak 	register char	*p = line;
    202  1.1   brezak 	register int	c;
    203  1.1   brezak 
    204  1.3  deraadt 	pwent->pw_passwd = "*";
    205  1.3  deraadt 	pwent->pw_uid = 0;
    206  1.3  deraadt 	pwent->pw_gid = 0;
    207  1.3  deraadt 	pwent->pw_gecos = "";
    208  1.3  deraadt 	pwent->pw_dir = "";
    209  1.3  deraadt 	pwent->pw_shell = "";
    210  1.1   brezak 	pwent->pw_change = 0;
    211  1.1   brezak 	pwent->pw_expire = 0;
    212  1.1   brezak 	pwent->pw_class = "";
    213  1.3  deraadt 
    214  1.3  deraadt 	/* line without colon separators is no good, so ignore it */
    215  1.3  deraadt 	if(!strchr(p,':'))
    216  1.3  deraadt 		return(NULL);
    217  1.1   brezak 
    218  1.1   brezak 	pwent->pw_name = p;
    219  1.1   brezak 	p = pwskip(p);
    220  1.1   brezak 	pwent->pw_passwd = p;
    221  1.1   brezak 	p = pwskip(p);
    222  1.1   brezak 	pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
    223  1.1   brezak 	p = pwskip(p);
    224  1.1   brezak 	pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
    225  1.1   brezak 	p = pwskip(p);
    226  1.1   brezak 	pwent->pw_gecos = p;
    227  1.1   brezak 	p = pwskip(p);
    228  1.1   brezak 	pwent->pw_dir = p;
    229  1.1   brezak 	p = pwskip(p);
    230  1.1   brezak 	pwent->pw_shell = p;
    231  1.1   brezak 	while (*p && *p != '\n')
    232  1.1   brezak 		p++;
    233  1.1   brezak 	*p = '\0';
    234  1.1   brezak 	return (pwent);
    235  1.1   brezak }
    236  1.1   brezak 
    237  1.1   brezak struct passwd *
    238  1.1   brezak ypgetpwnam(nam)
    239  1.3  deraadt 	char *nam;
    240  1.1   brezak {
    241  1.3  deraadt 	static struct passwd pwent;
    242  1.3  deraadt 	static char line[1024];
    243  1.3  deraadt 	char *val;
    244  1.3  deraadt 	int reason, vallen;
    245  1.3  deraadt 
    246  1.3  deraadt 	/*
    247  1.3  deraadt 	 * Get local domain
    248  1.3  deraadt 	 */
    249  1.6  thorpej 	if (!domain && (reason = yp_get_default_domain(&domain)))
    250  1.6  thorpej 		errx(1, "can't get local YP domain. Reason: %s",
    251  1.6  thorpej 		    yperr_string(reason));
    252  1.3  deraadt 
    253  1.3  deraadt 	reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
    254  1.3  deraadt 	    &val, &vallen);
    255  1.3  deraadt 	switch(reason) {
    256  1.3  deraadt 	case 0:
    257  1.3  deraadt 		break;
    258  1.3  deraadt 	default:
    259  1.3  deraadt 		return (NULL);
    260  1.3  deraadt 		break;
    261  1.3  deraadt 	}
    262  1.3  deraadt 	val[vallen] = '\0';
    263  1.3  deraadt 	strcpy(line, val);
    264  1.3  deraadt 	free(val);
    265  1.1   brezak 
    266  1.3  deraadt 	return(interpret(&pwent, line));
    267  1.1   brezak }
    268  1.1   brezak 
    269  1.1   brezak struct passwd *
    270  1.1   brezak ypgetpwuid(uid)
    271  1.3  deraadt 	uid_t uid;
    272  1.1   brezak {
    273  1.3  deraadt 	static struct passwd pwent;
    274  1.3  deraadt 	static char line[1024];
    275  1.3  deraadt 	char *val;
    276  1.3  deraadt 	int reason, vallen;
    277  1.3  deraadt 	char namebuf[16];
    278  1.3  deraadt 
    279  1.6  thorpej 	if (!domain && (reason = yp_get_default_domain(&domain)))
    280  1.6  thorpej 		errx(1, "can't get local YP domain. Reason: %s\n",
    281  1.6  thorpej 		    yperr_string(reason));
    282  1.3  deraadt 
    283  1.3  deraadt 	sprintf(namebuf, "%d", uid);
    284  1.3  deraadt 	reason = yp_match(domain, "passwd.byuid", namebuf, strlen(namebuf),
    285  1.3  deraadt 	    &val, &vallen);
    286  1.3  deraadt 	switch(reason) {
    287  1.3  deraadt 	case 0:
    288  1.3  deraadt 		break;
    289  1.3  deraadt 	default:
    290  1.3  deraadt 		return (NULL);
    291  1.3  deraadt 		break;
    292  1.3  deraadt 	}
    293  1.3  deraadt 	val[vallen] = '\0';
    294  1.3  deraadt 	strcpy(line, val);
    295  1.3  deraadt 	free(val);
    296  1.1   brezak 
    297  1.3  deraadt 	return(interpret(&pwent, line));
    298  1.1   brezak }
    299  1.1   brezak 
    300  1.6  thorpej void
    301  1.6  thorpej yppw_error(name, err, eval)
    302  1.6  thorpej 	const char *name;
    303  1.6  thorpej 	int err, eval;
    304  1.6  thorpej {
    305  1.6  thorpej 
    306  1.6  thorpej 	if (err)
    307  1.6  thorpej 		warn(name);
    308  1.6  thorpej 
    309  1.6  thorpej 	errx(eval, "YP passwd information unchanged");
    310  1.6  thorpej }
    311  1.6  thorpej 
    312  1.6  thorpej void
    313  1.6  thorpej yppw_prompt()
    314  1.6  thorpej {
    315  1.6  thorpej 	int c;
    316  1.6  thorpej 
    317  1.6  thorpej 	(void)printf("re-edit the password file? [y]: ");
    318  1.6  thorpej 	(void)fflush(stdout);
    319  1.6  thorpej 	c = getchar();
    320  1.6  thorpej 	if (c != EOF && c != '\n')
    321  1.6  thorpej 		while (getchar() != '\n');
    322  1.6  thorpej 	if (c == 'n')
    323  1.6  thorpej 		yppw_error(NULL, 0, 0);
    324  1.6  thorpej }
    325  1.1   brezak #endif	/* YP */
    326