Home | History | Annotate | Line # | Download | only in ypbind
ypbind.c revision 1.11
      1   1.2  deraadt /*
      2   1.7  deraadt  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt (at) fsa.ca>
      3   1.2  deraadt  * All rights reserved.
      4   1.2  deraadt  *
      5   1.2  deraadt  * Redistribution and use in source and binary forms, with or without
      6   1.2  deraadt  * modification, are permitted provided that the following conditions
      7   1.2  deraadt  * are met:
      8   1.2  deraadt  * 1. Redistributions of source code must retain the above copyright
      9   1.2  deraadt  *    notice, this list of conditions and the following disclaimer.
     10   1.2  deraadt  * 2. Redistributions in binary form must reproduce the above copyright
     11   1.2  deraadt  *    notice, this list of conditions and the following disclaimer in the
     12   1.2  deraadt  *    documentation and/or other materials provided with the distribution.
     13   1.7  deraadt  * 3. All advertising materials mentioning features or use of this software
     14   1.7  deraadt  *    must display the following acknowledgement:
     15   1.7  deraadt  *	This product includes software developed by Theo de Raadt.
     16   1.7  deraadt  * 4. The name of the author may not be used to endorse or promote
     17   1.2  deraadt  *    products derived from this software without specific prior written
     18   1.2  deraadt  *    permission.
     19   1.2  deraadt  *
     20   1.2  deraadt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     21   1.2  deraadt  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     22   1.2  deraadt  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.2  deraadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     24   1.2  deraadt  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25   1.2  deraadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26   1.2  deraadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27   1.2  deraadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28   1.2  deraadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29   1.2  deraadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30   1.2  deraadt  * SUCH DAMAGE.
     31   1.2  deraadt  */
     32   1.2  deraadt 
     33   1.2  deraadt #ifndef LINT
     34  1.11       ws static char rcsid[] = "$Id: ypbind.c,v 1.11 1994/07/16 11:50:58 ws Exp $";
     35   1.2  deraadt #endif
     36   1.2  deraadt 
     37   1.1  deraadt #include <sys/param.h>
     38   1.1  deraadt #include <sys/types.h>
     39   1.1  deraadt #include <sys/ioctl.h>
     40   1.1  deraadt #include <sys/signal.h>
     41   1.1  deraadt #include <sys/socket.h>
     42   1.1  deraadt #include <sys/file.h>
     43   1.1  deraadt #include <sys/fcntl.h>
     44   1.6  deraadt #include <sys/uio.h>
     45   1.1  deraadt #include <sys/syslog.h>
     46   1.1  deraadt #include <stdio.h>
     47   1.1  deraadt #include <errno.h>
     48   1.1  deraadt #include <ctype.h>
     49   1.1  deraadt #include <dirent.h>
     50   1.1  deraadt #include <netdb.h>
     51   1.1  deraadt #include <string.h>
     52   1.1  deraadt #include <rpc/rpc.h>
     53   1.1  deraadt #include <rpc/xdr.h>
     54   1.1  deraadt #include <net/if.h>
     55   1.1  deraadt #include <arpa/inet.h>
     56   1.1  deraadt #include <rpc/pmap_clnt.h>
     57   1.1  deraadt #include <rpc/pmap_prot.h>
     58   1.1  deraadt #include <rpc/pmap_rmt.h>
     59   1.1  deraadt #include <unistd.h>
     60   1.1  deraadt #include <rpcsvc/yp_prot.h>
     61   1.1  deraadt #include <rpcsvc/ypclnt.h>
     62   1.1  deraadt 
     63   1.1  deraadt #ifndef BINDINGDIR
     64   1.1  deraadt #define BINDINGDIR "/var/yp/binding"
     65   1.1  deraadt #endif
     66   1.1  deraadt 
     67   1.1  deraadt struct _dom_binding {
     68   1.1  deraadt 	struct _dom_binding *dom_pnext;
     69   1.1  deraadt 	char dom_domain[YPMAXDOMAIN + 1];
     70   1.1  deraadt 	struct sockaddr_in dom_server_addr;
     71   1.1  deraadt 	unsigned short int dom_server_port;
     72   1.1  deraadt 	int dom_socket;
     73   1.1  deraadt 	CLIENT *dom_client;
     74   1.1  deraadt 	long int dom_vers;
     75   1.1  deraadt 	time_t dom_check_t;
     76   1.9  deraadt 	time_t dom_ask_t;
     77   1.1  deraadt 	int dom_lockfd;
     78   1.1  deraadt 	int dom_alive;
     79   1.1  deraadt };
     80   1.1  deraadt 
     81   1.1  deraadt extern bool_t xdr_domainname(), xdr_ypbind_resp();
     82   1.1  deraadt extern bool_t xdr_ypreq_key(), xdr_ypresp_val();
     83   1.1  deraadt extern bool_t xdr_ypbind_setdom();
     84   1.1  deraadt 
     85   1.1  deraadt char *domainname;
     86   1.1  deraadt 
     87   1.1  deraadt struct _dom_binding *ypbindlist;
     88   1.1  deraadt int check;
     89   1.1  deraadt 
     90   1.1  deraadt #define YPSET_NO	0
     91   1.1  deraadt #define YPSET_LOCAL	1
     92   1.1  deraadt #define YPSET_ALL	2
     93   1.1  deraadt int ypsetmode = YPSET_NO;
     94   1.1  deraadt 
     95  1.10  deraadt int rpcsock, pingsock;
     96   1.1  deraadt struct rmtcallargs rmtca;
     97   1.1  deraadt struct rmtcallres rmtcr;
     98   1.1  deraadt char rmtcr_outval;
     99   1.1  deraadt u_long rmtcr_port;
    100   1.6  deraadt SVCXPRT *udptransp, *tcptransp;
    101   1.1  deraadt 
    102   1.1  deraadt void *
    103   1.1  deraadt ypbindproc_null_2(transp, argp, clnt)
    104   1.1  deraadt SVCXPRT *transp;
    105   1.1  deraadt void *argp;
    106   1.1  deraadt CLIENT *clnt;
    107   1.1  deraadt {
    108   1.1  deraadt 	static char res;
    109   1.1  deraadt 
    110   1.1  deraadt 	bzero((char *)&res, sizeof(res));
    111   1.1  deraadt 	return (void *)&res;
    112   1.1  deraadt }
    113   1.1  deraadt 
    114   1.1  deraadt struct ypbind_resp *
    115   1.1  deraadt ypbindproc_domain_2(transp, argp, clnt)
    116   1.1  deraadt SVCXPRT *transp;
    117   1.1  deraadt char *argp;
    118   1.1  deraadt CLIENT *clnt;
    119   1.1  deraadt {
    120   1.1  deraadt 	static struct ypbind_resp res;
    121   1.1  deraadt 	struct _dom_binding *ypdb;
    122   1.1  deraadt 	char path[MAXPATHLEN];
    123   1.9  deraadt 	time_t now;
    124   1.1  deraadt 
    125   1.1  deraadt 	bzero((char *)&res, sizeof res);
    126   1.1  deraadt 	res.ypbind_status = YPBIND_FAIL_VAL;
    127   1.1  deraadt 
    128   1.1  deraadt 	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext)
    129   1.1  deraadt 		if( strcmp(ypdb->dom_domain, argp) == 0)
    130   1.1  deraadt 			break;
    131   1.1  deraadt 
    132   1.1  deraadt 	if(ypdb==NULL) {
    133   1.1  deraadt 		ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
    134   1.1  deraadt 		bzero((char *)ypdb, sizeof *ypdb);
    135   1.1  deraadt 		strncpy(ypdb->dom_domain, argp, sizeof ypdb->dom_domain);
    136   1.1  deraadt 		ypdb->dom_vers = YPVERS;
    137   1.1  deraadt 		ypdb->dom_alive = 0;
    138   1.1  deraadt 		ypdb->dom_lockfd = -1;
    139   1.1  deraadt 		sprintf(path, "%s/%s.%d", BINDINGDIR, ypdb->dom_domain, ypdb->dom_vers);
    140   1.1  deraadt 		unlink(path);
    141   1.1  deraadt 		ypdb->dom_pnext = ypbindlist;
    142   1.1  deraadt 		ypbindlist = ypdb;
    143   1.1  deraadt 		check++;
    144   1.1  deraadt 		return NULL;
    145   1.1  deraadt 	}
    146   1.1  deraadt 
    147   1.1  deraadt 	if(ypdb->dom_alive==0)
    148   1.1  deraadt 		return NULL;
    149   1.1  deraadt 
    150   1.9  deraadt #ifdef HEURISTIC
    151   1.9  deraadt 	time(&now);
    152   1.9  deraadt 	if (now < ypdb->dom_ask_t + 5) {
    153   1.1  deraadt 		/*
    154   1.9  deraadt 		 * Hmm. More than 2 requests in 5 seconds have indicated
    155   1.9  deraadt 		 * that my binding is possibly incorrect.
    156   1.9  deraadt 		 * Ok, do an immediate poll of the server.
    157   1.1  deraadt 		 */
    158   1.9  deraadt 		if (ypdb->dom_check_t >= now) {
    159   1.9  deraadt 			/* don't flood it */
    160   1.9  deraadt 			ypdb->dom_check_t = 0;
    161   1.9  deraadt 			check++;
    162   1.9  deraadt 		}
    163   1.1  deraadt 	}
    164   1.9  deraadt 	ypdb->dom_ask_t = now;
    165   1.1  deraadt #endif
    166   1.1  deraadt 
    167   1.1  deraadt answer:
    168   1.1  deraadt 	res.ypbind_status = YPBIND_SUCC_VAL;
    169   1.1  deraadt 	res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr =
    170   1.1  deraadt 		ypdb->dom_server_addr.sin_addr.s_addr;
    171   1.1  deraadt 	res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
    172   1.1  deraadt 		ypdb->dom_server_port;
    173   1.1  deraadt 	/*printf("domain %s at %s/%d\n", ypdb->dom_domain,
    174   1.1  deraadt 		inet_ntoa(ypdb->dom_server_addr.sin_addr),
    175   1.1  deraadt 		ntohs(ypdb->dom_server_addr.sin_port));*/
    176   1.1  deraadt 	return &res;
    177   1.1  deraadt }
    178   1.1  deraadt 
    179   1.1  deraadt bool_t *
    180   1.1  deraadt ypbindproc_setdom_2(transp, argp, clnt)
    181   1.1  deraadt SVCXPRT *transp;
    182   1.1  deraadt struct ypbind_setdom *argp;
    183   1.1  deraadt CLIENT *clnt;
    184   1.1  deraadt {
    185   1.1  deraadt 	struct sockaddr_in *fromsin, bindsin;
    186   1.1  deraadt 	char res;
    187   1.1  deraadt 
    188   1.1  deraadt 	bzero((char *)&res, sizeof(res));
    189   1.1  deraadt 	fromsin = svc_getcaller(transp);
    190   1.1  deraadt 
    191   1.1  deraadt 	switch(ypsetmode) {
    192   1.1  deraadt 	case YPSET_LOCAL:
    193   1.1  deraadt 		if( fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK))
    194   1.1  deraadt 			return (void *)NULL;
    195   1.1  deraadt 		break;
    196   1.1  deraadt 	case YPSET_ALL:
    197   1.1  deraadt 		break;
    198   1.1  deraadt 	case YPSET_NO:
    199   1.1  deraadt 	default:
    200   1.1  deraadt 		return (void *)NULL;
    201   1.1  deraadt 	}
    202   1.4  deraadt 
    203   1.4  deraadt 	if(ntohs(fromsin->sin_port) >= IPPORT_RESERVED)
    204   1.5      cgd 		return (void *)&res;
    205   1.1  deraadt 
    206   1.1  deraadt 	if(argp->ypsetdom_vers != YPVERS)
    207   1.1  deraadt 		return (void *)&res;
    208   1.1  deraadt 
    209   1.1  deraadt 	bzero((char *)&bindsin, sizeof bindsin);
    210   1.1  deraadt 	bindsin.sin_family = AF_INET;
    211   1.1  deraadt 	bindsin.sin_addr.s_addr = argp->ypsetdom_addr.s_addr;
    212   1.1  deraadt 	bindsin.sin_port = argp->ypsetdom_port;
    213   1.1  deraadt 	rpc_received(argp->ypsetdom_domain, &bindsin, 1);
    214   1.1  deraadt 
    215   1.1  deraadt 	res = 1;
    216   1.1  deraadt 	return (void *)&res;
    217   1.1  deraadt }
    218   1.1  deraadt 
    219   1.1  deraadt static void
    220   1.1  deraadt ypbindprog_2(rqstp, transp)
    221   1.1  deraadt struct svc_req *rqstp;
    222   1.1  deraadt register SVCXPRT *transp;
    223   1.1  deraadt {
    224   1.1  deraadt 	union {
    225   1.1  deraadt 		char ypbindproc_domain_2_arg[MAXHOSTNAMELEN];
    226   1.1  deraadt 		struct ypbind_setdom ypbindproc_setdom_2_arg;
    227   1.1  deraadt 	} argument;
    228   1.1  deraadt 	struct authunix_parms *creds;
    229   1.1  deraadt 	char *result;
    230   1.1  deraadt 	bool_t (*xdr_argument)(), (*xdr_result)();
    231   1.1  deraadt 	char *(*local)();
    232   1.1  deraadt 
    233   1.1  deraadt 	switch (rqstp->rq_proc) {
    234   1.1  deraadt 	case YPBINDPROC_NULL:
    235   1.1  deraadt 		xdr_argument = xdr_void;
    236   1.1  deraadt 		xdr_result = xdr_void;
    237   1.1  deraadt 		local = (char *(*)()) ypbindproc_null_2;
    238   1.1  deraadt 		break;
    239   1.1  deraadt 
    240   1.1  deraadt 	case YPBINDPROC_DOMAIN:
    241   1.1  deraadt 		xdr_argument = xdr_domainname;
    242   1.1  deraadt 		xdr_result = xdr_ypbind_resp;
    243   1.1  deraadt 		local = (char *(*)()) ypbindproc_domain_2;
    244   1.1  deraadt 		break;
    245   1.1  deraadt 
    246   1.1  deraadt 	case YPBINDPROC_SETDOM:
    247   1.1  deraadt 		switch(rqstp->rq_cred.oa_flavor) {
    248   1.1  deraadt 		case AUTH_UNIX:
    249   1.1  deraadt 			creds = (struct authunix_parms *)rqstp->rq_clntcred;
    250   1.1  deraadt 			if( creds->aup_uid != 0) {
    251   1.1  deraadt 				svcerr_auth(transp, AUTH_BADCRED);
    252   1.1  deraadt 				return;
    253   1.1  deraadt 			}
    254   1.1  deraadt 			break;
    255   1.1  deraadt 		default:
    256   1.1  deraadt 			svcerr_auth(transp, AUTH_TOOWEAK);
    257   1.1  deraadt 			return;
    258   1.1  deraadt 		}
    259   1.1  deraadt 
    260   1.1  deraadt 		xdr_argument = xdr_ypbind_setdom;
    261   1.1  deraadt 		xdr_result = xdr_void;
    262   1.1  deraadt 		local = (char *(*)()) ypbindproc_setdom_2;
    263   1.1  deraadt 		break;
    264   1.1  deraadt 
    265   1.1  deraadt 	default:
    266   1.1  deraadt 		svcerr_noproc(transp);
    267   1.1  deraadt 		return;
    268   1.1  deraadt 	}
    269   1.1  deraadt 	bzero((char *)&argument, sizeof(argument));
    270   1.1  deraadt 	if (!svc_getargs(transp, xdr_argument, &argument)) {
    271   1.1  deraadt 		svcerr_decode(transp);
    272   1.1  deraadt 		return;
    273   1.1  deraadt 	}
    274   1.1  deraadt 	result = (*local)(transp, &argument, rqstp);
    275   1.1  deraadt 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
    276   1.1  deraadt 		svcerr_systemerr(transp);
    277   1.1  deraadt 	}
    278   1.1  deraadt 	return;
    279   1.1  deraadt }
    280   1.1  deraadt 
    281   1.1  deraadt main(argc, argv)
    282   1.1  deraadt char **argv;
    283   1.1  deraadt {
    284   1.1  deraadt 	char path[MAXPATHLEN];
    285   1.1  deraadt 	struct timeval tv;
    286   1.1  deraadt 	fd_set fdsr;
    287   1.1  deraadt 	int width;
    288   1.1  deraadt 	int i;
    289   1.1  deraadt 
    290   1.1  deraadt 	yp_get_default_domain(&domainname);
    291   1.1  deraadt 	if( domainname[0] == '\0') {
    292   1.1  deraadt 		fprintf(stderr, "domainname not set. Aborting.\n");
    293   1.1  deraadt 		exit(1);
    294   1.1  deraadt 	}
    295   1.1  deraadt 
    296   1.1  deraadt 	for(i=1; i<argc; i++) {
    297   1.1  deraadt 		if( strcmp("-ypset", argv[i]) == 0)
    298   1.1  deraadt 			ypsetmode = YPSET_ALL;
    299   1.1  deraadt 		else if (strcmp("-ypsetme", argv[i]) == 0)
    300   1.1  deraadt 			ypsetmode = YPSET_LOCAL;
    301   1.1  deraadt 	}
    302   1.1  deraadt 
    303   1.1  deraadt 	/* blow away everything in BINDINGDIR */
    304   1.1  deraadt 
    305   1.1  deraadt 
    306   1.1  deraadt 
    307   1.1  deraadt #ifdef DAEMON
    308   1.1  deraadt 	switch(fork()) {
    309   1.1  deraadt 	case 0:
    310   1.1  deraadt 		break;
    311   1.1  deraadt 	case -1:
    312   1.1  deraadt 		perror("fork");
    313   1.1  deraadt 		exit(1);
    314   1.1  deraadt 	default:
    315   1.1  deraadt 		exit(0);
    316   1.1  deraadt 	}
    317   1.1  deraadt 	setsid();
    318   1.1  deraadt #endif
    319   1.1  deraadt 
    320   1.1  deraadt 	pmap_unset(YPBINDPROG, YPBINDVERS);
    321   1.1  deraadt 
    322   1.6  deraadt 	udptransp = svcudp_create(RPC_ANYSOCK);
    323   1.6  deraadt 	if (udptransp == NULL) {
    324   1.1  deraadt 		fprintf(stderr, "cannot create udp service.");
    325   1.1  deraadt 		exit(1);
    326   1.1  deraadt 	}
    327   1.6  deraadt 	if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
    328   1.6  deraadt 	    IPPROTO_UDP)) {
    329   1.1  deraadt 		fprintf(stderr, "unable to register (YPBINDPROG, YPBINDVERS, udp).");
    330   1.1  deraadt 		exit(1);
    331   1.1  deraadt 	}
    332   1.1  deraadt 
    333   1.6  deraadt 	tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
    334   1.6  deraadt 	if (tcptransp == NULL) {
    335   1.1  deraadt 		fprintf(stderr, "cannot create tcp service.");
    336   1.1  deraadt 		exit(1);
    337   1.1  deraadt 	}
    338   1.1  deraadt 
    339   1.6  deraadt 	if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
    340   1.6  deraadt 	    IPPROTO_TCP)) {
    341   1.1  deraadt 		fprintf(stderr, "unable to register (YPBINDPROG, YPBINDVERS, tcp).");
    342   1.1  deraadt 		exit(1);
    343   1.1  deraadt 	}
    344   1.1  deraadt 
    345   1.1  deraadt 	if( (rpcsock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    346   1.1  deraadt 		perror("socket");
    347   1.1  deraadt 		return -1;
    348   1.1  deraadt 	}
    349  1.10  deraadt 	if( (pingsock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    350  1.10  deraadt 		perror("socket");
    351  1.10  deraadt 		return -1;
    352  1.10  deraadt 	}
    353   1.6  deraadt 
    354   1.1  deraadt 	fcntl(rpcsock, F_SETFL, fcntl(rpcsock, F_GETFL, 0) | FNDELAY);
    355  1.10  deraadt 	fcntl(pingsock, F_SETFL, fcntl(rpcsock, F_GETFL, 0) | FNDELAY);
    356   1.1  deraadt 	i = 1;
    357   1.1  deraadt 	setsockopt(rpcsock, SOL_SOCKET, SO_BROADCAST, &i, sizeof(i));
    358   1.1  deraadt 	rmtca.prog = YPPROG;
    359   1.1  deraadt 	rmtca.vers = YPVERS;
    360   1.1  deraadt 	rmtca.proc = YPPROC_DOMAIN_NONACK;
    361   1.1  deraadt 	rmtca.xdr_args = NULL;		/* set at call time */
    362   1.1  deraadt 	rmtca.args_ptr = NULL;		/* set at call time */
    363   1.1  deraadt 	rmtcr.port_ptr = &rmtcr_port;
    364   1.1  deraadt 	rmtcr.xdr_results = xdr_bool;
    365   1.1  deraadt 	rmtcr.results_ptr = (caddr_t)&rmtcr_outval;
    366   1.1  deraadt 
    367   1.1  deraadt 	/* build initial domain binding, make it "unsuccessful" */
    368   1.1  deraadt 	ypbindlist = (struct _dom_binding *)malloc(sizeof *ypbindlist);
    369   1.1  deraadt 	bzero((char *)ypbindlist, sizeof *ypbindlist);
    370   1.1  deraadt 	strncpy(ypbindlist->dom_domain, domainname, sizeof ypbindlist->dom_domain);
    371   1.1  deraadt 	ypbindlist->dom_vers = YPVERS;
    372   1.1  deraadt 	ypbindlist->dom_alive = 0;
    373   1.1  deraadt 	ypbindlist->dom_lockfd = -1;
    374   1.1  deraadt 	sprintf(path, "%s/%s.%d", BINDINGDIR, ypbindlist->dom_domain,
    375   1.1  deraadt 		ypbindlist->dom_vers);
    376   1.1  deraadt 	(void)unlink(path);
    377   1.1  deraadt 
    378   1.1  deraadt 	width = getdtablesize();
    379   1.1  deraadt 	while(1) {
    380   1.1  deraadt 		fdsr = svc_fdset;
    381   1.1  deraadt 		FD_SET(rpcsock, &fdsr);
    382  1.10  deraadt 		FD_SET(pingsock, &fdsr);
    383   1.1  deraadt 		tv.tv_sec = 1;
    384   1.1  deraadt 		tv.tv_usec = 0;
    385   1.1  deraadt 
    386   1.1  deraadt 		switch(select(width, &fdsr, NULL, NULL, &tv)) {
    387   1.1  deraadt 		case 0:
    388   1.1  deraadt 			checkwork();
    389   1.1  deraadt 			break;
    390   1.1  deraadt 		case -1:
    391   1.1  deraadt 			perror("select\n");
    392   1.1  deraadt 			break;
    393   1.1  deraadt 		default:
    394   1.1  deraadt 			if(FD_ISSET(rpcsock, &fdsr)) {
    395   1.1  deraadt 				FD_CLR(rpcsock, &fdsr);
    396   1.1  deraadt 				handle_replies();
    397   1.1  deraadt 			}
    398  1.10  deraadt 			if (FD_ISSET(pingsock, &fdsr)) {
    399  1.10  deraadt 				FD_CLR(pingsock, &fdsr);
    400  1.10  deraadt 				handle_ping();
    401  1.10  deraadt 			}
    402   1.1  deraadt 			svc_getreqset(&fdsr);
    403   1.1  deraadt 			if(check)
    404   1.1  deraadt 				checkwork();
    405   1.1  deraadt 			break;
    406   1.1  deraadt 		}
    407   1.1  deraadt 	}
    408   1.1  deraadt }
    409   1.1  deraadt 
    410   1.1  deraadt /*
    411   1.9  deraadt  * State transition is done like this:
    412   1.9  deraadt  *
    413  1.11       ws  * STATE	EVENT		ACTION			NEWSTATE	TIMEOUT
    414  1.11       ws  * no binding	timeout		broadcast 		no binding	5 sec
    415  1.11       ws  * no binding	answer		--			binding		60 sec
    416  1.11       ws  * binding	timeout		ping server		checking	5 sec
    417  1.11       ws  * checking	timeout		ping server + broadcast	checking	5 sec
    418  1.11       ws  * checking	answer		--			binding		60 sec
    419   1.1  deraadt  */
    420   1.1  deraadt checkwork()
    421   1.1  deraadt {
    422   1.1  deraadt 	struct _dom_binding *ypdb;
    423   1.1  deraadt 	time_t t;
    424   1.1  deraadt 
    425   1.1  deraadt 	check = 0;
    426   1.1  deraadt 
    427   1.1  deraadt 	time(&t);
    428   1.1  deraadt 	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) {
    429   1.9  deraadt 		if(ypdb->dom_check_t < t) {
    430  1.10  deraadt 			if (ypdb->dom_alive == 1)
    431  1.10  deraadt 				ping(ypdb);
    432  1.10  deraadt 			else
    433  1.10  deraadt 				broadcast(ypdb);
    434   1.1  deraadt 			time(&t);
    435   1.1  deraadt 			ypdb->dom_check_t = t + 5;
    436   1.1  deraadt 		}
    437   1.1  deraadt 	}
    438   1.1  deraadt }
    439   1.1  deraadt 
    440   1.8  deraadt ping(ypdb)
    441   1.8  deraadt 	struct _dom_binding *ypdb;
    442   1.1  deraadt {
    443   1.8  deraadt 	char *dom = ypdb->dom_domain;
    444   1.1  deraadt 	struct rpc_msg rpcmsg;
    445  1.10  deraadt 	char buf[1400];
    446  1.10  deraadt 	enum clnt_stat st;
    447  1.10  deraadt 	int outlen;
    448  1.10  deraadt 	AUTH *rpcua;
    449  1.10  deraadt 	XDR rpcxdr;
    450  1.10  deraadt 
    451  1.10  deraadt 	bzero((char *)&rpcxdr, sizeof rpcxdr);
    452  1.10  deraadt 	bzero((char *)&rpcmsg, sizeof rpcmsg);
    453  1.10  deraadt 
    454  1.10  deraadt 	rpcua = authunix_create_default();
    455  1.10  deraadt 	if( rpcua == (AUTH *)NULL) {
    456  1.10  deraadt 		/*printf("cannot get unix auth\n");*/
    457  1.10  deraadt 		return RPC_SYSTEMERROR;
    458  1.10  deraadt 	}
    459  1.10  deraadt 	rpcmsg.rm_direction = CALL;
    460  1.10  deraadt 	rpcmsg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
    461  1.10  deraadt 	rpcmsg.rm_call.cb_prog = YPPROG;
    462  1.10  deraadt 	rpcmsg.rm_call.cb_vers = YPVERS;
    463  1.10  deraadt 	rpcmsg.rm_call.cb_proc = YPPROC_DOMAIN_NONACK;
    464  1.10  deraadt 	rpcmsg.rm_call.cb_cred = rpcua->ah_cred;
    465  1.10  deraadt 	rpcmsg.rm_call.cb_verf = rpcua->ah_verf;
    466  1.10  deraadt 
    467  1.10  deraadt 	rpcmsg.rm_xid = (int)dom;
    468  1.10  deraadt 	xdrmem_create(&rpcxdr, buf, sizeof buf, XDR_ENCODE);
    469  1.10  deraadt 	if( (!xdr_callmsg(&rpcxdr, &rpcmsg)) ) {
    470  1.10  deraadt 		st = RPC_CANTENCODEARGS;
    471  1.10  deraadt 		AUTH_DESTROY(rpcua);
    472  1.10  deraadt 		return st;
    473  1.10  deraadt 	}
    474  1.10  deraadt 	if( (!xdr_domainname(&rpcxdr, dom)) ) {
    475  1.10  deraadt 		st = RPC_CANTENCODEARGS;
    476  1.10  deraadt 		AUTH_DESTROY(rpcua);
    477  1.10  deraadt 		return st;
    478  1.10  deraadt 	}
    479  1.10  deraadt 	outlen = (int)xdr_getpos(&rpcxdr);
    480  1.10  deraadt 	xdr_destroy(&rpcxdr);
    481  1.10  deraadt 	if(outlen<1) {
    482  1.10  deraadt 		st = RPC_CANTENCODEARGS;
    483  1.10  deraadt 		AUTH_DESTROY(rpcua);
    484  1.10  deraadt 		return st;
    485  1.10  deraadt 	}
    486  1.10  deraadt 	AUTH_DESTROY(rpcua);
    487  1.10  deraadt 
    488  1.10  deraadt 	ypdb->dom_alive = 2;
    489  1.10  deraadt 	if (sendto(pingsock, buf, outlen, 0,
    490  1.10  deraadt 		   (struct sockaddr *)&ypdb->dom_server_addr,
    491  1.10  deraadt 		   sizeof ypdb->dom_server_addr) < 0)
    492  1.10  deraadt 		perror("sendto");
    493  1.10  deraadt 	return 0;
    494  1.10  deraadt 
    495  1.10  deraadt }
    496  1.10  deraadt 
    497  1.10  deraadt broadcast(ypdb)
    498  1.10  deraadt 	struct _dom_binding *ypdb;
    499  1.10  deraadt {
    500  1.10  deraadt 	char *dom = ypdb->dom_domain;
    501  1.10  deraadt 	struct rpc_msg rpcmsg;
    502   1.1  deraadt 	char buf[1400], inbuf[8192];
    503   1.9  deraadt 	char path[MAXPATHLEN];
    504   1.1  deraadt 	enum clnt_stat st;
    505   1.1  deraadt 	int outlen, i, sock, len;
    506   1.1  deraadt 	struct sockaddr_in bsin;
    507   1.1  deraadt 	struct ifconf ifc;
    508   1.1  deraadt 	struct ifreq ifreq, *ifr;
    509   1.1  deraadt 	struct in_addr in;
    510   1.1  deraadt 	AUTH *rpcua;
    511   1.1  deraadt 	XDR rpcxdr;
    512   1.1  deraadt 
    513   1.1  deraadt 	rmtca.xdr_args = xdr_domainname;
    514   1.1  deraadt 	rmtca.args_ptr = dom;
    515   1.1  deraadt 
    516   1.1  deraadt 	bzero((char *)&rpcxdr, sizeof rpcxdr);
    517   1.1  deraadt 	bzero((char *)&rpcmsg, sizeof rpcmsg);
    518   1.1  deraadt 
    519   1.1  deraadt 	rpcua = authunix_create_default();
    520   1.1  deraadt 	if( rpcua == (AUTH *)NULL) {
    521   1.1  deraadt 		/*printf("cannot get unix auth\n");*/
    522   1.1  deraadt 		return RPC_SYSTEMERROR;
    523   1.1  deraadt 	}
    524   1.1  deraadt 	rpcmsg.rm_direction = CALL;
    525   1.1  deraadt 	rpcmsg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
    526   1.1  deraadt 	rpcmsg.rm_call.cb_prog = PMAPPROG;
    527   1.1  deraadt 	rpcmsg.rm_call.cb_vers = PMAPVERS;
    528   1.1  deraadt 	rpcmsg.rm_call.cb_proc = PMAPPROC_CALLIT;
    529   1.1  deraadt 	rpcmsg.rm_call.cb_cred = rpcua->ah_cred;
    530   1.1  deraadt 	rpcmsg.rm_call.cb_verf = rpcua->ah_verf;
    531   1.1  deraadt 
    532   1.1  deraadt 	rpcmsg.rm_xid = (int)dom;
    533   1.1  deraadt 	xdrmem_create(&rpcxdr, buf, sizeof buf, XDR_ENCODE);
    534   1.1  deraadt 	if( (!xdr_callmsg(&rpcxdr, &rpcmsg)) ) {
    535   1.1  deraadt 		st = RPC_CANTENCODEARGS;
    536   1.1  deraadt 		AUTH_DESTROY(rpcua);
    537   1.1  deraadt 		return st;
    538   1.1  deraadt 	}
    539   1.1  deraadt 	if( (!xdr_rmtcall_args(&rpcxdr, &rmtca)) ) {
    540   1.1  deraadt 		st = RPC_CANTENCODEARGS;
    541   1.1  deraadt 		AUTH_DESTROY(rpcua);
    542   1.1  deraadt 		return st;
    543   1.1  deraadt 	}
    544   1.1  deraadt 	outlen = (int)xdr_getpos(&rpcxdr);
    545   1.1  deraadt 	xdr_destroy(&rpcxdr);
    546   1.1  deraadt 	if(outlen<1) {
    547  1.10  deraadt 		st = RPC_CANTENCODEARGS;
    548   1.1  deraadt 		AUTH_DESTROY(rpcua);
    549   1.1  deraadt 		return st;
    550   1.1  deraadt 	}
    551   1.1  deraadt 	AUTH_DESTROY(rpcua);
    552   1.1  deraadt 
    553   1.9  deraadt 	if(ypdb->dom_lockfd!=-1) {
    554   1.9  deraadt 		close(ypdb->dom_lockfd);
    555   1.9  deraadt 		ypdb->dom_lockfd = -1;
    556   1.9  deraadt 		sprintf(path, "%s/%s.%d", BINDINGDIR,
    557  1.10  deraadt 			ypdb->dom_domain, ypdb->dom_vers);
    558   1.9  deraadt 		unlink(path);
    559   1.9  deraadt 	}
    560   1.8  deraadt 
    561   1.9  deraadt 	bzero((char *)&bsin, sizeof bsin);
    562   1.9  deraadt 	bsin.sin_family = AF_INET;
    563   1.9  deraadt 	bsin.sin_port = htons(PMAPPORT);
    564   1.9  deraadt 
    565  1.11       ws 	if (ypdb->dom_alive == 2) {
    566  1.11       ws 		/*
    567  1.11       ws 		 * This resolves the following situation:
    568  1.11       ws 		 * ypserver on other subnet was once bound,
    569  1.11       ws 		 * but rebooted and is now using a different port
    570  1.11       ws 		 */
    571  1.11       ws 		bsin.sin_addr = ypdb->dom_server_addr.sin_addr;
    572  1.11       ws 		if (sendto(rpcsock, buf, outlen, 0, (struct sockaddr *)&bsin,
    573  1.11       ws 			   sizeof bsin) < 0)
    574  1.11       ws 			perror("sendto");
    575  1.11       ws 	}
    576   1.1  deraadt 	/* find all networks and send the RPC packet out them all */
    577   1.1  deraadt 	if( (sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    578   1.1  deraadt 		perror("socket");
    579   1.1  deraadt 		return -1;
    580   1.1  deraadt 	}
    581   1.9  deraadt 
    582   1.1  deraadt 	ifc.ifc_len = sizeof inbuf;
    583   1.1  deraadt 	ifc.ifc_buf = inbuf;
    584   1.1  deraadt 	if( ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
    585   1.1  deraadt 		close(sock);
    586   1.1  deraadt 		perror("ioctl(SIOCGIFCONF)");
    587   1.1  deraadt 		return -1;
    588   1.1  deraadt 	}
    589   1.1  deraadt 	ifr = ifc.ifc_req;
    590   1.1  deraadt 	ifreq.ifr_name[0] = '\0';
    591   1.1  deraadt 	for(i=0; i<ifc.ifc_len; i+=len, ifr=(struct ifreq *)((caddr_t)ifr+len)) {
    592   1.1  deraadt #if defined(BSD) && BSD >= 199103
    593   1.1  deraadt 		len = sizeof ifr->ifr_name + ifr->ifr_addr.sa_len;
    594   1.1  deraadt #else
    595   1.1  deraadt 		len = sizeof ifc.ifc_len / sizeof(struct ifreq);
    596   1.1  deraadt #endif
    597   1.1  deraadt 		ifreq = *ifr;
    598   1.1  deraadt 		if( ifreq.ifr_addr.sa_family != AF_INET)
    599   1.1  deraadt 			continue;
    600   1.1  deraadt 		if( ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0) {
    601   1.1  deraadt 			perror("ioctl(SIOCGIFFLAGS)");
    602   1.1  deraadt 			continue;
    603   1.1  deraadt 		}
    604   1.1  deraadt 		if( (ifreq.ifr_flags & IFF_UP) == 0)
    605   1.1  deraadt 			continue;
    606   1.1  deraadt 
    607   1.1  deraadt 		ifreq.ifr_flags &= (IFF_LOOPBACK | IFF_BROADCAST);
    608   1.1  deraadt 		if( ifreq.ifr_flags==IFF_BROADCAST ) {
    609   1.1  deraadt 			if( ioctl(sock, SIOCGIFBRDADDR, &ifreq) < 0 ) {
    610   1.1  deraadt 				perror("ioctl(SIOCGIFBRDADDR)");
    611   1.1  deraadt 				continue;
    612   1.1  deraadt 			}
    613   1.1  deraadt 		} else if( ifreq.ifr_flags==IFF_LOOPBACK ) {
    614   1.1  deraadt 			if( ioctl(sock, SIOCGIFADDR, &ifreq) < 0 ) {
    615   1.1  deraadt 				perror("ioctl(SIOCGIFADDR)");
    616   1.1  deraadt 				continue;
    617   1.1  deraadt 			}
    618   1.1  deraadt 		} else
    619   1.1  deraadt 			continue;
    620   1.1  deraadt 
    621   1.1  deraadt 		in = ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr;
    622   1.1  deraadt 		bsin.sin_addr = in;
    623   1.1  deraadt 		if( sendto(rpcsock, buf, outlen, 0, (struct sockaddr *)&bsin,
    624  1.10  deraadt 			   sizeof bsin) < 0 )
    625   1.1  deraadt 			perror("sendto");
    626   1.1  deraadt 	}
    627   1.1  deraadt 	close(sock);
    628   1.1  deraadt 	return 0;
    629   1.1  deraadt }
    630   1.1  deraadt 
    631   1.1  deraadt /*enum clnt_stat*/
    632   1.1  deraadt handle_replies()
    633   1.1  deraadt {
    634   1.1  deraadt 	char buf[1400];
    635   1.1  deraadt 	int fromlen, inlen;
    636   1.1  deraadt 	struct sockaddr_in raddr;
    637   1.1  deraadt 	struct rpc_msg msg;
    638   1.1  deraadt 	XDR xdr;
    639   1.1  deraadt 
    640   1.1  deraadt recv_again:
    641   1.1  deraadt 	bzero((char *)&xdr, sizeof(xdr));
    642   1.1  deraadt 	bzero((char *)&msg, sizeof(msg));
    643   1.1  deraadt 	msg.acpted_rply.ar_verf = _null_auth;
    644   1.1  deraadt 	msg.acpted_rply.ar_results.where = (caddr_t)&rmtcr;
    645   1.1  deraadt 	msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
    646   1.1  deraadt 
    647   1.1  deraadt try_again:
    648   1.1  deraadt 	fromlen = sizeof (struct sockaddr);
    649   1.1  deraadt 	inlen = recvfrom(rpcsock, buf, sizeof buf, 0,
    650   1.1  deraadt 		(struct sockaddr *)&raddr, &fromlen);
    651   1.1  deraadt 	if(inlen<0) {
    652   1.1  deraadt 		if(errno==EINTR)
    653   1.1  deraadt 			goto try_again;
    654   1.1  deraadt 		return RPC_CANTRECV;
    655   1.1  deraadt 	}
    656   1.1  deraadt 	if(inlen<sizeof(u_long))
    657   1.1  deraadt 		goto recv_again;
    658   1.1  deraadt 
    659   1.1  deraadt 	/*
    660   1.1  deraadt 	 * see if reply transaction id matches sent id.
    661   1.1  deraadt 	 * If so, decode the results.
    662   1.1  deraadt 	 */
    663   1.1  deraadt 	xdrmem_create(&xdr, buf, (u_int)inlen, XDR_DECODE);
    664   1.1  deraadt 	if( xdr_replymsg(&xdr, &msg)) {
    665   1.1  deraadt 		if( (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
    666   1.1  deraadt 		   (msg.acpted_rply.ar_stat == SUCCESS)) {
    667   1.1  deraadt 			raddr.sin_port = htons((u_short)rmtcr_port);
    668   1.1  deraadt 			rpc_received(msg.rm_xid, &raddr, 0);
    669   1.1  deraadt 		}
    670   1.1  deraadt 	}
    671   1.1  deraadt 	xdr.x_op = XDR_FREE;
    672   1.1  deraadt 	msg.acpted_rply.ar_results.proc = xdr_void;
    673   1.1  deraadt 	xdr_destroy(&xdr);
    674   1.1  deraadt 
    675   1.1  deraadt 	return RPC_SUCCESS;
    676   1.1  deraadt }
    677   1.1  deraadt 
    678  1.10  deraadt /*enum clnt_stat*/
    679  1.10  deraadt handle_ping()
    680  1.10  deraadt {
    681  1.10  deraadt 	char buf[1400];
    682  1.10  deraadt 	int fromlen, inlen;
    683  1.10  deraadt 	struct sockaddr_in raddr;
    684  1.10  deraadt 	struct rpc_msg msg;
    685  1.10  deraadt 	XDR xdr;
    686  1.10  deraadt 	bool_t res;
    687  1.10  deraadt 
    688  1.10  deraadt recv_again:
    689  1.10  deraadt 	bzero((char *)&xdr, sizeof(xdr));
    690  1.10  deraadt 	bzero((char *)&msg, sizeof(msg));
    691  1.10  deraadt 	msg.acpted_rply.ar_verf = _null_auth;
    692  1.10  deraadt 	msg.acpted_rply.ar_results.where = (caddr_t)&res;
    693  1.10  deraadt 	msg.acpted_rply.ar_results.proc = xdr_bool;
    694  1.10  deraadt 
    695  1.10  deraadt try_again:
    696  1.10  deraadt 	fromlen = sizeof (struct sockaddr);
    697  1.10  deraadt 	inlen = recvfrom(pingsock, buf, sizeof buf, 0,
    698  1.10  deraadt 		(struct sockaddr *)&raddr, &fromlen);
    699  1.10  deraadt 	if(inlen<0) {
    700  1.10  deraadt 		if(errno==EINTR)
    701  1.10  deraadt 			goto try_again;
    702  1.10  deraadt 		return RPC_CANTRECV;
    703  1.10  deraadt 	}
    704  1.10  deraadt 	if(inlen<sizeof(u_long))
    705  1.10  deraadt 		goto recv_again;
    706  1.10  deraadt 
    707  1.10  deraadt 	/*
    708  1.10  deraadt 	 * see if reply transaction id matches sent id.
    709  1.10  deraadt 	 * If so, decode the results.
    710  1.10  deraadt 	 */
    711  1.10  deraadt 	xdrmem_create(&xdr, buf, (u_int)inlen, XDR_DECODE);
    712  1.10  deraadt 	if( xdr_replymsg(&xdr, &msg)) {
    713  1.10  deraadt 		if( (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
    714  1.10  deraadt 		   (msg.acpted_rply.ar_stat == SUCCESS)) {
    715  1.10  deraadt 			rpc_received(msg.rm_xid, &raddr, 0);
    716  1.10  deraadt 		}
    717  1.10  deraadt 	}
    718  1.10  deraadt 	xdr.x_op = XDR_FREE;
    719  1.10  deraadt 	msg.acpted_rply.ar_results.proc = xdr_void;
    720  1.10  deraadt 	xdr_destroy(&xdr);
    721  1.10  deraadt 
    722  1.10  deraadt 	return RPC_SUCCESS;
    723  1.10  deraadt }
    724  1.10  deraadt 
    725   1.1  deraadt /*
    726   1.1  deraadt  * LOOPBACK IS MORE IMPORTANT: PUT IN HACK
    727   1.1  deraadt  */
    728   1.1  deraadt rpc_received(dom, raddrp, force)
    729   1.1  deraadt char *dom;
    730   1.1  deraadt struct sockaddr_in *raddrp;
    731   1.1  deraadt int force;
    732   1.1  deraadt {
    733   1.1  deraadt 	struct _dom_binding *ypdb;
    734   1.6  deraadt 	struct iovec iov[2];
    735   1.6  deraadt 	struct ypbind_resp ybr;
    736   1.1  deraadt 	char path[MAXPATHLEN];
    737   1.1  deraadt 	int fd;
    738   1.1  deraadt 
    739   1.1  deraadt 	/*printf("returned from %s about %s\n", inet_ntoa(raddrp->sin_addr), dom);*/
    740   1.1  deraadt 
    741   1.1  deraadt 	if(dom==NULL)
    742   1.1  deraadt 		return;
    743   1.1  deraadt 
    744   1.1  deraadt 	for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext)
    745   1.1  deraadt 		if( strcmp(ypdb->dom_domain, dom) == 0)
    746   1.1  deraadt 			break;
    747   1.1  deraadt 
    748   1.1  deraadt 	if(ypdb==NULL) {
    749   1.1  deraadt 		if(force==0)
    750   1.1  deraadt 			return;
    751   1.1  deraadt 		ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
    752   1.1  deraadt 		bzero((char *)ypdb, sizeof *ypdb);
    753   1.1  deraadt 		strncpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain);
    754   1.1  deraadt 		ypdb->dom_lockfd = -1;
    755   1.1  deraadt 		ypdb->dom_pnext = ypbindlist;
    756   1.1  deraadt 		ypbindlist = ypdb;
    757   1.1  deraadt 	}
    758   1.1  deraadt 
    759   1.8  deraadt 	/* soft update, alive */
    760  1.11       ws 	if(ypdb->dom_alive==1 && force==0) {
    761   1.8  deraadt 		if (bcmp((char *)raddrp, (char *)&ypdb->dom_server_addr,
    762  1.10  deraadt 			 sizeof ypdb->dom_server_addr) == 0) {
    763   1.8  deraadt 			ypdb->dom_alive = 1;
    764  1.10  deraadt 			ypdb->dom_check_t = time(NULL) + 60; /* recheck binding in 60 sec */
    765   1.8  deraadt 		}
    766   1.1  deraadt 		return;
    767   1.8  deraadt 	}
    768   1.8  deraadt 
    769   1.1  deraadt 	bcopy((char *)raddrp, (char *)&ypdb->dom_server_addr,
    770   1.1  deraadt 		sizeof ypdb->dom_server_addr);
    771  1.10  deraadt 	ypdb->dom_check_t = time(NULL) + 60;	/* recheck binding in 60 seconds */
    772   1.1  deraadt 	ypdb->dom_vers = YPVERS;
    773   1.1  deraadt 	ypdb->dom_alive = 1;
    774   1.1  deraadt 
    775   1.1  deraadt 	if(ypdb->dom_lockfd != -1)
    776   1.1  deraadt 		close(ypdb->dom_lockfd);
    777   1.1  deraadt 
    778   1.1  deraadt 	sprintf(path, "%s/%s.%d", BINDINGDIR,
    779   1.1  deraadt 		ypdb->dom_domain, ypdb->dom_vers);
    780   1.1  deraadt #ifdef O_SHLOCK
    781   1.1  deraadt 	if( (fd=open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
    782   1.1  deraadt 		(void)mkdir(BINDINGDIR, 0755);
    783   1.1  deraadt 		if( (fd=open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
    784   1.1  deraadt 			return;
    785   1.1  deraadt 	}
    786   1.1  deraadt #else
    787   1.1  deraadt 	if( (fd=open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
    788   1.1  deraadt 		(void)mkdir(BINDINGDIR, 0755);
    789   1.1  deraadt 		if( (fd=open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1)
    790   1.1  deraadt 			return;
    791   1.1  deraadt 	}
    792   1.1  deraadt 	flock(fd, LOCK_SH);
    793   1.3  deraadt #endif
    794   1.1  deraadt 
    795   1.1  deraadt 	/*
    796   1.1  deraadt 	 * ok, if BINDINGDIR exists, and we can create the binding file,
    797   1.1  deraadt 	 * then write to it..
    798   1.1  deraadt 	 */
    799   1.1  deraadt 	ypdb->dom_lockfd = fd;
    800   1.6  deraadt 
    801   1.6  deraadt 	iov[0].iov_base = (caddr_t)&(udptransp->xp_port);
    802   1.6  deraadt 	iov[0].iov_len = sizeof udptransp->xp_port;
    803   1.6  deraadt 	iov[1].iov_base = (caddr_t)&ybr;
    804   1.6  deraadt 	iov[1].iov_len = sizeof ybr;
    805   1.6  deraadt 
    806   1.6  deraadt 	bzero(&ybr, sizeof ybr);
    807   1.6  deraadt 	ybr.ypbind_status = YPBIND_SUCC_VAL;
    808   1.6  deraadt 	ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr = raddrp->sin_addr;
    809   1.6  deraadt 	ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port = raddrp->sin_port;
    810   1.6  deraadt 
    811   1.6  deraadt 	if( writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) {
    812   1.1  deraadt 		perror("write");
    813   1.1  deraadt 		close(ypdb->dom_lockfd);
    814   1.9  deraadt 		unlink(path);
    815   1.1  deraadt 		ypdb->dom_lockfd = -1;
    816   1.1  deraadt 		return;
    817   1.1  deraadt 	}
    818   1.1  deraadt }
    819