-
Notifications
You must be signed in to change notification settings - Fork 4
/
query-ldns.c
352 lines (283 loc) · 8.35 KB
/
query-ldns.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* query-ldns.c
*
* Query Address and TLSA records with libldns
*
* Author: Shumon Huque <[email protected]>
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ldns/ldns.h>
#include "query-ldns.h"
#include "utils.h"
/*
* Flags: dns bogus or indeterminate; authenticated responses
*/
int dns_bogus_or_indeterminate = 0;
int v4_authenticated = 0;
int v6_authenticated = 0;
int mx_authenticated = 0;
int srv_authenticated = 0;
int tlsa_authenticated = 0;
/*
* Linked list of addrinfo structures and related metadata
*/
size_t address_count = 0;
struct addrinfo *
insert_addrinfo(struct addrinfo **headp,
struct addrinfo *current, struct addrinfo *new)
{
if (current == NULL)
*headp = new;
else
current->ai_next = new;
return new;
}
/*
* tlsa_rdata: structure to hold TLSA record rdata.
* insert_tlsa_rdata(): insert node at tail of linked list of tlsa_rdata.
* free_tlsa(): free memory in the linked list.
*/
size_t tlsa_count = 0;
tlsa_rdata *
insert_tlsa_rdata(tlsa_rdata **headp, tlsa_rdata *current, tlsa_rdata *new)
{
if (current == NULL)
*headp = new;
else
current->next = new;
return new;
}
void free_tlsa(tlsa_rdata *head)
{
tlsa_rdata *current;
while ((current = head) != NULL) {
head = head->next;
LDNS_FREE(current->data);
free(current);
}
return;
}
/*
* print_tlsa() - print TLSA record rdata set
*/
void print_tlsa(tlsa_rdata *tlist)
{
char *cp;
tlsa_rdata *rp;
if (tlist) {
fprintf(stdout, "\nTLSA records found: %ld\n", tlsa_count);
for (rp = tlist; rp != NULL; rp = rp->next) {
fprintf(stdout, "TLSA: %d %d %d %s\n", rp->usage, rp->selector,
rp->mtype, (cp = bin2hexstring(rp->data, rp->data_len)));
free(cp);
}
(void) fputc('\n', stdout);
}
return;
}
/*
* rrlist_cat()
*/
void rrlist_cat(ldns_rr_list **dest, ldns_rr_list *rr_list)
{
if (*dest == NULL)
*dest = rr_list;
else
(void) ldns_rr_list_cat(*dest, rr_list);
return;
}
/*
* load_address()
*/
struct addrinfo *load_address(struct addrinfo **headp, struct addrinfo *cur,
ldns_rr *rr, uint16_t port)
{
ldns_rr_type rrtype = ldns_rr_get_type(rr);
struct addrinfo *aip = malloc(sizeof(struct addrinfo));
aip->ai_flags = 0;
aip->ai_canonname = NULL;
aip->ai_next = NULL;
if (rrtype == LDNS_RR_TYPE_AAAA) {
struct sockaddr_in6 *sa6 = malloc(sizeof(struct sockaddr_storage));
aip->ai_family = sa6->sin6_family = AF_INET6;
sa6->sin6_port = htons(port);
memcpy(&(sa6->sin6_addr),
ldns_rdf_data(ldns_rr_rdf(rr, 0)),
ldns_rdf_size(ldns_rr_rdf(rr, 0)));
aip->ai_addr = (struct sockaddr *) sa6;
aip->ai_addrlen = sizeof(struct sockaddr_in6);
} else if (rrtype == LDNS_RR_TYPE_A) {
struct sockaddr_in *sa4 = malloc(sizeof(struct sockaddr_storage));
aip->ai_family = sa4->sin_family = AF_INET;
sa4->sin_port = htons(port);
memcpy(&(sa4->sin_addr),
ldns_rdf_data(ldns_rr_rdf(rr, 0)),
ldns_rdf_size(ldns_rr_rdf(rr, 0)));
aip->ai_addr = (struct sockaddr *) sa4;
aip->ai_addrlen = sizeof(struct sockaddr_in);
}
return insert_addrinfo(headp, cur, aip);
}
/*
* load_addresses()
* load addresses from an ldns_rr_list structure containing address RRs
* into the addresses linked list.
*/
struct addrinfo *load_addresses(ldns_rr_list *rr_list, uint16_t port)
{
size_t i;
ldns_rr *rr;
struct addrinfo *addresses = NULL, *current = NULL;
address_count = ldns_rr_list_rr_count(rr_list);
for (i = 0; i < address_count; i++) {
rr = ldns_rr_list_rr(rr_list, i);
current = load_address(&addresses, current, rr, port);
}
return addresses;
}
/*
* get_addresses_type()
*/
ldns_rr_list *get_addresses_type(ldns_resolver *resolver,
ldns_rr_type rrtype,
ldns_rdf *host_rdf)
{
ldns_pkt *ldns_p;
ldns_pkt_rcode rcode;
ldns_p = ldns_resolver_query(resolver, host_rdf, rrtype,
LDNS_RR_CLASS_IN, LDNS_RD | LDNS_AD);
if (ldns_p == (ldns_pkt *) NULL) {
dns_bogus_or_indeterminate = 1;
fprintf(stderr, "No response to address query.\n");
return NULL;
}
rcode = ldns_pkt_get_rcode(ldns_p);
switch (rcode) {
case LDNS_RCODE_NOERROR:
case LDNS_RCODE_NXDOMAIN:
break;
default:
dns_bogus_or_indeterminate = 1;
fprintf(stderr, "Error: address query failed; rcode=%d.\n", rcode);
ldns_pkt_free(ldns_p);
return NULL;
}
if (ldns_pkt_ad(ldns_p)) {
if (rrtype == LDNS_RR_TYPE_AAAA)
v6_authenticated = 1;
else if (rrtype == LDNS_RR_TYPE_A)
v4_authenticated = 1;
}
return ldns_pkt_rr_list_by_type(ldns_p, rrtype, LDNS_SECTION_ANSWER);
}
/*
* get_addresses()
* Obtain IPv4 and IPv6 addresses and populate addresses linked list.
*/
struct addrinfo *get_addresses(ldns_resolver *resolver,
const char *hostname, uint16_t port)
{
ldns_rdf *host_rdf;
ldns_rr_list *rr_list = NULL;
host_rdf = ldns_dname_new_frm_str(hostname);
rrlist_cat(&rr_list,
get_addresses_type(resolver, LDNS_RR_TYPE_AAAA, host_rdf));
rrlist_cat(&rr_list,
get_addresses_type(resolver, LDNS_RR_TYPE_A, host_rdf));
ldns_rdf_deep_free(host_rdf);
return load_addresses(rr_list, port);
}
/*
* get_tlsa(): get TLSA records.
* Populates tlsa_rdata_list linked list with TLSA record rdata.
*/
tlsa_rdata *get_tlsa(ldns_resolver *resolver,
const char *hostname, uint16_t port)
{
size_t i;
char domainstring[512];
ldns_rdf *tlsa_owner;
ldns_pkt *ldns_p;
ldns_rr_list *tlsa_rr_list;
ldns_rr *tlsa_rr;
ldns_pkt_rcode rcode;
tlsa_rdata *tlsa_rdata_list = NULL, *current = NULL;
snprintf(domainstring, sizeof(domainstring), "_%d._tcp.%s", port, hostname);
tlsa_owner = ldns_dname_new_frm_str(domainstring);
ldns_p = ldns_resolver_query(resolver, tlsa_owner, LDNS_RR_TYPE_TLSA,
LDNS_RR_CLASS_IN, LDNS_RD | LDNS_AD);
ldns_rdf_deep_free(tlsa_owner);
if (ldns_p == (ldns_pkt *) NULL) {
dns_bogus_or_indeterminate = 1;
fprintf(stderr, "No response to TLSA query.\n");
return NULL;
}
rcode = ldns_pkt_get_rcode(ldns_p);
switch (rcode) {
case LDNS_RCODE_NOERROR:
break;
case LDNS_RCODE_NXDOMAIN:
fprintf(stderr, "No TLSA records found.\n");
ldns_pkt_free(ldns_p);
return NULL;
default:
dns_bogus_or_indeterminate = 1;
fprintf(stderr, "Error: TLSA query failed; rcode=%d.\n", rcode);
ldns_pkt_free(ldns_p);
return NULL;
}
tlsa_rr_list = ldns_pkt_rr_list_by_type(ldns_p, LDNS_RR_TYPE_TLSA,
LDNS_SECTION_ANSWER);
if (tlsa_rr_list == NULL) {
fprintf(stderr, "No TLSA records found.\n");
ldns_pkt_free(ldns_p);
return NULL;
}
if (! ldns_pkt_ad(ldns_p)) {
fprintf(stderr, "Unauthenticated response for TLSA record set.\n");
ldns_pkt_free(ldns_p);
return NULL;
}
tlsa_authenticated = 1;
/* Extract list of RDATA fields from TLSA rrset */
tlsa_count = ldns_rr_list_rr_count(tlsa_rr_list);
for (i = 0; i < tlsa_count; i++) {
tlsa_rdata *rp = (tlsa_rdata *) malloc(sizeof(tlsa_rdata));
tlsa_rr = ldns_rr_list_rr(tlsa_rr_list, i);
rp->usage = ldns_rdf2native_int8(ldns_rr_rdf(tlsa_rr, 0));
rp->selector = ldns_rdf2native_int8(ldns_rr_rdf(tlsa_rr, 1));
rp->mtype = ldns_rdf2native_int8(ldns_rr_rdf(tlsa_rr, 2));
rp->data_len = ldns_rdf_size(ldns_rr_rdf(tlsa_rr, 3));
rp->data = ldns_rdf_data(ldns_rr_rdf(tlsa_rr, 3));
rp->next = NULL;
current = insert_tlsa_rdata(&tlsa_rdata_list, current, rp);
}
ldns_pkt_free(ldns_p);
return tlsa_rdata_list;
}
/*
* get_resolver()
* Initialize an ldns resolver. If conffile is NULL, then the system
* default resolver configuration is used (typically /etc/resolv.conf).
*/
ldns_resolver *get_resolver(char *conffile)
{
ldns_resolver *resolver;
ldns_status ldns_rc;
ldns_rc = ldns_resolver_new_frm_file(&resolver, conffile);
if (ldns_rc != LDNS_STATUS_OK) {
fprintf(stderr, "failed to initialize DNS resolver: %s\n",
ldns_get_errorstr_by_id(ldns_rc));
return NULL;
}
return resolver;
}