yp_server.c revision 14240
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
4814240Swpaulstatic char rcsid[] = "$Id: yp_server.c,v 1.2 1995/12/23 21:35:35 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
5512891Swpaulvoid *
5612891Swpaulypproc_null_2_svc(void *argp, struct svc_req *rqstp)
5712891Swpaul{
5812891Swpaul	static char * result;
5912891Swpaul	static char rval = 0;
6012891Swpaul
6112891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
6212891Swpaul		return(NULL);
6312891Swpaul
6412891Swpaul	result = &rval;
6512891Swpaul
6612891Swpaul	return((void *) &result);
6712891Swpaul}
6812891Swpaul
6912891Swpaulbool_t *
7012891Swpaulypproc_domain_2_svc(domainname *argp, struct svc_req *rqstp)
7112891Swpaul{
7212891Swpaul	static bool_t  result;
7312891Swpaul
7412891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp)) {
7512891Swpaul		result = FALSE;
7612891Swpaul		return (&result);
7712891Swpaul	}
7812891Swpaul
7912891Swpaul	if (argp == NULL || yp_validdomain(*argp))
8012891Swpaul		result = FALSE;
8112891Swpaul	else
8212891Swpaul		result = TRUE;
8312891Swpaul
8412891Swpaul	return (&result);
8512891Swpaul}
8612891Swpaul
8712891Swpaulbool_t *
8812891Swpaulypproc_domain_nonack_2_svc(domainname *argp, struct svc_req *rqstp)
8912891Swpaul{
9012891Swpaul	static bool_t  result;
9112891Swpaul
9212891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
9312891Swpaul		return (NULL);
9412891Swpaul
9512891Swpaul	if (argp == NULL || yp_validdomain(*argp))
9612891Swpaul		return (NULL);
9712891Swpaul	else
9812891Swpaul		result = TRUE;
9912891Swpaul
10012891Swpaul	return (&result);
10112891Swpaul}
10212891Swpaul
10312891Swpaulypresp_val *
10412891Swpaulypproc_match_2_svc(ypreq_key *argp, struct svc_req *rqstp)
10512891Swpaul{
10612891Swpaul	static ypresp_val  result;
10712891Swpaul	DBT key, data;
10812891Swpaul
10912891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
11012891Swpaul		result.stat = YP_YPERR;
11112891Swpaul		return (&result);
11212891Swpaul	}
11312891Swpaul
11412891Swpaul	if (argp->domain == NULL || argp->map == NULL) {
11512891Swpaul		result.stat = YP_BADARGS;
11612891Swpaul		return (&result);
11712891Swpaul	}
11812891Swpaul
11912891Swpaul	if (yp_validdomain(argp->domain)) {
12012891Swpaul		result.stat = YP_NODOM;
12112891Swpaul		return(&result);
12212891Swpaul	}
12312891Swpaul
12412891Swpaul	key.size = argp->key.keydat_len;
12512891Swpaul	key.data = argp->key.keydat_val;
12612891Swpaul
12712891Swpaul	result.stat = yp_get_record(argp->domain, argp->map, &key, &data, 0);
12812891Swpaul
12912891Swpaul	if (result.stat == YP_TRUE) {
13012891Swpaul		result.val.valdat_len = data.size;
13112891Swpaul		result.val.valdat_val = data.data;
13212891Swpaul	}
13312891Swpaul
13412891Swpaul	/*
13512891Swpaul	 * Do DNS lookups for hosts maps if database lookup failed.
13612891Swpaul	 */
13712891Swpaul
13812891Swpaul	if (do_dns && result.stat != YP_TRUE && strstr(argp->map, "hosts")) {
13912997Swpaul		char *rval = NULL;
14012891Swpaul
14112891Swpaul	/* DNS lookups can take time -- do them in a subprocess */
14212891Swpaul
14312891Swpaul		if (!debug && children < MAX_CHILDREN && fork()) {
14412891Swpaul			children++;
14512891Swpaul			forked = 0;
14612891Swpaul			/*
14712891Swpaul			 * Returning NULL here prevents svc_sendreply()
14812891Swpaul			 * from being called by the parent. This is vital
14912891Swpaul			 * since having both the parent and the child process
15012891Swpaul			 * call it would confuse the client.
15112891Swpaul			 */
15212891Swpaul			return (NULL);
15312891Swpaul		} else {
15412891Swpaul			forked++;
15512891Swpaul		}
15612891Swpaul
15712891Swpaul		if (debug)
15812891Swpaul			yp_error("Doing DNS lookup of %.*s",
15912891Swpaul			 	  argp->key.keydat_len,
16012891Swpaul				  argp->key.keydat_val);
16112891Swpaul
16212891Swpaul		/* NUL terminate! NUL terminate!! NUL TERMINATE!!! */
16312891Swpaul		argp->key.keydat_val[argp->key.keydat_len] = '\0';
16412891Swpaul
16512891Swpaul		if (!strcmp(argp->map, "hosts.byname"))
16612891Swpaul			rval = yp_dnsname((char *)argp->key.keydat_val);
16712891Swpaul		else if (!strcmp(argp->map, "hosts.byaddr"))
16812891Swpaul			rval = yp_dnsaddr((const char *)argp->key.keydat_val);
16912891Swpaul
17012891Swpaul
17112891Swpaul		if (rval) {
17212891Swpaul			if (debug)
17312891Swpaul				yp_error("DNS lookup successful. Result: %s", rval);
17412891Swpaul			result.val.valdat_len = strlen(rval);
17512891Swpaul			result.val.valdat_val = rval;
17612891Swpaul			result.stat = YP_TRUE;
17712891Swpaul		} else {
17812891Swpaul			if (debug)
17912891Swpaul				yp_error("DNS lookup failed.");
18012891Swpaul			result.stat = YP_NOKEY;
18112891Swpaul		}
18212891Swpaul	}
18312891Swpaul
18412891Swpaul	return (&result);
18512891Swpaul}
18612891Swpaul
18712891Swpaulypresp_key_val *
18812891Swpaulypproc_first_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
18912891Swpaul{
19012891Swpaul	static ypresp_key_val  result;
19112891Swpaul	DBT key, data;
19212891Swpaul	DB *dbp;
19312891Swpaul
19412891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
19512891Swpaul		result.stat = YP_YPERR;
19612891Swpaul		return (&result);
19712891Swpaul	}
19812891Swpaul
19912891Swpaul	if (argp->domain == NULL) {
20012891Swpaul		result.stat = YP_BADARGS;
20112891Swpaul		return (&result);
20212891Swpaul	}
20312891Swpaul
20412891Swpaul	if (yp_validdomain(argp->domain)) {
20512891Swpaul		result.stat = YP_NODOM;
20612891Swpaul		return(&result);
20712891Swpaul	}
20812891Swpaul
20912891Swpaul	if ((dbp = yp_open_db(argp->domain, argp->map)) == NULL) {
21012891Swpaul		result.stat = yp_errno;
21112891Swpaul		return(&result);
21212891Swpaul	}
21312891Swpaul
21412891Swpaul	key.data = NULL;
21512891Swpaul	key.size = 0;
21612891Swpaul	result.stat = yp_first_record(dbp, &key, &data);
21712891Swpaul	(void)(dbp->close)(dbp);
21812891Swpaul
21912891Swpaul	if (result.stat == YP_TRUE) {
22012891Swpaul		result.key.keydat_len = key.size;
22112891Swpaul		result.key.keydat_val = key.data;
22212891Swpaul		result.val.valdat_len = data.size;
22312891Swpaul		result.val.valdat_val = data.data;
22412891Swpaul	}
22512891Swpaul
22612891Swpaul	return (&result);
22712891Swpaul}
22812891Swpaul
22912891Swpaulypresp_key_val *
23012891Swpaulypproc_next_2_svc(ypreq_key *argp, struct svc_req *rqstp)
23112891Swpaul{
23212891Swpaul	static ypresp_key_val  result;
23312891Swpaul	DBT key, data;
23412891Swpaul	DB *dbp;
23512891Swpaul
23612891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
23712891Swpaul		result.stat = YP_YPERR;
23812891Swpaul		return (&result);
23912891Swpaul	}
24012891Swpaul
24112891Swpaul	if (argp->domain == NULL || argp->map == NULL) {
24212891Swpaul		result.stat = YP_BADARGS;
24312891Swpaul		return (&result);
24412891Swpaul	}
24512891Swpaul
24612891Swpaul	if (yp_validdomain(argp->domain)) {
24712891Swpaul		result.stat = YP_NODOM;
24812891Swpaul		return(&result);
24912891Swpaul	}
25012891Swpaul
25112891Swpaul	if ((dbp = yp_open_db(argp->domain, argp->map)) == NULL) {
25212891Swpaul		result.stat = yp_errno;
25312891Swpaul		return(&result);
25412891Swpaul	}
25512891Swpaul
25612891Swpaul	key.size = argp->key.keydat_len;
25712891Swpaul	key.data = argp->key.keydat_val;
25812891Swpaul
25912891Swpaul	result.stat = yp_next_record(dbp, &key, &data, 0);
26012891Swpaul	(void)(dbp->close)(dbp);
26112891Swpaul
26212891Swpaul	if (result.stat == YP_TRUE) {
26312891Swpaul		result.key.keydat_len = key.size;
26412891Swpaul		result.key.keydat_val = key.data;
26512891Swpaul		result.val.valdat_len = data.size;
26612891Swpaul		result.val.valdat_val = data.data;
26712891Swpaul	}
26812891Swpaul
26912891Swpaul	return (&result);
27012891Swpaul}
27112891Swpaul
27212997Swpaulstatic void ypxfr_callback(rval,addr,transid,prognum,port)
27312997Swpaul	ypxfrstat rval;
27412997Swpaul	struct sockaddr_in *addr;
27512997Swpaul	unsigned int transid;
27612997Swpaul	unsigned int prognum;
27712997Swpaul	unsigned long port;
27812997Swpaul{
27912997Swpaul	CLIENT *clnt;
28012997Swpaul	int sock = RPC_ANYSOCK;
28112997Swpaul	struct timeval timeout;
28212997Swpaul	yppushresp_xfr ypxfr_resp;
28313375Swpaul	struct rpc_err err;
28412997Swpaul
28513375Swpaul	timeout.tv_sec = 5;
28612997Swpaul	timeout.tv_usec = 0;
28712997Swpaul	addr->sin_port = htons(port);
28812997Swpaul
28912997Swpaul	if ((clnt = clntudp_create(addr, prognum, 1, timeout, &sock)) == NULL)
29012997Swpaul		yp_error("%s", clnt_spcreateerror("failed to establish \
29112997Swpaulcallback handle"));
29212997Swpaul
29312997Swpaul	ypxfr_resp.status = rval;
29412997Swpaul	ypxfr_resp.transid = transid;
29512997Swpaul
29613375Swpaul	/* Turn the timeout off -- we don't want to block. */
29713375Swpaul	timeout.tv_sec = 0;
29813375Swpaul	if (clnt_control(clnt, CLSET_TIMEOUT, (char *)&timeout) == FALSE)
29913375Swpaul		yp_error("failed to set timeout on ypproc_xfr callback");
30012997Swpaul
30113375Swpaul	if (yppushproc_xfrresp_1(&ypxfr_resp, clnt) == NULL) {
30213375Swpaul		clnt_geterr(clnt, &err);
30313375Swpaul		if (err.re_status != RPC_SUCCESS &&
30413375Swpaul		    err.re_status != RPC_TIMEDOUT)
30513375Swpaul			yp_error("%s", clnt_sperror(clnt,
30613375Swpaul				"ypxfr callback failed"));
30713375Swpaul	}
30813375Swpaul
30912997Swpaul	clnt_destroy(clnt);
31012997Swpaul	return;
31112997Swpaul}
31212997Swpaul
31312891Swpaulypresp_xfr *
31412891Swpaulypproc_xfr_2_svc(ypreq_xfr *argp, struct svc_req *rqstp)
31512891Swpaul{
31612891Swpaul	static ypresp_xfr  result;
31712997Swpaul	struct sockaddr_in *rqhost;
31812891Swpaul
31913375Swpaul	result.transid = argp->transid;
32013375Swpaul	rqhost = svc_getcaller(rqstp->rq_xprt);
32113375Swpaul
32212891Swpaul	if (yp_access(argp->map_parms.map, (struct svc_req *)rqstp)) {
32313375Swpaul		/* Order is important: send regular RPC reply, then callback */
32412891Swpaul		result.xfrstat = YPXFR_REFUSED;
32513375Swpaul		svc_sendreply(rqstp->rq_xprt, xdr_ypresp_xfr, (char *)&result);
32613375Swpaul		ypxfr_callback(YPXFR_REFUSED,rqhost,argp->transid,
32713375Swpaul			       argp->prog,argp->port);
32813375Swpaul		return(NULL);
32912891Swpaul	}
33012891Swpaul
33112891Swpaul	if (argp->map_parms.domain == NULL) {
33212891Swpaul		result.xfrstat = YPXFR_BADARGS;
33313375Swpaul		svc_sendreply(rqstp->rq_xprt, xdr_ypresp_xfr, (char *)&result);
33413375Swpaul		ypxfr_callback(YPXFR_BADARGS,rqhost,argp->transid,
33513375Swpaul			       argp->prog,argp->port);
33613375Swpaul		return(NULL);
33712891Swpaul	}
33812891Swpaul
33912891Swpaul	if (yp_validdomain(argp->map_parms.domain)) {
34012891Swpaul		result.xfrstat = YPXFR_NODOM;
34113375Swpaul		svc_sendreply(rqstp->rq_xprt, xdr_ypresp_xfr, (char *)&result);
34213375Swpaul		ypxfr_callback(YPXFR_NODOM,rqhost,argp->transid,
34313375Swpaul			       argp->prog,argp->port);
34413375Swpaul		return(NULL);
34512891Swpaul	}
34612891Swpaul
34712891Swpaul	switch(fork()) {
34812891Swpaul	case 0:
34912891Swpaul	{
35012891Swpaul		char g[11], t[11], p[11];
35112891Swpaul		char ypxfr_command[MAXPATHLEN + 2];
35212891Swpaul
35312891Swpaul		sprintf (ypxfr_command, "%sypxfr", _PATH_LIBEXEC);
35412891Swpaul		sprintf (t, "%u", argp->transid);
35512891Swpaul		sprintf (g, "%u", argp->prog);
35612891Swpaul		sprintf (p, "%u", argp->port);
35712997Swpaul		if (debug)
35812997Swpaul			close(0); close(1); close(2);
35912997Swpaul		if (strcmp(yp_dir, _PATH_YP)) {
36012997Swpaul			execl(ypxfr_command, "ypxfr", "-d", argp->map_parms.domain,
36113375Swpaul		      	"-h", argp->map_parms.peer, "-p", yp_dir, "-C", t,
36212997Swpaul		      	g, inet_ntoa(rqhost->sin_addr), p, argp->map_parms.map,
36312997Swpaul		      	NULL);
36412997Swpaul		} else {
36512997Swpaul			execl(ypxfr_command, "ypxfr", "-d", argp->map_parms.domain,
36613375Swpaul		      	"-h", argp->map_parms.peer, "-C", t, g,
36712997Swpaul		      	inet_ntoa(rqhost->sin_addr), p, argp->map_parms.map,
36812997Swpaul		      	NULL);
36912997Swpaul		}
37012997Swpaul		forked++;
37113375Swpaul		result.xfrstat = YPXFR_XFRERR;
37212891Swpaul		yp_error("ypxfr execl(): %s", strerror(errno));
37313375Swpaul		svc_sendreply(rqstp->rq_xprt, xdr_ypresp_xfr, (char *)&result);
37412997Swpaul		ypxfr_callback(YPXFR_XFRERR,rqhost,argp->transid,
37512997Swpaul			       argp->prog,argp->port);
37613375Swpaul		return(NULL);
37712997Swpaul		break;
37812891Swpaul	}
37912891Swpaul	case -1:
38012891Swpaul		yp_error("ypxfr fork(): %s", strerror(errno));
38113375Swpaul		result.xfrstat = YPXFR_XFRERR;
38213375Swpaul		svc_sendreply(rqstp->rq_xprt, xdr_ypresp_xfr, (char *)&result);
38312997Swpaul		ypxfr_callback(YPXFR_XFRERR,rqhost,argp->transid,
38412997Swpaul			       argp->prog,argp->port);
38513375Swpaul		return(NULL);
38612891Swpaul		break;
38712891Swpaul	default:
38813375Swpaul		result.xfrstat = YPXFR_SUCC;
38912997Swpaul		children++;
39012997Swpaul		forked = 0;
39112891Swpaul		break;
39212891Swpaul	}
39313375Swpaul
39413375Swpaul	return (&result);
39512891Swpaul}
39612891Swpaul
39712891Swpaulvoid *
39812891Swpaulypproc_clear_2_svc(void *argp, struct svc_req *rqstp)
39912891Swpaul{
40012891Swpaul	static char * result;
40112891Swpaul	static char rval = 0;
40212891Swpaul
40312891Swpaul	/*
40412891Swpaul	 * We don't have to do anything for ypproc_clear. Unlike
40512891Swpaul	 * the SunOS ypserv, we don't hold out database descriptors
40612891Swpaul	 * open forever.
40712891Swpaul	 */
40812891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp))
40912891Swpaul		return (NULL);
41012891Swpaul
41114240Swpaul	/* Re-read the securenets database for the hell of it. */
41214240Swpaul	load_securenets();
41314240Swpaul
41412891Swpaul	result = &rval;
41512891Swpaul	return((void *) &result);
41612891Swpaul}
41712891Swpaul
41812891Swpaul/*
41912891Swpaul * For ypproc_all, we have to send a stream of ypresp_all structures
42012891Swpaul * via TCP, but the XDR filter generated from the yp.x protocol
42112891Swpaul * definition file only serializes one such structure. This means that
42212891Swpaul * to send the whole stream, you need a wrapper which feeds all the
42312891Swpaul * records into the underlying XDR routine until it hits an 'EOF.'
42412891Swpaul * But to use the wrapper, you have to violate the boundaries between
42512891Swpaul * RPC layers by calling svc_sendreply() directly from the ypproc_all
42612891Swpaul * service routine instead of letting the RPC dispatcher do it.
42712891Swpaul *
42812891Swpaul * Bleah.
42912891Swpaul */
43012891Swpaul
43112891Swpaul/*
43212891Swpaul * Custom XDR routine for serialzing results of ypproc_all: keep
43312891Swpaul * reading from the database and spew until we run out of records
43412891Swpaul * or encounter an error.
43512891Swpaul */
43612891Swpaulstatic bool_t
43712891Swpaulxdr_my_ypresp_all(register XDR *xdrs, ypresp_all *objp)
43812891Swpaul{
43912891Swpaul	DBT key, data;
44012891Swpaul
44112891Swpaul	while (1) {
44212891Swpaul		/* Get a record. */
44312891Swpaul		key.size = objp->ypresp_all_u.val.key.keydat_len;
44412891Swpaul		key.data = objp->ypresp_all_u.val.key.keydat_val;
44512891Swpaul
44612891Swpaul		if ((objp->ypresp_all_u.val.stat =
44712891Swpaul		    yp_next_record(spec_dbp,&key,&data,1)) == 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;
47612891Swpaul
47712891Swpaul	if (yp_access(argp->map, (struct svc_req *)rqstp)) {
47812891Swpaul		result.ypresp_all_u.val.stat = YP_YPERR;
47912891Swpaul		return (&result);
48012891Swpaul	}
48112891Swpaul
48212891Swpaul	if (argp->domain == NULL || argp->map == NULL) {
48312891Swpaul		result.ypresp_all_u.val.stat = YP_BADARGS;
48412891Swpaul		return (&result);
48512891Swpaul	}
48612891Swpaul
48712891Swpaul	if (yp_validdomain(argp->domain)) {
48812891Swpaul		result.ypresp_all_u.val.stat = YP_NODOM;
48912891Swpaul		return(&result);
49012891Swpaul	}
49112891Swpaul
49212891Swpaul	/*
49312891Swpaul	 * The ypproc_all procedure can take a while to complete.
49412891Swpaul	 * Best to handle it in a subprocess so the parent doesn't
49512891Swpaul	 * block. We fork() here so we don't end up sharing a
49612891Swpaul	 * DB file handle with the parent.
49712891Swpaul	 */
49812891Swpaul
49912891Swpaul	if (!debug && children < MAX_CHILDREN && fork()) {
50012891Swpaul		children++;
50112891Swpaul		forked = 0;
50212891Swpaul		return (NULL);
50312891Swpaul	} else {
50412891Swpaul		forked++;
50512891Swpaul	}
50612891Swpaul
50712891Swpaul	if ((spec_dbp = yp_open_db(argp->domain, argp->map)) == NULL) {
50812891Swpaul		result.ypresp_all_u.val.stat = yp_errno;
50912891Swpaul		return(&result);
51012891Swpaul	}
51112891Swpaul
51212891Swpaul	/* Kick off the actual data transfer. */
51312891Swpaul	svc_sendreply(rqstp->rq_xprt, xdr_my_ypresp_all, (char *)&result);
51412891Swpaul
51512891Swpaul	/* Close database when done. */
51612891Swpaul	(void)(spec_dbp->close)(spec_dbp);
51712891Swpaul
51812891Swpaul	/*
51912891Swpaul	 * Returning NULL prevents the dispatcher from calling
52012891Swpaul	 * svc_sendreply() since we already did it.
52112891Swpaul	 */
52212891Swpaul	return (NULL);
52312891Swpaul}
52412891Swpaul
52512891Swpaulypresp_master *
52612891Swpaulypproc_master_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
52712891Swpaul{
52812891Swpaul	static ypresp_master  result;
52912891Swpaul	DBT key,data;
53012891Swpaul
53112891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp)) {
53212891Swpaul		result.stat = YP_YPERR;
53312891Swpaul		return(&result);
53412891Swpaul	}
53512891Swpaul
53612891Swpaul	if (argp->domain == NULL) {
53712891Swpaul		result.stat = YP_BADARGS;
53812891Swpaul		return (&result);
53912891Swpaul	}
54012891Swpaul
54112891Swpaul	if (yp_validdomain(argp->domain)) {
54212891Swpaul		result.stat = YP_NODOM;
54312891Swpaul		return (&result);
54412891Swpaul	}
54512891Swpaul
54612891Swpaul	key.data = "YP_MASTER_NAME";
54712891Swpaul	key.size = sizeof("YP_MASTER_NAME") - 1;
54812891Swpaul
54912891Swpaul	result.stat = yp_get_record(argp->domain, argp->map, &key, &data, 1);
55012891Swpaul
55112891Swpaul	if (result.stat == YP_TRUE) {
55212891Swpaul		result.peer = (char *)data.data;
55312891Swpaul		result.peer[data.size] = '\0';
55412891Swpaul	} else
55512891Swpaul		result.peer = "";
55612891Swpaul
55712891Swpaul	return (&result);
55812891Swpaul}
55912891Swpaul
56012891Swpaulypresp_order *
56112891Swpaulypproc_order_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
56212891Swpaul{
56312891Swpaul	static ypresp_order  result;
56412891Swpaul	DBT key,data;
56512891Swpaul
56612891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp)) {
56712891Swpaul		result.stat = YP_YPERR;
56812891Swpaul		return(&result);
56912891Swpaul	}
57012891Swpaul
57112891Swpaul	if (argp->domain == NULL) {
57212891Swpaul		result.stat = YP_BADARGS;
57312891Swpaul		return (&result);
57412891Swpaul	}
57512891Swpaul
57612891Swpaul	if (yp_validdomain(argp->domain)) {
57712891Swpaul		result.stat = YP_NODOM;
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	key.data = "YP_LAST_MODIFIED";
58812891Swpaul	key.size = sizeof("YP_LAST_MODIFIED") - 1;
58912891Swpaul
59012891Swpaul	result.stat = yp_get_record(argp->domain, argp->map, &key, &data, 1);
59112891Swpaul
59212891Swpaul	if (result.stat == YP_TRUE)
59312891Swpaul		result.ordernum = atoi((char *)data.data);
59412891Swpaul	else
59512891Swpaul		result.ordernum = 0;
59612891Swpaul
59712891Swpaul	return (&result);
59812891Swpaul}
59912891Swpaul
60012891Swpaulstatic void yp_maplist_free(yp_maplist)
60112891Swpaul	struct ypmaplist *yp_maplist;
60212891Swpaul{
60312891Swpaul	register struct ypmaplist *next;
60412891Swpaul
60512891Swpaul	while(yp_maplist) {
60612891Swpaul		next = yp_maplist->next;
60712891Swpaul		free(yp_maplist->map);
60812891Swpaul		free(yp_maplist);
60912891Swpaul		yp_maplist = next;
61012891Swpaul	}
61112891Swpaul	return;
61212891Swpaul}
61312891Swpaul
61412891Swpaulstatic struct ypmaplist *yp_maplist_create(domain)
61512891Swpaul	const char *domain;
61612891Swpaul{
61712891Swpaul	char yp_mapdir[MAXPATHLEN + 2];
61812891Swpaul	char yp_mapname[MAXPATHLEN + 2];
61912891Swpaul	struct ypmaplist *cur = NULL;
62012891Swpaul	struct ypmaplist *yp_maplist = NULL;
62112891Swpaul	DIR *dird;
62212891Swpaul	struct dirent *dirp;
62312891Swpaul	struct stat statbuf;
62412891Swpaul
62512891Swpaul	snprintf(yp_mapdir, sizeof(yp_mapdir), "%s/%s", yp_dir, domain);
62612891Swpaul
62712891Swpaul	if ((dird = opendir(yp_mapdir)) == NULL) {
62813800Swpaul		yp_error("opendir(%s) failed: %s", yp_mapdir, strerror(errno));
62912891Swpaul		return(NULL);
63012891Swpaul	}
63112891Swpaul
63212891Swpaul	while ((dirp = readdir(dird)) != NULL) {
63312891Swpaul		if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
63412891Swpaul			snprintf(yp_mapname, sizeof(yp_mapname), "%s/%s",yp_mapdir,dirp->d_name);
63512891Swpaul			if (stat(yp_mapname, &statbuf) < 0 || !S_ISREG(statbuf.st_mode))
63612891Swpaul				continue;
63712891Swpaul			if ((cur = (struct ypmaplist *)malloc(sizeof(struct ypmaplist))) < 0) {
63812891Swpaul				yp_error("malloc() failed: %s", strerror(errno));
63912891Swpaul				closedir(dird);
64012891Swpaul				yp_maplist_free(yp_maplist);
64112891Swpaul				return(NULL);
64212891Swpaul			}
64312891Swpaul			if ((cur->map = (char *)strdup(dirp->d_name)) == NULL) {
64412891Swpaul				yp_error("strdup() failed: %s", strerror(errno));
64512891Swpaul				closedir(dird);
64612891Swpaul				yp_maplist_free(yp_maplist);
64712891Swpaul				return(NULL);
64812891Swpaul			}
64912891Swpaul			cur->next = yp_maplist;
65012891Swpaul			yp_maplist = cur;
65112891Swpaul			if (debug)
65212891Swpaul				yp_error("map: %s", yp_maplist->map);
65312891Swpaul		}
65412891Swpaul
65512891Swpaul	}
65612891Swpaul	closedir(dird);
65712891Swpaul	return(yp_maplist);
65812891Swpaul}
65912891Swpaul
66012891Swpaulypresp_maplist *
66112891Swpaulypproc_maplist_2_svc(domainname *argp, struct svc_req *rqstp)
66212891Swpaul{
66312891Swpaul	static ypresp_maplist  result;
66412891Swpaul
66512891Swpaul	if (yp_access(NULL, (struct svc_req *)rqstp)) {
66612891Swpaul		result.stat = YP_YPERR;
66712891Swpaul		return(&result);
66812891Swpaul	}
66912891Swpaul
67012891Swpaul	if (argp == NULL) {
67112891Swpaul		result.stat = YP_BADARGS;
67212891Swpaul		return (&result);
67312891Swpaul	}
67412891Swpaul
67512891Swpaul	if (yp_validdomain(*argp)) {
67612891Swpaul		result.stat = YP_NODOM;
67712891Swpaul		return (&result);
67812891Swpaul	}
67912891Swpaul
68012891Swpaul	/*
68112891Swpaul	 * We have to construct a linked list for the ypproc_maplist
68212891Swpaul	 * procedure using dynamically allocated memory. Since the XDR
68312891Swpaul	 * layer won't free this list for us, we have to deal with it
68412891Swpaul	 * ourselves. We call yp_maplist_free() first to free any
68512891Swpaul	 * previously allocated data we may have accumulated to insure
68612891Swpaul	 * that we have only one linked list in memory at any given
68712891Swpaul	 * time.
68812891Swpaul	 */
68912891Swpaul
69012891Swpaul	yp_maplist_free(result.maps);
69112891Swpaul
69212891Swpaul	if ((result.maps = yp_maplist_create(*argp)) == NULL) {
69312891Swpaul		yp_error("yp_maplist_create failed");
69412891Swpaul		result.stat = YP_YPERR;
69512891Swpaul		return(&result);
69612891Swpaul	} else
69712891Swpaul		result.stat = YP_TRUE;
69812891Swpaul
69912891Swpaul	return (&result);
70012891Swpaul}
701