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