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