forked from trusteddomainproject/OpenDMARC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopendmarc_dns.c
377 lines (348 loc) · 8.47 KB
/
opendmarc_dns.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/***********************************************************************
** OPENDMARC_DNS.C
** DMARC_DNS_GET_RECORD -- looks up and returns the txt record
** DMARC_DNS_TEST_RECORD -- hook to test
**
** Copyright (c) 2012-2016, The Trusted Domain Project. All rights reserved.
************************************************************************/
#ifndef BIND_8_COMPAT
# define BIND_8_COMPAT
#endif /* ! BIND_8_COMPAT */
#include <netdb.h>
#include "opendmarc_internal.h"
#ifndef MAXPACKET
# define MAXPACKET (8192)
#endif
struct fake_dns_data
{
const char * fdns_name;
const char * fdns_answer;
struct fake_dns_data * fdns_next;
};
static struct fake_dns_data * fake_dns = NULL;
static struct fake_dns_data * fake_dns_tail = NULL;
/*************************************************************************
** OPENDMARC_DNS_FAKE_RECORD -- store a fake DNS reply
**
** Arguments:
** name -- name of fake record to add
** answer -- answer to fake record
**
** Return Values:
** None.
**
** Side Effects:
** Calls to dmarc_dns_get_record() will check this list for an answer
** rather than using live DNS. This is intended to be used by test
** harnesses that have no DNS access.
*************************************************************************/
void
opendmarc_dns_fake_record(const char *name, const char *answer)
{
struct fake_dns_data *new;
if (name == NULL)
return;
new = malloc(sizeof *new);
if (new != NULL)
{
new->fdns_name = strdup(name);
if (new->fdns_name == NULL)
{
free(new);
return;
}
new->fdns_answer = strdup(answer);
if (new->fdns_answer == NULL)
{
free((void *) new->fdns_name);
free(new);
return;
}
new->fdns_next = NULL;
if (fake_dns == NULL)
{
fake_dns = new;
fake_dns_tail = new;
}
else
{
fake_dns_tail->fdns_next = new;
fake_dns_tail = new;
}
}
}
/*************************************************************************
** DMARC_DNS_GET_RECORD -- looks up and returns the txt record
**
** Arguments:
** domain -- the domain name to look up.
** reply -- pointer to an integer to get query status
** got_txtbuf -- where to scribble the found txt record
** got_txtlen -- size of txt record buffer
**
** Return Values:
** got_txtbuf -- pointer to got_txtbuf on success
** NULL -- otherise, and place the h_errno error into reply
**
** Side Effects:
** Makes a connection to the local (or specified)
** name server and and may block waiting for a reply.
**
*************************************************************************/
char *
dmarc_dns_get_record(char *domain, int *reply, char *got_txtbuf, size_t got_txtlen)
{
u_char * end_ptr = NULL;
u_char * cur_ptr = NULL;
u_char * got_ptr = NULL;
u_char * gote_ptr = NULL;
int ch = 0;
short cur_len = 0;
HEADER header;
int class = -1;
int acnt = -1;
int qdcnt = -1;
u_short type = 0;
u_long ttl = 0;
char * bp = NULL;
int fake_reply = 0;
int * reply_ptr = NULL;
u_char answer_buf[MAXPACKET];
int answer_len;
char hbuf[MAXDNSHOSTNAME];
char namebuf[MAXDNSHOSTNAME + 1];
#if HAVE_RES_NINIT
struct __res_state resp;
#endif /* HAVE_RES_NINIT */
/*
* Short circuit the return "reply" if no variable provided.
*/
if (reply == NULL)
reply_ptr = &fake_reply;
else
reply_ptr = reply;
/*
* If a null or empy domain was given to us, just say that it
* was not found.
*/
*reply_ptr = 0;
if (domain == NULL || *domain == '\0')
{
*reply_ptr = HOST_NOT_FOUND;
return NULL;
}
/*
* If no buffer was supplied to return the txt record,
* pretend nothing was found.
*/
if (got_txtbuf == NULL || got_txtlen == 0)
{
*reply_ptr = HOST_NOT_FOUND;
return NULL;
}
/*
* Pull the answer from the fake DNS table if there is one.
*/
if (fake_dns != NULL)
{
struct fake_dns_data *cur;
for (cur = fake_dns; cur != NULL; cur = cur->fdns_next)
{
if (strcasecmp(cur->fdns_name, domain) == 0)
{
strncpy(got_txtbuf, cur->fdns_answer,
got_txtlen - 1);
*reply_ptr = NETDB_SUCCESS;
return got_txtbuf;
}
}
*reply_ptr = NO_DATA;
return NULL;
}
/*
* Copy the domain so we can scribble on it. The orginal
* may point to a static string.
* We should use strlcpy(), but not all systems have it.
*/
(void) memset(hbuf, '\0', sizeof hbuf);
(void) strncpy(hbuf, domain, sizeof hbuf - 1);
bp = hbuf;
/*
* Make sure host ends in a dot to short circuit lookups
*/
bp = hbuf + strlen(hbuf) - 1;
if (*bp != '.')
*++bp = '.';
*++bp = '\0';
/*
* Make user host does not begin with a dot.
*/
bp = hbuf;
while (*bp == '.')
++bp;
#ifdef HAVE_RES_NINIT
memset(&resp, '\0', sizeof resp);
resp.options |= RES_DEFAULT;
#if defined RES_USE_DNSSEC
resp.options |= RES_USE_DNSSEC;
#endif
res_ninit(&resp);
(void) opendmarc_policy_library_dns_hook(&resp.nscount,
&resp.nsaddr_list);
answer_len = res_nquery(&resp, bp, C_IN, T_TXT, answer_buf, sizeof answer_buf);
res_nclose(&resp);
#else /* HAVE_RES_NINIT */
#if defined RES_USE_DNSSEC
_res.options |= RES_USE_DNSSEC;
#endif
(void) opendmarc_policy_library_dns_hook(&_res.nscount,
_res.nsaddr_list);
answer_len = res_query(bp, C_IN, T_TXT, answer_buf, sizeof answer_buf);
#endif /* HAVE_RES_NINIT */
if (answer_len < 0)
{
if (h_errno == NETDB_SUCCESS)
h_errno = NO_DATA;
*reply_ptr = h_errno;
return NULL;
}
/*
* Truncate answer if it is too big.
*/
if (answer_len > sizeof answer_buf)
answer_len = sizeof answer_buf;
(void) memcpy(&header, answer_buf, sizeof header);
cur_ptr = (u_char *)&answer_buf + HFIXEDSZ;
end_ptr = (u_char *)&answer_buf + answer_len;
(void) memset(namebuf, '\0', sizeof namebuf);
/* skip question part of response -- we know what we asked */
for (qdcnt = ntohs(header.qdcount); qdcnt > 0; qdcnt--)
{
(void) dn_expand((unsigned char *) &answer_buf, end_ptr,
cur_ptr, namebuf, sizeof namebuf);
if ((answer_len = dn_skipname(cur_ptr, end_ptr)) < 0)
{
*reply_ptr = NO_DATA;
return NULL;
}
cur_ptr += answer_len;
if (cur_ptr + INT16SZ + INT16SZ > end_ptr)
{
*reply_ptr = NO_DATA;
return NULL;
}
GETSHORT(type, cur_ptr);
GETSHORT(class, cur_ptr);
}
if (header.rcode != NOERROR)
{
*reply_ptr = NO_DATA;
return NULL;
}
acnt = ntohs((unsigned short) header.ancount);
if (acnt == 0)
{
*reply_ptr = NO_DATA;
return NULL;
}
while (--acnt >= 0 && cur_ptr < end_ptr)
{
if ((answer_len = dn_expand((unsigned char *) &answer_buf,
end_ptr, cur_ptr, namebuf,
sizeof namebuf)) < 0)
{
*reply_ptr = NO_DATA;
return NULL;
}
cur_ptr += answer_len;
if (cur_ptr + INT16SZ + INT16SZ > end_ptr)
{
/* currupt answer */
*reply_ptr = NO_DATA;
return NULL;
}
GETSHORT(type, cur_ptr);
GETSHORT(class, cur_ptr);
/* we may want to use the ttl later */
GETLONG(ttl, cur_ptr);
if (type == T_CNAME)
{
/*
* Copy the cname just incase the resolver
* didn't also follow it an give us the text
* record.
*/
if (got_txtbuf[0] == '\0')
{
(void) memset(got_txtbuf, '\0', got_txtlen);
answer_len = dn_expand((unsigned char *)&answer_buf,
end_ptr, cur_ptr,
got_txtbuf, got_txtlen);
cur_ptr += answer_len;
}
else
{
cur_ptr += dn_skipname(end_ptr, cur_ptr);
}
continue;
}
#ifdef T_RRSIG
else if (type == T_RRSIG)
{
GETSHORT(answer_len, cur_ptr);
cur_ptr += answer_len;
}
#endif /* T_RRSIG */
else if (type != T_TXT)
{
/*
* TODO: Fail or should we ignore it?
*/
*reply_ptr = NO_DATA;
return NULL;
}
if (cur_ptr + INT16SZ > end_ptr)
{
/*
* Yikes. No payload length
*/
*reply_ptr = NO_DATA;
return NULL;
}
GETSHORT(cur_len, cur_ptr);
if (cur_ptr + cur_len > end_ptr)
{
/*
* If the payload length greater than remaining buffer
*/
*reply_ptr = NO_DATA;
return NULL;
}
(void) memset(got_txtbuf, '\0', got_txtlen);
/* copy the returned record into got_txtbuf */
got_ptr = (u_char *)got_txtbuf;
gote_ptr = (u_char *)got_txtbuf + got_txtlen -1;
while (cur_len > 0 && got_ptr < gote_ptr)
{
ch = *cur_ptr++;
cur_len--;
while (ch > 0 && got_ptr < gote_ptr)
{
*got_ptr++ = *cur_ptr++;
ch--;
cur_len--;
}
}
if (strstr(got_txtbuf, "v=DMARC") != NULL)
{
*reply_ptr = NETDB_SUCCESS;
return got_txtbuf;
}
*got_txtbuf = '\0';
cur_ptr += cur_len;
cur_ptr += answer_len;
continue;
}
*reply_ptr = NO_DATA;
return NULL;
}