133736Sbostic /* 233736Sbostic * Copyright (c) 1988 Regents of the University of California. 333736Sbostic * All rights reserved. 433736Sbostic * 533736Sbostic * Redistribution and use in source and binary forms are permitted 6*35655Skarels * provided that the above copyright notice and this paragraph are 7*35655Skarels * duplicated in all such forms and that any documentation, 8*35655Skarels * advertising materials, and other materials related to such 9*35655Skarels * distribution and use acknowledge that the software was developed 10*35655Skarels * by the University of California, Berkeley. The name of the 11*35655Skarels * University may not be used to endorse or promote products derived 12*35655Skarels * from this software without specific prior written permission. 13*35655Skarels * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*35655Skarels * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*35655Skarels * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1633736Sbostic */ 1733736Sbostic 1833736Sbostic #if defined(LIBC_SCCS) && !defined(lint) 19*35655Skarels static char sccsid[] = "@(#)res_query.c 5.5 (Berkeley) 09/21/88"; 2033736Sbostic #endif /* LIBC_SCCS and not lint */ 2133736Sbostic 2233736Sbostic #include <sys/param.h> 2333736Sbostic #include <sys/socket.h> 2433736Sbostic #include <netinet/in.h> 2533736Sbostic #include <ctype.h> 2633736Sbostic #include <netdb.h> 2733736Sbostic #include <stdio.h> 2833736Sbostic #include <errno.h> 2933736Sbostic #include <strings.h> 3033736Sbostic #include <arpa/inet.h> 3133736Sbostic #include <arpa/nameser.h> 3233736Sbostic #include <resolv.h> 3333736Sbostic 3433736Sbostic #if PACKETSZ > 1024 3533736Sbostic #define MAXPACKET PACKETSZ 3633736Sbostic #else 3733736Sbostic #define MAXPACKET 1024 3833736Sbostic #endif 3933736Sbostic 4033736Sbostic extern int errno; 4133736Sbostic int h_errno; 4233736Sbostic 4333736Sbostic /* 4433736Sbostic * Formulate a normal query, send, and await answer. 4533736Sbostic * Returned answer is placed in supplied buffer "answer". 4633736Sbostic * Perform preliminary check of answer, returning success only 4733736Sbostic * if no error is indicated and the answer count is nonzero. 4833736Sbostic * Return the size of the response on success, -1 on error. 4933736Sbostic * Error number is left in h_errno. 5033826Skarels * Caller must parse answer and determine whether it answers the question. 5133736Sbostic */ 5233736Sbostic res_query(name, class, type, answer, anslen) 5333736Sbostic char *name; /* domain name */ 5433736Sbostic int class, type; /* class and type of query */ 5533736Sbostic u_char *answer; /* buffer to put answer */ 5633736Sbostic int anslen; /* size of answer buffer */ 5733736Sbostic { 5833736Sbostic char buf[MAXPACKET]; 5933736Sbostic HEADER *hp; 6033736Sbostic int n; 6133736Sbostic 6233736Sbostic if ((_res.options & RES_INIT) == 0 && res_init() == -1) 6333736Sbostic return (-1); 6433736Sbostic #ifdef DEBUG 6533736Sbostic if (_res.options & RES_DEBUG) 6633736Sbostic printf("res_query(%s, %d, %d)\n", name, class, type); 6733736Sbostic #endif 6833736Sbostic n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL, 6933736Sbostic buf, sizeof(buf)); 7033736Sbostic 7133736Sbostic if (n <= 0) { 7233736Sbostic #ifdef DEBUG 7333736Sbostic if (_res.options & RES_DEBUG) 7433736Sbostic printf("res_query: mkquery failed\n"); 7533736Sbostic #endif 7633736Sbostic h_errno = NO_RECOVERY; 7733736Sbostic return (n); 7833736Sbostic } 7933736Sbostic n = res_send(buf, n, answer, anslen); 8033736Sbostic if (n < 0) { 8133736Sbostic #ifdef DEBUG 8233736Sbostic if (_res.options & RES_DEBUG) 8333736Sbostic printf("res_query: send error\n"); 8433736Sbostic #endif 8533736Sbostic h_errno = TRY_AGAIN; 8633736Sbostic return(n); 8733736Sbostic } 8833736Sbostic 8933736Sbostic hp = (HEADER *) answer; 9033736Sbostic if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { 9133736Sbostic #ifdef DEBUG 9233736Sbostic if (_res.options & RES_DEBUG) 9333736Sbostic printf("rcode = %d, ancount=%d\n", hp->rcode, 9433736Sbostic ntohs(hp->ancount)); 9533736Sbostic #endif 9633736Sbostic switch (hp->rcode) { 9733736Sbostic case NXDOMAIN: 9833826Skarels h_errno = HOST_NOT_FOUND; 9933736Sbostic break; 10033736Sbostic case SERVFAIL: 10133736Sbostic h_errno = TRY_AGAIN; 10233736Sbostic break; 10333736Sbostic case NOERROR: 10433977Skarels h_errno = NO_DATA; 10533736Sbostic break; 10633736Sbostic case FORMERR: 10733736Sbostic case NOTIMP: 10833736Sbostic case REFUSED: 10933826Skarels default: 11033736Sbostic h_errno = NO_RECOVERY; 11133826Skarels break; 11233736Sbostic } 11333736Sbostic return (-1); 11433736Sbostic } 11533736Sbostic return(n); 11633736Sbostic } 11733736Sbostic 11833736Sbostic /* 11933736Sbostic * Formulate a normal query, send, and retrieve answer in supplied buffer. 12033736Sbostic * Return the size of the response on success, -1 on error. 12133736Sbostic * If enabled, implement search rules until answer or unrecoverable failure 12233736Sbostic * is detected. Error number is left in h_errno. 12333736Sbostic * Only useful for queries in the same name hierarchy as the local host 12433736Sbostic * (not, for example, for host address-to-name lookups in domain in-addr.arpa). 12533736Sbostic */ 12633736Sbostic res_search(name, class, type, answer, anslen) 12733736Sbostic char *name; /* domain name */ 12833736Sbostic int class, type; /* class and type of query */ 12933736Sbostic u_char *answer; /* buffer to put answer */ 13033736Sbostic int anslen; /* size of answer */ 13133736Sbostic { 13233736Sbostic register char *cp, **domain; 13333977Skarels int n, ret, got_nodata = 0; 13433736Sbostic char *hostalias(); 13533736Sbostic 13633736Sbostic if ((_res.options & RES_INIT) == 0 && res_init() == -1) 13733736Sbostic return (-1); 13833736Sbostic 13933736Sbostic errno = 0; 14033977Skarels h_errno = HOST_NOT_FOUND; /* default, if we never query */ 14133736Sbostic for (cp = name, n = 0; *cp; cp++) 14233736Sbostic if (*cp == '.') 14333736Sbostic n++; 14433736Sbostic if (n == 0 && (cp = hostalias(name))) 14533736Sbostic return (res_query(cp, class, type, answer, anslen)); 14633736Sbostic 147*35655Skarels /* 148*35655Skarels * We do at least one level of search if 149*35655Skarels * - there is no dot and RES_DEFNAME is set, or 150*35655Skarels * - there is at least one dot, there is no trailing dot, 151*35655Skarels * and RES_DNSRCH is set. 152*35655Skarels */ 153*35655Skarels if ((n == 0 && _res.options & RES_DEFNAMES) || 154*35655Skarels (n != 0 && *--cp != '.' && _res.options & RES_DNSRCH)) 155*35655Skarels for (domain = _res.dnsrch; *domain; domain++) { 15633736Sbostic ret = res_querydomain(name, *domain, class, type, 15733736Sbostic answer, anslen); 15833736Sbostic if (ret > 0) 15933736Sbostic return (ret); 16033736Sbostic /* 16133736Sbostic * If no server present, give up. 16233736Sbostic * If name isn't found in this domain, 16333736Sbostic * keep trying higher domains in the search list 16433736Sbostic * (if that's enabled). 16533977Skarels * On a NO_DATA error, keep trying, otherwise 16633736Sbostic * a wildcard entry of another type could keep us 16733736Sbostic * from finding this entry higher in the domain. 168*35655Skarels * If we get some other error (negative answer or 169*35655Skarels * server failure), then stop searching up, 17033736Sbostic * but try the input name below in case it's fully-qualified. 17133736Sbostic */ 17233736Sbostic if (errno == ECONNREFUSED) { 17333736Sbostic h_errno = TRY_AGAIN; 17433736Sbostic return (-1); 17533736Sbostic } 17633977Skarels if (h_errno == NO_DATA) 17733977Skarels got_nodata++; 17833977Skarels if ((h_errno != HOST_NOT_FOUND && h_errno != NO_DATA) || 17933736Sbostic (_res.options & RES_DNSRCH) == 0) 18033736Sbostic break; 18133736Sbostic } 18233736Sbostic /* 18333736Sbostic * If the search/default failed, try the name as fully-qualified, 18433736Sbostic * but only if it contained at least one dot (even trailing). 185*35655Skarels * This is purely a heuristic; we assume that any reasonable query 186*35655Skarels * about a top-level domain (for servers, SOA, etc) will not use 187*35655Skarels * res_search. 18833736Sbostic */ 189*35655Skarels if (n && (ret = res_querydomain(name, (char *)NULL, class, type, 190*35655Skarels answer, anslen)) > 0) 191*35655Skarels return (ret); 19233977Skarels if (got_nodata) 19333977Skarels h_errno = NO_DATA; 19433736Sbostic return (-1); 19533736Sbostic } 19633736Sbostic 19733736Sbostic /* 19833736Sbostic * Perform a call on res_query on the concatenation of name and domain, 19933736Sbostic * removing a trailing dot from name if domain is NULL. 20033736Sbostic */ 20133736Sbostic res_querydomain(name, domain, class, type, answer, anslen) 20233736Sbostic char *name, *domain; 20333736Sbostic int class, type; /* class and type of query */ 20433736Sbostic u_char *answer; /* buffer to put answer */ 20533736Sbostic int anslen; /* size of answer */ 20633736Sbostic { 20733736Sbostic char nbuf[2*MAXDNAME+2]; 20833736Sbostic char *longname = nbuf; 20933736Sbostic int n; 21033736Sbostic 21133977Skarels #ifdef DEBUG 21233977Skarels if (_res.options & RES_DEBUG) 21333977Skarels printf("res_querydomain(%s, %s, %d, %d)\n", 21433977Skarels name, domain, class, type); 21533977Skarels #endif 21633736Sbostic if (domain == NULL) { 21733736Sbostic /* 21833736Sbostic * Check for trailing '.'; 21933736Sbostic * copy without '.' if present. 22033736Sbostic */ 22133736Sbostic n = strlen(name) - 1; 22233736Sbostic if (name[n] == '.' && n < sizeof(nbuf) - 1) { 22333736Sbostic bcopy(name, nbuf, n); 22433736Sbostic nbuf[n] = '\0'; 22533736Sbostic } else 22633736Sbostic longname = name; 22733736Sbostic } else 22833736Sbostic (void)sprintf(nbuf, "%.*s.%.*s", 22933736Sbostic MAXDNAME, name, MAXDNAME, domain); 23033736Sbostic 23133736Sbostic return (res_query(longname, class, type, answer, anslen)); 23233736Sbostic } 23333736Sbostic 23433736Sbostic char * 23533736Sbostic hostalias(name) 23633736Sbostic register char *name; 23733736Sbostic { 23833736Sbostic register char *C1, *C2; 23933736Sbostic FILE *fp; 24033736Sbostic char *file, *getenv(), *strcpy(), *strncpy(); 24133736Sbostic char buf[BUFSIZ]; 24233736Sbostic static char abuf[MAXDNAME]; 24333736Sbostic 24433736Sbostic file = getenv("HOSTALIASES"); 24533736Sbostic if (file == NULL || (fp = fopen(file, "r")) == NULL) 24633736Sbostic return (NULL); 24733736Sbostic buf[sizeof(buf) - 1] = '\0'; 24833736Sbostic while (fgets(buf, sizeof(buf), fp)) { 24933736Sbostic for (C1 = buf; *C1 && !isspace(*C1); ++C1); 25033736Sbostic if (!*C1) 25133736Sbostic break; 25233736Sbostic *C1 = '\0'; 25333736Sbostic if (!strcasecmp(buf, name)) { 25433736Sbostic while (isspace(*++C1)); 25533736Sbostic if (!*C1) 25633736Sbostic break; 25733736Sbostic for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2); 25833736Sbostic abuf[sizeof(abuf) - 1] = *C2 = '\0'; 25933736Sbostic (void)strncpy(abuf, C1, sizeof(abuf) - 1); 26033736Sbostic fclose(fp); 26133736Sbostic return (abuf); 26233736Sbostic } 26333736Sbostic } 26433736Sbostic fclose(fp); 26533736Sbostic return (NULL); 26633736Sbostic } 267