| 1 |  |  | /*	$OpenBSD: res_mkquery.c,v 1.11 2017/02/27 11:38:08 jca Exp $	*/ | 
    
    | 2 |  |  | /* | 
    
    | 3 |  |  |  * Copyright (c) 2012 Eric Faurot <eric@openbsd.org> | 
    
    | 4 |  |  |  * | 
    
    | 5 |  |  |  * Permission to use, copy, modify, and distribute this software for any | 
    
    | 6 |  |  |  * purpose with or without fee is hereby granted, provided that the above | 
    
    | 7 |  |  |  * copyright notice and this permission notice appear in all copies. | 
    
    | 8 |  |  |  * | 
    
    | 9 |  |  |  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | 
    
    | 10 |  |  |  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | 
    
    | 11 |  |  |  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | 
    
    | 12 |  |  |  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | 
    
    | 13 |  |  |  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | 
    
    | 14 |  |  |  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | 
    
    | 15 |  |  |  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 
    
    | 16 |  |  |  */ | 
    
    | 17 |  |  |  | 
    
    | 18 |  |  | #include <sys/types.h> | 
    
    | 19 |  |  | #include <sys/socket.h> | 
    
    | 20 |  |  | #include <netinet/in.h> | 
    
    | 21 |  |  | #include <arpa/nameser.h> /* for MAXDNAME */ | 
    
    | 22 |  |  | #include <netdb.h> | 
    
    | 23 |  |  |  | 
    
    | 24 |  |  | #include <asr.h> | 
    
    | 25 |  |  | #include <errno.h> | 
    
    | 26 |  |  | #include <resolv.h> | 
    
    | 27 |  |  | #include <string.h> | 
    
    | 28 |  |  |  | 
    
    | 29 |  |  | #include "asr_private.h" | 
    
    | 30 |  |  |  | 
    
    | 31 |  |  | /* This function is apparently needed by some ports. */ | 
    
    | 32 |  |  | int | 
    
    | 33 |  |  | res_mkquery(int op, const char *dname, int class, int type, | 
    
    | 34 |  |  |     const unsigned char *data, int datalen, const unsigned char *newrr, | 
    
    | 35 |  |  |     unsigned char *buf, int buflen) | 
    
    | 36 |  |  | { | 
    
    | 37 |  |  | 	struct asr_ctx		*ac; | 
    
    | 38 |  |  | 	struct asr_pack		 p; | 
    
    | 39 |  |  | 	struct asr_dns_header	 h; | 
    
    | 40 |  |  | 	char			 fqdn[MAXDNAME]; | 
    
    | 41 |  |  | 	char			 dn[MAXDNAME]; | 
    
    | 42 |  |  |  | 
    
    | 43 |  |  | 	/* we currently only support QUERY */ | 
    
    | 44 |  |  | 	if (op != QUERY || data) | 
    
    | 45 |  |  | 		return (-1); | 
    
    | 46 |  |  |  | 
    
    | 47 |  |  | 	if (dname[0] == '\0' || dname[strlen(dname) - 1] != '.') { | 
    
    | 48 |  |  | 		if (strlcpy(fqdn, dname, sizeof(fqdn)) >= sizeof(fqdn) || | 
    
    | 49 |  |  | 		    strlcat(fqdn, ".", sizeof(fqdn)) >= sizeof(fqdn)) | 
    
    | 50 |  |  | 			return (-1); | 
    
    | 51 |  |  | 		dname = fqdn; | 
    
    | 52 |  |  | 	} | 
    
    | 53 |  |  |  | 
    
    | 54 |  |  | 	if (_asr_dname_from_fqdn(dname, dn, sizeof(dn)) == -1) | 
    
    | 55 |  |  | 		return (-1); | 
    
    | 56 |  |  |  | 
    
    | 57 |  |  | 	ac = _asr_use_resolver(NULL); | 
    
    | 58 |  |  |  | 
    
    | 59 |  |  | 	memset(&h, 0, sizeof h); | 
    
    | 60 |  |  | 	h.id = res_randomid(); | 
    
    | 61 |  |  | 	if (ac->ac_options & RES_RECURSE) | 
    
    | 62 |  |  | 		h.flags |= RD_MASK; | 
    
    | 63 |  |  | 	h.qdcount = 1; | 
    
    | 64 |  |  | 	if (ac->ac_options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) | 
    
    | 65 |  |  | 		h.arcount = 1; | 
    
    | 66 |  |  |  | 
    
    | 67 |  |  | 	_asr_pack_init(&p, buf, buflen); | 
    
    | 68 |  |  | 	_asr_pack_header(&p, &h); | 
    
    | 69 |  |  | 	_asr_pack_query(&p, type, class, dn); | 
    
    | 70 |  |  | 	if (ac->ac_options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) | 
    
    | 71 |  |  | 		_asr_pack_edns0(&p, MAXPACKETSZ, | 
    
    | 72 |  |  | 		    ac->ac_options & RES_USE_DNSSEC); | 
    
    | 73 |  |  |  | 
    
    | 74 |  |  | 	_asr_ctx_unref(ac); | 
    
    | 75 |  |  |  | 
    
    | 76 |  |  | 	if (p.err) | 
    
    | 77 |  |  | 		return (-1); | 
    
    | 78 |  |  |  | 
    
    | 79 |  |  | 	return (p.offset); | 
    
    | 80 |  |  | } | 
    
    | 81 |  |  |  | 
    
    | 82 |  |  | /* | 
    
    | 83 |  |  |  * This function is not documented, but used by sendmail. | 
    
    | 84 |  |  |  * Put here because it uses asr_private.h too. | 
    
    | 85 |  |  |  */ | 
    
    | 86 |  |  | int | 
    
    | 87 |  |  | res_querydomain(const char *name, | 
    
    | 88 |  |  |     const char *domain, | 
    
    | 89 |  |  |     int class, | 
    
    | 90 |  |  |     int type, | 
    
    | 91 |  |  |     u_char *answer, | 
    
    | 92 |  |  |     int anslen) | 
    
    | 93 |  |  | { | 
    
    | 94 |  |  | 	char	fqdn[MAXDNAME], ndom[MAXDNAME]; | 
    
    | 95 |  |  | 	size_t	n; | 
    
    | 96 |  |  |  | 
    
    | 97 |  |  | 	/* we really want domain to end with a dot for now */ | 
    
    | 98 |  |  | 	if (domain && ((n = strlen(domain)) == 0 || domain[n - 1 ] != '.')) { | 
    
    | 99 |  |  | 		if (strlcpy(ndom, domain, sizeof(ndom)) >= sizeof(ndom) || | 
    
    | 100 |  |  | 		    strlcat(ndom, ".", sizeof(ndom)) >= sizeof(ndom)) { | 
    
    | 101 |  |  | 			h_errno = NETDB_INTERNAL; | 
    
    | 102 |  |  | 			errno = EINVAL; | 
    
    | 103 |  |  | 			return (-1); | 
    
    | 104 |  |  | 		} | 
    
    | 105 |  |  | 		domain = ndom; | 
    
    | 106 |  |  | 	} | 
    
    | 107 |  |  |  | 
    
    | 108 |  |  | 	if (_asr_make_fqdn(name, domain, fqdn, sizeof fqdn) == 0) { | 
    
    | 109 |  |  | 		h_errno = NETDB_INTERNAL; | 
    
    | 110 |  |  | 		errno = EINVAL; | 
    
    | 111 |  |  | 		return (-1); | 
    
    | 112 |  |  | 	} | 
    
    | 113 |  |  |  | 
    
    | 114 |  |  | 	return (res_query(fqdn, class, type, answer, anslen)); | 
    
    | 115 |  |  | } |