forked from trusteddomainproject/OpenDMARC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopendmarc_policy.c
1926 lines (1772 loc) · 52.4 KB
/
opendmarc_policy.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
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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************
** The user interface to the rest of this library.
**
** Copyright (c) 2012-2016, The Trusted Domain Project. All rights reserved.
**************************************************************************/
#include "opendmarc_internal.h"
#include "dmarc.h"
/* libbsd if found */
#ifdef USE_BSD_H
# include <bsd/string.h>
#endif /* USE_BSD_H */
/* libstrl if needed */
#ifdef USE_STRL_H
# include <strl.h>
#endif /* USE_STRL_H */
/* opendmarc_strl if needed */
#ifdef USE_DMARCSTRL_H
# include <opendmarc_strl.h>
#endif /* USE_DMARCSTRL_H */
/**************************************************************************
** OPENDMARC_POLICY_LIBRARY_INIT -- Initialize The Library
** Parameters:
** lib_init -- Address of a filled in DMARC_LIB_T structure
** Returns:
** DMARC_PARSE_OKAY -- on success
** DMARC_PARSE_ERROR_NULL_CTX -- if lib_init is null
** DMARC_TLD_ERROR_UNKNOWN -- If lip_init->tld_type is undefined
** Side Effects:
** Sets a global pointer
** Warning:
** This function is not thread safe so only call once on
** startup.
***************************************************************************/
static OPENDMARC_LIB_T *Opendmarc_Libp = NULL;
static OPENDMARC_LIB_T Opendmarc_Lib;
OPENDMARC_STATUS_T
opendmarc_policy_library_init(OPENDMARC_LIB_T *lib_init)
{
int ret = DMARC_PARSE_OKAY;
if (lib_init == NULL)
return DMARC_PARSE_ERROR_NULL_CTX;
(void) memcpy(&Opendmarc_Lib, lib_init, sizeof(OPENDMARC_LIB_T));
Opendmarc_Libp = &Opendmarc_Lib;
errno = 0;
if ((Opendmarc_Libp->tld_source_file)[0] != '\0')
{
switch (Opendmarc_Libp->tld_type)
{
case OPENDMARC_TLD_TYPE_MOZILLA:
ret = opendmarc_tld_read_file(Opendmarc_Libp->tld_source_file,
"//", "*.", "!");
if (ret != 0)
ret = errno;
break;
default:
return DMARC_TLD_ERROR_UNKNOWN;
}
}
return ret;
}
/**************************************************************************
** OPENDMARC_POLICY_LIBRARY_SHUTDOWN -- Shutdown The Libarary
** Parameters:
** lib_init -- The prior DMARC_LIB_T strucgture
** Returns:
** DMARC_PARSE_OKAY -- always
** Side Effects:
** May free memory
** Warning:
***************************************************************************/
OPENDMARC_STATUS_T
opendmarc_policy_library_shutdown(OPENDMARC_LIB_T *lib_init)
{
(void) opendmarc_tld_shutdown();
return DMARC_PARSE_OKAY;
}
/**************************************************************************
** OPENDMARC_POLICY_CONNECT_INIT -- Get policy context for connection
** Parameters:
** ip_addr -- An IP addresss in string form.
** is_ipv6 -- Zero for IPv4, non-zero for IPv6
** Returns:
** pctx -- An allocated and initialized context pointer.
** NULL -- On failure and sets errno
** Side Effects:
** Allocates memory.
***************************************************************************/
DMARC_POLICY_T *
opendmarc_policy_connect_init(u_char *ip_addr, int is_ipv6)
{
DMARC_POLICY_T *pctx;
int xerrno;
if (ip_addr == NULL)
{
errno = EINVAL;
return NULL;
}
pctx = malloc(sizeof(DMARC_POLICY_T));
if (pctx == NULL)
{
return NULL;
}
(void) memset(pctx, '\0', sizeof(DMARC_POLICY_T));
pctx->p = DMARC_RECORD_P_UNSPECIFIED;
pctx->sp = DMARC_RECORD_P_UNSPECIFIED;
pctx->ip_addr = (u_char *)strdup((char *)ip_addr);
if (pctx->ip_addr == NULL)
{
xerrno = errno;
(void) free(pctx);
errno = xerrno;
return NULL;
}
if (is_ipv6 == 0)
pctx->ip_type = DMARC_POLICY_IP_TYPE_IPV4;
else
pctx->ip_type = DMARC_POLICY_IP_TYPE_IPV6;
return pctx;
}
/**************************************************************************
** OPENDMARC_POLICY_CONNECT_CLEAR -- Zero the policy context but doesn't
** free it
**
** Parameters:
** pctx -- The context to zero.
** Returns:
** pctx -- Zeroed but still allocated context
** NULL -- On failure and sets errno
** Side Effects:
** Frees memory.
***************************************************************************/
DMARC_POLICY_T *
opendmarc_policy_connect_clear(DMARC_POLICY_T *pctx)
{
if (pctx == NULL)
{
errno = EINVAL;
return NULL;
}
if (pctx->ip_addr != NULL)
(void) free(pctx->ip_addr);
if (pctx->from_domain != NULL)
(void) free(pctx->from_domain);
if (pctx->spf_domain != NULL)
(void) free(pctx->spf_domain);
if (pctx->dkim_domain != NULL)
(void) free(pctx->dkim_domain);
if (pctx->spf_human_outcome != NULL)
(void) free(pctx->spf_human_outcome);
if (pctx->dkim_human_outcome != NULL)
(void) free(pctx->dkim_human_outcome);
if (pctx->organizational_domain != NULL)
(void) free(pctx->organizational_domain);
pctx->rua_list = opendmarc_util_clearargv(pctx->rua_list);
pctx->rua_cnt = 0;
pctx->ruf_list = opendmarc_util_clearargv(pctx->ruf_list);
pctx->ruf_cnt = 0;
pctx->fo = 0;
(void) memset(pctx, '\0', sizeof(DMARC_POLICY_T));
pctx->p = DMARC_RECORD_P_UNSPECIFIED;
return pctx;
}
/**************************************************************************
** OPENDMARC_POLICY_CONNECT_RSET -- Rset for another message
** Usefull if there is more than a single envelope per connection.
** Usefull during an SMTP RSET
**
** Parameters:
** pctx -- The context to rset.
** Returns:
** pctx -- RSET context
** NULL -- On failure and sets errno
** Side Effects:
** Frees memory.
** Preserves the IP address and type
***************************************************************************/
DMARC_POLICY_T *
opendmarc_policy_connect_rset(DMARC_POLICY_T *pctx)
{
u_char *ip_save;
int ip_type;
if (pctx == NULL)
{
errno = EINVAL;
return NULL;
}
ip_save = pctx->ip_addr;
pctx->ip_addr = NULL;
ip_type = pctx->ip_type;
pctx->ip_type = -1;
pctx = opendmarc_policy_connect_clear(pctx);
if (pctx == NULL)
return NULL;
pctx->ip_addr = ip_save;
pctx->ip_type = ip_type;
return pctx;
}
/**************************************************************************
** OPENDMARC_POLICY_CONNECT_SHUTDOWN -- Free the policy context
** Frees and deallocates the context
**
** Parameters:
** pctx -- The context to free and deallocate.
** Returns:
** NULL -- Always
** Side Effects:
** Frees memory.
***************************************************************************/
DMARC_POLICY_T *
opendmarc_policy_connect_shutdown(DMARC_POLICY_T *pctx)
{
if (pctx != NULL)
{
pctx = opendmarc_policy_connect_clear(pctx);
(void) free(pctx);
pctx = NULL;
}
return pctx;
}
int
opendmarc_policy_check_alignment(u_char *subdomain, u_char *tld, int mode)
{
u_char rev_sub[512];
u_char rev_tld[512];
u_char tld_buf[512];
u_char *ep;
int ret;
if (subdomain == NULL)
return EINVAL;
if (tld == NULL)
return EINVAL;
if (mode== DMARC_RECORD_A_UNSPECIFIED)
mode= DMARC_RECORD_A_RELAXED;
(void) memset(tld_buf, '\0', sizeof tld_buf);
(void) strlcpy(tld_buf, tld, sizeof tld_buf);
(void) memset(rev_sub, '\0', sizeof rev_sub);
(void) opendmarc_reverse_domain(subdomain, rev_sub, sizeof rev_sub);
ep = rev_sub + strlen(rev_sub) -1;
if (*ep != '.')
(void) strlcat((char *)rev_sub, ".", sizeof rev_sub);
(void) memset(rev_tld, '\0', sizeof rev_tld);
(void) opendmarc_reverse_domain(tld_buf, rev_tld, sizeof rev_tld);
ep = rev_tld + strlen(rev_tld) -1;
if (*ep != '.')
(void) strlcat((char *)rev_tld, ".", sizeof rev_tld);
/*
* Perfect match is aligned irrespective of relaxed or strict.
*/
if (strcasecmp(rev_tld, rev_sub) == 0)
return 0;
ret = strncasecmp(rev_tld, rev_sub, strlen(rev_tld));
if (ret == 0 && mode == DMARC_RECORD_A_RELAXED)
return 0;
ret = strncasecmp(rev_sub, rev_tld, strlen(rev_sub));
if (ret == 0 && mode == DMARC_RECORD_A_RELAXED)
return 0;
ret = opendmarc_get_tld(tld, tld_buf, sizeof tld_buf);
if (ret != 0)
return -1;
(void) memset(rev_tld, '\0', sizeof rev_tld);
(void) opendmarc_reverse_domain(tld_buf, rev_tld, sizeof rev_tld);
ep = rev_tld + strlen(rev_tld) -1;
if (*ep != '.')
(void) strlcat((char *)rev_tld, ".", sizeof rev_tld);
/*
* Perfect match is aligned irrespective of relaxed or strict.
*/
if (strcasecmp(rev_tld, rev_sub) == 0)
return 0;
ret = strncasecmp(rev_tld, rev_sub, strlen(rev_tld));
if (ret == 0 && mode == DMARC_RECORD_A_RELAXED)
return 0;
ret = strncasecmp(rev_sub, rev_tld, strlen(rev_sub));
if (ret == 0 && mode == DMARC_RECORD_A_RELAXED)
return 0;
return -1;
}
/**************************************************************************
** OPENDMARC_POLICY_STORE_FROM_DOMAIN -- Store domain from the From: header.
** If the domain is an address parse the domain from it.
** The domain is needed to perform alignment checks.
** Parameters:
** pctx -- The context to uptdate
** from_domain -- A string
** Returns:
** DMARC_PARSE_OKAY -- On success
** DMARC_PARSE_ERROR_NULL_CTX -- If pctx was NULL
** DMARC_PARSE_ERROR_EMPTY -- if from_domain NULL or zero
** DMARC_PARSE_ERROR_NO_DOMAIN -- No domain in from_domain
** Side Effects:
** Allocates memory.
** Note:
** Does not check to insure that the found domain is a
** syntactically valid domain. It is okay for domain to
** puney decoded into 8-bit data.
***************************************************************************/
OPENDMARC_STATUS_T
opendmarc_policy_store_from_domain(DMARC_POLICY_T *pctx, u_char *from_domain)
{
char domain_buf[256];
char *dp;
if (pctx == NULL)
return DMARC_PARSE_ERROR_NULL_CTX;
if (from_domain == NULL || strlen((char *)from_domain) == 0)
return DMARC_PARSE_ERROR_EMPTY;
dp = opendmarc_util_finddomain(from_domain, domain_buf, sizeof domain_buf);
if (dp == NULL)
return DMARC_PARSE_ERROR_NO_DOMAIN;
pctx->from_domain = strdup((char *)dp);
if (pctx->from_domain == NULL)
return DMARC_PARSE_ERROR_NO_ALLOC;
return DMARC_PARSE_OKAY;
}
/**************************************************************************
** OPENDMARC_POLICY_STORE_SPF -- Store spf results
** Okay to supply the raw MAIL From: data
**
** Parameters:
** pctx -- The context to uptdate
** domain -- The domain used to verify SPF
** result -- DMARC_POLICY_SPF_OUTCOME_NONE
** or DMARC_POLICY_SPF_OUTCOME_PASS
** or DMARC_POLICY_SPF_OUTCOME_FAIL
** or DMARC_POLICY_SPF_OUTCOME_TMPFAIL
** origin -- DMARC_POLICY_SPF_ORIGIN_MAILFROM
** or DMARC_POLICY_SPF_ORIGIN_HELO
** human_readable -- A human readable reason for failure
** Returns:
** DMARC_PARSE_OKAY -- On success
** DMARC_PARSE_ERROR_NULL_CTX -- If pctx was NULL
** DMARC_PARSE_ERROR_EMPTY -- if domain NULL or zero
** DMARC_PARSE_ERROR_NO_DOMAIN -- No domain in domain
** Side Effects:
** Allocates memory.
** Note:
** Does not check to insure that the domain is a
** syntactically valid domain. It is okay for domain to
** puney decoded into 8-bit data.
***************************************************************************/
OPENDMARC_STATUS_T
opendmarc_policy_store_spf(DMARC_POLICY_T *pctx, u_char *domain, int result, int origin, u_char *human_readable)
{
char domain_buf[256];
char *dp;
if (pctx == NULL)
return DMARC_PARSE_ERROR_NULL_CTX;
if (domain == NULL || strlen((char *)domain) == 0)
return DMARC_PARSE_ERROR_EMPTY;
dp = opendmarc_util_finddomain(domain, domain_buf, sizeof domain_buf);
if (dp == NULL)
return DMARC_PARSE_ERROR_NO_DOMAIN;
if (human_readable != NULL)
pctx->spf_human_outcome = strdup((char *)human_readable);
pctx->spf_domain = strdup((char *)dp);
if (pctx->spf_domain == NULL)
return DMARC_PARSE_ERROR_NO_ALLOC;
switch (result)
{
case DMARC_POLICY_SPF_OUTCOME_NONE:
case DMARC_POLICY_SPF_OUTCOME_PASS:
case DMARC_POLICY_SPF_OUTCOME_FAIL:
case DMARC_POLICY_SPF_OUTCOME_TMPFAIL:
pctx->spf_outcome = result;
break;
default:
return DMARC_PARSE_ERROR_BAD_SPF_MACRO;
}
switch (origin)
{
case DMARC_POLICY_SPF_ORIGIN_MAILFROM:
case DMARC_POLICY_SPF_ORIGIN_HELO:
pctx->spf_origin = origin;
break;
default:
return DMARC_PARSE_ERROR_BAD_SPF_MACRO;
}
return DMARC_PARSE_OKAY;
}
/**************************************************************************
** OPENDMARC_POLICY_STORE_DKIM -- Store dkim results
**
** Parameters:
** pctx -- The context to uptdate
** d_equal_domain -- The the domain from the p=
** dkim_result -- DMARC_POLICY_DKIM_OUTCOME_NONE
** or DMARC_POLICY_DKIM_OUTCOME_PASS
** or DMARC_POLICY_DKIM_OUTCOME_FAIL
** or DMARC_POLICY_DKIM_OUTCOME_TMPFAIL
** human_result -- A human readable reason for failure
** Returns:
** DMARC_PARSE_OKAY -- On success
** DMARC_PARSE_ERROR_NULL_CTX -- If pctx was NULL
** DMARC_PARSE_ERROR_EMPTY -- if domain NULL or zero
** DMARC_PARSE_ERROR_NO_DOMAIN -- No domain in domain
** DMARC_PARSE_ERROR_NO_ALLOC -- Memory allocation failed
** Side Effects:
** Allocates memory.
** Note:
** Does not check to insure that the domain is a
** syntactically valid domain. It is okay for domain to
** puney decoded into 8-bit data.
***************************************************************************/
OPENDMARC_STATUS_T
opendmarc_policy_store_dkim(DMARC_POLICY_T *pctx, u_char *d_equal_domain, int dkim_result, u_char *human_result)
{
char domain_buf[256];
u_char *dp;
int result = DMARC_POLICY_DKIM_OUTCOME_NONE;
if (pctx == NULL)
return DMARC_PARSE_ERROR_NULL_CTX;
if (d_equal_domain == NULL || strlen((char *)d_equal_domain) == 0)
return DMARC_PARSE_ERROR_EMPTY;
switch (dkim_result)
{
case DMARC_POLICY_DKIM_OUTCOME_NONE:
case DMARC_POLICY_DKIM_OUTCOME_PASS:
case DMARC_POLICY_DKIM_OUTCOME_FAIL:
case DMARC_POLICY_DKIM_OUTCOME_TMPFAIL:
result = dkim_result;
break;
default:
return DMARC_PARSE_ERROR_BAD_DKIM_MACRO;
}
if (pctx->dkim_final == TRUE)
return DMARC_PARSE_OKAY;
dp = opendmarc_util_finddomain(d_equal_domain, domain_buf, sizeof domain_buf);
if (dp == NULL || strlen(dp) == 0)
return DMARC_PARSE_ERROR_NO_DOMAIN;
/*
* If the d= domain is an exact match to the from_domain
* select this one as the domain of choice.
* If the outcome is pass, the is the final choice.
*/
if (strcasecmp((char *)dp, pctx->from_domain) == 0)
{
if (pctx->dkim_domain != NULL)
{
(void) free(pctx->dkim_domain);
pctx->dkim_domain = NULL;
}
if (result == DMARC_POLICY_DKIM_OUTCOME_PASS)
{
pctx->dkim_final = TRUE;
goto set_final;
}
if (pctx->dkim_outcome == DMARC_POLICY_DKIM_OUTCOME_PASS)
return DMARC_PARSE_OKAY;
goto set_final;
}
/*
* See if the d= is a superset of the from domain.
* If so and if we have not already found
* a best match, make this the temporary best match.
*/
if (opendmarc_policy_check_alignment(dp, pctx->from_domain,
pctx->adkim) == 0)
{
if (pctx->dkim_domain != NULL)
{
(void) free(pctx->dkim_domain);
pctx->dkim_domain = NULL;
}
if (result == DMARC_POLICY_DKIM_OUTCOME_PASS)
goto set_final;
}
/*
* If we found any record so far that passed.
* preserve it.
*/
if (pctx->dkim_outcome == DMARC_POLICY_DKIM_OUTCOME_PASS)
return DMARC_PARSE_OKAY;
set_final:
if (pctx->dkim_domain == NULL)
pctx->dkim_domain = strdup((char *)dp);
if (pctx->dkim_domain == NULL)
return DMARC_PARSE_ERROR_NO_ALLOC;
if (human_result != NULL)
{
if (pctx->dkim_human_outcome != NULL)
(void) free(pctx->dkim_human_outcome);
pctx->dkim_human_outcome = strdup((char *)human_result);
}
pctx->dkim_outcome = result;
return DMARC_PARSE_OKAY;
}
/**************************************************************************
** OPENDMARC_POLICY_QUERY_DMARC_XDOMAIN -- Verify that we have permission
** to send to domain
** Parameters:
** pctx -- The context to uptdate
** uri -- URI listed in DMARC record
** Returns:
** DMARC_PARSE_OKAY -- On success, and fills pctx
** DMARC_PARSE_ERROR_NULL_CTX -- If pctx was NULL
** DMARC_PARSE_ERROR_EMPTY -- if domain NULL or zero
** DMARC_PARSE_ERROR_NO_DOMAIN -- No domain in domain
** DMARC_DNS_ERROR_TMPERR -- No domain, try again later
** DMARC_DNS_ERROR_NO_RECORD -- No DMARC record found.
** Side Effects:
** Performs one or more DNS lookups
**
***************************************************************************/
OPENDMARC_STATUS_T
opendmarc_policy_query_dmarc_xdomain(DMARC_POLICY_T *pctx, u_char *uri)
{
u_char buf[BUFSIZ];
u_char copy[256];
u_char domain[256];
u_char domain_tld[256];
u_char uri_tld[256];
u_char *ret = NULL;
int dns_reply = 0;
int i = 0;
int err = 0;
if (pctx == NULL || pctx->from_domain == NULL)
return DMARC_PARSE_ERROR_NULL_CTX;
if (uri == NULL)
return DMARC_PARSE_ERROR_EMPTY;
memset(buf, '\0', sizeof buf);
memset(copy, '\0', sizeof copy);
memset(domain, '\0', sizeof domain);
memset(domain_tld, '\0', sizeof domain_tld);
memset(uri_tld, '\0', sizeof uri_tld);
/* Get out domain from our URI */
if (strncasecmp(uri, "mailto:", 7) == 0)
uri += 7;
if (opendmarc_util_finddomain(uri, domain, sizeof domain) == NULL)
return DMARC_PARSE_ERROR_NO_DOMAIN;
/* Ensure that we're not doing a cross-domain check */
err = 0;
err = opendmarc_get_tld(domain, uri_tld, sizeof uri_tld);
err += opendmarc_get_tld(pctx->from_domain, domain_tld, sizeof domain_tld);
if (err != 0)
return DMARC_DNS_ERROR_NO_RECORD;
if (strncasecmp((char *) uri_tld, (char *) domain_tld,
sizeof uri_tld) == 0)
return DMARC_PARSE_OKAY;
strlcpy((char *) copy, (char *) pctx->from_domain, sizeof copy);
strlcat((char *) copy, "._report._dmarc.", sizeof copy);
strlcat((char *) copy, (char *) domain, sizeof copy);
/* Query DNS */
for (i = 0; i < DNS_MAX_RETRIES && ret == NULL; i++)
{
ret = (u_char *) dmarc_dns_get_record((char *) copy, &dns_reply,
(char *) buf, sizeof buf);
if (ret != 0 || dns_reply == HOST_NOT_FOUND)
break;
/* requery if didn't resolve CNAME */
if (ret == NULL && *buf != '\0')
{
strlcpy((char *) copy, (char *) buf, sizeof copy);
continue;
}
}
if (dns_reply == NETDB_SUCCESS && buf != NULL)
{
/* Must include DMARC version */
if (strncasecmp((char *)buf, "v=DMARC1", sizeof buf) == 0)
{
return DMARC_PARSE_OKAY;
}
}
/*
** Retry with a * literal.
*/
strlcpy((char *) copy, (char *) "*", sizeof copy);
strlcat((char *) copy, "._report._dmarc.", sizeof copy);
strlcat((char *) copy, (char *) domain, sizeof copy);
for (i = 0; i < DNS_MAX_RETRIES && ret == NULL; i++)
{
ret = (u_char *) dmarc_dns_get_record((char *) copy, &dns_reply,
(char *) buf, sizeof buf);
if (ret != 0 || dns_reply == HOST_NOT_FOUND)
break;
/* requery if didn't resolve CNAME */
if (ret == NULL && *buf != '\0')
{
strlcpy((char *) copy, (char *) buf, sizeof copy);
continue;
}
}
if (dns_reply == NETDB_SUCCESS && buf != NULL)
{
/* Must include DMARC version */
if (strncasecmp((char *)buf, "v=DMARC1", sizeof buf) == 0)
{
return DMARC_PARSE_OKAY;
}
else
{
return DMARC_DNS_ERROR_NO_RECORD;
}
}
switch (dns_reply)
{
case HOST_NOT_FOUND:
case NO_DATA:
case NO_RECOVERY:
return DMARC_DNS_ERROR_NO_RECORD;
case TRY_AGAIN:
case NETDB_INTERNAL:
return DMARC_DNS_ERROR_TMPERR;
default:
return DMARC_DNS_ERROR_NO_RECORD;
}
}
/**************************************************************************
** OPENDMARC_POLICY_QUERY_DMARC -- Look up the _dmarc record for the
** specified domain. If not found
** try the organizational domain.
** Parameters:
** pctx -- The context to uptdate
** domain -- The domain for which to lookup the DMARC record
** Returns:
** DMARC_PARSE_OKAY -- On success, and fills pctx
** DMARC_PARSE_ERROR_NULL_CTX -- If pctx was NULL
** DMARC_PARSE_ERROR_EMPTY -- if domain NULL or zero
** DMARC_PARSE_ERROR_NO_DOMAIN -- No domain in domain
** DMARC_DNS_ERROR_NXDOMAIN -- No domain found in DNS
** DMARC_DNS_ERROR_TMPERR -- No domain, try again later
** DMARC_DNS_ERROR_NO_RECORD -- No DMARC record found.
** Side Effects:
** Performs one or more DNS lookups
** Allocates memory.
** Note:
** Does not check to insure that the domain is a
** syntactically valid domain.
** Looks up domain first. If that fails, finds the tld and
** looks up topmost domain under tld. If this later is found
** updates pctx->organizational_domain with the result.
** Warning:
** If no TLD file has been loaded, will silenty not do that
** fallback lookup.
**
***************************************************************************/
OPENDMARC_STATUS_T
opendmarc_policy_query_dmarc(DMARC_POLICY_T *pctx, u_char *domain)
{
u_char buf[BUFSIZ];
u_char copy[256];
u_char tld[256];
u_char * bp = NULL;
int dns_reply = 0;
int tld_reply = 0;
int loop_count = DNS_MAX_RETRIES;
if (pctx == NULL)
return DMARC_PARSE_ERROR_NULL_CTX;
if (domain == NULL || strlen(domain) == 0)
{
if (pctx->from_domain != NULL)
domain = pctx->from_domain;
else
return DMARC_PARSE_ERROR_EMPTY;
}
(void) strlcpy(copy, "_dmarc.", sizeof copy);
(void) strlcat(copy, domain, sizeof copy);
query_again:
(void) memset(buf, '\0', sizeof buf);
bp = dmarc_dns_get_record(copy, &dns_reply, buf, sizeof buf);
if (bp != NULL)
{
if (dns_reply != HOST_NOT_FOUND)
goto got_record;
}
/*
* Was a CNAME was found that the resolver did
* not follow on its own?
*/
if (bp == NULL && *buf != '\0')
{
(void) strlcpy(copy, buf, sizeof copy);
if (--loop_count != 0)
goto query_again;
}
(void) memset(tld, '\0', sizeof tld);
tld_reply = opendmarc_get_tld(domain, tld, sizeof tld);
if (tld_reply != 0)
goto dns_failed;
if (strlen(tld) > 0)
{
pctx->organizational_domain = strdup(tld);
loop_count = DNS_MAX_RETRIES;
(void) strlcpy(copy, "_dmarc.", sizeof copy);
(void) strlcat(copy, tld, sizeof copy);
query_again2:
(void) memset(buf, '\0', sizeof buf);
bp = dmarc_dns_get_record(copy, &dns_reply, buf, sizeof buf);
if (bp != NULL)
goto got_record;
/*
* Was a CNAME was found that the resolver did
* not follow on its own?
*/
if (bp == NULL && *buf != '\0')
{
(void) strlcpy(copy, buf, sizeof copy);
if (--loop_count != 0)
goto query_again2;
}
}
dns_failed:
switch (dns_reply)
{
case HOST_NOT_FOUND:
case NO_DATA:
case NO_RECOVERY:
return DMARC_DNS_ERROR_NO_RECORD;
case TRY_AGAIN:
case NETDB_INTERNAL:
return DMARC_DNS_ERROR_TMPERR;
default:
return DMARC_DNS_ERROR_NO_RECORD;
}
got_record:
return opendmarc_policy_parse_dmarc(pctx, domain, buf);
}
/**************************************************************************
** OPENDMARC_GET_POLICY_TO_ENFORCE -- What to do with this message. i.e. allow
** possible delivery, quarantine, or reject.
** Parameters:
** pctx -- A Policy context
** Returns:
** DMARC_PARSE_ERROR_NULL_CTX -- pctx == NULL
** DMARC_POLICY_ABSENT -- No DMARC record found
** DMARC_FROM_DOMAIN_ABSENT -- No From: domain
** DMARC_POLICY_NONE -- Accept if other policy allows
** DMARC_POLICY_REJECT -- Policy advises to reject the message
** DMARC_POLICY_QUARANTINE -- Policy advises to quarantine the message
** DMARC_POLICY_PASS -- Policy advises to accept the message
** Side Effects:
** Checks for domain alignment.
***************************************************************************/
OPENDMARC_STATUS_T
opendmarc_get_policy_to_enforce(DMARC_POLICY_T *pctx)
{
if (pctx == NULL)
return DMARC_PARSE_ERROR_NULL_CTX;
if (pctx->p == DMARC_RECORD_P_UNSPECIFIED)
return DMARC_POLICY_ABSENT;
if (pctx->from_domain == NULL)
return DMARC_FROM_DOMAIN_ABSENT;
pctx->dkim_alignment = DMARC_POLICY_DKIM_ALIGNMENT_FAIL;
pctx->spf_alignment = DMARC_POLICY_SPF_ALIGNMENT_FAIL;
/* check for DKIM alignment */
if (pctx->dkim_domain != NULL && pctx->dkim_outcome == DMARC_POLICY_DKIM_OUTCOME_PASS)
{
if (opendmarc_policy_check_alignment(pctx->from_domain, pctx->dkim_domain, pctx->adkim) == 0)
pctx->dkim_alignment = DMARC_POLICY_DKIM_ALIGNMENT_PASS;
}
/* check for SPF alignment */
if (pctx->spf_domain != NULL && pctx->spf_outcome == DMARC_POLICY_SPF_OUTCOME_PASS)
{
if (opendmarc_policy_check_alignment(pctx->from_domain, pctx->spf_domain, pctx->aspf) == 0)
pctx->spf_alignment = DMARC_POLICY_SPF_ALIGNMENT_PASS;
}
/*
* If dkim passes and dkim aligns OR spf passes and spf aligns
* Accept the message.
*/
if (pctx->spf_alignment == DMARC_POLICY_SPF_ALIGNMENT_PASS ||
pctx->dkim_alignment == DMARC_POLICY_DKIM_ALIGNMENT_PASS)
return DMARC_POLICY_PASS;
if (pctx->organizational_domain != NULL)
{
switch (pctx->sp)
{
case DMARC_RECORD_P_REJECT:
return DMARC_POLICY_REJECT;
case DMARC_RECORD_P_QUARANTINE:
return DMARC_POLICY_QUARANTINE;
case DMARC_RECORD_P_NONE:
return DMARC_POLICY_NONE;
}
}
switch (pctx->p)
{
case DMARC_RECORD_P_REJECT:
return DMARC_POLICY_REJECT;
case DMARC_RECORD_P_QUARANTINE:
return DMARC_POLICY_QUARANTINE;
case DMARC_RECORD_P_NONE:
return DMARC_POLICY_NONE;
default:
/* XXX -- shouldn't be possible */
return DMARC_POLICY_PASS;
}
}
/*******************************************************************************
** OPENDMARC_PARSE_DMARC -- Parse a DMARC record
**
** Parameters:
** pctx -- A Policy context
** domain -- The domain looked up to get this DMARC record
** record -- The DMARC record to parse
** Returns:
** DMARC_PARSE_ERROR_EMPTY -- if any argument is NULL
** DMARC_PARSE_ERROR_BAD_VERSION -- if v= was bad
** DMARC_PARSE_ERROR_BAD_VALUE -- if value following = was bad
** DMARC_PARSE_ERROR_NO_REQUIRED_P -- if p= was absent
** DMARC_PARSE_OKAY -- On Success
** Side Effects:
** Allocates memory.
*********************************************************************************/
OPENDMARC_STATUS_T
opendmarc_policy_parse_dmarc(DMARC_POLICY_T *pctx, u_char *domain, u_char *record)
{
u_char *cp, *eqp, *ep, *sp, *vp;
u_char copy[BUFSIZ];
u_char cbuf[512];
u_char vbuf[512];
if (pctx == NULL || domain == NULL || record == NULL || strlen((char *)record) == 0)
{
return DMARC_PARSE_ERROR_EMPTY;
}
/*
* Set the defaults to detect missing required items.
*/
pctx->pct = -1;
pctx->ri = -1;
(void) memset((char *)copy, '\0', sizeof copy);
(void) strlcpy((char *)copy, (char *)record, sizeof copy);
ep = copy + strlen((char *)copy);
for (cp = copy; cp != NULL && cp <= ep; )
{
sp = (u_char *)strchr(cp, ';');
if (sp != NULL)
*sp++ = '\0';
eqp = (u_char *)strchr((char *)cp, '=');
if (eqp == NULL)
{
cp = sp;
continue;
}
*eqp = '\0';
vp = eqp + 1;
cp = opendmarc_util_cleanup(cp, cbuf, sizeof cbuf);
if (cp == NULL || strlen((char *)cp) == 0)
{
cp = sp;
continue;
}
vp = opendmarc_util_cleanup(vp, vbuf, sizeof vbuf);
if (vp == NULL || strlen((char *)vp) == 0)
{
cp = sp;
continue;
}
/*
* cp nwo points to the token, and
* vp now points to the token's value
* both with all surronding whitepace removed.
*/
if (strcasecmp((char *)cp, "v") == 0)
{
/*
* Yes, this is required to be first, but why
* reject it if it is not first?
*/
if (strcasecmp((char *)vp, "DMARC1") != 0)
{
return DMARC_PARSE_ERROR_BAD_VERSION;
}
}
else if (strcasecmp((char *)cp, "p") == 0)
{
/*
* Be generous. Accept, for example, "p=r, p=rej, or any
* left match of "reject".
*/
if (strncasecmp((char *)vp, "reject", strlen((char *)vp)) == 0)
pctx->p = DMARC_RECORD_P_REJECT;
else if (strncasecmp((char *)vp, "none", strlen((char *)vp)) == 0)
pctx->p = DMARC_RECORD_P_NONE;
else if (strncasecmp((char *)vp, "quarantine", strlen((char *)vp)) == 0)
pctx->p = DMARC_RECORD_P_QUARANTINE;
else
{
/* A totaly unknown value */
return DMARC_PARSE_ERROR_BAD_VALUE;
}
}
else if (strcasecmp((char *)cp, "sp") == 0)
{
/*
* Be generous. Accept, for example, "sp=r, p=rej, or any
* left match of "reject".
*/
if (strncasecmp((char *)vp, "reject", strlen((char *)vp)) == 0)
pctx->sp = DMARC_RECORD_P_REJECT;
else if (strncasecmp((char *)vp, "none", strlen((char *)vp)) == 0)
pctx->sp = DMARC_RECORD_P_NONE;
else if (strncasecmp((char *)vp, "quarantine", strlen((char *)vp)) == 0)
pctx->sp = DMARC_RECORD_P_QUARANTINE;
else
{
/* A totaly unknown value */
return DMARC_PARSE_ERROR_BAD_VALUE;
}
}
else if (strcasecmp((char *)cp, "adkim") == 0)
{
/*
* Be generous. Accept, for example, "adkim=s, adkim=strict or any
* left match of "strict".
*/
if (strncasecmp((char *)vp, "strict", strlen((char *)vp)) == 0)
pctx->adkim = DMARC_RECORD_A_STRICT;
else if (strncasecmp((char *)vp, "relaxed", strlen((char *)vp)) == 0)
pctx->adkim = DMARC_RECORD_A_RELAXED;
else
{
/* A totaly unknown value */
return DMARC_PARSE_ERROR_BAD_VALUE;
}
}
else if (strcasecmp((char *)cp, "aspf") == 0)