4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
32 * In order to avoid duplicating libresolv code here, and since libresolv.so.2
33 * provides res_-equivalents of the getXbyY and {set,get}Xent, lets call
34 * re_gethostbyaddr() and so on from this file. Among other things, this
35 * should help us avoid problems like the one described in bug 1264386,
36 * where the internal getanswer() acquired new functionality in BIND 4.9.3,
37 * but the local copy of getanswer() in this file wasn't updated, so that new
38 * functionality wasn't available to the name service switch.
41 #define gethostbyaddr res_gethostbyaddr
42 #define gethostbyname res_gethostbyname
43 #define gethostbyname2 res_gethostbyname2
44 #define sethostent res_sethostent
45 #define endhostent res_endhostent
47 #include "dns_common.h"
49 extern char *inet_ntoa(struct in_addr in
);
51 struct hostent
*_gethostbyname(int *h_errnop
, const char *name
);
52 static struct hostent
*_gethostbyaddr(int *h_errnop
, const char *addr
,
54 struct hostent
*_nss_dns_gethostbyname2(int *h_errnop
, const char *name
);
56 #pragma weak res_gethostbyname
57 #pragma weak res_gethostbyname2
58 #pragma weak res_gethostbyaddr
59 #pragma weak res_sethostent
60 #pragma weak res_endhostent
62 nss_backend_t
*_nss_dns_constr(dns_backend_op_t ops
[], int n_ops
);
63 nss_status_t
__nss_dns_getbyaddr(dns_backend_ptr_t
, void *);
71 * Internet Name Domain Server (DNS) only implementation.
73 static struct hostent
*
74 _gethostbyaddr(int *h_errnop
, const char *addr
, int len
, int type
)
78 hp
= gethostbyaddr(addr
, len
, type
);
79 *h_errnop
= *get_h_errno();
84 _nss_dns_gethostbyname2(int *h_errnop
, const char *name
)
88 hp
= gethostbyname2(name
, AF_INET6
);
89 *h_errnop
= *get_h_errno();
94 _gethostbyname(int *h_errnop
, const char *name
)
98 hp
= gethostbyname(name
);
99 *h_errnop
= *get_h_errno();
104 _sethostent(errp
, stayopen
)
110 ret
= sethostent(stayopen
);
134 dns_backend_ptr_t be
;
138 nss_XbyY_args_t
*argp
= (nss_XbyY_args_t
*)a
;
139 int ret
, mt_disabled
;
143 switch_resolver_setup(&mt_disabled
, &oldmask
, &old_retry
);
145 he
= _gethostbyname(&argp
->h_errno
, argp
->key
.name
);
147 if (argp
->buf
.result
== NULL
) {
149 * if asked to return data in string,
150 * convert the hostent structure into
153 ret
= ent2str(he
, a
, AF_INET
);
154 if (ret
== NSS_STR_PARSE_SUCCESS
)
155 argp
->returnval
= argp
->buf
.buffer
;
157 ret
= ent2result(he
, a
, AF_INET
);
158 if (ret
== NSS_STR_PARSE_SUCCESS
)
159 argp
->returnval
= argp
->buf
.result
;
162 if (ret
!= NSS_STR_PARSE_SUCCESS
) {
163 argp
->h_errno
= HOST_NOT_FOUND
;
164 if (ret
== NSS_STR_PARSE_ERANGE
) {
170 switch_resolver_reset(mt_disabled
, oldmask
, old_retry
);
172 return (_herrno2nss(argp
->h_errno
));
180 dns_backend_ptr_t be
;
183 return (__nss_dns_getbyaddr(be
, a
));
188 * Exposing a DNS backend specific interface so that it doesn't conflict
189 * with other getbyaddr() routines from other switch backends.
193 __nss_dns_getbyaddr(be
, a
)
194 dns_backend_ptr_t be
;
198 nss_XbyY_args_t
*argp
= (nss_XbyY_args_t
*)a
;
199 int ret
, mt_disabled
;
200 struct in_addr unmapv4
;
206 switch_resolver_setup(&mt_disabled
, &oldmask
, &old_retry
);
208 /* LINTED: E_BAD_PTR_CAST_ALIGN */
209 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr
*)argp
->key
.hostaddr
.addr
)) {
211 addrlen
= sizeof (unmapv4
);
213 (void) memcpy(addrp
, &argp
->key
.hostaddr
.addr
[12], addrlen
);
215 addrp
= (void *)argp
->key
.hostaddr
.addr
;
216 addrlen
= argp
->key
.hostaddr
.len
;
217 af
= argp
->key
.hostaddr
.type
;
219 he
= _gethostbyaddr(&argp
->h_errno
, addrp
, addrlen
, af
);
223 * if asked to return data in string, convert
224 * the hostent structure into string data
226 if (argp
->buf
.result
== NULL
)
227 ret
= ent2str(he
, a
, argp
->key
.hostaddr
.type
);
229 ret
= ent2result(he
, a
, argp
->key
.hostaddr
.type
);
231 if (ret
== NSS_STR_PARSE_SUCCESS
) {
232 if (argp
->buf
.result
== NULL
)
233 argp
->returnval
= argp
->buf
.buffer
;
235 argp
->returnval
= argp
->buf
.result
;
237 argp
->h_errno
= HOST_NOT_FOUND
;
238 if (ret
== NSS_STR_PARSE_ERANGE
)
243 switch_resolver_reset(mt_disabled
, oldmask
, old_retry
);
245 return (_herrno2nss(argp
->h_errno
));
251 _nss_dns_getent(be
, args
)
252 dns_backend_ptr_t be
;
255 return (NSS_UNAVAIL
);
261 _nss_dns_setent(be
, dummy
)
262 dns_backend_ptr_t be
;
267 sigset_t oldmask
, newmask
;
271 * Try to enable MT; if not, we have to single-thread libresolv
274 if (enable_mt
== 0 || (mt_disabled
= (*enable_mt
)()) != 0) {
275 (void) sigfillset(&newmask
);
276 (void) thr_sigsetmask(SIG_SETMASK
, &newmask
, &oldmask
);
277 (void) mutex_lock(&one_lane
);
280 _sethostent(&errp
, 1);
283 (void) mutex_unlock(&one_lane
);
284 (void) thr_sigsetmask(SIG_SETMASK
, &oldmask
, NULL
);
286 (void) (*disable_mt
)();
295 _nss_dns_endent(be
, dummy
)
296 dns_backend_ptr_t be
;
301 sigset_t oldmask
, newmask
;
305 * Try to enable MT; if not, we have to single-thread libresolv
308 if (enable_mt
== 0 || (mt_disabled
= (*enable_mt
)()) != 0) {
309 (void) sigfillset(&newmask
);
310 (void) thr_sigsetmask(SIG_SETMASK
, &newmask
, &oldmask
);
311 (void) mutex_lock(&one_lane
);
317 (void) mutex_unlock(&one_lane
);
318 (void) thr_sigsetmask(SIG_SETMASK
, &oldmask
, NULL
);
320 (void) (*disable_mt
)();
329 _nss_dns_destr(be
, dummy
)
330 dns_backend_ptr_t be
;
336 /* === Should change to invoke ops[ENDENT] ? */
337 sigset_t oldmask
, newmask
;
340 if (enable_mt
== 0 || (mt_disabled
= (*enable_mt
)()) != 0) {
341 (void) sigfillset(&newmask
);
342 (void) thr_sigsetmask(SIG_SETMASK
, &newmask
, &oldmask
);
343 (void) mutex_lock(&one_lane
);
349 (void) mutex_unlock(&one_lane
);
350 (void) thr_sigsetmask(SIG_SETMASK
, &oldmask
, NULL
);
352 (void) (*disable_mt
)();
357 return (NSS_SUCCESS
); /* In case anyone is dumb enough to check */
361 static dns_backend_op_t host_ops
[] = {
372 _nss_dns_hosts_constr(dummy1
, dummy2
, dummy3
)
373 const char *dummy1
, *dummy2
, *dummy3
;
375 return (_nss_dns_constr(host_ops
,
376 sizeof (host_ops
) / sizeof (host_ops
[0])));
380 * optional NSS2 packed backend gethostsbyname with ttl
384 * NSS_SUCCESS - successful
385 * NSS_NOTFOUND - successful but nothing found
386 * NSS_ERROR - fallback to NSS backend lookup mode
387 * If successful, buffer will be filled with valid data
393 _nss_get_dns_hosts_name(dns_backend_ptr_t
*be
, void **bufp
, size_t *sizep
)
395 return (_nss_dns_gethost_withttl(*bufp
, *sizep
, 0));