xref: /freebsd/lib/libc/yp/yplib.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
1 /*
2  * Copyright (c) 1992/3 Theo de Raadt <deraadt@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. The name of the author may not be used to endorse or promote
14  *    products derived from this software without specific prior written
15  *    permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef LINT
31 static char *rcsid = "$Id: yplib.c,v 1.22 1996/06/04 17:35:15 jraynard Exp $";
32 #endif
33 
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/file.h>
38 #include <sys/uio.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <rpc/rpc.h>
45 #include <rpc/xdr.h>
46 #include <rpcsvc/yp.h>
47 
48 
49 /*
50  * We have to define these here due to clashes between yp_prot.h and
51  * yp.h.
52  */
53 
54 struct dom_binding {
55 	struct dom_binding *dom_pnext;
56 	char dom_domain[YPMAXDOMAIN + 1];
57 	struct sockaddr_in dom_server_addr;
58 	u_short dom_server_port;
59 	int dom_socket;
60 	CLIENT *dom_client;
61 	u_short dom_local_port;	/* now I finally know what this is for. */
62 	long dom_vers;
63 };
64 
65 #include <rpcsvc/ypclnt.h>
66 
67 #ifndef BINDINGDIR
68 #define BINDINGDIR "/var/yp/binding"
69 #endif
70 #define YPMATCHCACHE
71 #define MAX_RETRIES 20
72 
73 extern bool_t xdr_domainname(), xdr_ypbind_resp();
74 extern bool_t xdr_ypreq_key(), xdr_ypresp_val();
75 extern bool_t xdr_ypreq_nokey(), xdr_ypresp_key_val();
76 extern bool_t xdr_ypresp_all(), xdr_ypresp_all_seq();
77 extern bool_t xdr_ypresp_master();
78 
79 int (*ypresp_allfn)();
80 void *ypresp_data;
81 
82 struct dom_binding *_ypbindlist;
83 static char _yp_domain[MAXHOSTNAMELEN];
84 int _yplib_timeout = 10;
85 
86 #ifdef YPMATCHCACHE
87 int _yplib_cache = 5;
88 
89 static struct ypmatch_ent {
90 	struct ypmatch_ent *next;
91 	char *map, *key, *val;
92 	int keylen, vallen;
93 	time_t expire_t;
94 } *ypmc;
95 
96 static void
97 ypmatch_add(map, key, keylen, val, vallen)
98 	char *map;
99 	char *key;
100 	int keylen;
101 	char *val;
102 	int vallen;
103 {
104 	struct ypmatch_ent *ep;
105 	time_t t;
106 
107 	time(&t);
108 
109 	for(ep=ypmc; ep; ep=ep->next)
110 		if(ep->expire_t < t)
111 			break;
112 	if(ep==NULL) {
113 		ep = (struct ypmatch_ent *)malloc(sizeof *ep);
114 		bzero((char *)ep, sizeof *ep);
115 		if(ypmc)
116 			ep->next = ypmc;
117 		ypmc = ep;
118 	}
119 
120 	if(ep->key)
121 		free(ep->key);
122 	if(ep->val)
123 		free(ep->val);
124 
125 	ep->key = NULL;
126 	ep->val = NULL;
127 
128 	ep->key = (char *)malloc(keylen);
129 	if(ep->key==NULL)
130 		return;
131 
132 	ep->val = (char *)malloc(vallen);
133 	if(ep->key==NULL) {
134 		free(ep->key);
135 		ep->key = NULL;
136 		return;
137 	}
138 	ep->keylen = keylen;
139 	ep->vallen = vallen;
140 
141 	bcopy(key, ep->key, ep->keylen);
142 	bcopy(val, ep->val, ep->vallen);
143 
144 	if(ep->map) {
145 		if( strcmp(ep->map, map) ) {
146 			free(ep->map);
147 			ep->map = strdup(map);
148 		}
149 	} else {
150 		ep->map = strdup(map);
151 	}
152 
153 	ep->expire_t = t + _yplib_cache;
154 }
155 
156 static bool_t
157 ypmatch_find(map, key, keylen, val, vallen)
158 	char *map;
159 	char *key;
160 	int keylen;
161 	char **val;
162 	int *vallen;
163 {
164 	struct ypmatch_ent *ep;
165 	time_t t;
166 
167 	if(ypmc==NULL)
168 		return 0;
169 
170 	time(&t);
171 
172 	for(ep=ypmc; ep; ep=ep->next) {
173 		if(ep->keylen != keylen)
174 			continue;
175 		if(strcmp(ep->map, map))
176 			continue;
177 		if(bcmp(ep->key, key, keylen))
178 			continue;
179 		if(t > ep->expire_t)
180 			continue;
181 
182 		*val = ep->val;
183 		*vallen = ep->vallen;
184 		return 1;
185 	}
186 	return 0;
187 }
188 #endif
189 
190 char *
191 ypbinderr_string(incode)
192 	int incode;
193 {
194 	static char err[80];
195 	switch(incode) {
196 	case 0:
197 		return "Success";
198 	case YPBIND_ERR_ERR:
199 		return "Internal ypbind error";
200 	case YPBIND_ERR_NOSERV:
201 		return "Domain not bound";
202 	case YPBIND_ERR_RESC:
203 		return "System resource allocation failure";
204 	}
205 	sprintf(err, "Unknown ypbind error: #%d\n", incode);
206 	return err;
207 }
208 
209 int
210 _yp_dobind(dom, ypdb)
211 	char *dom;
212 	struct dom_binding **ypdb;
213 {
214 	static pid_t pid = -1;
215 	char path[MAXPATHLEN];
216 	struct dom_binding *ysd, *ysd2;
217 	struct ypbind_resp ypbr;
218 	struct timeval tv;
219 	struct sockaddr_in clnt_sin;
220 	int clnt_sock, fd;
221 	pid_t gpid;
222 	CLIENT *client;
223 	int new = 0, r;
224 	int retries = 0;
225 	struct sockaddr_in check;
226 	int checklen = sizeof(struct sockaddr_in);
227 
228 	gpid = getpid();
229 	if( !(pid==-1 || pid==gpid) ) {
230 		ysd = _ypbindlist;
231 		while(ysd) {
232 			if(ysd->dom_client)
233 				clnt_destroy(ysd->dom_client);
234 			ysd2 = ysd->dom_pnext;
235 			free(ysd);
236 			ysd = ysd2;
237 		}
238 		_ypbindlist = NULL;
239 	}
240 	pid = gpid;
241 
242 	if(ypdb!=NULL)
243 		*ypdb = NULL;
244 
245 	if(dom==NULL || strlen(dom)==0)
246 		return YPERR_BADARGS;
247 
248 	for(ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
249 		if( strcmp(dom, ysd->dom_domain) == 0)
250 			break;
251 
252 
253 	if(ysd==NULL) {
254 		ysd = (struct dom_binding *)malloc(sizeof *ysd);
255 		bzero((char *)ysd, sizeof *ysd);
256 		ysd->dom_socket = -1;
257 		ysd->dom_vers = 0;
258 		new = 1;
259 	} else {
260 	/* Check the socket -- may have been hosed by the caller. */
261 		if (getsockname(ysd->dom_socket, (struct sockaddr *)&check,
262 		    &checklen) == -1 || check.sin_family != AF_INET ||
263 		    check.sin_port != ysd->dom_local_port) {
264 		/* Socket became bogus somehow... need to rebind. */
265 			int save, sock;
266 
267 			sock = ysd->dom_socket;
268 			save = dup(ysd->dom_socket);
269 			clnt_destroy(ysd->dom_client);
270 			ysd->dom_vers = 0;
271 			ysd->dom_client = NULL;
272 			sock = dup2(save, sock);
273 			close(save);
274 		}
275 	}
276 
277 again:
278 	retries++;
279 	if (retries > MAX_RETRIES) {
280 		if (new)
281 			free(ysd);
282 		return(YPERR_YPBIND);
283 	}
284 #ifdef BINDINGDIR
285 	if(ysd->dom_vers==0) {
286 		/*
287 		 * We're trying to make a new binding: zorch the
288 		 * existing handle now (if any).
289 		 */
290 		if(ysd->dom_client) {
291 			clnt_destroy(ysd->dom_client);
292 			ysd->dom_client = NULL;
293 			ysd->dom_socket = -1;
294 		}
295 		sprintf(path, "%s/%s.%d", BINDINGDIR, dom, 2);
296 		if( (fd=open(path, O_RDONLY)) == -1) {
297 			/* no binding file, YP is dead. */
298 			/* Try to bring it back to life. */
299 			close(fd);
300 			goto skipit;
301 		}
302 		if( flock(fd, LOCK_EX|LOCK_NB) == -1 && errno==EWOULDBLOCK) {
303 			struct iovec iov[2];
304 			struct ypbind_resp ybr;
305 			u_short	ypb_port;
306 
307 			iov[0].iov_base = (caddr_t)&ypb_port;
308 			iov[0].iov_len = sizeof ypb_port;
309 			iov[1].iov_base = (caddr_t)&ybr;
310 			iov[1].iov_len = sizeof ybr;
311 
312 			r = readv(fd, iov, 2);
313 			if(r != iov[0].iov_len + iov[1].iov_len) {
314 				close(fd);
315 				ysd->dom_vers = -1;
316 				goto again;
317 			}
318 
319 			bzero(&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
320 			ysd->dom_server_addr.sin_family = AF_INET;
321 			ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
322 			ysd->dom_server_addr.sin_addr.s_addr =
323 			    *(u_long *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr;
324 			ysd->dom_server_addr.sin_port =
325 			    *(u_short *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
326 
327 			ysd->dom_server_port = ysd->dom_server_addr.sin_port;
328 			close(fd);
329 			goto gotit;
330 		} else {
331 			/* no lock on binding file, YP is dead. */
332 			/* Try to bring it back to life. */
333 			close(fd);
334 			goto skipit;
335 		}
336 	}
337 skipit:
338 #endif
339 	if(ysd->dom_vers==-1 || ysd->dom_vers==0) {
340 		/*
341 		 * We're trying to make a new binding: zorch the
342 		 * existing handle now (if any).
343 		 */
344 		if(ysd->dom_client) {
345 			clnt_destroy(ysd->dom_client);
346 			ysd->dom_client = NULL;
347 			ysd->dom_socket = -1;
348 		}
349 		bzero((char *)&clnt_sin, sizeof clnt_sin);
350 		clnt_sin.sin_family = AF_INET;
351 		clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
352 
353 		clnt_sock = RPC_ANYSOCK;
354 		client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, &clnt_sock,
355 			0, 0);
356 		if(client==NULL) {
357 			/*
358 			 * These conditions indicate ypbind just isn't
359 			 * alive -- we probably don't want to shoot our
360 			 * mouth off in this case; instead generate error
361 			 * messages only for really exotic problems.
362 			 */
363 			if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
364 			   (rpc_createerr.cf_stat != RPC_SYSTEMERROR &&
365 			   rpc_createerr.cf_error.re_errno == ECONNREFUSED))
366 				clnt_pcreateerror("clnttcp_create");
367 			if(new)
368 				free(ysd);
369 			return (YPERR_YPBIND);
370 		}
371 
372 		/*
373 		 * Check the port number -- should be < IPPORT_RESERVED.
374 		 * If not, it's possible someone has registered a bogus
375 		 * ypbind with the portmapper and is trying to trick us.
376 		 */
377 		if (ntohs(clnt_sin.sin_port) >= IPPORT_RESERVED) {
378 			clnt_destroy(client);
379 			if (new)
380 				free(ysd);
381 			return(YPERR_YPBIND);
382 		}
383 		tv.tv_sec = _yplib_timeout/2;
384 		tv.tv_usec = 0;
385 		r = clnt_call(client, YPBINDPROC_DOMAIN,
386 			xdr_domainname, (char *)&dom, xdr_ypbind_resp, &ypbr, tv);
387 		if(r != RPC_SUCCESS) {
388 			clnt_destroy(client);
389 			ysd->dom_vers = -1;
390 			if (r == RPC_PROGUNAVAIL || r == RPC_PROCUNAVAIL) {
391 				if (new)
392 					free(ysd);
393 				return(YPERR_YPBIND);
394 			}
395 			fprintf(stderr,
396 			"YP: server for domain %s not responding, retrying\n", dom);
397 			goto again;
398 		} else {
399 			if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
400 				clnt_destroy(client);
401 				ysd->dom_vers = -1;
402 				sleep(_yplib_timeout/2);
403 				goto again;
404 			}
405 		}
406 		clnt_destroy(client);
407 
408 		bzero((char *)&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
409 		ysd->dom_server_addr.sin_family = AF_INET;
410 		ysd->dom_server_addr.sin_port =
411 			*(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
412 		ysd->dom_server_addr.sin_addr.s_addr =
413 			*(u_long *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr;
414 
415 		/*
416 		 * We could do a reserved port check here too, but this
417 		 * could pose compatibility problems. The local ypbind is
418 		 * supposed to decide whether or not to trust yp servers
419 		 * on insecure ports. For now, we trust its judgement.
420 		 */
421 		ysd->dom_server_port =
422 			*(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
423 gotit:
424 		ysd->dom_vers = YPVERS;
425 		strcpy(ysd->dom_domain, dom);
426 	}
427 
428 	/* Don't rebuild the connection to the server unless we have to. */
429 	if (ysd->dom_client == NULL) {
430 		tv.tv_sec = _yplib_timeout/2;
431 		tv.tv_usec = 0;
432 		ysd->dom_socket = RPC_ANYSOCK;
433 		ysd->dom_client = clntudp_create(&ysd->dom_server_addr,
434 			YPPROG, YPVERS, tv, &ysd->dom_socket);
435 		if(ysd->dom_client==NULL) {
436 			clnt_pcreateerror("clntudp_create");
437 			ysd->dom_vers = -1;
438 			goto again;
439 		}
440 		if( fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
441 			perror("fcntl: F_SETFD");
442 		/*
443 		 * We want a port number associated with this socket
444 		 * so that we can check its authenticity later.
445 		 */
446 		checklen = sizeof(struct sockaddr_in);
447 		bzero((char *)&check, checklen);
448 		bind(ysd->dom_socket, (struct sockaddr *)&check, checklen);
449 		check.sin_family = AF_INET;
450 		if (!getsockname(ysd->dom_socket,
451 		    (struct sockaddr *)&check, &checklen)) {
452 			ysd->dom_local_port = check.sin_port;
453 		} else {
454 			clnt_destroy(ysd->dom_client);
455 			if (new)
456 				free(ysd);
457 			return(YPERR_YPBIND);
458 		}
459 	}
460 
461 	if(new) {
462 		ysd->dom_pnext = _ypbindlist;
463 		_ypbindlist = ysd;
464 	}
465 
466 	if(ypdb!=NULL)
467 		*ypdb = ysd;
468 	return 0;
469 }
470 
471 static void
472 _yp_unbind(ypb)
473 	struct dom_binding *ypb;
474 {
475 	if (ypb->dom_client)
476 		clnt_destroy(ypb->dom_client);
477 	ypb->dom_client = NULL;
478 	ypb->dom_socket = -1;
479 	ypb->dom_vers = -1;
480 }
481 
482 int
483 yp_bind(dom)
484 	char *dom;
485 {
486 	return _yp_dobind(dom, NULL);
487 }
488 
489 void
490 yp_unbind(dom)
491 	char *dom;
492 {
493 	struct dom_binding *ypb, *ypbp;
494 
495 	ypbp = NULL;
496 	for(ypb=_ypbindlist; ypb; ypb=ypb->dom_pnext) {
497 		if( strcmp(dom, ypb->dom_domain) == 0) {
498 			_yp_unbind(ypb);
499 			if(ypbp)
500 				ypbp->dom_pnext = ypb->dom_pnext;
501 			else
502 				_ypbindlist = ypb->dom_pnext;
503 			free(ypb);
504 			return;
505 		}
506 		ypbp = ypb;
507 	}
508 	return;
509 }
510 
511 int
512 yp_match(indomain, inmap, inkey, inkeylen, outval, outvallen)
513 	char *indomain;
514 	char *inmap;
515 	const char *inkey;
516 	int inkeylen;
517 	char **outval;
518 	int *outvallen;
519 {
520 	struct dom_binding *ysd;
521 	struct ypresp_val yprv;
522 	struct timeval tv;
523 	struct ypreq_key yprk;
524 	int r;
525 
526 	*outval = NULL;
527 	*outvallen = 0;
528 
529 	/* Sanity check */
530 
531 	if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
532 	    inmap == NULL || !strlen(inmap) ||
533 	    indomain == NULL || !strlen(indomain))
534 		return YPERR_BADARGS;
535 
536 #ifdef YPMATCHCACHE
537 	if( !strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
538 	    inkeylen, &yprv.val.valdat_val, &yprv.val.valdat_len)) {
539 		*outvallen = yprv.val.valdat_len;
540 		*outval = (char *)malloc(*outvallen+1);
541 		bcopy(yprv.val.valdat_val, *outval, *outvallen);
542 		(*outval)[*outvallen] = '\0';
543 		return 0;
544 	}
545 #endif
546 
547 again:
548 	if( _yp_dobind(indomain, &ysd) != 0)
549 		return YPERR_DOMAIN;
550 
551 	tv.tv_sec = _yplib_timeout;
552 	tv.tv_usec = 0;
553 
554 	yprk.domain = indomain;
555 	yprk.map = inmap;
556 	yprk.key.keydat_val = (char *)inkey;
557 	yprk.key.keydat_len = inkeylen;
558 
559 	bzero((char *)&yprv, sizeof yprv);
560 
561 	r = clnt_call(ysd->dom_client, YPPROC_MATCH,
562 		xdr_ypreq_key, &yprk, xdr_ypresp_val, &yprv, tv);
563 	if(r != RPC_SUCCESS) {
564 		clnt_perror(ysd->dom_client, "yp_match: clnt_call");
565 		_yp_unbind(ysd);
566 		goto again;
567 	}
568 
569 	if( !(r=ypprot_err(yprv.stat)) ) {
570 		*outvallen = yprv.val.valdat_len;
571 		*outval = (char *)malloc(*outvallen+1);
572 		bcopy(yprv.val.valdat_val, *outval, *outvallen);
573 		(*outval)[*outvallen] = '\0';
574 #ifdef YPMATCHCACHE
575 		if( strcmp(_yp_domain, indomain)==0 )
576 			 ypmatch_add(inmap, inkey, inkeylen, *outval, *outvallen);
577 #endif
578 	}
579 
580 	xdr_free(xdr_ypresp_val, (char *)&yprv);
581 	return r;
582 }
583 
584 int
585 yp_get_default_domain(domp)
586 char **domp;
587 {
588 	*domp = NULL;
589 	if(_yp_domain[0] == '\0')
590 		if( getdomainname(_yp_domain, sizeof _yp_domain))
591 			return YPERR_NODOM;
592 	*domp = _yp_domain;
593 	return 0;
594 }
595 
596 int
597 yp_first(indomain, inmap, outkey, outkeylen, outval, outvallen)
598 	char *indomain;
599 	char *inmap;
600 	char **outkey;
601 	int *outkeylen;
602 	char **outval;
603 	int *outvallen;
604 {
605 	struct ypresp_key_val yprkv;
606 	struct ypreq_nokey yprnk;
607 	struct dom_binding *ysd;
608 	struct timeval tv;
609 	int r;
610 
611 	/* Sanity check */
612 
613 	if (indomain == NULL || !strlen(indomain) ||
614 	    inmap == NULL || !strlen(inmap))
615 		return YPERR_BADARGS;
616 
617 	*outkey = *outval = NULL;
618 	*outkeylen = *outvallen = 0;
619 
620 again:
621 	if( _yp_dobind(indomain, &ysd) != 0)
622 		return YPERR_DOMAIN;
623 
624 	tv.tv_sec = _yplib_timeout;
625 	tv.tv_usec = 0;
626 
627 	yprnk.domain = indomain;
628 	yprnk.map = inmap;
629 	bzero((char *)&yprkv, sizeof yprkv);
630 
631 	r = clnt_call(ysd->dom_client, YPPROC_FIRST,
632 		xdr_ypreq_nokey, &yprnk, xdr_ypresp_key_val, &yprkv, tv);
633 	if(r != RPC_SUCCESS) {
634 		clnt_perror(ysd->dom_client, "yp_first: clnt_call");
635 		_yp_unbind(ysd);
636 		goto again;
637 	}
638 	if( !(r=ypprot_err(yprkv.stat)) ) {
639 		*outkeylen = yprkv.key.keydat_len;
640 		*outkey = (char *)malloc(*outkeylen+1);
641 		bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
642 		(*outkey)[*outkeylen] = '\0';
643 		*outvallen = yprkv.val.valdat_len;
644 		*outval = (char *)malloc(*outvallen+1);
645 		bcopy(yprkv.val.valdat_val, *outval, *outvallen);
646 		(*outval)[*outvallen] = '\0';
647 	}
648 
649 	xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
650 	return r;
651 }
652 
653 int
654 yp_next(indomain, inmap, inkey, inkeylen, outkey, outkeylen, outval, outvallen)
655 	char *indomain;
656 	char *inmap;
657 	char *inkey;
658 	int inkeylen;
659 	char **outkey;
660 	int *outkeylen;
661 	char **outval;
662 	int *outvallen;
663 {
664 	struct ypresp_key_val yprkv;
665 	struct ypreq_key yprk;
666 	struct dom_binding *ysd;
667 	struct timeval tv;
668 	int r;
669 
670 	/* Sanity check */
671 
672 	if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
673 	    inmap == NULL || !strlen(inmap) ||
674 	    indomain == NULL || !strlen(indomain))
675 		return YPERR_BADARGS;
676 
677 	*outkey = *outval = NULL;
678 	*outkeylen = *outvallen = 0;
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 	yprk.domain = indomain;
688 	yprk.map = inmap;
689 	yprk.key.keydat_val = inkey;
690 	yprk.key.keydat_len = inkeylen;
691 	bzero((char *)&yprkv, sizeof yprkv);
692 
693 	r = clnt_call(ysd->dom_client, YPPROC_NEXT,
694 		xdr_ypreq_key, &yprk, xdr_ypresp_key_val, &yprkv, tv);
695 	if(r != RPC_SUCCESS) {
696 		clnt_perror(ysd->dom_client, "yp_next: clnt_call");
697 		_yp_unbind(ysd);
698 		goto again;
699 	}
700 	if( !(r=ypprot_err(yprkv.stat)) ) {
701 		*outkeylen = yprkv.key.keydat_len;
702 		*outkey = (char *)malloc(*outkeylen+1);
703 		bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
704 		(*outkey)[*outkeylen] = '\0';
705 		*outvallen = yprkv.val.valdat_len;
706 		*outval = (char *)malloc(*outvallen+1);
707 		bcopy(yprkv.val.valdat_val, *outval, *outvallen);
708 		(*outval)[*outvallen] = '\0';
709 	}
710 
711 	xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
712 	return r;
713 }
714 
715 int
716 yp_all(indomain, inmap, incallback)
717 	char *indomain;
718 	char *inmap;
719 	struct ypall_callback *incallback;
720 {
721 	struct ypreq_nokey yprnk;
722 	struct dom_binding *ysd;
723 	struct timeval tv;
724 	struct sockaddr_in clnt_sin;
725 	CLIENT *clnt;
726 	u_long status, savstat;
727 	int clnt_sock;
728 
729 	/* Sanity check */
730 
731 	if (indomain == NULL || !strlen(indomain) ||
732 	    inmap == NULL || !strlen(inmap))
733 		return YPERR_BADARGS;
734 
735 again:
736 
737 	if( _yp_dobind(indomain, &ysd) != 0)
738 		return YPERR_DOMAIN;
739 
740 	tv.tv_sec = _yplib_timeout;
741 	tv.tv_usec = 0;
742 
743 	/* YPPROC_ALL manufactures its own channel to ypserv using TCP */
744 
745 	clnt_sock = RPC_ANYSOCK;
746 	clnt_sin = ysd->dom_server_addr;
747 	clnt_sin.sin_port = 0;
748 	clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0);
749 	if(clnt==NULL) {
750 		printf("clnttcp_create failed\n");
751 		return YPERR_PMAP;
752 	}
753 
754 	yprnk.domain = indomain;
755 	yprnk.map = inmap;
756 	ypresp_allfn = incallback->foreach;
757 	ypresp_data = (void *)incallback->data;
758 
759 	if (clnt_call(clnt, YPPROC_ALL,
760 		xdr_ypreq_nokey, &yprnk,
761 		xdr_ypresp_all_seq, &status, tv) != RPC_SUCCESS) {
762 			clnt_perror(ysd->dom_client, "yp_next: clnt_call");
763 			clnt_destroy(clnt);
764 			_yp_unbind(ysd);
765 			goto again;
766 	}
767 
768 	clnt_destroy(clnt);
769 	savstat = status;
770 	xdr_free(xdr_ypresp_all_seq, (char *)&status);	/* not really needed... */
771 	if(savstat != YP_NOMORE)
772 		return ypprot_err(savstat);
773 	return 0;
774 }
775 
776 int
777 yp_order(indomain, inmap, outorder)
778 	char *indomain;
779 	char *inmap;
780 	int *outorder;
781 {
782  	struct dom_binding *ysd;
783 	struct ypresp_order ypro;
784 	struct ypreq_nokey yprnk;
785 	struct timeval tv;
786 	int r;
787 
788 	/* Sanity check */
789 
790 	if (indomain == NULL || !strlen(indomain) ||
791 	    inmap == NULL || !strlen(inmap))
792 		return YPERR_BADARGS;
793 
794 again:
795 	if( _yp_dobind(indomain, &ysd) != 0)
796 		return YPERR_DOMAIN;
797 
798 	tv.tv_sec = _yplib_timeout;
799 	tv.tv_usec = 0;
800 
801 	yprnk.domain = indomain;
802 	yprnk.map = inmap;
803 
804 	bzero((char *)(char *)&ypro, sizeof ypro);
805 
806 	r = clnt_call(ysd->dom_client, YPPROC_ORDER,
807 		xdr_ypreq_nokey, &yprnk, xdr_ypresp_order, &ypro, tv);
808 	if(r != RPC_SUCCESS) {
809 		clnt_perror(ysd->dom_client, "yp_order: clnt_call");
810 		_yp_unbind(ysd);
811 		goto again;
812 	}
813 
814 	if( !(r=ypprot_err(ypro.stat)) ) {
815 		*outorder = ypro.ordernum;
816 	}
817 
818 	xdr_free(xdr_ypresp_order, (char *)&ypro);
819 	return (r);
820 }
821 
822 int
823 yp_master(indomain, inmap, outname)
824 	char *indomain;
825 	char *inmap;
826 	char **outname;
827 {
828 	struct dom_binding *ysd;
829 	struct ypresp_master yprm;
830 	struct ypreq_nokey yprnk;
831 	struct timeval tv;
832 	int r;
833 
834 	/* Sanity check */
835 
836 	if (indomain == NULL || !strlen(indomain) ||
837 	    inmap == NULL || !strlen(inmap))
838 		return YPERR_BADARGS;
839 again:
840 	if( _yp_dobind(indomain, &ysd) != 0)
841 		return YPERR_DOMAIN;
842 
843 	tv.tv_sec = _yplib_timeout;
844 	tv.tv_usec = 0;
845 
846 	yprnk.domain = indomain;
847 	yprnk.map = inmap;
848 
849 	bzero((char *)&yprm, sizeof yprm);
850 
851 	r = clnt_call(ysd->dom_client, YPPROC_MASTER,
852 		xdr_ypreq_nokey, &yprnk, xdr_ypresp_master, &yprm, tv);
853 	if(r != RPC_SUCCESS) {
854 		clnt_perror(ysd->dom_client, "yp_master: clnt_call");
855 		_yp_unbind(ysd);
856 		goto again;
857 	}
858 
859 	if( !(r=ypprot_err(yprm.stat)) ) {
860 		*outname = (char *)strdup(yprm.peer);
861 	}
862 
863 	xdr_free(xdr_ypresp_master, (char *)&yprm);
864 	return (r);
865 }
866 int
867 yp_maplist(indomain, outmaplist)
868 	char *indomain;
869 	struct ypmaplist **outmaplist;
870 {
871 	struct dom_binding *ysd;
872 	struct ypresp_maplist ypml;
873 	struct timeval tv;
874 	int r;
875 
876 	/* Sanity check */
877 
878 	if (indomain == NULL || !strlen(indomain))
879 		return YPERR_BADARGS;
880 
881 again:
882 	if( _yp_dobind(indomain, &ysd) != 0)
883 		return YPERR_DOMAIN;
884 
885 	tv.tv_sec = _yplib_timeout;
886 	tv.tv_usec = 0;
887 
888 	bzero((char *)&ypml, sizeof ypml);
889 
890 	r = clnt_call(ysd->dom_client, YPPROC_MAPLIST,
891 		xdr_domainname,(char *)&indomain,xdr_ypresp_maplist,&ypml,tv);
892 	if (r != RPC_SUCCESS) {
893 		clnt_perror(ysd->dom_client, "yp_maplist: clnt_call");
894 		_yp_unbind(ysd);
895 		goto again;
896 	}
897 	if( !(r=ypprot_err(ypml.stat)) ) {
898 		*outmaplist = ypml.maps;
899 	}
900 
901 	/* NO: xdr_free(xdr_ypresp_maplist, &ypml);*/
902 	return (r);
903 }
904 
905 char *
906 yperr_string(incode)
907 	int incode;
908 {
909 	static char err[80];
910 
911 	switch(incode) {
912 	case 0:
913 		return "Success";
914 	case YPERR_BADARGS:
915 		return "Request arguments bad";
916 	case YPERR_RPC:
917 		return "RPC failure";
918 	case YPERR_DOMAIN:
919 		return "Can't bind to server which serves this domain";
920 	case YPERR_MAP:
921 		return "No such map in server's domain";
922 	case YPERR_KEY:
923 		return "No such key in map";
924 	case YPERR_YPERR:
925 		return "YP server error";
926 	case YPERR_RESRC:
927 		return "Local resource allocation failure";
928 	case YPERR_NOMORE:
929 		return "No more records in map database";
930 	case YPERR_PMAP:
931 		return "Can't communicate with portmapper";
932 	case YPERR_YPBIND:
933 		return "Can't communicate with ypbind";
934 	case YPERR_YPSERV:
935 		return "Can't communicate with ypserv";
936 	case YPERR_NODOM:
937 		return "Local domain name not set";
938 	case YPERR_BADDB:
939 		return "Server data base is bad";
940 	case YPERR_VERS:
941 		return "YP server version mismatch - server can't supply service.";
942 	case YPERR_ACCESS:
943 		return "Access violation";
944 	case YPERR_BUSY:
945 		return "Database is busy";
946 	}
947 	sprintf(err, "YP unknown error %d\n", incode);
948 	return err;
949 }
950 
951 int
952 ypprot_err(incode)
953 	unsigned int incode;
954 {
955 	switch(incode) {
956 	case YP_TRUE:
957 		return 0;
958 	case YP_FALSE:
959 		return YPERR_YPBIND;
960 	case YP_NOMORE:
961 		return YPERR_NOMORE;
962 	case YP_NOMAP:
963 		return YPERR_MAP;
964 	case YP_NODOM:
965 		return YPERR_DOMAIN;
966 	case YP_NOKEY:
967 		return YPERR_KEY;
968 	case YP_BADOP:
969 		return YPERR_YPERR;
970 	case YP_BADDB:
971 		return YPERR_BADDB;
972 	case YP_YPERR:
973 		return YPERR_YPERR;
974 	case YP_BADARGS:
975 		return YPERR_BADARGS;
976 	case YP_VERS:
977 		return YPERR_VERS;
978 	}
979 	return YPERR_YPERR;
980 }
981 
982 int
983 _yp_check(dom)
984 	char **dom;
985 {
986 	char *unused;
987 
988 	if( _yp_domain[0]=='\0' )
989 		if( yp_get_default_domain(&unused) )
990 			return 0;
991 
992 	if(dom)
993 		*dom = _yp_domain;
994 
995 	if( yp_bind(_yp_domain)==0 ) {
996 		yp_unbind(_yp_domain);
997 		return 1;
998 	}
999 	return 0;
1000 }
1001