xref: /minix/usr.bin/chpass/pw_yp.c (revision 0a6a1f1d)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: pw_yp.c,v 1.23 2015/06/17 00:01:59 christos Exp $	*/
25c007436SBen Gras 
35c007436SBen Gras /*
45c007436SBen Gras  * Copyright (c) 1988 The Regents of the University of California.
55c007436SBen Gras  * All rights reserved.
65c007436SBen Gras  *
75c007436SBen Gras  * Redistribution and use in source and binary forms, with or without
85c007436SBen Gras  * modification, are permitted provided that the following conditions
95c007436SBen Gras  * are met:
105c007436SBen Gras  * 1. Redistributions of source code must retain the above copyright
115c007436SBen Gras  *    notice, this list of conditions and the following disclaimer.
125c007436SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
135c007436SBen Gras  *    notice, this list of conditions and the following disclaimer in the
145c007436SBen Gras  *    documentation and/or other materials provided with the distribution.
155c007436SBen Gras  * 3. Neither the name of the University nor the names of its contributors
165c007436SBen Gras  *    may be used to endorse or promote products derived from this software
175c007436SBen Gras  *    without specific prior written permission.
185c007436SBen Gras  *
195c007436SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
205c007436SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
215c007436SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
225c007436SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
235c007436SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
245c007436SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
255c007436SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
265c007436SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
275c007436SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
285c007436SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
295c007436SBen Gras  * SUCH DAMAGE.
305c007436SBen Gras  */
315c007436SBen Gras #include <sys/cdefs.h>
325c007436SBen Gras #ifndef lint
335c007436SBen Gras #if 0
345c007436SBen Gras static char sccsid[] = "@(#)pw_yp.c	1.0 2/2/93";
355c007436SBen Gras #else
36*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: pw_yp.c,v 1.23 2015/06/17 00:01:59 christos Exp $");
375c007436SBen Gras #endif
385c007436SBen Gras #endif /* not lint */
395c007436SBen Gras 
405c007436SBen Gras #ifdef	YP
415c007436SBen Gras 
425c007436SBen Gras #include <err.h>
435c007436SBen Gras #include <errno.h>
445c007436SBen Gras #include <netdb.h>
455c007436SBen Gras #include <pwd.h>
465c007436SBen Gras #include <stdio.h>
475c007436SBen Gras #include <stdlib.h>
485c007436SBen Gras #include <string.h>
495c007436SBen Gras #include <time.h>
505c007436SBen Gras #include <unistd.h>
515c007436SBen Gras 
525c007436SBen Gras #include <rpc/rpc.h>
535c007436SBen Gras #include <rpcsvc/yp_prot.h>
545c007436SBen Gras #include <rpcsvc/ypclnt.h>
555c007436SBen Gras 
565c007436SBen Gras #define passwd yp_passwd_rec
575c007436SBen Gras #include <rpcsvc/yppasswd.h>
585c007436SBen Gras #undef passwd
595c007436SBen Gras 
605c007436SBen Gras #include "chpass.h"
615c007436SBen Gras 
625c007436SBen Gras static char *domain;
635c007436SBen Gras 
645c007436SBen Gras /*
655c007436SBen Gras  * Check if rpc.yppasswdd is running on the master YP server.
665c007436SBen Gras  * XXX this duplicates some code, but is much less complex
675c007436SBen Gras  * than the alternative.
685c007436SBen Gras  */
695c007436SBen Gras int
check_yppasswdd(void)705c007436SBen Gras check_yppasswdd(void)
715c007436SBen Gras {
725c007436SBen Gras 	char *master;
735c007436SBen Gras 	int rpcport;
745c007436SBen Gras 
755c007436SBen Gras 	/*
765c007436SBen Gras 	 * Get local domain
775c007436SBen Gras 	 */
785c007436SBen Gras 	if (!domain && yp_get_default_domain(&domain) != 0)
795c007436SBen Gras 		return (1);
805c007436SBen Gras 
815c007436SBen Gras 	/*
825c007436SBen Gras 	 * Find the host for the passwd map; it should be running
835c007436SBen Gras 	 * the daemon.
845c007436SBen Gras 	 */
855c007436SBen Gras 	master = NULL;
865c007436SBen Gras 	if (yp_master(domain, "passwd.byname", &master) != 0) {
875c007436SBen Gras 		if (master != NULL)
885c007436SBen Gras 			free (master);
895c007436SBen Gras 		return (1);
905c007436SBen Gras 	}
915c007436SBen Gras 
925c007436SBen Gras 	/*
935c007436SBen Gras 	 * Ask the portmapper for the port of the daemon.
945c007436SBen Gras 	 */
955c007436SBen Gras 	if ((rpcport = getrpcport(master, YPPASSWDPROG, YPPASSWDPROC_UPDATE,
965c007436SBen Gras 	    IPPROTO_UDP)) == 0)
975c007436SBen Gras 		return (1);
985c007436SBen Gras 
995c007436SBen Gras 	/*
1005c007436SBen Gras 	 * Successful contact with rpc.yppasswdd.
1015c007436SBen Gras 	 */
1025c007436SBen Gras 	return (0);
1035c007436SBen Gras }
1045c007436SBen Gras 
1055c007436SBen Gras int
pw_yp(struct passwd * pw,uid_t ypuid)1065c007436SBen Gras pw_yp(struct passwd *pw, uid_t ypuid)
1075c007436SBen Gras {
1085c007436SBen Gras 	char *master;
1095c007436SBen Gras 	int r, rpcport, status;
1105c007436SBen Gras 	struct yppasswd yppw;
1115c007436SBen Gras 	struct timeval tv;
1125c007436SBen Gras 	CLIENT *client;
1135c007436SBen Gras 
1145c007436SBen Gras 	/*
1155c007436SBen Gras 	 * Get local domain
1165c007436SBen Gras 	 */
1175c007436SBen Gras 	if (!domain && (r = yp_get_default_domain(&domain)))
1185c007436SBen Gras 		errx(1, "can't get local YP domain.  Reason: %s",
1195c007436SBen Gras 		    yperr_string(r));
1205c007436SBen Gras 
1215c007436SBen Gras 	/*
1225c007436SBen Gras 	 * Find the host for the passwd map; it should be running
1235c007436SBen Gras 	 * the daemon.
1245c007436SBen Gras 	 */
1255c007436SBen Gras 	master = NULL;
1265c007436SBen Gras 	if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
1275c007436SBen Gras 		if (master)
1285c007436SBen Gras 			free (master);
1295c007436SBen Gras 		warnx("can't find the master YP server.  Reason: %s",
1305c007436SBen Gras 		    yperr_string(r));
1315c007436SBen Gras 		return (1);
1325c007436SBen Gras 	}
1335c007436SBen Gras 
1345c007436SBen Gras 	/*
1355c007436SBen Gras 	 * Ask the portmapper for the port of the daemon.
1365c007436SBen Gras 	 */
1375c007436SBen Gras 	if ((rpcport = getrpcport(master, YPPASSWDPROG, YPPASSWDPROC_UPDATE,
1385c007436SBen Gras 	    IPPROTO_UDP)) == 0) {
139*0a6a1f1dSLionel Sambuc 		warnx("master YP server not running yppasswd daemon.\n\t%s",
1405c007436SBen Gras 		    "Can't change password.");
1415c007436SBen Gras 		return (1);
1425c007436SBen Gras 	}
1435c007436SBen Gras 
1445c007436SBen Gras 	/*
1455c007436SBen Gras 	 * Be sure the port is privileged
1465c007436SBen Gras 	 */
1475c007436SBen Gras 	if (rpcport >= IPPORT_RESERVED) {
1485c007436SBen Gras 		warnx("yppasswd daemon is on an invalid port.");
1495c007436SBen Gras 		return (1);
1505c007436SBen Gras 	}
1515c007436SBen Gras 
1525c007436SBen Gras 	/* prompt for old password */
1535c007436SBen Gras 	memset(&yppw, 0, sizeof yppw);
1545c007436SBen Gras 	yppw.oldpass = getpass("Old password:");
1555c007436SBen Gras 	if (!yppw.oldpass) {
1565c007436SBen Gras 		warnx("Cancelled.");
1575c007436SBen Gras 		return (1);
1585c007436SBen Gras 	}
1595c007436SBen Gras 
1605c007436SBen Gras 	/* tell rpc.yppasswdd */
1615c007436SBen Gras 	yppw.newpw.pw_name	 = strdup(pw->pw_name);
1625c007436SBen Gras 	if (!yppw.newpw.pw_name) {
1635c007436SBen Gras 		err(1, "strdup");
1645c007436SBen Gras 		/*NOTREACHED*/
1655c007436SBen Gras 	}
1665c007436SBen Gras 	yppw.newpw.pw_passwd = strdup(pw->pw_passwd);
1675c007436SBen Gras 	if (!yppw.newpw.pw_passwd) {
1685c007436SBen Gras 		err(1, "strdup");
1695c007436SBen Gras 		/*NOTREACHED*/
1705c007436SBen Gras 	}
1715c007436SBen Gras 	yppw.newpw.pw_uid 	 = pw->pw_uid;
1725c007436SBen Gras 	yppw.newpw.pw_gid	 = pw->pw_gid;
1735c007436SBen Gras 	yppw.newpw.pw_gecos	 = strdup(pw->pw_gecos);
1745c007436SBen Gras 	if (!yppw.newpw.pw_gecos) {
1755c007436SBen Gras 		err(1, "strdup");
1765c007436SBen Gras 		/*NOTREACHED*/
1775c007436SBen Gras 	}
1785c007436SBen Gras 	yppw.newpw.pw_dir	 = strdup(pw->pw_dir);
1795c007436SBen Gras 	if (!yppw.newpw.pw_dir) {
1805c007436SBen Gras 		err(1, "strdup");
1815c007436SBen Gras 		/*NOTREACHED*/
1825c007436SBen Gras 	}
1835c007436SBen Gras 	yppw.newpw.pw_shell	 = strdup(pw->pw_shell);
1845c007436SBen Gras 	if (!yppw.newpw.pw_shell) {
1855c007436SBen Gras 		err(1, "strdup");
1865c007436SBen Gras 		/*NOTREACHED*/
1875c007436SBen Gras 	}
1885c007436SBen Gras 
1895c007436SBen Gras 	client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
1905c007436SBen Gras 	if (client == NULL) {
1915c007436SBen Gras 		warnx("cannot contact yppasswdd on %s:  Reason: %s",
1925c007436SBen Gras 		    master, yperr_string(YPERR_YPBIND));
1935c007436SBen Gras 		return (1);
1945c007436SBen Gras 	}
1955c007436SBen Gras 	client->cl_auth = authunix_create_default();
1965c007436SBen Gras 	tv.tv_sec = 5;
1975c007436SBen Gras 	tv.tv_usec = 0;
1985c007436SBen Gras 	r = clnt_call(client, YPPASSWDPROC_UPDATE,
1995c007436SBen Gras 	    xdr_yppasswd, &yppw, xdr_int, &status, tv);
2005c007436SBen Gras 	if (r) {
2015c007436SBen Gras 		warnx("rpc to yppasswdd failed.");
2025c007436SBen Gras 		return (1);
2035c007436SBen Gras 	} else if (status)
2045c007436SBen Gras 		printf("Couldn't change YP password.\n");
2055c007436SBen Gras 	else
2065c007436SBen Gras 		printf("%s %s, %s\n",
2075c007436SBen Gras 		    "The YP password information has been changed on",
2085c007436SBen Gras 		    master, "the master YP passwd server.");
2095c007436SBen Gras 	return (0);
2105c007436SBen Gras }
2115c007436SBen Gras 
2125c007436SBen Gras void
yppw_error(const char * name,int yperr,int eval)2135c007436SBen Gras yppw_error(const char *name, int yperr, int eval)
2145c007436SBen Gras {
2155c007436SBen Gras 
2165c007436SBen Gras 	if (yperr) {
2175c007436SBen Gras 		if (name)
2185c007436SBen Gras 			warn("%s", name);
2195c007436SBen Gras 		else
2205c007436SBen Gras 			warn(NULL);
2215c007436SBen Gras 	}
2225c007436SBen Gras 
2235c007436SBen Gras 	errx(eval, "YP passwd information unchanged");
2245c007436SBen Gras }
2255c007436SBen Gras 
2265c007436SBen Gras void
yppw_prompt(void)2275c007436SBen Gras yppw_prompt(void)
2285c007436SBen Gras {
2295c007436SBen Gras 	int c;
2305c007436SBen Gras 
2315c007436SBen Gras 	(void)printf("re-edit the password file? [y]: ");
2325c007436SBen Gras 	(void)fflush(stdout);
2335c007436SBen Gras 	c = getchar();
2345c007436SBen Gras 	if (c != EOF && c != '\n')
2355c007436SBen Gras 		while (getchar() != '\n');
2365c007436SBen Gras 	if (c == 'n')
2375c007436SBen Gras 		yppw_error(NULL, 0, 0);
2385c007436SBen Gras }
2395c007436SBen Gras #endif	/* YP */
240