]> git.ipfire.org Git - thirdparty/glibc.git/blame - resolv/nss_dns/dns-network.c
.
[thirdparty/glibc.git] / resolv / nss_dns / dns-network.c
CommitLineData
32c075e1 1/* Copyright (C) 1996, 1997, 1998, 1999, 2002, 2004, 2007
87bb6b6c 2 Free Software Foundation, Inc.
84384f5b
UD
3 This file is part of the GNU C Library.
4 Extended from original form by Ulrich Drepper <drepper@cygnus.com>, 1996.
5f0e6fc7 5
84384f5b 6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
5f0e6fc7 10
84384f5b
UD
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 14 Lesser General Public License for more details.
5f0e6fc7 15
41bdb6e2
AJ
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
5f0e6fc7
RM
20
21/* Parts of this file are plain copies of the file `getnetnamadr.c' from
22 the bind package and it has the following copyright. */
23
24/* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
25 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
26 *
27 * Permission to use, copy, modify, and distribute this software for any
28 * purpose with or without fee is hereby granted, provided that the above
29 * copyright notice and this permission notice appear in all copies.
30 */
31/*
32 * Copyright (c) 1983, 1993
33 * The Regents of the University of California. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
5f0e6fc7
RM
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 */
59
60#include <ctype.h>
61#include <errno.h>
62#include <netdb.h>
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.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
870d19de
RM
74#if PACKETSZ > 65536
75# define MAXPACKET PACKETSZ
5f0e6fc7 76#else
870d19de 77# define MAXPACKET 65536
5f0e6fc7
RM
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
8d8c6efa
UD
95/* These functions are defined in res_comp.c. */
96#define NS_MAXCDNAME 255 /* maximum compressed domain name */
79937577
UD
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;
8d8c6efa 100
5f0e6fc7 101
6d52618b 102/* Prototypes for local functions. */
84384f5b
UD
103static enum nss_status getanswer_r (const querybuf *answer, int anslen,
104 struct netent *result, char *buffer,
32c075e1
JJ
105 size_t buflen, int *errnop, int *h_errnop,
106 lookup_method net_i);
5f0e6fc7
RM
107
108
84384f5b 109enum nss_status
5f0e6fc7 110_nss_dns_getnetbyname_r (const char *name, struct netent *result,
51eecc4a
AJ
111 char *buffer, size_t buflen, int *errnop,
112 int *herrnop)
5f0e6fc7
RM
113{
114 /* Return entry for network with NAME. */
16b66062
AJ
115 union
116 {
117 querybuf *buf;
118 u_char *ptr;
119 } net_buffer;
120 querybuf *orig_net_buffer;
0420d888 121 int anslen;
5f0e6fc7 122 char *qbuf;
6166815d 123 enum nss_status status;
5f0e6fc7 124
87bb6b6c 125 if (__res_maybe_init (&_res, 0) == -1)
b43b13ac
UD
126 return NSS_STATUS_UNAVAIL;
127
5f0e6fc7 128 qbuf = strdupa (name);
6166815d 129
16b66062 130 net_buffer.buf = orig_net_buffer = (querybuf *) alloca (1024);
6166815d 131
16b66062
AJ
132 anslen = __libc_res_nsearch (&_res, qbuf, C_IN, T_PTR, net_buffer.buf->buf,
133 1024, &net_buffer.ptr);
5f0e6fc7 134 if (anslen < 0)
67479a70
UD
135 {
136 /* Nothing found. */
137 *errnop = errno;
16b66062
AJ
138 if (net_buffer.buf != orig_net_buffer)
139 free (net_buffer.buf);
67479a70
UD
140 return (errno == ECONNREFUSED
141 || errno == EPFNOSUPPORT
142 || errno == EAFNOSUPPORT)
143 ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
144 }
5f0e6fc7 145
32c075e1
JJ
146 status = getanswer_r (net_buffer.buf, anslen, result, buffer, buflen,
147 errnop, herrnop, BYNAME);
16b66062
AJ
148 if (net_buffer.buf != orig_net_buffer)
149 free (net_buffer.buf);
6166815d 150 return status;
5f0e6fc7
RM
151}
152
153
84384f5b 154enum nss_status
51eecc4a
AJ
155_nss_dns_getnetbyaddr_r (uint32_t net, int type, struct netent *result,
156 char *buffer, size_t buflen, int *errnop,
157 int *herrnop)
5f0e6fc7
RM
158{
159 /* Return entry for network with NAME. */
84384f5b 160 enum nss_status status;
16b66062
AJ
161 union
162 {
163 querybuf *buf;
164 u_char *ptr;
165 } net_buffer;
166 querybuf *orig_net_buffer;
5f0e6fc7
RM
167 unsigned int net_bytes[4];
168 char qbuf[MAXDNAME];
0420d888 169 int cnt, anslen;
5f0e6fc7 170 u_int32_t net2;
34816665 171 int olderr = errno;
5f0e6fc7
RM
172
173 /* No net address lookup for IPv6 yet. */
174 if (type != AF_INET)
175 return NSS_STATUS_UNAVAIL;
176
87bb6b6c 177 if (__res_maybe_init (&_res, 0) == -1)
b43b13ac 178 return NSS_STATUS_UNAVAIL;
51eecc4a 179
5f0e6fc7
RM
180 net2 = (u_int32_t) net;
181 for (cnt = 4; net2 != 0; net2 >>= 8)
182 net_bytes[--cnt] = net2 & 0xff;
183
184 switch (cnt)
185 {
186 case 3:
187 /* Class A network. */
188 sprintf (qbuf, "0.0.0.%u.in-addr.arpa", net_bytes[3]);
189 break;
190 case 2:
191 /* Class B network. */
192 sprintf (qbuf, "0.0.%u.%u.in-addr.arpa", net_bytes[3], net_bytes[2]);
193 break;
194 case 1:
195 /* Class C network. */
196 sprintf (qbuf, "0.%u.%u.%u.in-addr.arpa", net_bytes[3], net_bytes[2],
197 net_bytes[1]);
198 break;
199 case 0:
200 /* Class D - E network. */
201 sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", net_bytes[3], net_bytes[2],
202 net_bytes[1], net_bytes[0]);
203 break;
204 }
205
16b66062 206 net_buffer.buf = orig_net_buffer = (querybuf *) alloca (1024);
6166815d 207
16b66062
AJ
208 anslen = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, net_buffer.buf->buf,
209 1024, &net_buffer.ptr);
5f0e6fc7 210 if (anslen < 0)
67479a70
UD
211 {
212 /* Nothing found. */
34816665
UD
213 int err = errno;
214 __set_errno (olderr);
16b66062
AJ
215 if (net_buffer.buf != orig_net_buffer)
216 free (net_buffer.buf);
34816665
UD
217 return (err == ECONNREFUSED
218 || err == EPFNOSUPPORT
219 || err == EAFNOSUPPORT)
67479a70
UD
220 ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
221 }
5f0e6fc7 222
32c075e1
JJ
223 status = getanswer_r (net_buffer.buf, anslen, result, buffer, buflen,
224 errnop, herrnop, BYADDR);
16b66062
AJ
225 if (net_buffer.buf != orig_net_buffer)
226 free (net_buffer.buf);
5f0e6fc7
RM
227 if (status == NSS_STATUS_SUCCESS)
228 {
229 /* Strip trailing zeros. */
230 unsigned int u_net = net; /* Maybe net should be unsigned? */
231
232 while ((u_net & 0xff) == 0 && u_net != 0)
233 u_net >>= 8;
234 result->n_net = u_net;
235 }
236
237 return status;
238}
239
240
241#undef offsetof
242#define offsetof(Type, Member) ((size_t) &((Type *) NULL)->Member)
243
84384f5b 244static enum nss_status
5f0e6fc7 245getanswer_r (const querybuf *answer, int anslen, struct netent *result,
32c075e1
JJ
246 char *buffer, size_t buflen, int *errnop, int *h_errnop,
247 lookup_method net_i)
5f0e6fc7
RM
248{
249 /*
250 * Find first satisfactory answer
251 *
252 * answer --> +------------+ ( MESSAGE )
253 * | Header |
254 * +------------+
255 * | Question | the question for the name server
256 * +------------+
257 * | Answer | RRs answering the question
258 * +------------+
259 * | Authority | RRs pointing toward an authority
260 * | Additional | RRs holding additional information
261 * +------------+
262 */
263 struct net_data
264 {
265 char *aliases[MAX_NR_ALIASES];
266 char linebuffer[0];
32c075e1
JJ
267 } *net_data;
268
269 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct net_data);
270 buffer += pad;
271
272 if (__builtin_expect (buflen < sizeof (*net_data) + pad, 0))
273 {
274 /* The buffer is too small. */
275 too_small:
276 *errnop = ERANGE;
277 *h_errnop = NETDB_INTERNAL;
278 return NSS_STATUS_TRYAGAIN;
279 }
280 buflen -= pad;
281
282 net_data = (struct net_data *) buffer;
5f0e6fc7 283 int linebuflen = buflen - offsetof (struct net_data, linebuffer);
32c075e1
JJ
284 if (buflen - offsetof (struct net_data, linebuffer) != linebuflen)
285 linebuflen = INT_MAX;
9cfe5381 286 const unsigned char *end_of_message = &answer->buf[anslen];
5f0e6fc7
RM
287 const HEADER *header_pointer = &answer->hdr;
288 /* #/records in the answer section. */
289 int answer_count = ntohs (header_pointer->ancount);
290 /* #/entries in the question section. */
291 int question_count = ntohs (header_pointer->qdcount);
292 char *bp = net_data->linebuffer;
9cfe5381 293 const unsigned char *cp = &answer->buf[HFIXEDSZ];
5f0e6fc7
RM
294 char **alias_pointer;
295 int have_answer;
296 char *ans;
8d8c6efa 297 u_char packtmp[NS_MAXCDNAME];
5f0e6fc7
RM
298
299 if (question_count == 0)
7ef90c15
UD
300 {
301 /* FIXME: the Sun version uses for host name lookup an additional
302 parameter for pointing to h_errno. this is missing here.
303 OSF/1 has a per-thread h_errno variable. */
304 if (header_pointer->aa != 0)
305 {
306 __set_h_errno (HOST_NOT_FOUND);
307 return NSS_STATUS_NOTFOUND;
308 }
309 else
310 {
311 __set_h_errno (TRY_AGAIN);
312 return NSS_STATUS_TRYAGAIN;
313 }
314 }
5f0e6fc7
RM
315
316 /* Skip the question part. */
317 while (question_count-- > 0)
9b57c1c1
RM
318 {
319 int n = __dn_skipname (cp, end_of_message);
320 if (n < 0 || end_of_message - (cp + n) < QFIXEDSZ)
321 {
322 __set_h_errno (NO_RECOVERY);
323 return NSS_STATUS_UNAVAIL;
324 }
325 cp += n + QFIXEDSZ;
326 }
5f0e6fc7
RM
327
328 alias_pointer = result->n_aliases = &net_data->aliases[0];
329 *alias_pointer = NULL;
330 have_answer = 0;
331 ans = NULL;
332
333 while (--answer_count >= 0 && cp < end_of_message)
334 {
335 int n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen);
336 int type, class;
337
8d8c6efa
UD
338 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)
32c075e1 343 goto too_small;
8d8c6efa
UD
344
345 n = -1;
346 }
347
348 if (n > 0 && bp[0] == '.')
349 bp[0] = '\0';
350
5f0e6fc7
RM
351 if (n < 0 || res_dnok (bp) == 0)
352 break;
353 cp += n;
354 ans = strdupa (bp);
355 GETSHORT (type, cp);
356 GETSHORT (class, cp);
357 cp += INT32SZ; /* TTL */
358 GETSHORT (n, cp);
359
360 if (class == C_IN && type == T_PTR)
361 {
8d8c6efa
UD
362 n = __ns_name_unpack (answer->buf, end_of_message, cp,
363 packtmp, sizeof packtmp);
364 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
365 {
366 if (errno == EMSGSIZE)
32c075e1 367 goto too_small;
8d8c6efa
UD
368
369 n = -1;
370 }
371
5f0e6fc7
RM
372 if (n < 0 || !res_hnok (bp))
373 {
374 /* XXX What does this mean? The original form from bind
375 returns NULL. Incrementing cp has no effect in any case.
376 What should I return here. ??? */
377 cp += n;
378 return NSS_STATUS_UNAVAIL;
379 }
380 cp += n;
9b57c1c1
RM
381 if (alias_pointer + 2 < &net_data->aliases[MAX_NR_ALIASES])
382 {
383 *alias_pointer++ = bp;
384 n = strlen (bp) + 1;
385 bp += n;
386 linebuflen -= n;
387 result->n_addrtype = class == C_IN ? AF_INET : AF_UNSPEC;
388 ++have_answer;
389 }
5f0e6fc7
RM
390 }
391 }
392
393 if (have_answer)
394 {
5f0e6fc7
RM
395 *alias_pointer = NULL;
396 switch (net_i)
397 {
398 case BYADDR:
0bf5c050 399 result->n_name = *result->n_aliases++;
5f0e6fc7 400 result->n_net = 0L;
0bf5c050 401 return NSS_STATUS_SUCCESS;
5f0e6fc7 402
0bf5c050
RM
403 case BYNAME:
404 {
405 char **ap = result->n_aliases++;
406 while (*ap != NULL)
407 {
408 /* Check each alias name for being of the forms:
409 4.3.2.1.in-addr.arpa = net 1.2.3.4
410 3.2.1.in-addr.arpa = net 0.1.2.3
411 2.1.in-addr.arpa = net 0.0.1.2
412 1.in-addr.arpa = net 0.0.0.1
413 */
414 uint32_t val = 0; /* Accumulator for n_net value. */
415 unsigned int shift = 0; /* Which part we are parsing now. */
416 const char *p = *ap; /* Consuming the string. */
417 do
418 {
419 /* Match the leading 0 or 0[xX] base indicator. */
420 unsigned int base = 10;
421 if (*p == '0' && p[1] != '.')
422 {
423 base = 8;
424 ++p;
425 if (*p == 'x' || *p == 'X')
426 {
427 base = 16;
428 ++p;
429 if (*p == '.')
430 break; /* No digit here. Give up on alias. */
431 }
432 if (*p == '\0')
433 break;
434 }
435
436 uint32_t part = 0; /* Accumulates this part's number. */
437 do
438 {
439 if (isdigit (*p) && (*p - '0' < base))
440 part = (part * base) + (*p - '0');
441 else if (base == 16 && isxdigit (*p))
442 part = (part << 4) + 10 + (tolower (*p) - 'a');
443 ++p;
444 } while (*p != '\0' && *p != '.');
445
446 if (*p != '.')
447 break; /* Bad form. Give up on this name. */
448
449 /* Install this as the next more significant byte. */
450 val |= part << shift;
451 shift += 8;
452 ++p;
453
454 /* If we are out of digits now, there are two cases:
455 1. We are done with digits and now see "in-addr.arpa".
456 2. This is not the droid we are looking for. */
457 if (!isdigit (*p) && !strcasecmp (p, "in-addr.arpa"))
458 {
459 result->n_net = val;
460 return NSS_STATUS_SUCCESS;
461 }
462
463 /* Keep going when we have seen fewer than 4 parts. */
464 } while (shift < 32);
465 }
466 }
5f0e6fc7
RM
467 break;
468 }
5f0e6fc7
RM
469 }
470
a68b0d31 471 __set_h_errno (TRY_AGAIN);
5f0e6fc7
RM
472 return NSS_STATUS_TRYAGAIN;
473}