Debian Codegraph @

plugins/dmarc

raw
1 #!perl -w
2
3 =head1 NAME
4
5 Domain-based Message Authentication, Reporting and Conformance
6
7 =head1 SYNOPSIS
8
9 DMARC is a reliable means to authenticate email.
10
11 =head1 DESCRIPTION
12
13 From the DMARC Draft: "DMARC operates as a policy layer atop DKIM and SPF. These technologies are the building blocks of DMARC as each is widely deployed, supported by mature tools, and is readily available to both senders and receivers. They are complementary, as each is resilient to many of the failure modes of the other."
14
15 DMARC provides a way to exchange authentication information and policies among mail servers.
16
17 DMARC benefits domain owners by preventing others from impersonating them. A domain owner can reliably tell other mail servers that "it it doesn't originate from this list of servers (SPF) and it is not signed (DKIM), then [ignore|quarantine|reject] it." DMARC also provides domain owners with a means to receive feedback and determine that their policies are working as desired.
18
19 DMARC benefits mail server operators by providing them with a more reliable (than SPF or DKIM alone) means to block forged emails. Is that message really from PayPal, Chase, Gmail, or Facebook? Since those organizations publish DMARC policies, operators have a definitive means to know.
20
21 =head1 HOWTO
22
23 =head2 Protect a domain with DMARC
24
25 See Section 10 of the draft: Domain Owner Actions
26
27 1. Deploy DKIM & SPF
28 2. Ensure identifier alignment.
29 3. Publish a "monitor" record, ask for data reports
30 4. Roll policies from monitor to reject
31
32 =head3 Publish a DMARC policy
33
34 _dmarc IN TXT "v=DMARC1; p=reject; rua=mailto:[email protected];"
35
36 v=DMARC1; (version)
37 p=none; (disposition policy : reject, quarantine, none (monitor))
38 sp=reject; (subdomain policy: default, same as p)
39 adkim=s; (dkim alignment: s=strict, r=relaxed)
40 aspf=r; (spf alignment: s=strict, r=relaxed)
41 rua=mailto: [email protected]; (aggregate reports)
42 ruf=mailto: [email protected]; (forensic reports)
43 rf=afrf; (report format: afrf, iodef)
44 ri=8400; (report interval)
45 pct=50; (percent of messages to filter)
46
47 =head2 Validate messages with DMARC
48
49 1. install Mail::DMARC
50
51 2. install this plugin
52
53 3. activate this plugin. (add to config/plugins, listing it after SPF & DKIM. Check that SPF and DKIM are configured to not reject mail.
54
55
56 =head1 MORE INFORMATION
57
58 http://www.dmarc.org/draft-dmarc-base-00-02.txt
59
60 https://github.com/smtpd/qpsmtpd/wiki/DMARC-FAQ
61
62 =head1 TODO
63
64 reject messages with multiple From: headers
65
66 =head1 AUTHORS
67
68 2013 - Matt Simerson <[email protected]>
69
70 =cut
71
72 use strict;
73 use warnings;
74
75 use Data::Dumper;
76 use Qpsmtpd::Constants;
77
78 sub register {
79 my ($self, $qp, @args) = @_;
80
81 $self->log(LOGERROR, "Bad arguments") if @args % 2;
82 $self->{_args} = {@args};
83
84 $self->{_args}{reject} = 1 if !defined $self->{_args}{reject};
85 $self->{_args}{reject_type} ||= 'perm';
86 $self->{_args}{p_vals} = {map { $_ => 1 } qw/ none reject quarantine /};
87
88 eval "require Mail::DMARC::PurePerl";
89 if ( $@ ) {
90 $self->log(LOGERROR, "failed to load Mail::DMARC::PurePerl" );
91 }
92 else {
93 $self->{_dmarc} = Mail::DMARC::PurePerl->new();
94 $self->register_hook('data_post', 'data_post_handler');
95 };
96 }
97
98 sub data_post_handler {
99 my ($self, $transaction) = @_;
100
101 if ( $self->qp->connection->relay_client() ) {
102 $self->log(LOGINFO, "skip, relay client" );
103 return DECLINED; # disable reporting to ourself
104 };
105
106 my $dmarc = $self->{_dmarc};
107 $dmarc->init();
108 my $from = $transaction->header->get('From');
109 if ( ! $from ) {
110 $self->log(LOGINFO, "skip, null sender" );
111 return $self->get_reject("empty from address, null sender?");
112 };
113 eval { $dmarc->header_from_raw( $from ); };
114 if ( $@ ) {
115 $self->log(LOGERROR, "unparseable from header: $from" );
116 return $self->get_reject("unparseable from header");
117 };
118 my @recipients = $transaction->recipients;
119 eval { $dmarc->envelope_to( lc $recipients[0]->host ); }; # optional
120 eval { $dmarc->envelope_from( $transaction->sender->host ); }; # may be <>
121 $dmarc->spf( $transaction->notes('dmarc_spf') );
122 my $dkim = $self->connection->notes('dkim_verifier');
123 if ( $dkim ) { $dmarc->dkim( $dkim ); };
124 $dmarc->source_ip( $self->qp->connection->remote_ip );
125 eval { $dmarc->validate(); };
126 if ( $@ ) {
127 $self->log(LOGERROR, $@ );
128 return DECLINED if $self->is_immune;
129 $self->log(LOGINFO, "TODO: handle this validation failure");
130 return DECLINED;
131 return $self->get_reject( $@, $@ );
132 };
133
134 #$self->log(LOGINFO, "result: " . Dumper( $dmarc ) );
135
136 my $pol;
137 eval { $pol = $dmarc->result->published; };
138 if ( $pol ) {
139 if ( $dmarc->has_valid_reporting_uri($pol->rua) ) {
140 eval { $dmarc->save_aggregate(); };
141 $self->log(LOGERROR, $@ ) if $@;
142 }
143 else {
144 $self->log(LOGERROR, "has policy, no report URI" );
145 };
146 };
147
148 my $disposition = $dmarc->result->disposition;
149 my $auth_str = "dmarc=$disposition";
150 $auth_str = " (p=" . $pol->p . ")" if $pol;
151
152 if ( $dmarc->result->result eq 'pass' ) {
153 $self->log(LOGINFO, "pass");
154 $self->store_auth_results( $auth_str . " d=" . $dmarc->header_from);
155 return DECLINED;
156 };
157
158 my $reason_type = my $comment = '';
159 if ( $dmarc->result->reason && $dmarc->result->reason->[0] ) {
160 $reason_type = $dmarc->result->reason->[0]->type;
161 if ( $dmarc->result->reason->[0]->comment ) {
162 $comment = $dmarc->result->reason->[0]->comment;
163 };
164 };
165 if ( $disposition eq 'none' && $comment && $comment eq 'no policy') {
166 $self->log(LOGINFO, "skip, no policy");
167 return DECLINED;
168 };
169
170 my $log_mess = $dmarc->result->result;
171 $log_mess .= ", tolerated" if $disposition eq 'none';
172 $log_mess .= ", $reason_type" if $reason_type;
173 $log_mess .= ", $comment" if $comment;
174 $self->log(LOGINFO, $log_mess);
175
176 $self->store_auth_results( $auth_str . " d=" . $dmarc->header_from);
177
178 return DECLINED if $disposition eq 'none';
179 return DECLINED if ! $disposition; # for safety
180 return DECLINED if $self->is_immune;
181
182 $self->adjust_karma(-3);
183 # at what point do we reject?
184 return $self->get_reject("failed DMARC policy");
185 }
186