Home | History | Annotate | Line # | Download | only in yp
yplib.c revision 1.11
      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.11 1994/09/18 02:56:00 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 <stdlib.h>
     44 #include <string.h>
     45 #include <unistd.h>
     46 #include <rpc/rpc.h>
     47 #include <rpc/xdr.h>
     48 #include <rpcsvc/yp_prot.h>
     49 #include <rpcsvc/ypclnt.h>
     50 
     51 #ifndef BINDINGDIR
     52 #define BINDINGDIR "/var/yp/binding"
     53 #endif
     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 
    188 	gpid = getpid();
    189 	if( !(pid==-1 || pid==gpid) ) {
    190 		ysd = _ypbindlist;
    191 		while(ysd) {
    192 			if(ysd->dom_client)
    193 				clnt_destroy(ysd->dom_client);
    194 			ysd2 = ysd->dom_pnext;
    195 			free(ysd);
    196 			ysd = ysd2;
    197 		}
    198 		_ypbindlist = NULL;
    199 	}
    200 	pid = gpid;
    201 
    202 	if(ypdb!=NULL)
    203 		*ypdb = NULL;
    204 
    205 	if(dom==NULL || strlen(dom)==0)
    206 		return YPERR_BADARGS;
    207 
    208 	for(ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
    209 		if( strcmp(dom, ysd->dom_domain) == 0)
    210 			break;
    211 	if(ysd==NULL) {
    212 		ysd = (struct dom_binding *)malloc(sizeof *ysd);
    213 		memset(ysd, 0, sizeof *ysd);
    214 		ysd->dom_socket = -1;
    215 		ysd->dom_vers = 0;
    216 		new = 1;
    217 	}
    218 again:
    219 #ifdef BINDINGDIR
    220 	if(ysd->dom_vers==0) {
    221 		sprintf(path, "%s/%s.%d", BINDINGDIR, dom, 2);
    222 		if( (fd=open(path, O_RDONLY)) == -1) {
    223 			/* no binding file, YP is dead, or not yet fully alive. */
    224 			goto trynet;
    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 			memset(&ysd->dom_server_addr, 0, 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 trynet:
    263 #endif
    264 	if(ysd->dom_vers==-1 || ysd->dom_vers==0) {
    265 		memset(&clnt_sin, 0, sizeof clnt_sin);
    266 		clnt_sin.sin_family = AF_INET;
    267 		clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    268 
    269 		clnt_sock = RPC_ANYSOCK;
    270 		client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, &clnt_sock,
    271 			0, 0);
    272 		if(client==NULL) {
    273 			clnt_pcreateerror("clnttcp_create");
    274 			if(new)
    275 				free(ysd);
    276 			return YPERR_YPBIND;
    277 		}
    278 
    279 		tv.tv_sec = _yplib_timeout;
    280 		tv.tv_usec = 0;
    281 		r = clnt_call(client, YPBINDPROC_DOMAIN,
    282 			xdr_domainname, dom, xdr_ypbind_resp, &ypbr, tv);
    283 		if(r != RPC_SUCCESS) {
    284 			fprintf(stderr,
    285 			"YP: server for domain %s not responding, still trying\n", dom);
    286 			clnt_destroy(client);
    287 			ysd->dom_vers = -1;
    288 			goto again;
    289 		}
    290 		clnt_destroy(client);
    291 
    292 		memset(&ysd->dom_server_addr, 0, sizeof ysd->dom_server_addr);
    293 		ysd->dom_server_addr.sin_family = AF_INET;
    294 		ysd->dom_server_addr.sin_port =
    295 			ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port;
    296 		ysd->dom_server_addr.sin_addr.s_addr =
    297 			ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr;
    298 		ysd->dom_server_port =
    299 			ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port;
    300 gotit:
    301 		ysd->dom_vers = YPVERS;
    302 		strcpy(ysd->dom_domain, dom);
    303 	}
    304 
    305 	tv.tv_sec = _yplib_timeout/2;
    306 	tv.tv_usec = 0;
    307 	if(ysd->dom_client)
    308 		clnt_destroy(ysd->dom_client);
    309 	ysd->dom_socket = RPC_ANYSOCK;
    310 	ysd->dom_client = clntudp_create(&ysd->dom_server_addr,
    311 		YPPROG, YPVERS, tv, &ysd->dom_socket);
    312 	if(ysd->dom_client==NULL) {
    313 		clnt_pcreateerror("clntudp_create");
    314 		ysd->dom_vers = -1;
    315 		goto again;
    316 	}
    317 	if( fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
    318 		perror("fcntl: F_SETFD");
    319 
    320 	if(new) {
    321 		ysd->dom_pnext = _ypbindlist;
    322 		_ypbindlist = ysd;
    323 	}
    324 
    325 	if(ypdb!=NULL)
    326 		*ypdb = ysd;
    327 	return 0;
    328 }
    329 
    330 static void
    331 _yp_unbind(ypb)
    332 struct dom_binding *ypb;
    333 {
    334 	clnt_destroy(ypb->dom_client);
    335 	ypb->dom_client = NULL;
    336 	ypb->dom_socket = -1;
    337 }
    338 
    339 int
    340 yp_bind(dom)
    341 char *dom;
    342 {
    343 	return _yp_dobind(dom, NULL);
    344 }
    345 
    346 void
    347 yp_unbind(dom)
    348 char *dom;
    349 {
    350 	struct dom_binding *ypb, *ypbp;
    351 
    352 	ypbp = NULL;
    353 	for(ypb=_ypbindlist; ypb; ypb=ypb->dom_pnext) {
    354 		if( strcmp(dom, ypb->dom_domain) == 0) {
    355 			clnt_destroy(ypb->dom_client);
    356 			if(ypbp)
    357 				ypbp->dom_pnext = ypb->dom_pnext;
    358 			else
    359 				_ypbindlist = ypb->dom_pnext;
    360 			free(ypb);
    361 			return;
    362 		}
    363 		ypbp = ypb;
    364 	}
    365 	return;
    366 }
    367 
    368 int
    369 yp_match(indomain, inmap, inkey, inkeylen, outval, outvallen)
    370 char *indomain;
    371 char *inmap;
    372 const char *inkey;
    373 int inkeylen;
    374 char **outval;
    375 int *outvallen;
    376 {
    377 	struct dom_binding *ysd;
    378 	struct ypresp_val yprv;
    379 	struct timeval tv;
    380 	struct ypreq_key yprk;
    381 	int r;
    382 
    383 	*outval = NULL;
    384 	*outvallen = 0;
    385 
    386 again:
    387 	if( _yp_dobind(indomain, &ysd) != 0)
    388 		return YPERR_DOMAIN;
    389 
    390 #ifdef YPMATCHCACHE
    391 	if( !strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
    392 	    inkeylen, &yprv.valdat.dptr, &yprv.valdat.dsize)) {
    393 		*outvallen = yprv.valdat.dsize;
    394 		*outval = (char *)malloc(*outvallen+1);
    395 		memcpy(*outval, yprv.valdat.dptr, *outvallen);
    396 		(*outval)[*outvallen] = '\0';
    397 		return 0;
    398 	}
    399 #endif
    400 
    401 	tv.tv_sec = _yplib_timeout;
    402 	tv.tv_usec = 0;
    403 
    404 	yprk.domain = indomain;
    405 	yprk.map = inmap;
    406 	yprk.keydat.dptr = (char *)inkey;
    407 	yprk.keydat.dsize = inkeylen;
    408 
    409 	memset(&yprv, 0, sizeof yprv);
    410 
    411 	r = clnt_call(ysd->dom_client, YPPROC_MATCH,
    412 		xdr_ypreq_key, &yprk, xdr_ypresp_val, &yprv, tv);
    413 	if(r != RPC_SUCCESS) {
    414 		clnt_perror(ysd->dom_client, "yp_match: clnt_call");
    415 		ysd->dom_vers = -1;
    416 		goto again;
    417 	}
    418 	if( !(r=ypprot_err(yprv.status)) ) {
    419 		*outvallen = yprv.valdat.dsize;
    420 		*outval = (char *)malloc(*outvallen+1);
    421 		memcpy(*outval, yprv.valdat.dptr, *outvallen);
    422 		(*outval)[*outvallen] = '\0';
    423 #ifdef YPMATCHCACHE
    424 		if( strcmp(_yp_domain, indomain)==0 )
    425 			 ypmatch_add(inmap, inkey, inkeylen, *outval, *outvallen);
    426 #endif
    427 	}
    428 	xdr_free(xdr_ypresp_val, (char *)&yprv);
    429 	_yp_unbind(ysd);
    430 	return r;
    431 }
    432 
    433 int
    434 yp_get_default_domain(domp)
    435 char **domp;
    436 {
    437 	*domp = NULL;
    438 	if(_yp_domain[0] == '\0')
    439 		if( getdomainname(_yp_domain, sizeof _yp_domain))
    440 			return YPERR_NODOM;
    441 	*domp = _yp_domain;
    442 	return 0;
    443 }
    444 
    445 int
    446 yp_first(indomain, inmap, outkey, outkeylen, outval, outvallen)
    447 char *indomain;
    448 char *inmap;
    449 char **outkey;
    450 int *outkeylen;
    451 char **outval;
    452 int *outvallen;
    453 {
    454 	struct ypresp_key_val yprkv;
    455 	struct ypreq_nokey yprnk;
    456 	struct dom_binding *ysd;
    457 	struct timeval tv;
    458 	int r;
    459 
    460 	*outkey = *outval = NULL;
    461 	*outkeylen = *outvallen = 0;
    462 
    463 again:
    464 	if( _yp_dobind(indomain, &ysd) != 0)
    465 		return YPERR_DOMAIN;
    466 
    467 	tv.tv_sec = _yplib_timeout;
    468 	tv.tv_usec = 0;
    469 
    470 	yprnk.domain = indomain;
    471 	yprnk.map = inmap;
    472 	memset(&yprkv, 0, sizeof yprkv);
    473 
    474 	r = clnt_call(ysd->dom_client, YPPROC_FIRST,
    475 		xdr_ypreq_nokey, &yprnk, xdr_ypresp_key_val, &yprkv, tv);
    476 	if(r != RPC_SUCCESS) {
    477 		clnt_perror(ysd->dom_client, "yp_first: clnt_call");
    478 		ysd->dom_vers = -1;
    479 		goto again;
    480 	}
    481 	if( !(r=ypprot_err(yprkv.status)) ) {
    482 		*outkeylen = yprkv.keydat.dsize;
    483 		*outkey = (char *)malloc(*outkeylen+1);
    484 		memcpy(*outkey, yprkv.keydat.dptr, *outkeylen);
    485 		(*outkey)[*outkeylen] = '\0';
    486 		*outvallen = yprkv.valdat.dsize;
    487 		*outval = (char *)malloc(*outvallen+1);
    488 		memcpy(*outval, yprkv.valdat.dptr, *outvallen);
    489 		(*outval)[*outvallen] = '\0';
    490 	}
    491 	xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
    492 	_yp_unbind(ysd);
    493 	return r;
    494 }
    495 
    496 int
    497 yp_next(indomain, inmap, inkey, inkeylen, outkey, outkeylen, outval, outvallen)
    498 char *indomain;
    499 char *inmap;
    500 char *inkey;
    501 int inkeylen;
    502 char **outkey;
    503 int *outkeylen;
    504 char **outval;
    505 int *outvallen;
    506 {
    507 	struct ypresp_key_val yprkv;
    508 	struct ypreq_key yprk;
    509 	struct dom_binding *ysd;
    510 	struct timeval tv;
    511 	int r;
    512 
    513 	*outkey = *outval = NULL;
    514 	*outkeylen = *outvallen = 0;
    515 
    516 again:
    517 	if( _yp_dobind(indomain, &ysd) != 0)
    518 		return YPERR_DOMAIN;
    519 
    520 	tv.tv_sec = _yplib_timeout;
    521 	tv.tv_usec = 0;
    522 
    523 	yprk.domain = indomain;
    524 	yprk.map = inmap;
    525 	yprk.keydat.dptr = inkey;
    526 	yprk.keydat.dsize = inkeylen;
    527 	memset(&yprkv, 0, sizeof yprkv);
    528 
    529 	r = clnt_call(ysd->dom_client, YPPROC_NEXT,
    530 		xdr_ypreq_key, &yprk, xdr_ypresp_key_val, &yprkv, tv);
    531 	if(r != RPC_SUCCESS) {
    532 		clnt_perror(ysd->dom_client, "yp_next: clnt_call");
    533 		ysd->dom_vers = -1;
    534 		goto again;
    535 	}
    536 	if( !(r=ypprot_err(yprkv.status)) ) {
    537 		*outkeylen = yprkv.keydat.dsize;
    538 		*outkey = (char *)malloc(*outkeylen+1);
    539 		memcpy(*outkey, yprkv.keydat.dptr, *outkeylen);
    540 		(*outkey)[*outkeylen] = '\0';
    541 		*outvallen = yprkv.valdat.dsize;
    542 		*outval = (char *)malloc(*outvallen+1);
    543 		memcpy(*outval, yprkv.valdat.dptr, *outvallen);
    544 		(*outval)[*outvallen] = '\0';
    545 	}
    546 	xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
    547 	_yp_unbind(ysd);
    548 	return r;
    549 }
    550 
    551 int
    552 yp_all(indomain, inmap, incallback)
    553 char *indomain;
    554 char *inmap;
    555 struct ypall_callback *incallback;
    556 {
    557 	struct ypreq_nokey yprnk;
    558 	struct dom_binding *ysd;
    559 	struct timeval tv;
    560 	struct sockaddr_in clnt_sin;
    561 	CLIENT *clnt;
    562 	u_long status;
    563 	int clnt_sock;
    564 
    565 	if( _yp_dobind(indomain, &ysd) != 0)
    566 		return YPERR_DOMAIN;
    567 
    568 	tv.tv_sec = _yplib_timeout;
    569 	tv.tv_usec = 0;
    570 	clnt_sock = RPC_ANYSOCK;
    571 	clnt_sin = ysd->dom_server_addr;
    572 	clnt_sin.sin_port = 0;
    573 	clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0);
    574 	if(clnt==NULL) {
    575 		printf("clnttcp_create failed\n");
    576 		return YPERR_PMAP;
    577 	}
    578 
    579 	yprnk.domain = indomain;
    580 	yprnk.map = inmap;
    581 	ypresp_allfn = incallback->foreach;
    582 	ypresp_data = (void *)incallback->data;
    583 
    584 	(void) clnt_call(clnt, YPPROC_ALL,
    585 		xdr_ypreq_nokey, &yprnk, xdr_ypresp_all_seq, &status, tv);
    586 	clnt_destroy(clnt);
    587 	xdr_free(xdr_ypresp_all_seq, (char *)&status);	/* not really needed... */
    588 	_yp_unbind(ysd);
    589 
    590 	if(status != YP_FALSE)
    591 		return ypprot_err(status);
    592 	return 0;
    593 }
    594 
    595 int
    596 yp_order(indomain, inmap, outorder)
    597 char *indomain;
    598 char *inmap;
    599 int *outorder;
    600 {
    601  	struct dom_binding *ysd;
    602 	struct ypresp_order ypro;
    603 	struct ypreq_nokey yprnk;
    604 	struct timeval tv;
    605 	int r;
    606 
    607 again:
    608 	if( _yp_dobind(indomain, &ysd) != 0)
    609 		return YPERR_DOMAIN;
    610 
    611 	tv.tv_sec = _yplib_timeout;
    612 	tv.tv_usec = 0;
    613 
    614 	yprnk.domain = indomain;
    615 	yprnk.map = inmap;
    616 
    617 	memset(&ypro, 0, sizeof ypro);
    618 
    619 	r = clnt_call(ysd->dom_client, YPPROC_ORDER,
    620 		xdr_ypreq_nokey, &yprnk, xdr_ypresp_order, &ypro, tv);
    621 	if(r != RPC_SUCCESS) {
    622 		clnt_perror(ysd->dom_client, "yp_order: clnt_call");
    623 		ysd->dom_vers = -1;
    624 		goto again;
    625 	}
    626 
    627 	*outorder = ypro.ordernum;
    628 	xdr_free(xdr_ypresp_order, (char *)&ypro);
    629 	_yp_unbind(ysd);
    630 	return ypprot_err(ypro.status);
    631 }
    632 
    633 int
    634 yp_master(indomain, inmap, outname)
    635 char *indomain;
    636 char *inmap;
    637 char **outname;
    638 {
    639 	struct dom_binding *ysd;
    640 	struct ypresp_master yprm;
    641 	struct ypreq_nokey yprnk;
    642 	struct timeval tv;
    643 	int r;
    644 
    645 again:
    646 	if( _yp_dobind(indomain, &ysd) != 0)
    647 		return YPERR_DOMAIN;
    648 
    649 	tv.tv_sec = _yplib_timeout;
    650 	tv.tv_usec = 0;
    651 
    652 	yprnk.domain = indomain;
    653 	yprnk.map = inmap;
    654 
    655 	memset(&yprm, 0, sizeof yprm);
    656 
    657 	r = clnt_call(ysd->dom_client, YPPROC_MASTER,
    658 		xdr_ypreq_nokey, &yprnk, xdr_ypresp_master, &yprm, tv);
    659 	if(r != RPC_SUCCESS) {
    660 		clnt_perror(ysd->dom_client, "yp_master: clnt_call");
    661 		ysd->dom_vers = -1;
    662 		goto again;
    663 	}
    664 	if( !(r=ypprot_err(yprm.status)) ) {
    665 		*outname = (char *)strdup(yprm.master);
    666 	}
    667 	xdr_free(xdr_ypresp_master, (char *)&yprm);
    668 	_yp_unbind(ysd);
    669 	return r;
    670 }
    671 
    672 yp_maplist(indomain, outmaplist)
    673 char *indomain;
    674 struct ypmaplist **outmaplist;
    675 {
    676 	struct dom_binding *ysd;
    677 	struct ypresp_maplist ypml;
    678 	struct timeval tv;
    679 	int r;
    680 
    681 again:
    682 	if( _yp_dobind(indomain, &ysd) != 0)
    683 		return YPERR_DOMAIN;
    684 
    685 	tv.tv_sec = _yplib_timeout;
    686 	tv.tv_usec = 0;
    687 
    688 	memset(&ypml, 0, sizeof ypml);
    689 
    690 	r = clnt_call(ysd->dom_client, YPPROC_MAPLIST,
    691 		xdr_domainname, indomain, xdr_ypresp_maplist, &ypml, tv);
    692 	if (r != RPC_SUCCESS) {
    693 		clnt_perror(ysd->dom_client, "yp_maplist: clnt_call");
    694 		ysd->dom_vers = -1;
    695 		goto again;
    696 	}
    697 	*outmaplist = ypml.list;
    698 	/* NO: xdr_free(xdr_ypresp_maplist, &ypml);*/
    699 	_yp_unbind(ysd);
    700 	return ypprot_err(ypml.status);
    701 }
    702 
    703 char *
    704 yperr_string(incode)
    705 int incode;
    706 {
    707 	static char err[80];
    708 
    709 	switch(incode) {
    710 	case 0:
    711 		return "Success";
    712 	case YPERR_BADARGS:
    713 		return "Request arguments bad";
    714 	case YPERR_RPC:
    715 		return "RPC failure";
    716 	case YPERR_DOMAIN:
    717 		return "Can't bind to server which serves this domain";
    718 	case YPERR_MAP:
    719 		return "No such map in server's domain";
    720 	case YPERR_KEY:
    721 		return "No such key in map";
    722 	case YPERR_YPERR:
    723 		return "YP server error";
    724 	case YPERR_RESRC:
    725 		return "Local resource allocation failure";
    726 	case YPERR_NOMORE:
    727 		return "No more records in map database";
    728 	case YPERR_PMAP:
    729 		return "Can't communicate with portmapper";
    730 	case YPERR_YPBIND:
    731 		return "Can't communicate with ypbind";
    732 	case YPERR_YPSERV:
    733 		return "Can't communicate with ypserv";
    734 	case YPERR_NODOM:
    735 		return "Local domain name not set";
    736 	case YPERR_BADDB:
    737 		return "Server data base is bad";
    738 	case YPERR_VERS:
    739 		return "YP server version mismatch - server can't supply service.";
    740 	case YPERR_ACCESS:
    741 		return "Access violation";
    742 	case YPERR_BUSY:
    743 		return "Database is busy";
    744 	}
    745 	sprintf(err, "YP unknown error %d\n", incode);
    746 	return err;
    747 }
    748 
    749 int
    750 ypprot_err(incode)
    751 unsigned int incode;
    752 {
    753 	switch(incode) {
    754 	case YP_TRUE:
    755 		return 0;
    756 	case YP_FALSE:
    757 		return YPERR_YPBIND;
    758 	case YP_NOMORE:
    759 		return YPERR_NOMORE;
    760 	case YP_NOMAP:
    761 		return YPERR_MAP;
    762 	case YP_NODOM:
    763 		return YPERR_NODOM;
    764 	case YP_NOKEY:
    765 		return YPERR_KEY;
    766 	case YP_BADOP:
    767 		return YPERR_YPERR;
    768 	case YP_BADDB:
    769 		return YPERR_BADDB;
    770 	case YP_YPERR:
    771 		return YPERR_YPERR;
    772 	case YP_BADARGS:
    773 		return YPERR_BADARGS;
    774 	case YP_VERS:
    775 		return YPERR_VERS;
    776 	}
    777 	return YPERR_YPERR;
    778 }
    779 
    780 int
    781 _yp_check(dom)
    782 char **dom;
    783 {
    784 	int use_yp = 0;
    785 	char *unused;
    786 
    787 	if( _yp_domain[0]=='\0' )
    788 		if( yp_get_default_domain(&unused) )
    789 			return 0;
    790 
    791 	if(dom)
    792 		*dom = _yp_domain;
    793 
    794 	if( yp_bind(_yp_domain)==0 )
    795 		return 1;
    796 	return 0;
    797 }
    798