aboutsummaryrefslogtreecommitdiffstats
path: root/src/spf.c
blob: c7243e89c89492332edced324a210c9180a297ae (plain)
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/*
 * Copyright (c) 2009, Rambler media
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY Rambler media ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Rambler BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "evdns/evdns.h"
#include "spf.h"
#include "main.h"
#include "message.h"
#include "filter.h"

#define SPF_VER_STR "v=spf1"
#define SPF_ALL "all"
#define SPF_A "a"
#define SPF_IP4 "ip4"
#define SPF_IP6 "ip6"
#define SPF_PTR "ptr"
#define SPF_MX "mx"
#define SPF_EXISTS "exists"
#define SPF_INCLUDE "include"
#define SPF_REDIRECT "redirect"
#define SPF_EXP "exp"

/** SPF limits for avoiding abuse **/
#define SPF_MAX_NESTING 5
#define SPF_MAX_DNS_REQUESTS 10

/**
 * State machine for SPF record:
 *
 * spf_mech ::= +|-|~|?
 *
 * spf_body ::= spf=v1 <spf_command> [<spf_command>]
 * spf_command ::= [spf_mech]all|a|<ip4>|<ip6>|ptr|mx|<exists>|<include>|<redirect>
 *
 * spf_domain ::= [:domain][/mask]
 * spf_ip4 ::= ip[/mask]
 * ip4 ::= ip4:<spf_ip4>
 * mx ::= mx<spf_domain>
 * a ::= a<spf_domain>
 * ptr ::= ptr[:domain]
 * exists ::= exists:domain
 * include ::= include:domain
 * redirect ::= redirect:domain
 * exp ::= exp:domain
 *
 */

struct spf_dns_cb {
	struct spf_record *rec;
	struct spf_addr *addr;
	spf_action_t cur_action;
};

#define CHECK_REC(rec)										\
do {														\
	if ((rec)->nested > SPF_MAX_NESTING ||					\
		(rec)->dns_requests > SPF_MAX_DNS_REQUESTS) {		\
		return FALSE;										\
	}														\
} while (0)													\

static gboolean parse_spf_record (struct worker_task *task, struct spf_record *rec);
static void start_spf_parse (struct spf_record *rec, char *begin);

/* Determine spf mech */
static spf_mech_t
check_spf_mech (const char *elt, gboolean *need_shift)
{
	g_assert (elt != NULL);
	
	*need_shift = TRUE;

	switch (*elt) {
		case '-':
			return SPF_FAIL;
		case '~':
			return SPF_SOFT_FAIL;
		case '+':
			return SPF_PASS;
		case '?':
			return SPF_NEUTRAL;
		default:
			*need_shift = FALSE;
			return SPF_PASS;	
	}
}

static gboolean
parse_spf_ipmask (const char *begin, struct spf_addr *addr)
{
	const char *pos;
	char ip_buf[sizeof ("255.255.255.255")], mask_buf[3], *p;
	int state = 0, dots = 0;
	struct in_addr in;
	
	bzero (ip_buf, sizeof (ip_buf));
	bzero (mask_buf, sizeof (mask_buf));
	pos = begin;

	while (*pos) {
		switch (state) {
			case 0:
				/* Require ':' */
				if (*pos != ':') {
					return FALSE;
				}
				state = 1;
				pos ++;
				p = ip_buf;
				dots = 0;
				break;
			case 1:
				/* Begin parse ip */
				if (p - ip_buf >= sizeof (ip_buf) || dots > 3) {
					return FALSE;
				}
				if (g_ascii_isdigit (*pos)) {
					*p ++ = *pos ++;
				}
				else if (*pos == '.') {
					*p ++ = *pos ++;
					dots ++;
				}
				else if (*pos == '/') {
					pos ++;
					p = mask_buf;
					state = 2;
				}
				else {
					/* Invalid character */
					return FALSE;
				}
				break;
			case 2:
				/* Parse mask */
				if (p - mask_buf > 2) {
					return FALSE;
				}
				if (g_ascii_isdigit (*pos)) {
					*p ++ = *pos ++;
				}
				else {
					return FALSE;
				}
				break;
		}
	}

	if (!inet_aton (ip_buf, &in)) {
		return FALSE;
	}
	addr->addr = ntohl (in.s_addr);
	if (state == 2) {
		/* Also parse mask */
		addr->mask = (mask_buf[0] - '0') * 10 + mask_buf[1] - '0';
		if (addr->mask > 32) {
			msg_info ("parse_spf_ipmask: bad ipmask value: '%s'", begin);
			return FALSE;
		}
	}
	else {
		addr->mask = 32;
	}

	return TRUE;

}

