1/* Copyright (C) 1996-2018 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 <http://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/* Maximum number of aliases we allow. */
74#define MAX_NR_ALIASES 48
75
76
77#if PACKETSZ > 65536
78# define MAXPACKET PACKETSZ
79#else
80# define MAXPACKET 65536
81#endif
82
83
84typedef enum
85{
86 BYADDR,
87 BYNAME
88} lookup_method;
89
90
91/* We need this time later. */
92typedef union querybuf
93{
94 HEADER hdr;
95 u_char buf[MAXPACKET];
96} querybuf;
97
98/* Prototypes for local functions. */
99static enum nss_status getanswer_r (const querybuf *answer, int anslen,
100 struct netent *result, char *buffer,
101 size_t buflen, int *errnop, int *h_errnop,
102 lookup_method net_i);
103
104
105enum nss_status
106_nss_dns_getnetbyname_r (const char *name, struct netent *result,
107 char *buffer, size_t buflen, int *errnop,
108 int *herrnop)
109{
110 /* Return entry for network with NAME. */
111 union
112 {
113 querybuf *buf;
114 u_char *ptr;
115 } net_buffer;
116 querybuf *orig_net_buffer;
117 int anslen;
118 enum nss_status status;
119
120 struct resolv_context *ctx = __resolv_context_get ();
121 if (ctx == NULL)
122 {
123 *errnop = errno;
124 *herrnop = NETDB_INTERNAL;
125 return NSS_STATUS_UNAVAIL;
126 }
127
128 net_buffer.buf = orig_net_buffer = (querybuf *) alloca (1024);
129
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);
133 if (anslen < 0)
134 {
135 /* Nothing found. */
136 *errnop = errno;
137 if (net_buffer.buf != orig_net_buffer)
138 free (net_buffer.buf);
139 __resolv_context_put (ctx);
140 return (errno == ECONNREFUSED
141 || errno == EPFNOSUPPORT
142 || errno == EAFNOSUPPORT)
143 ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
144 }
145
146 status = getanswer_r (net_buffer.buf, anslen, result, buffer, buflen,
147 errnop, herrnop, BYNAME);
148 if (net_buffer.buf != orig_net_buffer)
149 free (net_buffer.buf);
150 __resolv_context_put (ctx);
151 return status;
152}
153
154
155enum nss_status
156_nss_dns_getnetbyaddr_r (uint32_t net, int type, struct netent *result,
157 char *buffer, size_t buflen, int *errnop,
158 int *herrnop)
159{
160 /* Return entry for network with NAME. */
161 enum nss_status status;
162 union
163 {
164 querybuf *buf;
165 u_char *ptr;
166 } net_buffer;
167 querybuf *orig_net_buffer;
168 unsigned int net_bytes[4];
169 char qbuf[MAXDNAME];
170 int cnt, anslen;
171 uint32_t net2;
172 int olderr = errno;
173
174 /* No net address lookup for IPv6 yet. */
175 if (type != AF_INET)
176 return NSS_STATUS_UNAVAIL;
177
178 struct resolv_context *ctx = __resolv_context_get ();
179 if (ctx == NULL)
180 {
181 *errnop = errno;
182 *herrnop = NETDB_INTERNAL;
183 return NSS_STATUS_UNAVAIL;
184 }
185
186 net2 = (uint32_t) net;
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
212 net_buffer.buf = orig_net_buffer = (querybuf *) alloca (1024);
213
214 anslen = __res_context_query (ctx, qbuf, C_IN, T_PTR, net_buffer.buf->buf,
215 1024, &net_buffer.ptr, NULL, NULL, NULL, NULL);
216 if (anslen < 0)
217 {
218 /* Nothing found. */
219 int err = errno;
220 __set_errno (olderr);
221 if (net_buffer.buf != orig_net_buffer)
222 free (net_buffer.buf);
223 __resolv_context_put (ctx);
224 return (err == ECONNREFUSED
225 || err == EPFNOSUPPORT
226 || err == EAFNOSUPPORT)
227 ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
228 }
229
230 status = getanswer_r (net_buffer.buf, anslen, result, buffer, buflen,
231 errnop, herrnop, BYADDR);
232 if (net_buffer.buf != orig_net_buffer)
233 free (net_buffer.buf);
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
244 __resolv_context_put (ctx);
245 return status;
246}
247
248
249static enum nss_status
250getanswer_r (const querybuf *answer, int anslen, struct netent *result,
251 char *buffer, size_t buflen, int *errnop, int *h_errnop,
252 lookup_method net_i)
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];
272 } *net_data;
273
274 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct net_data);
275 buffer += pad;
276
277 if (__glibc_unlikely (buflen < sizeof (*net_data) + pad))
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;
288 int linebuflen = buflen - offsetof (struct net_data, linebuffer);
289 if (buflen - offsetof (struct net_data, linebuffer) != linebuflen)
290 linebuflen = INT_MAX;
291 const unsigned char *end_of_message = &answer->buf[anslen];
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;
298 const unsigned char *cp = &answer->buf[HFIXEDSZ];
299 char **alias_pointer;
300 int have_answer;
301 u_char packtmp[NS_MAXCDNAME];
302
303 if (question_count == 0)
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 }
319
320 /* Skip the question part. */
321 while (question_count-- > 0)
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 }
331
332 alias_pointer = result->n_aliases = &net_data->aliases[0];
333 *alias_pointer = NULL;
334 have_answer = 0;
335
336 while (--answer_count >= 0 && cp < end_of_message)
337 {
338 int n = __ns_name_unpack (answer->buf, end_of_message, cp,
339 packtmp, sizeof packtmp);
340 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
341 {
342 if (errno == EMSGSIZE)
343 goto too_small;
344
345 n = -1;
346 }
347
348 if (n > 0 && bp[0] == '.')
349 bp[0] = '\0';
350
351 if (n < 0 || res_dnok (bp) == 0)
352 break;
353 cp += n;
354
355 if (end_of_message - cp < 10)
356 {
357 __set_h_errno (NO_RECOVERY);
358 return NSS_STATUS_UNAVAIL;
359 }
360
361 int type, class;
362 GETSHORT (type, cp);
363 GETSHORT (class, cp);
364 cp += INT32SZ; /* TTL */
365 uint16_t rdatalen;
366 GETSHORT (rdatalen, cp);
367 if (end_of_message - cp < rdatalen)
368 {
369 __set_h_errno (NO_RECOVERY);
370 return NSS_STATUS_UNAVAIL;
371 }
372
373 if (class == C_IN && type == T_PTR)
374 {
375 n = __ns_name_unpack (answer->buf, end_of_message, cp,
376 packtmp, sizeof packtmp);
377 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
378 {
379 if (errno == EMSGSIZE)
380 goto too_small;
381
382 n = -1;
383 }
384
385 if (n < 0 || !res_hnok (bp))
386 {
387 /* XXX What does this mean? The original form from bind
388 returns NULL. Incrementing cp has no effect in any case.
389 What should I return here. ??? */
390 cp += n;
391 return NSS_STATUS_UNAVAIL;
392 }
393 cp += rdatalen;
394 if (alias_pointer + 2 < &net_data->aliases[MAX_NR_ALIASES])
395 {
396 *alias_pointer++ = bp;
397 n = strlen (bp) + 1;
398 bp += n;
399 linebuflen -= n;
400 result->n_addrtype = class == C_IN ? AF_INET : AF_UNSPEC;
401 ++have_answer;
402 }
403 }
404 else
405 /* Skip over unknown record data. */
406 cp += rdatalen;
407 }
408
409 if (have_answer)
410 {
411 *alias_pointer = NULL;
412 switch (net_i)
413 {
414 case BYADDR:
415 result->n_name = *result->n_aliases++;
416 result->n_net = 0L;
417 return NSS_STATUS_SUCCESS;
418
419 case BYNAME:
420 {
421 char **ap;
422 for (ap = result->n_aliases; *ap != NULL; ++ap)
423 {
424 /* Check each alias name for being of the forms:
425 4.3.2.1.in-addr.arpa = net 1.2.3.4
426 3.2.1.in-addr.arpa = net 0.1.2.3
427 2.1.in-addr.arpa = net 0.0.1.2
428 1.in-addr.arpa = net 0.0.0.1
429 */
430 uint32_t val = 0; /* Accumulator for n_net value. */
431 unsigned int shift = 0; /* Which part we are parsing now. */
432 const char *p = *ap; /* Consuming the string. */
433 do
434 {
435 /* Match the leading 0 or 0[xX] base indicator. */
436 unsigned int base = 10;
437 if (*p == '0' && p[1] != '.')
438 {
439 base = 8;
440 ++p;
441 if (*p == 'x' || *p == 'X')
442 {
443 base = 16;
444 ++p;
445 if (*p == '.')
446 break; /* No digit here. Give up on alias. */
447 }
448 if (*p == '\0')
449 break;
450 }
451
452 uint32_t part = 0; /* Accumulates this part's number. */
453 do
454 {
455 if (isdigit (*p) && (*p - '0' < base))
456 part = (part * base) + (*p - '0');
457 else if (base == 16 && isxdigit (*p))
458 part = (part << 4) + 10 + (tolower (*p) - 'a');
459 ++p;
460 } while (*p != '\0' && *p != '.');
461
462 if (*p != '.')
463 break; /* Bad form. Give up on this name. */
464
465 /* Install this as the next more significant byte. */
466 val |= part << shift;
467 shift += 8;
468 ++p;
469
470 /* If we are out of digits now, there are two cases:
471 1. We are done with digits and now see "in-addr.arpa".
472 2. This is not the droid we are looking for. */
473 if (!isdigit (*p) && !strcasecmp (p, "in-addr.arpa"))
474 {
475 result->n_net = val;
476 return NSS_STATUS_SUCCESS;
477 }
478
479 /* Keep going when we have seen fewer than 4 parts. */
480 } while (shift < 32);
481 }
482 }
483 break;
484 }
485 }
486
487 __set_h_errno (TRY_AGAIN);
488 return NSS_STATUS_TRYAGAIN;
489}
490