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