ypbind.c revision 90297
11927Swollman/*
21927Swollman * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
31927Swollman * All rights reserved.
41927Swollman *
51927Swollman * Redistribution and use in source and binary forms, with or without
61927Swollman * modification, are permitted provided that the following conditions
71927Swollman * are met:
81927Swollman * 1. Redistributions of source code must retain the above copyright
91927Swollman *    notice, this list of conditions and the following disclaimer.
101927Swollman * 2. Redistributions in binary form must reproduce the above copyright
111927Swollman *    notice, this list of conditions and the following disclaimer in the
121927Swollman *    documentation and/or other materials provided with the distribution.
131927Swollman * 3. The name of the author may not be used to endorse or promote
141927Swollman *    products derived from this software without specific prior written
151927Swollman *    permission.
161927Swollman *
171927Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
181927Swollman * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
191927Swollman * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201927Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
211927Swollman * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221927Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231927Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241927Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251927Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261927Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271927Swollman * SUCH DAMAGE.
281927Swollman */
291927Swollman
3030762Scharnier#ifndef lint
3130762Scharnierstatic const char rcsid[] =
3250479Speter  "$FreeBSD: head/usr.sbin/ypbind/ypbind.c 90297 2002-02-06 13:30:31Z des $";
3330762Scharnier#endif /* not lint */
341927Swollman
351927Swollman#include <sys/param.h>
361927Swollman#include <sys/types.h>
378091Swpaul#include <sys/wait.h>
381927Swollman#include <sys/ioctl.h>
391927Swollman#include <sys/signal.h>
401927Swollman#include <sys/socket.h>
411927Swollman#include <sys/file.h>
421927Swollman#include <sys/fcntl.h>
438091Swpaul#include <sys/stat.h>
441927Swollman#include <sys/uio.h>
451927Swollman#include <ctype.h>
461927Swollman#include <dirent.h>
4730762Scharnier#include <err.h>
4830762Scharnier#include <errno.h>
491927Swollman#include <netdb.h>
5030762Scharnier#include <signal.h>
5130762Scharnier#include <stdio.h>
5230762Scharnier#include <stdlib.h>
531927Swollman#include <string.h>
5430762Scharnier#include <syslog.h>
5530762Scharnier#include <unistd.h>
561927Swollman#include <rpc/rpc.h>
571927Swollman#include <rpc/xdr.h>
581927Swollman#include <net/if.h>
596732Swpaul#include <netinet/in.h>
601927Swollman#include <arpa/inet.h>
611927Swollman#include <rpc/pmap_clnt.h>
621927Swollman#include <rpc/pmap_prot.h>
631927Swollman#include <rpc/pmap_rmt.h>
6430762Scharnier#include <rpc/rpc_com.h>
6512862Swpaul#include <rpcsvc/yp.h>
6612862Swpaulstruct dom_binding{};
671927Swollman#include <rpcsvc/ypclnt.h>
6826135Swpaul#include "yp_ping.h"
691927Swollman
701927Swollman#ifndef BINDINGDIR
711927Swollman#define BINDINGDIR "/var/yp/binding"
721927Swollman#endif
731927Swollman
748474Swpaul#ifndef YPBINDLOCK
758474Swpaul#define YPBINDLOCK "/var/run/ypbind.lock"
768474Swpaul#endif
778474Swpaul
781927Swollmanstruct _dom_binding {
791927Swollman	struct _dom_binding *dom_pnext;
801927Swollman	char dom_domain[YPMAXDOMAIN + 1];
811927Swollman	struct sockaddr_in dom_server_addr;
821927Swollman	long int dom_vers;
831927Swollman	int dom_lockfd;
841927Swollman	int dom_alive;
858755Swpaul	int dom_broadcast_pid;
868755Swpaul	int dom_pipe_fds[2];
878091Swpaul	int dom_default;
881927Swollman};
891927Swollman
908755Swpaul#define READFD ypdb->dom_pipe_fds[0]
918755Swpaul#define WRITEFD ypdb->dom_pipe_fds[1]
928755Swpaul#define BROADFD broad_domain->dom_pipe_fds[1]
938755Swpaul
941927Swollmanextern bool_t xdr_domainname(), xdr_ypbind_resp();
951927Swollmanextern bool_t xdr_ypreq_key(), xdr_ypresp_val();
961927Swollmanextern bool_t xdr_ypbind_setdom();
971927Swollman
9890297Sdesvoid	checkwork(void);
9990297Sdesvoid	*ypbindproc_null_2_yp(SVCXPRT *, void *, CLIENT *);
10090297Sdesvoid	*ypbindproc_setdom_2_yp(SVCXPRT *, struct ypbind_setdom *, CLIENT *);
10190297Sdesvoid	rpc_received(char *, struct sockaddr_in *, int);
10290297Sdesvoid	broadcast(struct _dom_binding *);
10390297Sdesint	ping(struct _dom_binding *);
10490297Sdesint	tell_parent(char *, struct sockaddr_in *);
10590297Sdesvoid	handle_children(struct _dom_binding *);
10690297Sdesvoid	reaper(int);
10790297Sdesvoid	terminate(int);
10890297Sdesvoid	yp_restricted_mode(char *);
10990297Sdesint	verify(struct in_addr);
1108091Swpaul
11112862Swpaulchar *domain_name;
1121927Swollmanstruct _dom_binding *ypbindlist;
1138755Swpaulstatic struct _dom_binding *broad_domain;
1141927Swollman
1151927Swollman#define YPSET_NO	0
1161927Swollman#define YPSET_LOCAL	1
1171927Swollman#define YPSET_ALL	2
1181927Swollmanint ypsetmode = YPSET_NO;
1196732Swpaulint ypsecuremode = 0;
12021581Swpaulint ppid;
1216732Swpaul
1229600Swpaul/*
1239600Swpaul * Special restricted mode variables: when in restricted mode, only the
1249600Swpaul * specified restricted_domain will be bound, and only the servers listed
1259600Swpaul * in restricted_addrs will be used for binding.
1269600Swpaul */
1279600Swpaul#define RESTRICTED_SERVERS 10
1289600Swpaulint yp_restricted = 0;
12926135Swpaulint yp_manycast = 0;
1309600Swpaulstruct in_addr restricted_addrs[RESTRICTED_SERVERS];
1319600Swpaul
1328091Swpaul/* No more than MAX_CHILDREN child broadcasters at a time. */
1338755Swpaul#ifndef MAX_CHILDREN
1348091Swpaul#define MAX_CHILDREN 5
1358755Swpaul#endif
1368425Swpaul/* No more than MAX_DOMAINS simultaneous domains */
1378755Swpaul#ifndef MAX_DOMAINS
1388425Swpaul#define MAX_DOMAINS 200
1398755Swpaul#endif
1408755Swpaul/* RPC timeout value */
1418425Swpaul#ifndef FAIL_THRESHOLD
14221539Swpaul#define FAIL_THRESHOLD 20
1438425Swpaul#endif
1448425Swpaul
1459600Swpaul/* Number of times to fish for a response froma particular set of hosts */
1469600Swpaul#ifndef MAX_RETRIES
1479600Swpaul#define MAX_RETRIES 30
1489600Swpaul#endif
1499600Swpaul
1509600Swpaulint retries = 0;
1518091Swpaulint children = 0;
1528425Swpaulint domains = 0;
1538474Swpaulint yplockfd;
1548755Swpaulfd_set fdsr;
1558091Swpaul
1561927SwollmanSVCXPRT *udptransp, *tcptransp;
1571927Swollman
1581927Swollmanvoid *
15932631Swpaulypbindproc_null_2_yp(transp, argp, clnt)
1601927SwollmanSVCXPRT *transp;
1611927Swollmanvoid *argp;
1621927SwollmanCLIENT *clnt;
1631927Swollman{
1641927Swollman	static char res;
1651927Swollman
1661927Swollman	bzero((char *)&res, sizeof(res));
1671927Swollman	return (void *)&res;
1681927Swollman}
1691927Swollman
1701927Swollmanstruct ypbind_resp *
17132631Swpaulypbindproc_domain_2_yp(transp, argp, clnt)
1721927SwollmanSVCXPRT *transp;
17312862Swpauldomainname *argp;
1741927SwollmanCLIENT *clnt;
1751927Swollman{
1761927Swollman	static struct ypbind_resp res;
1771927Swollman	struct _dom_binding *ypdb;
1781927Swollman	char path[MAXPATHLEN];
1791927Swollman
1801927Swollman	bzero((char *)&res, sizeof res);
1811927Swollman	res.ypbind_status = YPBIND_FAIL_VAL;
18212862Swpaul	res.ypbind_resp_u.ypbind_error = YPBIND_ERR_NOSERV;
1831927Swollman
18424782Swpaul	if (strchr(*argp, '/')) {
18524782Swpaul		syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \
18624782Swpaulrejecting.", *argp);
18724782Swpaul		return(&res);
18824782Swpaul	}
18924782Swpaul
19090297Sdes	for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
19190297Sdes		if (strcmp(ypdb->dom_domain, *argp) == 0)
1921927Swollman			break;
1938755Swpaul		}
1941927Swollman
19590297Sdes	if (ypdb == NULL) {
1969600Swpaul		if (yp_restricted) {
19712862Swpaul			syslog(LOG_NOTICE, "Running in restricted mode -- request to bind domain \"%s\" rejected.\n", *argp);
19890297Sdes			return (&res);
1999600Swpaul		}
2009600Swpaul
2018474Swpaul		if (domains >= MAX_DOMAINS) {
2028425Swpaul			syslog(LOG_WARNING, "domain limit (%d) exceeded",
2038425Swpaul							MAX_DOMAINS);
20412862Swpaul			res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
20590297Sdes			return (&res);
2068425Swpaul		}
2071927Swollman		ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
2088857Srgrimes		if (ypdb == NULL) {
20921539Swpaul			syslog(LOG_WARNING, "malloc: %m");
21012862Swpaul			res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
21190297Sdes			return (&res);
2128246Swpaul		}
2131927Swollman		bzero((char *)ypdb, sizeof *ypdb);
21412862Swpaul		strncpy(ypdb->dom_domain, *argp, sizeof ypdb->dom_domain);
2151927Swollman		ypdb->dom_vers = YPVERS;
2161927Swollman		ypdb->dom_alive = 0;
2178091Swpaul		ypdb->dom_default = 0;
2181927Swollman		ypdb->dom_lockfd = -1;
2198425Swpaul		sprintf(path, "%s/%s.%ld", BINDINGDIR,
2208425Swpaul					ypdb->dom_domain, ypdb->dom_vers);
2211927Swollman		unlink(path);
2221927Swollman		ypdb->dom_pnext = ypbindlist;
2231927Swollman		ypbindlist = ypdb;
2248425Swpaul		domains++;
2251927Swollman	}
2261927Swollman
2278755Swpaul	if (ping(ypdb)) {
22890297Sdes		return (&res);
2298755Swpaul	}
2301927Swollman
2311927Swollman	res.ypbind_status = YPBIND_SUCC_VAL;
23212862Swpaul	res.ypbind_resp_u.ypbind_error = 0; /* Success */
23345656Ssimokawa	*(u_int32_t *)&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr =
2341927Swollman		ypdb->dom_server_addr.sin_addr.s_addr;
23512862Swpaul	*(u_short *)&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port =
2368091Swpaul		ypdb->dom_server_addr.sin_port;
2371927Swollman	/*printf("domain %s at %s/%d\n", ypdb->dom_domain,
2381927Swollman		inet_ntoa(ypdb->dom_server_addr.sin_addr),
2391927Swollman		ntohs(ypdb->dom_server_addr.sin_port));*/
24090297Sdes	return (&res);
2411927Swollman}
2421927Swollman
2438755Swpaulvoid *
24432631Swpaulypbindproc_setdom_2_yp(transp, argp, clnt)
2451927SwollmanSVCXPRT *transp;
24612862Swpaulypbind_setdom *argp;
2471927SwollmanCLIENT *clnt;
2481927Swollman{
2491927Swollman	struct sockaddr_in *fromsin, bindsin;
25043854Swpaul	static char		*result = NULL;
2511927Swollman
25224782Swpaul	if (strchr(argp->ypsetdom_domain, '/')) {
25324782Swpaul		syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \
25424782Swpaulrejecting.", argp->ypsetdom_domain);
25530762Scharnier		return(NULL);
25624782Swpaul	}
2571927Swollman	fromsin = svc_getcaller(transp);
2581927Swollman
25990297Sdes	switch (ypsetmode) {
2601927Swollman	case YPSET_LOCAL:
26190297Sdes		if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
2628755Swpaul			svcerr_noprog(transp);
26330762Scharnier			return(NULL);
2648755Swpaul		}
2651927Swollman		break;
2661927Swollman	case YPSET_ALL:
2671927Swollman		break;
2681927Swollman	case YPSET_NO:
2691927Swollman	default:
2708755Swpaul		svcerr_noprog(transp);
27130762Scharnier		return(NULL);
2721927Swollman	}
2731927Swollman
27490297Sdes	if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
2758755Swpaul		svcerr_noprog(transp);
27630762Scharnier		return(NULL);
2778755Swpaul	}
2781927Swollman
27990297Sdes	if (argp->ypsetdom_vers != YPVERS) {
2808755Swpaul		svcerr_noprog(transp);
28130762Scharnier		return(NULL);
2828755Swpaul	}
2831927Swollman
2841927Swollman	bzero((char *)&bindsin, sizeof bindsin);
2851927Swollman	bindsin.sin_family = AF_INET;
28645656Ssimokawa	bindsin.sin_addr.s_addr = *(u_int32_t *)argp->ypsetdom_binding.ypbind_binding_addr;
28712862Swpaul	bindsin.sin_port = *(u_short *)argp->ypsetdom_binding.ypbind_binding_port;
2881927Swollman	rpc_received(argp->ypsetdom_domain, &bindsin, 1);
2891927Swollman
29043854Swpaul	return((void *) &result);
2911927Swollman}
2921927Swollman
2931927Swollmanstatic void
2941927Swollmanypbindprog_2(rqstp, transp)
2951927Swollmanstruct svc_req *rqstp;
2961927Swollmanregister SVCXPRT *transp;
2971927Swollman{
2981927Swollman	union {
29912862Swpaul		domainname ypbindproc_domain_2_arg;
3001927Swollman		struct ypbind_setdom ypbindproc_setdom_2_arg;
3011927Swollman	} argument;
3021927Swollman	struct authunix_parms *creds;
3031927Swollman	char *result;
3041927Swollman	bool_t (*xdr_argument)(), (*xdr_result)();
3051927Swollman	char *(*local)();
3061927Swollman
3071927Swollman	switch (rqstp->rq_proc) {
3081927Swollman	case YPBINDPROC_NULL:
3091927Swollman		xdr_argument = xdr_void;
3101927Swollman		xdr_result = xdr_void;
31132631Swpaul		local = (char *(*)()) ypbindproc_null_2_yp;
3121927Swollman		break;
3131927Swollman
3141927Swollman	case YPBINDPROC_DOMAIN:
3151927Swollman		xdr_argument = xdr_domainname;
3161927Swollman		xdr_result = xdr_ypbind_resp;
31732631Swpaul		local = (char *(*)()) ypbindproc_domain_2_yp;
3181927Swollman		break;
3191927Swollman
3201927Swollman	case YPBINDPROC_SETDOM:
32190297Sdes		switch (rqstp->rq_cred.oa_flavor) {
3221927Swollman		case AUTH_UNIX:
3231927Swollman			creds = (struct authunix_parms *)rqstp->rq_clntcred;
32490297Sdes			if (creds->aup_uid != 0) {
3251927Swollman				svcerr_auth(transp, AUTH_BADCRED);
3261927Swollman				return;
3271927Swollman			}
3281927Swollman			break;
3291927Swollman		default:
3301927Swollman			svcerr_auth(transp, AUTH_TOOWEAK);
3311927Swollman			return;
3321927Swollman		}
3331927Swollman
3341927Swollman		xdr_argument = xdr_ypbind_setdom;
3351927Swollman		xdr_result = xdr_void;
33632631Swpaul		local = (char *(*)()) ypbindproc_setdom_2_yp;
3371927Swollman		break;
3381927Swollman
3391927Swollman	default:
3401927Swollman		svcerr_noproc(transp);
3411927Swollman		return;
3421927Swollman	}
3431927Swollman	bzero((char *)&argument, sizeof(argument));
34421096Speter	if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
3451927Swollman		svcerr_decode(transp);
3461927Swollman		return;
3471927Swollman	}
3481927Swollman	result = (*local)(transp, &argument, rqstp);
3491927Swollman	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
3501927Swollman		svcerr_systemerr(transp);
3511927Swollman	}
3521927Swollman	return;
3531927Swollman}
3541927Swollman
3558091Swpaul/* Jack the reaper */
3568091Swpaulvoid reaper(sig)
3578091Swpaulint sig;
3588091Swpaul{
3598091Swpaul	int st;
3608091Swpaul
36190297Sdes	while (wait3(&st, WNOHANG, NULL) > 0)
3629532Swpaul		children--;
3638091Swpaul}
3648091Swpaul
3658425Swpaulvoid terminate(sig)
3668425Swpaulint sig;
3678425Swpaul{
3688425Swpaul	struct _dom_binding *ypdb;
3698425Swpaul	char path[MAXPATHLEN];
3708425Swpaul
37121581Swpaul	if (ppid != getpid())
37221581Swpaul		exit(0);
37321581Swpaul
37490297Sdes	for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
3758425Swpaul		close(ypdb->dom_lockfd);
3768755Swpaul		if (ypdb->dom_broadcast_pid)
3778755Swpaul			kill(ypdb->dom_broadcast_pid, SIGINT);
3788425Swpaul		sprintf(path, "%s/%s.%ld", BINDINGDIR,
3798425Swpaul			ypdb->dom_domain, ypdb->dom_vers);
3808425Swpaul		unlink(path);
3818425Swpaul	}
3828474Swpaul	close(yplockfd);
3838474Swpaul	unlink(YPBINDLOCK);
3848425Swpaul	pmap_unset(YPBINDPROG, YPBINDVERS);
3858425Swpaul	exit(0);
3868425Swpaul}
3878857Srgrimes
38821581Swpaulint
3891927Swollmanmain(argc, argv)
3908091Swpaulint argc;
3911927Swollmanchar **argv;
3921927Swollman{
3931927Swollman	struct timeval tv;
3941927Swollman	int i;
3958425Swpaul	DIR *dird;
3968425Swpaul	struct dirent *dirp;
39778674Sben	struct _dom_binding *ypdb, *next;
3981927Swollman
3998474Swpaul	/* Check that another ypbind isn't already running. */
40030762Scharnier	if ((yplockfd = (open(YPBINDLOCK, O_RDONLY|O_CREAT, 0444))) == -1)
40130762Scharnier		err(1, "%s", YPBINDLOCK);
4028474Swpaul
40390297Sdes	if (flock(yplockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
40430762Scharnier		errx(1, "another ypbind is already running. Aborting");
4058474Swpaul
4069600Swpaul	/* XXX domainname will be overriden if we use restricted mode */
40712862Swpaul	yp_get_default_domain(&domain_name);
40890297Sdes	if (domain_name[0] == '\0')
40930762Scharnier		errx(1, "domainname not set. Aborting");
4101927Swollman
41190297Sdes	for (i = 1; i<argc; i++) {
41290297Sdes		if (strcmp("-ypset", argv[i]) == 0)
4131927Swollman			ypsetmode = YPSET_ALL;
4141927Swollman		else if (strcmp("-ypsetme", argv[i]) == 0)
4156732Swpaul		        ypsetmode = YPSET_LOCAL;
4166732Swpaul		else if (strcmp("-s", argv[i]) == 0)
4176732Swpaul		        ypsecuremode++;
4189600Swpaul		else if (strcmp("-S", argv[i]) == 0 && argc > i)
41979953Sdd			yp_restricted_mode(argv[++i]);
42026135Swpaul		else if (strcmp("-m", argv[i]) == 0)
42126135Swpaul			yp_manycast++;
42279665Sdd		else
42379665Sdd			errx(1, "unknown option: %s", argv[i]);
4241927Swollman	}
4251927Swollman
4268425Swpaul	/* blow away everything in BINDINGDIR (if it exists) */
4271927Swollman
4288425Swpaul	if ((dird = opendir(BINDINGDIR)) != NULL) {
4298425Swpaul		char path[MAXPATHLEN];
4308425Swpaul		while ((dirp = readdir(dird)) != NULL)
4318425Swpaul			if (strcmp(dirp->d_name, ".") &&
4328425Swpaul			    strcmp(dirp->d_name, "..")) {
4338425Swpaul				sprintf(path,"%s/%s",BINDINGDIR,dirp->d_name);
4348425Swpaul				unlink(path);
4358425Swpaul			}
4368425Swpaul		closedir(dird);
4378425Swpaul	}
4381927Swollman
4391927Swollman#ifdef DAEMON
44030762Scharnier	if (daemon(0,0))
44130762Scharnier		err(1, "fork");
4421927Swollman#endif
4431927Swollman
4441927Swollman	pmap_unset(YPBINDPROG, YPBINDVERS);
4451927Swollman
4461927Swollman	udptransp = svcudp_create(RPC_ANYSOCK);
44730762Scharnier	if (udptransp == NULL)
44830762Scharnier		errx(1, "cannot create udp service");
4491927Swollman	if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
45030762Scharnier	    IPPROTO_UDP))
45130762Scharnier		errx(1, "unable to register (YPBINDPROG, YPBINDVERS, udp)");
4521927Swollman
4531927Swollman	tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
45430762Scharnier	if (tcptransp == NULL)
45530762Scharnier		errx(1, "cannot create tcp service");
4561927Swollman
4571927Swollman	if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
45830762Scharnier	    IPPROTO_TCP))
45930762Scharnier		errx(1, "unable to register (YPBINDPROG, YPBINDVERS, tcp)");
4601927Swollman
4611927Swollman	/* build initial domain binding, make it "unsuccessful" */
4621927Swollman	ypbindlist = (struct _dom_binding *)malloc(sizeof *ypbindlist);
46330762Scharnier	if (ypbindlist == NULL)
46430762Scharnier		errx(1, "malloc");
4651927Swollman	bzero((char *)ypbindlist, sizeof *ypbindlist);
46612862Swpaul	strncpy(ypbindlist->dom_domain, domain_name, sizeof ypbindlist->dom_domain);
4671927Swollman	ypbindlist->dom_vers = YPVERS;
4681927Swollman	ypbindlist->dom_alive = 0;
4691927Swollman	ypbindlist->dom_lockfd = -1;
4708091Swpaul	ypbindlist->dom_default = 1;
4718425Swpaul	domains++;
4721927Swollman
4738425Swpaul	signal(SIGCHLD, reaper);
4748425Swpaul	signal(SIGTERM, terminate);
4758425Swpaul
47621581Swpaul	ppid = getpid(); /* Remember who we are. */
47721581Swpaul
4788246Swpaul	openlog(argv[0], LOG_PID, LOG_DAEMON);
4796478Swpaul
4808425Swpaul	/* Kick off the default domain */
4818425Swpaul	broadcast(ypbindlist);
4828425Swpaul
48390297Sdes	while (1) {
4841927Swollman		fdsr = svc_fdset;
4858091Swpaul
4868425Swpaul		tv.tv_sec = 60;
4871927Swollman		tv.tv_usec = 0;
4881927Swollman
48990297Sdes		switch (select(_rpc_dtablesize(), &fdsr, NULL, NULL, &tv)) {
4901927Swollman		case 0:
4911927Swollman			checkwork();
4921927Swollman			break;
4931927Swollman		case -1:
4948755Swpaul			if (errno != EINTR)
49521539Swpaul				syslog(LOG_WARNING, "select: %m");
4968755Swpaul			break;
4971927Swollman		default:
49890297Sdes			for (ypdb = ypbindlist; ypdb; ypdb = next) {
49978674Sben				next = ypdb->dom_pnext;
5008755Swpaul				if (READFD > 0 && FD_ISSET(READFD, &fdsr)) {
5018853Swpaul					handle_children(ypdb);
5028755Swpaul					if (children == (MAX_CHILDREN - 1))
5038755Swpaul						checkwork();
5048091Swpaul				}
5051927Swollman			}
5061927Swollman			svc_getreqset(&fdsr);
5071927Swollman			break;
5081927Swollman		}
5091927Swollman	}
51021581Swpaul
51121581Swpaul	/* NOTREACHED */
51221581Swpaul	exit(1);
5131927Swollman}
5141927Swollman
5158091Swpaulvoid
5161927Swollmancheckwork()
5171927Swollman{
5181927Swollman	struct _dom_binding *ypdb;
5191927Swollman
52090297Sdes	for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
5218425Swpaul		ping(ypdb);
5221927Swollman}
5231927Swollman
5248091Swpaul/* The clnt_broadcast() callback mechanism sucks. */
5258091Swpaul
5268091Swpaul/*
5278091Swpaul * Receive results from broadcaster. Don't worry about passing
5288853Swpaul * bogus info to rpc_received() -- it can handle it. Note that we
5298853Swpaul * must be sure to invalidate the dom_pipe_fds descriptors here:
5308853Swpaul * since descriptors can be re-used, we have to make sure we
5318853Swpaul * don't mistake one of the RPC descriptors for one of the pipes.
5328853Swpaul * What's weird is that forgetting to invalidate the pipe descriptors
5338853Swpaul * doesn't always result in an error (otherwise I would have caught
5348853Swpaul * the mistake much sooner), even though logically it should.
5358091Swpaul */
5368853Swpaulvoid handle_children(ypdb)
5378853Swpaulstruct _dom_binding *ypdb;
5388091Swpaul{
5398091Swpaul	char buf[YPMAXDOMAIN + 1];
5408091Swpaul	struct sockaddr_in addr;
54121539Swpaul	int d = 0, a = 0;
54221539Swpaul	struct _dom_binding *y, *prev = NULL;
54321539Swpaul	char path[MAXPATHLEN];
5448091Swpaul
54521539Swpaul	if ((d = read(READFD, &buf, sizeof(buf))) <= 0)
54621539Swpaul		syslog(LOG_WARNING, "could not read from child: %m");
5478755Swpaul
54821539Swpaul	if ((a = read(READFD, &addr, sizeof(struct sockaddr_in))) < 0)
54921539Swpaul		syslog(LOG_WARNING, "could not read from child: %m");
55021539Swpaul
5518853Swpaul	close(READFD);
5528853Swpaul	FD_CLR(READFD, &fdsr);
5538853Swpaul	FD_CLR(READFD, &svc_fdset);
5548853Swpaul	READFD = WRITEFD = -1;
55521539Swpaul	if (d > 0 && a > 0)
55621539Swpaul		rpc_received((char *)&buf, &addr, 0);
55721539Swpaul	else {
55890297Sdes		for (y = ypbindlist; y; y = y->dom_pnext) {
55921539Swpaul			if (y == ypdb)
56021539Swpaul				break;
56121539Swpaul			prev = y;
56221539Swpaul		}
56390297Sdes		switch (ypdb->dom_default) {
56421539Swpaul		case 0:
56521539Swpaul			if (prev == NULL)
56621539Swpaul				ypbindlist = y->dom_pnext;
56721539Swpaul			else
56821539Swpaul				prev->dom_pnext = y->dom_pnext;
56921539Swpaul			sprintf(path, "%s/%s.%ld", BINDINGDIR,
57021539Swpaul				ypdb->dom_domain, YPVERS);
57121539Swpaul			close(ypdb->dom_lockfd);
57221539Swpaul			unlink(path);
57321539Swpaul			free(ypdb);
57421539Swpaul			domains--;
57521539Swpaul			return;
57621539Swpaul		case 1:
57721539Swpaul			ypdb->dom_broadcast_pid = 0;
57821539Swpaul			ypdb->dom_alive = 0;
57921539Swpaul			broadcast(ypdb);
58021539Swpaul			return;
58121539Swpaul		default:
58221539Swpaul			break;
58321539Swpaul		}
58421539Swpaul	}
58521539Swpaul
58621539Swpaul	return;
5878091Swpaul}
5888091Swpaul
5898091Swpaul/*
5908091Swpaul * Send our dying words back to our parent before we perish.
5918091Swpaul */
5928091Swpaulint
5938091Swpaultell_parent(dom, addr)
5941927Swollmanchar *dom;
5958091Swpaulstruct sockaddr_in *addr;
5961927Swollman{
5978091Swpaul	char buf[YPMAXDOMAIN + 1];
5988091Swpaul	struct timeval timeout;
5998091Swpaul	fd_set fds;
6001927Swollman
6018091Swpaul	timeout.tv_sec = 5;
6028091Swpaul	timeout.tv_usec = 0;
6031927Swollman
6048755Swpaul	sprintf(buf, "%s", broad_domain->dom_domain);
6058755Swpaul	if (write(BROADFD, &buf, sizeof(buf)) < 0)
6068091Swpaul		return(1);
6071927Swollman
6088091Swpaul	/*
6098091Swpaul	 * Stay in sync with parent: wait for it to read our first
6108091Swpaul	 * message before sending the second.
6118091Swpaul	 */
6121927Swollman
6138091Swpaul	FD_ZERO(&fds);
6148755Swpaul	FD_SET(BROADFD, &fds);
6158091Swpaul	if (select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == -1)
6168091Swpaul		return(1);
6178755Swpaul	if (FD_ISSET(BROADFD, &fds)) {
6188755Swpaul		if (write(BROADFD, addr, sizeof(struct sockaddr_in)) < 0)
6198091Swpaul			return(1);
6208091Swpaul	} else {
6218091Swpaul		return(1);
6221927Swollman	}
6231927Swollman
6248755Swpaul	close(BROADFD);
6258091Swpaul	return (0);
6268091Swpaul}
6271927Swollman
6288091Swpaulbool_t broadcast_result(out, addr)
6298091Swpaulbool_t *out;
6308091Swpaulstruct sockaddr_in *addr;
6318091Swpaul{
6329600Swpaul	if (retries >= MAX_RETRIES) {
6339600Swpaul		bzero((char *)addr, sizeof(struct sockaddr_in));
6349600Swpaul		if (tell_parent(broad_domain->dom_domain, addr))
6359600Swpaul			syslog(LOG_WARNING, "lost connection to parent");
63690297Sdes		return (TRUE);
6379600Swpaul	}
6389600Swpaul
6399600Swpaul	if (yp_restricted && verify(addr->sin_addr)) {
6409600Swpaul		retries++;
6419600Swpaul		syslog(LOG_NOTICE, "NIS server at %s not in restricted mode access list -- rejecting.\n",inet_ntoa(addr->sin_addr));
64290297Sdes		return (FALSE);
6439600Swpaul	} else {
6449600Swpaul		if (tell_parent(broad_domain->dom_domain, addr))
6459600Swpaul			syslog(LOG_WARNING, "lost connection to parent");
64690297Sdes		return (TRUE);
6479600Swpaul	}
6488091Swpaul}
6498091Swpaul
6508091Swpaul/*
6518091Swpaul * The right way to send RPC broadcasts.
6528091Swpaul * Use the clnt_broadcast() RPC service. Unfortunately, clnt_broadcast()
65372091Sasmodai * blocks while waiting for replies, so we have to fork off separate
6548091Swpaul * broadcaster processes that do the waiting and then transmit their
6558091Swpaul * results back to the parent for processing. We also have to remember
6568091Swpaul * to save the name of the domain we're trying to bind in a global
6578091Swpaul * variable since clnt_broadcast() provides no way to pass things to
6588091Swpaul * the 'eachresult' callback function.
6598091Swpaul */
6608091Swpaulvoid
6618091Swpaulbroadcast(ypdb)
6628091Swpaulstruct _dom_binding *ypdb;
6638091Swpaul{
6648091Swpaul	bool_t out = FALSE;
6658091Swpaul	enum clnt_stat stat;
6668091Swpaul
6678755Swpaul	if (children >= MAX_CHILDREN || ypdb->dom_broadcast_pid)
6688091Swpaul		return;
6698091Swpaul
6708755Swpaul	if (pipe(ypdb->dom_pipe_fds) < 0) {
67121539Swpaul		syslog(LOG_WARNING, "pipe: %m");
6728091Swpaul		return;
6731927Swollman	}
6741927Swollman
67530762Scharnier	if (ypdb->dom_vers == -1 && (long)ypdb->dom_server_addr.sin_addr.s_addr)
6768755Swpaul		syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" not responding",
6778425Swpaul		inet_ntoa(ypdb->dom_server_addr.sin_addr), ypdb->dom_domain);
6788425Swpaul
6798755Swpaul	broad_domain = ypdb;
6808425Swpaul	flock(ypdb->dom_lockfd, LOCK_UN);
6818425Swpaul
68290297Sdes	switch ((ypdb->dom_broadcast_pid = fork())) {
6838091Swpaul	case 0:
6848755Swpaul		close(READFD);
68521539Swpaul		signal(SIGCHLD, SIG_DFL);
68621539Swpaul		signal(SIGTERM, SIG_DFL);
6878091Swpaul		break;
6888091Swpaul	case -1:
68921539Swpaul		syslog(LOG_WARNING, "fork: %m");
6908755Swpaul		close(READFD);
6918755Swpaul		close(WRITEFD);
6928091Swpaul		return;
6938091Swpaul	default:
6948755Swpaul		close(WRITEFD);
6958755Swpaul		FD_SET(READFD, &svc_fdset);
6968091Swpaul		children++;
6978091Swpaul		return;
6981927Swollman	}
6996478Swpaul
7008755Swpaul	/* Release all locks before doing anything else. */
70190297Sdes	while (ypbindlist) {
7028755Swpaul		close(ypbindlist->dom_lockfd);
7038755Swpaul		ypbindlist = ypbindlist->dom_pnext;
7048755Swpaul	}
7058755Swpaul	close(yplockfd);
7068755Swpaul
70726135Swpaul	/*
70826135Swpaul	 * Special 'many-cast' behavior. If we're in restricted mode,
70926135Swpaul	 * we have a list of possible server addresses to try. What
71026135Swpaul	 * we can do is transmit to each ypserv's YPPROC_DOMAIN_NONACK
71126135Swpaul	 * procedure and time the replies. Whoever replies fastest
71226135Swpaul	 * gets to be our server. Note that this is not a broadcast
71326135Swpaul	 * operation: we transmit uni-cast datagrams only.
71426135Swpaul	 */
71526135Swpaul	if (yp_restricted && yp_manycast) {
71626135Swpaul		short			port;
71726135Swpaul		int			i;
71826135Swpaul		struct sockaddr_in	sin;
71926135Swpaul
72026135Swpaul		i = __yp_ping(restricted_addrs, yp_restricted,
72126135Swpaul				ypdb->dom_domain, &port);
72226135Swpaul		if (i == -1) {
72326135Swpaul			bzero((char *)&ypdb->dom_server_addr,
72426135Swpaul						sizeof(struct sockaddr_in));
72526135Swpaul			if (tell_parent(ypdb->dom_domain,
72626135Swpaul					&ypdb->dom_server_addr))
72726135Swpaul			syslog(LOG_WARNING, "lost connection to parent");
72826135Swpaul		} else {
72926135Swpaul			bzero((char *)&sin, sizeof(struct sockaddr_in));
73026135Swpaul			bcopy((char *)&restricted_addrs[i],
73126135Swpaul				(char *)&sin.sin_addr, sizeof(struct in_addr));
73226135Swpaul			sin.sin_family = AF_INET;
73326135Swpaul			sin.sin_port = port;
73426135Swpaul			if (tell_parent(broad_domain->dom_domain, &sin))
73526135Swpaul				syslog(LOG_WARNING,
73626135Swpaul					"lost connection to parent");
73726135Swpaul		}
73826135Swpaul		_exit(0);
73926135Swpaul	}
74026135Swpaul
7419600Swpaul	retries = 0;
7429600Swpaul
74312862Swpaul	{
74412862Swpaul		char *ptr;
7458091Swpaul
74612862Swpaul		ptr = (char *)&ypdb->dom_domain;
74712862Swpaul		stat = clnt_broadcast(YPPROG, YPVERS, YPPROC_DOMAIN_NONACK,
74812862Swpaul	    		xdr_domainname, (char *)&ptr, xdr_bool, (char *)&out,
74974462Salfred	    		(resultproc_t)broadcast_result);
75012862Swpaul	}
75112862Swpaul
7528091Swpaul	if (stat != RPC_SUCCESS) {
7538091Swpaul		bzero((char *)&ypdb->dom_server_addr,
7548091Swpaul						sizeof(struct sockaddr_in));
7558755Swpaul		if (tell_parent(ypdb->dom_domain, &ypdb->dom_server_addr))
7568091Swpaul			syslog(LOG_WARNING, "lost connection to parent");
7578091Swpaul	}
7588755Swpaul
75926135Swpaul	_exit(0);
7601927Swollman}
7611927Swollman
7628091Swpaul/*
7638091Swpaul * The right way to check if a server is alive.
7648091Swpaul * Attempt to get a client handle pointing to the server and send a
7658755Swpaul * YPPROC_DOMAIN. If we can't get a handle or we get a reply of FALSE,
7668755Swpaul * we invalidate this binding entry and send out a broadcast to try to
7678755Swpaul * establish a new binding. Note that we treat non-default domains
7688091Swpaul * specially: once bound, we keep tabs on our server, but if it
7698091Swpaul * goes away and fails to respond after one round of broadcasting, we
7708091Swpaul * abandon it until a client specifically references it again. We make
7718091Swpaul * every effort to keep our default domain bound, however, since we
7728091Swpaul * need it to keep the system on its feet.
7738091Swpaul */
7748091Swpaulint
7758425Swpaulping(ypdb)
7768091Swpaulstruct _dom_binding *ypdb;
7771927Swollman{
7788091Swpaul	bool_t out;
7798091Swpaul	struct timeval interval, timeout;
7808091Swpaul	enum clnt_stat stat;
7818091Swpaul	int rpcsock = RPC_ANYSOCK;
7828755Swpaul	CLIENT *client_handle;
7831927Swollman
7848755Swpaul	interval.tv_sec = FAIL_THRESHOLD;
7858091Swpaul	interval.tv_usec = 0;
7868091Swpaul	timeout.tv_sec = FAIL_THRESHOLD;
7878091Swpaul	timeout.tv_usec = 0;
7881927Swollman
7898755Swpaul	if (ypdb->dom_broadcast_pid)
7908091Swpaul		return(1);
7918091Swpaul
7928755Swpaul	if ((client_handle = clntudp_bufcreate(&ypdb->dom_server_addr,
7938755Swpaul		YPPROG, YPVERS, interval, &rpcsock, RPCSMALLMSGSIZE,
7948755Swpaul		RPCSMALLMSGSIZE)) == (CLIENT *)NULL) {
7958755Swpaul		/* Can't get a handle: we're dead. */
7968755Swpaul		ypdb->dom_alive = 0;
7978755Swpaul		ypdb->dom_vers = -1;
7988755Swpaul		broadcast(ypdb);
7998755Swpaul		return(1);
8001927Swollman	}
8011927Swollman
80212862Swpaul	{
80312862Swpaul		char *ptr;
80412862Swpaul
80512862Swpaul		ptr = (char *)&ypdb->dom_domain;
80612862Swpaul
80712862Swpaul		if ((stat = clnt_call(client_handle, YPPROC_DOMAIN,
80812862Swpaul			xdr_domainname, (char *)&ptr, xdr_bool, (char *)&out,
80912862Swpaul			timeout)) != RPC_SUCCESS || out == FALSE) {
81012862Swpaul			ypdb->dom_alive = 0;
81112862Swpaul			ypdb->dom_vers = -1;
81212862Swpaul			clnt_destroy(client_handle);
81312862Swpaul			broadcast(ypdb);
81412862Swpaul			return(1);
81512862Swpaul		}
8168091Swpaul	}
8171927Swollman
8188755Swpaul	clnt_destroy(client_handle);
8198091Swpaul	return(0);
8201927Swollman}
8211927Swollman
8228091Swpaulvoid rpc_received(dom, raddrp, force)
8231927Swollmanchar *dom;
8241927Swollmanstruct sockaddr_in *raddrp;
8251927Swollmanint force;
8261927Swollman{
8278425Swpaul	struct _dom_binding *ypdb, *prev = NULL;
8281927Swollman	struct iovec iov[2];
8291927Swollman	struct ypbind_resp ybr;
8301927Swollman	char path[MAXPATHLEN];
8311927Swollman	int fd;
8321927Swollman
8336732Swpaul	/*printf("returned from %s/%d about %s\n", inet_ntoa(raddrp->sin_addr),
8346732Swpaul	       ntohs(raddrp->sin_port), dom);*/
8351927Swollman
83690297Sdes	if (dom == NULL)
8371927Swollman		return;
8381927Swollman
83990297Sdes	for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
84090297Sdes		if (strcmp(ypdb->dom_domain, dom) == 0)
8418091Swpaul			break;
8428425Swpaul		prev = ypdb;
8438425Swpaul	}
8448091Swpaul
8458755Swpaul	if (ypdb && force) {
8468755Swpaul		if (ypdb->dom_broadcast_pid) {
8478755Swpaul			kill(ypdb->dom_broadcast_pid, SIGINT);
8488755Swpaul			close(READFD);
8498853Swpaul			FD_CLR(READFD, &fdsr);
8508853Swpaul			FD_CLR(READFD, &svc_fdset);
8518853Swpaul			READFD = WRITEFD = -1;
8528755Swpaul		}
8538755Swpaul	}
8548755Swpaul
8559600Swpaul	/* if in secure mode, check originating port number */
8569600Swpaul	if ((ypsecuremode && (ntohs(raddrp->sin_port) >= IPPORT_RESERVED))) {
8576732Swpaul	    syslog(LOG_WARNING, "Rejected NIS server on [%s/%d] for domain %s.",
8586732Swpaul		   inet_ntoa(raddrp->sin_addr), ntohs(raddrp->sin_port),
8596732Swpaul		   dom);
8608246Swpaul	    if (ypdb != NULL) {
8618755Swpaul		ypdb->dom_broadcast_pid = 0;
8628246Swpaul		ypdb->dom_alive = 0;
8638246Swpaul	    }
8646732Swpaul	    return;
8656732Swpaul	}
8666732Swpaul
8678246Swpaul	if (raddrp->sin_addr.s_addr == (long)0) {
86890297Sdes		switch (ypdb->dom_default) {
8698755Swpaul		case 0:
8708755Swpaul			if (prev == NULL)
8718755Swpaul				ypbindlist = ypdb->dom_pnext;
8728755Swpaul			else
8738755Swpaul				prev->dom_pnext = ypdb->dom_pnext;
8748755Swpaul			sprintf(path, "%s/%s.%ld", BINDINGDIR,
8758755Swpaul				ypdb->dom_domain, YPVERS);
8768755Swpaul			close(ypdb->dom_lockfd);
8778755Swpaul			unlink(path);
8788755Swpaul			free(ypdb);
8798755Swpaul			domains--;
8808755Swpaul			return;
8818755Swpaul		case 1:
8828755Swpaul			ypdb->dom_broadcast_pid = 0;
8838755Swpaul			ypdb->dom_alive = 0;
8848755Swpaul			broadcast(ypdb);
8858755Swpaul			return;
8868755Swpaul		default:
8878755Swpaul			break;
8888755Swpaul		}
8898246Swpaul	}
8908246Swpaul
89190297Sdes	if (ypdb == NULL) {
8928246Swpaul		if (force == 0)
8931927Swollman			return;
8941927Swollman		ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
8958857Srgrimes		if (ypdb == NULL) {
89621539Swpaul			syslog(LOG_WARNING, "malloc: %m");
8978246Swpaul			return;
8988246Swpaul		}
8991927Swollman		bzero((char *)ypdb, sizeof *ypdb);
9001927Swollman		strncpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain);
9011927Swollman		ypdb->dom_lockfd = -1;
9028091Swpaul		ypdb->dom_default = 0;
9031927Swollman		ypdb->dom_pnext = ypbindlist;
9041927Swollman		ypbindlist = ypdb;
9051927Swollman	}
9061927Swollman
9078091Swpaul	/* We've recovered from a crash: inform the world. */
90830762Scharnier	if (ypdb->dom_vers == -1 && ypdb->dom_server_addr.sin_addr.s_addr)
9098755Swpaul		syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" OK",
9108755Swpaul		inet_ntoa(raddrp->sin_addr), ypdb->dom_domain);
9116478Swpaul
9121927Swollman	bcopy((char *)raddrp, (char *)&ypdb->dom_server_addr,
9131927Swollman		sizeof ypdb->dom_server_addr);
9146478Swpaul
9151927Swollman	ypdb->dom_vers = YPVERS;
9161927Swollman	ypdb->dom_alive = 1;
9178755Swpaul	ypdb->dom_broadcast_pid = 0;
9181927Swollman
91990297Sdes	if (ypdb->dom_lockfd != -1)
9201927Swollman		close(ypdb->dom_lockfd);
9211927Swollman
9228091Swpaul	sprintf(path, "%s/%s.%ld", BINDINGDIR,
9231927Swollman		ypdb->dom_domain, ypdb->dom_vers);
9241927Swollman#ifdef O_SHLOCK
92590297Sdes	if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
9261927Swollman		(void)mkdir(BINDINGDIR, 0755);
92790297Sdes		if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
9281927Swollman			return;
9291927Swollman	}
9301927Swollman#else
93190297Sdes	if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
9321927Swollman		(void)mkdir(BINDINGDIR, 0755);
93390297Sdes		if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1)
9341927Swollman			return;
9351927Swollman	}
9361927Swollman	flock(fd, LOCK_SH);
9371927Swollman#endif
9381927Swollman
9391927Swollman	/*
9401927Swollman	 * ok, if BINDINGDIR exists, and we can create the binding file,
9411927Swollman	 * then write to it..
9421927Swollman	 */
9431927Swollman	ypdb->dom_lockfd = fd;
9441927Swollman
9451927Swollman	iov[0].iov_base = (caddr_t)&(udptransp->xp_port);
9461927Swollman	iov[0].iov_len = sizeof udptransp->xp_port;
9471927Swollman	iov[1].iov_base = (caddr_t)&ybr;
9481927Swollman	iov[1].iov_len = sizeof ybr;
9491927Swollman
9501927Swollman	bzero(&ybr, sizeof ybr);
9511927Swollman	ybr.ypbind_status = YPBIND_SUCC_VAL;
95245656Ssimokawa	*(u_int32_t *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr = raddrp->sin_addr.s_addr;
95312862Swpaul	*(u_short *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port = raddrp->sin_port;
9541927Swollman
95590297Sdes	if (writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) {
95621539Swpaul		syslog(LOG_WARNING, "write: %m");
9571927Swollman		close(ypdb->dom_lockfd);
9581927Swollman		ypdb->dom_lockfd = -1;
9591927Swollman		return;
9601927Swollman	}
9611927Swollman}
9629600Swpaul
9639600Swpaul/*
9649600Swpaul * Check address against list of allowed servers. Return 0 if okay,
9659600Swpaul * 1 if not matched.
9669600Swpaul */
9679600Swpaulint
9689600Swpaulverify(addr)
9699600Swpaulstruct in_addr addr;
9709600Swpaul{
9719600Swpaul	int i;
9729600Swpaul
9739600Swpaul	for (i = 0; i < RESTRICTED_SERVERS; i++)
9749600Swpaul		if (!bcmp((char *)&addr, (char *)&restricted_addrs[i],
9759600Swpaul			sizeof(struct in_addr)))
9769600Swpaul			return(0);
9779600Swpaul
9789600Swpaul	return(1);
9799600Swpaul}
9809600Swpaul
9819600Swpaul/*
9829600Swpaul * Try to set restricted mode. We default to normal mode if we can't
9839600Swpaul * resolve the specified hostnames.
9849600Swpaul */
9859600Swpaulvoid
9869600Swpaulyp_restricted_mode(args)
9879600Swpaulchar *args;
9889600Swpaul{
9899600Swpaul	struct hostent *h;
9909600Swpaul	int i = 0;
9919600Swpaul	char *s;
9929600Swpaul
9939600Swpaul	/* Find the restricted domain. */
9949600Swpaul	if ((s = strsep(&args, ",")) == NULL)
9959600Swpaul		return;
99612862Swpaul	domain_name = s;
9979600Swpaul
9989600Swpaul	/* Get the addresses of the servers. */
9999600Swpaul	while ((s = strsep(&args, ",")) != NULL && i < RESTRICTED_SERVERS) {
10009600Swpaul		if ((h = gethostbyname(s)) == NULL)
10019600Swpaul			return;
10029600Swpaul		bcopy ((char *)h->h_addr_list[0], (char *)&restricted_addrs[i],
10039600Swpaul			sizeof(struct in_addr));
10049600Swpaul	i++;
10059600Swpaul	}
10069600Swpaul
10079600Swpaul	/* ypset and ypsetme not allowed with restricted mode */
10089600Swpaul	ypsetmode = YPSET_NO;
100990297Sdes
101026135Swpaul	yp_restricted = i;
10119600Swpaul	return;
10129600Swpaul}
1013