xref: /titanic_52/usr/src/lib/libresolv/res_query.c (revision e8031f0a8ed0e45c6d8847c5e09424e66fd34a4b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*e8031f0aSraf 
237c478bd9Sstevel@tonic-gate /*
24*e8031f0aSraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
337c478bd9Sstevel@tonic-gate  * The Regents of the University of California
347c478bd9Sstevel@tonic-gate  * All Rights Reserved
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
377c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
387c478bd9Sstevel@tonic-gate  * contributors.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
427c478bd9Sstevel@tonic-gate 
43*e8031f0aSraf #include "c_synonyms.h"
447c478bd9Sstevel@tonic-gate #include <sys/param.h>
457c478bd9Sstevel@tonic-gate #include <sys/socket.h>
467c478bd9Sstevel@tonic-gate #include <netinet/in.h>
477c478bd9Sstevel@tonic-gate #include <ctype.h>
487c478bd9Sstevel@tonic-gate #include <netdb.h>
497c478bd9Sstevel@tonic-gate #include <stdio.h>
507c478bd9Sstevel@tonic-gate #include <errno.h>
517c478bd9Sstevel@tonic-gate #include <string.h>
527c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
537c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
547c478bd9Sstevel@tonic-gate #include <resolv.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #if PACKETSZ > 1024
577c478bd9Sstevel@tonic-gate #define	MAXPACKET	PACKETSZ
587c478bd9Sstevel@tonic-gate #else
597c478bd9Sstevel@tonic-gate #define	MAXPACKET	1024
607c478bd9Sstevel@tonic-gate #endif
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate int h_errno;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * Formulate a normal query, send, and await answer.
667c478bd9Sstevel@tonic-gate  * Returned answer is placed in supplied buffer "answer".
677c478bd9Sstevel@tonic-gate  * Perform preliminary check of answer, returning success only
687c478bd9Sstevel@tonic-gate  * if no error is indicated and the answer count is nonzero.
697c478bd9Sstevel@tonic-gate  * Return the size of the response on success, -1 on error.
707c478bd9Sstevel@tonic-gate  * Error number is left in h_errno.
717c478bd9Sstevel@tonic-gate  * Caller must parse answer and determine whether it answers the question.
727c478bd9Sstevel@tonic-gate  */
736a1c6faaSanay int
747c478bd9Sstevel@tonic-gate res_query(name, class, type, answer, anslen)
757c478bd9Sstevel@tonic-gate 	char *name;		/* domain name */
767c478bd9Sstevel@tonic-gate 	int class, type;	/* class and type of query */
777c478bd9Sstevel@tonic-gate 	u_char *answer;		/* buffer to put answer */
787c478bd9Sstevel@tonic-gate 	int anslen;		/* size of answer buffer */
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	char buf[MAXPACKET];
817c478bd9Sstevel@tonic-gate 	HEADER *hp;
827c478bd9Sstevel@tonic-gate 	int n;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
857c478bd9Sstevel@tonic-gate 		return (-1);
867c478bd9Sstevel@tonic-gate #ifdef DEBUG
877c478bd9Sstevel@tonic-gate 	if (_res.options & RES_DEBUG)
887c478bd9Sstevel@tonic-gate 		printf("res_query(%s, %d, %d)\n", name, class, type);
897c478bd9Sstevel@tonic-gate #endif
907c478bd9Sstevel@tonic-gate 	n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL,
917c478bd9Sstevel@tonic-gate 	    buf, sizeof (buf));
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	if (n <= 0) {
947c478bd9Sstevel@tonic-gate #ifdef DEBUG
957c478bd9Sstevel@tonic-gate 		if (_res.options & RES_DEBUG)
967c478bd9Sstevel@tonic-gate 			printf("res_query: mkquery failed\n");
977c478bd9Sstevel@tonic-gate #endif
987c478bd9Sstevel@tonic-gate 		h_errno = NO_RECOVERY;
997c478bd9Sstevel@tonic-gate 		return (n);
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 	n = res_send(buf, n, answer, anslen);
1027c478bd9Sstevel@tonic-gate 	if (n < 0) {
1037c478bd9Sstevel@tonic-gate #ifdef DEBUG
1047c478bd9Sstevel@tonic-gate 		if (_res.options & RES_DEBUG)
1057c478bd9Sstevel@tonic-gate 			printf("res_query: send error\n");
1067c478bd9Sstevel@tonic-gate #endif
1077c478bd9Sstevel@tonic-gate 		h_errno = TRY_AGAIN;
1087c478bd9Sstevel@tonic-gate 		return (n);
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	hp = (HEADER *) answer;
1127c478bd9Sstevel@tonic-gate 	if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1137c478bd9Sstevel@tonic-gate #ifdef DEBUG
1147c478bd9Sstevel@tonic-gate 		if (_res.options & RES_DEBUG)
1157c478bd9Sstevel@tonic-gate 			printf("rcode = %d, ancount=%d\n", hp->rcode,
1167c478bd9Sstevel@tonic-gate 			    ntohs(hp->ancount));
1177c478bd9Sstevel@tonic-gate #endif
1187c478bd9Sstevel@tonic-gate 		switch (hp->rcode) {
1197c478bd9Sstevel@tonic-gate 			case NXDOMAIN:
1207c478bd9Sstevel@tonic-gate 				h_errno = HOST_NOT_FOUND;
1217c478bd9Sstevel@tonic-gate 				break;
1227c478bd9Sstevel@tonic-gate 			case SERVFAIL:
1237c478bd9Sstevel@tonic-gate 				h_errno = TRY_AGAIN;
1247c478bd9Sstevel@tonic-gate 				break;
1257c478bd9Sstevel@tonic-gate 			case NOERROR:
1267c478bd9Sstevel@tonic-gate 				h_errno = NO_DATA;
1277c478bd9Sstevel@tonic-gate 				break;
1287c478bd9Sstevel@tonic-gate 			case FORMERR:
1297c478bd9Sstevel@tonic-gate 			case NOTIMP:
1307c478bd9Sstevel@tonic-gate 			case REFUSED:
1317c478bd9Sstevel@tonic-gate 			default:
1327c478bd9Sstevel@tonic-gate 				h_errno = NO_RECOVERY;
1337c478bd9Sstevel@tonic-gate 				break;
1347c478bd9Sstevel@tonic-gate 		}
1357c478bd9Sstevel@tonic-gate 		return (-1);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 	if (hp->rcode == NOERROR && ntohs(hp->ancount) > 0)
1387c478bd9Sstevel@tonic-gate 		h_errno = 0;
1397c478bd9Sstevel@tonic-gate 	return (n);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * Formulate a normal query, send, and retrieve answer in supplied buffer.
1447c478bd9Sstevel@tonic-gate  * Return the size of the response on success, -1 on error.
1457c478bd9Sstevel@tonic-gate  * If enabled, implement search rules until answer or unrecoverable failure
1467c478bd9Sstevel@tonic-gate  * is detected.  Error number is left in h_errno.
1477c478bd9Sstevel@tonic-gate  * Only useful for queries in the same name hierarchy as the local host
1487c478bd9Sstevel@tonic-gate  * (not, for example, for host address-to-name lookups in domain in-addr.arpa).
1497c478bd9Sstevel@tonic-gate  */
1506a1c6faaSanay int
1517c478bd9Sstevel@tonic-gate res_search(name, class, type, answer, anslen)
1527c478bd9Sstevel@tonic-gate 	char *name;		/* domain name */
1537c478bd9Sstevel@tonic-gate 	int class, type;	/* class and type of query */
1547c478bd9Sstevel@tonic-gate 	u_char *answer;		/* buffer to put answer */
1557c478bd9Sstevel@tonic-gate 	int anslen;		/* size of answer */
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	register char *cp, **domain;
1587c478bd9Sstevel@tonic-gate 	int n, ret, got_nodata = 0;
1597c478bd9Sstevel@tonic-gate 	char *hostalias();
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
1627c478bd9Sstevel@tonic-gate 		return (-1);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	errno = 0;
1657c478bd9Sstevel@tonic-gate 	h_errno = HOST_NOT_FOUND;		/* default, if we never query */
1667c478bd9Sstevel@tonic-gate 	for (cp = name, n = 0; *cp; cp++)
1677c478bd9Sstevel@tonic-gate 		if (*cp == '.')
1687c478bd9Sstevel@tonic-gate 			n++;
1697c478bd9Sstevel@tonic-gate 	if (n == 0 && (cp = hostalias(name)))
1707c478bd9Sstevel@tonic-gate 		return (res_query(cp, class, type, answer, anslen));
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	/*
1737c478bd9Sstevel@tonic-gate 	 * We do at least one level of search if
1747c478bd9Sstevel@tonic-gate 	 *	- there is no dot and RES_DEFNAME is set, or
1757c478bd9Sstevel@tonic-gate 	 *	- there is at least one dot, there is no trailing dot,
1767c478bd9Sstevel@tonic-gate 	 *	  and RES_DNSRCH is set.
1777c478bd9Sstevel@tonic-gate 	 */
1787c478bd9Sstevel@tonic-gate 	if ((n == 0 && _res.options & RES_DEFNAMES) ||
1797c478bd9Sstevel@tonic-gate 		(n != 0 && *--cp != '.' && _res.options & RES_DNSRCH)) {
1807c478bd9Sstevel@tonic-gate 		for (domain = _res.dnsrch; *domain; domain++) {
1817c478bd9Sstevel@tonic-gate 			ret = res_querydomain(name, *domain, class, type,
1827c478bd9Sstevel@tonic-gate 							answer, anslen);
1837c478bd9Sstevel@tonic-gate 			if (ret > 0)
1847c478bd9Sstevel@tonic-gate 				return (ret);
1857c478bd9Sstevel@tonic-gate 		/*
1867c478bd9Sstevel@tonic-gate 		 * If no server present, give up.
1877c478bd9Sstevel@tonic-gate 		 * If name isn't found in this domain,
1887c478bd9Sstevel@tonic-gate 		 * keep trying higher domains in the search list
1897c478bd9Sstevel@tonic-gate 		 * (if that's enabled).
1907c478bd9Sstevel@tonic-gate 		 * On a NO_DATA error, keep trying, otherwise
1917c478bd9Sstevel@tonic-gate 		 * a wildcard entry of another type could keep us
1927c478bd9Sstevel@tonic-gate 		 * from finding this entry higher in the domain.
1937c478bd9Sstevel@tonic-gate 		 * If we get some other error (negative answer or
1947c478bd9Sstevel@tonic-gate 		 * server failure), then stop searching up,
1957c478bd9Sstevel@tonic-gate 		 * but try the input name below in case it's fully-qualified.
1967c478bd9Sstevel@tonic-gate 		 */
1977c478bd9Sstevel@tonic-gate 			if (errno == ECONNREFUSED) {
1987c478bd9Sstevel@tonic-gate 				h_errno = TRY_AGAIN;
1997c478bd9Sstevel@tonic-gate 				return (-1);
2007c478bd9Sstevel@tonic-gate 			}
2017c478bd9Sstevel@tonic-gate 			if (h_errno == NO_DATA)
2027c478bd9Sstevel@tonic-gate 				got_nodata++;
2037c478bd9Sstevel@tonic-gate 			if ((h_errno != HOST_NOT_FOUND && h_errno != NO_DATA) ||
2047c478bd9Sstevel@tonic-gate 				(_res.options & RES_DNSRCH) == 0)
2057c478bd9Sstevel@tonic-gate 				break;
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * If the search/default failed, try the name as fully-qualified,
2107c478bd9Sstevel@tonic-gate 	 * but only if it contained at least one dot (even trailing).
2117c478bd9Sstevel@tonic-gate 	 * This is purely a heuristic; we assume that any reasonable query
2127c478bd9Sstevel@tonic-gate 	 * about a top-level domain (for servers, SOA, etc) will not use
2137c478bd9Sstevel@tonic-gate 	 * res_search.
2147c478bd9Sstevel@tonic-gate 	 */
2157c478bd9Sstevel@tonic-gate 	if (n && (ret = res_querydomain(name, (char *)NULL, class, type,
2167c478bd9Sstevel@tonic-gate 	    answer, anslen)) > 0)
2177c478bd9Sstevel@tonic-gate 		return (ret);
2187c478bd9Sstevel@tonic-gate 	if (got_nodata)
2197c478bd9Sstevel@tonic-gate 		h_errno = NO_DATA;
2207c478bd9Sstevel@tonic-gate 	return (-1);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate  * Perform a call on res_query on the concatenation of name and domain,
2257c478bd9Sstevel@tonic-gate  * removing a trailing dot from name if domain is NULL.
2267c478bd9Sstevel@tonic-gate  */
2276a1c6faaSanay int
2287c478bd9Sstevel@tonic-gate res_querydomain(name, domain, class, type, answer, anslen)
2297c478bd9Sstevel@tonic-gate 	char *name, *domain;
2307c478bd9Sstevel@tonic-gate 	int class, type;	/* class and type of query */
2317c478bd9Sstevel@tonic-gate 	u_char *answer;		/* buffer to put answer */
2327c478bd9Sstevel@tonic-gate 	int anslen;		/* size of answer */
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	char nbuf[2*MAXDNAME+2];
2357c478bd9Sstevel@tonic-gate 	char *longname = nbuf;
2367c478bd9Sstevel@tonic-gate 	int n;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate #ifdef DEBUG
2397c478bd9Sstevel@tonic-gate 	if (_res.options & RES_DEBUG) {
2407c478bd9Sstevel@tonic-gate 		if (domain == (char *)NULL)
2417c478bd9Sstevel@tonic-gate 			printf("res_querydomain(%s, NULL, %d, %d)\n",
2427c478bd9Sstevel@tonic-gate 					    name, class, type);
2437c478bd9Sstevel@tonic-gate 		else
2447c478bd9Sstevel@tonic-gate 			printf("res_querydomain(%s, %s, %d, %d)\n",
2457c478bd9Sstevel@tonic-gate 					    name, domain, class, type);
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate #endif
2487c478bd9Sstevel@tonic-gate 	if (domain == NULL) {
2497c478bd9Sstevel@tonic-gate 		/*
2507c478bd9Sstevel@tonic-gate 		 * Check for trailing '.';
2517c478bd9Sstevel@tonic-gate 		 * copy without '.' if present.
2527c478bd9Sstevel@tonic-gate 		 */
2537c478bd9Sstevel@tonic-gate 		n = strlen(name) - 1;
2547c478bd9Sstevel@tonic-gate 		if (name[n] == '.' && n < sizeof (nbuf) - 1) {
2557c478bd9Sstevel@tonic-gate #ifdef SYSV
2567c478bd9Sstevel@tonic-gate 			memcpy((void *)nbuf, (void *)name, n);
2577c478bd9Sstevel@tonic-gate #else
2587c478bd9Sstevel@tonic-gate 			bcopy(name, nbuf, n);
2597c478bd9Sstevel@tonic-gate #endif
2607c478bd9Sstevel@tonic-gate 			nbuf[n] = '\0';
2617c478bd9Sstevel@tonic-gate 		} else
2627c478bd9Sstevel@tonic-gate 			longname = name;
2637c478bd9Sstevel@tonic-gate 	} else
2647c478bd9Sstevel@tonic-gate 		(void) sprintf(nbuf, "%.*s.%.*s",
2657c478bd9Sstevel@tonic-gate 		    MAXDNAME, name, MAXDNAME, domain);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	return (res_query(longname, class, type, answer, anslen));
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate char *
2717c478bd9Sstevel@tonic-gate hostalias(name)
2727c478bd9Sstevel@tonic-gate 	register char *name;
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	register char *C1, *C2;
2757c478bd9Sstevel@tonic-gate 	FILE *fp;
2767c478bd9Sstevel@tonic-gate 	char *file, *getenv(), *strcpy(), *strncpy();
2777c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
2787c478bd9Sstevel@tonic-gate 	static char abuf[MAXDNAME];
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	file = getenv("HOSTALIASES");
2817c478bd9Sstevel@tonic-gate 	if (file == NULL || (fp = fopen(file, "r")) == NULL)
2827c478bd9Sstevel@tonic-gate 		return (NULL);
2837c478bd9Sstevel@tonic-gate 	buf[sizeof (buf) - 1] = '\0';
2847c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp)) {
2857c478bd9Sstevel@tonic-gate 		for (C1 = buf; *C1 && !isspace(*C1); ++C1);
2867c478bd9Sstevel@tonic-gate 		if (!*C1)
2877c478bd9Sstevel@tonic-gate 			break;
2887c478bd9Sstevel@tonic-gate 		*C1 = '\0';
2897c478bd9Sstevel@tonic-gate 		if (!strcasecmp(buf, name)) {
2907c478bd9Sstevel@tonic-gate 			while (isspace(*++C1));
2917c478bd9Sstevel@tonic-gate 			if (!*C1)
2927c478bd9Sstevel@tonic-gate 				break;
2937c478bd9Sstevel@tonic-gate 			for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2);
2947c478bd9Sstevel@tonic-gate 			abuf[sizeof (abuf) - 1] = *C2 = '\0';
2957c478bd9Sstevel@tonic-gate 			(void) strncpy(abuf, C1, sizeof (abuf) - 1);
2967c478bd9Sstevel@tonic-gate 			fclose(fp);
2977c478bd9Sstevel@tonic-gate 			return (abuf);
2987c478bd9Sstevel@tonic-gate 		}
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 	fclose(fp);
3017c478bd9Sstevel@tonic-gate 	return (NULL);
3027c478bd9Sstevel@tonic-gate }
303