xref: /freebsd/usr.sbin/ypserv/yp_server.c (revision 84943d6f38e936ac3b7a3947ca26eeb27a39f938)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1995
5  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Bill Paul.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  */
35 
36 #include <sys/cdefs.h>
37 #include "yp.h"
38 #include "yp_extern.h"
39 #include <dirent.h>
40 #include <errno.h>
41 #include <stdlib.h>
42 #include <sys/stat.h>
43 #include <sys/param.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <rpc/rpc.h>
49 
50 int children = 0;
51 
52 #define	MASTER_STRING	"YP_MASTER_NAME"
53 #define	MASTER_SZ	sizeof(MASTER_STRING) - 1
54 #define	ORDER_STRING	"YP_LAST_MODIFIED"
55 #define	ORDER_SZ	sizeof(ORDER_STRING) - 1
56 
57 static pid_t
58 yp_fork(void)
59 {
60 	if (yp_pid != getpid()) {
61 		yp_error("child %d trying to fork!", getpid());
62 		errno = EEXIST;
63 		return(-1);
64 	}
65 
66 	return(fork());
67 }
68 
69 /*
70  * NIS v2 support. This is where most of the action happens.
71  */
72 
73 void *
74 ypproc_null_2_svc(void *argp, struct svc_req *rqstp)
75 {
76 	static char * result;
77 	static char rval = 0;
78 
79 #ifdef DB_CACHE
80 	if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
81 #else
82 	if (yp_access(NULL, (struct svc_req *)rqstp))
83 #endif
84 		return(NULL);
85 
86 	result = &rval;
87 
88 	return((void *) &result);
89 }
90 
91 bool_t *
92 ypproc_domain_2_svc(domainname *argp, struct svc_req *rqstp)
93 {
94 	static bool_t  result;
95 
96 #ifdef DB_CACHE
97 	if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) {
98 #else
99 	if (yp_access(NULL, (struct svc_req *)rqstp)) {
100 #endif
101 		result = FALSE;
102 		return (&result);
103 	}
104 
105 	if (argp == NULL || yp_validdomain(*argp))
106 		result = FALSE;
107 	else
108 		result = TRUE;
109 
110 	return (&result);
111 }
112 
113 bool_t *
114 ypproc_domain_nonack_2_svc(domainname *argp, struct svc_req *rqstp)
115 {
116 	static bool_t  result;
117 
118 #ifdef DB_CACHE
119 	if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
120 #else
121 	if (yp_access(NULL, (struct svc_req *)rqstp))
122 #endif
123 		return (NULL);
124 
125 	if (argp == NULL || yp_validdomain(*argp))
126 		return (NULL);
127 	else
128 		result = TRUE;
129 
130 	return (&result);
131 }
132 
133 ypresp_val *
134 ypproc_match_2_svc(ypreq_key *argp, struct svc_req *rqstp)
135 {
136 	static ypresp_val  result;
137 
138 	result.val.valdat_val = "";
139 	result.val.valdat_len = 0;
140 
141 #ifdef DB_CACHE
142 	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
143 #else
144 	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
145 #endif
146 		result.stat = YP_YPERR;
147 		return (&result);
148 	}
149 
150 	if (argp->domain == NULL || argp->map == NULL) {
151 		result.stat = YP_BADARGS;
152 		return (&result);
153 	}
154 
155 	if (yp_select_map(argp->map, argp->domain, NULL, 1) != YP_TRUE) {
156 		result.stat = yp_errno;
157 		return(&result);
158 	}
159 
160 	result.stat = yp_getbykey(&argp->key, &result.val);
161 
162 	/*
163 	 * Do DNS lookups for hosts maps if database lookup failed.
164 	 */
165 
166 #ifdef DB_CACHE
167 	if (do_dns && result.stat != YP_TRUE &&
168 	    (yp_testflag(argp->map, argp->domain, YP_INTERDOMAIN) ||
169 	    (strstr(argp->map, "hosts") || strstr(argp->map, "ipnodes")))) {
170 #else
171 	if (do_dns && result.stat != YP_TRUE &&
172 	    (strstr(argp->map, "hosts") || strstr(argp->map, "ipnodes"))) {
173 #endif
174 		char *nbuf;
175 
176 		nbuf = alloca(argp->key.keydat_len + 1);
177 		/* NUL terminate! NUL terminate!! NUL TERMINATE!!! */
178 		bcopy(argp->key.keydat_val, nbuf, argp->key.keydat_len);
179 		nbuf[argp->key.keydat_len] = '\0';
180 
181 		if (debug)
182 			yp_error("doing DNS lookup of %s", nbuf);
183 
184 		if (!strcmp(argp->map, "hosts.byname"))
185 			result.stat = yp_async_lookup_name(rqstp, nbuf,
186 			    AF_INET);
187 		else if (!strcmp(argp->map, "hosts.byaddr"))
188 			result.stat = yp_async_lookup_addr(rqstp, nbuf,
189 			    AF_INET);
190 		else if (!strcmp(argp->map, "ipnodes.byname"))
191 			result.stat = yp_async_lookup_name(rqstp, nbuf,
192 			    AF_INET6);
193 		else if (!strcmp(argp->map, "ipnodes.byaddr"))
194 			result.stat = yp_async_lookup_addr(rqstp, nbuf,
195 			    AF_INET6);
196 
197 		if (result.stat == YP_TRUE)
198 			return(NULL);
199 	}
200 
201 	return (&result);
202 }
203 
204 ypresp_key_val *
205 ypproc_first_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
206 {
207 	static ypresp_key_val  result;
208 
209 	result.val.valdat_val = result.key.keydat_val = "";
210 	result.val.valdat_len = result.key.keydat_len = 0;
211 
212 #ifdef DB_CACHE
213 	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
214 #else
215 	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
216 #endif
217 		result.stat = YP_YPERR;
218 		return (&result);
219 	}
220 
221 	if (argp->domain == NULL) {
222 		result.stat = YP_BADARGS;
223 		return (&result);
224 	}
225 
226 	if (yp_select_map(argp->map, argp->domain, NULL, 0) != YP_TRUE) {
227 		result.stat = yp_errno;
228 		return(&result);
229 	}
230 
231 	result.stat = yp_firstbykey(&result.key, &result.val);
232 
233 	return (&result);
234 }
235 
236 ypresp_key_val *
237 ypproc_next_2_svc(ypreq_key *argp, struct svc_req *rqstp)
238 {
239 	static ypresp_key_val  result;
240 
241 	result.val.valdat_val = result.key.keydat_val = "";
242 	result.val.valdat_len = result.key.keydat_len = 0;
243 
244 #ifdef DB_CACHE
245 	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
246 #else
247 	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
248 #endif
249 		result.stat = YP_YPERR;
250 		return (&result);
251 	}
252 
253 	if (argp->domain == NULL || argp->map == NULL) {
254 		result.stat = YP_BADARGS;
255 		return (&result);
256 	}
257 
258 	if (yp_select_map(argp->map, argp->domain, &argp->key, 0) != YP_TRUE) {
259 		result.stat = yp_errno;
260 		return(&result);
261 	}
262 
263 	result.key.keydat_len = argp->key.keydat_len;
264 	result.key.keydat_val = argp->key.keydat_val;
265 
266 	result.stat = yp_nextbykey(&result.key, &result.val);
267 
268 	return (&result);
269 }
270 
271 static void
272 ypxfr_callback(ypxfrstat rval, struct sockaddr_in *addr, unsigned int transid,
273     unsigned int prognum, unsigned long port)
274 {
275 	CLIENT *clnt;
276 	int sock = RPC_ANYSOCK;
277 	struct timeval timeout;
278 	yppushresp_xfr ypxfr_resp;
279 	struct rpc_err err;
280 
281 	timeout.tv_sec = 5;
282 	timeout.tv_usec = 0;
283 	addr->sin_port = htons(port);
284 
285 	if ((clnt = clntudp_create(addr,prognum,1,timeout,&sock)) == NULL) {
286 		yp_error("%s: %s", inet_ntoa(addr->sin_addr),
287 		  clnt_spcreateerror("failed to establish callback handle"));
288 		return;
289 	}
290 
291 	ypxfr_resp.status = rval;
292 	ypxfr_resp.transid = transid;
293 
294 	/* Turn the timeout off -- we don't want to block. */
295 	timeout.tv_sec = 0;
296 	if (clnt_control(clnt, CLSET_TIMEOUT, &timeout) == FALSE)
297 		yp_error("failed to set timeout on ypproc_xfr callback");
298 
299 	if (yppushproc_xfrresp_1(&ypxfr_resp, clnt) == NULL) {
300 		clnt_geterr(clnt, &err);
301 		if (err.re_status != RPC_SUCCESS &&
302 		    err.re_status != RPC_TIMEDOUT)
303 			yp_error("%s", clnt_sperror(clnt,
304 				"ypxfr callback failed"));
305 	}
306 
307 	clnt_destroy(clnt);
308 }
309 
310 #define YPXFR_RETURN(CODE) 						\
311 	/* Order is important: send regular RPC reply, then callback */	\
312 	result.xfrstat = CODE; 						\
313 	svc_sendreply(rqstp->rq_xprt, (xdrproc_t)xdr_ypresp_xfr, &result); \
314 	ypxfr_callback(CODE,rqhost,argp->transid, 			\
315 					argp->prog,argp->port); 	\
316 	return(NULL);
317 
318 ypresp_xfr *
319 ypproc_xfr_2_svc(ypreq_xfr *argp, struct svc_req *rqstp)
320 {
321 	static ypresp_xfr  result;
322 	struct sockaddr_in *rqhost;
323 	ypresp_master *mres;
324 	ypreq_nokey mreq;
325 
326 	result.transid = argp->transid;
327 	rqhost = svc_getcaller(rqstp->rq_xprt);
328 
329 #ifdef DB_CACHE
330 	if (yp_access(argp->map_parms.map,
331 			argp->map_parms.domain, (struct svc_req *)rqstp)) {
332 #else
333 	if (yp_access(argp->map_parms.map, (struct svc_req *)rqstp)) {
334 #endif
335 		YPXFR_RETURN(YPXFR_REFUSED)
336 	}
337 
338 
339 	if (argp->map_parms.domain == NULL) {
340 		YPXFR_RETURN(YPXFR_BADARGS)
341 	}
342 
343 	if (yp_validdomain(argp->map_parms.domain)) {
344 		YPXFR_RETURN(YPXFR_NODOM)
345 	}
346 
347 	/*
348 	 * Determine the master host ourselves. The caller may
349 	 * be up to no good. This has the side effect of verifying
350 	 * that the requested map and domain actually exist.
351 	 */
352 
353 	mreq.domain = argp->map_parms.domain;
354 	mreq.map = argp->map_parms.map;
355 
356 	mres = ypproc_master_2_svc(&mreq, rqstp);
357 
358 	if (mres->stat != YP_TRUE) {
359 		yp_error("couldn't find master for map %s@%s",
360 						argp->map_parms.map,
361 						argp->map_parms.domain);
362 		yp_error("host at %s (%s) may be pulling my leg",
363 						argp->map_parms.peer,
364 						inet_ntoa(rqhost->sin_addr));
365 		YPXFR_RETURN(YPXFR_REFUSED)
366 	}
367 
368 	switch (yp_fork()) {
369 	case 0:
370 	{
371 		char g[11], t[11], p[11];
372 		char ypxfr_command[MAXPATHLEN + 2];
373 
374 		snprintf (ypxfr_command, sizeof(ypxfr_command), "%sypxfr", _PATH_LIBEXEC);
375 		snprintf (t, sizeof(t), "%u", argp->transid);
376 		snprintf (g, sizeof(g), "%u", argp->prog);
377 		snprintf (p, sizeof(p), "%u", argp->port);
378 		if (debug) {
379 			close(0); close(1); close(2);
380 		}
381 		if (strcmp(yp_dir, _PATH_YP)) {
382 			execl(ypxfr_command, "ypxfr",
383 			"-d", argp->map_parms.domain,
384 		      	"-h", mres->peer,
385 			"-p", yp_dir, "-C", t,
386 		      	g, inet_ntoa(rqhost->sin_addr),
387 			p, argp->map_parms.map,
388 		      	NULL);
389 		} else {
390 			execl(ypxfr_command, "ypxfr",
391 			"-d", argp->map_parms.domain,
392 		      	"-h", mres->peer,
393 			"-C", t,
394 		      	g, inet_ntoa(rqhost->sin_addr),
395 			p, argp->map_parms.map,
396 		      	NULL);
397 		}
398 		yp_error("ypxfr execl(%s): %s", ypxfr_command, strerror(errno));
399 		YPXFR_RETURN(YPXFR_XFRERR)
400 		/*
401 		 * Just to safe, prevent PR #10970 from biting us in
402 		 * the unlikely case that execing ypxfr fails. We don't
403 		 * want to have any child processes spawned from this
404 		 * child process.
405 		 */
406 		_exit(0);
407 		break;
408 	}
409 	case -1:
410 		yp_error("ypxfr fork(): %s", strerror(errno));
411 		YPXFR_RETURN(YPXFR_XFRERR)
412 		break;
413 	default:
414 		result.xfrstat = YPXFR_SUCC;
415 		children++;
416 		break;
417 	}
418 
419 	return (&result);
420 }
421 #undef YPXFR_RETURN
422 
423 void *
424 ypproc_clear_2_svc(void *argp, struct svc_req *rqstp)
425 {
426 	static char * result;
427 	static char rval = 0;
428 
429 #ifdef DB_CACHE
430 	if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
431 #else
432 	if (yp_access(NULL, (struct svc_req *)rqstp))
433 #endif
434 		return (NULL);
435 #ifdef DB_CACHE
436 	/* clear out the database cache */
437 	yp_flush_all();
438 #endif
439 	/* Re-read the securenets database for the hell of it. */
440 	load_securenets();
441 
442 	result = &rval;
443 	return((void *) &result);
444 }
445 
446 /*
447  * For ypproc_all, we have to send a stream of ypresp_all structures
448  * via TCP, but the XDR filter generated from the yp.x protocol
449  * definition file only serializes one such structure. This means that
450  * to send the whole stream, you need a wrapper which feeds all the
451  * records into the underlying XDR routine until it hits an 'EOF.'
452  * But to use the wrapper, you have to violate the boundaries between
453  * RPC layers by calling svc_sendreply() directly from the ypproc_all
454  * service routine instead of letting the RPC dispatcher do it.
455  *
456  * Bleah.
457  */
458 
459 /*
460  * Custom XDR routine for serialzing results of ypproc_all: keep
461  * reading from the database and spew until we run out of records
462  * or encounter an error.
463  */
464 static bool_t
465 xdr_my_ypresp_all(register XDR *xdrs, ypresp_all *objp)
466 {
467 	while (1) {
468 		/* Get a record. */
469 		if ((objp->ypresp_all_u.val.stat =
470 			yp_nextbykey(&objp->ypresp_all_u.val.key,
471 				     &objp->ypresp_all_u.val.val)) == YP_TRUE) {
472 			objp->more = TRUE;
473 		} else {
474 			objp->more = FALSE;
475 		}
476 
477 		/* Serialize. */
478 		if (!xdr_ypresp_all(xdrs, objp))
479 			return(FALSE);
480 		if (objp->more == FALSE)
481 			return(TRUE);
482 	}
483 }
484 
485 ypresp_all *
486 ypproc_all_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
487 {
488 	static ypresp_all  result;
489 
490 	/*
491 	 * Set this here so that the client will be forced to make
492 	 * at least one attempt to read from us even if all we're
493 	 * doing is returning an error.
494 	 */
495 	result.more = TRUE;
496 	result.ypresp_all_u.val.key.keydat_len = 0;
497 	result.ypresp_all_u.val.key.keydat_val = "";
498 
499 #ifdef DB_CACHE
500 	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
501 #else
502 	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
503 #endif
504 		result.ypresp_all_u.val.stat = YP_YPERR;
505 		return (&result);
506 	}
507 
508 	if (argp->domain == NULL || argp->map == NULL) {
509 		result.ypresp_all_u.val.stat = YP_BADARGS;
510 		return (&result);
511 	}
512 
513 	/*
514 	 * XXX If we hit the child limit, fail the request.
515 	 * If we don't, and the map is large, we could block for
516 	 * a long time in the parent.
517 	 */
518 	if (children >= MAX_CHILDREN) {
519 		result.ypresp_all_u.val.stat = YP_YPERR;
520 		return(&result);
521 	}
522 
523 	/*
524 	 * The ypproc_all procedure can take a while to complete.
525 	 * Best to handle it in a subprocess so the parent doesn't
526 	 * block. (Is there a better way to do this? Maybe with
527 	 * async socket I/O?)
528 	 */
529 	if (!debug) {
530 		switch (yp_fork()) {
531 		case 0:
532 			break;
533 		case -1:
534 			yp_error("ypall fork(): %s", strerror(errno));
535 			result.ypresp_all_u.val.stat = YP_YPERR;
536 			return(&result);
537 			break;
538 		default:
539 			children++;
540 			return (NULL);
541 			break;
542 		}
543 	}
544 
545 	/*
546 	 * Fix for PR #10971: don't let the child ypserv share
547 	 * DB handles with the parent process.
548 	 */
549 #ifdef DB_CACHE
550 	yp_flush_all();
551 #endif
552 
553 	if (yp_select_map(argp->map, argp->domain,
554 				&result.ypresp_all_u.val.key, 0) != YP_TRUE) {
555 		result.ypresp_all_u.val.stat = yp_errno;
556 		return(&result);
557 	}
558 
559 	/* Kick off the actual data transfer. */
560 	svc_sendreply(rqstp->rq_xprt, (xdrproc_t)xdr_my_ypresp_all, &result);
561 
562 	/*
563 	 * Proper fix for PR #10970: exit here so that we don't risk
564 	 * having a child spawned from this sub-process.
565 	 */
566 	if (!debug)
567 		_exit(0);
568 
569 	return &result;
570 }
571 
572 ypresp_master *
573 ypproc_master_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
574 {
575 	static ypresp_master  result;
576 	static char ypvalbuf[YPMAXRECORD];
577 	keydat key = { MASTER_SZ, MASTER_STRING };
578 	valdat val;
579 
580 	result.peer = "";
581 
582 #ifdef DB_CACHE
583 	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
584 #else
585 	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
586 #endif
587 		result.stat = YP_YPERR;
588 		return(&result);
589 	}
590 
591 	if (argp->domain == NULL) {
592 		result.stat = YP_BADARGS;
593 		return (&result);
594 	}
595 
596 	if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) {
597 		result.stat = yp_errno;
598 		return(&result);
599 	}
600 
601 	/*
602 	 * Note that we copy the data retrieved from the database to
603 	 * a private buffer and NUL terminate the buffer rather than
604 	 * terminating the data in place. We do this because by stuffing
605 	 * a '\0' into data.data, we will actually be corrupting memory
606 	 * allocated by the DB package. This is a bad thing now that we
607 	 * cache DB handles rather than closing the database immediately.
608 	 */
609 	result.stat = yp_getbykey(&key, &val);
610 	if (result.stat == YP_TRUE) {
611 		bcopy(val.valdat_val, &ypvalbuf, val.valdat_len);
612 		ypvalbuf[val.valdat_len] = '\0';
613 		result.peer = ypvalbuf;
614 	} else
615 		result.peer = "";
616 
617 	return (&result);
618 }
619 
620 ypresp_order *
621 ypproc_order_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
622 {
623 	static ypresp_order  result;
624 	keydat key = { ORDER_SZ, ORDER_STRING };
625 	valdat val;
626 
627 	result.ordernum = 0;
628 
629 #ifdef DB_CACHE
630 	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
631 #else
632 	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
633 #endif
634 		result.stat = YP_YPERR;
635 		return(&result);
636 	}
637 
638 	if (argp->domain == NULL) {
639 		result.stat = YP_BADARGS;
640 		return (&result);
641 	}
642 
643 	/*
644 	 * We could just check the timestamp on the map file,
645 	 * but that's a hack: we'll only know the last time the file
646 	 * was touched, not the last time the database contents were
647 	 * updated.
648 	 */
649 
650 	if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) {
651 		result.stat = yp_errno;
652 		return(&result);
653 	}
654 
655 	result.stat = yp_getbykey(&key, &val);
656 
657 	if (result.stat == YP_TRUE)
658 		result.ordernum = atoi(val.valdat_val);
659 	else
660 		result.ordernum = 0;
661 
662 	return (&result);
663 }
664 
665 static void yp_maplist_free(struct ypmaplist *yp_maplist)
666 {
667 	register struct ypmaplist *next;
668 
669 	while (yp_maplist) {
670 		next = yp_maplist->next;
671 		free(yp_maplist->map);
672 		free(yp_maplist);
673 		yp_maplist = next;
674 	}
675 }
676 
677 static struct ypmaplist *
678 yp_maplist_create(const char *domain)
679 {
680 	char yp_mapdir[MAXPATHLEN + 2];
681 	char yp_mapname[MAXPATHLEN + 2];
682 	struct ypmaplist *cur = NULL;
683 	struct ypmaplist *yp_maplist = NULL;
684 	DIR *dird;
685 	struct dirent *dirp;
686 	struct stat statbuf;
687 
688 	snprintf(yp_mapdir, sizeof(yp_mapdir), "%s/%s", yp_dir, domain);
689 
690 	if ((dird = opendir(yp_mapdir)) == NULL) {
691 		yp_error("opendir(%s) failed: %s", yp_mapdir, strerror(errno));
692 		return(NULL);
693 	}
694 
695 	while ((dirp = readdir(dird)) != NULL) {
696 		if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
697 			snprintf(yp_mapname, sizeof(yp_mapname), "%s/%s",
698 							yp_mapdir,dirp->d_name);
699 			if (stat(yp_mapname, &statbuf) < 0 ||
700 						!S_ISREG(statbuf.st_mode))
701 				continue;
702 			if ((cur = (struct ypmaplist *)
703 				malloc(sizeof(struct ypmaplist))) == NULL) {
704 				yp_error("malloc() failed");
705 				closedir(dird);
706 				yp_maplist_free(yp_maplist);
707 				return(NULL);
708 			}
709 			if ((cur->map = strdup(dirp->d_name)) == NULL) {
710 				yp_error("strdup() failed: %s",strerror(errno));
711 				closedir(dird);
712 				yp_maplist_free(yp_maplist);
713 				free(cur);
714 				return(NULL);
715 			}
716 			cur->next = yp_maplist;
717 			yp_maplist = cur;
718 			if (debug)
719 				yp_error("map: %s", yp_maplist->map);
720 		}
721 
722 	}
723 	closedir(dird);
724 	return(yp_maplist);
725 }
726 
727 ypresp_maplist *
728 ypproc_maplist_2_svc(domainname *argp, struct svc_req *rqstp)
729 {
730 	static ypresp_maplist  result = { 0, NULL };
731 
732 #ifdef DB_CACHE
733 	if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) {
734 #else
735 	if (yp_access(NULL, (struct svc_req *)rqstp)) {
736 #endif
737 		result.stat = YP_YPERR;
738 		return(&result);
739 	}
740 
741 	if (argp == NULL) {
742 		result.stat = YP_BADARGS;
743 		return (&result);
744 	}
745 
746 	if (yp_validdomain(*argp)) {
747 		result.stat = YP_NODOM;
748 		return (&result);
749 	}
750 
751 	/*
752 	 * We have to construct a linked list for the ypproc_maplist
753 	 * procedure using dynamically allocated memory. Since the XDR
754 	 * layer won't free this list for us, we have to deal with it
755 	 * ourselves. We call yp_maplist_free() first to free any
756 	 * previously allocated data we may have accumulated to insure
757 	 * that we have only one linked list in memory at any given
758 	 * time.
759 	 */
760 
761 	yp_maplist_free(result.maps);
762 
763 	if ((result.maps = yp_maplist_create(*argp)) == NULL) {
764 		yp_error("yp_maplist_create failed");
765 		result.stat = YP_YPERR;
766 		return(&result);
767 	} else
768 		result.stat = YP_TRUE;
769 
770 	return (&result);
771 }
772 
773 /*
774  * NIS v1 support. The nullproc, domain and domain_nonack
775  * functions from v1 are identical to those in v2, so all
776  * we have to do is hand off to them.
777  *
778  * The other functions are mostly just wrappers around their v2
779  * counterparts. For example, for the v1 'match' procedure, we
780  * crack open the argument structure, make a request to the v2
781  * 'match' function, repackage the data into a v1 response and
782  * then send it on its way.
783  *
784  * Note that we don't support the pull, push and get procedures.
785  * There's little documentation available to show what they
786  * do, and I suspect they're meant largely for map transfers
787  * between master and slave servers.
788  */
789 
790 void *
791 ypoldproc_null_1_svc(void *argp, struct svc_req *rqstp)
792 {
793 	return(ypproc_null_2_svc(argp, rqstp));
794 }
795 
796 bool_t *
797 ypoldproc_domain_1_svc(domainname *argp, struct svc_req *rqstp)
798 {
799 	return(ypproc_domain_2_svc(argp, rqstp));
800 }
801 
802 bool_t *
803 ypoldproc_domain_nonack_1_svc(domainname *argp, struct svc_req *rqstp)
804 {
805 	return (ypproc_domain_nonack_2_svc(argp, rqstp));
806 }
807 
808 /*
809  * the 'match' procedure sends a response of type YPRESP_VAL
810  */
811 ypresponse *
812 ypoldproc_match_1_svc(yprequest *argp, struct svc_req *rqstp)
813 {
814 	static ypresponse  result;
815 	ypresp_val *v2_result;
816 
817 	result.yp_resptype = YPRESP_VAL;
818 	result.ypresponse_u.yp_resp_valtype.val.valdat_val = "";
819 	result.ypresponse_u.yp_resp_valtype.val.valdat_len = 0;
820 
821 	if (argp->yp_reqtype != YPREQ_KEY) {
822 		result.ypresponse_u.yp_resp_valtype.stat = YP_BADARGS;
823 		return(&result);
824 	}
825 
826 	v2_result = ypproc_match_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp);
827 	if (v2_result == NULL)
828 		return(NULL);
829 
830 	bcopy(v2_result, &result.ypresponse_u.yp_resp_valtype,
831 	      sizeof(ypresp_val));
832 
833 	return (&result);
834 }
835 
836 /*
837  * the 'first' procedure sends a response of type YPRESP_KEY_VAL
838  */
839 ypresponse *
840 ypoldproc_first_1_svc(yprequest *argp, struct svc_req *rqstp)
841 {
842 	static ypresponse  result;
843 	ypresp_key_val *v2_result;
844 
845 	result.yp_resptype = YPRESP_KEY_VAL;
846 	result.ypresponse_u.yp_resp_key_valtype.val.valdat_val =
847 	result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = "";
848 	result.ypresponse_u.yp_resp_key_valtype.val.valdat_len =
849 	result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0;
850 
851 	if (argp->yp_reqtype != YPREQ_NOKEY) {
852 		result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS;
853 		return(&result);
854 	}
855 
856 	v2_result = ypproc_first_2_svc(&argp->yprequest_u.yp_req_nokeytype,
857 									rqstp);
858 	if (v2_result == NULL)
859 		return(NULL);
860 
861 	bcopy(v2_result, &result.ypresponse_u.yp_resp_key_valtype,
862 	      sizeof(ypresp_key_val));
863 
864 	return (&result);
865 }
866 
867 /*
868  * the 'next' procedure sends a response of type YPRESP_KEY_VAL
869  */
870 ypresponse *
871 ypoldproc_next_1_svc(yprequest *argp, struct svc_req *rqstp)
872 {
873 	static ypresponse  result;
874 	ypresp_key_val *v2_result;
875 
876 	result.yp_resptype = YPRESP_KEY_VAL;
877 	result.ypresponse_u.yp_resp_key_valtype.val.valdat_val =
878 	result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = "";
879 	result.ypresponse_u.yp_resp_key_valtype.val.valdat_len =
880 	result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0;
881 
882 	if (argp->yp_reqtype != YPREQ_KEY) {
883 		result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS;
884 		return(&result);
885 	}
886 
887 	v2_result = ypproc_next_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp);
888 	if (v2_result == NULL)
889 		return(NULL);
890 
891 	bcopy(v2_result, &result.ypresponse_u.yp_resp_key_valtype,
892 	      sizeof(ypresp_key_val));
893 
894 	return (&result);
895 }
896 
897 /*
898  * the 'poll' procedure sends a response of type YPRESP_MAP_PARMS
899  */
900 ypresponse *
901 ypoldproc_poll_1_svc(yprequest *argp, struct svc_req *rqstp)
902 {
903 	static ypresponse  result;
904 	ypresp_master *v2_result1;
905 	ypresp_order *v2_result2;
906 
907 	result.yp_resptype = YPRESP_MAP_PARMS;
908 	result.ypresponse_u.yp_resp_map_parmstype.domain =
909 		argp->yprequest_u.yp_req_nokeytype.domain;
910 	result.ypresponse_u.yp_resp_map_parmstype.map =
911 		argp->yprequest_u.yp_req_nokeytype.map;
912 	/*
913 	 * Hmm... there is no 'status' value in the
914 	 * yp_resp_map_parmstype structure, so I have to
915 	 * guess at what to do to indicate a failure.
916 	 * I hope this is right.
917 	 */
918 	result.ypresponse_u.yp_resp_map_parmstype.ordernum = 0;
919 	result.ypresponse_u.yp_resp_map_parmstype.peer = "";
920 
921 	if (argp->yp_reqtype != YPREQ_MAP_PARMS) {
922 		return(&result);
923 	}
924 
925 	v2_result1 = ypproc_master_2_svc(&argp->yprequest_u.yp_req_nokeytype,
926 									rqstp);
927 	if (v2_result1 == NULL)
928 		return(NULL);
929 
930 	if (v2_result1->stat != YP_TRUE) {
931 		return(&result);
932 	}
933 
934 	v2_result2 = ypproc_order_2_svc(&argp->yprequest_u.yp_req_nokeytype,
935 									rqstp);
936 	if (v2_result2 == NULL)
937 		return(NULL);
938 
939 	if (v2_result2->stat != YP_TRUE) {
940 		return(&result);
941 	}
942 
943 	result.ypresponse_u.yp_resp_map_parmstype.peer =
944 		v2_result1->peer;
945 	result.ypresponse_u.yp_resp_map_parmstype.ordernum =
946 		v2_result2->ordernum;
947 
948 	return (&result);
949 }
950 
951 ypresponse *
952 ypoldproc_push_1_svc(yprequest *argp, struct svc_req *rqstp)
953 {
954 	static ypresponse  result;
955 
956 	/*
957 	 * Not implemented.
958 	 */
959 
960 	return (&result);
961 }
962 
963 ypresponse *
964 ypoldproc_pull_1_svc(yprequest *argp, struct svc_req *rqstp)
965 {
966 	static ypresponse  result;
967 
968 	/*
969 	 * Not implemented.
970 	 */
971 
972 	return (&result);
973 }
974 
975 ypresponse *
976 ypoldproc_get_1_svc(yprequest *argp, struct svc_req *rqstp)
977 {
978 	static ypresponse  result;
979 
980 	/*
981 	 * Not implemented.
982 	 */
983 
984 	return (&result);
985 }
986