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