yp_server.c revision 43847
112891Swpaul/*
212891Swpaul * Copyright (c) 1995
312891Swpaul *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
412891Swpaul *
512891Swpaul * Redistribution and use in source and binary forms, with or without
612891Swpaul * modification, are permitted provided that the following conditions
712891Swpaul * are met:
812891Swpaul * 1. Redistributions of source code must retain the above copyright
912891Swpaul *    notice, this list of conditions and the following disclaimer.
1012891Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1112891Swpaul *    notice, this list of conditions and the following disclaimer in the
1212891Swpaul *    documentation and/or other materials provided with the distribution.
1312891Swpaul * 3. All advertising materials mentioning features or use of this software
1412891Swpaul *    must display the following acknowledgement:
1512891Swpaul *	This product includes software developed by Bill Paul.
1612891Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1712891Swpaul *    may be used to endorse or promote products derived from this software
1812891Swpaul *    without specific prior written permission.
1912891Swpaul *
2012891Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2112891Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2212891Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2312891Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
2412891Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2512891Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2612891Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2712891Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2812891Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2912891Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3012891Swpaul * SUCH DAMAGE.
3112891Swpaul *
3212891Swpaul */
3312891Swpaul
3430827Scharnier#ifndef lint
3530827Scharnierstatic const char rcsid[] =
3643847Swpaul	"$Id: yp_server.c,v 1.26 1998/02/11 19:15:32 wpaul Exp $";
3730827Scharnier#endif /* not lint */
3830827Scharnier
3920818Swpaul#include "yp.h"
4012891Swpaul#include "yp_extern.h"
4130827Scharnier#include <dirent.h>
4230827Scharnier#include <errno.h>
4312891Swpaul#include <stdlib.h>
4412891Swpaul#include <sys/stat.h>
4512891Swpaul#include <sys/param.h>
4612891Swpaul#include <sys/types.h>
4712891Swpaul#include <sys/socket.h>
4812891Swpaul#include <netinet/in.h>
4912891Swpaul#include <arpa/inet.h>
5012997Swpaul#include <rpc/rpc.h>
5112891Swpaul
5212891Swpaulint forked = 0;
5312891Swpaulint children = 0;
5412891Swpaul
5528042Swpaul#define	MASTER_STRING	"YP_MASTER_NAME"
5628042Swpaul#define	MASTER_SZ	sizeof(MASTER_STRING) - 1
5728042Swpaul#define	ORDER_STRING	"YP_LAST_MODIFIED"
5828042Swpaul#define	ORDER_SZ	sizeof(ORDER_STRING) - 1
5928042Swpaul
6014262Swpaul/*
6114262Swpaul * NIS v2 support. This is where most of the action happens.
6214262Swpaul */
6314262Swpaul
6412891Swpaulvoid *
6512891Swpaulypproc_null_2_svc(void *argp, struct svc_req *rqstp)
6612891Swpaul{
6712891Swpaul	static char * result;
6812891Swpaul	static char rval = 0;
6912891Swpaul
7019161Swpaul#ifdef DB_CACHE
7119161Swpaul	if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
7219161Swpaul#else
7312891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
7419161Swpaul#endif
7512891Swpaul		return(NULL);
7612891Swpaul
7712891Swpaul	result = &rval;
7812891Swpaul
7912891Swpaul	return((void *) &result);
8012891Swpaul}
8112891Swpaul
8212891Swpaulbool_t *
8312891Swpaulypproc_domain_2_svc(domainname *argp, struct svc_req *rqstp)
8412891Swpaul{
8512891Swpaul	static bool_t  result;
8612891Swpaul
8719161Swpaul#ifdef DB_CACHE
8819161Swpaul	if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) {
8919161Swpaul#else
9012891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp)) {
9119161Swpaul#endif
9212891Swpaul		result = FALSE;
9312891Swpaul		return (&result);
9412891Swpaul	}
9512891Swpaul
9612891Swpaul	if (argp == NULL || yp_validdomain(*argp))
9712891Swpaul		result = FALSE;
9812891Swpaul	else
9912891Swpaul		result = TRUE;
10012891Swpaul
10112891Swpaul	return (&result);
10212891Swpaul}
10312891Swpaul
10412891Swpaulbool_t *
10512891Swpaulypproc_domain_nonack_2_svc(domainname *argp, struct svc_req *rqstp)
10612891Swpaul{
10712891Swpaul	static bool_t  result;
10812891Swpaul
10919161Swpaul#ifdef DB_CACHE
11019161Swpaul	if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
11119161Swpaul#else
11212891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
11319161Swpaul#endif
11412891Swpaul		return (NULL);
11512891Swpaul
11612891Swpaul	if (argp == NULL || yp_validdomain(*argp))
11712891Swpaul		return (NULL);
11812891Swpaul	else
11912891Swpaul		result = TRUE;
12012891Swpaul
12112891Swpaul	return (&result);
12212891Swpaul}
12312891Swpaul
12412891Swpaulypresp_val *
12512891Swpaulypproc_match_2_svc(ypreq_key *argp, struct svc_req *rqstp)
12612891Swpaul{
12712891Swpaul	static ypresp_val  result;
12812891Swpaul
12914304Swpaul	result.val.valdat_val = "";
13014304Swpaul	result.val.valdat_len = 0;
13119161Swpaul
13219161Swpaul#ifdef DB_CACHE
13319161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
13419161Swpaul#else
13512891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
13619161Swpaul#endif
13712891Swpaul		result.stat = YP_YPERR;
13812891Swpaul		return (&result);
13912891Swpaul	}
14012891Swpaul
14112891Swpaul	if (argp->domain == NULL || argp->map == NULL) {
14212891Swpaul		result.stat = YP_BADARGS;
14312891Swpaul		return (&result);
14412891Swpaul	}
14512891Swpaul
14633250Swpaul	if (yp_select_map(argp->map, argp->domain, NULL, 1) != YP_TRUE) {
14720818Swpaul		result.stat = yp_errno;
14820818Swpaul		return(&result);
14912891Swpaul	}
15012891Swpaul
15120818Swpaul	result.stat = yp_getbykey(&argp->key, &result.val);
15220818Swpaul
15312891Swpaul	/*
15412891Swpaul	 * Do DNS lookups for hosts maps if database lookup failed.
15512891Swpaul	 */
15612891Swpaul
15719161Swpaul#ifdef DB_CACHE
15819161Swpaul	if (result.stat != YP_TRUE &&
15919161Swpaul	    (yp_testflag(argp->map, argp->domain, YP_INTERDOMAIN) ||
16019161Swpaul	    (strstr(argp->map, "hosts") && do_dns))) {
16119161Swpaul#else
16212891Swpaul	if (do_dns && result.stat != YP_TRUE && strstr(argp->map, "hosts")) {
16319161Swpaul#endif
16427589Swpaul		char			nbuf[YPMAXRECORD];
16520818Swpaul
16620818Swpaul		/* NUL terminate! NUL terminate!! NUL TERMINATE!!! */
16727589Swpaul		bcopy(argp->key.keydat_val, nbuf, argp->key.keydat_len);
16827589Swpaul		nbuf[argp->key.keydat_len] = '\0';
16912891Swpaul
17012891Swpaul		if (debug)
17130827Scharnier			yp_error("doing DNS lookup of %s", nbuf);
17212891Swpaul
17312891Swpaul		if (!strcmp(argp->map, "hosts.byname"))
17427589Swpaul			result.stat = yp_async_lookup_name(rqstp, nbuf);
17512891Swpaul		else if (!strcmp(argp->map, "hosts.byaddr"))
17627589Swpaul			result.stat = yp_async_lookup_addr(rqstp, nbuf);
17712891Swpaul
17820818Swpaul		if (result.stat == YP_TRUE)
17920818Swpaul			return(NULL);
18012891Swpaul	}
18112891Swpaul
18212891Swpaul	return (&result);
18312891Swpaul}
18412891Swpaul
18512891Swpaulypresp_key_val *
18612891Swpaulypproc_first_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
18712891Swpaul{
18812891Swpaul	static ypresp_key_val  result;
18912891Swpaul
19014304Swpaul	result.val.valdat_val = result.key.keydat_val = "";
19114304Swpaul	result.val.valdat_len = result.key.keydat_len = 0;
19219161Swpaul
19319161Swpaul#ifdef DB_CACHE
19419161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
19519161Swpaul#else
19612891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
19719161Swpaul#endif
19812891Swpaul		result.stat = YP_YPERR;
19912891Swpaul		return (&result);
20012891Swpaul	}
20112891Swpaul
20212891Swpaul	if (argp->domain == NULL) {
20312891Swpaul		result.stat = YP_BADARGS;
20412891Swpaul		return (&result);
20512891Swpaul	}
20612891Swpaul
20733250Swpaul	if (yp_select_map(argp->map, argp->domain, NULL, 0) != YP_TRUE) {
20812891Swpaul		result.stat = yp_errno;
20912891Swpaul		return(&result);
21012891Swpaul	}
21112891Swpaul
21220818Swpaul	result.stat = yp_firstbykey(&result.key, &result.val);
21312891Swpaul
21412891Swpaul	return (&result);
21512891Swpaul}
21612891Swpaul
21712891Swpaulypresp_key_val *
21812891Swpaulypproc_next_2_svc(ypreq_key *argp, struct svc_req *rqstp)
21912891Swpaul{
22012891Swpaul	static ypresp_key_val  result;
22112891Swpaul
22214304Swpaul	result.val.valdat_val = result.key.keydat_val = "";
22314304Swpaul	result.val.valdat_len = result.key.keydat_len = 0;
22415426Swpaul
22519161Swpaul#ifdef DB_CACHE
22619161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
22719161Swpaul#else
22812891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
22919161Swpaul#endif
23012891Swpaul		result.stat = YP_YPERR;
23112891Swpaul		return (&result);
23212891Swpaul	}
23312891Swpaul
23412891Swpaul	if (argp->domain == NULL || argp->map == NULL) {
23512891Swpaul		result.stat = YP_BADARGS;
23612891Swpaul		return (&result);
23712891Swpaul	}
23812891Swpaul
23920818Swpaul	if (yp_select_map(argp->map, argp->domain, &argp->key, 0) != YP_TRUE) {
24012891Swpaul		result.stat = yp_errno;
24112891Swpaul		return(&result);
24212891Swpaul	}
24312891Swpaul
24420818Swpaul	result.key.keydat_len = argp->key.keydat_len;
24520818Swpaul	result.key.keydat_val = argp->key.keydat_val;
24612891Swpaul
24720818Swpaul	result.stat = yp_nextbykey(&result.key, &result.val);
24820818Swpaul
24912891Swpaul	return (&result);
25012891Swpaul}
25112891Swpaul
25212997Swpaulstatic void ypxfr_callback(rval,addr,transid,prognum,port)
25312997Swpaul	ypxfrstat rval;
25412997Swpaul	struct sockaddr_in *addr;
25512997Swpaul	unsigned int transid;
25612997Swpaul	unsigned int prognum;
25712997Swpaul	unsigned long port;
25812997Swpaul{
25912997Swpaul	CLIENT *clnt;
26012997Swpaul	int sock = RPC_ANYSOCK;
26112997Swpaul	struct timeval timeout;
26212997Swpaul	yppushresp_xfr ypxfr_resp;
26313375Swpaul	struct rpc_err err;
26412997Swpaul
26513375Swpaul	timeout.tv_sec = 5;
26612997Swpaul	timeout.tv_usec = 0;
26712997Swpaul	addr->sin_port = htons(port);
26812997Swpaul
26919131Swpaul	if ((clnt = clntudp_create(addr,prognum,1,timeout,&sock)) == NULL) {
27019131Swpaul		yp_error("%s: %s", inet_ntoa(addr->sin_addr),
27119161Swpaul		  clnt_spcreateerror("failed to establish callback handle"));
27219131Swpaul		return;
27319131Swpaul	}
27412997Swpaul
27512997Swpaul	ypxfr_resp.status = rval;
27612997Swpaul	ypxfr_resp.transid = transid;
27712997Swpaul
27813375Swpaul	/* Turn the timeout off -- we don't want to block. */
27913375Swpaul	timeout.tv_sec = 0;
28013375Swpaul	if (clnt_control(clnt, CLSET_TIMEOUT, (char *)&timeout) == FALSE)
28113375Swpaul		yp_error("failed to set timeout on ypproc_xfr callback");
28212997Swpaul
28313375Swpaul	if (yppushproc_xfrresp_1(&ypxfr_resp, clnt) == NULL) {
28413375Swpaul		clnt_geterr(clnt, &err);
28513375Swpaul		if (err.re_status != RPC_SUCCESS &&
28613375Swpaul		    err.re_status != RPC_TIMEDOUT)
28713375Swpaul			yp_error("%s", clnt_sperror(clnt,
28813375Swpaul				"ypxfr callback failed"));
28913375Swpaul	}
29013375Swpaul
29112997Swpaul	clnt_destroy(clnt);
29212997Swpaul	return;
29312997Swpaul}
29412997Swpaul
29515426Swpaul#define YPXFR_RETURN(CODE) 						\
29615426Swpaul	/* Order is important: send regular RPC reply, then callback */	\
29715426Swpaul	result.xfrstat = CODE; 						\
29815426Swpaul	svc_sendreply(rqstp->rq_xprt, xdr_ypresp_xfr, (char *)&result); \
29915426Swpaul	ypxfr_callback(CODE,rqhost,argp->transid, 			\
30015426Swpaul					argp->prog,argp->port); 	\
30115426Swpaul	return(NULL);
30215426Swpaul
30312891Swpaulypresp_xfr *
30412891Swpaulypproc_xfr_2_svc(ypreq_xfr *argp, struct svc_req *rqstp)
30512891Swpaul{
30612891Swpaul	static ypresp_xfr  result;
30712997Swpaul	struct sockaddr_in *rqhost;
30824780Swpaul	ypresp_master *mres;
30924780Swpaul	ypreq_nokey mreq;
31012891Swpaul
31113375Swpaul	result.transid = argp->transid;
31213375Swpaul	rqhost = svc_getcaller(rqstp->rq_xprt);
31313375Swpaul
31419161Swpaul#ifdef DB_CACHE
31519161Swpaul	if (yp_access(argp->map_parms.map,
31619161Swpaul			argp->map_parms.domain, (struct svc_req *)rqstp)) {
31719161Swpaul#else
31812891Swpaul	if (yp_access(argp->map_parms.map, (struct svc_req *)rqstp)) {
31919161Swpaul#endif
32024780Swpaul		YPXFR_RETURN(YPXFR_REFUSED)
32112891Swpaul	}
32212891Swpaul
32324780Swpaul
32412891Swpaul	if (argp->map_parms.domain == NULL) {
32524780Swpaul		YPXFR_RETURN(YPXFR_BADARGS)
32612891Swpaul	}
32712891Swpaul
32812891Swpaul	if (yp_validdomain(argp->map_parms.domain)) {
32924780Swpaul		YPXFR_RETURN(YPXFR_NODOM)
33012891Swpaul	}
33112891Swpaul
33224780Swpaul	/*
33324780Swpaul	 * Determine the master host ourselves. The caller may
33424780Swpaul	 * be up to no good. This has the side effect of verifying
33524780Swpaul	 * that the requested map and domain actually exist.
33624780Swpaul	 */
33724780Swpaul
33824780Swpaul	mreq.domain = argp->map_parms.domain;
33924780Swpaul	mreq.map = argp->map_parms.map;
34024780Swpaul
34124780Swpaul	mres = ypproc_master_2_svc(&mreq, rqstp);
34224780Swpaul
34324780Swpaul	if (mres->stat != YP_TRUE) {
34424780Swpaul		yp_error("couldn't find master for map %s@%s",
34524780Swpaul						argp->map_parms.map,
34624780Swpaul						argp->map_parms.domain);
34724780Swpaul		yp_error("host at %s (%s) may be pulling my leg",
34824780Swpaul						argp->map_parms.peer,
34924780Swpaul						inet_ntoa(rqhost->sin_addr));
35024780Swpaul		YPXFR_RETURN(YPXFR_REFUSED)
35124780Swpaul	}
35224780Swpaul
35312891Swpaul	switch(fork()) {
35412891Swpaul	case 0:
35512891Swpaul	{
35612891Swpaul		char g[11], t[11], p[11];
35712891Swpaul		char ypxfr_command[MAXPATHLEN + 2];
35812891Swpaul
35912891Swpaul		sprintf (ypxfr_command, "%sypxfr", _PATH_LIBEXEC);
36012891Swpaul		sprintf (t, "%u", argp->transid);
36112891Swpaul		sprintf (g, "%u", argp->prog);
36212891Swpaul		sprintf (p, "%u", argp->port);
36322321Swpaul		if (debug) {
36412997Swpaul			close(0); close(1); close(2);
36522321Swpaul		}
36612997Swpaul		if (strcmp(yp_dir, _PATH_YP)) {
36714304Swpaul			execl(ypxfr_command, "ypxfr",
36814304Swpaul			"-d", argp->map_parms.domain,
36924780Swpaul		      	"-h", mres->peer,
37014304Swpaul			"-p", yp_dir, "-C", t,
37114304Swpaul		      	g, inet_ntoa(rqhost->sin_addr),
37214304Swpaul			p, argp->map_parms.map,
37312997Swpaul		      	NULL);
37412997Swpaul		} else {
37514304Swpaul			execl(ypxfr_command, "ypxfr",
37614304Swpaul			"-d", argp->map_parms.domain,
37724780Swpaul		      	"-h", mres->peer,
37814304Swpaul			"-C", t,
37914304Swpaul		      	g, inet_ntoa(rqhost->sin_addr),
38014304Swpaul			p, argp->map_parms.map,
38112997Swpaul		      	NULL);
38212997Swpaul		}
38312997Swpaul		forked++;
38415426Swpaul		yp_error("ypxfr execl(%s): %s", ypxfr_command, strerror(errno));
38524780Swpaul		YPXFR_RETURN(YPXFR_XFRERR)
38612997Swpaul		break;
38712891Swpaul	}
38812891Swpaul	case -1:
38912891Swpaul		yp_error("ypxfr fork(): %s", strerror(errno));
39024780Swpaul		YPXFR_RETURN(YPXFR_XFRERR)
39112891Swpaul		break;
39212891Swpaul	default:
39313375Swpaul		result.xfrstat = YPXFR_SUCC;
39412997Swpaul		children++;
39512997Swpaul		forked = 0;
39612891Swpaul		break;
39712891Swpaul	}
39813375Swpaul
39913375Swpaul	return (&result);
40012891Swpaul}
40115426Swpaul#undef YPXFR_RETURN
40212891Swpaul
40312891Swpaulvoid *
40412891Swpaulypproc_clear_2_svc(void *argp, struct svc_req *rqstp)
40512891Swpaul{
40612891Swpaul	static char * result;
40712891Swpaul	static char rval = 0;
40812891Swpaul
40919161Swpaul#ifdef DB_CACHE
41019161Swpaul	if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
41119161Swpaul#else
41212891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
41319161Swpaul#endif
41412891Swpaul		return (NULL);
41515426Swpaul#ifdef DB_CACHE
41615426Swpaul	/* clear out the database cache */
41715426Swpaul	yp_flush_all();
41815426Swpaul#endif
41914240Swpaul	/* Re-read the securenets database for the hell of it. */
42014240Swpaul	load_securenets();
42114240Swpaul
42212891Swpaul	result = &rval;
42312891Swpaul	return((void *) &result);
42412891Swpaul}
42512891Swpaul
42612891Swpaul/*
42712891Swpaul * For ypproc_all, we have to send a stream of ypresp_all structures
42812891Swpaul * via TCP, but the XDR filter generated from the yp.x protocol
42912891Swpaul * definition file only serializes one such structure. This means that
43012891Swpaul * to send the whole stream, you need a wrapper which feeds all the
43112891Swpaul * records into the underlying XDR routine until it hits an 'EOF.'
43212891Swpaul * But to use the wrapper, you have to violate the boundaries between
43312891Swpaul * RPC layers by calling svc_sendreply() directly from the ypproc_all
43412891Swpaul * service routine instead of letting the RPC dispatcher do it.
43512891Swpaul *
43612891Swpaul * Bleah.
43712891Swpaul */
43812891Swpaul
43912891Swpaul/*
44020100Swpaul * Custom XDR routine for serialzing results of ypproc_all: keep
44120100Swpaul * reading from the database and spew until we run out of records
44220100Swpaul * or encounter an error.
44312891Swpaul */
44412891Swpaulstatic bool_t
44512891Swpaulxdr_my_ypresp_all(register XDR *xdrs, ypresp_all *objp)
44612891Swpaul{
44720100Swpaul	while (1) {
44820100Swpaul		/* Get a record. */
44920100Swpaul		if ((objp->ypresp_all_u.val.stat =
45020818Swpaul			yp_nextbykey(&objp->ypresp_all_u.val.key,
45120818Swpaul				     &objp->ypresp_all_u.val.val)) == YP_TRUE) {
45220100Swpaul			objp->more = TRUE;
45320100Swpaul		} else {
45420100Swpaul			objp->more = FALSE;
45520100Swpaul		}
45620100Swpaul
45720100Swpaul		/* Serialize. */
45820100Swpaul		if (!xdr_ypresp_all(xdrs, objp))
45920100Swpaul			return(FALSE);
46020100Swpaul		if (objp->more == FALSE)
46120100Swpaul			return(TRUE);
46220100Swpaul	}
46312891Swpaul}
46412891Swpaul
46512891Swpaulypresp_all *
46612891Swpaulypproc_all_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
46712891Swpaul{
46812891Swpaul	static ypresp_all  result;
46912891Swpaul
47012891Swpaul	/*
47112891Swpaul	 * Set this here so that the client will be forced to make
47212891Swpaul	 * at least one attempt to read from us even if all we're
47312891Swpaul	 * doing is returning an error.
47412891Swpaul	 */
47512891Swpaul	result.more = TRUE;
47614304Swpaul	result.ypresp_all_u.val.key.keydat_len = 0;
47714304Swpaul	result.ypresp_all_u.val.key.keydat_val = "";
47812891Swpaul
47919161Swpaul#ifdef DB_CACHE
48019161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
48119161Swpaul#else
48212891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
48319161Swpaul#endif
48412891Swpaul		result.ypresp_all_u.val.stat = YP_YPERR;
48512891Swpaul		return (&result);
48612891Swpaul	}
48712891Swpaul
48812891Swpaul	if (argp->domain == NULL || argp->map == NULL) {
48912891Swpaul		result.ypresp_all_u.val.stat = YP_BADARGS;
49012891Swpaul		return (&result);
49112891Swpaul	}
49212891Swpaul
49320100Swpaul	/*
49421389Swpaul	 * XXX If we hit the child limit, fail the request.
49521389Swpaul	 * If we don't, and the map is large, we could block for
49621389Swpaul	 * a long time in the parent.
49721389Swpaul	 */
49821389Swpaul	if (children >= MAX_CHILDREN) {
49921389Swpaul		result.ypresp_all_u.val.stat = YP_YPERR;
50021389Swpaul		return(&result);
50121389Swpaul	}
50221389Swpaul
50321389Swpaul	/*
50420100Swpaul	 * The ypproc_all procedure can take a while to complete.
50520100Swpaul	 * Best to handle it in a subprocess so the parent doesn't
50620100Swpaul	 * block. (Is there a better way to do this? Maybe with
50720100Swpaul	 * async socket I/O?)
50820100Swpaul	 */
50943847Swpaul	if (!debug) {
51043847Swpaul		switch(fork()) {
51143847Swpaul		case 0:
51243847Swpaul			forked++;
51343847Swpaul			break;
51443847Swpaul		case -1:
51543847Swpaul			yp_error("ypall fork(): %s", strerror(errno));
51643847Swpaul			result.ypresp_all_u.val.stat = YP_YPERR;
51743847Swpaul			return(&result);
51843847Swpaul			break;
51943847Swpaul		default:
52043847Swpaul			children++;
52143847Swpaul			forked = 0;
52243847Swpaul			return (NULL);
52343847Swpaul			break;
52443847Swpaul		}
52520100Swpaul	}
52620100Swpaul
52720818Swpaul	if (yp_select_map(argp->map, argp->domain,
52820818Swpaul				&result.ypresp_all_u.val.key, 0) != YP_TRUE) {
52912891Swpaul		result.ypresp_all_u.val.stat = yp_errno;
53012891Swpaul		return(&result);
53112891Swpaul	}
53212891Swpaul
53312891Swpaul	/* Kick off the actual data transfer. */
53420100Swpaul	svc_sendreply(rqstp->rq_xprt, xdr_my_ypresp_all, (char *)&result);
53520100Swpaul
53612891Swpaul	/*
53712891Swpaul	 * Returning NULL prevents the dispatcher from calling
53812891Swpaul	 * svc_sendreply() since we already did it.
53912891Swpaul	 */
54012891Swpaul	return (NULL);
54112891Swpaul}
54212891Swpaul
54312891Swpaulypresp_master *
54412891Swpaulypproc_master_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
54512891Swpaul{
54612891Swpaul	static ypresp_master  result;
54715426Swpaul	static char ypvalbuf[YPMAXRECORD];
54828042Swpaul	keydat key = { MASTER_SZ, MASTER_STRING };
54920818Swpaul	valdat val;
55012891Swpaul
55114303Swpaul	result.peer = "";
55214303Swpaul
55319161Swpaul#ifdef DB_CACHE
55419161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
55519161Swpaul#else
55619161Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
55719161Swpaul#endif
55812891Swpaul		result.stat = YP_YPERR;
55912891Swpaul		return(&result);
56012891Swpaul	}
56112891Swpaul
56212891Swpaul	if (argp->domain == NULL) {
56312891Swpaul		result.stat = YP_BADARGS;
56412891Swpaul		return (&result);
56512891Swpaul	}
56612891Swpaul
56720818Swpaul	if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) {
56820818Swpaul		result.stat = yp_errno;
56920818Swpaul		return(&result);
57020818Swpaul	}
57120818Swpaul
57215426Swpaul	/*
57315426Swpaul	 * Note that we copy the data retrieved from the database to
57415426Swpaul	 * a private buffer and NUL terminate the buffer rather than
57515426Swpaul	 * terminating the data in place. We do this because by stuffing
57615426Swpaul	 * a '\0' into data.data, we will actually be corrupting memory
57715426Swpaul	 * allocated by the DB package. This is a bad thing now that we
57815426Swpaul	 * cache DB handles rather than closing the database immediately.
57915426Swpaul	 */
58020818Swpaul	result.stat = yp_getbykey(&key, &val);
58120818Swpaul	if (result.stat == YP_TRUE) {
58220818Swpaul		bcopy((char *)val.valdat_val, (char *)&ypvalbuf,
58320818Swpaul						val.valdat_len);
58420818Swpaul		ypvalbuf[val.valdat_len] = '\0';
58515426Swpaul		result.peer = (char *)&ypvalbuf;
58612891Swpaul	} else
58712891Swpaul		result.peer = "";
58812891Swpaul
58912891Swpaul	return (&result);
59012891Swpaul}
59112891Swpaul
59212891Swpaulypresp_order *
59312891Swpaulypproc_order_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
59412891Swpaul{
59512891Swpaul	static ypresp_order  result;
59628042Swpaul	keydat key = { ORDER_SZ, ORDER_STRING };
59720818Swpaul	valdat val;
59812891Swpaul
59914304Swpaul	result.ordernum = 0;
60014304Swpaul
60119161Swpaul#ifdef DB_CACHE
60219161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
60319161Swpaul#else
60419161Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
60519161Swpaul#endif
60612891Swpaul		result.stat = YP_YPERR;
60712891Swpaul		return(&result);
60812891Swpaul	}
60912891Swpaul
61012891Swpaul	if (argp->domain == NULL) {
61112891Swpaul		result.stat = YP_BADARGS;
61212891Swpaul		return (&result);
61312891Swpaul	}
61412891Swpaul
61512891Swpaul	/*
61612891Swpaul	 * We could just check the timestamp on the map file,
61712891Swpaul	 * but that's a hack: we'll only know the last time the file
61812891Swpaul	 * was touched, not the last time the database contents were
61912891Swpaul	 * updated.
62012891Swpaul	 */
62112891Swpaul
62220818Swpaul	if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) {
62320818Swpaul		result.stat = yp_errno;
62420818Swpaul		return(&result);
62520818Swpaul	}
62620818Swpaul
62720818Swpaul	result.stat = yp_getbykey(&key, &val);
62820818Swpaul
62920818Swpaul	if (result.stat == YP_TRUE)
63020818Swpaul		result.ordernum = atoi((char *)val.valdat_val);
63112891Swpaul	else
63212891Swpaul		result.ordernum = 0;
63312891Swpaul
63412891Swpaul	return (&result);
63512891Swpaul}
63612891Swpaul
63712891Swpaulstatic void yp_maplist_free(yp_maplist)
63812891Swpaul	struct ypmaplist *yp_maplist;
63912891Swpaul{
64012891Swpaul	register struct ypmaplist *next;
64112891Swpaul
64212891Swpaul	while(yp_maplist) {
64312891Swpaul		next = yp_maplist->next;
64412891Swpaul		free(yp_maplist->map);
64512891Swpaul		free(yp_maplist);
64612891Swpaul		yp_maplist = next;
64712891Swpaul	}
64812891Swpaul	return;
64912891Swpaul}
65012891Swpaul
65112891Swpaulstatic struct ypmaplist *yp_maplist_create(domain)
65212891Swpaul	const char *domain;
65312891Swpaul{
65412891Swpaul	char yp_mapdir[MAXPATHLEN + 2];
65512891Swpaul	char yp_mapname[MAXPATHLEN + 2];
65612891Swpaul	struct ypmaplist *cur = NULL;
65712891Swpaul	struct ypmaplist *yp_maplist = NULL;
65812891Swpaul	DIR *dird;
65912891Swpaul	struct dirent *dirp;
66012891Swpaul	struct stat statbuf;
66112891Swpaul
66212891Swpaul	snprintf(yp_mapdir, sizeof(yp_mapdir), "%s/%s", yp_dir, domain);
66312891Swpaul
66412891Swpaul	if ((dird = opendir(yp_mapdir)) == NULL) {
66513800Swpaul		yp_error("opendir(%s) failed: %s", yp_mapdir, strerror(errno));
66612891Swpaul		return(NULL);
66712891Swpaul	}
66812891Swpaul
66912891Swpaul	while ((dirp = readdir(dird)) != NULL) {
67012891Swpaul		if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
67114304Swpaul			snprintf(yp_mapname, sizeof(yp_mapname), "%s/%s",
67214304Swpaul							yp_mapdir,dirp->d_name);
67314304Swpaul			if (stat(yp_mapname, &statbuf) < 0 ||
67414304Swpaul						!S_ISREG(statbuf.st_mode))
67512891Swpaul				continue;
67614304Swpaul			if ((cur = (struct ypmaplist *)
67716044Swpaul				malloc(sizeof(struct ypmaplist))) == NULL) {
67830827Scharnier				yp_error("malloc() failed");
67912891Swpaul				closedir(dird);
68012891Swpaul				yp_maplist_free(yp_maplist);
68112891Swpaul				return(NULL);
68212891Swpaul			}
68312891Swpaul			if ((cur->map = (char *)strdup(dirp->d_name)) == NULL) {
68414304Swpaul				yp_error("strdup() failed: %s",strerror(errno));
68512891Swpaul				closedir(dird);
68612891Swpaul				yp_maplist_free(yp_maplist);
68712891Swpaul				return(NULL);
68812891Swpaul			}
68912891Swpaul			cur->next = yp_maplist;
69012891Swpaul			yp_maplist = cur;
69112891Swpaul			if (debug)
69212891Swpaul				yp_error("map: %s", yp_maplist->map);
69312891Swpaul		}
69412891Swpaul
69512891Swpaul	}
69612891Swpaul	closedir(dird);
69712891Swpaul	return(yp_maplist);
69812891Swpaul}
69912891Swpaul
70012891Swpaulypresp_maplist *
70112891Swpaulypproc_maplist_2_svc(domainname *argp, struct svc_req *rqstp)
70212891Swpaul{
70315426Swpaul	static ypresp_maplist  result = { 0, NULL };
70412891Swpaul
70519161Swpaul#ifdef DB_CACHE
70619161Swpaul	if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) {
70719161Swpaul#else
70812891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp)) {
70919161Swpaul#endif
71012891Swpaul		result.stat = YP_YPERR;
71112891Swpaul		return(&result);
71212891Swpaul	}
71312891Swpaul
71412891Swpaul	if (argp == NULL) {
71512891Swpaul		result.stat = YP_BADARGS;
71612891Swpaul		return (&result);
71712891Swpaul	}
71812891Swpaul
71912891Swpaul	if (yp_validdomain(*argp)) {
72012891Swpaul		result.stat = YP_NODOM;
72112891Swpaul		return (&result);
72212891Swpaul	}
72312891Swpaul
72412891Swpaul	/*
72512891Swpaul	 * We have to construct a linked list for the ypproc_maplist
72612891Swpaul	 * procedure using dynamically allocated memory. Since the XDR
72712891Swpaul	 * layer won't free this list for us, we have to deal with it
72812891Swpaul	 * ourselves. We call yp_maplist_free() first to free any
72912891Swpaul	 * previously allocated data we may have accumulated to insure
73012891Swpaul	 * that we have only one linked list in memory at any given
73112891Swpaul	 * time.
73212891Swpaul	 */
73312891Swpaul
73412891Swpaul	yp_maplist_free(result.maps);
73512891Swpaul
73612891Swpaul	if ((result.maps = yp_maplist_create(*argp)) == NULL) {
73712891Swpaul		yp_error("yp_maplist_create failed");
73812891Swpaul		result.stat = YP_YPERR;
73912891Swpaul		return(&result);
74012891Swpaul	} else
74112891Swpaul		result.stat = YP_TRUE;
74212891Swpaul
74312891Swpaul	return (&result);
74412891Swpaul}
74514262Swpaul
74614262Swpaul/*
74714262Swpaul * NIS v1 support. The nullproc, domain and domain_nonack
74814262Swpaul * functions from v1 are identical to those in v2, so all
74914262Swpaul * we have to do is hand off to them.
75014262Swpaul *
75114262Swpaul * The other functions are mostly just wrappers around their v2
75214262Swpaul * counterparts. For example, for the v1 'match' procedure, we
75314262Swpaul * crack open the argument structure, make a request to the v2
75414262Swpaul * 'match' function, repackage the data into a v1 response and
75514262Swpaul * then send it on its way.
75614262Swpaul *
75714262Swpaul * Note that we don't support the pull, push and get procedures.
75814262Swpaul * There's little documentation available to show what they
75914262Swpaul * do, and I suspect they're meant largely for map transfers
76014262Swpaul * between master and slave servers.
76114262Swpaul */
76214262Swpaul
76314262Swpaulvoid *
76414262Swpaulypoldproc_null_1_svc(void *argp, struct svc_req *rqstp)
76514262Swpaul{
76614262Swpaul	return(ypproc_null_2_svc(argp, rqstp));
76714262Swpaul}
76814262Swpaul
76914262Swpaulbool_t *
77014262Swpaulypoldproc_domain_1_svc(domainname *argp, struct svc_req *rqstp)
77114262Swpaul{
77214262Swpaul	return(ypproc_domain_2_svc(argp, rqstp));
77314262Swpaul}
77414262Swpaul
77514262Swpaulbool_t *
77614262Swpaulypoldproc_domain_nonack_1_svc(domainname *argp, struct svc_req *rqstp)
77714262Swpaul{
77814262Swpaul	return (ypproc_domain_nonack_2_svc(argp, rqstp));
77914262Swpaul}
78014262Swpaul
78114304Swpaul/*
78214304Swpaul * the 'match' procedure sends a response of type YPRESP_VAL
78314304Swpaul */
78414262Swpaulypresponse *
78514262Swpaulypoldproc_match_1_svc(yprequest *argp, struct svc_req *rqstp)
78614262Swpaul{
78714262Swpaul	static ypresponse  result;
78814262Swpaul	ypresp_val *v2_result;
78914262Swpaul
79014262Swpaul	result.yp_resptype = YPRESP_VAL;
79114304Swpaul	result.ypresponse_u.yp_resp_valtype.val.valdat_val = "";
79214304Swpaul	result.ypresponse_u.yp_resp_valtype.val.valdat_len = 0;
79314262Swpaul
79414262Swpaul	if (argp->yp_reqtype != YPREQ_KEY) {
79514262Swpaul		result.ypresponse_u.yp_resp_valtype.stat = YP_BADARGS;
79614262Swpaul		return(&result);
79714262Swpaul	}
79814262Swpaul
79914262Swpaul	v2_result = ypproc_match_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp);
80014262Swpaul	if (v2_result == NULL)
80114262Swpaul		return(NULL);
80214262Swpaul
80314262Swpaul	bcopy((char *)v2_result,
80414262Swpaul	      (char *)&result.ypresponse_u.yp_resp_valtype,
80514262Swpaul	      sizeof(ypresp_val));
80614262Swpaul
80714262Swpaul	return (&result);
80814262Swpaul}
80914262Swpaul
81014304Swpaul/*
81114304Swpaul * the 'first' procedure sends a response of type YPRESP_KEY_VAL
81214304Swpaul */
81314262Swpaulypresponse *
81414262Swpaulypoldproc_first_1_svc(yprequest *argp, struct svc_req *rqstp)
81514262Swpaul{
81614262Swpaul	static ypresponse  result;
81714262Swpaul	ypresp_key_val *v2_result;
81814262Swpaul
81914262Swpaul	result.yp_resptype = YPRESP_KEY_VAL;
82014304Swpaul	result.ypresponse_u.yp_resp_key_valtype.val.valdat_val =
82114304Swpaul	result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = "";
82214304Swpaul	result.ypresponse_u.yp_resp_key_valtype.val.valdat_len =
82314304Swpaul	result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0;
82414262Swpaul
82514262Swpaul	if (argp->yp_reqtype != YPREQ_NOKEY) {
82614262Swpaul		result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS;
82714262Swpaul		return(&result);
82814262Swpaul	}
82914262Swpaul
83014262Swpaul	v2_result = ypproc_first_2_svc(&argp->yprequest_u.yp_req_nokeytype,
83114262Swpaul									rqstp);
83214262Swpaul	if (v2_result == NULL)
83314262Swpaul		return(NULL);
83414262Swpaul
83514262Swpaul	bcopy((char *)v2_result,
83614262Swpaul	      (char *)&result.ypresponse_u.yp_resp_key_valtype,
83714262Swpaul	      sizeof(ypresp_key_val));
83814262Swpaul
83914262Swpaul	return (&result);
84014262Swpaul}
84114262Swpaul
84214304Swpaul/*
84314304Swpaul * the 'next' procedure sends a response of type YPRESP_KEY_VAL
84414304Swpaul */
84514262Swpaulypresponse *
84614262Swpaulypoldproc_next_1_svc(yprequest *argp, struct svc_req *rqstp)
84714262Swpaul{
84814262Swpaul	static ypresponse  result;
84914262Swpaul	ypresp_key_val *v2_result;
85014262Swpaul
85114262Swpaul	result.yp_resptype = YPRESP_KEY_VAL;
85214304Swpaul	result.ypresponse_u.yp_resp_key_valtype.val.valdat_val =
85314304Swpaul	result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = "";
85414304Swpaul	result.ypresponse_u.yp_resp_key_valtype.val.valdat_len =
85514304Swpaul	result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0;
85614262Swpaul
85714262Swpaul	if (argp->yp_reqtype != YPREQ_KEY) {
85814262Swpaul		result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS;
85914262Swpaul		return(&result);
86014262Swpaul	}
86114262Swpaul
86214262Swpaul	v2_result = ypproc_next_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp);
86314262Swpaul	if (v2_result == NULL)
86414262Swpaul		return(NULL);
86514262Swpaul
86614262Swpaul	bcopy((char *)v2_result,
86714262Swpaul	      (char *)&result.ypresponse_u.yp_resp_key_valtype,
86814262Swpaul	      sizeof(ypresp_key_val));
86914262Swpaul
87014262Swpaul	return (&result);
87114262Swpaul}
87214262Swpaul
87314304Swpaul/*
87414304Swpaul * the 'poll' procedure sends a response of type YPRESP_MAP_PARMS
87514304Swpaul */
87614262Swpaulypresponse *
87714262Swpaulypoldproc_poll_1_svc(yprequest *argp, struct svc_req *rqstp)
87814262Swpaul{
87914262Swpaul	static ypresponse  result;
88014262Swpaul	ypresp_master *v2_result1;
88114262Swpaul	ypresp_order *v2_result2;
88214262Swpaul
88314262Swpaul	result.yp_resptype = YPRESP_MAP_PARMS;
88414262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.domain =
88514262Swpaul		argp->yprequest_u.yp_req_nokeytype.domain;
88614262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.map =
88714262Swpaul		argp->yprequest_u.yp_req_nokeytype.map;
88814262Swpaul	/*
88914262Swpaul	 * Hmm... there is no 'status' value in the
89014262Swpaul	 * yp_resp_map_parmstype structure, so I have to
89114262Swpaul	 * guess at what to do to indicate a failure.
89214262Swpaul	 * I hope this is right.
89314262Swpaul	 */
89414262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.ordernum = 0;
89514262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.peer = "";
89614262Swpaul
89714262Swpaul	if (argp->yp_reqtype != YPREQ_MAP_PARMS) {
89814262Swpaul		return(&result);
89914262Swpaul	}
90014262Swpaul
90114262Swpaul	v2_result1 = ypproc_master_2_svc(&argp->yprequest_u.yp_req_nokeytype,
90214262Swpaul									rqstp);
90314262Swpaul	if (v2_result1 == NULL)
90414262Swpaul		return(NULL);
90514262Swpaul
90614262Swpaul	if (v2_result1->stat != YP_TRUE) {
90714262Swpaul		return(&result);
90814262Swpaul	}
90914262Swpaul
91014262Swpaul	v2_result2 = ypproc_order_2_svc(&argp->yprequest_u.yp_req_nokeytype,
91114262Swpaul									rqstp);
91214262Swpaul	if (v2_result2 == NULL)
91314262Swpaul		return(NULL);
91414262Swpaul
91514262Swpaul	if (v2_result2->stat != YP_TRUE) {
91614262Swpaul		return(&result);
91714262Swpaul	}
91814262Swpaul
91914262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.peer =
92014262Swpaul		v2_result1->peer;
92114262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.ordernum =
92214262Swpaul		v2_result2->ordernum;
92314262Swpaul
92414262Swpaul	return (&result);
92514262Swpaul}
92614262Swpaul
92714262Swpaulypresponse *
92814262Swpaulypoldproc_push_1_svc(yprequest *argp, struct svc_req *rqstp)
92914262Swpaul{
93014262Swpaul	static ypresponse  result;
93114262Swpaul
93214262Swpaul	/*
93314262Swpaul	 * Not implemented.
93414262Swpaul	 */
93514262Swpaul
93614262Swpaul	return (&result);
93714262Swpaul}
93814262Swpaul
93914262Swpaulypresponse *
94014262Swpaulypoldproc_pull_1_svc(yprequest *argp, struct svc_req *rqstp)
94114262Swpaul{
94214262Swpaul	static ypresponse  result;
94314262Swpaul
94414262Swpaul	/*
94514262Swpaul	 * Not implemented.
94614262Swpaul	 */
94714262Swpaul
94814262Swpaul	return (&result);
94914262Swpaul}
95014262Swpaul
95114262Swpaulypresponse *
95214262Swpaulypoldproc_get_1_svc(yprequest *argp, struct svc_req *rqstp)
95314262Swpaul{
95414262Swpaul	static ypresponse  result;
95514262Swpaul
95614262Swpaul	/*
95714262Swpaul	 * Not implemented.
95814262Swpaul	 */
95914262Swpaul
96014262Swpaul	return (&result);
96114262Swpaul}
962