dns_ho.c revision 9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829
2N/A/*
2N/A * Portions Copyright (C) 2004-2006, 2008 Internet Systems Consortium, Inc. ("ISC")
2N/A * Portions Copyright (C) 1996-2003 Internet Software Consortium.
2N/A *
2N/A * Permission to use, copy, modify, and/or distribute this software for any
2N/A * purpose with or without fee is hereby granted, provided that the above
2N/A * copyright notice and this permission notice appear in all copies.
2N/A *
2N/A * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
2N/A * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
2N/A * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
2N/A * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
2N/A * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
2N/A * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
2N/A * PERFORMANCE OF THIS SOFTWARE.
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 1985, 1988, 1993
2N/A * The Regents of the University of California. All rights reserved.
2N/A *
2N/A * Redistribution and use in source and binary forms, with or without
2N/A * modification, are permitted provided that the following conditions
2N/A * are met:
2N/A * 1. Redistributions of source code must retain the above copyright
2N/A * notice, this list of conditions and the following disclaimer.
2N/A * 2. Redistributions in binary form must reproduce the above copyright
2N/A * notice, this list of conditions and the following disclaimer in the
2N/A * documentation and/or other materials provided with the distribution.
2N/A * 3. All advertising materials mentioning features or use of this software
2N/A * must display the following acknowledgement:
2N/A * This product includes software developed by the University of
2N/A * California, Berkeley and its contributors.
2N/A * 4. Neither the name of the University nor the names of its contributors
2N/A * may be used to endorse or promote products derived from this software
2N/A * without specific prior written permission.
2N/A *
2N/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2N/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2N/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2N/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2N/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2N/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2N/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2N/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2N/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2N/A * SUCH DAMAGE.
2N/A */
2N/A
2N/A/* from gethostnamadr.c 8.1 (Berkeley) 6/4/93 */
2N/A/* BIND Id: gethnamaddr.c,v 8.15 1996/05/22 04:56:30 vixie Exp $ */
2N/A
2N/A#if defined(LIBC_SCCS) && !defined(lint)
2N/Astatic const char rcsid[] = "$Id: dns_ho.c,v 1.23 2008/11/14 02:36:51 marka Exp $";
2N/A#endif /* LIBC_SCCS and not lint */
2N/A
2N/A/* Imports. */
2N/A
2N/A#include "port_before.h"
2N/A
2N/A#include <sys/types.h>
2N/A#include <sys/param.h>
2N/A#include <sys/socket.h>
2N/A
2N/A#include <netinet/in.h>
2N/A#include <arpa/inet.h>
2N/A#include <arpa/nameser.h>
2N/A
2N/A#include <ctype.h>
2N/A#include <errno.h>
2N/A#include <stdlib.h>
2N/A#include <netdb.h>
2N/A#include <resolv.h>
2N/A#include <stdio.h>
2N/A#include <string.h>
2N/A#include <syslog.h>
2N/A
2N/A#include <isc/memcluster.h>
2N/A#include <irs.h>
2N/A
2N/A#include "port_after.h"
2N/A
2N/A#include "irs_p.h"
2N/A#include "dns_p.h"
2N/A
2N/A#ifdef SPRINTF_CHAR
2N/A# define SPRINTF(x) strlen(sprintf/**/x)
2N/A#else
2N/A# define SPRINTF(x) sprintf x
2N/A#endif
2N/A
2N/A/* Definitions. */
2N/A
2N/A#define MAXALIASES 35
2N/A#define MAXADDRS 35
2N/A
2N/A#define MAXPACKET (65535) /*%< Maximum TCP message size */
2N/A#define BOUNDS_CHECK(ptr, count) \
2N/A if ((ptr) + (count) > eom) { \
2N/A had_error++; \
2N/A continue; \
2N/A } else (void)0
2N/A
2N/Atypedef union {
2N/A HEADER hdr;
2N/A u_char buf[MAXPACKET];
2N/A} querybuf;
2N/A
2N/Astruct dns_res_target {
2N/A struct dns_res_target *next;
2N/A querybuf qbuf; /*%< query buffer */
2N/A u_char *answer; /*%< buffer to put answer */
2N/A int anslen; /*%< size of answer buffer */
2N/A int qclass, qtype; /*%< class and type of query */
2N/A int action; /*%< condition whether query is really issued */
2N/A char qname[MAXDNAME +1]; /*%< domain name */
2N/A#if 0
2N/A int n; /*%< result length */
2N/A#endif
2N/A};
2N/Aenum {RESTGT_DOALWAYS, RESTGT_AFTERFAILURE, RESTGT_IGNORE};
2N/Aenum {RESQRY_SUCCESS, RESQRY_FAIL};
2N/A
2N/Astruct pvt {
2N/A struct hostent host;
2N/A char * h_addr_ptrs[MAXADDRS + 1];
2N/A char * host_aliases[MAXALIASES];
2N/A char hostbuf[8*1024];
2N/A u_char host_addr[16]; /*%< IPv4 or IPv6 */
2N/A struct __res_state *res;
2N/A void (*free_res)(void *);
2N/A};
2N/A
2N/Atypedef union {
2N/A int32_t al;
2N/A char ac;
2N/A} align;
2N/A
2N/Astatic const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
2N/Astatic const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
2N/A/* Note: the IPv6 loopback address is in the "tunnel" space */
2N/Astatic const u_char v6local[] = { 0,0, 0,1 }; /*%< last 4 bytes of IPv6 addr */
2N/A/* Forwards. */
2N/A
2N/Astatic void ho_close(struct irs_ho *this);
2N/Astatic struct hostent * ho_byname(struct irs_ho *this, const char *name);
2N/Astatic struct hostent * ho_byname2(struct irs_ho *this, const char *name,
2N/A int af);
2N/Astatic struct hostent * ho_byaddr(struct irs_ho *this, const void *addr,
2N/A int len, int af);
2N/Astatic struct hostent * ho_next(struct irs_ho *this);
2N/Astatic void ho_rewind(struct irs_ho *this);
2N/Astatic void ho_minimize(struct irs_ho *this);
2N/Astatic struct __res_state * ho_res_get(struct irs_ho *this);
2N/Astatic void ho_res_set(struct irs_ho *this,
2N/A struct __res_state *res,
2N/A void (*free_res)(void *));
2N/Astatic struct addrinfo * ho_addrinfo(struct irs_ho *this, const char *name,
2N/A const struct addrinfo *pai);
2N/A
2N/Astatic void map_v4v6_hostent(struct hostent *hp, char **bp,
2N/A char *ep);
2N/Astatic void addrsort(res_state, char **, int);
2N/Astatic struct hostent * gethostans(struct irs_ho *this,
2N/A const u_char *ansbuf, int anslen,
2N/A const char *qname, int qtype,
2N/A int af, int size,
2N/A struct addrinfo **ret_aip,
2N/A const struct addrinfo *pai);
2N/Astatic int add_hostent(struct pvt *pvt, char *bp, char **hap,
2N/A struct addrinfo *ai);
2N/Astatic int init(struct irs_ho *this);
2N/A
2N/A/* Exports. */
2N/A
2N/Astruct irs_ho *
2N/Airs_dns_ho(struct irs_acc *this) {
2N/A struct irs_ho *ho;
2N/A struct pvt *pvt;
2N/A
2N/A UNUSED(this);
2N/A
2N/A if (!(pvt = memget(sizeof *pvt))) {
2N/A errno = ENOMEM;
2N/A return (NULL);
2N/A }
2N/A memset(pvt, 0, sizeof *pvt);
2N/A
2N/A if (!(ho = memget(sizeof *ho))) {
2N/A memput(pvt, sizeof *pvt);
2N/A errno = ENOMEM;
2N/A return (NULL);
2N/A }
2N/A memset(ho, 0x5e, sizeof *ho);
2N/A ho->private = pvt;
2N/A ho->close = ho_close;
2N/A ho->byname = ho_byname;
2N/A ho->byname2 = ho_byname2;
2N/A ho->byaddr = ho_byaddr;
2N/A ho->next = ho_next;
2N/A ho->rewind = ho_rewind;
2N/A ho->minimize = ho_minimize;
2N/A ho->res_get = ho_res_get;
2N/A ho->res_set = ho_res_set;
2N/A ho->addrinfo = ho_addrinfo;
2N/A return (ho);
2N/A}
2N/A
2N/A/* Methods. */
2N/A
2N/Astatic void
2N/Aho_close(struct irs_ho *this) {
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A
2N/A ho_minimize(this);
2N/A if (pvt->res && pvt->free_res)
2N/A (*pvt->free_res)(pvt->res);
2N/A memput(pvt, sizeof *pvt);
2N/A memput(this, sizeof *this);
2N/A}
2N/A
2N/Astatic struct hostent *
2N/Aho_byname(struct irs_ho *this, const char *name) {
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A struct hostent *hp;
2N/A
2N/A if (init(this) == -1)
2N/A return (NULL);
2N/A
2N/A if (pvt->res->options & RES_USE_INET6) {
2N/A hp = ho_byname2(this, name, AF_INET6);
2N/A if (hp)
2N/A return (hp);
2N/A }
2N/A return (ho_byname2(this, name, AF_INET));
2N/A}
2N/A
2N/Astatic struct hostent *
2N/Aho_byname2(struct irs_ho *this, const char *name, int af)
2N/A{
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A struct hostent *hp = NULL;
2N/A int n, size;
2N/A char tmp[NS_MAXDNAME];
2N/A const char *cp;
2N/A struct addrinfo ai;
2N/A struct dns_res_target *q, *p;
2N/A int querystate = RESQRY_FAIL;
2N/A
2N/A if (init(this) == -1)
2N/A return (NULL);
2N/A
2N/A q = memget(sizeof(*q));
2N/A if (q == NULL) {
2N/A RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
2N/A errno = ENOMEM;
2N/A goto cleanup;
2N/A }
2N/A memset(q, 0, sizeof(*q));
2N/A
2N/A switch (af) {
2N/A case AF_INET:
2N/A size = INADDRSZ;
2N/A q->qclass = C_IN;
2N/A q->qtype = T_A;
2N/A q->answer = q->qbuf.buf;
2N/A q->anslen = sizeof(q->qbuf);
2N/A q->action = RESTGT_DOALWAYS;
2N/A break;
2N/A case AF_INET6:
2N/A size = IN6ADDRSZ;
2N/A q->qclass = C_IN;
2N/A q->qtype = T_AAAA;
2N/A q->answer = q->qbuf.buf;
2N/A q->anslen = sizeof(q->qbuf);
2N/A q->action = RESTGT_DOALWAYS;
2N/A break;
2N/A default:
2N/A RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
2N/A errno = EAFNOSUPPORT;
2N/A hp = NULL;
2N/A goto cleanup;
2N/A }
2N/A
2N/A /*
2N/A * if there aren't any dots, it could be a user-level alias.
2N/A * this is also done in res_nquery() since we are not the only
2N/A * function that looks up host names.
2N/A */
2N/A if (!strchr(name, '.') && (cp = res_hostalias(pvt->res, name,
2N/A tmp, sizeof tmp)))
2N/A name = cp;
2N/A
2N/A for (p = q; p; p = p->next) {
2N/A switch(p->action) {
2N/A case RESTGT_DOALWAYS:
2N/A break;
2N/A case RESTGT_AFTERFAILURE:
2N/A if (querystate == RESQRY_SUCCESS)
2N/A continue;
2N/A break;
2N/A case RESTGT_IGNORE:
2N/A continue;
2N/A }
2N/A
2N/A if ((n = res_nsearch(pvt->res, name, p->qclass, p->qtype,
2N/A p->answer, p->anslen)) < 0) {
2N/A querystate = RESQRY_FAIL;
2N/A continue;
2N/A }
2N/A
2N/A memset(&ai, 0, sizeof(ai));
2N/A ai.ai_family = af;
2N/A if ((hp = gethostans(this, p->answer, n, name, p->qtype,
2N/A af, size, NULL,
2N/A (const struct addrinfo *)&ai)) != NULL)
2N/A goto cleanup; /*%< no more loop is necessary */
2N/A querystate = RESQRY_FAIL;
2N/A continue;
2N/A }
2N/A
2N/A cleanup:
2N/A if (q != NULL)
2N/A memput(q, sizeof(*q));
2N/A return(hp);
2N/A}
2N/A
2N/Astatic struct hostent *
2N/Aho_byaddr(struct irs_ho *this, const void *addr, int len, int af)
2N/A{
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A const u_char *uaddr = addr;
2N/A char *qp;
2N/A struct hostent *hp = NULL;
2N/A struct addrinfo ai;
2N/A struct dns_res_target *q, *q2, *p;
2N/A int n, size, i;
2N/A int querystate = RESQRY_FAIL;
2N/A
2N/A if (init(this) == -1)
2N/A return (NULL);
2N/A
2N/A q = memget(sizeof(*q));
2N/A q2 = memget(sizeof(*q2));
2N/A if (q == NULL || q2 == NULL) {
2N/A RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
2N/A errno = ENOMEM;
2N/A goto cleanup;
2N/A }
2N/A memset(q, 0, sizeof(*q));
2N/A memset(q2, 0, sizeof(*q2));
2N/A
2N/A if (af == AF_INET6 && len == IN6ADDRSZ &&
2N/A (!memcmp(uaddr, mapped, sizeof mapped) ||
2N/A (!memcmp(uaddr, tunnelled, sizeof tunnelled) &&
2N/A memcmp(&uaddr[sizeof tunnelled], v6local, sizeof(v6local))))) {
2N/A /* Unmap. */
2N/A addr = (const char *)addr + sizeof mapped;
2N/A uaddr += sizeof mapped;
2N/A af = AF_INET;
2N/A len = INADDRSZ;
2N/A }
2N/A switch (af) {
2N/A case AF_INET:
2N/A size = INADDRSZ;
2N/A q->qclass = C_IN;
2N/A q->qtype = T_PTR;
2N/A q->answer = q->qbuf.buf;
2N/A q->anslen = sizeof(q->qbuf);
2N/A q->action = RESTGT_DOALWAYS;
2N/A break;
2N/A case AF_INET6:
2N/A size = IN6ADDRSZ;
2N/A q->qclass = C_IN;
2N/A q->qtype = T_PTR;
2N/A q->answer = q->qbuf.buf;
2N/A q->anslen = sizeof(q->qbuf);
2N/A q->next = q2;
2N/A q->action = RESTGT_DOALWAYS;
2N/A q2->qclass = C_IN;
2N/A q2->qtype = T_PTR;
2N/A q2->answer = q2->qbuf.buf;
2N/A q2->anslen = sizeof(q2->qbuf);
2N/A if ((pvt->res->options & RES_NO_NIBBLE2) != 0U)
2N/A q2->action = RESTGT_IGNORE;
2N/A else
2N/A q2->action = RESTGT_AFTERFAILURE;
2N/A break;
2N/A default:
2N/A errno = EAFNOSUPPORT;
2N/A RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
2N/A hp = NULL;
2N/A goto cleanup;
2N/A }
2N/A if (size > len) {
2N/A errno = EINVAL;
2N/A RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
2N/A hp = NULL;
2N/A goto cleanup;
2N/A }
2N/A switch (af) {
2N/A case AF_INET:
2N/A qp = q->qname;
2N/A (void) sprintf(qp, "%u.%u.%u.%u.in-addr.arpa",
2N/A (uaddr[3] & 0xff),
2N/A (uaddr[2] & 0xff),
2N/A (uaddr[1] & 0xff),
2N/A (uaddr[0] & 0xff));
2N/A break;
2N/A case AF_INET6:
2N/A if (q->action != RESTGT_IGNORE) {
2N/A const char *nibsuff = res_get_nibblesuffix(pvt->res);
2N/A qp = q->qname;
2N/A for (n = IN6ADDRSZ - 1; n >= 0; n--) {
2N/A i = SPRINTF((qp, "%x.%x.",
2N/A uaddr[n] & 0xf,
2N/A (uaddr[n] >> 4) & 0xf));
2N/A if (i != 4)
2N/A abort();
2N/A qp += i;
2N/A }
2N/A if (strlen(q->qname) + strlen(nibsuff) + 1 >
2N/A sizeof q->qname) {
2N/A errno = ENAMETOOLONG;
2N/A RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
2N/A hp = NULL;
2N/A goto cleanup;
2N/A }
2N/A strcpy(qp, nibsuff); /* (checked) */
2N/A }
2N/A if (q2->action != RESTGT_IGNORE) {
2N/A const char *nibsuff2 = res_get_nibblesuffix2(pvt->res);
2N/A qp = q2->qname;
2N/A for (n = IN6ADDRSZ - 1; n >= 0; n--) {
2N/A i = SPRINTF((qp, "%x.%x.",
2N/A uaddr[n] & 0xf,
2N/A (uaddr[n] >> 4) & 0xf));
2N/A if (i != 4)
2N/A abort();
2N/A qp += i;
2N/A }
2N/A if (strlen(q2->qname) + strlen(nibsuff2) + 1 >
2N/A sizeof q2->qname) {
2N/A errno = ENAMETOOLONG;
2N/A RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
2N/A hp = NULL;
2N/A goto cleanup;
2N/A }
2N/A strcpy(qp, nibsuff2); /* (checked) */
2N/A }
2N/A break;
2N/A default:
2N/A abort();
2N/A }
2N/A
2N/A for (p = q; p; p = p->next) {
2N/A switch(p->action) {
2N/A case RESTGT_DOALWAYS:
2N/A break;
2N/A case RESTGT_AFTERFAILURE:
2N/A if (querystate == RESQRY_SUCCESS)
2N/A continue;
2N/A break;
2N/A case RESTGT_IGNORE:
2N/A continue;
2N/A }
2N/A
2N/A if ((n = res_nquery(pvt->res, p->qname, p->qclass, p->qtype,
2N/A p->answer, p->anslen)) < 0) {
2N/A querystate = RESQRY_FAIL;
2N/A continue;
2N/A }
2N/A
2N/A memset(&ai, 0, sizeof(ai));
2N/A ai.ai_family = af;
2N/A hp = gethostans(this, p->answer, n, p->qname, T_PTR, af, size,
2N/A NULL, (const struct addrinfo *)&ai);
2N/A if (!hp) {
2N/A querystate = RESQRY_FAIL;
2N/A continue;
2N/A }
2N/A
2N/A memcpy(pvt->host_addr, addr, len);
2N/A pvt->h_addr_ptrs[0] = (char *)pvt->host_addr;
2N/A pvt->h_addr_ptrs[1] = NULL;
2N/A if (af == AF_INET && (pvt->res->options & RES_USE_INET6)) {
2N/A map_v4v6_address((char*)pvt->host_addr,
2N/A (char*)pvt->host_addr);
2N/A pvt->host.h_addrtype = AF_INET6;
2N/A pvt->host.h_length = IN6ADDRSZ;
2N/A }
2N/A
2N/A RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS);
2N/A goto cleanup; /*%< no more loop is necessary. */
2N/A }
2N/A hp = NULL; /*%< H_ERRNO was set by subroutines */
2N/A cleanup:
2N/A if (q != NULL)
2N/A memput(q, sizeof(*q));
2N/A if (q2 != NULL)
2N/A memput(q2, sizeof(*q2));
2N/A return(hp);
2N/A}
2N/A
2N/Astatic struct hostent *
2N/Aho_next(struct irs_ho *this) {
2N/A
2N/A UNUSED(this);
2N/A
2N/A return (NULL);
2N/A}
2N/A
2N/Astatic void
2N/Aho_rewind(struct irs_ho *this) {
2N/A
2N/A UNUSED(this);
2N/A
2N/A /* NOOP */
2N/A}
2N/A
2N/Astatic void
2N/Aho_minimize(struct irs_ho *this) {
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A
2N/A if (pvt->res)
2N/A res_nclose(pvt->res);
2N/A}
2N/A
2N/Astatic struct __res_state *
2N/Aho_res_get(struct irs_ho *this) {
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A
2N/A if (!pvt->res) {
2N/A struct __res_state *res;
2N/A res = (struct __res_state *)malloc(sizeof *res);
2N/A if (!res) {
2N/A errno = ENOMEM;
2N/A return (NULL);
2N/A }
2N/A memset(res, 0, sizeof *res);
2N/A ho_res_set(this, res, free);
2N/A }
2N/A
2N/A return (pvt->res);
2N/A}
2N/A
2N/A/* XXX */
2N/Aextern struct addrinfo *addr2addrinfo __P((const struct addrinfo *,
2N/A const char *));
2N/A
2N/Astatic struct addrinfo *
2N/Aho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai)
2N/A{
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A int n;
2N/A char tmp[NS_MAXDNAME];
2N/A const char *cp;
2N/A struct dns_res_target *q, *q2, *p;
2N/A struct addrinfo sentinel, *cur;
2N/A int querystate = RESQRY_FAIL;
2N/A
2N/A if (init(this) == -1)
2N/A return (NULL);
2N/A
2N/A memset(&sentinel, 0, sizeof(sentinel));
2N/A cur = &sentinel;
2N/A
2N/A q = memget(sizeof(*q));
2N/A q2 = memget(sizeof(*q2));
2N/A if (q == NULL || q2 == NULL) {
2N/A RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
2N/A errno = ENOMEM;
2N/A goto cleanup;
2N/A }
2N/A memset(q, 0, sizeof(*q2));
2N/A memset(q2, 0, sizeof(*q2));
2N/A
2N/A switch (pai->ai_family) {
2N/A case AF_UNSPEC:
2N/A /* prefer IPv6 */
2N/A q->qclass = C_IN;
2N/A q->qtype = T_AAAA;
2N/A q->answer = q->qbuf.buf;
2N/A q->anslen = sizeof(q->qbuf);
2N/A q->next = q2;
2N/A q->action = RESTGT_DOALWAYS;
2N/A q2->qclass = C_IN;
2N/A q2->qtype = T_A;
2N/A q2->answer = q2->qbuf.buf;
2N/A q2->anslen = sizeof(q2->qbuf);
2N/A q2->action = RESTGT_DOALWAYS;
2N/A break;
2N/A case AF_INET:
2N/A q->qclass = C_IN;
2N/A q->qtype = T_A;
2N/A q->answer = q->qbuf.buf;
2N/A q->anslen = sizeof(q->qbuf);
2N/A q->action = RESTGT_DOALWAYS;
2N/A break;
2N/A case AF_INET6:
2N/A q->qclass = C_IN;
2N/A q->qtype = T_AAAA;
2N/A q->answer = q->qbuf.buf;
2N/A q->anslen = sizeof(q->qbuf);
2N/A q->action = RESTGT_DOALWAYS;
2N/A break;
2N/A default:
2N/A RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); /*%< better error? */
2N/A goto cleanup;
2N/A }
2N/A
2N/A /*
2N/A * if there aren't any dots, it could be a user-level alias.
2N/A * this is also done in res_nquery() since we are not the only
2N/A * function that looks up host names.
2N/A */
2N/A if (!strchr(name, '.') && (cp = res_hostalias(pvt->res, name,
2N/A tmp, sizeof tmp)))
2N/A name = cp;
2N/A
2N/A for (p = q; p; p = p->next) {
2N/A struct addrinfo *ai;
2N/A
2N/A switch(p->action) {
2N/A case RESTGT_DOALWAYS:
2N/A break;
2N/A case RESTGT_AFTERFAILURE:
2N/A if (querystate == RESQRY_SUCCESS)
2N/A continue;
2N/A break;
2N/A case RESTGT_IGNORE:
2N/A continue;
2N/A }
2N/A
2N/A if ((n = res_nsearch(pvt->res, name, p->qclass, p->qtype,
2N/A p->answer, p->anslen)) < 0) {
2N/A querystate = RESQRY_FAIL;
2N/A continue;
2N/A }
2N/A (void)gethostans(this, p->answer, n, name, p->qtype,
2N/A pai->ai_family, /*%< XXX: meaningless */
2N/A 0, &ai, pai);
2N/A if (ai) {
2N/A querystate = RESQRY_SUCCESS;
2N/A cur->ai_next = ai;
2N/A while (cur->ai_next)
2N/A cur = cur->ai_next;
2N/A } else
2N/A querystate = RESQRY_FAIL;
2N/A }
2N/A
2N/A cleanup:
2N/A if (q != NULL)
2N/A memput(q, sizeof(*q));
2N/A if (q2 != NULL)
2N/A memput(q2, sizeof(*q2));
2N/A return(sentinel.ai_next);
2N/A}
2N/A
2N/Astatic void
2N/Aho_res_set(struct irs_ho *this, struct __res_state *res,
2N/A void (*free_res)(void *)) {
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A
2N/A if (pvt->res && pvt->free_res) {
2N/A res_nclose(pvt->res);
2N/A (*pvt->free_res)(pvt->res);
2N/A }
2N/A
2N/A pvt->res = res;
2N/A pvt->free_res = free_res;
2N/A}
2N/A
2N/A/* Private. */
2N/A
2N/Astatic struct hostent *
2N/Agethostans(struct irs_ho *this,
2N/A const u_char *ansbuf, int anslen, const char *qname, int qtype,
2N/A int af, int size, /*!< meaningless for addrinfo cases */
2N/A struct addrinfo **ret_aip, const struct addrinfo *pai)
2N/A{
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A int type, class, ancount, qdcount, n, haveanswer, had_error;
2N/A int error = NETDB_SUCCESS;
2N/A int (*name_ok)(const char *);
2N/A const HEADER *hp;
2N/A const u_char *eom;
2N/A const u_char *eor;
2N/A const u_char *cp;
2N/A const char *tname;
2N/A const char *hname;
2N/A char *bp, *ep, **ap, **hap;
2N/A char tbuf[MAXDNAME+1];
2N/A struct addrinfo sentinel, *cur, ai;
2N/A
2N/A if (pai == NULL) abort();
2N/A if (ret_aip != NULL)
2N/A *ret_aip = NULL;
2N/A memset(&sentinel, 0, sizeof(sentinel));
2N/A cur = &sentinel;
2N/A
2N/A tname = qname;
2N/A eom = ansbuf + anslen;
2N/A switch (qtype) {
2N/A case T_A:
2N/A case T_AAAA:
2N/A case T_ANY: /*%< use T_ANY only for T_A/T_AAAA lookup */
2N/A name_ok = res_hnok;
2N/A break;
2N/A case T_PTR:
2N/A name_ok = res_dnok;
2N/A break;
2N/A default:
2N/A abort();
2N/A }
2N/A
2N/A pvt->host.h_addrtype = af;
2N/A pvt->host.h_length = size;
2N/A hname = pvt->host.h_name = NULL;
2N/A
2N/A /*
2N/A * Find first satisfactory answer.
2N/A */
2N/A if (ansbuf + HFIXEDSZ > eom) {
2N/A RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
2N/A return (NULL);
2N/A }
2N/A hp = (const HEADER *)ansbuf;
2N/A ancount = ntohs(hp->ancount);
2N/A qdcount = ntohs(hp->qdcount);
2N/A bp = pvt->hostbuf;
2N/A ep = pvt->hostbuf + sizeof(pvt->hostbuf);
2N/A cp = ansbuf + HFIXEDSZ;
2N/A if (qdcount != 1) {
2N/A RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
2N/A return (NULL);
2N/A }
2N/A n = dn_expand(ansbuf, eom, cp, bp, ep - bp);
2N/A if (n < 0 || !maybe_ok(pvt->res, bp, name_ok)) {
2N/A RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
2N/A return (NULL);
2N/A }
2N/A cp += n + QFIXEDSZ;
2N/A if (cp > eom) {
2N/A RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
2N/A return (NULL);
2N/A }
2N/A if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
2N/A /* res_nsend() has already verified that the query name is the
2N/A * same as the one we sent; this just gets the expanded name
2N/A * (i.e., with the succeeding search-domain tacked on).
2N/A */
2N/A n = strlen(bp) + 1; /*%< for the \\0 */
2N/A if (n > MAXHOSTNAMELEN) {
2N/A RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
2N/A return (NULL);
2N/A }
2N/A pvt->host.h_name = bp;
2N/A hname = bp;
2N/A bp += n;
2N/A /* The qname can be abbreviated, but hname is now absolute. */
2N/A qname = pvt->host.h_name;
2N/A }
2N/A ap = pvt->host_aliases;
2N/A *ap = NULL;
2N/A pvt->host.h_aliases = pvt->host_aliases;
2N/A hap = pvt->h_addr_ptrs;
2N/A *hap = NULL;
2N/A pvt->host.h_addr_list = pvt->h_addr_ptrs;
2N/A haveanswer = 0;
2N/A had_error = 0;
2N/A while (ancount-- > 0 && cp < eom && !had_error) {
2N/A n = dn_expand(ansbuf, eom, cp, bp, ep - bp);
2N/A if (n < 0 || !maybe_ok(pvt->res, bp, name_ok)) {
2N/A had_error++;
2N/A continue;
2N/A }
2N/A cp += n; /*%< name */
2N/A BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
2N/A type = ns_get16(cp);
2N/A cp += INT16SZ; /*%< type */
2N/A class = ns_get16(cp);
2N/A cp += INT16SZ + INT32SZ; /*%< class, TTL */
2N/A n = ns_get16(cp);
2N/A cp += INT16SZ; /*%< len */
2N/A BOUNDS_CHECK(cp, n);
2N/A if (class != C_IN) {
2N/A cp += n;
2N/A continue;
2N/A }
2N/A eor = cp + n;
2N/A if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
2N/A type == T_CNAME) {
2N/A if (haveanswer) {
2N/A int level = LOG_CRIT;
2N/A#ifdef LOG_SECURITY
2N/A level |= LOG_SECURITY;
2N/A#endif
2N/A syslog(level,
2N/A "gethostans: possible attempt to exploit buffer overflow while looking up %s",
2N/A *qname ? qname : ".");
2N/A }
2N/A n = dn_expand(ansbuf, eor, cp, tbuf, sizeof tbuf);
2N/A if (n < 0 || !maybe_ok(pvt->res, tbuf, name_ok)) {
2N/A had_error++;
2N/A continue;
2N/A }
2N/A cp += n;
2N/A /* Store alias. */
2N/A if (ap >= &pvt->host_aliases[MAXALIASES-1])
2N/A continue;
2N/A *ap++ = bp;
2N/A n = strlen(bp) + 1; /*%< for the \\0 */
2N/A bp += n;
2N/A /* Get canonical name. */
2N/A n = strlen(tbuf) + 1; /*%< for the \\0 */
2N/A if (n > (ep - bp) || n > MAXHOSTNAMELEN) {
2N/A had_error++;
2N/A continue;
2N/A }
2N/A strcpy(bp, tbuf); /* (checked) */
2N/A pvt->host.h_name = bp;
2N/A hname = bp;
2N/A bp += n;
2N/A continue;
2N/A }
2N/A if (qtype == T_PTR && type == T_CNAME) {
2N/A n = dn_expand(ansbuf, eor, cp, tbuf, sizeof tbuf);
2N/A if (n < 0 || !maybe_dnok(pvt->res, tbuf)) {
2N/A had_error++;
2N/A continue;
2N/A }
2N/A cp += n;
2N/A#ifdef RES_USE_DNAME
2N/A if ((pvt->res->options & RES_USE_DNAME) != 0U)
2N/A#endif
2N/A {
2N/A /*
2N/A * We may be able to check this regardless
2N/A * of the USE_DNAME bit, but we add the check
2N/A * for now since the DNAME support is
2N/A * experimental.
2N/A */
2N/A if (ns_samename(tname, bp) != 1)
2N/A continue;
2N/A }
2N/A /* Get canonical name. */
2N/A n = strlen(tbuf) + 1; /*%< for the \\0 */
2N/A if (n > (ep - bp)) {
2N/A had_error++;
2N/A continue;
2N/A }
2N/A strcpy(bp, tbuf); /* (checked) */
2N/A tname = bp;
2N/A bp += n;
2N/A continue;
2N/A }
2N/A if (qtype == T_ANY) {
2N/A if (!(type == T_A || type == T_AAAA)) {
2N/A cp += n;
2N/A continue;
2N/A }
2N/A } else if (type != qtype) {
2N/A cp += n;
2N/A continue;
2N/A }
2N/A switch (type) {
2N/A case T_PTR:
2N/A if (ret_aip != NULL) {
2N/A /* addrinfo never needs T_PTR */
2N/A cp += n;
2N/A continue;
2N/A }
2N/A if (ns_samename(tname, bp) != 1) {
2N/A cp += n;
2N/A continue;
2N/A }
2N/A n = dn_expand(ansbuf, eor, cp, bp, ep - bp);
2N/A if (n < 0 || !maybe_hnok(pvt->res, bp) ||
2N/A n >= MAXHOSTNAMELEN) {
2N/A had_error++;
2N/A break;
2N/A }
2N/A cp += n;
2N/A if (!haveanswer) {
2N/A pvt->host.h_name = bp;
2N/A hname = bp;
2N/A }
2N/A else if (ap < &pvt->host_aliases[MAXALIASES-1])
2N/A *ap++ = bp;
2N/A else
2N/A n = -1;
2N/A if (n != -1) {
2N/A n = strlen(bp) + 1; /*%< for the \\0 */
2N/A bp += n;
2N/A }
2N/A break;
2N/A case T_A:
2N/A case T_AAAA:
2N/A if (ns_samename(hname, bp) != 1) {
2N/A cp += n;
2N/A continue;
2N/A }
2N/A if (type == T_A && n != INADDRSZ) {
2N/A cp += n;
2N/A continue;
2N/A }
2N/A if (type == T_AAAA && n != IN6ADDRSZ) {
2N/A cp += n;
2N/A continue;
2N/A }
2N/A
2N/A /* make addrinfo. don't overwrite constant PAI */
2N/A ai = *pai;
2N/A ai.ai_family = (type == T_AAAA) ? AF_INET6 : AF_INET;
2N/A cur->ai_next = addr2addrinfo(
2N/A (const struct addrinfo *)&ai,
2N/A (const char *)cp);
2N/A if (cur->ai_next == NULL)
2N/A had_error++;
2N/A
2N/A if (!haveanswer) {
2N/A int nn;
2N/A
2N/A nn = strlen(bp) + 1; /*%< for the \\0 */
2N/A if (nn >= MAXHOSTNAMELEN) {
2N/A cp += n;
2N/A had_error++;
2N/A continue;
2N/A }
2N/A pvt->host.h_name = bp;
2N/A hname = bp;
2N/A bp += nn;
2N/A }
2N/A /* Ensure alignment. */
2N/A bp = (char *)(((u_long)bp + (sizeof(align) - 1)) &
2N/A ~(sizeof(align) - 1));
2N/A /* Avoid overflows. */
2N/A if (bp + n > &pvt->hostbuf[sizeof(pvt->hostbuf) - 1]) {
2N/A had_error++;
2N/A continue;
2N/A }
2N/A if (ret_aip) { /*%< need addrinfo. keep it. */
2N/A while (cur->ai_next)
2N/A cur = cur->ai_next;
2N/A } else if (cur->ai_next) { /*%< need hostent */
2N/A struct addrinfo *aip = cur->ai_next;
2N/A
2N/A for (aip = cur->ai_next; aip;
2N/A aip = aip->ai_next) {
2N/A int m;
2N/A
2N/A m = add_hostent(pvt, bp, hap, aip);
2N/A if (m < 0) {
2N/A had_error++;
2N/A break;
2N/A }
2N/A if (m == 0)
2N/A continue;
2N/A if (hap < &pvt->h_addr_ptrs[MAXADDRS])
2N/A hap++;
2N/A *hap = NULL;
2N/A bp += m;
2N/A }
2N/A
2N/A freeaddrinfo(cur->ai_next);
2N/A cur->ai_next = NULL;
2N/A }
2N/A cp += n;
2N/A break;
2N/A default:
2N/A abort();
2N/A }
2N/A if (!had_error)
2N/A haveanswer++;
2N/A }
2N/A if (haveanswer) {
2N/A if (ret_aip == NULL) {
2N/A *ap = NULL;
2N/A *hap = NULL;
2N/A
2N/A if (pvt->res->nsort && hap != pvt->h_addr_ptrs &&
2N/A qtype == T_A)
2N/A addrsort(pvt->res, pvt->h_addr_ptrs,
2N/A hap - pvt->h_addr_ptrs);
2N/A if (pvt->host.h_name == NULL) {
2N/A n = strlen(qname) + 1; /*%< for the \\0 */
2N/A if (n > (ep - bp) || n >= MAXHOSTNAMELEN)
2N/A goto no_recovery;
2N/A strcpy(bp, qname); /* (checked) */
2N/A pvt->host.h_name = bp;
2N/A bp += n;
2N/A }
2N/A if (pvt->res->options & RES_USE_INET6)
2N/A map_v4v6_hostent(&pvt->host, &bp, ep);
2N/A RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS);
2N/A return (&pvt->host);
2N/A } else {
2N/A if ((pai->ai_flags & AI_CANONNAME) != 0) {
2N/A if (pvt->host.h_name == NULL) {
2N/A sentinel.ai_next->ai_canonname =
2N/A strdup(qname);
2N/A }
2N/A else {
2N/A sentinel.ai_next->ai_canonname =
2N/A strdup(pvt->host.h_name);
2N/A }
2N/A }
2N/A *ret_aip = sentinel.ai_next;
2N/A return(NULL);
2N/A }
2N/A }
2N/A no_recovery:
2N/A if (sentinel.ai_next) {
2N/A /* this should be impossible, but check it for safety */
2N/A freeaddrinfo(sentinel.ai_next);
2N/A }
2N/A if (error == NETDB_SUCCESS)
2N/A RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
2N/A else
2N/A RES_SET_H_ERRNO(pvt->res, error);
2N/A return(NULL);
2N/A}
2N/A
2N/Astatic int
2N/Aadd_hostent(struct pvt *pvt, char *bp, char **hap, struct addrinfo *ai)
2N/A{
2N/A int addrlen;
2N/A char *addrp;
2N/A const char **tap;
2N/A char *obp = bp;
2N/A
2N/A switch(ai->ai_addr->sa_family) {
2N/A case AF_INET6:
2N/A addrlen = IN6ADDRSZ;
2N/A addrp = (char *)&((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
2N/A break;
2N/A case AF_INET:
2N/A addrlen = INADDRSZ;
2N/A addrp = (char *)&((struct sockaddr_in *)ai->ai_addr)->sin_addr;
2N/A break;
2N/A default:
2N/A return(-1); /*%< abort? */
2N/A }
2N/A
2N/A /* Ensure alignment. */
2N/A bp = (char *)(((u_long)bp + (sizeof(align) - 1)) &
2N/A ~(sizeof(align) - 1));
2N/A /* Avoid overflows. */
2N/A if (bp + addrlen > &pvt->hostbuf[sizeof(pvt->hostbuf) - 1])
2N/A return(-1);
2N/A if (hap >= &pvt->h_addr_ptrs[MAXADDRS])
2N/A return(0); /*%< fail, but not treat it as an error. */
2N/A /* Suppress duplicates. */
2N/A for (tap = (const char **)pvt->h_addr_ptrs;
2N/A *tap != NULL;
2N/A tap++)
2N/A if (memcmp(*tap, addrp, addrlen) == 0)
2N/A break;
2N/A if (*tap != NULL)
2N/A return (0);
2N/A
2N/A memcpy(*hap = bp, addrp, addrlen);
2N/A return((bp + addrlen) - obp);
2N/A}
2N/A
2N/Astatic void
2N/Amap_v4v6_hostent(struct hostent *hp, char **bpp, char *ep) {
2N/A char **ap;
2N/A
2N/A if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
2N/A return;
2N/A hp->h_addrtype = AF_INET6;
2N/A hp->h_length = IN6ADDRSZ;
2N/A for (ap = hp->h_addr_list; *ap; ap++) {
2N/A int i = (u_long)*bpp % sizeof(align);
2N/A
2N/A if (i != 0)
2N/A i = sizeof(align) - i;
2N/A
2N/A if ((ep - *bpp) < (i + IN6ADDRSZ)) {
2N/A /* Out of memory. Truncate address list here. */
2N/A *ap = NULL;
2N/A return;
2N/A }
2N/A *bpp += i;
2N/A map_v4v6_address(*ap, *bpp);
2N/A *ap = *bpp;
2N/A *bpp += IN6ADDRSZ;
2N/A }
2N/A}
2N/A
2N/Astatic void
2N/Aaddrsort(res_state statp, char **ap, int num) {
2N/A int i, j, needsort = 0, aval[MAXADDRS];
2N/A char **p;
2N/A
2N/A p = ap;
2N/A for (i = 0; i < num; i++, p++) {
2N/A for (j = 0 ; (unsigned)j < statp->nsort; j++)
2N/A if (statp->sort_list[j].addr.s_addr ==
2N/A (((struct in_addr *)(*p))->s_addr &
2N/A statp->sort_list[j].mask))
2N/A break;
2N/A aval[i] = j;
2N/A if (needsort == 0 && i > 0 && j < aval[i-1])
2N/A needsort = i;
2N/A }
2N/A if (!needsort)
2N/A return;
2N/A
2N/A while (needsort < num) {
2N/A for (j = needsort - 1; j >= 0; j--) {
2N/A if (aval[j] > aval[j+1]) {
2N/A char *hp;
2N/A
2N/A i = aval[j];
2N/A aval[j] = aval[j+1];
2N/A aval[j+1] = i;
2N/A
2N/A hp = ap[j];
2N/A ap[j] = ap[j+1];
2N/A ap[j+1] = hp;
2N/A
2N/A } else
2N/A break;
2N/A }
2N/A needsort++;
2N/A }
2N/A}
2N/A
2N/Astatic int
2N/Ainit(struct irs_ho *this) {
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A
2N/A if (!pvt->res && !ho_res_get(this))
2N/A return (-1);
2N/A if (((pvt->res->options & RES_INIT) == 0U) &&
2N/A res_ninit(pvt->res) == -1)
2N/A return (-1);
2N/A return (0);
2N/A}
2N/A