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