xref: /titanic_44/usr/src/lib/libnsl/netdir/netdir.c (revision cb6207858a9fcc2feaee22e626912fba281ac969)
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
5*cb620785Sraf  * Common Development and Distribution License (the "License").
6*cb620785Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2161961e0fSrobinson 
227c478bd9Sstevel@tonic-gate /*
23*cb620785Sraf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
327c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * netdir.c
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * This is the library routines that do the name to address
417c478bd9Sstevel@tonic-gate  * translation.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include "mt.h"
457c478bd9Sstevel@tonic-gate #include "../rpc/rpc_mt.h"		/* for MT declarations only */
467c478bd9Sstevel@tonic-gate #include <stdio.h>
477c478bd9Sstevel@tonic-gate #include <sys/types.h>
487c478bd9Sstevel@tonic-gate #include <errno.h>
497c478bd9Sstevel@tonic-gate #include <tiuser.h>
507c478bd9Sstevel@tonic-gate #include <netdir.h>
517c478bd9Sstevel@tonic-gate #include <netconfig.h>
527c478bd9Sstevel@tonic-gate #include <string.h>
537c478bd9Sstevel@tonic-gate #include <sys/file.h>
547c478bd9Sstevel@tonic-gate #include <dlfcn.h>
5561961e0fSrobinson #include <stdlib.h>
567c478bd9Sstevel@tonic-gate #include <malloc.h>
577c478bd9Sstevel@tonic-gate #include <syslog.h>
587c478bd9Sstevel@tonic-gate #include <nss_netdir.h>
597c478bd9Sstevel@tonic-gate #include <netinet/in.h>
607c478bd9Sstevel@tonic-gate #include <netdb.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /* messaging stuff. */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate extern const char __nsl_dom[];
657c478bd9Sstevel@tonic-gate extern char *dgettext(const char *, const char *);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate struct translator {
687c478bd9Sstevel@tonic-gate 	struct nd_addrlist	*(*gbn)();	/* _netdir_getbyname	*/
697c478bd9Sstevel@tonic-gate 	struct nd_hostservlist 	*(*gba)();	/* _netdir_getbyaddr	*/
707c478bd9Sstevel@tonic-gate 	int			(*opt)();	/* _netdir_options	*/
717c478bd9Sstevel@tonic-gate 	char			*(*t2u)();	/* _taddr2uaddr		*/
727c478bd9Sstevel@tonic-gate 	struct netbuf		*(*u2t)();	/* _uaddr2taddr		*/
737c478bd9Sstevel@tonic-gate 	void			*tr_fd;		/* dyn library handle	*/
747c478bd9Sstevel@tonic-gate 	char			*tr_name;	/* Full path		*/
757c478bd9Sstevel@tonic-gate 	struct translator	*next;
767c478bd9Sstevel@tonic-gate };
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * xlate_lock protects xlate_list during updates only.  The xlate_list linked
807c478bd9Sstevel@tonic-gate  * list is pre-pended when new entries are added, so threads that are already
817c478bd9Sstevel@tonic-gate  * using the list will continue correctly to the end of the list.
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate static struct translator *xlate_list = NULL;
847c478bd9Sstevel@tonic-gate static mutex_t xlate_lock = DEFAULTMUTEX;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static struct translator *load_xlate(char *);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * This is the common data (global data) that is exported
907c478bd9Sstevel@tonic-gate  * by public interfaces. It has been moved here from nd_comdata.c
917c478bd9Sstevel@tonic-gate  * which no longer exists. This fixes the problem for applications
927c478bd9Sstevel@tonic-gate  * that do not link directly with -lnsl but dlopen a shared object
937c478bd9Sstevel@tonic-gate  * that has a NEEDED dependency on -lnsl and uses the netdir
947c478bd9Sstevel@tonic-gate  * interface.
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #undef	_nderror
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate int	_nderror;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate int *
__nderror(void)10261961e0fSrobinson __nderror(void)
1037c478bd9Sstevel@tonic-gate {
104*cb620785Sraf 	static pthread_key_t nderror_key = PTHREAD_ONCE_KEY_NP;
1057c478bd9Sstevel@tonic-gate 	int *ret;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if (thr_main())
1087c478bd9Sstevel@tonic-gate 		return (&_nderror);
1097c478bd9Sstevel@tonic-gate 	ret = thr_get_storage(&nderror_key, sizeof (int), free);
1107c478bd9Sstevel@tonic-gate 	/* if thr_get_storage fails we return the address of _nderror */
1117c478bd9Sstevel@tonic-gate 	return (ret ? ret : &_nderror);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #define	_nderror	(*(__nderror()))
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * Adds a translator library to the xlate_list, but first check to see if
1187c478bd9Sstevel@tonic-gate  * it's already on the list.  Must be called while holding xlate_lock.
1197c478bd9Sstevel@tonic-gate  * We have to be careful for the case of the same library being loaded
1207c478bd9Sstevel@tonic-gate  * with different names (e.g., straddr.so and /usr/lib/straddr.so).
1217c478bd9Sstevel@tonic-gate  * We check for this case by looking at the gbn and name fields.
1227c478bd9Sstevel@tonic-gate  * If the gbn address is the same, but the names are different, then we
1237c478bd9Sstevel@tonic-gate  * have accidentally reloaded the library.  We dlclose the new version,
1247c478bd9Sstevel@tonic-gate  * and then update 'translate' with the old versions of the symbols.
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate void
add_to_xlate_list(struct translator * translate)12761961e0fSrobinson add_to_xlate_list(struct translator *translate)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	struct translator	*t;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	for (t = xlate_list; t; t = t->next) {
1327c478bd9Sstevel@tonic-gate 		if (strcmp(translate->tr_name, t->tr_name) == 0) {
1337c478bd9Sstevel@tonic-gate 			return;
1347c478bd9Sstevel@tonic-gate 		}
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 	translate->next = xlate_list;
1377c478bd9Sstevel@tonic-gate 	xlate_list = translate;
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * This routine is the main routine that resolves host/service/xprt triples
1427c478bd9Sstevel@tonic-gate  * into a bunch of netbufs that should connect you to that particular
1437c478bd9Sstevel@tonic-gate  * service. RPC uses it to contact the binder service (rpcbind).
1447c478bd9Sstevel@tonic-gate  *
1457c478bd9Sstevel@tonic-gate  * In the interest of consistency with the gethost/servbyYY() routines,
1467c478bd9Sstevel@tonic-gate  * this routine calls a common interface _get_hostserv_inetnetdir_byname
1477c478bd9Sstevel@tonic-gate  * if it's called with a netconfig with "inet" type transports and
1487c478bd9Sstevel@tonic-gate  * an empty list of nametoaddr libs (i.e. a "-" in /etc/netconfig),
1497c478bd9Sstevel@tonic-gate  * which indicates the use of the switch. For non-inet transports or
1507c478bd9Sstevel@tonic-gate  * inet transports with nametoaddr libs specified, it simply calls
1517c478bd9Sstevel@tonic-gate  * the SVr4-classic netdir_getbyname, which loops through the libs.
1527c478bd9Sstevel@tonic-gate  *
1537c478bd9Sstevel@tonic-gate  * After all, any problem can be solved by one more layer of abstraction..
1547c478bd9Sstevel@tonic-gate  *
1557c478bd9Sstevel@tonic-gate  * This routine when called with a netconfig with "inet6" type of transports
1567c478bd9Sstevel@tonic-gate  * returns pure IPv6 addresses only and if no IPv6 address is found it
1577c478bd9Sstevel@tonic-gate  * returns none - Bug Id. 4276329
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate int
netdir_getbyname(struct netconfig * tp,struct nd_hostserv * serv,struct nd_addrlist ** addrs)16061961e0fSrobinson netdir_getbyname(struct netconfig *tp, struct nd_hostserv *serv,
16161961e0fSrobinson 						struct nd_addrlist **addrs)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	if (tp == 0) {
1647c478bd9Sstevel@tonic-gate 		_nderror = ND_BADARG;
1657c478bd9Sstevel@tonic-gate 		return (_nderror);
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 	if ((strcmp(tp->nc_protofmly, NC_INET) == 0) &&
1687c478bd9Sstevel@tonic-gate 						(tp->nc_nlookups == 0)) {
1697c478bd9Sstevel@tonic-gate 		struct	nss_netdirbyname_in nssin;
1707c478bd9Sstevel@tonic-gate 		union	nss_netdirbyname_out nssout;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 		nssin.op_t = NETDIR_BY;
1737c478bd9Sstevel@tonic-gate 		nssin.arg.nd_hs = serv;
1747c478bd9Sstevel@tonic-gate 		/*
1757c478bd9Sstevel@tonic-gate 		 * In code path of case NETDIR_BY,
1767c478bd9Sstevel@tonic-gate 		 * it also calls DOOR_GETIPNODEBYNAME_R.
1777c478bd9Sstevel@tonic-gate 		 * So af_family and flags are set to
1787c478bd9Sstevel@tonic-gate 		 * get V4 addresses only.
1797c478bd9Sstevel@tonic-gate 		 */
1807c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host6.af_family = AF_INET;
1817c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host6.flags = 0;
1827c478bd9Sstevel@tonic-gate 		nssout.nd_alist = addrs;
1837c478bd9Sstevel@tonic-gate 		return (_get_hostserv_inetnetdir_byname(tp, &nssin, &nssout));
18461961e0fSrobinson 	}
18561961e0fSrobinson 	if ((strcmp(tp->nc_protofmly, NC_INET6) == 0) &&
1867c478bd9Sstevel@tonic-gate 						(tp->nc_nlookups == 0)) {
1877c478bd9Sstevel@tonic-gate 		struct	nss_netdirbyname_in nssin;
1887c478bd9Sstevel@tonic-gate 		union	nss_netdirbyname_out nssout;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 		nssin.op_t = NETDIR_BY6;
1917c478bd9Sstevel@tonic-gate 		nssin.arg.nd_hs = serv;
1927c478bd9Sstevel@tonic-gate 		/* get both V4 & V6 addresses */
1937c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host6.af_family = AF_INET6;
1947c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host6.flags = (AI_ALL | AI_V4MAPPED);
1957c478bd9Sstevel@tonic-gate 		nssout.nd_alist = addrs;
1967c478bd9Sstevel@tonic-gate 		return (_get_hostserv_inetnetdir_byname(tp, &nssin, &nssout));
1977c478bd9Sstevel@tonic-gate 	}
19861961e0fSrobinson 	return (__classic_netdir_getbyname(tp, serv, addrs));
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  * This routine is the svr4_classic routine for resolving host/service/xprt
2037c478bd9Sstevel@tonic-gate  * triples into a bunch of netbufs that should connect you to that particular
2047c478bd9Sstevel@tonic-gate  * service. RPC uses it to contact the binder service (rpcbind).
2057c478bd9Sstevel@tonic-gate  *
2067c478bd9Sstevel@tonic-gate  * It's either called by the real netdir_getbyname() interface above
2077c478bd9Sstevel@tonic-gate  * or by gethost/servbyname when nametoaddr libs are specified in
2087c478bd9Sstevel@tonic-gate  * /etc/netconfig with an intent of bypassing the name service switch.
2097c478bd9Sstevel@tonic-gate  */
2107c478bd9Sstevel@tonic-gate int
__classic_netdir_getbyname(struct netconfig * tp,struct nd_hostserv * serv,struct nd_addrlist ** addrs)21161961e0fSrobinson __classic_netdir_getbyname(struct netconfig *tp, struct nd_hostserv *serv,
21261961e0fSrobinson 						struct nd_addrlist **addrs)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	struct translator	*t;	/* pointer to translator list	*/
2157c478bd9Sstevel@tonic-gate 	struct nd_addrlist	*nn;	/* the results			*/
2167c478bd9Sstevel@tonic-gate 	char			*lr;	/* routines to try		*/
2177c478bd9Sstevel@tonic-gate 	int			i;	/* counts the routines		*/
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	_nderror = ND_SYSTEM;
2207c478bd9Sstevel@tonic-gate 	for (i = 0; i < tp->nc_nlookups; i++) {
2217c478bd9Sstevel@tonic-gate 		lr = *((tp->nc_lookups) + i);
2227c478bd9Sstevel@tonic-gate 		for (t = xlate_list; t; t = t->next) {
2237c478bd9Sstevel@tonic-gate 			if (strcmp(lr, t->tr_name) == 0) {
2247c478bd9Sstevel@tonic-gate 				nn = (*(t->gbn))(tp, serv);
2257c478bd9Sstevel@tonic-gate 				if (nn) {
2267c478bd9Sstevel@tonic-gate 					*addrs = nn;
2277c478bd9Sstevel@tonic-gate 					return (0);
22861961e0fSrobinson 				}
2297c478bd9Sstevel@tonic-gate 				if (_nderror < 0) {
2307c478bd9Sstevel@tonic-gate 					return (_nderror);
2317c478bd9Sstevel@tonic-gate 				}
2327c478bd9Sstevel@tonic-gate 				break;
2337c478bd9Sstevel@tonic-gate 			}
2347c478bd9Sstevel@tonic-gate 		}
2357c478bd9Sstevel@tonic-gate 		/* If we didn't find it try loading it */
2367c478bd9Sstevel@tonic-gate 		if (!t) {
2377c478bd9Sstevel@tonic-gate 			if ((t = load_xlate(lr)) != NULL) {
2387c478bd9Sstevel@tonic-gate 				/* add it to the list */
23961961e0fSrobinson 				(void) mutex_lock(&xlate_lock);
2407c478bd9Sstevel@tonic-gate 				add_to_xlate_list(t);
24161961e0fSrobinson 				(void) mutex_unlock(&xlate_lock);
2427c478bd9Sstevel@tonic-gate 				nn = (*(t->gbn))(tp, serv);
2437c478bd9Sstevel@tonic-gate 				if (nn) {
2447c478bd9Sstevel@tonic-gate 					*addrs = nn;
2457c478bd9Sstevel@tonic-gate 					return (0);
2467c478bd9Sstevel@tonic-gate 				}
24761961e0fSrobinson 				if (_nderror < 0) {
24861961e0fSrobinson 					return (_nderror);
2497c478bd9Sstevel@tonic-gate 				}
2507c478bd9Sstevel@tonic-gate 			} else {
2517c478bd9Sstevel@tonic-gate 				if (_nderror == ND_SYSTEM) { /* retry cache */
2527c478bd9Sstevel@tonic-gate 					_nderror = ND_OK;
2537c478bd9Sstevel@tonic-gate 					i--;
2547c478bd9Sstevel@tonic-gate 					continue;
2557c478bd9Sstevel@tonic-gate 				}
2567c478bd9Sstevel@tonic-gate 			}
2577c478bd9Sstevel@tonic-gate 		}
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 	return (_nderror);	/* No one works */
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate /*
2637c478bd9Sstevel@tonic-gate  * This routine is similar to the one above except that it tries to resolve
2647c478bd9Sstevel@tonic-gate  * the name by the address passed.
2657c478bd9Sstevel@tonic-gate  */
2667c478bd9Sstevel@tonic-gate int
netdir_getbyaddr(struct netconfig * tp,struct nd_hostservlist ** serv,struct netbuf * addr)26761961e0fSrobinson netdir_getbyaddr(struct netconfig *tp, struct nd_hostservlist **serv,
26861961e0fSrobinson 							struct netbuf *addr)
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate 	if (tp == 0) {
2717c478bd9Sstevel@tonic-gate 		_nderror = ND_BADARG;
2727c478bd9Sstevel@tonic-gate 		return (_nderror);
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 	if ((strcmp(tp->nc_protofmly, NC_INET) == 0) &&
2757c478bd9Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
2767c478bd9Sstevel@tonic-gate 		struct	nss_netdirbyaddr_in nssin;
2777c478bd9Sstevel@tonic-gate 		union	nss_netdirbyaddr_out nssout;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 		nssin.op_t = NETDIR_BY;
2807c478bd9Sstevel@tonic-gate 		nssin.arg.nd_nbuf = addr;
2817c478bd9Sstevel@tonic-gate 		nssout.nd_hslist = serv;
2827c478bd9Sstevel@tonic-gate 		return (_get_hostserv_inetnetdir_byaddr(tp, &nssin, &nssout));
28361961e0fSrobinson 	}
28461961e0fSrobinson 	if ((strcmp(tp->nc_protofmly, NC_INET6) == 0) &&
2857c478bd9Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
2867c478bd9Sstevel@tonic-gate 		struct	nss_netdirbyaddr_in nssin;
2877c478bd9Sstevel@tonic-gate 		union	nss_netdirbyaddr_out nssout;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 		nssin.op_t = NETDIR_BY6;
2907c478bd9Sstevel@tonic-gate 		nssin.arg.nd_nbuf = addr;
2917c478bd9Sstevel@tonic-gate 		nssout.nd_hslist = serv;
2927c478bd9Sstevel@tonic-gate 		return (_get_hostserv_inetnetdir_byaddr(tp, &nssin, &nssout));
2937c478bd9Sstevel@tonic-gate 	}
29461961e0fSrobinson 	return (__classic_netdir_getbyaddr(tp, serv, addr));
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate  * This routine is similar to the one above except that it instructs the
2987c478bd9Sstevel@tonic-gate  * _get_hostserv_inetnetdir_byaddr not to do a service lookup.
2997c478bd9Sstevel@tonic-gate  */
3007c478bd9Sstevel@tonic-gate int
__netdir_getbyaddr_nosrv(struct netconfig * tp,struct nd_hostservlist ** serv,struct netbuf * addr)30161961e0fSrobinson __netdir_getbyaddr_nosrv(struct netconfig *tp, struct nd_hostservlist **serv,
30261961e0fSrobinson 							struct netbuf *addr)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	if (tp == 0) {
3057c478bd9Sstevel@tonic-gate 		_nderror = ND_BADARG;
3067c478bd9Sstevel@tonic-gate 		return (_nderror);
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 	if ((strcmp(tp->nc_protofmly, NC_INET) == 0) &&
3097c478bd9Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
3107c478bd9Sstevel@tonic-gate 		struct	nss_netdirbyaddr_in nssin;
3117c478bd9Sstevel@tonic-gate 		union	nss_netdirbyaddr_out nssout;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 		nssin.op_t = NETDIR_BY_NOSRV;
3147c478bd9Sstevel@tonic-gate 		nssin.arg.nd_nbuf = addr;
3157c478bd9Sstevel@tonic-gate 		nssout.nd_hslist = serv;
3167c478bd9Sstevel@tonic-gate 		return (_get_hostserv_inetnetdir_byaddr(tp, &nssin, &nssout));
31761961e0fSrobinson 	}
31861961e0fSrobinson 	if ((strcmp(tp->nc_protofmly, NC_INET6) == 0) &&
3197c478bd9Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
3207c478bd9Sstevel@tonic-gate 		struct	nss_netdirbyaddr_in nssin;
3217c478bd9Sstevel@tonic-gate 		union	nss_netdirbyaddr_out nssout;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 		nssin.op_t = NETDIR_BY_NOSRV6;
3247c478bd9Sstevel@tonic-gate 		nssin.arg.nd_nbuf = addr;
3257c478bd9Sstevel@tonic-gate 		nssout.nd_hslist = serv;
3267c478bd9Sstevel@tonic-gate 		return (_get_hostserv_inetnetdir_byaddr(tp, &nssin, &nssout));
3277c478bd9Sstevel@tonic-gate 	}
32861961e0fSrobinson 	return (__classic_netdir_getbyaddr(tp, serv, addr));
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate /*
3327c478bd9Sstevel@tonic-gate  * This routine is the svr4_classic routine for resolving a netbuf struct
3337c478bd9Sstevel@tonic-gate  * into a bunch of host/service name pairs.
3347c478bd9Sstevel@tonic-gate  *
3357c478bd9Sstevel@tonic-gate  * It's either called by the real netdir_getbyaddr() interface above
3367c478bd9Sstevel@tonic-gate  * or by gethost/servbyaddr when nametoaddr libs are specified in
3377c478bd9Sstevel@tonic-gate  * /etc/netconfig with an intent of bypassing the name service switch.
3387c478bd9Sstevel@tonic-gate  */
3397c478bd9Sstevel@tonic-gate int
__classic_netdir_getbyaddr(struct netconfig * tp,struct nd_hostservlist ** serv,struct netbuf * addr)34061961e0fSrobinson __classic_netdir_getbyaddr(struct netconfig *tp, struct nd_hostservlist **serv,
34161961e0fSrobinson 						struct netbuf *addr)
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate 	struct translator	*t;	/* pointer to translator list	*/
3447c478bd9Sstevel@tonic-gate 	struct nd_hostservlist	*hs;	/* the results			*/
3457c478bd9Sstevel@tonic-gate 	char			*lr;	/* routines to try		*/
3467c478bd9Sstevel@tonic-gate 	int			i;	/* counts the routines		*/
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	_nderror = ND_SYSTEM;
3497c478bd9Sstevel@tonic-gate 	for (i = 0; i < tp->nc_nlookups; i++) {
3507c478bd9Sstevel@tonic-gate 		lr = *((tp->nc_lookups) + i);
3517c478bd9Sstevel@tonic-gate 		for (t = xlate_list; t; t = t->next) {
3527c478bd9Sstevel@tonic-gate 			if (strcmp(lr, t->tr_name) == 0) {
3537c478bd9Sstevel@tonic-gate 				hs = (*(t->gba))(tp, addr);
3547c478bd9Sstevel@tonic-gate 				if (hs) {
3557c478bd9Sstevel@tonic-gate 					*serv = hs;
3567c478bd9Sstevel@tonic-gate 					return (0);
35761961e0fSrobinson 				}
35861961e0fSrobinson 				if (_nderror < 0)
3597c478bd9Sstevel@tonic-gate 					return (_nderror);
3607c478bd9Sstevel@tonic-gate 				break;
3617c478bd9Sstevel@tonic-gate 			}
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 		/* If we didn't find it try loading it */
3647c478bd9Sstevel@tonic-gate 		if (!t) {
3657c478bd9Sstevel@tonic-gate 			if ((t = load_xlate(lr)) != NULL) {
3667c478bd9Sstevel@tonic-gate 				/* add it to the list */
36761961e0fSrobinson 				(void) mutex_lock(&xlate_lock);
3687c478bd9Sstevel@tonic-gate 				add_to_xlate_list(t);
36961961e0fSrobinson 				(void) mutex_unlock(&xlate_lock);
3707c478bd9Sstevel@tonic-gate 				hs = (*(t->gba))(tp, addr);
3717c478bd9Sstevel@tonic-gate 				if (hs) {
3727c478bd9Sstevel@tonic-gate 					*serv = hs;
3737c478bd9Sstevel@tonic-gate 					return (0);
37461961e0fSrobinson 				}
37561961e0fSrobinson 				if (_nderror < 0)
3767c478bd9Sstevel@tonic-gate 					return (_nderror);
3777c478bd9Sstevel@tonic-gate 			} else {
3787c478bd9Sstevel@tonic-gate 				if (_nderror == ND_SYSTEM) { /* retry cache */
3797c478bd9Sstevel@tonic-gate 					_nderror = ND_OK;
3807c478bd9Sstevel@tonic-gate 					i--;
3817c478bd9Sstevel@tonic-gate 					continue;
3827c478bd9Sstevel@tonic-gate 				}
3837c478bd9Sstevel@tonic-gate 			}
3847c478bd9Sstevel@tonic-gate 		}
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 	return (_nderror);	/* No one works */
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate  * This is the library routine to do transport specific stuff.
3917c478bd9Sstevel@tonic-gate  * The code is same as the other similar routines except that it does
3927c478bd9Sstevel@tonic-gate  * not bother to try whole bunch of routines since if the first
3937c478bd9Sstevel@tonic-gate  * libray cannot resolve the option, then no one can.
3947c478bd9Sstevel@tonic-gate  *
3957c478bd9Sstevel@tonic-gate  * If it gets a netconfig structure for inet transports with nametoddr libs,
3967c478bd9Sstevel@tonic-gate  * it simply calls the inet-specific built in implementation.
3977c478bd9Sstevel@tonic-gate  */
3987c478bd9Sstevel@tonic-gate int
netdir_options(struct netconfig * tp,int option,int fd,char * par)39961961e0fSrobinson netdir_options(struct netconfig *tp, int option, int fd, char *par)
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate 	struct translator	*t;	/* pointer to translator list	*/
4027c478bd9Sstevel@tonic-gate 	char			*lr;	/* routines to try		*/
4037c478bd9Sstevel@tonic-gate 	int			i;	/* counts the routines		*/
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if (tp == 0) {
4067c478bd9Sstevel@tonic-gate 		_nderror = ND_BADARG;
4077c478bd9Sstevel@tonic-gate 		return (_nderror);
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	if ((strcmp(tp->nc_protofmly, NC_INET) == 0 ||
4117c478bd9Sstevel@tonic-gate 		strcmp(tp->nc_protofmly, NC_INET6) == 0) &&
4127c478bd9Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
4137c478bd9Sstevel@tonic-gate 		return (__inet_netdir_options(tp, option, fd, par));
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	for (i = 0; i < tp->nc_nlookups; i++) {
4187c478bd9Sstevel@tonic-gate 		lr = *((tp->nc_lookups) + i);
4197c478bd9Sstevel@tonic-gate 		for (t = xlate_list; t; t = t->next) {
42061961e0fSrobinson 			if (strcmp(lr, t->tr_name) == 0)
4217c478bd9Sstevel@tonic-gate 				return ((*(t->opt))(tp, option, fd, par));
4227c478bd9Sstevel@tonic-gate 		}
4237c478bd9Sstevel@tonic-gate 		/* If we didn't find it try loading it */
4247c478bd9Sstevel@tonic-gate 		if (!t) {
4257c478bd9Sstevel@tonic-gate 			if ((t = load_xlate(lr)) != NULL) {
4267c478bd9Sstevel@tonic-gate 				/* add it to the list */
42761961e0fSrobinson 				(void) mutex_lock(&xlate_lock);
4287c478bd9Sstevel@tonic-gate 				add_to_xlate_list(t);
42961961e0fSrobinson 				(void) mutex_unlock(&xlate_lock);
4307c478bd9Sstevel@tonic-gate 				return ((*(t->opt))(tp, option, fd, par));
43161961e0fSrobinson 			}
4327c478bd9Sstevel@tonic-gate 			if (_nderror == ND_SYSTEM) { /* retry cache */
4337c478bd9Sstevel@tonic-gate 				_nderror = ND_OK;
4347c478bd9Sstevel@tonic-gate 				i--;
4357c478bd9Sstevel@tonic-gate 				continue;
4367c478bd9Sstevel@tonic-gate 			}
4377c478bd9Sstevel@tonic-gate 		}
4387c478bd9Sstevel@tonic-gate 	}
4397c478bd9Sstevel@tonic-gate 	return (_nderror);	/* No one works */
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate /*
4437c478bd9Sstevel@tonic-gate  * This is the library routine for translating universal addresses to
4447c478bd9Sstevel@tonic-gate  * transport specific addresses. Again it uses the same code as above
4457c478bd9Sstevel@tonic-gate  * to search for the appropriate translation routine. Only it doesn't
4467c478bd9Sstevel@tonic-gate  * bother trying a whole bunch of routines since either the transport
4477c478bd9Sstevel@tonic-gate  * can translate it or it can't.
4487c478bd9Sstevel@tonic-gate  */
4497c478bd9Sstevel@tonic-gate struct netbuf *
uaddr2taddr(struct netconfig * tp,char * addr)45061961e0fSrobinson uaddr2taddr(struct netconfig *tp, char *addr)
4517c478bd9Sstevel@tonic-gate {
4527c478bd9Sstevel@tonic-gate 	struct translator	*t;	/* pointer to translator list 	*/
4537c478bd9Sstevel@tonic-gate 	struct netbuf		*x;	/* the answer we want 		*/
4547c478bd9Sstevel@tonic-gate 	char			*lr;	/* routines to try		*/
4557c478bd9Sstevel@tonic-gate 	int			i;	/* counts the routines		*/
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	if (tp == 0) {
4587c478bd9Sstevel@tonic-gate 		_nderror = ND_BADARG;
4597c478bd9Sstevel@tonic-gate 		return (0);
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate 	if ((strcmp(tp->nc_protofmly, NC_INET) == 0 ||
4627c478bd9Sstevel@tonic-gate 		strcmp(tp->nc_protofmly, NC_INET6) == 0) &&
4637c478bd9Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
4647c478bd9Sstevel@tonic-gate 		return (__inet_uaddr2taddr(tp, addr));
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 	for (i = 0; i < tp->nc_nlookups; i++) {
4677c478bd9Sstevel@tonic-gate 		lr = *((tp->nc_lookups) + i);
4687c478bd9Sstevel@tonic-gate 		for (t = xlate_list; t; t = t->next) {
4697c478bd9Sstevel@tonic-gate 			if (strcmp(lr, t->tr_name) == 0) {
4707c478bd9Sstevel@tonic-gate 				x = (*(t->u2t))(tp, addr);
47161961e0fSrobinson 				if (x)
4727c478bd9Sstevel@tonic-gate 					return (x);
47361961e0fSrobinson 				if (_nderror < 0)
4747c478bd9Sstevel@tonic-gate 					return (0);
4757c478bd9Sstevel@tonic-gate 			}
4767c478bd9Sstevel@tonic-gate 		}
4777c478bd9Sstevel@tonic-gate 		/* If we didn't find it try loading it */
4787c478bd9Sstevel@tonic-gate 		if (!t) {
4797c478bd9Sstevel@tonic-gate 			if ((t = load_xlate(lr)) != NULL) {
4807c478bd9Sstevel@tonic-gate 				/* add it to the list */
48161961e0fSrobinson 				(void) mutex_lock(&xlate_lock);
4827c478bd9Sstevel@tonic-gate 				add_to_xlate_list(t);
48361961e0fSrobinson 				(void) mutex_unlock(&xlate_lock);
4847c478bd9Sstevel@tonic-gate 				x = (*(t->u2t))(tp, addr);
48561961e0fSrobinson 				if (x)
4867c478bd9Sstevel@tonic-gate 					return (x);
48761961e0fSrobinson 				if (_nderror < 0)
4887c478bd9Sstevel@tonic-gate 					return (0);
4897c478bd9Sstevel@tonic-gate 			} else {
4907c478bd9Sstevel@tonic-gate 				if (_nderror == ND_SYSTEM) { /* retry cache */
4917c478bd9Sstevel@tonic-gate 					_nderror = ND_OK;
4927c478bd9Sstevel@tonic-gate 					i--;
4937c478bd9Sstevel@tonic-gate 					continue;
4947c478bd9Sstevel@tonic-gate 				}
4957c478bd9Sstevel@tonic-gate 			}
4967c478bd9Sstevel@tonic-gate 		}
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 	return (0);	/* No one works */
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate /*
5027c478bd9Sstevel@tonic-gate  * This is the library routine for translating transport specific
5037c478bd9Sstevel@tonic-gate  * addresses to universal addresses. Again it uses the same code as above
5047c478bd9Sstevel@tonic-gate  * to search for the appropriate translation routine. Only it doesn't
5057c478bd9Sstevel@tonic-gate  * bother trying a whole bunch of routines since either the transport
5067c478bd9Sstevel@tonic-gate  * can translate it or it can't.
5077c478bd9Sstevel@tonic-gate  */
5087c478bd9Sstevel@tonic-gate char *
taddr2uaddr(struct netconfig * tp,struct netbuf * addr)50961961e0fSrobinson taddr2uaddr(struct netconfig *tp, struct netbuf *addr)
5107c478bd9Sstevel@tonic-gate {
5117c478bd9Sstevel@tonic-gate 	struct translator	*t;	/* pointer to translator list	*/
5127c478bd9Sstevel@tonic-gate 	char			*lr;	/* routines to try		*/
5137c478bd9Sstevel@tonic-gate 	char			*x;	/* the answer			*/
5147c478bd9Sstevel@tonic-gate 	int			i;	/* counts the routines		*/
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	if (tp == 0) {
5177c478bd9Sstevel@tonic-gate 		_nderror = ND_BADARG;
5187c478bd9Sstevel@tonic-gate 		return (0);
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 	if ((strcmp(tp->nc_protofmly, NC_INET) == 0 ||
5217c478bd9Sstevel@tonic-gate 		strcmp(tp->nc_protofmly, NC_INET6) == 0) &&
5227c478bd9Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
5237c478bd9Sstevel@tonic-gate 		return (__inet_taddr2uaddr(tp, addr));
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate 	for (i = 0; i < tp->nc_nlookups; i++) {
5267c478bd9Sstevel@tonic-gate 		lr = *((tp->nc_lookups) + i);
5277c478bd9Sstevel@tonic-gate 		for (t = xlate_list; t; t = t->next) {
5287c478bd9Sstevel@tonic-gate 			if (strcmp(lr, t->tr_name) == 0) {
5297c478bd9Sstevel@tonic-gate 				x = (*(t->t2u))(tp, addr);
53061961e0fSrobinson 				if (x)
5317c478bd9Sstevel@tonic-gate 					return (x);
53261961e0fSrobinson 				if (_nderror < 0)
5337c478bd9Sstevel@tonic-gate 					return (0);
5347c478bd9Sstevel@tonic-gate 			}
5357c478bd9Sstevel@tonic-gate 		}
5367c478bd9Sstevel@tonic-gate 		/* If we didn't find it try loading it */
5377c478bd9Sstevel@tonic-gate 		if (!t) {
5387c478bd9Sstevel@tonic-gate 			if ((t = load_xlate(lr)) != NULL) {
5397c478bd9Sstevel@tonic-gate 				/* add it to the list */
54061961e0fSrobinson 				(void) mutex_lock(&xlate_lock);
5417c478bd9Sstevel@tonic-gate 				add_to_xlate_list(t);
54261961e0fSrobinson 				(void) mutex_unlock(&xlate_lock);
5437c478bd9Sstevel@tonic-gate 				x = (*(t->t2u))(tp, addr);
54461961e0fSrobinson 				if (x)
5457c478bd9Sstevel@tonic-gate 					return (x);
54661961e0fSrobinson 				if (_nderror < 0)
5477c478bd9Sstevel@tonic-gate 					return (0);
5487c478bd9Sstevel@tonic-gate 			} else {
5497c478bd9Sstevel@tonic-gate 				if (_nderror == ND_SYSTEM) { /* retry cache */
5507c478bd9Sstevel@tonic-gate 					_nderror = ND_OK;
5517c478bd9Sstevel@tonic-gate 					i--;
5527c478bd9Sstevel@tonic-gate 					continue;
5537c478bd9Sstevel@tonic-gate 				}
5547c478bd9Sstevel@tonic-gate 			}
5557c478bd9Sstevel@tonic-gate 		}
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 	return (0);	/* No one works */
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate /*
5617c478bd9Sstevel@tonic-gate  * This is the routine that frees the objects that these routines allocate.
5627c478bd9Sstevel@tonic-gate  */
5637c478bd9Sstevel@tonic-gate void
netdir_free(void * ptr,int type)56461961e0fSrobinson netdir_free(void *ptr, int type)
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate 	struct netbuf		*na;
5677c478bd9Sstevel@tonic-gate 	struct nd_addrlist	*nas;
5687c478bd9Sstevel@tonic-gate 	struct nd_hostserv	*hs;
5697c478bd9Sstevel@tonic-gate 	struct nd_hostservlist	*hss;
5707c478bd9Sstevel@tonic-gate 	int			i;
5717c478bd9Sstevel@tonic-gate 
57261961e0fSrobinson 	if (ptr == NULL)
5737c478bd9Sstevel@tonic-gate 		return;
5747c478bd9Sstevel@tonic-gate 	switch (type) {
5757c478bd9Sstevel@tonic-gate 	case ND_ADDR :
5767c478bd9Sstevel@tonic-gate 		na = (struct netbuf *)ptr;
5777c478bd9Sstevel@tonic-gate 		if (na->buf)
5787c478bd9Sstevel@tonic-gate 			free(na->buf);
57961961e0fSrobinson 		free(na);
5807c478bd9Sstevel@tonic-gate 		break;
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	case ND_ADDRLIST :
5837c478bd9Sstevel@tonic-gate 		nas = (struct nd_addrlist *)ptr;
5847c478bd9Sstevel@tonic-gate 		/*
5857c478bd9Sstevel@tonic-gate 		 * XXX: We do NOT try to free all individual netbuf->buf
5867c478bd9Sstevel@tonic-gate 		 * pointers. Free only the first one since they are allocated
5877c478bd9Sstevel@tonic-gate 		 * using one calloc in
5887c478bd9Sstevel@tonic-gate 		 * libnsl/nss/netdir_inet.c:order_haddrlist().
5897c478bd9Sstevel@tonic-gate 		 * This potentially causes memory leaks if a nametoaddr
5907c478bd9Sstevel@tonic-gate 		 * implementation -- from a third party -- has a different
5917c478bd9Sstevel@tonic-gate 		 * allocation scheme.
5927c478bd9Sstevel@tonic-gate 		 */
5937c478bd9Sstevel@tonic-gate 		if (nas->n_addrs->buf)
5947c478bd9Sstevel@tonic-gate 			free(nas->n_addrs->buf);
59561961e0fSrobinson 		free(nas->n_addrs);
59661961e0fSrobinson 		free(nas);
5977c478bd9Sstevel@tonic-gate 		break;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	case ND_HOSTSERV :
6007c478bd9Sstevel@tonic-gate 		hs = (struct nd_hostserv *)ptr;
6017c478bd9Sstevel@tonic-gate 		if (hs->h_host)
6027c478bd9Sstevel@tonic-gate 			free(hs->h_host);
6037c478bd9Sstevel@tonic-gate 		if (hs->h_serv)
6047c478bd9Sstevel@tonic-gate 			free(hs->h_serv);
60561961e0fSrobinson 		free(hs);
6067c478bd9Sstevel@tonic-gate 		break;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	case ND_HOSTSERVLIST :
6097c478bd9Sstevel@tonic-gate 		hss = (struct nd_hostservlist *)ptr;
6107c478bd9Sstevel@tonic-gate 		for (hs = hss->h_hostservs, i = 0; i < hss->h_cnt; i++, hs++) {
6117c478bd9Sstevel@tonic-gate 			if (hs->h_host)
6127c478bd9Sstevel@tonic-gate 				free(hs->h_host);
6137c478bd9Sstevel@tonic-gate 			if (hs->h_serv)
6147c478bd9Sstevel@tonic-gate 				free(hs->h_serv);
6157c478bd9Sstevel@tonic-gate 		}
61661961e0fSrobinson 		free(hss->h_hostservs);
61761961e0fSrobinson 		free(hss);
6187c478bd9Sstevel@tonic-gate 		break;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	default :
6217c478bd9Sstevel@tonic-gate 		_nderror = ND_UKNWN;
6227c478bd9Sstevel@tonic-gate 		break;
6237c478bd9Sstevel@tonic-gate 	}
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate /*
6277c478bd9Sstevel@tonic-gate  * load_xlate is a routine that will attempt to dynamically link in the
6287c478bd9Sstevel@tonic-gate  * file specified by the network configuration structure.
6297c478bd9Sstevel@tonic-gate  */
6307c478bd9Sstevel@tonic-gate static struct translator *
load_xlate(char * name)63161961e0fSrobinson load_xlate(char *name)
6327c478bd9Sstevel@tonic-gate {
6337c478bd9Sstevel@tonic-gate 	struct translator	*t;
6347c478bd9Sstevel@tonic-gate 	static struct xlate_list {
6357c478bd9Sstevel@tonic-gate 		char *library;
6367c478bd9Sstevel@tonic-gate 		struct xlate_list *next;
6377c478bd9Sstevel@tonic-gate 	} *xlistp = NULL;
6387c478bd9Sstevel@tonic-gate 	struct xlate_list *xlp, **xlastp;
6397c478bd9Sstevel@tonic-gate 	static mutex_t xlist_lock = DEFAULTMUTEX;
6407c478bd9Sstevel@tonic-gate 
64161961e0fSrobinson 	(void) mutex_lock(&xlist_lock);
6427c478bd9Sstevel@tonic-gate 	/*
6437c478bd9Sstevel@tonic-gate 	 * We maintain a list of libraries we have loaded.  Loading a library
6447c478bd9Sstevel@tonic-gate 	 * twice is double-plus ungood!
6457c478bd9Sstevel@tonic-gate 	 */
6467c478bd9Sstevel@tonic-gate 	for (xlp = xlistp, xlastp = &xlistp; xlp != NULL;
6477c478bd9Sstevel@tonic-gate 			xlastp = &xlp->next, xlp = xlp->next) {
6487c478bd9Sstevel@tonic-gate 		if (xlp->library != NULL) {
6497c478bd9Sstevel@tonic-gate 			if (strcmp(xlp->library, name) == 0) {
6507c478bd9Sstevel@tonic-gate 				_nderror = ND_SYSTEM;	/* seen this lib */
65161961e0fSrobinson 				(void) mutex_unlock(&xlist_lock);
6527c478bd9Sstevel@tonic-gate 				return (0);
6537c478bd9Sstevel@tonic-gate 			}
6547c478bd9Sstevel@tonic-gate 		}
6557c478bd9Sstevel@tonic-gate 	}
65661961e0fSrobinson 	t = malloc(sizeof (struct translator));
6577c478bd9Sstevel@tonic-gate 	if (!t) {
6587c478bd9Sstevel@tonic-gate 		_nderror = ND_NOMEM;
65961961e0fSrobinson 		(void) mutex_unlock(&xlist_lock);
6607c478bd9Sstevel@tonic-gate 		return (0);
6617c478bd9Sstevel@tonic-gate 	}
6627c478bd9Sstevel@tonic-gate 	t->tr_name = strdup(name);
6637c478bd9Sstevel@tonic-gate 	if (!t->tr_name) {
6647c478bd9Sstevel@tonic-gate 		_nderror = ND_NOMEM;
66561961e0fSrobinson 		free(t);
66661961e0fSrobinson 		(void) mutex_unlock(&xlist_lock);
6677c478bd9Sstevel@tonic-gate 		return (NULL);
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	t->tr_fd = dlopen(name, RTLD_LAZY);
6717c478bd9Sstevel@tonic-gate 	if (t->tr_fd == NULL) {
6727c478bd9Sstevel@tonic-gate 		_nderror = ND_OPEN;
6737c478bd9Sstevel@tonic-gate 		goto error;
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	/* Resolve the getbyname symbol */
6777c478bd9Sstevel@tonic-gate 	t->gbn = (struct nd_addrlist *(*)())dlsym(t->tr_fd,
6787c478bd9Sstevel@tonic-gate 				"_netdir_getbyname");
6797c478bd9Sstevel@tonic-gate 	if (!(t->gbn)) {
6807c478bd9Sstevel@tonic-gate 		_nderror = ND_NOSYM;
6817c478bd9Sstevel@tonic-gate 		goto error;
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	/* resolve the getbyaddr symbol */
6857c478bd9Sstevel@tonic-gate 	t->gba = (struct nd_hostservlist *(*)())dlsym(t->tr_fd,
6867c478bd9Sstevel@tonic-gate 				"_netdir_getbyaddr");
6877c478bd9Sstevel@tonic-gate 	if (!(t->gba)) {
6887c478bd9Sstevel@tonic-gate 		_nderror = ND_NOSYM;
6897c478bd9Sstevel@tonic-gate 		goto error;
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	/* resolve the taddr2uaddr symbol */
6937c478bd9Sstevel@tonic-gate 	t->t2u = (char *(*)())dlsym(t->tr_fd, "_taddr2uaddr");
6947c478bd9Sstevel@tonic-gate 	if (!(t->t2u)) {
6957c478bd9Sstevel@tonic-gate 		_nderror = ND_NOSYM;
6967c478bd9Sstevel@tonic-gate 		goto error;
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	/* resolve the uaddr2taddr symbol */
7007c478bd9Sstevel@tonic-gate 	t->u2t = (struct netbuf *(*)())dlsym(t->tr_fd, "_uaddr2taddr");
7017c478bd9Sstevel@tonic-gate 	if (!(t->u2t)) {
7027c478bd9Sstevel@tonic-gate 		_nderror = ND_NOSYM;
7037c478bd9Sstevel@tonic-gate 		goto error;
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	/* resolve the netdir_options symbol */
7077c478bd9Sstevel@tonic-gate 	t->opt = (int (*)())dlsym(t->tr_fd, "_netdir_options");
7087c478bd9Sstevel@tonic-gate 	if (!(t->opt)) {
7097c478bd9Sstevel@tonic-gate 		_nderror = ND_NOSYM;
7107c478bd9Sstevel@tonic-gate 		goto error;
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 	/*
7137c478bd9Sstevel@tonic-gate 	 * Add this library to the list of loaded libraries.
7147c478bd9Sstevel@tonic-gate 	 */
71561961e0fSrobinson 	*xlastp = malloc(sizeof (struct xlate_list));
7167c478bd9Sstevel@tonic-gate 	if (*xlastp == NULL) {
7177c478bd9Sstevel@tonic-gate 		_nderror = ND_NOMEM;
7187c478bd9Sstevel@tonic-gate 		goto error;
7197c478bd9Sstevel@tonic-gate 	}
7207c478bd9Sstevel@tonic-gate 	(*xlastp)->library = strdup(name);
7217c478bd9Sstevel@tonic-gate 	if ((*xlastp)->library == NULL) {
7227c478bd9Sstevel@tonic-gate 		_nderror = ND_NOMEM;
7237c478bd9Sstevel@tonic-gate 		free(*xlastp);
7247c478bd9Sstevel@tonic-gate 		goto error;
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 	(*xlastp)->next = NULL;
72761961e0fSrobinson 	(void) mutex_unlock(&xlist_lock);
7287c478bd9Sstevel@tonic-gate 	return (t);
7297c478bd9Sstevel@tonic-gate error:
7307c478bd9Sstevel@tonic-gate 	if (t->tr_fd != NULL)
7317c478bd9Sstevel@tonic-gate 		(void) dlclose(t->tr_fd);
7327c478bd9Sstevel@tonic-gate 	free(t->tr_name);
73361961e0fSrobinson 	free(t);
73461961e0fSrobinson 	(void) mutex_unlock(&xlist_lock);
7357c478bd9Sstevel@tonic-gate 	return (NULL);
7367c478bd9Sstevel@tonic-gate }
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate #define	NDERR_BUFSZ	512
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate /*
7427c478bd9Sstevel@tonic-gate  * This is a routine that returns a string related to the current
7437c478bd9Sstevel@tonic-gate  * error in _nderror.
7447c478bd9Sstevel@tonic-gate  */
7457c478bd9Sstevel@tonic-gate char *
netdir_sperror(void)74661961e0fSrobinson netdir_sperror(void)
7477c478bd9Sstevel@tonic-gate {
748*cb620785Sraf 	static pthread_key_t nderrbuf_key = PTHREAD_ONCE_KEY_NP;
7497c478bd9Sstevel@tonic-gate 	static char buf_main[NDERR_BUFSZ];
7507c478bd9Sstevel@tonic-gate 	char	*str;
7517c478bd9Sstevel@tonic-gate 	char *dlerrstr;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	str = thr_main()?
7547c478bd9Sstevel@tonic-gate 		buf_main :
7557c478bd9Sstevel@tonic-gate 		thr_get_storage(&nderrbuf_key, NDERR_BUFSZ, free);
75661961e0fSrobinson 	if (str == NULL)
7577c478bd9Sstevel@tonic-gate 		return (NULL);
7587c478bd9Sstevel@tonic-gate 	dlerrstr = dlerror();
7597c478bd9Sstevel@tonic-gate 	switch (_nderror) {
7607c478bd9Sstevel@tonic-gate 	case ND_NOMEM :
7617c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7627c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: memory allocation failed"));
7637c478bd9Sstevel@tonic-gate 		break;
7647c478bd9Sstevel@tonic-gate 	case ND_OK :
7657c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7667c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: successful completion"));
7677c478bd9Sstevel@tonic-gate 		break;
7687c478bd9Sstevel@tonic-gate 	case ND_NOHOST :
7697c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7707c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: hostname not found"));
7717c478bd9Sstevel@tonic-gate 		break;
7727c478bd9Sstevel@tonic-gate 	case ND_NOSERV :
7737c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7747c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: service name not found"));
7757c478bd9Sstevel@tonic-gate 		break;
7767c478bd9Sstevel@tonic-gate 	case ND_NOSYM :
7777c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ, "%s : %s ",
7787c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom,
7797c478bd9Sstevel@tonic-gate 			"n2a: symbol missing in shared object"),
7807c478bd9Sstevel@tonic-gate 			dlerrstr ? dlerrstr : " ");
7817c478bd9Sstevel@tonic-gate 		break;
7827c478bd9Sstevel@tonic-gate 	case ND_OPEN :
7837c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ, "%s - %s ",
7847c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: couldn't open shared object"),
7857c478bd9Sstevel@tonic-gate 			dlerrstr ? dlerrstr : " ");
7867c478bd9Sstevel@tonic-gate 		break;
7877c478bd9Sstevel@tonic-gate 	case ND_ACCESS :
7887c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7897c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom,
7907c478bd9Sstevel@tonic-gate 			"n2a: access denied for shared object"));
7917c478bd9Sstevel@tonic-gate 		break;
7927c478bd9Sstevel@tonic-gate 	case ND_UKNWN :
7937c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7947c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom,
7957c478bd9Sstevel@tonic-gate 			"n2a: attempt to free unknown object"));
7967c478bd9Sstevel@tonic-gate 		break;
7977c478bd9Sstevel@tonic-gate 	case ND_BADARG :
7987c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7997c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom,
8007c478bd9Sstevel@tonic-gate 			"n2a: bad arguments passed to routine"));
8017c478bd9Sstevel@tonic-gate 		break;
8027c478bd9Sstevel@tonic-gate 	case ND_NOCTRL:
8037c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
8047c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: unknown option passed"));
8057c478bd9Sstevel@tonic-gate 		break;
8067c478bd9Sstevel@tonic-gate 	case ND_FAILCTRL:
8077c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
8087c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: control operation failed"));
8097c478bd9Sstevel@tonic-gate 		break;
8107c478bd9Sstevel@tonic-gate 	case ND_SYSTEM:
8117c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ, "%s: %s",
8127c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: system error"),
8137c478bd9Sstevel@tonic-gate 			strerror(errno));
8147c478bd9Sstevel@tonic-gate 		break;
8157c478bd9Sstevel@tonic-gate 	default :
8167c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ, "%s#%d",
8177c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: unknown error "), _nderror);
8187c478bd9Sstevel@tonic-gate 		break;
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate 	return (str);
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate /*
8247c478bd9Sstevel@tonic-gate  * This is a routine that prints out strings related to the current
8257c478bd9Sstevel@tonic-gate  * error in _nderror. Like perror() it takes a string to print with a
8267c478bd9Sstevel@tonic-gate  * colon first.
8277c478bd9Sstevel@tonic-gate  */
8287c478bd9Sstevel@tonic-gate void
netdir_perror(char * s)82961961e0fSrobinson netdir_perror(char *s)
8307c478bd9Sstevel@tonic-gate {
8317c478bd9Sstevel@tonic-gate 	char	*err;
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	err = netdir_sperror();
8347c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s\n", s, err ? err : "n2a: error");
8357c478bd9Sstevel@tonic-gate }
836