static char *
parse_spf_hostmask (struct worker_task *task, const char *begin, struct spf_addr *addr, struct spf_record *rec)
{
	char *host = NULL, *p,  mask_buf[3];
	int hostlen;

	bzero (mask_buf, sizeof (mask_buf));
	if (*begin == '\0' || *begin == '/') {
		/* Assume host as host to resolve from record */
		host = rec->cur_domain;
	}
	p = strchr (begin, '/');
	if (p != NULL) {
		/* Extract mask */
		g_strlcpy (mask_buf, p + 1, sizeof (mask_buf));
		addr->mask = mask_buf[0] * 10 + mask_buf[1];
		if (addr->mask > 32) {
			return FALSE;
		}
		if (host == NULL) {
			hostlen = p - begin;
			host = memory_pool_alloc (task->task_pool, hostlen);
			g_strlcpy (host, begin, hostlen);
		}
	}
	else {
		addr->mask = 32;
		if (host == NULL) {
			g_strlcpy (host, begin, strlen (begin));
		}
	}

	return host;
}

static void
spf_record_dns_callback (int result, char type, int count, int ttl, void *addresses, void *data)
{
	struct spf_dns_cb *cb = data;
	char *begin;
	struct evdns_mx *mx;
	GList *tmp = NULL;

	if (result == DNS_ERR_NONE) {
		if (addresses != NULL) {
			/* Add all logic for all DNS states here */
			switch (cb->cur_action) {
				case SPF_RESOLVE_MX:
					if (type == DNS_MX) {
						mx = (struct evdns_mx *)addresses;
						/* Now resolve A record for this MX */
						if (evdns_resolve_ipv4 (mx->host, DNS_QUERY_NO_SEARCH, spf_record_dns_callback, (void *)cb) == 0) {
							return;
						}
					}
					else if (type == DNS_IPv4_A) {
						/* XXX: process only one record */
						cb->addr->addr = ntohl (*((uint32_t *)addresses));
					}
					break;
				case SPF_RESOLVE_A:
					if (type == DNS_IPv4_A) {
						/* XXX: process only one record */
						cb->addr->addr = ntohl (*((uint32_t *)addresses));
					}
					break;
				case SPF_RESOLVE_PTR:
					break;
				case SPF_RESOLVE_REDIRECT:
					if (type == DNS_TXT) {
						if (addresses != NULL) {
							begin = *(char **)addresses;
							if (cb->rec->addrs) {
								g_list_free (cb->rec->addrs);
								cb->rec->addrs = NULL;
							}
							start_spf_parse (cb->rec, begin);
						}
					}
					break;
				case SPF_RESOLVE_INCLUDE:
					if (type == DNS_TXT) {
						if (addresses != NULL) {
							begin = *(char **)addresses;
							if (cb->rec->addrs) {
								tmp = cb->rec->addrs;
								cb->rec->addrs = NULL;
							}
							start_spf_parse (cb->rec, begin);
							if (tmp) {
								cb->rec->addrs = g_list_concat (tmp, cb->rec->addrs);
							}
						}
					}
					break;
				case SPF_RESOLVE_EXP:
					break;
				case SPF_RESOLVE_EXISTS:
					if (type == DNS_IPv4_A) {
						/* If specified address resolves, we can accept connection from every IP */
						cb->addr->addr = ntohl (INADDR_ANY);
					}
					break;
			}
		}
	}

	cb->rec->task->save.saved--;
	if (cb->rec->task->save.saved == 0 && cb->rec->callback) {
		if (cb->rec->addrs) {
			cb->rec->callback (cb->rec, cb->rec->task);
			g_list_free (cb->rec->addrs);
			cb->rec->addrs = NULL;
		}
	}
	remove_forced_event (cb->rec->task->s, (event_finalizer_t) spf_record_dns_callback);

}

