1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 /* 31 * University Copyright- Copyright (c) 1982, 1986, 1988 32 * The Regents of the University of California 33 * All Rights Reserved 34 * 35 * University Acknowledgment- Portions of this document are derived from 36 * software developed by the University of California, Berkeley, and its 37 * contributors. 38 */ 39 40 #pragma ident "%Z%%M% %I% %E% SMI" 41 42 #include <sys/param.h> 43 #include <sys/socket.h> 44 #include <netinet/in.h> 45 #include <ctype.h> 46 #include <netdb.h> 47 #include <stdio.h> 48 #include <errno.h> 49 #include <string.h> 50 #include <arpa/inet.h> 51 #include <arpa/nameser.h> 52 #include <resolv.h> 53 54 #if PACKETSZ > 1024 55 #define MAXPACKET PACKETSZ 56 #else 57 #define MAXPACKET 1024 58 #endif 59 60 int h_errno; 61 62 /* 63 * Formulate a normal query, send, and await answer. 64 * Returned answer is placed in supplied buffer "answer". 65 * Perform preliminary check of answer, returning success only 66 * if no error is indicated and the answer count is nonzero. 67 * Return the size of the response on success, -1 on error. 68 * Error number is left in h_errno. 69 * Caller must parse answer and determine whether it answers the question. 70 */ 71 int 72 res_query(name, class, type, answer, anslen) 73 char *name; /* domain name */ 74 int class, type; /* class and type of query */ 75 u_char *answer; /* buffer to put answer */ 76 int anslen; /* size of answer buffer */ 77 { 78 char buf[MAXPACKET]; 79 HEADER *hp; 80 int n; 81 82 if ((_res.options & RES_INIT) == 0 && res_init() == -1) 83 return (-1); 84 #ifdef DEBUG 85 if (_res.options & RES_DEBUG) 86 printf("res_query(%s, %d, %d)\n", name, class, type); 87 #endif 88 n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL, 89 buf, sizeof (buf)); 90 91 if (n <= 0) { 92 #ifdef DEBUG 93 if (_res.options & RES_DEBUG) 94 printf("res_query: mkquery failed\n"); 95 #endif 96 h_errno = NO_RECOVERY; 97 return (n); 98 } 99 n = res_send(buf, n, answer, anslen); 100 if (n < 0) { 101 #ifdef DEBUG 102 if (_res.options & RES_DEBUG) 103 printf("res_query: send error\n"); 104 #endif 105 h_errno = TRY_AGAIN; 106 return (n); 107 } 108 109 hp = (HEADER *) answer; 110 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { 111 #ifdef DEBUG 112 if (_res.options & RES_DEBUG) 113 printf("rcode = %d, ancount=%d\n", hp->rcode, 114 ntohs(hp->ancount)); 115 #endif 116 switch (hp->rcode) { 117 case NXDOMAIN: 118 h_errno = HOST_NOT_FOUND; 119 break; 120 case SERVFAIL: 121 h_errno = TRY_AGAIN; 122 break; 123 case NOERROR: 124 h_errno = NO_DATA; 125 break; 126 case FORMERR: 127 case NOTIMP: 128 case REFUSED: 129 default: 130 h_errno = NO_RECOVERY; 131 break; 132 } 133 return (-1); 134 } 135 if (hp->rcode == NOERROR && ntohs(hp->ancount) > 0) 136 h_errno = 0; 137 return (n); 138 } 139 140 /* 141 * Formulate a normal query, send, and retrieve answer in supplied buffer. 142 * Return the size of the response on success, -1 on error. 143 * If enabled, implement search rules until answer or unrecoverable failure 144 * is detected. Error number is left in h_errno. 145 * Only useful for queries in the same name hierarchy as the local host 146 * (not, for example, for host address-to-name lookups in domain in-addr.arpa). 147 */ 148 int 149 res_search(name, class, type, answer, anslen) 150 char *name; /* domain name */ 151 int class, type; /* class and type of query */ 152 u_char *answer; /* buffer to put answer */ 153 int anslen; /* size of answer */ 154 { 155 register char *cp, **domain; 156 int n, ret, got_nodata = 0; 157 char *hostalias(); 158 159 if ((_res.options & RES_INIT) == 0 && res_init() == -1) 160 return (-1); 161 162 errno = 0; 163 h_errno = HOST_NOT_FOUND; /* default, if we never query */ 164 for (cp = name, n = 0; *cp; cp++) 165 if (*cp == '.') 166 n++; 167 if (n == 0 && (cp = hostalias(name))) 168 return (res_query(cp, class, type, answer, anslen)); 169 170 /* 171 * We do at least one level of search if 172 * - there is no dot and RES_DEFNAME is set, or 173 * - there is at least one dot, there is no trailing dot, 174 * and RES_DNSRCH is set. 175 */ 176 if ((n == 0 && _res.options & RES_DEFNAMES) || 177 (n != 0 && *--cp != '.' && _res.options & RES_DNSRCH)) { 178 for (domain = _res.dnsrch; *domain; domain++) { 179 ret = res_querydomain(name, *domain, class, type, 180 answer, anslen); 181 if (ret > 0) 182 return (ret); 183 /* 184 * If no server present, give up. 185 * If name isn't found in this domain, 186 * keep trying higher domains in the search list 187 * (if that's enabled). 188 * On a NO_DATA error, keep trying, otherwise 189 * a wildcard entry of another type could keep us 190 * from finding this entry higher in the domain. 191 * If we get some other error (negative answer or 192 * server failure), then stop searching up, 193 * but try the input name below in case it's fully-qualified. 194 */ 195 if (errno == ECONNREFUSED) { 196 h_errno = TRY_AGAIN; 197 return (-1); 198 } 199 if (h_errno == NO_DATA) 200 got_nodata++; 201 if ((h_errno != HOST_NOT_FOUND && h_errno != NO_DATA) || 202 (_res.options & RES_DNSRCH) == 0) 203 break; 204 } 205 } 206 /* 207 * If the search/default failed, try the name as fully-qualified, 208 * but only if it contained at least one dot (even trailing). 209 * This is purely a heuristic; we assume that any reasonable query 210 * about a top-level domain (for servers, SOA, etc) will not use 211 * res_search. 212 */ 213 if (n && (ret = res_querydomain(name, (char *)NULL, class, type, 214 answer, anslen)) > 0) 215 return (ret); 216 if (got_nodata) 217 h_errno = NO_DATA; 218 return (-1); 219 } 220 221 /* 222 * Perform a call on res_query on the concatenation of name and domain, 223 * removing a trailing dot from name if domain is NULL. 224 */ 225 int 226 res_querydomain(name, domain, class, type, answer, anslen) 227 char *name, *domain; 228 int class, type; /* class and type of query */ 229 u_char *answer; /* buffer to put answer */ 230 int anslen; /* size of answer */ 231 { 232 char nbuf[2*MAXDNAME+2]; 233 char *longname = nbuf; 234 int n; 235 236 #ifdef DEBUG 237 if (_res.options & RES_DEBUG) { 238 if (domain == (char *)NULL) 239 printf("res_querydomain(%s, NULL, %d, %d)\n", 240 name, class, type); 241 else 242 printf("res_querydomain(%s, %s, %d, %d)\n", 243 name, domain, class, type); 244 } 245 #endif 246 if (domain == NULL) { 247 /* 248 * Check for trailing '.'; 249 * copy without '.' if present. 250 */ 251 n = strlen(name) - 1; 252 if (name[n] == '.' && n < sizeof (nbuf) - 1) { 253 #ifdef SYSV 254 memcpy((void *)nbuf, (void *)name, n); 255 #else 256 bcopy(name, nbuf, n); 257 #endif 258 nbuf[n] = '\0'; 259 } else 260 longname = name; 261 } else 262 (void) sprintf(nbuf, "%.*s.%.*s", 263 MAXDNAME, name, MAXDNAME, domain); 264 265 return (res_query(longname, class, type, answer, anslen)); 266 } 267 268 char * 269 hostalias(name) 270 register char *name; 271 { 272 register char *C1, *C2; 273 FILE *fp; 274 char *file, *getenv(), *strcpy(), *strncpy(); 275 char buf[BUFSIZ]; 276 static char abuf[MAXDNAME]; 277 278 file = getenv("HOSTALIASES"); 279 if (file == NULL || (fp = fopen(file, "r")) == NULL) 280 return (NULL); 281 buf[sizeof (buf) - 1] = '\0'; 282 while (fgets(buf, sizeof (buf), fp)) { 283 for (C1 = buf; *C1 && !isspace(*C1); ++C1); 284 if (!*C1) 285 break; 286 *C1 = '\0'; 287 if (!strcasecmp(buf, name)) { 288 while (isspace(*++C1)); 289 if (!*C1) 290 break; 291 for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2); 292 abuf[sizeof (abuf) - 1] = *C2 = '\0'; 293 (void) strncpy(abuf, C1, sizeof (abuf) - 1); 294 fclose(fp); 295 return (abuf); 296 } 297 } 298 fclose(fp); 299 return (NULL); 300 } 301