yp_server.c revision 95658
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[] =
3650479Speter  "$FreeBSD: head/usr.sbin/ypserv/yp_server.c 95658 2002-04-28 15:18:50Z des $";
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
5990298Sdesstatic pid_t
6090298Sdesyp_fork(void)
6146186Swpaul{
6246186Swpaul	if (yp_pid != getpid()) {
6346186Swpaul		yp_error("child %d trying to fork!", getpid());
6446186Swpaul		errno = EEXIST;
6546186Swpaul		return(-1);
6646186Swpaul	}
6746186Swpaul
6846186Swpaul	return(fork());
6946186Swpaul}
7046186Swpaul
7114262Swpaul/*
7214262Swpaul * NIS v2 support. This is where most of the action happens.
7314262Swpaul */
7414262Swpaul
7512891Swpaulvoid *
7612891Swpaulypproc_null_2_svc(void *argp, struct svc_req *rqstp)
7712891Swpaul{
7812891Swpaul	static char * result;
7912891Swpaul	static char rval = 0;
8012891Swpaul
8119161Swpaul#ifdef DB_CACHE
8219161Swpaul	if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
8319161Swpaul#else
8412891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
8519161Swpaul#endif
8612891Swpaul		return(NULL);
8712891Swpaul
8812891Swpaul	result = &rval;
8912891Swpaul
9012891Swpaul	return((void *) &result);
9112891Swpaul}
9212891Swpaul
9312891Swpaulbool_t *
9412891Swpaulypproc_domain_2_svc(domainname *argp, struct svc_req *rqstp)
9512891Swpaul{
9612891Swpaul	static bool_t  result;
9712891Swpaul
9819161Swpaul#ifdef DB_CACHE
9919161Swpaul	if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) {
10019161Swpaul#else
10112891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp)) {
10219161Swpaul#endif
10312891Swpaul		result = FALSE;
10412891Swpaul		return (&result);
10512891Swpaul	}
10612891Swpaul
10712891Swpaul	if (argp == NULL || yp_validdomain(*argp))
10812891Swpaul		result = FALSE;
10912891Swpaul	else
11012891Swpaul		result = TRUE;
11112891Swpaul
11212891Swpaul	return (&result);
11312891Swpaul}
11412891Swpaul
11512891Swpaulbool_t *
11612891Swpaulypproc_domain_nonack_2_svc(domainname *argp, struct svc_req *rqstp)
11712891Swpaul{
11812891Swpaul	static bool_t  result;
11912891Swpaul
12019161Swpaul#ifdef DB_CACHE
12119161Swpaul	if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
12219161Swpaul#else
12312891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
12419161Swpaul#endif
12512891Swpaul		return (NULL);
12612891Swpaul
12712891Swpaul	if (argp == NULL || yp_validdomain(*argp))
12812891Swpaul		return (NULL);
12912891Swpaul	else
13012891Swpaul		result = TRUE;
13112891Swpaul
13212891Swpaul	return (&result);
13312891Swpaul}
13412891Swpaul
13512891Swpaulypresp_val *
13612891Swpaulypproc_match_2_svc(ypreq_key *argp, struct svc_req *rqstp)
13712891Swpaul{
13812891Swpaul	static ypresp_val  result;
13912891Swpaul
14014304Swpaul	result.val.valdat_val = "";
14114304Swpaul	result.val.valdat_len = 0;
14219161Swpaul
14319161Swpaul#ifdef DB_CACHE
14419161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
14519161Swpaul#else
14612891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
14719161Swpaul#endif
14812891Swpaul		result.stat = YP_YPERR;
14912891Swpaul		return (&result);
15012891Swpaul	}
15112891Swpaul
15212891Swpaul	if (argp->domain == NULL || argp->map == NULL) {
15312891Swpaul		result.stat = YP_BADARGS;
15412891Swpaul		return (&result);
15512891Swpaul	}
15612891Swpaul
15733250Swpaul	if (yp_select_map(argp->map, argp->domain, NULL, 1) != YP_TRUE) {
15820818Swpaul		result.stat = yp_errno;
15920818Swpaul		return(&result);
16012891Swpaul	}
16112891Swpaul
16220818Swpaul	result.stat = yp_getbykey(&argp->key, &result.val);
16320818Swpaul
16412891Swpaul	/*
16512891Swpaul	 * Do DNS lookups for hosts maps if database lookup failed.
16612891Swpaul	 */
16712891Swpaul
16819161Swpaul#ifdef DB_CACHE
16919161Swpaul	if (result.stat != YP_TRUE &&
17019161Swpaul	    (yp_testflag(argp->map, argp->domain, YP_INTERDOMAIN) ||
17119161Swpaul	    (strstr(argp->map, "hosts") && do_dns))) {
17219161Swpaul#else
17312891Swpaul	if (do_dns && result.stat != YP_TRUE && strstr(argp->map, "hosts")) {
17419161Swpaul#endif
17527589Swpaul		char			nbuf[YPMAXRECORD];
17690297Sdes
17720818Swpaul		/* NUL terminate! NUL terminate!! NUL TERMINATE!!! */
17827589Swpaul		bcopy(argp->key.keydat_val, nbuf, argp->key.keydat_len);
17927589Swpaul		nbuf[argp->key.keydat_len] = '\0';
18012891Swpaul
18112891Swpaul		if (debug)
18230827Scharnier			yp_error("doing DNS lookup of %s", nbuf);
18312891Swpaul
18412891Swpaul		if (!strcmp(argp->map, "hosts.byname"))
18527589Swpaul			result.stat = yp_async_lookup_name(rqstp, nbuf);
18612891Swpaul		else if (!strcmp(argp->map, "hosts.byaddr"))
18727589Swpaul			result.stat = yp_async_lookup_addr(rqstp, nbuf);
18812891Swpaul
18920818Swpaul		if (result.stat == YP_TRUE)
19020818Swpaul			return(NULL);
19112891Swpaul	}
19212891Swpaul
19312891Swpaul	return (&result);
19412891Swpaul}
19512891Swpaul
19612891Swpaulypresp_key_val *
19712891Swpaulypproc_first_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
19812891Swpaul{
19912891Swpaul	static ypresp_key_val  result;
20012891Swpaul
20114304Swpaul	result.val.valdat_val = result.key.keydat_val = "";
20214304Swpaul	result.val.valdat_len = result.key.keydat_len = 0;
20319161Swpaul
20419161Swpaul#ifdef DB_CACHE
20519161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
20619161Swpaul#else
20712891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
20819161Swpaul#endif
20912891Swpaul		result.stat = YP_YPERR;
21012891Swpaul		return (&result);
21112891Swpaul	}
21212891Swpaul
21312891Swpaul	if (argp->domain == NULL) {
21412891Swpaul		result.stat = YP_BADARGS;
21512891Swpaul		return (&result);
21612891Swpaul	}
21712891Swpaul
21833250Swpaul	if (yp_select_map(argp->map, argp->domain, NULL, 0) != YP_TRUE) {
21912891Swpaul		result.stat = yp_errno;
22012891Swpaul		return(&result);
22112891Swpaul	}
22212891Swpaul
22320818Swpaul	result.stat = yp_firstbykey(&result.key, &result.val);
22412891Swpaul
22512891Swpaul	return (&result);
22612891Swpaul}
22712891Swpaul
22812891Swpaulypresp_key_val *
22912891Swpaulypproc_next_2_svc(ypreq_key *argp, struct svc_req *rqstp)
23012891Swpaul{
23112891Swpaul	static ypresp_key_val  result;
23212891Swpaul
23314304Swpaul	result.val.valdat_val = result.key.keydat_val = "";
23414304Swpaul	result.val.valdat_len = result.key.keydat_len = 0;
23515426Swpaul
23619161Swpaul#ifdef DB_CACHE
23719161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
23819161Swpaul#else
23912891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
24019161Swpaul#endif
24112891Swpaul		result.stat = YP_YPERR;
24212891Swpaul		return (&result);
24312891Swpaul	}
24412891Swpaul
24512891Swpaul	if (argp->domain == NULL || argp->map == NULL) {
24612891Swpaul		result.stat = YP_BADARGS;
24712891Swpaul		return (&result);
24812891Swpaul	}
24912891Swpaul
25020818Swpaul	if (yp_select_map(argp->map, argp->domain, &argp->key, 0) != YP_TRUE) {
25112891Swpaul		result.stat = yp_errno;
25212891Swpaul		return(&result);
25312891Swpaul	}
25412891Swpaul
25520818Swpaul	result.key.keydat_len = argp->key.keydat_len;
25620818Swpaul	result.key.keydat_val = argp->key.keydat_val;
25712891Swpaul
25820818Swpaul	result.stat = yp_nextbykey(&result.key, &result.val);
25920818Swpaul
26012891Swpaul	return (&result);
26112891Swpaul}
26212891Swpaul
26390298Sdesstatic void
26490298Sdesypxfr_callback(ypxfrstat rval, struct sockaddr_in *addr, unsigned int transid,
26590298Sdes    unsigned int prognum, unsigned long port)
26612997Swpaul{
26712997Swpaul	CLIENT *clnt;
26812997Swpaul	int sock = RPC_ANYSOCK;
26912997Swpaul	struct timeval timeout;
27012997Swpaul	yppushresp_xfr ypxfr_resp;
27113375Swpaul	struct rpc_err err;
27212997Swpaul
27313375Swpaul	timeout.tv_sec = 5;
27412997Swpaul	timeout.tv_usec = 0;
27512997Swpaul	addr->sin_port = htons(port);
27612997Swpaul
27719131Swpaul	if ((clnt = clntudp_create(addr,prognum,1,timeout,&sock)) == NULL) {
27819131Swpaul		yp_error("%s: %s", inet_ntoa(addr->sin_addr),
27919161Swpaul		  clnt_spcreateerror("failed to establish callback handle"));
28019131Swpaul		return;
28119131Swpaul	}
28212997Swpaul
28312997Swpaul	ypxfr_resp.status = rval;
28412997Swpaul	ypxfr_resp.transid = transid;
28512997Swpaul
28613375Swpaul	/* Turn the timeout off -- we don't want to block. */
28713375Swpaul	timeout.tv_sec = 0;
28895658Sdes	if (clnt_control(clnt, CLSET_TIMEOUT, &timeout) == FALSE)
28913375Swpaul		yp_error("failed to set timeout on ypproc_xfr callback");
29012997Swpaul
29113375Swpaul	if (yppushproc_xfrresp_1(&ypxfr_resp, clnt) == NULL) {
29213375Swpaul		clnt_geterr(clnt, &err);
29313375Swpaul		if (err.re_status != RPC_SUCCESS &&
29413375Swpaul		    err.re_status != RPC_TIMEDOUT)
29513375Swpaul			yp_error("%s", clnt_sperror(clnt,
29613375Swpaul				"ypxfr callback failed"));
29713375Swpaul	}
29813375Swpaul
29912997Swpaul	clnt_destroy(clnt);
30012997Swpaul	return;
30112997Swpaul}
30212997Swpaul
30315426Swpaul#define YPXFR_RETURN(CODE) 						\
30415426Swpaul	/* Order is important: send regular RPC reply, then callback */	\
30515426Swpaul	result.xfrstat = CODE; 						\
30695658Sdes	svc_sendreply(rqstp->rq_xprt, (xdrproc_t)xdr_ypresp_xfr, &result); \
30715426Swpaul	ypxfr_callback(CODE,rqhost,argp->transid, 			\
30815426Swpaul					argp->prog,argp->port); 	\
30915426Swpaul	return(NULL);
31015426Swpaul
31112891Swpaulypresp_xfr *
31212891Swpaulypproc_xfr_2_svc(ypreq_xfr *argp, struct svc_req *rqstp)
31312891Swpaul{
31412891Swpaul	static ypresp_xfr  result;
31512997Swpaul	struct sockaddr_in *rqhost;
31624780Swpaul	ypresp_master *mres;
31724780Swpaul	ypreq_nokey mreq;
31812891Swpaul
31913375Swpaul	result.transid = argp->transid;
32013375Swpaul	rqhost = svc_getcaller(rqstp->rq_xprt);
32113375Swpaul
32219161Swpaul#ifdef DB_CACHE
32319161Swpaul	if (yp_access(argp->map_parms.map,
32419161Swpaul			argp->map_parms.domain, (struct svc_req *)rqstp)) {
32519161Swpaul#else
32612891Swpaul	if (yp_access(argp->map_parms.map, (struct svc_req *)rqstp)) {
32719161Swpaul#endif
32824780Swpaul		YPXFR_RETURN(YPXFR_REFUSED)
32912891Swpaul	}
33012891Swpaul
33124780Swpaul
33212891Swpaul	if (argp->map_parms.domain == NULL) {
33324780Swpaul		YPXFR_RETURN(YPXFR_BADARGS)
33412891Swpaul	}
33512891Swpaul
33612891Swpaul	if (yp_validdomain(argp->map_parms.domain)) {
33724780Swpaul		YPXFR_RETURN(YPXFR_NODOM)
33812891Swpaul	}
33912891Swpaul
34024780Swpaul	/*
34124780Swpaul	 * Determine the master host ourselves. The caller may
34224780Swpaul	 * be up to no good. This has the side effect of verifying
34324780Swpaul	 * that the requested map and domain actually exist.
34424780Swpaul	 */
34524780Swpaul
34624780Swpaul	mreq.domain = argp->map_parms.domain;
34724780Swpaul	mreq.map = argp->map_parms.map;
34824780Swpaul
34924780Swpaul	mres = ypproc_master_2_svc(&mreq, rqstp);
35024780Swpaul
35124780Swpaul	if (mres->stat != YP_TRUE) {
35224780Swpaul		yp_error("couldn't find master for map %s@%s",
35324780Swpaul						argp->map_parms.map,
35424780Swpaul						argp->map_parms.domain);
35524780Swpaul		yp_error("host at %s (%s) may be pulling my leg",
35624780Swpaul						argp->map_parms.peer,
35724780Swpaul						inet_ntoa(rqhost->sin_addr));
35824780Swpaul		YPXFR_RETURN(YPXFR_REFUSED)
35924780Swpaul	}
36024780Swpaul
36190297Sdes	switch (yp_fork()) {
36212891Swpaul	case 0:
36312891Swpaul	{
36412891Swpaul		char g[11], t[11], p[11];
36512891Swpaul		char ypxfr_command[MAXPATHLEN + 2];
36612891Swpaul
36780184Skris		snprintf (ypxfr_command, sizeof(ypxfr_command), "%sypxfr", _PATH_LIBEXEC);
36880184Skris		snprintf (t, sizeof(t), "%u", argp->transid);
36980184Skris		snprintf (g, sizeof(g), "%u", argp->prog);
37080184Skris		snprintf (p, sizeof(p), "%u", argp->port);
37122321Swpaul		if (debug) {
37212997Swpaul			close(0); close(1); close(2);
37322321Swpaul		}
37412997Swpaul		if (strcmp(yp_dir, _PATH_YP)) {
37514304Swpaul			execl(ypxfr_command, "ypxfr",
37614304Swpaul			"-d", argp->map_parms.domain,
37724780Swpaul		      	"-h", mres->peer,
37814304Swpaul			"-p", yp_dir, "-C", t,
37914304Swpaul		      	g, inet_ntoa(rqhost->sin_addr),
38014304Swpaul			p, argp->map_parms.map,
38195658Sdes		      	NULL);
38212997Swpaul		} else {
38314304Swpaul			execl(ypxfr_command, "ypxfr",
38414304Swpaul			"-d", argp->map_parms.domain,
38524780Swpaul		      	"-h", mres->peer,
38614304Swpaul			"-C", t,
38714304Swpaul		      	g, inet_ntoa(rqhost->sin_addr),
38814304Swpaul			p, argp->map_parms.map,
38995658Sdes		      	NULL);
39012997Swpaul		}
39115426Swpaul		yp_error("ypxfr execl(%s): %s", ypxfr_command, strerror(errno));
39224780Swpaul		YPXFR_RETURN(YPXFR_XFRERR)
39346205Swpaul		/*
39446205Swpaul		 * Just to safe, prevent PR #10970 from biting us in
39546205Swpaul		 * the unlikely case that execing ypxfr fails. We don't
39646205Swpaul		 * want to have any child processes spawned from this
39746205Swpaul		 * child process.
39846205Swpaul		 */
39946205Swpaul		_exit(0);
40012997Swpaul		break;
40112891Swpaul	}
40212891Swpaul	case -1:
40312891Swpaul		yp_error("ypxfr fork(): %s", strerror(errno));
40424780Swpaul		YPXFR_RETURN(YPXFR_XFRERR)
40512891Swpaul		break;
40612891Swpaul	default:
40713375Swpaul		result.xfrstat = YPXFR_SUCC;
40812997Swpaul		children++;
40912891Swpaul		break;
41012891Swpaul	}
41113375Swpaul
41213375Swpaul	return (&result);
41312891Swpaul}
41415426Swpaul#undef YPXFR_RETURN
41512891Swpaul
41612891Swpaulvoid *
41712891Swpaulypproc_clear_2_svc(void *argp, struct svc_req *rqstp)
41812891Swpaul{
41912891Swpaul	static char * result;
42012891Swpaul	static char rval = 0;
42112891Swpaul
42219161Swpaul#ifdef DB_CACHE
42319161Swpaul	if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
42419161Swpaul#else
42512891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
42619161Swpaul#endif
42712891Swpaul		return (NULL);
42815426Swpaul#ifdef DB_CACHE
42915426Swpaul	/* clear out the database cache */
43015426Swpaul	yp_flush_all();
43115426Swpaul#endif
43214240Swpaul	/* Re-read the securenets database for the hell of it. */
43314240Swpaul	load_securenets();
43414240Swpaul
43512891Swpaul	result = &rval;
43612891Swpaul	return((void *) &result);
43712891Swpaul}
43812891Swpaul
43912891Swpaul/*
44012891Swpaul * For ypproc_all, we have to send a stream of ypresp_all structures
44112891Swpaul * via TCP, but the XDR filter generated from the yp.x protocol
44212891Swpaul * definition file only serializes one such structure. This means that
44312891Swpaul * to send the whole stream, you need a wrapper which feeds all the
44412891Swpaul * records into the underlying XDR routine until it hits an 'EOF.'
44512891Swpaul * But to use the wrapper, you have to violate the boundaries between
44612891Swpaul * RPC layers by calling svc_sendreply() directly from the ypproc_all
44712891Swpaul * service routine instead of letting the RPC dispatcher do it.
44812891Swpaul *
44912891Swpaul * Bleah.
45012891Swpaul */
45112891Swpaul
45212891Swpaul/*
45320100Swpaul * Custom XDR routine for serialzing results of ypproc_all: keep
45420100Swpaul * reading from the database and spew until we run out of records
45520100Swpaul * or encounter an error.
45612891Swpaul */
45712891Swpaulstatic bool_t
45812891Swpaulxdr_my_ypresp_all(register XDR *xdrs, ypresp_all *objp)
45912891Swpaul{
46020100Swpaul	while (1) {
46120100Swpaul		/* Get a record. */
46220100Swpaul		if ((objp->ypresp_all_u.val.stat =
46320818Swpaul			yp_nextbykey(&objp->ypresp_all_u.val.key,
46420818Swpaul				     &objp->ypresp_all_u.val.val)) == YP_TRUE) {
46520100Swpaul			objp->more = TRUE;
46620100Swpaul		} else {
46720100Swpaul			objp->more = FALSE;
46820100Swpaul		}
46920100Swpaul
47020100Swpaul		/* Serialize. */
47120100Swpaul		if (!xdr_ypresp_all(xdrs, objp))
47220100Swpaul			return(FALSE);
47320100Swpaul		if (objp->more == FALSE)
47420100Swpaul			return(TRUE);
47520100Swpaul	}
47612891Swpaul}
47712891Swpaul
47812891Swpaulypresp_all *
47912891Swpaulypproc_all_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
48012891Swpaul{
48112891Swpaul	static ypresp_all  result;
48212891Swpaul
48312891Swpaul	/*
48412891Swpaul	 * Set this here so that the client will be forced to make
48512891Swpaul	 * at least one attempt to read from us even if all we're
48612891Swpaul	 * doing is returning an error.
48712891Swpaul	 */
48812891Swpaul	result.more = TRUE;
48914304Swpaul	result.ypresp_all_u.val.key.keydat_len = 0;
49014304Swpaul	result.ypresp_all_u.val.key.keydat_val = "";
49112891Swpaul
49219161Swpaul#ifdef DB_CACHE
49319161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
49419161Swpaul#else
49512891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
49619161Swpaul#endif
49712891Swpaul		result.ypresp_all_u.val.stat = YP_YPERR;
49812891Swpaul		return (&result);
49912891Swpaul	}
50012891Swpaul
50112891Swpaul	if (argp->domain == NULL || argp->map == NULL) {
50212891Swpaul		result.ypresp_all_u.val.stat = YP_BADARGS;
50312891Swpaul		return (&result);
50412891Swpaul	}
50512891Swpaul
50620100Swpaul	/*
50721389Swpaul	 * XXX If we hit the child limit, fail the request.
50821389Swpaul	 * If we don't, and the map is large, we could block for
50921389Swpaul	 * a long time in the parent.
51021389Swpaul	 */
51121389Swpaul	if (children >= MAX_CHILDREN) {
51221389Swpaul		result.ypresp_all_u.val.stat = YP_YPERR;
51321389Swpaul		return(&result);
51421389Swpaul	}
51521389Swpaul
51621389Swpaul	/*
51720100Swpaul	 * The ypproc_all procedure can take a while to complete.
51820100Swpaul	 * Best to handle it in a subprocess so the parent doesn't
51920100Swpaul	 * block. (Is there a better way to do this? Maybe with
52020100Swpaul	 * async socket I/O?)
52120100Swpaul	 */
52243847Swpaul	if (!debug) {
52390297Sdes		switch (yp_fork()) {
52443847Swpaul		case 0:
52543847Swpaul			break;
52643847Swpaul		case -1:
52743847Swpaul			yp_error("ypall fork(): %s", strerror(errno));
52843847Swpaul			result.ypresp_all_u.val.stat = YP_YPERR;
52943847Swpaul			return(&result);
53043847Swpaul			break;
53143847Swpaul		default:
53243847Swpaul			children++;
53343847Swpaul			return (NULL);
53443847Swpaul			break;
53543847Swpaul		}
53620100Swpaul	}
53720100Swpaul
53846207Swpaul	/*
53946207Swpaul	 * Fix for PR #10971: don't let the child ypserv share
54046207Swpaul	 * DB handles with the parent process.
54146207Swpaul	 */
54246207Swpaul#ifdef DB_CACHE
54346207Swpaul	yp_flush_all();
54446207Swpaul#endif
54546207Swpaul
54620818Swpaul	if (yp_select_map(argp->map, argp->domain,
54720818Swpaul				&result.ypresp_all_u.val.key, 0) != YP_TRUE) {
54812891Swpaul		result.ypresp_all_u.val.stat = yp_errno;
54912891Swpaul		return(&result);
55012891Swpaul	}
55112891Swpaul
55212891Swpaul	/* Kick off the actual data transfer. */
55395658Sdes	svc_sendreply(rqstp->rq_xprt, (xdrproc_t)xdr_my_ypresp_all, &result);
55420100Swpaul
55512891Swpaul	/*
55646205Swpaul	 * Proper fix for PR #10970: exit here so that we don't risk
55746205Swpaul	 * having a child spawned from this sub-process.
55812891Swpaul	 */
55946205Swpaul	_exit(0);
56012891Swpaul}
56112891Swpaul
56212891Swpaulypresp_master *
56312891Swpaulypproc_master_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
56412891Swpaul{
56512891Swpaul	static ypresp_master  result;
56615426Swpaul	static char ypvalbuf[YPMAXRECORD];
56728042Swpaul	keydat key = { MASTER_SZ, MASTER_STRING };
56820818Swpaul	valdat val;
56912891Swpaul
57014303Swpaul	result.peer = "";
57114303Swpaul
57219161Swpaul#ifdef DB_CACHE
57319161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
57419161Swpaul#else
57519161Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
57619161Swpaul#endif
57712891Swpaul		result.stat = YP_YPERR;
57812891Swpaul		return(&result);
57912891Swpaul	}
58012891Swpaul
58112891Swpaul	if (argp->domain == NULL) {
58212891Swpaul		result.stat = YP_BADARGS;
58312891Swpaul		return (&result);
58412891Swpaul	}
58512891Swpaul
58620818Swpaul	if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) {
58720818Swpaul		result.stat = yp_errno;
58820818Swpaul		return(&result);
58920818Swpaul	}
59020818Swpaul
59115426Swpaul	/*
59215426Swpaul	 * Note that we copy the data retrieved from the database to
59315426Swpaul	 * a private buffer and NUL terminate the buffer rather than
59415426Swpaul	 * terminating the data in place. We do this because by stuffing
59515426Swpaul	 * a '\0' into data.data, we will actually be corrupting memory
59615426Swpaul	 * allocated by the DB package. This is a bad thing now that we
59715426Swpaul	 * cache DB handles rather than closing the database immediately.
59815426Swpaul	 */
59920818Swpaul	result.stat = yp_getbykey(&key, &val);
60020818Swpaul	if (result.stat == YP_TRUE) {
60195658Sdes		bcopy(val.valdat_val, &ypvalbuf, val.valdat_len);
60220818Swpaul		ypvalbuf[val.valdat_len] = '\0';
60395658Sdes		result.peer = ypvalbuf;
60412891Swpaul	} else
60512891Swpaul		result.peer = "";
60612891Swpaul
60712891Swpaul	return (&result);
60812891Swpaul}
60912891Swpaul
61012891Swpaulypresp_order *
61112891Swpaulypproc_order_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
61212891Swpaul{
61312891Swpaul	static ypresp_order  result;
61428042Swpaul	keydat key = { ORDER_SZ, ORDER_STRING };
61520818Swpaul	valdat val;
61612891Swpaul
61714304Swpaul	result.ordernum = 0;
61814304Swpaul
61919161Swpaul#ifdef DB_CACHE
62019161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
62119161Swpaul#else
62219161Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
62319161Swpaul#endif
62412891Swpaul		result.stat = YP_YPERR;
62512891Swpaul		return(&result);
62612891Swpaul	}
62712891Swpaul
62812891Swpaul	if (argp->domain == NULL) {
62912891Swpaul		result.stat = YP_BADARGS;
63012891Swpaul		return (&result);
63112891Swpaul	}
63290297Sdes
63312891Swpaul	/*
63412891Swpaul	 * We could just check the timestamp on the map file,
63512891Swpaul	 * but that's a hack: we'll only know the last time the file
63612891Swpaul	 * was touched, not the last time the database contents were
63712891Swpaul	 * updated.
63812891Swpaul	 */
63912891Swpaul
64020818Swpaul	if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) {
64120818Swpaul		result.stat = yp_errno;
64220818Swpaul		return(&result);
64320818Swpaul	}
64420818Swpaul
64520818Swpaul	result.stat = yp_getbykey(&key, &val);
64620818Swpaul
64720818Swpaul	if (result.stat == YP_TRUE)
64895658Sdes		result.ordernum = atoi(val.valdat_val);
64912891Swpaul	else
65012891Swpaul		result.ordernum = 0;
65112891Swpaul
65212891Swpaul	return (&result);
65312891Swpaul}
65412891Swpaul
65590298Sdesstatic void yp_maplist_free(struct ypmaplist *yp_maplist)
65612891Swpaul{
65712891Swpaul	register struct ypmaplist *next;
65812891Swpaul
65990297Sdes	while (yp_maplist) {
66012891Swpaul		next = yp_maplist->next;
66112891Swpaul		free(yp_maplist->map);
66212891Swpaul		free(yp_maplist);
66312891Swpaul		yp_maplist = next;
66412891Swpaul	}
66512891Swpaul	return;
66612891Swpaul}
66712891Swpaul
66890298Sdesstatic struct ypmaplist *
66990298Sdesyp_maplist_create(const char *domain)
67012891Swpaul{
67112891Swpaul	char yp_mapdir[MAXPATHLEN + 2];
67212891Swpaul	char yp_mapname[MAXPATHLEN + 2];
67312891Swpaul	struct ypmaplist *cur = NULL;
67412891Swpaul	struct ypmaplist *yp_maplist = NULL;
67512891Swpaul	DIR *dird;
67612891Swpaul	struct dirent *dirp;
67712891Swpaul	struct stat statbuf;
67812891Swpaul
67912891Swpaul	snprintf(yp_mapdir, sizeof(yp_mapdir), "%s/%s", yp_dir, domain);
68012891Swpaul
68112891Swpaul	if ((dird = opendir(yp_mapdir)) == NULL) {
68213800Swpaul		yp_error("opendir(%s) failed: %s", yp_mapdir, strerror(errno));
68312891Swpaul		return(NULL);
68412891Swpaul	}
68512891Swpaul
68612891Swpaul	while ((dirp = readdir(dird)) != NULL) {
68712891Swpaul		if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
68814304Swpaul			snprintf(yp_mapname, sizeof(yp_mapname), "%s/%s",
68914304Swpaul							yp_mapdir,dirp->d_name);
69014304Swpaul			if (stat(yp_mapname, &statbuf) < 0 ||
69114304Swpaul						!S_ISREG(statbuf.st_mode))
69212891Swpaul				continue;
69314304Swpaul			if ((cur = (struct ypmaplist *)
69416044Swpaul				malloc(sizeof(struct ypmaplist))) == NULL) {
69530827Scharnier				yp_error("malloc() failed");
69612891Swpaul				closedir(dird);
69712891Swpaul				yp_maplist_free(yp_maplist);
69812891Swpaul				return(NULL);
69912891Swpaul			}
70095658Sdes			if ((cur->map = strdup(dirp->d_name)) == NULL) {
70114304Swpaul				yp_error("strdup() failed: %s",strerror(errno));
70212891Swpaul				closedir(dird);
70312891Swpaul				yp_maplist_free(yp_maplist);
70412891Swpaul				return(NULL);
70512891Swpaul			}
70612891Swpaul			cur->next = yp_maplist;
70712891Swpaul			yp_maplist = cur;
70812891Swpaul			if (debug)
70912891Swpaul				yp_error("map: %s", yp_maplist->map);
71012891Swpaul		}
71112891Swpaul
71212891Swpaul	}
71312891Swpaul	closedir(dird);
71412891Swpaul	return(yp_maplist);
71512891Swpaul}
71612891Swpaul
71712891Swpaulypresp_maplist *
71812891Swpaulypproc_maplist_2_svc(domainname *argp, struct svc_req *rqstp)
71912891Swpaul{
72015426Swpaul	static ypresp_maplist  result = { 0, NULL };
72112891Swpaul
72219161Swpaul#ifdef DB_CACHE
72319161Swpaul	if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) {
72419161Swpaul#else
72512891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp)) {
72619161Swpaul#endif
72712891Swpaul		result.stat = YP_YPERR;
72812891Swpaul		return(&result);
72912891Swpaul	}
73012891Swpaul
73112891Swpaul	if (argp == NULL) {
73212891Swpaul		result.stat = YP_BADARGS;
73312891Swpaul		return (&result);
73412891Swpaul	}
73590297Sdes
73612891Swpaul	if (yp_validdomain(*argp)) {
73712891Swpaul		result.stat = YP_NODOM;
73812891Swpaul		return (&result);
73912891Swpaul	}
74012891Swpaul
74112891Swpaul	/*
74212891Swpaul	 * We have to construct a linked list for the ypproc_maplist
74312891Swpaul	 * procedure using dynamically allocated memory. Since the XDR
74412891Swpaul	 * layer won't free this list for us, we have to deal with it
74512891Swpaul	 * ourselves. We call yp_maplist_free() first to free any
74612891Swpaul	 * previously allocated data we may have accumulated to insure
74712891Swpaul	 * that we have only one linked list in memory at any given
74812891Swpaul	 * time.
74912891Swpaul	 */
75012891Swpaul
75112891Swpaul	yp_maplist_free(result.maps);
75212891Swpaul
75312891Swpaul	if ((result.maps = yp_maplist_create(*argp)) == NULL) {
75412891Swpaul		yp_error("yp_maplist_create failed");
75512891Swpaul		result.stat = YP_YPERR;
75612891Swpaul		return(&result);
75712891Swpaul	} else
75812891Swpaul		result.stat = YP_TRUE;
75912891Swpaul
76012891Swpaul	return (&result);
76112891Swpaul}
76214262Swpaul
76314262Swpaul/*
76414262Swpaul * NIS v1 support. The nullproc, domain and domain_nonack
76514262Swpaul * functions from v1 are identical to those in v2, so all
76614262Swpaul * we have to do is hand off to them.
76714262Swpaul *
76814262Swpaul * The other functions are mostly just wrappers around their v2
76914262Swpaul * counterparts. For example, for the v1 'match' procedure, we
77014262Swpaul * crack open the argument structure, make a request to the v2
77114262Swpaul * 'match' function, repackage the data into a v1 response and
77214262Swpaul * then send it on its way.
77314262Swpaul *
77414262Swpaul * Note that we don't support the pull, push and get procedures.
77514262Swpaul * There's little documentation available to show what they
77614262Swpaul * do, and I suspect they're meant largely for map transfers
77714262Swpaul * between master and slave servers.
77814262Swpaul */
77914262Swpaul
78014262Swpaulvoid *
78114262Swpaulypoldproc_null_1_svc(void *argp, struct svc_req *rqstp)
78214262Swpaul{
78314262Swpaul	return(ypproc_null_2_svc(argp, rqstp));
78414262Swpaul}
78514262Swpaul
78614262Swpaulbool_t *
78714262Swpaulypoldproc_domain_1_svc(domainname *argp, struct svc_req *rqstp)
78814262Swpaul{
78914262Swpaul	return(ypproc_domain_2_svc(argp, rqstp));
79014262Swpaul}
79114262Swpaul
79214262Swpaulbool_t *
79314262Swpaulypoldproc_domain_nonack_1_svc(domainname *argp, struct svc_req *rqstp)
79414262Swpaul{
79514262Swpaul	return (ypproc_domain_nonack_2_svc(argp, rqstp));
79614262Swpaul}
79714262Swpaul
79814304Swpaul/*
79914304Swpaul * the 'match' procedure sends a response of type YPRESP_VAL
80014304Swpaul */
80114262Swpaulypresponse *
80214262Swpaulypoldproc_match_1_svc(yprequest *argp, struct svc_req *rqstp)
80314262Swpaul{
80414262Swpaul	static ypresponse  result;
80514262Swpaul	ypresp_val *v2_result;
80614262Swpaul
80714262Swpaul	result.yp_resptype = YPRESP_VAL;
80814304Swpaul	result.ypresponse_u.yp_resp_valtype.val.valdat_val = "";
80914304Swpaul	result.ypresponse_u.yp_resp_valtype.val.valdat_len = 0;
81014262Swpaul
81114262Swpaul	if (argp->yp_reqtype != YPREQ_KEY) {
81214262Swpaul		result.ypresponse_u.yp_resp_valtype.stat = YP_BADARGS;
81314262Swpaul		return(&result);
81414262Swpaul	}
81514262Swpaul
81614262Swpaul	v2_result = ypproc_match_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp);
81714262Swpaul	if (v2_result == NULL)
81814262Swpaul		return(NULL);
81914262Swpaul
82095658Sdes	bcopy(v2_result, &result.ypresponse_u.yp_resp_valtype,
82114262Swpaul	      sizeof(ypresp_val));
82214262Swpaul
82314262Swpaul	return (&result);
82414262Swpaul}
82514262Swpaul
82614304Swpaul/*
82714304Swpaul * the 'first' procedure sends a response of type YPRESP_KEY_VAL
82814304Swpaul */
82914262Swpaulypresponse *
83014262Swpaulypoldproc_first_1_svc(yprequest *argp, struct svc_req *rqstp)
83114262Swpaul{
83214262Swpaul	static ypresponse  result;
83314262Swpaul	ypresp_key_val *v2_result;
83414262Swpaul
83514262Swpaul	result.yp_resptype = YPRESP_KEY_VAL;
83614304Swpaul	result.ypresponse_u.yp_resp_key_valtype.val.valdat_val =
83714304Swpaul	result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = "";
83814304Swpaul	result.ypresponse_u.yp_resp_key_valtype.val.valdat_len =
83914304Swpaul	result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0;
84014262Swpaul
84114262Swpaul	if (argp->yp_reqtype != YPREQ_NOKEY) {
84214262Swpaul		result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS;
84314262Swpaul		return(&result);
84414262Swpaul	}
84514262Swpaul
84614262Swpaul	v2_result = ypproc_first_2_svc(&argp->yprequest_u.yp_req_nokeytype,
84714262Swpaul									rqstp);
84814262Swpaul	if (v2_result == NULL)
84914262Swpaul		return(NULL);
85014262Swpaul
85195658Sdes	bcopy(v2_result, &result.ypresponse_u.yp_resp_key_valtype,
85214262Swpaul	      sizeof(ypresp_key_val));
85314262Swpaul
85414262Swpaul	return (&result);
85514262Swpaul}
85614262Swpaul
85714304Swpaul/*
85814304Swpaul * the 'next' procedure sends a response of type YPRESP_KEY_VAL
85914304Swpaul */
86014262Swpaulypresponse *
86114262Swpaulypoldproc_next_1_svc(yprequest *argp, struct svc_req *rqstp)
86214262Swpaul{
86314262Swpaul	static ypresponse  result;
86414262Swpaul	ypresp_key_val *v2_result;
86514262Swpaul
86614262Swpaul	result.yp_resptype = YPRESP_KEY_VAL;
86714304Swpaul	result.ypresponse_u.yp_resp_key_valtype.val.valdat_val =
86814304Swpaul	result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = "";
86914304Swpaul	result.ypresponse_u.yp_resp_key_valtype.val.valdat_len =
87014304Swpaul	result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0;
87114262Swpaul
87214262Swpaul	if (argp->yp_reqtype != YPREQ_KEY) {
87314262Swpaul		result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS;
87414262Swpaul		return(&result);
87514262Swpaul	}
87614262Swpaul
87714262Swpaul	v2_result = ypproc_next_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp);
87814262Swpaul	if (v2_result == NULL)
87914262Swpaul		return(NULL);
88014262Swpaul
88195658Sdes	bcopy(v2_result, &result.ypresponse_u.yp_resp_key_valtype,
88214262Swpaul	      sizeof(ypresp_key_val));
88314262Swpaul
88414262Swpaul	return (&result);
88514262Swpaul}
88614262Swpaul
88714304Swpaul/*
88814304Swpaul * the 'poll' procedure sends a response of type YPRESP_MAP_PARMS
88914304Swpaul */
89014262Swpaulypresponse *
89114262Swpaulypoldproc_poll_1_svc(yprequest *argp, struct svc_req *rqstp)
89214262Swpaul{
89314262Swpaul	static ypresponse  result;
89414262Swpaul	ypresp_master *v2_result1;
89514262Swpaul	ypresp_order *v2_result2;
89614262Swpaul
89714262Swpaul	result.yp_resptype = YPRESP_MAP_PARMS;
89814262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.domain =
89914262Swpaul		argp->yprequest_u.yp_req_nokeytype.domain;
90014262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.map =
90114262Swpaul		argp->yprequest_u.yp_req_nokeytype.map;
90214262Swpaul	/*
90314262Swpaul	 * Hmm... there is no 'status' value in the
90414262Swpaul	 * yp_resp_map_parmstype structure, so I have to
90514262Swpaul	 * guess at what to do to indicate a failure.
90614262Swpaul	 * I hope this is right.
90714262Swpaul	 */
90814262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.ordernum = 0;
90914262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.peer = "";
91014262Swpaul
91114262Swpaul	if (argp->yp_reqtype != YPREQ_MAP_PARMS) {
91214262Swpaul		return(&result);
91314262Swpaul	}
91414262Swpaul
91514262Swpaul	v2_result1 = ypproc_master_2_svc(&argp->yprequest_u.yp_req_nokeytype,
91614262Swpaul									rqstp);
91714262Swpaul	if (v2_result1 == NULL)
91814262Swpaul		return(NULL);
91914262Swpaul
92014262Swpaul	if (v2_result1->stat != YP_TRUE) {
92114262Swpaul		return(&result);
92214262Swpaul	}
92314262Swpaul
92414262Swpaul	v2_result2 = ypproc_order_2_svc(&argp->yprequest_u.yp_req_nokeytype,
92514262Swpaul									rqstp);
92614262Swpaul	if (v2_result2 == NULL)
92714262Swpaul		return(NULL);
92814262Swpaul
92914262Swpaul	if (v2_result2->stat != YP_TRUE) {
93014262Swpaul		return(&result);
93114262Swpaul	}
93214262Swpaul
93314262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.peer =
93414262Swpaul		v2_result1->peer;
93514262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.ordernum =
93614262Swpaul		v2_result2->ordernum;
93714262Swpaul
93814262Swpaul	return (&result);
93914262Swpaul}
94014262Swpaul
94114262Swpaulypresponse *
94214262Swpaulypoldproc_push_1_svc(yprequest *argp, struct svc_req *rqstp)
94314262Swpaul{
94414262Swpaul	static ypresponse  result;
94514262Swpaul
94614262Swpaul	/*
94714262Swpaul	 * Not implemented.
94814262Swpaul	 */
94914262Swpaul
95014262Swpaul	return (&result);
95114262Swpaul}
95214262Swpaul
95314262Swpaulypresponse *
95414262Swpaulypoldproc_pull_1_svc(yprequest *argp, struct svc_req *rqstp)
95514262Swpaul{
95614262Swpaul	static ypresponse  result;
95714262Swpaul
95814262Swpaul	/*
95914262Swpaul	 * Not implemented.
96014262Swpaul	 */
96114262Swpaul
96214262Swpaul	return (&result);
96314262Swpaul}
96414262Swpaul
96514262Swpaulypresponse *
96614262Swpaulypoldproc_get_1_svc(yprequest *argp, struct svc_req *rqstp)
96714262Swpaul{
96814262Swpaul	static ypresponse  result;
96914262Swpaul
97014262Swpaul	/*
97114262Swpaul	 * Not implemented.
97214262Swpaul	 */
97314262Swpaul
97414262Swpaul	return (&result);
97514262Swpaul}
976