17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
2548bbca8Daniel Hoffman * Copyright (c) 2016 by Delphix. All rights reserved.
267c478bdstevel@tonic-gate */
277c478bdstevel@tonic-gate
287c478bdstevel@tonic-gate/*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
297c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
307c478bdstevel@tonic-gate
317c478bdstevel@tonic-gate/*
327c478bdstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD
337c478bdstevel@tonic-gate * under license from the Regents of the University of California.
347c478bdstevel@tonic-gate */
357c478bdstevel@tonic-gate
367c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
377c478bdstevel@tonic-gate
387c478bdstevel@tonic-gate/*
397c478bdstevel@tonic-gate *
407c478bdstevel@tonic-gate * This contains YP server code which supplies the set of functions
417c478bdstevel@tonic-gate * requested using rpc.   The top level functions in this module
427c478bdstevel@tonic-gate * are those which have symbols of the form YPPROC_xxxx defined in
437c478bdstevel@tonic-gate * yp_prot.h, and symbols of the form YPOLDPROC_xxxx defined in ypsym.h.
447c478bdstevel@tonic-gate * The latter exist to provide compatibility to the old version of the yp
457c478bdstevel@tonic-gate * protocol/server, and may emulate the behavior of the previous software
467c478bdstevel@tonic-gate * by invoking some other program.
477c478bdstevel@tonic-gate *
487c478bdstevel@tonic-gate * This module also contains functions which are used by (and only by) the
497c478bdstevel@tonic-gate * top-level functions here.
507c478bdstevel@tonic-gate */
517c478bdstevel@tonic-gate
527c478bdstevel@tonic-gate#include <sys/types.h>
537c478bdstevel@tonic-gate#include <sys/socket.h>
547c478bdstevel@tonic-gate#include <netinet/in.h>
557c478bdstevel@tonic-gate#include <arpa/inet.h>
567c478bdstevel@tonic-gate#include <dirent.h>
577c478bdstevel@tonic-gate#include <limits.h>
587c478bdstevel@tonic-gate#include <sys/systeminfo.h>
597c478bdstevel@tonic-gate#include <rpc/rpc.h>
607c478bdstevel@tonic-gate#include <string.h>
617c478bdstevel@tonic-gate#include <malloc.h>
627c478bdstevel@tonic-gate#include <stdlib.h>
637c478bdstevel@tonic-gate#include <unistd.h>
647c478bdstevel@tonic-gate#include <stdio.h>
657c478bdstevel@tonic-gate#include "ypsym.h"
667c478bdstevel@tonic-gate#include "ypdefs.h"
677c478bdstevel@tonic-gate#include <ctype.h>
687c478bdstevel@tonic-gate
697c478bdstevel@tonic-gate/* Use shim version of DBM calls */
707c478bdstevel@tonic-gate#include "shim.h"
717c478bdstevel@tonic-gate#include "shim_hooks.h"
727c478bdstevel@tonic-gate
737c478bdstevel@tonic-gateUSE_YP_PREFIX
747c478bdstevel@tonic-gateUSE_YP_SECURE
757c478bdstevel@tonic-gateUSE_YP_INTERDOMAIN
767c478bdstevel@tonic-gate
777c478bdstevel@tonic-gate#ifndef	YPXFR_PROC
787c478bdstevel@tonic-gate#define	YPXFR_PROC "/usr/lib/netsvc/yp/ypxfr"
797c478bdstevel@tonic-gate#endif
807c478bdstevel@tonic-gatestatic char ypxfr_proc[] = YPXFR_PROC;
817c478bdstevel@tonic-gate#ifndef	YPPUSH_PROC
827c478bdstevel@tonic-gate#define	YPPUSH_PROC "/usr/lib/netsvc/yp/yppush"
837c478bdstevel@tonic-gate#endif
847c478bdstevel@tonic-gatestatic char yppush_proc[] = YPPUSH_PROC;
857c478bdstevel@tonic-gatestruct yppriv_sym {
867c478bdstevel@tonic-gate    char *sym;
877c478bdstevel@tonic-gate    unsigned len;
887c478bdstevel@tonic-gate};
897c478bdstevel@tonic-gatestatic	char err_fork[] = "ypserv:  %s fork failure.\n";
907c478bdstevel@tonic-gate#define	FORK_ERR logprintf(err_fork, fun)
917c478bdstevel@tonic-gatestatic char err_execl[] = "ypserv:  %s execl failure.\n";
927c478bdstevel@tonic-gate#define	EXEC_ERR logprintf(err_execl, fun)
937c478bdstevel@tonic-gatestatic char err_respond[] = "ypserv: %s can't respond to rpc request.\n";
947c478bdstevel@tonic-gate#define	RESPOND_ERR logprintf(err_respond, fun)
957c478bdstevel@tonic-gatestatic char err_free[] = "ypserv: %s can't free args.\n";
967c478bdstevel@tonic-gate#define	FREE_ERR logprintf(err_free, fun)
977c478bdstevel@tonic-gatestatic char err_map[] = "ypserv: %s no such map or access denied.\n";
987c478bdstevel@tonic-gate#define	MAP_ERR logprintf(err_map, fun)
997c478bdstevel@tonic-gatestatic char err_vers[] = "ypserv: %s version not supported.\n";
1007c478bdstevel@tonic-gate#define	VERS_ERR logprintf(err_vers, fun)
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gatestatic void ypfilter(DBM *fdb, datum *inkey, datum *outkey, datum *val,
1037c478bdstevel@tonic-gate			uint_t *status, bool_t update);
1047c478bdstevel@tonic-gatestatic bool isypsym(datum *key);
1057c478bdstevel@tonic-gatestatic bool xdrypserv_ypall(XDR *xdrs, struct ypreq_nokey *req);
1067c478bdstevel@tonic-gatestatic int multihomed(struct ypreq_key req, struct ypresp_val *resp,
1077c478bdstevel@tonic-gate			SVCXPRT *xprt, DBM *fdb);
1087c478bdstevel@tonic-gatestatic int omultihomed(struct yprequest req, struct ypresponse *resp,
1097c478bdstevel@tonic-gate			SVCXPRT *xprt, DBM *fdb);
1107c478bdstevel@tonic-gate
1117c478bdstevel@tonic-gate
1127c478bdstevel@tonic-gate/* For DNS forwarding */
1137c478bdstevel@tonic-gateextern bool dnsforward;
1147c478bdstevel@tonic-gateextern bool client_setup_failure;
1157c478bdstevel@tonic-gateextern int resolv_pid;
1167c478bdstevel@tonic-gateextern CLIENT *resolv_client;
1177c478bdstevel@tonic-gateextern char *resolv_tp;
1187c478bdstevel@tonic-gate
1197c478bdstevel@tonic-gate/*
1207c478bdstevel@tonic-gate * This determines whether or not a passed domain is served by this
1217c478bdstevel@tonic-gate * server, and returns a boolean.  Used by both old and new protocol
1227c478bdstevel@tonic-gate * versions.
1237c478bdstevel@tonic-gate */
1247c478bdstevel@tonic-gatevoid
1257c478bdstevel@tonic-gateypdomain(SVCXPRT *transp, bool always_respond)
1267c478bdstevel@tonic-gate{
1277c478bdstevel@tonic-gate	char domain_name[YPMAXDOMAIN + 1];
1287c478bdstevel@tonic-gate	char *pdomain_name = domain_name;
1297c478bdstevel@tonic-gate	bool isserved;
1307c478bdstevel@tonic-gate	char *fun = "ypdomain";
1317c478bdstevel@tonic-gate	struct netbuf *nbuf;
1327c478bdstevel@tonic-gate	sa_family_t af;
1337c478bdstevel@tonic-gate
1347c478bdstevel@tonic-gate	memset(domain_name, 0, sizeof (domain_name));
1357c478bdstevel@tonic-gate
1367c478bdstevel@tonic-gate	if (!svc_getargs(transp, (xdrproc_t)xdr_ypdomain_wrap_string,
1377c478bdstevel@tonic-gate				(caddr_t)&pdomain_name)) {
1387c478bdstevel@tonic-gate		svcerr_decode(transp);
1397c478bdstevel@tonic-gate		return;
1407c478bdstevel@tonic-gate	}
1417c478bdstevel@tonic-gate
1427c478bdstevel@tonic-gate	/*
1437c478bdstevel@tonic-gate	 * If the file /var/yp/securenets is present on the server, and if
1447c478bdstevel@tonic-gate	 * the hostname is present in the file, then let the client bind to
1457c478bdstevel@tonic-gate	 * the server.
1467c478bdstevel@tonic-gate	 */
1477c478bdstevel@tonic-gate	nbuf = svc_getrpccaller(transp);
1487c478bdstevel@tonic-gate	af = ((struct sockaddr_storage *)nbuf->buf)->ss_family;
1497c478bdstevel@tonic-gate	if (af != AF_INET && af != AF_INET6) {
1507c478bdstevel@tonic-gate		logprintf("Protocol incorrect\n");
1517c478bdstevel@tonic-gate		return;
1527c478bdstevel@tonic-gate	}
1537c478bdstevel@tonic-gate
1547c478bdstevel@tonic-gate	if (!(check_secure_net_ti(nbuf, fun))) {
1557c478bdstevel@tonic-gate		MAP_ERR;
1567c478bdstevel@tonic-gate		return;
1577c478bdstevel@tonic-gate	}
1587c478bdstevel@tonic-gate
1597c478bdstevel@tonic-gate	isserved = ypcheck_domain(domain_name);
1607c478bdstevel@tonic-gate
1617c478bdstevel@tonic-gate	if (isserved || always_respond) {
1627c478bdstevel@tonic-gate
1637c478bdstevel@tonic-gate		if (!svc_sendreply(transp, xdr_bool, (char *)&isserved)) {
1647c478bdstevel@tonic-gate		    RESPOND_ERR;
1657c478bdstevel@tonic-gate		}
1667c478bdstevel@tonic-gate		if (!isserved)
1677c478bdstevel@tonic-gate			logprintf("Domain %s not supported\n",
1687c478bdstevel@tonic-gate					domain_name);
1697c478bdstevel@tonic-gate
1707c478bdstevel@tonic-gate	} else {
1717c478bdstevel@tonic-gate		/*
1727c478bdstevel@tonic-gate		 * This case is the one in which the domain is not
1737c478bdstevel@tonic-gate		 * supported, and in which we are not to respond in the
1747c478bdstevel@tonic-gate		 * unsupported case.  We are going to make an error happen
17548bbca8Daniel Hoffman		 * to allow the portmapper to end its wait without the
1767c478bdstevel@tonic-gate		 * normal timeout period.  The assumption here is that
1777c478bdstevel@tonic-gate		 * the only process in the world which is using the function
1787c478bdstevel@tonic-gate		 * in its no-answer-if-nack form is the portmapper, which is
1797c478bdstevel@tonic-gate		 * doing the krock for pseudo-broadcast.  If some poor fool
1807c478bdstevel@tonic-gate		 * calls this function as a single-cast message, the nack
1817c478bdstevel@tonic-gate		 * case will look like an incomprehensible error.  Sigh...
1827c478bdstevel@tonic-gate		 * (The traditional Unix disclaimer)
1837c478bdstevel@tonic-gate		 */
1847c478bdstevel@tonic-gate
1857c478bdstevel@tonic-gate		svcerr_decode(transp);
1867c478bdstevel@tonic-gate		logprintf("Domain %s not supported (broadcast)\n",
1877c478bdstevel@tonic-gate				domain_name);
1887c478bdstevel@tonic-gate	}
1897c478bdstevel@tonic-gate}
1907c478bdstevel@tonic-gate
1917c478bdstevel@tonic-gate/*
1927c478bdstevel@tonic-gate * This implements the yp "match" function.
1937c478bdstevel@tonic-gate */
1947c478bdstevel@tonic-gatevoid
1957c478bdstevel@tonic-gateypmatch(SVCXPRT *transp, struct svc_req *rqstp)
1967c478bdstevel@tonic-gate{
1977c478bdstevel@tonic-gate	struct ypreq_key req;
1987c478bdstevel@tonic-gate	struct ypresp_val resp;
1997c478bdstevel@tonic-gate	char *fun = "ypmatch";
2007c478bdstevel@tonic-gate	DBM *fdb;
2017c478bdstevel@tonic-gate
2027c478bdstevel@tonic-gate	memset(&req, 0, sizeof (req));
2037c478bdstevel@tonic-gate	memset(&resp, 0, sizeof (resp));
2047c478bdstevel@tonic-gate	resp.status = (unsigned)YP_NOKEY;
2057c478bdstevel@tonic-gate
2067c478bdstevel@tonic-gate	if (!svc_getargs(transp, (xdrproc_t)xdr_ypreq_key, (char *)&req)) {
2077c478bdstevel@tonic-gate		svcerr_decode(transp);
2087c478bdstevel@tonic-gate		return;
2097c478bdstevel@tonic-gate	}
2107c478bdstevel@tonic-gate
2117c478bdstevel@tonic-gate	/*
2127c478bdstevel@tonic-gate	 * sanity check the map name and to a DBM lookup
2137c478bdstevel@tonic-gate	 * also perform an access check...
2147c478bdstevel@tonic-gate	 */
2157c478bdstevel@tonic-gate	if ((fdb = ypset_current_map(req.map, req.domain,
2167c478bdstevel@tonic-gate					&resp.status)) != NULL &&
2177c478bdstevel@tonic-gate		yp_map_access(transp, &resp.status, fdb)) {
2187c478bdstevel@tonic-gate
2197c478bdstevel@tonic-gate		/* Check with the DBM database */
2207c478bdstevel@tonic-gate		resp.valdat = dbm_fetch(fdb, req.keydat);
2217c478bdstevel@tonic-gate		if (resp.valdat.dptr != NULL) {
2227c478bdstevel@tonic-gate			resp.status = YP_TRUE;
2237c478bdstevel@tonic-gate			if (!silent)
2247c478bdstevel@tonic-gate				printf("%s: dbm: %40.40s\n",
2257c478bdstevel@tonic-gate					fun, resp.valdat.dptr);
2267c478bdstevel@tonic-gate			goto send_reply;
2277c478bdstevel@tonic-gate		}
2287c478bdstevel@tonic-gate
2297c478bdstevel@tonic-gate		/*
2307c478bdstevel@tonic-gate		 * If we're being asked to match YP_SECURE or YP_INTERDOMAIN
2317c478bdstevel@tonic-gate		 * and we haven't found it in the dbm file, then we don't
2327c478bdstevel@tonic-gate		 * really want to waste any more time.  Specifically, we don't
2337c478bdstevel@tonic-gate		 * want to ask DNS
2347c478bdstevel@tonic-gate		 */
2357c478bdstevel@tonic-gate		if (req.keydat.dsize == 0 ||
2367c478bdstevel@tonic-gate		    req.keydat.dptr == NULL ||
2377c478bdstevel@tonic-gate		    req.keydat.dptr[0] == '\0' ||
2387c478bdstevel@tonic-gate	strncmp(req.keydat.dptr, yp_secure, req.keydat.dsize) == 0 ||
2397c478bdstevel@tonic-gate	strncmp(req.keydat.dptr, yp_interdomain, req.keydat.dsize) == 0) {
2407c478bdstevel@tonic-gate			goto send_reply;
2417c478bdstevel@tonic-gate		}
2427c478bdstevel@tonic-gate
2437c478bdstevel@tonic-gate		/* Let's try the YP_MULTI_ hack... */
2447c478bdstevel@tonic-gate#ifdef MINUS_C_OPTION
2457c478bdstevel@tonic-gate		if (multiflag == TRUE && multihomed(req, &resp, transp, fdb))
2467c478bdstevel@tonic-gate			goto send_reply;
2477c478bdstevel@tonic-gate#else
2487c478bdstevel@tonic-gate		if (multihomed(req, &resp, transp, fdb))
2497c478bdstevel@tonic-gate			goto send_reply;
2507c478bdstevel@tonic-gate#endif
2517c478bdstevel@tonic-gate
2527c478bdstevel@tonic-gate		/*
2537c478bdstevel@tonic-gate		 * Let's try DNS, but if client_setup_failure is set,
2547c478bdstevel@tonic-gate		 * we have tried DNS in the past and failed, there is
2557c478bdstevel@tonic-gate		 * no reason in forcing an infinite loop by turning
2567c478bdstevel@tonic-gate		 * off DNS in setup_resolv() only to turn it back on
2577c478bdstevel@tonic-gate		 * again here.
2587c478bdstevel@tonic-gate		 */
2597c478bdstevel@tonic-gate		if (!dnsforward && !client_setup_failure) {
2607c478bdstevel@tonic-gate			datum idkey, idval;
2617c478bdstevel@tonic-gate			idkey.dptr = yp_interdomain;
2627c478bdstevel@tonic-gate			idkey.dsize = yp_interdomain_sz;
2637c478bdstevel@tonic-gate			idval = dbm_fetch(fdb, idkey);
2647c478bdstevel@tonic-gate			if (idval.dptr)
2657c478bdstevel@tonic-gate				dnsforward = TRUE;
2667c478bdstevel@tonic-gate		}
2677c478bdstevel@tonic-gate
2687c478bdstevel@tonic-gate		if (dnsforward) {
2697c478bdstevel@tonic-gate			if (!resolv_pid || !resolv_client) {
2707c478bdstevel@tonic-gate				setup_resolv(&dnsforward, &resolv_pid,
2717c478bdstevel@tonic-gate						&resolv_client, resolv_tp, 0);
2727c478bdstevel@tonic-gate				if (resolv_client == NULL)
2737c478bdstevel@tonic-gate					client_setup_failure = TRUE;
2747c478bdstevel@tonic-gate			}
2757c478bdstevel@tonic-gate
2767c478bdstevel@tonic-gate			if (resolv_req(&dnsforward, &resolv_client,
2777c478bdstevel@tonic-gate						&resolv_pid, resolv_tp,
2787c478bdstevel@tonic-gate						rqstp->rq_xprt, &req,
2797c478bdstevel@tonic-gate						req.map) == TRUE)
2807c478bdstevel@tonic-gate				goto free_args;
2817c478bdstevel@tonic-gate		}
2827c478bdstevel@tonic-gate	}
2837c478bdstevel@tonic-gate	send_reply:
2847c478bdstevel@tonic-gate
2857c478bdstevel@tonic-gate	if (!svc_sendreply(transp, (xdrproc_t)xdr_ypresp_val,
2867c478bdstevel@tonic-gate				(caddr_t)&resp)) {
2877c478bdstevel@tonic-gate		RESPOND_ERR;
2887c478bdstevel@tonic-gate	}
2897c478bdstevel@tonic-gate
2907c478bdstevel@tonic-gate	free_args:
2917c478bdstevel@tonic-gate
2927c478bdstevel@tonic-gate	if (!svc_freeargs(transp, (xdrproc_t)xdr_ypreq_key,
2937c478bdstevel@tonic-gate				(char *)&req)) {
2947c478bdstevel@tonic-gate		FREE_ERR;
2957c478bdstevel@tonic-gate	}
2967c478bdstevel@tonic-gate}
2977c478bdstevel@tonic-gate
2987c478bdstevel@tonic-gate
2997c478bdstevel@tonic-gate/*
3007c478bdstevel@tonic-gate * This implements the yp "get first" function.
3017c478bdstevel@tonic-gate */
3027c478bdstevel@tonic-gatevoid
3037c478bdstevel@tonic-gateypfirst(SVCXPRT *transp)
3047c478bdstevel@tonic-gate{
3057c478bdstevel@tonic-gate	struct ypreq_nokey req;
3067c478bdstevel@tonic-gate	struct ypresp_key_val resp;
3077c478bdstevel@tonic-gate	char *fun = "ypfirst";
3087c478bdstevel@tonic-gate	DBM *fdb;
3097c478bdstevel@tonic-gate
3107c478bdstevel@tonic-gate	memset(&req, 0, sizeof (req));
3117c478bdstevel@tonic-gate	memset(&resp, 0, sizeof (resp));
3127c478bdstevel@tonic-gate
3137c478bdstevel@tonic-gate	if (!svc_getargs(transp,
3147c478bdstevel@tonic-gate				(xdrproc_t)xdr_ypreq_nokey,
3157c478bdstevel@tonic-gate				(char *)&req)) {
3167c478bdstevel@tonic-gate		svcerr_decode(transp);
3177c478bdstevel@tonic-gate		return;
3187c478bdstevel@tonic-gate	}
3197c478bdstevel@tonic-gate
3207c478bdstevel@tonic-gate	if ((fdb = ypset_current_map(req.map, req.domain,
3217c478bdstevel@tonic-gate					&resp.status)) != NULL &&
3227c478bdstevel@tonic-gate		yp_map_access(transp, &resp.status, fdb)) {
3237c478bdstevel@tonic-gate		ypfilter(fdb, NULL,
3247c478bdstevel@tonic-gate			&resp.keydat, &resp.valdat, &resp.status, FALSE);
3257c478bdstevel@tonic-gate	}
3267c478bdstevel@tonic-gate
3277c478bdstevel@tonic-gate	if (!svc_sendreply(transp,
3287c478bdstevel@tonic-gate				(xdrproc_t)xdr_ypresp_key_val,
3297c478bdstevel@tonic-gate				(char *)&resp)) {
3307c478bdstevel@tonic-gate		RESPOND_ERR;
3317c478bdstevel@tonic-gate	}
3327c478bdstevel@tonic-gate
3337c478bdstevel@tonic-gate	if (!svc_freeargs(transp, (xdrproc_t)xdr_ypreq_nokey,
3347c478bdstevel@tonic-gate				(char *)&req)) {
3357c478bdstevel@tonic-gate		FREE_ERR;
3367c478bdstevel@tonic-gate	}
3377c478bdstevel@tonic-gate}
3387c478bdstevel@tonic-gate
3397c478bdstevel@tonic-gate/*
3407c478bdstevel@tonic-gate * This implements the yp "get next" function.
3417c478bdstevel@tonic-gate */
3427c478bdstevel@tonic-gatevoid
3437c478bdstevel@tonic-gateypnext(SVCXPRT *transp)
3447c478bdstevel@tonic-gate{
3457c478bdstevel@tonic-gate	struct ypreq_key req;
3467c478bdstevel@tonic-gate	struct ypresp_key_val resp;
3477c478bdstevel@tonic-gate	char *fun = "ypnext";
3487c478bdstevel@tonic-gate	DBM *fdb;
3497c478bdstevel@tonic-gate
3507c478bdstevel@tonic-gate	memset(&req, 0, sizeof (req));
3517c478bdstevel@tonic-gate	memset(&resp, 0, sizeof (resp));
3527c478bdstevel@tonic-gate
3537c478bdstevel@tonic-gate	if (!svc_getargs(transp, (xdrproc_t)xdr_ypreq_key, (char *)&req)) {
3547c478bdstevel@tonic-gate		svcerr_decode(transp);
3557c478bdstevel@tonic-gate		return;
3567c478bdstevel@tonic-gate	}
3577c478bdstevel@tonic-gate
3587c478bdstevel@tonic-gate	if ((fdb = ypset_current_map(req.map, req.domain,
3597c478bdstevel@tonic-gate					&resp.status)) != NULL &&
3607c478bdstevel@tonic-gate		yp_map_access(transp, &resp.status, fdb)) {
3617c478bdstevel@tonic-gate		ypfilter(fdb, &req.keydat,
3627c478bdstevel@tonic-gate			&resp.keydat, &resp.valdat, &resp.status, FALSE);
3637c478bdstevel@tonic-gate	}
3647c478bdstevel@tonic-gate
3657c478bdstevel@tonic-gate	if (!svc_sendreply(transp,
3667c478bdstevel@tonic-gate				(xdrproc_t)xdr_ypresp_key_val,
3677c478bdstevel@tonic-gate				(char *)&resp)) {
3687c478bdstevel@tonic-gate		RESPOND_ERR;
3697c478bdstevel@tonic-gate	}
3707c478bdstevel@tonic-gate
3717c478bdstevel@tonic-gate	if (!svc_freeargs(transp,
3727c478bdstevel@tonic-gate				(xdrproc_t)xdr_ypreq_key,
3737c478bdstevel@tonic-gate				(char *)&req)) {
3747c478bdstevel@tonic-gate		FREE_ERR;
3757c478bdstevel@tonic-gate	}
3767c478bdstevel@tonic-gate}
3777c478bdstevel@tonic-gate
3787c478bdstevel@tonic-gate/*
3797c478bdstevel@tonic-gate * This implements the "transfer map" function.  It takes the domain
3807c478bdstevel@tonic-gate * and map names and the callback information provided by the
3817c478bdstevel@tonic-gate * requester (yppush on some node), and execs a ypxfr process to do
3827c478bdstevel@tonic-gate * the actual transfer.
3837c478bdstevel@tonic-gate */
3847c478bdstevel@tonic-gatevoid
3857c478bdstevel@tonic-gateypxfr(SVCXPRT *transp, int prog)
3867c478bdstevel@tonic-gate{
3877c478bdstevel@tonic-gate	struct ypreq_newxfr newreq;
3887c478bdstevel@tonic-gate	struct ypreq_xfr oldreq;
3897c478bdstevel@tonic-gate	struct ypresp_val resp;  /* not returned to the caller */
3907c478bdstevel@tonic-gate	char transid[32];
3917c478bdstevel@tonic-gate	char proto[32];
3927c478bdstevel@tonic-gate	char name[256];
3937c478bdstevel@tonic-gate	char *pdomain, *pmap;
3947c478bdstevel@tonic-gate	pid_t pid = -1;
3957c478bdstevel@tonic-gate	char *fun = "ypxfr";
3967c478bdstevel@tonic-gate	DBM *fdb;
3977c478bdstevel@tonic-gate
3987c478bdstevel@tonic-gate	if (prog == YPPROC_NEWXFR) {
3997c478bdstevel@tonic-gate		memset(&newreq, 0, sizeof (newreq));
4007c478bdstevel@tonic-gate		if (!svc_getargs(transp, (xdrproc_t)xdr_ypreq_newxfr,
4017c478bdstevel@tonic-gate				(char *)&newreq)) {
4027c478bdstevel@tonic-gate			svcerr_decode(transp);
4037c478bdstevel@tonic-gate			return;
4047c478bdstevel@tonic-gate		}
4057c478bdstevel@tonic-gate
4067c478bdstevel@tonic-gate#ifdef OPCOM_DEBUG
4077c478bdstevel@tonic-gate		fprintf(stderr, "newreq:\n"
4087c478bdstevel@tonic-gate			"\tmap_parms:\n"
4097c478bdstevel@tonic-gate			"\t\tdomain:    %s\n"
4107c478bdstevel@tonic-gate			"\t\tmap:       %s\n"
4117c478bdstevel@tonic-gate			"\t\tordernum:  %u\n"
4127c478bdstevel@tonic-gate			"\t\towner:     %s\n"
4137c478bdstevel@tonic-gate			"\ttransid:    %u\n"
4147c478bdstevel@tonic-gate			"\tproto:      %u\n"
4157c478bdstevel@tonic-gate			"\tname:       %s\n\n",
4167c478bdstevel@tonic-gate			newreq.map_parms.domain,
4177c478bdstevel@tonic-gate			newreq.map_parms.map,
4187c478bdstevel@tonic-gate			newreq.map_parms.ordernum,
4197c478bdstevel@tonic-gate			newreq.map_parms.owner,
4207c478bdstevel@tonic-gate			newreq.transid,
4217c478bdstevel@tonic-gate			newreq.proto,
4227c478bdstevel@tonic-gate			newreq.name);
4237c478bdstevel@tonic-gate#endif
4247c478bdstevel@tonic-gate		sprintf(transid, "%u", newreq.transid);
4257c478bdstevel@tonic-gate		sprintf(proto, "%u", newreq.proto);
4267c478bdstevel@tonic-gate		sprintf(name, "%s", newreq.ypxfr_owner);
4277c478bdstevel@tonic-gate		pdomain = newreq.ypxfr_domain;
4287c478bdstevel@tonic-gate		pmap = newreq.ypxfr_map;
4297c478bdstevel@tonic-gate	} else if (prog == YPPROC_XFR) {
4307c478bdstevel@tonic-gate		memset(&oldreq, 0, sizeof (oldreq));
4317c478bdstevel@tonic-gate		if (!svc_getargs(transp,
4327c478bdstevel@tonic-gate					(xdrproc_t)xdr_ypreq_xfr,
4337c478bdstevel@tonic-gate					(char *)&oldreq)) {
4347c478bdstevel@tonic-gate		    svcerr_decode(transp);
4357c478bdstevel@tonic-gate		    return;
4367c478bdstevel@tonic-gate		}
4377c478bdstevel@tonic-gate
4387c478bdstevel@tonic-gate#ifdef OPCOM_DEBUG
4397c478bdstevel@tonic-gate		fprintf(stderr, "oldreq:\n"
4407c478bdstevel@tonic-gate			"\tmap_parms:\n"
4417c478bdstevel@tonic-gate			"\t\tdomain:    %s\n"
4427c478bdstevel@tonic-gate			"\t\tmap:       %s\n"
4437c478bdstevel@tonic-gate			"\t\tordernum:  %u\n"
4447c478bdstevel@tonic-gate			"\t\towner:     %s\n"
4457c478bdstevel@tonic-gate			"\ttransid:    %u\n"
4467c478bdstevel@tonic-gate			"\tproto:      %u\n"
4477c478bdstevel@tonic-gate			"\tport:       %u\n\n",
4487c478bdstevel@tonic-gate			oldreq.map_parms.domain,
4497c478bdstevel@tonic-gate			oldreq.map_parms.map,
4507c478bdstevel@tonic-gate			oldreq.map_parms.ordernum,
4517c478bdstevel@tonic-gate			oldreq.map_parms.owner,
4527c478bdstevel@tonic-gate			oldreq.transid,
4537c478bdstevel@tonic-gate			oldreq.proto,
4547c478bdstevel@tonic-gate			oldreq.port);
4557c478bdstevel@tonic-gate#endif
4567c478bdstevel@tonic-gate
4577c478bdstevel@tonic-gate		sprintf(transid, "%u", oldreq.transid);
4587c478bdstevel@tonic-gate		sprintf(proto, "%u", oldreq.proto);
4597c478bdstevel@tonic-gate		sprintf(name, "%s", oldreq.ypxfr_owner);
4607c478bdstevel@tonic-gate		pdomain = oldreq.ypxfr_domain;
4617c478bdstevel@tonic-gate		pmap = oldreq.ypxfr_map;
4627c478bdstevel@tonic-gate	} else {
4637c478bdstevel@tonic-gate		VERS_ERR;
4647c478bdstevel@tonic-gate	}
4657c478bdstevel@tonic-gate
4667c478bdstevel@tonic-gate	/* Check that the map exists and is accessible */
4677c478bdstevel@tonic-gate	if ((fdb = ypset_current_map(pmap, pdomain, &resp.status)) != NULL &&
4687c478bdstevel@tonic-gate		yp_map_access(transp, &resp.status, fdb)) {
4697c478bdstevel@tonic-gate
4707c478bdstevel@tonic-gate		pid = vfork();
4717c478bdstevel@tonic-gate		if (pid == -1) {
4727c478bdstevel@tonic-gate			FORK_ERR;
4737c478bdstevel@tonic-gate		} else if (pid == 0) {
4747c478bdstevel@tonic-gate		    if (prog == YPPROC_NEWXFR || prog == YPPROC_XFR) {
4757c478bdstevel@tonic-gate#ifdef OPCOM_DEBUG
4767c478bdstevel@tonic-gate			fprintf(stderr,
4777c478bdstevel@tonic-gate				"EXECL: %s, -d, %s, -C, %s, %s, %s, %s\n",
4787c478bdstevel@tonic-gate				ypxfr_proc, pdomain,
4797c478bdstevel@tonic-gate				transid, proto, name, pmap);
4807c478bdstevel@tonic-gate#endif
4817c478bdstevel@tonic-gate			if (execl(ypxfr_proc, "ypxfr", "-d",
4827c478bdstevel@tonic-gate					pdomain, "-C", transid, proto,
4837c478bdstevel@tonic-gate					name, pmap, NULL))
4847c478bdstevel@tonic-gate			    EXEC_ERR;
4857c478bdstevel@tonic-gate		    } else {
4867c478bdstevel@tonic-gate			VERS_ERR;
4877c478bdstevel@tonic-gate		    }
4887c478bdstevel@tonic-gate		    _exit(1);
4897c478bdstevel@tonic-gate		}
4907c478bdstevel@tonic-gate
4917c478bdstevel@tonic-gate	} else {
4927c478bdstevel@tonic-gate		MAP_ERR;
4937c478bdstevel@tonic-gate	}
4947c478bdstevel@tonic-gate	if (!svc_sendreply(transp, xdr_void, 0)) {
4957c478bdstevel@tonic-gate		RESPOND_ERR;
4967c478bdstevel@tonic-gate	}
4977c478bdstevel@tonic-gate
4987c478bdstevel@tonic-gate	if (prog == YPPROC_NEWXFR) {
4997c478bdstevel@tonic-gate		if (!svc_freeargs(transp,
5007c478bdstevel@tonic-gate					(xdrproc_t)xdr_ypreq_newxfr,
5017c478bdstevel@tonic-gate					(char *)&newreq)) {
5027c478bdstevel@tonic-gate		    FREE_ERR;
5037c478bdstevel@tonic-gate		}
5047c478bdstevel@tonic-gate	}
5057c478bdstevel@tonic-gate}
5067c478bdstevel@tonic-gate
5077c478bdstevel@tonic-gate/*
5087c478bdstevel@tonic-gate * This implements the "get all" function.
5097c478bdstevel@tonic-gate */
5107c478bdstevel@tonic-gatevoid
5117c478bdstevel@tonic-gateypall(SVCXPRT *transp)
5127c478bdstevel@tonic-gate{
5137c478bdstevel@tonic-gate	struct ypreq_nokey req;
5147c478bdstevel@tonic-gate	struct ypresp_val resp;  /* not returned to the caller */
5157c478bdstevel@tonic-gate	pid_t pid;
5167c478bdstevel@tonic-gate	char *fun = "ypall";
5177c478bdstevel@tonic-gate	DBM *fdb;
5187c478bdstevel@tonic-gate
5197c478bdstevel@tonic-gate	req.domain = req.map = NULL;
5207c478bdstevel@tonic-gate
5217c478bdstevel@tonic-gate	memset((char *)&req, 0, sizeof (req));
5227c478bdstevel@tonic-gate
5237c478bdstevel@tonic-gate	if (!svc_getargs(transp,
5247c478bdstevel@tonic-gate				(xdrproc_t)xdr_ypreq_nokey,
5257c478bdstevel@tonic-gate				(char *)&req)) {
5267c478bdstevel@tonic-gate		svcerr_decode(transp);
5277c478bdstevel@tonic-gate		return;
5287c478bdstevel@tonic-gate	}
5297c478bdstevel@tonic-gate
5307c478bdstevel@tonic-gate	pid = fork1();
5317c478bdstevel@tonic-gate
5327c478bdstevel@tonic-gate	if (pid) {
5337c478bdstevel@tonic-gate
5347c478bdstevel@tonic-gate		if (pid == -1) {
5357c478bdstevel@tonic-gate			FORK_ERR;
5367c478bdstevel@tonic-gate		}
5377c478bdstevel@tonic-gate
5387c478bdstevel@tonic-gate		if (!svc_freeargs(transp,
5397c478bdstevel@tonic-gate					(xdrproc_t)xdr_ypreq_nokey,
5407c478bdstevel@tonic-gate					(char *)&req)) {
5417c478bdstevel@tonic-gate			FREE_ERR;
5427c478bdstevel@tonic-gate		}
5437c478bdstevel@tonic-gate
5447c478bdstevel@tonic-gate		return;
5457c478bdstevel@tonic-gate	}
5467c478bdstevel@tonic-gate
5477c478bdstevel@tonic-gate	/*
5487c478bdstevel@tonic-gate	 * access control hack:  If denied then invalidate the map name.
5497c478bdstevel@tonic-gate	 */
5507c478bdstevel@tonic-gate	ypclr_current_map();
5517c478bdstevel@tonic-gate	if ((fdb = ypset_current_map(req.map,
5527c478bdstevel@tonic-gate		req.domain, &resp.status)) != NULL &&
5537c478bdstevel@tonic-gate		!yp_map_access(transp, &resp.status, fdb)) {
5547c478bdstevel@tonic-gate
5557c478bdstevel@tonic-gate		req.map[0] = '-';
5567c478bdstevel@tonic-gate	}
5577c478bdstevel@tonic-gate
5587c478bdstevel@tonic-gate	/*
5597c478bdstevel@tonic-gate	 * This is the child process.  The work gets done by xdrypserv_ypall/
5607c478bdstevel@tonic-gate	 * we must clear the "current map" first so that we do not
5617c478bdstevel@tonic-gate	 * share a seek pointer with the parent server.
5627c478bdstevel@tonic-gate	 */
5637c478bdstevel@tonic-gate
5647c478bdstevel@tonic-gate	if (!svc_sendreply(transp,
5657c478bdstevel@tonic-gate				(xdrproc_t)xdrypserv_ypall,
5667c478bdstevel@tonic-gate				(char *)&req)) {
5677c478bdstevel@tonic-gate		RESPOND_ERR;
5687c478bdstevel@tonic-gate	}
5697c478bdstevel@tonic-gate
5707c478bdstevel@tonic-gate	if (!svc_freeargs(transp,
5717c478bdstevel@tonic-gate				(xdrproc_t)xdr_ypreq_nokey,
5727c478bdstevel@tonic-gate				(char *)&req)) {
5737c478bdstevel@tonic-gate		FREE_ERR;
5747c478bdstevel@tonic-gate	}
5757c478bdstevel@tonic-gate
5767c478bdstevel@tonic-gate	/*
5777c478bdstevel@tonic-gate	 * In yptol mode we may start a cache update thread within a child
5787c478bdstevel@tonic-gate	 * process. It is thus important that child processes do not exit,
5797c478bdstevel@tonic-gate	 * killing any such threads, before the thread has completed.
5807c478bdstevel@tonic-gate	 */
5817c478bdstevel@tonic-gate	if (yptol_mode) {
5827c478bdstevel@tonic-gate		thr_join(0, NULL, NULL);
5837c478bdstevel@tonic-gate	}
5847c478bdstevel@tonic-gate
5857c478bdstevel@tonic-gate	exit(0);
5867c478bdstevel@tonic-gate}
5877c478bdstevel@tonic-gate
5887c478bdstevel@tonic-gate/*
5897c478bdstevel@tonic-gate * This implements the "get master name" function.
5907c478bdstevel@tonic-gate */
5917c478bdstevel@tonic-gatevoid
5927c478bdstevel@tonic-gateypmaster(SVCXPRT *transp)
5937c478bdstevel@tonic-gate{
5947c478bdstevel@tonic-gate	struct ypreq_nokey req;
5957c478bdstevel@tonic-gate	struct ypresp_master resp;
5967c478bdstevel@tonic-gate	char *nullstring = "";
5977c478bdstevel@tonic-gate	char *fun = "ypmaster";
5987c478bdstevel@tonic-gate	DBM *fdb;
5997c478bdstevel@tonic-gate
6007c478bdstevel@tonic-gate	memset((char *)&req, 0, sizeof (req));
6017c478bdstevel@tonic-gate	resp.master = nullstring;
6027c478bdstevel@tonic-gate	resp.status = YP_TRUE;
6037c478bdstevel@tonic-gate
6047c478bdstevel@tonic-gate	if (!svc_getargs(transp,
6057c478bdstevel@tonic-gate				(xdrproc_t)xdr_ypreq_nokey,
6067c478bdstevel@tonic-gate				(char *)&req)) {
6077c478bdstevel@tonic-gate		svcerr_decode(transp);
6087c478bdstevel@tonic-gate		return;
6097c478bdstevel@tonic-gate	}
6107c478bdstevel@tonic-gate
6117c478bdstevel@tonic-gate	if ((fdb = ypset_current_map(req.map,
6127c478bdstevel@tonic-gate		req.domain, &resp.status)) != NULL &&
6137c478bdstevel@tonic-gate		yp_map_access(transp, &resp.status, fdb)) {
6147c478bdstevel@tonic-gate
6157c478bdstevel@tonic-gate		if (!ypget_map_master(&resp.master, fdb)) {
6167c478bdstevel@tonic-gate			resp.status = (unsigned)YP_BADDB;
6177c478bdstevel@tonic-gate		}
6187c478bdstevel@tonic-gate	}
6197c478bdstevel@tonic-gate
6207c478bdstevel@tonic-gate	if (!svc_sendreply(transp,
6217c478bdstevel@tonic-gate				(xdrproc_t)xdr_ypresp_master,
6227c478bdstevel@tonic-gate				(char *)&resp)) {
6237c478bdstevel@tonic-gate		RESPOND_ERR;
6247c478bdstevel@tonic-gate	}
6257c478bdstevel@tonic-gate
6267c478bdstevel@tonic-gate	if (!svc_freeargs(transp,
6277c478bdstevel@tonic-gate				(xdrproc_t)xdr_ypreq_nokey,
6287c478bdstevel@tonic-gate				(char *)&req)) {
6297c478bdstevel@tonic-gate		FREE_ERR;
6307c478bdstevel@tonic-gate	}
6317c478bdstevel@tonic-gate}
6327c478bdstevel@tonic-gate
6337c478bdstevel@tonic-gate/*
6347c478bdstevel@tonic-gate * This implements the "get order number" function.
6357c478bdstevel@tonic-gate */
6367c478bdstevel@tonic-gatevoid
6377c478bdstevel@tonic-gateyporder(SVCXPRT *transp)
6387c478bdstevel@tonic-gate{
6397c478bdstevel@tonic-gate	struct ypreq_nokey req;
6407c478bdstevel@tonic-gate	struct ypresp_order resp;
6417c478bdstevel@tonic-gate	char *fun = "yporder";
6427c478bdstevel@tonic-gate	DBM *fdb;
6437c478bdstevel@tonic-gate
6447c478bdstevel@tonic-gate	req.domain = req.map = NULL;
6457c478bdstevel@tonic-gate	resp.status  = YP_TRUE;
6467c478bdstevel@tonic-gate	resp.ordernum  = 0;
6477c478bdstevel@tonic-gate
6487c478bdstevel@tonic-gate	memset((char *)&req, 0, sizeof (req));
6497c478bdstevel@tonic-gate
6507c478bdstevel@tonic-gate	if (!svc_getargs(transp,
6517c478bdstevel@tonic-gate				(xdrproc_t)xdr_ypreq_nokey,
6527c478bdstevel@tonic-gate				(char *)&req)) {
6537c478bdstevel@tonic-gate		svcerr_decode(transp);
6547c478bdstevel@tonic-gate		return;
6557c478bdstevel@tonic-gate	}
6567c478bdstevel@tonic-gate
6577c478bdstevel@tonic-gate	resp.ordernum = 0;
6587c478bdstevel@tonic-gate
6597c478bdstevel@tonic-gate	if ((fdb = ypset_current_map(req.map,
6607c478bdstevel@tonic-gate					req.domain,
6617c478bdstevel@tonic-gate					&resp.status)) != NULL &&
6627c478bdstevel@tonic-gate		yp_map_access(transp, &resp.status, fdb)) {
6637c478bdstevel@tonic-gate
6647c478bdstevel@tonic-gate		if (!ypget_map_order(req.map, req.domain, &resp.ordernum)) {
6657c478bdstevel@tonic-gate			resp.status = (unsigned)YP_BADDB;
6667c478bdstevel@tonic-gate		}
6677c478bdstevel@tonic-gate	}
6687c478bdstevel@tonic-gate
6697c478bdstevel@tonic-gate	if (!svc_sendreply(transp,
6707c478bdstevel@tonic-gate				(xdrproc_t)xdr_ypresp_order,
6717c478bdstevel@tonic-gate				(char *)&resp)) {
6727c478bdstevel@tonic-gate		RESPOND_ERR;
6737c478bdstevel@tonic-gate	}
6747c478bdstevel@tonic-gate
6757c478bdstevel@tonic-gate	if (!svc_freeargs(transp,
6767c478bdstevel@tonic-gate				(xdrproc_t)xdr_ypreq_nokey,
6777c478bdstevel@tonic-gate				(char *)&req)) {
6787c478bdstevel@tonic-gate		FREE_ERR;
6797c478bdstevel@tonic-gate	}
6807c478bdstevel@tonic-gate}
6817c478bdstevel@tonic-gate
6827c478bdstevel@tonic-gatevoid
6837c478bdstevel@tonic-gateypmaplist(SVCXPRT *transp)
6847c478bdstevel@tonic-gate{
6857c478bdstevel@tonic-gate	char domain_name[YPMAXDOMAIN + 1];
6867c478bdstevel@tonic-gate	char *pdomain = domain_name;
6877c478bdstevel@tonic-gate	char *fun = "ypmaplist";
6887c478bdstevel@tonic-gate	struct ypresp_maplist maplist;
6897c478bdstevel@tonic-gate	struct ypmaplist *tmp;
6907c478bdstevel@tonic-gate
6917c478bdstevel@tonic-gate	maplist.list = (struct ypmaplist *)NULL;
6927c478bdstevel@tonic-gate
6937c478bdstevel@tonic-gate	memset(domain_name, 0, sizeof (domain_name));
6947c478bdstevel@tonic-gate
6957c478bdstevel@tonic-gate	if (!svc_getargs(transp,
6967c478bdstevel@tonic-gate				(xdrproc_t)xdr_ypdomain_wrap_string,
6977c478bdstevel@tonic-gate				(caddr_t)&pdomain)) {
6987c478bdstevel@tonic-gate		svcerr_decode(transp);
6997c478bdstevel@tonic-gate		return;
7007c478bdstevel@tonic-gate	}
7017c478bdstevel@tonic-gate
7027c478bdstevel@tonic-gate	maplist.status = yplist_maps(domain_name, &maplist.list);
7037c478bdstevel@tonic-gate
7047c478bdstevel@tonic-gate	if (!svc_sendreply(transp,
7057c478bdstevel@tonic-gate				(xdrproc_t)xdr_ypresp_maplist,
7067c478bdstevel@tonic-gate				(char *)&maplist)) {
7077c478bdstevel@tonic-gate		RESPOND_ERR;
7087c478bdstevel@tonic-gate	}
7097c478bdstevel@tonic-gate
7107c478bdstevel@tonic-gate	while (maplist.list) {
7117c478bdstevel@tonic-gate		tmp = maplist.list->ypml_next;
7127c478bdstevel@tonic-gate		free((char *)maplist.list);
7137c478bdstevel@tonic-gate		maplist.list = tmp;
7147c478bdstevel@tonic-gate	}
7157c478bdstevel@tonic-gate}
7167c478bdstevel@tonic-gate
7177c478bdstevel@tonic-gate/*
7187c478bdstevel@tonic-gate * Ancillary functions used by the top-level functions within this
7197c478bdstevel@tonic-gate * module
7207c478bdstevel@tonic-gate */
7217c478bdstevel@tonic-gate
7227c478bdstevel@tonic-gate/*
7237c478bdstevel@tonic-gate * This returns TRUE if a given key is a yp-private symbol, otherwise
7247c478bdstevel@tonic-gate * FALSE
7257c478bdstevel@tonic-gate */
7267c478bdstevel@tonic-gatestatic bool
7277c478bdstevel@tonic-gateisypsym(datum *key)
7287c478bdstevel@tonic-gate{
7297c478bdstevel@tonic-gate	if ((key->dptr == NULL) ||
7307c478bdstevel@tonic-gate		(key->dsize < yp_prefix_sz) ||
7317c478bdstevel@tonic-gate		memcmp(yp_prefix, key->dptr, yp_prefix_sz) ||
7327c478bdstevel@tonic-gate		(!memcmp(key->dptr, "YP_MULTI_", 9))) {
7337c478bdstevel@tonic-gate		return (FALSE);
7347c478bdstevel@tonic-gate	}
7357c478bdstevel@tonic-gate	return (TRUE);
7367c478bdstevel@tonic-gate}
7377c478bdstevel@tonic-gate
7387c478bdstevel@tonic-gate/*
7397c478bdstevel@tonic-gate * This provides private-symbol filtration for the enumeration functions.
7407c478bdstevel@tonic-gate */
7417c478bdstevel@tonic-gatestatic void
7427c478bdstevel@tonic-gateypfilter(DBM *fdb, datum *inkey, datum *outkey, datum *val, uint_t *status,
7437c478bdstevel@tonic-gate							bool_t update)
7447c478bdstevel@tonic-gate{
7457c478bdstevel@tonic-gate	datum k;
7467c478bdstevel@tonic-gate
7477c478bdstevel@tonic-gate	if (inkey) {
7487c478bdstevel@tonic-gate
7497c478bdstevel@tonic-gate		if (isypsym(inkey)) {
7507c478bdstevel@tonic-gate			*status = (unsigned)YP_BADARGS;
7517c478bdstevel@tonic-gate			return;
7527c478bdstevel@tonic-gate		}
7537c478bdstevel@tonic-gate
7547c478bdstevel@tonic-gate		k = dbm_do_nextkey(fdb, *inkey);
7557c478bdstevel@tonic-gate	} else {
7567c478bdstevel@tonic-gate		k = dbm_firstkey(fdb);
7577c478bdstevel@tonic-gate	}
7587c478bdstevel@tonic-gate
7597c478bdstevel@tonic-gate	while (k.dptr && isypsym(&k)) {
7607c478bdstevel@tonic-gate		k = dbm_nextkey(fdb);
7617c478bdstevel@tonic-gate	}
7627c478bdstevel@tonic-gate
7637c478bdstevel@tonic-gate	if (k.dptr == NULL) {
7647c478bdstevel@tonic-gate		*status = YP_NOMORE;
7657c478bdstevel@tonic-gate		return;
7667c478bdstevel@tonic-gate	}
7677c478bdstevel@tonic-gate
7687c478bdstevel@tonic-gate	*outkey = k;
7697c478bdstevel@tonic-gate
7707c478bdstevel@tonic-gate	/*
7717c478bdstevel@tonic-gate	 * In N2L mode we must call a version of dbm_fetch() that either does
7727c478bdstevel@tonic-gate	 * or does not check for entry updates. In non N2L mode both of these
7737c478bdstevel@tonic-gate	 * will end up doing a normal dbm_fetch().
7747c478bdstevel@tonic-gate	 */
7757c478bdstevel@tonic-gate	if (update)
7767c478bdstevel@tonic-gate		*val = shim_dbm_fetch(fdb, k);
7777c478bdstevel@tonic-gate	else
7787c478bdstevel@tonic-gate		*val = shim_dbm_fetch_noupdate(fdb, k);
7797c478bdstevel@tonic-gate
7807c478bdstevel@tonic-gate	if (val->dptr != NULL) {
7817c478bdstevel@tonic-gate		*status = YP_TRUE;
7827c478bdstevel@tonic-gate	} else {
7837c478bdstevel@tonic-gate		*status = (unsigned)YP_BADDB;
7847c478bdstevel@tonic-gate	}
7857c478bdstevel@tonic-gate}
7867c478bdstevel@tonic-gate
7877c478bdstevel@tonic-gate/*
7887c478bdstevel@tonic-gate * Serializes a stream of struct ypresp_key_val's.  This is used
7897c478bdstevel@tonic-gate * only by the ypserv side of the transaction.
7907c478bdstevel@tonic-gate */
7917c478bdstevel@tonic-gatestatic bool
7927c478bdstevel@tonic-gatexdrypserv_ypall(XDR *xdrs, struct ypreq_nokey *req)
7937c478bdstevel@tonic-gate{
7947c478bdstevel@tonic-gate	bool_t more = TRUE;
7957c478bdstevel@tonic-gate	struct ypresp_key_val resp;
7967c478bdstevel@tonic-gate	DBM *fdb;
7977c478bdstevel@tonic-gate
7987c478bdstevel@tonic-gate	resp.keydat.dptr = resp.valdat.dptr = (char *)NULL;
7997c478bdstevel@tonic-gate	resp.keydat.dsize = resp.valdat.dsize = 0;
8007c478bdstevel@tonic-gate
8017c478bdstevel@tonic-gate	if ((fdb = ypset_current_map(req->map, req->domain,
8027c478bdstevel@tonic-gate					&resp.status)) != NULL) {
8037c478bdstevel@tonic-gate		ypfilter(fdb, (datum *) NULL, &resp.keydat, &resp.valdat,
8047c478bdstevel@tonic-gate				&resp.status, FALSE);
8057c478bdstevel@tonic-gate
8067c478bdstevel@tonic-gate		while (resp.status == YP_TRUE) {
8077c478bdstevel@tonic-gate			if (!xdr_bool(xdrs, &more)) {
8087c478bdstevel@tonic-gate				return (FALSE);
8097c478bdstevel@tonic-gate			}
8107c478bdstevel@tonic-gate
8117c478bdstevel@tonic-gate			if (!xdr_ypresp_key_val(xdrs, &resp)) {
8127c478bdstevel@tonic-gate				return (FALSE);
8137c478bdstevel@tonic-gate			}
8147c478bdstevel@tonic-gate
8157c478bdstevel@tonic-gate			ypfilter(fdb, &resp.keydat, &resp.keydat, &resp.valdat,
8167c478bdstevel@tonic-gate					&resp.status, FALSE);
8177c478bdstevel@tonic-gate		}
8187c478bdstevel@tonic-gate	}
8197c478bdstevel@tonic-gate
8207c478bdstevel@tonic-gate	if (!xdr_bool(xdrs, &more)) {
8217c478bdstevel@tonic-gate		return (FALSE);
8227c478bdstevel@tonic-gate	}
8237c478bdstevel@tonic-gate
8247c478bdstevel@tonic-gate	if (!xdr_ypresp_key_val(xdrs, &resp)) {
8257c478bdstevel@tonic-gate		return (FALSE);
8267c478bdstevel@tonic-gate	}
8277c478bdstevel@tonic-gate
8287c478bdstevel@tonic-gate	more = FALSE;
8297c478bdstevel@tonic-gate
8307c478bdstevel@tonic-gate	if (!xdr_bool(xdrs, &more)) {
8317c478bdstevel@tonic-gate		return (FALSE);
8327c478bdstevel@tonic-gate	}
8337c478bdstevel@tonic-gate
8347c478bdstevel@tonic-gate	return (TRUE);
8357c478bdstevel@tonic-gate}
8367c478bdstevel@tonic-gate
8377c478bdstevel@tonic-gate/*
8387c478bdstevel@tonic-gate * Additions for sparc cluster support
8397c478bdstevel@tonic-gate */
8407c478bdstevel@tonic-gate
8417c478bdstevel@tonic-gate/*
8427c478bdstevel@tonic-gate * Check for special multihomed host cookie in the key.  If there,
8437c478bdstevel@tonic-gate * collect the addresses from the comma separated list and return
8447c478bdstevel@tonic-gate * the one that's nearest the client.
8457c478bdstevel@tonic-gate */
8467c478bdstevel@tonic-gatestatic int
8477c478bdstevel@tonic-gatemultihomed(struct ypreq_key req, struct ypresp_val *resp,
8487c478bdstevel@tonic-gate		SVCXPRT *xprt, DBM *fdb)
8497c478bdstevel@tonic-gate{
8507c478bdstevel@tonic-gate	char *cp, *bp;
8517c478bdstevel@tonic-gate	ulong_t bestaddr, call_addr;
8527c478bdstevel@tonic-gate	struct netbuf *nbuf;
8537c478bdstevel@tonic-gate	char name[PATH_MAX];
8547c478bdstevel@tonic-gate	static char localbuf[_PBLKSIZ];	/* buffer for multihomed IPv6 addr */
8557c478bdstevel@tonic-gate
8567c478bdstevel@tonic-gate	if (strcmp(req.map, "hosts.byname") &&
8577c478bdstevel@tonic-gate			strcmp(req.map, "ipnodes.byname"))
8587c478bdstevel@tonic-gate		/* default status is YP_NOKEY */
8597c478bdstevel@tonic-gate		return (0);
8607c478bdstevel@tonic-gate
8617c478bdstevel@tonic-gate	if (strncmp(req.keydat.dptr, "YP_MULTI_", 9)) {
8627c478bdstevel@tonic-gate		datum tmpname;
8637c478bdstevel@tonic-gate
8647c478bdstevel@tonic-gate		strncpy(name, "YP_MULTI_", 9);
8657c478bdstevel@tonic-gate		strncpy(name + 9, req.keydat.dptr, req.keydat.dsize);
8667c478bdstevel@tonic-gate		tmpname.dsize = req.keydat.dsize + 9;
8677c478bdstevel@tonic-gate		tmpname.dptr = name;
8687c478bdstevel@tonic-gate		resp->valdat = dbm_fetch(fdb, tmpname);
8697c478bdstevel@tonic-gate	} else {
8707c478bdstevel@tonic-gate		/*
8717c478bdstevel@tonic-gate		 * Return whole line (for debugging) if YP_MULTI_hostnam
8727c478bdstevel@tonic-gate		 * is specified.
8737c478bdstevel@tonic-gate		 */
8747c478bdstevel@tonic-gate		resp->valdat = dbm_fetch(fdb, req.keydat);
8757c478bdstevel@tonic-gate		if (resp->valdat.dptr != NULL)
8767c478bdstevel@tonic-gate			return (1);
8777c478bdstevel@tonic-gate	}
8787c478bdstevel@tonic-gate
8797c478bdstevel@tonic-gate	if (resp->valdat.dptr == NULL)
8807c478bdstevel@tonic-gate		return (0);
8817c478bdstevel@tonic-gate
8827c478bdstevel@tonic-gate	strncpy(name, req.keydat.dptr, req.keydat.dsize);
8837c478bdstevel@tonic-gate	name[req.keydat.dsize] = NULL;
8847c478bdstevel@tonic-gate
8857c478bdstevel@tonic-gate	if (strcmp(req.map, "ipnodes.byname") == 0) {
8867c478bdstevel@tonic-gate		/*
8877c478bdstevel@tonic-gate		 * This section handles multihomed IPv6 addresses.
8887c478bdstevel@tonic-gate		 * It returns all the IPv6 addresses one per line and only
8897c478bdstevel@tonic-gate		 * the requested hostname is returned.  NO aliases will be
8907c478bdstevel@tonic-gate		 * returned.  This is done exactly the same way DNS forwarding
8917c478bdstevel@tonic-gate		 * daemon handles multihomed hosts.
8927c478bdstevel@tonic-gate		 * New IPv6 enabled clients should be able to handle this
8937c478bdstevel@tonic-gate		 * information returned.  The sorting is also the client's
8947c478bdstevel@tonic-gate		 * responsibility.
8957c478bdstevel@tonic-gate		 */
8967c478bdstevel@tonic-gate
8977c478bdstevel@tonic-gate		char *buf, *endbuf;
8987c478bdstevel@tonic-gate
8997c478bdstevel@tonic-gate		if ((buf = strdup(resp->valdat.dptr)) == NULL) /* no memory */
9007c478bdstevel@tonic-gate			return (0);
9017c478bdstevel@tonic-gate		if ((bp = strtok(buf, " \t")) == NULL) { /* no address field */
9027c478bdstevel@tonic-gate			free(buf);
9037c478bdstevel@tonic-gate			return (0);
9047c478bdstevel@tonic-gate		}
9057c478bdstevel@tonic-gate		if ((cp = strtok(NULL, "")) == NULL) { /* no host field */
9067c478bdstevel@tonic-gate			free(buf);
9077c478bdstevel@tonic-gate			return (0);
9087c478bdstevel@tonic-gate		}
9097c478bdstevel@tonic-gate		if ((cp = strtok(bp, ",")) != NULL) { /* multihomed host */
9107c478bdstevel@tonic-gate			int bsize;
9117c478bdstevel@tonic-gate
9127c478bdstevel@tonic-gate			localbuf[0] = '\0';
9137c478bdstevel@tonic-gate			bsize = sizeof (localbuf);
9147c478bdstevel@tonic-gate			endbuf = localbuf;
9157c478bdstevel@tonic-gate
9167c478bdstevel@tonic-gate			while (cp) {
9177c478bdstevel@tonic-gate				if ((strlen(cp) + strlen(name)) >= bsize) {
9187c478bdstevel@tonic-gate					/* out of range */
9197c478bdstevel@tonic-gate					break;
9207c478bdstevel@tonic-gate				}
9217c478bdstevel@tonic-gate				sprintf(endbuf, "%s %s\n", cp, name);
9227c478bdstevel@tonic-gate				cp = strtok(NULL, ",");
9237c478bdstevel@tonic-gate				endbuf = &endbuf[strlen(endbuf)];
9247c478bdstevel@tonic-gate				bsize = &localbuf[sizeof (localbuf)] - endbuf;
9257c478bdstevel@tonic-gate			}
9267c478bdstevel@tonic-gate			resp->valdat.dptr = localbuf;
9277c478bdstevel@tonic-gate			resp->valdat.dsize = strlen(localbuf);
9287c478bdstevel@tonic-gate		}
9297c478bdstevel@tonic-gate
9307c478bdstevel@tonic-gate		free(buf);
9317c478bdstevel@tonic-gate		/* remove trailing newline */
9327c478bdstevel@tonic-gate		if (resp->valdat.dsize &&
9337c478bdstevel@tonic-gate			resp->valdat.dptr[resp->valdat.dsize-1] == '\n') {
9347c478bdstevel@tonic-gate			resp->valdat.dptr[resp->valdat.dsize-1] = '\0';
9357c478bdstevel@tonic-gate			resp->valdat.dsize -= 1;
9367c478bdstevel@tonic-gate		}
9377c478bdstevel@tonic-gate
9387c478bdstevel@tonic-gate		resp->status = YP_TRUE;
9397c478bdstevel@tonic-gate		return (1);
9407c478bdstevel@tonic-gate	}
9417c478bdstevel@tonic-gate	nbuf = svc_getrpccaller(xprt);
9427c478bdstevel@tonic-gate	/*
9437c478bdstevel@tonic-gate	 * OK, now I have a netbuf structure which I'm supposed to
9447c478bdstevel@tonic-gate	 * treat as opaque...  I hate transport independance!
9457c478bdstevel@tonic-gate	 * So, we're just gonna doit wrong...  By wrong I mean that
9467c478bdstevel@tonic-gate	 * we assume that the buf part of the netbuf structure is going
9477c478bdstevel@tonic-gate	 * to be a sockaddr_in.  We'll then check the assumed family
9487c478bdstevel@tonic-gate	 * member and hope that we find AF_INET in there...  if not
9497c478bdstevel@tonic-gate	 * then we can't continue.
9507c478bdstevel@tonic-gate	 */
9517c478bdstevel@tonic-gate	if (((struct sockaddr_in *)(nbuf->buf))->sin_family != AF_INET)
9527c478bdstevel@tonic-gate		return (0);
9537c478bdstevel@tonic-gate
9547c478bdstevel@tonic-gate	call_addr = ((struct sockaddr_in *)(nbuf->buf))->sin_addr.s_addr;
9557c478bdstevel@tonic-gate
9567c478bdstevel@tonic-gate	cp = resp->valdat.dptr;
9577c478bdstevel@tonic-gate	if ((bp = strtok(cp, " \t")) == NULL) /* no address field */
9587c478bdstevel@tonic-gate		return (0);
9597c478bdstevel@tonic-gate	if ((cp = strtok(NULL, "")) == NULL)  /* no host field */
9607c478bdstevel@tonic-gate		return (0);
9617c478bdstevel@tonic-gate	bp = strtok(bp, ",");
9627c478bdstevel@tonic-gate
9637c478bdstevel@tonic-gate	bestaddr = inet_addr(bp);
9647c478bdstevel@tonic-gate	while (cp = strtok(NULL, ",")) {
9657c478bdstevel@tonic-gate		ulong_t taddr;
9667c478bdstevel@tonic-gate
9677c478bdstevel@tonic-gate		taddr = inet_addr(cp);
9687c478bdstevel@tonic-gate		if (ntohl(call_addr ^ taddr) < ntohl(call_addr ^ bestaddr))
9697c478bdstevel@tonic-gate			bestaddr = taddr;
9707c478bdstevel@tonic-gate	}
9717c478bdstevel@tonic-gate	cp = resp->valdat.dptr;
9727c478bdstevel@tonic-gate	sprintf(cp, "%s %s", inet_ntoa(*(struct in_addr *)&bestaddr), name);
9737c478bdstevel@tonic-gate	resp->valdat.dsize = strlen(cp);
9747c478bdstevel@tonic-gate
9757c478bdstevel@tonic-gate	resp->status = YP_TRUE;
9767c478bdstevel@tonic-gate
9777c478bdstevel@tonic-gate	return (1);
9787c478bdstevel@tonic-gate}
9797c478bdstevel@tonic-gate
9807c478bdstevel@tonic-gate/* V1 dispatch routines */
9817c478bdstevel@tonic-gatevoid
9827c478bdstevel@tonic-gateypoldmatch(SVCXPRT *transp, struct svc_req *rqstp)
9837c478bdstevel@tonic-gate{
9847c478bdstevel@tonic-gate	bool dbmop_ok = TRUE;
9857c478bdstevel@tonic-gate	struct yprequest req;
9867c478bdstevel@tonic-gate	struct ypreq_key nrq;
9877c478bdstevel@tonic-gate	struct ypresponse resp;
9887c478bdstevel@tonic-gate	char *fun = "ypoldmatch";
9897c478bdstevel@tonic-gate	DBM *fdb;
9907c478bdstevel@tonic-gate
9917c478bdstevel@tonic-gate	memset((void *) &req, 0, sizeof (req));
9927c478bdstevel@tonic-gate	memset((void *) &resp, 0, sizeof (resp));
9937c478bdstevel@tonic-gate
9947c478bdstevel@tonic-gate	if (!svc_getargs(transp,
9957c478bdstevel@tonic-gate				(xdrproc_t)_xdr_yprequest,
9967c478bdstevel@tonic-gate				(caddr_t)&req)) {
9977c478bdstevel@tonic-gate		svcerr_decode(transp);
9987c478bdstevel@tonic-gate		return;
9997c478bdstevel@tonic-gate	}
10007c478bdstevel@tonic-gate
10017c478bdstevel@tonic-gate	if (req.yp_reqtype != YPMATCH_REQTYPE) {
10027c478bdstevel@tonic-gate		resp.ypmatch_resp_status = (unsigned)YP_BADARGS;
10037c478bdstevel@tonic-gate		dbmop_ok = FALSE;
10047c478bdstevel@tonic-gate	}
10057c478bdstevel@tonic-gate
10067c478bdstevel@tonic-gate	if (dbmop_ok &&
10077c478bdstevel@tonic-gate		(((fdb = ypset_current_map(req.ypmatch_req_map,
10087c478bdstevel@tonic-gate						req.ypmatch_req_domain,
10097c478bdstevel@tonic-gate						&resp.ypmatch_resp_status))
10107c478bdstevel@tonic-gate						!= NULL) &&
10117c478bdstevel@tonic-gate						yp_map_access(transp,
10127c478bdstevel@tonic-gate						&resp.ypmatch_resp_status,
10137c478bdstevel@tonic-gate						fdb))) {
10147c478bdstevel@tonic-gate
10157c478bdstevel@tonic-gate		/* Check with the DBM database */
10167c478bdstevel@tonic-gate		resp.ypmatch_resp_valdat = dbm_fetch(fdb,
10177c478bdstevel@tonic-gate						req.ypmatch_req_keydat);
10187c478bdstevel@tonic-gate
10197c478bdstevel@tonic-gate		if (resp.ypmatch_resp_valptr != NULL) {
10207c478bdstevel@tonic-gate			resp.ypmatch_resp_status = YP_TRUE;
10217c478bdstevel@tonic-gate			if (!silent)
10227c478bdstevel@tonic-gate				printf("%s: dbm: %s\n",
10237c478bdstevel@tonic-gate					fun, resp.ypmatch_resp_valptr);
10247c478bdstevel@tonic-gate			goto send_oldreply;
10257c478bdstevel@tonic-gate		}
10267c478bdstevel@tonic-gate
10277c478bdstevel@tonic-gate		/*
10287c478bdstevel@tonic-gate		 * If we're being asked to match YP_SECURE or YP_INTERDOMAIN
10297c478bdstevel@tonic-gate		 * and we haven't found it in the dbm file, then we don't
10307c478bdstevel@tonic-gate		 * really want to waste any more time.  Specifically, we don't
10317c478bdstevel@tonic-gate		 * want to ask DNS
10327c478bdstevel@tonic-gate		 */
10337c478bdstevel@tonic-gate		if (req.ypmatch_req_keysize == 0 ||
10347c478bdstevel@tonic-gate		    req.ypmatch_req_keyptr == NULL ||
10357c478bdstevel@tonic-gate		    req.ypmatch_req_keyptr[0] == '\0' ||
10367c478bdstevel@tonic-gate		    strncmp(req.ypmatch_req_keyptr, "YP_SECURE", 9) == 0 ||
10377c478bdstevel@tonic-gate		    strncmp(req.ypmatch_req_keyptr, "YP_INTERDOMAIN", 14) == 0)
10387c478bdstevel@tonic-gate
10397c478bdstevel@tonic-gate		    goto send_oldreply;
10407c478bdstevel@tonic-gate
10417c478bdstevel@tonic-gate		/* Let's try the YP_MULTI_ hack... */
10427c478bdstevel@tonic-gate#ifdef MINUS_C_OPTION
10437c478bdstevel@tonic-gate		if (multiflag == TRUE && omultihomed(req, &resp, transp, fdb))
10447c478bdstevel@tonic-gate			goto send_oldreply;
10457c478bdstevel@tonic-gate#else
10467c478bdstevel@tonic-gate		if (omultihomed(req, &resp, transp, fdb))
10477c478bdstevel@tonic-gate			goto send_oldreply;
10487c478bdstevel@tonic-gate#endif
10497c478bdstevel@tonic-gate
10507c478bdstevel@tonic-gate		/* Let's try DNS */
10517c478bdstevel@tonic-gate		if (!dnsforward) {
10527c478bdstevel@tonic-gate			USE_YP_INTERDOMAIN
10537c478bdstevel@tonic-gate			datum idkey, idval;
10547c478bdstevel@tonic-gate			idkey.dptr = yp_interdomain;
10557c478bdstevel@tonic-gate			idkey.dsize = yp_interdomain_sz;
10567c478bdstevel@tonic-gate			idval = dbm_fetch(fdb, idkey);
10577c478bdstevel@tonic-gate			if (idval.dptr)
10587c478bdstevel@tonic-gate				dnsforward = TRUE;
10597c478bdstevel@tonic-gate		}
10607c478bdstevel@tonic-gate
10617c478bdstevel@tonic-gate		if (dnsforward) {
10627c478bdstevel@tonic-gate		    if (!resolv_pid)
10637c478bdstevel@tonic-gate			setup_resolv(&dnsforward, &resolv_pid, &resolv_client,
10647c478bdstevel@tonic-gate					resolv_tp, 0);
10657c478bdstevel@tonic-gate
10667c478bdstevel@tonic-gate		    if (req.yp_reqtype == YPREQ_KEY) {
10677c478bdstevel@tonic-gate			nrq = req.yp_reqbody.yp_req_keytype;
10687c478bdstevel@tonic-gate
10697c478bdstevel@tonic-gate			resolv_req(&dnsforward, &resolv_client, &resolv_pid,
10707c478bdstevel@tonic-gate					resolv_tp, rqstp->rq_xprt,
10717c478bdstevel@tonic-gate					&nrq, nrq.map);
10727c478bdstevel@tonic-gate		    }
10737c478bdstevel@tonic-gate		    return;
10747c478bdstevel@tonic-gate		}
10757c478bdstevel@tonic-gate	}
10767c478bdstevel@tonic-gate
10777c478bdstevel@tonic-gate	send_oldreply:
10787c478bdstevel@tonic-gate
10797c478bdstevel@tonic-gate	if (!svc_sendreply(transp,
10807c478bdstevel@tonic-gate				(xdrproc_t)_xdr_ypresponse,
10817c478bdstevel@tonic-gate				(caddr_t)&resp)) {
10827c478bdstevel@tonic-gate		RESPOND_ERR;
10837c478bdstevel@tonic-gate	}
10847c478bdstevel@tonic-gate
10857c478bdstevel@tonic-gate	if (!svc_freeargs(transp,
10867c478bdstevel@tonic-gate				(xdrproc_t)_xdr_yprequest,
10877c478bdstevel@tonic-gate				(char *)&req)) {
10887c478bdstevel@tonic-gate		FREE_ERR;
10897c478bdstevel@tonic-gate	}
10907c478bdstevel@tonic-gate}
10917c478bdstevel@tonic-gate
10927c478bdstevel@tonic-gatevoid
10937c478bdstevel@tonic-gateypoldfirst(SVCXPRT *transp)
10947c478bdstevel@tonic-gate{
10957c478bdstevel@tonic-gate	bool dbmop_ok = TRUE;
10967c478bdstevel@tonic-gate	struct yprequest req;
10977c478bdstevel@tonic-gate	struct ypresponse resp;
10987c478bdstevel@tonic-gate	char *fun = "ypoldfirst";
10997c478bdstevel@tonic-gate	DBM *fdb;
11007c478bdstevel@tonic-gate
11017c478bdstevel@tonic-gate	memset((void *) &req, 0, sizeof (req));
11027c478bdstevel@tonic-gate	memset((void *) &resp, 0, sizeof (resp));
11037c478bdstevel@tonic-gate
11047c478bdstevel@tonic-gate	if (!svc_getargs(transp,
11057c478bdstevel@tonic-gate				(xdrproc_t)_xdr_yprequest,
11067c478bdstevel@tonic-gate				(caddr_t)&req)) {
11077c478bdstevel@tonic-gate		svcerr_decode(transp);
11087c478bdstevel@tonic-gate		return;
11097c478bdstevel@tonic-gate	}
11107c478bdstevel@tonic-gate
11117c478bdstevel@tonic-gate	if (req.yp_reqtype != YPFIRST_REQTYPE) {
11127c478bdstevel@tonic-gate		resp.ypfirst_resp_status = (unsigned)YP_BADARGS;
11137c478bdstevel@tonic-gate		dbmop_ok = FALSE;
11147c478bdstevel@tonic-gate	}
11157c478bdstevel@tonic-gate
11167c478bdstevel@tonic-gate	if (dbmop_ok &&
11177c478bdstevel@tonic-gate		((fdb = ypset_current_map(req.ypfirst_req_map,
11187c478bdstevel@tonic-gate						req.ypfirst_req_domain,
11197c478bdstevel@tonic-gate						&resp.ypfirst_resp_status))
11207c478bdstevel@tonic-gate						!= NULL) &&
11217c478bdstevel@tonic-gate						yp_map_access(transp,
11227c478bdstevel@tonic-gate						&resp.ypfirst_resp_status,
11237c478bdstevel@tonic-gate						fdb)) {
11247c478bdstevel@tonic-gate
11257c478bdstevel@tonic-gate		resp.ypfirst_resp_keydat = dbm_firstkey(fdb);
11267c478bdstevel@tonic-gate
11277c478bdstevel@tonic-gate		if (resp.ypfirst_resp_keyptr != NULL) {
11287c478bdstevel@tonic-gate			resp.ypfirst_resp_valdat =
11297c478bdstevel@tonic-gate				dbm_fetch(fdb, resp.ypfirst_resp_keydat);
11307c478bdstevel@tonic-gate
11317c478bdstevel@tonic-gate			if (resp.ypfirst_resp_valptr != NULL) {
11327c478bdstevel@tonic-gate				resp.ypfirst_resp_status = YP_TRUE;
11337c478bdstevel@tonic-gate			} else {
11347c478bdstevel@tonic-gate				resp.ypfirst_resp_status = (unsigned)YP_BADDB;
11357c478bdstevel@tonic-gate			}
11367c478bdstevel@tonic-gate		} else {
11377c478bdstevel@tonic-gate			resp.ypfirst_resp_status = (unsigned)YP_NOKEY;
11387c478bdstevel@tonic-gate		}
11397c478bdstevel@tonic-gate	}
11407c478bdstevel@tonic-gate
11417c478bdstevel@tonic-gate	resp.yp_resptype = YPFIRST_RESPTYPE;
11427c478bdstevel@tonic-gate
11437c478bdstevel@tonic-gate	if (!svc_sendreply(transp,
11447c478bdstevel@tonic-gate				(xdrproc_t)_xdr_ypresponse,
11457c478bdstevel@tonic-gate				(caddr_t)&resp)) {
11467c478bdstevel@tonic-gate		RESPOND_ERR;
11477c478bdstevel@tonic-gate	}
11487c478bdstevel@tonic-gate
11497c478bdstevel@tonic-gate	if (!svc_freeargs(transp,
11507c478bdstevel@tonic-gate				(xdrproc_t)_xdr_yprequest,
11517c478bdstevel@tonic-gate				(caddr_t)&req)) {
11527c478bdstevel@tonic-gate		FREE_ERR;
11537c478bdstevel@tonic-gate	}
11547c478bdstevel@tonic-gate}
11557c478bdstevel@tonic-gate
11567c478bdstevel@tonic-gatevoid
11577c478bdstevel@tonic-gateypoldnext(SVCXPRT *transp)
11587c478bdstevel@tonic-gate{
11597c478bdstevel@tonic-gate	bool dbmop_ok = TRUE;
11607c478bdstevel@tonic-gate	struct yprequest req;
11617c478bdstevel@tonic-gate	struct ypresponse resp;
11627c478bdstevel@tonic-gate	char *fun = "ypoldnext";
11637c478bdstevel@tonic-gate	DBM *fdb;
11647c478bdstevel@tonic-gate
11657c478bdstevel@tonic-gate	memset((void *) &req, 0, sizeof (req));
11667c478bdstevel@tonic-gate	memset((void *) &resp, 0, sizeof (resp));
11677c478bdstevel@tonic-gate
11687c478bdstevel@tonic-gate	if (!svc_getargs(transp,
11697c478bdstevel@tonic-gate				(xdrproc_t)_xdr_yprequest,
11707c478bdstevel@tonic-gate				(caddr_t)&req)) {
11717c478bdstevel@tonic-gate		svcerr_decode(transp);
11727c478bdstevel@tonic-gate		return;
11737c478bdstevel@tonic-gate	}
11747c478bdstevel@tonic-gate
11757c478bdstevel@tonic-gate	if (req.yp_reqtype != YPNEXT_REQTYPE) {
11767c478bdstevel@tonic-gate		resp.ypnext_resp_status = (unsigned)YP_BADARGS;
11777c478bdstevel@tonic-gate		dbmop_ok = FALSE;
11787c478bdstevel@tonic-gate	}
11797c478bdstevel@tonic-gate
11807c478bdstevel@tonic-gate	if (dbmop_ok &&
11817c478bdstevel@tonic-gate		((fdb = ypset_current_map(req.ypnext_req_map,
11827c478bdstevel@tonic-gate					req.ypnext_req_domain,
11837c478bdstevel@tonic-gate					&resp.ypnext_resp_status)) != NULL &&
11847c478bdstevel@tonic-gate		yp_map_access(transp, &resp.ypnext_resp_status, fdb))) {
11857c478bdstevel@tonic-gate
11867c478bdstevel@tonic-gate		resp.ypnext_resp_keydat = dbm_nextkey(fdb);
11877c478bdstevel@tonic-gate
11887c478bdstevel@tonic-gate		if (resp.ypnext_resp_keyptr != NULL) {
11897c478bdstevel@tonic-gate			resp.ypnext_resp_valdat =
11907c478bdstevel@tonic-gate			dbm_fetch(fdb, resp.ypnext_resp_keydat);
11917c478bdstevel@tonic-gate
11927c478bdstevel@tonic-gate			if (resp.ypnext_resp_valptr != NULL) {
11937c478bdstevel@tonic-gate				resp.ypnext_resp_status = YP_TRUE;
11947c478bdstevel@tonic-gate			} else {
11957c478bdstevel@tonic-gate				resp.ypnext_resp_status = (unsigned)YP_BADDB;
11967c478bdstevel@tonic-gate			}
11977c478bdstevel@tonic-gate		} else {
11987c478bdstevel@tonic-gate			resp.ypnext_resp_status = (unsigned)YP_NOMORE;
11997c478bdstevel@tonic-gate		}
12007c478bdstevel@tonic-gate	}
12017c478bdstevel@tonic-gate
12027c478bdstevel@tonic-gate	resp.yp_resptype = YPNEXT_RESPTYPE;
12037c478bdstevel@tonic-gate
12047c478bdstevel@tonic-gate	if (!svc_sendreply(transp,
12057c478bdstevel@tonic-gate				(xdrproc_t)_xdr_ypresponse,
12067c478bdstevel@tonic-gate				(caddr_t)&resp)) {
12077c478bdstevel@tonic-gate		RESPOND_ERR;
12087c478bdstevel@tonic-gate	}
12097c478bdstevel@tonic-gate
12107c478bdstevel@tonic-gate	if (!svc_freeargs(transp,
12117c478bdstevel@tonic-gate				(xdrproc_t)_xdr_yprequest,
12127c478bdstevel@tonic-gate				(caddr_t)&req)) {
12137c478bdstevel@tonic-gate		FREE_ERR;
12147c478bdstevel@tonic-gate	}
12157c478bdstevel@tonic-gate}
12167c478bdstevel@tonic-gate
12177c478bdstevel@tonic-gate/*
12187c478bdstevel@tonic-gate * This retrieves the order number and master peer name from the map.
12197c478bdstevel@tonic-gate * The conditions for the various message fields are: domain is filled
12207c478bdstevel@tonic-gate * in iff the domain exists.  map is filled in iff the map exists.
12217c478bdstevel@tonic-gate * order number is filled in iff it's in the map.  owner is filled in
12227c478bdstevel@tonic-gate * iff the master peer is in the map.
12237c478bdstevel@tonic-gate */
12247c478bdstevel@tonic-gatevoid
12257c478bdstevel@tonic-gateypoldpoll(SVCXPRT *transp)
12267c478bdstevel@tonic-gate{
12277c478bdstevel@tonic-gate	struct yprequest req;
12287c478bdstevel@tonic-gate	struct ypresponse resp;
12297c478bdstevel@tonic-gate	char *map = "";
12307c478bdstevel@tonic-gate	char *domain = "";
12317c478bdstevel@tonic-gate	char *owner = "";
12327c478bdstevel@tonic-gate	uint_t error;
12337c478bdstevel@tonic-gate	char *fun = "ypoldpoll";
12347c478bdstevel@tonic-gate	DBM *fdb;
12357c478bdstevel@tonic-gate
12367c478bdstevel@tonic-gate	memset((void *) &req, 0, sizeof (req));
12377c478bdstevel@tonic-gate	memset((void *) &resp, 0, sizeof (resp));
12387c478bdstevel@tonic-gate
12397c478bdstevel@tonic-gate	if (!svc_getargs(transp,
12407c478bdstevel@tonic-gate				(xdrproc_t)_xdr_yprequest,
12417c478bdstevel@tonic-gate				(caddr_t)&req)) {
12427c478bdstevel@tonic-gate		svcerr_decode(transp);
12437c478bdstevel@tonic-gate		return;
12447c478bdstevel@tonic-gate	}
12457c478bdstevel@tonic-gate
12467c478bdstevel@tonic-gate	if (req.yp_reqtype == YPPOLL_REQTYPE) {
12477c478bdstevel@tonic-gate		if (strcmp(req.yppoll_req_domain, "yp_private") == 0 ||
12487c478bdstevel@tonic-gate			strcmp(req.yppoll_req_map, "ypdomains") == 0 ||
12497c478bdstevel@tonic-gate			strcmp(req.yppoll_req_map, "ypmaps") == 0) {
12507c478bdstevel@tonic-gate
12517c478bdstevel@tonic-gate			/*
12527c478bdstevel@tonic-gate			 * Backward comatibility for 2.0 NIS servers
12537c478bdstevel@tonic-gate			 */
12547c478bdstevel@tonic-gate			domain = req.yppoll_req_domain;
12557c478bdstevel@tonic-gate			map = req.yppoll_req_map;
12567c478bdstevel@tonic-gate		} else if ((fdb = ypset_current_map(req.yppoll_req_map,
12577c478bdstevel@tonic-gate				req.yppoll_req_domain,
12587c478bdstevel@tonic-gate				&error)) != NULL) {
12597c478bdstevel@tonic-gate			domain = req.yppoll_req_domain;
12607c478bdstevel@tonic-gate			map = req.yppoll_req_map;
12617c478bdstevel@tonic-gate			ypget_map_order(map, domain,
12627c478bdstevel@tonic-gate					&resp.yppoll_resp_ordernum);
12637c478bdstevel@tonic-gate			ypget_map_master(&owner, fdb);
12647c478bdstevel@tonic-gate		} else {
12657c478bdstevel@tonic-gate			switch ((int)error) {
12667c478bdstevel@tonic-gate			case YP_BADDB:
12677c478bdstevel@tonic-gate				map = req.yppoll_req_map;
12687c478bdstevel@tonic-gate				/* Fall through to set the domain too. */
12697c478bdstevel@tonic-gate
12707c478bdstevel@tonic-gate			case YP_NOMAP:
12717c478bdstevel@tonic-gate				domain = req.yppoll_req_domain;
12727c478bdstevel@tonic-gate				break;
12737c478bdstevel@tonic-gate			}
12747c478bdstevel@tonic-gate		}
12757c478bdstevel@tonic-gate	}
12767c478bdstevel@tonic-gate
12777c478bdstevel@tonic-gate	resp.yp_resptype = YPPOLL_RESPTYPE;
12787c478bdstevel@tonic-gate	resp.yppoll_resp_domain = domain;
12797c478bdstevel@tonic-gate	resp.yppoll_resp_map = map;
12807c478bdstevel@tonic-gate	resp.yppoll_resp_owner = owner;
12817c478bdstevel@tonic-gate
12827c478bdstevel@tonic-gate	if (!svc_sendreply(transp,
12837c478bdstevel@tonic-gate				(xdrproc_t)_xdr_ypresponse,
12847c478bdstevel@tonic-gate				(caddr_t)&resp)) {
12857c478bdstevel@tonic-gate		RESPOND_ERR;
12867c478bdstevel@tonic-gate	}
12877c478bdstevel@tonic-gate
12887c478bdstevel@tonic-gate	if (!svc_freeargs(transp,
12897c478bdstevel@tonic-gate				(xdrproc_t)_xdr_yprequest,
12907c478bdstevel@tonic-gate				(caddr_t)&req)) {
12917c478bdstevel@tonic-gate		FREE_ERR;
12927c478bdstevel@tonic-gate	}
12937c478bdstevel@tonic-gate}
12947c478bdstevel@tonic-gate
12957c478bdstevel@tonic-gatevoid
12967c478bdstevel@tonic-gateypoldpush(SVCXPRT *transp)
12977c478bdstevel@tonic-gate{
12987c478bdstevel@tonic-gate	struct yprequest req;
12997c478bdstevel@tonic-gate	struct ypresp_val resp;
13007c478bdstevel@tonic-gate	pid_t pid = -1;
13017c478bdstevel@tonic-gate	char *fun = "ypoldpush";
13027c478bdstevel@tonic-gate	DBM *fdb;
13037c478bdstevel@tonic-gate
13047c478bdstevel@tonic-gate	memset((void *) &req, 0, sizeof (req));
13057c478bdstevel@tonic-gate
13067c478bdstevel@tonic-gate	if (!svc_getargs(transp,
13077c478bdstevel@tonic-gate				(xdrproc_t)_xdr_yprequest,
13087c478bdstevel@tonic-gate				(caddr_t)&req)) {
13097c478bdstevel@tonic-gate		svcerr_decode(transp);
13107c478bdstevel@tonic-gate		return;
13117c478bdstevel@tonic-gate	}
13127c478bdstevel@tonic-gate
13137c478bdstevel@tonic-gate	if (((fdb = ypset_current_map(req.yppush_req_map,
13147c478bdstevel@tonic-gate					req.yppush_req_domain,
13157c478bdstevel@tonic-gate					&resp.status)) != NULL) &&
13167c478bdstevel@tonic-gate		(yp_map_access(transp, &resp.status, fdb))) {
13177c478bdstevel@tonic-gate
13187c478bdstevel@tonic-gate		pid = vfork();
13197c478bdstevel@tonic-gate	}
13207c478bdstevel@tonic-gate
13217c478bdstevel@tonic-gate	if (pid == -1) {
13227c478bdstevel@tonic-gate		FORK_ERR;
13237c478bdstevel@tonic-gate	} else if (pid == 0) {
13247c478bdstevel@tonic-gate		ypclr_current_map();
13257c478bdstevel@tonic-gate
13267c478bdstevel@tonic-gate		if (execl(yppush_proc, "yppush", "-d", req.yppush_req_domain,
13277c478bdstevel@tonic-gate				req.yppush_req_map, NULL)) {
13287c478bdstevel@tonic-gate			EXEC_ERR;
13297c478bdstevel@tonic-gate		}
13307c478bdstevel@tonic-gate		_exit(1);
13317c478bdstevel@tonic-gate	}
13327c478bdstevel@tonic-gate
13337c478bdstevel@tonic-gate	if (!svc_sendreply(transp,
13347c478bdstevel@tonic-gate				(xdrproc_t)xdr_void,
13357c478bdstevel@tonic-gate				(caddr_t)NULL)) {
13367c478bdstevel@tonic-gate		RESPOND_ERR;
13377c478bdstevel@tonic-gate	}
13387c478bdstevel@tonic-gate
13397c478bdstevel@tonic-gate	if (!svc_freeargs(transp,
13407c478bdstevel@tonic-gate				(xdrproc_t)_xdr_yprequest,
13417c478bdstevel@tonic-gate				(caddr_t)&req)) {
13427c478bdstevel@tonic-gate		FREE_ERR;
13437c478bdstevel@tonic-gate	}
13447c478bdstevel@tonic-gate}
13457c478bdstevel@tonic-gate
13467c478bdstevel@tonic-gatevoid
13477c478bdstevel@tonic-gateypoldpull(SVCXPRT *transp)
13487c478bdstevel@tonic-gate{
13497c478bdstevel@tonic-gate	struct yprequest req;
13507c478bdstevel@tonic-gate	struct ypresp_val resp;
13517c478bdstevel@tonic-gate	pid_t pid = -1;
13527c478bdstevel@tonic-gate	char *fun = "ypoldpull";
13537c478bdstevel@tonic-gate	DBM *fdb;
13547c478bdstevel@tonic-gate
13557c478bdstevel@tonic-gate	memset((void *) &req, 0, sizeof (req));
13567c478bdstevel@tonic-gate
13577c478bdstevel@tonic-gate	if (!svc_getargs(transp,
13587c478bdstevel@tonic-gate				(xdrproc_t)_xdr_yprequest,
13597c478bdstevel@tonic-gate				(caddr_t)&req)) {
13607c478bdstevel@tonic-gate		svcerr_decode(transp);
13617c478bdstevel@tonic-gate		return;
13627c478bdstevel@tonic-gate	}
13637c478bdstevel@tonic-gate
13647c478bdstevel@tonic-gate	if (req.yp_reqtype == YPPULL_REQTYPE) {
13657c478bdstevel@tonic-gate
13667c478bdstevel@tonic-gate		if (((fdb = ypset_current_map(req.yppull_req_map,
13677c478bdstevel@tonic-gate						req.yppull_req_domain,
13687c478bdstevel@tonic-gate						&resp.status)) == NULL) ||
13697c478bdstevel@tonic-gate			(yp_map_access(transp, &resp.status, fdb))) {
13707c478bdstevel@tonic-gate			pid = vfork();
13717c478bdstevel@tonic-gate		}
13727c478bdstevel@tonic-gate
13737c478bdstevel@tonic-gate		if (pid == -1) {
13747c478bdstevel@tonic-gate			FORK_ERR;
13757c478bdstevel@tonic-gate		} else if (pid == 0) {
13767c478bdstevel@tonic-gate			ypclr_current_map();
13777c478bdstevel@tonic-gate
13787c478bdstevel@tonic-gate			if (execl(ypxfr_proc, "ypxfr", "-d",
13797c478bdstevel@tonic-gate					req.yppull_req_domain,
13807c478bdstevel@tonic-gate					req.yppull_req_map, NULL)) {
13817c478bdstevel@tonic-gate				EXEC_ERR;
13827c478bdstevel@tonic-gate			}
13837c478bdstevel@tonic-gate			_exit(1);
13847c478bdstevel@tonic-gate		}
13857c478bdstevel@tonic-gate	}
13867c478bdstevel@tonic-gate
13877c478bdstevel@tonic-gate	if (!svc_freeargs(transp,
13887c478bdstevel@tonic-gate				(xdrproc_t)_xdr_yprequest,
13897c478bdstevel@tonic-gate				(caddr_t)&req)) {
13907c478bdstevel@tonic-gate		FREE_ERR;
13917c478bdstevel@tonic-gate	}
13927c478bdstevel@tonic-gate}
13937c478bdstevel@tonic-gate
13947c478bdstevel@tonic-gatevoid
13957c478bdstevel@tonic-gateypoldget(SVCXPRT *transp)
13967c478bdstevel@tonic-gate{
13977c478bdstevel@tonic-gate	struct yprequest req;
13987c478bdstevel@tonic-gate	struct ypresp_val resp;
13997c478bdstevel@tonic-gate	pid_t pid = -1;
14007c478bdstevel@tonic-gate	char *fun = "ypoldget";
14017c478bdstevel@tonic-gate	DBM *fdb;
14027c478bdstevel@tonic-gate
14037c478bdstevel@tonic-gate	memset((void *) &req, 0, sizeof (req));
14047c478bdstevel@tonic-gate
14057c478bdstevel@tonic-gate	if (!svc_getargs(transp,
14067c478bdstevel@tonic-gate				(xdrproc_t)_xdr_yprequest,
14077c478bdstevel@tonic-gate				(caddr_t)&req)) {
14087c478bdstevel@tonic-gate		svcerr_decode(transp);
14097c478bdstevel@tonic-gate		return;
14107c478bdstevel@tonic-gate	}
14117c478bdstevel@tonic-gate
14127c478bdstevel@tonic-gate	if (!svc_sendreply(transp, xdr_void, 0)) {
14137c478bdstevel@tonic-gate		RESPOND_ERR;
14147c478bdstevel@tonic-gate	}
14157c478bdstevel@tonic-gate
14167c478bdstevel@tonic-gate	if (req.yp_reqtype == YPGET_REQTYPE) {
14177c478bdstevel@tonic-gate
14187c478bdstevel@tonic-gate		if (((fdb = ypset_current_map(req.ypget_req_map,
14197c478bdstevel@tonic-gate						req.ypget_req_domain,
14207c478bdstevel@tonic-gate						&resp.status)) == NULL) ||
14217c478bdstevel@tonic-gate			(yp_map_access(transp, &resp.status, fdb))) {
14227c478bdstevel@tonic-gate
14237c478bdstevel@tonic-gate			pid = vfork();
14247c478bdstevel@tonic-gate		}
14257c478bdstevel@tonic-gate
14267c478bdstevel@tonic-gate		if (pid == -1) {
14277c478bdstevel@tonic-gate			FORK_ERR;
14287c478bdstevel@tonic-gate		} else if (pid == 0) {
14297c478bdstevel@tonic-gate
14307c478bdstevel@tonic-gate			ypclr_current_map();
14317c478bdstevel@tonic-gate
14327c478bdstevel@tonic-gate			if (execl(ypxfr_proc, "ypxfr", "-d",
14337c478bdstevel@tonic-gate					req.ypget_req_domain, "-h",
14347c478bdstevel@tonic-gate					req.ypget_req_owner,
14357c478bdstevel@tonic-gate					req.ypget_req_map, NULL)) {
14367c478bdstevel@tonic-gate
14377c478bdstevel@tonic-gate				EXEC_ERR;
14387c478bdstevel@tonic-gate			}
14397c478bdstevel@tonic-gate			_exit(1);
14407c478bdstevel@tonic-gate		}
14417c478bdstevel@tonic-gate	}
14427c478bdstevel@tonic-gate
14437c478bdstevel@tonic-gate	if (!svc_freeargs(transp,
14447c478bdstevel@tonic-gate				(xdrproc_t)_xdr_yprequest,
14457c478bdstevel@tonic-gate				(caddr_t)&req)) {
14467c478bdstevel@tonic-gate		RESPOND_ERR;
14477c478bdstevel@tonic-gate	}
14487c478bdstevel@tonic-gate}
14497c478bdstevel@tonic-gate
14507c478bdstevel@tonic-gatestatic int
14517c478bdstevel@tonic-gateomultihomed(struct yprequest req,
14527c478bdstevel@tonic-gate	    struct ypresponse *resp, SVCXPRT *xprt, DBM *fdb)
14537c478bdstevel@tonic-gate{
14547c478bdstevel@tonic-gate	char *cp, *bp;
14557c478bdstevel@tonic-gate	char name[PATH_MAX];
14567c478bdstevel@tonic-gate	struct netbuf *nbuf;
14577c478bdstevel@tonic-gate	ulong_t bestaddr, call_addr;
14587c478bdstevel@tonic-gate
14597c478bdstevel@tonic-gate	if (strcmp(req.ypmatch_req_map, "hosts.byname"))
14607c478bdstevel@tonic-gate		return (0);
14617c478bdstevel@tonic-gate
14627c478bdstevel@tonic-gate	if (strncmp(req.ypmatch_req_keyptr, "YP_MULTI_", 9)) {
14637c478bdstevel@tonic-gate		datum tmpname;
14647c478bdstevel@tonic-gate
14657c478bdstevel@tonic-gate		strncpy(name, "YP_MULTI_", 9);
14667c478bdstevel@tonic-gate		strncpy(name + 9, req.ypmatch_req_keyptr,
14677c478bdstevel@tonic-gate			req.ypmatch_req_keysize);
14687c478bdstevel@tonic-gate		tmpname.dsize = req.ypmatch_req_keysize + 9;
14697c478bdstevel@tonic-gate		tmpname.dptr = name;
14707c478bdstevel@tonic-gate		resp->ypmatch_resp_valdat = dbm_fetch(fdb, tmpname);
14717c478bdstevel@tonic-gate	} else {
14727c478bdstevel@tonic-gate		resp->ypmatch_resp_valdat =
14737c478bdstevel@tonic-gate			dbm_fetch(fdb, req.ypmatch_req_keydat);
14747c478bdstevel@tonic-gate		if (resp->ypmatch_resp_valptr != NULL)
14757c478bdstevel@tonic-gate			return (1);
14767c478bdstevel@tonic-gate	}
14777c478bdstevel@tonic-gate
14787c478bdstevel@tonic-gate	if (resp->ypmatch_resp_valptr == NULL)
14797c478bdstevel@tonic-gate		return (0);
14807c478bdstevel@tonic-gate
14817c478bdstevel@tonic-gate	strncpy(name, req.ypmatch_req_keyptr, req.ypmatch_req_keysize);
14827c478bdstevel@tonic-gate	name[req.ypmatch_req_keysize] = NULL;
14837c478bdstevel@tonic-gate
14847c478bdstevel@tonic-gate	nbuf = svc_getrpccaller(xprt);
14857c478bdstevel@tonic-gate
14867c478bdstevel@tonic-gate	/*
14877c478bdstevel@tonic-gate	 * OK, now I have a netbuf structure which I'm supposed to treat
14887c478bdstevel@tonic-gate	 * as opaque...  I hate transport independance!  So, we're just
14897c478bdstevel@tonic-gate	 * gonna doit wrong...  By wrong I mean that we assume that the
14907c478bdstevel@tonic-gate	 * buf part of the netbuf structure is going to be a sockaddr_in.
14917c478bdstevel@tonic-gate	 * We'll then check the assumed family member and hope that we
14927c478bdstevel@tonic-gate	 * find AF_INET in there...  if not then we can't continue.
14937c478bdstevel@tonic-gate	 */
14947c478bdstevel@tonic-gate	if (((struct sockaddr_in *)(nbuf->buf))->sin_family != AF_INET)
14957c478bdstevel@tonic-gate		return (0);
14967c478bdstevel@tonic-gate
14977c478bdstevel@tonic-gate	call_addr = ((struct sockaddr_in *)(nbuf->buf))->sin_addr.s_addr;
14987c478bdstevel@tonic-gate
14997c478bdstevel@tonic-gate	cp = resp->ypmatch_resp_valptr;
15007c478bdstevel@tonic-gate	if ((bp = strtok(cp, "\t")) == NULL)	/* No address field */
15017c478bdstevel@tonic-gate		return (0);
15027c478bdstevel@tonic-gate	if ((cp = strtok(NULL, "")) == NULL)	/* No host field */
15037c478bdstevel@tonic-gate		return (0);
15047c478bdstevel@tonic-gate	bp = strtok(bp, ",");
15057c478bdstevel@tonic-gate
15067c478bdstevel@tonic-gate	bestaddr = inet_addr(bp);
15077c478bdstevel@tonic-gate	while (cp = strtok(NULL, ",")) {
15087c478bdstevel@tonic-gate		ulong_t taddr;
15097c478bdstevel@tonic-gate
15107c478bdstevel@tonic-gate		taddr = inet_addr(cp);
15117c478bdstevel@tonic-gate		if (ntohl(call_addr ^ taddr) < ntohl(call_addr ^ bestaddr))
15127c478bdstevel@tonic-gate			bestaddr = taddr;
15137c478bdstevel@tonic-gate	}
15147c478bdstevel@tonic-gate
15157c478bdstevel@tonic-gate	cp = resp->ypmatch_resp_valptr;
15167c478bdstevel@tonic-gate	sprintf(cp, "%s %s", inet_ntoa(*(struct in_addr *)&bestaddr), name);
15177c478bdstevel@tonic-gate	resp->ypmatch_resp_valsize = strlen(cp);
15187c478bdstevel@tonic-gate
15197c478bdstevel@tonic-gate	resp->ypmatch_resp_status = YP_TRUE;
15207c478bdstevel@tonic-gate
15217c478bdstevel@tonic-gate	return (1);
15227c478bdstevel@tonic-gate}
1523