Debian Codegraph @

plugins/sender_permitted_from

raw
1 #!perl -w
2
3 =head1 NAME
4
5 SPF - implement Sender Permitted From
6
7 =head1 SYNOPSIS
8
9 Prevents email sender address spoofing by checking the SPF policy of the purported senders domain.
10
11 Sets the transaction note spf_pass_host if the SPF result is pass.
12
13 =head1 DESCRIPTION
14
15 Sender Policy Framework (SPF) is an email validation system designed to prevent source address spoofing. SPF allows administrators to specify which hosts are allowed to send email from a given domain by creating a specific SPF record in the public DNS. Mail exchangers then use the DNS to verify that mail is being sent by a host sanctioned by a given domain administrators. -- http://en.wikipedia.org/wiki/Sender_Policy_Framework
16
17 The results of a SPF query are stored in a transaction note named 'spfquery';
18
19 =head1 CONFIGURATION
20
21 In config/plugins, add arguments to the sender_permitted_from line.
22
23 sender_permitted_from reject 3
24
25 =head2 reject
26
27 Set to a value between 1 and 6 to enable the following SPF behaviors:
28
29 1 annotate-only, add Received-SPF header, no rejections.
30 2 defer on DNS failures. Assure there's always a meaningful SPF header.
31 3 rejected if SPF record says 'fail'
32 4 stricter reject. Also rejects 'softfail'
33 5 reject 'neutral'
34 6 reject if no SPF records, or a syntax error
35
36 Most sites should start at level 3. It temporarily defers connections (4xx) that have soft SFP failures and only rejects (5xx) messages when the sending domains policy suggests it.
37
38 SPF levels above 4 are for crusaders who don't mind rejecting some valid mail when the sending server administrator hasn't dotted his i's and crossed his t's. May the deities bless their obsessive little hearts.
39
40 =head1 SEE ALSO
41
42 http://spf.pobox.com/
43 http://en.wikipedia.org/wiki/Sender_Policy_Framework
44
45 =head1 TODO
46
47 Check the scope of the SPF policy. If it's too broad (ie, the whole internet is valid), apply karma penalty
48 Examples of too broad: +all,
49
50 =head1 ACKNOWLDGEMENTS
51
52 The reject options are modeled after, and aim to match the functionality of those found in the SPF patch for qmail-smtpd.
53
54 =head1 AUTHOR
55
56 Matt Simerson - 2013 - populate dmarc_spf note with SPF results
57 Matt Simerson - 2012 - increased policy options from 3 to 6
58 Matt Simerson - 2011 - rewrote using Mail::SPF
59 Matt Sergeant - 2003 - initial plugin
60
61 =cut
62
63 use strict;
64 use warnings;
65
66 #use Mail::SPF 2.000; # eval'ed in ->register
67 use Qpsmtpd::Constants;
68
69 sub register {
70 my ($self, $qp, %args) = @_;
71 eval 'use Mail::SPF';
72 if ($@) {
73 warn "skip: plugin disabled, is Mail::SPF installed?\n";
74 $self->log(LOGERROR, "skip: plugin disabled, is Mail::SPF installed?");
75 return;
76 }
77 $self->{_args} = {%args};
78 if ($self->{_args}{spf_deny}) {
79 $self->{_args}{reject} = 3 if $self->{_args}{spf_deny} == 1;
80 $self->{_args}{reject} = 4 if $self->{_args}{spf_deny} == 2;
81 }
82 if (!$self->{_args}{reject} && $self->qp->config('spfbehavior')) {
83 $self->{_args}{reject} = $self->qp->config('spfbehavior');
84 }
85 $self->register_hook('mail', 'mail_handler');
86 $self->register_hook('data_post', 'data_post_handler');
87 }
88
89 sub mail_handler {
90 my ($self, $transaction, $sender, %param) = @_;
91
92 if ( $self->is_immune() ) {
93 $transaction->notes('dmarc_spf', {
94 domain => $sender->host,
95 scope => 'mfrom',
96 result => 'pass',
97 } );
98 return (DECLINED);
99 };
100
101 my $format = $sender->format;
102 if ($format eq '<>' || !$sender->host || !$sender->user) {
103 $self->log(LOGINFO, "skip, null sender");
104 $transaction->notes('dmarc_spf', {
105 scope => 'helo',
106 result => 'none',
107 } );
108 return (DECLINED, "SPF - null sender");
109 }
110
111 my $from = $sender->user . '@' . lc($sender->host);
112 my $helo = $self->qp->connection->hello_host;
113 my $scope = $from ? 'mfrom' : 'helo';
114 my %req_params = (
115 versions => [1, 2], # optional
116 scope => $scope,
117 ip_address => $self->qp->connection->remote_ip,
118 );
119
120 if ($scope =~ /^mfrom|pra$/) {
121 $req_params{identity} = $from;
122 $req_params{helo_identity} = $helo if $helo;
123 }
124 elsif ($scope eq 'helo') {
125 $req_params{identity} = $helo;
126 $req_params{helo_identity} = $helo;
127 }
128
129 $transaction->notes('dmarc_spf', {
130 domain => $scope eq 'helo' ? $helo : $sender->host,
131 scope => $scope,
132 result => 'none',
133 } );
134
135 my $spf_server = Mail::SPF::Server->new();
136 my $request = Mail::SPF::Request->new(%req_params);
137 my $result = $spf_server->process($request) or do {
138 $self->log(LOGINFO, "fail, no result");
139 return DECLINED;
140 };
141
142 $transaction->notes('spfquery', $result);
143
144 my $code = $result->code;
145 my $why = $result->local_explanation;
146 my $reject = $self->{_args}{reject};
147
148 if (!$code) {
149 $self->log(LOGINFO, "fail, no response");
150 return (DENYSOFT, "SPF - no response") if $reject >= 2;
151 return (DECLINED, "SPF - no response");
152 }
153
154 $transaction->notes('dmarc_spf', {
155 domain => $scope eq 'helo' ? $helo : $sender->host,
156 scope => $scope,
157 result => $code,
158 } );
159
160 $self->store_auth_results("spf=$code smtp.mailfrom=".$sender->host);
161
162 if ($code eq 'pass') {
163 $self->adjust_karma(1);
164 $transaction->notes('spf_pass_host', lc $sender->host);
165 $self->log(LOGINFO, "pass, $why");
166 return (DECLINED);
167 }
168
169 if (!$reject) {
170 $self->log(LOGINFO, "skip, tolerated ($code: $why)");
171 return (DECLINED, "SPF - $code: $why");
172 }
173
174 # SPF result codes: pass fail softfail neutral none error permerror temperror
175 return $self->handle_code_none($reject, $why) if $code eq 'none';
176 return $self->handle_code_fail($reject, $why) if $code eq 'fail';
177 return $self->handle_code_softfail($reject, $why) if $code eq 'softfail';
178
179 if ($code eq 'neutral') {
180 if ($reject >= 5 ) {
181 $self->log(LOGINFO, "fail, $code, $why");
182 return (DENY, "SPF - $code: $why");
183 };
184 $self->log(LOGINFO, "fail, tolerated, $code, $why");
185 return (DECLINED);
186 }
187 if ($code =~ /(?:permerror|error)/ ) {
188 $self->log(LOGINFO, "fail, $code, $why") if $reject > 3;
189 return (DENY, "SPF - $code: $why") if $reject >= 6;
190 return (DENYSOFT, "SPF - $code: $why") if $reject > 3;
191 $self->log(LOGINFO, "fail, tolerated, $code, $why");
192 return (DECLINED);
193 }
194 if ($code eq 'temperror') {
195 $self->log(LOGINFO, "fail, $code, $why");
196 return (DENYSOFT, "SPF - $code: $why") if $reject >= 2;
197 $self->log(LOGINFO, "fail, tolerated, $code, $why");
198 return (DECLINED);
199 }
200
201 $self->log(LOGINFO, "SPF from $from was $code: $why");
202 return (DECLINED);
203 }
204
205 sub handle_code_none {
206 my ($self, $reject, $why) = @_;
207
208 if ($reject >= 6) {
209 $self->log(LOGINFO, "fail, none, $why");
210 return (DENY, "SPF - none: $why");
211 }
212
213 $self->log(LOGINFO, "skip, tolerated, none, $why");
214 return DECLINED;
215 }
216
217 sub handle_code_fail {
218 my ($self, $reject, $why) = @_;
219
220 $self->adjust_karma(-1);
221
222 if ($reject >= 2) {
223 $self->log(LOGINFO, "fail, $why");
224 return (DENY, "SPF - forgery: $why") if $reject >= 3;
225 return (DENYSOFT, "SPF - fail: $why");
226 }
227
228 $self->log(LOGINFO, "fail, tolerated, $why");
229 return DECLINED;
230 }
231
232 sub handle_code_softfail {
233 my ($self, $reject, $why) = @_;
234
235 $self->adjust_karma(-1);
236
237 if ($reject >= 3) {
238 $self->log(LOGINFO, "fail, soft, $why");
239 return (DENY, "SPF - fail: $why") if $reject >= 4;
240 return (DENYSOFT, "SPF - fail: $why") if $reject >= 3;
241 }
242
243 $self->log(LOGINFO, "fail, tolerated, soft, $why");
244 return DECLINED;
245 }
246
247 sub data_post_handler {
248 my ($self, $transaction) = @_;
249
250 my $result = $transaction->notes('spfquery') or return DECLINED;
251
252 # if we skipped processing in mail_handler, we should skip here too
253 return (DECLINED) if $self->is_immune();
254
255 $self->log(LOGDEBUG, "result was $result->code");
256
257 if (!$transaction->header) {
258 $self->log(LOGERROR, "missing headers!");
259 return DECLINED;
260 }
261
262 $transaction->header->add('Received-SPF', $result->received_spf_header, 0);
263
264 return DECLINED;
265 }
266
267 sub is_special_recipient {
268 my ($self, $rcpt) = @_;
269
270 if (!$rcpt) {
271 $self->log(LOGINFO, "skip: missing recipient");
272 return 1;
273 }
274 if (!$rcpt->user) {
275 $self->log(LOGINFO, "skip: missing user");
276 return 1;
277 }
278
279 # special addresses don't get SPF-tested.
280 if ($rcpt->user =~ /^(?:postmaster|abuse|mailer-daemon|root)$/i) {
281 $self->log(LOGINFO, "skip: special user (" . $rcpt->user . ")");
282 return 1;
283 }
284
285 return;
286 }