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