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