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