xref: /illumos-gate/usr/src/lib/libnsl/netdir/netdir.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
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 /*
367c478bd9Sstevel@tonic-gate  * netdir.c
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * This is the library routines that do the name to address
397c478bd9Sstevel@tonic-gate  * translation.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "mt.h"
437c478bd9Sstevel@tonic-gate #include "../rpc/rpc_mt.h"		/* for MT declarations only */
447c478bd9Sstevel@tonic-gate #include <stdio.h>
457c478bd9Sstevel@tonic-gate #include <sys/types.h>
467c478bd9Sstevel@tonic-gate #include <errno.h>
477c478bd9Sstevel@tonic-gate #include <tiuser.h>
487c478bd9Sstevel@tonic-gate #include <netdir.h>
497c478bd9Sstevel@tonic-gate #include <netconfig.h>
507c478bd9Sstevel@tonic-gate #include <string.h>
517c478bd9Sstevel@tonic-gate #include <sys/file.h>
527c478bd9Sstevel@tonic-gate #include <dlfcn.h>
5361961e0fSrobinson #include <stdlib.h>
547c478bd9Sstevel@tonic-gate #include <malloc.h>
557c478bd9Sstevel@tonic-gate #include <syslog.h>
567c478bd9Sstevel@tonic-gate #include <nss_netdir.h>
577c478bd9Sstevel@tonic-gate #include <netinet/in.h>
587c478bd9Sstevel@tonic-gate #include <netdb.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /* messaging stuff. */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate extern const char __nsl_dom[];
637c478bd9Sstevel@tonic-gate extern char *dgettext(const char *, const char *);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate struct translator {
667c478bd9Sstevel@tonic-gate 	struct nd_addrlist	*(*gbn)();	/* _netdir_getbyname	*/
677c478bd9Sstevel@tonic-gate 	struct nd_hostservlist 	*(*gba)();	/* _netdir_getbyaddr	*/
687c478bd9Sstevel@tonic-gate 	int			(*opt)();	/* _netdir_options	*/
697c478bd9Sstevel@tonic-gate 	char			*(*t2u)();	/* _taddr2uaddr		*/
707c478bd9Sstevel@tonic-gate 	struct netbuf		*(*u2t)();	/* _uaddr2taddr		*/
717c478bd9Sstevel@tonic-gate 	void			*tr_fd;		/* dyn library handle	*/
727c478bd9Sstevel@tonic-gate 	char			*tr_name;	/* Full path		*/
737c478bd9Sstevel@tonic-gate 	struct translator	*next;
747c478bd9Sstevel@tonic-gate };
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * xlate_lock protects xlate_list during updates only.  The xlate_list linked
787c478bd9Sstevel@tonic-gate  * list is pre-pended when new entries are added, so threads that are already
797c478bd9Sstevel@tonic-gate  * using the list will continue correctly to the end of the list.
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate static struct translator *xlate_list = NULL;
827c478bd9Sstevel@tonic-gate static mutex_t xlate_lock = DEFAULTMUTEX;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static struct translator *load_xlate(char *);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * This is the common data (global data) that is exported
887c478bd9Sstevel@tonic-gate  * by public interfaces. It has been moved here from nd_comdata.c
897c478bd9Sstevel@tonic-gate  * which no longer exists. This fixes the problem for applications
907c478bd9Sstevel@tonic-gate  * that do not link directly with -lnsl but dlopen a shared object
917c478bd9Sstevel@tonic-gate  * that has a NEEDED dependency on -lnsl and uses the netdir
927c478bd9Sstevel@tonic-gate  * interface.
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #undef	_nderror
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate int	_nderror;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate int *
__nderror(void)10061961e0fSrobinson __nderror(void)
1017c478bd9Sstevel@tonic-gate {
102*cb620785Sraf 	static pthread_key_t nderror_key = PTHREAD_ONCE_KEY_NP;
1037c478bd9Sstevel@tonic-gate 	int *ret;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if (thr_main())
1067c478bd9Sstevel@tonic-gate 		return (&_nderror);
1077c478bd9Sstevel@tonic-gate 	ret = thr_get_storage(&nderror_key, sizeof (int), free);
1087c478bd9Sstevel@tonic-gate 	/* if thr_get_storage fails we return the address of _nderror */
1097c478bd9Sstevel@tonic-gate 	return (ret ? ret : &_nderror);
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate #define	_nderror	(*(__nderror()))
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * Adds a translator library to the xlate_list, but first check to see if
1167c478bd9Sstevel@tonic-gate  * it's already on the list.  Must be called while holding xlate_lock.
1177c478bd9Sstevel@tonic-gate  * We have to be careful for the case of the same library being loaded
1187c478bd9Sstevel@tonic-gate  * with different names (e.g., straddr.so and /usr/lib/straddr.so).
1197c478bd9Sstevel@tonic-gate  * We check for this case by looking at the gbn and name fields.
1207c478bd9Sstevel@tonic-gate  * If the gbn address is the same, but the names are different, then we
1217c478bd9Sstevel@tonic-gate  * have accidentally reloaded the library.  We dlclose the new version,
1227c478bd9Sstevel@tonic-gate  * and then update 'translate' with the old versions of the symbols.
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate void
add_to_xlate_list(struct translator * translate)12561961e0fSrobinson add_to_xlate_list(struct translator *translate)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	struct translator	*t;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	for (t = xlate_list; t; t = t->next) {
1307c478bd9Sstevel@tonic-gate 		if (strcmp(translate->tr_name, t->tr_name) == 0) {
1317c478bd9Sstevel@tonic-gate 			return;
1327c478bd9Sstevel@tonic-gate 		}
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 	translate->next = xlate_list;
1357c478bd9Sstevel@tonic-gate 	xlate_list = translate;
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * This routine is the main routine that resolves host/service/xprt triples
1407c478bd9Sstevel@tonic-gate  * into a bunch of netbufs that should connect you to that particular
1417c478bd9Sstevel@tonic-gate  * service. RPC uses it to contact the binder service (rpcbind).
1427c478bd9Sstevel@tonic-gate  *
1437c478bd9Sstevel@tonic-gate  * In the interest of consistency with the gethost/servbyYY() routines,
1447c478bd9Sstevel@tonic-gate  * this routine calls a common interface _get_hostserv_inetnetdir_byname
1457c478bd9Sstevel@tonic-gate  * if it's called with a netconfig with "inet" type transports and
1467c478bd9Sstevel@tonic-gate  * an empty list of nametoaddr libs (i.e. a "-" in /etc/netconfig),
1477c478bd9Sstevel@tonic-gate  * which indicates the use of the switch. For non-inet transports or
1487c478bd9Sstevel@tonic-gate  * inet transports with nametoaddr libs specified, it simply calls
1497c478bd9Sstevel@tonic-gate  * the SVr4-classic netdir_getbyname, which loops through the libs.
1507c478bd9Sstevel@tonic-gate  *
1517c478bd9Sstevel@tonic-gate  * After all, any problem can be solved by one more layer of abstraction..
1527c478bd9Sstevel@tonic-gate  *
1537c478bd9Sstevel@tonic-gate  * This routine when called with a netconfig with "inet6" type of transports
1547c478bd9Sstevel@tonic-gate  * returns pure IPv6 addresses only and if no IPv6 address is found it
1557c478bd9Sstevel@tonic-gate  * returns none - Bug Id. 4276329
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate int
netdir_getbyname(struct netconfig * tp,struct nd_hostserv * serv,struct nd_addrlist ** addrs)15861961e0fSrobinson netdir_getbyname(struct netconfig *tp, struct nd_hostserv *serv,
15961961e0fSrobinson 						struct nd_addrlist **addrs)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	if (tp == 0) {
1627c478bd9Sstevel@tonic-gate 		_nderror = ND_BADARG;
1637c478bd9Sstevel@tonic-gate 		return (_nderror);
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 	if ((strcmp(tp->nc_protofmly, NC_INET) == 0) &&
16661961e0fSrobinson 						(tp->nc_nlookups == 0)) {
1677c478bd9Sstevel@tonic-gate 		struct	nss_netdirbyname_in nssin;
1687c478bd9Sstevel@tonic-gate 		union	nss_netdirbyname_out nssout;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 		nssin.op_t = NETDIR_BY;
1717c478bd9Sstevel@tonic-gate 		nssin.arg.nd_hs = serv;
1727c478bd9Sstevel@tonic-gate 		/*
1737c478bd9Sstevel@tonic-gate 		 * In code path of case NETDIR_BY,
1747c478bd9Sstevel@tonic-gate 		 * it also calls DOOR_GETIPNODEBYNAME_R.
1757c478bd9Sstevel@tonic-gate 		 * So af_family and flags are set to
1767c478bd9Sstevel@tonic-gate 		 * get V4 addresses only.
1777c478bd9Sstevel@tonic-gate 		 */
1787c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host6.af_family = AF_INET;
1797c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host6.flags = 0;
1807c478bd9Sstevel@tonic-gate 		nssout.nd_alist = addrs;
1817c478bd9Sstevel@tonic-gate 		return (_get_hostserv_inetnetdir_byname(tp, &nssin, &nssout));
18261961e0fSrobinson 	}
18361961e0fSrobinson 	if ((strcmp(tp->nc_protofmly, NC_INET6) == 0) &&
18461961e0fSrobinson 						(tp->nc_nlookups == 0)) {
1857c478bd9Sstevel@tonic-gate 		struct	nss_netdirbyname_in nssin;
1867c478bd9Sstevel@tonic-gate 		union	nss_netdirbyname_out nssout;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		nssin.op_t = NETDIR_BY6;
1897c478bd9Sstevel@tonic-gate 		nssin.arg.nd_hs = serv;
1907c478bd9Sstevel@tonic-gate 		/* get both V4 & V6 addresses */
1917c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host6.af_family = AF_INET6;
1927c478bd9Sstevel@tonic-gate 		nssin.arg.nss.host6.flags = (AI_ALL | AI_V4MAPPED);
1937c478bd9Sstevel@tonic-gate 		nssout.nd_alist = addrs;
1947c478bd9Sstevel@tonic-gate 		return (_get_hostserv_inetnetdir_byname(tp, &nssin, &nssout));
1957c478bd9Sstevel@tonic-gate 	}
19661961e0fSrobinson 	return (__classic_netdir_getbyname(tp, serv, addrs));
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /*
2007c478bd9Sstevel@tonic-gate  * This routine is the svr4_classic routine for resolving host/service/xprt
2017c478bd9Sstevel@tonic-gate  * triples into a bunch of netbufs that should connect you to that particular
2027c478bd9Sstevel@tonic-gate  * service. RPC uses it to contact the binder service (rpcbind).
2037c478bd9Sstevel@tonic-gate  *
2047c478bd9Sstevel@tonic-gate  * It's either called by the real netdir_getbyname() interface above
2057c478bd9Sstevel@tonic-gate  * or by gethost/servbyname when nametoaddr libs are specified in
2067c478bd9Sstevel@tonic-gate  * /etc/netconfig with an intent of bypassing the name service switch.
2077c478bd9Sstevel@tonic-gate  */
2087c478bd9Sstevel@tonic-gate int
__classic_netdir_getbyname(struct netconfig * tp,struct nd_hostserv * serv,struct nd_addrlist ** addrs)20961961e0fSrobinson __classic_netdir_getbyname(struct netconfig *tp, struct nd_hostserv *serv,
21061961e0fSrobinson 						struct nd_addrlist **addrs)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate 	struct translator	*t;	/* pointer to translator list	*/
2137c478bd9Sstevel@tonic-gate 	struct nd_addrlist	*nn;	/* the results			*/
2147c478bd9Sstevel@tonic-gate 	char			*lr;	/* routines to try		*/
2157c478bd9Sstevel@tonic-gate 	int			i;	/* counts the routines		*/
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	_nderror = ND_SYSTEM;
2187c478bd9Sstevel@tonic-gate 	for (i = 0; i < tp->nc_nlookups; i++) {
2197c478bd9Sstevel@tonic-gate 		lr = *((tp->nc_lookups) + i);
2207c478bd9Sstevel@tonic-gate 		for (t = xlate_list; t; t = t->next) {
2217c478bd9Sstevel@tonic-gate 			if (strcmp(lr, t->tr_name) == 0) {
2227c478bd9Sstevel@tonic-gate 				nn = (*(t->gbn))(tp, serv);
2237c478bd9Sstevel@tonic-gate 				if (nn) {
2247c478bd9Sstevel@tonic-gate 					*addrs = nn;
2257c478bd9Sstevel@tonic-gate 					return (0);
2267c478bd9Sstevel@tonic-gate 				}
22761961e0fSrobinson 				if (_nderror < 0) {
22861961e0fSrobinson 					return (_nderror);
22961961e0fSrobinson 				}
23061961e0fSrobinson 				break;
2317c478bd9Sstevel@tonic-gate 			}
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 		/* If we didn't find it try loading it */
2347c478bd9Sstevel@tonic-gate 		if (!t) {
2357c478bd9Sstevel@tonic-gate 			if ((t = load_xlate(lr)) != NULL) {
2367c478bd9Sstevel@tonic-gate 				/* add it to the list */
23761961e0fSrobinson 				(void) mutex_lock(&xlate_lock);
2387c478bd9Sstevel@tonic-gate 				add_to_xlate_list(t);
23961961e0fSrobinson 				(void) mutex_unlock(&xlate_lock);
2407c478bd9Sstevel@tonic-gate 				nn = (*(t->gbn))(tp, serv);
2417c478bd9Sstevel@tonic-gate 				if (nn) {
2427c478bd9Sstevel@tonic-gate 					*addrs = nn;
2437c478bd9Sstevel@tonic-gate 					return (0);
24461961e0fSrobinson 				}
24561961e0fSrobinson 				if (_nderror < 0) {
24661961e0fSrobinson 					return (_nderror);
2477c478bd9Sstevel@tonic-gate 				}
2487c478bd9Sstevel@tonic-gate 			} else {
2497c478bd9Sstevel@tonic-gate 				if (_nderror == ND_SYSTEM) { /* retry cache */
2507c478bd9Sstevel@tonic-gate 					_nderror = ND_OK;
2517c478bd9Sstevel@tonic-gate 					i--;
2527c478bd9Sstevel@tonic-gate 					continue;
2537c478bd9Sstevel@tonic-gate 				}
2547c478bd9Sstevel@tonic-gate 			}
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 	return (_nderror);	/* No one works */
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate  * This routine is similar to the one above except that it tries to resolve
2627c478bd9Sstevel@tonic-gate  * the name by the address passed.
2637c478bd9Sstevel@tonic-gate  */
2647c478bd9Sstevel@tonic-gate int
netdir_getbyaddr(struct netconfig * tp,struct nd_hostservlist ** serv,struct netbuf * addr)26561961e0fSrobinson netdir_getbyaddr(struct netconfig *tp, struct nd_hostservlist **serv,
26661961e0fSrobinson 							struct netbuf *addr)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	if (tp == 0) {
2697c478bd9Sstevel@tonic-gate 		_nderror = ND_BADARG;
2707c478bd9Sstevel@tonic-gate 		return (_nderror);
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 	if ((strcmp(tp->nc_protofmly, NC_INET) == 0) &&
2737c478bd9Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
2747c478bd9Sstevel@tonic-gate 		struct	nss_netdirbyaddr_in nssin;
2757c478bd9Sstevel@tonic-gate 		union	nss_netdirbyaddr_out nssout;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		nssin.op_t = NETDIR_BY;
2787c478bd9Sstevel@tonic-gate 		nssin.arg.nd_nbuf = addr;
2797c478bd9Sstevel@tonic-gate 		nssout.nd_hslist = serv;
2807c478bd9Sstevel@tonic-gate 		return (_get_hostserv_inetnetdir_byaddr(tp, &nssin, &nssout));
28161961e0fSrobinson 	}
28261961e0fSrobinson 	if ((strcmp(tp->nc_protofmly, NC_INET6) == 0) &&
2837c478bd9Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
2847c478bd9Sstevel@tonic-gate 		struct	nss_netdirbyaddr_in nssin;
2857c478bd9Sstevel@tonic-gate 		union	nss_netdirbyaddr_out nssout;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		nssin.op_t = NETDIR_BY6;
2887c478bd9Sstevel@tonic-gate 		nssin.arg.nd_nbuf = addr;
2897c478bd9Sstevel@tonic-gate 		nssout.nd_hslist = serv;
2907c478bd9Sstevel@tonic-gate 		return (_get_hostserv_inetnetdir_byaddr(tp, &nssin, &nssout));
2917c478bd9Sstevel@tonic-gate 	}
29261961e0fSrobinson 	return (__classic_netdir_getbyaddr(tp, serv, addr));
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate /*
2957c478bd9Sstevel@tonic-gate  * This routine is similar to the one above except that it instructs the
2967c478bd9Sstevel@tonic-gate  * _get_hostserv_inetnetdir_byaddr not to do a service lookup.
2977c478bd9Sstevel@tonic-gate  */
2987c478bd9Sstevel@tonic-gate int
__netdir_getbyaddr_nosrv(struct netconfig * tp,struct nd_hostservlist ** serv,struct netbuf * addr)29961961e0fSrobinson __netdir_getbyaddr_nosrv(struct netconfig *tp, struct nd_hostservlist **serv,
30061961e0fSrobinson 							struct netbuf *addr)
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate 	if (tp == 0) {
3037c478bd9Sstevel@tonic-gate 		_nderror = ND_BADARG;
3047c478bd9Sstevel@tonic-gate 		return (_nderror);
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 	if ((strcmp(tp->nc_protofmly, NC_INET) == 0) &&
3077c478bd9Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
3087c478bd9Sstevel@tonic-gate 		struct	nss_netdirbyaddr_in nssin;
3097c478bd9Sstevel@tonic-gate 		union	nss_netdirbyaddr_out nssout;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 		nssin.op_t = NETDIR_BY_NOSRV;
3127c478bd9Sstevel@tonic-gate 		nssin.arg.nd_nbuf = addr;
3137c478bd9Sstevel@tonic-gate 		nssout.nd_hslist = serv;
3147c478bd9Sstevel@tonic-gate 		return (_get_hostserv_inetnetdir_byaddr(tp, &nssin, &nssout));
31561961e0fSrobinson 	}
31661961e0fSrobinson 	if ((strcmp(tp->nc_protofmly, NC_INET6) == 0) &&
3177c478bd9Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
3187c478bd9Sstevel@tonic-gate 		struct	nss_netdirbyaddr_in nssin;
3197c478bd9Sstevel@tonic-gate 		union	nss_netdirbyaddr_out nssout;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 		nssin.op_t = NETDIR_BY_NOSRV6;
3227c478bd9Sstevel@tonic-gate 		nssin.arg.nd_nbuf = addr;
3237c478bd9Sstevel@tonic-gate 		nssout.nd_hslist = serv;
3247c478bd9Sstevel@tonic-gate 		return (_get_hostserv_inetnetdir_byaddr(tp, &nssin, &nssout));
3257c478bd9Sstevel@tonic-gate 	}
32661961e0fSrobinson 	return (__classic_netdir_getbyaddr(tp, serv, addr));
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate /*
3307c478bd9Sstevel@tonic-gate  * This routine is the svr4_classic routine for resolving a netbuf struct
3317c478bd9Sstevel@tonic-gate  * into a bunch of host/service name pairs.
3327c478bd9Sstevel@tonic-gate  *
3337c478bd9Sstevel@tonic-gate  * It's either called by the real netdir_getbyaddr() interface above
3347c478bd9Sstevel@tonic-gate  * or by gethost/servbyaddr when nametoaddr libs are specified in
3357c478bd9Sstevel@tonic-gate  * /etc/netconfig with an intent of bypassing the name service switch.
3367c478bd9Sstevel@tonic-gate  */
3377c478bd9Sstevel@tonic-gate int
__classic_netdir_getbyaddr(struct netconfig * tp,struct nd_hostservlist ** serv,struct netbuf * addr)33861961e0fSrobinson __classic_netdir_getbyaddr(struct netconfig *tp, struct nd_hostservlist **serv,
33961961e0fSrobinson 						struct netbuf *addr)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate 	struct translator	*t;	/* pointer to translator list	*/
3427c478bd9Sstevel@tonic-gate 	struct nd_hostservlist	*hs;	/* the results			*/
3437c478bd9Sstevel@tonic-gate 	char			*lr;	/* routines to try		*/
3447c478bd9Sstevel@tonic-gate 	int			i;	/* counts the routines		*/
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	_nderror = ND_SYSTEM;
3477c478bd9Sstevel@tonic-gate 	for (i = 0; i < tp->nc_nlookups; i++) {
3487c478bd9Sstevel@tonic-gate 		lr = *((tp->nc_lookups) + i);
3497c478bd9Sstevel@tonic-gate 		for (t = xlate_list; t; t = t->next) {
3507c478bd9Sstevel@tonic-gate 			if (strcmp(lr, t->tr_name) == 0) {
3517c478bd9Sstevel@tonic-gate 				hs = (*(t->gba))(tp, addr);
3527c478bd9Sstevel@tonic-gate 				if (hs) {
3537c478bd9Sstevel@tonic-gate 					*serv = hs;
3547c478bd9Sstevel@tonic-gate 					return (0);
3557c478bd9Sstevel@tonic-gate 				}
35661961e0fSrobinson 				if (_nderror < 0)
35761961e0fSrobinson 					return (_nderror);
35861961e0fSrobinson 				break;
3597c478bd9Sstevel@tonic-gate 			}
3607c478bd9Sstevel@tonic-gate 		}
3617c478bd9Sstevel@tonic-gate 		/* If we didn't find it try loading it */
3627c478bd9Sstevel@tonic-gate 		if (!t) {
3637c478bd9Sstevel@tonic-gate 			if ((t = load_xlate(lr)) != NULL) {
3647c478bd9Sstevel@tonic-gate 				/* add it to the list */
36561961e0fSrobinson 				(void) mutex_lock(&xlate_lock);
3667c478bd9Sstevel@tonic-gate 				add_to_xlate_list(t);
36761961e0fSrobinson 				(void) mutex_unlock(&xlate_lock);
3687c478bd9Sstevel@tonic-gate 				hs = (*(t->gba))(tp, addr);
3697c478bd9Sstevel@tonic-gate 				if (hs) {
3707c478bd9Sstevel@tonic-gate 					*serv = hs;
3717c478bd9Sstevel@tonic-gate 					return (0);
3727c478bd9Sstevel@tonic-gate 				}
37361961e0fSrobinson 				if (_nderror < 0)
37461961e0fSrobinson 					return (_nderror);
3757c478bd9Sstevel@tonic-gate 			} else {
3767c478bd9Sstevel@tonic-gate 				if (_nderror == ND_SYSTEM) { /* retry cache */
3777c478bd9Sstevel@tonic-gate 					_nderror = ND_OK;
3787c478bd9Sstevel@tonic-gate 					i--;
3797c478bd9Sstevel@tonic-gate 					continue;
3807c478bd9Sstevel@tonic-gate 				}
3817c478bd9Sstevel@tonic-gate 			}
3827c478bd9Sstevel@tonic-gate 		}
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 	return (_nderror);	/* No one works */
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate /*
3887c478bd9Sstevel@tonic-gate  * This is the library routine to do transport specific stuff.
3897c478bd9Sstevel@tonic-gate  * The code is same as the other similar routines except that it does
3907c478bd9Sstevel@tonic-gate  * not bother to try whole bunch of routines since if the first
3917c478bd9Sstevel@tonic-gate  * libray cannot resolve the option, then no one can.
3927c478bd9Sstevel@tonic-gate  *
3937c478bd9Sstevel@tonic-gate  * If it gets a netconfig structure for inet transports with nametoddr libs,
3947c478bd9Sstevel@tonic-gate  * it simply calls the inet-specific built in implementation.
3957c478bd9Sstevel@tonic-gate  */
3967c478bd9Sstevel@tonic-gate int
netdir_options(struct netconfig * tp,int option,int fd,char * par)39761961e0fSrobinson netdir_options(struct netconfig *tp, int option, int fd, char *par)
3987c478bd9Sstevel@tonic-gate {
3997c478bd9Sstevel@tonic-gate 	struct translator	*t;	/* pointer to translator list	*/
4007c478bd9Sstevel@tonic-gate 	char			*lr;	/* routines to try		*/
4017c478bd9Sstevel@tonic-gate 	int			i;	/* counts the routines		*/
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	if (tp == 0) {
4047c478bd9Sstevel@tonic-gate 		_nderror = ND_BADARG;
4057c478bd9Sstevel@tonic-gate 		return (_nderror);
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	if ((strcmp(tp->nc_protofmly, NC_INET) == 0 ||
4097c478bd9Sstevel@tonic-gate 		strcmp(tp->nc_protofmly, NC_INET6) == 0) &&
4107c478bd9Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
4117c478bd9Sstevel@tonic-gate 		return (__inet_netdir_options(tp, option, fd, par));
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	for (i = 0; i < tp->nc_nlookups; i++) {
4167c478bd9Sstevel@tonic-gate 		lr = *((tp->nc_lookups) + i);
4177c478bd9Sstevel@tonic-gate 		for (t = xlate_list; t; t = t->next) {
41861961e0fSrobinson 			if (strcmp(lr, t->tr_name) == 0)
4197c478bd9Sstevel@tonic-gate 				return ((*(t->opt))(tp, option, fd, par));
4207c478bd9Sstevel@tonic-gate 		}
4217c478bd9Sstevel@tonic-gate 		/* If we didn't find it try loading it */
4227c478bd9Sstevel@tonic-gate 		if (!t) {
4237c478bd9Sstevel@tonic-gate 			if ((t = load_xlate(lr)) != NULL) {
4247c478bd9Sstevel@tonic-gate 				/* add it to the list */
42561961e0fSrobinson 				(void) mutex_lock(&xlate_lock);
4267c478bd9Sstevel@tonic-gate 				add_to_xlate_list(t);
42761961e0fSrobinson 				(void) mutex_unlock(&xlate_lock);
4287c478bd9Sstevel@tonic-gate 				return ((*(t->opt))(tp, option, fd, par));
42961961e0fSrobinson 			}
43061961e0fSrobinson 			if (_nderror == ND_SYSTEM) { /* retry cache */
43161961e0fSrobinson 				_nderror = ND_OK;
43261961e0fSrobinson 				i--;
43361961e0fSrobinson 				continue;
4347c478bd9Sstevel@tonic-gate 			}
4357c478bd9Sstevel@tonic-gate 		}
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 	return (_nderror);	/* No one works */
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate /*
4417c478bd9Sstevel@tonic-gate  * This is the library routine for translating universal addresses to
4427c478bd9Sstevel@tonic-gate  * transport specific addresses. Again it uses the same code as above
4437c478bd9Sstevel@tonic-gate  * to search for the appropriate translation routine. Only it doesn't
4447c478bd9Sstevel@tonic-gate  * bother trying a whole bunch of routines since either the transport
4457c478bd9Sstevel@tonic-gate  * can translate it or it can't.
4467c478bd9Sstevel@tonic-gate  */
4477c478bd9Sstevel@tonic-gate struct netbuf *
uaddr2taddr(struct netconfig * tp,char * addr)44861961e0fSrobinson uaddr2taddr(struct netconfig *tp, char *addr)
4497c478bd9Sstevel@tonic-gate {
4507c478bd9Sstevel@tonic-gate 	struct translator	*t;	/* pointer to translator list 	*/
4517c478bd9Sstevel@tonic-gate 	struct netbuf		*x;	/* the answer we want 		*/
4527c478bd9Sstevel@tonic-gate 	char			*lr;	/* routines to try		*/
4537c478bd9Sstevel@tonic-gate 	int			i;	/* counts the routines		*/
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if (tp == 0) {
4567c478bd9Sstevel@tonic-gate 		_nderror = ND_BADARG;
4577c478bd9Sstevel@tonic-gate 		return (0);
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 	if ((strcmp(tp->nc_protofmly, NC_INET) == 0 ||
4607c478bd9Sstevel@tonic-gate 		strcmp(tp->nc_protofmly, NC_INET6) == 0) &&
4617c478bd9Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
4627c478bd9Sstevel@tonic-gate 		return (__inet_uaddr2taddr(tp, addr));
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 	for (i = 0; i < tp->nc_nlookups; i++) {
4657c478bd9Sstevel@tonic-gate 		lr = *((tp->nc_lookups) + i);
4667c478bd9Sstevel@tonic-gate 		for (t = xlate_list; t; t = t->next) {
4677c478bd9Sstevel@tonic-gate 			if (strcmp(lr, t->tr_name) == 0) {
4687c478bd9Sstevel@tonic-gate 				x = (*(t->u2t))(tp, addr);
46961961e0fSrobinson 				if (x)
4707c478bd9Sstevel@tonic-gate 					return (x);
47161961e0fSrobinson 				if (_nderror < 0)
4727c478bd9Sstevel@tonic-gate 					return (0);
4737c478bd9Sstevel@tonic-gate 			}
4747c478bd9Sstevel@tonic-gate 		}
4757c478bd9Sstevel@tonic-gate 		/* If we didn't find it try loading it */
4767c478bd9Sstevel@tonic-gate 		if (!t) {
4777c478bd9Sstevel@tonic-gate 			if ((t = load_xlate(lr)) != NULL) {
4787c478bd9Sstevel@tonic-gate 				/* add it to the list */
47961961e0fSrobinson 				(void) mutex_lock(&xlate_lock);
4807c478bd9Sstevel@tonic-gate 				add_to_xlate_list(t);
48161961e0fSrobinson 				(void) mutex_unlock(&xlate_lock);
4827c478bd9Sstevel@tonic-gate 				x = (*(t->u2t))(tp, addr);
48361961e0fSrobinson 				if (x)
4847c478bd9Sstevel@tonic-gate 					return (x);
48561961e0fSrobinson 				if (_nderror < 0)
4867c478bd9Sstevel@tonic-gate 					return (0);
4877c478bd9Sstevel@tonic-gate 			} else {
4887c478bd9Sstevel@tonic-gate 				if (_nderror == ND_SYSTEM) { /* retry cache */
4897c478bd9Sstevel@tonic-gate 					_nderror = ND_OK;
4907c478bd9Sstevel@tonic-gate 					i--;
4917c478bd9Sstevel@tonic-gate 					continue;
4927c478bd9Sstevel@tonic-gate 				}
4937c478bd9Sstevel@tonic-gate 			}
4947c478bd9Sstevel@tonic-gate 		}
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 	return (0);	/* No one works */
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate /*
5007c478bd9Sstevel@tonic-gate  * This is the library routine for translating transport specific
5017c478bd9Sstevel@tonic-gate  * addresses to universal addresses. Again it uses the same code as above
5027c478bd9Sstevel@tonic-gate  * to search for the appropriate translation routine. Only it doesn't
5037c478bd9Sstevel@tonic-gate  * bother trying a whole bunch of routines since either the transport
5047c478bd9Sstevel@tonic-gate  * can translate it or it can't.
5057c478bd9Sstevel@tonic-gate  */
5067c478bd9Sstevel@tonic-gate char *
taddr2uaddr(struct netconfig * tp,struct netbuf * addr)50761961e0fSrobinson taddr2uaddr(struct netconfig *tp, struct netbuf *addr)
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	struct translator	*t;	/* pointer to translator list	*/
5107c478bd9Sstevel@tonic-gate 	char			*lr;	/* routines to try		*/
5117c478bd9Sstevel@tonic-gate 	char			*x;	/* the answer			*/
5127c478bd9Sstevel@tonic-gate 	int			i;	/* counts the routines		*/
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	if (tp == 0) {
5157c478bd9Sstevel@tonic-gate 		_nderror = ND_BADARG;
5167c478bd9Sstevel@tonic-gate 		return (0);
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 	if ((strcmp(tp->nc_protofmly, NC_INET) == 0 ||
5197c478bd9Sstevel@tonic-gate 		strcmp(tp->nc_protofmly, NC_INET6) == 0) &&
5207c478bd9Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
5217c478bd9Sstevel@tonic-gate 		return (__inet_taddr2uaddr(tp, addr));
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 	for (i = 0; i < tp->nc_nlookups; i++) {
5247c478bd9Sstevel@tonic-gate 		lr = *((tp->nc_lookups) + i);
5257c478bd9Sstevel@tonic-gate 		for (t = xlate_list; t; t = t->next) {
5267c478bd9Sstevel@tonic-gate 			if (strcmp(lr, t->tr_name) == 0) {
5277c478bd9Sstevel@tonic-gate 				x = (*(t->t2u))(tp, addr);
52861961e0fSrobinson 				if (x)
5297c478bd9Sstevel@tonic-gate 					return (x);
53061961e0fSrobinson 				if (_nderror < 0)
5317c478bd9Sstevel@tonic-gate 					return (0);
5327c478bd9Sstevel@tonic-gate 			}
5337c478bd9Sstevel@tonic-gate 		}
5347c478bd9Sstevel@tonic-gate 		/* If we didn't find it try loading it */
5357c478bd9Sstevel@tonic-gate 		if (!t) {
5367c478bd9Sstevel@tonic-gate 			if ((t = load_xlate(lr)) != NULL) {
5377c478bd9Sstevel@tonic-gate 				/* add it to the list */
53861961e0fSrobinson 				(void) mutex_lock(&xlate_lock);
5397c478bd9Sstevel@tonic-gate 				add_to_xlate_list(t);
54061961e0fSrobinson 				(void) mutex_unlock(&xlate_lock);
5417c478bd9Sstevel@tonic-gate 				x = (*(t->t2u))(tp, addr);
54261961e0fSrobinson 				if (x)
5437c478bd9Sstevel@tonic-gate 					return (x);
54461961e0fSrobinson 				if (_nderror < 0)
5457c478bd9Sstevel@tonic-gate 					return (0);
5467c478bd9Sstevel@tonic-gate 			} else {
5477c478bd9Sstevel@tonic-gate 				if (_nderror == ND_SYSTEM) { /* retry cache */
5487c478bd9Sstevel@tonic-gate 					_nderror = ND_OK;
5497c478bd9Sstevel@tonic-gate 					i--;
5507c478bd9Sstevel@tonic-gate 					continue;
5517c478bd9Sstevel@tonic-gate 				}
5527c478bd9Sstevel@tonic-gate 			}
5537c478bd9Sstevel@tonic-gate 		}
5547c478bd9Sstevel@tonic-gate 	}
5557c478bd9Sstevel@tonic-gate 	return (0);	/* No one works */
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate /*
5597c478bd9Sstevel@tonic-gate  * This is the routine that frees the objects that these routines allocate.
5607c478bd9Sstevel@tonic-gate  */
5617c478bd9Sstevel@tonic-gate void
netdir_free(void * ptr,int type)56261961e0fSrobinson netdir_free(void *ptr, int type)
5637c478bd9Sstevel@tonic-gate {
5647c478bd9Sstevel@tonic-gate 	struct netbuf		*na;
5657c478bd9Sstevel@tonic-gate 	struct nd_addrlist	*nas;
5667c478bd9Sstevel@tonic-gate 	struct nd_hostserv	*hs;
5677c478bd9Sstevel@tonic-gate 	struct nd_hostservlist	*hss;
5687c478bd9Sstevel@tonic-gate 	int			i;
5697c478bd9Sstevel@tonic-gate 
57061961e0fSrobinson 	if (ptr == NULL)
5717c478bd9Sstevel@tonic-gate 		return;
5727c478bd9Sstevel@tonic-gate 	switch (type) {
5737c478bd9Sstevel@tonic-gate 	case ND_ADDR :
5747c478bd9Sstevel@tonic-gate 		na = (struct netbuf *)ptr;
5757c478bd9Sstevel@tonic-gate 		if (na->buf)
5767c478bd9Sstevel@tonic-gate 			free(na->buf);
57761961e0fSrobinson 		free(na);
5787c478bd9Sstevel@tonic-gate 		break;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	case ND_ADDRLIST :
5817c478bd9Sstevel@tonic-gate 		nas = (struct nd_addrlist *)ptr;
5827c478bd9Sstevel@tonic-gate 		/*
5837c478bd9Sstevel@tonic-gate 		 * XXX: We do NOT try to free all individual netbuf->buf
5847c478bd9Sstevel@tonic-gate 		 * pointers. Free only the first one since they are allocated
5857c478bd9Sstevel@tonic-gate 		 * using one calloc in
5867c478bd9Sstevel@tonic-gate 		 * libnsl/nss/netdir_inet.c:order_haddrlist().
5877c478bd9Sstevel@tonic-gate 		 * This potentially causes memory leaks if a nametoaddr
5887c478bd9Sstevel@tonic-gate 		 * implementation -- from a third party -- has a different
5897c478bd9Sstevel@tonic-gate 		 * allocation scheme.
5907c478bd9Sstevel@tonic-gate 		 */
5917c478bd9Sstevel@tonic-gate 		if (nas->n_addrs->buf)
5927c478bd9Sstevel@tonic-gate 			free(nas->n_addrs->buf);
59361961e0fSrobinson 		free(nas->n_addrs);
59461961e0fSrobinson 		free(nas);
5957c478bd9Sstevel@tonic-gate 		break;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	case ND_HOSTSERV :
5987c478bd9Sstevel@tonic-gate 		hs = (struct nd_hostserv *)ptr;
5997c478bd9Sstevel@tonic-gate 		if (hs->h_host)
6007c478bd9Sstevel@tonic-gate 			free(hs->h_host);
6017c478bd9Sstevel@tonic-gate 		if (hs->h_serv)
6027c478bd9Sstevel@tonic-gate 			free(hs->h_serv);
60361961e0fSrobinson 		free(hs);
6047c478bd9Sstevel@tonic-gate 		break;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	case ND_HOSTSERVLIST :
6077c478bd9Sstevel@tonic-gate 		hss = (struct nd_hostservlist *)ptr;
6087c478bd9Sstevel@tonic-gate 		for (hs = hss->h_hostservs, i = 0; i < hss->h_cnt; i++, hs++) {
6097c478bd9Sstevel@tonic-gate 			if (hs->h_host)
6107c478bd9Sstevel@tonic-gate 				free(hs->h_host);
6117c478bd9Sstevel@tonic-gate 			if (hs->h_serv)
6127c478bd9Sstevel@tonic-gate 				free(hs->h_serv);
6137c478bd9Sstevel@tonic-gate 		}
61461961e0fSrobinson 		free(hss->h_hostservs);
61561961e0fSrobinson 		free(hss);
6167c478bd9Sstevel@tonic-gate 		break;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	default :
6197c478bd9Sstevel@tonic-gate 		_nderror = ND_UKNWN;
6207c478bd9Sstevel@tonic-gate 		break;
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate /*
6257c478bd9Sstevel@tonic-gate  * load_xlate is a routine that will attempt to dynamically link in the
6267c478bd9Sstevel@tonic-gate  * file specified by the network configuration structure.
6277c478bd9Sstevel@tonic-gate  */
6287c478bd9Sstevel@tonic-gate static struct translator *
load_xlate(char * name)62961961e0fSrobinson load_xlate(char *name)
6307c478bd9Sstevel@tonic-gate {
6317c478bd9Sstevel@tonic-gate 	struct translator	*t;
6327c478bd9Sstevel@tonic-gate 	static struct xlate_list {
6337c478bd9Sstevel@tonic-gate 		char *library;
6347c478bd9Sstevel@tonic-gate 		struct xlate_list *next;
6357c478bd9Sstevel@tonic-gate 	} *xlistp = NULL;
6367c478bd9Sstevel@tonic-gate 	struct xlate_list *xlp, **xlastp;
6377c478bd9Sstevel@tonic-gate 	static mutex_t xlist_lock = DEFAULTMUTEX;
6387c478bd9Sstevel@tonic-gate 
63961961e0fSrobinson 	(void) mutex_lock(&xlist_lock);
6407c478bd9Sstevel@tonic-gate 	/*
6417c478bd9Sstevel@tonic-gate 	 * We maintain a list of libraries we have loaded.  Loading a library
6427c478bd9Sstevel@tonic-gate 	 * twice is double-plus ungood!
6437c478bd9Sstevel@tonic-gate 	 */
6447c478bd9Sstevel@tonic-gate 	for (xlp = xlistp, xlastp = &xlistp; xlp != NULL;
6457c478bd9Sstevel@tonic-gate 			xlastp = &xlp->next, xlp = xlp->next) {
6467c478bd9Sstevel@tonic-gate 		if (xlp->library != NULL) {
6477c478bd9Sstevel@tonic-gate 			if (strcmp(xlp->library, name) == 0) {
6487c478bd9Sstevel@tonic-gate 				_nderror = ND_SYSTEM;	/* seen this lib */
64961961e0fSrobinson 				(void) mutex_unlock(&xlist_lock);
6507c478bd9Sstevel@tonic-gate 				return (0);
6517c478bd9Sstevel@tonic-gate 			}
6527c478bd9Sstevel@tonic-gate 		}
6537c478bd9Sstevel@tonic-gate 	}
65461961e0fSrobinson 	t = malloc(sizeof (struct translator));
6557c478bd9Sstevel@tonic-gate 	if (!t) {
6567c478bd9Sstevel@tonic-gate 		_nderror = ND_NOMEM;
65761961e0fSrobinson 		(void) mutex_unlock(&xlist_lock);
6587c478bd9Sstevel@tonic-gate 		return (0);
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 	t->tr_name = strdup(name);
6617c478bd9Sstevel@tonic-gate 	if (!t->tr_name) {
6627c478bd9Sstevel@tonic-gate 		_nderror = ND_NOMEM;
66361961e0fSrobinson 		free(t);
66461961e0fSrobinson 		(void) mutex_unlock(&xlist_lock);
6657c478bd9Sstevel@tonic-gate 		return (NULL);
6667c478bd9Sstevel@tonic-gate 	}
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	t->tr_fd = dlopen(name, RTLD_LAZY);
6697c478bd9Sstevel@tonic-gate 	if (t->tr_fd == NULL) {
6707c478bd9Sstevel@tonic-gate 		_nderror = ND_OPEN;
6717c478bd9Sstevel@tonic-gate 		goto error;
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	/* Resolve the getbyname symbol */
6757c478bd9Sstevel@tonic-gate 	t->gbn = (struct nd_addrlist *(*)())dlsym(t->tr_fd,
6767c478bd9Sstevel@tonic-gate 				"_netdir_getbyname");
6777c478bd9Sstevel@tonic-gate 	if (!(t->gbn)) {
6787c478bd9Sstevel@tonic-gate 		_nderror = ND_NOSYM;
6797c478bd9Sstevel@tonic-gate 		goto error;
6807c478bd9Sstevel@tonic-gate 	}
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	/* resolve the getbyaddr symbol */
6837c478bd9Sstevel@tonic-gate 	t->gba = (struct nd_hostservlist *(*)())dlsym(t->tr_fd,
6847c478bd9Sstevel@tonic-gate 				"_netdir_getbyaddr");
6857c478bd9Sstevel@tonic-gate 	if (!(t->gba)) {
6867c478bd9Sstevel@tonic-gate 		_nderror = ND_NOSYM;
6877c478bd9Sstevel@tonic-gate 		goto error;
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	/* resolve the taddr2uaddr symbol */
6917c478bd9Sstevel@tonic-gate 	t->t2u = (char *(*)())dlsym(t->tr_fd, "_taddr2uaddr");
6927c478bd9Sstevel@tonic-gate 	if (!(t->t2u)) {
6937c478bd9Sstevel@tonic-gate 		_nderror = ND_NOSYM;
6947c478bd9Sstevel@tonic-gate 		goto error;
6957c478bd9Sstevel@tonic-gate 	}
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	/* resolve the uaddr2taddr symbol */
6987c478bd9Sstevel@tonic-gate 	t->u2t = (struct netbuf *(*)())dlsym(t->tr_fd, "_uaddr2taddr");
6997c478bd9Sstevel@tonic-gate 	if (!(t->u2t)) {
7007c478bd9Sstevel@tonic-gate 		_nderror = ND_NOSYM;
7017c478bd9Sstevel@tonic-gate 		goto error;
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	/* resolve the netdir_options symbol */
7057c478bd9Sstevel@tonic-gate 	t->opt = (int (*)())dlsym(t->tr_fd, "_netdir_options");
7067c478bd9Sstevel@tonic-gate 	if (!(t->opt)) {
7077c478bd9Sstevel@tonic-gate 		_nderror = ND_NOSYM;
7087c478bd9Sstevel@tonic-gate 		goto error;
7097c478bd9Sstevel@tonic-gate 	}
7107c478bd9Sstevel@tonic-gate 	/*
7117c478bd9Sstevel@tonic-gate 	 * Add this library to the list of loaded libraries.
7127c478bd9Sstevel@tonic-gate 	 */
71361961e0fSrobinson 	*xlastp = malloc(sizeof (struct xlate_list));
7147c478bd9Sstevel@tonic-gate 	if (*xlastp == NULL) {
7157c478bd9Sstevel@tonic-gate 		_nderror = ND_NOMEM;
7167c478bd9Sstevel@tonic-gate 		goto error;
7177c478bd9Sstevel@tonic-gate 	}
7187c478bd9Sstevel@tonic-gate 	(*xlastp)->library = strdup(name);
7197c478bd9Sstevel@tonic-gate 	if ((*xlastp)->library == NULL) {
7207c478bd9Sstevel@tonic-gate 		_nderror = ND_NOMEM;
7217c478bd9Sstevel@tonic-gate 		free(*xlastp);
7227c478bd9Sstevel@tonic-gate 		goto error;
7237c478bd9Sstevel@tonic-gate 	}
7247c478bd9Sstevel@tonic-gate 	(*xlastp)->next = NULL;
72561961e0fSrobinson 	(void) mutex_unlock(&xlist_lock);
7267c478bd9Sstevel@tonic-gate 	return (t);
7277c478bd9Sstevel@tonic-gate error:
7287c478bd9Sstevel@tonic-gate 	if (t->tr_fd != NULL)
7297c478bd9Sstevel@tonic-gate 		(void) dlclose(t->tr_fd);
7307c478bd9Sstevel@tonic-gate 	free(t->tr_name);
73161961e0fSrobinson 	free(t);
73261961e0fSrobinson 	(void) mutex_unlock(&xlist_lock);
7337c478bd9Sstevel@tonic-gate 	return (NULL);
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate #define	NDERR_BUFSZ	512
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate /*
7407c478bd9Sstevel@tonic-gate  * This is a routine that returns a string related to the current
7417c478bd9Sstevel@tonic-gate  * error in _nderror.
7427c478bd9Sstevel@tonic-gate  */
7437c478bd9Sstevel@tonic-gate char *
netdir_sperror(void)74461961e0fSrobinson netdir_sperror(void)
7457c478bd9Sstevel@tonic-gate {
746*cb620785Sraf 	static pthread_key_t nderrbuf_key = PTHREAD_ONCE_KEY_NP;
7477c478bd9Sstevel@tonic-gate 	static char buf_main[NDERR_BUFSZ];
7487c478bd9Sstevel@tonic-gate 	char	*str;
7497c478bd9Sstevel@tonic-gate 	char *dlerrstr;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	str = thr_main()?
7527c478bd9Sstevel@tonic-gate 		buf_main :
7537c478bd9Sstevel@tonic-gate 		thr_get_storage(&nderrbuf_key, NDERR_BUFSZ, free);
75461961e0fSrobinson 	if (str == NULL)
7557c478bd9Sstevel@tonic-gate 		return (NULL);
7567c478bd9Sstevel@tonic-gate 	dlerrstr = dlerror();
7577c478bd9Sstevel@tonic-gate 	switch (_nderror) {
7587c478bd9Sstevel@tonic-gate 	case ND_NOMEM :
7597c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7607c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: memory allocation failed"));
7617c478bd9Sstevel@tonic-gate 		break;
7627c478bd9Sstevel@tonic-gate 	case ND_OK :
7637c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7647c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: successful completion"));
7657c478bd9Sstevel@tonic-gate 		break;
7667c478bd9Sstevel@tonic-gate 	case ND_NOHOST :
7677c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7687c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: hostname not found"));
7697c478bd9Sstevel@tonic-gate 		break;
7707c478bd9Sstevel@tonic-gate 	case ND_NOSERV :
7717c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7727c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: service name not found"));
7737c478bd9Sstevel@tonic-gate 		break;
7747c478bd9Sstevel@tonic-gate 	case ND_NOSYM :
7757c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ, "%s : %s ",
7767c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom,
7777c478bd9Sstevel@tonic-gate 			"n2a: symbol missing in shared object"),
7787c478bd9Sstevel@tonic-gate 			dlerrstr ? dlerrstr : " ");
7797c478bd9Sstevel@tonic-gate 		break;
7807c478bd9Sstevel@tonic-gate 	case ND_OPEN :
7817c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ, "%s - %s ",
7827c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: couldn't open shared object"),
7837c478bd9Sstevel@tonic-gate 			dlerrstr ? dlerrstr : " ");
7847c478bd9Sstevel@tonic-gate 		break;
7857c478bd9Sstevel@tonic-gate 	case ND_ACCESS :
7867c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7877c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom,
7887c478bd9Sstevel@tonic-gate 			"n2a: access denied for shared object"));
7897c478bd9Sstevel@tonic-gate 		break;
7907c478bd9Sstevel@tonic-gate 	case ND_UKNWN :
7917c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7927c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom,
7937c478bd9Sstevel@tonic-gate 			"n2a: attempt to free unknown object"));
7947c478bd9Sstevel@tonic-gate 		break;
7957c478bd9Sstevel@tonic-gate 	case ND_BADARG :
7967c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7977c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom,
7987c478bd9Sstevel@tonic-gate 			"n2a: bad arguments passed to routine"));
7997c478bd9Sstevel@tonic-gate 		break;
8007c478bd9Sstevel@tonic-gate 	case ND_NOCTRL:
8017c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
8027c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: unknown option passed"));
8037c478bd9Sstevel@tonic-gate 		break;
8047c478bd9Sstevel@tonic-gate 	case ND_FAILCTRL:
8057c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
8067c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: control operation failed"));
8077c478bd9Sstevel@tonic-gate 		break;
8087c478bd9Sstevel@tonic-gate 	case ND_SYSTEM:
8097c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ, "%s: %s",
8107c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: system error"),
8117c478bd9Sstevel@tonic-gate 			strerror(errno));
8127c478bd9Sstevel@tonic-gate 		break;
8137c478bd9Sstevel@tonic-gate 	default :
8147c478bd9Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ, "%s#%d",
8157c478bd9Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: unknown error "), _nderror);
8167c478bd9Sstevel@tonic-gate 		break;
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 	return (str);
8197c478bd9Sstevel@tonic-gate }
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate /*
8227c478bd9Sstevel@tonic-gate  * This is a routine that prints out strings related to the current
8237c478bd9Sstevel@tonic-gate  * error in _nderror. Like perror() it takes a string to print with a
8247c478bd9Sstevel@tonic-gate  * colon first.
8257c478bd9Sstevel@tonic-gate  */
8267c478bd9Sstevel@tonic-gate void
netdir_perror(char * s)82761961e0fSrobinson netdir_perror(char *s)
8287c478bd9Sstevel@tonic-gate {
8297c478bd9Sstevel@tonic-gate 	char	*err;
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	err = netdir_sperror();
8327c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s\n", s, err ? err : "n2a: error");
8337c478bd9Sstevel@tonic-gate }
834