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*cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6*cb5caa98Sdjl  * 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  */
217c478bd9Sstevel@tonic-gate /*
22*cb5caa98Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*cb5caa98Sdjl  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * gethostent.c
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * In order to avoid duplicating libresolv code here, and since libresolv.so.2
327c478bd9Sstevel@tonic-gate  * provides res_-equivalents of the getXbyY and {set,get}Xent, lets call
337c478bd9Sstevel@tonic-gate  * re_gethostbyaddr() and so on from this file. Among other things, this
347c478bd9Sstevel@tonic-gate  * should help us avoid problems like the one described in bug 1264386,
357c478bd9Sstevel@tonic-gate  * where the internal getanswer() acquired new functionality in BIND 4.9.3,
367c478bd9Sstevel@tonic-gate  * but the local copy of getanswer() in this file wasn't updated, so that new
377c478bd9Sstevel@tonic-gate  * functionality wasn't available to the name service switch.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #define	gethostbyaddr	res_gethostbyaddr
417c478bd9Sstevel@tonic-gate #define	gethostbyname	res_gethostbyname
427c478bd9Sstevel@tonic-gate #define	gethostbyname2	res_gethostbyname2
437c478bd9Sstevel@tonic-gate #define	sethostent	res_sethostent
447c478bd9Sstevel@tonic-gate #define	endhostent	res_endhostent
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include "dns_common.h"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate extern char *inet_ntoa(struct in_addr in);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate struct hostent *_gethostbyname(int *h_errnop, const char *name);
517c478bd9Sstevel@tonic-gate static struct hostent *_gethostbyaddr(int *h_errnop, const char *addr,
527c478bd9Sstevel@tonic-gate     int len, int type);
537c478bd9Sstevel@tonic-gate struct hostent *_nss_dns_gethostbyname2(int *h_errnop, const char *name);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #pragma weak	res_gethostbyname
567c478bd9Sstevel@tonic-gate #pragma weak	res_gethostbyname2
577c478bd9Sstevel@tonic-gate #pragma weak	res_gethostbyaddr
587c478bd9Sstevel@tonic-gate #pragma weak	res_sethostent
597c478bd9Sstevel@tonic-gate #pragma weak	res_endhostent
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate nss_backend_t *_nss_dns_constr(dns_backend_op_t ops[], int n_ops);
627c478bd9Sstevel@tonic-gate nss_status_t __nss_dns_getbyaddr(dns_backend_ptr_t, void *);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate typedef union {
657c478bd9Sstevel@tonic-gate 	long al;
667c478bd9Sstevel@tonic-gate 	char ac;
677c478bd9Sstevel@tonic-gate } align;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate  * Internet Name Domain Server (DNS) only implementation.
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate static struct hostent *
737c478bd9Sstevel@tonic-gate _gethostbyaddr(int *h_errnop, const char *addr, int len, int type)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	struct hostent	*hp;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	hp = gethostbyaddr(addr, len, type);
787c478bd9Sstevel@tonic-gate 	*h_errnop = *get_h_errno();
797c478bd9Sstevel@tonic-gate 	return (hp);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate struct hostent *
837c478bd9Sstevel@tonic-gate _nss_dns_gethostbyname2(int *h_errnop, const char *name)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	struct hostent *hp;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	hp = gethostbyname2(name, AF_INET6);
887c478bd9Sstevel@tonic-gate 	*h_errnop = *get_h_errno();
897c478bd9Sstevel@tonic-gate 	return (hp);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate struct hostent *
937c478bd9Sstevel@tonic-gate _gethostbyname(int *h_errnop, const char *name)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	struct hostent *hp;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	hp = gethostbyname(name);
987c478bd9Sstevel@tonic-gate 	*h_errnop = *get_h_errno();
997c478bd9Sstevel@tonic-gate 	return (hp);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static void
1037c478bd9Sstevel@tonic-gate _sethostent(errp, stayopen)
1047c478bd9Sstevel@tonic-gate 	nss_status_t	*errp;
1057c478bd9Sstevel@tonic-gate 	int		stayopen;
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	int	ret;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	ret = sethostent(stayopen);
1107c478bd9Sstevel@tonic-gate 	if (ret == 0)
1117c478bd9Sstevel@tonic-gate 		*errp = NSS_SUCCESS;
1127c478bd9Sstevel@tonic-gate 	else
1137c478bd9Sstevel@tonic-gate 		*errp = NSS_UNAVAIL;
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate static void
1177c478bd9Sstevel@tonic-gate _endhostent(errp)
1187c478bd9Sstevel@tonic-gate 	nss_status_t	*errp;
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	int	ret;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	ret = endhostent();
1237c478bd9Sstevel@tonic-gate 	if (ret == 0)
1247c478bd9Sstevel@tonic-gate 		*errp = NSS_SUCCESS;
1257c478bd9Sstevel@tonic-gate 	else
1267c478bd9Sstevel@tonic-gate 		*errp = NSS_UNAVAIL;
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1317c478bd9Sstevel@tonic-gate static nss_status_t
1327c478bd9Sstevel@tonic-gate getbyname(be, a)
1337c478bd9Sstevel@tonic-gate 	dns_backend_ptr_t	be;
1347c478bd9Sstevel@tonic-gate 	void			*a;
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	struct hostent	*he;
1377c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
1387c478bd9Sstevel@tonic-gate 	int		ret, mt_disabled;
1397c478bd9Sstevel@tonic-gate 	int		old_retry;
1407c478bd9Sstevel@tonic-gate 	sigset_t	oldmask;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	switch_resolver_setup(&mt_disabled, &oldmask, &old_retry);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	he = _gethostbyname(&argp->h_errno, argp->key.name);
1457c478bd9Sstevel@tonic-gate 	if (he != NULL) {
146*cb5caa98Sdjl 		if (argp->buf.result == NULL) {
147*cb5caa98Sdjl 			/*
148*cb5caa98Sdjl 			 * if asked to return data in string,
149*cb5caa98Sdjl 			 * convert the hostent structure into
150*cb5caa98Sdjl 			 * string data
151*cb5caa98Sdjl 			 */
152*cb5caa98Sdjl 			ret = ent2str(he, a, AF_INET);
153*cb5caa98Sdjl 			if (ret == NSS_STR_PARSE_SUCCESS)
154*cb5caa98Sdjl 				argp->returnval = argp->buf.buffer;
1557c478bd9Sstevel@tonic-gate 		} else {
156*cb5caa98Sdjl 			ret = ent2result(he, a, AF_INET);
157*cb5caa98Sdjl 			if (ret == NSS_STR_PARSE_SUCCESS)
158*cb5caa98Sdjl 				argp->returnval = argp->buf.result;
159*cb5caa98Sdjl 		}
160*cb5caa98Sdjl 
161*cb5caa98Sdjl 		if (ret != NSS_STR_PARSE_SUCCESS) {
1627c478bd9Sstevel@tonic-gate 			argp->h_errno = HOST_NOT_FOUND;
1637c478bd9Sstevel@tonic-gate 			if (ret == NSS_STR_PARSE_ERANGE) {
1647c478bd9Sstevel@tonic-gate 				argp->erange = 1;
1657c478bd9Sstevel@tonic-gate 			}
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	switch_resolver_reset(mt_disabled, oldmask, old_retry);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	return (_herrno2nss(argp->h_errno));
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1777c478bd9Sstevel@tonic-gate static nss_status_t
1787c478bd9Sstevel@tonic-gate getbyaddr(be, a)
1797c478bd9Sstevel@tonic-gate 	dns_backend_ptr_t	be;
1807c478bd9Sstevel@tonic-gate 	void			*a;
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	return (__nss_dns_getbyaddr(be, a));
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate  * Exposing a DNS backend specific interface so that it doesn't conflict
1887c478bd9Sstevel@tonic-gate  * with other getbyaddr() routines from other switch backends.
1897c478bd9Sstevel@tonic-gate  */
190*cb5caa98Sdjl /*ARGSUSED*/
1917c478bd9Sstevel@tonic-gate nss_status_t
1927c478bd9Sstevel@tonic-gate __nss_dns_getbyaddr(be, a)
1937c478bd9Sstevel@tonic-gate 	dns_backend_ptr_t	be;
1947c478bd9Sstevel@tonic-gate 	void			*a;
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	size_t	n;
1977c478bd9Sstevel@tonic-gate 	struct hostent	*he, *he2;
1987c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
1997c478bd9Sstevel@tonic-gate 	int		ret, save_h_errno, mt_disabled;
2007c478bd9Sstevel@tonic-gate 	char		**ans, hbuf[MAXHOSTNAMELEN];
2017c478bd9Sstevel@tonic-gate 	char		dst[INET6_ADDRSTRLEN];
2027c478bd9Sstevel@tonic-gate 	struct in_addr	unmapv4;
2037c478bd9Sstevel@tonic-gate 	sigset_t	oldmask;
2047c478bd9Sstevel@tonic-gate 	int		af, addrlen;
2057c478bd9Sstevel@tonic-gate 	void		*addrp;
2067c478bd9Sstevel@tonic-gate 	int		old_retry;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	switch_resolver_setup(&mt_disabled, &oldmask, &old_retry);
2097c478bd9Sstevel@tonic-gate 
210*cb5caa98Sdjl 	/* LINTED: E_BAD_PTR_CAST_ALIGN */
2117c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)argp->key.hostaddr.addr)) {
2127c478bd9Sstevel@tonic-gate 		addrp = &unmapv4;
2137c478bd9Sstevel@tonic-gate 		addrlen = sizeof (unmapv4);
2147c478bd9Sstevel@tonic-gate 		af = AF_INET;
215*cb5caa98Sdjl 		(void) memcpy(addrp, &argp->key.hostaddr.addr[12], addrlen);
2167c478bd9Sstevel@tonic-gate 	} else {
2177c478bd9Sstevel@tonic-gate 		addrp = (void *)argp->key.hostaddr.addr;
2187c478bd9Sstevel@tonic-gate 		addrlen = argp->key.hostaddr.len;
2197c478bd9Sstevel@tonic-gate 		af = argp->key.hostaddr.type;
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 	he = _gethostbyaddr(&argp->h_errno, addrp, addrlen, af);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	if (he != NULL) {
2247c478bd9Sstevel@tonic-gate 		if (strlen(he->h_name) >= MAXHOSTNAMELEN)
2257c478bd9Sstevel@tonic-gate 			ret = NSS_STR_PARSE_ERANGE;
2267c478bd9Sstevel@tonic-gate 		else {
2277c478bd9Sstevel@tonic-gate 			/* save a copy of the (alleged) hostname */
2287c478bd9Sstevel@tonic-gate 			(void) strcpy(hbuf, he->h_name);
2297c478bd9Sstevel@tonic-gate 			n = strlen(hbuf);
2307c478bd9Sstevel@tonic-gate 			if (n < MAXHOSTNAMELEN-1 && hbuf[n-1] != '.') {
2317c478bd9Sstevel@tonic-gate 				(void) strcat(hbuf, ".");
2327c478bd9Sstevel@tonic-gate 			}
233*cb5caa98Sdjl 
234*cb5caa98Sdjl 			/*
235*cb5caa98Sdjl 			 * if asked to return data in string,
236*cb5caa98Sdjl 			 * convert the hostent structure into
237*cb5caa98Sdjl 			 * string data
238*cb5caa98Sdjl 			 */
239*cb5caa98Sdjl 			if (argp->buf.result == NULL)
240*cb5caa98Sdjl 				ret = ent2str(he, a, argp->key.hostaddr.type);
241*cb5caa98Sdjl 			else
242*cb5caa98Sdjl 				ret = ent2result(he, a,
243*cb5caa98Sdjl 					argp->key.hostaddr.type);
244*cb5caa98Sdjl 
2457c478bd9Sstevel@tonic-gate 			save_h_errno = argp->h_errno;
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 		if (ret == NSS_STR_PARSE_SUCCESS) {
2487c478bd9Sstevel@tonic-gate 			/*
2497c478bd9Sstevel@tonic-gate 			 * check to make sure by doing a forward query
2507c478bd9Sstevel@tonic-gate 			 * We use _gethostbyname() to avoid the stack, and
2517c478bd9Sstevel@tonic-gate 			 * then we throw the result from argp->h_errno away,
2527c478bd9Sstevel@tonic-gate 			 * becase we don't care.  And besides you want the
2537c478bd9Sstevel@tonic-gate 			 * return code from _gethostbyaddr() anyway.
2547c478bd9Sstevel@tonic-gate 			 */
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 			if (af == AF_INET)
2577c478bd9Sstevel@tonic-gate 				he2 = _gethostbyname(&argp->h_errno, hbuf);
2587c478bd9Sstevel@tonic-gate 			else
2597c478bd9Sstevel@tonic-gate 				he2 = _nss_dns_gethostbyname2(&argp->h_errno,
2607c478bd9Sstevel@tonic-gate 					hbuf);
2617c478bd9Sstevel@tonic-gate 			if (he2 != (struct hostent *)NULL) {
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 				/* until we prove name and addr match */
2647c478bd9Sstevel@tonic-gate 				argp->h_errno = HOST_NOT_FOUND;
2657c478bd9Sstevel@tonic-gate 				for (ans = he2->h_addr_list; *ans; ans++)
2667c478bd9Sstevel@tonic-gate 					if (memcmp(*ans, addrp,	addrlen) ==
2677c478bd9Sstevel@tonic-gate 						0) {
2687c478bd9Sstevel@tonic-gate 					argp->h_errno = save_h_errno;
269*cb5caa98Sdjl 					if (argp->buf.result == NULL)
270*cb5caa98Sdjl 						argp->returnval =
271*cb5caa98Sdjl 							argp->buf.buffer;
272*cb5caa98Sdjl 					else
273*cb5caa98Sdjl 						argp->returnval =
274*cb5caa98Sdjl 							argp->buf.result;
2757c478bd9Sstevel@tonic-gate 					break;
2767c478bd9Sstevel@tonic-gate 						}
2777c478bd9Sstevel@tonic-gate 			} else {
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 				/*
2807c478bd9Sstevel@tonic-gate 				 * What to do if _gethostbyname() fails ???
2817c478bd9Sstevel@tonic-gate 				 * We assume they are doing something stupid
2827c478bd9Sstevel@tonic-gate 				 * like registering addresses but not names
2837c478bd9Sstevel@tonic-gate 				 * (some people actually think that provides
2847c478bd9Sstevel@tonic-gate 				 * some "security", through obscurity).  So for
2857c478bd9Sstevel@tonic-gate 				 * these poor lost souls, because we can't
2867c478bd9Sstevel@tonic-gate 				 * PROVE spoofing and because we did try (and
2877c478bd9Sstevel@tonic-gate 				 * we don't want a bug filed on this), we let
2887c478bd9Sstevel@tonic-gate 				 * this go.  And return the name from byaddr.
2897c478bd9Sstevel@tonic-gate 				 */
2907c478bd9Sstevel@tonic-gate 				argp->h_errno = save_h_errno;
291*cb5caa98Sdjl 				if (argp->buf.result == NULL)
292*cb5caa98Sdjl 					argp->returnval = argp->buf.buffer;
293*cb5caa98Sdjl 				else
294*cb5caa98Sdjl 					argp->returnval = argp->buf.result;
2957c478bd9Sstevel@tonic-gate 			}
2967c478bd9Sstevel@tonic-gate 			/* we've been spoofed, make sure to log it. */
2977c478bd9Sstevel@tonic-gate 			if (argp->h_errno == HOST_NOT_FOUND) {
2987c478bd9Sstevel@tonic-gate 				if (argp->key.hostaddr.type == AF_INET)
2997c478bd9Sstevel@tonic-gate 		syslog(LOG_NOTICE, "gethostbyaddr: %s != %s",
300*cb5caa98Sdjl 		/* LINTED: E_BAD_PTR_CAST_ALIGN */
3017c478bd9Sstevel@tonic-gate 		hbuf, inet_ntoa(*(struct in_addr *)argp->key.hostaddr.addr));
3027c478bd9Sstevel@tonic-gate 				else
3037c478bd9Sstevel@tonic-gate 		syslog(LOG_NOTICE, "gethostbyaddr: %s != %s",
3047c478bd9Sstevel@tonic-gate 		hbuf, inet_ntop(AF_INET6, (void *) argp->key.hostaddr.addr,
3057c478bd9Sstevel@tonic-gate 		dst, sizeof (dst)));
3067c478bd9Sstevel@tonic-gate 			}
3077c478bd9Sstevel@tonic-gate 		} else {
3087c478bd9Sstevel@tonic-gate 			argp->h_errno = HOST_NOT_FOUND;
3097c478bd9Sstevel@tonic-gate 			if (ret == NSS_STR_PARSE_ERANGE) {
3107c478bd9Sstevel@tonic-gate 				argp->erange = 1;
3117c478bd9Sstevel@tonic-gate 			}
3127c478bd9Sstevel@tonic-gate 		}
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	switch_resolver_reset(mt_disabled, oldmask, old_retry);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	return (_herrno2nss(argp->h_errno));
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3227c478bd9Sstevel@tonic-gate static nss_status_t
3237c478bd9Sstevel@tonic-gate _nss_dns_getent(be, args)
3247c478bd9Sstevel@tonic-gate 	dns_backend_ptr_t	be;
3257c478bd9Sstevel@tonic-gate 	void			*args;
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate 	return (NSS_UNAVAIL);
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3327c478bd9Sstevel@tonic-gate static nss_status_t
3337c478bd9Sstevel@tonic-gate _nss_dns_setent(be, dummy)
3347c478bd9Sstevel@tonic-gate 	dns_backend_ptr_t	be;
3357c478bd9Sstevel@tonic-gate 	void			*dummy;
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate 	nss_status_t	errp;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	sigset_t	oldmask, newmask;
3407c478bd9Sstevel@tonic-gate 	int		mt_disabled = 1;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	/*
3437c478bd9Sstevel@tonic-gate 	 * Try to enable MT; if not, we have to single-thread libresolv
3447c478bd9Sstevel@tonic-gate 	 * access
3457c478bd9Sstevel@tonic-gate 	 */
3467c478bd9Sstevel@tonic-gate 	if (enable_mt == 0 || (mt_disabled = (*enable_mt)()) != 0) {
3477c478bd9Sstevel@tonic-gate 		(void) sigfillset(&newmask);
3487c478bd9Sstevel@tonic-gate 		_thr_sigsetmask(SIG_SETMASK, &newmask, &oldmask);
3497c478bd9Sstevel@tonic-gate 		_mutex_lock(&one_lane);
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	_sethostent(&errp, 1);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	if (mt_disabled) {
3557c478bd9Sstevel@tonic-gate 		_mutex_unlock(&one_lane);
3567c478bd9Sstevel@tonic-gate 		_thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
3577c478bd9Sstevel@tonic-gate 	} else {
3587c478bd9Sstevel@tonic-gate 		(void) (*disable_mt)();
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	return (errp);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3667c478bd9Sstevel@tonic-gate static nss_status_t
3677c478bd9Sstevel@tonic-gate _nss_dns_endent(be, dummy)
3687c478bd9Sstevel@tonic-gate 	dns_backend_ptr_t	be;
3697c478bd9Sstevel@tonic-gate 	void			*dummy;
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate 	nss_status_t	errp;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	sigset_t	oldmask, newmask;
3747c478bd9Sstevel@tonic-gate 	int		mt_disabled = 1;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/*
3777c478bd9Sstevel@tonic-gate 	 * Try to enable MT; if not, we have to single-thread libresolv
3787c478bd9Sstevel@tonic-gate 	 * access
3797c478bd9Sstevel@tonic-gate 	 */
3807c478bd9Sstevel@tonic-gate 	if (enable_mt == 0 || (mt_disabled = (*enable_mt)()) != 0) {
3817c478bd9Sstevel@tonic-gate 		(void) sigfillset(&newmask);
3827c478bd9Sstevel@tonic-gate 		_thr_sigsetmask(SIG_SETMASK, &newmask, &oldmask);
3837c478bd9Sstevel@tonic-gate 		_mutex_lock(&one_lane);
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	_endhostent(&errp);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if (mt_disabled) {
3897c478bd9Sstevel@tonic-gate 		_mutex_unlock(&one_lane);
3907c478bd9Sstevel@tonic-gate 		_thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
3917c478bd9Sstevel@tonic-gate 	} else {
3927c478bd9Sstevel@tonic-gate 		(void) (*disable_mt)();
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	return (errp);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4007c478bd9Sstevel@tonic-gate static nss_status_t
4017c478bd9Sstevel@tonic-gate _nss_dns_destr(be, dummy)
4027c478bd9Sstevel@tonic-gate 	dns_backend_ptr_t	be;
4037c478bd9Sstevel@tonic-gate 	void			*dummy;
4047c478bd9Sstevel@tonic-gate {
4057c478bd9Sstevel@tonic-gate 	nss_status_t	errp;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if (be != 0) {
4087c478bd9Sstevel@tonic-gate 		/* === Should change to invoke ops[ENDENT] ? */
4097c478bd9Sstevel@tonic-gate 		sigset_t	oldmask, newmask;
4107c478bd9Sstevel@tonic-gate 		int		mt_disabled = 1;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 		if (enable_mt == 0 || (mt_disabled = (*enable_mt)()) != 0) {
4137c478bd9Sstevel@tonic-gate 			(void) sigfillset(&newmask);
4147c478bd9Sstevel@tonic-gate 			_thr_sigsetmask(SIG_SETMASK, &newmask, &oldmask);
4157c478bd9Sstevel@tonic-gate 			_mutex_lock(&one_lane);
4167c478bd9Sstevel@tonic-gate 		}
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 		_endhostent(&errp);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 		if (mt_disabled) {
4217c478bd9Sstevel@tonic-gate 			_mutex_unlock(&one_lane);
4227c478bd9Sstevel@tonic-gate 			_thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
4237c478bd9Sstevel@tonic-gate 		} else {
4247c478bd9Sstevel@tonic-gate 			(void) (*disable_mt)();
4257c478bd9Sstevel@tonic-gate 		}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 		free(be);
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 	return (NSS_SUCCESS);   /* In case anyone is dumb enough to check */
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate static dns_backend_op_t host_ops[] = {
4347c478bd9Sstevel@tonic-gate 	_nss_dns_destr,
4357c478bd9Sstevel@tonic-gate 	_nss_dns_endent,
4367c478bd9Sstevel@tonic-gate 	_nss_dns_setent,
4377c478bd9Sstevel@tonic-gate 	_nss_dns_getent,
4387c478bd9Sstevel@tonic-gate 	getbyname,
4397c478bd9Sstevel@tonic-gate 	getbyaddr,
4407c478bd9Sstevel@tonic-gate };
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4437c478bd9Sstevel@tonic-gate nss_backend_t *
4447c478bd9Sstevel@tonic-gate _nss_dns_hosts_constr(dummy1, dummy2, dummy3)
4457c478bd9Sstevel@tonic-gate 	const char	*dummy1, *dummy2, *dummy3;
4467c478bd9Sstevel@tonic-gate {
4477c478bd9Sstevel@tonic-gate 	return (_nss_dns_constr(host_ops,
4487c478bd9Sstevel@tonic-gate 		sizeof (host_ops) / sizeof (host_ops[0])));
4497c478bd9Sstevel@tonic-gate }
450*cb5caa98Sdjl 
451*cb5caa98Sdjl /*
452*cb5caa98Sdjl  * optional NSS2 packed backend gethostsbyname with ttl
453*cb5caa98Sdjl  * entry point.
454*cb5caa98Sdjl  *
455*cb5caa98Sdjl  * Returns:
456*cb5caa98Sdjl  *	NSS_SUCCESS - successful
457*cb5caa98Sdjl  *	NSS_NOTFOUND - successful but nothing found
458*cb5caa98Sdjl  *	NSS_ERROR - fallback to NSS backend lookup mode
459*cb5caa98Sdjl  * If successful, buffer will be filled with valid data
460*cb5caa98Sdjl  *
461*cb5caa98Sdjl  */
462*cb5caa98Sdjl 
463*cb5caa98Sdjl /*ARGSUSED*/
464*cb5caa98Sdjl nss_status_t
465*cb5caa98Sdjl _nss_get_dns_hosts_name(dns_backend_ptr_t *be, void **bufp, size_t *sizep)
466*cb5caa98Sdjl {
467*cb5caa98Sdjl 	return (_nss_dns_gethost_withttl(*bufp, *sizep, 0));
468*cb5caa98Sdjl }
469