]>
Commit | Line | Data |
---|---|---|
f4cf5f2d | 1 | /* Copyright (C) 1996-2012 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 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
5f0e6fc7 RM |
18 | |
19 | /* Parts of this file are plain copies of the file `getnetnamadr.c' from | |
20 | the bind package and it has the following copyright. */ | |
21 | ||
22 | /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro | |
23 | * Dep. Matematica Universidade de Coimbra, Portugal, Europe | |
24 | * | |
25 | * Permission to use, copy, modify, and distribute this software for any | |
26 | * purpose with or without fee is hereby granted, provided that the above | |
27 | * copyright notice and this permission notice appear in all copies. | |
28 | */ | |
29 | /* | |
30 | * Copyright (c) 1983, 1993 | |
31 | * The Regents of the University of California. All rights reserved. | |
32 | * | |
33 | * Redistribution and use in source and binary forms, with or without | |
34 | * modification, are permitted provided that the following conditions | |
35 | * are met: | |
36 | * 1. Redistributions of source code must retain the above copyright | |
37 | * notice, this list of conditions and the following disclaimer. | |
38 | * 2. Redistributions in binary form must reproduce the above copyright | |
39 | * notice, this list of conditions and the following disclaimer in the | |
40 | * documentation and/or other materials provided with the distribution. | |
5f0e6fc7 RM |
41 | * 4. Neither the name of the University nor the names of its contributors |
42 | * may be used to endorse or promote products derived from this software | |
43 | * without specific prior written permission. | |
44 | * | |
45 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
46 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
47 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
48 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
49 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
50 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
51 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
52 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
53 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
54 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
55 | * SUCH DAMAGE. | |
56 | */ | |
57 | ||
58 | #include <ctype.h> | |
59 | #include <errno.h> | |
60 | #include <netdb.h> | |
61 | #include <stdio.h> | |
62 | #include <stdlib.h> | |
63 | #include <string.h> | |
64 | ||
65 | #include "nsswitch.h" | |
66 | #include <arpa/inet.h> | |
67 | ||
68 | /* Maximum number of aliases we allow. */ | |
69 | #define MAX_NR_ALIASES 48 | |
70 | ||
71 | ||
870d19de RM |
72 | #if PACKETSZ > 65536 |
73 | # define MAXPACKET PACKETSZ | |
5f0e6fc7 | 74 | #else |
870d19de | 75 | # define MAXPACKET 65536 |
5f0e6fc7 RM |
76 | #endif |
77 | ||
78 | ||
79 | typedef enum | |
80 | { | |
81 | BYADDR, | |
82 | BYNAME | |
83 | } lookup_method; | |
84 | ||
85 | ||
86 | /* We need this time later. */ | |
87 | typedef union querybuf | |
88 | { | |
89 | HEADER hdr; | |
90 | u_char buf[MAXPACKET]; | |
91 | } querybuf; | |
92 | ||
8d8c6efa UD |
93 | /* These functions are defined in res_comp.c. */ |
94 | #define NS_MAXCDNAME 255 /* maximum compressed domain name */ | |
79937577 UD |
95 | extern int __ns_name_ntop (const u_char *, char *, size_t) __THROW; |
96 | extern int __ns_name_unpack (const u_char *, const u_char *, | |
97 | const u_char *, u_char *, size_t) __THROW; | |
8d8c6efa | 98 | |
5f0e6fc7 | 99 | |
6d52618b | 100 | /* Prototypes for local functions. */ |
84384f5b UD |
101 | static enum nss_status getanswer_r (const querybuf *answer, int anslen, |
102 | struct netent *result, char *buffer, | |
2f1687b9 UD |
103 | size_t buflen, int *errnop, int *h_errnop, |
104 | lookup_method net_i); | |
5f0e6fc7 RM |
105 | |
106 | ||
84384f5b | 107 | enum nss_status |
5f0e6fc7 | 108 | _nss_dns_getnetbyname_r (const char *name, struct netent *result, |
51eecc4a AJ |
109 | char *buffer, size_t buflen, int *errnop, |
110 | int *herrnop) | |
5f0e6fc7 RM |
111 | { |
112 | /* Return entry for network with NAME. */ | |
16b66062 AJ |
113 | union |
114 | { | |
115 | querybuf *buf; | |
116 | u_char *ptr; | |
117 | } net_buffer; | |
118 | querybuf *orig_net_buffer; | |
0420d888 | 119 | int anslen; |
5f0e6fc7 | 120 | char *qbuf; |
6166815d | 121 | enum nss_status status; |
5f0e6fc7 | 122 | |
87bb6b6c | 123 | if (__res_maybe_init (&_res, 0) == -1) |
b43b13ac UD |
124 | return NSS_STATUS_UNAVAIL; |
125 | ||
5f0e6fc7 | 126 | qbuf = strdupa (name); |
6166815d | 127 | |
16b66062 | 128 | net_buffer.buf = orig_net_buffer = (querybuf *) alloca (1024); |
6166815d | 129 | |
16b66062 | 130 | anslen = __libc_res_nsearch (&_res, qbuf, C_IN, T_PTR, net_buffer.buf->buf, |
b7da31a1 | 131 | 1024, &net_buffer.ptr, NULL, NULL, NULL); |
5f0e6fc7 | 132 | if (anslen < 0) |
67479a70 UD |
133 | { |
134 | /* Nothing found. */ | |
135 | *errnop = errno; | |
16b66062 AJ |
136 | if (net_buffer.buf != orig_net_buffer) |
137 | free (net_buffer.buf); | |
67479a70 UD |
138 | return (errno == ECONNREFUSED |
139 | || errno == EPFNOSUPPORT | |
140 | || errno == EAFNOSUPPORT) | |
141 | ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND; | |
142 | } | |
5f0e6fc7 | 143 | |
2f1687b9 UD |
144 | status = getanswer_r (net_buffer.buf, anslen, result, buffer, buflen, |
145 | errnop, herrnop, BYNAME); | |
16b66062 AJ |
146 | if (net_buffer.buf != orig_net_buffer) |
147 | free (net_buffer.buf); | |
6166815d | 148 | return status; |
5f0e6fc7 RM |
149 | } |
150 | ||
151 | ||
84384f5b | 152 | enum nss_status |
51eecc4a AJ |
153 | _nss_dns_getnetbyaddr_r (uint32_t net, int type, struct netent *result, |
154 | char *buffer, size_t buflen, int *errnop, | |
155 | int *herrnop) | |
5f0e6fc7 RM |
156 | { |
157 | /* Return entry for network with NAME. */ | |
84384f5b | 158 | enum nss_status status; |
16b66062 AJ |
159 | union |
160 | { | |
161 | querybuf *buf; | |
162 | u_char *ptr; | |
163 | } net_buffer; | |
164 | querybuf *orig_net_buffer; | |
5f0e6fc7 RM |
165 | unsigned int net_bytes[4]; |
166 | char qbuf[MAXDNAME]; | |
0420d888 | 167 | int cnt, anslen; |
5f0e6fc7 | 168 | u_int32_t net2; |
34816665 | 169 | int olderr = errno; |
5f0e6fc7 RM |
170 | |
171 | /* No net address lookup for IPv6 yet. */ | |
172 | if (type != AF_INET) | |
173 | return NSS_STATUS_UNAVAIL; | |
174 | ||
87bb6b6c | 175 | if (__res_maybe_init (&_res, 0) == -1) |
b43b13ac | 176 | return NSS_STATUS_UNAVAIL; |
51eecc4a | 177 | |
5f0e6fc7 RM |
178 | net2 = (u_int32_t) net; |
179 | for (cnt = 4; net2 != 0; net2 >>= 8) | |
180 | net_bytes[--cnt] = net2 & 0xff; | |
181 | ||
182 | switch (cnt) | |
183 | { | |
184 | case 3: | |
185 | /* Class A network. */ | |
186 | sprintf (qbuf, "0.0.0.%u.in-addr.arpa", net_bytes[3]); | |
187 | break; | |
188 | case 2: | |
189 | /* Class B network. */ | |
190 | sprintf (qbuf, "0.0.%u.%u.in-addr.arpa", net_bytes[3], net_bytes[2]); | |
191 | break; | |
192 | case 1: | |
193 | /* Class C network. */ | |
194 | sprintf (qbuf, "0.%u.%u.%u.in-addr.arpa", net_bytes[3], net_bytes[2], | |
195 | net_bytes[1]); | |
196 | break; | |
197 | case 0: | |
198 | /* Class D - E network. */ | |
199 | sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", net_bytes[3], net_bytes[2], | |
200 | net_bytes[1], net_bytes[0]); | |
201 | break; | |
202 | } | |
203 | ||
16b66062 | 204 | net_buffer.buf = orig_net_buffer = (querybuf *) alloca (1024); |
6166815d | 205 | |
16b66062 | 206 | anslen = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, net_buffer.buf->buf, |
b7da31a1 | 207 | 1024, &net_buffer.ptr, NULL, NULL, NULL); |
5f0e6fc7 | 208 | if (anslen < 0) |
67479a70 UD |
209 | { |
210 | /* Nothing found. */ | |
34816665 UD |
211 | int err = errno; |
212 | __set_errno (olderr); | |
16b66062 AJ |
213 | if (net_buffer.buf != orig_net_buffer) |
214 | free (net_buffer.buf); | |
34816665 UD |
215 | return (err == ECONNREFUSED |
216 | || err == EPFNOSUPPORT | |
217 | || err == EAFNOSUPPORT) | |
67479a70 UD |
218 | ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND; |
219 | } | |
5f0e6fc7 | 220 | |
2f1687b9 UD |
221 | status = getanswer_r (net_buffer.buf, anslen, result, buffer, buflen, |
222 | errnop, herrnop, BYADDR); | |
16b66062 AJ |
223 | if (net_buffer.buf != orig_net_buffer) |
224 | free (net_buffer.buf); | |
5f0e6fc7 RM |
225 | if (status == NSS_STATUS_SUCCESS) |
226 | { | |
227 | /* Strip trailing zeros. */ | |
228 | unsigned int u_net = net; /* Maybe net should be unsigned? */ | |
229 | ||
230 | while ((u_net & 0xff) == 0 && u_net != 0) | |
231 | u_net >>= 8; | |
232 | result->n_net = u_net; | |
233 | } | |
234 | ||
235 | return status; | |
236 | } | |
237 | ||
238 | ||
239 | #undef offsetof | |
240 | #define offsetof(Type, Member) ((size_t) &((Type *) NULL)->Member) | |
241 | ||
84384f5b | 242 | static enum nss_status |
5f0e6fc7 | 243 | getanswer_r (const querybuf *answer, int anslen, struct netent *result, |
2f1687b9 UD |
244 | char *buffer, size_t buflen, int *errnop, int *h_errnop, |
245 | lookup_method net_i) | |
5f0e6fc7 RM |
246 | { |
247 | /* | |
248 | * Find first satisfactory answer | |
249 | * | |
250 | * answer --> +------------+ ( MESSAGE ) | |
251 | * | Header | | |
252 | * +------------+ | |
253 | * | Question | the question for the name server | |
254 | * +------------+ | |
255 | * | Answer | RRs answering the question | |
256 | * +------------+ | |
257 | * | Authority | RRs pointing toward an authority | |
258 | * | Additional | RRs holding additional information | |
259 | * +------------+ | |
260 | */ | |
261 | struct net_data | |
262 | { | |
263 | char *aliases[MAX_NR_ALIASES]; | |
264 | char linebuffer[0]; | |
2f1687b9 UD |
265 | } *net_data; |
266 | ||
267 | uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct net_data); | |
268 | buffer += pad; | |
269 | ||
270 | if (__builtin_expect (buflen < sizeof (*net_data) + pad, 0)) | |
271 | { | |
272 | /* The buffer is too small. */ | |
273 | too_small: | |
274 | *errnop = ERANGE; | |
275 | *h_errnop = NETDB_INTERNAL; | |
276 | return NSS_STATUS_TRYAGAIN; | |
277 | } | |
278 | buflen -= pad; | |
279 | ||
280 | net_data = (struct net_data *) buffer; | |
5f0e6fc7 | 281 | int linebuflen = buflen - offsetof (struct net_data, linebuffer); |
2f1687b9 UD |
282 | if (buflen - offsetof (struct net_data, linebuffer) != linebuflen) |
283 | linebuflen = INT_MAX; | |
9cfe5381 | 284 | const unsigned char *end_of_message = &answer->buf[anslen]; |
5f0e6fc7 RM |
285 | const HEADER *header_pointer = &answer->hdr; |
286 | /* #/records in the answer section. */ | |
287 | int answer_count = ntohs (header_pointer->ancount); | |
288 | /* #/entries in the question section. */ | |
289 | int question_count = ntohs (header_pointer->qdcount); | |
290 | char *bp = net_data->linebuffer; | |
9cfe5381 | 291 | const unsigned char *cp = &answer->buf[HFIXEDSZ]; |
5f0e6fc7 RM |
292 | char **alias_pointer; |
293 | int have_answer; | |
8d8c6efa | 294 | u_char packtmp[NS_MAXCDNAME]; |
5f0e6fc7 RM |
295 | |
296 | if (question_count == 0) | |
7ef90c15 UD |
297 | { |
298 | /* FIXME: the Sun version uses for host name lookup an additional | |
299 | parameter for pointing to h_errno. this is missing here. | |
300 | OSF/1 has a per-thread h_errno variable. */ | |
301 | if (header_pointer->aa != 0) | |
302 | { | |
303 | __set_h_errno (HOST_NOT_FOUND); | |
304 | return NSS_STATUS_NOTFOUND; | |
305 | } | |
306 | else | |
307 | { | |
308 | __set_h_errno (TRY_AGAIN); | |
309 | return NSS_STATUS_TRYAGAIN; | |
310 | } | |
311 | } | |
5f0e6fc7 RM |
312 | |
313 | /* Skip the question part. */ | |
314 | while (question_count-- > 0) | |
9b57c1c1 RM |
315 | { |
316 | int n = __dn_skipname (cp, end_of_message); | |
317 | if (n < 0 || end_of_message - (cp + n) < QFIXEDSZ) | |
318 | { | |
319 | __set_h_errno (NO_RECOVERY); | |
320 | return NSS_STATUS_UNAVAIL; | |
321 | } | |
322 | cp += n + QFIXEDSZ; | |
323 | } | |
5f0e6fc7 RM |
324 | |
325 | alias_pointer = result->n_aliases = &net_data->aliases[0]; | |
326 | *alias_pointer = NULL; | |
327 | have_answer = 0; | |
5f0e6fc7 RM |
328 | |
329 | while (--answer_count >= 0 && cp < end_of_message) | |
330 | { | |
331 | int n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen); | |
332 | int type, class; | |
333 | ||
8d8c6efa UD |
334 | n = __ns_name_unpack (answer->buf, end_of_message, cp, |
335 | packtmp, sizeof packtmp); | |
336 | if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1) | |
337 | { | |
338 | if (errno == EMSGSIZE) | |
2f1687b9 | 339 | goto too_small; |
8d8c6efa UD |
340 | |
341 | n = -1; | |
342 | } | |
343 | ||
344 | if (n > 0 && bp[0] == '.') | |
345 | bp[0] = '\0'; | |
346 | ||
5f0e6fc7 RM |
347 | if (n < 0 || res_dnok (bp) == 0) |
348 | break; | |
349 | cp += n; | |
5f0e6fc7 RM |
350 | GETSHORT (type, cp); |
351 | GETSHORT (class, cp); | |
352 | cp += INT32SZ; /* TTL */ | |
353 | GETSHORT (n, cp); | |
354 | ||
355 | if (class == C_IN && type == T_PTR) | |
356 | { | |
8d8c6efa UD |
357 | n = __ns_name_unpack (answer->buf, end_of_message, cp, |
358 | packtmp, sizeof packtmp); | |
359 | if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1) | |
360 | { | |
361 | if (errno == EMSGSIZE) | |
2f1687b9 | 362 | goto too_small; |
8d8c6efa UD |
363 | |
364 | n = -1; | |
365 | } | |
366 | ||
5f0e6fc7 RM |
367 | if (n < 0 || !res_hnok (bp)) |
368 | { | |
369 | /* XXX What does this mean? The original form from bind | |
370 | returns NULL. Incrementing cp has no effect in any case. | |
371 | What should I return here. ??? */ | |
372 | cp += n; | |
373 | return NSS_STATUS_UNAVAIL; | |
374 | } | |
375 | cp += n; | |
9b57c1c1 RM |
376 | if (alias_pointer + 2 < &net_data->aliases[MAX_NR_ALIASES]) |
377 | { | |
378 | *alias_pointer++ = bp; | |
379 | n = strlen (bp) + 1; | |
380 | bp += n; | |
381 | linebuflen -= n; | |
382 | result->n_addrtype = class == C_IN ? AF_INET : AF_UNSPEC; | |
383 | ++have_answer; | |
384 | } | |
5f0e6fc7 RM |
385 | } |
386 | } | |
387 | ||
388 | if (have_answer) | |
389 | { | |
5f0e6fc7 RM |
390 | *alias_pointer = NULL; |
391 | switch (net_i) | |
392 | { | |
393 | case BYADDR: | |
0bf5c050 | 394 | result->n_name = *result->n_aliases++; |
5f0e6fc7 | 395 | result->n_net = 0L; |
0bf5c050 | 396 | return NSS_STATUS_SUCCESS; |
5f0e6fc7 | 397 | |
0bf5c050 RM |
398 | case BYNAME: |
399 | { | |
400 | char **ap = result->n_aliases++; | |
401 | while (*ap != NULL) | |
402 | { | |
403 | /* Check each alias name for being of the forms: | |
404 | 4.3.2.1.in-addr.arpa = net 1.2.3.4 | |
405 | 3.2.1.in-addr.arpa = net 0.1.2.3 | |
406 | 2.1.in-addr.arpa = net 0.0.1.2 | |
407 | 1.in-addr.arpa = net 0.0.0.1 | |
408 | */ | |
409 | uint32_t val = 0; /* Accumulator for n_net value. */ | |
410 | unsigned int shift = 0; /* Which part we are parsing now. */ | |
411 | const char *p = *ap; /* Consuming the string. */ | |
412 | do | |
413 | { | |
414 | /* Match the leading 0 or 0[xX] base indicator. */ | |
415 | unsigned int base = 10; | |
416 | if (*p == '0' && p[1] != '.') | |
417 | { | |
418 | base = 8; | |
419 | ++p; | |
420 | if (*p == 'x' || *p == 'X') | |
421 | { | |
422 | base = 16; | |
423 | ++p; | |
424 | if (*p == '.') | |
425 | break; /* No digit here. Give up on alias. */ | |
426 | } | |
427 | if (*p == '\0') | |
428 | break; | |
429 | } | |
430 | ||
431 | uint32_t part = 0; /* Accumulates this part's number. */ | |
432 | do | |
433 | { | |
434 | if (isdigit (*p) && (*p - '0' < base)) | |
435 | part = (part * base) + (*p - '0'); | |
436 | else if (base == 16 && isxdigit (*p)) | |
437 | part = (part << 4) + 10 + (tolower (*p) - 'a'); | |
438 | ++p; | |
439 | } while (*p != '\0' && *p != '.'); | |
440 | ||
441 | if (*p != '.') | |
442 | break; /* Bad form. Give up on this name. */ | |
443 | ||
444 | /* Install this as the next more significant byte. */ | |
445 | val |= part << shift; | |
446 | shift += 8; | |
447 | ++p; | |
448 | ||
449 | /* If we are out of digits now, there are two cases: | |
450 | 1. We are done with digits and now see "in-addr.arpa". | |
451 | 2. This is not the droid we are looking for. */ | |
452 | if (!isdigit (*p) && !strcasecmp (p, "in-addr.arpa")) | |
453 | { | |
454 | result->n_net = val; | |
455 | return NSS_STATUS_SUCCESS; | |
456 | } | |
457 | ||
458 | /* Keep going when we have seen fewer than 4 parts. */ | |
459 | } while (shift < 32); | |
460 | } | |
461 | } | |
5f0e6fc7 RM |
462 | break; |
463 | } | |
5f0e6fc7 RM |
464 | } |
465 | ||
a68b0d31 | 466 | __set_h_errno (TRY_AGAIN); |
5f0e6fc7 RM |
467 | return NSS_STATUS_TRYAGAIN; |
468 | } |