xref: /openbsd-src/lib/libc/asr/res_mkquery.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: res_mkquery.c,v 1.8 2014/03/26 18:13:15 eric 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 
65 	asr_pack_init(&p, buf, buflen);
66 	asr_pack_header(&p, &h);
67 	asr_pack_query(&p, type, class, dn);
68 
69 	asr_ctx_unref(ac);
70 
71 	if (p.err)
72 		return (-1);
73 
74 	return (p.offset);
75 }
76 
77 /*
78  * This function is not documented, but used by sendmail.
79  * Put here because it uses asr_private.h too.
80  */
81 int
82 res_querydomain(const char *name,
83     const char *domain,
84     int class,
85     int type,
86     u_char *answer,
87     int anslen)
88 {
89 	char	fqdn[MAXDNAME], ndom[MAXDNAME];
90 	size_t	n;
91 
92 	/* we really want domain to end with a dot for now */
93 	if (domain && ((n = strlen(domain)) == 0 || domain[n - 1 ] != '.')) {
94 		if (strlcpy(ndom, domain, sizeof(ndom)) >= sizeof(ndom) ||
95 		    strlcat(ndom, ".", sizeof(ndom)) >= sizeof(ndom)) {
96 			h_errno = NETDB_INTERNAL;
97 			errno = EINVAL;
98 			return (-1);
99 		}
100 		domain = ndom;
101 	}
102 
103 	if (asr_make_fqdn(name, domain, fqdn, sizeof fqdn) == 0) {
104 		h_errno = NETDB_INTERNAL;
105 		errno = EINVAL;
106 		return (-1);
107 	}
108 
109 	return (res_query(fqdn, class, type, answer, anslen));
110 }
111