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