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