xref: /illumos-gate/usr/src/lib/libresolv/res_query.c (revision f4fe62a3)
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 /*
23bd0e95e6SGary 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	*/
29*f4fe62a3SToomas Soome /*	  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>
49bd0e95e6SGary Mills #include <stdlib.h>
507c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
517c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
527c478bd9Sstevel@tonic-gate #include <resolv.h>
53bd0e95e6SGary 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 /*
627c478bd9Sstevel@tonic-gate  * Formulate a normal query, send, and await answer.
637c478bd9Sstevel@tonic-gate  * Returned answer is placed in supplied buffer "answer".
647c478bd9Sstevel@tonic-gate  * Perform preliminary check of answer, returning success only
657c478bd9Sstevel@tonic-gate  * if no error is indicated and the answer count is nonzero.
667c478bd9Sstevel@tonic-gate  * Return the size of the response on success, -1 on error.
677c478bd9Sstevel@tonic-gate  * Error number is left in h_errno.
687c478bd9Sstevel@tonic-gate  * Caller must parse answer and determine whether it answers the question.
697c478bd9Sstevel@tonic-gate  */
706a1c6faaSanay int
res_query(name,class,type,answer,anslen)717c478bd9Sstevel@tonic-gate res_query(name, class, type, answer, anslen)
727c478bd9Sstevel@tonic-gate 	char *name;		/* domain name */
737c478bd9Sstevel@tonic-gate 	int class, type;	/* class and type of query */
747c478bd9Sstevel@tonic-gate 	u_char *answer;		/* buffer to put answer */
757c478bd9Sstevel@tonic-gate 	int anslen;		/* size of answer buffer */
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	char buf[MAXPACKET];
787c478bd9Sstevel@tonic-gate 	HEADER *hp;
797c478bd9Sstevel@tonic-gate 	int n;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
827c478bd9Sstevel@tonic-gate 		return (-1);
837c478bd9Sstevel@tonic-gate #ifdef DEBUG
847c478bd9Sstevel@tonic-gate 	if (_res.options & RES_DEBUG)
857c478bd9Sstevel@tonic-gate 		printf("res_query(%s, %d, %d)\n", name, class, type);
867c478bd9Sstevel@tonic-gate #endif
877c478bd9Sstevel@tonic-gate 	n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL,
887c478bd9Sstevel@tonic-gate 	    buf, sizeof (buf));
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if (n <= 0) {
917c478bd9Sstevel@tonic-gate #ifdef DEBUG
927c478bd9Sstevel@tonic-gate 		if (_res.options & RES_DEBUG)
937c478bd9Sstevel@tonic-gate 			printf("res_query: mkquery failed\n");
947c478bd9Sstevel@tonic-gate #endif
957c478bd9Sstevel@tonic-gate 		h_errno = NO_RECOVERY;
967c478bd9Sstevel@tonic-gate 		return (n);
977c478bd9Sstevel@tonic-gate 	}
98bd0e95e6SGary Mills 	n = res_send(buf, n, (char *)answer, anslen);
997c478bd9Sstevel@tonic-gate 	if (n < 0) {
1007c478bd9Sstevel@tonic-gate #ifdef DEBUG
1017c478bd9Sstevel@tonic-gate 		if (_res.options & RES_DEBUG)
1027c478bd9Sstevel@tonic-gate 			printf("res_query: send error\n");
1037c478bd9Sstevel@tonic-gate #endif
1047c478bd9Sstevel@tonic-gate 		h_errno = TRY_AGAIN;
1057c478bd9Sstevel@tonic-gate 		return (n);
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	hp = (HEADER *) answer;
1097c478bd9Sstevel@tonic-gate 	if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1107c478bd9Sstevel@tonic-gate #ifdef DEBUG
1117c478bd9Sstevel@tonic-gate 		if (_res.options & RES_DEBUG)
1127c478bd9Sstevel@tonic-gate 			printf("rcode = %d, ancount=%d\n", hp->rcode,
1137c478bd9Sstevel@tonic-gate 			    ntohs(hp->ancount));
1147c478bd9Sstevel@tonic-gate #endif
1157c478bd9Sstevel@tonic-gate 		switch (hp->rcode) {
1167c478bd9Sstevel@tonic-gate 			case NXDOMAIN:
1177c478bd9Sstevel@tonic-gate 				h_errno = HOST_NOT_FOUND;
1187c478bd9Sstevel@tonic-gate 				break;
1197c478bd9Sstevel@tonic-gate 			case SERVFAIL:
1207c478bd9Sstevel@tonic-gate 				h_errno = TRY_AGAIN;
1217c478bd9Sstevel@tonic-gate 				break;
1227c478bd9Sstevel@tonic-gate 			case NOERROR:
1237c478bd9Sstevel@tonic-gate 				h_errno = NO_DATA;
1247c478bd9Sstevel@tonic-gate 				break;
1257c478bd9Sstevel@tonic-gate 			case FORMERR:
1267c478bd9Sstevel@tonic-gate 			case NOTIMP:
1277c478bd9Sstevel@tonic-gate 			case REFUSED:
1287c478bd9Sstevel@tonic-gate 			default:
1297c478bd9Sstevel@tonic-gate 				h_errno = NO_RECOVERY;
1307c478bd9Sstevel@tonic-gate 				break;
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 		return (-1);
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 	if (hp->rcode == NOERROR && ntohs(hp->ancount) > 0)
1357c478bd9Sstevel@tonic-gate 		h_errno = 0;
1367c478bd9Sstevel@tonic-gate 	return (n);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate  * Formulate a normal query, send, and retrieve answer in supplied buffer.
1417c478bd9Sstevel@tonic-gate  * Return the size of the response on success, -1 on error.
1427c478bd9Sstevel@tonic-gate  * If enabled, implement search rules until answer or unrecoverable failure
1437c478bd9Sstevel@tonic-gate  * is detected.  Error number is left in h_errno.
1447c478bd9Sstevel@tonic-gate  * Only useful for queries in the same name hierarchy as the local host
1457c478bd9Sstevel@tonic-gate  * (not, for example, for host address-to-name lookups in domain in-addr.arpa).
1467c478bd9Sstevel@tonic-gate  */
1476a1c6faaSanay int
res_search(name,class,type,answer,anslen)1487c478bd9Sstevel@tonic-gate res_search(name, class, type, answer, anslen)
1497c478bd9Sstevel@tonic-gate 	char *name;		/* domain name */
1507c478bd9Sstevel@tonic-gate 	int class, type;	/* class and type of query */
1517c478bd9Sstevel@tonic-gate 	u_char *answer;		/* buffer to put answer */
1527c478bd9Sstevel@tonic-gate 	int anslen;		/* size of answer */
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	register char *cp, **domain;
1557c478bd9Sstevel@tonic-gate 	int n, ret, got_nodata = 0;
1567c478bd9Sstevel@tonic-gate 	char *hostalias();
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
1597c478bd9Sstevel@tonic-gate 		return (-1);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	errno = 0;
1627c478bd9Sstevel@tonic-gate 	h_errno = HOST_NOT_FOUND;		/* default, if we never query */
1637c478bd9Sstevel@tonic-gate 	for (cp = name, n = 0; *cp; cp++)
1647c478bd9Sstevel@tonic-gate 		if (*cp == '.')
1657c478bd9Sstevel@tonic-gate 			n++;
1667c478bd9Sstevel@tonic-gate 	if (n == 0 && (cp = hostalias(name)))
1677c478bd9Sstevel@tonic-gate 		return (res_query(cp, class, type, answer, anslen));
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * We do at least one level of search if
1717c478bd9Sstevel@tonic-gate 	 *	- there is no dot and RES_DEFNAME is set, or
1727c478bd9Sstevel@tonic-gate 	 *	- there is at least one dot, there is no trailing dot,
1737c478bd9Sstevel@tonic-gate 	 *	  and RES_DNSRCH is set.
1747c478bd9Sstevel@tonic-gate 	 */
1757c478bd9Sstevel@tonic-gate 	if ((n == 0 && _res.options & RES_DEFNAMES) ||
1767c478bd9Sstevel@tonic-gate 		(n != 0 && *--cp != '.' && _res.options & RES_DNSRCH)) {
1777c478bd9Sstevel@tonic-gate 		for (domain = _res.dnsrch; *domain; domain++) {
1787c478bd9Sstevel@tonic-gate 			ret = res_querydomain(name, *domain, class, type,
1797c478bd9Sstevel@tonic-gate 							answer, anslen);
1807c478bd9Sstevel@tonic-gate 			if (ret > 0)
1817c478bd9Sstevel@tonic-gate 				return (ret);
1827c478bd9Sstevel@tonic-gate 		/*
1837c478bd9Sstevel@tonic-gate 		 * If no server present, give up.
1847c478bd9Sstevel@tonic-gate 		 * If name isn't found in this domain,
1857c478bd9Sstevel@tonic-gate 		 * keep trying higher domains in the search list
1867c478bd9Sstevel@tonic-gate 		 * (if that's enabled).
1877c478bd9Sstevel@tonic-gate 		 * On a NO_DATA error, keep trying, otherwise
1887c478bd9Sstevel@tonic-gate 		 * a wildcard entry of another type could keep us
1897c478bd9Sstevel@tonic-gate 		 * from finding this entry higher in the domain.
1907c478bd9Sstevel@tonic-gate 		 * If we get some other error (negative answer or
1917c478bd9Sstevel@tonic-gate 		 * server failure), then stop searching up,
1927c478bd9Sstevel@tonic-gate 		 * but try the input name below in case it's fully-qualified.
1937c478bd9Sstevel@tonic-gate 		 */
1947c478bd9Sstevel@tonic-gate 			if (errno == ECONNREFUSED) {
1957c478bd9Sstevel@tonic-gate 				h_errno = TRY_AGAIN;
1967c478bd9Sstevel@tonic-gate 				return (-1);
1977c478bd9Sstevel@tonic-gate 			}
1987c478bd9Sstevel@tonic-gate 			if (h_errno == NO_DATA)
1997c478bd9Sstevel@tonic-gate 				got_nodata++;
2007c478bd9Sstevel@tonic-gate 			if ((h_errno != HOST_NOT_FOUND && h_errno != NO_DATA) ||
2017c478bd9Sstevel@tonic-gate 				(_res.options & RES_DNSRCH) == 0)
2027c478bd9Sstevel@tonic-gate 				break;
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 	/*
2067c478bd9Sstevel@tonic-gate 	 * If the search/default failed, try the name as fully-qualified,
2077c478bd9Sstevel@tonic-gate 	 * but only if it contained at least one dot (even trailing).
2087c478bd9Sstevel@tonic-gate 	 * This is purely a heuristic; we assume that any reasonable query
2097c478bd9Sstevel@tonic-gate 	 * about a top-level domain (for servers, SOA, etc) will not use
2107c478bd9Sstevel@tonic-gate 	 * res_search.
2117c478bd9Sstevel@tonic-gate 	 */
2127c478bd9Sstevel@tonic-gate 	if (n && (ret = res_querydomain(name, (char *)NULL, class, type,
2137c478bd9Sstevel@tonic-gate 	    answer, anslen)) > 0)
2147c478bd9Sstevel@tonic-gate 		return (ret);
2157c478bd9Sstevel@tonic-gate 	if (got_nodata)
2167c478bd9Sstevel@tonic-gate 		h_errno = NO_DATA;
2177c478bd9Sstevel@tonic-gate 	return (-1);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate  * Perform a call on res_query on the concatenation of name and domain,
2227c478bd9Sstevel@tonic-gate  * removing a trailing dot from name if domain is NULL.
2237c478bd9Sstevel@tonic-gate  */
2246a1c6faaSanay int
res_querydomain(name,domain,class,type,answer,anslen)2257c478bd9Sstevel@tonic-gate res_querydomain(name, domain, class, type, answer, anslen)
2267c478bd9Sstevel@tonic-gate 	char *name, *domain;
2277c478bd9Sstevel@tonic-gate 	int class, type;	/* class and type of query */
2287c478bd9Sstevel@tonic-gate 	u_char *answer;		/* buffer to put answer */
2297c478bd9Sstevel@tonic-gate 	int anslen;		/* size of answer */
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate 	char nbuf[2*MAXDNAME+2];
2327c478bd9Sstevel@tonic-gate 	char *longname = nbuf;
2337c478bd9Sstevel@tonic-gate 	int n;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate #ifdef DEBUG
2367c478bd9Sstevel@tonic-gate 	if (_res.options & RES_DEBUG) {
2377c478bd9Sstevel@tonic-gate 		if (domain == (char *)NULL)
2387c478bd9Sstevel@tonic-gate 			printf("res_querydomain(%s, NULL, %d, %d)\n",
2397c478bd9Sstevel@tonic-gate 					    name, class, type);
2407c478bd9Sstevel@tonic-gate 		else
2417c478bd9Sstevel@tonic-gate 			printf("res_querydomain(%s, %s, %d, %d)\n",
2427c478bd9Sstevel@tonic-gate 					    name, domain, class, type);
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate #endif
2457c478bd9Sstevel@tonic-gate 	if (domain == NULL) {
2467c478bd9Sstevel@tonic-gate 		/*
2477c478bd9Sstevel@tonic-gate 		 * Check for trailing '.';
2487c478bd9Sstevel@tonic-gate 		 * copy without '.' if present.
2497c478bd9Sstevel@tonic-gate 		 */
2507c478bd9Sstevel@tonic-gate 		n = strlen(name) - 1;
2517c478bd9Sstevel@tonic-gate 		if (name[n] == '.' && n < sizeof (nbuf) - 1) {
2527c478bd9Sstevel@tonic-gate #ifdef SYSV
2537c478bd9Sstevel@tonic-gate 			memcpy((void *)nbuf, (void *)name, n);
2547c478bd9Sstevel@tonic-gate #else
2557c478bd9Sstevel@tonic-gate 			bcopy(name, nbuf, n);
2567c478bd9Sstevel@tonic-gate #endif
2577c478bd9Sstevel@tonic-gate 			nbuf[n] = '\0';
2587c478bd9Sstevel@tonic-gate 		} else
2597c478bd9Sstevel@tonic-gate 			longname = name;
2607c478bd9Sstevel@tonic-gate 	} else
2617c478bd9Sstevel@tonic-gate 		(void) sprintf(nbuf, "%.*s.%.*s",
2627c478bd9Sstevel@tonic-gate 		    MAXDNAME, name, MAXDNAME, domain);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	return (res_query(longname, class, type, answer, anslen));
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate char *
hostalias(name)2687c478bd9Sstevel@tonic-gate hostalias(name)
2697c478bd9Sstevel@tonic-gate 	register char *name;
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	register char *C1, *C2;
2727c478bd9Sstevel@tonic-gate 	FILE *fp;
273bd0e95e6SGary Mills 	char *file;
2747c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
2757c478bd9Sstevel@tonic-gate 	static char abuf[MAXDNAME];
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	file = getenv("HOSTALIASES");
2787c478bd9Sstevel@tonic-gate 	if (file == NULL || (fp = fopen(file, "r")) == NULL)
2797c478bd9Sstevel@tonic-gate 		return (NULL);
2807c478bd9Sstevel@tonic-gate 	buf[sizeof (buf) - 1] = '\0';
2817c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp)) {
2827c478bd9Sstevel@tonic-gate 		for (C1 = buf; *C1 && !isspace(*C1); ++C1);
2837c478bd9Sstevel@tonic-gate 		if (!*C1)
2847c478bd9Sstevel@tonic-gate 			break;
2857c478bd9Sstevel@tonic-gate 		*C1 = '\0';
2867c478bd9Sstevel@tonic-gate 		if (!strcasecmp(buf, name)) {
2877c478bd9Sstevel@tonic-gate 			while (isspace(*++C1));
2887c478bd9Sstevel@tonic-gate 			if (!*C1)
2897c478bd9Sstevel@tonic-gate 				break;
2907c478bd9Sstevel@tonic-gate 			for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2);
2917c478bd9Sstevel@tonic-gate 			abuf[sizeof (abuf) - 1] = *C2 = '\0';
2927c478bd9Sstevel@tonic-gate 			(void) strncpy(abuf, C1, sizeof (abuf) - 1);
2937c478bd9Sstevel@tonic-gate 			fclose(fp);
2947c478bd9Sstevel@tonic-gate 			return (abuf);
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 	fclose(fp);
2987c478bd9Sstevel@tonic-gate 	return (NULL);
2997c478bd9Sstevel@tonic-gate }
300