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