2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A/*
2N/A * University Copyright- Copyright (c) 1982, 1986, 1988
2N/A * The Regents of the University of California
2N/A * All Rights Reserved
2N/A *
2N/A * University Acknowledgment- Portions of this document are derived from
2N/A * software developed by the University of California, Berkeley, and its
2N/A * contributors.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <sys/param.h>
2N/A#include <sys/socket.h>
2N/A#include <netinet/in.h>
2N/A#include <ctype.h>
2N/A#include <netdb.h>
2N/A#include <stdio.h>
2N/A#include <errno.h>
2N/A#include <string.h>
2N/A#include <arpa/inet.h>
2N/A#include <arpa/nameser.h>
2N/A#include <resolv.h>
2N/A
2N/A#if PACKETSZ > 1024
2N/A#define MAXPACKET PACKETSZ
2N/A#else
2N/A#define MAXPACKET 1024
2N/A#endif
2N/A
2N/Aint h_errno;
2N/A
2N/A/*
2N/A * Formulate a normal query, send, and await answer.
2N/A * Returned answer is placed in supplied buffer "answer".
2N/A * Perform preliminary check of answer, returning success only
2N/A * if no error is indicated and the answer count is nonzero.
2N/A * Return the size of the response on success, -1 on error.
2N/A * Error number is left in h_errno.
2N/A * Caller must parse answer and determine whether it answers the question.
2N/A */
2N/Aint
2N/Ares_query(name, class, type, answer, anslen)
2N/A char *name; /* domain name */
2N/A int class, type; /* class and type of query */
2N/A u_char *answer; /* buffer to put answer */
2N/A int anslen; /* size of answer buffer */
2N/A{
2N/A char buf[MAXPACKET];
2N/A HEADER *hp;
2N/A int n;
2N/A
2N/A if ((_res.options & RES_INIT) == 0 && res_init() == -1)
2N/A return (-1);
2N/A#ifdef DEBUG
2N/A if (_res.options & RES_DEBUG)
2N/A printf("res_query(%s, %d, %d)\n", name, class, type);
2N/A#endif
2N/A n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL,
2N/A buf, sizeof (buf));
2N/A
2N/A if (n <= 0) {
2N/A#ifdef DEBUG
2N/A if (_res.options & RES_DEBUG)
2N/A printf("res_query: mkquery failed\n");
2N/A#endif
2N/A h_errno = NO_RECOVERY;
2N/A return (n);
2N/A }
2N/A n = res_send(buf, n, answer, anslen);
2N/A if (n < 0) {
2N/A#ifdef DEBUG
2N/A if (_res.options & RES_DEBUG)
2N/A printf("res_query: send error\n");
2N/A#endif
2N/A h_errno = TRY_AGAIN;
2N/A return (n);
2N/A }
2N/A
2N/A hp = (HEADER *) answer;
2N/A if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
2N/A#ifdef DEBUG
2N/A if (_res.options & RES_DEBUG)
2N/A printf("rcode = %d, ancount=%d\n", hp->rcode,
2N/A ntohs(hp->ancount));
2N/A#endif
2N/A switch (hp->rcode) {
2N/A case NXDOMAIN:
2N/A h_errno = HOST_NOT_FOUND;
2N/A break;
2N/A case SERVFAIL:
2N/A h_errno = TRY_AGAIN;
2N/A break;
2N/A case NOERROR:
2N/A h_errno = NO_DATA;
2N/A break;
2N/A case FORMERR:
2N/A case NOTIMP:
2N/A case REFUSED:
2N/A default:
2N/A h_errno = NO_RECOVERY;
2N/A break;
2N/A }
2N/A return (-1);
2N/A }
2N/A if (hp->rcode == NOERROR && ntohs(hp->ancount) > 0)
2N/A h_errno = 0;
2N/A return (n);
2N/A}
2N/A
2N/A/*
2N/A * Formulate a normal query, send, and retrieve answer in supplied buffer.
2N/A * Return the size of the response on success, -1 on error.
2N/A * If enabled, implement search rules until answer or unrecoverable failure
2N/A * is detected. Error number is left in h_errno.
2N/A * Only useful for queries in the same name hierarchy as the local host
2N/A * (not, for example, for host address-to-name lookups in domain in-addr.arpa).
2N/A */
2N/Aint
2N/Ares_search(name, class, type, answer, anslen)
2N/A char *name; /* domain name */
2N/A int class, type; /* class and type of query */
2N/A u_char *answer; /* buffer to put answer */
2N/A int anslen; /* size of answer */
2N/A{
2N/A register char *cp, **domain;
2N/A int n, ret, got_nodata = 0;
2N/A char *hostalias();
2N/A
2N/A if ((_res.options & RES_INIT) == 0 && res_init() == -1)
2N/A return (-1);
2N/A
2N/A errno = 0;
2N/A h_errno = HOST_NOT_FOUND; /* default, if we never query */
2N/A for (cp = name, n = 0; *cp; cp++)
2N/A if (*cp == '.')
2N/A n++;
2N/A if (n == 0 && (cp = hostalias(name)))
2N/A return (res_query(cp, class, type, answer, anslen));
2N/A
2N/A /*
2N/A * We do at least one level of search if
2N/A * - there is no dot and RES_DEFNAME is set, or
2N/A * - there is at least one dot, there is no trailing dot,
2N/A * and RES_DNSRCH is set.
2N/A */
2N/A if ((n == 0 && _res.options & RES_DEFNAMES) ||
2N/A (n != 0 && *--cp != '.' && _res.options & RES_DNSRCH)) {
2N/A for (domain = _res.dnsrch; *domain; domain++) {
2N/A ret = res_querydomain(name, *domain, class, type,
2N/A answer, anslen);
2N/A if (ret > 0)
2N/A return (ret);
2N/A /*
2N/A * If no server present, give up.
2N/A * If name isn't found in this domain,
2N/A * keep trying higher domains in the search list
2N/A * (if that's enabled).
2N/A * On a NO_DATA error, keep trying, otherwise
2N/A * a wildcard entry of another type could keep us
2N/A * from finding this entry higher in the domain.
2N/A * If we get some other error (negative answer or
2N/A * server failure), then stop searching up,
2N/A * but try the input name below in case it's fully-qualified.
2N/A */
2N/A if (errno == ECONNREFUSED) {
2N/A h_errno = TRY_AGAIN;
2N/A return (-1);
2N/A }
2N/A if (h_errno == NO_DATA)
2N/A got_nodata++;
2N/A if ((h_errno != HOST_NOT_FOUND && h_errno != NO_DATA) ||
2N/A (_res.options & RES_DNSRCH) == 0)
2N/A break;
2N/A }
2N/A }
2N/A /*
2N/A * If the search/default failed, try the name as fully-qualified,
2N/A * but only if it contained at least one dot (even trailing).
2N/A * This is purely a heuristic; we assume that any reasonable query
2N/A * about a top-level domain (for servers, SOA, etc) will not use
2N/A * res_search.
2N/A */
2N/A if (n && (ret = res_querydomain(name, (char *)NULL, class, type,
2N/A answer, anslen)) > 0)
2N/A return (ret);
2N/A if (got_nodata)
2N/A h_errno = NO_DATA;
2N/A return (-1);
2N/A}
2N/A
2N/A/*
2N/A * Perform a call on res_query on the concatenation of name and domain,
2N/A * removing a trailing dot from name if domain is NULL.
2N/A */
2N/Aint
2N/Ares_querydomain(name, domain, class, type, answer, anslen)
2N/A char *name, *domain;
2N/A int class, type; /* class and type of query */
2N/A u_char *answer; /* buffer to put answer */
2N/A int anslen; /* size of answer */
2N/A{
2N/A char nbuf[2*MAXDNAME+2];
2N/A char *longname = nbuf;
2N/A int n;
2N/A
2N/A#ifdef DEBUG
2N/A if (_res.options & RES_DEBUG) {
2N/A if (domain == (char *)NULL)
2N/A printf("res_querydomain(%s, NULL, %d, %d)\n",
2N/A name, class, type);
2N/A else
2N/A printf("res_querydomain(%s, %s, %d, %d)\n",
2N/A name, domain, class, type);
2N/A }
2N/A#endif
2N/A if (domain == NULL) {
2N/A /*
2N/A * Check for trailing '.';
2N/A * copy without '.' if present.
2N/A */
2N/A n = strlen(name) - 1;
2N/A if (name[n] == '.' && n < sizeof (nbuf) - 1) {
2N/A#ifdef SYSV
2N/A memcpy((void *)nbuf, (void *)name, n);
2N/A#else
2N/A bcopy(name, nbuf, n);
2N/A#endif
2N/A nbuf[n] = '\0';
2N/A } else
2N/A longname = name;
2N/A } else
2N/A (void) sprintf(nbuf, "%.*s.%.*s",
2N/A MAXDNAME, name, MAXDNAME, domain);
2N/A
2N/A return (res_query(longname, class, type, answer, anslen));
2N/A}
2N/A
2N/Achar *
2N/Ahostalias(name)
2N/A register char *name;
2N/A{
2N/A register char *C1, *C2;
2N/A FILE *fp;
2N/A char *file, *getenv(), *strcpy(), *strncpy();
2N/A char buf[BUFSIZ];
2N/A static char abuf[MAXDNAME];
2N/A
2N/A file = getenv("HOSTALIASES");
2N/A if (file == NULL || (fp = fopen(file, "r")) == NULL)
2N/A return (NULL);
2N/A buf[sizeof (buf) - 1] = '\0';
2N/A while (fgets(buf, sizeof (buf), fp)) {
2N/A for (C1 = buf; *C1 && !isspace(*C1); ++C1);
2N/A if (!*C1)
2N/A break;
2N/A *C1 = '\0';
2N/A if (!strcasecmp(buf, name)) {
2N/A while (isspace(*++C1));
2N/A if (!*C1)
2N/A break;
2N/A for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2);
2N/A abuf[sizeof (abuf) - 1] = *C2 = '\0';
2N/A (void) strncpy(abuf, C1, sizeof (abuf) - 1);
2N/A fclose(fp);
2N/A return (abuf);
2N/A }
2N/A }
2N/A fclose(fp);
2N/A return (NULL);
2N/A}