ypbind.c revision 12862
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
301927Swollman#ifndef LINT
3112862Swpaulstatic char rcsid[] = "$Id: ypbind.c,v 1.17 1995/07/20 22:32:59 wpaul Exp $";
321927Swollman#endif
331927Swollman
341927Swollman#include <sys/param.h>
351927Swollman#include <sys/types.h>
368091Swpaul#include <sys/wait.h>
371927Swollman#include <sys/ioctl.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>
446478Swpaul#include <syslog.h>
451927Swollman#include <stdio.h>
461927Swollman#include <errno.h>
471927Swollman#include <ctype.h>
481927Swollman#include <dirent.h>
491927Swollman#include <netdb.h>
501927Swollman#include <string.h>
511927Swollman#include <rpc/rpc.h>
521927Swollman#include <rpc/xdr.h>
531927Swollman#include <net/if.h>
546732Swpaul#include <netinet/in.h>
551927Swollman#include <arpa/inet.h>
561927Swollman#include <rpc/pmap_clnt.h>
571927Swollman#include <rpc/pmap_prot.h>
581927Swollman#include <rpc/pmap_rmt.h>
591927Swollman#include <unistd.h>
608091Swpaul#include <stdlib.h>
6112862Swpaul#include <rpcsvc/yp.h>
6212862Swpaulstruct dom_binding{};
631927Swollman#include <rpcsvc/ypclnt.h>
641927Swollman
651927Swollman#ifndef BINDINGDIR
661927Swollman#define BINDINGDIR "/var/yp/binding"
671927Swollman#endif
681927Swollman
698474Swpaul#ifndef YPBINDLOCK
708474Swpaul#define YPBINDLOCK "/var/run/ypbind.lock"
718474Swpaul#endif
728474Swpaul
731927Swollmanstruct _dom_binding {
741927Swollman	struct _dom_binding *dom_pnext;
751927Swollman	char dom_domain[YPMAXDOMAIN + 1];
761927Swollman	struct sockaddr_in dom_server_addr;
771927Swollman	long int dom_vers;
781927Swollman	int dom_lockfd;
791927Swollman	int dom_alive;
808755Swpaul	int dom_broadcast_pid;
818755Swpaul	int dom_pipe_fds[2];
828091Swpaul	int dom_default;
831927Swollman};
841927Swollman
858755Swpaul#define READFD ypdb->dom_pipe_fds[0]
868755Swpaul#define WRITEFD ypdb->dom_pipe_fds[1]
878755Swpaul#define BROADFD broad_domain->dom_pipe_fds[1]
888755Swpaul
891927Swollmanextern bool_t xdr_domainname(), xdr_ypbind_resp();
901927Swollmanextern bool_t xdr_ypreq_key(), xdr_ypresp_val();
911927Swollmanextern bool_t xdr_ypbind_setdom();
921927Swollman
938091Swpaulvoid	checkwork __P((void));
948091Swpaulvoid	*ypbindproc_null_2 __P((SVCXPRT *, void *, CLIENT *));
958755Swpaulvoid	*ypbindproc_setdom_2 __P((SVCXPRT *, struct ypbind_setdom *, CLIENT *));
968091Swpaulvoid	rpc_received __P((char *, struct sockaddr_in *, int ));
978091Swpaulvoid	broadcast __P((struct _dom_binding *));
988425Swpaulint	ping __P((struct _dom_binding *));
998755Swpaulint	tell_parent __P((char *, struct sockaddr_in *));
1008853Swpaulvoid	handle_children __P(( struct _dom_binding * ));
1018425Swpaulvoid	reaper __P((int));
1028425Swpaulvoid	terminate __P((int));
1039600Swpaulvoid	yp_restricted_mode __P((char *));
1049600Swpaulint	verify __P((struct in_addr));
1058091Swpaul
10612862Swpaulchar *domain_name;
1071927Swollmanstruct _dom_binding *ypbindlist;
1088755Swpaulstatic struct _dom_binding *broad_domain;
1091927Swollman
1101927Swollman#define YPSET_NO	0
1111927Swollman#define YPSET_LOCAL	1
1121927Swollman#define YPSET_ALL	2
1131927Swollmanint ypsetmode = YPSET_NO;
1146732Swpaulint ypsecuremode = 0;
1156732Swpaul
1169600Swpaul/*
1179600Swpaul * Special restricted mode variables: when in restricted mode, only the
1189600Swpaul * specified restricted_domain will be bound, and only the servers listed
1199600Swpaul * in restricted_addrs will be used for binding.
1209600Swpaul */
1219600Swpaul#define RESTRICTED_SERVERS 10
1229600Swpaulint yp_restricted = 0;
1239600Swpaulstruct in_addr restricted_addrs[RESTRICTED_SERVERS];
1249600Swpaul
1258091Swpaul/* No more than MAX_CHILDREN child broadcasters at a time. */
1268755Swpaul#ifndef MAX_CHILDREN
1278091Swpaul#define MAX_CHILDREN 5
1288755Swpaul#endif
1298425Swpaul/* No more than MAX_DOMAINS simultaneous domains */
1308755Swpaul#ifndef MAX_DOMAINS
1318425Swpaul#define MAX_DOMAINS 200
1328755Swpaul#endif
1338755Swpaul/* RPC timeout value */
1348425Swpaul#ifndef FAIL_THRESHOLD
1358755Swpaul#define FAIL_THRESHOLD 10
1368425Swpaul#endif
1378425Swpaul
1389600Swpaul/* Number of times to fish for a response froma particular set of hosts */
1399600Swpaul#ifndef MAX_RETRIES
1409600Swpaul#define MAX_RETRIES 30
1419600Swpaul#endif
1429600Swpaul
1439600Swpaulint retries = 0;
1448091Swpaulint children = 0;
1458425Swpaulint domains = 0;
1468474Swpaulint yplockfd;
1478755Swpaulfd_set fdsr;
1488091Swpaul
1491927SwollmanSVCXPRT *udptransp, *tcptransp;
1501927Swollman
1511927Swollmanvoid *
1521927Swollmanypbindproc_null_2(transp, argp, clnt)
1531927SwollmanSVCXPRT *transp;
1541927Swollmanvoid *argp;
1551927SwollmanCLIENT *clnt;
1561927Swollman{
1571927Swollman	static char res;
1581927Swollman
1591927Swollman	bzero((char *)&res, sizeof(res));
1601927Swollman	return (void *)&res;
1611927Swollman}
1621927Swollman
1631927Swollmanstruct ypbind_resp *
1641927Swollmanypbindproc_domain_2(transp, argp, clnt)
1651927SwollmanSVCXPRT *transp;
16612862Swpauldomainname *argp;
1671927SwollmanCLIENT *clnt;
1681927Swollman{
1691927Swollman	static struct ypbind_resp res;
1701927Swollman	struct _dom_binding *ypdb;
1711927Swollman	char path[MAXPATHLEN];
1721927Swollman
1731927Swollman	bzero((char *)&res, sizeof res);
1741927Swollman	res.ypbind_status = YPBIND_FAIL_VAL;
17512862Swpaul	res.ypbind_resp_u.ypbind_error = YPBIND_ERR_NOSERV;
1761927Swollman
1778755Swpaul	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) {
17812862Swpaul		if( strcmp(ypdb->dom_domain, *argp) == 0)
1791927Swollman			break;
1808755Swpaul		}
1811927Swollman
1821927Swollman	if(ypdb==NULL) {
1839600Swpaul		if (yp_restricted) {
18412862Swpaul			syslog(LOG_NOTICE, "Running in restricted mode -- request to bind domain \"%s\" rejected.\n", *argp);
1859600Swpaul			return &res;
1869600Swpaul		}
1879600Swpaul
1888474Swpaul		if (domains >= MAX_DOMAINS) {
1898425Swpaul			syslog(LOG_WARNING, "domain limit (%d) exceeded",
1908425Swpaul							MAX_DOMAINS);
19112862Swpaul			res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
1928428Swpaul			return &res;
1938425Swpaul		}
1941927Swollman		ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
1958857Srgrimes		if (ypdb == NULL) {
1968246Swpaul			syslog(LOG_WARNING, "malloc: %s", strerror(errno));
19712862Swpaul			res.ypbind_resp_u.ypbind_error = YPBIND_ERR_RESC;
1988428Swpaul			return &res;
1998246Swpaul		}
2001927Swollman		bzero((char *)ypdb, sizeof *ypdb);
20112862Swpaul		strncpy(ypdb->dom_domain, *argp, sizeof ypdb->dom_domain);
2021927Swollman		ypdb->dom_vers = YPVERS;
2031927Swollman		ypdb->dom_alive = 0;
2048091Swpaul		ypdb->dom_default = 0;
2051927Swollman		ypdb->dom_lockfd = -1;
2068425Swpaul		sprintf(path, "%s/%s.%ld", BINDINGDIR,
2078425Swpaul					ypdb->dom_domain, ypdb->dom_vers);
2081927Swollman		unlink(path);
2091927Swollman		ypdb->dom_pnext = ypbindlist;
2101927Swollman		ypbindlist = ypdb;
2118425Swpaul		domains++;
2121927Swollman	}
2131927Swollman
2148755Swpaul	if (ping(ypdb)) {
2157982Swpaul		return &res;
2168755Swpaul	}
2171927Swollman
2181927Swollman	res.ypbind_status = YPBIND_SUCC_VAL;
21912862Swpaul	res.ypbind_resp_u.ypbind_error = 0; /* Success */
22012862Swpaul	*(u_long *)&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr =
2211927Swollman		ypdb->dom_server_addr.sin_addr.s_addr;
22212862Swpaul	*(u_short *)&res.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port =
2238091Swpaul		ypdb->dom_server_addr.sin_port;
2241927Swollman	/*printf("domain %s at %s/%d\n", ypdb->dom_domain,
2251927Swollman		inet_ntoa(ypdb->dom_server_addr.sin_addr),
2261927Swollman		ntohs(ypdb->dom_server_addr.sin_port));*/
2271927Swollman	return &res;
2281927Swollman}
2291927Swollman
2308755Swpaulvoid *
2311927Swollmanypbindproc_setdom_2(transp, argp, clnt)
2321927SwollmanSVCXPRT *transp;
23312862Swpaulypbind_setdom *argp;
2341927SwollmanCLIENT *clnt;
2351927Swollman{
2361927Swollman	struct sockaddr_in *fromsin, bindsin;
2371927Swollman
2381927Swollman	fromsin = svc_getcaller(transp);
2391927Swollman
2401927Swollman	switch(ypsetmode) {
2411927Swollman	case YPSET_LOCAL:
2428755Swpaul		if( fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
2438755Swpaul			svcerr_noprog(transp);
2448755Swpaul			return;
2458755Swpaul		}
2461927Swollman		break;
2471927Swollman	case YPSET_ALL:
2481927Swollman		break;
2491927Swollman	case YPSET_NO:
2501927Swollman	default:
2518755Swpaul		svcerr_noprog(transp);
2528755Swpaul		return;
2531927Swollman	}
2541927Swollman
2558755Swpaul	if(ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
2568755Swpaul		svcerr_noprog(transp);
2578755Swpaul		return;
2588755Swpaul	}
2591927Swollman
2608755Swpaul	if(argp->ypsetdom_vers != YPVERS) {
2618755Swpaul		svcerr_noprog(transp);
2628755Swpaul		return;
2638755Swpaul	}
2641927Swollman
2651927Swollman	bzero((char *)&bindsin, sizeof bindsin);
2661927Swollman	bindsin.sin_family = AF_INET;
26712862Swpaul	bindsin.sin_addr.s_addr = *(u_long *)argp->ypsetdom_binding.ypbind_binding_addr;
26812862Swpaul	bindsin.sin_port = *(u_short *)argp->ypsetdom_binding.ypbind_binding_port;
2691927Swollman	rpc_received(argp->ypsetdom_domain, &bindsin, 1);
2701927Swollman
2718755Swpaul	return;
2721927Swollman}
2731927Swollman
2741927Swollmanstatic void
2751927Swollmanypbindprog_2(rqstp, transp)
2761927Swollmanstruct svc_req *rqstp;
2771927Swollmanregister SVCXPRT *transp;
2781927Swollman{
2791927Swollman	union {
28012862Swpaul		domainname ypbindproc_domain_2_arg;
2811927Swollman		struct ypbind_setdom ypbindproc_setdom_2_arg;
2821927Swollman	} argument;
2831927Swollman	struct authunix_parms *creds;
2841927Swollman	char *result;
2851927Swollman	bool_t (*xdr_argument)(), (*xdr_result)();
2861927Swollman	char *(*local)();
2871927Swollman
2881927Swollman	switch (rqstp->rq_proc) {
2891927Swollman	case YPBINDPROC_NULL:
2901927Swollman		xdr_argument = xdr_void;
2911927Swollman		xdr_result = xdr_void;
2921927Swollman		local = (char *(*)()) ypbindproc_null_2;
2931927Swollman		break;
2941927Swollman
2951927Swollman	case YPBINDPROC_DOMAIN:
2961927Swollman		xdr_argument = xdr_domainname;
2971927Swollman		xdr_result = xdr_ypbind_resp;
2981927Swollman		local = (char *(*)()) ypbindproc_domain_2;
2991927Swollman		break;
3001927Swollman
3011927Swollman	case YPBINDPROC_SETDOM:
3021927Swollman		switch(rqstp->rq_cred.oa_flavor) {
3031927Swollman		case AUTH_UNIX:
3041927Swollman			creds = (struct authunix_parms *)rqstp->rq_clntcred;
3051927Swollman			if( creds->aup_uid != 0) {
3061927Swollman				svcerr_auth(transp, AUTH_BADCRED);
3071927Swollman				return;
3081927Swollman			}
3091927Swollman			break;
3101927Swollman		default:
3111927Swollman			svcerr_auth(transp, AUTH_TOOWEAK);
3121927Swollman			return;
3131927Swollman		}
3141927Swollman
3151927Swollman		xdr_argument = xdr_ypbind_setdom;
3161927Swollman		xdr_result = xdr_void;
3171927Swollman		local = (char *(*)()) ypbindproc_setdom_2;
3181927Swollman		break;
3191927Swollman
3201927Swollman	default:
3211927Swollman		svcerr_noproc(transp);
3221927Swollman		return;
3231927Swollman	}
3241927Swollman	bzero((char *)&argument, sizeof(argument));
3251927Swollman	if (!svc_getargs(transp, xdr_argument, &argument)) {
3261927Swollman		svcerr_decode(transp);
3271927Swollman		return;
3281927Swollman	}
3291927Swollman	result = (*local)(transp, &argument, rqstp);
3301927Swollman	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
3311927Swollman		svcerr_systemerr(transp);
3321927Swollman	}
3331927Swollman	return;
3341927Swollman}
3351927Swollman
3368091Swpaul/* Jack the reaper */
3378091Swpaulvoid reaper(sig)
3388091Swpaulint sig;
3398091Swpaul{
3408091Swpaul	int st;
3418091Swpaul
3429532Swpaul	while(wait3(&st, WNOHANG, NULL) > 0)
3439532Swpaul		children--;
3448091Swpaul}
3458091Swpaul
3468425Swpaulvoid terminate(sig)
3478425Swpaulint sig;
3488425Swpaul{
3498425Swpaul	struct _dom_binding *ypdb;
3508425Swpaul	char path[MAXPATHLEN];
3518425Swpaul
3528425Swpaul	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) {
3538425Swpaul		close(ypdb->dom_lockfd);
3548755Swpaul		if (ypdb->dom_broadcast_pid)
3558755Swpaul			kill(ypdb->dom_broadcast_pid, SIGINT);
3568425Swpaul		sprintf(path, "%s/%s.%ld", BINDINGDIR,
3578425Swpaul			ypdb->dom_domain, ypdb->dom_vers);
3588425Swpaul		unlink(path);
3598425Swpaul	}
3608474Swpaul	close(yplockfd);
3618474Swpaul	unlink(YPBINDLOCK);
3628425Swpaul	pmap_unset(YPBINDPROG, YPBINDVERS);
3638425Swpaul	exit(0);
3648425Swpaul}
3658857Srgrimes
3668091Swpaulvoid
3671927Swollmanmain(argc, argv)
3688091Swpaulint argc;
3691927Swollmanchar **argv;
3701927Swollman{
3711927Swollman	char path[MAXPATHLEN];
3721927Swollman	struct timeval tv;
3731927Swollman	int i;
3748425Swpaul	DIR *dird;
3758425Swpaul	struct dirent *dirp;
3768755Swpaul	struct _dom_binding *ypdb;
3771927Swollman
3788474Swpaul	/* Check that another ypbind isn't already running. */
3798474Swpaul	if ((yplockfd = (open(YPBINDLOCK, O_RDONLY|O_CREAT, 0444))) == -1) {
3808474Swpaul		perror(YPBINDLOCK);
3818474Swpaul		exit(1);
3828474Swpaul	}
3838474Swpaul
3848474Swpaul	if(flock(yplockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) {
3858474Swpaul		fprintf (stderr, "Another ypbind is already running. Aborting.\n");
3868474Swpaul		exit(1);
3878474Swpaul	}
3888474Swpaul
3899600Swpaul	/* XXX domainname will be overriden if we use restricted mode */
39012862Swpaul	yp_get_default_domain(&domain_name);
39112862Swpaul	if( domain_name[0] == '\0') {
3921927Swollman		fprintf(stderr, "domainname not set. Aborting.\n");
3931927Swollman		exit(1);
3941927Swollman	}
3951927Swollman
3961927Swollman	for(i=1; i<argc; i++) {
3971927Swollman		if( strcmp("-ypset", argv[i]) == 0)
3981927Swollman			ypsetmode = YPSET_ALL;
3991927Swollman		else if (strcmp("-ypsetme", argv[i]) == 0)
4006732Swpaul		        ypsetmode = YPSET_LOCAL;
4016732Swpaul		else if (strcmp("-s", argv[i]) == 0)
4026732Swpaul		        ypsecuremode++;
4039600Swpaul		else if (strcmp("-S", argv[i]) == 0 && argc > i)
4049600Swpaul			yp_restricted_mode(argv[i+1]);
4051927Swollman	}
4061927Swollman
4078425Swpaul	/* blow away everything in BINDINGDIR (if it exists) */
4081927Swollman
4098425Swpaul	if ((dird = opendir(BINDINGDIR)) != NULL) {
4108425Swpaul		char path[MAXPATHLEN];
4118425Swpaul		while ((dirp = readdir(dird)) != NULL)
4128425Swpaul			if (strcmp(dirp->d_name, ".") &&
4138425Swpaul			    strcmp(dirp->d_name, "..")) {
4148425Swpaul				sprintf(path,"%s/%s",BINDINGDIR,dirp->d_name);
4158425Swpaul				unlink(path);
4168425Swpaul			}
4178425Swpaul		closedir(dird);
4188425Swpaul	}
4191927Swollman
4201927Swollman#ifdef DAEMON
4218425Swpaul	if (daemon(0,0)) {
4221927Swollman		perror("fork");
4231927Swollman		exit(1);
4241927Swollman	}
4251927Swollman#endif
4261927Swollman
4271927Swollman	pmap_unset(YPBINDPROG, YPBINDVERS);
4281927Swollman
4291927Swollman	udptransp = svcudp_create(RPC_ANYSOCK);
4301927Swollman	if (udptransp == NULL) {
4318425Swpaul		fprintf(stderr, "cannot create udp service.\n");
4321927Swollman		exit(1);
4331927Swollman	}
4341927Swollman	if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
4351927Swollman	    IPPROTO_UDP)) {
4368425Swpaul		fprintf(stderr, "unable to register (YPBINDPROG, YPBINDVERS, udp).\n");
4371927Swollman		exit(1);
4381927Swollman	}
4391927Swollman
4401927Swollman	tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
4411927Swollman	if (tcptransp == NULL) {
4428425Swpaul		fprintf(stderr, "cannot create tcp service.\n");
4431927Swollman		exit(1);
4441927Swollman	}
4451927Swollman
4461927Swollman	if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
4471927Swollman	    IPPROTO_TCP)) {
4488425Swpaul		fprintf(stderr, "unable to register (YPBINDPROG, YPBINDVERS, tcp).\n");
4491927Swollman		exit(1);
4501927Swollman	}
4511927Swollman
4521927Swollman	/* build initial domain binding, make it "unsuccessful" */
4531927Swollman	ypbindlist = (struct _dom_binding *)malloc(sizeof *ypbindlist);
4548246Swpaul	if (ypbindlist == NULL) {
4558246Swpaul		perror("malloc");
4568246Swpaul		exit(1);
4578246Swpaul	}
4581927Swollman	bzero((char *)ypbindlist, sizeof *ypbindlist);
45912862Swpaul	strncpy(ypbindlist->dom_domain, domain_name, sizeof ypbindlist->dom_domain);
4601927Swollman	ypbindlist->dom_vers = YPVERS;
4611927Swollman	ypbindlist->dom_alive = 0;
4621927Swollman	ypbindlist->dom_lockfd = -1;
4638091Swpaul	ypbindlist->dom_default = 1;
4648425Swpaul	domains++;
4651927Swollman
4668425Swpaul	signal(SIGCHLD, reaper);
4678425Swpaul	signal(SIGTERM, terminate);
4688425Swpaul
4698246Swpaul	openlog(argv[0], LOG_PID, LOG_DAEMON);
4706478Swpaul
4718425Swpaul	/* Kick off the default domain */
4728425Swpaul	broadcast(ypbindlist);
4738425Swpaul
4741927Swollman	while(1) {
4751927Swollman		fdsr = svc_fdset;
4768091Swpaul
4778425Swpaul		tv.tv_sec = 60;
4781927Swollman		tv.tv_usec = 0;
4791927Swollman
4808091Swpaul		switch(select(_rpc_dtablesize(), &fdsr, NULL, NULL, &tv)) {
4811927Swollman		case 0:
4821927Swollman			checkwork();
4831927Swollman			break;
4841927Swollman		case -1:
4858755Swpaul			if (errno != EINTR)
4868425Swpaul				syslog(LOG_WARNING, "select: %s", strerror(errno));
4878755Swpaul			break;
4881927Swollman		default:
4898755Swpaul			for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) {
4908755Swpaul				if (READFD > 0 && FD_ISSET(READFD, &fdsr)) {
4918853Swpaul					handle_children(ypdb);
4928755Swpaul					if (children == (MAX_CHILDREN - 1))
4938755Swpaul						checkwork();
4948091Swpaul				}
4951927Swollman			}
4961927Swollman			svc_getreqset(&fdsr);
4971927Swollman			break;
4981927Swollman		}
4991927Swollman	}
5001927Swollman}
5011927Swollman
5028091Swpaulvoid
5031927Swollmancheckwork()
5041927Swollman{
5051927Swollman	struct _dom_binding *ypdb;
5061927Swollman
5078425Swpaul	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext)
5088425Swpaul		ping(ypdb);
5091927Swollman}
5101927Swollman
5118091Swpaul/* The clnt_broadcast() callback mechanism sucks. */
5128091Swpaul
5138091Swpaul/*
5148091Swpaul * Receive results from broadcaster. Don't worry about passing
5158853Swpaul * bogus info to rpc_received() -- it can handle it. Note that we
5168853Swpaul * must be sure to invalidate the dom_pipe_fds descriptors here:
5178853Swpaul * since descriptors can be re-used, we have to make sure we
5188853Swpaul * don't mistake one of the RPC descriptors for one of the pipes.
5198853Swpaul * What's weird is that forgetting to invalidate the pipe descriptors
5208853Swpaul * doesn't always result in an error (otherwise I would have caught
5218853Swpaul * the mistake much sooner), even though logically it should.
5228091Swpaul */
5238853Swpaulvoid handle_children(ypdb)
5248853Swpaulstruct _dom_binding *ypdb;
5258091Swpaul{
5268091Swpaul	char buf[YPMAXDOMAIN + 1];
5278091Swpaul	struct sockaddr_in addr;
5288091Swpaul
5298853Swpaul	if (read(READFD, &buf, sizeof(buf)) < 0)
5308246Swpaul		syslog(LOG_WARNING, "could not read from child: %s", strerror(errno));
5318853Swpaul	if (read(READFD, &addr, sizeof(struct sockaddr_in)) < 0)
5328246Swpaul		syslog(LOG_WARNING, "could not read from child: %s", strerror(errno));
5338755Swpaul
5348853Swpaul	close(READFD);
5358853Swpaul	FD_CLR(READFD, &fdsr);
5368853Swpaul	FD_CLR(READFD, &svc_fdset);
5378853Swpaul	READFD = WRITEFD = -1;
5388091Swpaul	rpc_received((char *)&buf, &addr, 0);
5398091Swpaul}
5408091Swpaul
5418091Swpaul/*
5428091Swpaul * Send our dying words back to our parent before we perish.
5438091Swpaul */
5448091Swpaulint
5458091Swpaultell_parent(dom, addr)
5461927Swollmanchar *dom;
5478091Swpaulstruct sockaddr_in *addr;
5481927Swollman{
5498091Swpaul	char buf[YPMAXDOMAIN + 1];
5508091Swpaul	struct timeval timeout;
5518091Swpaul	fd_set fds;
5521927Swollman
5538091Swpaul	timeout.tv_sec = 5;
5548091Swpaul	timeout.tv_usec = 0;
5551927Swollman
5568755Swpaul	sprintf(buf, "%s", broad_domain->dom_domain);
5578755Swpaul	if (write(BROADFD, &buf, sizeof(buf)) < 0)
5588091Swpaul		return(1);
5591927Swollman
5608091Swpaul	/*
5618091Swpaul	 * Stay in sync with parent: wait for it to read our first
5628091Swpaul	 * message before sending the second.
5638091Swpaul	 */
5641927Swollman
5658091Swpaul	FD_ZERO(&fds);
5668755Swpaul	FD_SET(BROADFD, &fds);
5678091Swpaul	if (select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == -1)
5688091Swpaul		return(1);
5698755Swpaul	if (FD_ISSET(BROADFD, &fds)) {
5708755Swpaul		if (write(BROADFD, addr, sizeof(struct sockaddr_in)) < 0)
5718091Swpaul			return(1);
5728091Swpaul	} else {
5738091Swpaul		return(1);
5741927Swollman	}
5751927Swollman
5768755Swpaul	close(BROADFD);
5778091Swpaul	return (0);
5788091Swpaul}
5791927Swollman
5808091Swpaulbool_t broadcast_result(out, addr)
5818091Swpaulbool_t *out;
5828091Swpaulstruct sockaddr_in *addr;
5838091Swpaul{
5849600Swpaul	if (retries >= MAX_RETRIES) {
5859600Swpaul		bzero((char *)addr, sizeof(struct sockaddr_in));
5869600Swpaul		if (tell_parent(broad_domain->dom_domain, addr))
5879600Swpaul			syslog(LOG_WARNING, "lost connection to parent");
5889600Swpaul		return TRUE;
5899600Swpaul	}
5909600Swpaul
5919600Swpaul	if (yp_restricted && verify(addr->sin_addr)) {
5929600Swpaul		retries++;
5939600Swpaul		syslog(LOG_NOTICE, "NIS server at %s not in restricted mode access list -- rejecting.\n",inet_ntoa(addr->sin_addr));
5949600Swpaul		return FALSE;
5959600Swpaul	} else {
5969600Swpaul		if (tell_parent(broad_domain->dom_domain, addr))
5979600Swpaul			syslog(LOG_WARNING, "lost connection to parent");
5989600Swpaul		return TRUE;
5999600Swpaul	}
6008091Swpaul}
6018091Swpaul
6028091Swpaul/*
6038091Swpaul * The right way to send RPC broadcasts.
6048091Swpaul * Use the clnt_broadcast() RPC service. Unfortunately, clnt_broadcast()
6058091Swpaul * blocks while waiting for replies, so we have to fork off seperate
6068091Swpaul * broadcaster processes that do the waiting and then transmit their
6078091Swpaul * results back to the parent for processing. We also have to remember
6088091Swpaul * to save the name of the domain we're trying to bind in a global
6098091Swpaul * variable since clnt_broadcast() provides no way to pass things to
6108091Swpaul * the 'eachresult' callback function.
6118091Swpaul */
6128091Swpaulvoid
6138091Swpaulbroadcast(ypdb)
6148091Swpaulstruct _dom_binding *ypdb;
6158091Swpaul{
6168091Swpaul	bool_t out = FALSE;
6178091Swpaul	enum clnt_stat stat;
6188091Swpaul
6198755Swpaul	if (children >= MAX_CHILDREN || ypdb->dom_broadcast_pid)
6208091Swpaul		return;
6218091Swpaul
6228755Swpaul	if (pipe(ypdb->dom_pipe_fds) < 0) {
6238091Swpaul		syslog(LOG_WARNING, "pipe: %s",strerror(errno));
6248091Swpaul		return;
6251927Swollman	}
6261927Swollman
6278425Swpaul	if (ypdb->dom_vers = -1 && (long)ypdb->dom_server_addr.sin_addr.s_addr)
6288755Swpaul		syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" not responding",
6298425Swpaul		inet_ntoa(ypdb->dom_server_addr.sin_addr), ypdb->dom_domain);
6308425Swpaul
6318755Swpaul	broad_domain = ypdb;
6328425Swpaul	flock(ypdb->dom_lockfd, LOCK_UN);
6338425Swpaul
6348755Swpaul	switch((ypdb->dom_broadcast_pid = fork())) {
6358091Swpaul	case 0:
6368755Swpaul		close(READFD);
6378091Swpaul		break;
6388091Swpaul	case -1:
6398091Swpaul		syslog(LOG_WARNING, "fork: %s", strerror(errno));
6408755Swpaul		close(READFD);
6418755Swpaul		close(WRITEFD);
6428091Swpaul		return;
6438091Swpaul	default:
6448755Swpaul		close(WRITEFD);
6458755Swpaul		FD_SET(READFD, &svc_fdset);
6468091Swpaul		children++;
6478091Swpaul		return;
6481927Swollman	}
6496478Swpaul
6508755Swpaul	/* Release all locks before doing anything else. */
6518755Swpaul	while(ypbindlist) {
6528755Swpaul		close(ypbindlist->dom_lockfd);
6538755Swpaul		ypbindlist = ypbindlist->dom_pnext;
6548755Swpaul	}
6558755Swpaul	close(yplockfd);
6568755Swpaul
6579600Swpaul	retries = 0;
6589600Swpaul
65912862Swpaul	{
66012862Swpaul		char *ptr;
6618091Swpaul
66212862Swpaul		ptr = (char *)&ypdb->dom_domain;
66312862Swpaul		stat = clnt_broadcast(YPPROG, YPVERS, YPPROC_DOMAIN_NONACK,
66412862Swpaul	    		xdr_domainname, (char *)&ptr, xdr_bool, (char *)&out,
66512862Swpaul	    		broadcast_result);
66612862Swpaul	}
66712862Swpaul
6688091Swpaul	if (stat != RPC_SUCCESS) {
6698091Swpaul		bzero((char *)&ypdb->dom_server_addr,
6708091Swpaul						sizeof(struct sockaddr_in));
6718755Swpaul		if (tell_parent(ypdb->dom_domain, &ypdb->dom_server_addr))
6728091Swpaul			syslog(LOG_WARNING, "lost connection to parent");
6738091Swpaul	}
6748755Swpaul
6758091Swpaul	exit(0);
6761927Swollman}
6771927Swollman
6788091Swpaul/*
6798091Swpaul * The right way to check if a server is alive.
6808091Swpaul * Attempt to get a client handle pointing to the server and send a
6818755Swpaul * YPPROC_DOMAIN. If we can't get a handle or we get a reply of FALSE,
6828755Swpaul * we invalidate this binding entry and send out a broadcast to try to
6838755Swpaul * establish a new binding. Note that we treat non-default domains
6848091Swpaul * specially: once bound, we keep tabs on our server, but if it
6858091Swpaul * goes away and fails to respond after one round of broadcasting, we
6868091Swpaul * abandon it until a client specifically references it again. We make
6878091Swpaul * every effort to keep our default domain bound, however, since we
6888091Swpaul * need it to keep the system on its feet.
6898091Swpaul */
6908091Swpaulint
6918425Swpaulping(ypdb)
6928091Swpaulstruct _dom_binding *ypdb;
6931927Swollman{
6948091Swpaul	bool_t out;
6958091Swpaul	struct timeval interval, timeout;
6968091Swpaul	enum clnt_stat stat;
6978091Swpaul	int rpcsock = RPC_ANYSOCK;
6988755Swpaul	CLIENT *client_handle;
6998091Swpaul	time_t t;
7001927Swollman
7018755Swpaul	interval.tv_sec = FAIL_THRESHOLD;
7028091Swpaul	interval.tv_usec = 0;
7038091Swpaul	timeout.tv_sec = FAIL_THRESHOLD;
7048091Swpaul	timeout.tv_usec = 0;
7051927Swollman
7068755Swpaul	if (ypdb->dom_broadcast_pid)
7078091Swpaul		return(1);
7088091Swpaul
7098755Swpaul	if ((client_handle = clntudp_bufcreate(&ypdb->dom_server_addr,
7108755Swpaul		YPPROG, YPVERS, interval, &rpcsock, RPCSMALLMSGSIZE,
7118755Swpaul		RPCSMALLMSGSIZE)) == (CLIENT *)NULL) {
7128755Swpaul		/* Can't get a handle: we're dead. */
7138755Swpaul		ypdb->dom_alive = 0;
7148755Swpaul		ypdb->dom_vers = -1;
7158755Swpaul		broadcast(ypdb);
7168755Swpaul		return(1);
7171927Swollman	}
7181927Swollman
71912862Swpaul	{
72012862Swpaul		char *ptr;
72112862Swpaul
72212862Swpaul		ptr = (char *)&ypdb->dom_domain;
72312862Swpaul
72412862Swpaul		if ((stat = clnt_call(client_handle, YPPROC_DOMAIN,
72512862Swpaul			xdr_domainname, (char *)&ptr, xdr_bool, (char *)&out,
72612862Swpaul			timeout)) != RPC_SUCCESS || out == FALSE) {
72712862Swpaul			ypdb->dom_alive = 0;
72812862Swpaul			ypdb->dom_vers = -1;
72912862Swpaul			clnt_destroy(client_handle);
73012862Swpaul			broadcast(ypdb);
73112862Swpaul			return(1);
73212862Swpaul		}
7338091Swpaul	}
7341927Swollman
7358755Swpaul	clnt_destroy(client_handle);
7368091Swpaul	return(0);
7371927Swollman}
7381927Swollman
7398091Swpaulvoid rpc_received(dom, raddrp, force)
7401927Swollmanchar *dom;
7411927Swollmanstruct sockaddr_in *raddrp;
7421927Swollmanint force;
7431927Swollman{
7448425Swpaul	struct _dom_binding *ypdb, *prev = NULL;
7451927Swollman	struct iovec iov[2];
7461927Swollman	struct ypbind_resp ybr;
7471927Swollman	char path[MAXPATHLEN];
7481927Swollman	int fd;
7491927Swollman
7506732Swpaul	/*printf("returned from %s/%d about %s\n", inet_ntoa(raddrp->sin_addr),
7516732Swpaul	       ntohs(raddrp->sin_port), dom);*/
7521927Swollman
7531927Swollman	if(dom==NULL)
7541927Swollman		return;
7551927Swollman
7568425Swpaul	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) {
7578091Swpaul		if( strcmp(ypdb->dom_domain, dom) == 0)
7588091Swpaul			break;
7598425Swpaul		prev = ypdb;
7608425Swpaul	}
7618091Swpaul
7628755Swpaul	if (ypdb && force) {
7638755Swpaul		if (ypdb->dom_broadcast_pid) {
7648755Swpaul			kill(ypdb->dom_broadcast_pid, SIGINT);
7658755Swpaul			close(READFD);
7668853Swpaul			FD_CLR(READFD, &fdsr);
7678853Swpaul			FD_CLR(READFD, &svc_fdset);
7688853Swpaul			READFD = WRITEFD = -1;
7698755Swpaul		}
7708755Swpaul	}
7718755Swpaul
7729600Swpaul	/* if in secure mode, check originating port number */
7739600Swpaul	if ((ypsecuremode && (ntohs(raddrp->sin_port) >= IPPORT_RESERVED))) {
7746732Swpaul	    syslog(LOG_WARNING, "Rejected NIS server on [%s/%d] for domain %s.",
7756732Swpaul		   inet_ntoa(raddrp->sin_addr), ntohs(raddrp->sin_port),
7766732Swpaul		   dom);
7778246Swpaul	    if (ypdb != NULL) {
7788755Swpaul		ypdb->dom_broadcast_pid = 0;
7798246Swpaul		ypdb->dom_alive = 0;
7808246Swpaul	    }
7816732Swpaul	    return;
7826732Swpaul	}
7836732Swpaul
7848246Swpaul	if (raddrp->sin_addr.s_addr == (long)0) {
7858755Swpaul		switch(ypdb->dom_default) {
7868755Swpaul		case 0:
7878755Swpaul			if (prev == NULL)
7888755Swpaul				ypbindlist = ypdb->dom_pnext;
7898755Swpaul			else
7908755Swpaul				prev->dom_pnext = ypdb->dom_pnext;
7918755Swpaul			sprintf(path, "%s/%s.%ld", BINDINGDIR,
7928755Swpaul				ypdb->dom_domain, YPVERS);
7938755Swpaul			close(ypdb->dom_lockfd);
7948755Swpaul			unlink(path);
7958755Swpaul			free(ypdb);
7968755Swpaul			domains--;
7978755Swpaul			return;
7988755Swpaul		case 1:
7998755Swpaul			ypdb->dom_broadcast_pid = 0;
8008755Swpaul			ypdb->dom_alive = 0;
8018755Swpaul			broadcast(ypdb);
8028755Swpaul			return;
8038755Swpaul		default:
8048755Swpaul			break;
8058755Swpaul		}
8068246Swpaul	}
8078246Swpaul
8081927Swollman	if(ypdb==NULL) {
8098246Swpaul		if (force == 0)
8101927Swollman			return;
8111927Swollman		ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
8128857Srgrimes		if (ypdb == NULL) {
8138246Swpaul			syslog(LOG_WARNING, "malloc: %s", strerror(errno));
8148246Swpaul			return;
8158246Swpaul		}
8161927Swollman		bzero((char *)ypdb, sizeof *ypdb);
8171927Swollman		strncpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain);
8181927Swollman		ypdb->dom_lockfd = -1;
8198091Swpaul		ypdb->dom_default = 0;
8201927Swollman		ypdb->dom_pnext = ypbindlist;
8211927Swollman		ypbindlist = ypdb;
8221927Swollman	}
8231927Swollman
8248091Swpaul	/* We've recovered from a crash: inform the world. */
8258091Swpaul	if (ypdb->dom_vers = -1 && ypdb->dom_server_addr.sin_addr.s_addr)
8268755Swpaul		syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" OK",
8278755Swpaul		inet_ntoa(raddrp->sin_addr), ypdb->dom_domain);
8286478Swpaul
8291927Swollman	bcopy((char *)raddrp, (char *)&ypdb->dom_server_addr,
8301927Swollman		sizeof ypdb->dom_server_addr);
8316478Swpaul
8321927Swollman	ypdb->dom_vers = YPVERS;
8331927Swollman	ypdb->dom_alive = 1;
8348755Swpaul	ypdb->dom_broadcast_pid = 0;
8351927Swollman
8361927Swollman	if(ypdb->dom_lockfd != -1)
8371927Swollman		close(ypdb->dom_lockfd);
8381927Swollman
8398091Swpaul	sprintf(path, "%s/%s.%ld", BINDINGDIR,
8401927Swollman		ypdb->dom_domain, ypdb->dom_vers);
8411927Swollman#ifdef O_SHLOCK
8427864Swpaul	if( (fd=open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
8431927Swollman		(void)mkdir(BINDINGDIR, 0755);
8447864Swpaul		if( (fd=open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
8451927Swollman			return;
8461927Swollman	}
8471927Swollman#else
8487864Swpaul	if( (fd=open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
8491927Swollman		(void)mkdir(BINDINGDIR, 0755);
8507864Swpaul		if( (fd=open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1)
8511927Swollman			return;
8521927Swollman	}
8531927Swollman	flock(fd, LOCK_SH);
8541927Swollman#endif
8551927Swollman
8561927Swollman	/*
8571927Swollman	 * ok, if BINDINGDIR exists, and we can create the binding file,
8581927Swollman	 * then write to it..
8591927Swollman	 */
8601927Swollman	ypdb->dom_lockfd = fd;
8611927Swollman
8621927Swollman	iov[0].iov_base = (caddr_t)&(udptransp->xp_port);
8631927Swollman	iov[0].iov_len = sizeof udptransp->xp_port;
8641927Swollman	iov[1].iov_base = (caddr_t)&ybr;
8651927Swollman	iov[1].iov_len = sizeof ybr;
8661927Swollman
8671927Swollman	bzero(&ybr, sizeof ybr);
8681927Swollman	ybr.ypbind_status = YPBIND_SUCC_VAL;
86912862Swpaul	*(u_long *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr = raddrp->sin_addr.s_addr;
87012862Swpaul	*(u_short *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port = raddrp->sin_port;
8711927Swollman
8721927Swollman	if( writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) {
8738091Swpaul		syslog(LOG_WARNING, "write: %s", strerror(errno));
8741927Swollman		close(ypdb->dom_lockfd);
8751927Swollman		ypdb->dom_lockfd = -1;
8761927Swollman		return;
8771927Swollman	}
8781927Swollman}
8799600Swpaul
8809600Swpaul/*
8819600Swpaul * Check address against list of allowed servers. Return 0 if okay,
8829600Swpaul * 1 if not matched.
8839600Swpaul */
8849600Swpaulint
8859600Swpaulverify(addr)
8869600Swpaulstruct in_addr addr;
8879600Swpaul{
8889600Swpaul	int i;
8899600Swpaul
8909600Swpaul	for (i = 0; i < RESTRICTED_SERVERS; i++)
8919600Swpaul		if (!bcmp((char *)&addr, (char *)&restricted_addrs[i],
8929600Swpaul			sizeof(struct in_addr)))
8939600Swpaul			return(0);
8949600Swpaul
8959600Swpaul	return(1);
8969600Swpaul}
8979600Swpaul
8989600Swpaul/*
8999600Swpaul * Try to set restricted mode. We default to normal mode if we can't
9009600Swpaul * resolve the specified hostnames.
9019600Swpaul */
9029600Swpaulvoid
9039600Swpaulyp_restricted_mode(args)
9049600Swpaulchar *args;
9059600Swpaul{
9069600Swpaul	struct hostent *h;
9079600Swpaul	int i = 0;
9089600Swpaul	char *s;
9099600Swpaul
9109600Swpaul	/* Find the restricted domain. */
9119600Swpaul	if ((s = strsep(&args, ",")) == NULL)
9129600Swpaul		return;
91312862Swpaul	domain_name = s;
9149600Swpaul
9159600Swpaul	/* Get the addresses of the servers. */
9169600Swpaul	while ((s = strsep(&args, ",")) != NULL && i < RESTRICTED_SERVERS) {
9179600Swpaul		if ((h = gethostbyname(s)) == NULL)
9189600Swpaul			return;
9199600Swpaul		bcopy ((char *)h->h_addr_list[0], (char *)&restricted_addrs[i],
9209600Swpaul			sizeof(struct in_addr));
9219600Swpaul	i++;
9229600Swpaul	}
9239600Swpaul
9249600Swpaul	/* ypset and ypsetme not allowed with restricted mode */
9259600Swpaul	ypsetmode = YPSET_NO;
9269600Swpaul
9279600Swpaul	yp_restricted = 1;
9289600Swpaul	return;
9299600Swpaul}
930