1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * lib/libnsl/nss/gethostbyname_r.c
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  * gethostbyname_r() is defined in this file.  It is implemented on top of
33*0Sstevel@tonic-gate  *   _get_hostserv_inetnetdir_byname() which is also used to implement
34*0Sstevel@tonic-gate  *   netdir_getbyname() for inet family transports.  In turn the common code
35*0Sstevel@tonic-gate  *   uses the name service switch policy for "hosts" and "services" unless
36*0Sstevel@tonic-gate  *   the administrator chooses to bypass the name service switch by
37*0Sstevel@tonic-gate  *   specifying third-party supplied nametoaddr libs for inet transports
38*0Sstevel@tonic-gate  *   in /etc/netconfig.
39*0Sstevel@tonic-gate  *
40*0Sstevel@tonic-gate  * gethostbyaddr_r() is similarly related to _get_hostserv_inetnetdir_byaddr()
41*0Sstevel@tonic-gate  *   and netdir_getbyaddr();
42*0Sstevel@tonic-gate  *
43*0Sstevel@tonic-gate  * The common code lives in netdir_inet.c.
44*0Sstevel@tonic-gate  *
45*0Sstevel@tonic-gate  * gethostent_r(), sethostent() and endhostent() are *not* implemented on top
46*0Sstevel@tonic-gate  *   of the common interface;  they go straight to the switch and are
47*0Sstevel@tonic-gate  *   defined in gethostent_r.c.
48*0Sstevel@tonic-gate  *
49*0Sstevel@tonic-gate  * There is absolutely no data sharing, not even the stayopen flag or
50*0Sstevel@tonic-gate  *   enumeration state, between gethostbyYY_r() and gethostent_r();
51*0Sstevel@tonic-gate  */
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate #include <netdb.h>
54*0Sstevel@tonic-gate #include <rpc/trace.h>
55*0Sstevel@tonic-gate #include <netdir.h>
56*0Sstevel@tonic-gate #include <sys/types.h>
57*0Sstevel@tonic-gate #include <nss_netdir.h>
58*0Sstevel@tonic-gate #include <string.h>
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate extern struct netconfig *__rpc_getconfip();
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate /*
63*0Sstevel@tonic-gate  * h_errno POLICY: The frontends expect the name service
64*0Sstevel@tonic-gate  * backends to modify the h_errno in "arg"; _switch_gethostbyYY_r()
65*0Sstevel@tonic-gate  * will copy that over onto user's h_errnop pointer. This h_errno is
66*0Sstevel@tonic-gate  * never used for "switching" -- status from nss_search serves
67*0Sstevel@tonic-gate  * the purpose. There is no explicit zeroing in the case of success.
68*0Sstevel@tonic-gate  */
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate extern struct hostent *
71*0Sstevel@tonic-gate _switch_gethostbyname_r(const char *nam, struct hostent *result, char *buffer,
72*0Sstevel@tonic-gate 	int buflen, int *h_errnop);
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate extern struct hostent *
75*0Sstevel@tonic-gate _switch_gethostbyaddr_r(const char *addr, int length, int type,
76*0Sstevel@tonic-gate 	struct hostent *result, char *buffer, int buflen, int *h_errnop);
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate #ifdef PIC
79*0Sstevel@tonic-gate struct hostent *
80*0Sstevel@tonic-gate _uncached_gethostbyname_r(const char *nam, struct hostent *result,
81*0Sstevel@tonic-gate 	char *buffer, int buflen, int *h_errnop)
82*0Sstevel@tonic-gate {
83*0Sstevel@tonic-gate 	return (_switch_gethostbyname_r(nam, result,
84*0Sstevel@tonic-gate 	buffer, buflen, h_errnop));
85*0Sstevel@tonic-gate }
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate struct hostent *
88*0Sstevel@tonic-gate _uncached_gethostbyaddr_r(const char *addr, int length, int type,
89*0Sstevel@tonic-gate 	struct hostent *result, char *buffer, int buflen, int *h_errnop)
90*0Sstevel@tonic-gate {
91*0Sstevel@tonic-gate 	return (_switch_gethostbyaddr_r(addr, length, type,
92*0Sstevel@tonic-gate 					result, buffer, buflen, h_errnop));
93*0Sstevel@tonic-gate }
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate #endif
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate extern struct hostent *
98*0Sstevel@tonic-gate gethostbyname_r(const char *nam, struct hostent *result, char *buffer,
99*0Sstevel@tonic-gate 	int buflen, int *h_errnop);
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate extern struct hostent *
102*0Sstevel@tonic-gate gethostbyaddr_r(const char *addr, int length, int type,
103*0Sstevel@tonic-gate 	struct hostent *result, char *buffer, int buflen, int *h_errnop);
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate struct hostent *
106*0Sstevel@tonic-gate gethostbyname_r(const char *nam, struct hostent *result, char *buffer,
107*0Sstevel@tonic-gate 	int buflen, int *h_errnop)
108*0Sstevel@tonic-gate {
109*0Sstevel@tonic-gate 	struct netconfig *nconf;
110*0Sstevel@tonic-gate 	struct	nss_netdirbyname_in nssin;
111*0Sstevel@tonic-gate 	union	nss_netdirbyname_out nssout;
112*0Sstevel@tonic-gate 	int neterr, dummy;
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	trace2(TR_gethostbyname_r, 0, buflen);
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	if (h_errnop == NULL)
117*0Sstevel@tonic-gate 		h_errnop = &dummy;
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	if (strlen(nam) == 0) {
120*0Sstevel@tonic-gate 		*h_errnop = HOST_NOT_FOUND;
121*0Sstevel@tonic-gate 		trace2(TR_gethostbyname_r, 1, buflen);
122*0Sstevel@tonic-gate 		return ((struct hostent *)NULL);
123*0Sstevel@tonic-gate 	}
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	if ((nconf = __rpc_getconfip("udp")) == NULL &&
126*0Sstevel@tonic-gate 	    (nconf = __rpc_getconfip("tcp")) == NULL) {
127*0Sstevel@tonic-gate 		*h_errnop = NO_RECOVERY;
128*0Sstevel@tonic-gate 		trace2(TR_gethostbyname_r, 1, buflen);
129*0Sstevel@tonic-gate 		return ((struct hostent *)NULL);
130*0Sstevel@tonic-gate 	}
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	nssin.op_t = NSS_HOST;
133*0Sstevel@tonic-gate 	nssin.arg.nss.host.name = nam;
134*0Sstevel@tonic-gate 	nssin.arg.nss.host.buf = buffer;
135*0Sstevel@tonic-gate 	nssin.arg.nss.host.buflen = buflen;
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	nssout.nss.host.hent = result;
138*0Sstevel@tonic-gate 	nssout.nss.host.herrno_p = h_errnop;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	/*
141*0Sstevel@tonic-gate 	 * We pass in nconf and let the implementation of the long-named func
142*0Sstevel@tonic-gate 	 * decide whether to use the switch based on nc_nlookups.
143*0Sstevel@tonic-gate 	 */
144*0Sstevel@tonic-gate 	neterr = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout);
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	(void) freenetconfigent(nconf);
147*0Sstevel@tonic-gate 	if (neterr != ND_OK) {
148*0Sstevel@tonic-gate 		trace2(TR_gethostbyname_r, 1, buflen);
149*0Sstevel@tonic-gate 		return ((struct hostent *)NULL);
150*0Sstevel@tonic-gate 	}
151*0Sstevel@tonic-gate 	trace2(TR_gethostbyname_r, 1, buflen);
152*0Sstevel@tonic-gate 	return (nssout.nss.host.hent);
153*0Sstevel@tonic-gate }
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate struct hostent *
156*0Sstevel@tonic-gate gethostbyaddr_r(const char *addr, int length, int type,
157*0Sstevel@tonic-gate 	struct hostent *result, char *buffer, int buflen, int *h_errnop)
158*0Sstevel@tonic-gate {
159*0Sstevel@tonic-gate 	struct netconfig *nconf;
160*0Sstevel@tonic-gate 	struct	nss_netdirbyaddr_in nssin;
161*0Sstevel@tonic-gate 	union	nss_netdirbyaddr_out nssout;
162*0Sstevel@tonic-gate 	int neterr;
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	trace3(TR_gethostbyaddr_r, 0, length, buflen);
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	if (type != AF_INET) {
167*0Sstevel@tonic-gate 		*h_errnop = HOST_NOT_FOUND;
168*0Sstevel@tonic-gate 		return ((struct hostent *)NULL);
169*0Sstevel@tonic-gate 	}
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	if ((nconf = __rpc_getconfip("udp")) == NULL &&
172*0Sstevel@tonic-gate 	    (nconf = __rpc_getconfip("tcp")) == NULL) {
173*0Sstevel@tonic-gate 		*h_errnop = NO_RECOVERY;
174*0Sstevel@tonic-gate 		trace3(TR_gethostbyaddr_r, 0, length, buflen);
175*0Sstevel@tonic-gate 		return ((struct hostent *)NULL);
176*0Sstevel@tonic-gate 	}
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	nssin.op_t = NSS_HOST;
179*0Sstevel@tonic-gate 	nssin.arg.nss.host.addr = addr;
180*0Sstevel@tonic-gate 	nssin.arg.nss.host.len = length;
181*0Sstevel@tonic-gate 	nssin.arg.nss.host.type = type;
182*0Sstevel@tonic-gate 	nssin.arg.nss.host.buf = buffer;
183*0Sstevel@tonic-gate 	nssin.arg.nss.host.buflen = buflen;
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	nssout.nss.host.hent = result;
186*0Sstevel@tonic-gate 	nssout.nss.host.herrno_p = h_errnop;
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	/*
189*0Sstevel@tonic-gate 	 * We pass in nconf and let the implementation of this long-named func
190*0Sstevel@tonic-gate 	 * decide whether to use the switch based on nc_nlookups.
191*0Sstevel@tonic-gate 	 */
192*0Sstevel@tonic-gate 	neterr = _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout);
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	(void) freenetconfigent(nconf);
195*0Sstevel@tonic-gate 	if (neterr != ND_OK) {
196*0Sstevel@tonic-gate 		trace3(TR_gethostbyaddr_r, 0, length, buflen);
197*0Sstevel@tonic-gate 		return ((struct hostent *)NULL);
198*0Sstevel@tonic-gate 	}
199*0Sstevel@tonic-gate 	return (nssout.nss.host.hent);
200*0Sstevel@tonic-gate }
201