static gboolean
parse_spf_a (struct worker_task *task, const char *begin, struct spf_record *rec, struct spf_addr *addr)
{
	struct spf_dns_cb *cb;
	char *host;
	
	CHECK_REC (rec);
	
	if (begin == NULL || *begin != ':') {
		return FALSE;
	}
	begin ++;
	
	host = parse_spf_hostmask (task, begin, addr, rec);
	
	if (!host) {
		return FALSE;
	}

	rec->dns_requests ++;
	cb = memory_pool_alloc (task->task_pool, sizeof (struct spf_dns_cb));
	cb->rec = rec;
	cb->addr = addr;
	cb->cur_action = SPF_RESOLVE_A;

	if (evdns_resolve_ipv4 (host, DNS_QUERY_NO_SEARCH, spf_record_dns_callback, (void *)cb) == 0) {
		task->save.saved++;
		register_async_event (task->s, (event_finalizer_t) spf_record_dns_callback, NULL, TRUE);
		
		return TRUE;
	}

	return FALSE;

}

static gboolean
parse_spf_ptr (struct worker_task *task, const char *begin, struct spf_record *rec, struct spf_addr *addr)
{
	CHECK_REC (rec);
	
	msg_info ("parse_spf_ptr: ptr parsing is unimplemented");
	return FALSE;
}

static gboolean
parse_spf_mx (struct worker_task *task, const char *begin, struct spf_record *rec, struct spf_addr *addr)
{
	struct spf_dns_cb *cb;
	char *host;
	
	CHECK_REC (rec);
	
	if (begin == NULL) {
		return FALSE;
	}
	if (*begin == ':') {
		begin ++;
	}
	
	host = parse_spf_hostmask (task, begin, addr, rec);
	
	if (!host) {
		return FALSE;
	}

	rec->dns_requests ++;
	cb = memory_pool_alloc (task->task_pool, sizeof (struct spf_dns_cb));
	cb->rec = rec;
	cb->addr = addr;
	cb->cur_action = SPF_RESOLVE_MX;

	if (evdns_resolve_mx (host, DNS_QUERY_NO_SEARCH, spf_record_dns_callback, (void *)cb) == 0) {
		task->save.saved++;
		register_async_event (task->s, (event_finalizer_t) spf_record_dns_callback, NULL, TRUE);
		
		return TRUE;
	}

	return FALSE;
}

static gboolean
parse_spf_all (struct worker_task *task, const char *begin, struct spf_record *rec, struct spf_addr *addr)
{
	/* All is 0/0 */
	addr->addr = 0;
	addr->mask = 0;

	return TRUE;
}

static gboolean
parse_spf_ip4 (struct worker_task *task, const char *begin, struct spf_record *rec, struct spf_addr *addr)
{
	/* ip4:addr[/mask] */

	CHECK_REC (rec);
	return parse_spf_ipmask (begin, addr);
}

static gboolean
parse_spf_include (struct worker_task *task, const char *begin, struct spf_record *rec, struct spf_addr *addr)
{
	struct spf_dns_cb *cb;
	char              *domain;

	CHECK_REC (rec);

	if (begin == NULL || *begin != ':') {
		return FALSE;
	}
	begin ++;
	rec->dns_requests ++;

	cb = memory_pool_alloc (task->task_pool, sizeof (struct spf_dns_cb));
	cb->rec = rec;
	cb->addr = addr;
	cb->cur_action = SPF_RESOLVE_REDIRECT;
	domain = memory_pool_strdup (task->task_pool, begin);

	if (evdns_resolve_txt (domain, DNS_QUERY_NO_SEARCH, spf_record_dns_callback, (void *)cb) == 0) {
		task->save.saved++;
		register_async_event (task->s, (event_finalizer_t) spf_record_dns_callback, NULL, TRUE);
		
		return TRUE;
	}

	return FALSE;
}

static gboolean
parse_spf_exp (struct worker_task *task, const char *begin, struct spf_record *rec, struct spf_addr *addr)
{
	CHECK_REC (rec);

	msg_info ("parse_spf_exp: exp record is ignored");
	return TRUE;
}

static gboolean
parse_spf_redirect (struct worker_task *task, const char *begin, struct spf_record *rec, struct spf_addr *addr)
{
	struct spf_dns_cb *cb;
	char              *domain;

	CHECK_REC (rec);

	if (begin == NULL || *begin != '=') {
		return FALSE;
	}
	begin ++;
	rec->dns_requests ++;

	cb = memory_pool_alloc (task->task_pool, sizeof (struct spf_dns_cb));
	cb->rec = rec;
	cb->addr = addr;
	cb->cur_action = SPF_RESOLVE_INCLUDE;
	domain = memory_pool_strdup (task->task_pool, begin);

	if (evdns_resolve_txt (domain, DNS_QUERY_NO_SEARCH, spf_record_dns_callback, (void *)cb) == 0) {
		task->save.saved++;
		register_async_event (task->s, (event_finalizer_t) spf_record_dns_callback, NULL, TRUE);
		
		return TRUE;
	}

	return FALSE;
}

