yp_server.c revision 14304
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
4814304Swpaulstatic char rcsid[] = "$Id: yp_server.c,v 1.5 1996/03/01 03:28:31 wpaul Exp $";
4912891Swpaul#endif /* not lint */
5012891Swpaul
5112891Swpaulint forked = 0;
5212891Swpaulint children = 0;
5312891SwpaulDB *spec_dbp = NULL;	/* Special global DB handle for ypproc_all. */
5412891Swpaul
5514262Swpaul/*
5614262Swpaul * NIS v2 support. This is where most of the action happens.
5714262Swpaul */
5814262Swpaul
5912891Swpaulvoid *
6012891Swpaulypproc_null_2_svc(void *argp, struct svc_req *rqstp)
6112891Swpaul{
6212891Swpaul	static char * result;
6312891Swpaul	static char rval = 0;
6412891Swpaul
6512891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
6612891Swpaul		return(NULL);
6712891Swpaul
6812891Swpaul	result = &rval;
6912891Swpaul
7012891Swpaul	return((void *) &result);
7112891Swpaul}
7212891Swpaul
7312891Swpaulbool_t *
7412891Swpaulypproc_domain_2_svc(domainname *argp, struct svc_req *rqstp)
7512891Swpaul{
7612891Swpaul	static bool_t  result;
7712891Swpaul
7812891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp)) {
7912891Swpaul		result = FALSE;
8012891Swpaul		return (&result);
8112891Swpaul	}
8212891Swpaul
8312891Swpaul	if (argp == NULL || yp_validdomain(*argp))
8412891Swpaul		result = FALSE;
8512891Swpaul	else
8612891Swpaul		result = TRUE;
8712891Swpaul
8812891Swpaul	return (&result);
8912891Swpaul}
9012891Swpaul
9112891Swpaulbool_t *
9212891Swpaulypproc_domain_nonack_2_svc(domainname *argp, struct svc_req *rqstp)
9312891Swpaul{
9412891Swpaul	static bool_t  result;
9512891Swpaul
9612891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
9712891Swpaul		return (NULL);
9812891Swpaul
9912891Swpaul	if (argp == NULL || yp_validdomain(*argp))
10012891Swpaul		return (NULL);
10112891Swpaul	else
10212891Swpaul		result = TRUE;
10312891Swpaul
10412891Swpaul	return (&result);
10512891Swpaul}
10612891Swpaul
10712891Swpaulypresp_val *
10812891Swpaulypproc_match_2_svc(ypreq_key *argp, struct svc_req *rqstp)
10912891Swpaul{
11012891Swpaul	static ypresp_val  result;
11112891Swpaul	DBT key, data;
11212891Swpaul
11314304Swpaul	result.val.valdat_val = "";
11414304Swpaul	result.val.valdat_len = 0;
11514304Swpaul
11612891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
11712891Swpaul		result.stat = YP_YPERR;
11812891Swpaul		return (&result);
11912891Swpaul	}
12012891Swpaul
12112891Swpaul	if (argp->domain == NULL || argp->map == NULL) {
12212891Swpaul		result.stat = YP_BADARGS;
12312891Swpaul		return (&result);
12412891Swpaul	}
12512891Swpaul
12612891Swpaul	if (yp_validdomain(argp->domain)) {
12712891Swpaul		result.stat = YP_NODOM;
12812891Swpaul		return(&result);
12912891Swpaul	}
13012891Swpaul
13112891Swpaul	key.size = argp->key.keydat_len;
13212891Swpaul	key.data = argp->key.keydat_val;
13312891Swpaul
13412891Swpaul	result.stat = yp_get_record(argp->domain, argp->map, &key, &data, 0);
13512891Swpaul
13612891Swpaul	if (result.stat == YP_TRUE) {
13712891Swpaul		result.val.valdat_len = data.size;
13812891Swpaul		result.val.valdat_val = data.data;
13912891Swpaul	}
14012891Swpaul
14112891Swpaul	/*
14212891Swpaul	 * Do DNS lookups for hosts maps if database lookup failed.
14312891Swpaul	 */
14412891Swpaul
14512891Swpaul	if (do_dns && result.stat != YP_TRUE && strstr(argp->map, "hosts")) {
14612997Swpaul		char *rval = NULL;
14712891Swpaul
14812891Swpaul	/* DNS lookups can take time -- do them in a subprocess */
14912891Swpaul
15012891Swpaul		if (!debug && children < MAX_CHILDREN && fork()) {
15112891Swpaul			children++;
15212891Swpaul			forked = 0;
15312891Swpaul			/*
15412891Swpaul			 * Returning NULL here prevents svc_sendreply()
15512891Swpaul			 * from being called by the parent. This is vital
15612891Swpaul			 * since having both the parent and the child process
15712891Swpaul			 * call it would confuse the client.
15812891Swpaul			 */
15912891Swpaul			return (NULL);
16012891Swpaul		} else {
16112891Swpaul			forked++;
16212891Swpaul		}
16312891Swpaul
16412891Swpaul		if (debug)
16512891Swpaul			yp_error("Doing DNS lookup of %.*s",
16612891Swpaul			 	  argp->key.keydat_len,
16712891Swpaul				  argp->key.keydat_val);
16812891Swpaul
16912891Swpaul		/* NUL terminate! NUL terminate!! NUL TERMINATE!!! */
17012891Swpaul		argp->key.keydat_val[argp->key.keydat_len] = '\0';
17112891Swpaul
17212891Swpaul		if (!strcmp(argp->map, "hosts.byname"))
17312891Swpaul			rval = yp_dnsname((char *)argp->key.keydat_val);
17412891Swpaul		else if (!strcmp(argp->map, "hosts.byaddr"))
17512891Swpaul			rval = yp_dnsaddr((const char *)argp->key.keydat_val);
17612891Swpaul
17712891Swpaul
17812891Swpaul		if (rval) {
17912891Swpaul			if (debug)
18014304Swpaul				yp_error("DNS lookup successful. Result: %s",
18114304Swpaul									rval);
18212891Swpaul			result.val.valdat_len = strlen(rval);
18312891Swpaul			result.val.valdat_val = rval;
18412891Swpaul			result.stat = YP_TRUE;
18512891Swpaul		} else {
18612891Swpaul			if (debug)
18712891Swpaul				yp_error("DNS lookup failed.");
18812891Swpaul			result.stat = YP_NOKEY;
18912891Swpaul		}
19012891Swpaul	}
19112891Swpaul
19212891Swpaul	return (&result);
19312891Swpaul}
19412891Swpaul
19512891Swpaulypresp_key_val *
19612891Swpaulypproc_first_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
19712891Swpaul{
19812891Swpaul	static ypresp_key_val  result;
19912891Swpaul	DBT key, data;
20012891Swpaul	DB *dbp;
20112891Swpaul
20214304Swpaul	result.val.valdat_val = result.key.keydat_val = "";
20314304Swpaul	result.val.valdat_len = result.key.keydat_len = 0;
20414304Swpaul
20512891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
20612891Swpaul		result.stat = YP_YPERR;
20712891Swpaul		return (&result);
20812891Swpaul	}
20912891Swpaul
21012891Swpaul	if (argp->domain == NULL) {
21112891Swpaul		result.stat = YP_BADARGS;
21212891Swpaul		return (&result);
21312891Swpaul	}
21412891Swpaul
21512891Swpaul	if (yp_validdomain(argp->domain)) {
21612891Swpaul		result.stat = YP_NODOM;
21712891Swpaul		return(&result);
21812891Swpaul	}
21912891Swpaul
22012891Swpaul	if ((dbp = yp_open_db(argp->domain, argp->map)) == NULL) {
22112891Swpaul		result.stat = yp_errno;
22212891Swpaul		return(&result);
22312891Swpaul	}
22412891Swpaul
22512891Swpaul	key.data = NULL;
22612891Swpaul	key.size = 0;
22712891Swpaul	result.stat = yp_first_record(dbp, &key, &data);
22812891Swpaul	(void)(dbp->close)(dbp);
22912891Swpaul
23012891Swpaul	if (result.stat == YP_TRUE) {
23112891Swpaul		result.key.keydat_len = key.size;
23212891Swpaul		result.key.keydat_val = key.data;
23312891Swpaul		result.val.valdat_len = data.size;
23412891Swpaul		result.val.valdat_val = data.data;
23512891Swpaul	}
23612891Swpaul
23712891Swpaul	return (&result);
23812891Swpaul}
23912891Swpaul
24012891Swpaulypresp_key_val *
24112891Swpaulypproc_next_2_svc(ypreq_key *argp, struct svc_req *rqstp)
24212891Swpaul{
24312891Swpaul	static ypresp_key_val  result;
24412891Swpaul	DBT key, data;
24512891Swpaul	DB *dbp;
24612891Swpaul
24714304Swpaul	result.val.valdat_val = result.key.keydat_val = "";
24814304Swpaul	result.val.valdat_len = result.key.keydat_len = 0;
24914304Swpaul
25012891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
25112891Swpaul		result.stat = YP_YPERR;
25212891Swpaul		return (&result);
25312891Swpaul	}
25412891Swpaul
25512891Swpaul	if (argp->domain == NULL || argp->map == NULL) {
25612891Swpaul		result.stat = YP_BADARGS;
25712891Swpaul		return (&result);
25812891Swpaul	}
25912891Swpaul
26012891Swpaul	if (yp_validdomain(argp->domain)) {
26112891Swpaul		result.stat = YP_NODOM;
26212891Swpaul		return(&result);
26312891Swpaul	}
26412891Swpaul
26512891Swpaul	if ((dbp = yp_open_db(argp->domain, argp->map)) == NULL) {
26612891Swpaul		result.stat = yp_errno;
26712891Swpaul		return(&result);
26812891Swpaul	}
26912891Swpaul
27012891Swpaul	key.size = argp->key.keydat_len;
27112891Swpaul	key.data = argp->key.keydat_val;
27212891Swpaul
27312891Swpaul	result.stat = yp_next_record(dbp, &key, &data, 0);
27412891Swpaul	(void)(dbp->close)(dbp);
27512891Swpaul
27612891Swpaul	if (result.stat == YP_TRUE) {
27712891Swpaul		result.key.keydat_len = key.size;
27812891Swpaul		result.key.keydat_val = key.data;
27912891Swpaul		result.val.valdat_len = data.size;
28012891Swpaul		result.val.valdat_val = data.data;
28112891Swpaul	}
28212891Swpaul
28312891Swpaul	return (&result);
28412891Swpaul}
28512891Swpaul
28612997Swpaulstatic void ypxfr_callback(rval,addr,transid,prognum,port)
28712997Swpaul	ypxfrstat rval;
28812997Swpaul	struct sockaddr_in *addr;
28912997Swpaul	unsigned int transid;
29012997Swpaul	unsigned int prognum;
29112997Swpaul	unsigned long port;
29212997Swpaul{
29312997Swpaul	CLIENT *clnt;
29412997Swpaul	int sock = RPC_ANYSOCK;
29512997Swpaul	struct timeval timeout;
29612997Swpaul	yppushresp_xfr ypxfr_resp;
29713375Swpaul	struct rpc_err err;
29812997Swpaul
29913375Swpaul	timeout.tv_sec = 5;
30012997Swpaul	timeout.tv_usec = 0;
30112997Swpaul	addr->sin_port = htons(port);
30212997Swpaul
30312997Swpaul	if ((clnt = clntudp_create(addr, prognum, 1, timeout, &sock)) == NULL)
30412997Swpaul		yp_error("%s", clnt_spcreateerror("failed to establish \
30512997Swpaulcallback handle"));
30612997Swpaul
30712997Swpaul	ypxfr_resp.status = rval;
30812997Swpaul	ypxfr_resp.transid = transid;
30912997Swpaul
31013375Swpaul	/* Turn the timeout off -- we don't want to block. */
31113375Swpaul	timeout.tv_sec = 0;
31213375Swpaul	if (clnt_control(clnt, CLSET_TIMEOUT, (char *)&timeout) == FALSE)
31313375Swpaul		yp_error("failed to set timeout on ypproc_xfr callback");
31412997Swpaul
31513375Swpaul	if (yppushproc_xfrresp_1(&ypxfr_resp, clnt) == NULL) {
31613375Swpaul		clnt_geterr(clnt, &err);
31713375Swpaul		if (err.re_status != RPC_SUCCESS &&
31813375Swpaul		    err.re_status != RPC_TIMEDOUT)
31913375Swpaul			yp_error("%s", clnt_sperror(clnt,
32013375Swpaul				"ypxfr callback failed"));
32113375Swpaul	}
32213375Swpaul
32312997Swpaul	clnt_destroy(clnt);
32412997Swpaul	return;
32512997Swpaul}
32612997Swpaul
32712891Swpaulypresp_xfr *
32812891Swpaulypproc_xfr_2_svc(ypreq_xfr *argp, struct svc_req *rqstp)
32912891Swpaul{
33012891Swpaul	static ypresp_xfr  result;
33112997Swpaul	struct sockaddr_in *rqhost;
33212891Swpaul
33313375Swpaul	result.transid = argp->transid;
33413375Swpaul	rqhost = svc_getcaller(rqstp->rq_xprt);
33513375Swpaul
33612891Swpaul	if (yp_access(argp->map_parms.map, (struct svc_req *)rqstp)) {
33713375Swpaul		/* Order is important: send regular RPC reply, then callback */
33812891Swpaul		result.xfrstat = YPXFR_REFUSED;
33913375Swpaul		svc_sendreply(rqstp->rq_xprt, xdr_ypresp_xfr, (char *)&result);
34013375Swpaul		ypxfr_callback(YPXFR_REFUSED,rqhost,argp->transid,
34113375Swpaul			       argp->prog,argp->port);
34213375Swpaul		return(NULL);
34312891Swpaul	}
34412891Swpaul
34512891Swpaul	if (argp->map_parms.domain == NULL) {
34612891Swpaul		result.xfrstat = YPXFR_BADARGS;
34713375Swpaul		svc_sendreply(rqstp->rq_xprt, xdr_ypresp_xfr, (char *)&result);
34813375Swpaul		ypxfr_callback(YPXFR_BADARGS,rqhost,argp->transid,
34913375Swpaul			       argp->prog,argp->port);
35013375Swpaul		return(NULL);
35112891Swpaul	}
35212891Swpaul
35312891Swpaul	if (yp_validdomain(argp->map_parms.domain)) {
35412891Swpaul		result.xfrstat = YPXFR_NODOM;
35513375Swpaul		svc_sendreply(rqstp->rq_xprt, xdr_ypresp_xfr, (char *)&result);
35613375Swpaul		ypxfr_callback(YPXFR_NODOM,rqhost,argp->transid,
35713375Swpaul			       argp->prog,argp->port);
35813375Swpaul		return(NULL);
35912891Swpaul	}
36012891Swpaul
36112891Swpaul	switch(fork()) {
36212891Swpaul	case 0:
36312891Swpaul	{
36412891Swpaul		char g[11], t[11], p[11];
36512891Swpaul		char ypxfr_command[MAXPATHLEN + 2];
36612891Swpaul
36712891Swpaul		sprintf (ypxfr_command, "%sypxfr", _PATH_LIBEXEC);
36812891Swpaul		sprintf (t, "%u", argp->transid);
36912891Swpaul		sprintf (g, "%u", argp->prog);
37012891Swpaul		sprintf (p, "%u", argp->port);
37112997Swpaul		if (debug)
37212997Swpaul			close(0); close(1); close(2);
37312997Swpaul		if (strcmp(yp_dir, _PATH_YP)) {
37414304Swpaul			execl(ypxfr_command, "ypxfr",
37514304Swpaul			"-d", argp->map_parms.domain,
37614304Swpaul		      	"-h", argp->map_parms.peer,
37714304Swpaul			"-p", yp_dir, "-C", t,
37814304Swpaul		      	g, inet_ntoa(rqhost->sin_addr),
37914304Swpaul			p, argp->map_parms.map,
38012997Swpaul		      	NULL);
38112997Swpaul		} else {
38214304Swpaul			execl(ypxfr_command, "ypxfr",
38314304Swpaul			"-d", argp->map_parms.domain,
38414304Swpaul		      	"-h", argp->map_parms.peer,
38514304Swpaul			"-C", t,
38614304Swpaul		      	g, inet_ntoa(rqhost->sin_addr),
38714304Swpaul			p, argp->map_parms.map,
38812997Swpaul		      	NULL);
38912997Swpaul		}
39012997Swpaul		forked++;
39113375Swpaul		result.xfrstat = YPXFR_XFRERR;
39212891Swpaul		yp_error("ypxfr execl(): %s", strerror(errno));
39313375Swpaul		svc_sendreply(rqstp->rq_xprt, xdr_ypresp_xfr, (char *)&result);
39412997Swpaul		ypxfr_callback(YPXFR_XFRERR,rqhost,argp->transid,
39512997Swpaul			       argp->prog,argp->port);
39613375Swpaul		return(NULL);
39712997Swpaul		break;
39812891Swpaul	}
39912891Swpaul	case -1:
40012891Swpaul		yp_error("ypxfr fork(): %s", strerror(errno));
40113375Swpaul		result.xfrstat = YPXFR_XFRERR;
40213375Swpaul		svc_sendreply(rqstp->rq_xprt, xdr_ypresp_xfr, (char *)&result);
40312997Swpaul		ypxfr_callback(YPXFR_XFRERR,rqhost,argp->transid,
40412997Swpaul			       argp->prog,argp->port);
40513375Swpaul		return(NULL);
40612891Swpaul		break;
40712891Swpaul	default:
40813375Swpaul		result.xfrstat = YPXFR_SUCC;
40912997Swpaul		children++;
41012997Swpaul		forked = 0;
41112891Swpaul		break;
41212891Swpaul	}
41313375Swpaul
41413375Swpaul	return (&result);
41512891Swpaul}
41612891Swpaul
41712891Swpaulvoid *
41812891Swpaulypproc_clear_2_svc(void *argp, struct svc_req *rqstp)
41912891Swpaul{
42012891Swpaul	static char * result;
42112891Swpaul	static char rval = 0;
42212891Swpaul
42312891Swpaul	/*
42412891Swpaul	 * We don't have to do anything for ypproc_clear. Unlike
42514304Swpaul	 * the SunOS ypserv, we don't hold our database descriptors
42612891Swpaul	 * open forever.
42712891Swpaul	 */
42812891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
42912891Swpaul		return (NULL);
43012891Swpaul
43114240Swpaul	/* Re-read the securenets database for the hell of it. */
43214240Swpaul	load_securenets();
43314240Swpaul
43412891Swpaul	result = &rval;
43512891Swpaul	return((void *) &result);
43612891Swpaul}
43712891Swpaul
43812891Swpaul/*
43912891Swpaul * For ypproc_all, we have to send a stream of ypresp_all structures
44012891Swpaul * via TCP, but the XDR filter generated from the yp.x protocol
44112891Swpaul * definition file only serializes one such structure. This means that
44212891Swpaul * to send the whole stream, you need a wrapper which feeds all the
44312891Swpaul * records into the underlying XDR routine until it hits an 'EOF.'
44412891Swpaul * But to use the wrapper, you have to violate the boundaries between
44512891Swpaul * RPC layers by calling svc_sendreply() directly from the ypproc_all
44612891Swpaul * service routine instead of letting the RPC dispatcher do it.
44712891Swpaul *
44812891Swpaul * Bleah.
44912891Swpaul */
45012891Swpaul
45112891Swpaul/*
45212891Swpaul * Custom XDR routine for serialzing results of ypproc_all: keep
45312891Swpaul * reading from the database and spew until we run out of records
45412891Swpaul * or encounter an error.
45512891Swpaul */
45612891Swpaulstatic bool_t
45712891Swpaulxdr_my_ypresp_all(register XDR *xdrs, ypresp_all *objp)
45812891Swpaul{
45912891Swpaul	DBT key, data;
46012891Swpaul
46112891Swpaul	while (1) {
46212891Swpaul		/* Get a record. */
46312891Swpaul		key.size = objp->ypresp_all_u.val.key.keydat_len;
46412891Swpaul		key.data = objp->ypresp_all_u.val.key.keydat_val;
46512891Swpaul
46612891Swpaul		if ((objp->ypresp_all_u.val.stat =
46712891Swpaul		    yp_next_record(spec_dbp,&key,&data,1)) == YP_TRUE) {
46812891Swpaul			objp->ypresp_all_u.val.val.valdat_len = data.size;
46912891Swpaul			objp->ypresp_all_u.val.val.valdat_val = data.data;
47012891Swpaul			objp->ypresp_all_u.val.key.keydat_len = key.size;
47112891Swpaul			objp->ypresp_all_u.val.key.keydat_val = key.data;
47212891Swpaul			objp->more = TRUE;
47312891Swpaul		} else {
47412891Swpaul			objp->more = FALSE;
47512891Swpaul		}
47612891Swpaul
47712891Swpaul		/* Serialize. */
47812891Swpaul		if (!xdr_ypresp_all(xdrs, objp))
47912891Swpaul			return(FALSE);
48012891Swpaul		if (objp->more == FALSE)
48112891Swpaul			return(TRUE);
48212891Swpaul	}
48312891Swpaul}
48412891Swpaul
48512891Swpaulypresp_all *
48612891Swpaulypproc_all_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
48712891Swpaul{
48812891Swpaul	static ypresp_all  result;
48912891Swpaul
49012891Swpaul	/*
49112891Swpaul	 * Set this here so that the client will be forced to make
49212891Swpaul	 * at least one attempt to read from us even if all we're
49312891Swpaul	 * doing is returning an error.
49412891Swpaul	 */
49512891Swpaul	result.more = TRUE;
49614304Swpaul	result.ypresp_all_u.val.key.keydat_len = 0;
49714304Swpaul	result.ypresp_all_u.val.key.keydat_val = "";
49812891Swpaul
49912891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
50012891Swpaul		result.ypresp_all_u.val.stat = YP_YPERR;
50112891Swpaul		return (&result);
50212891Swpaul	}
50312891Swpaul
50412891Swpaul	if (argp->domain == NULL || argp->map == NULL) {
50512891Swpaul		result.ypresp_all_u.val.stat = YP_BADARGS;
50612891Swpaul		return (&result);
50712891Swpaul	}
50812891Swpaul
50912891Swpaul	if (yp_validdomain(argp->domain)) {
51012891Swpaul		result.ypresp_all_u.val.stat = YP_NODOM;
51112891Swpaul		return(&result);
51212891Swpaul	}
51312891Swpaul
51412891Swpaul	/*
51512891Swpaul	 * The ypproc_all procedure can take a while to complete.
51612891Swpaul	 * Best to handle it in a subprocess so the parent doesn't
51712891Swpaul	 * block. We fork() here so we don't end up sharing a
51812891Swpaul	 * DB file handle with the parent.
51912891Swpaul	 */
52012891Swpaul
52112891Swpaul	if (!debug && children < MAX_CHILDREN && fork()) {
52212891Swpaul		children++;
52312891Swpaul		forked = 0;
52412891Swpaul		return (NULL);
52512891Swpaul	} else {
52612891Swpaul		forked++;
52712891Swpaul	}
52812891Swpaul
52912891Swpaul	if ((spec_dbp = yp_open_db(argp->domain, argp->map)) == NULL) {
53012891Swpaul		result.ypresp_all_u.val.stat = yp_errno;
53112891Swpaul		return(&result);
53212891Swpaul	}
53312891Swpaul
53412891Swpaul	/* Kick off the actual data transfer. */
53512891Swpaul	svc_sendreply(rqstp->rq_xprt, xdr_my_ypresp_all, (char *)&result);
53612891Swpaul
53712891Swpaul	/* Close database when done. */
53812891Swpaul	(void)(spec_dbp->close)(spec_dbp);
53912891Swpaul
54012891Swpaul	/*
54112891Swpaul	 * Returning NULL prevents the dispatcher from calling
54212891Swpaul	 * svc_sendreply() since we already did it.
54312891Swpaul	 */
54412891Swpaul	return (NULL);
54512891Swpaul}
54612891Swpaul
54712891Swpaulypresp_master *
54812891Swpaulypproc_master_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
54912891Swpaul{
55012891Swpaul	static ypresp_master  result;
55112891Swpaul	DBT key,data;
55212891Swpaul
55314303Swpaul	result.peer = "";
55414303Swpaul
55512891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp)) {
55612891Swpaul		result.stat = YP_YPERR;
55712891Swpaul		return(&result);
55812891Swpaul	}
55912891Swpaul
56012891Swpaul	if (argp->domain == NULL) {
56112891Swpaul		result.stat = YP_BADARGS;
56212891Swpaul		return (&result);
56312891Swpaul	}
56412891Swpaul
56512891Swpaul	if (yp_validdomain(argp->domain)) {
56612891Swpaul		result.stat = YP_NODOM;
56712891Swpaul		return (&result);
56812891Swpaul	}
56912891Swpaul
57012891Swpaul	key.data = "YP_MASTER_NAME";
57112891Swpaul	key.size = sizeof("YP_MASTER_NAME") - 1;
57212891Swpaul
57312891Swpaul	result.stat = yp_get_record(argp->domain, argp->map, &key, &data, 1);
57412891Swpaul
57512891Swpaul	if (result.stat == YP_TRUE) {
57612891Swpaul		result.peer = (char *)data.data;
57712891Swpaul		result.peer[data.size] = '\0';
57812891Swpaul	} else
57912891Swpaul		result.peer = "";
58012891Swpaul
58112891Swpaul	return (&result);
58212891Swpaul}
58312891Swpaul
58412891Swpaulypresp_order *
58512891Swpaulypproc_order_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
58612891Swpaul{
58712891Swpaul	static ypresp_order  result;
58812891Swpaul	DBT key,data;
58912891Swpaul
59014304Swpaul	result.ordernum = 0;
59114304Swpaul
59212891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp)) {
59312891Swpaul		result.stat = YP_YPERR;
59412891Swpaul		return(&result);
59512891Swpaul	}
59612891Swpaul
59712891Swpaul	if (argp->domain == NULL) {
59812891Swpaul		result.stat = YP_BADARGS;
59912891Swpaul		return (&result);
60012891Swpaul	}
60112891Swpaul
60212891Swpaul	if (yp_validdomain(argp->domain)) {
60312891Swpaul		result.stat = YP_NODOM;
60412891Swpaul		return (&result);
60512891Swpaul	}
60612891Swpaul
60712891Swpaul	/*
60812891Swpaul	 * We could just check the timestamp on the map file,
60912891Swpaul	 * but that's a hack: we'll only know the last time the file
61012891Swpaul	 * was touched, not the last time the database contents were
61112891Swpaul	 * updated.
61212891Swpaul	 */
61312891Swpaul	key.data = "YP_LAST_MODIFIED";
61412891Swpaul	key.size = sizeof("YP_LAST_MODIFIED") - 1;
61512891Swpaul
61612891Swpaul	result.stat = yp_get_record(argp->domain, argp->map, &key, &data, 1);
61712891Swpaul
61812891Swpaul	if (result.stat == YP_TRUE)
61912891Swpaul		result.ordernum = atoi((char *)data.data);
62012891Swpaul	else
62112891Swpaul		result.ordernum = 0;
62212891Swpaul
62312891Swpaul	return (&result);
62412891Swpaul}
62512891Swpaul
62612891Swpaulstatic void yp_maplist_free(yp_maplist)
62712891Swpaul	struct ypmaplist *yp_maplist;
62812891Swpaul{
62912891Swpaul	register struct ypmaplist *next;
63012891Swpaul
63112891Swpaul	while(yp_maplist) {
63212891Swpaul		next = yp_maplist->next;
63312891Swpaul		free(yp_maplist->map);
63412891Swpaul		free(yp_maplist);
63512891Swpaul		yp_maplist = next;
63612891Swpaul	}
63712891Swpaul	return;
63812891Swpaul}
63912891Swpaul
64012891Swpaulstatic struct ypmaplist *yp_maplist_create(domain)
64112891Swpaul	const char *domain;
64212891Swpaul{
64312891Swpaul	char yp_mapdir[MAXPATHLEN + 2];
64412891Swpaul	char yp_mapname[MAXPATHLEN + 2];
64512891Swpaul	struct ypmaplist *cur = NULL;
64612891Swpaul	struct ypmaplist *yp_maplist = NULL;
64712891Swpaul	DIR *dird;
64812891Swpaul	struct dirent *dirp;
64912891Swpaul	struct stat statbuf;
65012891Swpaul
65112891Swpaul	snprintf(yp_mapdir, sizeof(yp_mapdir), "%s/%s", yp_dir, domain);
65212891Swpaul
65312891Swpaul	if ((dird = opendir(yp_mapdir)) == NULL) {
65413800Swpaul		yp_error("opendir(%s) failed: %s", yp_mapdir, strerror(errno));
65512891Swpaul		return(NULL);
65612891Swpaul	}
65712891Swpaul
65812891Swpaul	while ((dirp = readdir(dird)) != NULL) {
65912891Swpaul		if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
66014304Swpaul			snprintf(yp_mapname, sizeof(yp_mapname), "%s/%s",
66114304Swpaul							yp_mapdir,dirp->d_name);
66214304Swpaul			if (stat(yp_mapname, &statbuf) < 0 ||
66314304Swpaul						!S_ISREG(statbuf.st_mode))
66412891Swpaul				continue;
66514304Swpaul			if ((cur = (struct ypmaplist *)
66614304Swpaul					malloc(sizeof(struct ypmaplist))) < 0) {
66714304Swpaul				yp_error("malloc() failed: %s",strerror(errno));
66812891Swpaul				closedir(dird);
66912891Swpaul				yp_maplist_free(yp_maplist);
67012891Swpaul				return(NULL);
67112891Swpaul			}
67212891Swpaul			if ((cur->map = (char *)strdup(dirp->d_name)) == NULL) {
67314304Swpaul				yp_error("strdup() failed: %s",strerror(errno));
67412891Swpaul				closedir(dird);
67512891Swpaul				yp_maplist_free(yp_maplist);
67612891Swpaul				return(NULL);
67712891Swpaul			}
67812891Swpaul			cur->next = yp_maplist;
67912891Swpaul			yp_maplist = cur;
68012891Swpaul			if (debug)
68112891Swpaul				yp_error("map: %s", yp_maplist->map);
68212891Swpaul		}
68312891Swpaul
68412891Swpaul	}
68512891Swpaul	closedir(dird);
68612891Swpaul	return(yp_maplist);
68712891Swpaul}
68812891Swpaul
68912891Swpaulypresp_maplist *
69012891Swpaulypproc_maplist_2_svc(domainname *argp, struct svc_req *rqstp)
69112891Swpaul{
69212891Swpaul	static ypresp_maplist  result;
69312891Swpaul
69414304Swpaul	result.maps = NULL;
69514304Swpaul
69612891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp)) {
69712891Swpaul		result.stat = YP_YPERR;
69812891Swpaul		return(&result);
69912891Swpaul	}
70012891Swpaul
70112891Swpaul	if (argp == NULL) {
70212891Swpaul		result.stat = YP_BADARGS;
70312891Swpaul		return (&result);
70412891Swpaul	}
70512891Swpaul
70612891Swpaul	if (yp_validdomain(*argp)) {
70712891Swpaul		result.stat = YP_NODOM;
70812891Swpaul		return (&result);
70912891Swpaul	}
71012891Swpaul
71112891Swpaul	/*
71212891Swpaul	 * We have to construct a linked list for the ypproc_maplist
71312891Swpaul	 * procedure using dynamically allocated memory. Since the XDR
71412891Swpaul	 * layer won't free this list for us, we have to deal with it
71512891Swpaul	 * ourselves. We call yp_maplist_free() first to free any
71612891Swpaul	 * previously allocated data we may have accumulated to insure
71712891Swpaul	 * that we have only one linked list in memory at any given
71812891Swpaul	 * time.
71912891Swpaul	 */
72012891Swpaul
72112891Swpaul	yp_maplist_free(result.maps);
72212891Swpaul
72312891Swpaul	if ((result.maps = yp_maplist_create(*argp)) == NULL) {
72412891Swpaul		yp_error("yp_maplist_create failed");
72512891Swpaul		result.stat = YP_YPERR;
72612891Swpaul		return(&result);
72712891Swpaul	} else
72812891Swpaul		result.stat = YP_TRUE;
72912891Swpaul
73012891Swpaul	return (&result);
73112891Swpaul}
73214262Swpaul
73314262Swpaul/*
73414262Swpaul * NIS v1 support. The nullproc, domain and domain_nonack
73514262Swpaul * functions from v1 are identical to those in v2, so all
73614262Swpaul * we have to do is hand off to them.
73714262Swpaul *
73814262Swpaul * The other functions are mostly just wrappers around their v2
73914262Swpaul * counterparts. For example, for the v1 'match' procedure, we
74014262Swpaul * crack open the argument structure, make a request to the v2
74114262Swpaul * 'match' function, repackage the data into a v1 response and
74214262Swpaul * then send it on its way.
74314262Swpaul *
74414262Swpaul * Note that we don't support the pull, push and get procedures.
74514262Swpaul * There's little documentation available to show what they
74614262Swpaul * do, and I suspect they're meant largely for map transfers
74714262Swpaul * between master and slave servers.
74814262Swpaul */
74914262Swpaul
75014262Swpaulvoid *
75114262Swpaulypoldproc_null_1_svc(void *argp, struct svc_req *rqstp)
75214262Swpaul{
75314262Swpaul	return(ypproc_null_2_svc(argp, rqstp));
75414262Swpaul}
75514262Swpaul
75614262Swpaulbool_t *
75714262Swpaulypoldproc_domain_1_svc(domainname *argp, struct svc_req *rqstp)
75814262Swpaul{
75914262Swpaul	return(ypproc_domain_2_svc(argp, rqstp));
76014262Swpaul}
76114262Swpaul
76214262Swpaulbool_t *
76314262Swpaulypoldproc_domain_nonack_1_svc(domainname *argp, struct svc_req *rqstp)
76414262Swpaul{
76514262Swpaul	return (ypproc_domain_nonack_2_svc(argp, rqstp));
76614262Swpaul}
76714262Swpaul
76814304Swpaul/*
76914304Swpaul * the 'match' procedure sends a response of type YPRESP_VAL
77014304Swpaul */
77114262Swpaulypresponse *
77214262Swpaulypoldproc_match_1_svc(yprequest *argp, struct svc_req *rqstp)
77314262Swpaul{
77414262Swpaul	static ypresponse  result;
77514262Swpaul	ypresp_val *v2_result;
77614262Swpaul
77714262Swpaul	result.yp_resptype = YPRESP_VAL;
77814304Swpaul	result.ypresponse_u.yp_resp_valtype.val.valdat_val = "";
77914304Swpaul	result.ypresponse_u.yp_resp_valtype.val.valdat_len = 0;
78014262Swpaul
78114262Swpaul	if (argp->yp_reqtype != YPREQ_KEY) {
78214262Swpaul		result.ypresponse_u.yp_resp_valtype.stat = YP_BADARGS;
78314262Swpaul		return(&result);
78414262Swpaul	}
78514262Swpaul
78614262Swpaul	v2_result = ypproc_match_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp);
78714262Swpaul	if (v2_result == NULL)
78814262Swpaul		return(NULL);
78914262Swpaul
79014262Swpaul	bcopy((char *)v2_result,
79114262Swpaul	      (char *)&result.ypresponse_u.yp_resp_valtype,
79214262Swpaul	      sizeof(ypresp_val));
79314262Swpaul
79414262Swpaul	return (&result);
79514262Swpaul}
79614262Swpaul
79714304Swpaul/*
79814304Swpaul * the 'first' procedure sends a response of type YPRESP_KEY_VAL
79914304Swpaul */
80014262Swpaulypresponse *
80114262Swpaulypoldproc_first_1_svc(yprequest *argp, struct svc_req *rqstp)
80214262Swpaul{
80314262Swpaul	static ypresponse  result;
80414262Swpaul	ypresp_key_val *v2_result;
80514262Swpaul
80614262Swpaul	result.yp_resptype = YPRESP_KEY_VAL;
80714304Swpaul	result.ypresponse_u.yp_resp_key_valtype.val.valdat_val =
80814304Swpaul	result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = "";
80914304Swpaul	result.ypresponse_u.yp_resp_key_valtype.val.valdat_len =
81014304Swpaul	result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0;
81114262Swpaul
81214262Swpaul	if (argp->yp_reqtype != YPREQ_NOKEY) {
81314262Swpaul		result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS;
81414262Swpaul		return(&result);
81514262Swpaul	}
81614262Swpaul
81714262Swpaul	v2_result = ypproc_first_2_svc(&argp->yprequest_u.yp_req_nokeytype,
81814262Swpaul									rqstp);
81914262Swpaul	if (v2_result == NULL)
82014262Swpaul		return(NULL);
82114262Swpaul
82214262Swpaul	bcopy((char *)v2_result,
82314262Swpaul	      (char *)&result.ypresponse_u.yp_resp_key_valtype,
82414262Swpaul	      sizeof(ypresp_key_val));
82514262Swpaul
82614262Swpaul	return (&result);
82714262Swpaul}
82814262Swpaul
82914304Swpaul/*
83014304Swpaul * the 'next' procedure sends a response of type YPRESP_KEY_VAL
83114304Swpaul */
83214262Swpaulypresponse *
83314262Swpaulypoldproc_next_1_svc(yprequest *argp, struct svc_req *rqstp)
83414262Swpaul{
83514262Swpaul	static ypresponse  result;
83614262Swpaul	ypresp_key_val *v2_result;
83714262Swpaul
83814262Swpaul	result.yp_resptype = YPRESP_KEY_VAL;
83914304Swpaul	result.ypresponse_u.yp_resp_key_valtype.val.valdat_val =
84014304Swpaul	result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = "";
84114304Swpaul	result.ypresponse_u.yp_resp_key_valtype.val.valdat_len =
84214304Swpaul	result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0;
84314262Swpaul
84414262Swpaul	if (argp->yp_reqtype != YPREQ_KEY) {
84514262Swpaul		result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS;
84614262Swpaul		return(&result);
84714262Swpaul	}
84814262Swpaul
84914262Swpaul	v2_result = ypproc_next_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp);
85014262Swpaul	if (v2_result == NULL)
85114262Swpaul		return(NULL);
85214262Swpaul
85314262Swpaul	bcopy((char *)v2_result,
85414262Swpaul	      (char *)&result.ypresponse_u.yp_resp_key_valtype,
85514262Swpaul	      sizeof(ypresp_key_val));
85614262Swpaul
85714262Swpaul	return (&result);
85814262Swpaul}
85914262Swpaul
86014304Swpaul/*
86114304Swpaul * the 'poll' procedure sends a response of type YPRESP_MAP_PARMS
86214304Swpaul */
86314262Swpaulypresponse *
86414262Swpaulypoldproc_poll_1_svc(yprequest *argp, struct svc_req *rqstp)
86514262Swpaul{
86614262Swpaul	static ypresponse  result;
86714262Swpaul	ypresp_master *v2_result1;
86814262Swpaul	ypresp_order *v2_result2;
86914262Swpaul
87014262Swpaul	result.yp_resptype = YPRESP_MAP_PARMS;
87114262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.domain =
87214262Swpaul		argp->yprequest_u.yp_req_nokeytype.domain;
87314262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.map =
87414262Swpaul		argp->yprequest_u.yp_req_nokeytype.map;
87514262Swpaul	/*
87614262Swpaul	 * Hmm... there is no 'status' value in the
87714262Swpaul	 * yp_resp_map_parmstype structure, so I have to
87814262Swpaul	 * guess at what to do to indicate a failure.
87914262Swpaul	 * I hope this is right.
88014262Swpaul	 */
88114262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.ordernum = 0;
88214262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.peer = "";
88314262Swpaul
88414262Swpaul	if (argp->yp_reqtype != YPREQ_MAP_PARMS) {
88514262Swpaul		return(&result);
88614262Swpaul	}
88714262Swpaul
88814262Swpaul	v2_result1 = ypproc_master_2_svc(&argp->yprequest_u.yp_req_nokeytype,
88914262Swpaul									rqstp);
89014262Swpaul	if (v2_result1 == NULL)
89114262Swpaul		return(NULL);
89214262Swpaul
89314262Swpaul	if (v2_result1->stat != YP_TRUE) {
89414262Swpaul		return(&result);
89514262Swpaul	}
89614262Swpaul
89714262Swpaul	v2_result2 = ypproc_order_2_svc(&argp->yprequest_u.yp_req_nokeytype,
89814262Swpaul									rqstp);
89914262Swpaul	if (v2_result2 == NULL)
90014262Swpaul		return(NULL);
90114262Swpaul
90214262Swpaul	if (v2_result2->stat != YP_TRUE) {
90314262Swpaul		return(&result);
90414262Swpaul	}
90514262Swpaul
90614262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.peer =
90714262Swpaul		v2_result1->peer;
90814262Swpaul	result.ypresponse_u.yp_resp_map_parmstype.ordernum =
90914262Swpaul		v2_result2->ordernum;
91014262Swpaul
91114262Swpaul	return (&result);
91214262Swpaul}
91314262Swpaul
91414262Swpaulypresponse *
91514262Swpaulypoldproc_push_1_svc(yprequest *argp, struct svc_req *rqstp)
91614262Swpaul{
91714262Swpaul	static ypresponse  result;
91814262Swpaul
91914262Swpaul	/*
92014262Swpaul	 * Not implemented.
92114262Swpaul	 */
92214262Swpaul
92314262Swpaul	return (&result);
92414262Swpaul}
92514262Swpaul
92614262Swpaulypresponse *
92714262Swpaulypoldproc_pull_1_svc(yprequest *argp, struct svc_req *rqstp)
92814262Swpaul{
92914262Swpaul	static ypresponse  result;
93014262Swpaul
93114262Swpaul	/*
93214262Swpaul	 * Not implemented.
93314262Swpaul	 */
93414262Swpaul
93514262Swpaul	return (&result);
93614262Swpaul}
93714262Swpaul
93814262Swpaulypresponse *
93914262Swpaulypoldproc_get_1_svc(yprequest *argp, struct svc_req *rqstp)
94014262Swpaul{
94114262Swpaul	static ypresponse  result;
94214262Swpaul
94314262Swpaul	/*
94414262Swpaul	 * Not implemented.
94514262Swpaul	 */
94614262Swpaul
94714262Swpaul	return (&result);
94814262Swpaul}
949