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