static gboolean
parse_spf_exists (struct worker_task *task, const char *begin, struct spf_record *rec, struct spf_addr *addr)
{
	struct spf_dns_cb *cb;
	char              *host;

	CHECK_REC (rec);
	
	if (begin == NULL || *begin != ':') {
		return FALSE;
	}
	begin ++;
	rec->dns_requests ++;

	cb = memory_pool_alloc (task->task_pool, sizeof (struct spf_dns_cb));
	cb->rec = rec;
	cb->addr = addr;
	cb->cur_action = SPF_RESOLVE_EXISTS;
	host = memory_pool_strdup (task->task_pool, begin);

	if (evdns_resolve_ipv4 (host, DNS_QUERY_NO_SEARCH, spf_record_dns_callback, (void *)cb) == 0) {
		task->save.saved++;
		register_async_event (task->s, (event_finalizer_t) spf_record_dns_callback, NULL, TRUE);
		
		return TRUE;
	}

	return FALSE;
}

#define NEW_ADDR(x) do {														\
	(x) = memory_pool_alloc (task->task_pool, sizeof (struct spf_addr));		\
	(x)->mech = check_spf_mech (rec->cur_elt, &need_shift);						\
	(x)->spf_string = memory_pool_strdup (task->task_pool, begin);				\
} while (0);

/* Read current element and try to parse record */
static gboolean
parse_spf_record (struct worker_task *task, struct spf_record *rec)
{
	struct spf_addr *new = NULL;
	gboolean need_shift, res = FALSE;
	char *begin;
	
	rec->cur_elt = rec->elts[rec->elt_num];
	if (rec->cur_elt == NULL) {
		return FALSE;
	}
	else {
		/* Check spf mech */
		if (need_shift) {
			rec->cur_elt ++;
		}
		begin = rec->cur_elt;
		if (*begin == '?' || *begin == '+' || *begin == '-' || *begin == '~') {
			begin ++;
		}


		/* Now check what we have */
		switch (*begin) {
			case 'a':
				/* all or a */
				if (strncmp (begin, SPF_ALL, sizeof (SPF_ALL) - 1) == 0) {
					NEW_ADDR (new);
					begin += sizeof (SPF_ALL) - 1;
					res = parse_spf_all (task, begin, rec, new);
				}
				else if (strncmp (begin, SPF_A, sizeof (SPF_A) - 1) == 0) {
					NEW_ADDR (new);
					begin += sizeof (SPF_A) - 1;
					res = parse_spf_a (task, begin, rec, new);
				}
				else {
					msg_info ("parse_spf_record: bad spf command: %s", begin);
				}
				break;
			case 'i':
				/* include or ip4 */
				if (strncmp (begin, SPF_IP4, sizeof (SPF_IP4) - 1) == 0) {
					NEW_ADDR (new);
					begin += sizeof (SPF_IP4) - 1;
					res = parse_spf_ip4 (task, begin, rec, new);
				}
				else if (strncmp (begin, SPF_INCLUDE, sizeof (SPF_INCLUDE) - 1) == 0) {
					begin += sizeof (SPF_INCLUDE) - 1;
					res = parse_spf_include (task, begin, rec, NULL);
				}
				else {
					msg_info ("parse_spf_record: bad spf command: %s", begin);
				}
				break;
			case 'm':
				/* mx */
				if (strncmp (begin, SPF_MX, sizeof (SPF_MX) - 1) == 0) {
					NEW_ADDR (new);
					begin += sizeof (SPF_MX) - 1;
					res = parse_spf_mx (task, begin, rec, new);
				}
				else {
					msg_info ("parse_spf_record: bad spf command: %s", begin);
				}
				break;
			case 'p':
				/* ptr */
				if (strncmp (begin, SPF_PTR, sizeof (SPF_PTR) - 1) == 0) {
					NEW_ADDR (new);
					begin += sizeof (SPF_PTR) - 1;
					res = parse_spf_ptr (task, begin, rec, new);
				}
				else {
					msg_info ("parse_spf_record: bad spf command: %s", begin);
				}
				break;
			case 'e':
				/* exp or exists */
				if (strncmp (begin, SPF_EXP, sizeof (SPF_EXP) - 1) == 0) {
					begin += sizeof (SPF_EXP) - 1;
					res = parse_spf_exp (task, begin, rec, NULL);
				}
				else if (strncmp (begin, SPF_EXISTS, sizeof (SPF_EXISTS) - 1) == 0) {
					NEW_ADDR (new);
					begin += sizeof (SPF_EXISTS) - 1;
					res = parse_spf_exists (task, begin, rec, new);
				}
				else {
					msg_info ("parse_spf_record: bad spf command: %s", begin);
				}
				break;
			case 'r':
				/* redirect */
				if (strncmp (begin, SPF_REDIRECT, sizeof (SPF_REDIRECT) - 1) == 0) {
					begin += sizeof (SPF_REDIRECT) - 1;
					res = parse_spf_redirect (task, begin, rec, NULL);
				}
				else {
					msg_info ("parse_spf_record: bad spf command: %s", begin);
				}
				break;
			default:
				msg_info ("parse_spf_record: bad spf command: %s", begin);
				break;
		}
		if (res) {
			if (new != NULL) {
				rec->addrs = g_list_prepend (rec->addrs, new);
			}
			rec->elt_num ++;
		}
	}

	return res;
}
#undef NEW_ADDR

