Home | History | Annotate | Line # | Download | only in yp
yplib.c revision 1.14
      1 /*	$NetBSD: yplib.c,v 1.14 1995/02/27 13:00:53 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt (at) fsa.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Theo de Raadt.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef LINT
     35 static char *rcsid = "$NetBSD: yplib.c,v 1.14 1995/02/27 13:00:53 cgd Exp $";
     36 #endif
     37 
     38 #include <sys/param.h>
     39 #include <sys/types.h>
     40 #include <sys/socket.h>
     41 #include <sys/file.h>
     42 #include <sys/uio.h>
     43 #include <errno.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <unistd.h>
     48 #include <rpc/rpc.h>
     49 #include <rpc/xdr.h>
     50 #include <rpcsvc/yp_prot.h>
     51 #include <rpcsvc/ypclnt.h>
     52 
     53 #define BINDINGDIR	"/var/yp/binding"
     54 #define YPBINDLOCK	"/var/run/ypbind.lock"
     55 #define YPMATCHCACHE
     56 
     57 extern bool_t xdr_domainname(), xdr_ypbind_resp();
     58 extern bool_t xdr_ypreq_key(), xdr_ypresp_val();
     59 extern bool_t xdr_ypreq_nokey(), xdr_ypresp_key_val();
     60 extern bool_t xdr_ypresp_all(), xdr_ypresp_all_seq();
     61 extern bool_t xdr_ypresp_master();
     62 
     63 int (*ypresp_allfn)();
     64 void *ypresp_data;
     65 
     66 struct dom_binding *_ypbindlist;
     67 static char _yp_domain[MAXHOSTNAMELEN];
     68 int _yplib_timeout = 10;
     69 
     70 #ifdef YPMATCHCACHE
     71 int _yplib_cache = 5;
     72 
     73 static struct ypmatch_ent {
     74 	struct ypmatch_ent *next;
     75 	char *map, *key, *val;
     76 	int keylen, vallen;
     77 	time_t expire_t;
     78 } *ypmc;
     79 
     80 static void
     81 ypmatch_add(map, key, keylen, val, vallen)
     82 char *map;
     83 char *key;
     84 int keylen;
     85 char *val;
     86 int vallen;
     87 {
     88 	struct ypmatch_ent *ep;
     89 	time_t t;
     90 
     91 	time(&t);
     92 
     93 	for(ep=ypmc; ep; ep=ep->next)
     94 		if(ep->expire_t < t)
     95 			break;
     96 	if(ep==NULL) {
     97 		ep = (struct ypmatch_ent *)malloc(sizeof *ep);
     98 		memset(ep, 0, sizeof *ep);
     99 		if(ypmc)
    100 			ep->next = ypmc;
    101 		ypmc = ep;
    102 	}
    103 
    104 	if(ep->key)
    105 		free(ep->key);
    106 	if(ep->val)
    107 		free(ep->val);
    108 
    109 	ep->key = NULL;
    110 	ep->val = NULL;
    111 
    112 	ep->key = (char *)malloc(keylen);
    113 	if(ep->key==NULL)
    114 		return;
    115 
    116 	ep->val = (char *)malloc(vallen);
    117 	if(ep->key==NULL) {
    118 		free(ep->key);
    119 		ep->key = NULL;
    120 		return;
    121 	}
    122 	ep->keylen = keylen;
    123 	ep->vallen = vallen;
    124 
    125 	memcpy(ep->key, key, ep->keylen);
    126 	memcpy(ep->val, val, ep->vallen);
    127 
    128 	if(ep->map) {
    129 		if( strcmp(ep->map, map) ) {
    130 			free(ep->map);
    131 			ep->map = strdup(map);
    132 		}
    133 	} else {
    134 		ep->map = strdup(map);
    135 	}
    136 
    137 	ep->expire_t = t + _yplib_cache;
    138 }
    139 
    140 static bool_t
    141 ypmatch_find(map, key, keylen, val, vallen)
    142 char *map;
    143 char *key;
    144 int keylen;
    145 char **val;
    146 int *vallen;
    147 {
    148 	struct ypmatch_ent *ep;
    149 	time_t t;
    150 
    151 	if(ypmc==NULL)
    152 		return 0;
    153 
    154 	time(&t);
    155 
    156 	for(ep=ypmc; ep; ep=ep->next) {
    157 		if(ep->keylen != keylen)
    158 			continue;
    159 		if(strcmp(ep->map, map))
    160 			continue;
    161 		if(memcmp(ep->key, key, keylen))
    162 			continue;
    163 		if(t > ep->expire_t)
    164 			continue;
    165 
    166 		*val = ep->val;
    167 		*vallen = ep->vallen;
    168 		return 1;
    169 	}
    170 	return 0;
    171 }
    172 #endif
    173 
    174 int
    175 _yp_dobind(dom, ypdb)
    176 char *dom;
    177 struct dom_binding **ypdb;
    178 {
    179 	static int pid = -1;
    180 	char path[MAXPATHLEN];
    181 	struct dom_binding *ysd, *ysd2;
    182 	struct ypbind_resp ypbr;
    183 	struct timeval tv;
    184 	struct sockaddr_in clnt_sin;
    185 	int clnt_sock, fd, gpid;
    186 	CLIENT *client;
    187 	int new=0, r;
    188 	int count = 0;
    189 
    190 	/*
    191 	 * test if YP is running or not
    192 	 */
    193 	if ((fd=open(YPBINDLOCK, O_RDONLY)) == -1)
    194 		return YPERR_YPBIND;
    195 	if( !(flock(fd, LOCK_EX|LOCK_NB) == -1 && errno==EWOULDBLOCK)) {
    196 		close(fd);
    197 		return YPERR_YPBIND;
    198 	}
    199 	close(fd);
    200 
    201 	gpid = getpid();
    202 	if( !(pid==-1 || pid==gpid) ) {
    203 		ysd = _ypbindlist;
    204 		while(ysd) {
    205 			if(ysd->dom_client)
    206 				clnt_destroy(ysd->dom_client);
    207 			ysd2 = ysd->dom_pnext;
    208 			free(ysd);
    209 			ysd = ysd2;
    210 		}
    211 		_ypbindlist = NULL;
    212 	}
    213 	pid = gpid;
    214 
    215 	if(ypdb!=NULL)
    216 		*ypdb = NULL;
    217 
    218 	if(dom==NULL || strlen(dom)==0)
    219 		return YPERR_BADARGS;
    220 
    221 	for(ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
    222 		if( strcmp(dom, ysd->dom_domain) == 0)
    223 			break;
    224 	if(ysd==NULL) {
    225 		ysd = (struct dom_binding *)malloc(sizeof *ysd);
    226 		memset(ysd, 0, sizeof *ysd);
    227 		ysd->dom_socket = -1;
    228 		ysd->dom_vers = 0;
    229 		new = 1;
    230 	}
    231 again:
    232 	if(ysd->dom_vers==0) {
    233 		sprintf(path, "%s/%s.%d", BINDINGDIR, dom, 2);
    234 		if( (fd=open(path, O_RDONLY)) == -1) {
    235 			/* no binding file, YP is dead, or not yet fully alive. */
    236 			goto trynet;
    237 		}
    238 		if( flock(fd, LOCK_EX|LOCK_NB) == -1 && errno==EWOULDBLOCK) {
    239 			struct iovec iov[2];
    240 			struct ypbind_resp ybr;
    241 			u_short	ypb_port;
    242 
    243 			iov[0].iov_base = (caddr_t)&ypb_port;
    244 			iov[0].iov_len = sizeof ypb_port;
    245 			iov[1].iov_base = (caddr_t)&ybr;
    246 			iov[1].iov_len = sizeof ybr;
    247 
    248 			r = readv(fd, iov, 2);
    249 			if(r != iov[0].iov_len + iov[1].iov_len) {
    250 				close(fd);
    251 				ysd->dom_vers = -1;
    252 				goto again;
    253 			}
    254 
    255 			memset(&ysd->dom_server_addr, 0, sizeof ysd->dom_server_addr);
    256 			ysd->dom_server_addr.sin_family = AF_INET;
    257 			ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
    258 			ysd->dom_server_addr.sin_addr =
    259 			    ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr;
    260 			ysd->dom_server_addr.sin_port =
    261 			    ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port;
    262 
    263 			ysd->dom_server_port = ysd->dom_server_addr.sin_port;
    264 			close(fd);
    265 			goto gotit;
    266 		} else {
    267 			/* no lock on binding file, YP is dead. */
    268 			close(fd);
    269 			if(new)
    270 				free(ysd);
    271 			return YPERR_YPBIND;
    272 		}
    273 	}
    274 trynet:
    275 	if(ysd->dom_vers==-1 || ysd->dom_vers==0) {
    276 		memset(&clnt_sin, 0, sizeof clnt_sin);
    277 		clnt_sin.sin_family = AF_INET;
    278 		clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    279 
    280 		clnt_sock = RPC_ANYSOCK;
    281 		client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, &clnt_sock,
    282 			0, 0);
    283 		if(client==NULL) {
    284 			clnt_pcreateerror("clnttcp_create");
    285 			if(new)
    286 				free(ysd);
    287 			return YPERR_YPBIND;
    288 		}
    289 
    290 		tv.tv_sec = _yplib_timeout;
    291 		tv.tv_usec = 0;
    292 		r = clnt_call(client, YPBINDPROC_DOMAIN,
    293 			xdr_domainname, dom, xdr_ypbind_resp, &ypbr, tv);
    294 		if(r != RPC_SUCCESS) {
    295 			if (new==0 || count)
    296 				fprintf(stderr,
    297 				    "YP server for domain %s not responding, still trying\n",
    298 				    dom);
    299 			count++;
    300 			clnt_destroy(client);
    301 			ysd->dom_vers = -1;
    302 			goto again;
    303 		}
    304 		clnt_destroy(client);
    305 
    306 		memset(&ysd->dom_server_addr, 0, sizeof ysd->dom_server_addr);
    307 		ysd->dom_server_addr.sin_family = AF_INET;
    308 		ysd->dom_server_addr.sin_port =
    309 			ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port;
    310 		ysd->dom_server_addr.sin_addr.s_addr =
    311 			ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr;
    312 		ysd->dom_server_port =
    313 			ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port;
    314 gotit:
    315 		ysd->dom_vers = YPVERS;
    316 		strcpy(ysd->dom_domain, dom);
    317 	}
    318 
    319 	tv.tv_sec = _yplib_timeout/2;
    320 	tv.tv_usec = 0;
    321 	if(ysd->dom_client)
    322 		clnt_destroy(ysd->dom_client);
    323 	ysd->dom_socket = RPC_ANYSOCK;
    324 	ysd->dom_client = clntudp_create(&ysd->dom_server_addr,
    325 		YPPROG, YPVERS, tv, &ysd->dom_socket);
    326 	if(ysd->dom_client==NULL) {
    327 		clnt_pcreateerror("clntudp_create");
    328 		ysd->dom_vers = -1;
    329 		goto again;
    330 	}
    331 	if( fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
    332 		perror("fcntl: F_SETFD");
    333 
    334 	if(new) {
    335 		ysd->dom_pnext = _ypbindlist;
    336 		_ypbindlist = ysd;
    337 	}
    338 
    339 	if(ypdb!=NULL)
    340 		*ypdb = ysd;
    341 	return 0;
    342 }
    343 
    344 static void
    345 _yp_unbind(ypb)
    346 struct dom_binding *ypb;
    347 {
    348 	clnt_destroy(ypb->dom_client);
    349 	ypb->dom_client = NULL;
    350 	ypb->dom_socket = -1;
    351 }
    352 
    353 int
    354 yp_bind(dom)
    355 char *dom;
    356 {
    357 	return _yp_dobind(dom, NULL);
    358 }
    359 
    360 void
    361 yp_unbind(dom)
    362 char *dom;
    363 {
    364 	struct dom_binding *ypb, *ypbp;
    365 
    366 	ypbp = NULL;
    367 	for(ypb=_ypbindlist; ypb; ypb=ypb->dom_pnext) {
    368 		if( strcmp(dom, ypb->dom_domain) == 0) {
    369 			clnt_destroy(ypb->dom_client);
    370 			if(ypbp)
    371 				ypbp->dom_pnext = ypb->dom_pnext;
    372 			else
    373 				_ypbindlist = ypb->dom_pnext;
    374 			free(ypb);
    375 			return;
    376 		}
    377 		ypbp = ypb;
    378 	}
    379 	return;
    380 }
    381 
    382 int
    383 yp_match(indomain, inmap, inkey, inkeylen, outval, outvallen)
    384 char *indomain;
    385 char *inmap;
    386 const char *inkey;
    387 int inkeylen;
    388 char **outval;
    389 int *outvallen;
    390 {
    391 	struct dom_binding *ysd;
    392 	struct ypresp_val yprv;
    393 	struct timeval tv;
    394 	struct ypreq_key yprk;
    395 	int r;
    396 
    397 	*outval = NULL;
    398 	*outvallen = 0;
    399 
    400 again:
    401 	if( _yp_dobind(indomain, &ysd) != 0)
    402 		return YPERR_DOMAIN;
    403 
    404 #ifdef YPMATCHCACHE
    405 	if( !strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
    406 	    inkeylen, &yprv.valdat.dptr, &yprv.valdat.dsize)) {
    407 		*outvallen = yprv.valdat.dsize;
    408 		*outval = (char *)malloc(*outvallen+1);
    409 		memcpy(*outval, yprv.valdat.dptr, *outvallen);
    410 		(*outval)[*outvallen] = '\0';
    411 		return 0;
    412 	}
    413 #endif
    414 
    415 	tv.tv_sec = _yplib_timeout;
    416 	tv.tv_usec = 0;
    417 
    418 	yprk.domain = indomain;
    419 	yprk.map = inmap;
    420 	yprk.keydat.dptr = (char *)inkey;
    421 	yprk.keydat.dsize = inkeylen;
    422 
    423 	memset(&yprv, 0, sizeof yprv);
    424 
    425 	r = clnt_call(ysd->dom_client, YPPROC_MATCH,
    426 		xdr_ypreq_key, &yprk, xdr_ypresp_val, &yprv, tv);
    427 	if(r != RPC_SUCCESS) {
    428 		clnt_perror(ysd->dom_client, "yp_match: clnt_call");
    429 		ysd->dom_vers = -1;
    430 		goto again;
    431 	}
    432 	if( !(r=ypprot_err(yprv.status)) ) {
    433 		*outvallen = yprv.valdat.dsize;
    434 		*outval = (char *)malloc(*outvallen+1);
    435 		memcpy(*outval, yprv.valdat.dptr, *outvallen);
    436 		(*outval)[*outvallen] = '\0';
    437 #ifdef YPMATCHCACHE
    438 		if( strcmp(_yp_domain, indomain)==0 )
    439 			 ypmatch_add(inmap, inkey, inkeylen, *outval, *outvallen);
    440 #endif
    441 	}
    442 	xdr_free(xdr_ypresp_val, (char *)&yprv);
    443 	_yp_unbind(ysd);
    444 	return r;
    445 }
    446 
    447 int
    448 yp_get_default_domain(domp)
    449 char **domp;
    450 {
    451 	*domp = NULL;
    452 	if(_yp_domain[0] == '\0')
    453 		if( getdomainname(_yp_domain, sizeof _yp_domain))
    454 			return YPERR_NODOM;
    455 	*domp = _yp_domain;
    456 	return 0;
    457 }
    458 
    459 int
    460 yp_first(indomain, inmap, outkey, outkeylen, outval, outvallen)
    461 char *indomain;
    462 char *inmap;
    463 char **outkey;
    464 int *outkeylen;
    465 char **outval;
    466 int *outvallen;
    467 {
    468 	struct ypresp_key_val yprkv;
    469 	struct ypreq_nokey yprnk;
    470 	struct dom_binding *ysd;
    471 	struct timeval tv;
    472 	int r;
    473 
    474 	*outkey = *outval = NULL;
    475 	*outkeylen = *outvallen = 0;
    476 
    477 again:
    478 	if( _yp_dobind(indomain, &ysd) != 0)
    479 		return YPERR_DOMAIN;
    480 
    481 	tv.tv_sec = _yplib_timeout;
    482 	tv.tv_usec = 0;
    483 
    484 	yprnk.domain = indomain;
    485 	yprnk.map = inmap;
    486 	memset(&yprkv, 0, sizeof yprkv);
    487 
    488 	r = clnt_call(ysd->dom_client, YPPROC_FIRST,
    489 		xdr_ypreq_nokey, &yprnk, xdr_ypresp_key_val, &yprkv, tv);
    490 	if(r != RPC_SUCCESS) {
    491 		clnt_perror(ysd->dom_client, "yp_first: clnt_call");
    492 		ysd->dom_vers = -1;
    493 		goto again;
    494 	}
    495 	if( !(r=ypprot_err(yprkv.status)) ) {
    496 		*outkeylen = yprkv.keydat.dsize;
    497 		*outkey = (char *)malloc(*outkeylen+1);
    498 		memcpy(*outkey, yprkv.keydat.dptr, *outkeylen);
    499 		(*outkey)[*outkeylen] = '\0';
    500 		*outvallen = yprkv.valdat.dsize;
    501 		*outval = (char *)malloc(*outvallen+1);
    502 		memcpy(*outval, yprkv.valdat.dptr, *outvallen);
    503 		(*outval)[*outvallen] = '\0';
    504 	}
    505 	xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
    506 	_yp_unbind(ysd);
    507 	return r;
    508 }
    509 
    510 int
    511 yp_next(indomain, inmap, inkey, inkeylen, outkey, outkeylen, outval, outvallen)
    512 char *indomain;
    513 char *inmap;
    514 char *inkey;
    515 int inkeylen;
    516 char **outkey;
    517 int *outkeylen;
    518 char **outval;
    519 int *outvallen;
    520 {
    521 	struct ypresp_key_val yprkv;
    522 	struct ypreq_key yprk;
    523 	struct dom_binding *ysd;
    524 	struct timeval tv;
    525 	int r;
    526 
    527 	*outkey = *outval = NULL;
    528 	*outkeylen = *outvallen = 0;
    529 
    530 again:
    531 	if( _yp_dobind(indomain, &ysd) != 0)
    532 		return YPERR_DOMAIN;
    533 
    534 	tv.tv_sec = _yplib_timeout;
    535 	tv.tv_usec = 0;
    536 
    537 	yprk.domain = indomain;
    538 	yprk.map = inmap;
    539 	yprk.keydat.dptr = inkey;
    540 	yprk.keydat.dsize = inkeylen;
    541 	memset(&yprkv, 0, sizeof yprkv);
    542 
    543 	r = clnt_call(ysd->dom_client, YPPROC_NEXT,
    544 		xdr_ypreq_key, &yprk, xdr_ypresp_key_val, &yprkv, tv);
    545 	if(r != RPC_SUCCESS) {
    546 		clnt_perror(ysd->dom_client, "yp_next: clnt_call");
    547 		ysd->dom_vers = -1;
    548 		goto again;
    549 	}
    550 	if( !(r=ypprot_err(yprkv.status)) ) {
    551 		*outkeylen = yprkv.keydat.dsize;
    552 		*outkey = (char *)malloc(*outkeylen+1);
    553 		memcpy(*outkey, yprkv.keydat.dptr, *outkeylen);
    554 		(*outkey)[*outkeylen] = '\0';
    555 		*outvallen = yprkv.valdat.dsize;
    556 		*outval = (char *)malloc(*outvallen+1);
    557 		memcpy(*outval, yprkv.valdat.dptr, *outvallen);
    558 		(*outval)[*outvallen] = '\0';
    559 	}
    560 	xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
    561 	_yp_unbind(ysd);
    562 	return r;
    563 }
    564 
    565 int
    566 yp_all(indomain, inmap, incallback)
    567 char *indomain;
    568 char *inmap;
    569 struct ypall_callback *incallback;
    570 {
    571 	struct ypreq_nokey yprnk;
    572 	struct dom_binding *ysd;
    573 	struct timeval tv;
    574 	struct sockaddr_in clnt_sin;
    575 	CLIENT *clnt;
    576 	u_long status;
    577 	int clnt_sock;
    578 
    579 	if( _yp_dobind(indomain, &ysd) != 0)
    580 		return YPERR_DOMAIN;
    581 
    582 	tv.tv_sec = _yplib_timeout;
    583 	tv.tv_usec = 0;
    584 	clnt_sock = RPC_ANYSOCK;
    585 	clnt_sin = ysd->dom_server_addr;
    586 	clnt_sin.sin_port = 0;
    587 	clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0);
    588 	if(clnt==NULL) {
    589 		printf("clnttcp_create failed\n");
    590 		return YPERR_PMAP;
    591 	}
    592 
    593 	yprnk.domain = indomain;
    594 	yprnk.map = inmap;
    595 	ypresp_allfn = incallback->foreach;
    596 	ypresp_data = (void *)incallback->data;
    597 
    598 	(void) clnt_call(clnt, YPPROC_ALL,
    599 		xdr_ypreq_nokey, &yprnk, xdr_ypresp_all_seq, &status, tv);
    600 	clnt_destroy(clnt);
    601 	xdr_free(xdr_ypresp_all_seq, (char *)&status);	/* not really needed... */
    602 	_yp_unbind(ysd);
    603 
    604 	if(status != YP_FALSE)
    605 		return ypprot_err(status);
    606 	return 0;
    607 }
    608 
    609 int
    610 yp_order(indomain, inmap, outorder)
    611 char *indomain;
    612 char *inmap;
    613 int *outorder;
    614 {
    615  	struct dom_binding *ysd;
    616 	struct ypresp_order ypro;
    617 	struct ypreq_nokey yprnk;
    618 	struct timeval tv;
    619 	int r;
    620 
    621 again:
    622 	if( _yp_dobind(indomain, &ysd) != 0)
    623 		return YPERR_DOMAIN;
    624 
    625 	tv.tv_sec = _yplib_timeout;
    626 	tv.tv_usec = 0;
    627 
    628 	yprnk.domain = indomain;
    629 	yprnk.map = inmap;
    630 
    631 	memset(&ypro, 0, sizeof ypro);
    632 
    633 	r = clnt_call(ysd->dom_client, YPPROC_ORDER,
    634 		xdr_ypreq_nokey, &yprnk, xdr_ypresp_order, &ypro, tv);
    635 	if(r != RPC_SUCCESS) {
    636 		clnt_perror(ysd->dom_client, "yp_order: clnt_call");
    637 		ysd->dom_vers = -1;
    638 		goto again;
    639 	}
    640 
    641 	*outorder = ypro.ordernum;
    642 	xdr_free(xdr_ypresp_order, (char *)&ypro);
    643 	_yp_unbind(ysd);
    644 	return ypprot_err(ypro.status);
    645 }
    646 
    647 int
    648 yp_master(indomain, inmap, outname)
    649 char *indomain;
    650 char *inmap;
    651 char **outname;
    652 {
    653 	struct dom_binding *ysd;
    654 	struct ypresp_master yprm;
    655 	struct ypreq_nokey yprnk;
    656 	struct timeval tv;
    657 	int r;
    658 
    659 again:
    660 	if( _yp_dobind(indomain, &ysd) != 0)
    661 		return YPERR_DOMAIN;
    662 
    663 	tv.tv_sec = _yplib_timeout;
    664 	tv.tv_usec = 0;
    665 
    666 	yprnk.domain = indomain;
    667 	yprnk.map = inmap;
    668 
    669 	memset(&yprm, 0, sizeof yprm);
    670 
    671 	r = clnt_call(ysd->dom_client, YPPROC_MASTER,
    672 		xdr_ypreq_nokey, &yprnk, xdr_ypresp_master, &yprm, tv);
    673 	if(r != RPC_SUCCESS) {
    674 		clnt_perror(ysd->dom_client, "yp_master: clnt_call");
    675 		ysd->dom_vers = -1;
    676 		goto again;
    677 	}
    678 	if( !(r=ypprot_err(yprm.status)) ) {
    679 		*outname = (char *)strdup(yprm.master);
    680 	}
    681 	xdr_free(xdr_ypresp_master, (char *)&yprm);
    682 	_yp_unbind(ysd);
    683 	return r;
    684 }
    685 
    686 yp_maplist(indomain, outmaplist)
    687 char *indomain;
    688 struct ypmaplist **outmaplist;
    689 {
    690 	struct dom_binding *ysd;
    691 	struct ypresp_maplist ypml;
    692 	struct timeval tv;
    693 	int r;
    694 
    695 again:
    696 	if( _yp_dobind(indomain, &ysd) != 0)
    697 		return YPERR_DOMAIN;
    698 
    699 	tv.tv_sec = _yplib_timeout;
    700 	tv.tv_usec = 0;
    701 
    702 	memset(&ypml, 0, sizeof ypml);
    703 
    704 	r = clnt_call(ysd->dom_client, YPPROC_MAPLIST,
    705 		xdr_domainname, indomain, xdr_ypresp_maplist, &ypml, tv);
    706 	if (r != RPC_SUCCESS) {
    707 		clnt_perror(ysd->dom_client, "yp_maplist: clnt_call");
    708 		ysd->dom_vers = -1;
    709 		goto again;
    710 	}
    711 	*outmaplist = ypml.list;
    712 	/* NO: xdr_free(xdr_ypresp_maplist, &ypml);*/
    713 	_yp_unbind(ysd);
    714 	return ypprot_err(ypml.status);
    715 }
    716 
    717 char *
    718 yperr_string(incode)
    719 int incode;
    720 {
    721 	static char err[80];
    722 
    723 	switch(incode) {
    724 	case 0:
    725 		return "Success";
    726 	case YPERR_BADARGS:
    727 		return "Request arguments bad";
    728 	case YPERR_RPC:
    729 		return "RPC failure";
    730 	case YPERR_DOMAIN:
    731 		return "Can't bind to server which serves this domain";
    732 	case YPERR_MAP:
    733 		return "No such map in server's domain";
    734 	case YPERR_KEY:
    735 		return "No such key in map";
    736 	case YPERR_YPERR:
    737 		return "YP server error";
    738 	case YPERR_RESRC:
    739 		return "Local resource allocation failure";
    740 	case YPERR_NOMORE:
    741 		return "No more records in map database";
    742 	case YPERR_PMAP:
    743 		return "Can't communicate with portmapper";
    744 	case YPERR_YPBIND:
    745 		return "Can't communicate with ypbind";
    746 	case YPERR_YPSERV:
    747 		return "Can't communicate with ypserv";
    748 	case YPERR_NODOM:
    749 		return "Local domain name not set";
    750 	case YPERR_BADDB:
    751 		return "Server data base is bad";
    752 	case YPERR_VERS:
    753 		return "YP server version mismatch - server can't supply service.";
    754 	case YPERR_ACCESS:
    755 		return "Access violation";
    756 	case YPERR_BUSY:
    757 		return "Database is busy";
    758 	}
    759 	sprintf(err, "YP unknown error %d\n", incode);
    760 	return err;
    761 }
    762 
    763 int
    764 ypprot_err(incode)
    765 unsigned int incode;
    766 {
    767 	switch(incode) {
    768 	case YP_TRUE:
    769 		return 0;
    770 	case YP_FALSE:
    771 		return YPERR_YPBIND;
    772 	case YP_NOMORE:
    773 		return YPERR_NOMORE;
    774 	case YP_NOMAP:
    775 		return YPERR_MAP;
    776 	case YP_NODOM:
    777 		return YPERR_NODOM;
    778 	case YP_NOKEY:
    779 		return YPERR_KEY;
    780 	case YP_BADOP:
    781 		return YPERR_YPERR;
    782 	case YP_BADDB:
    783 		return YPERR_BADDB;
    784 	case YP_YPERR:
    785 		return YPERR_YPERR;
    786 	case YP_BADARGS:
    787 		return YPERR_BADARGS;
    788 	case YP_VERS:
    789 		return YPERR_VERS;
    790 	}
    791 	return YPERR_YPERR;
    792 }
    793 
    794 int
    795 _yp_check(dom)
    796 char **dom;
    797 {
    798 	int use_yp = 0;
    799 	char *unused;
    800 
    801 	if( _yp_domain[0]=='\0' )
    802 		if( yp_get_default_domain(&unused) )
    803 			return 0;
    804 
    805 	if(dom)
    806 		*dom = _yp_domain;
    807 
    808 	if( yp_bind(_yp_domain)==0 )
    809 		return 1;
    810 	return 0;
    811 }
    812