xref: /illumos-gate/usr/src/lib/libnsl/yp/yp_all.c (revision 1da57d55)
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
2161961e0fSrobinson  */
2261961e0fSrobinson 
2361961e0fSrobinson /*
24*e8031f0aSraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
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
337c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of
347c478bd9Sstevel@tonic-gate  * California.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
37*e8031f0aSraf #include "mt.h"
3861961e0fSrobinson #include <stdlib.h>
3961961e0fSrobinson #include <unistd.h>
407c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
417c478bd9Sstevel@tonic-gate #include <syslog.h>
427c478bd9Sstevel@tonic-gate #include "yp_b.h"
437c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
447c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
457c478bd9Sstevel@tonic-gate #include <netdir.h>
467c478bd9Sstevel@tonic-gate #include <string.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate extern int __yp_dobind_cflookup(char *, struct dom_binding **, int);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static struct timeval tp_timout = { 120, 0};
517c478bd9Sstevel@tonic-gate static char nullstring[] = "\000";
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * __yp_all_cflookup() is a variant of the yp_all() code,
557c478bd9Sstevel@tonic-gate  * which adds a 'hardlookup' parameter. This parameter is passed
567c478bd9Sstevel@tonic-gate  * to __yp_dobind_cflookup(), and determines whether the server
577c478bd9Sstevel@tonic-gate  * binding attempt is hard (try forever) of soft (retry a compiled-
587c478bd9Sstevel@tonic-gate  * in number of times).
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate int
__yp_all_cflookup(char * domain,char * map,struct ypall_callback * callback,int hardlookup)6161961e0fSrobinson __yp_all_cflookup(char *domain, char *map, struct ypall_callback *callback,
6261961e0fSrobinson 								int hardlookup)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	size_t domlen;
657c478bd9Sstevel@tonic-gate 	size_t maplen;
667c478bd9Sstevel@tonic-gate 	struct ypreq_nokey req;
677c478bd9Sstevel@tonic-gate 	int reason;
687c478bd9Sstevel@tonic-gate 	struct dom_binding *pdomb;
697c478bd9Sstevel@tonic-gate 	enum clnt_stat s;
707c478bd9Sstevel@tonic-gate 	CLIENT *allc;
717c478bd9Sstevel@tonic-gate 	char server_name[MAXHOSTNAMELEN];
727c478bd9Sstevel@tonic-gate 	char errbuf[BUFSIZ];
737c478bd9Sstevel@tonic-gate 
7461961e0fSrobinson 	if ((map == NULL) || (domain == NULL))
757c478bd9Sstevel@tonic-gate 		return (YPERR_BADARGS);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	domlen = strlen(domain);
787c478bd9Sstevel@tonic-gate 	maplen = strlen(map);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	if ((domlen == 0) || (domlen > YPMAXDOMAIN) ||
817c478bd9Sstevel@tonic-gate 	    (maplen == 0) || (maplen > YPMAXMAP) ||
8261961e0fSrobinson 	    (callback == NULL))
837c478bd9Sstevel@tonic-gate 		return (YPERR_BADARGS);
847c478bd9Sstevel@tonic-gate 
8561961e0fSrobinson 	if (reason = __yp_dobind_cflookup(domain, &pdomb, hardlookup))
867c478bd9Sstevel@tonic-gate 		return (reason);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if (pdomb->dom_binding->ypbind_hi_vers < YPVERS) {
897c478bd9Sstevel@tonic-gate 		__yp_rel_binding(pdomb);
907c478bd9Sstevel@tonic-gate 		return (YPERR_VERS);
917c478bd9Sstevel@tonic-gate 	}
9261961e0fSrobinson 	(void) mutex_lock(&pdomb->server_name_lock);
937c478bd9Sstevel@tonic-gate 	if (!pdomb->dom_binding->ypbind_servername) {
9461961e0fSrobinson 		(void) mutex_unlock(&pdomb->server_name_lock);
957c478bd9Sstevel@tonic-gate 		__yp_rel_binding(pdomb);
967c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "yp_all: failed to get server's name\n");
977c478bd9Sstevel@tonic-gate 		return (YPERR_RPC);
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 	(void) strcpy(server_name, pdomb->dom_binding->ypbind_servername);
10061961e0fSrobinson 	(void) mutex_unlock(&pdomb->server_name_lock);
1017c478bd9Sstevel@tonic-gate 	if (strcmp(server_name, nullstring) == 0) {
1027c478bd9Sstevel@tonic-gate 		/*
1037c478bd9Sstevel@tonic-gate 		 * This is the case where ypbind is running in broadcast mode,
1047c478bd9Sstevel@tonic-gate 		 * we have to do the jugglery to get the
1057c478bd9Sstevel@tonic-gate 		 * ypserv's address on COTS transport based
1067c478bd9Sstevel@tonic-gate 		 * on the CLTS address ypbind gave us !
1077c478bd9Sstevel@tonic-gate 		 */
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 		struct nd_hostservlist *nhs;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 		if (netdir_getbyaddr(pdomb->dom_binding->ypbind_nconf,
1127c478bd9Sstevel@tonic-gate 			&nhs, pdomb->dom_binding->ypbind_svcaddr) != ND_OK) {
1137c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
1147c478bd9Sstevel@tonic-gate 				"yp_all: failed to get server's name\n");
1157c478bd9Sstevel@tonic-gate 			__yp_rel_binding(pdomb);
1167c478bd9Sstevel@tonic-gate 			return (YPERR_RPC);
1177c478bd9Sstevel@tonic-gate 		}
1187c478bd9Sstevel@tonic-gate 		/* check server name again, some other thread may have set it */
11961961e0fSrobinson 		(void) mutex_lock(&pdomb->server_name_lock);
1207c478bd9Sstevel@tonic-gate 		if (strcmp(pdomb->dom_binding->ypbind_servername,
1217c478bd9Sstevel@tonic-gate 					nullstring) == 0) {
1227c478bd9Sstevel@tonic-gate 			pdomb->dom_binding->ypbind_servername =
1237c478bd9Sstevel@tonic-gate 				(char *)strdup(nhs->h_hostservs->h_host);
1247c478bd9Sstevel@tonic-gate 		}
1257c478bd9Sstevel@tonic-gate 		(void) strcpy(server_name,
1267c478bd9Sstevel@tonic-gate 		    pdomb->dom_binding->ypbind_servername);
12761961e0fSrobinson 		(void) mutex_unlock(&pdomb->server_name_lock);
1287c478bd9Sstevel@tonic-gate 		netdir_free((char *)nhs, ND_HOSTSERVLIST);
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 	__yp_rel_binding(pdomb);
1317c478bd9Sstevel@tonic-gate 	if ((allc = clnt_create(server_name, YPPROG,
13261961e0fSrobinson 		YPVERS, "circuit_n")) == NULL) {
13361961e0fSrobinson 			(void) snprintf(errbuf, BUFSIZ, "yp_all \
1347c478bd9Sstevel@tonic-gate - transport level create failure for domain %s / map %s", domain, map);
13561961e0fSrobinson 			syslog(LOG_ERR, "%s", clnt_spcreateerror(errbuf));
1367c478bd9Sstevel@tonic-gate 			return (YPERR_RPC);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	req.domain = domain;
1407c478bd9Sstevel@tonic-gate 	req.map = map;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	s = clnt_call(allc, YPPROC_ALL,
1447c478bd9Sstevel@tonic-gate 		(xdrproc_t)xdr_ypreq_nokey, (char *)&req,
1457c478bd9Sstevel@tonic-gate 	    (xdrproc_t)xdr_ypall, (char *)callback, tp_timout);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if (s != RPC_SUCCESS && s != RPC_TIMEDOUT) {
14861961e0fSrobinson 		syslog(LOG_ERR, "%s", clnt_sperror(allc,
1497c478bd9Sstevel@tonic-gate 		    "yp_all - RPC clnt_call (transport level) failure"));
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	clnt_destroy(allc);
1537c478bd9Sstevel@tonic-gate 	switch (s) {
1547c478bd9Sstevel@tonic-gate 	case RPC_SUCCESS:
1557c478bd9Sstevel@tonic-gate 		return (0);
1567c478bd9Sstevel@tonic-gate 	case RPC_TIMEDOUT:
1577c478bd9Sstevel@tonic-gate 		return (YPERR_YPSERV);
1587c478bd9Sstevel@tonic-gate 	default:
1597c478bd9Sstevel@tonic-gate 		return (YPERR_RPC);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate  * This does the "glommed enumeration" stuff.  callback->foreach is the name
1667c478bd9Sstevel@tonic-gate  * of a function which gets called per decoded key-value pair:
1677c478bd9Sstevel@tonic-gate  *
1687c478bd9Sstevel@tonic-gate  * (*callback->foreach)(status, key, keylen, val, vallen, callback->data);
1697c478bd9Sstevel@tonic-gate  *
1707c478bd9Sstevel@tonic-gate  * If the server we get back from __yp_dobind speaks the old protocol, this
1717c478bd9Sstevel@tonic-gate  * returns YPERR_VERS, and does not attempt to emulate the new functionality
1727c478bd9Sstevel@tonic-gate  * by using the old protocol.
1737c478bd9Sstevel@tonic-gate  */
1747c478bd9Sstevel@tonic-gate int
yp_all(char * domain,char * map,struct ypall_callback * callback)17561961e0fSrobinson yp_all(char *domain, char *map, struct ypall_callback *callback)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	return (__yp_all_cflookup(domain, map, callback, 1));
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * This function is identical to 'yp_all' with the exception that it
1837c478bd9Sstevel@tonic-gate  * attempts to use reserve ports.
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate int
__yp_all_rsvdport(char * domain,char * map,struct ypall_callback * callback)18661961e0fSrobinson __yp_all_rsvdport(char *domain, char *map, struct ypall_callback *callback)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	size_t domlen;
1897c478bd9Sstevel@tonic-gate 	size_t maplen;
1907c478bd9Sstevel@tonic-gate 	struct ypreq_nokey req;
1917c478bd9Sstevel@tonic-gate 	int reason;
1927c478bd9Sstevel@tonic-gate 	struct dom_binding *pdomb;
1937c478bd9Sstevel@tonic-gate 	enum clnt_stat s;
1947c478bd9Sstevel@tonic-gate 	CLIENT *allc;
1957c478bd9Sstevel@tonic-gate 	char server_name[MAXHOSTNAMELEN];
1967c478bd9Sstevel@tonic-gate 	char errbuf[BUFSIZ];
1977c478bd9Sstevel@tonic-gate 
19861961e0fSrobinson 	if ((map == NULL) || (domain == NULL))
1997c478bd9Sstevel@tonic-gate 		return (YPERR_BADARGS);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	domlen =  strlen(domain);
2027c478bd9Sstevel@tonic-gate 	maplen =  strlen(map);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	if ((domlen == 0) || (domlen > YPMAXDOMAIN) ||
2057c478bd9Sstevel@tonic-gate 	    (maplen == 0) || (maplen > YPMAXMAP) ||
20661961e0fSrobinson 	    (callback == NULL))
2077c478bd9Sstevel@tonic-gate 		return (YPERR_BADARGS);
2087c478bd9Sstevel@tonic-gate 
20961961e0fSrobinson 	if (reason = __yp_dobind_rsvdport(domain, &pdomb))
2107c478bd9Sstevel@tonic-gate 		return (reason);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	if (pdomb->dom_binding->ypbind_hi_vers < YPVERS) {
2137c478bd9Sstevel@tonic-gate 		/*
2147c478bd9Sstevel@tonic-gate 		 * Have to free the binding since the reserved
2157c478bd9Sstevel@tonic-gate 		 * port bindings are not cached.
2167c478bd9Sstevel@tonic-gate 		 */
2177c478bd9Sstevel@tonic-gate 		__yp_rel_binding(pdomb);
2187c478bd9Sstevel@tonic-gate 		free_dom_binding(pdomb);
2197c478bd9Sstevel@tonic-gate 		return (YPERR_VERS);
2207c478bd9Sstevel@tonic-gate 	}
22161961e0fSrobinson 	(void) mutex_lock(&pdomb->server_name_lock);
2227c478bd9Sstevel@tonic-gate 	if (!pdomb->dom_binding->ypbind_servername) {
22361961e0fSrobinson 		(void) mutex_unlock(&pdomb->server_name_lock);
2247c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "yp_all: failed to get server's name\n");
2257c478bd9Sstevel@tonic-gate 		__yp_rel_binding(pdomb);
2267c478bd9Sstevel@tonic-gate 		free_dom_binding(pdomb);
2277c478bd9Sstevel@tonic-gate 		return (YPERR_RPC);
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 	(void) strcpy(server_name, pdomb->dom_binding->ypbind_servername);
23061961e0fSrobinson 	(void) mutex_unlock(&pdomb->server_name_lock);
2317c478bd9Sstevel@tonic-gate 	if (strcmp(server_name, nullstring) == 0) {
2327c478bd9Sstevel@tonic-gate 		/*
2337c478bd9Sstevel@tonic-gate 		 * This is the case where ypbind is running in broadcast mode,
2347c478bd9Sstevel@tonic-gate 		 * we have to do the jugglery to get the
2357c478bd9Sstevel@tonic-gate 		 * ypserv's address on COTS transport based
2367c478bd9Sstevel@tonic-gate 		 * on the CLTS address ypbind gave us !
2377c478bd9Sstevel@tonic-gate 		 */
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 		struct nd_hostservlist *nhs;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 		if (netdir_getbyaddr(pdomb->dom_binding->ypbind_nconf,
2427c478bd9Sstevel@tonic-gate 			&nhs, pdomb->dom_binding->ypbind_svcaddr) != ND_OK) {
2437c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
2447c478bd9Sstevel@tonic-gate 				"yp_all: failed to get server's name\n");
2457c478bd9Sstevel@tonic-gate 			__yp_rel_binding(pdomb);
2467c478bd9Sstevel@tonic-gate 			free_dom_binding(pdomb);
2477c478bd9Sstevel@tonic-gate 			return (YPERR_RPC);
2487c478bd9Sstevel@tonic-gate 		}
2497c478bd9Sstevel@tonic-gate 		/* check server name again, some other thread may have set it */
25061961e0fSrobinson 		(void) mutex_lock(&pdomb->server_name_lock);
2517c478bd9Sstevel@tonic-gate 		if (strcmp(pdomb->dom_binding->ypbind_servername,
2527c478bd9Sstevel@tonic-gate 					nullstring) == 0) {
2537c478bd9Sstevel@tonic-gate 			pdomb->dom_binding->ypbind_servername =
2547c478bd9Sstevel@tonic-gate 			(char *)strdup(nhs->h_hostservs->h_host);
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate 		(void) strcpy(server_name,
2577c478bd9Sstevel@tonic-gate 		    pdomb->dom_binding->ypbind_servername);
25861961e0fSrobinson 		(void) mutex_unlock(&pdomb->server_name_lock);
2597c478bd9Sstevel@tonic-gate 		netdir_free((char *)nhs, ND_HOSTSERVLIST);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 	__yp_rel_binding(pdomb);
2637c478bd9Sstevel@tonic-gate 	if ((allc = __yp_clnt_create_rsvdport(server_name, YPPROG, YPVERS,
26461961e0fSrobinson 	    "tcp6", 0, 0)) == NULL &&
2657c478bd9Sstevel@tonic-gate 		(allc = __yp_clnt_create_rsvdport(server_name, YPPROG, YPVERS,
26661961e0fSrobinson 	    "tcp", 0, 0)) == NULL) {
26761961e0fSrobinson 		(void) snprintf(errbuf, BUFSIZ, "yp_all \
2687c478bd9Sstevel@tonic-gate - transport level create failure for domain %s / map %s", domain, map);
26961961e0fSrobinson 		syslog(LOG_ERR, "%s", clnt_spcreateerror(errbuf));
2707c478bd9Sstevel@tonic-gate 		free_dom_binding(pdomb);
2717c478bd9Sstevel@tonic-gate 		return (YPERR_RPC);
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	req.domain = domain;
2757c478bd9Sstevel@tonic-gate 	req.map = map;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	s = clnt_call(allc, YPPROC_ALL,
2787c478bd9Sstevel@tonic-gate 		(xdrproc_t)xdr_ypreq_nokey, (char *)&req,
2797c478bd9Sstevel@tonic-gate 	    (xdrproc_t)xdr_ypall, (char *)callback, tp_timout);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	if (s != RPC_SUCCESS && s != RPC_TIMEDOUT) {
28261961e0fSrobinson 		syslog(LOG_ERR, "%s", clnt_sperror(allc,
2837c478bd9Sstevel@tonic-gate 		    "yp_all - RPC clnt_call (transport level) failure"));
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	clnt_destroy(allc);
2877c478bd9Sstevel@tonic-gate 	free_dom_binding(pdomb);
2887c478bd9Sstevel@tonic-gate 	switch (s) {
2897c478bd9Sstevel@tonic-gate 	case RPC_SUCCESS:
2907c478bd9Sstevel@tonic-gate 		return (0);
2917c478bd9Sstevel@tonic-gate 	case RPC_TIMEDOUT:
2927c478bd9Sstevel@tonic-gate 		return (YPERR_YPSERV);
2937c478bd9Sstevel@tonic-gate 	default:
2947c478bd9Sstevel@tonic-gate 		return (YPERR_RPC);
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate }
297