1 /* $OpenBSD: res_mkquery.c,v 1.13 2019/01/14 06:49:42 otto 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 if (ac->ac_options & RES_USE_CD) 64 h.flags |= CD_MASK; 65 h.qdcount = 1; 66 if (ac->ac_options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) 67 h.arcount = 1; 68 69 _asr_pack_init(&p, buf, buflen); 70 _asr_pack_header(&p, &h); 71 _asr_pack_query(&p, type, class, dn); 72 if (ac->ac_options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) 73 _asr_pack_edns0(&p, MAXPACKETSZ, 74 ac->ac_options & RES_USE_DNSSEC); 75 76 _asr_ctx_unref(ac); 77 78 if (p.err) 79 return (-1); 80 81 return (p.offset); 82 } 83 84 /* 85 * This function is not documented, but used by sendmail. 86 * Put here because it uses asr_private.h too. 87 */ 88 int 89 res_querydomain(const char *name, 90 const char *domain, 91 int class, 92 int type, 93 u_char *answer, 94 int anslen) 95 { 96 char fqdn[MAXDNAME], ndom[MAXDNAME]; 97 size_t n; 98 99 /* we really want domain to end with a dot for now */ 100 if (domain && ((n = strlen(domain)) == 0 || domain[n - 1 ] != '.')) { 101 if (strlcpy(ndom, domain, sizeof(ndom)) >= sizeof(ndom) || 102 strlcat(ndom, ".", sizeof(ndom)) >= sizeof(ndom)) { 103 h_errno = NETDB_INTERNAL; 104 errno = EINVAL; 105 return (-1); 106 } 107 domain = ndom; 108 } 109 110 if (_asr_make_fqdn(name, domain, fqdn, sizeof fqdn) == 0) { 111 h_errno = NETDB_INTERNAL; 112 errno = EINVAL; 113 return (-1); 114 } 115 116 return (res_query(fqdn, class, type, answer, anslen)); 117 } 118