xref: /freebsd/lib/libc/yp/yplib.c (revision e4e9813eb92cd7c4d4b819a8fbed5cbd3d92f5d8)
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 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include "namespace.h"
35 #include "reentrant.h"
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 <arpa/inet.h>
42 #include <errno.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <rpc/rpc.h>
48 #include <rpc/xdr.h>
49 #include <rpcsvc/yp.h>
50 #include "un-namespace.h"
51 #include "libc_private.h"
52 
53 /*
54  * We have to define these here due to clashes between yp_prot.h and
55  * yp.h.
56  */
57 
58 #define YPMATCHCACHE
59 
60 #ifdef YPMATCHCACHE
61 struct ypmatch_ent {
62         char			*ypc_map;
63 	keydat			ypc_key;
64 	valdat			ypc_val;
65         time_t			ypc_expire_t;
66         struct ypmatch_ent	*ypc_next;
67 };
68 #define YPLIB_MAXCACHE	5	/* At most 5 entries */
69 #define YPLIB_EXPIRE	5	/* Expire after 5 seconds */
70 #endif
71 
72 struct dom_binding {
73         struct dom_binding *dom_pnext;
74         char dom_domain[YPMAXDOMAIN + 1];
75         struct sockaddr_in dom_server_addr;
76         u_short dom_server_port;
77         int dom_socket;
78         CLIENT *dom_client;
79         u_short dom_local_port; /* now I finally know what this is for. */
80         long dom_vers;
81 #ifdef YPMATCHCACHE
82 	struct ypmatch_ent *cache;
83 	int ypmatch_cachecnt;
84 #endif
85 };
86 
87 #include <rpcsvc/ypclnt.h>
88 
89 #ifndef BINDINGDIR
90 #define BINDINGDIR "/var/yp/binding"
91 #endif
92 #define MAX_RETRIES 20
93 
94 extern bool_t xdr_domainname(), xdr_ypbind_resp();
95 extern bool_t xdr_ypreq_key(), xdr_ypresp_val();
96 extern bool_t xdr_ypreq_nokey(), xdr_ypresp_key_val();
97 extern bool_t xdr_ypresp_all(), xdr_ypresp_all_seq();
98 extern bool_t xdr_ypresp_master();
99 
100 int (*ypresp_allfn)();
101 void *ypresp_data;
102 
103 static void _yp_unbind(struct dom_binding *);
104 struct dom_binding *_ypbindlist;
105 static char _yp_domain[MAXHOSTNAMELEN];
106 int _yplib_timeout = 10;
107 
108 static mutex_t _ypmutex = MUTEX_INITIALIZER;
109 #define YPLOCK()	mutex_lock(&_ypmutex);
110 #define YPUNLOCK()	mutex_unlock(&_ypmutex);
111 
112 #ifdef YPMATCHCACHE
113 static void
114 ypmatch_cache_delete(struct dom_binding *ypdb, struct ypmatch_ent *prev,
115     struct ypmatch_ent *cur)
116 {
117 	if (prev == NULL)
118 		ypdb->cache = cur->ypc_next;
119 	else
120 		prev->ypc_next = cur->ypc_next;
121 
122 	free(cur->ypc_map);
123 	free(cur->ypc_key.keydat_val);
124 	free(cur->ypc_val.valdat_val);
125 	free(cur);
126 
127 	ypdb->ypmatch_cachecnt--;
128 
129 	return;
130 }
131 
132 static void
133 ypmatch_cache_flush(struct dom_binding *ypdb)
134 {
135 	struct ypmatch_ent	*n, *c = ypdb->cache;
136 
137 	while (c != NULL) {
138 		n = c->ypc_next;
139 		ypmatch_cache_delete(ypdb, NULL, c);
140 		c = n;
141 	}
142 
143 	return;
144 }
145 
146 static void
147 ypmatch_cache_expire(struct dom_binding *ypdb)
148 {
149 	struct ypmatch_ent	*c = ypdb->cache;
150 	struct ypmatch_ent	*n, *p = NULL;
151 	time_t			t;
152 
153 	time(&t);
154 
155 	while (c != NULL) {
156 		if (t >= c->ypc_expire_t) {
157 			n = c->ypc_next;
158 			ypmatch_cache_delete(ypdb, p, c);
159 			c = n;
160 		} else {
161 			p = c;
162 			c = c->ypc_next;
163 		}
164 	}
165 
166 	return;
167 }
168 
169 static void
170 ypmatch_cache_insert(struct dom_binding *ypdb, char *map, keydat *key,
171     valdat *val)
172 {
173 	struct ypmatch_ent	*new;
174 
175 	/* Do an expire run to maybe open up a slot. */
176 	if (ypdb->ypmatch_cachecnt)
177 		ypmatch_cache_expire(ypdb);
178 
179 	/*
180 	 * If there are no slots free, then force an expire of
181 	 * the least recently used entry.
182  	 */
183 	if (ypdb->ypmatch_cachecnt >= YPLIB_MAXCACHE) {
184 		struct ypmatch_ent	*o = NULL, *c = ypdb->cache;
185 		time_t			oldest = 0;
186 
187 		oldest = ~oldest;
188 
189 		while (c != NULL) {
190 			if (c->ypc_expire_t < oldest) {
191 				oldest = c->ypc_expire_t;
192 				o = c;
193 			}
194 			c = c->ypc_next;
195 		}
196 
197 		if (o == NULL)
198 			return;
199 		o->ypc_expire_t = 0;
200 		ypmatch_cache_expire(ypdb);
201 	}
202 
203 	new = malloc(sizeof(struct ypmatch_ent));
204 	if (new == NULL)
205 		return;
206 
207 	new->ypc_map = strdup(map);
208 	if (new->ypc_map == NULL) {
209 		free(new);
210 		return;
211 	}
212 	new->ypc_key.keydat_val = malloc(key->keydat_len);
213 	if (new->ypc_key.keydat_val == NULL) {
214 		free(new->ypc_map);
215 		free(new);
216 		return;
217 	}
218 	new->ypc_val.valdat_val = malloc(val->valdat_len);
219 	if (new->ypc_val.valdat_val == NULL) {
220 		free(new->ypc_val.valdat_val);
221 		free(new->ypc_map);
222 		free(new);
223 		return;
224 	}
225 
226 	new->ypc_expire_t = time(NULL) + YPLIB_EXPIRE;
227 	new->ypc_key.keydat_len = key->keydat_len;
228 	new->ypc_val.valdat_len = val->valdat_len;
229 	bcopy(key->keydat_val, new->ypc_key.keydat_val, key->keydat_len);
230 	bcopy(val->valdat_val, new->ypc_val.valdat_val, val->valdat_len);
231 
232 	new->ypc_next = ypdb->cache;
233 	ypdb->cache = new;
234 
235 	ypdb->ypmatch_cachecnt++;
236 
237 	return;
238 }
239 
240 static bool_t
241 ypmatch_cache_lookup(struct dom_binding *ypdb, char *map, keydat *key,
242     valdat *val)
243 {
244 	struct ypmatch_ent	*c = ypdb->cache;
245 
246 	ypmatch_cache_expire(ypdb);
247 
248 	for (c = ypdb->cache; c != NULL; c = c->ypc_next) {
249 		if (strcmp(map, c->ypc_map))
250 			continue;
251 		if (key->keydat_len != c->ypc_key.keydat_len)
252 			continue;
253 		if (bcmp(key->keydat_val, c->ypc_key.keydat_val,
254 				key->keydat_len))
255 			continue;
256 	}
257 
258 	if (c == NULL)
259 		return(FALSE);
260 
261 	val->valdat_len = c->ypc_val.valdat_len;
262 	val->valdat_val = c->ypc_val.valdat_val;
263 
264 	return(TRUE);
265 }
266 #endif
267 
268 const char *
269 ypbinderr_string(int incode)
270 {
271 	static char err[80];
272 	switch (incode) {
273 	case 0:
274 		return ("Success");
275 	case YPBIND_ERR_ERR:
276 		return ("Internal ypbind error");
277 	case YPBIND_ERR_NOSERV:
278 		return ("Domain not bound");
279 	case YPBIND_ERR_RESC:
280 		return ("System resource allocation failure");
281 	}
282 	sprintf(err, "Unknown ypbind error: #%d\n", incode);
283 	return (err);
284 }
285 
286 int
287 _yp_dobind(char *dom, 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 	socklen_t 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 		snprintf(path, sizeof(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 			bcopy(&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
403 			    &ysd->dom_server_addr.sin_addr.s_addr,
404 			    sizeof(ysd->dom_server_addr.sin_addr.s_addr));
405 			bcopy(&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port,
406 			    &ysd->dom_server_addr.sin_port,
407 			    sizeof(ysd->dom_server_addr.sin_port));
408 
409 			ysd->dom_server_port = ysd->dom_server_addr.sin_port;
410 			_close(fd);
411 			goto gotit;
412 		} else {
413 			/* no lock on binding file, YP is dead. */
414 			/* Try to bring it back to life. */
415 			_close(fd);
416 			goto skipit;
417 		}
418 	}
419 skipit:
420 #endif
421 	if (ysd->dom_vers == -1 || ysd->dom_vers == 0) {
422 		/*
423 		 * We're trying to make a new binding: zorch the
424 		 * existing handle now (if any).
425 		 */
426 		if (ysd->dom_client != NULL) {
427 			clnt_destroy(ysd->dom_client);
428 			ysd->dom_client = NULL;
429 			ysd->dom_socket = -1;
430 		}
431 		bzero((char *)&clnt_sin, sizeof clnt_sin);
432 		clnt_sin.sin_family = AF_INET;
433 		clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
434 
435 		clnt_sock = RPC_ANYSOCK;
436 		client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, &clnt_sock,
437 			0, 0);
438 		if (client == NULL) {
439 			/*
440 			 * These conditions indicate ypbind just isn't
441 			 * alive -- we probably don't want to shoot our
442 			 * mouth off in this case; instead generate error
443 			 * messages only for really exotic problems.
444 			 */
445 			if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
446 			   (rpc_createerr.cf_stat != RPC_SYSTEMERROR &&
447 			   rpc_createerr.cf_error.re_errno == ECONNREFUSED))
448 				clnt_pcreateerror("clnttcp_create");
449 			if (new)
450 				free(ysd);
451 			return (YPERR_YPBIND);
452 		}
453 
454 		/*
455 		 * Check the port number -- should be < IPPORT_RESERVED.
456 		 * If not, it's possible someone has registered a bogus
457 		 * ypbind with the portmapper and is trying to trick us.
458 		 */
459 		if (ntohs(clnt_sin.sin_port) >= IPPORT_RESERVED) {
460 			if (client != NULL)
461 				clnt_destroy(client);
462 			if (new)
463 				free(ysd);
464 			return(YPERR_YPBIND);
465 		}
466 		tv.tv_sec = _yplib_timeout/2;
467 		tv.tv_usec = 0;
468 		r = clnt_call(client, YPBINDPROC_DOMAIN,
469 			(xdrproc_t)xdr_domainname, &dom,
470 			(xdrproc_t)xdr_ypbind_resp, &ypbr, tv);
471 		if (r != RPC_SUCCESS) {
472 			clnt_destroy(client);
473 			ysd->dom_vers = -1;
474 			if (r == RPC_PROGUNAVAIL || r == RPC_PROCUNAVAIL) {
475 				if (new)
476 					free(ysd);
477 				return(YPERR_YPBIND);
478 			}
479 			fprintf(stderr,
480 			"YP: server for domain %s not responding, retrying\n", dom);
481 			goto again;
482 		} else {
483 			if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
484 				struct timespec time_to_sleep, time_remaining;
485 
486 				clnt_destroy(client);
487 				ysd->dom_vers = -1;
488 
489 				time_to_sleep.tv_sec = _yplib_timeout/2;
490 				time_to_sleep.tv_nsec = 0;
491 				_nanosleep(&time_to_sleep,
492 				    &time_remaining);
493 				goto again;
494 			}
495 		}
496 		clnt_destroy(client);
497 
498 		bzero((char *)&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
499 		ysd->dom_server_addr.sin_family = AF_INET;
500 		bcopy(&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port,
501 		    &ysd->dom_server_addr.sin_port,
502 		    sizeof(ysd->dom_server_addr.sin_port));
503 		bcopy(&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
504 		    &ysd->dom_server_addr.sin_addr.s_addr,
505 		    sizeof(ysd->dom_server_addr.sin_addr.s_addr));
506 
507 		/*
508 		 * We could do a reserved port check here too, but this
509 		 * could pose compatibility problems. The local ypbind is
510 		 * supposed to decide whether or not to trust yp servers
511 		 * on insecure ports. For now, we trust its judgement.
512 		 */
513 		ysd->dom_server_port =
514 			*(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
515 gotit:
516 		ysd->dom_vers = YPVERS;
517 		strlcpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain));
518 	}
519 
520 	/* Don't rebuild the connection to the server unless we have to. */
521 	if (ysd->dom_client == NULL) {
522 		tv.tv_sec = _yplib_timeout/2;
523 		tv.tv_usec = 0;
524 		ysd->dom_socket = RPC_ANYSOCK;
525 		ysd->dom_client = clntudp_bufcreate(&ysd->dom_server_addr,
526 			YPPROG, YPVERS, tv, &ysd->dom_socket, 1280, 2304);
527 		if (ysd->dom_client == NULL) {
528 			clnt_pcreateerror("clntudp_create");
529 			ysd->dom_vers = -1;
530 			goto again;
531 		}
532 		if (_fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
533 			perror("fcntl: F_SETFD");
534 		/*
535 		 * We want a port number associated with this socket
536 		 * so that we can check its authenticity later.
537 		 */
538 		checklen = sizeof(struct sockaddr_in);
539 		bzero((char *)&check, checklen);
540 		_bind(ysd->dom_socket, (struct sockaddr *)&check, checklen);
541 		check.sin_family = AF_INET;
542 		if (!_getsockname(ysd->dom_socket,
543 		    (struct sockaddr *)&check, &checklen)) {
544 			ysd->dom_local_port = check.sin_port;
545 		} else {
546 			clnt_destroy(ysd->dom_client);
547 			if (new)
548 				free(ysd);
549 			return(YPERR_YPBIND);
550 		}
551 	}
552 
553 	if (new) {
554 		ysd->dom_pnext = _ypbindlist;
555 		_ypbindlist = ysd;
556 	}
557 
558 	if (ypdb != NULL)
559 		*ypdb = ysd;
560 	return (0);
561 }
562 
563 static void
564 _yp_unbind(struct dom_binding *ypb)
565 {
566 	struct sockaddr_in check;
567 	socklen_t checklen = sizeof(struct sockaddr_in);
568 
569 	if (ypb->dom_client != NULL) {
570 		/* Check the socket -- may have been hosed by the caller. */
571 		if (_getsockname(ypb->dom_socket, (struct sockaddr *)&check,
572 	    	&checklen) == -1 || check.sin_family != AF_INET ||
573 	    	check.sin_port != ypb->dom_local_port) {
574 			int save, sock;
575 
576 			sock = ypb->dom_socket;
577 			save = _dup(ypb->dom_socket);
578 			clnt_destroy(ypb->dom_client);
579 			sock = _dup2(save, sock);
580 			_close(save);
581 		} else
582 			clnt_destroy(ypb->dom_client);
583 	}
584 
585 	ypb->dom_client = NULL;
586 	ypb->dom_socket = -1;
587 	ypb->dom_vers = -1;
588 #ifdef YPMATCHCACHE
589 	ypmatch_cache_flush(ypb);
590 #endif
591 }
592 
593 static int
594 yp_bind_locked(char *dom)
595 {
596 	return (_yp_dobind(dom, NULL));
597 }
598 
599 int
600 yp_bind(char *dom)
601 {
602 	int r;
603 
604 	YPLOCK();
605 	r = yp_bind_locked(dom);
606 	YPUNLOCK();
607 	return (r);
608 }
609 
610 static void
611 yp_unbind_locked(char *dom)
612 {
613 	struct dom_binding *ypb, *ypbp;
614 
615 	ypbp = NULL;
616 	for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) {
617 		if (strcmp(dom, ypb->dom_domain) == 0) {
618 			_yp_unbind(ypb);
619 			if (ypbp)
620 				ypbp->dom_pnext = ypb->dom_pnext;
621 			else
622 				_ypbindlist = ypb->dom_pnext;
623 			free(ypb);
624 			return;
625 		}
626 		ypbp = ypb;
627 	}
628 	return;
629 }
630 
631 void
632 yp_unbind(char *dom)
633 {
634 	YPLOCK();
635 	yp_unbind_locked(dom);
636 	YPUNLOCK();
637 }
638 
639 int
640 yp_match(char *indomain, char *inmap, const char *inkey, int inkeylen,
641     char **outval, int *outvallen)
642 {
643 	struct dom_binding *ysd;
644 	struct ypresp_val yprv;
645 	struct timeval tv;
646 	struct ypreq_key yprk;
647 	int r;
648 
649 	*outval = NULL;
650 	*outvallen = 0;
651 
652 	/* Sanity check */
653 
654 	if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
655 	    inmap == NULL || !strlen(inmap) ||
656 	    indomain == NULL || !strlen(indomain))
657 		return (YPERR_BADARGS);
658 
659 	YPLOCK();
660 	if (_yp_dobind(indomain, &ysd) != 0) {
661 		YPUNLOCK();
662 		return(YPERR_DOMAIN);
663 	}
664 
665 	yprk.domain = indomain;
666 	yprk.map = inmap;
667 	yprk.key.keydat_val = (char *)inkey;
668 	yprk.key.keydat_len = inkeylen;
669 
670 #ifdef YPMATCHCACHE
671 	if (ypmatch_cache_lookup(ysd, yprk.map, &yprk.key, &yprv.val) == TRUE) {
672 /*
673 	if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
674 	    inkeylen, &yprv.val.valdat_val, &yprv.val.valdat_len)) {
675 */
676 		*outvallen = yprv.val.valdat_len;
677 		*outval = (char *)malloc(*outvallen+1);
678 		bcopy(yprv.val.valdat_val, *outval, *outvallen);
679 		(*outval)[*outvallen] = '\0';
680 		YPUNLOCK();
681 		return (0);
682 	}
683 #endif
684 
685 again:
686 	if (_yp_dobind(indomain, &ysd) != 0) {
687 		YPUNLOCK();
688 		return (YPERR_DOMAIN);
689 	}
690 
691 	tv.tv_sec = _yplib_timeout;
692 	tv.tv_usec = 0;
693 
694 	bzero((char *)&yprv, sizeof yprv);
695 
696 	r = clnt_call(ysd->dom_client, YPPROC_MATCH,
697 		(xdrproc_t)xdr_ypreq_key, &yprk,
698 		(xdrproc_t)xdr_ypresp_val, &yprv, tv);
699 	if (r != RPC_SUCCESS) {
700 		clnt_perror(ysd->dom_client, "yp_match: clnt_call");
701 		_yp_unbind(ysd);
702 		goto again;
703 	}
704 
705 	if (!(r = ypprot_err(yprv.stat))) {
706 		*outvallen = yprv.val.valdat_len;
707 		*outval = (char *)malloc(*outvallen+1);
708 		bcopy(yprv.val.valdat_val, *outval, *outvallen);
709 		(*outval)[*outvallen] = '\0';
710 #ifdef YPMATCHCACHE
711 		ypmatch_cache_insert(ysd, yprk.map, &yprk.key, &yprv.val);
712 #endif
713 	}
714 
715 	xdr_free((xdrproc_t)xdr_ypresp_val, &yprv);
716 	YPUNLOCK();
717 	return (r);
718 }
719 
720 static int
721 yp_get_default_domain_locked(char **domp)
722 {
723 	*domp = NULL;
724 	if (_yp_domain[0] == '\0')
725 		if (getdomainname(_yp_domain, sizeof _yp_domain))
726 			return (YPERR_NODOM);
727 	*domp = _yp_domain;
728 	return (0);
729 }
730 
731 int
732 yp_get_default_domain(char **domp)
733 {
734 	int r;
735 
736 	YPLOCK();
737 	r = yp_get_default_domain_locked(domp);
738 	YPUNLOCK();
739 	return (r);
740 }
741 
742 int
743 yp_first(char *indomain, char *inmap, char **outkey, int *outkeylen,
744     char **outval, int *outvallen)
745 {
746 	struct ypresp_key_val yprkv;
747 	struct ypreq_nokey yprnk;
748 	struct dom_binding *ysd;
749 	struct timeval tv;
750 	int r;
751 
752 	/* Sanity check */
753 
754 	if (indomain == NULL || !strlen(indomain) ||
755 	    inmap == NULL || !strlen(inmap))
756 		return (YPERR_BADARGS);
757 
758 	*outkey = *outval = NULL;
759 	*outkeylen = *outvallen = 0;
760 
761 	YPLOCK();
762 again:
763 	if (_yp_dobind(indomain, &ysd) != 0) {
764 		YPUNLOCK();
765 		return (YPERR_DOMAIN);
766 	}
767 
768 	tv.tv_sec = _yplib_timeout;
769 	tv.tv_usec = 0;
770 
771 	yprnk.domain = indomain;
772 	yprnk.map = inmap;
773 	bzero((char *)&yprkv, sizeof yprkv);
774 
775 	r = clnt_call(ysd->dom_client, YPPROC_FIRST,
776 		(xdrproc_t)xdr_ypreq_nokey, &yprnk,
777 		(xdrproc_t)xdr_ypresp_key_val, &yprkv, tv);
778 	if (r != RPC_SUCCESS) {
779 		clnt_perror(ysd->dom_client, "yp_first: clnt_call");
780 		_yp_unbind(ysd);
781 		goto again;
782 	}
783 	if (!(r = ypprot_err(yprkv.stat))) {
784 		*outkeylen = yprkv.key.keydat_len;
785 		*outkey = (char *)malloc(*outkeylen+1);
786 		bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
787 		(*outkey)[*outkeylen] = '\0';
788 		*outvallen = yprkv.val.valdat_len;
789 		*outval = (char *)malloc(*outvallen+1);
790 		bcopy(yprkv.val.valdat_val, *outval, *outvallen);
791 		(*outval)[*outvallen] = '\0';
792 	}
793 
794 	xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
795 	YPUNLOCK();
796 	return (r);
797 }
798 
799 int
800 yp_next(char *indomain, char *inmap, char *inkey, int inkeylen,
801     char **outkey, int *outkeylen, char **outval, int *outvallen)
802 {
803 	struct ypresp_key_val yprkv;
804 	struct ypreq_key yprk;
805 	struct dom_binding *ysd;
806 	struct timeval tv;
807 	int r;
808 
809 	/* Sanity check */
810 
811 	if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
812 	    inmap == NULL || !strlen(inmap) ||
813 	    indomain == NULL || !strlen(indomain))
814 		return (YPERR_BADARGS);
815 
816 	*outkey = *outval = NULL;
817 	*outkeylen = *outvallen = 0;
818 
819 	YPLOCK();
820 again:
821 	if (_yp_dobind(indomain, &ysd) != 0) {
822 		YPUNLOCK();
823 		return (YPERR_DOMAIN);
824 	}
825 
826 	tv.tv_sec = _yplib_timeout;
827 	tv.tv_usec = 0;
828 
829 	yprk.domain = indomain;
830 	yprk.map = inmap;
831 	yprk.key.keydat_val = inkey;
832 	yprk.key.keydat_len = inkeylen;
833 	bzero((char *)&yprkv, sizeof yprkv);
834 
835 	r = clnt_call(ysd->dom_client, YPPROC_NEXT,
836 		(xdrproc_t)xdr_ypreq_key, &yprk,
837 		(xdrproc_t)xdr_ypresp_key_val, &yprkv, tv);
838 	if (r != RPC_SUCCESS) {
839 		clnt_perror(ysd->dom_client, "yp_next: clnt_call");
840 		_yp_unbind(ysd);
841 		goto again;
842 	}
843 	if (!(r = ypprot_err(yprkv.stat))) {
844 		*outkeylen = yprkv.key.keydat_len;
845 		*outkey = (char *)malloc(*outkeylen+1);
846 		bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
847 		(*outkey)[*outkeylen] = '\0';
848 		*outvallen = yprkv.val.valdat_len;
849 		*outval = (char *)malloc(*outvallen+1);
850 		bcopy(yprkv.val.valdat_val, *outval, *outvallen);
851 		(*outval)[*outvallen] = '\0';
852 	}
853 
854 	xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
855 	YPUNLOCK();
856 	return (r);
857 }
858 
859 int
860 yp_all(char *indomain, char *inmap, struct ypall_callback *incallback)
861 {
862 	struct ypreq_nokey yprnk;
863 	struct dom_binding *ysd;
864 	struct timeval tv;
865 	struct sockaddr_in clnt_sin;
866 	CLIENT *clnt;
867 	u_long status, savstat;
868 	int clnt_sock;
869 
870 	/* Sanity check */
871 
872 	if (indomain == NULL || !strlen(indomain) ||
873 	    inmap == NULL || !strlen(inmap))
874 		return (YPERR_BADARGS);
875 
876 	YPLOCK();
877 again:
878 
879 	if (_yp_dobind(indomain, &ysd) != 0) {
880 		YPUNLOCK();
881 		return (YPERR_DOMAIN);
882 	}
883 
884 	tv.tv_sec = _yplib_timeout;
885 	tv.tv_usec = 0;
886 
887 	/* YPPROC_ALL manufactures its own channel to ypserv using TCP */
888 
889 	clnt_sock = RPC_ANYSOCK;
890 	clnt_sin = ysd->dom_server_addr;
891 	clnt_sin.sin_port = 0;
892 	clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0);
893 	if (clnt == NULL) {
894 		YPUNLOCK();
895 		printf("clnttcp_create failed\n");
896 		return (YPERR_PMAP);
897 	}
898 
899 	yprnk.domain = indomain;
900 	yprnk.map = inmap;
901 	ypresp_allfn = incallback->foreach;
902 	ypresp_data = (void *)incallback->data;
903 
904 	if (clnt_call(clnt, YPPROC_ALL,
905 		(xdrproc_t)xdr_ypreq_nokey, &yprnk,
906 		(xdrproc_t)xdr_ypresp_all_seq, &status, tv) != RPC_SUCCESS) {
907 			clnt_perror(ysd->dom_client, "yp_all: clnt_call");
908 			clnt_destroy(clnt);
909 			_yp_unbind(ysd);
910 			goto again;
911 	}
912 
913 	clnt_destroy(clnt);
914 	savstat = status;
915 	xdr_free((xdrproc_t)xdr_ypresp_all_seq, &status);	/* not really needed... */
916 	YPUNLOCK();
917 	if (savstat != YP_NOMORE)
918 		return (ypprot_err(savstat));
919 	return (0);
920 }
921 
922 int
923 yp_order(char *indomain, char *inmap, int *outorder)
924 {
925  	struct dom_binding *ysd;
926 	struct ypresp_order ypro;
927 	struct ypreq_nokey yprnk;
928 	struct timeval tv;
929 	int r;
930 
931 	/* Sanity check */
932 
933 	if (indomain == NULL || !strlen(indomain) ||
934 	    inmap == NULL || !strlen(inmap))
935 		return (YPERR_BADARGS);
936 
937 	YPLOCK();
938 again:
939 	if (_yp_dobind(indomain, &ysd) != 0) {
940 		YPUNLOCK();
941 		return (YPERR_DOMAIN);
942 	}
943 
944 	tv.tv_sec = _yplib_timeout;
945 	tv.tv_usec = 0;
946 
947 	yprnk.domain = indomain;
948 	yprnk.map = inmap;
949 
950 	bzero((char *)(char *)&ypro, sizeof ypro);
951 
952 	r = clnt_call(ysd->dom_client, YPPROC_ORDER,
953 		(xdrproc_t)xdr_ypreq_nokey, &yprnk,
954 		(xdrproc_t)xdr_ypresp_order, &ypro, tv);
955 
956 	/*
957 	 * NIS+ in YP compat mode doesn't support the YPPROC_ORDER
958 	 * procedure.
959 	 */
960 	if (r == RPC_PROCUNAVAIL) {
961 		YPUNLOCK();
962 		return(YPERR_YPERR);
963 	}
964 
965 	if (r != RPC_SUCCESS) {
966 		clnt_perror(ysd->dom_client, "yp_order: clnt_call");
967 		_yp_unbind(ysd);
968 		goto again;
969 	}
970 
971 	if (!(r = ypprot_err(ypro.stat))) {
972 		*outorder = ypro.ordernum;
973 	}
974 
975 	xdr_free((xdrproc_t)xdr_ypresp_order, &ypro);
976 	YPUNLOCK();
977 	return (r);
978 }
979 
980 int
981 yp_master(char *indomain, char *inmap, char **outname)
982 {
983 	struct dom_binding *ysd;
984 	struct ypresp_master yprm;
985 	struct ypreq_nokey yprnk;
986 	struct timeval tv;
987 	int r;
988 
989 	/* Sanity check */
990 
991 	if (indomain == NULL || !strlen(indomain) ||
992 	    inmap == NULL || !strlen(inmap))
993 		return (YPERR_BADARGS);
994 	YPLOCK();
995 again:
996 	if (_yp_dobind(indomain, &ysd) != 0) {
997 		YPUNLOCK();
998 		return (YPERR_DOMAIN);
999 	}
1000 
1001 	tv.tv_sec = _yplib_timeout;
1002 	tv.tv_usec = 0;
1003 
1004 	yprnk.domain = indomain;
1005 	yprnk.map = inmap;
1006 
1007 	bzero((char *)&yprm, sizeof yprm);
1008 
1009 	r = clnt_call(ysd->dom_client, YPPROC_MASTER,
1010 		(xdrproc_t)xdr_ypreq_nokey, &yprnk,
1011 		(xdrproc_t)xdr_ypresp_master, &yprm, tv);
1012 	if (r != RPC_SUCCESS) {
1013 		clnt_perror(ysd->dom_client, "yp_master: clnt_call");
1014 		_yp_unbind(ysd);
1015 		goto again;
1016 	}
1017 
1018 	if (!(r = ypprot_err(yprm.stat))) {
1019 		*outname = (char *)strdup(yprm.peer);
1020 	}
1021 
1022 	xdr_free((xdrproc_t)xdr_ypresp_master, &yprm);
1023 	YPUNLOCK();
1024 	return (r);
1025 }
1026 
1027 int
1028 yp_maplist(char *indomain, struct ypmaplist **outmaplist)
1029 {
1030 	struct dom_binding *ysd;
1031 	struct ypresp_maplist ypml;
1032 	struct timeval tv;
1033 	int r;
1034 
1035 	/* Sanity check */
1036 
1037 	if (indomain == NULL || !strlen(indomain))
1038 		return (YPERR_BADARGS);
1039 
1040 	YPLOCK();
1041 again:
1042 	if (_yp_dobind(indomain, &ysd) != 0) {
1043 		YPUNLOCK();
1044 		return (YPERR_DOMAIN);
1045 	}
1046 
1047 	tv.tv_sec = _yplib_timeout;
1048 	tv.tv_usec = 0;
1049 
1050 	bzero((char *)&ypml, sizeof ypml);
1051 
1052 	r = clnt_call(ysd->dom_client, YPPROC_MAPLIST,
1053 		(xdrproc_t)xdr_domainname, &indomain,
1054 		(xdrproc_t)xdr_ypresp_maplist, &ypml,tv);
1055 	if (r != RPC_SUCCESS) {
1056 		clnt_perror(ysd->dom_client, "yp_maplist: clnt_call");
1057 		_yp_unbind(ysd);
1058 		goto again;
1059 	}
1060 	if (!(r = ypprot_err(ypml.stat))) {
1061 		*outmaplist = ypml.maps;
1062 	}
1063 
1064 	/* NO: xdr_free((xdrproc_t)xdr_ypresp_maplist, &ypml);*/
1065 	YPUNLOCK();
1066 	return (r);
1067 }
1068 
1069 const char *
1070 yperr_string(int incode)
1071 {
1072 	static char err[80];
1073 
1074 	switch (incode) {
1075 	case 0:
1076 		return ("Success");
1077 	case YPERR_BADARGS:
1078 		return ("Request arguments bad");
1079 	case YPERR_RPC:
1080 		return ("RPC failure");
1081 	case YPERR_DOMAIN:
1082 		return ("Can't bind to server which serves this domain");
1083 	case YPERR_MAP:
1084 		return ("No such map in server's domain");
1085 	case YPERR_KEY:
1086 		return ("No such key in map");
1087 	case YPERR_YPERR:
1088 		return ("YP server error");
1089 	case YPERR_RESRC:
1090 		return ("Local resource allocation failure");
1091 	case YPERR_NOMORE:
1092 		return ("No more records in map database");
1093 	case YPERR_PMAP:
1094 		return ("Can't communicate with portmapper");
1095 	case YPERR_YPBIND:
1096 		return ("Can't communicate with ypbind");
1097 	case YPERR_YPSERV:
1098 		return ("Can't communicate with ypserv");
1099 	case YPERR_NODOM:
1100 		return ("Local domain name not set");
1101 	case YPERR_BADDB:
1102 		return ("Server data base is bad");
1103 	case YPERR_VERS:
1104 		return ("YP server version mismatch - server can't supply service.");
1105 	case YPERR_ACCESS:
1106 		return ("Access violation");
1107 	case YPERR_BUSY:
1108 		return ("Database is busy");
1109 	}
1110 	sprintf(err, "YP unknown error %d\n", incode);
1111 	return (err);
1112 }
1113 
1114 int
1115 ypprot_err(unsigned int incode)
1116 {
1117 	switch (incode) {
1118 	case YP_TRUE:
1119 		return (0);
1120 	case YP_FALSE:
1121 		return (YPERR_YPBIND);
1122 	case YP_NOMORE:
1123 		return (YPERR_NOMORE);
1124 	case YP_NOMAP:
1125 		return (YPERR_MAP);
1126 	case YP_NODOM:
1127 		return (YPERR_DOMAIN);
1128 	case YP_NOKEY:
1129 		return (YPERR_KEY);
1130 	case YP_BADOP:
1131 		return (YPERR_YPERR);
1132 	case YP_BADDB:
1133 		return (YPERR_BADDB);
1134 	case YP_YPERR:
1135 		return (YPERR_YPERR);
1136 	case YP_BADARGS:
1137 		return (YPERR_BADARGS);
1138 	case YP_VERS:
1139 		return (YPERR_VERS);
1140 	}
1141 	return (YPERR_YPERR);
1142 }
1143 
1144 int
1145 _yp_check(char **dom)
1146 {
1147 	char *unused;
1148 
1149 	YPLOCK();
1150 	if (_yp_domain[0]=='\0')
1151 		if (yp_get_default_domain_locked(&unused)) {
1152 			YPUNLOCK();
1153 			return (0);
1154 		}
1155 
1156 	if (dom)
1157 		*dom = _yp_domain;
1158 
1159 	if (yp_bind_locked(_yp_domain) == 0) {
1160 		yp_unbind_locked(_yp_domain);
1161 		YPUNLOCK();
1162 		return (1);
1163 	}
1164 	YPUNLOCK();
1165 	return (0);
1166 }
1167