yp_server.c revision 19131
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
3412891Swpaul#include "yp_extern.h"
3512891Swpaul#include "yp.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
4819131Swpaulstatic const char rcsid[] = "$Id: yp_server.c,v 1.10 1996/05/31 16:01:51 wpaul Exp $";
4912891Swpaul#endif /* not lint */
5012891Swpaul
5112891Swpaulint forked = 0;
5212891Swpaulint children = 0;
5312891SwpaulDB *spec_dbp = NULL;	/* Special global DB handle for ypproc_all. */
5415426Swpaulchar *master_string = "YP_MASTER_NAME";
5515426Swpaulchar *order_string = "YP_LAST_MODIFIED";
5612891Swpaul
5714262Swpaul/*
5814262Swpaul * NIS v2 support. This is where most of the action happens.
5914262Swpaul */
6014262Swpaul
6112891Swpaulvoid *
6212891Swpaulypproc_null_2_svc(void *argp, struct svc_req *rqstp)
6312891Swpaul{
6412891Swpaul	static char * result;
6512891Swpaul	static char rval = 0;
6612891Swpaul
6712891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
6812891Swpaul		return(NULL);
6912891Swpaul
7012891Swpaul	result = &rval;
7112891Swpaul
7212891Swpaul	return((void *) &result);
7312891Swpaul}
7412891Swpaul
7512891Swpaulbool_t *
7612891Swpaulypproc_domain_2_svc(domainname *argp, struct svc_req *rqstp)
7712891Swpaul{
7812891Swpaul	static bool_t  result;
7912891Swpaul
8012891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp)) {
8112891Swpaul		result = FALSE;
8212891Swpaul		return (&result);
8312891Swpaul	}
8412891Swpaul
8512891Swpaul	if (argp == NULL || yp_validdomain(*argp))
8612891Swpaul		result = FALSE;
8712891Swpaul	else
8812891Swpaul		result = TRUE;
8912891Swpaul
9012891Swpaul	return (&result);
9112891Swpaul}
9212891Swpaul
9312891Swpaulbool_t *
9412891Swpaulypproc_domain_nonack_2_svc(domainname *argp, struct svc_req *rqstp)
9512891Swpaul{
9612891Swpaul	static bool_t  result;
9712891Swpaul
9812891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
9912891Swpaul		return (NULL);
10012891Swpaul
10112891Swpaul	if (argp == NULL || yp_validdomain(*argp))
10212891Swpaul		return (NULL);
10312891Swpaul	else
10412891Swpaul		result = TRUE;
10512891Swpaul
10612891Swpaul	return (&result);
10712891Swpaul}
10812891Swpaul
10912891Swpaulypresp_val *
11012891Swpaulypproc_match_2_svc(ypreq_key *argp, struct svc_req *rqstp)
11112891Swpaul{
11212891Swpaul	static ypresp_val  result;
11312891Swpaul	DBT key, data;
11412891Swpaul
11514304Swpaul	result.val.valdat_val = "";
11614304Swpaul	result.val.valdat_len = 0;
11714304Swpaul
11812891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
11912891Swpaul		result.stat = YP_YPERR;
12012891Swpaul		return (&result);
12112891Swpaul	}
12212891Swpaul
12312891Swpaul	if (argp->domain == NULL || argp->map == NULL) {
12412891Swpaul		result.stat = YP_BADARGS;
12512891Swpaul		return (&result);
12612891Swpaul	}
12712891Swpaul
12812891Swpaul	key.size = argp->key.keydat_len;
12912891Swpaul	key.data = argp->key.keydat_val;
13012891Swpaul
13115426Swpaul	if ((result.stat = yp_get_record(argp->domain, argp->map,
13215426Swpaul						&key, &data, 1)) == YP_TRUE) {
13312891Swpaul		result.val.valdat_len = data.size;
13412891Swpaul		result.val.valdat_val = data.data;
13512891Swpaul	}
13612891Swpaul
13712891Swpaul	/*
13812891Swpaul	 * Do DNS lookups for hosts maps if database lookup failed.
13912891Swpaul	 */
14012891Swpaul
14112891Swpaul	if (do_dns && result.stat != YP_TRUE && strstr(argp->map, "hosts")) {
14212997Swpaul		char *rval = NULL;
14312891Swpaul
14412891Swpaul	/* DNS lookups can take time -- do them in a subprocess */
14512891Swpaul
14612891Swpaul		if (!debug && children < MAX_CHILDREN && fork()) {
14712891Swpaul			children++;
14812891Swpaul			forked = 0;
14912891Swpaul			/*
15012891Swpaul			 * Returning NULL here prevents svc_sendreply()
15112891Swpaul			 * from being called by the parent. This is vital
15212891Swpaul			 * since having both the parent and the child process
15312891Swpaul			 * call it would confuse the client.
15412891Swpaul			 */
15512891Swpaul			return (NULL);
15612891Swpaul		} else {
15712891Swpaul			forked++;
15812891Swpaul		}
15912891Swpaul
16012891Swpaul		if (debug)
16112891Swpaul			yp_error("Doing DNS lookup of %.*s",
16212891Swpaul			 	  argp->key.keydat_len,
16312891Swpaul				  argp->key.keydat_val);
16412891Swpaul
16512891Swpaul		/* NUL terminate! NUL terminate!! NUL TERMINATE!!! */
16612891Swpaul		argp->key.keydat_val[argp->key.keydat_len] = '\0';
16712891Swpaul
16812891Swpaul		if (!strcmp(argp->map, "hosts.byname"))
16912891Swpaul			rval = yp_dnsname((char *)argp->key.keydat_val);
17012891Swpaul		else if (!strcmp(argp->map, "hosts.byaddr"))
17112891Swpaul			rval = yp_dnsaddr((const char *)argp->key.keydat_val);
17212891Swpaul
17312891Swpaul
17412891Swpaul		if (rval) {
17512891Swpaul			if (debug)
17614304Swpaul				yp_error("DNS lookup successful. Result: %s",
17714304Swpaul									rval);
17812891Swpaul			result.val.valdat_len = strlen(rval);
17912891Swpaul			result.val.valdat_val = rval;
18012891Swpaul			result.stat = YP_TRUE;
18112891Swpaul		} else {
18212891Swpaul			if (debug)
18312891Swpaul				yp_error("DNS lookup failed.");
18412891Swpaul			result.stat = YP_NOKEY;
18512891Swpaul		}
18612891Swpaul	}
18712891Swpaul
18812891Swpaul	return (&result);
18912891Swpaul}
19012891Swpaul
19112891Swpaulypresp_key_val *
19212891Swpaulypproc_first_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
19312891Swpaul{
19412891Swpaul	static ypresp_key_val  result;
19512891Swpaul	DBT key, data;
19612891Swpaul	DB *dbp;
19712891Swpaul
19814304Swpaul	result.val.valdat_val = result.key.keydat_val = "";
19914304Swpaul	result.val.valdat_len = result.key.keydat_len = 0;
20014304Swpaul
20112891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
20212891Swpaul		result.stat = YP_YPERR;
20312891Swpaul		return (&result);
20412891Swpaul	}
20512891Swpaul
20612891Swpaul	if (argp->domain == NULL) {
20712891Swpaul		result.stat = YP_BADARGS;
20812891Swpaul		return (&result);
20912891Swpaul	}
21012891Swpaul
21115426Swpaul#ifdef DB_CACHE
21215426Swpaul	if ((dbp = yp_open_db_cache(argp->domain, argp->map, NULL, 0)) == NULL) {
21315426Swpaul#else
21412891Swpaul	if ((dbp = yp_open_db(argp->domain, argp->map)) == NULL) {
21515426Swpaul#endif
21612891Swpaul		result.stat = yp_errno;
21712891Swpaul		return(&result);
21812891Swpaul	}
21912891Swpaul
22012891Swpaul	key.data = NULL;
22112891Swpaul	key.size = 0;
22212891Swpaul
22315426Swpaul	if ((result.stat = yp_first_record(dbp, &key, &data, 0)) == YP_TRUE) {
22412891Swpaul		result.key.keydat_len = key.size;
22512891Swpaul		result.key.keydat_val = key.data;
22612891Swpaul		result.val.valdat_len = data.size;
22712891Swpaul		result.val.valdat_val = data.data;
22812891Swpaul	}
22915426Swpaul#ifndef DB_CACHE
23015426Swpaul	(void)(dbp->close)(dbp);
23115426Swpaul#endif
23212891Swpaul	return (&result);
23312891Swpaul}
23412891Swpaul
23512891Swpaulypresp_key_val *
23612891Swpaulypproc_next_2_svc(ypreq_key *argp, struct svc_req *rqstp)
23712891Swpaul{
23812891Swpaul	static ypresp_key_val  result;
23912891Swpaul	DBT key, data;
24012891Swpaul	DB *dbp;
24112891Swpaul
24214304Swpaul	result.val.valdat_val = result.key.keydat_val = "";
24314304Swpaul	result.val.valdat_len = result.key.keydat_len = 0;
24415426Swpaul
24512891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
24612891Swpaul		result.stat = YP_YPERR;
24712891Swpaul		return (&result);
24812891Swpaul	}
24912891Swpaul
25012891Swpaul	if (argp->domain == NULL || argp->map == NULL) {
25112891Swpaul		result.stat = YP_BADARGS;
25212891Swpaul		return (&result);
25312891Swpaul	}
25412891Swpaul
25515426Swpaul#ifdef DB_CACHE
25615426Swpaul	if ((dbp = yp_open_db_cache(argp->domain, argp->map,
25715426Swpaul					argp->key.keydat_val,
25815426Swpaul					argp->key.keydat_len)) == NULL) {
25915426Swpaul#else
26012891Swpaul	if ((dbp = yp_open_db(argp->domain, argp->map)) == NULL) {
26115426Swpaul#endif
26212891Swpaul		result.stat = yp_errno;
26312891Swpaul		return(&result);
26412891Swpaul	}
26512891Swpaul
26612891Swpaul	key.size = argp->key.keydat_len;
26712891Swpaul	key.data = argp->key.keydat_val;
26812891Swpaul
26915426Swpaul	if ((result.stat = yp_next_record(dbp, &key, &data,0,0)) == YP_TRUE) {
27012891Swpaul		result.key.keydat_len = key.size;
27112891Swpaul		result.key.keydat_val = key.data;
27212891Swpaul		result.val.valdat_len = data.size;
27312891Swpaul		result.val.valdat_val = data.data;
27412891Swpaul	}
27515426Swpaul#ifndef DB_CACHE
27615426Swpaul	(void)(dbp->close)(dbp);
27715426Swpaul#endif
27812891Swpaul	return (&result);
27912891Swpaul}
28012891Swpaul
28112997Swpaulstatic void ypxfr_callback(rval,addr,transid,prognum,port)
28212997Swpaul	ypxfrstat rval;
28312997Swpaul	struct sockaddr_in *addr;
28412997Swpaul	unsigned int transid;
28512997Swpaul	unsigned int prognum;
28612997Swpaul	unsigned long port;
28712997Swpaul{
28812997Swpaul	CLIENT *clnt;
28912997Swpaul	int sock = RPC_ANYSOCK;
29012997Swpaul	struct timeval timeout;
29112997Swpaul	yppushresp_xfr ypxfr_resp;
29213375Swpaul	struct rpc_err err;
29312997Swpaul
29413375Swpaul	timeout.tv_sec = 5;
29512997Swpaul	timeout.tv_usec = 0;
29612997Swpaul	addr->sin_port = htons(port);
29712997Swpaul
29819131Swpaul	if ((clnt = clntudp_create(addr,prognum,1,timeout,&sock)) == NULL) {
29919131Swpaul		yp_error("%s: %s", inet_ntoa(addr->sin_addr),
30019131Swpaul		    clnt_spcreateerror("failed to establish callback handle"));
30119131Swpaul		return;
30219131Swpaul	}
30312997Swpaul
30412997Swpaul	ypxfr_resp.status = rval;
30512997Swpaul	ypxfr_resp.transid = transid;
30612997Swpaul
30713375Swpaul	/* Turn the timeout off -- we don't want to block. */
30813375Swpaul	timeout.tv_sec = 0;
30913375Swpaul	if (clnt_control(clnt, CLSET_TIMEOUT, (char *)&timeout) == FALSE)
31013375Swpaul		yp_error("failed to set timeout on ypproc_xfr callback");
31112997Swpaul
31213375Swpaul	if (yppushproc_xfrresp_1(&ypxfr_resp, clnt) == NULL) {
31313375Swpaul		clnt_geterr(clnt, &err);
31413375Swpaul		if (err.re_status != RPC_SUCCESS &&
31513375Swpaul		    err.re_status != RPC_TIMEDOUT)
31613375Swpaul			yp_error("%s", clnt_sperror(clnt,
31713375Swpaul				"ypxfr callback failed"));
31813375Swpaul	}
31913375Swpaul
32012997Swpaul	clnt_destroy(clnt);
32112997Swpaul	return;
32212997Swpaul}
32312997Swpaul
32415426Swpaul#define YPXFR_RETURN(CODE) 						\
32515426Swpaul	/* Order is important: send regular RPC reply, then callback */	\
32615426Swpaul	result.xfrstat = CODE; 						\
32715426Swpaul	svc_sendreply(rqstp->rq_xprt, xdr_ypresp_xfr, (char *)&result); \
32815426Swpaul	ypxfr_callback(CODE,rqhost,argp->transid, 			\
32915426Swpaul					argp->prog,argp->port); 	\
33015426Swpaul	return(NULL);
33115426Swpaul
33212891Swpaulypresp_xfr *
33312891Swpaulypproc_xfr_2_svc(ypreq_xfr *argp, struct svc_req *rqstp)
33412891Swpaul{
33512891Swpaul	static ypresp_xfr  result;
33612997Swpaul	struct sockaddr_in *rqhost;
33712891Swpaul
33813375Swpaul	result.transid = argp->transid;
33913375Swpaul	rqhost = svc_getcaller(rqstp->rq_xprt);
34013375Swpaul
34112891Swpaul	if (yp_access(argp->map_parms.map, (struct svc_req *)rqstp)) {
34215426Swpaul		YPXFR_RETURN(YPXFR_REFUSED);
34312891Swpaul	}
34412891Swpaul
34512891Swpaul	if (argp->map_parms.domain == NULL) {
34615426Swpaul		YPXFR_RETURN(YPXFR_BADARGS);
34712891Swpaul	}
34812891Swpaul
34912891Swpaul	if (yp_validdomain(argp->map_parms.domain)) {
35015426Swpaul		YPXFR_RETURN(YPXFR_NODOM);
35112891Swpaul	}
35212891Swpaul
35312891Swpaul	switch(fork()) {
35412891Swpaul	case 0:
35512891Swpaul	{
35612891Swpaul		char g[11], t[11], p[11];
35712891Swpaul		char ypxfr_command[MAXPATHLEN + 2];
35812891Swpaul
35912891Swpaul		sprintf (ypxfr_command, "%sypxfr", _PATH_LIBEXEC);
36012891Swpaul		sprintf (t, "%u", argp->transid);
36112891Swpaul		sprintf (g, "%u", argp->prog);
36212891Swpaul		sprintf (p, "%u", argp->port);
36312997Swpaul		if (debug)
36412997Swpaul			close(0); close(1); close(2);
36512997Swpaul		if (strcmp(yp_dir, _PATH_YP)) {
36614304Swpaul			execl(ypxfr_command, "ypxfr",
36714304Swpaul			"-d", argp->map_parms.domain,
36814304Swpaul		      	"-h", argp->map_parms.peer,
36914304Swpaul			"-p", yp_dir, "-C", t,
37014304Swpaul		      	g, inet_ntoa(rqhost->sin_addr),
37114304Swpaul			p, argp->map_parms.map,
37212997Swpaul		      	NULL);
37312997Swpaul		} else {
37414304Swpaul			execl(ypxfr_command, "ypxfr",
37514304Swpaul			"-d", argp->map_parms.domain,
37614304Swpaul		      	"-h", argp->map_parms.peer,
37714304Swpaul			"-C", t,
37814304Swpaul		      	g, inet_ntoa(rqhost->sin_addr),
37914304Swpaul			p, argp->map_parms.map,
38012997Swpaul		      	NULL);
38112997Swpaul		}
38212997Swpaul		forked++;
38315426Swpaul		yp_error("ypxfr execl(%s): %s", ypxfr_command, strerror(errno));
38415426Swpaul		YPXFR_RETURN(YPXFR_XFRERR);
38512997Swpaul		break;
38612891Swpaul	}
38712891Swpaul	case -1:
38812891Swpaul		yp_error("ypxfr fork(): %s", strerror(errno));
38915426Swpaul		YPXFR_RETURN(YPXFR_XFRERR);
39012891Swpaul		break;
39112891Swpaul	default:
39213375Swpaul		result.xfrstat = YPXFR_SUCC;
39312997Swpaul		children++;
39412997Swpaul		forked = 0;
39512891Swpaul		break;
39612891Swpaul	}
39713375Swpaul
39813375Swpaul	return (&result);
39912891Swpaul}
40015426Swpaul#undef YPXFR_RETURN
40112891Swpaul
40212891Swpaulvoid *
40312891Swpaulypproc_clear_2_svc(void *argp, struct svc_req *rqstp)
40412891Swpaul{
40512891Swpaul	static char * result;
40612891Swpaul	static char rval = 0;
40712891Swpaul
40812891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
40912891Swpaul		return (NULL);
41015426Swpaul#ifdef DB_CACHE
41115426Swpaul	/* clear out the database cache */
41215426Swpaul	yp_flush_all();
41315426Swpaul#endif
41414240Swpaul	/* Re-read the securenets database for the hell of it. */
41514240Swpaul	load_securenets();
41614240Swpaul
41712891Swpaul	result = &rval;
41812891Swpaul	return((void *) &result);
41912891Swpaul}
42012891Swpaul
42112891Swpaul/*
42212891Swpaul * For ypproc_all, we have to send a stream of ypresp_all structures
42312891Swpaul * via TCP, but the XDR filter generated from the yp.x protocol
42412891Swpaul * definition file only serializes one such structure. This means that
42512891Swpaul * to send the whole stream, you need a wrapper which feeds all the
42612891Swpaul * records into the underlying XDR routine until it hits an 'EOF.'
42712891Swpaul * But to use the wrapper, you have to violate the boundaries between
42812891Swpaul * RPC layers by calling svc_sendreply() directly from the ypproc_all
42912891Swpaul * service routine instead of letting the RPC dispatcher do it.
43012891Swpaul *
43112891Swpaul * Bleah.
43212891Swpaul */
43312891Swpaul
43412891Swpaul/*
43512891Swpaul * Custom XDR routine for serialzing results of ypproc_all: keep
43612891Swpaul * reading from the database and spew until we run out of records
43712891Swpaul * or encounter an error.
43812891Swpaul */
43912891Swpaulstatic bool_t
44012891Swpaulxdr_my_ypresp_all(register XDR *xdrs, ypresp_all *objp)
44112891Swpaul{
44215426Swpaul	DBT key = { NULL, 0 } , data = { NULL, 0 };
44312891Swpaul
44412891Swpaul	while (1) {
44512891Swpaul		/* Get a record. */
44612891Swpaul		if ((objp->ypresp_all_u.val.stat =
44715426Swpaul	    		yp_next_record(spec_dbp,&key,&data,1,0)) == YP_TRUE) {
44812891Swpaul			objp->ypresp_all_u.val.val.valdat_len = data.size;
44912891Swpaul			objp->ypresp_all_u.val.val.valdat_val = data.data;
45012891Swpaul			objp->ypresp_all_u.val.key.keydat_len = key.size;
45112891Swpaul			objp->ypresp_all_u.val.key.keydat_val = key.data;
45212891Swpaul			objp->more = TRUE;
45312891Swpaul		} else {
45412891Swpaul			objp->more = FALSE;
45512891Swpaul		}
45612891Swpaul
45712891Swpaul		/* Serialize. */
45812891Swpaul		if (!xdr_ypresp_all(xdrs, objp))
45912891Swpaul			return(FALSE);
46012891Swpaul		if (objp->more == FALSE)
46112891Swpaul			return(TRUE);
46212891Swpaul	}
46312891Swpaul}
46412891Swpaul
46512891Swpaulypresp_all *
46612891Swpaulypproc_all_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
46712891Swpaul{
46812891Swpaul	static ypresp_all  result;
46912891Swpaul
47012891Swpaul	/*
47112891Swpaul	 * Set this here so that the client will be forced to make
47212891Swpaul	 * at least one attempt to read from us even if all we're
47312891Swpaul	 * doing is returning an error.
47412891Swpaul	 */
47512891Swpaul	result.more = TRUE;
47614304Swpaul	result.ypresp_all_u.val.key.keydat_len = 0;
47714304Swpaul	result.ypresp_all_u.val.key.keydat_val = "";
47812891Swpaul
47912891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
48012891Swpaul		result.ypresp_all_u.val.stat = YP_YPERR;
48112891Swpaul		return (&result);
48212891Swpaul	}
48312891Swpaul
48412891Swpaul	if (argp->domain == NULL || argp->map == NULL) {
48512891Swpaul		result.ypresp_all_u.val.stat = YP_BADARGS;
48612891Swpaul		return (&result);
48712891Swpaul	}
48812891Swpaul
48912891Swpaul	/*
49012891Swpaul	 * The ypproc_all procedure can take a while to complete.
49112891Swpaul	 * Best to handle it in a subprocess so the parent doesn't
49215426Swpaul	 * block. (Is there a better way to do this? Maybe with
49315426Swpaul	 * async socket I/O?)
49412891Swpaul	 */
49512891Swpaul	if (!debug && children < MAX_CHILDREN && fork()) {
49612891Swpaul		children++;
49712891Swpaul		forked = 0;
49812891Swpaul		return (NULL);
49912891Swpaul	} else {
50012891Swpaul		forked++;
50112891Swpaul	}
50212891Swpaul
50315426Swpaul#ifndef DB_CACHE
50412891Swpaul	if ((spec_dbp = yp_open_db(argp->domain, argp->map)) == NULL) {
50512891Swpaul		result.ypresp_all_u.val.stat = yp_errno;
50612891Swpaul		return(&result);
50712891Swpaul	}
50815426Swpaul#else
50915426Swpaul	if ((spec_dbp = yp_open_db_cache(argp->domain, argp->map, NULL, 0)) == NULL) {
51015426Swpaul		result.ypresp_all_u.val.stat = yp_errno;
51115426Swpaul		return(&result);
51215426Swpaul	}
51315426Swpaul#endif
51412891Swpaul
51512891Swpaul	/* Kick off the actual data transfer. */
51612891Swpaul	svc_sendreply(rqstp->rq_xprt, xdr_my_ypresp_all, (char *)&result);
51712891Swpaul
51815426Swpaul#ifndef DB_CACHE
51912891Swpaul	(void)(spec_dbp->close)(spec_dbp);
52015426Swpaul#endif
52112891Swpaul	/*
52212891Swpaul	 * Returning NULL prevents the dispatcher from calling
52312891Swpaul	 * svc_sendreply() since we already did it.
52412891Swpaul	 */
52512891Swpaul	return (NULL);
52612891Swpaul}
52712891Swpaul
52812891Swpaulypresp_master *
52912891Swpaulypproc_master_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
53012891Swpaul{
53112891Swpaul	static ypresp_master  result;
53215426Swpaul	static char ypvalbuf[YPMAXRECORD];
53315426Swpaul	DBT key, data;
53412891Swpaul
53514303Swpaul	result.peer = "";
53614303Swpaul
53712891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp)) {
53812891Swpaul		result.stat = YP_YPERR;
53912891Swpaul		return(&result);
54012891Swpaul	}
54112891Swpaul
54212891Swpaul	if (argp->domain == NULL) {
54312891Swpaul		result.stat = YP_BADARGS;
54412891Swpaul		return (&result);
54512891Swpaul	}
54612891Swpaul
54715426Swpaul	key.data = master_string;
54815426Swpaul	key.size = strlen(master_string);
54912891Swpaul
55015426Swpaul	/*
55115426Swpaul	 * Note that we copy the data retrieved from the database to
55215426Swpaul	 * a private buffer and NUL terminate the buffer rather than
55315426Swpaul	 * terminating the data in place. We do this because by stuffing
55415426Swpaul	 * a '\0' into data.data, we will actually be corrupting memory
55515426Swpaul	 * allocated by the DB package. This is a bad thing now that we
55615426Swpaul	 * cache DB handles rather than closing the database immediately.
55715426Swpaul	 */
55815426Swpaul	if ((result.stat = yp_get_record(argp->domain, argp->map,
55915426Swpaul						&key, &data, 1)) == YP_TRUE) {
56015426Swpaul		bcopy((char *)data.data, (char *)&ypvalbuf, data.size);
56115426Swpaul		ypvalbuf[data.size] = '\0';
56215426Swpaul		result.peer = (char *)&ypvalbuf;
56312891Swpaul	} else
56412891Swpaul		result.peer = "";
56512891Swpaul
56612891Swpaul	return (&result);
56712891Swpaul}
56812891Swpaul
56912891Swpaulypresp_order *
57012891Swpaulypproc_order_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
57112891Swpaul{
57212891Swpaul	static ypresp_order  result;
57312891Swpaul	DBT key,data;
57412891Swpaul
57514304Swpaul	result.ordernum = 0;
57614304Swpaul
57712891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp)) {
57812891Swpaul		result.stat = YP_YPERR;
57912891Swpaul		return(&result);
58012891Swpaul	}
58112891Swpaul
58212891Swpaul	if (argp->domain == NULL) {
58312891Swpaul		result.stat = YP_BADARGS;
58412891Swpaul		return (&result);
58512891Swpaul	}
58612891Swpaul
58712891Swpaul	/*
58812891Swpaul	 * We could just check the timestamp on the map file,
58912891Swpaul	 * but that's a hack: we'll only know the last time the file
59012891Swpaul	 * was touched, not the last time the database contents were
59112891Swpaul	 * updated.
59212891Swpaul	 */
59312891Swpaul
59415426Swpaul	key.data = order_string;
59515426Swpaul	key.size = strlen(order_string);
59612891Swpaul
59715426Swpaul	if ((result.stat = yp_get_record(argp->domain, argp->map,
59815426Swpaul						&key, &data, 1)) == YP_TRUE)
59912891Swpaul		result.ordernum = atoi((char *)data.data);
60012891Swpaul	else
60112891Swpaul		result.ordernum = 0;
60212891Swpaul
60315426Swpaul
60412891Swpaul	return (&result);
60512891Swpaul}
60612891Swpaul
60712891Swpaulstatic void yp_maplist_free(yp_maplist)
60812891Swpaul	struct ypmaplist *yp_maplist;
60912891Swpaul{
61012891Swpaul	register struct ypmaplist *next;
61112891Swpaul
61212891Swpaul	while(yp_maplist) {
61312891Swpaul		next = yp_maplist->next;
61412891Swpaul		free(yp_maplist->map);
61512891Swpaul		free(yp_maplist);
61612891Swpaul		yp_maplist = next;
61712891Swpaul	}
61812891Swpaul	return;
61912891Swpaul}
62012891Swpaul
62112891Swpaulstatic struct ypmaplist *yp_maplist_create(domain)
62212891Swpaul	const char *domain;
62312891Swpaul{
62412891Swpaul	char yp_mapdir[MAXPATHLEN + 2];
62512891Swpaul	char yp_mapname[MAXPATHLEN + 2];
62612891Swpaul	struct ypmaplist *cur = NULL;
62712891Swpaul	struct ypmaplist *yp_maplist = NULL;
62812891Swpaul	DIR *dird;
62912891Swpaul	struct dirent *dirp;
63012891Swpaul	struct stat statbuf;
63112891Swpaul
63212891Swpaul	snprintf(yp_mapdir, sizeof(yp_mapdir), "%s/%s", yp_dir, domain);
63312891Swpaul
63412891Swpaul	if ((dird = opendir(yp_mapdir)) == NULL) {
63513800Swpaul		yp_error("opendir(%s) failed: %s", yp_mapdir, strerror(errno));
63612891Swpaul		return(NULL);
63712891Swpaul	}
63812891Swpaul
63912891Swpaul	while ((dirp = readdir(dird)) != NULL) {
64012891Swpaul		if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
64114304Swpaul			snprintf(yp_mapname, sizeof(yp_mapname), "%s/%s",
64214304Swpaul							yp_mapdir,dirp->d_name);
64314304Swpaul			if (stat(yp_mapname, &statbuf) < 0 ||
64414304Swpaul						!S_ISREG(statbuf.st_mode))
64512891Swpaul				continue;
64614304Swpaul			if ((cur = (struct ypmaplist *)
64716044Swpaul				malloc(sizeof(struct ypmaplist))) == NULL) {
64814304Swpaul				yp_error("malloc() failed: %s",strerror(errno));
64912891Swpaul				closedir(dird);
65012891Swpaul				yp_maplist_free(yp_maplist);
65112891Swpaul				return(NULL);
65212891Swpaul			}
65312891Swpaul			if ((cur->map = (char *)strdup(dirp->d_name)) == NULL) {
65414304Swpaul				yp_error("strdup() failed: %s",strerror(errno));
65512891Swpaul				closedir(dird);
65612891Swpaul				yp_maplist_free(yp_maplist);
65712891Swpaul				return(NULL);
65812891Swpaul			}
65912891Swpaul			cur->next = yp_maplist;
66012891Swpaul			yp_maplist = cur;
66112891Swpaul			if (debug)
66212891Swpaul				yp_error("map: %s", yp_maplist->map);
66312891Swpaul		}
66412891Swpaul
66512891Swpaul	}
66612891Swpaul	closedir(dird);
66712891Swpaul	return(yp_maplist);
66812891Swpaul}
66912891Swpaul
67012891Swpaulypresp_maplist *
67112891Swpaulypproc_maplist_2_svc(domainname *argp, struct svc_req *rqstp)
67212891Swpaul{
67315426Swpaul	static ypresp_maplist  result = { 0, NULL };
67412891Swpaul
67512891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp)) {
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