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