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