yp_server.c revision 22321
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
3420818Swpaul#include "yp.h"
3512891Swpaul#include "yp_extern.h"
3612891Swpaul#include <stdlib.h>
3712891Swpaul#include <dirent.h>
3812891Swpaul#include <sys/stat.h>
3912891Swpaul#include <sys/param.h>
4012891Swpaul#include <errno.h>
4112891Swpaul#include <sys/types.h>
4212891Swpaul#include <sys/socket.h>
4312891Swpaul#include <netinet/in.h>
4412891Swpaul#include <arpa/inet.h>
4512997Swpaul#include <rpc/rpc.h>
4612891Swpaul
4712891Swpaul#ifndef lint
4821673Sjkhstatic const char rcsid[] = "$FreeBSD: head/usr.sbin/ypserv/yp_server.c 22321 1997-02-06 05:28:52Z wpaul $";
4912891Swpaul#endif /* not lint */
5012891Swpaul
5112891Swpaulint forked = 0;
5212891Swpaulint children = 0;
5319161Swpaulstatic char *master_string = "YP_MASTER_NAME";
5419161Swpaulstatic char *order_string = "YP_LAST_MODIFIED";
5519161Swpaulstatic int master_sz = sizeof("YP_MASTER_NAME") - 1;
5619161Swpaulstatic int order_sz = sizeof("YP_LAST_MODIFIED") - 1;
5712891Swpaul
5814262Swpaul/*
5914262Swpaul * NIS v2 support. This is where most of the action happens.
6014262Swpaul */
6114262Swpaul
6212891Swpaulvoid *
6312891Swpaulypproc_null_2_svc(void *argp, struct svc_req *rqstp)
6412891Swpaul{
6512891Swpaul	static char * result;
6612891Swpaul	static char rval = 0;
6712891Swpaul
6819161Swpaul#ifdef DB_CACHE
6919161Swpaul	if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
7019161Swpaul#else
7112891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
7219161Swpaul#endif
7312891Swpaul		return(NULL);
7412891Swpaul
7512891Swpaul	result = &rval;
7612891Swpaul
7712891Swpaul	return((void *) &result);
7812891Swpaul}
7912891Swpaul
8012891Swpaulbool_t *
8112891Swpaulypproc_domain_2_svc(domainname *argp, struct svc_req *rqstp)
8212891Swpaul{
8312891Swpaul	static bool_t  result;
8412891Swpaul
8519161Swpaul#ifdef DB_CACHE
8619161Swpaul	if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) {
8719161Swpaul#else
8812891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp)) {
8919161Swpaul#endif
9012891Swpaul		result = FALSE;
9112891Swpaul		return (&result);
9212891Swpaul	}
9312891Swpaul
9412891Swpaul	if (argp == NULL || yp_validdomain(*argp))
9512891Swpaul		result = FALSE;
9612891Swpaul	else
9712891Swpaul		result = TRUE;
9812891Swpaul
9912891Swpaul	return (&result);
10012891Swpaul}
10112891Swpaul
10212891Swpaulbool_t *
10312891Swpaulypproc_domain_nonack_2_svc(domainname *argp, struct svc_req *rqstp)
10412891Swpaul{
10512891Swpaul	static bool_t  result;
10612891Swpaul
10719161Swpaul#ifdef DB_CACHE
10819161Swpaul	if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
10919161Swpaul#else
11012891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
11119161Swpaul#endif
11212891Swpaul		return (NULL);
11312891Swpaul
11412891Swpaul	if (argp == NULL || yp_validdomain(*argp))
11512891Swpaul		return (NULL);
11612891Swpaul	else
11712891Swpaul		result = TRUE;
11812891Swpaul
11912891Swpaul	return (&result);
12012891Swpaul}
12112891Swpaul
12212891Swpaulypresp_val *
12312891Swpaulypproc_match_2_svc(ypreq_key *argp, struct svc_req *rqstp)
12412891Swpaul{
12512891Swpaul	static ypresp_val  result;
12612891Swpaul
12714304Swpaul	result.val.valdat_val = "";
12814304Swpaul	result.val.valdat_len = 0;
12919161Swpaul
13019161Swpaul#ifdef DB_CACHE
13119161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
13219161Swpaul#else
13312891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
13419161Swpaul#endif
13512891Swpaul		result.stat = YP_YPERR;
13612891Swpaul		return (&result);
13712891Swpaul	}
13812891Swpaul
13912891Swpaul	if (argp->domain == NULL || argp->map == NULL) {
14012891Swpaul		result.stat = YP_BADARGS;
14112891Swpaul		return (&result);
14212891Swpaul	}
14312891Swpaul
14420818Swpaul	if (yp_select_map(argp->map, argp->domain, &argp->key, 1) != YP_TRUE) {
14520818Swpaul		result.stat = yp_errno;
14620818Swpaul		return(&result);
14712891Swpaul	}
14812891Swpaul
14920818Swpaul	result.stat = yp_getbykey(&argp->key, &result.val);
15020818Swpaul
15112891Swpaul	/*
15212891Swpaul	 * Do DNS lookups for hosts maps if database lookup failed.
15312891Swpaul	 */
15412891Swpaul
15519161Swpaul#ifdef DB_CACHE
15619161Swpaul	if (result.stat != YP_TRUE &&
15719161Swpaul	    (yp_testflag(argp->map, argp->domain, YP_INTERDOMAIN) ||
15819161Swpaul	    (strstr(argp->map, "hosts") && do_dns))) {
15919161Swpaul#else
16012891Swpaul	if (do_dns && result.stat != YP_TRUE && strstr(argp->map, "hosts")) {
16119161Swpaul#endif
16220818Swpaul
16320818Swpaul		/* NUL terminate! NUL terminate!! NUL TERMINATE!!! */
16420818Swpaul		argp->key.keydat_val[argp->key.keydat_len] = '\0';
16512891Swpaul
16612891Swpaul		if (debug)
16712891Swpaul			yp_error("Doing DNS lookup of %.*s",
16812891Swpaul			 	  argp->key.keydat_len,
16912891Swpaul				  argp->key.keydat_val);
17012891Swpaul
17112891Swpaul		if (!strcmp(argp->map, "hosts.byname"))
17220907Swpaul			result.stat = yp_async_lookup_name(rqstp,
17320818Swpaul					(char *)argp->key.keydat_val);
17412891Swpaul		else if (!strcmp(argp->map, "hosts.byaddr"))
17520907Swpaul			result.stat = yp_async_lookup_addr(rqstp,
17620818Swpaul					(char *)argp->key.keydat_val);
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
20720818Swpaul	if (yp_select_map(argp->map, argp->domain, &result.key, 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;
30812891Swpaul
30913375Swpaul	result.transid = argp->transid;
31013375Swpaul	rqhost = svc_getcaller(rqstp->rq_xprt);
31113375Swpaul
31219161Swpaul#ifdef DB_CACHE
31319161Swpaul	if (yp_access(argp->map_parms.map,
31419161Swpaul			argp->map_parms.domain, (struct svc_req *)rqstp)) {
31519161Swpaul#else
31612891Swpaul	if (yp_access(argp->map_parms.map, (struct svc_req *)rqstp)) {
31719161Swpaul#endif
31815426Swpaul		YPXFR_RETURN(YPXFR_REFUSED);
31912891Swpaul	}
32012891Swpaul
32112891Swpaul	if (argp->map_parms.domain == NULL) {
32215426Swpaul		YPXFR_RETURN(YPXFR_BADARGS);
32312891Swpaul	}
32412891Swpaul
32512891Swpaul	if (yp_validdomain(argp->map_parms.domain)) {
32615426Swpaul		YPXFR_RETURN(YPXFR_NODOM);
32712891Swpaul	}
32812891Swpaul
32912891Swpaul	switch(fork()) {
33012891Swpaul	case 0:
33112891Swpaul	{
33212891Swpaul		char g[11], t[11], p[11];
33312891Swpaul		char ypxfr_command[MAXPATHLEN + 2];
33412891Swpaul
33512891Swpaul		sprintf (ypxfr_command, "%sypxfr", _PATH_LIBEXEC);
33612891Swpaul		sprintf (t, "%u", argp->transid);
33712891Swpaul		sprintf (g, "%u", argp->prog);
33812891Swpaul		sprintf (p, "%u", argp->port);
33922321Swpaul		if (debug) {
34012997Swpaul			close(0); close(1); close(2);
34122321Swpaul		}
34212997Swpaul		if (strcmp(yp_dir, _PATH_YP)) {
34314304Swpaul			execl(ypxfr_command, "ypxfr",
34414304Swpaul			"-d", argp->map_parms.domain,
34514304Swpaul		      	"-h", argp->map_parms.peer,
34614304Swpaul			"-p", yp_dir, "-C", t,
34714304Swpaul		      	g, inet_ntoa(rqhost->sin_addr),
34814304Swpaul			p, argp->map_parms.map,
34912997Swpaul		      	NULL);
35012997Swpaul		} else {
35114304Swpaul			execl(ypxfr_command, "ypxfr",
35214304Swpaul			"-d", argp->map_parms.domain,
35314304Swpaul		      	"-h", argp->map_parms.peer,
35414304Swpaul			"-C", t,
35514304Swpaul		      	g, inet_ntoa(rqhost->sin_addr),
35614304Swpaul			p, argp->map_parms.map,
35712997Swpaul		      	NULL);
35812997Swpaul		}
35912997Swpaul		forked++;
36015426Swpaul		yp_error("ypxfr execl(%s): %s", ypxfr_command, strerror(errno));
36115426Swpaul		YPXFR_RETURN(YPXFR_XFRERR);
36212997Swpaul		break;
36312891Swpaul	}
36412891Swpaul	case -1:
36512891Swpaul		yp_error("ypxfr fork(): %s", strerror(errno));
36615426Swpaul		YPXFR_RETURN(YPXFR_XFRERR);
36712891Swpaul		break;
36812891Swpaul	default:
36913375Swpaul		result.xfrstat = YPXFR_SUCC;
37012997Swpaul		children++;
37112997Swpaul		forked = 0;
37212891Swpaul		break;
37312891Swpaul	}
37413375Swpaul
37513375Swpaul	return (&result);
37612891Swpaul}
37715426Swpaul#undef YPXFR_RETURN
37812891Swpaul
37912891Swpaulvoid *
38012891Swpaulypproc_clear_2_svc(void *argp, struct svc_req *rqstp)
38112891Swpaul{
38212891Swpaul	static char * result;
38312891Swpaul	static char rval = 0;
38412891Swpaul
38519161Swpaul#ifdef DB_CACHE
38619161Swpaul	if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
38719161Swpaul#else
38812891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
38919161Swpaul#endif
39012891Swpaul		return (NULL);
39115426Swpaul#ifdef DB_CACHE
39215426Swpaul	/* clear out the database cache */
39315426Swpaul	yp_flush_all();
39415426Swpaul#endif
39514240Swpaul	/* Re-read the securenets database for the hell of it. */
39614240Swpaul	load_securenets();
39714240Swpaul
39812891Swpaul	result = &rval;
39912891Swpaul	return((void *) &result);
40012891Swpaul}
40112891Swpaul
40212891Swpaul/*
40312891Swpaul * For ypproc_all, we have to send a stream of ypresp_all structures
40412891Swpaul * via TCP, but the XDR filter generated from the yp.x protocol
40512891Swpaul * definition file only serializes one such structure. This means that
40612891Swpaul * to send the whole stream, you need a wrapper which feeds all the
40712891Swpaul * records into the underlying XDR routine until it hits an 'EOF.'
40812891Swpaul * But to use the wrapper, you have to violate the boundaries between
40912891Swpaul * RPC layers by calling svc_sendreply() directly from the ypproc_all
41012891Swpaul * service routine instead of letting the RPC dispatcher do it.
41112891Swpaul *
41212891Swpaul * Bleah.
41312891Swpaul */
41412891Swpaul
41512891Swpaul/*
41620100Swpaul * Custom XDR routine for serialzing results of ypproc_all: keep
41720100Swpaul * reading from the database and spew until we run out of records
41820100Swpaul * or encounter an error.
41912891Swpaul */
42012891Swpaulstatic bool_t
42112891Swpaulxdr_my_ypresp_all(register XDR *xdrs, ypresp_all *objp)
42212891Swpaul{
42320100Swpaul	while (1) {
42420100Swpaul		/* Get a record. */
42520100Swpaul		if ((objp->ypresp_all_u.val.stat =
42620818Swpaul			yp_nextbykey(&objp->ypresp_all_u.val.key,
42720818Swpaul				     &objp->ypresp_all_u.val.val)) == YP_TRUE) {
42820100Swpaul			objp->more = TRUE;
42920100Swpaul		} else {
43020100Swpaul			objp->more = FALSE;
43120100Swpaul		}
43220100Swpaul
43320100Swpaul		/* Serialize. */
43420100Swpaul		if (!xdr_ypresp_all(xdrs, objp))
43520100Swpaul			return(FALSE);
43620100Swpaul		if (objp->more == FALSE)
43720100Swpaul			return(TRUE);
43820100Swpaul	}
43912891Swpaul}
44012891Swpaul
44112891Swpaulypresp_all *
44212891Swpaulypproc_all_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
44312891Swpaul{
44412891Swpaul	static ypresp_all  result;
44512891Swpaul
44612891Swpaul	/*
44712891Swpaul	 * Set this here so that the client will be forced to make
44812891Swpaul	 * at least one attempt to read from us even if all we're
44912891Swpaul	 * doing is returning an error.
45012891Swpaul	 */
45112891Swpaul	result.more = TRUE;
45214304Swpaul	result.ypresp_all_u.val.key.keydat_len = 0;
45314304Swpaul	result.ypresp_all_u.val.key.keydat_val = "";
45412891Swpaul
45519161Swpaul#ifdef DB_CACHE
45619161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
45719161Swpaul#else
45812891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
45919161Swpaul#endif
46012891Swpaul		result.ypresp_all_u.val.stat = YP_YPERR;
46112891Swpaul		return (&result);
46212891Swpaul	}
46312891Swpaul
46412891Swpaul	if (argp->domain == NULL || argp->map == NULL) {
46512891Swpaul		result.ypresp_all_u.val.stat = YP_BADARGS;
46612891Swpaul		return (&result);
46712891Swpaul	}
46812891Swpaul
46920100Swpaul	/*
47021389Swpaul	 * XXX If we hit the child limit, fail the request.
47121389Swpaul	 * If we don't, and the map is large, we could block for
47221389Swpaul	 * a long time in the parent.
47321389Swpaul	 */
47421389Swpaul	if (children >= MAX_CHILDREN) {
47521389Swpaul		result.ypresp_all_u.val.stat = YP_YPERR;
47621389Swpaul		return(&result);
47721389Swpaul	}
47821389Swpaul
47921389Swpaul	/*
48020100Swpaul	 * The ypproc_all procedure can take a while to complete.
48120100Swpaul	 * Best to handle it in a subprocess so the parent doesn't
48220100Swpaul	 * block. (Is there a better way to do this? Maybe with
48320100Swpaul	 * async socket I/O?)
48420100Swpaul	 */
48520100Swpaul	if (!debug && children < MAX_CHILDREN && fork()) {
48620100Swpaul		children++;
48720100Swpaul		forked = 0;
48820100Swpaul		return (NULL);
48920100Swpaul	} else {
49020100Swpaul		forked++;
49120100Swpaul	}
49220100Swpaul
49320818Swpaul	if (yp_select_map(argp->map, argp->domain,
49420818Swpaul				&result.ypresp_all_u.val.key, 0) != YP_TRUE) {
49512891Swpaul		result.ypresp_all_u.val.stat = yp_errno;
49612891Swpaul		return(&result);
49712891Swpaul	}
49812891Swpaul
49912891Swpaul	/* Kick off the actual data transfer. */
50020100Swpaul	svc_sendreply(rqstp->rq_xprt, xdr_my_ypresp_all, (char *)&result);
50120100Swpaul
50212891Swpaul	/*
50312891Swpaul	 * Returning NULL prevents the dispatcher from calling
50412891Swpaul	 * svc_sendreply() since we already did it.
50512891Swpaul	 */
50612891Swpaul	return (NULL);
50712891Swpaul}
50812891Swpaul
50912891Swpaulypresp_master *
51012891Swpaulypproc_master_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
51112891Swpaul{
51212891Swpaul	static ypresp_master  result;
51315426Swpaul	static char ypvalbuf[YPMAXRECORD];
51420818Swpaul	keydat key = { master_sz, master_string };
51520818Swpaul	valdat val;
51612891Swpaul
51714303Swpaul	result.peer = "";
51814303Swpaul
51919161Swpaul#ifdef DB_CACHE
52019161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
52119161Swpaul#else
52219161Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
52319161Swpaul#endif
52412891Swpaul		result.stat = YP_YPERR;
52512891Swpaul		return(&result);
52612891Swpaul	}
52712891Swpaul
52812891Swpaul	if (argp->domain == NULL) {
52912891Swpaul		result.stat = YP_BADARGS;
53012891Swpaul		return (&result);
53112891Swpaul	}
53212891Swpaul
53320818Swpaul	if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) {
53420818Swpaul		result.stat = yp_errno;
53520818Swpaul		return(&result);
53620818Swpaul	}
53720818Swpaul
53815426Swpaul	/*
53915426Swpaul	 * Note that we copy the data retrieved from the database to
54015426Swpaul	 * a private buffer and NUL terminate the buffer rather than
54115426Swpaul	 * terminating the data in place. We do this because by stuffing
54215426Swpaul	 * a '\0' into data.data, we will actually be corrupting memory
54315426Swpaul	 * allocated by the DB package. This is a bad thing now that we
54415426Swpaul	 * cache DB handles rather than closing the database immediately.
54515426Swpaul	 */
54620818Swpaul	result.stat = yp_getbykey(&key, &val);
54720818Swpaul	if (result.stat == YP_TRUE) {
54820818Swpaul		bcopy((char *)val.valdat_val, (char *)&ypvalbuf,
54920818Swpaul						val.valdat_len);
55020818Swpaul		ypvalbuf[val.valdat_len] = '\0';
55115426Swpaul		result.peer = (char *)&ypvalbuf;
55212891Swpaul	} else
55312891Swpaul		result.peer = "";
55412891Swpaul
55512891Swpaul	return (&result);
55612891Swpaul}
55712891Swpaul
55812891Swpaulypresp_order *
55912891Swpaulypproc_order_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
56012891Swpaul{
56112891Swpaul	static ypresp_order  result;
56220818Swpaul	keydat key = { order_sz, order_string };
56320818Swpaul	valdat val;
56412891Swpaul
56514304Swpaul	result.ordernum = 0;
56614304Swpaul
56719161Swpaul#ifdef DB_CACHE
56819161Swpaul	if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
56919161Swpaul#else
57019161Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
57119161Swpaul#endif
57212891Swpaul		result.stat = YP_YPERR;
57312891Swpaul		return(&result);
57412891Swpaul	}
57512891Swpaul
57612891Swpaul	if (argp->domain == NULL) {
57712891Swpaul		result.stat = YP_BADARGS;
57812891Swpaul		return (&result);
57912891Swpaul	}
58012891Swpaul
58112891Swpaul	/*
58212891Swpaul	 * We could just check the timestamp on the map file,
58312891Swpaul	 * but that's a hack: we'll only know the last time the file
58412891Swpaul	 * was touched, not the last time the database contents were
58512891Swpaul	 * updated.
58612891Swpaul	 */
58712891Swpaul
58820818Swpaul	if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) {
58920818Swpaul		result.stat = yp_errno;
59020818Swpaul		return(&result);
59120818Swpaul	}
59220818Swpaul
59320818Swpaul	result.stat = yp_getbykey(&key, &val);
59420818Swpaul
59520818Swpaul	if (result.stat == YP_TRUE)
59620818Swpaul		result.ordernum = atoi((char *)val.valdat_val);
59712891Swpaul	else
59812891Swpaul		result.ordernum = 0;
59912891Swpaul
60012891Swpaul	return (&result);
60112891Swpaul}
60212891Swpaul
60312891Swpaulstatic void yp_maplist_free(yp_maplist)
60412891Swpaul	struct ypmaplist *yp_maplist;
60512891Swpaul{
60612891Swpaul	register struct ypmaplist *next;
60712891Swpaul
60812891Swpaul	while(yp_maplist) {
60912891Swpaul		next = yp_maplist->next;
61012891Swpaul		free(yp_maplist->map);
61112891Swpaul		free(yp_maplist);
61212891Swpaul		yp_maplist = next;
61312891Swpaul	}
61412891Swpaul	return;
61512891Swpaul}
61612891Swpaul
61712891Swpaulstatic struct ypmaplist *yp_maplist_create(domain)
61812891Swpaul	const char *domain;
61912891Swpaul{
62012891Swpaul	char yp_mapdir[MAXPATHLEN + 2];
62112891Swpaul	char yp_mapname[MAXPATHLEN + 2];
62212891Swpaul	struct ypmaplist *cur = NULL;
62312891Swpaul	struct ypmaplist *yp_maplist = NULL;
62412891Swpaul	DIR *dird;
62512891Swpaul	struct dirent *dirp;
62612891Swpaul	struct stat statbuf;
62712891Swpaul
62812891Swpaul	snprintf(yp_mapdir, sizeof(yp_mapdir), "%s/%s", yp_dir, domain);
62912891Swpaul
63012891Swpaul	if ((dird = opendir(yp_mapdir)) == NULL) {
63113800Swpaul		yp_error("opendir(%s) failed: %s", yp_mapdir, strerror(errno));
63212891Swpaul		return(NULL);
63312891Swpaul	}
63412891Swpaul
63512891Swpaul	while ((dirp = readdir(dird)) != NULL) {
63612891Swpaul		if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
63714304Swpaul			snprintf(yp_mapname, sizeof(yp_mapname), "%s/%s",
63814304Swpaul							yp_mapdir,dirp->d_name);
63914304Swpaul			if (stat(yp_mapname, &statbuf) < 0 ||
64014304Swpaul						!S_ISREG(statbuf.st_mode))
64112891Swpaul				continue;
64214304Swpaul			if ((cur = (struct ypmaplist *)
64316044Swpaul				malloc(sizeof(struct ypmaplist))) == NULL) {
64414304Swpaul				yp_error("malloc() failed: %s",strerror(errno));
64512891Swpaul				closedir(dird);
64612891Swpaul				yp_maplist_free(yp_maplist);
64712891Swpaul				return(NULL);
64812891Swpaul			}
64912891Swpaul			if ((cur->map = (char *)strdup(dirp->d_name)) == NULL) {
65014304Swpaul				yp_error("strdup() failed: %s",strerror(errno));
65112891Swpaul				closedir(dird);
65212891Swpaul				yp_maplist_free(yp_maplist);
65312891Swpaul				return(NULL);
65412891Swpaul			}
65512891Swpaul			cur->next = yp_maplist;
65612891Swpaul			yp_maplist = cur;
65712891Swpaul			if (debug)
65812891Swpaul				yp_error("map: %s", yp_maplist->map);
65912891Swpaul		}
66012891Swpaul
66112891Swpaul	}
66212891Swpaul	closedir(dird);
66312891Swpaul	return(yp_maplist);
66412891Swpaul}
66512891Swpaul
66612891Swpaulypresp_maplist *
66712891Swpaulypproc_maplist_2_svc(domainname *argp, struct svc_req *rqstp)
66812891Swpaul{
66915426Swpaul	static ypresp_maplist  result = { 0, NULL };
67012891Swpaul
67119161Swpaul#ifdef DB_CACHE
67219161Swpaul	if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) {
67319161Swpaul#else
67412891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp)) {
67519161Swpaul#endif
67612891Swpaul		result.stat = YP_YPERR;
67712891Swpaul		return(&result);
67812891Swpaul	}
67912891Swpaul
68012891Swpaul	if (argp == NULL) {
68112891Swpaul		result.stat = YP_BADARGS;
68212891Swpaul		return (&result);
68312891Swpaul	}
68412891Swpaul
68512891Swpaul	if (yp_validdomain(*argp)) {
68612891Swpaul		result.stat = YP_NODOM;
68712891Swpaul		return (&result);
68812891Swpaul	}
68912891Swpaul
69012891Swpaul	/*
69112891Swpaul	 * We have to construct a linked list for the ypproc_maplist
69212891Swpaul	 * procedure using dynamically allocated memory. Since the XDR
69312891Swpaul	 * layer won't free this list for us, we have to deal with it
69412891Swpaul	 * ourselves. We call yp_maplist_free() first to free any
69512891Swpaul	 * previously allocated data we may have accumulated to insure
69612891Swpaul	 * that we have only one linked list in memory at any given
69712891Swpaul	 * time.
69812891Swpaul	 */
69912891Swpaul
70012891Swpaul	yp_maplist_free(result.maps);
70112891Swpaul
70212891Swpaul	if ((result.maps = yp_maplist_create(*argp)) == NULL) {
70312891Swpaul		yp_error("yp_maplist_create failed");
70412891Swpaul		result.stat = YP_YPERR;
70512891Swpaul		return(&result);
70612891Swpaul	} else
70712891Swpaul		result.stat = YP_TRUE;
70812891Swpaul
70912891Swpaul	return (&result);
71012891Swpaul}
71114262Swpaul
71214262Swpaul/*
71314262Swpaul * NIS v1 support. The nullproc, domain and domain_nonack
71414262Swpaul * functions from v1 are identical to those in v2, so all
71514262Swpaul * we have to do is hand off to them.
71614262Swpaul *
71714262Swpaul * The other functions are mostly just wrappers around their v2
71814262Swpaul * counterparts. For example, for the v1 'match' procedure, we
71914262Swpaul * crack open the argument structure, make a request to the v2
72014262Swpaul * 'match' function, repackage the data into a v1 response and
72114262Swpaul * then send it on its way.
72214262Swpaul *
72314262Swpaul * Note that we don't support the pull, push and get procedures.
72414262Swpaul * There's little documentation available to show what they
72514262Swpaul * do, and I suspect they're meant largely for map transfers
72614262Swpaul * between master and slave servers.
72714262Swpaul */
72814262Swpaul
72914262Swpaulvoid *
73014262Swpaulypoldproc_null_1_svc(void *argp, struct svc_req *rqstp)
73114262Swpaul{
73214262Swpaul	return(ypproc_null_2_svc(argp, rqstp));
73314262Swpaul}
73414262Swpaul
73514262Swpaulbool_t *
73614262Swpaulypoldproc_domain_1_svc(domainname *argp, struct svc_req *rqstp)
73714262Swpaul{
73814262Swpaul	return(ypproc_domain_2_svc(argp, rqstp));
73914262Swpaul}
74014262Swpaul
74114262Swpaulbool_t *
74214262Swpaulypoldproc_domain_nonack_1_svc(domainname *argp, struct svc_req *rqstp)
74314262Swpaul{
74414262Swpaul	return (ypproc_domain_nonack_2_svc(argp, rqstp));
74514262Swpaul}
74614262Swpaul
74714304Swpaul/*
74814304Swpaul * the 'match' procedure sends a response of type YPRESP_VAL
74914304Swpaul */
75014262Swpaulypresponse *
75114262Swpaulypoldproc_match_1_svc(yprequest *argp, struct svc_req *rqstp)
75214262Swpaul{
75314262Swpaul	static ypresponse  result;
75414262Swpaul	ypresp_val *v2_result;
75514262Swpaul
75614262Swpaul	result.yp_resptype = YPRESP_VAL;
75714304Swpaul	result.ypresponse_u.yp_resp_valtype.val.valdat_val = "";
75814304Swpaul	result.ypresponse_u.yp_resp_valtype.val.valdat_len = 0;
75914262Swpaul
76014262Swpaul	if (argp->yp_reqtype != YPREQ_KEY) {
76114262Swpaul		result.ypresponse_u.yp_resp_valtype.stat = YP_BADARGS;
76214262Swpaul		return(&result);
76314262Swpaul	}
76414262Swpaul
76514262Swpaul	v2_result = ypproc_match_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp);
76614262Swpaul	if (v2_result == NULL)
76714262Swpaul		return(NULL);
76814262Swpaul
76914262Swpaul	bcopy((char *)v2_result,
77014262Swpaul	      (char *)&result.ypresponse_u.yp_resp_valtype,
77114262Swpaul	      sizeof(ypresp_val));
77214262Swpaul
77314262Swpaul	return (&result);
77414262Swpaul}
77514262Swpaul
77614304Swpaul/*
77714304Swpaul * the 'first' procedure sends a response of type YPRESP_KEY_VAL
77814304Swpaul */
77914262Swpaulypresponse *
78014262Swpaulypoldproc_first_1_svc(yprequest *argp, struct svc_req *rqstp)
78114262Swpaul{
78214262Swpaul	static ypresponse  result;
78314262Swpaul	ypresp_key_val *v2_result;
78414262Swpaul
78514262Swpaul	result.yp_resptype = YPRESP_KEY_VAL;
78614304Swpaul	result.ypresponse_u.yp_resp_key_valtype.val.valdat_val =
78714304Swpaul	result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = "";
78814304Swpaul	result.ypresponse_u.yp_resp_key_valtype.val.valdat_len =
78914304Swpaul	result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0;
79014262Swpaul
79114262Swpaul	if (argp->yp_reqtype != YPREQ_NOKEY) {
79214262Swpaul		result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS;
79314262Swpaul		return(&result);
79414262Swpaul	}
79514262Swpaul
79614262Swpaul	v2_result = ypproc_first_2_svc(&argp->yprequest_u.yp_req_nokeytype,
79714262Swpaul									rqstp);
79814262Swpaul	if (v2_result == NULL)
79914262Swpaul		return(NULL);
80014262Swpaul
80114262Swpaul	bcopy((char *)v2_result,
80214262Swpaul	      (char *)&result.ypresponse_u.yp_resp_key_valtype,
80314262Swpaul	      sizeof(ypresp_key_val));
80414262Swpaul
80514262Swpaul	return (&result);
80614262Swpaul}
80714262Swpaul
80814304Swpaul/*
80914304Swpaul * the 'next' procedure sends a response of type YPRESP_KEY_VAL
81014304Swpaul */
81114262Swpaulypresponse *
81214262Swpaulypoldproc_next_1_svc(yprequest *argp, struct svc_req *rqstp)
81314262Swpaul{
81414262Swpaul	static ypresponse  result;
81514262Swpaul	ypresp_key_val *v2_result;
81614262Swpaul
81714262Swpaul	result.yp_resptype = YPRESP_KEY_VAL;
81814304Swpaul	result.ypresponse_u.yp_resp_key_valtype.val.valdat_val =
81914304Swpaul	result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = "";
82014304Swpaul	result.ypresponse_u.yp_resp_key_valtype.val.valdat_len =
82114304Swpaul	result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0;
82214262Swpaul
82314262Swpaul	if (argp->yp_reqtype != YPREQ_KEY) {
82414262Swpaul		result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS;
82514262Swpaul		return(&result);
82614262Swpaul	}
82714262Swpaul
82814262Swpaul	v2_result = ypproc_next_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp);
82914262Swpaul	if (v2_result == NULL)
83014262Swpaul		return(NULL);
83114262Swpaul
83214262Swpaul	bcopy((char *)v2_result,
83314262Swpaul	      (char *)&result.ypresponse_u.yp_resp_key_valtype,
83414262Swpaul	      sizeof(ypresp_key_val));
83514262Swpaul
83614262Swpaul	return (&result);
83714262Swpaul}
83814262Swpaul
83914304Swpaul/*
84014304Swpaul * the 'poll' procedure sends a response of type YPRESP_MAP_PARMS
84114304Swpaul */
84214262Swpaulypresponse *
84314262Swpaulypoldproc_poll_1_svc(yprequest *argp, struct svc_req *rqstp)
84414262Swpaul{
84514262Swpaul	static ypresponse  result;
84614262Swpaul	ypresp_master *v2_result1;
84714262Swpaul	ypresp_order *v2_result2;
84814262Swpaul
84914262Swpaul	result.yp_resptype = YPRESP_MAP_PARMS;
85014262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.domain =
85114262Swpaul		argp->yprequest_u.yp_req_nokeytype.domain;
85214262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.map =
85314262Swpaul		argp->yprequest_u.yp_req_nokeytype.map;
85414262Swpaul	/*
85514262Swpaul	 * Hmm... there is no 'status' value in the
85614262Swpaul	 * yp_resp_map_parmstype structure, so I have to
85714262Swpaul	 * guess at what to do to indicate a failure.
85814262Swpaul	 * I hope this is right.
85914262Swpaul	 */
86014262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.ordernum = 0;
86114262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.peer = "";
86214262Swpaul
86314262Swpaul	if (argp->yp_reqtype != YPREQ_MAP_PARMS) {
86414262Swpaul		return(&result);
86514262Swpaul	}
86614262Swpaul
86714262Swpaul	v2_result1 = ypproc_master_2_svc(&argp->yprequest_u.yp_req_nokeytype,
86814262Swpaul									rqstp);
86914262Swpaul	if (v2_result1 == NULL)
87014262Swpaul		return(NULL);
87114262Swpaul
87214262Swpaul	if (v2_result1->stat != YP_TRUE) {
87314262Swpaul		return(&result);
87414262Swpaul	}
87514262Swpaul
87614262Swpaul	v2_result2 = ypproc_order_2_svc(&argp->yprequest_u.yp_req_nokeytype,
87714262Swpaul									rqstp);
87814262Swpaul	if (v2_result2 == NULL)
87914262Swpaul		return(NULL);
88014262Swpaul
88114262Swpaul	if (v2_result2->stat != YP_TRUE) {
88214262Swpaul		return(&result);
88314262Swpaul	}
88414262Swpaul
88514262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.peer =
88614262Swpaul		v2_result1->peer;
88714262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.ordernum =
88814262Swpaul		v2_result2->ordernum;
88914262Swpaul
89014262Swpaul	return (&result);
89114262Swpaul}
89214262Swpaul
89314262Swpaulypresponse *
89414262Swpaulypoldproc_push_1_svc(yprequest *argp, struct svc_req *rqstp)
89514262Swpaul{
89614262Swpaul	static ypresponse  result;
89714262Swpaul
89814262Swpaul	/*
89914262Swpaul	 * Not implemented.
90014262Swpaul	 */
90114262Swpaul
90214262Swpaul	return (&result);
90314262Swpaul}
90414262Swpaul
90514262Swpaulypresponse *
90614262Swpaulypoldproc_pull_1_svc(yprequest *argp, struct svc_req *rqstp)
90714262Swpaul{
90814262Swpaul	static ypresponse  result;
90914262Swpaul
91014262Swpaul	/*
91114262Swpaul	 * Not implemented.
91214262Swpaul	 */
91314262Swpaul
91414262Swpaul	return (&result);
91514262Swpaul}
91614262Swpaul
91714262Swpaulypresponse *
91814262Swpaulypoldproc_get_1_svc(yprequest *argp, struct svc_req *rqstp)
91914262Swpaul{
92014262Swpaul	static ypresponse  result;
92114262Swpaul
92214262Swpaul	/*
92314262Swpaul	 * Not implemented.
92414262Swpaul	 */
92514262Swpaul
92614262Swpaul	return (&result);
92714262Swpaul}
928