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