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