ypbind.c revision 8857
1/*
2 * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 *    products derived from this software without specific prior written
15 *    permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifndef LINT
31static char rcsid[] = "$Id: ypbind.c,v 1.14 1995/05/29 16:39:52 wpaul Exp $";
32#endif
33
34#include <sys/param.h>
35#include <sys/types.h>
36#include <sys/wait.h>
37#include <sys/ioctl.h>
38#include <sys/signal.h>
39#include <sys/socket.h>
40#include <sys/file.h>
41#include <sys/fcntl.h>
42#include <sys/stat.h>
43#include <sys/uio.h>
44#include <syslog.h>
45#include <stdio.h>
46#include <errno.h>
47#include <ctype.h>
48#include <dirent.h>
49#include <netdb.h>
50#include <string.h>
51#include <rpc/rpc.h>
52#include <rpc/xdr.h>
53#include <net/if.h>
54#include <netinet/in.h>
55#include <arpa/inet.h>
56#include <rpc/pmap_clnt.h>
57#include <rpc/pmap_prot.h>
58#include <rpc/pmap_rmt.h>
59#include <unistd.h>
60#include <stdlib.h>
61#include <rpcsvc/yp_prot.h>
62#include <rpcsvc/ypclnt.h>
63
64#ifndef BINDINGDIR
65#define BINDINGDIR "/var/yp/binding"
66#endif
67
68#ifndef YPBINDLOCK
69#define YPBINDLOCK "/var/run/ypbind.lock"
70#endif
71
72struct _dom_binding {
73	struct _dom_binding *dom_pnext;
74	char dom_domain[YPMAXDOMAIN + 1];
75	struct sockaddr_in dom_server_addr;
76	long int dom_vers;
77	int dom_lockfd;
78	int dom_alive;
79	int dom_broadcast_pid;
80	int dom_pipe_fds[2];
81	int dom_default;
82};
83
84#define READFD ypdb->dom_pipe_fds[0]
85#define WRITEFD ypdb->dom_pipe_fds[1]
86#define BROADFD broad_domain->dom_pipe_fds[1]
87
88extern bool_t xdr_domainname(), xdr_ypbind_resp();
89extern bool_t xdr_ypreq_key(), xdr_ypresp_val();
90extern bool_t xdr_ypbind_setdom();
91
92void	checkwork __P((void));
93void	*ypbindproc_null_2 __P((SVCXPRT *, void *, CLIENT *));
94void	*ypbindproc_setdom_2 __P((SVCXPRT *, struct ypbind_setdom *, CLIENT *));
95void	rpc_received __P((char *, struct sockaddr_in *, int ));
96void	broadcast __P((struct _dom_binding *));
97int	ping __P((struct _dom_binding *));
98int	tell_parent __P((char *, struct sockaddr_in *));
99void	handle_children __P(( struct _dom_binding * ));
100void	reaper __P((int));
101void	terminate __P((int));
102
103char *domainname;
104struct _dom_binding *ypbindlist;
105static struct _dom_binding *broad_domain;
106
107#define YPSET_NO	0
108#define YPSET_LOCAL	1
109#define YPSET_ALL	2
110int ypsetmode = YPSET_NO;
111int ypsecuremode = 0;
112
113/* No more than MAX_CHILDREN child broadcasters at a time. */
114#ifndef MAX_CHILDREN
115#define MAX_CHILDREN 5
116#endif
117/* No more than MAX_DOMAINS simultaneous domains */
118#ifndef MAX_DOMAINS
119#define MAX_DOMAINS 200
120#endif
121/* RPC timeout value */
122#ifndef FAIL_THRESHOLD
123#define FAIL_THRESHOLD 10
124#endif
125
126int children = 0;
127int domains = 0;
128int yplockfd;
129fd_set fdsr;
130
131SVCXPRT *udptransp, *tcptransp;
132
133void *
134ypbindproc_null_2(transp, argp, clnt)
135SVCXPRT *transp;
136void *argp;
137CLIENT *clnt;
138{
139	static char res;
140
141	bzero((char *)&res, sizeof(res));
142	return (void *)&res;
143}
144
145struct ypbind_resp *
146ypbindproc_domain_2(transp, argp, clnt)
147SVCXPRT *transp;
148char *argp;
149CLIENT *clnt;
150{
151	static struct ypbind_resp res;
152	struct _dom_binding *ypdb;
153	char path[MAXPATHLEN];
154
155	bzero((char *)&res, sizeof res);
156	res.ypbind_status = YPBIND_FAIL_VAL;
157	res.ypbind_respbody.ypbind_error = YPBIND_ERR_NOSERV;
158
159	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) {
160		if( strcmp(ypdb->dom_domain, argp) == 0)
161			break;
162		}
163
164	if(ypdb==NULL) {
165		if (domains >= MAX_DOMAINS) {
166			syslog(LOG_WARNING, "domain limit (%d) exceeded",
167							MAX_DOMAINS);
168			res.ypbind_respbody.ypbind_error = YPBIND_ERR_RESC;
169			return &res;
170		}
171		ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
172		if (ypdb == NULL) {
173			syslog(LOG_WARNING, "malloc: %s", strerror(errno));
174			res.ypbind_respbody.ypbind_error = YPBIND_ERR_RESC;
175			return &res;
176		}
177		bzero((char *)ypdb, sizeof *ypdb);
178		strncpy(ypdb->dom_domain, argp, sizeof ypdb->dom_domain);
179		ypdb->dom_vers = YPVERS;
180		ypdb->dom_alive = 0;
181		ypdb->dom_default = 0;
182		ypdb->dom_lockfd = -1;
183		sprintf(path, "%s/%s.%ld", BINDINGDIR,
184					ypdb->dom_domain, ypdb->dom_vers);
185		unlink(path);
186		ypdb->dom_pnext = ypbindlist;
187		ypbindlist = ypdb;
188		domains++;
189	}
190
191	if (ping(ypdb)) {
192		return &res;
193	}
194
195	res.ypbind_status = YPBIND_SUCC_VAL;
196	res.ypbind_respbody.ypbind_error = 0; /* Success */
197	res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr =
198		ypdb->dom_server_addr.sin_addr.s_addr;
199	res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
200		ypdb->dom_server_addr.sin_port;
201	/*printf("domain %s at %s/%d\n", ypdb->dom_domain,
202		inet_ntoa(ypdb->dom_server_addr.sin_addr),
203		ntohs(ypdb->dom_server_addr.sin_port));*/
204	return &res;
205}
206
207void *
208ypbindproc_setdom_2(transp, argp, clnt)
209SVCXPRT *transp;
210struct ypbind_setdom *argp;
211CLIENT *clnt;
212{
213	struct sockaddr_in *fromsin, bindsin;
214
215	fromsin = svc_getcaller(transp);
216
217	switch(ypsetmode) {
218	case YPSET_LOCAL:
219		if( fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
220			svcerr_noprog(transp);
221			return;
222		}
223		break;
224	case YPSET_ALL:
225		break;
226	case YPSET_NO:
227	default:
228		svcerr_noprog(transp);
229		return;
230	}
231
232	if(ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
233		svcerr_noprog(transp);
234		return;
235	}
236
237	if(argp->ypsetdom_vers != YPVERS) {
238		svcerr_noprog(transp);
239		return;
240	}
241
242	bzero((char *)&bindsin, sizeof bindsin);
243	bindsin.sin_family = AF_INET;
244	bindsin.sin_addr.s_addr = argp->ypsetdom_addr.s_addr;
245	bindsin.sin_port = argp->ypsetdom_port;
246	rpc_received(argp->ypsetdom_domain, &bindsin, 1);
247
248	return;
249}
250
251static void
252ypbindprog_2(rqstp, transp)
253struct svc_req *rqstp;
254register SVCXPRT *transp;
255{
256	union {
257		char ypbindproc_domain_2_arg[MAXHOSTNAMELEN];
258		struct ypbind_setdom ypbindproc_setdom_2_arg;
259	} argument;
260	struct authunix_parms *creds;
261	char *result;
262	bool_t (*xdr_argument)(), (*xdr_result)();
263	char *(*local)();
264
265	switch (rqstp->rq_proc) {
266	case YPBINDPROC_NULL:
267		xdr_argument = xdr_void;
268		xdr_result = xdr_void;
269		local = (char *(*)()) ypbindproc_null_2;
270		break;
271
272	case YPBINDPROC_DOMAIN:
273		xdr_argument = xdr_domainname;
274		xdr_result = xdr_ypbind_resp;
275		local = (char *(*)()) ypbindproc_domain_2;
276		break;
277
278	case YPBINDPROC_SETDOM:
279		switch(rqstp->rq_cred.oa_flavor) {
280		case AUTH_UNIX:
281			creds = (struct authunix_parms *)rqstp->rq_clntcred;
282			if( creds->aup_uid != 0) {
283				svcerr_auth(transp, AUTH_BADCRED);
284				return;
285			}
286			break;
287		default:
288			svcerr_auth(transp, AUTH_TOOWEAK);
289			return;
290		}
291
292		xdr_argument = xdr_ypbind_setdom;
293		xdr_result = xdr_void;
294		local = (char *(*)()) ypbindproc_setdom_2;
295		break;
296
297	default:
298		svcerr_noproc(transp);
299		return;
300	}
301	bzero((char *)&argument, sizeof(argument));
302	if (!svc_getargs(transp, xdr_argument, &argument)) {
303		svcerr_decode(transp);
304		return;
305	}
306	result = (*local)(transp, &argument, rqstp);
307	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
308		svcerr_systemerr(transp);
309	}
310	return;
311}
312
313/* Jack the reaper */
314void reaper(sig)
315int sig;
316{
317	int st;
318
319	wait3(&st, WNOHANG, NULL);
320}
321
322void terminate(sig)
323int sig;
324{
325	struct _dom_binding *ypdb;
326	char path[MAXPATHLEN];
327
328	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) {
329		close(ypdb->dom_lockfd);
330		if (ypdb->dom_broadcast_pid)
331			kill(ypdb->dom_broadcast_pid, SIGINT);
332		sprintf(path, "%s/%s.%ld", BINDINGDIR,
333			ypdb->dom_domain, ypdb->dom_vers);
334		unlink(path);
335	}
336	close(yplockfd);
337	unlink(YPBINDLOCK);
338	pmap_unset(YPBINDPROG, YPBINDVERS);
339	exit(0);
340}
341
342void
343main(argc, argv)
344int argc;
345char **argv;
346{
347	char path[MAXPATHLEN];
348	struct timeval tv;
349	int i;
350	DIR *dird;
351	struct dirent *dirp;
352	struct _dom_binding *ypdb;
353
354	/* Check that another ypbind isn't already running. */
355	if ((yplockfd = (open(YPBINDLOCK, O_RDONLY|O_CREAT, 0444))) == -1) {
356		perror(YPBINDLOCK);
357		exit(1);
358	}
359
360	if(flock(yplockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) {
361		fprintf (stderr, "Another ypbind is already running. Aborting.\n");
362		exit(1);
363	}
364
365	yp_get_default_domain(&domainname);
366	if( domainname[0] == '\0') {
367		fprintf(stderr, "domainname not set. Aborting.\n");
368		exit(1);
369	}
370
371	for(i=1; i<argc; i++) {
372		if( strcmp("-ypset", argv[i]) == 0)
373			ypsetmode = YPSET_ALL;
374		else if (strcmp("-ypsetme", argv[i]) == 0)
375		        ypsetmode = YPSET_LOCAL;
376		else if (strcmp("-s", argv[i]) == 0)
377		        ypsecuremode++;
378	}
379
380	/* blow away everything in BINDINGDIR (if it exists) */
381
382	if ((dird = opendir(BINDINGDIR)) != NULL) {
383		char path[MAXPATHLEN];
384		while ((dirp = readdir(dird)) != NULL)
385			if (strcmp(dirp->d_name, ".") &&
386			    strcmp(dirp->d_name, "..")) {
387				sprintf(path,"%s/%s",BINDINGDIR,dirp->d_name);
388				unlink(path);
389			}
390		closedir(dird);
391	}
392
393#ifdef DAEMON
394	if (daemon(0,0)) {
395		perror("fork");
396		exit(1);
397	}
398#endif
399
400	pmap_unset(YPBINDPROG, YPBINDVERS);
401
402	udptransp = svcudp_create(RPC_ANYSOCK);
403	if (udptransp == NULL) {
404		fprintf(stderr, "cannot create udp service.\n");
405		exit(1);
406	}
407	if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
408	    IPPROTO_UDP)) {
409		fprintf(stderr, "unable to register (YPBINDPROG, YPBINDVERS, udp).\n");
410		exit(1);
411	}
412
413	tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
414	if (tcptransp == NULL) {
415		fprintf(stderr, "cannot create tcp service.\n");
416		exit(1);
417	}
418
419	if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
420	    IPPROTO_TCP)) {
421		fprintf(stderr, "unable to register (YPBINDPROG, YPBINDVERS, tcp).\n");
422		exit(1);
423	}
424
425	/* build initial domain binding, make it "unsuccessful" */
426	ypbindlist = (struct _dom_binding *)malloc(sizeof *ypbindlist);
427	if (ypbindlist == NULL) {
428		perror("malloc");
429		exit(1);
430	}
431	bzero((char *)ypbindlist, sizeof *ypbindlist);
432	strncpy(ypbindlist->dom_domain, domainname, sizeof ypbindlist->dom_domain);
433	ypbindlist->dom_vers = YPVERS;
434	ypbindlist->dom_alive = 0;
435	ypbindlist->dom_lockfd = -1;
436	ypbindlist->dom_default = 1;
437	domains++;
438
439	signal(SIGCHLD, reaper);
440	signal(SIGTERM, terminate);
441
442	openlog(argv[0], LOG_PID, LOG_DAEMON);
443
444	/* Kick off the default domain */
445	broadcast(ypbindlist);
446
447	while(1) {
448		fdsr = svc_fdset;
449
450		tv.tv_sec = 60;
451		tv.tv_usec = 0;
452
453		switch(select(_rpc_dtablesize(), &fdsr, NULL, NULL, &tv)) {
454		case 0:
455			checkwork();
456			break;
457		case -1:
458			if (errno != EINTR)
459				syslog(LOG_WARNING, "select: %s", strerror(errno));
460			break;
461		default:
462			for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) {
463				if (READFD > 0 && FD_ISSET(READFD, &fdsr)) {
464					handle_children(ypdb);
465					children--;
466					if (children == (MAX_CHILDREN - 1))
467						checkwork();
468				}
469			}
470			svc_getreqset(&fdsr);
471			break;
472		}
473	}
474}
475
476void
477checkwork()
478{
479	struct _dom_binding *ypdb;
480
481	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext)
482		ping(ypdb);
483}
484
485/* The clnt_broadcast() callback mechanism sucks. */
486
487/*
488 * Receive results from broadcaster. Don't worry about passing
489 * bogus info to rpc_received() -- it can handle it. Note that we
490 * must be sure to invalidate the dom_pipe_fds descriptors here:
491 * since descriptors can be re-used, we have to make sure we
492 * don't mistake one of the RPC descriptors for one of the pipes.
493 * What's weird is that forgetting to invalidate the pipe descriptors
494 * doesn't always result in an error (otherwise I would have caught
495 * the mistake much sooner), even though logically it should.
496 */
497void handle_children(ypdb)
498struct _dom_binding *ypdb;
499{
500	char buf[YPMAXDOMAIN + 1];
501	struct sockaddr_in addr;
502
503	if (read(READFD, &buf, sizeof(buf)) < 0)
504		syslog(LOG_WARNING, "could not read from child: %s", strerror(errno));
505	if (read(READFD, &addr, sizeof(struct sockaddr_in)) < 0)
506		syslog(LOG_WARNING, "could not read from child: %s", strerror(errno));
507
508	close(READFD);
509	FD_CLR(READFD, &fdsr);
510	FD_CLR(READFD, &svc_fdset);
511	READFD = WRITEFD = -1;
512	rpc_received((char *)&buf, &addr, 0);
513}
514
515/*
516 * Send our dying words back to our parent before we perish.
517 */
518int
519tell_parent(dom, addr)
520char *dom;
521struct sockaddr_in *addr;
522{
523	char buf[YPMAXDOMAIN + 1];
524	struct timeval timeout;
525	fd_set fds;
526
527	timeout.tv_sec = 5;
528	timeout.tv_usec = 0;
529
530	sprintf(buf, "%s", broad_domain->dom_domain);
531	if (write(BROADFD, &buf, sizeof(buf)) < 0)
532		return(1);
533
534	/*
535	 * Stay in sync with parent: wait for it to read our first
536	 * message before sending the second.
537	 */
538
539	FD_ZERO(&fds);
540	FD_SET(BROADFD, &fds);
541	if (select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == -1)
542		return(1);
543	if (FD_ISSET(BROADFD, &fds)) {
544		if (write(BROADFD, addr, sizeof(struct sockaddr_in)) < 0)
545			return(1);
546	} else {
547		return(1);
548	}
549
550	close(BROADFD);
551	return (0);
552}
553
554bool_t broadcast_result(out, addr)
555bool_t *out;
556struct sockaddr_in *addr;
557{
558	if (tell_parent(broad_domain->dom_domain, addr))
559		syslog(LOG_WARNING, "lost connection to parent");
560	return TRUE;
561}
562
563/*
564 * The right way to send RPC broadcasts.
565 * Use the clnt_broadcast() RPC service. Unfortunately, clnt_broadcast()
566 * blocks while waiting for replies, so we have to fork off seperate
567 * broadcaster processes that do the waiting and then transmit their
568 * results back to the parent for processing. We also have to remember
569 * to save the name of the domain we're trying to bind in a global
570 * variable since clnt_broadcast() provides no way to pass things to
571 * the 'eachresult' callback function.
572 */
573void
574broadcast(ypdb)
575struct _dom_binding *ypdb;
576{
577	bool_t out = FALSE;
578	enum clnt_stat stat;
579
580	if (children >= MAX_CHILDREN || ypdb->dom_broadcast_pid)
581		return;
582
583	if (pipe(ypdb->dom_pipe_fds) < 0) {
584		syslog(LOG_WARNING, "pipe: %s",strerror(errno));
585		return;
586	}
587
588	if (ypdb->dom_vers = -1 && (long)ypdb->dom_server_addr.sin_addr.s_addr)
589		syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" not responding",
590		inet_ntoa(ypdb->dom_server_addr.sin_addr), ypdb->dom_domain);
591
592	broad_domain = ypdb;
593	flock(ypdb->dom_lockfd, LOCK_UN);
594
595	switch((ypdb->dom_broadcast_pid = fork())) {
596	case 0:
597		close(READFD);
598		break;
599	case -1:
600		syslog(LOG_WARNING, "fork: %s", strerror(errno));
601		close(READFD);
602		close(WRITEFD);
603		return;
604	default:
605		close(WRITEFD);
606		FD_SET(READFD, &svc_fdset);
607		children++;
608		return;
609	}
610
611	/* Release all locks before doing anything else. */
612	while(ypbindlist) {
613		close(ypbindlist->dom_lockfd);
614		ypbindlist = ypbindlist->dom_pnext;
615	}
616	close(yplockfd);
617
618	stat = clnt_broadcast(YPPROG, YPVERS, YPPROC_DOMAIN_NONACK,
619	    xdr_domainname, (char *)ypdb->dom_domain, xdr_bool, (char *)&out,
620	    broadcast_result);
621
622	if (stat != RPC_SUCCESS) {
623		bzero((char *)&ypdb->dom_server_addr,
624						sizeof(struct sockaddr_in));
625		if (tell_parent(ypdb->dom_domain, &ypdb->dom_server_addr))
626			syslog(LOG_WARNING, "lost connection to parent");
627	}
628
629	exit(0);
630}
631
632/*
633 * The right way to check if a server is alive.
634 * Attempt to get a client handle pointing to the server and send a
635 * YPPROC_DOMAIN. If we can't get a handle or we get a reply of FALSE,
636 * we invalidate this binding entry and send out a broadcast to try to
637 * establish a new binding. Note that we treat non-default domains
638 * specially: once bound, we keep tabs on our server, but if it
639 * goes away and fails to respond after one round of broadcasting, we
640 * abandon it until a client specifically references it again. We make
641 * every effort to keep our default domain bound, however, since we
642 * need it to keep the system on its feet.
643 */
644int
645ping(ypdb)
646struct _dom_binding *ypdb;
647{
648	bool_t out;
649	struct timeval interval, timeout;
650	enum clnt_stat stat;
651	int rpcsock = RPC_ANYSOCK;
652	CLIENT *client_handle;
653	time_t t;
654
655	interval.tv_sec = FAIL_THRESHOLD;
656	interval.tv_usec = 0;
657	timeout.tv_sec = FAIL_THRESHOLD;
658	timeout.tv_usec = 0;
659
660	if (ypdb->dom_broadcast_pid)
661		return(1);
662
663	if ((client_handle = clntudp_bufcreate(&ypdb->dom_server_addr,
664		YPPROG, YPVERS, interval, &rpcsock, RPCSMALLMSGSIZE,
665		RPCSMALLMSGSIZE)) == (CLIENT *)NULL) {
666		/* Can't get a handle: we're dead. */
667		ypdb->dom_alive = 0;
668		ypdb->dom_vers = -1;
669		broadcast(ypdb);
670		return(1);
671	}
672
673	if ((stat = clnt_call(client_handle, YPPROC_DOMAIN,
674		xdr_domainname, (char *)ypdb->dom_domain, xdr_bool,
675		(char *)&out, timeout)) != RPC_SUCCESS || out == FALSE) {
676		ypdb->dom_alive = 0;
677		ypdb->dom_vers = -1;
678		clnt_destroy(client_handle);
679		broadcast(ypdb);
680		return(1);
681	}
682
683	clnt_destroy(client_handle);
684	return(0);
685}
686
687void rpc_received(dom, raddrp, force)
688char *dom;
689struct sockaddr_in *raddrp;
690int force;
691{
692	struct _dom_binding *ypdb, *prev = NULL;
693	struct iovec iov[2];
694	struct ypbind_resp ybr;
695	char path[MAXPATHLEN];
696	int fd;
697
698	/*printf("returned from %s/%d about %s\n", inet_ntoa(raddrp->sin_addr),
699	       ntohs(raddrp->sin_port), dom);*/
700
701	if(dom==NULL)
702		return;
703
704	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) {
705		if( strcmp(ypdb->dom_domain, dom) == 0)
706			break;
707		prev = ypdb;
708	}
709
710	if (ypdb && force) {
711		if (ypdb->dom_broadcast_pid) {
712			kill(ypdb->dom_broadcast_pid, SIGINT);
713			close(READFD);
714			FD_CLR(READFD, &fdsr);
715			FD_CLR(READFD, &svc_fdset);
716			READFD = WRITEFD = -1;
717		}
718	}
719
720	/* if in securemode, check originating port number */
721	if (ypsecuremode && (ntohs(raddrp->sin_port) >= IPPORT_RESERVED)) {
722	    syslog(LOG_WARNING, "Rejected NIS server on [%s/%d] for domain %s.",
723		   inet_ntoa(raddrp->sin_addr), ntohs(raddrp->sin_port),
724		   dom);
725	    if (ypdb != NULL) {
726		ypdb->dom_broadcast_pid = 0;
727		ypdb->dom_alive = 0;
728	    }
729	    return;
730	}
731
732	if (raddrp->sin_addr.s_addr == (long)0) {
733		switch(ypdb->dom_default) {
734		case 0:
735			if (prev == NULL)
736				ypbindlist = ypdb->dom_pnext;
737			else
738				prev->dom_pnext = ypdb->dom_pnext;
739			sprintf(path, "%s/%s.%ld", BINDINGDIR,
740				ypdb->dom_domain, YPVERS);
741			close(ypdb->dom_lockfd);
742			unlink(path);
743			free(ypdb);
744			domains--;
745			return;
746		case 1:
747			ypdb->dom_broadcast_pid = 0;
748			ypdb->dom_alive = 0;
749			broadcast(ypdb);
750			return;
751		default:
752			break;
753		}
754	}
755
756	if(ypdb==NULL) {
757		if (force == 0)
758			return;
759		ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
760		if (ypdb == NULL) {
761			syslog(LOG_WARNING, "malloc: %s", strerror(errno));
762			return;
763		}
764		bzero((char *)ypdb, sizeof *ypdb);
765		strncpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain);
766		ypdb->dom_lockfd = -1;
767		ypdb->dom_default = 0;
768		ypdb->dom_pnext = ypbindlist;
769		ypbindlist = ypdb;
770	}
771
772	/* We've recovered from a crash: inform the world. */
773	if (ypdb->dom_vers = -1 && ypdb->dom_server_addr.sin_addr.s_addr)
774		syslog(LOG_WARNING, "NIS server [%s] for domain \"%s\" OK",
775		inet_ntoa(raddrp->sin_addr), ypdb->dom_domain);
776
777	bcopy((char *)raddrp, (char *)&ypdb->dom_server_addr,
778		sizeof ypdb->dom_server_addr);
779
780	ypdb->dom_vers = YPVERS;
781	ypdb->dom_alive = 1;
782	ypdb->dom_broadcast_pid = 0;
783
784	if(ypdb->dom_lockfd != -1)
785		close(ypdb->dom_lockfd);
786
787	sprintf(path, "%s/%s.%ld", BINDINGDIR,
788		ypdb->dom_domain, ypdb->dom_vers);
789#ifdef O_SHLOCK
790	if( (fd=open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
791		(void)mkdir(BINDINGDIR, 0755);
792		if( (fd=open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
793			return;
794	}
795#else
796	if( (fd=open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
797		(void)mkdir(BINDINGDIR, 0755);
798		if( (fd=open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1)
799			return;
800	}
801	flock(fd, LOCK_SH);
802#endif
803
804	/*
805	 * ok, if BINDINGDIR exists, and we can create the binding file,
806	 * then write to it..
807	 */
808	ypdb->dom_lockfd = fd;
809
810	iov[0].iov_base = (caddr_t)&(udptransp->xp_port);
811	iov[0].iov_len = sizeof udptransp->xp_port;
812	iov[1].iov_base = (caddr_t)&ybr;
813	iov[1].iov_len = sizeof ybr;
814
815	bzero(&ybr, sizeof ybr);
816	ybr.ypbind_status = YPBIND_SUCC_VAL;
817	ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr = raddrp->sin_addr;
818	ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port = raddrp->sin_port;
819
820	if( writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) {
821		syslog(LOG_WARNING, "write: %s", strerror(errno));
822		close(ypdb->dom_lockfd);
823		ypdb->dom_lockfd = -1;
824		return;
825	}
826}
827