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