Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / lib / nsswitch / dns / common / gethostent.c
blob648ea8ba01d94c8d2b9047f883b6a5f989eb5a71
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * gethostent.c
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,
53 int len, int type);
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 *);
65 typedef union {
66 long al;
67 char ac;
68 } align;
71 * Internet Name Domain Server (DNS) only implementation.
73 static struct hostent *
74 _gethostbyaddr(int *h_errnop, const char *addr, int len, int type)
76 struct hostent *hp;
78 hp = gethostbyaddr(addr, len, type);
79 *h_errnop = *get_h_errno();
80 return (hp);
83 struct hostent *
84 _nss_dns_gethostbyname2(int *h_errnop, const char *name)
86 struct hostent *hp;
88 hp = gethostbyname2(name, AF_INET6);
89 *h_errnop = *get_h_errno();
90 return (hp);
93 struct hostent *
94 _gethostbyname(int *h_errnop, const char *name)
96 struct hostent *hp;
98 hp = gethostbyname(name);
99 *h_errnop = *get_h_errno();
100 return (hp);
103 static void
104 _sethostent(errp, stayopen)
105 nss_status_t *errp;
106 int stayopen;
108 int ret;
110 ret = sethostent(stayopen);
111 if (ret == 0)
112 *errp = NSS_SUCCESS;
113 else
114 *errp = NSS_UNAVAIL;
117 static void
118 _endhostent(errp)
119 nss_status_t *errp;
121 int ret;
123 ret = endhostent();
124 if (ret == 0)
125 *errp = NSS_SUCCESS;
126 else
127 *errp = NSS_UNAVAIL;
131 /*ARGSUSED*/
132 static nss_status_t
133 getbyname(be, a)
134 dns_backend_ptr_t be;
135 void *a;
137 struct hostent *he;
138 nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
139 int ret, mt_disabled;
140 int old_retry;
141 sigset_t oldmask;
143 switch_resolver_setup(&mt_disabled, &oldmask, &old_retry);
145 he = _gethostbyname(&argp->h_errno, argp->key.name);
146 if (he != NULL) {
147 if (argp->buf.result == NULL) {
149 * if asked to return data in string,
150 * convert the hostent structure into
151 * string data
153 ret = ent2str(he, a, AF_INET);
154 if (ret == NSS_STR_PARSE_SUCCESS)
155 argp->returnval = argp->buf.buffer;
156 } else {
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) {
165 argp->erange = 1;
170 switch_resolver_reset(mt_disabled, oldmask, old_retry);
172 return (_herrno2nss(argp->h_errno));
177 /*ARGSUSED*/
178 static nss_status_t
179 getbyaddr(be, a)
180 dns_backend_ptr_t be;
181 void *a;
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.
191 /*ARGSUSED*/
192 nss_status_t
193 __nss_dns_getbyaddr(be, a)
194 dns_backend_ptr_t be;
195 void *a;
197 struct hostent *he;
198 nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
199 int ret, mt_disabled;
200 struct in_addr unmapv4;
201 sigset_t oldmask;
202 int af, addrlen;
203 void *addrp;
204 int old_retry;
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)) {
210 addrp = &unmapv4;
211 addrlen = sizeof (unmapv4);
212 af = AF_INET;
213 (void) memcpy(addrp, &argp->key.hostaddr.addr[12], addrlen);
214 } else {
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);
221 if (he != NULL) {
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);
228 else
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;
234 else
235 argp->returnval = argp->buf.result;
236 } else {
237 argp->h_errno = HOST_NOT_FOUND;
238 if (ret == NSS_STR_PARSE_ERANGE)
239 argp->erange = 1;
243 switch_resolver_reset(mt_disabled, oldmask, old_retry);
245 return (_herrno2nss(argp->h_errno));
249 /*ARGSUSED*/
250 static nss_status_t
251 _nss_dns_getent(be, args)
252 dns_backend_ptr_t be;
253 void *args;
255 return (NSS_UNAVAIL);
259 /*ARGSUSED*/
260 static nss_status_t
261 _nss_dns_setent(be, dummy)
262 dns_backend_ptr_t be;
263 void *dummy;
265 nss_status_t errp;
267 sigset_t oldmask, newmask;
268 int mt_disabled = 1;
271 * Try to enable MT; if not, we have to single-thread libresolv
272 * access
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);
282 if (mt_disabled) {
283 (void) mutex_unlock(&one_lane);
284 (void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
285 } else {
286 (void) (*disable_mt)();
289 return (errp);
293 /*ARGSUSED*/
294 static nss_status_t
295 _nss_dns_endent(be, dummy)
296 dns_backend_ptr_t be;
297 void *dummy;
299 nss_status_t errp;
301 sigset_t oldmask, newmask;
302 int mt_disabled = 1;
305 * Try to enable MT; if not, we have to single-thread libresolv
306 * access
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);
314 _endhostent(&errp);
316 if (mt_disabled) {
317 (void) mutex_unlock(&one_lane);
318 (void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
319 } else {
320 (void) (*disable_mt)();
323 return (errp);
327 /*ARGSUSED*/
328 static nss_status_t
329 _nss_dns_destr(be, dummy)
330 dns_backend_ptr_t be;
331 void *dummy;
333 nss_status_t errp;
335 if (be != 0) {
336 /* === Should change to invoke ops[ENDENT] ? */
337 sigset_t oldmask, newmask;
338 int mt_disabled = 1;
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);
346 _endhostent(&errp);
348 if (mt_disabled) {
349 (void) mutex_unlock(&one_lane);
350 (void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
351 } else {
352 (void) (*disable_mt)();
355 free(be);
357 return (NSS_SUCCESS); /* In case anyone is dumb enough to check */
361 static dns_backend_op_t host_ops[] = {
362 _nss_dns_destr,
363 _nss_dns_endent,
364 _nss_dns_setent,
365 _nss_dns_getent,
366 getbyname,
367 getbyaddr,
370 /*ARGSUSED*/
371 nss_backend_t *
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
381 * entry point.
383 * Returns:
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
391 /*ARGSUSED*/
392 nss_status_t
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));