static void
start_spf_parse (struct spf_record *rec, char *begin)
{
	if (strncmp (begin, SPF_VER_STR, sizeof (SPF_VER_STR) - 1) == 0) {
		begin += sizeof (SPF_VER_STR) - 1;
		while (g_ascii_isspace (*begin) && *begin) {
			begin ++;
		}
		rec->elts = g_strsplit (begin, " ", 0);
		rec->elt_num = 0;
		if (rec->elts) {
			memory_pool_add_destructor (rec->task->task_pool, (pool_destruct_func)g_strfreev, rec->elts);
			rec->cur_elt = rec->elts[0];
			while (parse_spf_record (rec->task, rec));
			if (rec->addrs) {
				rec->addrs = g_list_reverse (rec->addrs);
			}
		}
	}
	else {
		msg_info ("start_spf_parse: bad spf record version: %*s", sizeof (SPF_VER_STR) - 1, begin);
	}
}

static void
spf_dns_callback (int result, char type, int count, int ttl, void *addresses, void *data)
{
	struct spf_record *rec = data;
	char *begin;

	if (result == DNS_ERR_NONE && type == DNS_TXT) {
		if (addresses != NULL) {
			begin = *(char **)addresses;
			start_spf_parse (rec, begin);
		}
	}

	rec->task->save.saved--;
	if (rec->task->save.saved == 0 && rec->callback) {
		rec->callback (rec, rec->task);
	}
	remove_forced_event (rec->task->s, (event_finalizer_t) spf_dns_callback);

}


gboolean
resolve_spf (struct worker_task *task, spf_cb_t callback)
{
	struct spf_record *rec;
	char *domain;
	GList *domains;

	rec = memory_pool_alloc0 (task->task_pool, sizeof (struct spf_record));
	rec->task = task;
	rec->callback = callback;

	if (task->from && (domain = strchr (task->from, '@'))) {
		rec->cur_domain = memory_pool_strdup (task->task_pool, domain + 1);
		if ((domain = strchr (rec->cur_domain, '>')) != NULL) {
			*domain = '\0';
		}

		if (evdns_resolve_txt (rec->cur_domain, DNS_QUERY_NO_SEARCH, spf_dns_callback, (void *)rec) == 0) {
			task->save.saved++;
			register_async_event (task->s, (event_finalizer_t) spf_dns_callback, NULL, TRUE);

			return TRUE;
		}
	}
	else {
		domains = message_get_header (task->task_pool, task->message, "From");

		if (domains != NULL) {
			rec->cur_domain = memory_pool_strdup (task->task_pool, domains->data);
			g_list_free (domains);
			if ((domain = strchr (rec->cur_domain, '@')) == NULL) {
				return FALSE;
			}
			rec->cur_domain = domain + 1;

			if ((domain = strchr (rec->cur_domain, '>')) != NULL) {
				*domain = '\0';
			}
			if (evdns_resolve_txt (rec->cur_domain, DNS_QUERY_NO_SEARCH, spf_dns_callback, (void *)rec) == 0) {
				task->save.saved++;
				register_async_event (task->s, (event_finalizer_t) spf_dns_callback, NULL, TRUE);
	
				return TRUE;
			}
		}
	}

	return FALSE;
}

/* 
 * vi:ts=4 
 */