yp_server.c revision 46205
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
35static const char rcsid[] =
36	"$Id: yp_server.c,v 1.27 1999/02/10 16:16:14 wpaul 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
52int 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
59static 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
74void *
75ypproc_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
92bool_t *
93ypproc_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
114bool_t *
115ypproc_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
134ypresp_val *
135ypproc_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
195ypresp_key_val *
196ypproc_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
227ypresp_key_val *
228ypproc_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
262static 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
313ypresp_xfr *
314ypproc_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
418void *
419ypproc_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 */
459static bool_t
460xdr_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
480ypresp_all *
481ypproc_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	if (yp_select_map(argp->map, argp->domain,
541				&result.ypresp_all_u.val.key, 0) != YP_TRUE) {
542		result.ypresp_all_u.val.stat = yp_errno;
543		return(&result);
544	}
545
546	/* Kick off the actual data transfer. */
547	svc_sendreply(rqstp->rq_xprt, xdr_my_ypresp_all, (char *)&result);
548
549	/*
550	 * Proper fix for PR #10970: exit here so that we don't risk
551	 * having a child spawned from this sub-process.
552	 */
553	_exit(0);
554}
555
556ypresp_master *
557ypproc_master_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
558{
559	static ypresp_master  result;
560	static char ypvalbuf[YPMAXRECORD];
561	keydat key = { MASTER_SZ, MASTER_STRING };
562	valdat val;
563
564	result.peer = "";
565
566#ifdef DB_CACHE
567	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
568#else
569	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
570#endif
571		result.stat = YP_YPERR;
572		return(&result);
573	}
574
575	if (argp->domain == NULL) {
576		result.stat = YP_BADARGS;
577		return (&result);
578	}
579
580	if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) {
581		result.stat = yp_errno;
582		return(&result);
583	}
584
585	/*
586	 * Note that we copy the data retrieved from the database to
587	 * a private buffer and NUL terminate the buffer rather than
588	 * terminating the data in place. We do this because by stuffing
589	 * a '\0' into data.data, we will actually be corrupting memory
590	 * allocated by the DB package. This is a bad thing now that we
591	 * cache DB handles rather than closing the database immediately.
592	 */
593	result.stat = yp_getbykey(&key, &val);
594	if (result.stat == YP_TRUE) {
595		bcopy((char *)val.valdat_val, (char *)&ypvalbuf,
596						val.valdat_len);
597		ypvalbuf[val.valdat_len] = '\0';
598		result.peer = (char *)&ypvalbuf;
599	} else
600		result.peer = "";
601
602	return (&result);
603}
604
605ypresp_order *
606ypproc_order_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
607{
608	static ypresp_order  result;
609	keydat key = { ORDER_SZ, ORDER_STRING };
610	valdat val;
611
612	result.ordernum = 0;
613
614#ifdef DB_CACHE
615	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
616#else
617	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
618#endif
619		result.stat = YP_YPERR;
620		return(&result);
621	}
622
623	if (argp->domain == NULL) {
624		result.stat = YP_BADARGS;
625		return (&result);
626	}
627
628	/*
629	 * We could just check the timestamp on the map file,
630	 * but that's a hack: we'll only know the last time the file
631	 * was touched, not the last time the database contents were
632	 * updated.
633	 */
634
635	if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) {
636		result.stat = yp_errno;
637		return(&result);
638	}
639
640	result.stat = yp_getbykey(&key, &val);
641
642	if (result.stat == YP_TRUE)
643		result.ordernum = atoi((char *)val.valdat_val);
644	else
645		result.ordernum = 0;
646
647	return (&result);
648}
649
650static void yp_maplist_free(yp_maplist)
651	struct ypmaplist *yp_maplist;
652{
653	register struct ypmaplist *next;
654
655	while(yp_maplist) {
656		next = yp_maplist->next;
657		free(yp_maplist->map);
658		free(yp_maplist);
659		yp_maplist = next;
660	}
661	return;
662}
663
664static struct ypmaplist *yp_maplist_create(domain)
665	const char *domain;
666{
667	char yp_mapdir[MAXPATHLEN + 2];
668	char yp_mapname[MAXPATHLEN + 2];
669	struct ypmaplist *cur = NULL;
670	struct ypmaplist *yp_maplist = NULL;
671	DIR *dird;
672	struct dirent *dirp;
673	struct stat statbuf;
674
675	snprintf(yp_mapdir, sizeof(yp_mapdir), "%s/%s", yp_dir, domain);
676
677	if ((dird = opendir(yp_mapdir)) == NULL) {
678		yp_error("opendir(%s) failed: %s", yp_mapdir, strerror(errno));
679		return(NULL);
680	}
681
682	while ((dirp = readdir(dird)) != NULL) {
683		if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
684			snprintf(yp_mapname, sizeof(yp_mapname), "%s/%s",
685							yp_mapdir,dirp->d_name);
686			if (stat(yp_mapname, &statbuf) < 0 ||
687						!S_ISREG(statbuf.st_mode))
688				continue;
689			if ((cur = (struct ypmaplist *)
690				malloc(sizeof(struct ypmaplist))) == NULL) {
691				yp_error("malloc() failed");
692				closedir(dird);
693				yp_maplist_free(yp_maplist);
694				return(NULL);
695			}
696			if ((cur->map = (char *)strdup(dirp->d_name)) == NULL) {
697				yp_error("strdup() failed: %s",strerror(errno));
698				closedir(dird);
699				yp_maplist_free(yp_maplist);
700				return(NULL);
701			}
702			cur->next = yp_maplist;
703			yp_maplist = cur;
704			if (debug)
705				yp_error("map: %s", yp_maplist->map);
706		}
707
708	}
709	closedir(dird);
710	return(yp_maplist);
711}
712
713ypresp_maplist *
714ypproc_maplist_2_svc(domainname *argp, struct svc_req *rqstp)
715{
716	static ypresp_maplist  result = { 0, NULL };
717
718#ifdef DB_CACHE
719	if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) {
720#else
721	if (yp_access(NULL, (struct svc_req *)rqstp)) {
722#endif
723		result.stat = YP_YPERR;
724		return(&result);
725	}
726
727	if (argp == NULL) {
728		result.stat = YP_BADARGS;
729		return (&result);
730	}
731
732	if (yp_validdomain(*argp)) {
733		result.stat = YP_NODOM;
734		return (&result);
735	}
736
737	/*
738	 * We have to construct a linked list for the ypproc_maplist
739	 * procedure using dynamically allocated memory. Since the XDR
740	 * layer won't free this list for us, we have to deal with it
741	 * ourselves. We call yp_maplist_free() first to free any
742	 * previously allocated data we may have accumulated to insure
743	 * that we have only one linked list in memory at any given
744	 * time.
745	 */
746
747	yp_maplist_free(result.maps);
748
749	if ((result.maps = yp_maplist_create(*argp)) == NULL) {
750		yp_error("yp_maplist_create failed");
751		result.stat = YP_YPERR;
752		return(&result);
753	} else
754		result.stat = YP_TRUE;
755
756	return (&result);
757}
758
759/*
760 * NIS v1 support. The nullproc, domain and domain_nonack
761 * functions from v1 are identical to those in v2, so all
762 * we have to do is hand off to them.
763 *
764 * The other functions are mostly just wrappers around their v2
765 * counterparts. For example, for the v1 'match' procedure, we
766 * crack open the argument structure, make a request to the v2
767 * 'match' function, repackage the data into a v1 response and
768 * then send it on its way.
769 *
770 * Note that we don't support the pull, push and get procedures.
771 * There's little documentation available to show what they
772 * do, and I suspect they're meant largely for map transfers
773 * between master and slave servers.
774 */
775
776void *
777ypoldproc_null_1_svc(void *argp, struct svc_req *rqstp)
778{
779	return(ypproc_null_2_svc(argp, rqstp));
780}
781
782bool_t *
783ypoldproc_domain_1_svc(domainname *argp, struct svc_req *rqstp)
784{
785	return(ypproc_domain_2_svc(argp, rqstp));
786}
787
788bool_t *
789ypoldproc_domain_nonack_1_svc(domainname *argp, struct svc_req *rqstp)
790{
791	return (ypproc_domain_nonack_2_svc(argp, rqstp));
792}
793
794/*
795 * the 'match' procedure sends a response of type YPRESP_VAL
796 */
797ypresponse *
798ypoldproc_match_1_svc(yprequest *argp, struct svc_req *rqstp)
799{
800	static ypresponse  result;
801	ypresp_val *v2_result;
802
803	result.yp_resptype = YPRESP_VAL;
804	result.ypresponse_u.yp_resp_valtype.val.valdat_val = "";
805	result.ypresponse_u.yp_resp_valtype.val.valdat_len = 0;
806
807	if (argp->yp_reqtype != YPREQ_KEY) {
808		result.ypresponse_u.yp_resp_valtype.stat = YP_BADARGS;
809		return(&result);
810	}
811
812	v2_result = ypproc_match_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp);
813	if (v2_result == NULL)
814		return(NULL);
815
816	bcopy((char *)v2_result,
817	      (char *)&result.ypresponse_u.yp_resp_valtype,
818	      sizeof(ypresp_val));
819
820	return (&result);
821}
822
823/*
824 * the 'first' procedure sends a response of type YPRESP_KEY_VAL
825 */
826ypresponse *
827ypoldproc_first_1_svc(yprequest *argp, struct svc_req *rqstp)
828{
829	static ypresponse  result;
830	ypresp_key_val *v2_result;
831
832	result.yp_resptype = YPRESP_KEY_VAL;
833	result.ypresponse_u.yp_resp_key_valtype.val.valdat_val =
834	result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = "";
835	result.ypresponse_u.yp_resp_key_valtype.val.valdat_len =
836	result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0;
837
838	if (argp->yp_reqtype != YPREQ_NOKEY) {
839		result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS;
840		return(&result);
841	}
842
843	v2_result = ypproc_first_2_svc(&argp->yprequest_u.yp_req_nokeytype,
844									rqstp);
845	if (v2_result == NULL)
846		return(NULL);
847
848	bcopy((char *)v2_result,
849	      (char *)&result.ypresponse_u.yp_resp_key_valtype,
850	      sizeof(ypresp_key_val));
851
852	return (&result);
853}
854
855/*
856 * the 'next' procedure sends a response of type YPRESP_KEY_VAL
857 */
858ypresponse *
859ypoldproc_next_1_svc(yprequest *argp, struct svc_req *rqstp)
860{
861	static ypresponse  result;
862	ypresp_key_val *v2_result;
863
864	result.yp_resptype = YPRESP_KEY_VAL;
865	result.ypresponse_u.yp_resp_key_valtype.val.valdat_val =
866	result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = "";
867	result.ypresponse_u.yp_resp_key_valtype.val.valdat_len =
868	result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0;
869
870	if (argp->yp_reqtype != YPREQ_KEY) {
871		result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS;
872		return(&result);
873	}
874
875	v2_result = ypproc_next_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp);
876	if (v2_result == NULL)
877		return(NULL);
878
879	bcopy((char *)v2_result,
880	      (char *)&result.ypresponse_u.yp_resp_key_valtype,
881	      sizeof(ypresp_key_val));
882
883	return (&result);
884}
885
886/*
887 * the 'poll' procedure sends a response of type YPRESP_MAP_PARMS
888 */
889ypresponse *
890ypoldproc_poll_1_svc(yprequest *argp, struct svc_req *rqstp)
891{
892	static ypresponse  result;
893	ypresp_master *v2_result1;
894	ypresp_order *v2_result2;
895
896	result.yp_resptype = YPRESP_MAP_PARMS;
897	result.ypresponse_u.yp_resp_map_parmstype.domain =
898		argp->yprequest_u.yp_req_nokeytype.domain;
899	result.ypresponse_u.yp_resp_map_parmstype.map =
900		argp->yprequest_u.yp_req_nokeytype.map;
901	/*
902	 * Hmm... there is no 'status' value in the
903	 * yp_resp_map_parmstype structure, so I have to
904	 * guess at what to do to indicate a failure.
905	 * I hope this is right.
906	 */
907	result.ypresponse_u.yp_resp_map_parmstype.ordernum = 0;
908	result.ypresponse_u.yp_resp_map_parmstype.peer = "";
909
910	if (argp->yp_reqtype != YPREQ_MAP_PARMS) {
911		return(&result);
912	}
913
914	v2_result1 = ypproc_master_2_svc(&argp->yprequest_u.yp_req_nokeytype,
915									rqstp);
916	if (v2_result1 == NULL)
917		return(NULL);
918
919	if (v2_result1->stat != YP_TRUE) {
920		return(&result);
921	}
922
923	v2_result2 = ypproc_order_2_svc(&argp->yprequest_u.yp_req_nokeytype,
924									rqstp);
925	if (v2_result2 == NULL)
926		return(NULL);
927
928	if (v2_result2->stat != YP_TRUE) {
929		return(&result);
930	}
931
932	result.ypresponse_u.yp_resp_map_parmstype.peer =
933		v2_result1->peer;
934	result.ypresponse_u.yp_resp_map_parmstype.ordernum =
935		v2_result2->ordernum;
936
937	return (&result);
938}
939
940ypresponse *
941ypoldproc_push_1_svc(yprequest *argp, struct svc_req *rqstp)
942{
943	static ypresponse  result;
944
945	/*
946	 * Not implemented.
947	 */
948
949	return (&result);
950}
951
952ypresponse *
953ypoldproc_pull_1_svc(yprequest *argp, struct svc_req *rqstp)
954{
955	static ypresponse  result;
956
957	/*
958	 * Not implemented.
959	 */
960
961	return (&result);
962}
963
964ypresponse *
965ypoldproc_get_1_svc(yprequest *argp, struct svc_req *rqstp)
966{
967	static ypresponse  result;
968
969	/*
970	 * Not implemented.
971	 */
972
973	return (&result);
974}
975