-
Notifications
You must be signed in to change notification settings - Fork 52
/
opendkim.c
16922 lines (14502 loc) · 364 KB
/
opendkim.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
/*
** Copyright (c) 2005-2009 Sendmail, Inc. and its suppliers.
** All rights reserved.
**
** Copyright (c) 2009-2015, The Trusted Domain Project. All rights reserved.
*/
#include "build-config.h"
#ifndef _POSIX_PTHREAD_SEMANTICS
# define _POSIX_PTHREAD_SEMANTICS
#endif /* ! _POSIX_PTHREAD_SEMANTICS */
/* system includes */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/wait.h>
#ifdef HAVE_ISO_LIMITS_ISO_H
# include <iso/limits_iso.h>
#endif /* HAVE_ISO_LIMITS_ISO_H */
#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif /* HAVE_LIMITS_H */
#ifdef __linux__
# include <sys/prctl.h>
#endif /* __linux__ */
#ifdef USE_LUA
# include <netinet/in.h>
# include <arpa/inet.h>
#endif /* USE_LUA */
#ifdef AF_INET6
# include <arpa/inet.h>
#endif /* AF_INET6 */
#ifdef HAVE_STDBOOL_H
# include <stdbool.h>
#endif /* HAVE_STDBOOL_H */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <sysexits.h>
#include <errno.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <math.h>
#include <assert.h>
#include <ctype.h>
#include <fcntl.h>
#include <pthread.h>
#include <netdb.h>
#include <signal.h>
#include <regex.h>
#ifdef USE_GNUTLS
# include <gnutls/gnutls.h>
# include <gnutls/crypto.h>
#else /* USE_GNUTLS */
# include <openssl/sha.h>
# include <openssl/err.h>
#endif /* USE_GNUTLS */
#ifndef SHA_DIGEST_LENGTH
# define SHA_DIGEST_LENGTH 20
#endif /* ! SHA_DIGEST_LENGTH */
#ifdef HAVE_PATHS_H
# include <paths.h>
#endif /* HAVE_PATHS_H */
#ifndef _PATH_DEVNULL
# define _PATH_DEVNULL "/dev/null"
#endif /* ! _PATH_DEVNULL */
/* libmilter includes */
#include "libmilter/mfapi.h"
#ifdef USE_LUA
/* LUA includes */
# include <lua.h>
#endif /* USE_LUA */
#ifdef _FFR_RBL
/* librbl includes */
# include <rbl.h>
#endif /* _FFR_RBL */
/* libopendkim includes */
#include "dkim.h"
#ifdef _FFR_VBR
# include "vbr.h"
#endif /* _FFR_VBR */
/* 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 */
#ifdef _FFR_REPUTATION
/* reputation includes */
# include <repute.h>
#endif /* _FFR_REPUTATION */
#ifdef _FFR_REPRRD
# include <reprrd.h>
#endif /* _FFR_REPRRD */
/* opendkim includes */
#include "config.h"
#ifdef _FFR_RATE_LIMIT
# include "flowrate.h"
#endif /* _FFR_RATE_LIMIT */
#include "opendkim-db.h"
#include "opendkim-config.h"
#include "opendkim-crypto.h"
#include "opendkim.h"
#include "opendkim-ar.h"
#include "opendkim-arf.h"
#include "opendkim-dns.h"
#ifdef USE_LUA
# include "opendkim-lua.h"
#endif /* USE_LUA */
#include "util.h"
#include "test.h"
#ifdef _FFR_STATS
# include "stats.h"
#endif /* _FFR_STATS */
#ifdef _FFR_REPUTATION
# include "reputation.h"
#endif /* _FFR_REPUTATION */
/* macros */
#define CMDLINEOPTS "Ab:c:d:De:fF:k:lL:no:p:P:Qrs:S:t:T:u:vVWx:X?"
#ifndef MIN
# define MIN(x,y) ((x) < (y) ? (x) : (y))
#endif /* ! MIN */
#define DKIMF_MILTER_ACCEPT 0
#define DKIMF_MILTER_REJECT 1
#define DKIMF_MILTER_TEMPFAIL 2
#define DKIMF_MILTER_DISCARD 3
#define DKIMF_MILTER_QUARANTINE 4
/*
** ADDRLIST -- address list
*/
struct addrlist
{
char * a_addr; /* address */
struct addrlist * a_next; /* next record */
};
/*
** HANDLING -- message handling requests
*/
struct handling
{
int hndl_nosig; /* no signature */
int hndl_badsig; /* bad signature */
int hndl_nokey; /* no key in DNS */
int hndl_dnserr; /* DNS error */
int hndl_internal; /* internal error */
#if defined(_FFR_REPUTATION) || defined(_FFR_REPRRD)
int hndl_reperr; /* reputation error */
#endif /* _FFR_REPUTATION || _FFR_REPRRD */
int hndl_security; /* security concerns */
int hndl_siggen; /* sig generation errors */
};
struct handling defaults =
{
DKIMF_MILTER_ACCEPT, /* nosig */
DKIMF_MILTER_ACCEPT, /* badsig */
DKIMF_MILTER_ACCEPT, /* nokey */
DKIMF_MILTER_TEMPFAIL, /* dnserr */
DKIMF_MILTER_TEMPFAIL, /* internal */
#ifdef _FFR_REPUTATION
DKIMF_MILTER_ACCEPT, /* reperror */
#endif /* _FFR_REPUTATION */
DKIMF_MILTER_TEMPFAIL, /* security */
DKIMF_MILTER_REJECT /* siggen */
};
/*
** LUA_GLOBAL -- linked list of Lua globals
*/
struct lua_global
{
int lg_type;
char * lg_name;
void * lg_value;
struct lua_global * lg_next;
};
/*
** CONFIG -- configuration data
*/
struct dkimf_config
{
_Bool conf_disablecryptoinit; /* initialize SSL libs? */
#if defined(USE_LDAP) || defined(USE_ODBX)
_Bool conf_softstart; /* do LDAP/SQL soft starts */
#endif /* defined(USE_LDAP) || defined(USE_ODBX) */
#ifdef _FFR_LUA_ONLY_SIGNING
_Bool conf_luasigning; /* signing via Lua only */
#endif /* _FFR_LUA_ONLY_SIGNING */
_Bool conf_weaksyntax; /* do weaker syntax checking */
_Bool conf_passmalformed; /* pass malformed messages */
_Bool conf_logresults; /* log all results */
_Bool conf_dnsconnect; /* request TCP mode from DNS */
_Bool conf_capture; /* capture unknown errors */
_Bool conf_restrace; /* resolver tracing? */
_Bool conf_acceptdk; /* accept DK keys? */
_Bool conf_addswhdr; /* add identifying header? */
_Bool conf_blen; /* use "l=" when signing */
_Bool conf_ztags; /* use "z=" when signing */
_Bool conf_alwaysaddar; /* always add Auth-Results:? */
_Bool conf_reqreports; /* request reports */
_Bool conf_sendreports; /* signature failure reports */
_Bool conf_reqhdrs; /* required header checks */
_Bool conf_authservidwithjobid; /* use jobids in A-R headers */
_Bool conf_subdomains; /* sign subdomains */
_Bool conf_remsigs; /* remove current signatures? */
_Bool conf_remarall; /* remove all matching ARs? */
_Bool conf_keepar; /* keep our ARs? */
_Bool conf_dolog; /* syslog interesting stuff? */
_Bool conf_dolog_success; /* syslog successes too? */
_Bool conf_milterv2; /* using milter v2? */
_Bool conf_fixcrlf; /* fix bare CRs and LFs? */
_Bool conf_logwhy; /* log mode decision logic */
_Bool conf_allowsha1only; /* allow rsa-sha1 verifying */
_Bool conf_stricthdrs; /* strict header checks */
_Bool conf_keeptmpfiles; /* keep temporary files */
_Bool conf_multisig; /* multiple signatures */
_Bool conf_enablecores; /* enable coredumps */
_Bool conf_noheaderb; /* suppress "header.b" */
_Bool conf_singleauthres; /* single Auth-Results */
_Bool conf_safekeys; /* check key permissions */
#ifdef _FFR_RESIGN
_Bool conf_resignall; /* resign unverified mail */
#endif /* _FFR_RESIGN */
#ifdef USE_LDAP
_Bool conf_ldap_usetls; /* LDAP TLS */
#endif /* USE_LDAP */
#ifdef _FFR_VBR
_Bool conf_vbr_purge; /* purge X-VBR-* fields */
_Bool conf_vbr_trustedonly; /* trusted certifiers only */
#endif /* _FFR_VBR */
#if defined(_FFR_REPUTATION) || defined(_FFR_REPRRD)
_Bool conf_reptest; /* reputation test mode */
_Bool conf_repverbose; /* verbose reputation logs */
#endif /* _FFR_REPUTATION || _FFR_REPRRD */
unsigned int conf_mode; /* operating mode */
unsigned int conf_refcnt; /* reference count */
unsigned int conf_dnstimeout; /* DNS timeout */
unsigned int conf_maxhdrsz; /* max header bytes */
unsigned int conf_maxverify; /* max sigs to verify */
unsigned int conf_minkeybits; /* min key size (bits) */
#ifdef _FFR_REPUTATION
unsigned int conf_repfactor; /* reputation factor */
unsigned int conf_repminimum; /* reputation minimum */
unsigned int conf_repcachettl; /* reputation cache TTL */
unsigned int conf_reptimeout; /* reputation query timeout */
#endif /* _FFR_REPUTATION */
#ifdef USE_UNBOUND
unsigned int conf_boguskey; /* bogus key action */
unsigned int conf_unprotectedkey; /* unprotected key action */
#endif /* USE_UNBOUND */
#ifdef _FFR_RATE_LIMIT
unsigned int conf_flowdatattl; /* flow data TTL */
unsigned int conf_flowfactor; /* flow factor */
#endif /* _FFR_RATE_LIMIT */
int conf_clockdrift; /* tolerable clock drift */
int conf_sigmintype; /* signature minimum type */
size_t conf_sigmin; /* signature minimum */
size_t conf_keylen; /* size of secret key */
#ifdef USE_LUA
size_t conf_screenfuncsz; /* screening function size */
size_t conf_setupfuncsz; /* setup function size */
# ifdef _FFR_STATS
size_t conf_statsfuncsz; /* stats function size */
# endif /* _FFR_STATS */
size_t conf_finalfuncsz; /* final function size */
#endif /* USE_LUA */
ssize_t conf_signbytes; /* bytes to sign */
dkim_canon_t conf_hdrcanon; /* canon. method for headers */
dkim_canon_t conf_bodycanon; /* canon. method for body */
unsigned long conf_sigttl; /* signature TTLs */
dkim_alg_t conf_signalg; /* signing algorithm */
struct config * conf_data; /* configuration data */
#ifdef HAVE_CURL_EASY_STRERROR
char * conf_smtpuri; /* outgoing mail URI */
#endif /* HAVE_CURL_EASY_STRERROR */
char * conf_authservid; /* authserv-id */
char * conf_keyfile; /* key file for single key */
char * conf_keytable; /* key table */
char * conf_signtable; /* signing table */
char * conf_peerfile; /* peer file */
char * conf_internalfile; /* internal hosts file */
char * conf_externalfile; /* external hosts file */
char * conf_exemptfile; /* exempt domains file */
char * conf_tmpdir; /* temp directory */
char * conf_omitlist; /* omit header list */
char * conf_domlist; /* signing domain list */
char * conf_signalgstr; /* signature algorithm string */
char * conf_modestr; /* mode string */
char * conf_canonstr; /* canonicalization(s) string */
char * conf_siglimit; /* signing limits */
char * conf_chroot; /* chroot(2) directory */
char * conf_selectcanonhdr; /* canon select header name */
u_char * conf_selector; /* key selector */
#ifdef _FFR_DEFAULT_SENDER
char * conf_defsender; /* default sender address */
#endif /* _FFR_DEFAULT_SENDER */
#ifdef _FFR_RESIGN
char * conf_resign; /* resign mail to */
#endif /* _FFR_RESIGN */
#ifdef _FFR_SENDER_MACRO
char * conf_sendermacro; /* macro containing sender */
#endif /* _FFR_SENDER_MACRO */
char * conf_testdnsdata; /* test DNS data */
#ifdef _FFR_IDENTITY_HEADER
char * conf_identityhdr; /* identity header */
_Bool conf_rmidentityhdr; /* remove identity header */
#endif /* _FFR_IDENTITY_HEADER */
char * conf_diagdir; /* diagnostics directory */
#ifdef _FFR_STATS
char * conf_statspath; /* path for stats file */
char * conf_reporthost; /* reporter name */
char * conf_reportprefix; /* stats data prefix */
#endif /* _FFR_STATS */
char * conf_reportaddr; /* report sender address */
char * conf_reportaddrbcc; /* report repcipient address as bcc */
char * conf_mtacommand; /* MTA command (reports) */
char * conf_redirect; /* redirect failures to */
#ifdef USE_LDAP
char * conf_ldap_timeout; /* LDAP timeout */
char * conf_ldap_kaidle; /* LDAP keepalive idle */
char * conf_ldap_kaprobes; /* LDAP keepalive probes */
char * conf_ldap_kainterval; /* LDAP keepalive interval */
char * conf_ldap_binduser; /* LDAP bind user */
char * conf_ldap_bindpw; /* LDAP bind password */
char * conf_ldap_authmech; /* LDAP auth mechanism */
# ifdef USE_SASL
char * conf_ldap_authname; /* LDAP auth name */
char * conf_ldap_authuser; /* LDAP auth user */
char * conf_ldap_authrealm; /* LDAP auth realm */
# endif /* USE_SASL */
#endif /* USE_LDAP */
#ifdef USE_LUA
char * conf_screenscript; /* Lua script: screening */
void * conf_screenfunc; /* Lua function: screening */
char * conf_setupscript; /* Lua script: setup */
void * conf_setupfunc; /* Lua function: setup */
# ifdef _FFR_STATSEXT
char * conf_statsscript; /* Lua script: stats */
void * conf_statsfunc; /* Lua function: stats */
# endif /* _FFR_STATSEXT */
char * conf_finalscript; /* Lua script: final */
void * conf_finalfunc; /* Lua function: final */
#endif /* USE_LUA */
#ifdef _FFR_REPLACE_RULES
char * conf_rephdrs; /* replacement headers */
struct replace * conf_replist; /* replacement list */
DKIMF_DB conf_rephdrsdb; /* replacement headers (DB) */
#endif /* _FFR_REPLACE_RULES */
dkim_sigkey_t conf_seckey; /* secret key data */
char * conf_nslist; /* replacement NS list */
char * conf_trustanchorpath; /* trust anchor file */
char * conf_resolverconfig; /* resolver config file */
#ifdef _FFR_VBR
char * conf_vbr_deftype; /* default VBR type */
char * conf_vbr_defcert; /* default VBR certifiers */
DKIMF_DB conf_vbr_trusteddb; /* trusted certifiers (DB) */
u_char ** conf_vbr_trusted; /* trusted certifiers */
#endif /* _FFR_VBR */
DKIMF_DB conf_testdnsdb; /* test TXT records */
DKIMF_DB conf_bldb; /* l= recipients (DB) */
DKIMF_DB conf_domainsdb; /* domains to sign (DB) */
DKIMF_DB conf_omithdrdb; /* headers to omit (DB) */
char ** conf_omithdrs; /* headers to omit (array) */
DKIMF_DB conf_signhdrsdb; /* headers to sign (DB) */
char ** conf_signhdrs; /* headers to sign (array) */
DKIMF_DB conf_senderhdrsdb; /* sender headers (DB) */
char ** conf_senderhdrs; /* sender headers (array) */
DKIMF_DB conf_mtasdb; /* MTA ports to sign (DB) */
char ** conf_mtas; /* MTA ports to sign (array) */
DKIMF_DB conf_remardb; /* A-R removal list (DB) */
char ** conf_remar; /* A-R removal list (array) */
DKIMF_DB conf_mbsdb; /* must-be-signed hdrs (DB) */
char ** conf_mbs; /* must-be-signed (array) */
DKIMF_DB conf_oversigndb; /* fields to over-sign (DB) */
char ** conf_oversignhdrs; /* " " " (array) */
DKIMF_DB conf_dontsigntodb; /* don't-sign-to addrs (DB) */
#ifdef _FFR_ATPS
DKIMF_DB conf_atpsdb; /* ATPS domains */
char * conf_atpshash; /* ATPS hash algorithm */
#endif /* _FFR_ATPS */
DKIMF_DB conf_thirdpartydb; /* trustsigsfrom DB */
DKIMF_DB conf_macrosdb; /* macros/values (DB) */
char ** conf_macros; /* macros/values to check */
regex_t ** conf_nosignpats; /* do-not-sign patterns */
DKIMF_DB conf_peerdb; /* DB of "peers" */
DKIMF_DB conf_internal; /* DB of "internal" hosts */
DKIMF_DB conf_exignore; /* "external ignore" host DB */
DKIMF_DB conf_exemptdb; /* exempt domains DB */
DKIMF_DB conf_keytabledb; /* key table DB */
DKIMF_DB conf_signtabledb; /* signing table DB */
#ifdef _FFR_STATS
DKIMF_DB conf_anondb; /* anonymized domains DB */
#endif /* _FFR_STATS */
#ifdef _FFR_RESIGN
DKIMF_DB conf_resigndb; /* resigning addresses */
#endif /* _FFR_RESIGN */
#ifdef _FFR_RATE_LIMIT
DKIMF_DB conf_ratelimitdb; /* domain rate limits */
DKIMF_DB conf_flowdatadb; /* domain flow data */
#endif /* _FFR_RATE_LIMIT */
#ifdef _FFR_REPUTATION
char * conf_repratios; /* reputed ratios */
DKIMF_DB conf_repratiosdb; /* reputed ratios DB */
char * conf_replimits; /* reputed limits */
DKIMF_DB conf_replimitsdb; /* reputed limits DB */
char * conf_replimitmods; /* reputed limit modifiers */
DKIMF_DB conf_replimitmodsdb; /* reputed limit mods DB */
char * conf_replowtime; /* reputed low timers */
DKIMF_DB conf_replowtimedb; /* reputed low timers DB */
DKIMF_REP conf_rep; /* reputation subsystem */
char * conf_repcache; /* reputation cache DB */
char * conf_repdups; /* reputation duplicates DB */
char * conf_repspamcheck; /* reputation spam RE string */
regex_t conf_repspamre; /* reputation spam RE */
#endif /* _FFR_REPUTATION */
#ifdef _FFR_REPRRD
REPRRD conf_reprrd; /* reputation RRD handle */
#endif /* _FFR_REPRRD */
DKIM_LIB * conf_libopendkim; /* DKIM library handle */
struct handling conf_handling; /* message handling */
};
/*
** MSGCTX -- message context, containing transaction-specific data
*/
typedef struct msgctx * msgctx;
struct msgctx
{
_Bool mctx_internal; /* internal source? */
_Bool mctx_bldbdone; /* BodyLengthDB applied? */
_Bool mctx_eom; /* in EOM? (enables progress) */
_Bool mctx_addheader; /* Authentication-Results: */
_Bool mctx_headeronly; /* in EOM, only add headers */
_Bool mctx_ltag; /* sign with l= tag? */
_Bool mctx_capture; /* capture message? */
_Bool mctx_susp; /* suspicious message? */
#ifdef _FFR_RESIGN
_Bool mctx_resign; /* arrange to re-sign */
#endif /* _FFR_RESIGN */
#ifdef _FFR_VBR
_Bool mctx_vbrpurge; /* purge X-VBR-* headers */
#endif /* _FFR_VBR */
#ifdef _FFR_REPUTATION
_Bool mctx_spam; /* is spam? */
#endif /* _FFR_REPUTATION */
#ifdef _FFR_ATPS
int mctx_atps; /* ATPS */
#endif /* _FFR_ATPS */
#ifdef USE_LUA
int mctx_mresult; /* SMFI status code */
#endif /* USE_LUA */
int mctx_status; /* status to report back */
dkim_canon_t mctx_hdrcanon; /* header canonicalization */
dkim_canon_t mctx_bodycanon; /* body canonicalization */
dkim_alg_t mctx_signalg; /* signature algorithm */
#ifdef USE_UNBOUND
int mctx_dnssec_key; /* DNSSEC results for key */
#endif /* USE_UNBOUND */
int mctx_queryalg; /* query algorithm */
int mctx_hdrbytes; /* header space allocated */
struct dkimf_dstring * mctx_tmpstr; /* temporary string */
u_char * mctx_jobid; /* job ID */
u_char * mctx_laddr; /* address triggering l= */
DKIM * mctx_dkimv; /* verification handle */
#ifdef _FFR_VBR
VBR * mctx_vbr; /* VBR handle */
char * mctx_vbrinfo; /* VBR-Info header field */
#endif /* _FFR_VBR */
struct Header * mctx_hqhead; /* header queue head */
struct Header * mctx_hqtail; /* header queue tail */
struct signreq * mctx_srhead; /* signature request head */
struct signreq * mctx_srtail; /* signature request tail */
struct addrlist * mctx_rcptlist; /* recipient list */
#ifdef _FFR_STATSEXT
struct statsext * mctx_statsext; /* extension stats list */
#endif /* _FFR_STATSEXT */
struct lua_global * mctx_luaglobalh; /* Lua global list */
struct lua_global * mctx_luaglobalt; /* Lua global list */
#ifdef _FFR_REPUTATION
# ifdef USE_GNUTLS
gnutls_hash_hd_t mctx_hash; /* hash, for dup detection */
# else /* USE_GNUTLS */
SHA_CTX mctx_hash; /* hash, for dup detection */
# endif /* USE_GNUTLS */
#endif /* _FFR_REPUTATION */
unsigned char mctx_envfrom[MAXADDRESS + 1];
/* envelope sender */
unsigned char mctx_domain[DKIM_MAXHOSTNAMELEN + 1];
/* primary domain */
unsigned char mctx_dkimar[DKIM_MAXHEADER + 1];
/* DKIM Auth-Results content */
};
/*
** CONNCTX -- connection context, containing thread-specific data
*/
typedef struct connctx * connctx;
struct connctx
{
_Bool cctx_milterv2; /* milter v2 available */
_Bool cctx_noleadspc; /* no leading spaces */
char cctx_host[DKIM_MAXHOSTNAMELEN + 1];
/* hostname */
struct sockaddr_storage cctx_ip; /* IP info */
struct dkimf_config * cctx_config; /* configuration in use */
struct msgctx * cctx_msg; /* message context */
};
/*
** LOOKUP -- lookup table
*/
struct lookup
{
char * str;
int code;
};
#define HNDL_DEFAULT 0
#define HNDL_NOSIGNATURE 1
#define HNDL_BADSIGNATURE 2
#define HNDL_DNSERROR 3
#define HNDL_INTERNAL 4
#define HNDL_SECURITY 5
#define HNDL_NOKEY 6
#define HNDL_POLICYERROR 7
#define HNDL_REPERROR 8
#define HNDL_SIGGEN 9
#define DKIMF_MODE_SIGNER 0x01
#define DKIMF_MODE_VERIFIER 0x02
#define DKIMF_MODE_DEFAULT (DKIMF_MODE_SIGNER|DKIMF_MODE_VERIFIER)
#define DKIMF_STATUS_GOOD 0
#define DKIMF_STATUS_BAD 1
#define DKIMF_STATUS_NOKEY 2
#define DKIMF_STATUS_REVOKED 3
#define DKIMF_STATUS_NOSIGNATURE 4
#define DKIMF_STATUS_BADFORMAT 5
#define DKIMF_STATUS_PARTIAL 6
#define DKIMF_STATUS_VERIFYERR 7
#define DKIMF_STATUS_UNKNOWN 8
#define SIGMIN_BYTES 0
#define SIGMIN_PERCENT 1
#define SIGMIN_MAXADD 2
#if defined(_FFR_REPUTATION) || defined(_FFR_REPRRD)
# define REPDENYSMTP "450"
# define REPDENYESC "4.7.1"
# define REPDENYTXT "Message deferred for policy reasons"
#endif /* _FFR_REPUTATION || _FFR_REPRRD */
#define DELIMITER "\001"
struct lookup dkimf_params[] =
{
{ "badsignature", HNDL_BADSIGNATURE },
{ "default", HNDL_DEFAULT },
{ "dnserror", HNDL_DNSERROR },
{ "internalerror", HNDL_INTERNAL },
{ "keynotfound", HNDL_NOKEY },
{ "nosignature", HNDL_NOSIGNATURE },
#ifdef _FFR_REPUTATION
{ "reputationerror", HNDL_REPERROR },
#endif /* _FFR_REPUTATION */
{ "security", HNDL_SECURITY },
{ "signatureerror", HNDL_SIGGEN },
{ NULL, -1 },
};
struct lookup dkimf_values[] =
{
{ "a", DKIMF_MILTER_ACCEPT },
{ "accept", DKIMF_MILTER_ACCEPT },
{ "d", DKIMF_MILTER_DISCARD },
{ "discard", DKIMF_MILTER_DISCARD },
#ifdef SMFIF_QUARANTINE
{ "q", DKIMF_MILTER_QUARANTINE },
{ "quarantine", DKIMF_MILTER_QUARANTINE },
#endif /* SMFIF_QUARANTINE */
{ "r", DKIMF_MILTER_REJECT },
{ "reject", DKIMF_MILTER_REJECT },
{ "t", DKIMF_MILTER_TEMPFAIL },
{ "tempfail", DKIMF_MILTER_TEMPFAIL },
{ NULL, -1 },
};
struct lookup dkimf_canon[] =
{
{ "relaxed", DKIM_CANON_RELAXED },
{ "simple", DKIM_CANON_SIMPLE },
{ NULL, -1 },
};
struct lookup dkimf_sign[] =
{
{ "rsa-sha1", DKIM_SIGN_RSASHA1 },
{ "rsa-sha256", DKIM_SIGN_RSASHA256 },
{ NULL, -1 },
};
struct lookup dkimf_atpshash[] =
{
#ifdef HAVE_SHA256
{ "sha256", 1 },
#endif /* HAVE_SHA256 */
{ "sha1", 1 },
{ "none", 1 },
{ NULL, -1 },
};
struct lookup log_facilities[] =
{
{ "auth", LOG_AUTH },
{ "cron", LOG_CRON },
{ "daemon", LOG_DAEMON },
{ "kern", LOG_KERN },
{ "lpr", LOG_LPR },
{ "mail", LOG_MAIL },
{ "news", LOG_NEWS },
{ "security", LOG_AUTH }, /* DEPRECATED */
{ "syslog", LOG_SYSLOG },
{ "user", LOG_USER },
{ "uucp", LOG_UUCP },
{ "local0", LOG_LOCAL0 },
{ "local1", LOG_LOCAL1 },
{ "local2", LOG_LOCAL2 },
{ "local3", LOG_LOCAL3 },
{ "local4", LOG_LOCAL4 },
{ "local5", LOG_LOCAL5 },
{ "local6", LOG_LOCAL6 },
{ "local7", LOG_LOCAL7 },
{ NULL, -1 }
};
#ifdef USE_UNBOUND
# define DKIMF_KEYACTIONS_NONE 0
# define DKIMF_KEYACTIONS_NEUTRAL 1
# define DKIMF_KEYACTIONS_FAIL 2
struct lookup dkimf_keyactions[] =
{
{ "none", DKIMF_KEYACTIONS_NONE },
{ "neutral", DKIMF_KEYACTIONS_NEUTRAL },
{ "fail", DKIMF_KEYACTIONS_FAIL },
{ NULL, -1 },
};
#endif /* USE_UNBOUND */
struct lookup dkimf_statusstrings[] =
{
{ "no error", DKIMF_STATUS_GOOD },
{ "bad signature", DKIMF_STATUS_BAD },
{ "key retrieval failed", DKIMF_STATUS_NOKEY },
{ "key revoked", DKIMF_STATUS_REVOKED },
{ "no signature", DKIMF_STATUS_NOSIGNATURE },
{ "bad message/signature format", DKIMF_STATUS_BADFORMAT },
{ "invalid partial signature", DKIMF_STATUS_PARTIAL },
{ "verification error", DKIMF_STATUS_VERIFYERR },
{ "unknown error", DKIMF_STATUS_UNKNOWN },
{ NULL, -1 }
};
/* PROTOTYPES */
#ifdef LEAK_TRACKING
void dkimf_debug_free __P((void *, char *, int));
void *dkim_debug_malloc __P((size_t, char *, int));
void *dkim_debug_realloc __P((void *, size_t, char *, int));
# define free(x) dkimf_debug_free((x), __FILE__, __LINE__)
# define malloc(x) dkimf_debug_malloc((x), __FILE__, __LINE__)
# define realloc(x,y) dkimf_debug_realloc((x), (y), __FILE__, __LINE__)
#endif /* LEAK_TRACKING */
sfsistat mlfi_abort __P((SMFICTX *));
sfsistat mlfi_body __P((SMFICTX *, u_char *, size_t));
sfsistat mlfi_close __P((SMFICTX *));
sfsistat mlfi_connect __P((SMFICTX *, char *, _SOCK_ADDR *));
sfsistat mlfi_envfrom __P((SMFICTX *, char **));
sfsistat mlfi_envrcpt __P((SMFICTX *, char **));
sfsistat mlfi_eoh __P((SMFICTX *));
sfsistat mlfi_eom __P((SMFICTX *));
sfsistat mlfi_header __P((SMFICTX *, char *, char *));
sfsistat mlfi_negotiate __P((SMFICTX *, unsigned long, unsigned long,
unsigned long, unsigned long,
unsigned long *, unsigned long *,
unsigned long *, unsigned long *));
static int dkimf_add_signrequest __P((struct msgctx *, DKIMF_DB,
char *, char *, ssize_t));
sfsistat dkimf_addheader __P((SMFICTX *, char *, char *));
sfsistat dkimf_addrcpt __P((SMFICTX *, char *));
static int dkimf_apply_signtable __P((struct msgctx *, DKIMF_DB, DKIMF_DB,
unsigned char *, unsigned char *, char *,
size_t, _Bool));
sfsistat dkimf_chgheader __P((SMFICTX *, char *, int, char *));
static void dkimf_cleanup __P((SMFICTX *));
static void dkimf_config_reload __P((void));
sfsistat dkimf_delrcpt __P((SMFICTX *, char *));
static Header dkimf_findheader __P((msgctx, char *, int));
void *dkimf_getpriv __P((SMFICTX *));
char *dkimf_getsymval __P((SMFICTX *, char *));
sfsistat dkimf_insheader __P((SMFICTX *, int, char *, char *));
sfsistat dkimf_quarantine __P((SMFICTX *, char *));
void dkimf_sendprogress __P((const void *));
sfsistat dkimf_setpriv __P((SMFICTX *, void *));
sfsistat dkimf_setreply __P((SMFICTX *, char *, char *, char *));
static void dkimf_sigreport __P((connctx, struct dkimf_config *, char *));
/* GLOBALS */
_Bool dolog; /* logging? (exported) */
_Bool reload; /* reload requested */
_Bool no_i_whine; /* noted ${i} is undefined */
_Bool testmode; /* test mode */
_Bool allowdeprecated; /* allow deprecated config values */
#ifdef QUERY_CACHE
_Bool querycache; /* local query cache */
#endif /* QUERY_CACHE */
_Bool die; /* global "die" flag */
int diesig; /* signal to distribute */
int thread_count; /* thread count */
#ifdef QUERY_CACHE
time_t cache_lastlog; /* last cache stats logged */
#endif /* QUERY_CACHE */
char *progname; /* program name */
char *sock; /* listening socket */
char *conffile; /* configuration file */
struct dkimf_config *curconf; /* current configuration */
#ifdef POPAUTH
DKIMF_DB popdb; /* POP auth DB */
#endif /* POPAUTH */
char reportcmd[BUFRSZ + 1]; /* reporting command */
char reportaddr[MAXADDRESS + 1]; /* reporting address */
char myhostname[DKIM_MAXHOSTNAMELEN + 1]; /* hostname */
pthread_mutex_t conf_lock; /* config lock */
pthread_mutex_t pwdb_lock; /* passwd/group lock */
/* Other useful definitions */
#define CRLF "\r\n" /* CRLF */
/* MACROS */
#define JOBID(x) ((x) == NULL ? JOBIDUNKNOWN : (char *) (x))
#define TRYFREE(x) do { \
if ((x) != NULL) \
{ \
free(x); \
(x) = NULL; \
} \
} while (0)
#define DKIMF_EOHMACROS "i {daemon_name} {auth_type}"
/*
** ==================================================================
** BEGIN private section
*/
#ifndef HAVE_SMFI_INSHEADER
/*
** SMFI_INSHEADER -- stub for smfi_insheader() which didn't exist before
** sendmail 8.13.0
**
** Parameters:
** ctx -- milter context
** idx -- insertion index
** hname -- header name
** hvalue -- header value
**
** Return value:
** An sfsistat.
*/
sfsistat
smfi_insheader(SMFICTX *ctx, int idx, char *hname, char *hvalue)
{
assert(ctx != NULL);
assert(hname != NULL);
assert(hvalue != NULL);
return smfi_addheader(ctx, hname, hvalue);
}
#endif /* ! HAVE_SMFI_INSHEADER */
/*
** DKIMF_GETPRIV -- wrapper for smfi_getpriv()
**
** Parameters:
** ctx -- milter (or test) context
**
** Return value:
** The stored private pointer, or NULL.
*/
void *
dkimf_getpriv(SMFICTX *ctx)
{
assert(ctx != NULL);
if (testmode)
return dkimf_test_getpriv((void *) ctx);
else
return smfi_getpriv(ctx);
}
/*
** DKIMF_SETPRIV -- wrapper for smfi_setpriv()
**
** Parameters:
** ctx -- milter (or test) context
**
** Return value:
** An sfsistat.
*/
sfsistat
dkimf_setpriv(SMFICTX *ctx, void *ptr)
{
assert(ctx != NULL);
if (testmode)
return dkimf_test_setpriv((void *) ctx, ptr);
else
return smfi_setpriv(ctx, ptr);
}
/*
** DKIMF_INSHEADER -- wrapper for smfi_insheader()
**
** Parameters:
** ctx -- milter (or test) context
** idx -- index at which to insert
** hname -- header name
** hvalue -- header value
**
** Return value:
** An sfsistat.
*/
sfsistat
dkimf_insheader(SMFICTX *ctx, int idx, char *hname, char *hvalue)
{
assert(ctx != NULL);
assert(hname != NULL);
assert(hvalue != NULL);
if (testmode)
return dkimf_test_insheader(ctx, idx, hname, hvalue);
else
#ifdef HAVE_SMFI_INSHEADER
return smfi_insheader(ctx, idx, hname, hvalue);
#else /* HAVE_SMFI_INSHEADER */
return smfi_addheader(ctx, hname, hvalue);
#endif /* HAVE_SMFI_INSHEADER */
}
/*
** DKIMF_CHGHEADER -- wrapper for smfi_chgheader()
**
** Parameters:
** ctx -- milter (or test) context
** hname -- header name
** idx -- index of header to be changed
** hvalue -- header value
**
** Return value:
** An sfsistat.
*/
sfsistat
dkimf_chgheader(SMFICTX *ctx, char *hname, int idx, char *hvalue)
{
assert(ctx != NULL);
assert(hname != NULL);
if (testmode)
return dkimf_test_chgheader(ctx, hname, idx, hvalue);
else
return smfi_chgheader(ctx, hname, idx, hvalue);
}
/*
** DKIMF_QUARANTINE -- wrapper for smfi_quarantine()
**
** Parameters:
** ctx -- milter (or test) context
** reason -- quarantine reason
**
** Return value:
** An sfsistat.
*/
sfsistat
dkimf_quarantine(SMFICTX *ctx, char *reason)
{
assert(ctx != NULL);
if (testmode)
return dkimf_test_quarantine(ctx, reason);
#ifdef SMFIF_QUARANTINE
else
return smfi_quarantine(ctx, reason);
#endif /* SMFIF_QUARANTINE */
}
/*
** DKIMF_ADDHEADER -- wrapper for smfi_addheader()
**
** Parameters:
** ctx -- milter (or test) context
** hname -- header name
** hvalue -- header value
**
** Return value:
** An sfsistat.
*/
sfsistat
dkimf_addheader(SMFICTX *ctx, char *hname, char *hvalue)
{
assert(ctx != NULL);
assert(hname != NULL);
assert(hvalue != NULL);
if (testmode)
return dkimf_test_addheader(ctx, hname, hvalue);
else
return smfi_addheader(ctx, hname, hvalue);
}
/*
** DKIMF_ADDRCPT -- wrapper for smfi_addrcpt()
**
** Parameters:
** ctx -- milter (or test) context
** addr -- address to add
**
** Return value:
** An sfsistat.
*/
sfsistat
dkimf_addrcpt(SMFICTX *ctx, char *addr)
{
assert(ctx != NULL);
assert(addr != NULL);
if (testmode)
return dkimf_test_addrcpt(ctx, addr);
else
return smfi_addrcpt(ctx, addr);
}
/*
** DKIMF_DELRCPT -- wrapper for smfi_delrcpt()
**
** Parameters:
** ctx -- milter (or test) context
** addr -- address to delete
**
** Return value:
** An sfsistat.
*/
sfsistat
dkimf_delrcpt(SMFICTX *ctx, char *addr)
{
assert(ctx != NULL);