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