-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathacl.c
2766 lines (2417 loc) · 60.5 KB
/
acl.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
/* $Id: acl.c,v 1.93 2009/08/14 00:09:02 manu Exp $ */
/*
* Copyright (c) 2004-2007 Emmanuel Dreyfus
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Emmanuel Dreyfus
*
* THIS SOFTWARE IS PROVIDED ``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 THE AUTHOR 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"
#ifdef HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#ifdef __RCSID
__RCSID("$Id: acl.c,v 1.93 2009/08/14 00:09:02 manu Exp $");
#endif
#endif
#ifdef HAVE_OLD_QUEUE_H
#include "queue.h"
#else
#include <sys/queue.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <syslog.h>
#include <pthread.h>
#include <sysexits.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <regex.h>
#include "spf.h"
#include "acl.h"
#include "conf.h"
#include "sync.h"
#include "list.h"
#ifdef USE_DNSRBL
#include "dnsrbl.h"
#endif
#ifdef USE_CURL
#include "urlcheck.h"
#endif
#ifdef USE_LDAP
#include "ldapcheck.h"
#endif
#if defined(USE_CURL) || defined(USE_LDAP)
#include "prop.h"
#endif
#ifdef USE_GEOIP
#include "geoip.h"
#endif
#ifdef USE_P0F
#include "p0f.h"
#endif
#if (defined(HAVE_SPF) || defined(HAVE_SPF_ALT) || \
defined(HAVE_SPF2_10) || defined(HAVE_SPF2))
#include "spf.h"
#endif
#ifdef USE_DKIM
#include "dkimcheck.h"
#endif
#ifdef USE_SPAMD
#include "spamd.h"
#endif
#include "macro.h"
#include "clock.h"
#include "milter-greylist.h"
#ifdef USE_DMALLOC
#include <dmalloc.h>
#endif
struct acllist acl_head;
pthread_rwlock_t acl_lock;
static struct acl_entry *gacl;
int gneg;
char *acl_print_netblock(acl_data_t *, char *, size_t);
char *acl_print_string(acl_data_t *, char *, size_t);
char *acl_print_regex(acl_data_t *, char *, size_t);
char *acl_print_list(acl_data_t *, char *, size_t);
char *acl_print_null(acl_data_t *, char *, size_t);
char *acl_print_opnum(acl_data_t *, char *, size_t);
char *acl_print_time(acl_data_t *, char *, size_t);
int acl_opnum_cmp(int, enum operator, int);
void acl_free_entry(struct acl_entry *);
void acl_free_netblock(acl_data_t *);
void acl_free_string(acl_data_t *);
void acl_free_regex(acl_data_t *);
void acl_add_netblock(acl_data_t *, void *);
void acl_add_string(acl_data_t *, void *);
void acl_add_regex(acl_data_t *, void *);
void acl_add_body_string(acl_data_t *, void *);
void acl_add_body_regex(acl_data_t *, void *);
void acl_add_macro(acl_data_t *, void *);
void acl_add_opnum(acl_data_t *, void *);
void acl_add_opnum_body(acl_data_t *, void *);
void acl_add_time(acl_data_t *, void *);
void acl_add_list(acl_data_t *, void *);
char *acl_print_macro(acl_data_t *, char *, size_t);
#ifdef USE_DNSRBL
void acl_add_dnsrbl(acl_data_t *, void *);
char *acl_print_dnsrbl(acl_data_t *, char *, size_t);
#endif
#ifdef USE_CURL
void acl_add_urlcheck(acl_data_t *, void *);
char *acl_print_urlcheck(acl_data_t *, char *, size_t);
#endif
#ifdef USE_LDAP
void acl_add_ldapcheck(acl_data_t *, void *);
char *acl_print_ldapcheck(acl_data_t *, char *, size_t);
#endif
#if defined(USE_CURL) || defined(USE_LDAP)
void acl_add_prop_string(acl_data_t *, void *);
void acl_add_prop_regex(acl_data_t *, void *);
char *acl_print_prop_string(acl_data_t *, char *, size_t);
char *acl_print_prop_regex(acl_data_t *, char *, size_t);
void acl_free_prop_string(acl_data_t *);
void acl_free_prop_regex(acl_data_t *);
#endif
struct acl_clause_rec acl_clause_rec[] = {
/* Temporary types for lists */
{ AC_LIST, MULTIPLE_OK, AS_NONE, "list",
AT_LIST, AC_NONE, AC_NONE, EXF_NONE,
acl_print_list, acl_add_list,
NULL, acl_list_filter },
{ AC_EMAIL, MULTIPLE_OK, AS_NONE, "email",
AT_NONE, AC_NONE, AC_NONE, EXF_NONE,
acl_print_string, acl_add_string,
acl_free_string, NULL },
{ AC_REGEX, MULTIPLE_OK, AS_NONE, "regex",
AT_NONE, AC_NONE, AC_NONE, EXF_NONE,
acl_print_regex, acl_add_regex,
acl_free_regex, NULL },
{ AC_STRING, MULTIPLE_OK, AS_NONE, "string",
AT_NONE, AC_NONE, AC_NONE, EXF_NONE,
acl_print_string, acl_add_string,
acl_free_string, NULL },
/* Real types used in clauses */
{ AC_NETBLOCK, UNIQUE, AS_ANY, "net",
AT_NETBLOCK, AC_NETBLOCK_LIST, AC_NETBLOCK, EXF_ADDR,
acl_print_netblock, acl_add_netblock,
acl_free_netblock, acl_netblock_filter },
{ AC_NETBLOCK_LIST, UNIQUE, AS_ANY, "net_list",
AT_LIST, AC_NONE, AC_NONE, EXF_ADDR,
acl_print_list, acl_add_list,
NULL, acl_list_filter },
{ AC_DOMAIN, UNIQUE, AS_ANY, "domain",
AT_STRING, AC_DOMAIN_LIST, AC_DOMAIN, EXF_DOMAIN,
acl_print_string, acl_add_string,
acl_free_string, acl_domain_cmp },
{ AC_DOMAIN_RE, UNIQUE, AS_ANY, "domain_re",
AT_REGEX, AC_DOMAIN_LIST, AC_REGEX, EXF_DOMAIN,
acl_print_regex, acl_add_regex,
acl_free_regex, acl_domain_regexec },
{ AC_DOMAIN_LIST, UNIQUE, AS_ANY, "domain_list",
AT_LIST, AC_NONE, AC_NONE, EXF_DOMAIN,
acl_print_list, acl_add_list,
NULL, acl_list_filter },
{ AC_HELO, UNIQUE, AS_RCPT, "helo",
AT_STRING, AC_HELO_LIST, AC_STRING, EXF_HELO,
acl_print_string, acl_add_string,
acl_free_string, acl_helo_strstr },
{ AC_HELO_RE, UNIQUE, AS_RCPT, "helo_re",
AT_REGEX, AC_HELO_LIST, AC_REGEX, EXF_HELO,
acl_print_regex, acl_add_regex,
acl_free_regex, acl_helo_regexec },
{ AC_HELO_LIST, UNIQUE, AS_RCPT, "helo_list",
AT_LIST, AC_NONE, AC_NONE, EXF_HELO,
acl_print_list, acl_add_list,
NULL, acl_list_filter },
{ AC_FROM, UNIQUE, AS_ANY, "from",
AT_STRING, AC_FROM_LIST, AC_EMAIL, EXF_FROM,
acl_print_string, acl_add_string,
acl_free_string, acl_from_cmp },
{ AC_FROM_RE, UNIQUE, AS_ANY, "from_re",
AT_REGEX, AC_FROM_LIST, AC_REGEX, EXF_FROM,
acl_print_regex, acl_add_regex,
acl_free_regex, acl_from_regexec },
{ AC_FROM_LIST, UNIQUE, AS_ANY, "from_list",
AT_LIST, AC_NONE, AC_NONE, EXF_FROM,
acl_print_list, acl_add_list,
NULL, acl_list_filter },
{ AC_RCPT, MULTIPLE_OK, AS_ANY, "rcpt",
AT_STRING, AC_RCPT_LIST, AC_EMAIL, EXF_RCPT,
acl_print_string, acl_add_string,
acl_free_string, acl_rcpt_cmp },
{ AC_RCPT_RE, MULTIPLE_OK, AS_ANY, "rcpt_re",
AT_REGEX, AC_RCPT_LIST, AC_REGEX, EXF_RCPT,
acl_print_regex, acl_add_regex,
acl_free_regex, acl_rcpt_regexec },
{ AC_RCPT_LIST, MULTIPLE_OK, AS_ANY, "rcpt_list",
AT_LIST, AC_NONE, AC_NONE, EXF_RCPT,
acl_print_list, acl_add_list,
NULL, acl_list_filter },
{ AC_BODY, MULTIPLE_OK, AS_DATA, "body",
AT_STRING, AC_BODY_LIST, AC_STRING, EXF_BODY,
acl_print_string, acl_add_body_string,
acl_free_string, acl_body_strstr },
{ AC_BODY_RE, MULTIPLE_OK, AS_DATA, "body_re",
AT_REGEX, AC_BODY_LIST, AC_REGEX, EXF_BODY,
acl_print_regex, acl_add_body_regex,
acl_free_regex, acl_body_regexec },
{ AC_BODY_LIST, MULTIPLE_OK, AS_DATA, "body_list",
AT_LIST, AC_NONE, AC_NONE, EXF_BODY,
acl_print_list, acl_add_list,
NULL, acl_list_filter },
{ AC_HEADER, MULTIPLE_OK, AS_DATA, "header",
AT_STRING, AC_HEADER_LIST, AC_STRING, EXF_HEADER,
acl_print_string, acl_add_body_string,
acl_free_string, acl_header_strstr },
{ AC_HEADER_RE, MULTIPLE_OK, AS_DATA, "header_re",
AT_REGEX, AC_HEADER_LIST, AC_REGEX, EXF_HEADER,
acl_print_regex, acl_add_body_regex,
acl_free_regex, acl_header_regexec },
{ AC_HEADER_LIST, MULTIPLE_OK, AS_DATA, "header_list",
AT_LIST, AC_NONE, AC_NONE, EXF_HEADER,
acl_print_list, acl_add_list,
NULL, acl_list_filter },
{ AC_MACRO, MULTIPLE_OK, AS_ANY, "macro",
AT_MACRO, AC_MACRO_LIST, AC_STRING, EXF_MACRO,
acl_print_macro, acl_add_macro,
NULL, macro_check },
{ AC_MACRO_LIST, MULTIPLE_OK, AS_ANY, "macro_list",
AT_LIST, AC_NONE, AC_NONE, EXF_MACRO,
acl_print_list, acl_add_list,
NULL, acl_list_filter },
#ifdef USE_DNSRBL
{ AC_DNSRBL, MULTIPLE_OK, AS_ANY, "dnsrbl",
AT_DNSRBL, AC_DNSRBL_LIST, AC_STRING, EXF_DNSRBL,
acl_print_dnsrbl, acl_add_dnsrbl,
NULL, dnsrbl_check_source },
{ AC_DNSRBL_LIST, MULTIPLE_OK, AS_ANY, "dnsrbl_list",
AT_LIST, AC_NONE, AC_NONE, EXF_DNSRBL,
acl_print_list, acl_add_list,
NULL, acl_list_filter },
#endif
#ifdef USE_CURL
{ AC_URLCHECK, MULTIPLE_OK, AS_ANY, "urlcheck",
AT_URLCHECK, AC_URLCHECK_LIST, AC_STRING, EXF_URLCHECK,
acl_print_urlcheck, acl_add_urlcheck,
NULL, urlcheck_validate },
{ AC_URLCHECK_LIST, MULTIPLE_OK, AS_ANY, "urlcheck_list",
AT_LIST, AC_NONE, AC_NONE, EXF_URLCHECK,
acl_print_list, acl_add_list,
NULL, acl_list_filter },
#endif
#ifdef USE_LDAP
{ AC_LDAPCHECK, MULTIPLE_OK, AS_ANY, "ldapcheck",
AT_LDAPCHECK, AC_NONE, AC_STRING, EXF_LDAPCHECK,
acl_print_ldapcheck, acl_add_ldapcheck,
NULL, ldapcheck_validate },
#endif
#if defined(USE_CURL) || defined(USE_LDAP)
{ AC_PROP, MULTIPLE_OK, AS_ANY, "prop",
AT_PROP, AC_NONE, AC_PROP, EXF_PROP,
acl_print_prop_string, acl_add_prop_string,
acl_free_prop_string, prop_string_validate },
{ AC_PROP_RE, MULTIPLE_OK, AS_ANY, "prop_re",
AT_PROP, AC_NONE, AC_PROP_RE, EXF_PROP,
acl_print_prop_regex, acl_add_prop_regex,
acl_free_prop_regex, prop_regex_validate },
#endif
{ AC_AUTH, MULTIPLE_OK, AS_ANY, "auth",
AT_STRING, AC_AUTH_LIST, AC_STRING, EXF_AUTH,
acl_print_string, acl_add_string,
acl_free_string, acl_auth_strcmp },
{ AC_AUTH_RE, MULTIPLE_OK, AS_ANY, "auth_re",
AT_REGEX, AC_AUTH_LIST, AC_REGEX, EXF_AUTH,
acl_print_regex, acl_add_regex,
acl_free_regex, acl_auth_regexec },
{ AC_AUTH_LIST, MULTIPLE_OK, AS_ANY, "auth_list",
AT_LIST, AC_NONE, AC_NONE, EXF_AUTH,
acl_print_list, acl_add_list,
NULL, acl_list_filter },
{ AC_TLS, MULTIPLE_OK, AS_ANY, "tls",
AT_STRING, AC_TLS_LIST, AC_STRING, EXF_STARTTLS,
acl_print_string, acl_add_string,
acl_free_string, acl_tls_strcmp },
{ AC_TLS_RE, MULTIPLE_OK, AS_ANY, "tls_re",
AT_REGEX, AC_TLS_LIST, AC_REGEX, EXF_STARTTLS,
acl_print_regex, acl_add_regex,
acl_free_regex, acl_tls_regexec },
{ AC_TLS_LIST, MULTIPLE_OK, AS_ANY, "tls_list",
AT_LIST, AC_NONE, AC_NONE, EXF_STARTTLS,
acl_print_list, acl_add_list,
NULL, acl_list_filter },
#if (defined(HAVE_SPF) || defined(HAVE_SPF_ALT) || \
defined(HAVE_SPF2_10) || defined(HAVE_SPF2))
{ AC_SPF, MULTIPLE_OK, AS_ANY, "spf",
AT_SPF, AC_NONE, AC_SPF, EXF_SPF,
acl_print_spf, acl_add_spf,
NULL, spf_check },
#endif
#ifdef USE_DKIM
{ AC_DKIM, MULTIPLE_OK, AS_DATA, "dkim",
AT_DKIM, AC_NONE, AC_DKIM, EXF_DKIM,
acl_print_dkim, acl_add_dkim,
NULL, dkimcheck_validate },
#endif
{ AC_MSGSIZE, MULTIPLE_OK, AS_DATA, "msgsize",
AT_OPNUM, AC_NONE, AC_MSGSIZE, EXF_MSGSIZE,
acl_print_opnum, acl_add_opnum,
NULL, acl_msgsize_cmp },
{ AC_RCPTCOUNT, MULTIPLE_OK, AS_ANY, "rcptcount",
AT_OPNUM, AC_NONE, AC_RCPTCOUNT, EXF_RCPTCOUNT,
acl_print_opnum, acl_add_opnum_body,
NULL, acl_rcptcount_cmp },
{ AC_CLOCKSPEC, MULTIPLE_OK, AS_ANY, "time",
AT_CLOCKSPEC, AC_NONE, AC_CLOCKSPEC, EXF_CLOCKSPEC,
print_clockspec, add_clockspec,
clockspec_free, clockspec_filter },
{ AC_CLOCKSPEC_LIST, MULTIPLE_OK, AS_ANY, "time_list",
AT_LIST, AC_NONE, AC_NONE, EXF_CLOCKSPEC,
acl_print_list, acl_add_list,
NULL, acl_list_filter },
#ifdef USE_GEOIP
{ AC_GEOIP, MULTIPLE_OK, AS_ANY, "geoip",
AT_STRING, AC_GEOIP_LIST, AC_STRING, EXF_GEOIP,
acl_print_string, acl_add_string,
acl_free_string, geoip_filter },
{ AC_GEOIP_LIST, MULTIPLE_OK, AS_ANY, "geoip_list",
AT_LIST, AC_NONE, AC_NONE, EXF_GEOIP,
acl_print_list, acl_add_list,
NULL, acl_list_filter },
#endif
#ifdef USE_P0F
{ AC_P0F, MULTIPLE_OK, AS_ANY, "p0f",
AT_STRING, AC_P0F_LIST, AC_STRING, EXF_P0F,
acl_print_string, acl_add_string,
acl_free_string, p0f_cmp },
{ AC_P0F_RE, UNIQUE, AS_ANY, "p0f_re",
AT_REGEX, AC_P0F_LIST, AC_REGEX, EXF_DOMAIN,
acl_print_regex, acl_add_regex,
acl_free_regex, p0f_regexec },
{ AC_P0F_LIST, MULTIPLE_OK, AS_ANY, "p0f_list",
AT_LIST, AC_NONE, AC_NONE, EXF_P0F,
acl_print_list, acl_add_list,
NULL, acl_list_filter },
#endif
#ifdef USE_SPAMD
{ AC_SA, MULTIPLE_OK, AS_DATA, "spamd",
AT_NONE, AC_NONE, AC_NONE, EXF_SA,
acl_print_null, NULL, NULL, spamd_isspam },
{ AC_SASCORE, MULTIPLE_OK, AS_DATA, "spamd score",
AT_OPNUM, AC_NONE, AC_NONE, EXF_SA,
acl_print_opnum, acl_add_opnum, NULL, spamd_score },
#endif
{ AC_TARPIT, UNIQUE, AS_ANY, "tarpit",
AT_TIME, AC_NONE, AC_NONE, EXF_TARPIT,
acl_print_time, acl_add_time,
NULL, acl_tarpit_filter },
};
struct {
acl_stage_t ss_stage;
char *ss_string;
} stage_string_rec[] = {
{ AS_NONE, "NONE" },
{ AS_RCPT, "RCPT" },
{ AS_DATA, "DATA" },
{ AS_ANY, "ANY" },
};
char *
stage_string(stage)
acl_stage_t stage;
{
int i;
int count = sizeof(stage_string_rec) / sizeof(*stage_string_rec);
for (i = 0; i < count; i++)
if (stage_string_rec[i].ss_stage == stage)
return stage_string_rec[i].ss_string;
mg_log(LOG_ERR, "unexpected ACL stage %d", stage);
exit(EX_SOFTWARE);
/* NOTREACHED */
return NULL;
}
int
acl_opnum_cmp(val1, op, val2)
int val1;
enum operator op;
int val2;
{
switch(op) {
case OP_EQ:
return (val1 == val2);
break;
case OP_NE:
return (val1 != val2);
break;
case OP_LT:
return (val1 < val2);
break;
case OP_GT:
return (val1 > val2);
break;
case OP_LE:
return (val1 <= val2);
break;
case OP_GE:
return (val1 >= val2);
break;
default:
mg_log(LOG_ERR, "unexpected operator");
exit(EX_SOFTWARE);
break;
}
/* NOTREACHED */
return 0;
}
int
acl_rcptcount_cmp(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
if (acl_opnum_cmp(priv->priv_rcptcount, ad->opnum.op, ad->opnum.num))
return 1;
return 0;
}
int
acl_msgsize_cmp(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
if (acl_opnum_cmp(priv->priv_msgcount, ad->opnum.op, ad->opnum.num))
return 1;
return 0;
}
int
acl_domain_cmp(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
char *host = priv->priv_hostname;
char *domain = ad->string;
int hidx, didx;
if ((host[0] == '\0') && domain[0] == '\0')
return 1;
if ((host[0] == '\0') || domain[0] == '\0')
return 0;
hidx = strlen(host) - 1;
didx = strlen(domain) - 1;
while ((hidx >= 0) && (didx >= 0)) {
if (tolower((int)host[hidx]) != tolower((int)domain[didx])) {
return (0);
}
hidx--;
didx--;
}
if (didx >= 0)
return (0);
if ( (conf.c_domainexact == 1) &&(hidx >= 0) && (host[hidx] != '.')) {
mg_log(LOG_INFO, "domainexact option overrides %s suffix "
"match of %s", domain, host);
return (0);
}
return (1);
}
int
acl_header_strstr(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
struct header *h;
if (stage != AS_DATA) {
mg_log(LOG_ERR, "header filter called at non DATA stage");
exit(EX_SOFTWARE);
}
TAILQ_FOREACH(h, &priv->priv_header, h_list)
if (strstr(h->h_line, ad->string) != NULL)
return 1;
return 0;
}
int
acl_body_strstr(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
struct body *b;
if (stage != AS_DATA) {
mg_log(LOG_ERR, "body filter called at non DATA stage");
exit(EX_SOFTWARE);
}
TAILQ_FOREACH(b, &priv->priv_body, b_list)
if (strstr(b->b_lines, ad->string) != NULL)
return 1;
return 0;
}
int
acl_tarpit_filter(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
ap->ap_tarpit = ad->time;
return pending_check_tarpit(SA(&priv->priv_addr), priv->priv_addrlen,
priv->priv_from, priv->priv_cur_rcpt,
ad->time);
}
int
myregexec(priv, ad, ap, string)
struct mlfi_priv *priv;
acl_data_t *ad;
struct acl_param *ap;
const char *string;
{
size_t len;
int nmatch;
regmatch_t *pmatch = NULL;
int retval;
int i;
/*
* Placeholder for information from regexec, +1 for \0
*/
nmatch = ad->regex.nmatch + 1;
if ((pmatch = malloc(nmatch * sizeof(*pmatch))) == NULL) {
mg_log(LOG_ERR, "malloc failed: %s", strerror(errno));
exit(EX_OSERR);
}
bzero(pmatch, nmatch * sizeof(*pmatch));
/*
* The real regexec
*/
retval = regexec(ad->regex.re, string, nmatch, pmatch, 0);
if (retval != 0) /* No match */
goto out;
/*
* Add room for matched parenthesized substrings
*/
len = (ap->ap_nmatch + ad->regex.nmatch) * sizeof(*ap->ap_pmatch);;
if (len > 0) {
if ((ap->ap_pmatch = realloc(ap->ap_pmatch, len)) == NULL) {
mg_log(LOG_ERR, "realloc failed: %s", strerror(errno));
exit(EX_OSERR);
}
}
/* Move the previous matches to the end of the array */
if (ap->ap_nmatch != 0) {
memmove(&ap->ap_pmatch[ad->regex.nmatch],
&ap->ap_pmatch[0], ap->ap_nmatch * sizeof(char *));
}
bzero(&ap->ap_pmatch[0], ad->regex.nmatch * sizeof(char *));
ap->ap_nmatch += ad->regex.nmatch;
/*
* Gather the strings, skipping the first one (\0)
*/
for (i = 1; i < nmatch; i++) {
if (pmatch[i].rm_so == -1) {
mg_log(LOG_DEBUG, "unexpected void backreference no %d "
"in regex %s against \"%s\"",
i, ad->regex.re_copy, string);
break;
}
len = pmatch[i].rm_eo - pmatch[i].rm_so + 1;
if ((ap->ap_pmatch[i - 1] = malloc(len)) == NULL) {
mg_log(LOG_ERR, "malloc failed: %s", strerror(errno));
exit(EX_OSERR);
}
memcpy(ap->ap_pmatch[i - 1], string + pmatch[i].rm_so, len - 1);
ap->ap_pmatch[i - 1][len - 1] = '\0';
if (conf.c_debug)
mg_log(LOG_DEBUG,
"regex /%s/ against \"%s\": match[%d] = \"%s\"",
ad->regex.re_copy, string, i, ap->ap_pmatch[i - 1]);
}
out:
if (pmatch != NULL)
free(pmatch);
#if 0
if (conf.c_debug) {
int i;
for (i = 0; i < ap->ap_nmatch; i++)
mg_log(LOG_DEBUG,
" match[%d] = \"%s\"",
i, ap->ap_pmatch[i]);
}
#endif
return retval;
}
int
acl_helo_regexec(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
if (myregexec(priv, ad, ap, priv->priv_helo) == 0)
return 1;
return 0;
}
int
acl_from_regexec(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
if (myregexec(priv, ad, ap, priv->priv_from) == 0)
return 1;
return 0;
}
int
acl_auth_regexec(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
char *auth_authen;
auth_authen = smfi_getsymval(priv->priv_ctx, "{auth_authen}");
if (auth_authen == NULL)
return 0;
if (myregexec(priv, ad, ap, auth_authen) == 0)
return 1;
return 0;
}
int
acl_tls_regexec(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
char *verify;
char *dn;
if (((verify = smfi_getsymval(priv->priv_ctx, "{verify}")) == NULL) ||
(strcmp(verify, "OK") != 0) ||
((dn = smfi_getsymval(priv->priv_ctx, "{cert_subject}")) == NULL))
return 0;
if (myregexec(priv, ad, ap, dn) == 0)
return 1;
return 0;
}
int
acl_rcpt_regexec(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
if (stage == AS_RCPT) {
if (myregexec(priv, ad, ap, priv->priv_cur_rcpt) == 0)
return 1;
} else {
struct rcpt *r;
LIST_FOREACH(r, &priv->priv_rcpt, r_list)
if (myregexec(priv, ad, ap, r->r_addr) == 0)
return 1;
}
return 0;
}
int
acl_domain_regexec(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
if (myregexec(priv, ad, ap, priv->priv_hostname) == 0)
return 1;
return 0;
}
int
acl_header_regexec(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
struct header *h;
if (stage != AS_DATA) {
mg_log(LOG_ERR, "header filter called at non DATA stage");
exit(EX_SOFTWARE);
}
TAILQ_FOREACH(h, &priv->priv_header, h_list)
if (myregexec(priv, ad, ap, h->h_line) == 0)
return 1;
return 0;
}
int
acl_helo_strstr(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
char *helo = ad->string;
printf("-> %s/%s\n", priv->priv_helo, helo);
if (strstr(priv->priv_helo, helo) != NULL)
return 1;
return 0;
}
int
acl_from_cmp(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
char *from = ad->string;
if (emailcmp(priv->priv_from, from) == 0)
return 1;
return 0;
}
int
acl_rcpt_cmp(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
char *rcpt = ad->string;
if (stage == AS_RCPT) {
if (emailcmp(priv->priv_cur_rcpt, rcpt) == 0)
return 1;
} else {
struct rcpt *r;
LIST_FOREACH(r, &priv->priv_rcpt, r_list)
if (emailcmp(r->r_addr, rcpt) == 0)
return 1;
}
return 0;
}
int
acl_auth_strcmp(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
char *auth_authen;
auth_authen = smfi_getsymval(priv->priv_ctx, "{auth_authen}");
if (auth_authen == NULL)
return 0;
if (strcmp(auth_authen, ad->string) == 0)
return 1;
return 0;
}
int
acl_tls_strcmp(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
char *verify;
char *dn;
if (((verify = smfi_getsymval(priv->priv_ctx, "{verify}")) == NULL) ||
(strcmp(verify, "OK") != 0) ||
((dn = smfi_getsymval(priv->priv_ctx, "{cert_subject}")) == NULL))
return 0;
if (strcmp(dn, ad->string) == 0)
return 1;
return 0;
}
int
acl_list_filter(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
struct all_list_entry *ale;
struct list_entry *le;
int retval;
ale = ad->list;
TAILQ_FOREACH(le, &ale->al_head, l_list) {
retval = (*le->l_acr->acr_filter)(&le->l_data, stage, ap, priv);
if (retval != 0)
return retval;
}
return 0;
}
int
acl_body_regexec(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
struct body *b;
if (stage != AS_DATA) {
mg_log(LOG_ERR, "body filter called at non DATA stage");
exit(EX_SOFTWARE);
}
TAILQ_FOREACH(b, &priv->priv_body, b_list)
if (myregexec(priv, ad, ap, b->b_lines) == 0)
return 1;
return 0;
}
struct acl_clause_rec *
acl_list_item_fixup(item_type, list_type)
acl_clause_t item_type;
acl_clause_t list_type;
{
struct acl_clause_rec *cur_acr;
int i;
int count = sizeof(acl_clause_rec) / sizeof(*acl_clause_rec);
for (i = 0; i < count; i++) {
cur_acr = &acl_clause_rec[i];
if ((cur_acr->acr_list_type == list_type) &&
(cur_acr->acr_item_type == item_type))
return cur_acr;
}
return NULL;
}
struct acl_clause_rec *
get_acl_clause_rec(type)
acl_clause_t type;
{
int i;
int count = sizeof(acl_clause_rec) / sizeof(*acl_clause_rec);
for (i = 0; i < count; i++)
if (acl_clause_rec[i].acr_type == type)
return &acl_clause_rec[i];
mg_log(LOG_ERR, "unexpected acl clause type %d", type);
exit(EX_SOFTWARE);
/* NOTREACHED */
return NULL;
}
char *
acl_print_string(ad, buf, len)
acl_data_t *ad;
char *buf;
size_t len;
{
snprintf(buf, len, "\"%s\"", ad->string);
return buf;
}
char *
acl_print_regex(ad, buf, len)
acl_data_t *ad;
char *buf;
size_t len;
{
snprintf(buf, len, "%s", ad->regex.re_copy);
return buf;
}
char *
acl_print_list(ad, buf, len)
acl_data_t *ad;
char *buf;
size_t len;
{
snprintf(buf, len, "\"%s\"", ad->list->al_name);
return buf;
}
char *
acl_print_null(ad, buf, len)
acl_data_t *ad;
char *buf;
size_t len;
{
if (len > 0)
buf[0] = '\0';
return buf;
}
char *
acl_print_opnum(ad, buf, len)
acl_data_t *ad;
char *buf;
size_t len;
{
struct {
enum operator op;