]> git.ipfire.org Git - thirdparty/glibc.git/blob - resolv/nss_dns/dns-host.c
update from main archive 960105
[thirdparty/glibc.git] / resolv / nss_dns / dns-host.c
1 /* Copyright (C) 1996, 1997 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 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.
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 Library General Public License for more details.
14
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. */
19
20 /* Parts of this file are plain copies of the file `gethtnamadr.c' from
21 the bind package and it has the following copyright. */
22
23 /*
24 * ++Copyright++ 1985, 1988, 1993
25 * -
26 * Copyright (c) 1985, 1988, 1993
27 * The Regents of the University of California. All rights reserved.
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:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
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 * 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--
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
87 #include <netinet/in.h>
88 #include <arpa/inet.h>
89 #include <arpa/nameser.h>
90 #include <resolv.h>
91
92 #include "nsswitch.h"
93
94 /* Get implementation for some internal functions. */
95 #include "../resolv/mapv4v6addr.h"
96 #include "../resolv/mapv4v6hostent.h"
97
98 /* Maximum number of aliases we allow. */
99 #define MAX_NR_ALIASES 48
100 #define MAX_NR_ADDRS 48
101
102 #if PACKETSZ > 1024
103 # define MAXPACKET PACKETSZ
104 #else
105 # define MAXPACKET 1024
106 #endif
107
108 static const char AskedForGot[] = "\
109 gethostby*.getanswer: asked for \"%s\", got \"%s\"";
110
111
112 /* We need this time later. */
113 typedef union querybuf
114 {
115 HEADER hdr;
116 u_char buf[MAXPACKET];
117 } querybuf;
118
119
120 static enum nss_status getanswer_r (const querybuf *answer, int anslen,
121 const char *qname, int qtype,
122 struct hostent *result, char *buffer,
123 size_t buflen, int *h_errnop);
124
125 enum nss_status
126 _nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result,
127 char *buffer, size_t buflen, int *h_errnop)
128 {
129 struct host_data
130 {
131 char *aliases[MAX_NR_ALIASES];
132 unsigned char host_addr[16]; /* IPv4 or IPv6 */
133 char *h_addr_ptrs[MAX_NR_ADDRS + 1];
134 char linebuffer[0];
135 } *host_data = (struct host_data *) buffer;
136 int linebuflen = buflen - offsetof (struct host_data, linebuffer);
137 querybuf host_buffer;
138 int size, type, n;
139 const char *cp;
140
141 switch (af) {
142 case AF_INET:
143 size = INADDRSZ;
144 type = T_A;
145 break;
146 case AF_INET6:
147 size = IN6ADDRSZ;
148 type = T_AAAA;
149 break;
150 default:
151 *h_errnop = NETDB_INTERNAL;
152 __set_errno (EAFNOSUPPORT);
153 return NSS_STATUS_UNAVAIL;
154 }
155
156 result->h_addrtype = af;
157 result->h_length = size;
158
159 /*
160 * if there aren't any dots, it could be a user-level alias.
161 * this is also done in res_query() since we are not the only
162 * function that looks up host names.
163 */
164 if (strchr (name, '.') == NULL && (cp = __hostalias (name)) != NULL)
165 name = cp;
166
167 /*
168 * disallow names consisting only of digits/dots, unless
169 * they end in a dot.
170 */
171 if (isdigit (name[0]))
172 for (cp = name;; ++cp)
173 {
174 if (*cp == '\0')
175 {
176 char *bp;
177
178 if (*--cp == '.')
179 break;
180 /*
181 * All-numeric, no dot at the end. Fake up a hostent
182 * as if we'd actually done a lookup.
183 */
184 if (inet_pton (af, name, host_data->host_addr) <= 0)
185 {
186 *h_errnop = HOST_NOT_FOUND;
187 return NSS_STATUS_NOTFOUND;
188 }
189
190 bp = __stpncpy (host_data->linebuffer, name, linebuflen);
191 host_data->linebuffer[linebuflen - 1] = '\0';
192 linebuflen -= bp - host_data->linebuffer;
193
194 result->h_name = host_data->linebuffer;
195 result->h_aliases = host_data->aliases;
196 host_data->aliases[0] = NULL;
197 host_data->h_addr_ptrs[0] = (char *) host_data->host_addr;
198 host_data->h_addr_ptrs[1] = NULL;
199 result->h_addr_list = host_data->h_addr_ptrs;
200
201 if (_res.options & RES_USE_INET6)
202 map_v4v6_hostent (result, &bp, &linebuflen);
203 *h_errnop = NETDB_SUCCESS;
204 return NSS_STATUS_SUCCESS;
205 }
206 if (!isdigit (*cp) && *cp != '.')
207 break;
208 }
209 if (isxdigit (name[0]) || name[0] == ':')
210 for (cp = name;; ++cp)
211 {
212 if (*cp == '\0')
213 {
214 char *bp;
215
216 if (*--cp == '.')
217 break;
218 /*
219 * All-IPv6-legal, no dot at the end. Fake up a hostent
220 * as if we'd actually done a lookup.
221 */
222 if (inet_pton (af, name, host_data->host_addr) <= 0)
223 {
224 *h_errnop = HOST_NOT_FOUND;
225 return NSS_STATUS_NOTFOUND;
226 }
227
228 bp = __stpncpy (host_data->linebuffer, name, linebuflen);
229 host_data->linebuffer[linebuflen - 1] = '\0';
230 linebuflen -= bp - host_data->linebuffer;
231
232 result->h_name = host_data->linebuffer;
233 result->h_aliases = host_data->aliases;
234 host_data->aliases[0] = NULL;
235 host_data->h_addr_ptrs[0] = (char *) host_data->host_addr;
236 host_data->h_addr_ptrs[1] = NULL;
237 result->h_addr_list = host_data->h_addr_ptrs;
238 *h_errnop = NETDB_SUCCESS;
239 return NSS_STATUS_SUCCESS;
240 }
241 if (!isxdigit (*cp) && *cp != ':' && *cp != '.')
242 break;
243 }
244
245 n = res_search (name, C_IN, type, host_buffer.buf, sizeof (host_buffer));
246 if (n < 0)
247 return errno == ECONNREFUSED ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
248
249 return getanswer_r (&host_buffer, n, name, type, result, buffer, buflen,
250 h_errnop);
251 }
252
253
254 enum nss_status
255 _nss_dns_gethostbyname_r (const char *name, struct hostent *result,
256 char *buffer, size_t buflen, int *h_errnop)
257 {
258 enum nss_status status = NSS_STATUS_NOTFOUND;
259
260 if (_res.options & RES_USE_INET6)
261 status = _nss_dns_gethostbyname2_r (name, AF_INET6, result, buffer,
262 buflen, h_errnop);
263 if (status == NSS_STATUS_NOTFOUND)
264 status = _nss_dns_gethostbyname2_r (name, AF_INET, result, buffer,
265 buflen, h_errnop);
266
267 return status;
268 }
269
270
271 enum nss_status
272 _nss_dns_gethostbyaddr_r (const char *addr, int len, int af,
273 struct hostent *result, char *buffer, size_t buflen,
274 int *h_errnop)
275 {
276 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
277 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
278 const u_char *uaddr = (const u_char *)addr;
279 struct host_data
280 {
281 char *aliases[MAX_NR_ALIASES];
282 unsigned char host_addr[16]; /* IPv4 or IPv6 */
283 char *h_addr_ptrs[MAX_NR_ADDRS + 1];
284 char linebuffer[0];
285 } *host_data = (struct host_data *) buffer;
286 querybuf host_buffer;
287 char qbuf[MAXDNAME+1], *qp;
288 int size, n, status;
289
290 if (af == AF_INET6 && len == IN6ADDRSZ &&
291 (bcmp (uaddr, mapped, sizeof mapped) == 0
292 || bcmp (uaddr, tunnelled, sizeof tunnelled) == 0))
293 {
294 /* Unmap. */
295 addr += sizeof mapped;
296 uaddr += sizeof mapped;
297 af = AF_INET;
298 len = INADDRSZ;
299 }
300
301 switch (af)
302 {
303 case AF_INET:
304 size = INADDRSZ;
305 break;
306 case AF_INET6:
307 size = IN6ADDRSZ;
308 break;
309 default:
310 __set_errno (EAFNOSUPPORT);
311 *h_errnop = NETDB_INTERNAL;
312 return NSS_STATUS_UNAVAIL;
313 }
314 if (size != len)
315 {
316 __set_errno (EAFNOSUPPORT);
317 *h_errnop = NETDB_INTERNAL;
318 return NSS_STATUS_UNAVAIL;
319 }
320
321 switch (af)
322 {
323 case AF_INET:
324 sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
325 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
326 break;
327 case AF_INET6:
328 qp = qbuf;
329 for (n = IN6ADDRSZ - 1; n >= 0; n--)
330 qp += sprintf (qp, "%x.%x.", uaddr[n] & 0xf, (uaddr[n] >> 4) & 0xf);
331 strcpy(qp, "ip6.int");
332 break;
333 default:
334 /* Cannot happen. */
335 }
336
337 n = res_query (qbuf, C_IN, T_PTR, (u_char *)host_buffer.buf,
338 sizeof host_buffer);
339 if (n < 0)
340 return errno == ECONNREFUSED ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
341
342 status = getanswer_r (&host_buffer, n, qbuf, T_PTR, result, buffer, buflen,
343 h_errnop);
344 if (status != NSS_STATUS_SUCCESS)
345 return status;
346
347 #ifdef SUNSECURITY
348 This is not implemented because it is not possible to use the current
349 source from bind in a multi-threaded program.
350 #endif
351
352 result->h_addrtype = af;
353 result->h_length = len;
354 bcopy (addr, host_data->host_addr, len);
355 host_data->h_addr_ptrs[0] = (char *) host_data->host_addr;
356 host_data->h_addr_ptrs[1] = NULL;
357 if (af == AF_INET && (_res.options & RES_USE_INET6))
358 {
359 map_v4v6_address ((char *) host_data->host_addr,
360 (char *) host_data->host_addr);
361 result->h_addrtype = AF_INET6;
362 result->h_length = IN6ADDRSZ;
363 }
364 *h_errnop = NETDB_SUCCESS;
365 return NSS_STATUS_SUCCESS;
366 }
367
368
369 static enum nss_status
370 getanswer_r (const querybuf *answer, int anslen, const char *qname, int qtype,
371 struct hostent *result, char *buffer, size_t buflen,
372 int *h_errnop)
373 {
374 struct host_data
375 {
376 char *aliases[MAX_NR_ALIASES];
377 unsigned char host_addr[16]; /* IPv4 or IPv6 */
378 char *h_addr_ptrs[MAX_NR_ADDRS + 1];
379 char linebuffer[0];
380 } *host_data = (struct host_data *) buffer;
381 int linebuflen = buflen - offsetof (struct host_data, linebuffer);
382 register const HEADER *hp;
383 const u_char *end_of_message, *cp;
384 int n, ancount, qdcount;
385 int haveanswer, had_error;
386 char *bp, **ap, **hap;
387 char tbuf[MAXDNAME];
388 const char *tname;
389 int (*name_ok) __P ((const char *));
390
391 tname = qname;
392 result->h_name = NULL;
393 end_of_message = answer->buf + anslen;
394 switch (qtype)
395 {
396 case T_A:
397 case T_AAAA:
398 name_ok = res_hnok;
399 break;
400 case T_PTR:
401 name_ok = res_dnok;
402 break;
403 default:
404 return NSS_STATUS_UNAVAIL; /* XXX should be abort(); */
405 }
406
407 /*
408 * find first satisfactory answer
409 */
410 hp = &answer->hdr;
411 bp = host_data->linebuffer;
412 ancount = ntohs (hp->ancount);
413 qdcount = ntohs (hp->qdcount);
414 cp = answer->buf + HFIXEDSZ;
415 if (qdcount != 1)
416 {
417 *h_errnop = NO_RECOVERY;
418 return NSS_STATUS_UNAVAIL;
419 }
420
421 n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen);
422 if (n < 0 || (*name_ok) (bp) == 0)
423 {
424 *h_errnop = NO_RECOVERY;
425 return NSS_STATUS_UNAVAIL;
426 }
427 cp += n + QFIXEDSZ;
428
429 if (qtype == T_A || qtype == T_AAAA)
430 {
431 /* res_send() has already verified that the query name is the
432 * same as the one we sent; this just gets the expanded name
433 * (i.e., with the succeeding search-domain tacked on).
434 */
435 n = strlen (bp) + 1; /* for the \0 */
436 result->h_name = bp;
437 bp += n;
438 linebuflen -= n;
439 /* The qname can be abbreviated, but h_name is now absolute. */
440 qname = result->h_name;
441 }
442
443 ap = host_data->aliases;
444 *ap = NULL;
445 result->h_aliases = host_data->aliases;
446 hap = host_data->h_addr_ptrs;
447 *hap = NULL;
448 result->h_addr_list = host_data->h_addr_ptrs;
449 haveanswer = 0;
450 had_error = 0;
451
452 while (ancount-- > 0 && cp < end_of_message && had_error == 0)
453 {
454 int type, class;
455
456 n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen);
457 if (n < 0 || (*name_ok) (bp) == 0)
458 {
459 ++had_error;
460 continue;
461 }
462 cp += n; /* name */
463 type = _getshort (cp);
464 cp += INT16SZ; /* type */
465 class = _getshort(cp);
466 cp += INT16SZ + INT32SZ; /* class, TTL */
467 n = _getshort(cp);
468 cp += INT16SZ; /* len */
469 if (class != C_IN)
470 {
471 /* XXX - debug? syslog? */
472 cp += n;
473 continue; /* XXX - had_error++ ? */
474 }
475
476 if ((qtype ==T_A || qtype == T_AAAA) && type == T_CNAME)
477 {
478 if (ap >= &host_data->aliases[MAX_NR_ALIASES - 1])
479 continue;
480 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
481 if (n < 0 || (*name_ok) (tbuf) == 0)
482 {
483 ++had_error;
484 continue;
485 }
486 cp += n;
487 /* Store alias. */
488 *ap++ = bp;
489 n = strlen (bp) + 1; /* For the \0. */
490 bp += n;
491 linebuflen -= n;
492 /* Get canonical name. */
493 n = strlen (tbuf) + 1; /* For the \0. */
494 if (n > buflen)
495 {
496 ++had_error;
497 continue;
498 }
499 strcpy (bp, tbuf); /* Cannot overflow. */
500 result->h_name = bp;
501 bp += n;
502 linebuflen -= n;
503 continue;
504 }
505
506 if (qtype == T_PTR && type == T_CNAME)
507 {
508 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
509 if (n < 0 || res_hnok (tbuf) == 0)
510 {
511 ++had_error;
512 continue;
513 }
514 cp += n;
515 /* Get canonical name. */
516 n = strlen (tbuf) + 1; /* For the \0. */
517 if (n > buflen)
518 {
519 ++had_error;
520 continue;
521 }
522 strcpy (bp, tbuf); /* Cannot overflow. */
523 tname = bp;
524 bp += n;
525 linebuflen -= n;
526 continue;
527 }
528 if (type != qtype)
529 {
530 syslog (LOG_NOTICE | LOG_AUTH,
531 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
532 qname, p_class (C_IN), p_type (qtype), p_type (type));
533 cp += n;
534 continue; /* XXX - had_error++ ? */
535 }
536
537 switch (type)
538 {
539 case T_PTR:
540 if (strcasecmp (tname, bp) != 0)
541 {
542 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
543 cp += n;
544 continue; /* XXX - had_error++ ? */
545 }
546 n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen);
547 if (n < 0 || res_hnok (bp) == 0)
548 {
549 ++had_error;
550 break;
551 }
552 #if MULTI_PTRS_ARE_ALIASES
553 cp += n;
554 if (haveanswer == 0)
555 result->h_name = bp;
556 else if (ap < &host_data->aliases[MAXALIASES-1])
557 *ap++ = bp;
558 else
559 n = -1;
560 if (n != -1)
561 {
562 n = strlen (bp) + 1; /* for the \0 */
563 bp += n;
564 linebuflen -= n;
565 }
566 break;
567 #else
568 result->h_name = bp;
569 if (_res.options & RES_USE_INET6)
570 {
571 n = strlen (bp) + 1; /* for the \0 */
572 bp += n;
573 linebuflen -= n;
574 map_v4v6_hostent (result, &bp, &linebuflen);
575 }
576 *h_errnop = NETDB_SUCCESS;
577 return NSS_STATUS_SUCCESS;
578 #endif
579 case T_A:
580 case T_AAAA:
581 if (strcasecmp (result->h_name, bp) != 0)
582 {
583 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, result->h_name, bp);
584 cp += n;
585 continue; /* XXX - had_error++ ? */
586 }
587 if (n != result->h_length)
588 {
589 cp += n;
590 continue;
591 }
592 if (!haveanswer)
593 {
594 register int nn;
595
596 result->h_name = bp;
597 nn = strlen (bp) + 1; /* for the \0 */
598 bp += nn;
599 linebuflen -= nn;
600 }
601
602 bp += sizeof (align) - ((u_long) bp % sizeof (align));
603
604 if (n >= linebuflen)
605 {
606 ++had_error;
607 continue;
608 }
609 if (hap >= &host_data->h_addr_ptrs[MAX_NR_ADDRS-1])
610 {
611 cp += n;
612 continue;
613 }
614 bcopy (cp, *hap++ = bp, n);
615 bp += n;
616 cp += n;
617 linebuflen -= n;
618 break;
619 default:
620 abort ();
621 }
622 if (had_error == 0)
623 ++haveanswer;
624 }
625
626 if (haveanswer > 0)
627 {
628 *ap = NULL;
629 *hap = NULL;
630 #if defined(RESOLVSORT)
631 /*
632 * Note: we sort even if host can take only one address
633 * in its return structures - should give it the "best"
634 * address in that case, not some random one
635 */
636 if (_res.nsort && haveanswer > 1 && qtype == T_A)
637 addrsort (host_data->h_addr_ptrs, haveanswer);
638 #endif /*RESOLVSORT*/
639
640 if (result->h_name == NULL)
641 {
642 n = strlen (qname) + 1; /* For the \0. */
643 if (n > linebuflen)
644 goto try_again;
645 strcpy (bp, qname); /* Cannot overflow. */
646 result->h_name = bp;
647 bp += n;
648 linebuflen -= n;
649 }
650
651 if (_res.options & RES_USE_INET6)
652 map_v4v6_hostent (result, &bp, &linebuflen);
653 *h_errnop = NETDB_SUCCESS;
654 return NSS_STATUS_SUCCESS;
655 }
656 try_again:
657 *h_errnop = TRY_AGAIN;
658 return NSS_STATUS_TRYAGAIN;
659 }