xref: /freebsd/lib/libc/yp/yplib.c (revision ceaec73d406831b1251babb61675df0a1aa54a31)
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 int
594 yp_bind(char *dom)
595 {
596 	return (_yp_dobind(dom, NULL));
597 }
598 
599 void
600 yp_unbind(char *dom)
601 {
602 	struct dom_binding *ypb, *ypbp;
603 
604 	ypbp = NULL;
605 	for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) {
606 		if (strcmp(dom, ypb->dom_domain) == 0) {
607 			_yp_unbind(ypb);
608 			if (ypbp)
609 				ypbp->dom_pnext = ypb->dom_pnext;
610 			else
611 				_ypbindlist = ypb->dom_pnext;
612 			free(ypb);
613 			return;
614 		}
615 		ypbp = ypb;
616 	}
617 	return;
618 }
619 
620 int
621 yp_match(char *indomain, char *inmap, const char *inkey, int inkeylen,
622     char **outval, int *outvallen)
623 {
624 	struct dom_binding *ysd;
625 	struct ypresp_val yprv;
626 	struct timeval tv;
627 	struct ypreq_key yprk;
628 	int r;
629 
630 	*outval = NULL;
631 	*outvallen = 0;
632 
633 	/* Sanity check */
634 
635 	if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
636 	    inmap == NULL || !strlen(inmap) ||
637 	    indomain == NULL || !strlen(indomain))
638 		return (YPERR_BADARGS);
639 
640 	if (_yp_dobind(indomain, &ysd) != 0)
641 		return(YPERR_DOMAIN);
642 
643 	yprk.domain = indomain;
644 	yprk.map = inmap;
645 	yprk.key.keydat_val = (char *)inkey;
646 	yprk.key.keydat_len = inkeylen;
647 
648 #ifdef YPMATCHCACHE
649 	if (ypmatch_cache_lookup(ysd, yprk.map, &yprk.key, &yprv.val) == TRUE) {
650 /*
651 	if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
652 	    inkeylen, &yprv.val.valdat_val, &yprv.val.valdat_len)) {
653 */
654 		*outvallen = yprv.val.valdat_len;
655 		*outval = (char *)malloc(*outvallen+1);
656 		bcopy(yprv.val.valdat_val, *outval, *outvallen);
657 		(*outval)[*outvallen] = '\0';
658 		return (0);
659 	}
660 #endif
661 
662 again:
663 	if (_yp_dobind(indomain, &ysd) != 0)
664 		return (YPERR_DOMAIN);
665 
666 	tv.tv_sec = _yplib_timeout;
667 	tv.tv_usec = 0;
668 
669 	bzero((char *)&yprv, sizeof yprv);
670 
671 	r = clnt_call(ysd->dom_client, YPPROC_MATCH,
672 		(xdrproc_t)xdr_ypreq_key, &yprk,
673 		(xdrproc_t)xdr_ypresp_val, &yprv, tv);
674 	if (r != RPC_SUCCESS) {
675 		clnt_perror(ysd->dom_client, "yp_match: clnt_call");
676 		_yp_unbind(ysd);
677 		goto again;
678 	}
679 
680 	if (!(r = ypprot_err(yprv.stat))) {
681 		*outvallen = yprv.val.valdat_len;
682 		*outval = (char *)malloc(*outvallen+1);
683 		bcopy(yprv.val.valdat_val, *outval, *outvallen);
684 		(*outval)[*outvallen] = '\0';
685 #ifdef YPMATCHCACHE
686 		ypmatch_cache_insert(ysd, yprk.map, &yprk.key, &yprv.val);
687 #endif
688 	}
689 
690 	xdr_free((xdrproc_t)xdr_ypresp_val, &yprv);
691 	return (r);
692 }
693 
694 static int
695 yp_get_default_domain_locked(char **domp)
696 {
697 	*domp = NULL;
698 	if (_yp_domain[0] == '\0')
699 		if (getdomainname(_yp_domain, sizeof _yp_domain))
700 			return (YPERR_NODOM);
701 	*domp = _yp_domain;
702 	return (0);
703 }
704 
705 int
706 yp_get_default_domain(char **domp)
707 {
708 	int r;
709 
710 	YPLOCK();
711 	r = yp_get_default_domain_locked(domp);
712 	YPUNLOCK();
713 	return (r);
714 }
715 
716 int
717 yp_first(char *indomain, char *inmap, char **outkey, int *outkeylen,
718     char **outval, int *outvallen)
719 {
720 	struct ypresp_key_val yprkv;
721 	struct ypreq_nokey yprnk;
722 	struct dom_binding *ysd;
723 	struct timeval tv;
724 	int r;
725 
726 	/* Sanity check */
727 
728 	if (indomain == NULL || !strlen(indomain) ||
729 	    inmap == NULL || !strlen(inmap))
730 		return (YPERR_BADARGS);
731 
732 	*outkey = *outval = NULL;
733 	*outkeylen = *outvallen = 0;
734 
735 again:
736 	if (_yp_dobind(indomain, &ysd) != 0)
737 		return (YPERR_DOMAIN);
738 
739 	tv.tv_sec = _yplib_timeout;
740 	tv.tv_usec = 0;
741 
742 	yprnk.domain = indomain;
743 	yprnk.map = inmap;
744 	bzero((char *)&yprkv, sizeof yprkv);
745 
746 	r = clnt_call(ysd->dom_client, YPPROC_FIRST,
747 		(xdrproc_t)xdr_ypreq_nokey, &yprnk,
748 		(xdrproc_t)xdr_ypresp_key_val, &yprkv, tv);
749 	if (r != RPC_SUCCESS) {
750 		clnt_perror(ysd->dom_client, "yp_first: clnt_call");
751 		_yp_unbind(ysd);
752 		goto again;
753 	}
754 	if (!(r = ypprot_err(yprkv.stat))) {
755 		*outkeylen = yprkv.key.keydat_len;
756 		*outkey = (char *)malloc(*outkeylen+1);
757 		bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
758 		(*outkey)[*outkeylen] = '\0';
759 		*outvallen = yprkv.val.valdat_len;
760 		*outval = (char *)malloc(*outvallen+1);
761 		bcopy(yprkv.val.valdat_val, *outval, *outvallen);
762 		(*outval)[*outvallen] = '\0';
763 	}
764 
765 	xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
766 	return (r);
767 }
768 
769 int
770 yp_next(char *indomain, char *inmap, char *inkey, int inkeylen,
771     char **outkey, int *outkeylen, char **outval, int *outvallen)
772 {
773 	struct ypresp_key_val yprkv;
774 	struct ypreq_key yprk;
775 	struct dom_binding *ysd;
776 	struct timeval tv;
777 	int r;
778 
779 	/* Sanity check */
780 
781 	if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
782 	    inmap == NULL || !strlen(inmap) ||
783 	    indomain == NULL || !strlen(indomain))
784 		return (YPERR_BADARGS);
785 
786 	*outkey = *outval = NULL;
787 	*outkeylen = *outvallen = 0;
788 
789 again:
790 	if (_yp_dobind(indomain, &ysd) != 0)
791 		return (YPERR_DOMAIN);
792 
793 	tv.tv_sec = _yplib_timeout;
794 	tv.tv_usec = 0;
795 
796 	yprk.domain = indomain;
797 	yprk.map = inmap;
798 	yprk.key.keydat_val = inkey;
799 	yprk.key.keydat_len = inkeylen;
800 	bzero((char *)&yprkv, sizeof yprkv);
801 
802 	r = clnt_call(ysd->dom_client, YPPROC_NEXT,
803 		(xdrproc_t)xdr_ypreq_key, &yprk,
804 		(xdrproc_t)xdr_ypresp_key_val, &yprkv, tv);
805 	if (r != RPC_SUCCESS) {
806 		clnt_perror(ysd->dom_client, "yp_next: clnt_call");
807 		_yp_unbind(ysd);
808 		goto again;
809 	}
810 	if (!(r = ypprot_err(yprkv.stat))) {
811 		*outkeylen = yprkv.key.keydat_len;
812 		*outkey = (char *)malloc(*outkeylen+1);
813 		bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
814 		(*outkey)[*outkeylen] = '\0';
815 		*outvallen = yprkv.val.valdat_len;
816 		*outval = (char *)malloc(*outvallen+1);
817 		bcopy(yprkv.val.valdat_val, *outval, *outvallen);
818 		(*outval)[*outvallen] = '\0';
819 	}
820 
821 	xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
822 	return (r);
823 }
824 
825 int
826 yp_all(char *indomain, char *inmap, struct ypall_callback *incallback)
827 {
828 	struct ypreq_nokey yprnk;
829 	struct dom_binding *ysd;
830 	struct timeval tv;
831 	struct sockaddr_in clnt_sin;
832 	CLIENT *clnt;
833 	u_long status, savstat;
834 	int clnt_sock;
835 
836 	/* Sanity check */
837 
838 	if (indomain == NULL || !strlen(indomain) ||
839 	    inmap == NULL || !strlen(inmap))
840 		return (YPERR_BADARGS);
841 
842 again:
843 
844 	if (_yp_dobind(indomain, &ysd) != 0)
845 		return (YPERR_DOMAIN);
846 
847 	tv.tv_sec = _yplib_timeout;
848 	tv.tv_usec = 0;
849 
850 	/* YPPROC_ALL manufactures its own channel to ypserv using TCP */
851 
852 	clnt_sock = RPC_ANYSOCK;
853 	clnt_sin = ysd->dom_server_addr;
854 	clnt_sin.sin_port = 0;
855 	clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0);
856 	if (clnt == NULL) {
857 		printf("clnttcp_create failed\n");
858 		return (YPERR_PMAP);
859 	}
860 
861 	yprnk.domain = indomain;
862 	yprnk.map = inmap;
863 	ypresp_allfn = incallback->foreach;
864 	ypresp_data = (void *)incallback->data;
865 
866 	if (clnt_call(clnt, YPPROC_ALL,
867 		(xdrproc_t)xdr_ypreq_nokey, &yprnk,
868 		(xdrproc_t)xdr_ypresp_all_seq, &status, tv) != RPC_SUCCESS) {
869 			clnt_perror(ysd->dom_client, "yp_all: clnt_call");
870 			clnt_destroy(clnt);
871 			_yp_unbind(ysd);
872 			goto again;
873 	}
874 
875 	clnt_destroy(clnt);
876 	savstat = status;
877 	xdr_free((xdrproc_t)xdr_ypresp_all_seq, &status);	/* not really needed... */
878 	if (savstat != YP_NOMORE)
879 		return (ypprot_err(savstat));
880 	return (0);
881 }
882 
883 int
884 yp_order(char *indomain, char *inmap, int *outorder)
885 {
886  	struct dom_binding *ysd;
887 	struct ypresp_order ypro;
888 	struct ypreq_nokey yprnk;
889 	struct timeval tv;
890 	int r;
891 
892 	/* Sanity check */
893 
894 	if (indomain == NULL || !strlen(indomain) ||
895 	    inmap == NULL || !strlen(inmap))
896 		return (YPERR_BADARGS);
897 
898 again:
899 	if (_yp_dobind(indomain, &ysd) != 0)
900 		return (YPERR_DOMAIN);
901 
902 	tv.tv_sec = _yplib_timeout;
903 	tv.tv_usec = 0;
904 
905 	yprnk.domain = indomain;
906 	yprnk.map = inmap;
907 
908 	bzero((char *)(char *)&ypro, sizeof ypro);
909 
910 	r = clnt_call(ysd->dom_client, YPPROC_ORDER,
911 		(xdrproc_t)xdr_ypreq_nokey, &yprnk,
912 		(xdrproc_t)xdr_ypresp_order, &ypro, tv);
913 
914 	/*
915 	 * NIS+ in YP compat mode doesn't support the YPPROC_ORDER
916 	 * procedure.
917 	 */
918 	if (r == RPC_PROCUNAVAIL) {
919 		return(YPERR_YPERR);
920 	}
921 
922 	if (r != RPC_SUCCESS) {
923 		clnt_perror(ysd->dom_client, "yp_order: clnt_call");
924 		_yp_unbind(ysd);
925 		goto again;
926 	}
927 
928 	if (!(r = ypprot_err(ypro.stat))) {
929 		*outorder = ypro.ordernum;
930 	}
931 
932 	xdr_free((xdrproc_t)xdr_ypresp_order, &ypro);
933 	return (r);
934 }
935 
936 int
937 yp_master(char *indomain, char *inmap, char **outname)
938 {
939 	struct dom_binding *ysd;
940 	struct ypresp_master yprm;
941 	struct ypreq_nokey yprnk;
942 	struct timeval tv;
943 	int r;
944 
945 	/* Sanity check */
946 
947 	if (indomain == NULL || !strlen(indomain) ||
948 	    inmap == NULL || !strlen(inmap))
949 		return (YPERR_BADARGS);
950 again:
951 	if (_yp_dobind(indomain, &ysd) != 0)
952 		return (YPERR_DOMAIN);
953 
954 	tv.tv_sec = _yplib_timeout;
955 	tv.tv_usec = 0;
956 
957 	yprnk.domain = indomain;
958 	yprnk.map = inmap;
959 
960 	bzero((char *)&yprm, sizeof yprm);
961 
962 	r = clnt_call(ysd->dom_client, YPPROC_MASTER,
963 		(xdrproc_t)xdr_ypreq_nokey, &yprnk,
964 		(xdrproc_t)xdr_ypresp_master, &yprm, tv);
965 	if (r != RPC_SUCCESS) {
966 		clnt_perror(ysd->dom_client, "yp_master: clnt_call");
967 		_yp_unbind(ysd);
968 		goto again;
969 	}
970 
971 	if (!(r = ypprot_err(yprm.stat))) {
972 		*outname = (char *)strdup(yprm.peer);
973 	}
974 
975 	xdr_free((xdrproc_t)xdr_ypresp_master, &yprm);
976 	return (r);
977 }
978 
979 int
980 yp_maplist(char *indomain, struct ypmaplist **outmaplist)
981 {
982 	struct dom_binding *ysd;
983 	struct ypresp_maplist ypml;
984 	struct timeval tv;
985 	int r;
986 
987 	/* Sanity check */
988 
989 	if (indomain == NULL || !strlen(indomain))
990 		return (YPERR_BADARGS);
991 
992 again:
993 	if (_yp_dobind(indomain, &ysd) != 0)
994 		return (YPERR_DOMAIN);
995 
996 	tv.tv_sec = _yplib_timeout;
997 	tv.tv_usec = 0;
998 
999 	bzero((char *)&ypml, sizeof ypml);
1000 
1001 	r = clnt_call(ysd->dom_client, YPPROC_MAPLIST,
1002 		(xdrproc_t)xdr_domainname, &indomain,
1003 		(xdrproc_t)xdr_ypresp_maplist, &ypml,tv);
1004 	if (r != RPC_SUCCESS) {
1005 		clnt_perror(ysd->dom_client, "yp_maplist: clnt_call");
1006 		_yp_unbind(ysd);
1007 		goto again;
1008 	}
1009 	if (!(r = ypprot_err(ypml.stat))) {
1010 		*outmaplist = ypml.maps;
1011 	}
1012 
1013 	/* NO: xdr_free((xdrproc_t)xdr_ypresp_maplist, &ypml);*/
1014 	return (r);
1015 }
1016 
1017 const char *
1018 yperr_string(int incode)
1019 {
1020 	static char err[80];
1021 
1022 	switch (incode) {
1023 	case 0:
1024 		return ("Success");
1025 	case YPERR_BADARGS:
1026 		return ("Request arguments bad");
1027 	case YPERR_RPC:
1028 		return ("RPC failure");
1029 	case YPERR_DOMAIN:
1030 		return ("Can't bind to server which serves this domain");
1031 	case YPERR_MAP:
1032 		return ("No such map in server's domain");
1033 	case YPERR_KEY:
1034 		return ("No such key in map");
1035 	case YPERR_YPERR:
1036 		return ("YP server error");
1037 	case YPERR_RESRC:
1038 		return ("Local resource allocation failure");
1039 	case YPERR_NOMORE:
1040 		return ("No more records in map database");
1041 	case YPERR_PMAP:
1042 		return ("Can't communicate with portmapper");
1043 	case YPERR_YPBIND:
1044 		return ("Can't communicate with ypbind");
1045 	case YPERR_YPSERV:
1046 		return ("Can't communicate with ypserv");
1047 	case YPERR_NODOM:
1048 		return ("Local domain name not set");
1049 	case YPERR_BADDB:
1050 		return ("Server data base is bad");
1051 	case YPERR_VERS:
1052 		return ("YP server version mismatch - server can't supply service.");
1053 	case YPERR_ACCESS:
1054 		return ("Access violation");
1055 	case YPERR_BUSY:
1056 		return ("Database is busy");
1057 	}
1058 	sprintf(err, "YP unknown error %d\n", incode);
1059 	return (err);
1060 }
1061 
1062 int
1063 ypprot_err(unsigned int incode)
1064 {
1065 	switch (incode) {
1066 	case YP_TRUE:
1067 		return (0);
1068 	case YP_FALSE:
1069 		return (YPERR_YPBIND);
1070 	case YP_NOMORE:
1071 		return (YPERR_NOMORE);
1072 	case YP_NOMAP:
1073 		return (YPERR_MAP);
1074 	case YP_NODOM:
1075 		return (YPERR_DOMAIN);
1076 	case YP_NOKEY:
1077 		return (YPERR_KEY);
1078 	case YP_BADOP:
1079 		return (YPERR_YPERR);
1080 	case YP_BADDB:
1081 		return (YPERR_BADDB);
1082 	case YP_YPERR:
1083 		return (YPERR_YPERR);
1084 	case YP_BADARGS:
1085 		return (YPERR_BADARGS);
1086 	case YP_VERS:
1087 		return (YPERR_VERS);
1088 	}
1089 	return (YPERR_YPERR);
1090 }
1091 
1092 int
1093 _yp_check(char **dom)
1094 {
1095 	char *unused;
1096 
1097 	YPLOCK();
1098 	if (_yp_domain[0]=='\0')
1099 		if (yp_get_default_domain_locked(&unused)) {
1100 			YPUNLOCK();
1101 			return (0);
1102 		}
1103 
1104 	if (dom)
1105 		*dom = _yp_domain;
1106 
1107 	if (yp_bind(_yp_domain) == 0) {
1108 		yp_unbind(_yp_domain);
1109 		YPUNLOCK();
1110 		return (1);
1111 	}
1112 	YPUNLOCK();
1113 	return (0);
1114 }
1115