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