]>
Commit | Line | Data |
---|---|---|
4535680d | 1 | /* Copyright (C) 1996-2004, 2007-2009, 2010 Free Software Foundation, Inc. |
84384f5b UD |
2 | This file is part of the GNU C Library. |
3 | Extended from original form by Ulrich Drepper <drepper@cygnus.com>, 1996. | |
5f0e6fc7 | 4 | |
84384f5b | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
5f0e6fc7 | 9 | |
84384f5b UD |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
5f0e6fc7 | 14 | |
41bdb6e2 AJ |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, write to the Free | |
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
18 | 02111-1307 USA. */ | |
5f0e6fc7 | 19 | |
df21c858 | 20 | /* Parts of this file are plain copies of the file `gethtnamadr.c' from |
5f0e6fc7 RM |
21 | the bind package and it has the following copyright. */ |
22 | ||
5f0e6fc7 | 23 | /* |
df21c858 UD |
24 | * ++Copyright++ 1985, 1988, 1993 |
25 | * - | |
26 | * Copyright (c) 1985, 1988, 1993 | |
27 | * The Regents of the University of California. All rights reserved. | |
5f0e6fc7 RM |
28 | * |
29 | * Redistribution and use in source and binary forms, with or without | |
30 | * modification, are permitted provided that the following conditions | |
31 | * are met: | |
32 | * 1. Redistributions of source code must retain the above copyright | |
33 | * notice, this list of conditions and the following disclaimer. | |
34 | * 2. Redistributions in binary form must reproduce the above copyright | |
35 | * notice, this list of conditions and the following disclaimer in the | |
36 | * documentation and/or other materials provided with the distribution. | |
5f0e6fc7 RM |
37 | * 4. Neither the name of the University nor the names of its contributors |
38 | * may be used to endorse or promote products derived from this software | |
39 | * without specific prior written permission. | |
40 | * | |
41 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
51 | * SUCH DAMAGE. | |
df21c858 UD |
52 | * - |
53 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | |
54 | * | |
55 | * Permission to use, copy, modify, and distribute this software for any | |
56 | * purpose with or without fee is hereby granted, provided that the above | |
57 | * copyright notice and this permission notice appear in all copies, and that | |
58 | * the name of Digital Equipment Corporation not be used in advertising or | |
59 | * publicity pertaining to distribution of the document or software without | |
60 | * specific, written prior permission. | |
61 | * | |
62 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | |
63 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | |
64 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | |
65 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | |
66 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | |
67 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |
68 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
69 | * SOFTWARE. | |
70 | * - | |
71 | * --Copyright-- | |
5f0e6fc7 RM |
72 | */ |
73 | ||
1eb946b9 | 74 | #include <assert.h> |
5f0e6fc7 RM |
75 | #include <ctype.h> |
76 | #include <errno.h> | |
77 | #include <netdb.h> | |
78 | #include <stdio.h> | |
79 | #include <stdlib.h> | |
80 | #include <stddef.h> | |
81 | #include <string.h> | |
82 | #include <sys/syslog.h> | |
83 | ||
5f0e6fc7 RM |
84 | #include "nsswitch.h" |
85 | ||
86 | /* Get implementation for some internal functions. */ | |
da2d1bc5 UD |
87 | #include <resolv/mapv4v6addr.h> |
88 | #include <resolv/mapv4v6hostent.h> | |
5f0e6fc7 | 89 | |
5db91571 UD |
90 | #define RESOLVSORT |
91 | ||
5f0e6fc7 RM |
92 | /* Maximum number of aliases we allow. */ |
93 | #define MAX_NR_ALIASES 48 | |
94 | #define MAX_NR_ADDRS 48 | |
95 | ||
e2dced82 | 96 | #if PACKETSZ > 65536 |
5f0e6fc7 RM |
97 | # define MAXPACKET PACKETSZ |
98 | #else | |
e2dced82 | 99 | # define MAXPACKET 65536 |
5f0e6fc7 | 100 | #endif |
1522c368 UD |
101 | /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */ |
102 | #ifdef MAXHOSTNAMELEN | |
103 | # undef MAXHOSTNAMELEN | |
104 | #endif | |
105 | #define MAXHOSTNAMELEN 256 | |
5f0e6fc7 RM |
106 | |
107 | static const char AskedForGot[] = "\ | |
108 | gethostby*.getanswer: asked for \"%s\", got \"%s\""; | |
109 | ||
110 | ||
111 | /* We need this time later. */ | |
112 | typedef union querybuf | |
113 | { | |
114 | HEADER hdr; | |
115 | u_char buf[MAXPACKET]; | |
116 | } querybuf; | |
117 | ||
8d8c6efa UD |
118 | /* These functions are defined in res_comp.c. */ |
119 | #define NS_MAXCDNAME 255 /* maximum compressed domain name */ | |
b455972f UD |
120 | extern int __ns_name_ntop (const u_char *, char *, size_t); |
121 | extern int __ns_name_unpack (const u_char *, const u_char *, | |
122 | const u_char *, u_char *, size_t); | |
8d8c6efa | 123 | |
5f0e6fc7 RM |
124 | |
125 | static enum nss_status getanswer_r (const querybuf *answer, int anslen, | |
126 | const char *qname, int qtype, | |
df4ef2ab | 127 | struct hostent *result, char *buffer, |
b455972f | 128 | size_t buflen, int *errnop, int *h_errnop, |
d1fe1f22 | 129 | int map, int32_t *ttlp, char **canonp); |
5f0e6fc7 | 130 | |
1eb946b9 UD |
131 | static enum nss_status gaih_getanswer (const querybuf *answer1, int anslen1, |
132 | const querybuf *answer2, int anslen2, | |
133 | const char *qname, | |
134 | struct gaih_addrtuple **pat, | |
135 | char *buffer, size_t buflen, | |
136 | int *errnop, int *h_errnop, | |
137 | int32_t *ttlp); | |
138 | ||
78d8d211 UD |
139 | extern enum nss_status _nss_dns_gethostbyname3_r (const char *name, int af, |
140 | struct hostent *result, | |
141 | char *buffer, size_t buflen, | |
142 | int *errnop, int *h_errnop, | |
143 | int32_t *ttlp, | |
144 | char **canonp); | |
145 | hidden_proto (_nss_dns_gethostbyname3_r) | |
146 | ||
5f0e6fc7 | 147 | enum nss_status |
d1fe1f22 | 148 | _nss_dns_gethostbyname3_r (const char *name, int af, struct hostent *result, |
67479a70 | 149 | char *buffer, size_t buflen, int *errnop, |
d1fe1f22 | 150 | int *h_errnop, int32_t *ttlp, char **canonp) |
5f0e6fc7 | 151 | { |
16b66062 AJ |
152 | union |
153 | { | |
154 | querybuf *buf; | |
155 | u_char *ptr; | |
156 | } host_buffer; | |
157 | querybuf *orig_host_buffer; | |
f80f1a4a | 158 | char tmp[NS_MAXDNAME]; |
5f0e6fc7 RM |
159 | int size, type, n; |
160 | const char *cp; | |
0420d888 | 161 | int map = 0; |
34816665 | 162 | int olderr = errno; |
6166815d | 163 | enum nss_status status; |
5f0e6fc7 | 164 | |
87bb6b6c | 165 | if (__res_maybe_init (&_res, 0) == -1) |
b43b13ac UD |
166 | return NSS_STATUS_UNAVAIL; |
167 | ||
5f0e6fc7 RM |
168 | switch (af) { |
169 | case AF_INET: | |
170 | size = INADDRSZ; | |
171 | type = T_A; | |
172 | break; | |
173 | case AF_INET6: | |
174 | size = IN6ADDRSZ; | |
175 | type = T_AAAA; | |
176 | break; | |
177 | default: | |
0cc70fcf | 178 | *h_errnop = NO_DATA; |
67479a70 | 179 | *errnop = EAFNOSUPPORT; |
5f0e6fc7 RM |
180 | return NSS_STATUS_UNAVAIL; |
181 | } | |
182 | ||
183 | result->h_addrtype = af; | |
184 | result->h_length = size; | |
185 | ||
186 | /* | |
187 | * if there aren't any dots, it could be a user-level alias. | |
188 | * this is also done in res_query() since we are not the only | |
189 | * function that looks up host names. | |
190 | */ | |
f80f1a4a UD |
191 | if (strchr (name, '.') == NULL |
192 | && (cp = res_hostalias (&_res, name, tmp, sizeof (tmp))) != NULL) | |
5f0e6fc7 RM |
193 | name = cp; |
194 | ||
16b66062 | 195 | host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024); |
6166815d | 196 | |
16b66062 | 197 | n = __libc_res_nsearch (&_res, name, C_IN, type, host_buffer.buf->buf, |
b7da31a1 | 198 | 1024, &host_buffer.ptr, NULL, NULL, NULL); |
5f0e6fc7 | 199 | if (n < 0) |
dd7d45e8 | 200 | { |
4535680d | 201 | switch (errno) |
da5ac135 | 202 | { |
4535680d | 203 | case ESRCH: |
da5ac135 UD |
204 | status = NSS_STATUS_TRYAGAIN; |
205 | h_errno = TRY_AGAIN; | |
4535680d UD |
206 | break; |
207 | case ECONNREFUSED: | |
208 | case ETIMEDOUT: | |
209 | status = NSS_STATUS_UNAVAIL; | |
210 | break; | |
211 | default: | |
212 | status = NSS_STATUS_NOTFOUND; | |
213 | break; | |
da5ac135 | 214 | } |
dd7d45e8 | 215 | *h_errnop = h_errno; |
34816665 UD |
216 | if (h_errno == TRY_AGAIN) |
217 | *errnop = EAGAIN; | |
218 | else | |
219 | __set_errno (olderr); | |
b455972f | 220 | |
4535680d | 221 | /* If we are looking for an IPv6 address and mapping is enabled |
b455972f UD |
222 | by having the RES_USE_INET6 bit in _res.options set, we try |
223 | another lookup. */ | |
224 | if (af == AF_INET6 && (_res.options & RES_USE_INET6)) | |
16b66062 AJ |
225 | n = __libc_res_nsearch (&_res, name, C_IN, T_A, host_buffer.buf->buf, |
226 | host_buffer.buf != orig_host_buffer | |
1eb946b9 | 227 | ? MAXPACKET : 1024, &host_buffer.ptr, |
b7da31a1 | 228 | NULL, NULL, NULL); |
b455972f UD |
229 | |
230 | if (n < 0) | |
6166815d | 231 | { |
16b66062 AJ |
232 | if (host_buffer.buf != orig_host_buffer) |
233 | free (host_buffer.buf); | |
6166815d UD |
234 | return status; |
235 | } | |
b455972f UD |
236 | |
237 | map = 1; | |
238 | ||
239 | result->h_addrtype = AF_INET; | |
2884dad4 | 240 | result->h_length = INADDRSZ; |
dd7d45e8 | 241 | } |
5f0e6fc7 | 242 | |
16b66062 | 243 | status = getanswer_r (host_buffer.buf, n, name, type, result, buffer, buflen, |
d1fe1f22 | 244 | errnop, h_errnop, map, ttlp, canonp); |
16b66062 AJ |
245 | if (host_buffer.buf != orig_host_buffer) |
246 | free (host_buffer.buf); | |
6166815d | 247 | return status; |
5f0e6fc7 | 248 | } |
78d8d211 | 249 | hidden_def (_nss_dns_gethostbyname3_r) |
5f0e6fc7 RM |
250 | |
251 | ||
d1fe1f22 UD |
252 | enum nss_status |
253 | _nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result, | |
254 | char *buffer, size_t buflen, int *errnop, | |
255 | int *h_errnop) | |
256 | { | |
257 | return _nss_dns_gethostbyname3_r (name, af, result, buffer, buflen, errnop, | |
258 | h_errnop, NULL, NULL); | |
259 | } | |
260 | ||
261 | ||
5f0e6fc7 RM |
262 | enum nss_status |
263 | _nss_dns_gethostbyname_r (const char *name, struct hostent *result, | |
67479a70 UD |
264 | char *buffer, size_t buflen, int *errnop, |
265 | int *h_errnop) | |
5f0e6fc7 RM |
266 | { |
267 | enum nss_status status = NSS_STATUS_NOTFOUND; | |
268 | ||
269 | if (_res.options & RES_USE_INET6) | |
78d8d211 UD |
270 | status = _nss_dns_gethostbyname3_r (name, AF_INET6, result, buffer, |
271 | buflen, errnop, h_errnop, NULL, NULL); | |
5f0e6fc7 | 272 | if (status == NSS_STATUS_NOTFOUND) |
78d8d211 UD |
273 | status = _nss_dns_gethostbyname3_r (name, AF_INET, result, buffer, |
274 | buflen, errnop, h_errnop, NULL, NULL); | |
5f0e6fc7 RM |
275 | |
276 | return status; | |
277 | } | |
278 | ||
279 | ||
1eb946b9 UD |
280 | enum nss_status |
281 | _nss_dns_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat, | |
282 | char *buffer, size_t buflen, int *errnop, | |
283 | int *herrnop, int32_t *ttlp) | |
284 | { | |
285 | if (__res_maybe_init (&_res, 0) == -1) | |
286 | return NSS_STATUS_UNAVAIL; | |
287 | ||
1eb946b9 UD |
288 | /* |
289 | * if there aren't any dots, it could be a user-level alias. | |
290 | * this is also done in res_query() since we are not the only | |
291 | * function that looks up host names. | |
292 | */ | |
293 | if (strchr (name, '.') == NULL) | |
294 | { | |
b7da31a1 UD |
295 | char *tmp = alloca (NS_MAXDNAME); |
296 | const char *cp = res_hostalias (&_res, name, tmp, NS_MAXDNAME); | |
1eb946b9 UD |
297 | if (cp != NULL) |
298 | name = cp; | |
299 | } | |
300 | ||
301 | union | |
302 | { | |
303 | querybuf *buf; | |
304 | u_char *ptr; | |
305 | } host_buffer; | |
306 | querybuf *orig_host_buffer; | |
307 | host_buffer.buf = orig_host_buffer = (querybuf *) alloca (2048); | |
308 | u_char *ans2p = NULL; | |
309 | int nans2p = 0; | |
b7da31a1 | 310 | int resplen2 = 0; |
1eb946b9 UD |
311 | |
312 | int olderr = errno; | |
313 | enum nss_status status; | |
314 | int n = __libc_res_nsearch (&_res, name, C_IN, T_UNSPEC, | |
315 | host_buffer.buf->buf, 2048, &host_buffer.ptr, | |
b7da31a1 | 316 | &ans2p, &nans2p, &resplen2); |
1eb946b9 UD |
317 | if (n < 0) |
318 | { | |
da5ac135 UD |
319 | if (errno == ESRCH) |
320 | { | |
321 | status = NSS_STATUS_TRYAGAIN; | |
322 | h_errno = TRY_AGAIN; | |
323 | } | |
324 | else | |
325 | status = (errno == ECONNREFUSED | |
326 | ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND); | |
1eb946b9 UD |
327 | *herrnop = h_errno; |
328 | if (h_errno == TRY_AGAIN) | |
329 | *errnop = EAGAIN; | |
330 | else | |
331 | __set_errno (olderr); | |
332 | ||
333 | if (host_buffer.buf != orig_host_buffer) | |
334 | free (host_buffer.buf); | |
335 | ||
336 | return status; | |
337 | } | |
338 | ||
339 | status = gaih_getanswer(host_buffer.buf, n, (const querybuf *) ans2p, | |
b7da31a1 | 340 | resplen2, name, pat, buffer, buflen, |
1eb946b9 UD |
341 | errnop, herrnop, ttlp); |
342 | ||
343 | if (host_buffer.buf != orig_host_buffer) | |
344 | free (host_buffer.buf); | |
345 | ||
346 | return status; | |
347 | } | |
348 | ||
349 | ||
31e2791c UD |
350 | extern enum nss_status _nss_dns_gethostbyaddr2_r (const void *addr, |
351 | socklen_t len, int af, | |
352 | struct hostent *result, | |
353 | char *buffer, size_t buflen, | |
354 | int *errnop, int *h_errnop, | |
355 | int32_t *ttlp); | |
356 | hidden_proto (_nss_dns_gethostbyaddr2_r) | |
357 | ||
5f0e6fc7 | 358 | enum nss_status |
31e2791c UD |
359 | _nss_dns_gethostbyaddr2_r (const void *addr, socklen_t len, int af, |
360 | struct hostent *result, char *buffer, size_t buflen, | |
361 | int *errnop, int *h_errnop, int32_t *ttlp) | |
5f0e6fc7 RM |
362 | { |
363 | static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff }; | |
364 | static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 }; | |
20cc4c87 | 365 | static const u_char v6local[] = { 0,0, 0,1 }; |
5f0e6fc7 RM |
366 | const u_char *uaddr = (const u_char *)addr; |
367 | struct host_data | |
368 | { | |
369 | char *aliases[MAX_NR_ALIASES]; | |
370 | unsigned char host_addr[16]; /* IPv4 or IPv6 */ | |
371 | char *h_addr_ptrs[MAX_NR_ADDRS + 1]; | |
372 | char linebuffer[0]; | |
373 | } *host_data = (struct host_data *) buffer; | |
16b66062 AJ |
374 | union |
375 | { | |
376 | querybuf *buf; | |
377 | u_char *ptr; | |
378 | } host_buffer; | |
379 | querybuf *orig_host_buffer; | |
1f3f143e | 380 | char qbuf[MAXDNAME+1], *qp = NULL; |
30f22ab1 | 381 | size_t size; |
0420d888 | 382 | int n, status; |
34816665 | 383 | int olderr = errno; |
5f0e6fc7 | 384 | |
df9293cb UD |
385 | uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct host_data); |
386 | buffer += pad; | |
387 | buflen = buflen > pad ? buflen - pad : 0; | |
388 | ||
389 | if (__builtin_expect (buflen < sizeof (struct host_data), 0)) | |
390 | { | |
391 | *errnop = ERANGE; | |
392 | *h_errnop = NETDB_INTERNAL; | |
393 | return NSS_STATUS_TRYAGAIN; | |
394 | } | |
395 | ||
396 | host_data = (struct host_data *) buffer; | |
397 | ||
87bb6b6c | 398 | if (__res_maybe_init (&_res, 0) == -1) |
b43b13ac UD |
399 | return NSS_STATUS_UNAVAIL; |
400 | ||
20cc4c87 UD |
401 | if (af == AF_INET6 && len == IN6ADDRSZ |
402 | && (memcmp (uaddr, mapped, sizeof mapped) == 0 | |
403 | || (memcmp (uaddr, tunnelled, sizeof tunnelled) == 0 | |
404 | && memcmp (&uaddr[sizeof tunnelled], v6local, sizeof v6local)))) | |
5f0e6fc7 RM |
405 | { |
406 | /* Unmap. */ | |
407 | addr += sizeof mapped; | |
408 | uaddr += sizeof mapped; | |
409 | af = AF_INET; | |
410 | len = INADDRSZ; | |
411 | } | |
412 | ||
413 | switch (af) | |
414 | { | |
415 | case AF_INET: | |
416 | size = INADDRSZ; | |
417 | break; | |
418 | case AF_INET6: | |
419 | size = IN6ADDRSZ; | |
420 | break; | |
421 | default: | |
67479a70 | 422 | *errnop = EAFNOSUPPORT; |
5f0e6fc7 RM |
423 | *h_errnop = NETDB_INTERNAL; |
424 | return NSS_STATUS_UNAVAIL; | |
425 | } | |
20cc4c87 | 426 | if (size > len) |
5f0e6fc7 | 427 | { |
67479a70 | 428 | *errnop = EAFNOSUPPORT; |
5f0e6fc7 RM |
429 | *h_errnop = NETDB_INTERNAL; |
430 | return NSS_STATUS_UNAVAIL; | |
431 | } | |
432 | ||
ee778b56 UD |
433 | host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024); |
434 | ||
5f0e6fc7 RM |
435 | switch (af) |
436 | { | |
437 | case AF_INET: | |
438 | sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff), | |
439 | (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff)); | |
440 | break; | |
441 | case AF_INET6: | |
ee778b56 UD |
442 | /* Only lookup with the byte string format if the user wants it. */ |
443 | if (__builtin_expect (_res.options & RES_USEBSTRING, 0)) | |
444 | { | |
445 | qp = stpcpy (qbuf, "\\[x"); | |
446 | for (n = 0; n < IN6ADDRSZ; ++n) | |
447 | qp += sprintf (qp, "%02hhx", uaddr[n]); | |
448 | strcpy (qp, "].ip6.arpa"); | |
449 | n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, | |
1eb946b9 | 450 | host_buffer.buf->buf, 1024, &host_buffer.ptr, |
b7da31a1 | 451 | NULL, NULL, NULL); |
ee778b56 UD |
452 | if (n >= 0) |
453 | goto got_it_already; | |
454 | } | |
5f0e6fc7 | 455 | qp = qbuf; |
ee778b56 UD |
456 | for (n = IN6ADDRSZ - 1; n >= 0; n--) |
457 | { | |
458 | static const char nibblechar[16] = "0123456789abcdef"; | |
459 | *qp++ = nibblechar[uaddr[n] & 0xf]; | |
460 | *qp++ = '.'; | |
461 | *qp++ = nibblechar[(uaddr[n] >> 4) & 0xf]; | |
462 | *qp++ = '.'; | |
463 | } | |
464 | strcpy(qp, "ip6.arpa"); | |
5f0e6fc7 RM |
465 | break; |
466 | default: | |
467 | /* Cannot happen. */ | |
9fea9ed6 | 468 | break; |
5f0e6fc7 RM |
469 | } |
470 | ||
16b66062 | 471 | n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf, |
b7da31a1 | 472 | 1024, &host_buffer.ptr, NULL, NULL, NULL); |
340ef046 | 473 | if (n < 0 && af == AF_INET6 && (_res.options & RES_NOIP6DOTINT) == 0) |
62a08e44 UD |
474 | { |
475 | strcpy (qp, "ip6.int"); | |
16b66062 AJ |
476 | n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf, |
477 | host_buffer.buf != orig_host_buffer | |
1eb946b9 | 478 | ? MAXPACKET : 1024, &host_buffer.ptr, |
b7da31a1 | 479 | NULL, NULL, NULL); |
62a08e44 | 480 | } |
5f0e6fc7 | 481 | if (n < 0) |
dd7d45e8 UD |
482 | { |
483 | *h_errnop = h_errno; | |
34816665 | 484 | __set_errno (olderr); |
16b66062 AJ |
485 | if (host_buffer.buf != orig_host_buffer) |
486 | free (host_buffer.buf); | |
dd7d45e8 UD |
487 | return errno == ECONNREFUSED ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND; |
488 | } | |
5f0e6fc7 | 489 | |
ee778b56 | 490 | got_it_already: |
16b66062 | 491 | status = getanswer_r (host_buffer.buf, n, qbuf, T_PTR, result, buffer, buflen, |
31e2791c | 492 | errnop, h_errnop, 0 /* XXX */, ttlp, NULL); |
16b66062 AJ |
493 | if (host_buffer.buf != orig_host_buffer) |
494 | free (host_buffer.buf); | |
5f0e6fc7 | 495 | if (status != NSS_STATUS_SUCCESS) |
65b4743a | 496 | return status; |
5f0e6fc7 RM |
497 | |
498 | #ifdef SUNSECURITY | |
499 | This is not implemented because it is not possible to use the current | |
500 | source from bind in a multi-threaded program. | |
501 | #endif | |
502 | ||
503 | result->h_addrtype = af; | |
504 | result->h_length = len; | |
63551311 | 505 | memcpy (host_data->host_addr, addr, len); |
5f0e6fc7 RM |
506 | host_data->h_addr_ptrs[0] = (char *) host_data->host_addr; |
507 | host_data->h_addr_ptrs[1] = NULL; | |
b455972f UD |
508 | #if 0 |
509 | /* XXX I think this is wrong. Why should an IPv4 address be | |
510 | converted to IPv6 if the user explicitly asked for IPv4? */ | |
5f0e6fc7 RM |
511 | if (af == AF_INET && (_res.options & RES_USE_INET6)) |
512 | { | |
513 | map_v4v6_address ((char *) host_data->host_addr, | |
514 | (char *) host_data->host_addr); | |
515 | result->h_addrtype = AF_INET6; | |
516 | result->h_length = IN6ADDRSZ; | |
517 | } | |
b455972f | 518 | #endif |
5f0e6fc7 RM |
519 | *h_errnop = NETDB_SUCCESS; |
520 | return NSS_STATUS_SUCCESS; | |
521 | } | |
31e2791c UD |
522 | hidden_def (_nss_dns_gethostbyaddr2_r) |
523 | ||
524 | ||
525 | enum nss_status | |
526 | _nss_dns_gethostbyaddr_r (const void *addr, socklen_t len, int af, | |
527 | struct hostent *result, char *buffer, size_t buflen, | |
528 | int *errnop, int *h_errnop) | |
529 | { | |
530 | return _nss_dns_gethostbyaddr2_r (addr, len, af, result, buffer, buflen, | |
531 | errnop, h_errnop, NULL); | |
532 | } | |
5f0e6fc7 | 533 | |
5db91571 UD |
534 | #ifdef RESOLVSORT |
535 | static void addrsort (char **ap, int num); | |
536 | ||
537 | static void | |
538 | addrsort (char **ap, int num) | |
539 | { | |
540 | int i, j; | |
541 | char **p; | |
542 | short aval[MAX_NR_ADDRS]; | |
543 | int needsort = 0; | |
544 | ||
545 | p = ap; | |
546 | if (num > MAX_NR_ADDRS) | |
547 | num = MAX_NR_ADDRS; | |
548 | for (i = 0; i < num; i++, p++) | |
549 | { | |
550 | for (j = 0 ; (unsigned)j < _res.nsort; j++) | |
551 | if (_res.sort_list[j].addr.s_addr == | |
552 | (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask)) | |
553 | break; | |
554 | aval[i] = j; | |
555 | if (needsort == 0 && i > 0 && j < aval[i-1]) | |
556 | needsort = i; | |
557 | } | |
558 | if (!needsort) | |
559 | return; | |
560 | ||
561 | while (needsort++ < num) | |
562 | for (j = needsort - 2; j >= 0; j--) | |
563 | if (aval[j] > aval[j+1]) | |
564 | { | |
565 | char *hp; | |
566 | ||
567 | i = aval[j]; | |
568 | aval[j] = aval[j+1]; | |
569 | aval[j+1] = i; | |
570 | ||
571 | hp = ap[j]; | |
572 | ap[j] = ap[j+1]; | |
573 | ap[j+1] = hp; | |
574 | } | |
575 | else | |
576 | break; | |
577 | } | |
578 | #endif | |
5f0e6fc7 RM |
579 | |
580 | static enum nss_status | |
581 | getanswer_r (const querybuf *answer, int anslen, const char *qname, int qtype, | |
df4ef2ab | 582 | struct hostent *result, char *buffer, size_t buflen, |
d1fe1f22 | 583 | int *errnop, int *h_errnop, int map, int32_t *ttlp, char **canonp) |
5f0e6fc7 RM |
584 | { |
585 | struct host_data | |
586 | { | |
587 | char *aliases[MAX_NR_ALIASES]; | |
588 | unsigned char host_addr[16]; /* IPv4 or IPv6 */ | |
88a96b81 | 589 | char *h_addr_ptrs[0]; |
2f1687b9 UD |
590 | } *host_data; |
591 | int linebuflen; | |
5f0e6fc7 RM |
592 | register const HEADER *hp; |
593 | const u_char *end_of_message, *cp; | |
594 | int n, ancount, qdcount; | |
595 | int haveanswer, had_error; | |
596 | char *bp, **ap, **hap; | |
845dcb57 | 597 | char tbuf[MAXDNAME]; |
5f0e6fc7 | 598 | const char *tname; |
b455972f | 599 | int (*name_ok) (const char *); |
8d8c6efa | 600 | u_char packtmp[NS_MAXCDNAME]; |
b455972f | 601 | int have_to_map = 0; |
2f1687b9 UD |
602 | uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct host_data); |
603 | buffer += pad; | |
604 | if (__builtin_expect (buflen < sizeof (struct host_data) + pad, 0)) | |
560d3b76 UD |
605 | { |
606 | /* The buffer is too small. */ | |
607 | too_small: | |
608 | *errnop = ERANGE; | |
609 | *h_errnop = NETDB_INTERNAL; | |
610 | return NSS_STATUS_TRYAGAIN; | |
611 | } | |
2f1687b9 UD |
612 | host_data = (struct host_data *) buffer; |
613 | linebuflen = buflen - sizeof (struct host_data); | |
614 | if (buflen - sizeof (struct host_data) != linebuflen) | |
615 | linebuflen = INT_MAX; | |
560d3b76 | 616 | |
5f0e6fc7 RM |
617 | tname = qname; |
618 | result->h_name = NULL; | |
619 | end_of_message = answer->buf + anslen; | |
620 | switch (qtype) | |
621 | { | |
622 | case T_A: | |
623 | case T_AAAA: | |
624 | name_ok = res_hnok; | |
625 | break; | |
626 | case T_PTR: | |
627 | name_ok = res_dnok; | |
628 | break; | |
629 | default: | |
a0bf6ac7 | 630 | *errnop = ENOENT; |
5f0e6fc7 RM |
631 | return NSS_STATUS_UNAVAIL; /* XXX should be abort(); */ |
632 | } | |
633 | ||
634 | /* | |
635 | * find first satisfactory answer | |
636 | */ | |
637 | hp = &answer->hdr; | |
5f0e6fc7 RM |
638 | ancount = ntohs (hp->ancount); |
639 | qdcount = ntohs (hp->qdcount); | |
640 | cp = answer->buf + HFIXEDSZ; | |
b455972f | 641 | if (__builtin_expect (qdcount, 1) != 1) |
5f0e6fc7 RM |
642 | { |
643 | *h_errnop = NO_RECOVERY; | |
644 | return NSS_STATUS_UNAVAIL; | |
645 | } | |
88a96b81 UD |
646 | if (sizeof (struct host_data) + (ancount + 1) * sizeof (char *) >= buflen) |
647 | goto too_small; | |
648 | bp = (char *) &host_data->h_addr_ptrs[ancount + 1]; | |
649 | linebuflen -= (ancount + 1) * sizeof (char *); | |
5f0e6fc7 | 650 | |
8d8c6efa UD |
651 | n = __ns_name_unpack (answer->buf, end_of_message, cp, |
652 | packtmp, sizeof packtmp); | |
653 | if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1) | |
5f0e6fc7 | 654 | { |
b455972f | 655 | if (__builtin_expect (errno, 0) == EMSGSIZE) |
560d3b76 | 656 | goto too_small; |
8d8c6efa UD |
657 | |
658 | n = -1; | |
659 | } | |
660 | ||
661 | if (n > 0 && bp[0] == '.') | |
662 | bp[0] = '\0'; | |
663 | ||
1eb946b9 UD |
664 | if (__builtin_expect (n < 0 || ((*name_ok) (bp) == 0 && (errno = EBADMSG)), |
665 | 0)) | |
8d8c6efa UD |
666 | { |
667 | *errnop = errno; | |
668 | *h_errnop = NO_RECOVERY; | |
5f0e6fc7 RM |
669 | return NSS_STATUS_UNAVAIL; |
670 | } | |
671 | cp += n + QFIXEDSZ; | |
672 | ||
673 | if (qtype == T_A || qtype == T_AAAA) | |
674 | { | |
675 | /* res_send() has already verified that the query name is the | |
676 | * same as the one we sent; this just gets the expanded name | |
677 | * (i.e., with the succeeding search-domain tacked on). | |
678 | */ | |
679 | n = strlen (bp) + 1; /* for the \0 */ | |
76b87c03 UD |
680 | if (n >= MAXHOSTNAMELEN) |
681 | { | |
67479a70 | 682 | *h_errnop = NO_RECOVERY; |
a0bf6ac7 | 683 | *errnop = ENOENT; |
76b87c03 UD |
684 | return NSS_STATUS_TRYAGAIN; |
685 | } | |
5f0e6fc7 RM |
686 | result->h_name = bp; |
687 | bp += n; | |
688 | linebuflen -= n; | |
560d3b76 UD |
689 | if (linebuflen < 0) |
690 | goto too_small; | |
5f0e6fc7 RM |
691 | /* The qname can be abbreviated, but h_name is now absolute. */ |
692 | qname = result->h_name; | |
693 | } | |
694 | ||
695 | ap = host_data->aliases; | |
696 | *ap = NULL; | |
697 | result->h_aliases = host_data->aliases; | |
698 | hap = host_data->h_addr_ptrs; | |
699 | *hap = NULL; | |
700 | result->h_addr_list = host_data->h_addr_ptrs; | |
701 | haveanswer = 0; | |
702 | had_error = 0; | |
703 | ||
704 | while (ancount-- > 0 && cp < end_of_message && had_error == 0) | |
705 | { | |
706 | int type, class; | |
707 | ||
8d8c6efa UD |
708 | n = __ns_name_unpack (answer->buf, end_of_message, cp, |
709 | packtmp, sizeof packtmp); | |
710 | if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1) | |
711 | { | |
b455972f | 712 | if (__builtin_expect (errno, 0) == EMSGSIZE) |
0cc70fcf | 713 | goto too_small; |
8d8c6efa UD |
714 | |
715 | n = -1; | |
716 | } | |
717 | ||
b7da31a1 | 718 | if (__builtin_expect (n < 0 || (*name_ok) (bp) == 0, 0)) |
5f0e6fc7 RM |
719 | { |
720 | ++had_error; | |
721 | continue; | |
722 | } | |
723 | cp += n; /* name */ | |
b7da31a1 UD |
724 | |
725 | if (__builtin_expect (cp + 10 > end_of_message, 0)) | |
726 | { | |
727 | ++had_error; | |
728 | continue; | |
729 | } | |
730 | ||
cd5743fd | 731 | type = __ns_get16 (cp); |
5f0e6fc7 | 732 | cp += INT16SZ; /* type */ |
cd5743fd | 733 | class = __ns_get16 (cp); |
d1fe1f22 | 734 | cp += INT16SZ; /* class */ |
a7690819 | 735 | int32_t ttl = __ns_get32 (cp); |
d1fe1f22 | 736 | cp += INT32SZ; /* TTL */ |
cd5743fd | 737 | n = __ns_get16 (cp); |
5f0e6fc7 | 738 | cp += INT16SZ; /* len */ |
b7da31a1 | 739 | if (__builtin_expect (class != C_IN, 0)) |
5f0e6fc7 RM |
740 | { |
741 | /* XXX - debug? syslog? */ | |
742 | cp += n; | |
743 | continue; /* XXX - had_error++ ? */ | |
744 | } | |
745 | ||
1eb946b9 | 746 | if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) |
5f0e6fc7 RM |
747 | { |
748 | if (ap >= &host_data->aliases[MAX_NR_ALIASES - 1]) | |
749 | continue; | |
750 | n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf); | |
b7da31a1 | 751 | if (__builtin_expect (n < 0 || (*name_ok) (tbuf) == 0, 0)) |
5f0e6fc7 RM |
752 | { |
753 | ++had_error; | |
754 | continue; | |
755 | } | |
756 | cp += n; | |
757 | /* Store alias. */ | |
758 | *ap++ = bp; | |
759 | n = strlen (bp) + 1; /* For the \0. */ | |
b455972f | 760 | if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN) |
76b87c03 UD |
761 | { |
762 | ++had_error; | |
763 | continue; | |
764 | } | |
5f0e6fc7 RM |
765 | bp += n; |
766 | linebuflen -= n; | |
767 | /* Get canonical name. */ | |
768 | n = strlen (tbuf) + 1; /* For the \0. */ | |
b455972f | 769 | if (__builtin_expect (n > linebuflen, 0)) |
0cc70fcf | 770 | goto too_small; |
b455972f | 771 | if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN) |
5f0e6fc7 RM |
772 | { |
773 | ++had_error; | |
774 | continue; | |
775 | } | |
8d8c6efa UD |
776 | result->h_name = bp; |
777 | bp = __mempcpy (bp, tbuf, n); /* Cannot overflow. */ | |
5f0e6fc7 RM |
778 | linebuflen -= n; |
779 | continue; | |
780 | } | |
781 | ||
782 | if (qtype == T_PTR && type == T_CNAME) | |
783 | { | |
784 | n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf); | |
b7da31a1 | 785 | if (__builtin_expect (n < 0 || res_dnok (tbuf) == 0, 0)) |
5f0e6fc7 RM |
786 | { |
787 | ++had_error; | |
788 | continue; | |
789 | } | |
790 | cp += n; | |
da2d1bc5 | 791 | /* Get canonical name. */ |
5f0e6fc7 | 792 | n = strlen (tbuf) + 1; /* For the \0. */ |
b455972f | 793 | if (__builtin_expect (n > linebuflen, 0)) |
0cc70fcf | 794 | goto too_small; |
b455972f | 795 | if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN) |
5f0e6fc7 RM |
796 | { |
797 | ++had_error; | |
798 | continue; | |
799 | } | |
8d8c6efa UD |
800 | tname = bp; |
801 | bp = __mempcpy (bp, tbuf, n); /* Cannot overflow. */ | |
5f0e6fc7 RM |
802 | linebuflen -= n; |
803 | continue; | |
804 | } | |
b455972f UD |
805 | if (__builtin_expect (type == T_SIG, 0) |
806 | || __builtin_expect (type == T_KEY, 0) | |
807 | || __builtin_expect (type == T_NXT, 0)) | |
1bc21e7a UD |
808 | { |
809 | /* We don't support DNSSEC yet. For now, ignore the record | |
810 | and send a low priority message to syslog. */ | |
811 | syslog (LOG_DEBUG | LOG_AUTH, | |
812 | "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", | |
813 | qname, p_class (C_IN), p_type(qtype), p_type (type)); | |
814 | cp += n; | |
815 | continue; | |
816 | } | |
b455972f UD |
817 | |
818 | if (type == T_A && qtype == T_AAAA && map) | |
819 | have_to_map = 1; | |
820 | else if (__builtin_expect (type != qtype, 0)) | |
5f0e6fc7 RM |
821 | { |
822 | syslog (LOG_NOTICE | LOG_AUTH, | |
823 | "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", | |
824 | qname, p_class (C_IN), p_type (qtype), p_type (type)); | |
825 | cp += n; | |
826 | continue; /* XXX - had_error++ ? */ | |
827 | } | |
828 | ||
829 | switch (type) | |
830 | { | |
831 | case T_PTR: | |
c6807d38 | 832 | if (__builtin_expect (strcasecmp (tname, bp) != 0, 0)) |
5f0e6fc7 RM |
833 | { |
834 | syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp); | |
835 | cp += n; | |
836 | continue; /* XXX - had_error++ ? */ | |
837 | } | |
8d8c6efa UD |
838 | |
839 | n = __ns_name_unpack (answer->buf, end_of_message, cp, | |
840 | packtmp, sizeof packtmp); | |
841 | if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1) | |
842 | { | |
b455972f | 843 | if (__builtin_expect (errno, 0) == EMSGSIZE) |
0cc70fcf | 844 | goto too_small; |
8d8c6efa UD |
845 | |
846 | n = -1; | |
847 | } | |
848 | ||
b7da31a1 | 849 | if (__builtin_expect (n < 0 || res_hnok (bp) == 0, 0)) |
5f0e6fc7 RM |
850 | { |
851 | ++had_error; | |
852 | break; | |
853 | } | |
854 | #if MULTI_PTRS_ARE_ALIASES | |
855 | cp += n; | |
856 | if (haveanswer == 0) | |
857 | result->h_name = bp; | |
858 | else if (ap < &host_data->aliases[MAXALIASES-1]) | |
859 | *ap++ = bp; | |
860 | else | |
861 | n = -1; | |
862 | if (n != -1) | |
863 | { | |
864 | n = strlen (bp) + 1; /* for the \0 */ | |
b455972f | 865 | if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN) |
76b87c03 UD |
866 | { |
867 | ++had_error; | |
868 | break; | |
869 | } | |
5f0e6fc7 RM |
870 | bp += n; |
871 | linebuflen -= n; | |
872 | } | |
873 | break; | |
874 | #else | |
875 | result->h_name = bp; | |
b455972f | 876 | if (have_to_map) |
5f0e6fc7 RM |
877 | { |
878 | n = strlen (bp) + 1; /* for the \0 */ | |
b7da31a1 | 879 | if (__builtin_expect (n >= MAXHOSTNAMELEN, 0)) |
76b87c03 UD |
880 | { |
881 | ++had_error; | |
882 | break; | |
883 | } | |
5f0e6fc7 RM |
884 | bp += n; |
885 | linebuflen -= n; | |
51e4196f AS |
886 | if (map_v4v6_hostent (result, &bp, &linebuflen)) |
887 | goto too_small; | |
5f0e6fc7 RM |
888 | } |
889 | *h_errnop = NETDB_SUCCESS; | |
890 | return NSS_STATUS_SUCCESS; | |
891 | #endif | |
892 | case T_A: | |
893 | case T_AAAA: | |
b455972f | 894 | if (__builtin_expect (strcasecmp (result->h_name, bp), 0) != 0) |
5f0e6fc7 RM |
895 | { |
896 | syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, result->h_name, bp); | |
897 | cp += n; | |
898 | continue; /* XXX - had_error++ ? */ | |
899 | } | |
1f64ac13 | 900 | if (n != result->h_length) |
5f0e6fc7 | 901 | { |
1f64ac13 UD |
902 | cp += n; |
903 | continue; | |
5f0e6fc7 | 904 | } |
1f64ac13 | 905 | if (!haveanswer) |
5f0e6fc7 RM |
906 | { |
907 | register int nn; | |
908 | ||
a7690819 | 909 | if (ttlp != NULL) |
d1fe1f22 UD |
910 | *ttlp = ttl; |
911 | if (canonp != NULL) | |
912 | *canonp = bp; | |
5f0e6fc7 RM |
913 | result->h_name = bp; |
914 | nn = strlen (bp) + 1; /* for the \0 */ | |
915 | bp += nn; | |
916 | linebuflen -= nn; | |
917 | } | |
918 | ||
560d3b76 | 919 | linebuflen -= sizeof (align) - ((u_long) bp % sizeof (align)); |
5f0e6fc7 RM |
920 | bp += sizeof (align) - ((u_long) bp % sizeof (align)); |
921 | ||
b455972f | 922 | if (__builtin_expect (n > linebuflen, 0)) |
0cc70fcf | 923 | goto too_small; |
8d8c6efa | 924 | bp = __mempcpy (*hap++ = bp, cp, n); |
5f0e6fc7 RM |
925 | cp += n; |
926 | linebuflen -= n; | |
927 | break; | |
928 | default: | |
929 | abort (); | |
930 | } | |
931 | if (had_error == 0) | |
932 | ++haveanswer; | |
933 | } | |
934 | ||
935 | if (haveanswer > 0) | |
936 | { | |
937 | *ap = NULL; | |
938 | *hap = NULL; | |
da2d1bc5 | 939 | #if defined RESOLVSORT |
5f0e6fc7 RM |
940 | /* |
941 | * Note: we sort even if host can take only one address | |
942 | * in its return structures - should give it the "best" | |
943 | * address in that case, not some random one | |
944 | */ | |
945 | if (_res.nsort && haveanswer > 1 && qtype == T_A) | |
946 | addrsort (host_data->h_addr_ptrs, haveanswer); | |
947 | #endif /*RESOLVSORT*/ | |
948 | ||
949 | if (result->h_name == NULL) | |
950 | { | |
951 | n = strlen (qname) + 1; /* For the \0. */ | |
8d8c6efa | 952 | if (n > linebuflen) |
0cc70fcf | 953 | goto too_small; |
8d8c6efa | 954 | if (n >= MAXHOSTNAMELEN) |
76b87c03 | 955 | goto no_recovery; |
8d8c6efa UD |
956 | result->h_name = bp; |
957 | bp = __mempcpy (bp, qname, n); /* Cannot overflow. */ | |
5f0e6fc7 RM |
958 | linebuflen -= n; |
959 | } | |
960 | ||
b455972f | 961 | if (have_to_map) |
51e4196f AS |
962 | if (map_v4v6_hostent (result, &bp, &linebuflen)) |
963 | goto too_small; | |
5f0e6fc7 RM |
964 | *h_errnop = NETDB_SUCCESS; |
965 | return NSS_STATUS_SUCCESS; | |
966 | } | |
76b87c03 UD |
967 | no_recovery: |
968 | *h_errnop = NO_RECOVERY; | |
a0bf6ac7 | 969 | *errnop = ENOENT; |
2884dad4 UD |
970 | /* Special case here: if the resolver sent a result but it only |
971 | contains a CNAME while we are looking for a T_A or T_AAAA record, | |
972 | we fail with NOTFOUND instead of TRYAGAIN. */ | |
973 | return ((qtype == T_A || qtype == T_AAAA) && ap != host_data->aliases | |
974 | ? NSS_STATUS_NOTFOUND : NSS_STATUS_TRYAGAIN); | |
5f0e6fc7 | 975 | } |
1eb946b9 UD |
976 | |
977 | ||
978 | static enum nss_status | |
979 | gaih_getanswer_slice (const querybuf *answer, int anslen, const char *qname, | |
980 | struct gaih_addrtuple ***patp, | |
981 | char **bufferp, size_t *buflenp, | |
982 | int *errnop, int *h_errnop, int32_t *ttlp, int *firstp) | |
983 | { | |
984 | char *buffer = *bufferp; | |
985 | size_t buflen = *buflenp; | |
986 | ||
987 | struct gaih_addrtuple **pat = *patp; | |
988 | const HEADER *hp = &answer->hdr; | |
989 | int ancount = ntohs (hp->ancount); | |
990 | int qdcount = ntohs (hp->qdcount); | |
991 | const u_char *cp = answer->buf + HFIXEDSZ; | |
992 | const u_char *end_of_message = answer->buf + anslen; | |
993 | if (__builtin_expect (qdcount != 1, 0)) | |
994 | { | |
995 | *h_errnop = NO_RECOVERY; | |
996 | return NSS_STATUS_UNAVAIL; | |
997 | } | |
998 | ||
b7da31a1 | 999 | u_char packtmp[NS_MAXCDNAME]; |
1eb946b9 UD |
1000 | int n = __ns_name_unpack (answer->buf, end_of_message, cp, |
1001 | packtmp, sizeof packtmp); | |
1002 | /* We unpack the name to check it for validity. But we do not need | |
1003 | it later. */ | |
1004 | if (n != -1 && __ns_name_ntop (packtmp, buffer, buflen) == -1) | |
1005 | { | |
1006 | if (__builtin_expect (errno, 0) == EMSGSIZE) | |
1007 | { | |
1008 | too_small: | |
1009 | *errnop = ERANGE; | |
1010 | *h_errnop = NETDB_INTERNAL; | |
1011 | return NSS_STATUS_TRYAGAIN; | |
1012 | } | |
1013 | ||
1014 | n = -1; | |
1015 | } | |
1016 | ||
1017 | if (__builtin_expect (n < 0 || (res_hnok (buffer) == 0 | |
1018 | && (errno = EBADMSG)), 0)) | |
1019 | { | |
1020 | *errnop = errno; | |
1021 | *h_errnop = NO_RECOVERY; | |
1022 | return NSS_STATUS_UNAVAIL; | |
1023 | } | |
1024 | cp += n + QFIXEDSZ; | |
1025 | ||
1026 | int haveanswer = 0; | |
1027 | int had_error = 0; | |
1028 | char *canon = NULL; | |
1029 | char *h_name = NULL; | |
1030 | int h_namelen = 0; | |
1031 | ||
343996c4 UD |
1032 | if (ancount == 0) |
1033 | return NSS_STATUS_NOTFOUND; | |
1034 | ||
1eb946b9 UD |
1035 | while (ancount-- > 0 && cp < end_of_message && had_error == 0) |
1036 | { | |
1037 | n = __ns_name_unpack (answer->buf, end_of_message, cp, | |
1038 | packtmp, sizeof packtmp); | |
1039 | if (n != -1 && | |
1040 | (h_namelen = __ns_name_ntop (packtmp, buffer, buflen)) == -1) | |
1041 | { | |
1042 | if (__builtin_expect (errno, 0) == EMSGSIZE) | |
1043 | goto too_small; | |
1044 | ||
1045 | n = -1; | |
1046 | } | |
b7da31a1 | 1047 | if (__builtin_expect (n < 0 || res_hnok (buffer) == 0, 0)) |
1eb946b9 UD |
1048 | { |
1049 | ++had_error; | |
1050 | continue; | |
1051 | } | |
ea42a20c | 1052 | if (*firstp && canon == NULL) |
1eb946b9 UD |
1053 | { |
1054 | h_name = buffer; | |
1055 | buffer += h_namelen; | |
1056 | buflen -= h_namelen; | |
1057 | } | |
1058 | ||
1059 | cp += n; /* name */ | |
b7da31a1 UD |
1060 | |
1061 | if (__builtin_expect (cp + 10 > end_of_message, 0)) | |
1062 | { | |
1063 | ++had_error; | |
1064 | continue; | |
1065 | } | |
1066 | ||
cd5743fd | 1067 | int type = __ns_get16 (cp); |
1eb946b9 | 1068 | cp += INT16SZ; /* type */ |
cd5743fd | 1069 | int class = __ns_get16 (cp); |
1eb946b9 | 1070 | cp += INT16SZ; /* class */ |
cd5743fd | 1071 | int32_t ttl = __ns_get32 (cp); |
1eb946b9 | 1072 | cp += INT32SZ; /* TTL */ |
cd5743fd | 1073 | n = __ns_get16 (cp); |
1eb946b9 UD |
1074 | cp += INT16SZ; /* len */ |
1075 | ||
1076 | if (class != C_IN) | |
1077 | { | |
1078 | cp += n; | |
1079 | continue; | |
1080 | } | |
1081 | ||
1082 | if (type == T_CNAME) | |
1083 | { | |
1084 | char tbuf[MAXDNAME]; | |
1085 | n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf); | |
b7da31a1 | 1086 | if (__builtin_expect (n < 0 || res_hnok (tbuf) == 0, 0)) |
1eb946b9 UD |
1087 | { |
1088 | ++had_error; | |
1089 | continue; | |
1090 | } | |
1091 | cp += n; | |
1092 | ||
1093 | if (*firstp) | |
1094 | { | |
1095 | /* Reclaim buffer space. */ | |
1096 | if (h_name + h_namelen == buffer) | |
1097 | { | |
1098 | buffer = h_name; | |
1099 | buflen += h_namelen; | |
1100 | } | |
1101 | ||
1102 | n = strlen (tbuf) + 1; | |
1103 | if (__builtin_expect (n > buflen, 0)) | |
1104 | goto too_small; | |
1105 | if (__builtin_expect (n >= MAXHOSTNAMELEN, 0)) | |
1106 | { | |
1107 | ++had_error; | |
1108 | continue; | |
1109 | } | |
1110 | ||
1111 | canon = buffer; | |
1112 | buffer = __mempcpy (buffer, tbuf, n); | |
1113 | buflen -= n; | |
1114 | h_namelen = 0; | |
1115 | } | |
1116 | continue; | |
1117 | } | |
9ba27474 UD |
1118 | #if 1 |
1119 | // We should not see any types other than those explicitly listed | |
1120 | // below. Some types sent by server seem missing, though. Just | |
1121 | // collect the data for now. | |
1122 | if (__builtin_expect (type != T_A && type != T_AAAA, 0)) | |
1123 | #else | |
1eb946b9 UD |
1124 | if (__builtin_expect (type == T_SIG, 0) |
1125 | || __builtin_expect (type == T_KEY, 0) | |
1126 | || __builtin_expect (type == T_NXT, 0) | |
c1283552 UD |
1127 | || __builtin_expect (type == T_PTR, 0) |
1128 | || __builtin_expect (type == T_DNAME, 0)) | |
9ba27474 | 1129 | #endif |
1eb946b9 UD |
1130 | { |
1131 | /* We don't support DNSSEC yet. For now, ignore the record | |
1132 | and send a low priority message to syslog. | |
1133 | ||
c1283552 | 1134 | We also don't expect T_PTR or T_DNAME messages. */ |
1eb946b9 UD |
1135 | syslog (LOG_DEBUG | LOG_AUTH, |
1136 | "getaddrinfo*.gaih_getanswer: got type \"%s\"", | |
1137 | p_type (type)); | |
1138 | cp += n; | |
1139 | continue; | |
1140 | } | |
1141 | if (type != T_A && type != T_AAAA) | |
1142 | abort (); | |
1143 | ||
1144 | if (*pat == NULL) | |
1145 | { | |
1146 | uintptr_t pad = (-(uintptr_t) buffer | |
1147 | % __alignof__ (struct gaih_addrtuple)); | |
1148 | buffer += pad; | |
1149 | buflen = buflen > pad ? buflen - pad : 0; | |
1150 | ||
1151 | if (__builtin_expect (buflen < sizeof (struct gaih_addrtuple), | |
1152 | 0)) | |
f854efd7 | 1153 | goto too_small; |
1eb946b9 UD |
1154 | |
1155 | *pat = (struct gaih_addrtuple *) buffer; | |
1156 | buffer += sizeof (struct gaih_addrtuple); | |
1157 | buflen -= sizeof (struct gaih_addrtuple); | |
1158 | } | |
1159 | ||
1160 | (*pat)->name = NULL; | |
1161 | (*pat)->next = NULL; | |
1162 | ||
1163 | if (*firstp) | |
1164 | { | |
a7690819 | 1165 | if (ttlp != NULL) |
1eb946b9 UD |
1166 | *ttlp = ttl; |
1167 | ||
ea42a20c | 1168 | (*pat)->name = canon ?: h_name; |
1eb946b9 UD |
1169 | |
1170 | *firstp = 0; | |
1171 | } | |
1172 | ||
1173 | (*pat)->family = type == T_A ? AF_INET : AF_INET6; | |
b7da31a1 UD |
1174 | if (__builtin_expect ((type == T_A && n != INADDRSZ) |
1175 | || (type == T_AAAA && n != IN6ADDRSZ), 0)) | |
1176 | { | |
1177 | ++had_error; | |
1178 | continue; | |
1179 | } | |
1eb946b9 UD |
1180 | memcpy ((*pat)->addr, cp, n); |
1181 | cp += n; | |
1182 | (*pat)->scopeid = 0; | |
1183 | ||
1184 | pat = &((*pat)->next); | |
1185 | ||
1186 | haveanswer = 1; | |
1187 | } | |
1188 | ||
1189 | if (haveanswer) | |
1190 | { | |
1191 | *patp = pat; | |
1192 | *bufferp = buffer; | |
1193 | *buflenp = buflen; | |
1194 | ||
1195 | *h_errnop = NETDB_SUCCESS; | |
1196 | return NSS_STATUS_SUCCESS; | |
1197 | } | |
1198 | ||
1199 | /* Special case here: if the resolver sent a result but it only | |
1200 | contains a CNAME while we are looking for a T_A or T_AAAA record, | |
1201 | we fail with NOTFOUND instead of TRYAGAIN. */ | |
1202 | return canon == NULL ? NSS_STATUS_TRYAGAIN : NSS_STATUS_NOTFOUND; | |
1203 | } | |
1204 | ||
1205 | ||
1206 | static enum nss_status | |
1207 | gaih_getanswer (const querybuf *answer1, int anslen1, const querybuf *answer2, | |
1208 | int anslen2, const char *qname, | |
1209 | struct gaih_addrtuple **pat, char *buffer, size_t buflen, | |
1210 | int *errnop, int *h_errnop, int32_t *ttlp) | |
1211 | { | |
1212 | int first = 1; | |
1213 | ||
343996c4 UD |
1214 | enum nss_status status = NSS_STATUS_NOTFOUND; |
1215 | ||
1216 | if (anslen1 > 0) | |
1217 | status = gaih_getanswer_slice(answer1, anslen1, qname, | |
1218 | &pat, &buffer, &buflen, | |
1219 | errnop, h_errnop, ttlp, | |
1220 | &first); | |
f854efd7 | 1221 | if ((status == NSS_STATUS_SUCCESS || status == NSS_STATUS_NOTFOUND |
b7da31a1 UD |
1222 | || (status == NSS_STATUS_TRYAGAIN |
1223 | && (errno != ERANGE || *h_errnop != NO_RECOVERY))) | |
343996c4 | 1224 | && answer2 != NULL && anslen2 > 0) |
f854efd7 UD |
1225 | { |
1226 | enum nss_status status2 = gaih_getanswer_slice(answer2, anslen2, qname, | |
1227 | &pat, &buffer, &buflen, | |
1228 | errnop, h_errnop, ttlp, | |
1229 | &first); | |
b7da31a1 | 1230 | if (status != NSS_STATUS_SUCCESS && status2 != NSS_STATUS_NOTFOUND) |
f854efd7 UD |
1231 | status = status2; |
1232 | } | |
1eb946b9 UD |
1233 | |
1234 | return status; | |
1235 | } |