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  */
2161961e0fSrobinson 
227c478bd9Sstevel@tonic-gate /*
23e8031f0aSraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * gethostbyname_r() is defined in this file.  It is implemented on top of
317c478bd9Sstevel@tonic-gate  *   _get_hostserv_inetnetdir_byname() which is also used to implement
327c478bd9Sstevel@tonic-gate  *   netdir_getbyname() for inet family transports.  In turn the common code
337c478bd9Sstevel@tonic-gate  *   uses the name service switch policy for "hosts" and "services" unless
347c478bd9Sstevel@tonic-gate  *   the administrator chooses to bypass the name service switch by
357c478bd9Sstevel@tonic-gate  *   specifying third-party supplied nametoaddr libs for inet transports
367c478bd9Sstevel@tonic-gate  *   in /etc/netconfig.
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * gethostbyaddr_r() is similarly related to _get_hostserv_inetnetdir_byaddr()
397c478bd9Sstevel@tonic-gate  *   and netdir_getbyaddr();
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  * The common code lives in netdir_inet.c.
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * gethostent_r(), sethostent() and endhostent() are *not* implemented on top
447c478bd9Sstevel@tonic-gate  *   of the common interface;  they go straight to the switch and are
457c478bd9Sstevel@tonic-gate  *   defined in gethostent_r.c.
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * There is absolutely no data sharing, not even the stayopen flag or
487c478bd9Sstevel@tonic-gate  *   enumeration state, between gethostbyYY_r() and gethostent_r();
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
51e8031f0aSraf #include "mt.h"
527c478bd9Sstevel@tonic-gate #include <netdb.h>
537c478bd9Sstevel@tonic-gate #include <netdir.h>
547c478bd9Sstevel@tonic-gate #include <sys/types.h>
557c478bd9Sstevel@tonic-gate #include <nss_netdir.h>
567c478bd9Sstevel@tonic-gate #include <string.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate extern struct netconfig *__rpc_getconfip();
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * h_errno POLICY: The frontends expect the name service
627c478bd9Sstevel@tonic-gate  * backends to modify the h_errno in "arg"; _switch_gethostbyYY_r()
637c478bd9Sstevel@tonic-gate  * will copy that over onto user's h_errnop pointer. This h_errno is
647c478bd9Sstevel@tonic-gate  * never used for "switching" -- status from nss_search serves
657c478bd9Sstevel@tonic-gate  * the purpose. There is no explicit zeroing in the case of success.
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate extern struct hostent *
697c478bd9Sstevel@tonic-gate _switch_gethostbyname_r(const char *nam, struct hostent *result, char *buffer,
707c478bd9Sstevel@tonic-gate 	int buflen, int *h_errnop);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate extern struct hostent *
737c478bd9Sstevel@tonic-gate _switch_gethostbyaddr_r(const char *addr, int length, int type,
747c478bd9Sstevel@tonic-gate 	struct hostent *result, char *buffer, int buflen, int *h_errnop);
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #ifdef PIC
777c478bd9Sstevel@tonic-gate struct hostent *
_uncached_gethostbyname_r(const char * nam,struct hostent * result,char * buffer,int buflen,int * h_errnop)787c478bd9Sstevel@tonic-gate _uncached_gethostbyname_r(const char *nam, struct hostent *result,
797c478bd9Sstevel@tonic-gate 	char *buffer, int buflen, int *h_errnop)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	return (_switch_gethostbyname_r(nam, result,
827c478bd9Sstevel@tonic-gate 	buffer, buflen, h_errnop));
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate struct hostent *
_uncached_gethostbyaddr_r(const char * addr,int length,int type,struct hostent * result,char * buffer,int buflen,int * h_errnop)867c478bd9Sstevel@tonic-gate _uncached_gethostbyaddr_r(const char *addr, int length, int type,
877c478bd9Sstevel@tonic-gate 	struct hostent *result, char *buffer, int buflen, int *h_errnop)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	return (_switch_gethostbyaddr_r(addr, length, type,
907c478bd9Sstevel@tonic-gate 					result, buffer, buflen, h_errnop));
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #endif
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate extern struct hostent *
967c478bd9Sstevel@tonic-gate gethostbyname_r(const char *nam, struct hostent *result, char *buffer,
977c478bd9Sstevel@tonic-gate 	int buflen, int *h_errnop);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate extern struct hostent *
1007c478bd9Sstevel@tonic-gate gethostbyaddr_r(const char *addr, int length, int type,
1017c478bd9Sstevel@tonic-gate 	struct hostent *result, char *buffer, int buflen, int *h_errnop);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate struct hostent *
gethostbyname_r(const char * nam,struct hostent * result,char * buffer,int buflen,int * h_errnop)1047c478bd9Sstevel@tonic-gate gethostbyname_r(const char *nam, struct hostent *result, char *buffer,
1057c478bd9Sstevel@tonic-gate 	int buflen, int *h_errnop)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
1087c478bd9Sstevel@tonic-gate 	struct	nss_netdirbyname_in nssin;
1097c478bd9Sstevel@tonic-gate 	union	nss_netdirbyname_out nssout;
1107c478bd9Sstevel@tonic-gate 	int neterr, dummy;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	if (h_errnop == NULL)
1137c478bd9Sstevel@tonic-gate 		h_errnop = &dummy;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	if (strlen(nam) == 0) {
1167c478bd9Sstevel@tonic-gate 		*h_errnop = HOST_NOT_FOUND;
11761961e0fSrobinson 		return (NULL);
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	if ((nconf = __rpc_getconfip("udp")) == NULL &&
1217c478bd9Sstevel@tonic-gate 	    (nconf = __rpc_getconfip("tcp")) == NULL) {
1227c478bd9Sstevel@tonic-gate 		*h_errnop = NO_RECOVERY;
12361961e0fSrobinson 		return (NULL);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	nssin.op_t = NSS_HOST;
1277c478bd9Sstevel@tonic-gate 	nssin.arg.nss.host.name = nam;
1287c478bd9Sstevel@tonic-gate 	nssin.arg.nss.host.buf = buffer;
1297c478bd9Sstevel@tonic-gate 	nssin.arg.nss.host.buflen = buflen;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	nssout.nss.host.hent = result;
1327c478bd9Sstevel@tonic-gate 	nssout.nss.host.herrno_p = h_errnop;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	/*
1357c478bd9Sstevel@tonic-gate 	 * We pass in nconf and let the implementation of the long-named func
1367c478bd9Sstevel@tonic-gate 	 * decide whether to use the switch based on nc_nlookups.
1377c478bd9Sstevel@tonic-gate 	 */
1387c478bd9Sstevel@tonic-gate 	neterr = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	(void) freenetconfigent(nconf);
14161961e0fSrobinson 	if (neterr != ND_OK)
14261961e0fSrobinson 		return (NULL);
1437c478bd9Sstevel@tonic-gate 	return (nssout.nss.host.hent);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate struct hostent *
gethostbyaddr_r(const char * addr,int length,int type,struct hostent * result,char * buffer,int buflen,int * h_errnop)1477c478bd9Sstevel@tonic-gate gethostbyaddr_r(const char *addr, int length, int type,
1487c478bd9Sstevel@tonic-gate 	struct hostent *result, char *buffer, int buflen, int *h_errnop)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
1517c478bd9Sstevel@tonic-gate 	struct	nss_netdirbyaddr_in nssin;
1527c478bd9Sstevel@tonic-gate 	union	nss_netdirbyaddr_out nssout;
153*cb5caa98Sdjl 	int neterr, dummy;
154*cb5caa98Sdjl 
155*cb5caa98Sdjl 	if (h_errnop == NULL)
156*cb5caa98Sdjl 		h_errnop = &dummy;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (type != AF_INET) {
1597c478bd9Sstevel@tonic-gate 		*h_errnop = HOST_NOT_FOUND;
16061961e0fSrobinson 		return (NULL);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if ((nconf = __rpc_getconfip("udp")) == NULL &&
1647c478bd9Sstevel@tonic-gate 	    (nconf = __rpc_getconfip("tcp")) == NULL) {
1657c478bd9Sstevel@tonic-gate 		*h_errnop = NO_RECOVERY;
16661961e0fSrobinson 		return (NULL);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	nssin.op_t = NSS_HOST;
1707c478bd9Sstevel@tonic-gate 	nssin.arg.nss.host.addr = addr;
1717c478bd9Sstevel@tonic-gate 	nssin.arg.nss.host.len = length;
1727c478bd9Sstevel@tonic-gate 	nssin.arg.nss.host.type = type;
1737c478bd9Sstevel@tonic-gate 	nssin.arg.nss.host.buf = buffer;
1747c478bd9Sstevel@tonic-gate 	nssin.arg.nss.host.buflen = buflen;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	nssout.nss.host.hent = result;
1777c478bd9Sstevel@tonic-gate 	nssout.nss.host.herrno_p = h_errnop;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	/*
1807c478bd9Sstevel@tonic-gate 	 * We pass in nconf and let the implementation of this long-named func
1817c478bd9Sstevel@tonic-gate 	 * decide whether to use the switch based on nc_nlookups.
1827c478bd9Sstevel@tonic-gate 	 */
1837c478bd9Sstevel@tonic-gate 	neterr = _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	(void) freenetconfigent(nconf);
18661961e0fSrobinson 	if (neterr != ND_OK)
18761961e0fSrobinson 		return (NULL);
1887c478bd9Sstevel@tonic-gate 	return (nssout.nss.host.hent);
1897c478bd9Sstevel@tonic-gate }
190