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
30114601Sobrien#include <sys/cdefs.h>
31114601Sobrien__FBSDID("$FreeBSD$");
321927Swollman
331927Swollman#include <sys/param.h>
341927Swollman#include <sys/types.h>
358091Swpaul#include <sys/wait.h>
361927Swollman#include <sys/ioctl.h>
37226690Sglebius#include <sys/mman.h>
381927Swollman#include <sys/signal.h>
391927Swollman#include <sys/socket.h>
401927Swollman#include <sys/file.h>
411927Swollman#include <sys/fcntl.h>
428091Swpaul#include <sys/stat.h>
431927Swollman#include <sys/uio.h>
441927Swollman#include <ctype.h>
451927Swollman#include <dirent.h>
4630762Scharnier#include <err.h>
4730762Scharnier#include <errno.h>
481927Swollman#include <netdb.h>
4930762Scharnier#include <signal.h>
5030762Scharnier#include <stdio.h>
5130762Scharnier#include <stdlib.h>
521927Swollman#include <string.h>
5330762Scharnier#include <syslog.h>
5430762Scharnier#include <unistd.h>
551927Swollman#include <rpc/rpc.h>
561927Swollman#include <rpc/xdr.h>
571927Swollman#include <net/if.h>
586732Swpaul#include <netinet/in.h>
591927Swollman#include <arpa/inet.h>
601927Swollman#include <rpc/pmap_clnt.h>
611927Swollman#include <rpc/pmap_prot.h>
621927Swollman#include <rpc/pmap_rmt.h>
6330762Scharnier#include <rpc/rpc_com.h>
6412862Swpaul#include <rpcsvc/yp.h>
651927Swollman#include <rpcsvc/ypclnt.h>
6626135Swpaul#include "yp_ping.h"
671927Swollman
681927Swollman#ifndef BINDINGDIR
691927Swollman#define BINDINGDIR "/var/yp/binding"
701927Swollman#endif
711927Swollman
728474Swpaul#ifndef YPBINDLOCK
738474Swpaul#define YPBINDLOCK "/var/run/ypbind.lock"
748474Swpaul#endif
758474Swpaul
761927Swollmanstruct _dom_binding {
771927Swollman	struct _dom_binding *dom_pnext;
781927Swollman	char dom_domain[YPMAXDOMAIN + 1];
791927Swollman	struct sockaddr_in dom_server_addr;
801927Swollman	long int dom_vers;
811927Swollman	int dom_lockfd;
821927Swollman	int dom_alive;
838755Swpaul	int dom_broadcast_pid;
848755Swpaul	int dom_pipe_fds[2];
858091Swpaul	int dom_default;
861927Swollman};
871927Swollman
888755Swpaul#define READFD ypdb->dom_pipe_fds[0]
898755Swpaul#define WRITEFD ypdb->dom_pipe_fds[1]
908755Swpaul#define BROADFD broad_domain->dom_pipe_fds[1]
918755Swpaul
921927Swollmanextern bool_t xdr_domainname(), xdr_ypbind_resp();
931927Swollmanextern bool_t xdr_ypreq_key(), xdr_ypresp_val();
941927Swollmanextern bool_t xdr_ypbind_setdom();
951927Swollman
9690297Sdesvoid	checkwork(void);
9790297Sdesvoid	*ypbindproc_null_2_yp(SVCXPRT *, void *, CLIENT *);
9890297Sdesvoid	*ypbindproc_setdom_2_yp(SVCXPRT *, struct ypbind_setdom *, CLIENT *);
9990297Sdesvoid	rpc_received(char *, struct sockaddr_in *, int);
10090297Sdesvoid	broadcast(struct _dom_binding *);
10190297Sdesint	ping(struct _dom_binding *);
10290297Sdesint	tell_parent(char *, struct sockaddr_in *);
10390297Sdesvoid	handle_children(struct _dom_binding *);
10490297Sdesvoid	reaper(int);
10590297Sdesvoid	terminate(int);
10690297Sdesvoid	yp_restricted_mode(char *);
10790297Sdesint	verify(struct in_addr);
1088091Swpaul
10912862Swpaulchar *domain_name;
1101927Swollmanstruct _dom_binding *ypbindlist;
1118755Swpaulstatic struct _dom_binding *broad_domain;
1121927Swollman
1131927Swollman#define YPSET_NO	0
1141927Swollman#define YPSET_LOCAL	1
1151927Swollman#define YPSET_ALL	2
1161927Swollmanint ypsetmode = YPSET_NO;
1176732Swpaulint ypsecuremode = 0;
11821581Swpaulint ppid;
1196732Swpaul
120213611Smarkm#define NOT_RESPONDING_HYSTERESIS 10
121213611Smarkmstatic int not_responding_count = 0;
122213611Smarkm
1239600Swpaul/*
1249600Swpaul * Special restricted mode variables: when in restricted mode, only the
1259600Swpaul * specified restricted_domain will be bound, and only the servers listed
1269600Swpaul * in restricted_addrs will be used for binding.
1279600Swpaul */
1289600Swpaul#define RESTRICTED_SERVERS 10
1299600Swpaulint yp_restricted = 0;
13026135Swpaulint yp_manycast = 0;
1319600Swpaulstruct in_addr restricted_addrs[RESTRICTED_SERVERS];
1329600Swpaul
1338091Swpaul/* No more than MAX_CHILDREN child broadcasters at a time. */
1348755Swpaul#ifndef MAX_CHILDREN
1358091Swpaul#define MAX_CHILDREN 5
1368755Swpaul#endif
1378425Swpaul/* No more than MAX_DOMAINS simultaneous domains */
1388755Swpaul#ifndef MAX_DOMAINS
1398425Swpaul#define MAX_DOMAINS 200
1408755Swpaul#endif
1418755Swpaul/* RPC timeout value */
1428425Swpaul#ifndef FAIL_THRESHOLD
14321539Swpaul#define FAIL_THRESHOLD 20
1448425Swpaul#endif
1458425Swpaul
1469600Swpaul/* Number of times to fish for a response froma particular set of hosts */
1479600Swpaul#ifndef MAX_RETRIES
1489600Swpaul#define MAX_RETRIES 30
1499600Swpaul#endif
1509600Swpaul
1519600Swpaulint retries = 0;
1528091Swpaulint children = 0;
1538425Swpaulint domains = 0;
1548474Swpaulint yplockfd;
1558755Swpaulfd_set fdsr;
1568091Swpaul
1571927SwollmanSVCXPRT *udptransp, *tcptransp;
1581927Swollman
1591927Swollmanvoid *
16090298Sdesypbindproc_null_2_yp(SVCXPRT *transp, void *argp, CLIENT *clnt)
1611927Swollman{
1621927Swollman	static char res;
1631927Swollman
16495658Sdes	bzero(&res, sizeof(res));
16595658Sdes	return &res;
1661927Swollman}
1671927Swollman
1681927Swollmanstruct ypbind_resp *
16990298Sdesypbindproc_domain_2_yp(SVCXPRT *transp, domainname *argp, CLIENT *clnt)
1701927Swollman{
1711927Swollman	static struct ypbind_resp res;
1721927Swollman	struct _dom_binding *ypdb;
1731927Swollman	char path[MAXPATHLEN];
1741927Swollman
17595658Sdes	bzero(&res, sizeof res);
1761927Swollman	res.ypbind_status = YPBIND_FAIL_VAL;
17712862Swpaul	res.ypbind_resp_u.ypbind_error = YPBIND_ERR_NOSERV;
1781927Swollman
17924782Swpaul	if (strchr(*argp, '/')) {
18024782Swpaul		syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \
18124782Swpaulrejecting.", *argp);
18224782Swpaul		return(&res);
18324782Swpaul	}
18424782Swpaul
18590297Sdes	for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
18690297Sdes		if (strcmp(ypdb->dom_domain, *argp) == 0)
1871927Swollman			break;
1888755Swpaul		}
1891927Swollman
19090297Sdes	if (ypdb == NULL) {
1919600Swpaul		if (yp_restricted) {
19212862Swpaul			syslog(LOG_NOTICE, "Running in restricted mode -- request to bind domain \"%s\" rejected.\n", *argp);
19390297Sdes			return (&res);
1949600Swpaul		}
1959600Swpaul
1968474Swpaul		if (domains >= MAX_DOMAINS) {
1978425Swpaul			syslog(LOG_WARNING, "domain limit (%d) exceeded",
1988425Swpaul							MAX_DOMAINS);
19912862Swpaul			res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
20090297Sdes			return (&res);
2018425Swpaul		}
202300268Struckman		if (strlen(*argp) > YPMAXDOMAIN) {
203300268Struckman			syslog(LOG_WARNING, "domain %s too long", *argp);
204300268Struckman			res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
205300268Struckman			return (&res);
206300268Struckman		}
207300268Struckman		ypdb = 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		}
21395658Sdes		bzero(ypdb, sizeof *ypdb);
214300268Struckman		strlcpy(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 *
24490298Sdesypbindproc_setdom_2_yp(SVCXPRT *transp, ypbind_setdom *argp, CLIENT *clnt)
2451927Swollman{
2461927Swollman	struct sockaddr_in *fromsin, bindsin;
24743854Swpaul	static char		*result = NULL;
2481927Swollman
24924782Swpaul	if (strchr(argp->ypsetdom_domain, '/')) {
25024782Swpaul		syslog(LOG_WARNING, "Domain name '%s' has embedded slash -- \
25124782Swpaulrejecting.", argp->ypsetdom_domain);
25230762Scharnier		return(NULL);
25324782Swpaul	}
2541927Swollman	fromsin = svc_getcaller(transp);
2551927Swollman
25690297Sdes	switch (ypsetmode) {
2571927Swollman	case YPSET_LOCAL:
25890297Sdes		if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
2598755Swpaul			svcerr_noprog(transp);
26030762Scharnier			return(NULL);
2618755Swpaul		}
2621927Swollman		break;
2631927Swollman	case YPSET_ALL:
2641927Swollman		break;
2651927Swollman	case YPSET_NO:
2661927Swollman	default:
2678755Swpaul		svcerr_noprog(transp);
26830762Scharnier		return(NULL);
2691927Swollman	}
2701927Swollman
27190297Sdes	if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
2728755Swpaul		svcerr_noprog(transp);
27330762Scharnier		return(NULL);
2748755Swpaul	}
2751927Swollman
27690297Sdes	if (argp->ypsetdom_vers != YPVERS) {
2778755Swpaul		svcerr_noprog(transp);
27830762Scharnier		return(NULL);
2798755Swpaul	}
2801927Swollman
28195658Sdes	bzero(&bindsin, sizeof bindsin);
2821927Swollman	bindsin.sin_family = AF_INET;
28345656Ssimokawa	bindsin.sin_addr.s_addr = *(u_int32_t *)argp->ypsetdom_binding.ypbind_binding_addr;
28412862Swpaul	bindsin.sin_port = *(u_short *)argp->ypsetdom_binding.ypbind_binding_port;
2851927Swollman	rpc_received(argp->ypsetdom_domain, &bindsin, 1);
2861927Swollman
28743854Swpaul	return((void *) &result);
2881927Swollman}
2891927Swollman
29095658Sdesvoid
29190298Sdesypbindprog_2(struct svc_req *rqstp, register SVCXPRT *transp)
2921927Swollman{
2931927Swollman	union {
29412862Swpaul		domainname ypbindproc_domain_2_arg;
2951927Swollman		struct ypbind_setdom ypbindproc_setdom_2_arg;
2961927Swollman	} argument;
2971927Swollman	struct authunix_parms *creds;
2981927Swollman	char *result;
2991927Swollman	bool_t (*xdr_argument)(), (*xdr_result)();
3001927Swollman	char *(*local)();
3011927Swollman
3021927Swollman	switch (rqstp->rq_proc) {
3031927Swollman	case YPBINDPROC_NULL:
3041927Swollman		xdr_argument = xdr_void;
3051927Swollman		xdr_result = xdr_void;
30632631Swpaul		local = (char *(*)()) ypbindproc_null_2_yp;
3071927Swollman		break;
3081927Swollman
3091927Swollman	case YPBINDPROC_DOMAIN:
3101927Swollman		xdr_argument = xdr_domainname;
3111927Swollman		xdr_result = xdr_ypbind_resp;
31232631Swpaul		local = (char *(*)()) ypbindproc_domain_2_yp;
3131927Swollman		break;
3141927Swollman
3151927Swollman	case YPBINDPROC_SETDOM:
31690297Sdes		switch (rqstp->rq_cred.oa_flavor) {
3171927Swollman		case AUTH_UNIX:
3181927Swollman			creds = (struct authunix_parms *)rqstp->rq_clntcred;
31990297Sdes			if (creds->aup_uid != 0) {
3201927Swollman				svcerr_auth(transp, AUTH_BADCRED);
3211927Swollman				return;
3221927Swollman			}
3231927Swollman			break;
3241927Swollman		default:
3251927Swollman			svcerr_auth(transp, AUTH_TOOWEAK);
3261927Swollman			return;
3271927Swollman		}
3281927Swollman
3291927Swollman		xdr_argument = xdr_ypbind_setdom;
3301927Swollman		xdr_result = xdr_void;
33132631Swpaul		local = (char *(*)()) ypbindproc_setdom_2_yp;
3321927Swollman		break;
3331927Swollman
3341927Swollman	default:
3351927Swollman		svcerr_noproc(transp);
3361927Swollman		return;
3371927Swollman	}
33895658Sdes	bzero(&argument, sizeof(argument));
33995658Sdes	if (!svc_getargs(transp, (xdrproc_t)xdr_argument, &argument)) {
3401927Swollman		svcerr_decode(transp);
3411927Swollman		return;
3421927Swollman	}
3431927Swollman	result = (*local)(transp, &argument, rqstp);
34495658Sdes	if (result != NULL &&
34595658Sdes	    !svc_sendreply(transp, (xdrproc_t)xdr_result, result)) {
3461927Swollman		svcerr_systemerr(transp);
3471927Swollman	}
3481927Swollman	return;
3491927Swollman}
3501927Swollman
3518091Swpaul/* Jack the reaper */
35290298Sdesvoid
35390298Sdesreaper(int sig)
3548091Swpaul{
3558091Swpaul	int st;
3568091Swpaul
35790297Sdes	while (wait3(&st, WNOHANG, NULL) > 0)
3589532Swpaul		children--;
3598091Swpaul}
3608091Swpaul
36190298Sdesvoid
36290298Sdesterminate(int sig)
3638425Swpaul{
3648425Swpaul	struct _dom_binding *ypdb;
3658425Swpaul	char path[MAXPATHLEN];
3668425Swpaul
36721581Swpaul	if (ppid != getpid())
36821581Swpaul		exit(0);
36921581Swpaul
37090297Sdes	for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
3718425Swpaul		close(ypdb->dom_lockfd);
3728755Swpaul		if (ypdb->dom_broadcast_pid)
3738755Swpaul			kill(ypdb->dom_broadcast_pid, SIGINT);
3748425Swpaul		sprintf(path, "%s/%s.%ld", BINDINGDIR,
3758425Swpaul			ypdb->dom_domain, ypdb->dom_vers);
3768425Swpaul		unlink(path);
3778425Swpaul	}
3788474Swpaul	close(yplockfd);
3798474Swpaul	unlink(YPBINDLOCK);
3808425Swpaul	pmap_unset(YPBINDPROG, YPBINDVERS);
3818425Swpaul	exit(0);
3828425Swpaul}
3838857Srgrimes
38421581Swpaulint
38590298Sdesmain(int argc, char *argv[])
3861927Swollman{
3871927Swollman	struct timeval tv;
3881927Swollman	int i;
3898425Swpaul	DIR *dird;
3908425Swpaul	struct dirent *dirp;
39178674Sben	struct _dom_binding *ypdb, *next;
3921927Swollman
3938474Swpaul	/* Check that another ypbind isn't already running. */
39430762Scharnier	if ((yplockfd = (open(YPBINDLOCK, O_RDONLY|O_CREAT, 0444))) == -1)
39530762Scharnier		err(1, "%s", YPBINDLOCK);
3968474Swpaul
39790297Sdes	if (flock(yplockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
39830762Scharnier		errx(1, "another ypbind is already running. Aborting");
3998474Swpaul
400293290Sbdrewery	/* XXX domainname will be overridden if we use restricted mode */
40112862Swpaul	yp_get_default_domain(&domain_name);
40290297Sdes	if (domain_name[0] == '\0')
40330762Scharnier		errx(1, "domainname not set. Aborting");
4041927Swollman
40590297Sdes	for (i = 1; i<argc; i++) {
40690297Sdes		if (strcmp("-ypset", argv[i]) == 0)
4071927Swollman			ypsetmode = YPSET_ALL;
4081927Swollman		else if (strcmp("-ypsetme", argv[i]) == 0)
4096732Swpaul		        ypsetmode = YPSET_LOCAL;
4106732Swpaul		else if (strcmp("-s", argv[i]) == 0)
4116732Swpaul		        ypsecuremode++;
4129600Swpaul		else if (strcmp("-S", argv[i]) == 0 && argc > i)
41379953Sdd			yp_restricted_mode(argv[++i]);
41426135Swpaul		else if (strcmp("-m", argv[i]) == 0)
41526135Swpaul			yp_manycast++;
41679665Sdd		else
41779665Sdd			errx(1, "unknown option: %s", argv[i]);
4181927Swollman	}
4191927Swollman
420300268Struckman	if (strlen(domain_name) > YPMAXDOMAIN)
421300268Struckman		warnx("truncating domain name %s", domain_name);
422300268Struckman
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" */
459300268Struckman	ypbindlist = malloc(sizeof *ypbindlist);
46030762Scharnier	if (ypbindlist == NULL)
46130762Scharnier		errx(1, "malloc");
46295658Sdes	bzero(ypbindlist, sizeof *ypbindlist);
463300268Struckman	strlcpy(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
477226690Sglebius	if (madvise(NULL, 0, MADV_PROTECT) != 0)
478226690Sglebius		syslog(LOG_WARNING, "madvise(): %m");
479226690Sglebius
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
51690298Sdescheckwork(void)
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 */
53690298Sdesvoid
53790298Sdeshandle_children(struct _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)
55695658Sdes		rpc_received(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
59390298Sdestell_parent(char *dom, struct sockaddr_in *addr)
5941927Swollman{
5958091Swpaul	char buf[YPMAXDOMAIN + 1];
5968091Swpaul	struct timeval timeout;
5978091Swpaul	fd_set fds;
5981927Swollman
5998091Swpaul	timeout.tv_sec = 5;
6008091Swpaul	timeout.tv_usec = 0;
6011927Swollman
6028755Swpaul	sprintf(buf, "%s", broad_domain->dom_domain);
6038755Swpaul	if (write(BROADFD, &buf, sizeof(buf)) < 0)
6048091Swpaul		return(1);
6051927Swollman
6068091Swpaul	/*
6078091Swpaul	 * Stay in sync with parent: wait for it to read our first
6088091Swpaul	 * message before sending the second.
6098091Swpaul	 */
6101927Swollman
6118091Swpaul	FD_ZERO(&fds);
6128755Swpaul	FD_SET(BROADFD, &fds);
6138091Swpaul	if (select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == -1)
6148091Swpaul		return(1);
6158755Swpaul	if (FD_ISSET(BROADFD, &fds)) {
6168755Swpaul		if (write(BROADFD, addr, sizeof(struct sockaddr_in)) < 0)
6178091Swpaul			return(1);
6188091Swpaul	} else {
6198091Swpaul		return(1);
6201927Swollman	}
6211927Swollman
6228755Swpaul	close(BROADFD);
6238091Swpaul	return (0);
6248091Swpaul}
6251927Swollman
6268091Swpaulbool_t broadcast_result(out, addr)
6278091Swpaulbool_t *out;
6288091Swpaulstruct sockaddr_in *addr;
6298091Swpaul{
6309600Swpaul	if (retries >= MAX_RETRIES) {
63195658Sdes		bzero(addr, sizeof(struct sockaddr_in));
6329600Swpaul		if (tell_parent(broad_domain->dom_domain, addr))
6339600Swpaul			syslog(LOG_WARNING, "lost connection to parent");
63490297Sdes		return (TRUE);
6359600Swpaul	}
6369600Swpaul
6379600Swpaul	if (yp_restricted && verify(addr->sin_addr)) {
6389600Swpaul		retries++;
6399600Swpaul		syslog(LOG_NOTICE, "NIS server at %s not in restricted mode access list -- rejecting.\n",inet_ntoa(addr->sin_addr));
64090297Sdes		return (FALSE);
6419600Swpaul	} else {
6429600Swpaul		if (tell_parent(broad_domain->dom_domain, addr))
6439600Swpaul			syslog(LOG_WARNING, "lost connection to parent");
64490297Sdes		return (TRUE);
6459600Swpaul	}
6468091Swpaul}
6478091Swpaul
6488091Swpaul/*
6498091Swpaul * The right way to send RPC broadcasts.
6508091Swpaul * Use the clnt_broadcast() RPC service. Unfortunately, clnt_broadcast()
65172091Sasmodai * blocks while waiting for replies, so we have to fork off separate
6528091Swpaul * broadcaster processes that do the waiting and then transmit their
6538091Swpaul * results back to the parent for processing. We also have to remember
6548091Swpaul * to save the name of the domain we're trying to bind in a global
6558091Swpaul * variable since clnt_broadcast() provides no way to pass things to
6568091Swpaul * the 'eachresult' callback function.
6578091Swpaul */
6588091Swpaulvoid
65990298Sdesbroadcast(struct _dom_binding *ypdb)
6608091Swpaul{
6618091Swpaul	bool_t out = FALSE;
6628091Swpaul	enum clnt_stat stat;
6638091Swpaul
6648755Swpaul	if (children >= MAX_CHILDREN || ypdb->dom_broadcast_pid)
6658091Swpaul		return;
6668091Swpaul
6678755Swpaul	if (pipe(ypdb->dom_pipe_fds) < 0) {
66821539Swpaul		syslog(LOG_WARNING, "pipe: %m");
6698091Swpaul		return;
6701927Swollman	}
6711927Swollman
672213611Smarkm	if (ypdb->dom_vers == -1 && (long)ypdb->dom_server_addr.sin_addr.s_addr) {
673213611Smarkm		if (not_responding_count++ >= NOT_RESPONDING_HYSTERESIS) {
674213611Smarkm			not_responding_count = NOT_RESPONDING_HYSTERESIS;
675213611Smarkm			syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" not responding",
676213611Smarkm			    inet_ntoa(ypdb->dom_server_addr.sin_addr), ypdb->dom_domain);
677213611Smarkm		}
678213611Smarkm	}
6798425Swpaul
6808755Swpaul	broad_domain = ypdb;
6818425Swpaul	flock(ypdb->dom_lockfd, LOCK_UN);
6828425Swpaul
68390297Sdes	switch ((ypdb->dom_broadcast_pid = fork())) {
6848091Swpaul	case 0:
6858755Swpaul		close(READFD);
68621539Swpaul		signal(SIGCHLD, SIG_DFL);
68721539Swpaul		signal(SIGTERM, SIG_DFL);
6888091Swpaul		break;
6898091Swpaul	case -1:
69021539Swpaul		syslog(LOG_WARNING, "fork: %m");
6918755Swpaul		close(READFD);
6928755Swpaul		close(WRITEFD);
6938091Swpaul		return;
6948091Swpaul	default:
6958755Swpaul		close(WRITEFD);
6968755Swpaul		FD_SET(READFD, &svc_fdset);
6978091Swpaul		children++;
6988091Swpaul		return;
6991927Swollman	}
7006478Swpaul
7018755Swpaul	/* Release all locks before doing anything else. */
70290297Sdes	while (ypbindlist) {
7038755Swpaul		close(ypbindlist->dom_lockfd);
7048755Swpaul		ypbindlist = ypbindlist->dom_pnext;
7058755Swpaul	}
7068755Swpaul	close(yplockfd);
7078755Swpaul
70826135Swpaul	/*
70926135Swpaul	 * Special 'many-cast' behavior. If we're in restricted mode,
71026135Swpaul	 * we have a list of possible server addresses to try. What
71126135Swpaul	 * we can do is transmit to each ypserv's YPPROC_DOMAIN_NONACK
71226135Swpaul	 * procedure and time the replies. Whoever replies fastest
71326135Swpaul	 * gets to be our server. Note that this is not a broadcast
71426135Swpaul	 * operation: we transmit uni-cast datagrams only.
71526135Swpaul	 */
71626135Swpaul	if (yp_restricted && yp_manycast) {
71726135Swpaul		short			port;
71826135Swpaul		int			i;
71926135Swpaul		struct sockaddr_in	sin;
72026135Swpaul
72126135Swpaul		i = __yp_ping(restricted_addrs, yp_restricted,
72226135Swpaul				ypdb->dom_domain, &port);
72326135Swpaul		if (i == -1) {
72495658Sdes			bzero(&ypdb->dom_server_addr,
72595658Sdes			    sizeof(struct sockaddr_in));
72626135Swpaul			if (tell_parent(ypdb->dom_domain,
72795658Sdes				&ypdb->dom_server_addr))
72826135Swpaul			syslog(LOG_WARNING, "lost connection to parent");
72926135Swpaul		} else {
73095658Sdes			bzero(&sin, sizeof(struct sockaddr_in));
73195658Sdes			bcopy(&restricted_addrs[i],
73295658Sdes			    &sin.sin_addr, sizeof(struct in_addr));
73326135Swpaul			sin.sin_family = AF_INET;
73426135Swpaul			sin.sin_port = port;
73526135Swpaul			if (tell_parent(broad_domain->dom_domain, &sin))
73626135Swpaul				syslog(LOG_WARNING,
73726135Swpaul					"lost connection to parent");
73826135Swpaul		}
73926135Swpaul		_exit(0);
74026135Swpaul	}
74126135Swpaul
7429600Swpaul	retries = 0;
7439600Swpaul
74412862Swpaul	{
74512862Swpaul		char *ptr;
7468091Swpaul
74795658Sdes		ptr = ypdb->dom_domain;
74812862Swpaul		stat = clnt_broadcast(YPPROG, YPVERS, YPPROC_DOMAIN_NONACK,
74995658Sdes	    		(xdrproc_t)xdr_domainname, &ptr,
75095658Sdes		        (xdrproc_t)xdr_bool, &out,
75174462Salfred	    		(resultproc_t)broadcast_result);
75212862Swpaul	}
75312862Swpaul
7548091Swpaul	if (stat != RPC_SUCCESS) {
75595658Sdes		bzero(&ypdb->dom_server_addr,
75695658Sdes		    sizeof(struct sockaddr_in));
7578755Swpaul		if (tell_parent(ypdb->dom_domain, &ypdb->dom_server_addr))
7588091Swpaul			syslog(LOG_WARNING, "lost connection to parent");
7598091Swpaul	}
7608755Swpaul
76126135Swpaul	_exit(0);
7621927Swollman}
7631927Swollman
7648091Swpaul/*
7658091Swpaul * The right way to check if a server is alive.
7668091Swpaul * Attempt to get a client handle pointing to the server and send a
7678755Swpaul * YPPROC_DOMAIN. If we can't get a handle or we get a reply of FALSE,
7688755Swpaul * we invalidate this binding entry and send out a broadcast to try to
7698755Swpaul * establish a new binding. Note that we treat non-default domains
7708091Swpaul * specially: once bound, we keep tabs on our server, but if it
7718091Swpaul * goes away and fails to respond after one round of broadcasting, we
7728091Swpaul * abandon it until a client specifically references it again. We make
7738091Swpaul * every effort to keep our default domain bound, however, since we
7748091Swpaul * need it to keep the system on its feet.
7758091Swpaul */
7768091Swpaulint
77790298Sdesping(struct _dom_binding *ypdb)
7781927Swollman{
7798091Swpaul	bool_t out;
7808091Swpaul	struct timeval interval, timeout;
7818091Swpaul	enum clnt_stat stat;
7828091Swpaul	int rpcsock = RPC_ANYSOCK;
7838755Swpaul	CLIENT *client_handle;
7841927Swollman
7858755Swpaul	interval.tv_sec = FAIL_THRESHOLD;
7868091Swpaul	interval.tv_usec = 0;
7878091Swpaul	timeout.tv_sec = FAIL_THRESHOLD;
7888091Swpaul	timeout.tv_usec = 0;
7891927Swollman
7908755Swpaul	if (ypdb->dom_broadcast_pid)
7918091Swpaul		return(1);
7928091Swpaul
7938755Swpaul	if ((client_handle = clntudp_bufcreate(&ypdb->dom_server_addr,
7948755Swpaul		YPPROG, YPVERS, interval, &rpcsock, RPCSMALLMSGSIZE,
7958755Swpaul		RPCSMALLMSGSIZE)) == (CLIENT *)NULL) {
7968755Swpaul		/* Can't get a handle: we're dead. */
7978755Swpaul		ypdb->dom_alive = 0;
7988755Swpaul		ypdb->dom_vers = -1;
7998755Swpaul		broadcast(ypdb);
8008755Swpaul		return(1);
8011927Swollman	}
8021927Swollman
80312862Swpaul	{
80412862Swpaul		char *ptr;
80512862Swpaul
80695658Sdes		ptr = ypdb->dom_domain;
80712862Swpaul
80895658Sdes		stat = clnt_call(client_handle, YPPROC_DOMAIN,
80995658Sdes		    (xdrproc_t)xdr_domainname, &ptr,
81095658Sdes		    (xdrproc_t)xdr_bool, &out, timeout);
81195658Sdes		if (stat != RPC_SUCCESS || out == FALSE) {
81212862Swpaul			ypdb->dom_alive = 0;
81312862Swpaul			ypdb->dom_vers = -1;
81412862Swpaul			clnt_destroy(client_handle);
81512862Swpaul			broadcast(ypdb);
81612862Swpaul			return(1);
81712862Swpaul		}
8188091Swpaul	}
8191927Swollman
8208755Swpaul	clnt_destroy(client_handle);
8218091Swpaul	return(0);
8221927Swollman}
8231927Swollman
82490298Sdesvoid
82590298Sdesrpc_received(char *dom, struct sockaddr_in *raddrp, int 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;
894300268Struckman		if (strlen(dom) > YPMAXDOMAIN) {
895300268Struckman			syslog(LOG_WARNING, "domain %s too long", dom);
896300268Struckman			return;
897300268Struckman		}
898300268Struckman		ypdb = malloc(sizeof *ypdb);
8998857Srgrimes		if (ypdb == NULL) {
90021539Swpaul			syslog(LOG_WARNING, "malloc: %m");
9018246Swpaul			return;
9028246Swpaul		}
90395658Sdes		bzero(ypdb, sizeof *ypdb);
904300268Struckman		strlcpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain);
9051927Swollman		ypdb->dom_lockfd = -1;
9068091Swpaul		ypdb->dom_default = 0;
9071927Swollman		ypdb->dom_pnext = ypbindlist;
9081927Swollman		ypbindlist = ypdb;
9091927Swollman	}
9101927Swollman
9118091Swpaul	/* We've recovered from a crash: inform the world. */
912213611Smarkm	if (ypdb->dom_vers == -1 && ypdb->dom_server_addr.sin_addr.s_addr) {
913213611Smarkm		if (not_responding_count >= NOT_RESPONDING_HYSTERESIS) {
914213611Smarkm			not_responding_count = 0;
915213611Smarkm			syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" OK",
916213611Smarkm			    inet_ntoa(raddrp->sin_addr), ypdb->dom_domain);
917213611Smarkm		}
918213611Smarkm	}
9196478Swpaul
92095658Sdes	bcopy(raddrp, &ypdb->dom_server_addr,
9211927Swollman		sizeof ypdb->dom_server_addr);
9226478Swpaul
9231927Swollman	ypdb->dom_vers = YPVERS;
9241927Swollman	ypdb->dom_alive = 1;
9258755Swpaul	ypdb->dom_broadcast_pid = 0;
9261927Swollman
92790297Sdes	if (ypdb->dom_lockfd != -1)
9281927Swollman		close(ypdb->dom_lockfd);
9291927Swollman
9308091Swpaul	sprintf(path, "%s/%s.%ld", BINDINGDIR,
9311927Swollman		ypdb->dom_domain, ypdb->dom_vers);
9321927Swollman#ifdef O_SHLOCK
93390297Sdes	if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
9341927Swollman		(void)mkdir(BINDINGDIR, 0755);
93590297Sdes		if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
9361927Swollman			return;
9371927Swollman	}
9381927Swollman#else
93990297Sdes	if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
9401927Swollman		(void)mkdir(BINDINGDIR, 0755);
94190297Sdes		if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1)
9421927Swollman			return;
9431927Swollman	}
9441927Swollman	flock(fd, LOCK_SH);
9451927Swollman#endif
9461927Swollman
9471927Swollman	/*
9481927Swollman	 * ok, if BINDINGDIR exists, and we can create the binding file,
9491927Swollman	 * then write to it..
9501927Swollman	 */
9511927Swollman	ypdb->dom_lockfd = fd;
9521927Swollman
95395658Sdes	iov[0].iov_base = (char *)&(udptransp->xp_port);
9541927Swollman	iov[0].iov_len = sizeof udptransp->xp_port;
95595658Sdes	iov[1].iov_base = (char *)&ybr;
9561927Swollman	iov[1].iov_len = sizeof ybr;
9571927Swollman
9581927Swollman	bzero(&ybr, sizeof ybr);
9591927Swollman	ybr.ypbind_status = YPBIND_SUCC_VAL;
96045656Ssimokawa	*(u_int32_t *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr = raddrp->sin_addr.s_addr;
96112862Swpaul	*(u_short *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port = raddrp->sin_port;
9621927Swollman
96390297Sdes	if (writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) {
96421539Swpaul		syslog(LOG_WARNING, "write: %m");
9651927Swollman		close(ypdb->dom_lockfd);
9661927Swollman		ypdb->dom_lockfd = -1;
9671927Swollman		return;
9681927Swollman	}
9691927Swollman}
9709600Swpaul
9719600Swpaul/*
9729600Swpaul * Check address against list of allowed servers. Return 0 if okay,
9739600Swpaul * 1 if not matched.
9749600Swpaul */
9759600Swpaulint
97690298Sdesverify(struct in_addr addr)
9779600Swpaul{
9789600Swpaul	int i;
9799600Swpaul
9809600Swpaul	for (i = 0; i < RESTRICTED_SERVERS; i++)
98195658Sdes		if (!bcmp(&addr, &restricted_addrs[i], sizeof(struct in_addr)))
9829600Swpaul			return(0);
9839600Swpaul
9849600Swpaul	return(1);
9859600Swpaul}
9869600Swpaul
9879600Swpaul/*
9889600Swpaul * Try to set restricted mode. We default to normal mode if we can't
9899600Swpaul * resolve the specified hostnames.
9909600Swpaul */
9919600Swpaulvoid
99290298Sdesyp_restricted_mode(char *args)
9939600Swpaul{
9949600Swpaul	struct hostent *h;
9959600Swpaul	int i = 0;
9969600Swpaul	char *s;
9979600Swpaul
9989600Swpaul	/* Find the restricted domain. */
9999600Swpaul	if ((s = strsep(&args, ",")) == NULL)
10009600Swpaul		return;
100112862Swpaul	domain_name = s;
10029600Swpaul
10039600Swpaul	/* Get the addresses of the servers. */
10049600Swpaul	while ((s = strsep(&args, ",")) != NULL && i < RESTRICTED_SERVERS) {
10059600Swpaul		if ((h = gethostbyname(s)) == NULL)
10069600Swpaul			return;
100795658Sdes		bcopy (h->h_addr_list[0], &restricted_addrs[i],
100895658Sdes		    sizeof(struct in_addr));
100995658Sdes		i++;
10109600Swpaul	}
10119600Swpaul
10129600Swpaul	/* ypset and ypsetme not allowed with restricted mode */
10139600Swpaul	ypsetmode = YPSET_NO;
101490297Sdes
101526135Swpaul	yp_restricted = i;
10169600Swpaul	return;
10179600Swpaul}
1018