]> git.ipfire.org Git - thirdparty/glibc.git/blame - resolv/nss_dns/dns-host.c
Update.
[thirdparty/glibc.git] / resolv / nss_dns / dns-host.c
CommitLineData
da2d1bc5 1/* Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
84384f5b
UD
2 This file is part of the GNU C Library.
3 Extended from original form by Ulrich Drepper <drepper@cygnus.com>, 1996.
5f0e6fc7 4
84384f5b
UD
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
5f0e6fc7 9
84384f5b
UD
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 Library General Public License for more details.
5f0e6fc7 14
84384f5b
UD
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
5f0e6fc7 19
df21c858 20/* Parts of this file are plain copies of the file `gethtnamadr.c' from
5f0e6fc7
RM
21 the bind package and it has the following copyright. */
22
5f0e6fc7 23/*
df21c858
UD
24 * ++Copyright++ 1985, 1988, 1993
25 * -
26 * Copyright (c) 1985, 1988, 1993
27 * The Regents of the University of California. All rights reserved.
5f0e6fc7
RM
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
df21c858
UD
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
5f0e6fc7
RM
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.
df21c858
UD
56 * -
57 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
58 *
59 * Permission to use, copy, modify, and distribute this software for any
60 * purpose with or without fee is hereby granted, provided that the above
61 * copyright notice and this permission notice appear in all copies, and that
62 * the name of Digital Equipment Corporation not be used in advertising or
63 * publicity pertaining to distribution of the document or software without
64 * specific, written prior permission.
65 *
66 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
67 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
68 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
69 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
70 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
71 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
72 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
73 * SOFTWARE.
74 * -
75 * --Copyright--
5f0e6fc7
RM
76 */
77
78#include <ctype.h>
79#include <errno.h>
80#include <netdb.h>
81#include <stdio.h>
82#include <stdlib.h>
83#include <stddef.h>
84#include <string.h>
85#include <sys/syslog.h>
86
5f0e6fc7
RM
87#include "nsswitch.h"
88
89/* Get implementation for some internal functions. */
da2d1bc5
UD
90#include <resolv/mapv4v6addr.h>
91#include <resolv/mapv4v6hostent.h>
5f0e6fc7
RM
92
93/* Maximum number of aliases we allow. */
94#define MAX_NR_ALIASES 48
95#define MAX_NR_ADDRS 48
96
97#if PACKETSZ > 1024
98# define MAXPACKET PACKETSZ
99#else
100# define MAXPACKET 1024
101#endif
1522c368
UD
102/* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
103#ifdef MAXHOSTNAMELEN
104# undef MAXHOSTNAMELEN
105#endif
106#define MAXHOSTNAMELEN 256
5f0e6fc7
RM
107
108static const char AskedForGot[] = "\
109gethostby*.getanswer: asked for \"%s\", got \"%s\"";
110
111
112/* We need this time later. */
113typedef union querybuf
114{
115 HEADER hdr;
116 u_char buf[MAXPACKET];
117} querybuf;
118
119
120static enum nss_status getanswer_r (const querybuf *answer, int anslen,
121 const char *qname, int qtype,
df4ef2ab
UD
122 struct hostent *result, char *buffer,
123 size_t buflen, int *h_errnop);
5f0e6fc7
RM
124
125enum nss_status
126_nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result,
df4ef2ab 127 char *buffer, size_t buflen, int *h_errnop)
5f0e6fc7 128{
5f0e6fc7
RM
129 querybuf host_buffer;
130 int size, type, n;
131 const char *cp;
132
133 switch (af) {
134 case AF_INET:
135 size = INADDRSZ;
136 type = T_A;
137 break;
138 case AF_INET6:
139 size = IN6ADDRSZ;
140 type = T_AAAA;
141 break;
142 default:
143 *h_errnop = NETDB_INTERNAL;
c4029823 144 __set_errno (EAFNOSUPPORT);
5f0e6fc7
RM
145 return NSS_STATUS_UNAVAIL;
146 }
147
148 result->h_addrtype = af;
149 result->h_length = size;
150
151 /*
152 * if there aren't any dots, it could be a user-level alias.
153 * this is also done in res_query() since we are not the only
154 * function that looks up host names.
155 */
156 if (strchr (name, '.') == NULL && (cp = __hostalias (name)) != NULL)
157 name = cp;
158
795fbc9a 159 n = res_search (name, C_IN, type, host_buffer.buf, sizeof (host_buffer.buf));
5f0e6fc7 160 if (n < 0)
dd7d45e8
UD
161 {
162 *h_errnop = h_errno;
163 return errno == ECONNREFUSED ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
164 }
5f0e6fc7
RM
165
166 return getanswer_r (&host_buffer, n, name, type, result, buffer, buflen,
167 h_errnop);
168}
169
170
171enum nss_status
172_nss_dns_gethostbyname_r (const char *name, struct hostent *result,
df4ef2ab 173 char *buffer, size_t buflen, int *h_errnop)
5f0e6fc7
RM
174{
175 enum nss_status status = NSS_STATUS_NOTFOUND;
176
177 if (_res.options & RES_USE_INET6)
178 status = _nss_dns_gethostbyname2_r (name, AF_INET6, result, buffer,
179 buflen, h_errnop);
180 if (status == NSS_STATUS_NOTFOUND)
181 status = _nss_dns_gethostbyname2_r (name, AF_INET, result, buffer,
182 buflen, h_errnop);
183
184 return status;
185}
186
187
188enum nss_status
189_nss_dns_gethostbyaddr_r (const char *addr, int len, int af,
df4ef2ab 190 struct hostent *result, char *buffer, size_t buflen,
5f0e6fc7
RM
191 int *h_errnop)
192{
193 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
194 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
195 const u_char *uaddr = (const u_char *)addr;
196 struct host_data
197 {
198 char *aliases[MAX_NR_ALIASES];
199 unsigned char host_addr[16]; /* IPv4 or IPv6 */
200 char *h_addr_ptrs[MAX_NR_ADDRS + 1];
201 char linebuffer[0];
202 } *host_data = (struct host_data *) buffer;
203 querybuf host_buffer;
204 char qbuf[MAXDNAME+1], *qp;
205 int size, n, status;
206
207 if (af == AF_INET6 && len == IN6ADDRSZ &&
63551311
UD
208 (memcmp (uaddr, mapped, sizeof mapped) == 0
209 || memcmp (uaddr, tunnelled, sizeof tunnelled) == 0))
5f0e6fc7
RM
210 {
211 /* Unmap. */
212 addr += sizeof mapped;
213 uaddr += sizeof mapped;
214 af = AF_INET;
215 len = INADDRSZ;
216 }
217
218 switch (af)
219 {
220 case AF_INET:
221 size = INADDRSZ;
222 break;
223 case AF_INET6:
224 size = IN6ADDRSZ;
225 break;
226 default:
c4029823 227 __set_errno (EAFNOSUPPORT);
5f0e6fc7
RM
228 *h_errnop = NETDB_INTERNAL;
229 return NSS_STATUS_UNAVAIL;
230 }
231 if (size != len)
232 {
c4029823 233 __set_errno (EAFNOSUPPORT);
5f0e6fc7
RM
234 *h_errnop = NETDB_INTERNAL;
235 return NSS_STATUS_UNAVAIL;
236 }
237
238 switch (af)
239 {
240 case AF_INET:
241 sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
242 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
243 break;
244 case AF_INET6:
245 qp = qbuf;
246 for (n = IN6ADDRSZ - 1; n >= 0; n--)
247 qp += sprintf (qp, "%x.%x.", uaddr[n] & 0xf, (uaddr[n] >> 4) & 0xf);
da2d1bc5 248 strcpy (qp, "ip6.int");
5f0e6fc7
RM
249 break;
250 default:
251 /* Cannot happen. */
252 }
253
254 n = res_query (qbuf, C_IN, T_PTR, (u_char *)host_buffer.buf,
255 sizeof host_buffer);
256 if (n < 0)
dd7d45e8
UD
257 {
258 *h_errnop = h_errno;
259 return errno == ECONNREFUSED ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
260 }
5f0e6fc7
RM
261
262 status = getanswer_r (&host_buffer, n, qbuf, T_PTR, result, buffer, buflen,
263 h_errnop);
264 if (status != NSS_STATUS_SUCCESS)
dd7d45e8
UD
265 {
266 *h_errnop = h_errno;
267 return status;
268 }
5f0e6fc7
RM
269
270#ifdef SUNSECURITY
271 This is not implemented because it is not possible to use the current
272 source from bind in a multi-threaded program.
273#endif
274
275 result->h_addrtype = af;
276 result->h_length = len;
63551311 277 memcpy (host_data->host_addr, addr, len);
5f0e6fc7
RM
278 host_data->h_addr_ptrs[0] = (char *) host_data->host_addr;
279 host_data->h_addr_ptrs[1] = NULL;
280 if (af == AF_INET && (_res.options & RES_USE_INET6))
281 {
282 map_v4v6_address ((char *) host_data->host_addr,
283 (char *) host_data->host_addr);
284 result->h_addrtype = AF_INET6;
285 result->h_length = IN6ADDRSZ;
286 }
287 *h_errnop = NETDB_SUCCESS;
288 return NSS_STATUS_SUCCESS;
289}
290
291
292static enum nss_status
293getanswer_r (const querybuf *answer, int anslen, const char *qname, int qtype,
df4ef2ab
UD
294 struct hostent *result, char *buffer, size_t buflen,
295 int *h_errnop)
5f0e6fc7
RM
296{
297 struct host_data
298 {
299 char *aliases[MAX_NR_ALIASES];
300 unsigned char host_addr[16]; /* IPv4 or IPv6 */
301 char *h_addr_ptrs[MAX_NR_ADDRS + 1];
302 char linebuffer[0];
303 } *host_data = (struct host_data *) buffer;
304 int linebuflen = buflen - offsetof (struct host_data, linebuffer);
305 register const HEADER *hp;
306 const u_char *end_of_message, *cp;
307 int n, ancount, qdcount;
308 int haveanswer, had_error;
309 char *bp, **ap, **hap;
845dcb57 310 char tbuf[MAXDNAME];
5f0e6fc7
RM
311 const char *tname;
312 int (*name_ok) __P ((const char *));
313
314 tname = qname;
315 result->h_name = NULL;
316 end_of_message = answer->buf + anslen;
317 switch (qtype)
318 {
319 case T_A:
320 case T_AAAA:
321 name_ok = res_hnok;
322 break;
323 case T_PTR:
324 name_ok = res_dnok;
325 break;
326 default:
327 return NSS_STATUS_UNAVAIL; /* XXX should be abort(); */
328 }
329
330 /*
331 * find first satisfactory answer
332 */
333 hp = &answer->hdr;
334 bp = host_data->linebuffer;
335 ancount = ntohs (hp->ancount);
336 qdcount = ntohs (hp->qdcount);
337 cp = answer->buf + HFIXEDSZ;
338 if (qdcount != 1)
339 {
340 *h_errnop = NO_RECOVERY;
341 return NSS_STATUS_UNAVAIL;
342 }
343
344 n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen);
345 if (n < 0 || (*name_ok) (bp) == 0)
346 {
347 *h_errnop = NO_RECOVERY;
348 return NSS_STATUS_UNAVAIL;
349 }
350 cp += n + QFIXEDSZ;
351
352 if (qtype == T_A || qtype == T_AAAA)
353 {
354 /* res_send() has already verified that the query name is the
355 * same as the one we sent; this just gets the expanded name
356 * (i.e., with the succeeding search-domain tacked on).
357 */
358 n = strlen (bp) + 1; /* for the \0 */
76b87c03
UD
359 if (n >= MAXHOSTNAMELEN)
360 {
361 __set_h_errno (NO_RECOVERY);
362 return NSS_STATUS_TRYAGAIN;
363 }
5f0e6fc7
RM
364 result->h_name = bp;
365 bp += n;
366 linebuflen -= n;
367 /* The qname can be abbreviated, but h_name is now absolute. */
368 qname = result->h_name;
369 }
370
371 ap = host_data->aliases;
372 *ap = NULL;
373 result->h_aliases = host_data->aliases;
374 hap = host_data->h_addr_ptrs;
375 *hap = NULL;
376 result->h_addr_list = host_data->h_addr_ptrs;
377 haveanswer = 0;
378 had_error = 0;
379
380 while (ancount-- > 0 && cp < end_of_message && had_error == 0)
381 {
382 int type, class;
383
384 n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen);
385 if (n < 0 || (*name_ok) (bp) == 0)
386 {
387 ++had_error;
388 continue;
389 }
390 cp += n; /* name */
391 type = _getshort (cp);
392 cp += INT16SZ; /* type */
393 class = _getshort(cp);
394 cp += INT16SZ + INT32SZ; /* class, TTL */
395 n = _getshort(cp);
396 cp += INT16SZ; /* len */
397 if (class != C_IN)
398 {
399 /* XXX - debug? syslog? */
400 cp += n;
401 continue; /* XXX - had_error++ ? */
402 }
403
404 if ((qtype ==T_A || qtype == T_AAAA) && type == T_CNAME)
405 {
406 if (ap >= &host_data->aliases[MAX_NR_ALIASES - 1])
407 continue;
408 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
409 if (n < 0 || (*name_ok) (tbuf) == 0)
410 {
411 ++had_error;
412 continue;
413 }
414 cp += n;
415 /* Store alias. */
416 *ap++ = bp;
417 n = strlen (bp) + 1; /* For the \0. */
76b87c03
UD
418 if (n >= MAXHOSTNAMELEN)
419 {
420 ++had_error;
421 continue;
422 }
5f0e6fc7
RM
423 bp += n;
424 linebuflen -= n;
425 /* Get canonical name. */
426 n = strlen (tbuf) + 1; /* For the \0. */
76b87c03 427 if ((size_t) n > buflen || n >= MAXHOSTNAMELEN)
5f0e6fc7
RM
428 {
429 ++had_error;
430 continue;
431 }
da2d1bc5 432 result->h_name = strcpy (bp, tbuf); /* Cannot overflow. */
5f0e6fc7
RM
433 bp += n;
434 linebuflen -= n;
435 continue;
436 }
437
438 if (qtype == T_PTR && type == T_CNAME)
439 {
440 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
da2d1bc5 441 if (n < 0 || res_dnok (tbuf) == 0)
5f0e6fc7
RM
442 {
443 ++had_error;
444 continue;
445 }
446 cp += n;
da2d1bc5 447 /* Get canonical name. */
5f0e6fc7 448 n = strlen (tbuf) + 1; /* For the \0. */
76b87c03 449 if ((size_t) n > buflen || n >= MAXHOSTNAMELEN)
5f0e6fc7
RM
450 {
451 ++had_error;
452 continue;
453 }
da2d1bc5 454 tname = strcpy (bp, tbuf); /* Cannot overflow. */
5f0e6fc7
RM
455 bp += n;
456 linebuflen -= n;
457 continue;
458 }
459 if (type != qtype)
460 {
461 syslog (LOG_NOTICE | LOG_AUTH,
462 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
463 qname, p_class (C_IN), p_type (qtype), p_type (type));
464 cp += n;
465 continue; /* XXX - had_error++ ? */
466 }
467
468 switch (type)
469 {
470 case T_PTR:
471 if (strcasecmp (tname, bp) != 0)
472 {
473 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
474 cp += n;
475 continue; /* XXX - had_error++ ? */
476 }
477 n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen);
478 if (n < 0 || res_hnok (bp) == 0)
479 {
480 ++had_error;
481 break;
482 }
483#if MULTI_PTRS_ARE_ALIASES
484 cp += n;
485 if (haveanswer == 0)
486 result->h_name = bp;
487 else if (ap < &host_data->aliases[MAXALIASES-1])
488 *ap++ = bp;
489 else
490 n = -1;
491 if (n != -1)
492 {
493 n = strlen (bp) + 1; /* for the \0 */
76b87c03
UD
494 if (n >= MAXHOSTNAMELEN)
495 {
496 ++had_error;
497 break;
498 }
5f0e6fc7
RM
499 bp += n;
500 linebuflen -= n;
501 }
502 break;
503#else
504 result->h_name = bp;
505 if (_res.options & RES_USE_INET6)
506 {
507 n = strlen (bp) + 1; /* for the \0 */
76b87c03
UD
508 if (n >= MAXHOSTNAMELEN)
509 {
510 ++had_error;
511 break;
512 }
5f0e6fc7
RM
513 bp += n;
514 linebuflen -= n;
515 map_v4v6_hostent (result, &bp, &linebuflen);
516 }
517 *h_errnop = NETDB_SUCCESS;
518 return NSS_STATUS_SUCCESS;
519#endif
520 case T_A:
521 case T_AAAA:
522 if (strcasecmp (result->h_name, bp) != 0)
523 {
524 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, result->h_name, bp);
525 cp += n;
526 continue; /* XXX - had_error++ ? */
527 }
1f64ac13 528 if (n != result->h_length)
5f0e6fc7 529 {
1f64ac13
UD
530 cp += n;
531 continue;
5f0e6fc7 532 }
1f64ac13 533 if (!haveanswer)
5f0e6fc7
RM
534 {
535 register int nn;
536
537 result->h_name = bp;
538 nn = strlen (bp) + 1; /* for the \0 */
539 bp += nn;
540 linebuflen -= nn;
541 }
542
543 bp += sizeof (align) - ((u_long) bp % sizeof (align));
544
545 if (n >= linebuflen)
546 {
547 ++had_error;
548 continue;
549 }
550 if (hap >= &host_data->h_addr_ptrs[MAX_NR_ADDRS-1])
551 {
552 cp += n;
553 continue;
554 }
63551311 555 memcpy (*hap++ = bp, cp, n);
5f0e6fc7
RM
556 bp += n;
557 cp += n;
558 linebuflen -= n;
559 break;
560 default:
561 abort ();
562 }
563 if (had_error == 0)
564 ++haveanswer;
565 }
566
567 if (haveanswer > 0)
568 {
569 *ap = NULL;
570 *hap = NULL;
da2d1bc5 571#if defined RESOLVSORT
5f0e6fc7
RM
572 /*
573 * Note: we sort even if host can take only one address
574 * in its return structures - should give it the "best"
575 * address in that case, not some random one
576 */
577 if (_res.nsort && haveanswer > 1 && qtype == T_A)
578 addrsort (host_data->h_addr_ptrs, haveanswer);
579#endif /*RESOLVSORT*/
580
581 if (result->h_name == NULL)
582 {
583 n = strlen (qname) + 1; /* For the \0. */
76b87c03
UD
584 if (n > linebuflen || n >= MAXHOSTNAMELEN)
585 goto no_recovery;
da2d1bc5 586 result->h_name = strcpy (bp, qname); /* Cannot overflow. */
5f0e6fc7
RM
587 bp += n;
588 linebuflen -= n;
589 }
590
591 if (_res.options & RES_USE_INET6)
592 map_v4v6_hostent (result, &bp, &linebuflen);
593 *h_errnop = NETDB_SUCCESS;
594 return NSS_STATUS_SUCCESS;
595 }
76b87c03
UD
596 no_recovery:
597 *h_errnop = NO_RECOVERY;
5f0e6fc7
RM
598 return NSS_STATUS_TRYAGAIN;
599}