ypbind.c revision 30762
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[] =
3230762Scharnier	"$Id$";
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
988091Swpaulvoid	checkwork __P((void));
998091Swpaulvoid	*ypbindproc_null_2 __P((SVCXPRT *, void *, CLIENT *));
1008755Swpaulvoid	*ypbindproc_setdom_2 __P((SVCXPRT *, struct ypbind_setdom *, CLIENT *));
1018091Swpaulvoid	rpc_received __P((char *, struct sockaddr_in *, int ));
1028091Swpaulvoid	broadcast __P((struct _dom_binding *));
1038425Swpaulint	ping __P((struct _dom_binding *));
1048755Swpaulint	tell_parent __P((char *, struct sockaddr_in *));
1058853Swpaulvoid	handle_children __P(( struct _dom_binding * ));
1068425Swpaulvoid	reaper __P((int));
1078425Swpaulvoid	terminate __P((int));
1089600Swpaulvoid	yp_restricted_mode __P((char *));
1099600Swpaulint	verify __P((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 *
1591927Swollmanypbindproc_null_2(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 *
1711927Swollmanypbindproc_domain_2(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
1908755Swpaul	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) {
19112862Swpaul		if( strcmp(ypdb->dom_domain, *argp) == 0)
1921927Swollman			break;
1938755Swpaul		}
1941927Swollman
1951927Swollman	if(ypdb==NULL) {
1969600Swpaul		if (yp_restricted) {
19712862Swpaul			syslog(LOG_NOTICE, "Running in restricted mode -- request to bind domain \"%s\" rejected.\n", *argp);
1989600Swpaul			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;
2058428Swpaul			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;
2118428Swpaul			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)) {
2287982Swpaul		return &res;
2298755Swpaul	}
2301927Swollman
2311927Swollman	res.ypbind_status = YPBIND_SUCC_VAL;
23212862Swpaul	res.ypbind_resp_u.ypbind_error = 0; /* Success */
23312862Swpaul	*(u_long *)&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));*/
2401927Swollman	return &res;
2411927Swollman}
2421927Swollman
2438755Swpaulvoid *
2441927Swollmanypbindproc_setdom_2(transp, argp, clnt)
2451927SwollmanSVCXPRT *transp;
24612862Swpaulypbind_setdom *argp;
2471927SwollmanCLIENT *clnt;
2481927Swollman{
2491927Swollman	struct sockaddr_in *fromsin, bindsin;
2501927Swollman
25124782Swpaul	if (strchr(argp->ypsetdom_domain, '/')) {
25224782Swpaul		syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \
25324782Swpaulrejecting.", argp->ypsetdom_domain);
25430762Scharnier		return(NULL);
25524782Swpaul	}
2561927Swollman	fromsin = svc_getcaller(transp);
2571927Swollman
2581927Swollman	switch(ypsetmode) {
2591927Swollman	case YPSET_LOCAL:
2608755Swpaul		if( fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
2618755Swpaul			svcerr_noprog(transp);
26230762Scharnier			return(NULL);
2638755Swpaul		}
2641927Swollman		break;
2651927Swollman	case YPSET_ALL:
2661927Swollman		break;
2671927Swollman	case YPSET_NO:
2681927Swollman	default:
2698755Swpaul		svcerr_noprog(transp);
27030762Scharnier		return(NULL);
2711927Swollman	}
2721927Swollman
2738755Swpaul	if(ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
2748755Swpaul		svcerr_noprog(transp);
27530762Scharnier		return(NULL);
2768755Swpaul	}
2771927Swollman
2788755Swpaul	if(argp->ypsetdom_vers != YPVERS) {
2798755Swpaul		svcerr_noprog(transp);
28030762Scharnier		return(NULL);
2818755Swpaul	}
2821927Swollman
2831927Swollman	bzero((char *)&bindsin, sizeof bindsin);
2841927Swollman	bindsin.sin_family = AF_INET;
28512862Swpaul	bindsin.sin_addr.s_addr = *(u_long *)argp->ypsetdom_binding.ypbind_binding_addr;
28612862Swpaul	bindsin.sin_port = *(u_short *)argp->ypsetdom_binding.ypbind_binding_port;
2871927Swollman	rpc_received(argp->ypsetdom_domain, &bindsin, 1);
2881927Swollman
28930762Scharnier	return(NULL);
2901927Swollman}
2911927Swollman
2921927Swollmanstatic void
2931927Swollmanypbindprog_2(rqstp, transp)
2941927Swollmanstruct svc_req *rqstp;
2951927Swollmanregister SVCXPRT *transp;
2961927Swollman{
2971927Swollman	union {
29812862Swpaul		domainname ypbindproc_domain_2_arg;
2991927Swollman		struct ypbind_setdom ypbindproc_setdom_2_arg;
3001927Swollman	} argument;
3011927Swollman	struct authunix_parms *creds;
3021927Swollman	char *result;
3031927Swollman	bool_t (*xdr_argument)(), (*xdr_result)();
3041927Swollman	char *(*local)();
3051927Swollman
3061927Swollman	switch (rqstp->rq_proc) {
3071927Swollman	case YPBINDPROC_NULL:
3081927Swollman		xdr_argument = xdr_void;
3091927Swollman		xdr_result = xdr_void;
3101927Swollman		local = (char *(*)()) ypbindproc_null_2;
3111927Swollman		break;
3121927Swollman
3131927Swollman	case YPBINDPROC_DOMAIN:
3141927Swollman		xdr_argument = xdr_domainname;
3151927Swollman		xdr_result = xdr_ypbind_resp;
3161927Swollman		local = (char *(*)()) ypbindproc_domain_2;
3171927Swollman		break;
3181927Swollman
3191927Swollman	case YPBINDPROC_SETDOM:
3201927Swollman		switch(rqstp->rq_cred.oa_flavor) {
3211927Swollman		case AUTH_UNIX:
3221927Swollman			creds = (struct authunix_parms *)rqstp->rq_clntcred;
3231927Swollman			if( creds->aup_uid != 0) {
3241927Swollman				svcerr_auth(transp, AUTH_BADCRED);
3251927Swollman				return;
3261927Swollman			}
3271927Swollman			break;
3281927Swollman		default:
3291927Swollman			svcerr_auth(transp, AUTH_TOOWEAK);
3301927Swollman			return;
3311927Swollman		}
3321927Swollman
3331927Swollman		xdr_argument = xdr_ypbind_setdom;
3341927Swollman		xdr_result = xdr_void;
3351927Swollman		local = (char *(*)()) ypbindproc_setdom_2;
3361927Swollman		break;
3371927Swollman
3381927Swollman	default:
3391927Swollman		svcerr_noproc(transp);
3401927Swollman		return;
3411927Swollman	}
3421927Swollman	bzero((char *)&argument, sizeof(argument));
34321096Speter	if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
3441927Swollman		svcerr_decode(transp);
3451927Swollman		return;
3461927Swollman	}
3471927Swollman	result = (*local)(transp, &argument, rqstp);
3481927Swollman	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
3491927Swollman		svcerr_systemerr(transp);
3501927Swollman	}
3511927Swollman	return;
3521927Swollman}
3531927Swollman
3548091Swpaul/* Jack the reaper */
3558091Swpaulvoid reaper(sig)
3568091Swpaulint sig;
3578091Swpaul{
3588091Swpaul	int st;
3598091Swpaul
3609532Swpaul	while(wait3(&st, WNOHANG, NULL) > 0)
3619532Swpaul		children--;
3628091Swpaul}
3638091Swpaul
3648425Swpaulvoid terminate(sig)
3658425Swpaulint sig;
3668425Swpaul{
3678425Swpaul	struct _dom_binding *ypdb;
3688425Swpaul	char path[MAXPATHLEN];
3698425Swpaul
37021581Swpaul	if (ppid != getpid())
37121581Swpaul		exit(0);
37221581Swpaul
3738425Swpaul	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) {
3748425Swpaul		close(ypdb->dom_lockfd);
3758755Swpaul		if (ypdb->dom_broadcast_pid)
3768755Swpaul			kill(ypdb->dom_broadcast_pid, SIGINT);
3778425Swpaul		sprintf(path, "%s/%s.%ld", BINDINGDIR,
3788425Swpaul			ypdb->dom_domain, ypdb->dom_vers);
3798425Swpaul		unlink(path);
3808425Swpaul	}
3818474Swpaul	close(yplockfd);
3828474Swpaul	unlink(YPBINDLOCK);
3838425Swpaul	pmap_unset(YPBINDPROG, YPBINDVERS);
3848425Swpaul	exit(0);
3858425Swpaul}
3868857Srgrimes
38721581Swpaulint
3881927Swollmanmain(argc, argv)
3898091Swpaulint argc;
3901927Swollmanchar **argv;
3911927Swollman{
3921927Swollman	struct timeval tv;
3931927Swollman	int i;
3948425Swpaul	DIR *dird;
3958425Swpaul	struct dirent *dirp;
3968755Swpaul	struct _dom_binding *ypdb;
3971927Swollman
3988474Swpaul	/* Check that another ypbind isn't already running. */
39930762Scharnier	if ((yplockfd = (open(YPBINDLOCK, O_RDONLY|O_CREAT, 0444))) == -1)
40030762Scharnier		err(1, "%s", YPBINDLOCK);
4018474Swpaul
40230762Scharnier	if(flock(yplockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
40330762Scharnier		errx(1, "another ypbind is already running. Aborting");
4048474Swpaul
4059600Swpaul	/* XXX domainname will be overriden if we use restricted mode */
40612862Swpaul	yp_get_default_domain(&domain_name);
40730762Scharnier	if( domain_name[0] == '\0')
40830762Scharnier		errx(1, "domainname not set. Aborting");
4091927Swollman
4101927Swollman	for(i=1; i<argc; i++) {
4111927Swollman		if( strcmp("-ypset", argv[i]) == 0)
4121927Swollman			ypsetmode = YPSET_ALL;
4131927Swollman		else if (strcmp("-ypsetme", argv[i]) == 0)
4146732Swpaul		        ypsetmode = YPSET_LOCAL;
4156732Swpaul		else if (strcmp("-s", argv[i]) == 0)
4166732Swpaul		        ypsecuremode++;
4179600Swpaul		else if (strcmp("-S", argv[i]) == 0 && argc > i)
4189600Swpaul			yp_restricted_mode(argv[i+1]);
41926135Swpaul		else if (strcmp("-m", argv[i]) == 0)
42026135Swpaul			yp_manycast++;
4211927Swollman	}
4221927Swollman
4238425Swpaul	/* blow away everything in BINDINGDIR (if it exists) */
4241927Swollman
4258425Swpaul	if ((dird = opendir(BINDINGDIR)) != NULL) {
4268425Swpaul		char path[MAXPATHLEN];
4278425Swpaul		while ((dirp = readdir(dird)) != NULL)
4288425Swpaul			if (strcmp(dirp->d_name, ".") &&
4298425Swpaul			    strcmp(dirp->d_name, "..")) {
4308425Swpaul				sprintf(path,"%s/%s",BINDINGDIR,dirp->d_name);
4318425Swpaul				unlink(path);
4328425Swpaul			}
4338425Swpaul		closedir(dird);
4348425Swpaul	}
4351927Swollman
4361927Swollman#ifdef DAEMON
43730762Scharnier	if (daemon(0,0))
43830762Scharnier		err(1, "fork");
4391927Swollman#endif
4401927Swollman
4411927Swollman	pmap_unset(YPBINDPROG, YPBINDVERS);
4421927Swollman
4431927Swollman	udptransp = svcudp_create(RPC_ANYSOCK);
44430762Scharnier	if (udptransp == NULL)
44530762Scharnier		errx(1, "cannot create udp service");
4461927Swollman	if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
44730762Scharnier	    IPPROTO_UDP))
44830762Scharnier		errx(1, "unable to register (YPBINDPROG, YPBINDVERS, udp)");
4491927Swollman
4501927Swollman	tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
45130762Scharnier	if (tcptransp == NULL)
45230762Scharnier		errx(1, "cannot create tcp service");
4531927Swollman
4541927Swollman	if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
45530762Scharnier	    IPPROTO_TCP))
45630762Scharnier		errx(1, "unable to register (YPBINDPROG, YPBINDVERS, tcp)");
4571927Swollman
4581927Swollman	/* build initial domain binding, make it "unsuccessful" */
4591927Swollman	ypbindlist = (struct _dom_binding *)malloc(sizeof *ypbindlist);
46030762Scharnier	if (ypbindlist == NULL)
46130762Scharnier		errx(1, "malloc");
4621927Swollman	bzero((char *)ypbindlist, sizeof *ypbindlist);
46312862Swpaul	strncpy(ypbindlist->dom_domain, domain_name, sizeof ypbindlist->dom_domain);
4641927Swollman	ypbindlist->dom_vers = YPVERS;
4651927Swollman	ypbindlist->dom_alive = 0;
4661927Swollman	ypbindlist->dom_lockfd = -1;
4678091Swpaul	ypbindlist->dom_default = 1;
4688425Swpaul	domains++;
4691927Swollman
4708425Swpaul	signal(SIGCHLD, reaper);
4718425Swpaul	signal(SIGTERM, terminate);
4728425Swpaul
47321581Swpaul	ppid = getpid(); /* Remember who we are. */
47421581Swpaul
4758246Swpaul	openlog(argv[0], LOG_PID, LOG_DAEMON);
4766478Swpaul
4778425Swpaul	/* Kick off the default domain */
4788425Swpaul	broadcast(ypbindlist);
4798425Swpaul
4801927Swollman	while(1) {
4811927Swollman		fdsr = svc_fdset;
4828091Swpaul
4838425Swpaul		tv.tv_sec = 60;
4841927Swollman		tv.tv_usec = 0;
4851927Swollman
4868091Swpaul		switch(select(_rpc_dtablesize(), &fdsr, NULL, NULL, &tv)) {
4871927Swollman		case 0:
4881927Swollman			checkwork();
4891927Swollman			break;
4901927Swollman		case -1:
4918755Swpaul			if (errno != EINTR)
49221539Swpaul				syslog(LOG_WARNING, "select: %m");
4938755Swpaul			break;
4941927Swollman		default:
4958755Swpaul			for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) {
4968755Swpaul				if (READFD > 0 && FD_ISSET(READFD, &fdsr)) {
4978853Swpaul					handle_children(ypdb);
4988755Swpaul					if (children == (MAX_CHILDREN - 1))
4998755Swpaul						checkwork();
5008091Swpaul				}
5011927Swollman			}
5021927Swollman			svc_getreqset(&fdsr);
5031927Swollman			break;
5041927Swollman		}
5051927Swollman	}
50621581Swpaul
50721581Swpaul	/* NOTREACHED */
50821581Swpaul	exit(1);
5091927Swollman}
5101927Swollman
5118091Swpaulvoid
5121927Swollmancheckwork()
5131927Swollman{
5141927Swollman	struct _dom_binding *ypdb;
5151927Swollman
5168425Swpaul	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext)
5178425Swpaul		ping(ypdb);
5181927Swollman}
5191927Swollman
5208091Swpaul/* The clnt_broadcast() callback mechanism sucks. */
5218091Swpaul
5228091Swpaul/*
5238091Swpaul * Receive results from broadcaster. Don't worry about passing
5248853Swpaul * bogus info to rpc_received() -- it can handle it. Note that we
5258853Swpaul * must be sure to invalidate the dom_pipe_fds descriptors here:
5268853Swpaul * since descriptors can be re-used, we have to make sure we
5278853Swpaul * don't mistake one of the RPC descriptors for one of the pipes.
5288853Swpaul * What's weird is that forgetting to invalidate the pipe descriptors
5298853Swpaul * doesn't always result in an error (otherwise I would have caught
5308853Swpaul * the mistake much sooner), even though logically it should.
5318091Swpaul */
5328853Swpaulvoid handle_children(ypdb)
5338853Swpaulstruct _dom_binding *ypdb;
5348091Swpaul{
5358091Swpaul	char buf[YPMAXDOMAIN + 1];
5368091Swpaul	struct sockaddr_in addr;
53721539Swpaul	int d = 0, a = 0;
53821539Swpaul	struct _dom_binding *y, *prev = NULL;
53921539Swpaul	char path[MAXPATHLEN];
5408091Swpaul
54121539Swpaul	if ((d = read(READFD, &buf, sizeof(buf))) <= 0)
54221539Swpaul		syslog(LOG_WARNING, "could not read from child: %m");
5438755Swpaul
54421539Swpaul	if ((a = read(READFD, &addr, sizeof(struct sockaddr_in))) < 0)
54521539Swpaul		syslog(LOG_WARNING, "could not read from child: %m");
54621539Swpaul
5478853Swpaul	close(READFD);
5488853Swpaul	FD_CLR(READFD, &fdsr);
5498853Swpaul	FD_CLR(READFD, &svc_fdset);
5508853Swpaul	READFD = WRITEFD = -1;
55121539Swpaul	if (d > 0 && a > 0)
55221539Swpaul		rpc_received((char *)&buf, &addr, 0);
55321539Swpaul	else {
55421539Swpaul		for(y=ypbindlist; y; y=y->dom_pnext) {
55521539Swpaul			if (y == ypdb)
55621539Swpaul				break;
55721539Swpaul			prev = y;
55821539Swpaul		}
55921539Swpaul		switch(ypdb->dom_default) {
56021539Swpaul		case 0:
56121539Swpaul			if (prev == NULL)
56221539Swpaul				ypbindlist = y->dom_pnext;
56321539Swpaul			else
56421539Swpaul				prev->dom_pnext = y->dom_pnext;
56521539Swpaul			sprintf(path, "%s/%s.%ld", BINDINGDIR,
56621539Swpaul				ypdb->dom_domain, YPVERS);
56721539Swpaul			close(ypdb->dom_lockfd);
56821539Swpaul			unlink(path);
56921539Swpaul			free(ypdb);
57021539Swpaul			domains--;
57121539Swpaul			return;
57221539Swpaul		case 1:
57321539Swpaul			ypdb->dom_broadcast_pid = 0;
57421539Swpaul			ypdb->dom_alive = 0;
57521539Swpaul			broadcast(ypdb);
57621539Swpaul			return;
57721539Swpaul		default:
57821539Swpaul			break;
57921539Swpaul		}
58021539Swpaul	}
58121539Swpaul
58221539Swpaul	return;
5838091Swpaul}
5848091Swpaul
5858091Swpaul/*
5868091Swpaul * Send our dying words back to our parent before we perish.
5878091Swpaul */
5888091Swpaulint
5898091Swpaultell_parent(dom, addr)
5901927Swollmanchar *dom;
5918091Swpaulstruct sockaddr_in *addr;
5921927Swollman{
5938091Swpaul	char buf[YPMAXDOMAIN + 1];
5948091Swpaul	struct timeval timeout;
5958091Swpaul	fd_set fds;
5961927Swollman
5978091Swpaul	timeout.tv_sec = 5;
5988091Swpaul	timeout.tv_usec = 0;
5991927Swollman
6008755Swpaul	sprintf(buf, "%s", broad_domain->dom_domain);
6018755Swpaul	if (write(BROADFD, &buf, sizeof(buf)) < 0)
6028091Swpaul		return(1);
6031927Swollman
6048091Swpaul	/*
6058091Swpaul	 * Stay in sync with parent: wait for it to read our first
6068091Swpaul	 * message before sending the second.
6078091Swpaul	 */
6081927Swollman
6098091Swpaul	FD_ZERO(&fds);
6108755Swpaul	FD_SET(BROADFD, &fds);
6118091Swpaul	if (select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == -1)
6128091Swpaul		return(1);
6138755Swpaul	if (FD_ISSET(BROADFD, &fds)) {
6148755Swpaul		if (write(BROADFD, addr, sizeof(struct sockaddr_in)) < 0)
6158091Swpaul			return(1);
6168091Swpaul	} else {
6178091Swpaul		return(1);
6181927Swollman	}
6191927Swollman
6208755Swpaul	close(BROADFD);
6218091Swpaul	return (0);
6228091Swpaul}
6231927Swollman
6248091Swpaulbool_t broadcast_result(out, addr)
6258091Swpaulbool_t *out;
6268091Swpaulstruct sockaddr_in *addr;
6278091Swpaul{
6289600Swpaul	if (retries >= MAX_RETRIES) {
6299600Swpaul		bzero((char *)addr, sizeof(struct sockaddr_in));
6309600Swpaul		if (tell_parent(broad_domain->dom_domain, addr))
6319600Swpaul			syslog(LOG_WARNING, "lost connection to parent");
6329600Swpaul		return TRUE;
6339600Swpaul	}
6349600Swpaul
6359600Swpaul	if (yp_restricted && verify(addr->sin_addr)) {
6369600Swpaul		retries++;
6379600Swpaul		syslog(LOG_NOTICE, "NIS server at %s not in restricted mode access list -- rejecting.\n",inet_ntoa(addr->sin_addr));
6389600Swpaul		return FALSE;
6399600Swpaul	} else {
6409600Swpaul		if (tell_parent(broad_domain->dom_domain, addr))
6419600Swpaul			syslog(LOG_WARNING, "lost connection to parent");
6429600Swpaul		return TRUE;
6439600Swpaul	}
6448091Swpaul}
6458091Swpaul
6468091Swpaul/*
6478091Swpaul * The right way to send RPC broadcasts.
6488091Swpaul * Use the clnt_broadcast() RPC service. Unfortunately, clnt_broadcast()
6498091Swpaul * blocks while waiting for replies, so we have to fork off seperate
6508091Swpaul * broadcaster processes that do the waiting and then transmit their
6518091Swpaul * results back to the parent for processing. We also have to remember
6528091Swpaul * to save the name of the domain we're trying to bind in a global
6538091Swpaul * variable since clnt_broadcast() provides no way to pass things to
6548091Swpaul * the 'eachresult' callback function.
6558091Swpaul */
6568091Swpaulvoid
6578091Swpaulbroadcast(ypdb)
6588091Swpaulstruct _dom_binding *ypdb;
6598091Swpaul{
6608091Swpaul	bool_t out = FALSE;
6618091Swpaul	enum clnt_stat stat;
6628091Swpaul
6638755Swpaul	if (children >= MAX_CHILDREN || ypdb->dom_broadcast_pid)
6648091Swpaul		return;
6658091Swpaul
6668755Swpaul	if (pipe(ypdb->dom_pipe_fds) < 0) {
66721539Swpaul		syslog(LOG_WARNING, "pipe: %m");
6688091Swpaul		return;
6691927Swollman	}
6701927Swollman
67130762Scharnier	if (ypdb->dom_vers == -1 && (long)ypdb->dom_server_addr.sin_addr.s_addr)
6728755Swpaul		syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" not responding",
6738425Swpaul		inet_ntoa(ypdb->dom_server_addr.sin_addr), ypdb->dom_domain);
6748425Swpaul
6758755Swpaul	broad_domain = ypdb;
6768425Swpaul	flock(ypdb->dom_lockfd, LOCK_UN);
6778425Swpaul
6788755Swpaul	switch((ypdb->dom_broadcast_pid = fork())) {
6798091Swpaul	case 0:
6808755Swpaul		close(READFD);
68121539Swpaul		signal(SIGCHLD, SIG_DFL);
68221539Swpaul		signal(SIGTERM, SIG_DFL);
6838091Swpaul		break;
6848091Swpaul	case -1:
68521539Swpaul		syslog(LOG_WARNING, "fork: %m");
6868755Swpaul		close(READFD);
6878755Swpaul		close(WRITEFD);
6888091Swpaul		return;
6898091Swpaul	default:
6908755Swpaul		close(WRITEFD);
6918755Swpaul		FD_SET(READFD, &svc_fdset);
6928091Swpaul		children++;
6938091Swpaul		return;
6941927Swollman	}
6956478Swpaul
6968755Swpaul	/* Release all locks before doing anything else. */
6978755Swpaul	while(ypbindlist) {
6988755Swpaul		close(ypbindlist->dom_lockfd);
6998755Swpaul		ypbindlist = ypbindlist->dom_pnext;
7008755Swpaul	}
7018755Swpaul	close(yplockfd);
7028755Swpaul
70326135Swpaul	/*
70426135Swpaul	 * Special 'many-cast' behavior. If we're in restricted mode,
70526135Swpaul	 * we have a list of possible server addresses to try. What
70626135Swpaul	 * we can do is transmit to each ypserv's YPPROC_DOMAIN_NONACK
70726135Swpaul	 * procedure and time the replies. Whoever replies fastest
70826135Swpaul	 * gets to be our server. Note that this is not a broadcast
70926135Swpaul	 * operation: we transmit uni-cast datagrams only.
71026135Swpaul	 */
71126135Swpaul	if (yp_restricted && yp_manycast) {
71226135Swpaul		short			port;
71326135Swpaul		int			i;
71426135Swpaul		struct sockaddr_in	sin;
71526135Swpaul
71626135Swpaul		i = __yp_ping(restricted_addrs, yp_restricted,
71726135Swpaul				ypdb->dom_domain, &port);
71826135Swpaul		if (i == -1) {
71926135Swpaul			bzero((char *)&ypdb->dom_server_addr,
72026135Swpaul						sizeof(struct sockaddr_in));
72126135Swpaul			if (tell_parent(ypdb->dom_domain,
72226135Swpaul					&ypdb->dom_server_addr))
72326135Swpaul			syslog(LOG_WARNING, "lost connection to parent");
72426135Swpaul		} else {
72526135Swpaul			bzero((char *)&sin, sizeof(struct sockaddr_in));
72626135Swpaul			bcopy((char *)&restricted_addrs[i],
72726135Swpaul				(char *)&sin.sin_addr, sizeof(struct in_addr));
72826135Swpaul			sin.sin_family = AF_INET;
72926135Swpaul			sin.sin_port = port;
73026135Swpaul			if (tell_parent(broad_domain->dom_domain, &sin))
73126135Swpaul				syslog(LOG_WARNING,
73226135Swpaul					"lost connection to parent");
73326135Swpaul		}
73426135Swpaul		_exit(0);
73526135Swpaul	}
73626135Swpaul
7379600Swpaul	retries = 0;
7389600Swpaul
73912862Swpaul	{
74012862Swpaul		char *ptr;
7418091Swpaul
74212862Swpaul		ptr = (char *)&ypdb->dom_domain;
74312862Swpaul		stat = clnt_broadcast(YPPROG, YPVERS, YPPROC_DOMAIN_NONACK,
74412862Swpaul	    		xdr_domainname, (char *)&ptr, xdr_bool, (char *)&out,
74512862Swpaul	    		broadcast_result);
74612862Swpaul	}
74712862Swpaul
7488091Swpaul	if (stat != RPC_SUCCESS) {
7498091Swpaul		bzero((char *)&ypdb->dom_server_addr,
7508091Swpaul						sizeof(struct sockaddr_in));
7518755Swpaul		if (tell_parent(ypdb->dom_domain, &ypdb->dom_server_addr))
7528091Swpaul			syslog(LOG_WARNING, "lost connection to parent");
7538091Swpaul	}
7548755Swpaul
75526135Swpaul	_exit(0);
7561927Swollman}
7571927Swollman
7588091Swpaul/*
7598091Swpaul * The right way to check if a server is alive.
7608091Swpaul * Attempt to get a client handle pointing to the server and send a
7618755Swpaul * YPPROC_DOMAIN. If we can't get a handle or we get a reply of FALSE,
7628755Swpaul * we invalidate this binding entry and send out a broadcast to try to
7638755Swpaul * establish a new binding. Note that we treat non-default domains
7648091Swpaul * specially: once bound, we keep tabs on our server, but if it
7658091Swpaul * goes away and fails to respond after one round of broadcasting, we
7668091Swpaul * abandon it until a client specifically references it again. We make
7678091Swpaul * every effort to keep our default domain bound, however, since we
7688091Swpaul * need it to keep the system on its feet.
7698091Swpaul */
7708091Swpaulint
7718425Swpaulping(ypdb)
7728091Swpaulstruct _dom_binding *ypdb;
7731927Swollman{
7748091Swpaul	bool_t out;
7758091Swpaul	struct timeval interval, timeout;
7768091Swpaul	enum clnt_stat stat;
7778091Swpaul	int rpcsock = RPC_ANYSOCK;
7788755Swpaul	CLIENT *client_handle;
7791927Swollman
7808755Swpaul	interval.tv_sec = FAIL_THRESHOLD;
7818091Swpaul	interval.tv_usec = 0;
7828091Swpaul	timeout.tv_sec = FAIL_THRESHOLD;
7838091Swpaul	timeout.tv_usec = 0;
7841927Swollman
7858755Swpaul	if (ypdb->dom_broadcast_pid)
7868091Swpaul		return(1);
7878091Swpaul
7888755Swpaul	if ((client_handle = clntudp_bufcreate(&ypdb->dom_server_addr,
7898755Swpaul		YPPROG, YPVERS, interval, &rpcsock, RPCSMALLMSGSIZE,
7908755Swpaul		RPCSMALLMSGSIZE)) == (CLIENT *)NULL) {
7918755Swpaul		/* Can't get a handle: we're dead. */
7928755Swpaul		ypdb->dom_alive = 0;
7938755Swpaul		ypdb->dom_vers = -1;
7948755Swpaul		broadcast(ypdb);
7958755Swpaul		return(1);
7961927Swollman	}
7971927Swollman
79812862Swpaul	{
79912862Swpaul		char *ptr;
80012862Swpaul
80112862Swpaul		ptr = (char *)&ypdb->dom_domain;
80212862Swpaul
80312862Swpaul		if ((stat = clnt_call(client_handle, YPPROC_DOMAIN,
80412862Swpaul			xdr_domainname, (char *)&ptr, xdr_bool, (char *)&out,
80512862Swpaul			timeout)) != RPC_SUCCESS || out == FALSE) {
80612862Swpaul			ypdb->dom_alive = 0;
80712862Swpaul			ypdb->dom_vers = -1;
80812862Swpaul			clnt_destroy(client_handle);
80912862Swpaul			broadcast(ypdb);
81012862Swpaul			return(1);
81112862Swpaul		}
8128091Swpaul	}
8131927Swollman
8148755Swpaul	clnt_destroy(client_handle);
8158091Swpaul	return(0);
8161927Swollman}
8171927Swollman
8188091Swpaulvoid rpc_received(dom, raddrp, force)
8191927Swollmanchar *dom;
8201927Swollmanstruct sockaddr_in *raddrp;
8211927Swollmanint force;
8221927Swollman{
8238425Swpaul	struct _dom_binding *ypdb, *prev = NULL;
8241927Swollman	struct iovec iov[2];
8251927Swollman	struct ypbind_resp ybr;
8261927Swollman	char path[MAXPATHLEN];
8271927Swollman	int fd;
8281927Swollman
8296732Swpaul	/*printf("returned from %s/%d about %s\n", inet_ntoa(raddrp->sin_addr),
8306732Swpaul	       ntohs(raddrp->sin_port), dom);*/
8311927Swollman
8321927Swollman	if(dom==NULL)
8331927Swollman		return;
8341927Swollman
8358425Swpaul	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) {
8368091Swpaul		if( strcmp(ypdb->dom_domain, dom) == 0)
8378091Swpaul			break;
8388425Swpaul		prev = ypdb;
8398425Swpaul	}
8408091Swpaul
8418755Swpaul	if (ypdb && force) {
8428755Swpaul		if (ypdb->dom_broadcast_pid) {
8438755Swpaul			kill(ypdb->dom_broadcast_pid, SIGINT);
8448755Swpaul			close(READFD);
8458853Swpaul			FD_CLR(READFD, &fdsr);
8468853Swpaul			FD_CLR(READFD, &svc_fdset);
8478853Swpaul			READFD = WRITEFD = -1;
8488755Swpaul		}
8498755Swpaul	}
8508755Swpaul
8519600Swpaul	/* if in secure mode, check originating port number */
8529600Swpaul	if ((ypsecuremode && (ntohs(raddrp->sin_port) >= IPPORT_RESERVED))) {
8536732Swpaul	    syslog(LOG_WARNING, "Rejected NIS server on [%s/%d] for domain %s.",
8546732Swpaul		   inet_ntoa(raddrp->sin_addr), ntohs(raddrp->sin_port),
8556732Swpaul		   dom);
8568246Swpaul	    if (ypdb != NULL) {
8578755Swpaul		ypdb->dom_broadcast_pid = 0;
8588246Swpaul		ypdb->dom_alive = 0;
8598246Swpaul	    }
8606732Swpaul	    return;
8616732Swpaul	}
8626732Swpaul
8638246Swpaul	if (raddrp->sin_addr.s_addr == (long)0) {
8648755Swpaul		switch(ypdb->dom_default) {
8658755Swpaul		case 0:
8668755Swpaul			if (prev == NULL)
8678755Swpaul				ypbindlist = ypdb->dom_pnext;
8688755Swpaul			else
8698755Swpaul				prev->dom_pnext = ypdb->dom_pnext;
8708755Swpaul			sprintf(path, "%s/%s.%ld", BINDINGDIR,
8718755Swpaul				ypdb->dom_domain, YPVERS);
8728755Swpaul			close(ypdb->dom_lockfd);
8738755Swpaul			unlink(path);
8748755Swpaul			free(ypdb);
8758755Swpaul			domains--;
8768755Swpaul			return;
8778755Swpaul		case 1:
8788755Swpaul			ypdb->dom_broadcast_pid = 0;
8798755Swpaul			ypdb->dom_alive = 0;
8808755Swpaul			broadcast(ypdb);
8818755Swpaul			return;
8828755Swpaul		default:
8838755Swpaul			break;
8848755Swpaul		}
8858246Swpaul	}
8868246Swpaul
8871927Swollman	if(ypdb==NULL) {
8888246Swpaul		if (force == 0)
8891927Swollman			return;
8901927Swollman		ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
8918857Srgrimes		if (ypdb == NULL) {
89221539Swpaul			syslog(LOG_WARNING, "malloc: %m");
8938246Swpaul			return;
8948246Swpaul		}
8951927Swollman		bzero((char *)ypdb, sizeof *ypdb);
8961927Swollman		strncpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain);
8971927Swollman		ypdb->dom_lockfd = -1;
8988091Swpaul		ypdb->dom_default = 0;
8991927Swollman		ypdb->dom_pnext = ypbindlist;
9001927Swollman		ypbindlist = ypdb;
9011927Swollman	}
9021927Swollman
9038091Swpaul	/* We've recovered from a crash: inform the world. */
90430762Scharnier	if (ypdb->dom_vers == -1 && ypdb->dom_server_addr.sin_addr.s_addr)
9058755Swpaul		syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" OK",
9068755Swpaul		inet_ntoa(raddrp->sin_addr), ypdb->dom_domain);
9076478Swpaul
9081927Swollman	bcopy((char *)raddrp, (char *)&ypdb->dom_server_addr,
9091927Swollman		sizeof ypdb->dom_server_addr);
9106478Swpaul
9111927Swollman	ypdb->dom_vers = YPVERS;
9121927Swollman	ypdb->dom_alive = 1;
9138755Swpaul	ypdb->dom_broadcast_pid = 0;
9141927Swollman
9151927Swollman	if(ypdb->dom_lockfd != -1)
9161927Swollman		close(ypdb->dom_lockfd);
9171927Swollman
9188091Swpaul	sprintf(path, "%s/%s.%ld", BINDINGDIR,
9191927Swollman		ypdb->dom_domain, ypdb->dom_vers);
9201927Swollman#ifdef O_SHLOCK
9217864Swpaul	if( (fd=open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
9221927Swollman		(void)mkdir(BINDINGDIR, 0755);
9237864Swpaul		if( (fd=open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
9241927Swollman			return;
9251927Swollman	}
9261927Swollman#else
9277864Swpaul	if( (fd=open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
9281927Swollman		(void)mkdir(BINDINGDIR, 0755);
9297864Swpaul		if( (fd=open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1)
9301927Swollman			return;
9311927Swollman	}
9321927Swollman	flock(fd, LOCK_SH);
9331927Swollman#endif
9341927Swollman
9351927Swollman	/*
9361927Swollman	 * ok, if BINDINGDIR exists, and we can create the binding file,
9371927Swollman	 * then write to it..
9381927Swollman	 */
9391927Swollman	ypdb->dom_lockfd = fd;
9401927Swollman
9411927Swollman	iov[0].iov_base = (caddr_t)&(udptransp->xp_port);
9421927Swollman	iov[0].iov_len = sizeof udptransp->xp_port;
9431927Swollman	iov[1].iov_base = (caddr_t)&ybr;
9441927Swollman	iov[1].iov_len = sizeof ybr;
9451927Swollman
9461927Swollman	bzero(&ybr, sizeof ybr);
9471927Swollman	ybr.ypbind_status = YPBIND_SUCC_VAL;
94812862Swpaul	*(u_long *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr = raddrp->sin_addr.s_addr;
94912862Swpaul	*(u_short *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port = raddrp->sin_port;
9501927Swollman
9511927Swollman	if( writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) {
95221539Swpaul		syslog(LOG_WARNING, "write: %m");
9531927Swollman		close(ypdb->dom_lockfd);
9541927Swollman		ypdb->dom_lockfd = -1;
9551927Swollman		return;
9561927Swollman	}
9571927Swollman}
9589600Swpaul
9599600Swpaul/*
9609600Swpaul * Check address against list of allowed servers. Return 0 if okay,
9619600Swpaul * 1 if not matched.
9629600Swpaul */
9639600Swpaulint
9649600Swpaulverify(addr)
9659600Swpaulstruct in_addr addr;
9669600Swpaul{
9679600Swpaul	int i;
9689600Swpaul
9699600Swpaul	for (i = 0; i < RESTRICTED_SERVERS; i++)
9709600Swpaul		if (!bcmp((char *)&addr, (char *)&restricted_addrs[i],
9719600Swpaul			sizeof(struct in_addr)))
9729600Swpaul			return(0);
9739600Swpaul
9749600Swpaul	return(1);
9759600Swpaul}
9769600Swpaul
9779600Swpaul/*
9789600Swpaul * Try to set restricted mode. We default to normal mode if we can't
9799600Swpaul * resolve the specified hostnames.
9809600Swpaul */
9819600Swpaulvoid
9829600Swpaulyp_restricted_mode(args)
9839600Swpaulchar *args;
9849600Swpaul{
9859600Swpaul	struct hostent *h;
9869600Swpaul	int i = 0;
9879600Swpaul	char *s;
9889600Swpaul
9899600Swpaul	/* Find the restricted domain. */
9909600Swpaul	if ((s = strsep(&args, ",")) == NULL)
9919600Swpaul		return;
99212862Swpaul	domain_name = s;
9939600Swpaul
9949600Swpaul	/* Get the addresses of the servers. */
9959600Swpaul	while ((s = strsep(&args, ",")) != NULL && i < RESTRICTED_SERVERS) {
9969600Swpaul		if ((h = gethostbyname(s)) == NULL)
9979600Swpaul			return;
9989600Swpaul		bcopy ((char *)h->h_addr_list[0], (char *)&restricted_addrs[i],
9999600Swpaul			sizeof(struct in_addr));
10009600Swpaul	i++;
10019600Swpaul	}
10029600Swpaul
10039600Swpaul	/* ypset and ypsetme not allowed with restricted mode */
10049600Swpaul	ypsetmode = YPSET_NO;
10059600Swpaul
100626135Swpaul	yp_restricted = i;
10079600Swpaul	return;
10089600Swpaul}
1009