dns_ho.c revision b07ce584f4e28873b8927d7f83d9d3275a0f3ed2
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 1985, 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Portions Copyright (c) 1996-1999 by Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
/* from gethostnamadr.c 8.1 (Berkeley) 6/4/93 */
/* BIND Id: gethnamaddr.c,v 8.15 1996/05/22 04:56:30 vixie Exp $ */
#endif /* LIBC_SCCS and not lint */
/* Imports. */
#include "port_before.h"
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <netdb.h>
#include <resolv.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <isc/memcluster.h>
#include <irs.h>
#include "port_after.h"
#include "irs_p.h"
#include "dns_p.h"
#ifdef SPRINTF_CHAR
#else
#endif
/* Definitions. */
#define MAXALIASES 35
#define MAXADDRS 35
had_error++; \
continue; \
} else (void)0
typedef union {
} querybuf;
struct dns_res_target {
struct dns_res_target *next;
int anslen; /* size of answer buffer */
int action; /* condition whether query is really issued */
#if 0
int n; /* result length */
#endif
};
enum {RESQRY_SUCCESS, RESQRY_FAIL};
struct pvt {
char * host_aliases[MAXALIASES];
struct __res_state *res;
void (*free_res)(void *);
};
typedef union {
char ac;
} align;
/* Note: the IPv6 loopback address is in the "tunnel" space */
/* Forwards. */
int af);
struct __res_state *res,
void (*free_res)(void *));
char *ep);
/* Exports. */
struct irs_ho *
return (NULL);
}
return (NULL);
}
return (ho);
}
/* Methods. */
static void
if (pvt)
}
static struct hostent *
return (NULL);
if (hp)
return (hp);
}
}
static struct hostent *
{
int n, size;
char tmp[NS_MAXDNAME];
const char *cp;
struct dns_res_target *q, *p;
int querystate = RESQRY_FAIL;
return (NULL);
q = memget(sizeof(*q));
if (q == NULL) {
goto cleanup;
}
memset(q, 0, sizeof(q));
switch (af) {
case AF_INET:
q->action = RESTGT_DOALWAYS;
break;
case AF_INET6:
q->action = RESTGT_DOALWAYS;
break;
default:
goto cleanup;
}
/*
* if there aren't any dots, it could be a user-level alias.
* this is also done in res_nquery() since we are not the only
* function that looks up host names.
*/
for (p = q; p; p = p->next) {
switch(p->action) {
case RESTGT_DOALWAYS:
break;
case RESTGT_AFTERFAILURE:
if (querystate == RESQRY_SUCCESS)
continue;
break;
case RESTGT_IGNORE:
continue;
}
continue;
}
goto cleanup; /* no more loop is necessary */
continue;
}
if (q != NULL)
memput(q, sizeof(*q));
return(hp);
}
static struct hostent *
{
char *qp;
struct dns_res_target *q, *q2, *p;
int n, size, i;
int querystate = RESQRY_FAIL;
return (NULL);
q = memget(sizeof(*q));
goto cleanup;
}
memset(q, 0, sizeof(q));
/* Unmap. */
}
switch (af) {
case AF_INET:
q->action = RESTGT_DOALWAYS;
break;
case AF_INET6:
q->action = RESTGT_DOALWAYS;
else
break;
default:
goto cleanup;
}
goto cleanup;
}
switch (af) {
case AF_INET:
(uaddr[0] & 0xff));
break;
case AF_INET6:
if (q->action != RESTGT_IGNORE) {
for (n = IN6ADDRSZ - 1; n >= 0; n--) {
uaddr[n] & 0xf,
if (i < 0)
abort();
qp += i;
}
#ifdef HAVE_STRLCAT
sizeof(q->qname));
#else
#endif
}
for (n = IN6ADDRSZ - 1; n >= 0; n--) {
uaddr[n] & 0xf,
if (i < 0)
abort();
qp += i;
}
#ifdef HAVE_STRLCAT
sizeof(q->qname));
#else
#endif
}
break;
default:
abort();
}
for (p = q; p; p = p->next) {
switch(p->action) {
case RESTGT_DOALWAYS:
break;
case RESTGT_AFTERFAILURE:
if (querystate == RESQRY_SUCCESS)
continue;
break;
case RESTGT_IGNORE:
continue;
}
continue;
}
if (!hp) {
continue;
}
}
goto cleanup; /* no more loop is necessary. */
}
if (q != NULL)
memput(q, sizeof(*q));
return(hp);
}
static struct hostent *
return (NULL);
}
static void
/* NOOP */
}
static void
}
static struct __res_state *
struct __res_state *res;
if (!res) {
return (NULL);
}
}
}
/* XXX */
const char *));
static struct addrinfo *
{
int n;
char tmp[NS_MAXDNAME];
const char *cp;
struct dns_res_target *q, *q2, *p;
int querystate = RESQRY_FAIL;
return (NULL);
q = memget(sizeof(*q));
goto cleanup;
}
case AF_UNSPEC:
/* prefer IPv6 */
q->action = RESTGT_DOALWAYS;
break;
case AF_INET:
q->action = RESTGT_DOALWAYS;
break;
case AF_INET6:
q->action = RESTGT_DOALWAYS;
break;
default:
goto cleanup;
}
/*
* if there aren't any dots, it could be a user-level alias.
* this is also done in res_nquery() since we are not the only
* function that looks up host names.
*/
for (p = q; p; p = p->next) {
switch(p->action) {
case RESTGT_DOALWAYS:
break;
case RESTGT_AFTERFAILURE:
if (querystate == RESQRY_SUCCESS)
continue;
break;
case RESTGT_IGNORE:
continue;
}
continue;
}
if (ai) {
}
else
}
if (q != NULL)
memput(q, sizeof(*q));
}
static void
void (*free_res)(void *)) {
}
}
/* Private. */
static struct hostent *
{
int (*name_ok)(const char *);
const char *tname;
const char *hname;
switch (qtype) {
case T_A:
case T_AAAA:
break;
case T_PTR:
break;
default:
abort();
}
/*
* Find first satisfactory answer.
*/
return (NULL);
}
if (qdcount != 1) {
return (NULL);
}
return (NULL);
}
return (NULL);
}
/* res_nsend() has already verified that the query name is the
* same as the one we sent; this just gets the expanded name
* (i.e., with the succeeding search-domain tacked on).
*/
if (n > MAXHOSTNAMELEN) {
return (NULL);
}
bp += n;
/* The qname can be abbreviated, but hname is now absolute. */
}
haveanswer = 0;
had_error = 0;
had_error++;
continue;
}
cp += n; /* name */
BOUNDS_CHECK(cp, n);
cp += n;
continue;
}
if (haveanswer) {
#ifdef LOG_SECURITY
level |= LOG_SECURITY;
#endif
"gethostans: possible attempt to exploit buffer overflow while looking up %s",
}
had_error++;
continue;
}
cp += n;
/* Store alias. */
continue;
bp += n;
/* Get canonical name. */
had_error++;
continue;
}
#ifdef HAVE_STRLCPY
#else
#endif
bp += n;
continue;
}
had_error++;
continue;
}
cp += n;
#ifdef RES_USE_DNAME
#endif
{
/*
* We may be able to check this regardless
* of the USE_DNAME bit, but we add the check
* for now since the DNAME support is
* experimental.
*/
continue;
}
/* Get canonical name. */
had_error++;
continue;
}
#ifdef HAVE_STRLCPY
#else
#endif
bp += n;
continue;
}
cp += n;
continue;
}
cp += n;
continue;
}
switch (type) {
case T_PTR:
/* addrinfo never needs T_PTR */
cp += n;
continue;
}
cp += n;
continue;
}
n >= MAXHOSTNAMELEN) {
had_error++;
break;
}
cp += n;
if (!haveanswer) {
}
else
n = -1;
if (n != -1) {
bp += n;
}
break;
case T_A:
case T_AAAA:
cp += n;
continue;
}
cp += n;
continue;
}
cp += n;
continue;
}
/* make addrinfo. don't overwrite constant PAI */
(const char *)cp);
had_error++;
if (!haveanswer) {
int nn;
if (nn >= MAXHOSTNAMELEN) {
cp += n;
had_error++;
continue;
}
}
/* Ensure alignment. */
~(sizeof(align) - 1));
/* Avoid overflows. */
had_error++;
continue;
}
if (ret_aip) { /* need addrinfo. keep it. */
int m;
if (m < 0) {
had_error++;
break;
}
if (m == 0)
continue;
hap++;
bp += m;
}
}
cp += n;
break;
default:
abort();
}
if (!had_error)
haveanswer++;
}
if (haveanswer) {
goto no_recovery;
#ifdef HAVE_STRLCPY
#else
#endif
bp += n;
}
} else {
}
else {
}
}
return(NULL);
}
}
/* this should be impossible, but check it for safety */
}
if (error == NETDB_SUCCESS)
else
return(NULL);
}
static int
{
int addrlen;
char *addrp;
const char **tap;
case AF_INET6:
break;
case AF_INET:
break;
default:
return(-1); /* abort? */
}
/* Ensure alignment. */
~(sizeof(align) - 1));
/* Avoid overflows. */
return(-1);
return(0); /* fail, but not treat it as an error. */
/* Suppress duplicates. */
tap++)
break;
return (0);
}
static void
char **ap;
return;
if (i != 0)
i = sizeof(align) - i;
/* Out of memory. Truncate address list here. */
return;
}
*bpp += i;
}
}
static void
char **p;
p = ap;
for (i = 0; i < num; i++, p++) {
break;
aval[i] = j;
needsort = i;
}
if (!needsort)
return;
for (j = needsort - 1; j >= 0; j--) {
char *hp;
i = aval[j];
aval[j+1] = i;
} else
break;
}
needsort++;
}
}
static int
return (-1);
return (-1);
return (0);
}