xref: /onnv-gate/usr/src/lib/libnsl/netdir/netdir.c (revision 3864:2ae506652d11)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3864Sraf  * Common Development and Distribution License (the "License").
6*3864Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21132Srobinson 
220Sstevel@tonic-gate /*
23*3864Sraf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
320Sstevel@tonic-gate  * under license from the Regents of the University of California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * netdir.c
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  * This is the library routines that do the name to address
410Sstevel@tonic-gate  * translation.
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include "mt.h"
450Sstevel@tonic-gate #include "../rpc/rpc_mt.h"		/* for MT declarations only */
460Sstevel@tonic-gate #include <stdio.h>
470Sstevel@tonic-gate #include <sys/types.h>
480Sstevel@tonic-gate #include <errno.h>
490Sstevel@tonic-gate #include <tiuser.h>
500Sstevel@tonic-gate #include <netdir.h>
510Sstevel@tonic-gate #include <netconfig.h>
520Sstevel@tonic-gate #include <string.h>
530Sstevel@tonic-gate #include <sys/file.h>
540Sstevel@tonic-gate #include <dlfcn.h>
55132Srobinson #include <stdlib.h>
560Sstevel@tonic-gate #include <malloc.h>
570Sstevel@tonic-gate #include <syslog.h>
580Sstevel@tonic-gate #include <nss_netdir.h>
590Sstevel@tonic-gate #include <netinet/in.h>
600Sstevel@tonic-gate #include <netdb.h>
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /* messaging stuff. */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate extern const char __nsl_dom[];
650Sstevel@tonic-gate extern char *dgettext(const char *, const char *);
660Sstevel@tonic-gate 
670Sstevel@tonic-gate struct translator {
680Sstevel@tonic-gate 	struct nd_addrlist	*(*gbn)();	/* _netdir_getbyname	*/
690Sstevel@tonic-gate 	struct nd_hostservlist 	*(*gba)();	/* _netdir_getbyaddr	*/
700Sstevel@tonic-gate 	int			(*opt)();	/* _netdir_options	*/
710Sstevel@tonic-gate 	char			*(*t2u)();	/* _taddr2uaddr		*/
720Sstevel@tonic-gate 	struct netbuf		*(*u2t)();	/* _uaddr2taddr		*/
730Sstevel@tonic-gate 	void			*tr_fd;		/* dyn library handle	*/
740Sstevel@tonic-gate 	char			*tr_name;	/* Full path		*/
750Sstevel@tonic-gate 	struct translator	*next;
760Sstevel@tonic-gate };
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate  * xlate_lock protects xlate_list during updates only.  The xlate_list linked
800Sstevel@tonic-gate  * list is pre-pended when new entries are added, so threads that are already
810Sstevel@tonic-gate  * using the list will continue correctly to the end of the list.
820Sstevel@tonic-gate  */
830Sstevel@tonic-gate static struct translator *xlate_list = NULL;
840Sstevel@tonic-gate static mutex_t xlate_lock = DEFAULTMUTEX;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate static struct translator *load_xlate(char *);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  * This is the common data (global data) that is exported
900Sstevel@tonic-gate  * by public interfaces. It has been moved here from nd_comdata.c
910Sstevel@tonic-gate  * which no longer exists. This fixes the problem for applications
920Sstevel@tonic-gate  * that do not link directly with -lnsl but dlopen a shared object
930Sstevel@tonic-gate  * that has a NEEDED dependency on -lnsl and uses the netdir
940Sstevel@tonic-gate  * interface.
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate #undef	_nderror
980Sstevel@tonic-gate 
990Sstevel@tonic-gate int	_nderror;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate int *
__nderror(void)102132Srobinson __nderror(void)
1030Sstevel@tonic-gate {
104*3864Sraf 	static pthread_key_t nderror_key = PTHREAD_ONCE_KEY_NP;
1050Sstevel@tonic-gate 	int *ret;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	if (thr_main())
1080Sstevel@tonic-gate 		return (&_nderror);
1090Sstevel@tonic-gate 	ret = thr_get_storage(&nderror_key, sizeof (int), free);
1100Sstevel@tonic-gate 	/* if thr_get_storage fails we return the address of _nderror */
1110Sstevel@tonic-gate 	return (ret ? ret : &_nderror);
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate #define	_nderror	(*(__nderror()))
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate  * Adds a translator library to the xlate_list, but first check to see if
1180Sstevel@tonic-gate  * it's already on the list.  Must be called while holding xlate_lock.
1190Sstevel@tonic-gate  * We have to be careful for the case of the same library being loaded
1200Sstevel@tonic-gate  * with different names (e.g., straddr.so and /usr/lib/straddr.so).
1210Sstevel@tonic-gate  * We check for this case by looking at the gbn and name fields.
1220Sstevel@tonic-gate  * If the gbn address is the same, but the names are different, then we
1230Sstevel@tonic-gate  * have accidentally reloaded the library.  We dlclose the new version,
1240Sstevel@tonic-gate  * and then update 'translate' with the old versions of the symbols.
1250Sstevel@tonic-gate  */
1260Sstevel@tonic-gate void
add_to_xlate_list(struct translator * translate)127132Srobinson add_to_xlate_list(struct translator *translate)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate 	struct translator	*t;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	for (t = xlate_list; t; t = t->next) {
1320Sstevel@tonic-gate 		if (strcmp(translate->tr_name, t->tr_name) == 0) {
1330Sstevel@tonic-gate 			return;
1340Sstevel@tonic-gate 		}
1350Sstevel@tonic-gate 	}
1360Sstevel@tonic-gate 	translate->next = xlate_list;
1370Sstevel@tonic-gate 	xlate_list = translate;
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate /*
1410Sstevel@tonic-gate  * This routine is the main routine that resolves host/service/xprt triples
1420Sstevel@tonic-gate  * into a bunch of netbufs that should connect you to that particular
1430Sstevel@tonic-gate  * service. RPC uses it to contact the binder service (rpcbind).
1440Sstevel@tonic-gate  *
1450Sstevel@tonic-gate  * In the interest of consistency with the gethost/servbyYY() routines,
1460Sstevel@tonic-gate  * this routine calls a common interface _get_hostserv_inetnetdir_byname
1470Sstevel@tonic-gate  * if it's called with a netconfig with "inet" type transports and
1480Sstevel@tonic-gate  * an empty list of nametoaddr libs (i.e. a "-" in /etc/netconfig),
1490Sstevel@tonic-gate  * which indicates the use of the switch. For non-inet transports or
1500Sstevel@tonic-gate  * inet transports with nametoaddr libs specified, it simply calls
1510Sstevel@tonic-gate  * the SVr4-classic netdir_getbyname, which loops through the libs.
1520Sstevel@tonic-gate  *
1530Sstevel@tonic-gate  * After all, any problem can be solved by one more layer of abstraction..
1540Sstevel@tonic-gate  *
1550Sstevel@tonic-gate  * This routine when called with a netconfig with "inet6" type of transports
1560Sstevel@tonic-gate  * returns pure IPv6 addresses only and if no IPv6 address is found it
1570Sstevel@tonic-gate  * returns none - Bug Id. 4276329
1580Sstevel@tonic-gate  */
1590Sstevel@tonic-gate int
netdir_getbyname(struct netconfig * tp,struct nd_hostserv * serv,struct nd_addrlist ** addrs)160132Srobinson netdir_getbyname(struct netconfig *tp, struct nd_hostserv *serv,
161132Srobinson 						struct nd_addrlist **addrs)
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate 	if (tp == 0) {
1640Sstevel@tonic-gate 		_nderror = ND_BADARG;
1650Sstevel@tonic-gate 		return (_nderror);
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 	if ((strcmp(tp->nc_protofmly, NC_INET) == 0) &&
168132Srobinson 						(tp->nc_nlookups == 0)) {
1690Sstevel@tonic-gate 		struct	nss_netdirbyname_in nssin;
1700Sstevel@tonic-gate 		union	nss_netdirbyname_out nssout;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 		nssin.op_t = NETDIR_BY;
1730Sstevel@tonic-gate 		nssin.arg.nd_hs = serv;
1740Sstevel@tonic-gate 		/*
1750Sstevel@tonic-gate 		 * In code path of case NETDIR_BY,
1760Sstevel@tonic-gate 		 * it also calls DOOR_GETIPNODEBYNAME_R.
1770Sstevel@tonic-gate 		 * So af_family and flags are set to
1780Sstevel@tonic-gate 		 * get V4 addresses only.
1790Sstevel@tonic-gate 		 */
1800Sstevel@tonic-gate 		nssin.arg.nss.host6.af_family = AF_INET;
1810Sstevel@tonic-gate 		nssin.arg.nss.host6.flags = 0;
1820Sstevel@tonic-gate 		nssout.nd_alist = addrs;
1830Sstevel@tonic-gate 		return (_get_hostserv_inetnetdir_byname(tp, &nssin, &nssout));
184132Srobinson 	}
185132Srobinson 	if ((strcmp(tp->nc_protofmly, NC_INET6) == 0) &&
186132Srobinson 						(tp->nc_nlookups == 0)) {
1870Sstevel@tonic-gate 		struct	nss_netdirbyname_in nssin;
1880Sstevel@tonic-gate 		union	nss_netdirbyname_out nssout;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 		nssin.op_t = NETDIR_BY6;
1910Sstevel@tonic-gate 		nssin.arg.nd_hs = serv;
1920Sstevel@tonic-gate 		/* get both V4 & V6 addresses */
1930Sstevel@tonic-gate 		nssin.arg.nss.host6.af_family = AF_INET6;
1940Sstevel@tonic-gate 		nssin.arg.nss.host6.flags = (AI_ALL | AI_V4MAPPED);
1950Sstevel@tonic-gate 		nssout.nd_alist = addrs;
1960Sstevel@tonic-gate 		return (_get_hostserv_inetnetdir_byname(tp, &nssin, &nssout));
1970Sstevel@tonic-gate 	}
198132Srobinson 	return (__classic_netdir_getbyname(tp, serv, addrs));
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate  * This routine is the svr4_classic routine for resolving host/service/xprt
2030Sstevel@tonic-gate  * triples into a bunch of netbufs that should connect you to that particular
2040Sstevel@tonic-gate  * service. RPC uses it to contact the binder service (rpcbind).
2050Sstevel@tonic-gate  *
2060Sstevel@tonic-gate  * It's either called by the real netdir_getbyname() interface above
2070Sstevel@tonic-gate  * or by gethost/servbyname when nametoaddr libs are specified in
2080Sstevel@tonic-gate  * /etc/netconfig with an intent of bypassing the name service switch.
2090Sstevel@tonic-gate  */
2100Sstevel@tonic-gate int
__classic_netdir_getbyname(struct netconfig * tp,struct nd_hostserv * serv,struct nd_addrlist ** addrs)211132Srobinson __classic_netdir_getbyname(struct netconfig *tp, struct nd_hostserv *serv,
212132Srobinson 						struct nd_addrlist **addrs)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate 	struct translator	*t;	/* pointer to translator list	*/
2150Sstevel@tonic-gate 	struct nd_addrlist	*nn;	/* the results			*/
2160Sstevel@tonic-gate 	char			*lr;	/* routines to try		*/
2170Sstevel@tonic-gate 	int			i;	/* counts the routines		*/
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	_nderror = ND_SYSTEM;
2200Sstevel@tonic-gate 	for (i = 0; i < tp->nc_nlookups; i++) {
2210Sstevel@tonic-gate 		lr = *((tp->nc_lookups) + i);
2220Sstevel@tonic-gate 		for (t = xlate_list; t; t = t->next) {
2230Sstevel@tonic-gate 			if (strcmp(lr, t->tr_name) == 0) {
2240Sstevel@tonic-gate 				nn = (*(t->gbn))(tp, serv);
2250Sstevel@tonic-gate 				if (nn) {
2260Sstevel@tonic-gate 					*addrs = nn;
2270Sstevel@tonic-gate 					return (0);
2280Sstevel@tonic-gate 				}
229132Srobinson 				if (_nderror < 0) {
230132Srobinson 					return (_nderror);
231132Srobinson 				}
232132Srobinson 				break;
2330Sstevel@tonic-gate 			}
2340Sstevel@tonic-gate 		}
2350Sstevel@tonic-gate 		/* If we didn't find it try loading it */
2360Sstevel@tonic-gate 		if (!t) {
2370Sstevel@tonic-gate 			if ((t = load_xlate(lr)) != NULL) {
2380Sstevel@tonic-gate 				/* add it to the list */
239132Srobinson 				(void) mutex_lock(&xlate_lock);
2400Sstevel@tonic-gate 				add_to_xlate_list(t);
241132Srobinson 				(void) mutex_unlock(&xlate_lock);
2420Sstevel@tonic-gate 				nn = (*(t->gbn))(tp, serv);
2430Sstevel@tonic-gate 				if (nn) {
2440Sstevel@tonic-gate 					*addrs = nn;
2450Sstevel@tonic-gate 					return (0);
246132Srobinson 				}
247132Srobinson 				if (_nderror < 0) {
248132Srobinson 					return (_nderror);
2490Sstevel@tonic-gate 				}
2500Sstevel@tonic-gate 			} else {
2510Sstevel@tonic-gate 				if (_nderror == ND_SYSTEM) { /* retry cache */
2520Sstevel@tonic-gate 					_nderror = ND_OK;
2530Sstevel@tonic-gate 					i--;
2540Sstevel@tonic-gate 					continue;
2550Sstevel@tonic-gate 				}
2560Sstevel@tonic-gate 			}
2570Sstevel@tonic-gate 		}
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 	return (_nderror);	/* No one works */
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate /*
2630Sstevel@tonic-gate  * This routine is similar to the one above except that it tries to resolve
2640Sstevel@tonic-gate  * the name by the address passed.
2650Sstevel@tonic-gate  */
2660Sstevel@tonic-gate int
netdir_getbyaddr(struct netconfig * tp,struct nd_hostservlist ** serv,struct netbuf * addr)267132Srobinson netdir_getbyaddr(struct netconfig *tp, struct nd_hostservlist **serv,
268132Srobinson 							struct netbuf *addr)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate 	if (tp == 0) {
2710Sstevel@tonic-gate 		_nderror = ND_BADARG;
2720Sstevel@tonic-gate 		return (_nderror);
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 	if ((strcmp(tp->nc_protofmly, NC_INET) == 0) &&
2750Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
2760Sstevel@tonic-gate 		struct	nss_netdirbyaddr_in nssin;
2770Sstevel@tonic-gate 		union	nss_netdirbyaddr_out nssout;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 		nssin.op_t = NETDIR_BY;
2800Sstevel@tonic-gate 		nssin.arg.nd_nbuf = addr;
2810Sstevel@tonic-gate 		nssout.nd_hslist = serv;
2820Sstevel@tonic-gate 		return (_get_hostserv_inetnetdir_byaddr(tp, &nssin, &nssout));
283132Srobinson 	}
284132Srobinson 	if ((strcmp(tp->nc_protofmly, NC_INET6) == 0) &&
2850Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
2860Sstevel@tonic-gate 		struct	nss_netdirbyaddr_in nssin;
2870Sstevel@tonic-gate 		union	nss_netdirbyaddr_out nssout;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 		nssin.op_t = NETDIR_BY6;
2900Sstevel@tonic-gate 		nssin.arg.nd_nbuf = addr;
2910Sstevel@tonic-gate 		nssout.nd_hslist = serv;
2920Sstevel@tonic-gate 		return (_get_hostserv_inetnetdir_byaddr(tp, &nssin, &nssout));
2930Sstevel@tonic-gate 	}
294132Srobinson 	return (__classic_netdir_getbyaddr(tp, serv, addr));
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate /*
2970Sstevel@tonic-gate  * This routine is similar to the one above except that it instructs the
2980Sstevel@tonic-gate  * _get_hostserv_inetnetdir_byaddr not to do a service lookup.
2990Sstevel@tonic-gate  */
3000Sstevel@tonic-gate int
__netdir_getbyaddr_nosrv(struct netconfig * tp,struct nd_hostservlist ** serv,struct netbuf * addr)301132Srobinson __netdir_getbyaddr_nosrv(struct netconfig *tp, struct nd_hostservlist **serv,
302132Srobinson 							struct netbuf *addr)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate 	if (tp == 0) {
3050Sstevel@tonic-gate 		_nderror = ND_BADARG;
3060Sstevel@tonic-gate 		return (_nderror);
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 	if ((strcmp(tp->nc_protofmly, NC_INET) == 0) &&
3090Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
3100Sstevel@tonic-gate 		struct	nss_netdirbyaddr_in nssin;
3110Sstevel@tonic-gate 		union	nss_netdirbyaddr_out nssout;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 		nssin.op_t = NETDIR_BY_NOSRV;
3140Sstevel@tonic-gate 		nssin.arg.nd_nbuf = addr;
3150Sstevel@tonic-gate 		nssout.nd_hslist = serv;
3160Sstevel@tonic-gate 		return (_get_hostserv_inetnetdir_byaddr(tp, &nssin, &nssout));
317132Srobinson 	}
318132Srobinson 	if ((strcmp(tp->nc_protofmly, NC_INET6) == 0) &&
3190Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
3200Sstevel@tonic-gate 		struct	nss_netdirbyaddr_in nssin;
3210Sstevel@tonic-gate 		union	nss_netdirbyaddr_out nssout;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 		nssin.op_t = NETDIR_BY_NOSRV6;
3240Sstevel@tonic-gate 		nssin.arg.nd_nbuf = addr;
3250Sstevel@tonic-gate 		nssout.nd_hslist = serv;
3260Sstevel@tonic-gate 		return (_get_hostserv_inetnetdir_byaddr(tp, &nssin, &nssout));
3270Sstevel@tonic-gate 	}
328132Srobinson 	return (__classic_netdir_getbyaddr(tp, serv, addr));
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate /*
3320Sstevel@tonic-gate  * This routine is the svr4_classic routine for resolving a netbuf struct
3330Sstevel@tonic-gate  * into a bunch of host/service name pairs.
3340Sstevel@tonic-gate  *
3350Sstevel@tonic-gate  * It's either called by the real netdir_getbyaddr() interface above
3360Sstevel@tonic-gate  * or by gethost/servbyaddr when nametoaddr libs are specified in
3370Sstevel@tonic-gate  * /etc/netconfig with an intent of bypassing the name service switch.
3380Sstevel@tonic-gate  */
3390Sstevel@tonic-gate int
__classic_netdir_getbyaddr(struct netconfig * tp,struct nd_hostservlist ** serv,struct netbuf * addr)340132Srobinson __classic_netdir_getbyaddr(struct netconfig *tp, struct nd_hostservlist **serv,
341132Srobinson 						struct netbuf *addr)
3420Sstevel@tonic-gate {
3430Sstevel@tonic-gate 	struct translator	*t;	/* pointer to translator list	*/
3440Sstevel@tonic-gate 	struct nd_hostservlist	*hs;	/* the results			*/
3450Sstevel@tonic-gate 	char			*lr;	/* routines to try		*/
3460Sstevel@tonic-gate 	int			i;	/* counts the routines		*/
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	_nderror = ND_SYSTEM;
3490Sstevel@tonic-gate 	for (i = 0; i < tp->nc_nlookups; i++) {
3500Sstevel@tonic-gate 		lr = *((tp->nc_lookups) + i);
3510Sstevel@tonic-gate 		for (t = xlate_list; t; t = t->next) {
3520Sstevel@tonic-gate 			if (strcmp(lr, t->tr_name) == 0) {
3530Sstevel@tonic-gate 				hs = (*(t->gba))(tp, addr);
3540Sstevel@tonic-gate 				if (hs) {
3550Sstevel@tonic-gate 					*serv = hs;
3560Sstevel@tonic-gate 					return (0);
3570Sstevel@tonic-gate 				}
358132Srobinson 				if (_nderror < 0)
359132Srobinson 					return (_nderror);
360132Srobinson 				break;
3610Sstevel@tonic-gate 			}
3620Sstevel@tonic-gate 		}
3630Sstevel@tonic-gate 		/* If we didn't find it try loading it */
3640Sstevel@tonic-gate 		if (!t) {
3650Sstevel@tonic-gate 			if ((t = load_xlate(lr)) != NULL) {
3660Sstevel@tonic-gate 				/* add it to the list */
367132Srobinson 				(void) mutex_lock(&xlate_lock);
3680Sstevel@tonic-gate 				add_to_xlate_list(t);
369132Srobinson 				(void) mutex_unlock(&xlate_lock);
3700Sstevel@tonic-gate 				hs = (*(t->gba))(tp, addr);
3710Sstevel@tonic-gate 				if (hs) {
3720Sstevel@tonic-gate 					*serv = hs;
3730Sstevel@tonic-gate 					return (0);
3740Sstevel@tonic-gate 				}
375132Srobinson 				if (_nderror < 0)
376132Srobinson 					return (_nderror);
3770Sstevel@tonic-gate 			} else {
3780Sstevel@tonic-gate 				if (_nderror == ND_SYSTEM) { /* retry cache */
3790Sstevel@tonic-gate 					_nderror = ND_OK;
3800Sstevel@tonic-gate 					i--;
3810Sstevel@tonic-gate 					continue;
3820Sstevel@tonic-gate 				}
3830Sstevel@tonic-gate 			}
3840Sstevel@tonic-gate 		}
3850Sstevel@tonic-gate 	}
3860Sstevel@tonic-gate 	return (_nderror);	/* No one works */
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate /*
3900Sstevel@tonic-gate  * This is the library routine to do transport specific stuff.
3910Sstevel@tonic-gate  * The code is same as the other similar routines except that it does
3920Sstevel@tonic-gate  * not bother to try whole bunch of routines since if the first
3930Sstevel@tonic-gate  * libray cannot resolve the option, then no one can.
3940Sstevel@tonic-gate  *
3950Sstevel@tonic-gate  * If it gets a netconfig structure for inet transports with nametoddr libs,
3960Sstevel@tonic-gate  * it simply calls the inet-specific built in implementation.
3970Sstevel@tonic-gate  */
3980Sstevel@tonic-gate int
netdir_options(struct netconfig * tp,int option,int fd,char * par)399132Srobinson netdir_options(struct netconfig *tp, int option, int fd, char *par)
4000Sstevel@tonic-gate {
4010Sstevel@tonic-gate 	struct translator	*t;	/* pointer to translator list	*/
4020Sstevel@tonic-gate 	char			*lr;	/* routines to try		*/
4030Sstevel@tonic-gate 	int			i;	/* counts the routines		*/
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	if (tp == 0) {
4060Sstevel@tonic-gate 		_nderror = ND_BADARG;
4070Sstevel@tonic-gate 		return (_nderror);
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	if ((strcmp(tp->nc_protofmly, NC_INET) == 0 ||
4110Sstevel@tonic-gate 		strcmp(tp->nc_protofmly, NC_INET6) == 0) &&
4120Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
4130Sstevel@tonic-gate 		return (__inet_netdir_options(tp, option, fd, par));
4140Sstevel@tonic-gate 	}
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	for (i = 0; i < tp->nc_nlookups; i++) {
4180Sstevel@tonic-gate 		lr = *((tp->nc_lookups) + i);
4190Sstevel@tonic-gate 		for (t = xlate_list; t; t = t->next) {
420132Srobinson 			if (strcmp(lr, t->tr_name) == 0)
4210Sstevel@tonic-gate 				return ((*(t->opt))(tp, option, fd, par));
4220Sstevel@tonic-gate 		}
4230Sstevel@tonic-gate 		/* If we didn't find it try loading it */
4240Sstevel@tonic-gate 		if (!t) {
4250Sstevel@tonic-gate 			if ((t = load_xlate(lr)) != NULL) {
4260Sstevel@tonic-gate 				/* add it to the list */
427132Srobinson 				(void) mutex_lock(&xlate_lock);
4280Sstevel@tonic-gate 				add_to_xlate_list(t);
429132Srobinson 				(void) mutex_unlock(&xlate_lock);
4300Sstevel@tonic-gate 				return ((*(t->opt))(tp, option, fd, par));
431132Srobinson 			}
432132Srobinson 			if (_nderror == ND_SYSTEM) { /* retry cache */
433132Srobinson 				_nderror = ND_OK;
434132Srobinson 				i--;
435132Srobinson 				continue;
4360Sstevel@tonic-gate 			}
4370Sstevel@tonic-gate 		}
4380Sstevel@tonic-gate 	}
4390Sstevel@tonic-gate 	return (_nderror);	/* No one works */
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate /*
4430Sstevel@tonic-gate  * This is the library routine for translating universal addresses to
4440Sstevel@tonic-gate  * transport specific addresses. Again it uses the same code as above
4450Sstevel@tonic-gate  * to search for the appropriate translation routine. Only it doesn't
4460Sstevel@tonic-gate  * bother trying a whole bunch of routines since either the transport
4470Sstevel@tonic-gate  * can translate it or it can't.
4480Sstevel@tonic-gate  */
4490Sstevel@tonic-gate struct netbuf *
uaddr2taddr(struct netconfig * tp,char * addr)450132Srobinson uaddr2taddr(struct netconfig *tp, char *addr)
4510Sstevel@tonic-gate {
4520Sstevel@tonic-gate 	struct translator	*t;	/* pointer to translator list 	*/
4530Sstevel@tonic-gate 	struct netbuf		*x;	/* the answer we want 		*/
4540Sstevel@tonic-gate 	char			*lr;	/* routines to try		*/
4550Sstevel@tonic-gate 	int			i;	/* counts the routines		*/
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	if (tp == 0) {
4580Sstevel@tonic-gate 		_nderror = ND_BADARG;
4590Sstevel@tonic-gate 		return (0);
4600Sstevel@tonic-gate 	}
4610Sstevel@tonic-gate 	if ((strcmp(tp->nc_protofmly, NC_INET) == 0 ||
4620Sstevel@tonic-gate 		strcmp(tp->nc_protofmly, NC_INET6) == 0) &&
4630Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
4640Sstevel@tonic-gate 		return (__inet_uaddr2taddr(tp, addr));
4650Sstevel@tonic-gate 	}
4660Sstevel@tonic-gate 	for (i = 0; i < tp->nc_nlookups; i++) {
4670Sstevel@tonic-gate 		lr = *((tp->nc_lookups) + i);
4680Sstevel@tonic-gate 		for (t = xlate_list; t; t = t->next) {
4690Sstevel@tonic-gate 			if (strcmp(lr, t->tr_name) == 0) {
4700Sstevel@tonic-gate 				x = (*(t->u2t))(tp, addr);
471132Srobinson 				if (x)
4720Sstevel@tonic-gate 					return (x);
473132Srobinson 				if (_nderror < 0)
4740Sstevel@tonic-gate 					return (0);
4750Sstevel@tonic-gate 			}
4760Sstevel@tonic-gate 		}
4770Sstevel@tonic-gate 		/* If we didn't find it try loading it */
4780Sstevel@tonic-gate 		if (!t) {
4790Sstevel@tonic-gate 			if ((t = load_xlate(lr)) != NULL) {
4800Sstevel@tonic-gate 				/* add it to the list */
481132Srobinson 				(void) mutex_lock(&xlate_lock);
4820Sstevel@tonic-gate 				add_to_xlate_list(t);
483132Srobinson 				(void) mutex_unlock(&xlate_lock);
4840Sstevel@tonic-gate 				x = (*(t->u2t))(tp, addr);
485132Srobinson 				if (x)
4860Sstevel@tonic-gate 					return (x);
487132Srobinson 				if (_nderror < 0)
4880Sstevel@tonic-gate 					return (0);
4890Sstevel@tonic-gate 			} else {
4900Sstevel@tonic-gate 				if (_nderror == ND_SYSTEM) { /* retry cache */
4910Sstevel@tonic-gate 					_nderror = ND_OK;
4920Sstevel@tonic-gate 					i--;
4930Sstevel@tonic-gate 					continue;
4940Sstevel@tonic-gate 				}
4950Sstevel@tonic-gate 			}
4960Sstevel@tonic-gate 		}
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 	return (0);	/* No one works */
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate /*
5020Sstevel@tonic-gate  * This is the library routine for translating transport specific
5030Sstevel@tonic-gate  * addresses to universal addresses. Again it uses the same code as above
5040Sstevel@tonic-gate  * to search for the appropriate translation routine. Only it doesn't
5050Sstevel@tonic-gate  * bother trying a whole bunch of routines since either the transport
5060Sstevel@tonic-gate  * can translate it or it can't.
5070Sstevel@tonic-gate  */
5080Sstevel@tonic-gate char *
taddr2uaddr(struct netconfig * tp,struct netbuf * addr)509132Srobinson taddr2uaddr(struct netconfig *tp, struct netbuf *addr)
5100Sstevel@tonic-gate {
5110Sstevel@tonic-gate 	struct translator	*t;	/* pointer to translator list	*/
5120Sstevel@tonic-gate 	char			*lr;	/* routines to try		*/
5130Sstevel@tonic-gate 	char			*x;	/* the answer			*/
5140Sstevel@tonic-gate 	int			i;	/* counts the routines		*/
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	if (tp == 0) {
5170Sstevel@tonic-gate 		_nderror = ND_BADARG;
5180Sstevel@tonic-gate 		return (0);
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 	if ((strcmp(tp->nc_protofmly, NC_INET) == 0 ||
5210Sstevel@tonic-gate 		strcmp(tp->nc_protofmly, NC_INET6) == 0) &&
5220Sstevel@tonic-gate 		(tp->nc_nlookups == 0)) {
5230Sstevel@tonic-gate 		return (__inet_taddr2uaddr(tp, addr));
5240Sstevel@tonic-gate 	}
5250Sstevel@tonic-gate 	for (i = 0; i < tp->nc_nlookups; i++) {
5260Sstevel@tonic-gate 		lr = *((tp->nc_lookups) + i);
5270Sstevel@tonic-gate 		for (t = xlate_list; t; t = t->next) {
5280Sstevel@tonic-gate 			if (strcmp(lr, t->tr_name) == 0) {
5290Sstevel@tonic-gate 				x = (*(t->t2u))(tp, addr);
530132Srobinson 				if (x)
5310Sstevel@tonic-gate 					return (x);
532132Srobinson 				if (_nderror < 0)
5330Sstevel@tonic-gate 					return (0);
5340Sstevel@tonic-gate 			}
5350Sstevel@tonic-gate 		}
5360Sstevel@tonic-gate 		/* If we didn't find it try loading it */
5370Sstevel@tonic-gate 		if (!t) {
5380Sstevel@tonic-gate 			if ((t = load_xlate(lr)) != NULL) {
5390Sstevel@tonic-gate 				/* add it to the list */
540132Srobinson 				(void) mutex_lock(&xlate_lock);
5410Sstevel@tonic-gate 				add_to_xlate_list(t);
542132Srobinson 				(void) mutex_unlock(&xlate_lock);
5430Sstevel@tonic-gate 				x = (*(t->t2u))(tp, addr);
544132Srobinson 				if (x)
5450Sstevel@tonic-gate 					return (x);
546132Srobinson 				if (_nderror < 0)
5470Sstevel@tonic-gate 					return (0);
5480Sstevel@tonic-gate 			} else {
5490Sstevel@tonic-gate 				if (_nderror == ND_SYSTEM) { /* retry cache */
5500Sstevel@tonic-gate 					_nderror = ND_OK;
5510Sstevel@tonic-gate 					i--;
5520Sstevel@tonic-gate 					continue;
5530Sstevel@tonic-gate 				}
5540Sstevel@tonic-gate 			}
5550Sstevel@tonic-gate 		}
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 	return (0);	/* No one works */
5580Sstevel@tonic-gate }
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate /*
5610Sstevel@tonic-gate  * This is the routine that frees the objects that these routines allocate.
5620Sstevel@tonic-gate  */
5630Sstevel@tonic-gate void
netdir_free(void * ptr,int type)564132Srobinson netdir_free(void *ptr, int type)
5650Sstevel@tonic-gate {
5660Sstevel@tonic-gate 	struct netbuf		*na;
5670Sstevel@tonic-gate 	struct nd_addrlist	*nas;
5680Sstevel@tonic-gate 	struct nd_hostserv	*hs;
5690Sstevel@tonic-gate 	struct nd_hostservlist	*hss;
5700Sstevel@tonic-gate 	int			i;
5710Sstevel@tonic-gate 
572132Srobinson 	if (ptr == NULL)
5730Sstevel@tonic-gate 		return;
5740Sstevel@tonic-gate 	switch (type) {
5750Sstevel@tonic-gate 	case ND_ADDR :
5760Sstevel@tonic-gate 		na = (struct netbuf *)ptr;
5770Sstevel@tonic-gate 		if (na->buf)
5780Sstevel@tonic-gate 			free(na->buf);
579132Srobinson 		free(na);
5800Sstevel@tonic-gate 		break;
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	case ND_ADDRLIST :
5830Sstevel@tonic-gate 		nas = (struct nd_addrlist *)ptr;
5840Sstevel@tonic-gate 		/*
5850Sstevel@tonic-gate 		 * XXX: We do NOT try to free all individual netbuf->buf
5860Sstevel@tonic-gate 		 * pointers. Free only the first one since they are allocated
5870Sstevel@tonic-gate 		 * using one calloc in
5880Sstevel@tonic-gate 		 * libnsl/nss/netdir_inet.c:order_haddrlist().
5890Sstevel@tonic-gate 		 * This potentially causes memory leaks if a nametoaddr
5900Sstevel@tonic-gate 		 * implementation -- from a third party -- has a different
5910Sstevel@tonic-gate 		 * allocation scheme.
5920Sstevel@tonic-gate 		 */
5930Sstevel@tonic-gate 		if (nas->n_addrs->buf)
5940Sstevel@tonic-gate 			free(nas->n_addrs->buf);
595132Srobinson 		free(nas->n_addrs);
596132Srobinson 		free(nas);
5970Sstevel@tonic-gate 		break;
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	case ND_HOSTSERV :
6000Sstevel@tonic-gate 		hs = (struct nd_hostserv *)ptr;
6010Sstevel@tonic-gate 		if (hs->h_host)
6020Sstevel@tonic-gate 			free(hs->h_host);
6030Sstevel@tonic-gate 		if (hs->h_serv)
6040Sstevel@tonic-gate 			free(hs->h_serv);
605132Srobinson 		free(hs);
6060Sstevel@tonic-gate 		break;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	case ND_HOSTSERVLIST :
6090Sstevel@tonic-gate 		hss = (struct nd_hostservlist *)ptr;
6100Sstevel@tonic-gate 		for (hs = hss->h_hostservs, i = 0; i < hss->h_cnt; i++, hs++) {
6110Sstevel@tonic-gate 			if (hs->h_host)
6120Sstevel@tonic-gate 				free(hs->h_host);
6130Sstevel@tonic-gate 			if (hs->h_serv)
6140Sstevel@tonic-gate 				free(hs->h_serv);
6150Sstevel@tonic-gate 		}
616132Srobinson 		free(hss->h_hostservs);
617132Srobinson 		free(hss);
6180Sstevel@tonic-gate 		break;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	default :
6210Sstevel@tonic-gate 		_nderror = ND_UKNWN;
6220Sstevel@tonic-gate 		break;
6230Sstevel@tonic-gate 	}
6240Sstevel@tonic-gate }
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate /*
6270Sstevel@tonic-gate  * load_xlate is a routine that will attempt to dynamically link in the
6280Sstevel@tonic-gate  * file specified by the network configuration structure.
6290Sstevel@tonic-gate  */
6300Sstevel@tonic-gate static struct translator *
load_xlate(char * name)631132Srobinson load_xlate(char *name)
6320Sstevel@tonic-gate {
6330Sstevel@tonic-gate 	struct translator	*t;
6340Sstevel@tonic-gate 	static struct xlate_list {
6350Sstevel@tonic-gate 		char *library;
6360Sstevel@tonic-gate 		struct xlate_list *next;
6370Sstevel@tonic-gate 	} *xlistp = NULL;
6380Sstevel@tonic-gate 	struct xlate_list *xlp, **xlastp;
6390Sstevel@tonic-gate 	static mutex_t xlist_lock = DEFAULTMUTEX;
6400Sstevel@tonic-gate 
641132Srobinson 	(void) mutex_lock(&xlist_lock);
6420Sstevel@tonic-gate 	/*
6430Sstevel@tonic-gate 	 * We maintain a list of libraries we have loaded.  Loading a library
6440Sstevel@tonic-gate 	 * twice is double-plus ungood!
6450Sstevel@tonic-gate 	 */
6460Sstevel@tonic-gate 	for (xlp = xlistp, xlastp = &xlistp; xlp != NULL;
6470Sstevel@tonic-gate 			xlastp = &xlp->next, xlp = xlp->next) {
6480Sstevel@tonic-gate 		if (xlp->library != NULL) {
6490Sstevel@tonic-gate 			if (strcmp(xlp->library, name) == 0) {
6500Sstevel@tonic-gate 				_nderror = ND_SYSTEM;	/* seen this lib */
651132Srobinson 				(void) mutex_unlock(&xlist_lock);
6520Sstevel@tonic-gate 				return (0);
6530Sstevel@tonic-gate 			}
6540Sstevel@tonic-gate 		}
6550Sstevel@tonic-gate 	}
656132Srobinson 	t = malloc(sizeof (struct translator));
6570Sstevel@tonic-gate 	if (!t) {
6580Sstevel@tonic-gate 		_nderror = ND_NOMEM;
659132Srobinson 		(void) mutex_unlock(&xlist_lock);
6600Sstevel@tonic-gate 		return (0);
6610Sstevel@tonic-gate 	}
6620Sstevel@tonic-gate 	t->tr_name = strdup(name);
6630Sstevel@tonic-gate 	if (!t->tr_name) {
6640Sstevel@tonic-gate 		_nderror = ND_NOMEM;
665132Srobinson 		free(t);
666132Srobinson 		(void) mutex_unlock(&xlist_lock);
6670Sstevel@tonic-gate 		return (NULL);
6680Sstevel@tonic-gate 	}
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	t->tr_fd = dlopen(name, RTLD_LAZY);
6710Sstevel@tonic-gate 	if (t->tr_fd == NULL) {
6720Sstevel@tonic-gate 		_nderror = ND_OPEN;
6730Sstevel@tonic-gate 		goto error;
6740Sstevel@tonic-gate 	}
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	/* Resolve the getbyname symbol */
6770Sstevel@tonic-gate 	t->gbn = (struct nd_addrlist *(*)())dlsym(t->tr_fd,
6780Sstevel@tonic-gate 				"_netdir_getbyname");
6790Sstevel@tonic-gate 	if (!(t->gbn)) {
6800Sstevel@tonic-gate 		_nderror = ND_NOSYM;
6810Sstevel@tonic-gate 		goto error;
6820Sstevel@tonic-gate 	}
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	/* resolve the getbyaddr symbol */
6850Sstevel@tonic-gate 	t->gba = (struct nd_hostservlist *(*)())dlsym(t->tr_fd,
6860Sstevel@tonic-gate 				"_netdir_getbyaddr");
6870Sstevel@tonic-gate 	if (!(t->gba)) {
6880Sstevel@tonic-gate 		_nderror = ND_NOSYM;
6890Sstevel@tonic-gate 		goto error;
6900Sstevel@tonic-gate 	}
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	/* resolve the taddr2uaddr symbol */
6930Sstevel@tonic-gate 	t->t2u = (char *(*)())dlsym(t->tr_fd, "_taddr2uaddr");
6940Sstevel@tonic-gate 	if (!(t->t2u)) {
6950Sstevel@tonic-gate 		_nderror = ND_NOSYM;
6960Sstevel@tonic-gate 		goto error;
6970Sstevel@tonic-gate 	}
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	/* resolve the uaddr2taddr symbol */
7000Sstevel@tonic-gate 	t->u2t = (struct netbuf *(*)())dlsym(t->tr_fd, "_uaddr2taddr");
7010Sstevel@tonic-gate 	if (!(t->u2t)) {
7020Sstevel@tonic-gate 		_nderror = ND_NOSYM;
7030Sstevel@tonic-gate 		goto error;
7040Sstevel@tonic-gate 	}
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	/* resolve the netdir_options symbol */
7070Sstevel@tonic-gate 	t->opt = (int (*)())dlsym(t->tr_fd, "_netdir_options");
7080Sstevel@tonic-gate 	if (!(t->opt)) {
7090Sstevel@tonic-gate 		_nderror = ND_NOSYM;
7100Sstevel@tonic-gate 		goto error;
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate 	/*
7130Sstevel@tonic-gate 	 * Add this library to the list of loaded libraries.
7140Sstevel@tonic-gate 	 */
715132Srobinson 	*xlastp = malloc(sizeof (struct xlate_list));
7160Sstevel@tonic-gate 	if (*xlastp == NULL) {
7170Sstevel@tonic-gate 		_nderror = ND_NOMEM;
7180Sstevel@tonic-gate 		goto error;
7190Sstevel@tonic-gate 	}
7200Sstevel@tonic-gate 	(*xlastp)->library = strdup(name);
7210Sstevel@tonic-gate 	if ((*xlastp)->library == NULL) {
7220Sstevel@tonic-gate 		_nderror = ND_NOMEM;
7230Sstevel@tonic-gate 		free(*xlastp);
7240Sstevel@tonic-gate 		goto error;
7250Sstevel@tonic-gate 	}
7260Sstevel@tonic-gate 	(*xlastp)->next = NULL;
727132Srobinson 	(void) mutex_unlock(&xlist_lock);
7280Sstevel@tonic-gate 	return (t);
7290Sstevel@tonic-gate error:
7300Sstevel@tonic-gate 	if (t->tr_fd != NULL)
7310Sstevel@tonic-gate 		(void) dlclose(t->tr_fd);
7320Sstevel@tonic-gate 	free(t->tr_name);
733132Srobinson 	free(t);
734132Srobinson 	(void) mutex_unlock(&xlist_lock);
7350Sstevel@tonic-gate 	return (NULL);
7360Sstevel@tonic-gate }
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate #define	NDERR_BUFSZ	512
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate /*
7420Sstevel@tonic-gate  * This is a routine that returns a string related to the current
7430Sstevel@tonic-gate  * error in _nderror.
7440Sstevel@tonic-gate  */
7450Sstevel@tonic-gate char *
netdir_sperror(void)746132Srobinson netdir_sperror(void)
7470Sstevel@tonic-gate {
748*3864Sraf 	static pthread_key_t nderrbuf_key = PTHREAD_ONCE_KEY_NP;
7490Sstevel@tonic-gate 	static char buf_main[NDERR_BUFSZ];
7500Sstevel@tonic-gate 	char	*str;
7510Sstevel@tonic-gate 	char *dlerrstr;
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	str = thr_main()?
7540Sstevel@tonic-gate 		buf_main :
7550Sstevel@tonic-gate 		thr_get_storage(&nderrbuf_key, NDERR_BUFSZ, free);
756132Srobinson 	if (str == NULL)
7570Sstevel@tonic-gate 		return (NULL);
7580Sstevel@tonic-gate 	dlerrstr = dlerror();
7590Sstevel@tonic-gate 	switch (_nderror) {
7600Sstevel@tonic-gate 	case ND_NOMEM :
7610Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7620Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: memory allocation failed"));
7630Sstevel@tonic-gate 		break;
7640Sstevel@tonic-gate 	case ND_OK :
7650Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7660Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: successful completion"));
7670Sstevel@tonic-gate 		break;
7680Sstevel@tonic-gate 	case ND_NOHOST :
7690Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7700Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: hostname not found"));
7710Sstevel@tonic-gate 		break;
7720Sstevel@tonic-gate 	case ND_NOSERV :
7730Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7740Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: service name not found"));
7750Sstevel@tonic-gate 		break;
7760Sstevel@tonic-gate 	case ND_NOSYM :
7770Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ, "%s : %s ",
7780Sstevel@tonic-gate 			dgettext(__nsl_dom,
7790Sstevel@tonic-gate 			"n2a: symbol missing in shared object"),
7800Sstevel@tonic-gate 			dlerrstr ? dlerrstr : " ");
7810Sstevel@tonic-gate 		break;
7820Sstevel@tonic-gate 	case ND_OPEN :
7830Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ, "%s - %s ",
7840Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: couldn't open shared object"),
7850Sstevel@tonic-gate 			dlerrstr ? dlerrstr : " ");
7860Sstevel@tonic-gate 		break;
7870Sstevel@tonic-gate 	case ND_ACCESS :
7880Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7890Sstevel@tonic-gate 			dgettext(__nsl_dom,
7900Sstevel@tonic-gate 			"n2a: access denied for shared object"));
7910Sstevel@tonic-gate 		break;
7920Sstevel@tonic-gate 	case ND_UKNWN :
7930Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7940Sstevel@tonic-gate 			dgettext(__nsl_dom,
7950Sstevel@tonic-gate 			"n2a: attempt to free unknown object"));
7960Sstevel@tonic-gate 		break;
7970Sstevel@tonic-gate 	case ND_BADARG :
7980Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
7990Sstevel@tonic-gate 			dgettext(__nsl_dom,
8000Sstevel@tonic-gate 			"n2a: bad arguments passed to routine"));
8010Sstevel@tonic-gate 		break;
8020Sstevel@tonic-gate 	case ND_NOCTRL:
8030Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
8040Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: unknown option passed"));
8050Sstevel@tonic-gate 		break;
8060Sstevel@tonic-gate 	case ND_FAILCTRL:
8070Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ,
8080Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: control operation failed"));
8090Sstevel@tonic-gate 		break;
8100Sstevel@tonic-gate 	case ND_SYSTEM:
8110Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ, "%s: %s",
8120Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: system error"),
8130Sstevel@tonic-gate 			strerror(errno));
8140Sstevel@tonic-gate 		break;
8150Sstevel@tonic-gate 	default :
8160Sstevel@tonic-gate 		(void) snprintf(str, NDERR_BUFSZ, "%s#%d",
8170Sstevel@tonic-gate 			dgettext(__nsl_dom, "n2a: unknown error "), _nderror);
8180Sstevel@tonic-gate 		break;
8190Sstevel@tonic-gate 	}
8200Sstevel@tonic-gate 	return (str);
8210Sstevel@tonic-gate }
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate /*
8240Sstevel@tonic-gate  * This is a routine that prints out strings related to the current
8250Sstevel@tonic-gate  * error in _nderror. Like perror() it takes a string to print with a
8260Sstevel@tonic-gate  * colon first.
8270Sstevel@tonic-gate  */
8280Sstevel@tonic-gate void
netdir_perror(char * s)829132Srobinson netdir_perror(char *s)
8300Sstevel@tonic-gate {
8310Sstevel@tonic-gate 	char	*err;
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	err = netdir_sperror();
8340Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s\n", s, err ? err : "n2a: error");
8350Sstevel@tonic-gate }
836