xref: /openbsd-src/lib/libc/asr/res_mkquery.c (revision 931108e92d6451cf619fc74dec5b7d66c67f834c)
1*931108e9Sjca /*	$OpenBSD: res_mkquery.c,v 1.14 2021/11/22 20:18:27 jca Exp $	*/
28082e013Seric /*
38082e013Seric  * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
48082e013Seric  *
58082e013Seric  * Permission to use, copy, modify, and distribute this software for any
68082e013Seric  * purpose with or without fee is hereby granted, provided that the above
78082e013Seric  * copyright notice and this permission notice appear in all copies.
88082e013Seric  *
98082e013Seric  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
108082e013Seric  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
118082e013Seric  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
128082e013Seric  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
138082e013Seric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
148082e013Seric  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
158082e013Seric  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
168082e013Seric  */
178082e013Seric 
188082e013Seric #include <sys/types.h>
19d216d6b1Seric #include <sys/socket.h>
208082e013Seric #include <netinet/in.h>
218082e013Seric #include <arpa/nameser.h> /* for MAXDNAME */
22d216d6b1Seric #include <netdb.h>
2380f48568Seric 
24d216d6b1Seric #include <asr.h>
258082e013Seric #include <errno.h>
268082e013Seric #include <resolv.h>
278082e013Seric #include <string.h>
288082e013Seric 
298082e013Seric #include "asr_private.h"
308082e013Seric 
318082e013Seric /* This function is apparently needed by some ports. */
328082e013Seric 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)338082e013Seric res_mkquery(int op, const char *dname, int class, int type,
348082e013Seric     const unsigned char *data, int datalen, const unsigned char *newrr,
358082e013Seric     unsigned char *buf, int buflen)
368082e013Seric {
378082e013Seric 	struct asr_ctx		*ac;
38f90bf415Seric 	struct asr_pack		 p;
39f90bf415Seric 	struct asr_dns_header	 h;
408082e013Seric 	char			 fqdn[MAXDNAME];
418082e013Seric 	char			 dn[MAXDNAME];
428082e013Seric 
438082e013Seric 	/* we currently only support QUERY */
448082e013Seric 	if (op != QUERY || data)
458082e013Seric 		return (-1);
468082e013Seric 
478082e013Seric 	if (dname[0] == '\0' || dname[strlen(dname) - 1] != '.') {
486dde8a29Seric 		if (strlcpy(fqdn, dname, sizeof(fqdn)) >= sizeof(fqdn) ||
496dde8a29Seric 		    strlcat(fqdn, ".", sizeof(fqdn)) >= sizeof(fqdn))
508082e013Seric 			return (-1);
518082e013Seric 		dname = fqdn;
528082e013Seric 	}
538082e013Seric 
54253ef892Sderaadt 	if (_asr_dname_from_fqdn(dname, dn, sizeof(dn)) == -1)
558082e013Seric 		return (-1);
568082e013Seric 
57253ef892Sderaadt 	ac = _asr_use_resolver(NULL);
588082e013Seric 
598082e013Seric 	memset(&h, 0, sizeof h);
608082e013Seric 	h.id = res_randomid();
618082e013Seric 	if (ac->ac_options & RES_RECURSE)
628082e013Seric 		h.flags |= RD_MASK;
63ca9d64e0Sotto 	if (ac->ac_options & RES_USE_CD)
643d657e16Sotto 		h.flags |= CD_MASK;
65*931108e9Sjca 	if (ac->ac_options & RES_TRUSTAD)
66*931108e9Sjca 		h.flags |= AD_MASK;
678082e013Seric 	h.qdcount = 1;
68d4d39a6fSjca 	if (ac->ac_options & (RES_USE_EDNS0 | RES_USE_DNSSEC))
692aa4cd21Sjca 		h.arcount = 1;
708082e013Seric 
71253ef892Sderaadt 	_asr_pack_init(&p, buf, buflen);
72253ef892Sderaadt 	_asr_pack_header(&p, &h);
73253ef892Sderaadt 	_asr_pack_query(&p, type, class, dn);
74d4d39a6fSjca 	if (ac->ac_options & (RES_USE_EDNS0 | RES_USE_DNSSEC))
75d4d39a6fSjca 		_asr_pack_edns0(&p, MAXPACKETSZ,
76d4d39a6fSjca 		    ac->ac_options & RES_USE_DNSSEC);
778082e013Seric 
78253ef892Sderaadt 	_asr_ctx_unref(ac);
798082e013Seric 
808082e013Seric 	if (p.err)
818082e013Seric 		return (-1);
828082e013Seric 
838082e013Seric 	return (p.offset);
848082e013Seric }
858082e013Seric 
868082e013Seric /*
878082e013Seric  * This function is not documented, but used by sendmail.
888082e013Seric  * Put here because it uses asr_private.h too.
898082e013Seric  */
908082e013Seric int
res_querydomain(const char * name,const char * domain,int class,int type,u_char * answer,int anslen)918082e013Seric res_querydomain(const char *name,
928082e013Seric     const char *domain,
938082e013Seric     int class,
948082e013Seric     int type,
958082e013Seric     u_char *answer,
968082e013Seric     int anslen)
978082e013Seric {
988082e013Seric 	char	fqdn[MAXDNAME], ndom[MAXDNAME];
998082e013Seric 	size_t	n;
1008082e013Seric 
1018082e013Seric 	/* we really want domain to end with a dot for now */
1028082e013Seric 	if (domain && ((n = strlen(domain)) == 0 || domain[n - 1 ] != '.')) {
1036dde8a29Seric 		if (strlcpy(ndom, domain, sizeof(ndom)) >= sizeof(ndom) ||
1046dde8a29Seric 		    strlcat(ndom, ".", sizeof(ndom)) >= sizeof(ndom)) {
1056dde8a29Seric 			h_errno = NETDB_INTERNAL;
1066dde8a29Seric 			errno = EINVAL;
1076dde8a29Seric 			return (-1);
1086dde8a29Seric 		}
109368b4e6aSguenther 		domain = ndom;
1108082e013Seric 	}
1118082e013Seric 
112253ef892Sderaadt 	if (_asr_make_fqdn(name, domain, fqdn, sizeof fqdn) == 0) {
1136dde8a29Seric 		h_errno = NETDB_INTERNAL;
1148082e013Seric 		errno = EINVAL;
1158082e013Seric 		return (-1);
1168082e013Seric 	}
1178082e013Seric 
1188082e013Seric 	return (res_query(fqdn, class, type, answer, anslen));
1198082e013Seric }
120