yp_server.c revision 46205
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[] =
3646186Swpaul	"$Id: yp_server.c,v 1.27 1999/02/10 16:16:14 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 children = 0;
5312891Swpaul
5428042Swpaul#define	MASTER_STRING	"YP_MASTER_NAME"
5528042Swpaul#define	MASTER_SZ	sizeof(MASTER_STRING) - 1
5628042Swpaul#define	ORDER_STRING	"YP_LAST_MODIFIED"
5728042Swpaul#define	ORDER_SZ	sizeof(ORDER_STRING) - 1
5828042Swpaul
5946186Swpaulstatic pid_t yp_fork()
6046186Swpaul{
6146186Swpaul	if (yp_pid != getpid()) {
6246186Swpaul		yp_error("child %d trying to fork!", getpid());
6346186Swpaul		errno = EEXIST;
6446186Swpaul		return(-1);
6546186Swpaul	}
6646186Swpaul
6746186Swpaul	return(fork());
6846186Swpaul}
6946186Swpaul
7014262Swpaul/*
7114262Swpaul * NIS v2 support. This is where most of the action happens.
7214262Swpaul */
7314262Swpaul
7412891Swpaulvoid *
7512891Swpaulypproc_null_2_svc(void *argp, struct svc_req *rqstp)
7612891Swpaul{
7712891Swpaul	static char * result;
7812891Swpaul	static char rval = 0;
7912891Swpaul
8019161Swpaul#ifdef DB_CACHE
8119161Swpaul	if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
8219161Swpaul#else
8312891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
8419161Swpaul#endif
8512891Swpaul		return(NULL);
8612891Swpaul
8712891Swpaul	result = &rval;
8812891Swpaul
8912891Swpaul	return((void *) &result);
9012891Swpaul}
9112891Swpaul
9212891Swpaulbool_t *
9312891Swpaulypproc_domain_2_svc(domainname *argp, struct svc_req *rqstp)
9412891Swpaul{
9512891Swpaul	static bool_t  result;
9612891Swpaul
9719161Swpaul#ifdef DB_CACHE
9819161Swpaul	if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) {
9919161Swpaul#else
10012891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp)) {
10119161Swpaul#endif
10212891Swpaul		result = FALSE;
10312891Swpaul		return (&result);
10412891Swpaul	}
10512891Swpaul
10612891Swpaul	if (argp == NULL || yp_validdomain(*argp))
10712891Swpaul		result = FALSE;
10812891Swpaul	else
10912891Swpaul		result = TRUE;
11012891Swpaul
11112891Swpaul	return (&result);
11212891Swpaul}
11312891Swpaul
11412891Swpaulbool_t *
11512891Swpaulypproc_domain_nonack_2_svc(domainname *argp, struct svc_req *rqstp)
11612891Swpaul{
11712891Swpaul	static bool_t  result;
11812891Swpaul
11919161Swpaul#ifdef DB_CACHE
12019161Swpaul	if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
12119161Swpaul#else
12212891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
12319161Swpaul#endif
12412891Swpaul		return (NULL);
12512891Swpaul
12612891Swpaul	if (argp == NULL || yp_validdomain(*argp))
12712891Swpaul		return (NULL);
12812891Swpaul	else
12912891Swpaul		result = TRUE;
13012891Swpaul
13112891Swpaul	return (&result);
13212891Swpaul}
13312891Swpaul
13412891Swpaulypresp_val *
13512891Swpaulypproc_match_2_svc(ypreq_key *argp, struct svc_req *rqstp)
13612891Swpaul{
13712891Swpaul	static ypresp_val  result;
13812891Swpaul
13914304Swpaul	result.val.valdat_val = "";
14014304Swpaul	result.val.valdat_len = 0;
14119161Swpaul
14219161Swpaul#ifdef DB_CACHE
14319161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
14419161Swpaul#else
14512891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
14619161Swpaul#endif
14712891Swpaul		result.stat = YP_YPERR;
14812891Swpaul		return (&result);
14912891Swpaul	}
15012891Swpaul
15112891Swpaul	if (argp->domain == NULL || argp->map == NULL) {
15212891Swpaul		result.stat = YP_BADARGS;
15312891Swpaul		return (&result);
15412891Swpaul	}
15512891Swpaul
15633250Swpaul	if (yp_select_map(argp->map, argp->domain, NULL, 1) != YP_TRUE) {
15720818Swpaul		result.stat = yp_errno;
15820818Swpaul		return(&result);
15912891Swpaul	}
16012891Swpaul
16120818Swpaul	result.stat = yp_getbykey(&argp->key, &result.val);
16220818Swpaul
16312891Swpaul	/*
16412891Swpaul	 * Do DNS lookups for hosts maps if database lookup failed.
16512891Swpaul	 */
16612891Swpaul
16719161Swpaul#ifdef DB_CACHE
16819161Swpaul	if (result.stat != YP_TRUE &&
16919161Swpaul	    (yp_testflag(argp->map, argp->domain, YP_INTERDOMAIN) ||
17019161Swpaul	    (strstr(argp->map, "hosts") && do_dns))) {
17119161Swpaul#else
17212891Swpaul	if (do_dns && result.stat != YP_TRUE && strstr(argp->map, "hosts")) {
17319161Swpaul#endif
17427589Swpaul		char			nbuf[YPMAXRECORD];
17520818Swpaul
17620818Swpaul		/* NUL terminate! NUL terminate!! NUL TERMINATE!!! */
17727589Swpaul		bcopy(argp->key.keydat_val, nbuf, argp->key.keydat_len);
17827589Swpaul		nbuf[argp->key.keydat_len] = '\0';
17912891Swpaul
18012891Swpaul		if (debug)
18130827Scharnier			yp_error("doing DNS lookup of %s", nbuf);
18212891Swpaul
18312891Swpaul		if (!strcmp(argp->map, "hosts.byname"))
18427589Swpaul			result.stat = yp_async_lookup_name(rqstp, nbuf);
18512891Swpaul		else if (!strcmp(argp->map, "hosts.byaddr"))
18627589Swpaul			result.stat = yp_async_lookup_addr(rqstp, nbuf);
18712891Swpaul
18820818Swpaul		if (result.stat == YP_TRUE)
18920818Swpaul			return(NULL);
19012891Swpaul	}
19112891Swpaul
19212891Swpaul	return (&result);
19312891Swpaul}
19412891Swpaul
19512891Swpaulypresp_key_val *
19612891Swpaulypproc_first_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
19712891Swpaul{
19812891Swpaul	static ypresp_key_val  result;
19912891Swpaul
20014304Swpaul	result.val.valdat_val = result.key.keydat_val = "";
20114304Swpaul	result.val.valdat_len = result.key.keydat_len = 0;
20219161Swpaul
20319161Swpaul#ifdef DB_CACHE
20419161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
20519161Swpaul#else
20612891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
20719161Swpaul#endif
20812891Swpaul		result.stat = YP_YPERR;
20912891Swpaul		return (&result);
21012891Swpaul	}
21112891Swpaul
21212891Swpaul	if (argp->domain == NULL) {
21312891Swpaul		result.stat = YP_BADARGS;
21412891Swpaul		return (&result);
21512891Swpaul	}
21612891Swpaul
21733250Swpaul	if (yp_select_map(argp->map, argp->domain, NULL, 0) != YP_TRUE) {
21812891Swpaul		result.stat = yp_errno;
21912891Swpaul		return(&result);
22012891Swpaul	}
22112891Swpaul
22220818Swpaul	result.stat = yp_firstbykey(&result.key, &result.val);
22312891Swpaul
22412891Swpaul	return (&result);
22512891Swpaul}
22612891Swpaul
22712891Swpaulypresp_key_val *
22812891Swpaulypproc_next_2_svc(ypreq_key *argp, struct svc_req *rqstp)
22912891Swpaul{
23012891Swpaul	static ypresp_key_val  result;
23112891Swpaul
23214304Swpaul	result.val.valdat_val = result.key.keydat_val = "";
23314304Swpaul	result.val.valdat_len = result.key.keydat_len = 0;
23415426Swpaul
23519161Swpaul#ifdef DB_CACHE
23619161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
23719161Swpaul#else
23812891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
23919161Swpaul#endif
24012891Swpaul		result.stat = YP_YPERR;
24112891Swpaul		return (&result);
24212891Swpaul	}
24312891Swpaul
24412891Swpaul	if (argp->domain == NULL || argp->map == NULL) {
24512891Swpaul		result.stat = YP_BADARGS;
24612891Swpaul		return (&result);
24712891Swpaul	}
24812891Swpaul
24920818Swpaul	if (yp_select_map(argp->map, argp->domain, &argp->key, 0) != YP_TRUE) {
25012891Swpaul		result.stat = yp_errno;
25112891Swpaul		return(&result);
25212891Swpaul	}
25312891Swpaul
25420818Swpaul	result.key.keydat_len = argp->key.keydat_len;
25520818Swpaul	result.key.keydat_val = argp->key.keydat_val;
25612891Swpaul
25720818Swpaul	result.stat = yp_nextbykey(&result.key, &result.val);
25820818Swpaul
25912891Swpaul	return (&result);
26012891Swpaul}
26112891Swpaul
26212997Swpaulstatic void ypxfr_callback(rval,addr,transid,prognum,port)
26312997Swpaul	ypxfrstat rval;
26412997Swpaul	struct sockaddr_in *addr;
26512997Swpaul	unsigned int transid;
26612997Swpaul	unsigned int prognum;
26712997Swpaul	unsigned long port;
26812997Swpaul{
26912997Swpaul	CLIENT *clnt;
27012997Swpaul	int sock = RPC_ANYSOCK;
27112997Swpaul	struct timeval timeout;
27212997Swpaul	yppushresp_xfr ypxfr_resp;
27313375Swpaul	struct rpc_err err;
27412997Swpaul
27513375Swpaul	timeout.tv_sec = 5;
27612997Swpaul	timeout.tv_usec = 0;
27712997Swpaul	addr->sin_port = htons(port);
27812997Swpaul
27919131Swpaul	if ((clnt = clntudp_create(addr,prognum,1,timeout,&sock)) == NULL) {
28019131Swpaul		yp_error("%s: %s", inet_ntoa(addr->sin_addr),
28119161Swpaul		  clnt_spcreateerror("failed to establish callback handle"));
28219131Swpaul		return;
28319131Swpaul	}
28412997Swpaul
28512997Swpaul	ypxfr_resp.status = rval;
28612997Swpaul	ypxfr_resp.transid = transid;
28712997Swpaul
28813375Swpaul	/* Turn the timeout off -- we don't want to block. */
28913375Swpaul	timeout.tv_sec = 0;
29013375Swpaul	if (clnt_control(clnt, CLSET_TIMEOUT, (char *)&timeout) == FALSE)
29113375Swpaul		yp_error("failed to set timeout on ypproc_xfr callback");
29212997Swpaul
29313375Swpaul	if (yppushproc_xfrresp_1(&ypxfr_resp, clnt) == NULL) {
29413375Swpaul		clnt_geterr(clnt, &err);
29513375Swpaul		if (err.re_status != RPC_SUCCESS &&
29613375Swpaul		    err.re_status != RPC_TIMEDOUT)
29713375Swpaul			yp_error("%s", clnt_sperror(clnt,
29813375Swpaul				"ypxfr callback failed"));
29913375Swpaul	}
30013375Swpaul
30112997Swpaul	clnt_destroy(clnt);
30212997Swpaul	return;
30312997Swpaul}
30412997Swpaul
30515426Swpaul#define YPXFR_RETURN(CODE) 						\
30615426Swpaul	/* Order is important: send regular RPC reply, then callback */	\
30715426Swpaul	result.xfrstat = CODE; 						\
30815426Swpaul	svc_sendreply(rqstp->rq_xprt, xdr_ypresp_xfr, (char *)&result); \
30915426Swpaul	ypxfr_callback(CODE,rqhost,argp->transid, 			\
31015426Swpaul					argp->prog,argp->port); 	\
31115426Swpaul	return(NULL);
31215426Swpaul
31312891Swpaulypresp_xfr *
31412891Swpaulypproc_xfr_2_svc(ypreq_xfr *argp, struct svc_req *rqstp)
31512891Swpaul{
31612891Swpaul	static ypresp_xfr  result;
31712997Swpaul	struct sockaddr_in *rqhost;
31824780Swpaul	ypresp_master *mres;
31924780Swpaul	ypreq_nokey mreq;
32012891Swpaul
32113375Swpaul	result.transid = argp->transid;
32213375Swpaul	rqhost = svc_getcaller(rqstp->rq_xprt);
32313375Swpaul
32419161Swpaul#ifdef DB_CACHE
32519161Swpaul	if (yp_access(argp->map_parms.map,
32619161Swpaul			argp->map_parms.domain, (struct svc_req *)rqstp)) {
32719161Swpaul#else
32812891Swpaul	if (yp_access(argp->map_parms.map, (struct svc_req *)rqstp)) {
32919161Swpaul#endif
33024780Swpaul		YPXFR_RETURN(YPXFR_REFUSED)
33112891Swpaul	}
33212891Swpaul
33324780Swpaul
33412891Swpaul	if (argp->map_parms.domain == NULL) {
33524780Swpaul		YPXFR_RETURN(YPXFR_BADARGS)
33612891Swpaul	}
33712891Swpaul
33812891Swpaul	if (yp_validdomain(argp->map_parms.domain)) {
33924780Swpaul		YPXFR_RETURN(YPXFR_NODOM)
34012891Swpaul	}
34112891Swpaul
34224780Swpaul	/*
34324780Swpaul	 * Determine the master host ourselves. The caller may
34424780Swpaul	 * be up to no good. This has the side effect of verifying
34524780Swpaul	 * that the requested map and domain actually exist.
34624780Swpaul	 */
34724780Swpaul
34824780Swpaul	mreq.domain = argp->map_parms.domain;
34924780Swpaul	mreq.map = argp->map_parms.map;
35024780Swpaul
35124780Swpaul	mres = ypproc_master_2_svc(&mreq, rqstp);
35224780Swpaul
35324780Swpaul	if (mres->stat != YP_TRUE) {
35424780Swpaul		yp_error("couldn't find master for map %s@%s",
35524780Swpaul						argp->map_parms.map,
35624780Swpaul						argp->map_parms.domain);
35724780Swpaul		yp_error("host at %s (%s) may be pulling my leg",
35824780Swpaul						argp->map_parms.peer,
35924780Swpaul						inet_ntoa(rqhost->sin_addr));
36024780Swpaul		YPXFR_RETURN(YPXFR_REFUSED)
36124780Swpaul	}
36224780Swpaul
36346186Swpaul	switch(yp_fork()) {
36412891Swpaul	case 0:
36512891Swpaul	{
36612891Swpaul		char g[11], t[11], p[11];
36712891Swpaul		char ypxfr_command[MAXPATHLEN + 2];
36812891Swpaul
36912891Swpaul		sprintf (ypxfr_command, "%sypxfr", _PATH_LIBEXEC);
37012891Swpaul		sprintf (t, "%u", argp->transid);
37112891Swpaul		sprintf (g, "%u", argp->prog);
37212891Swpaul		sprintf (p, "%u", argp->port);
37322321Swpaul		if (debug) {
37412997Swpaul			close(0); close(1); close(2);
37522321Swpaul		}
37612997Swpaul		if (strcmp(yp_dir, _PATH_YP)) {
37714304Swpaul			execl(ypxfr_command, "ypxfr",
37814304Swpaul			"-d", argp->map_parms.domain,
37924780Swpaul		      	"-h", mres->peer,
38014304Swpaul			"-p", yp_dir, "-C", t,
38114304Swpaul		      	g, inet_ntoa(rqhost->sin_addr),
38214304Swpaul			p, argp->map_parms.map,
38312997Swpaul		      	NULL);
38412997Swpaul		} else {
38514304Swpaul			execl(ypxfr_command, "ypxfr",
38614304Swpaul			"-d", argp->map_parms.domain,
38724780Swpaul		      	"-h", mres->peer,
38814304Swpaul			"-C", t,
38914304Swpaul		      	g, inet_ntoa(rqhost->sin_addr),
39014304Swpaul			p, argp->map_parms.map,
39112997Swpaul		      	NULL);
39212997Swpaul		}
39315426Swpaul		yp_error("ypxfr execl(%s): %s", ypxfr_command, strerror(errno));
39424780Swpaul		YPXFR_RETURN(YPXFR_XFRERR)
39546205Swpaul		/*
39646205Swpaul		 * Just to safe, prevent PR #10970 from biting us in
39746205Swpaul		 * the unlikely case that execing ypxfr fails. We don't
39846205Swpaul		 * want to have any child processes spawned from this
39946205Swpaul		 * child process.
40046205Swpaul		 */
40146205Swpaul		_exit(0);
40212997Swpaul		break;
40312891Swpaul	}
40412891Swpaul	case -1:
40512891Swpaul		yp_error("ypxfr fork(): %s", strerror(errno));
40624780Swpaul		YPXFR_RETURN(YPXFR_XFRERR)
40712891Swpaul		break;
40812891Swpaul	default:
40913375Swpaul		result.xfrstat = YPXFR_SUCC;
41012997Swpaul		children++;
41112891Swpaul		break;
41212891Swpaul	}
41313375Swpaul
41413375Swpaul	return (&result);
41512891Swpaul}
41615426Swpaul#undef YPXFR_RETURN
41712891Swpaul
41812891Swpaulvoid *
41912891Swpaulypproc_clear_2_svc(void *argp, struct svc_req *rqstp)
42012891Swpaul{
42112891Swpaul	static char * result;
42212891Swpaul	static char rval = 0;
42312891Swpaul
42419161Swpaul#ifdef DB_CACHE
42519161Swpaul	if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
42619161Swpaul#else
42712891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
42819161Swpaul#endif
42912891Swpaul		return (NULL);
43015426Swpaul#ifdef DB_CACHE
43115426Swpaul	/* clear out the database cache */
43215426Swpaul	yp_flush_all();
43315426Swpaul#endif
43414240Swpaul	/* Re-read the securenets database for the hell of it. */
43514240Swpaul	load_securenets();
43614240Swpaul
43712891Swpaul	result = &rval;
43812891Swpaul	return((void *) &result);
43912891Swpaul}
44012891Swpaul
44112891Swpaul/*
44212891Swpaul * For ypproc_all, we have to send a stream of ypresp_all structures
44312891Swpaul * via TCP, but the XDR filter generated from the yp.x protocol
44412891Swpaul * definition file only serializes one such structure. This means that
44512891Swpaul * to send the whole stream, you need a wrapper which feeds all the
44612891Swpaul * records into the underlying XDR routine until it hits an 'EOF.'
44712891Swpaul * But to use the wrapper, you have to violate the boundaries between
44812891Swpaul * RPC layers by calling svc_sendreply() directly from the ypproc_all
44912891Swpaul * service routine instead of letting the RPC dispatcher do it.
45012891Swpaul *
45112891Swpaul * Bleah.
45212891Swpaul */
45312891Swpaul
45412891Swpaul/*
45520100Swpaul * Custom XDR routine for serialzing results of ypproc_all: keep
45620100Swpaul * reading from the database and spew until we run out of records
45720100Swpaul * or encounter an error.
45812891Swpaul */
45912891Swpaulstatic bool_t
46012891Swpaulxdr_my_ypresp_all(register XDR *xdrs, ypresp_all *objp)
46112891Swpaul{
46220100Swpaul	while (1) {
46320100Swpaul		/* Get a record. */
46420100Swpaul		if ((objp->ypresp_all_u.val.stat =
46520818Swpaul			yp_nextbykey(&objp->ypresp_all_u.val.key,
46620818Swpaul				     &objp->ypresp_all_u.val.val)) == YP_TRUE) {
46720100Swpaul			objp->more = TRUE;
46820100Swpaul		} else {
46920100Swpaul			objp->more = FALSE;
47020100Swpaul		}
47120100Swpaul
47220100Swpaul		/* Serialize. */
47320100Swpaul		if (!xdr_ypresp_all(xdrs, objp))
47420100Swpaul			return(FALSE);
47520100Swpaul		if (objp->more == FALSE)
47620100Swpaul			return(TRUE);
47720100Swpaul	}
47812891Swpaul}
47912891Swpaul
48012891Swpaulypresp_all *
48112891Swpaulypproc_all_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
48212891Swpaul{
48312891Swpaul	static ypresp_all  result;
48412891Swpaul
48512891Swpaul	/*
48612891Swpaul	 * Set this here so that the client will be forced to make
48712891Swpaul	 * at least one attempt to read from us even if all we're
48812891Swpaul	 * doing is returning an error.
48912891Swpaul	 */
49012891Swpaul	result.more = TRUE;
49114304Swpaul	result.ypresp_all_u.val.key.keydat_len = 0;
49214304Swpaul	result.ypresp_all_u.val.key.keydat_val = "";
49312891Swpaul
49419161Swpaul#ifdef DB_CACHE
49519161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
49619161Swpaul#else
49712891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
49819161Swpaul#endif
49912891Swpaul		result.ypresp_all_u.val.stat = YP_YPERR;
50012891Swpaul		return (&result);
50112891Swpaul	}
50212891Swpaul
50312891Swpaul	if (argp->domain == NULL || argp->map == NULL) {
50412891Swpaul		result.ypresp_all_u.val.stat = YP_BADARGS;
50512891Swpaul		return (&result);
50612891Swpaul	}
50712891Swpaul
50820100Swpaul	/*
50921389Swpaul	 * XXX If we hit the child limit, fail the request.
51021389Swpaul	 * If we don't, and the map is large, we could block for
51121389Swpaul	 * a long time in the parent.
51221389Swpaul	 */
51321389Swpaul	if (children >= MAX_CHILDREN) {
51421389Swpaul		result.ypresp_all_u.val.stat = YP_YPERR;
51521389Swpaul		return(&result);
51621389Swpaul	}
51721389Swpaul
51821389Swpaul	/*
51920100Swpaul	 * The ypproc_all procedure can take a while to complete.
52020100Swpaul	 * Best to handle it in a subprocess so the parent doesn't
52120100Swpaul	 * block. (Is there a better way to do this? Maybe with
52220100Swpaul	 * async socket I/O?)
52320100Swpaul	 */
52443847Swpaul	if (!debug) {
52546186Swpaul		switch(yp_fork()) {
52643847Swpaul		case 0:
52743847Swpaul			break;
52843847Swpaul		case -1:
52943847Swpaul			yp_error("ypall fork(): %s", strerror(errno));
53043847Swpaul			result.ypresp_all_u.val.stat = YP_YPERR;
53143847Swpaul			return(&result);
53243847Swpaul			break;
53343847Swpaul		default:
53443847Swpaul			children++;
53543847Swpaul			return (NULL);
53643847Swpaul			break;
53743847Swpaul		}
53820100Swpaul	}
53920100Swpaul
54020818Swpaul	if (yp_select_map(argp->map, argp->domain,
54120818Swpaul				&result.ypresp_all_u.val.key, 0) != YP_TRUE) {
54212891Swpaul		result.ypresp_all_u.val.stat = yp_errno;
54312891Swpaul		return(&result);
54412891Swpaul	}
54512891Swpaul
54612891Swpaul	/* Kick off the actual data transfer. */
54720100Swpaul	svc_sendreply(rqstp->rq_xprt, xdr_my_ypresp_all, (char *)&result);
54820100Swpaul
54912891Swpaul	/*
55046205Swpaul	 * Proper fix for PR #10970: exit here so that we don't risk
55146205Swpaul	 * having a child spawned from this sub-process.
55212891Swpaul	 */
55346205Swpaul	_exit(0);
55412891Swpaul}
55512891Swpaul
55612891Swpaulypresp_master *
55712891Swpaulypproc_master_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
55812891Swpaul{
55912891Swpaul	static ypresp_master  result;
56015426Swpaul	static char ypvalbuf[YPMAXRECORD];
56128042Swpaul	keydat key = { MASTER_SZ, MASTER_STRING };
56220818Swpaul	valdat val;
56312891Swpaul
56414303Swpaul	result.peer = "";
56514303Swpaul
56619161Swpaul#ifdef DB_CACHE
56719161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
56819161Swpaul#else
56919161Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
57019161Swpaul#endif
57112891Swpaul		result.stat = YP_YPERR;
57212891Swpaul		return(&result);
57312891Swpaul	}
57412891Swpaul
57512891Swpaul	if (argp->domain == NULL) {
57612891Swpaul		result.stat = YP_BADARGS;
57712891Swpaul		return (&result);
57812891Swpaul	}
57912891Swpaul
58020818Swpaul	if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) {
58120818Swpaul		result.stat = yp_errno;
58220818Swpaul		return(&result);
58320818Swpaul	}
58420818Swpaul
58515426Swpaul	/*
58615426Swpaul	 * Note that we copy the data retrieved from the database to
58715426Swpaul	 * a private buffer and NUL terminate the buffer rather than
58815426Swpaul	 * terminating the data in place. We do this because by stuffing
58915426Swpaul	 * a '\0' into data.data, we will actually be corrupting memory
59015426Swpaul	 * allocated by the DB package. This is a bad thing now that we
59115426Swpaul	 * cache DB handles rather than closing the database immediately.
59215426Swpaul	 */
59320818Swpaul	result.stat = yp_getbykey(&key, &val);
59420818Swpaul	if (result.stat == YP_TRUE) {
59520818Swpaul		bcopy((char *)val.valdat_val, (char *)&ypvalbuf,
59620818Swpaul						val.valdat_len);
59720818Swpaul		ypvalbuf[val.valdat_len] = '\0';
59815426Swpaul		result.peer = (char *)&ypvalbuf;
59912891Swpaul	} else
60012891Swpaul		result.peer = "";
60112891Swpaul
60212891Swpaul	return (&result);
60312891Swpaul}
60412891Swpaul
60512891Swpaulypresp_order *
60612891Swpaulypproc_order_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
60712891Swpaul{
60812891Swpaul	static ypresp_order  result;
60928042Swpaul	keydat key = { ORDER_SZ, ORDER_STRING };
61020818Swpaul	valdat val;
61112891Swpaul
61214304Swpaul	result.ordernum = 0;
61314304Swpaul
61419161Swpaul#ifdef DB_CACHE
61519161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
61619161Swpaul#else
61719161Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
61819161Swpaul#endif
61912891Swpaul		result.stat = YP_YPERR;
62012891Swpaul		return(&result);
62112891Swpaul	}
62212891Swpaul
62312891Swpaul	if (argp->domain == NULL) {
62412891Swpaul		result.stat = YP_BADARGS;
62512891Swpaul		return (&result);
62612891Swpaul	}
62712891Swpaul
62812891Swpaul	/*
62912891Swpaul	 * We could just check the timestamp on the map file,
63012891Swpaul	 * but that's a hack: we'll only know the last time the file
63112891Swpaul	 * was touched, not the last time the database contents were
63212891Swpaul	 * updated.
63312891Swpaul	 */
63412891Swpaul
63520818Swpaul	if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) {
63620818Swpaul		result.stat = yp_errno;
63720818Swpaul		return(&result);
63820818Swpaul	}
63920818Swpaul
64020818Swpaul	result.stat = yp_getbykey(&key, &val);
64120818Swpaul
64220818Swpaul	if (result.stat == YP_TRUE)
64320818Swpaul		result.ordernum = atoi((char *)val.valdat_val);
64412891Swpaul	else
64512891Swpaul		result.ordernum = 0;
64612891Swpaul
64712891Swpaul	return (&result);
64812891Swpaul}
64912891Swpaul
65012891Swpaulstatic void yp_maplist_free(yp_maplist)
65112891Swpaul	struct ypmaplist *yp_maplist;
65212891Swpaul{
65312891Swpaul	register struct ypmaplist *next;
65412891Swpaul
65512891Swpaul	while(yp_maplist) {
65612891Swpaul		next = yp_maplist->next;
65712891Swpaul		free(yp_maplist->map);
65812891Swpaul		free(yp_maplist);
65912891Swpaul		yp_maplist = next;
66012891Swpaul	}
66112891Swpaul	return;
66212891Swpaul}
66312891Swpaul
66412891Swpaulstatic struct ypmaplist *yp_maplist_create(domain)
66512891Swpaul	const char *domain;
66612891Swpaul{
66712891Swpaul	char yp_mapdir[MAXPATHLEN + 2];
66812891Swpaul	char yp_mapname[MAXPATHLEN + 2];
66912891Swpaul	struct ypmaplist *cur = NULL;
67012891Swpaul	struct ypmaplist *yp_maplist = NULL;
67112891Swpaul	DIR *dird;
67212891Swpaul	struct dirent *dirp;
67312891Swpaul	struct stat statbuf;
67412891Swpaul
67512891Swpaul	snprintf(yp_mapdir, sizeof(yp_mapdir), "%s/%s", yp_dir, domain);
67612891Swpaul
67712891Swpaul	if ((dird = opendir(yp_mapdir)) == NULL) {
67813800Swpaul		yp_error("opendir(%s) failed: %s", yp_mapdir, strerror(errno));
67912891Swpaul		return(NULL);
68012891Swpaul	}
68112891Swpaul
68212891Swpaul	while ((dirp = readdir(dird)) != NULL) {
68312891Swpaul		if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
68414304Swpaul			snprintf(yp_mapname, sizeof(yp_mapname), "%s/%s",
68514304Swpaul							yp_mapdir,dirp->d_name);
68614304Swpaul			if (stat(yp_mapname, &statbuf) < 0 ||
68714304Swpaul						!S_ISREG(statbuf.st_mode))
68812891Swpaul				continue;
68914304Swpaul			if ((cur = (struct ypmaplist *)
69016044Swpaul				malloc(sizeof(struct ypmaplist))) == NULL) {
69130827Scharnier				yp_error("malloc() failed");
69212891Swpaul				closedir(dird);
69312891Swpaul				yp_maplist_free(yp_maplist);
69412891Swpaul				return(NULL);
69512891Swpaul			}
69612891Swpaul			if ((cur->map = (char *)strdup(dirp->d_name)) == NULL) {
69714304Swpaul				yp_error("strdup() failed: %s",strerror(errno));
69812891Swpaul				closedir(dird);
69912891Swpaul				yp_maplist_free(yp_maplist);
70012891Swpaul				return(NULL);
70112891Swpaul			}
70212891Swpaul			cur->next = yp_maplist;
70312891Swpaul			yp_maplist = cur;
70412891Swpaul			if (debug)
70512891Swpaul				yp_error("map: %s", yp_maplist->map);
70612891Swpaul		}
70712891Swpaul
70812891Swpaul	}
70912891Swpaul	closedir(dird);
71012891Swpaul	return(yp_maplist);
71112891Swpaul}
71212891Swpaul
71312891Swpaulypresp_maplist *
71412891Swpaulypproc_maplist_2_svc(domainname *argp, struct svc_req *rqstp)
71512891Swpaul{
71615426Swpaul	static ypresp_maplist  result = { 0, NULL };
71712891Swpaul
71819161Swpaul#ifdef DB_CACHE
71919161Swpaul	if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) {
72019161Swpaul#else
72112891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp)) {
72219161Swpaul#endif
72312891Swpaul		result.stat = YP_YPERR;
72412891Swpaul		return(&result);
72512891Swpaul	}
72612891Swpaul
72712891Swpaul	if (argp == NULL) {
72812891Swpaul		result.stat = YP_BADARGS;
72912891Swpaul		return (&result);
73012891Swpaul	}
73112891Swpaul
73212891Swpaul	if (yp_validdomain(*argp)) {
73312891Swpaul		result.stat = YP_NODOM;
73412891Swpaul		return (&result);
73512891Swpaul	}
73612891Swpaul
73712891Swpaul	/*
73812891Swpaul	 * We have to construct a linked list for the ypproc_maplist
73912891Swpaul	 * procedure using dynamically allocated memory. Since the XDR
74012891Swpaul	 * layer won't free this list for us, we have to deal with it
74112891Swpaul	 * ourselves. We call yp_maplist_free() first to free any
74212891Swpaul	 * previously allocated data we may have accumulated to insure
74312891Swpaul	 * that we have only one linked list in memory at any given
74412891Swpaul	 * time.
74512891Swpaul	 */
74612891Swpaul
74712891Swpaul	yp_maplist_free(result.maps);
74812891Swpaul
74912891Swpaul	if ((result.maps = yp_maplist_create(*argp)) == NULL) {
75012891Swpaul		yp_error("yp_maplist_create failed");
75112891Swpaul		result.stat = YP_YPERR;
75212891Swpaul		return(&result);
75312891Swpaul	} else
75412891Swpaul		result.stat = YP_TRUE;
75512891Swpaul
75612891Swpaul	return (&result);
75712891Swpaul}
75814262Swpaul
75914262Swpaul/*
76014262Swpaul * NIS v1 support. The nullproc, domain and domain_nonack
76114262Swpaul * functions from v1 are identical to those in v2, so all
76214262Swpaul * we have to do is hand off to them.
76314262Swpaul *
76414262Swpaul * The other functions are mostly just wrappers around their v2
76514262Swpaul * counterparts. For example, for the v1 'match' procedure, we
76614262Swpaul * crack open the argument structure, make a request to the v2
76714262Swpaul * 'match' function, repackage the data into a v1 response and
76814262Swpaul * then send it on its way.
76914262Swpaul *
77014262Swpaul * Note that we don't support the pull, push and get procedures.
77114262Swpaul * There's little documentation available to show what they
77214262Swpaul * do, and I suspect they're meant largely for map transfers
77314262Swpaul * between master and slave servers.
77414262Swpaul */
77514262Swpaul
77614262Swpaulvoid *
77714262Swpaulypoldproc_null_1_svc(void *argp, struct svc_req *rqstp)
77814262Swpaul{
77914262Swpaul	return(ypproc_null_2_svc(argp, rqstp));
78014262Swpaul}
78114262Swpaul
78214262Swpaulbool_t *
78314262Swpaulypoldproc_domain_1_svc(domainname *argp, struct svc_req *rqstp)
78414262Swpaul{
78514262Swpaul	return(ypproc_domain_2_svc(argp, rqstp));
78614262Swpaul}
78714262Swpaul
78814262Swpaulbool_t *
78914262Swpaulypoldproc_domain_nonack_1_svc(domainname *argp, struct svc_req *rqstp)
79014262Swpaul{
79114262Swpaul	return (ypproc_domain_nonack_2_svc(argp, rqstp));
79214262Swpaul}
79314262Swpaul
79414304Swpaul/*
79514304Swpaul * the 'match' procedure sends a response of type YPRESP_VAL
79614304Swpaul */
79714262Swpaulypresponse *
79814262Swpaulypoldproc_match_1_svc(yprequest *argp, struct svc_req *rqstp)
79914262Swpaul{
80014262Swpaul	static ypresponse  result;
80114262Swpaul	ypresp_val *v2_result;
80214262Swpaul
80314262Swpaul	result.yp_resptype = YPRESP_VAL;
80414304Swpaul	result.ypresponse_u.yp_resp_valtype.val.valdat_val = "";
80514304Swpaul	result.ypresponse_u.yp_resp_valtype.val.valdat_len = 0;
80614262Swpaul
80714262Swpaul	if (argp->yp_reqtype != YPREQ_KEY) {
80814262Swpaul		result.ypresponse_u.yp_resp_valtype.stat = YP_BADARGS;
80914262Swpaul		return(&result);
81014262Swpaul	}
81114262Swpaul
81214262Swpaul	v2_result = ypproc_match_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp);
81314262Swpaul	if (v2_result == NULL)
81414262Swpaul		return(NULL);
81514262Swpaul
81614262Swpaul	bcopy((char *)v2_result,
81714262Swpaul	      (char *)&result.ypresponse_u.yp_resp_valtype,
81814262Swpaul	      sizeof(ypresp_val));
81914262Swpaul
82014262Swpaul	return (&result);
82114262Swpaul}
82214262Swpaul
82314304Swpaul/*
82414304Swpaul * the 'first' procedure sends a response of type YPRESP_KEY_VAL
82514304Swpaul */
82614262Swpaulypresponse *
82714262Swpaulypoldproc_first_1_svc(yprequest *argp, struct svc_req *rqstp)
82814262Swpaul{
82914262Swpaul	static ypresponse  result;
83014262Swpaul	ypresp_key_val *v2_result;
83114262Swpaul
83214262Swpaul	result.yp_resptype = YPRESP_KEY_VAL;
83314304Swpaul	result.ypresponse_u.yp_resp_key_valtype.val.valdat_val =
83414304Swpaul	result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = "";
83514304Swpaul	result.ypresponse_u.yp_resp_key_valtype.val.valdat_len =
83614304Swpaul	result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0;
83714262Swpaul
83814262Swpaul	if (argp->yp_reqtype != YPREQ_NOKEY) {
83914262Swpaul		result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS;
84014262Swpaul		return(&result);
84114262Swpaul	}
84214262Swpaul
84314262Swpaul	v2_result = ypproc_first_2_svc(&argp->yprequest_u.yp_req_nokeytype,
84414262Swpaul									rqstp);
84514262Swpaul	if (v2_result == NULL)
84614262Swpaul		return(NULL);
84714262Swpaul
84814262Swpaul	bcopy((char *)v2_result,
84914262Swpaul	      (char *)&result.ypresponse_u.yp_resp_key_valtype,
85014262Swpaul	      sizeof(ypresp_key_val));
85114262Swpaul
85214262Swpaul	return (&result);
85314262Swpaul}
85414262Swpaul
85514304Swpaul/*
85614304Swpaul * the 'next' procedure sends a response of type YPRESP_KEY_VAL
85714304Swpaul */
85814262Swpaulypresponse *
85914262Swpaulypoldproc_next_1_svc(yprequest *argp, struct svc_req *rqstp)
86014262Swpaul{
86114262Swpaul	static ypresponse  result;
86214262Swpaul	ypresp_key_val *v2_result;
86314262Swpaul
86414262Swpaul	result.yp_resptype = YPRESP_KEY_VAL;
86514304Swpaul	result.ypresponse_u.yp_resp_key_valtype.val.valdat_val =
86614304Swpaul	result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = "";
86714304Swpaul	result.ypresponse_u.yp_resp_key_valtype.val.valdat_len =
86814304Swpaul	result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0;
86914262Swpaul
87014262Swpaul	if (argp->yp_reqtype != YPREQ_KEY) {
87114262Swpaul		result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS;
87214262Swpaul		return(&result);
87314262Swpaul	}
87414262Swpaul
87514262Swpaul	v2_result = ypproc_next_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp);
87614262Swpaul	if (v2_result == NULL)
87714262Swpaul		return(NULL);
87814262Swpaul
87914262Swpaul	bcopy((char *)v2_result,
88014262Swpaul	      (char *)&result.ypresponse_u.yp_resp_key_valtype,
88114262Swpaul	      sizeof(ypresp_key_val));
88214262Swpaul
88314262Swpaul	return (&result);
88414262Swpaul}
88514262Swpaul
88614304Swpaul/*
88714304Swpaul * the 'poll' procedure sends a response of type YPRESP_MAP_PARMS
88814304Swpaul */
88914262Swpaulypresponse *
89014262Swpaulypoldproc_poll_1_svc(yprequest *argp, struct svc_req *rqstp)
89114262Swpaul{
89214262Swpaul	static ypresponse  result;
89314262Swpaul	ypresp_master *v2_result1;
89414262Swpaul	ypresp_order *v2_result2;
89514262Swpaul
89614262Swpaul	result.yp_resptype = YPRESP_MAP_PARMS;
89714262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.domain =
89814262Swpaul		argp->yprequest_u.yp_req_nokeytype.domain;
89914262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.map =
90014262Swpaul		argp->yprequest_u.yp_req_nokeytype.map;
90114262Swpaul	/*
90214262Swpaul	 * Hmm... there is no 'status' value in the
90314262Swpaul	 * yp_resp_map_parmstype structure, so I have to
90414262Swpaul	 * guess at what to do to indicate a failure.
90514262Swpaul	 * I hope this is right.
90614262Swpaul	 */
90714262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.ordernum = 0;
90814262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.peer = "";
90914262Swpaul
91014262Swpaul	if (argp->yp_reqtype != YPREQ_MAP_PARMS) {
91114262Swpaul		return(&result);
91214262Swpaul	}
91314262Swpaul
91414262Swpaul	v2_result1 = ypproc_master_2_svc(&argp->yprequest_u.yp_req_nokeytype,
91514262Swpaul									rqstp);
91614262Swpaul	if (v2_result1 == NULL)
91714262Swpaul		return(NULL);
91814262Swpaul
91914262Swpaul	if (v2_result1->stat != YP_TRUE) {
92014262Swpaul		return(&result);
92114262Swpaul	}
92214262Swpaul
92314262Swpaul	v2_result2 = ypproc_order_2_svc(&argp->yprequest_u.yp_req_nokeytype,
92414262Swpaul									rqstp);
92514262Swpaul	if (v2_result2 == NULL)
92614262Swpaul		return(NULL);
92714262Swpaul
92814262Swpaul	if (v2_result2->stat != YP_TRUE) {
92914262Swpaul		return(&result);
93014262Swpaul	}
93114262Swpaul
93214262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.peer =
93314262Swpaul		v2_result1->peer;
93414262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.ordernum =
93514262Swpaul		v2_result2->ordernum;
93614262Swpaul
93714262Swpaul	return (&result);
93814262Swpaul}
93914262Swpaul
94014262Swpaulypresponse *
94114262Swpaulypoldproc_push_1_svc(yprequest *argp, struct svc_req *rqstp)
94214262Swpaul{
94314262Swpaul	static ypresponse  result;
94414262Swpaul
94514262Swpaul	/*
94614262Swpaul	 * Not implemented.
94714262Swpaul	 */
94814262Swpaul
94914262Swpaul	return (&result);
95014262Swpaul}
95114262Swpaul
95214262Swpaulypresponse *
95314262Swpaulypoldproc_pull_1_svc(yprequest *argp, struct svc_req *rqstp)
95414262Swpaul{
95514262Swpaul	static ypresponse  result;
95614262Swpaul
95714262Swpaul	/*
95814262Swpaul	 * Not implemented.
95914262Swpaul	 */
96014262Swpaul
96114262Swpaul	return (&result);
96214262Swpaul}
96314262Swpaul
96414262Swpaulypresponse *
96514262Swpaulypoldproc_get_1_svc(yprequest *argp, struct svc_req *rqstp)
96614262Swpaul{
96714262Swpaul	static ypresponse  result;
96814262Swpaul
96914262Swpaul	/*
97014262Swpaul	 * Not implemented.
97114262Swpaul	 */
97214262Swpaul
97314262Swpaul	return (&result);
97414262Swpaul}
975