Debian Codegraph @

plugins/headers

raw
1 #!perl -w
2
3 =head1 NAME
4
5 headers - validate message headers
6
7 =head1 DESCRIPTION
8
9 Checks for missing or empty values in the From or Date headers.
10
11 Make sure no singular headers are duplicated. Singular headers are:
12
13 Date From Sender Reply-To To Cc Bcc
14 Message-Id In-Reply-To References Subject
15
16 Optionally test if the Date header is too many days in the past or future. If
17 I<future> or I<past> are not defined, they are not tested.
18
19 If the remote IP is whitelisted, header validation is skipped.
20
21 =head1 CONFIGURATION
22
23 The following optional settings exist:
24
25 =head2 require
26
27 headers require [ From | Date | From,Date | From,Date,Subject,Message-ID,Received ]
28
29 A comma separated list of headers to require.
30
31 Default: From
32
33 =head3 Requiring the Date header
34
35 As of 2012, requiring a valid date header will almost certainly cause the loss
36 of valid mail. The JavaMail sender used by some banks, photo processing
37 services, health insurance companies, bounce senders, and others do send
38 messages without a Date header. For this reason, and despite RFC 5322, the
39 default is not to require Date.
40
41 However, if the date header is present, and I<future> and/or I<past> are
42 defined, it will be validated.
43
44 =head2 future
45
46 The number of days in the future beyond which messages are invalid.
47
48 headers [ future 1 ]
49
50 =head2 past
51
52 The number of days in the past beyond which a message is invalid. The Date header is added by the MUA, so there are many valid reasons a message may have an older date in the header. It could have been delayed by the client, the sending server, connectivity problems, recipient server problem, recipient server configuration, etc. The I<past> setting should take those factors into consideration.
53
54 I would be surprised if a valid message ever had a date header older than a week.
55
56 headers [ past 5 ]
57
58 =head2 reject
59
60 Determine if the connection is denied. Use the I<reject 0> option when first enabling the plugin, and then watch your logs to see what would have been rejected. When you are no longer concerned that valid messages will be rejected, enable with I<reject 1>.
61
62 headers reject [ 0 | 1 ]
63
64 Default: 1
65
66 =head2 reject_type
67
68 Whether to issue a permanent or temporary rejection. The default is permanent.
69
70 headers reject_type [ temp | perm ]
71
72 Using a temporary rejection is a cautious way to enable rejections. It allows an administrator to watch for a trial period and assure no valid messages are rejected. If a deferral of valid mail is noticed, I<reject 0> can be set to permit the deferred message to be delivered.
73
74 Default: perm
75
76 =head2 loglevel
77
78 Adjust the quantity of logging for this plugin. See docs/logging.pod
79
80 =head1 TODO
81
82 =head1 SEE ALSO
83
84 https://tools.ietf.org/html/rfc5322
85
86 =head1 AUTHOR
87
88 2012 - Matt Simerson
89
90 =head1 ACKNOWLEDGEMENTS
91
92 based in part upon check_basicheaders by Jim Winstead Jr.
93
94 Singular headers idea from Haraka's data.rfc5322_header_checks.js by Steve Freegard
95
96 =cut
97
98 use strict;
99 use warnings;
100
101 use Qpsmtpd::Constants;
102
103 use Date::Parse qw(str2time);
104
105 my @required_headers = qw/ From /; # <- to be RFC 5322 compliant, add Date here
106
107 #my @should_headers = qw/ Message-ID /;
108 my @singular_headers = qw/ Date From Sender Reply-To To Cc Bcc
109 Message-Id In-Reply-To References
110 Subject /;
111
112 sub register {
113 my ($self, $qp) = (shift, shift);
114
115 $self->log(LOGWARN, "invalid arguments") if @_ % 2;
116 $self->{_args} = {@_};
117
118 $self->{_args}{reject_type} ||= 'perm'; # set default
119 if (!defined $self->{_args}{reject}) {
120 $self->{_args}{reject} = 1; # set default
121 }
122
123 if ($self->{_args}{require}) {
124 @required_headers = split /,/, $self->{_args}{require};
125 }
126 }
127
128 sub hook_data_post {
129 my ($self, $transaction) = @_;
130
131 if ($transaction->data_size == 0) {
132 return $self->get_reject("You must send some data first", "no data");
133 }
134
135 my $header = $transaction->header or do {
136 return $self->get_reject("Headers are missing", "missing headers");
137 };
138
139 return DECLINED if $self->is_immune();
140
141 my $errors = $self->has_required_headers( $header ) || 0;
142 $errors += $self->has_singular_headers( $header );
143
144 my $err_msg = $self->invalid_date_range();
145 if ($err_msg) {
146 return $self->get_reject($err_msg, $err_msg);
147 }
148
149 if ( $errors ) {
150 return $self->get_reject($self->get_reject_type(),
151 "RFC 5322 validation errors" );
152 };
153
154 $self->log(LOGINFO, 'pass');
155 return (DECLINED);
156 }
157
158 sub has_required_headers {
159 my ($self, $header) = @_;
160
161 my $errors = 0;
162 foreach my $h (@required_headers) {
163 next if $header->get($h);
164 $errors++;
165 $self->adjust_karma(-1);
166 $self->is_naughty(1) if $self->{args}{reject};
167 $self->store_deferred_reject("We require a valid $h header");
168 $self->log(LOGINFO, "fail, no $h header" );
169 }
170 return $errors;
171 };
172
173 sub has_singular_headers {
174 my ($self, $header) = @_;
175
176 my $errors = 0;
177 foreach my $h (@singular_headers) {
178 next if !$header->get($h); # doesn't exist
179 my @qty = $header->get($h);
180 next if @qty == 1; # only 1 header
181 $errors++;
182 $self->adjust_karma(-1);
183 $self->is_naughty(1) if $self->{args}{reject};
184 $self->store_deferred_reject(
185 "Only one $h header allowed. See RFC 5322, Section 3.6",
186 );
187 $self->log(LOGINFO, "fail, too many $h headers" );
188 }
189 return $errors;
190 };
191
192 sub invalid_date_range {
193 my $self = shift;
194
195 return if !$self->transaction->header;
196 my $date = shift || $self->transaction->header->get('Date') or return;
197 chomp $date;
198
199 my $msg_ts = str2time($date) or do {
200 $self->log(LOGINFO, "skip, date not parseable ($date)");
201 return;
202 };
203
204 my $past = $self->{_args}{past};
205 if ($past && $msg_ts < time - ($past * 24 * 3600)) {
206 $self->log(LOGINFO, "fail, date too old ($date)");
207 $self->adjust_karma(-1);
208 return "The Date header is too far in the past";
209 }
210
211 my $future = $self->{_args}{future};
212 if ($future && $msg_ts > time + ($future * 24 * 3600)) {
213 $self->log(LOGINFO, "fail, date in future ($date)");
214 $self->adjust_karma(-1);
215 return "The Date header is too far in the future";
216 }
217
218 return;
219 }
220
221 __END__
222
223 =head1 SMTP HEADERS
224
225 http://forum.unifiedemail.net/default.aspx?g=posts&t=68
226
227 =head2 From:
228
229 The eMail address, and optionally the name of the author(s). In many eMail clients not changeable except through changing account settings.
230
231 =head2 To:
232
233 The eMail address(es), and optionally name(s) of the message's recipient(s). Indicates primary recipients (multiple allowed), for secondary recipients see Cc: and Bcc: below.
234
235 =head2 Subject:
236
237 A brief summary of the topic of the message. Certain abbreviations are commonly used in the subject, including "RE:" and "FW:".
238
239 =head2 Date:
240
241 The local time and date when the message was written. Like the From: field, many email clients fill this in automatically when sending. The recipient's client may then display the time in the format and time zone local to him/her.
242
243 =head2 Message-ID:
244
245 Also an automatically generated field; used to prevent multiple delivery and for reference in In-Reply-To: (see below).
246
247 =head2 Bcc:
248
249 Blind Carbon Copy; addresses added to the SMTP delivery list but not (usually) listed in the message data, remaining invisible to other recipients.
250
251 =head2 Cc:
252
253 Carbon copy; Many eMail clients will mark eMail in your inbox differently depending on whether you are in the To: or Cc: list.
254
255 =head2 Content-Type:
256
257 Information about how the message is to be displayed, usually a MIME type.
258
259 =head2 In-Reply-To:
260
261 Message-ID of the message that this is a reply to. Used to link related messages together.
262
263 =head2 Precedence:
264
265 Commonly with values "bulk", "junk", or "list"; used to indicate that automated "vacation" or "out of office" responses should not be returned for this mail, e.g. to prevent vacation notices from being sent to all other subscribers of a mailinglist.
266
267 =head2 Received:
268
269 Tracking information generated by mail servers that have previously handled a message, in reverse order (last handler first).
270
271 =head2 References:
272
273 Message-ID of the message that this is a reply to, and the message-id of the message the previous was reply a reply to, etc.
274
275 =head2 Reply-To:
276
277 Address that should be used to reply to the message.
278
279 =head2 Sender:
280
281 Address of the actual sender acting on behalf of the author listed in the From: field (secretary, list manager, etc.).
282
283 =head2 Return-Path:
284
285 When the delivery SMTP server makes the "final delivery" of a message, it inserts a return-path line at the beginning of the mail data. Thisuse of return-path is required; mail systems MUST support it. The return-path line preserves the information in the from the MAIL command.
286
287 =head2 Error-To:
288
289 Indicates where error messages should be sent. In the absence of this line, they go to the Sender:, and absent that, the From: address.
290
291 =head2 X-*
292
293 No standard header field will ever begin with the characters "X-", so application developers are free to use them for their own purposes.
294
295 =cut
296