]>
Commit | Line | Data |
---|---|---|
d614a753 | 1 | /* Copyright (C) 1996-2020 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 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://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> | |
e054f494 | 64 | #include <stdint.h> |
c3bae689 | 65 | #include <stddef.h> |
5f0e6fc7 RM |
66 | |
67 | #include "nsswitch.h" | |
68 | #include <arpa/inet.h> | |
44f28da7 | 69 | #include <arpa/nameser.h> |
352f4ff9 FW |
70 | #include <resolv/resolv-internal.h> |
71 | #include <resolv/resolv_context.h> | |
5f0e6fc7 RM |
72 | |
73 | /* Maximum number of aliases we allow. */ | |
74 | #define MAX_NR_ALIASES 48 | |
75 | ||
76 | ||
870d19de RM |
77 | #if PACKETSZ > 65536 |
78 | # define MAXPACKET PACKETSZ | |
5f0e6fc7 | 79 | #else |
870d19de | 80 | # define MAXPACKET 65536 |
5f0e6fc7 RM |
81 | #endif |
82 | ||
83 | ||
84 | typedef enum | |
85 | { | |
86 | BYADDR, | |
87 | BYNAME | |
88 | } lookup_method; | |
89 | ||
90 | ||
91 | /* We need this time later. */ | |
92 | typedef union querybuf | |
93 | { | |
94 | HEADER hdr; | |
95 | u_char buf[MAXPACKET]; | |
96 | } querybuf; | |
97 | ||
6d52618b | 98 | /* Prototypes for local functions. */ |
84384f5b UD |
99 | static enum nss_status getanswer_r (const querybuf *answer, int anslen, |
100 | struct netent *result, char *buffer, | |
2f1687b9 UD |
101 | size_t buflen, int *errnop, int *h_errnop, |
102 | lookup_method net_i); | |
5f0e6fc7 RM |
103 | |
104 | ||
84384f5b | 105 | enum nss_status |
5f0e6fc7 | 106 | _nss_dns_getnetbyname_r (const char *name, struct netent *result, |
51eecc4a AJ |
107 | char *buffer, size_t buflen, int *errnop, |
108 | int *herrnop) | |
5f0e6fc7 RM |
109 | { |
110 | /* Return entry for network with NAME. */ | |
16b66062 AJ |
111 | union |
112 | { | |
113 | querybuf *buf; | |
114 | u_char *ptr; | |
115 | } net_buffer; | |
116 | querybuf *orig_net_buffer; | |
0420d888 | 117 | int anslen; |
6166815d | 118 | enum nss_status status; |
5f0e6fc7 | 119 | |
352f4ff9 FW |
120 | struct resolv_context *ctx = __resolv_context_get (); |
121 | if (ctx == NULL) | |
ea4924ce FW |
122 | { |
123 | *errnop = errno; | |
124 | *herrnop = NETDB_INTERNAL; | |
125 | return NSS_STATUS_UNAVAIL; | |
126 | } | |
b43b13ac | 127 | |
16b66062 | 128 | net_buffer.buf = orig_net_buffer = (querybuf *) alloca (1024); |
6166815d | 129 | |
352f4ff9 FW |
130 | anslen = __res_context_search |
131 | (ctx, name, C_IN, T_PTR, net_buffer.buf->buf, | |
132 | 1024, &net_buffer.ptr, NULL, NULL, NULL, NULL); | |
5f0e6fc7 | 133 | if (anslen < 0) |
67479a70 UD |
134 | { |
135 | /* Nothing found. */ | |
136 | *errnop = errno; | |
16b66062 AJ |
137 | if (net_buffer.buf != orig_net_buffer) |
138 | free (net_buffer.buf); | |
352f4ff9 | 139 | __resolv_context_put (ctx); |
67479a70 UD |
140 | return (errno == ECONNREFUSED |
141 | || errno == EPFNOSUPPORT | |
142 | || errno == EAFNOSUPPORT) | |
143 | ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND; | |
144 | } | |
5f0e6fc7 | 145 | |
2f1687b9 UD |
146 | status = getanswer_r (net_buffer.buf, anslen, result, buffer, buflen, |
147 | errnop, herrnop, BYNAME); | |
16b66062 AJ |
148 | if (net_buffer.buf != orig_net_buffer) |
149 | free (net_buffer.buf); | |
352f4ff9 | 150 | __resolv_context_put (ctx); |
6166815d | 151 | return status; |
5f0e6fc7 RM |
152 | } |
153 | ||
154 | ||
84384f5b | 155 | enum nss_status |
51eecc4a AJ |
156 | _nss_dns_getnetbyaddr_r (uint32_t net, int type, struct netent *result, |
157 | char *buffer, size_t buflen, int *errnop, | |
158 | int *herrnop) | |
5f0e6fc7 RM |
159 | { |
160 | /* Return entry for network with NAME. */ | |
84384f5b | 161 | enum nss_status status; |
16b66062 AJ |
162 | union |
163 | { | |
164 | querybuf *buf; | |
165 | u_char *ptr; | |
166 | } net_buffer; | |
167 | querybuf *orig_net_buffer; | |
5f0e6fc7 RM |
168 | unsigned int net_bytes[4]; |
169 | char qbuf[MAXDNAME]; | |
0420d888 | 170 | int cnt, anslen; |
d9fee042 | 171 | uint32_t net2; |
34816665 | 172 | int olderr = errno; |
5f0e6fc7 RM |
173 | |
174 | /* No net address lookup for IPv6 yet. */ | |
175 | if (type != AF_INET) | |
176 | return NSS_STATUS_UNAVAIL; | |
177 | ||
352f4ff9 FW |
178 | struct resolv_context *ctx = __resolv_context_get (); |
179 | if (ctx == NULL) | |
ea4924ce FW |
180 | { |
181 | *errnop = errno; | |
182 | *herrnop = NETDB_INTERNAL; | |
183 | return NSS_STATUS_UNAVAIL; | |
184 | } | |
51eecc4a | 185 | |
d9fee042 | 186 | net2 = (uint32_t) net; |
5f0e6fc7 RM |
187 | for (cnt = 4; net2 != 0; net2 >>= 8) |
188 | net_bytes[--cnt] = net2 & 0xff; | |
189 | ||
190 | switch (cnt) | |
191 | { | |
192 | case 3: | |
193 | /* Class A network. */ | |
194 | sprintf (qbuf, "0.0.0.%u.in-addr.arpa", net_bytes[3]); | |
195 | break; | |
196 | case 2: | |
197 | /* Class B network. */ | |
198 | sprintf (qbuf, "0.0.%u.%u.in-addr.arpa", net_bytes[3], net_bytes[2]); | |
199 | break; | |
200 | case 1: | |
201 | /* Class C network. */ | |
202 | sprintf (qbuf, "0.%u.%u.%u.in-addr.arpa", net_bytes[3], net_bytes[2], | |
203 | net_bytes[1]); | |
204 | break; | |
205 | case 0: | |
206 | /* Class D - E network. */ | |
207 | sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", net_bytes[3], net_bytes[2], | |
208 | net_bytes[1], net_bytes[0]); | |
209 | break; | |
210 | } | |
211 | ||
16b66062 | 212 | net_buffer.buf = orig_net_buffer = (querybuf *) alloca (1024); |
6166815d | 213 | |
352f4ff9 FW |
214 | anslen = __res_context_query (ctx, qbuf, C_IN, T_PTR, net_buffer.buf->buf, |
215 | 1024, &net_buffer.ptr, NULL, NULL, NULL, NULL); | |
5f0e6fc7 | 216 | if (anslen < 0) |
67479a70 UD |
217 | { |
218 | /* Nothing found. */ | |
34816665 UD |
219 | int err = errno; |
220 | __set_errno (olderr); | |
16b66062 AJ |
221 | if (net_buffer.buf != orig_net_buffer) |
222 | free (net_buffer.buf); | |
352f4ff9 | 223 | __resolv_context_put (ctx); |
34816665 UD |
224 | return (err == ECONNREFUSED |
225 | || err == EPFNOSUPPORT | |
226 | || err == EAFNOSUPPORT) | |
67479a70 UD |
227 | ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND; |
228 | } | |
5f0e6fc7 | 229 | |
2f1687b9 UD |
230 | status = getanswer_r (net_buffer.buf, anslen, result, buffer, buflen, |
231 | errnop, herrnop, BYADDR); | |
16b66062 AJ |
232 | if (net_buffer.buf != orig_net_buffer) |
233 | free (net_buffer.buf); | |
5f0e6fc7 RM |
234 | if (status == NSS_STATUS_SUCCESS) |
235 | { | |
236 | /* Strip trailing zeros. */ | |
237 | unsigned int u_net = net; /* Maybe net should be unsigned? */ | |
238 | ||
239 | while ((u_net & 0xff) == 0 && u_net != 0) | |
240 | u_net >>= 8; | |
241 | result->n_net = u_net; | |
242 | } | |
243 | ||
352f4ff9 | 244 | __resolv_context_put (ctx); |
5f0e6fc7 RM |
245 | return status; |
246 | } | |
247 | ||
248 | ||
84384f5b | 249 | static enum nss_status |
5f0e6fc7 | 250 | getanswer_r (const querybuf *answer, int anslen, struct netent *result, |
2f1687b9 UD |
251 | char *buffer, size_t buflen, int *errnop, int *h_errnop, |
252 | lookup_method net_i) | |
5f0e6fc7 RM |
253 | { |
254 | /* | |
255 | * Find first satisfactory answer | |
256 | * | |
257 | * answer --> +------------+ ( MESSAGE ) | |
258 | * | Header | | |
259 | * +------------+ | |
260 | * | Question | the question for the name server | |
261 | * +------------+ | |
262 | * | Answer | RRs answering the question | |
263 | * +------------+ | |
264 | * | Authority | RRs pointing toward an authority | |
265 | * | Additional | RRs holding additional information | |
266 | * +------------+ | |
267 | */ | |
268 | struct net_data | |
269 | { | |
270 | char *aliases[MAX_NR_ALIASES]; | |
271 | char linebuffer[0]; | |
2f1687b9 UD |
272 | } *net_data; |
273 | ||
274 | uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct net_data); | |
275 | buffer += pad; | |
276 | ||
a1ffb40e | 277 | if (__glibc_unlikely (buflen < sizeof (*net_data) + pad)) |
2f1687b9 UD |
278 | { |
279 | /* The buffer is too small. */ | |
280 | too_small: | |
281 | *errnop = ERANGE; | |
282 | *h_errnop = NETDB_INTERNAL; | |
283 | return NSS_STATUS_TRYAGAIN; | |
284 | } | |
285 | buflen -= pad; | |
286 | ||
287 | net_data = (struct net_data *) buffer; | |
5f0e6fc7 | 288 | int linebuflen = buflen - offsetof (struct net_data, linebuffer); |
2f1687b9 UD |
289 | if (buflen - offsetof (struct net_data, linebuffer) != linebuflen) |
290 | linebuflen = INT_MAX; | |
9cfe5381 | 291 | const unsigned char *end_of_message = &answer->buf[anslen]; |
5f0e6fc7 RM |
292 | const HEADER *header_pointer = &answer->hdr; |
293 | /* #/records in the answer section. */ | |
294 | int answer_count = ntohs (header_pointer->ancount); | |
295 | /* #/entries in the question section. */ | |
296 | int question_count = ntohs (header_pointer->qdcount); | |
297 | char *bp = net_data->linebuffer; | |
9cfe5381 | 298 | const unsigned char *cp = &answer->buf[HFIXEDSZ]; |
5f0e6fc7 RM |
299 | char **alias_pointer; |
300 | int have_answer; | |
8d8c6efa | 301 | u_char packtmp[NS_MAXCDNAME]; |
5f0e6fc7 RM |
302 | |
303 | if (question_count == 0) | |
7ef90c15 UD |
304 | { |
305 | /* FIXME: the Sun version uses for host name lookup an additional | |
306 | parameter for pointing to h_errno. this is missing here. | |
307 | OSF/1 has a per-thread h_errno variable. */ | |
308 | if (header_pointer->aa != 0) | |
309 | { | |
310 | __set_h_errno (HOST_NOT_FOUND); | |
311 | return NSS_STATUS_NOTFOUND; | |
312 | } | |
313 | else | |
314 | { | |
315 | __set_h_errno (TRY_AGAIN); | |
316 | return NSS_STATUS_TRYAGAIN; | |
317 | } | |
318 | } | |
5f0e6fc7 RM |
319 | |
320 | /* Skip the question part. */ | |
321 | while (question_count-- > 0) | |
9b57c1c1 RM |
322 | { |
323 | int n = __dn_skipname (cp, end_of_message); | |
324 | if (n < 0 || end_of_message - (cp + n) < QFIXEDSZ) | |
325 | { | |
326 | __set_h_errno (NO_RECOVERY); | |
327 | return NSS_STATUS_UNAVAIL; | |
328 | } | |
329 | cp += n + QFIXEDSZ; | |
330 | } | |
5f0e6fc7 RM |
331 | |
332 | alias_pointer = result->n_aliases = &net_data->aliases[0]; | |
333 | *alias_pointer = NULL; | |
334 | have_answer = 0; | |
5f0e6fc7 RM |
335 | |
336 | while (--answer_count >= 0 && cp < end_of_message) | |
337 | { | |
75b3a15e FW |
338 | int n = __ns_name_unpack (answer->buf, end_of_message, cp, |
339 | packtmp, sizeof packtmp); | |
8d8c6efa UD |
340 | if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1) |
341 | { | |
342 | if (errno == EMSGSIZE) | |
2f1687b9 | 343 | goto too_small; |
8d8c6efa UD |
344 | |
345 | n = -1; | |
346 | } | |
347 | ||
5f0e6fc7 RM |
348 | if (n < 0 || res_dnok (bp) == 0) |
349 | break; | |
350 | cp += n; | |
a12f9431 FW |
351 | |
352 | if (end_of_message - cp < 10) | |
353 | { | |
354 | __set_h_errno (NO_RECOVERY); | |
355 | return NSS_STATUS_UNAVAIL; | |
356 | } | |
357 | ||
75b3a15e | 358 | int type, class; |
5f0e6fc7 RM |
359 | GETSHORT (type, cp); |
360 | GETSHORT (class, cp); | |
361 | cp += INT32SZ; /* TTL */ | |
a12f9431 FW |
362 | uint16_t rdatalen; |
363 | GETSHORT (rdatalen, cp); | |
364 | if (end_of_message - cp < rdatalen) | |
365 | { | |
366 | __set_h_errno (NO_RECOVERY); | |
367 | return NSS_STATUS_UNAVAIL; | |
368 | } | |
5f0e6fc7 RM |
369 | |
370 | if (class == C_IN && type == T_PTR) | |
371 | { | |
8d8c6efa UD |
372 | n = __ns_name_unpack (answer->buf, end_of_message, cp, |
373 | packtmp, sizeof packtmp); | |
374 | if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1) | |
375 | { | |
376 | if (errno == EMSGSIZE) | |
2f1687b9 | 377 | goto too_small; |
8d8c6efa UD |
378 | |
379 | n = -1; | |
380 | } | |
381 | ||
5f0e6fc7 RM |
382 | if (n < 0 || !res_hnok (bp)) |
383 | { | |
384 | /* XXX What does this mean? The original form from bind | |
385 | returns NULL. Incrementing cp has no effect in any case. | |
386 | What should I return here. ??? */ | |
387 | cp += n; | |
388 | return NSS_STATUS_UNAVAIL; | |
389 | } | |
a12f9431 | 390 | cp += rdatalen; |
9b57c1c1 RM |
391 | if (alias_pointer + 2 < &net_data->aliases[MAX_NR_ALIASES]) |
392 | { | |
393 | *alias_pointer++ = bp; | |
394 | n = strlen (bp) + 1; | |
395 | bp += n; | |
396 | linebuflen -= n; | |
397 | result->n_addrtype = class == C_IN ? AF_INET : AF_UNSPEC; | |
398 | ++have_answer; | |
399 | } | |
5f0e6fc7 | 400 | } |
a12f9431 FW |
401 | else |
402 | /* Skip over unknown record data. */ | |
403 | cp += rdatalen; | |
5f0e6fc7 RM |
404 | } |
405 | ||
406 | if (have_answer) | |
407 | { | |
5f0e6fc7 RM |
408 | *alias_pointer = NULL; |
409 | switch (net_i) | |
410 | { | |
411 | case BYADDR: | |
0bf5c050 | 412 | result->n_name = *result->n_aliases++; |
5f0e6fc7 | 413 | result->n_net = 0L; |
0bf5c050 | 414 | return NSS_STATUS_SUCCESS; |
5f0e6fc7 | 415 | |
0bf5c050 RM |
416 | case BYNAME: |
417 | { | |
11e3417a FW |
418 | char **ap; |
419 | for (ap = result->n_aliases; *ap != NULL; ++ap) | |
0bf5c050 RM |
420 | { |
421 | /* Check each alias name for being of the forms: | |
422 | 4.3.2.1.in-addr.arpa = net 1.2.3.4 | |
423 | 3.2.1.in-addr.arpa = net 0.1.2.3 | |
424 | 2.1.in-addr.arpa = net 0.0.1.2 | |
425 | 1.in-addr.arpa = net 0.0.0.1 | |
426 | */ | |
427 | uint32_t val = 0; /* Accumulator for n_net value. */ | |
428 | unsigned int shift = 0; /* Which part we are parsing now. */ | |
429 | const char *p = *ap; /* Consuming the string. */ | |
430 | do | |
431 | { | |
432 | /* Match the leading 0 or 0[xX] base indicator. */ | |
433 | unsigned int base = 10; | |
434 | if (*p == '0' && p[1] != '.') | |
435 | { | |
436 | base = 8; | |
437 | ++p; | |
438 | if (*p == 'x' || *p == 'X') | |
439 | { | |
440 | base = 16; | |
441 | ++p; | |
442 | if (*p == '.') | |
443 | break; /* No digit here. Give up on alias. */ | |
444 | } | |
445 | if (*p == '\0') | |
446 | break; | |
447 | } | |
448 | ||
449 | uint32_t part = 0; /* Accumulates this part's number. */ | |
450 | do | |
451 | { | |
452 | if (isdigit (*p) && (*p - '0' < base)) | |
453 | part = (part * base) + (*p - '0'); | |
454 | else if (base == 16 && isxdigit (*p)) | |
455 | part = (part << 4) + 10 + (tolower (*p) - 'a'); | |
456 | ++p; | |
457 | } while (*p != '\0' && *p != '.'); | |
458 | ||
459 | if (*p != '.') | |
460 | break; /* Bad form. Give up on this name. */ | |
461 | ||
462 | /* Install this as the next more significant byte. */ | |
463 | val |= part << shift; | |
464 | shift += 8; | |
465 | ++p; | |
466 | ||
467 | /* If we are out of digits now, there are two cases: | |
468 | 1. We are done with digits and now see "in-addr.arpa". | |
469 | 2. This is not the droid we are looking for. */ | |
470 | if (!isdigit (*p) && !strcasecmp (p, "in-addr.arpa")) | |
471 | { | |
472 | result->n_net = val; | |
473 | return NSS_STATUS_SUCCESS; | |
474 | } | |
475 | ||
476 | /* Keep going when we have seen fewer than 4 parts. */ | |
477 | } while (shift < 32); | |
478 | } | |
479 | } | |
5f0e6fc7 RM |
480 | break; |
481 | } | |
5f0e6fc7 RM |
482 | } |
483 | ||
a68b0d31 | 484 | __set_h_errno (TRY_AGAIN); |
5f0e6fc7 RM |
485 | return NSS_STATUS_TRYAGAIN; |
486 | } |