Bring in a transport-independent RPC (TI-RPC).
[dragonfly.git] / usr.sbin / ypserv / yp_server.c
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  * $DragonFly: src/usr.sbin/ypserv/yp_server.c,v 1.4 2005/11/24 22:23:02 swildner Exp $
34  */
35
36 #include "yp.h"
37 #include "yp_extern.h"
38 #include <dirent.h>
39 #include <errno.h>
40 #include <stdlib.h>
41 #include <sys/stat.h>
42 #include <sys/param.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #include <rpc/rpc.h>
48
49 int children = 0;
50
51 #define MASTER_STRING   "YP_MASTER_NAME"
52 #define MASTER_SZ       sizeof(MASTER_STRING) - 1
53 #define ORDER_STRING    "YP_LAST_MODIFIED"
54 #define ORDER_SZ        sizeof(ORDER_STRING) - 1
55
56 static pid_t
57 yp_fork(void)
58 {
59         if (yp_pid != getpid()) {
60                 yp_error("child %d trying to fork!", getpid());
61                 errno = EEXIST;
62                 return(-1);
63         }
64
65         return(fork());
66 }
67
68 /*
69  * NIS v2 support. This is where most of the action happens.
70  */
71
72 void *
73 ypproc_null_2_svc(void *argp, struct svc_req *rqstp)
74 {
75         static char * result;
76         static char rval = 0;
77
78 #ifdef DB_CACHE
79         if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
80 #else
81         if (yp_access(NULL, (struct svc_req *)rqstp))
82 #endif
83                 return(NULL);
84
85         result = &rval;
86
87         return((void *) &result);
88 }
89
90 bool_t *
91 ypproc_domain_2_svc(domainname *argp, struct svc_req *rqstp)
92 {
93         static bool_t  result;
94
95 #ifdef DB_CACHE
96         if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) {
97 #else
98         if (yp_access(NULL, (struct svc_req *)rqstp)) {
99 #endif
100                 result = FALSE;
101                 return (&result);
102         }
103
104         if (argp == NULL || yp_validdomain(*argp))
105                 result = FALSE;
106         else
107                 result = TRUE;
108
109         return (&result);
110 }
111
112 bool_t *
113 ypproc_domain_nonack_2_svc(domainname *argp, struct svc_req *rqstp)
114 {
115         static bool_t  result;
116
117 #ifdef DB_CACHE
118         if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
119 #else
120         if (yp_access(NULL, (struct svc_req *)rqstp))
121 #endif
122                 return (NULL);
123
124         if (argp == NULL || yp_validdomain(*argp))
125                 return (NULL);
126         else
127                 result = TRUE;
128
129         return (&result);
130 }
131
132 ypresp_val *
133 ypproc_match_2_svc(ypreq_key *argp, struct svc_req *rqstp)
134 {
135         static ypresp_val  result;
136
137         result.val.valdat_val = "";
138         result.val.valdat_len = 0;
139
140 #ifdef DB_CACHE
141         if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
142 #else
143         if (yp_access(argp->map, (struct svc_req *)rqstp)) {
144 #endif
145                 result.stat = YP_YPERR;
146                 return (&result);
147         }
148
149         if (argp->domain == NULL || argp->map == NULL) {
150                 result.stat = YP_BADARGS;
151                 return (&result);
152         }
153
154         if (yp_select_map(argp->map, argp->domain, NULL, 1) != YP_TRUE) {
155                 result.stat = yp_errno;
156                 return(&result);
157         }
158
159         result.stat = yp_getbykey(&argp->key, &result.val);
160
161         /*
162          * Do DNS lookups for hosts maps if database lookup failed.
163          */
164
165 #ifdef DB_CACHE
166         if (do_dns && result.stat != YP_TRUE &&
167             (yp_testflag(argp->map, argp->domain, YP_INTERDOMAIN) ||
168             (strstr(argp->map, "hosts") || strstr(argp->map, "ipnodes")))) {
169 #else
170         if (do_dns && result.stat != YP_TRUE &&
171             (strstr(argp->map, "hosts") || strstr(argp->map, "ipnodes"))) {
172 #endif
173                 char                    nbuf[YPMAXRECORD];
174
175                 /* NUL terminate! NUL terminate!! NUL TERMINATE!!! */
176                 bcopy(argp->key.keydat_val, nbuf, argp->key.keydat_len);
177                 nbuf[argp->key.keydat_len] = '\0';
178
179                 if (debug)
180                         yp_error("doing DNS lookup of %s", nbuf);
181
182                 if (!strcmp(argp->map, "hosts.byname"))
183                         result.stat = yp_async_lookup_name(rqstp, nbuf,
184                             AF_INET);
185                 else if (!strcmp(argp->map, "hosts.byaddr"))
186                         result.stat = yp_async_lookup_addr(rqstp, nbuf,
187                             AF_INET);
188                 else if (!strcmp(argp->map, "ipnodes.byname"))
189                         result.stat = yp_async_lookup_name(rqstp, nbuf,
190                             AF_INET6);
191                 else if (!strcmp(argp->map, "ipnodes.byaddr"))
192                         result.stat = yp_async_lookup_addr(rqstp, nbuf,
193                             AF_INET6);
194
195                 if (result.stat == YP_TRUE)
196                         return(NULL);
197         }
198
199         return (&result);
200 }
201
202 ypresp_key_val *
203 ypproc_first_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
204 {
205         static ypresp_key_val  result;
206
207         result.val.valdat_val = result.key.keydat_val = "";
208         result.val.valdat_len = result.key.keydat_len = 0;
209
210 #ifdef DB_CACHE
211         if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
212 #else
213         if (yp_access(argp->map, (struct svc_req *)rqstp)) {
214 #endif
215                 result.stat = YP_YPERR;
216                 return (&result);
217         }
218
219         if (argp->domain == NULL) {
220                 result.stat = YP_BADARGS;
221                 return (&result);
222         }
223
224         if (yp_select_map(argp->map, argp->domain, NULL, 0) != YP_TRUE) {
225                 result.stat = yp_errno;
226                 return(&result);
227         }
228
229         result.stat = yp_firstbykey(&result.key, &result.val);
230
231         return (&result);
232 }
233
234 ypresp_key_val *
235 ypproc_next_2_svc(ypreq_key *argp, struct svc_req *rqstp)
236 {
237         static ypresp_key_val  result;
238
239         result.val.valdat_val = result.key.keydat_val = "";
240         result.val.valdat_len = result.key.keydat_len = 0;
241
242 #ifdef DB_CACHE
243         if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
244 #else
245         if (yp_access(argp->map, (struct svc_req *)rqstp)) {
246 #endif
247                 result.stat = YP_YPERR;
248                 return (&result);
249         }
250
251         if (argp->domain == NULL || argp->map == NULL) {
252                 result.stat = YP_BADARGS;
253                 return (&result);
254         }
255
256         if (yp_select_map(argp->map, argp->domain, &argp->key, 0) != YP_TRUE) {
257                 result.stat = yp_errno;
258                 return(&result);
259         }
260
261         result.key.keydat_len = argp->key.keydat_len;
262         result.key.keydat_val = argp->key.keydat_val;
263
264         result.stat = yp_nextbykey(&result.key, &result.val);
265
266         return (&result);
267 }
268
269 static void
270 ypxfr_callback(ypxfrstat rval, struct sockaddr_in *addr, unsigned int transid,
271                unsigned int prognum, unsigned long port)
272 {
273         CLIENT *clnt;
274         int sock = RPC_ANYSOCK;
275         struct timeval timeout;
276         yppushresp_xfr ypxfr_resp;
277         struct rpc_err err;
278
279         timeout.tv_sec = 5;
280         timeout.tv_usec = 0;
281         addr->sin_port = htons(port);
282
283         if ((clnt = clntudp_create(addr,prognum,1,timeout,&sock)) == NULL) {
284                 yp_error("%s: %s", inet_ntoa(addr->sin_addr),
285                   clnt_spcreateerror("failed to establish callback handle"));
286                 return;
287         }
288
289         ypxfr_resp.status = rval;
290         ypxfr_resp.transid = transid;
291
292         /* Turn the timeout off -- we don't want to block. */
293         timeout.tv_sec = 0;
294         if (clnt_control(clnt, CLSET_TIMEOUT, &timeout) == FALSE)
295                 yp_error("failed to set timeout on ypproc_xfr callback");
296
297         if (yppushproc_xfrresp_1(&ypxfr_resp, clnt) == NULL) {
298                 clnt_geterr(clnt, &err);
299                 if (err.re_status != RPC_SUCCESS &&
300                     err.re_status != RPC_TIMEDOUT)
301                         yp_error("%s", clnt_sperror(clnt,
302                                 "ypxfr callback failed"));
303         }
304
305         clnt_destroy(clnt);
306         return;
307 }
308
309 #define YPXFR_RETURN(CODE)                                              \
310         /* Order is important: send regular RPC reply, then callback */ \
311         result.xfrstat = CODE;                                          \
312         svc_sendreply(rqstp->rq_xprt, (xdrproc_t)xdr_ypresp_xfr, &result); \
313         ypxfr_callback(CODE,rqhost,argp->transid,                       \
314                                         argp->prog,argp->port);         \
315         return(NULL);
316
317 ypresp_xfr *
318 ypproc_xfr_2_svc(ypreq_xfr *argp, struct svc_req *rqstp)
319 {
320         static ypresp_xfr  result;
321         struct sockaddr_in *rqhost;
322         ypresp_master *mres;
323         ypreq_nokey mreq;
324
325         result.transid = argp->transid;
326         rqhost = svc_getcaller(rqstp->rq_xprt);
327
328 #ifdef DB_CACHE
329         if (yp_access(argp->map_parms.map,
330                         argp->map_parms.domain, (struct svc_req *)rqstp)) {
331 #else
332         if (yp_access(argp->map_parms.map, (struct svc_req *)rqstp)) {
333 #endif
334                 YPXFR_RETURN(YPXFR_REFUSED)
335         }
336
337
338         if (argp->map_parms.domain == NULL) {
339                 YPXFR_RETURN(YPXFR_BADARGS)
340         }
341
342         if (yp_validdomain(argp->map_parms.domain)) {
343                 YPXFR_RETURN(YPXFR_NODOM)
344         }
345
346         /*
347          * Determine the master host ourselves. The caller may
348          * be up to no good. This has the side effect of verifying
349          * that the requested map and domain actually exist.
350          */
351
352         mreq.domain = argp->map_parms.domain;
353         mreq.map = argp->map_parms.map;
354
355         mres = ypproc_master_2_svc(&mreq, rqstp);
356
357         if (mres->stat != YP_TRUE) {
358                 yp_error("couldn't find master for map %s@%s",
359                                                 argp->map_parms.map,
360                                                 argp->map_parms.domain);
361                 yp_error("host at %s (%s) may be pulling my leg",
362                                                 argp->map_parms.peer,
363                                                 inet_ntoa(rqhost->sin_addr));
364                 YPXFR_RETURN(YPXFR_REFUSED)
365         }
366
367         switch (yp_fork()) {
368         case 0:
369         {
370                 char g[11], t[11], p[11];
371                 char ypxfr_command[MAXPATHLEN + 2];
372
373                 snprintf (ypxfr_command, sizeof(ypxfr_command), "%sypxfr", _PATH_LIBEXEC);
374                 snprintf (t, sizeof(t), "%u", argp->transid);
375                 snprintf (g, sizeof(g), "%u", argp->prog);
376                 snprintf (p, sizeof(p), "%u", argp->port);
377                 if (debug) {
378                         close(0); close(1); close(2);
379                 }
380                 if (strcmp(yp_dir, _PATH_YP)) {
381                         execl(ypxfr_command, "ypxfr",
382                         "-d", argp->map_parms.domain,
383                         "-h", mres->peer,
384                         "-p", yp_dir, "-C", t,
385                         g, inet_ntoa(rqhost->sin_addr),
386                         p, argp->map_parms.map,
387                         NULL);
388                 } else {
389                         execl(ypxfr_command, "ypxfr",
390                         "-d", argp->map_parms.domain,
391                         "-h", mres->peer,
392                         "-C", t,
393                         g, inet_ntoa(rqhost->sin_addr),
394                         p, argp->map_parms.map,
395                         NULL);
396                 }
397                 yp_error("ypxfr execl(%s): %s", ypxfr_command, strerror(errno));
398                 YPXFR_RETURN(YPXFR_XFRERR)
399                 /*
400                  * Just to safe, prevent PR #10970 from biting us in
401                  * the unlikely case that execing ypxfr fails. We don't
402                  * want to have any child processes spawned from this
403                  * child process.
404                  */
405                 _exit(0);
406                 break;
407         }
408         case -1:
409                 yp_error("ypxfr fork(): %s", strerror(errno));
410                 YPXFR_RETURN(YPXFR_XFRERR)
411                 break;
412         default:
413                 result.xfrstat = YPXFR_SUCC;
414                 children++;
415                 break;
416         }
417
418         return (&result);
419 }
420 #undef YPXFR_RETURN
421
422 void *
423 ypproc_clear_2_svc(void *argp, struct svc_req *rqstp)
424 {
425         static char * result;
426         static char rval = 0;
427
428 #ifdef DB_CACHE
429         if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
430 #else
431         if (yp_access(NULL, (struct svc_req *)rqstp))
432 #endif
433                 return (NULL);
434 #ifdef DB_CACHE
435         /* clear out the database cache */
436         yp_flush_all();
437 #endif
438         /* Re-read the securenets database for the hell of it. */
439         load_securenets();
440
441         result = &rval;
442         return((void *) &result);
443 }
444
445 /*
446  * For ypproc_all, we have to send a stream of ypresp_all structures
447  * via TCP, but the XDR filter generated from the yp.x protocol
448  * definition file only serializes one such structure. This means that
449  * to send the whole stream, you need a wrapper which feeds all the
450  * records into the underlying XDR routine until it hits an 'EOF.'
451  * But to use the wrapper, you have to violate the boundaries between
452  * RPC layers by calling svc_sendreply() directly from the ypproc_all
453  * service routine instead of letting the RPC dispatcher do it.
454  *
455  * Bleah.
456  */
457
458 /*
459  * Custom XDR routine for serialzing results of ypproc_all: keep
460  * reading from the database and spew until we run out of records
461  * or encounter an error.
462  */
463 static bool_t
464 xdr_my_ypresp_all(XDR *xdrs, ypresp_all *objp)
465 {
466         while (1) {
467                 /* Get a record. */
468                 if ((objp->ypresp_all_u.val.stat =
469                         yp_nextbykey(&objp->ypresp_all_u.val.key,
470                                      &objp->ypresp_all_u.val.val)) == YP_TRUE) {
471                         objp->more = TRUE;
472                 } else {
473                         objp->more = FALSE;
474                 }
475
476                 /* Serialize. */
477                 if (!xdr_ypresp_all(xdrs, objp))
478                         return(FALSE);
479                 if (objp->more == FALSE)
480                         return(TRUE);
481         }
482 }
483
484 ypresp_all *
485 ypproc_all_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
486 {
487         static ypresp_all  result;
488
489         /*
490          * Set this here so that the client will be forced to make
491          * at least one attempt to read from us even if all we're
492          * doing is returning an error.
493          */
494         result.more = TRUE;
495         result.ypresp_all_u.val.key.keydat_len = 0;
496         result.ypresp_all_u.val.key.keydat_val = "";
497
498 #ifdef DB_CACHE
499         if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
500 #else
501         if (yp_access(argp->map, (struct svc_req *)rqstp)) {
502 #endif
503                 result.ypresp_all_u.val.stat = YP_YPERR;
504                 return (&result);
505         }
506
507         if (argp->domain == NULL || argp->map == NULL) {
508                 result.ypresp_all_u.val.stat = YP_BADARGS;
509                 return (&result);
510         }
511
512         /*
513          * XXX If we hit the child limit, fail the request.
514          * If we don't, and the map is large, we could block for
515          * a long time in the parent.
516          */
517         if (children >= MAX_CHILDREN) {
518                 result.ypresp_all_u.val.stat = YP_YPERR;
519                 return(&result);
520         }
521
522         /*
523          * The ypproc_all procedure can take a while to complete.
524          * Best to handle it in a subprocess so the parent doesn't
525          * block. (Is there a better way to do this? Maybe with
526          * async socket I/O?)
527          */
528         if (!debug) {
529                 switch (yp_fork()) {
530                 case 0:
531                         break;
532                 case -1:
533                         yp_error("ypall fork(): %s", strerror(errno));
534                         result.ypresp_all_u.val.stat = YP_YPERR;
535                         return(&result);
536                         break;
537                 default:
538                         children++;
539                         return (NULL);
540                         break;
541                 }
542         }
543
544         /*
545          * Fix for PR #10971: don't let the child ypserv share
546          * DB handles with the parent process.
547          */
548 #ifdef DB_CACHE
549         yp_flush_all();
550 #endif
551
552         if (yp_select_map(argp->map, argp->domain,
553                                 &result.ypresp_all_u.val.key, 0) != YP_TRUE) {
554                 result.ypresp_all_u.val.stat = yp_errno;
555                 return(&result);
556         }
557
558         /* Kick off the actual data transfer. */
559         svc_sendreply(rqstp->rq_xprt, (xdrproc_t)xdr_my_ypresp_all, &result);
560
561         /*
562          * Proper fix for PR #10970: exit here so that we don't risk
563          * having a child spawned from this sub-process.
564          */
565         _exit(0);
566 }
567
568 ypresp_master *
569 ypproc_master_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
570 {
571         static ypresp_master  result;
572         static char ypvalbuf[YPMAXRECORD];
573         keydat key = { MASTER_SZ, MASTER_STRING };
574         valdat val;
575
576         result.peer = "";
577
578 #ifdef DB_CACHE
579         if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
580 #else
581         if (yp_access(argp->map, (struct svc_req *)rqstp)) {
582 #endif
583                 result.stat = YP_YPERR;
584                 return(&result);
585         }
586
587         if (argp->domain == NULL) {
588                 result.stat = YP_BADARGS;
589                 return (&result);
590         }
591
592         if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) {
593                 result.stat = yp_errno;
594                 return(&result);
595         }
596
597         /*
598          * Note that we copy the data retrieved from the database to
599          * a private buffer and NUL terminate the buffer rather than
600          * terminating the data in place. We do this because by stuffing
601          * a '\0' into data.data, we will actually be corrupting memory
602          * allocated by the DB package. This is a bad thing now that we
603          * cache DB handles rather than closing the database immediately.
604          */
605         result.stat = yp_getbykey(&key, &val);
606         if (result.stat == YP_TRUE) {
607                 bcopy(val.valdat_val, &ypvalbuf, val.valdat_len);
608                 ypvalbuf[val.valdat_len] = '\0';
609                 result.peer = ypvalbuf;
610         } else
611                 result.peer = "";
612
613         return (&result);
614 }
615
616 ypresp_order *
617 ypproc_order_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
618 {
619         static ypresp_order  result;
620         keydat key = { ORDER_SZ, ORDER_STRING };
621         valdat val;
622
623         result.ordernum = 0;
624
625 #ifdef DB_CACHE
626         if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
627 #else
628         if (yp_access(argp->map, (struct svc_req *)rqstp)) {
629 #endif
630                 result.stat = YP_YPERR;
631                 return(&result);
632         }
633
634         if (argp->domain == NULL) {
635                 result.stat = YP_BADARGS;
636                 return (&result);
637         }
638
639         /*
640          * We could just check the timestamp on the map file,
641          * but that's a hack: we'll only know the last time the file
642          * was touched, not the last time the database contents were
643          * updated.
644          */
645
646         if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) {
647                 result.stat = yp_errno;
648                 return(&result);
649         }
650
651         result.stat = yp_getbykey(&key, &val);
652
653         if (result.stat == YP_TRUE)
654                 result.ordernum = atoi(val.valdat_val);
655         else
656                 result.ordernum = 0;
657
658         return (&result);
659 }
660
661 static void
662 yp_maplist_free(struct ypmaplist *yp_maplist)
663 {
664         struct ypmaplist *next;
665
666         while (yp_maplist) {
667                 next = yp_maplist->next;
668                 free(yp_maplist->map);
669                 free(yp_maplist);
670                 yp_maplist = next;
671         }
672         return;
673 }
674
675 static struct ypmaplist *
676 yp_maplist_create(const char *domain)
677 {
678         char yp_mapdir[MAXPATHLEN + 2];
679         char yp_mapname[MAXPATHLEN + 2];
680         struct ypmaplist *cur = NULL;
681         struct ypmaplist *yp_maplist = NULL;
682         DIR *dird;
683         struct dirent *dirp;
684         struct stat statbuf;
685
686         snprintf(yp_mapdir, sizeof(yp_mapdir), "%s/%s", yp_dir, domain);
687
688         if ((dird = opendir(yp_mapdir)) == NULL) {
689                 yp_error("opendir(%s) failed: %s", yp_mapdir, strerror(errno));
690                 return(NULL);
691         }
692
693         while ((dirp = readdir(dird)) != NULL) {
694                 if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
695                         snprintf(yp_mapname, sizeof(yp_mapname), "%s/%s",
696                                                         yp_mapdir,dirp->d_name);
697                         if (stat(yp_mapname, &statbuf) < 0 ||
698                                                 !S_ISREG(statbuf.st_mode))
699                                 continue;
700                         if ((cur = (struct ypmaplist *)
701                                 malloc(sizeof(struct ypmaplist))) == NULL) {
702                                 yp_error("malloc() failed");
703                                 closedir(dird);
704                                 yp_maplist_free(yp_maplist);
705                                 return(NULL);
706                         }
707                         if ((cur->map = strdup(dirp->d_name)) == NULL) {
708                                 yp_error("strdup() failed: %s",strerror(errno));
709                                 closedir(dird);
710                                 yp_maplist_free(yp_maplist);
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 }