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