File Coverage

blib/lib/Mail/SpamAssassin/Plugin/Hashcash.pm
Criterion Covered Total %
statement 69 145 47.5
branch 4 46 8.7
condition 2 12 16.6
subroutine 17 20 85.0
pod 1 5 20.0
total 93 228 40.7


line stmt bran cond sub pod time code
1             # <@LICENSE>
2             # Licensed to the Apache Software Foundation (ASF) under one or more
3             # contributor license agreements. See the NOTICE file distributed with
4             # this work for additional information regarding copyright ownership.
5             # The ASF licenses this file to you under the Apache License, Version 2.0
6             # (the "License"); you may not use this file except in compliance with
7             # the License. You may obtain a copy of the License at:
8             #
9             # http://www.apache.org/licenses/LICENSE-2.0
10             #
11             # Unless required by applicable law or agreed to in writing, software
12             # distributed under the License is distributed on an "AS IS" BASIS,
13             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14             # See the License for the specific language governing permissions and
15             # limitations under the License.
16             # </@LICENSE>
17              
18             =head1 NAME
19              
20             Mail::SpamAssassin::Plugin::Hashcash - perform hashcash verification tests
21              
22             =head1 SYNOPSIS
23              
24             loadplugin Mail::SpamAssassin::Plugin::Hashcash
25              
26             =head1 DESCRIPTION
27              
28             Hashcash is a payment system for email where CPU cycles used as the
29             basis for an e-cash system. This plugin makes it possible to use valid
30             hashcash tokens added by mail programs as a bonus for messages.
31              
32             =cut
33              
34             =head1 USER SETTINGS
35              
36             =over 4
37              
38             =item use_hashcash { 1 | 0 } (default: 1)
39              
40             Whether to use hashcash, if it is available.
41              
42             =cut
43              
44             =item hashcash_accept user@example.com ...
45              
46             Used to specify addresses that we accept HashCash tokens for. You should set
47             it to match all the addresses that you may receive mail at.
48              
49             Like whitelist and blacklist entries, the addresses are file-glob-style
50             patterns, so C<friend@somewhere.com>, C<*@isp.com>, or C<*.domain.net> will all
51             work. Specifically, C<*> and C<?> are allowed, but all other metacharacters
52             are not. Regular expressions are not used for security reasons.
53              
54             The sequence C<%u> is replaced with the current user's username, which
55             is useful for ISPs or multi-user domains.
56              
57             Multiple addresses per line, separated by spaces, is OK. Multiple
58             C<hashcash_accept> lines is also OK.
59              
60             =cut
61              
62             =item hashcash_doublespend_path /path/to/file (default: ~/.spamassassin/hashcash_seen)
63              
64             Path for HashCash double-spend database. HashCash tokens are only usable once,
65             so their use is tracked in this database to avoid providing a loophole.
66              
67             By default, each user has their own, in their C<~/.spamassassin> directory with
68             mode 0700/0600. Note that once a token is 'spent' it is written to this file,
69             and double-spending of a hashcash token makes it invalid, so this is not
70             suitable for sharing between multiple users.
71              
72             =cut
73              
74             =item hashcash_doublespend_file_mode (default: 0700)
75              
76             The file mode bits used for the HashCash double-spend database file.
77              
78             Make sure you specify this using the 'x' mode bits set, as it may also be used
79             to create directories. However, if a file is created, the resulting file will
80             not have any execute bits set (the umask is set to 111).
81              
82             =cut
83              
84             package Mail::SpamAssassin::Plugin::Hashcash;
85              
86 22     22   149 use strict;
  22         47  
  22         758  
87 22     22   129 use warnings;
  22         44  
  22         808  
88             # use bytes;
89 22     22   137 use re 'taint';
  22         51  
  22         757  
90              
91 22     22   143 use Mail::SpamAssassin::Plugin;
  22         64  
  22         542  
92 22     22   135 use Mail::SpamAssassin::Logger;
  22         48  
  22         1445  
93 22     22   155 use Mail::SpamAssassin::Util qw(untaint_var);
  22         39  
  22         1292  
94              
95 22     22   165 use Errno qw(ENOENT EACCES);
  22         38  
  22         1749  
96 22     22   135 use Fcntl;
  22         59  
  22         5625  
97 22     22   175 use File::Path;
  22         45  
  22         1674  
98 22     22   181 use File::Basename;
  22         48  
  22         3322  
99              
100             BEGIN {
101 22         132 eval { require Digest::SHA; import Digest::SHA qw(sha1); 1 }
  22         1269  
  22         1261  
102 22 50   22   73 or do { require Digest::SHA1; import Digest::SHA1 qw(sha1) }
  0         0  
  0         0  
103             }
104              
105             our @ISA = qw(Mail::SpamAssassin::Plugin);
106              
107 22     22   150 use constant HAS_DB_FILE => eval { require DB_File; };
  22         42  
  22         63  
  22         33202  
108              
109             # constructor: register the eval rule
110             sub new {
111 63     63 1 213 my $class = shift;
112 63         163 my $mailsaobject = shift;
113              
114             # some boilerplate...
115 63   33     451 $class = ref($class) || $class;
116 63         342 my $self = $class->SUPER::new($mailsaobject);
117 63         198 bless ($self, $class);
118              
119 63         299 $self->register_eval_rule ("check_hashcash_value");
120 63         212 $self->register_eval_rule ("check_hashcash_double_spend");
121              
122 63         309 $self->set_config($mailsaobject->{conf});
123              
124 63         630 return $self;
125             }
126              
127             ###########################################################################
128              
129             sub set_config {
130 63     63 0 189 my($self, $conf) = @_;
131 63         128 my @cmds;
132              
133 63         356 push(@cmds, {
134             setting => 'use_hashcash',
135             default => 1,
136             type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
137             });
138              
139 63         334 push(@cmds, {
140             setting => 'hashcash_doublespend_path',
141             default => '__userstate__/hashcash_seen',
142             type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
143             });
144              
145 63         286 push(@cmds, {
146             setting => 'hashcash_doublespend_file_mode',
147             default => "0700",
148             type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
149             });
150              
151 63         246 push(@cmds, {
152             setting => 'hashcash_accept',
153             default => {},
154             type => $Mail::SpamAssassin::Conf::CONF_TYPE_ADDRLIST,
155             });
156              
157 63         292 $conf->{parser}->register_commands(\@cmds);
158             }
159              
160             ###########################################################################
161              
162             sub check_hashcash_value {
163 539     539 0 1237 my ($self, $scanner, $valmin, $valmax) = @_;
164 539         1212 my $val = $self->_run_hashcash($scanner);
165 539   33     8866 return ($val >= $valmin && $val < $valmax);
166             }
167              
168             sub check_hashcash_double_spend {
169 77     77 0 247 my ($self, $scanner) = @_;
170 77         288 $self->_run_hashcash($scanner);
171 77         1159 return ($scanner->{hashcash_double_spent});
172             }
173              
174             ############################################################################
175              
176             sub _run_hashcash {
177 616     616   965 my ($self, $scanner) = @_;
178              
179 616 100       1310 if (defined $scanner->{hashcash_value}) { return $scanner->{hashcash_value}; }
  539         1151  
180              
181 77         253 $scanner->{hashcash_value} = 0;
182              
183             # X-Hashcash: 0:031118:camram-spam@camram.org:c068b58ade6dcbaf
184             # or:
185             # X-hashcash: 1:20:040803:hashcash@freelists.org::6dcdb3a3ad4e1b86:1519d
186             # X-hashcash: 1:20:040803:jm@jmason.org::6b484d06469ccb28:8838a
187             # X-hashcash: 1:20:040803:adam@cypherspace.org::a1cbc54bf0182ea8:5d6a0
188              
189             # call down to {msg} so that we can get it as an array of
190             # individual headers
191 77         315 my @hdrs = $scanner->{msg}->get_header ("X-Hashcash");
192 77 50       342 if (scalar @hdrs == 0) {
193 77         300 @hdrs = $scanner->{msg}->get_header ("Hashcash");
194             }
195              
196 77         330 foreach my $hc (@hdrs) {
197 0         0 my $value = $self->_run_hashcash_for_one_string($scanner, $hc);
198 0 0       0 if ($value) {
199             # remove the "double-spend" bool if we did find a usable string;
200             # this happens when one string is already spent, but another
201             # string has not yet been.
202 0         0 delete $scanner->{hashcash_double_spent};
203 0         0 return $value;
204             }
205             }
206 77         223 return 0;
207             }
208              
209             sub _run_hashcash_for_one_string {
210 0     0     my ($self, $scanner, $hc) = @_;
211              
212 0 0         if (!$hc) { return 0; }
  0            
213 0           $hc =~ s/\s+//gs; # remove whitespace from multiline, folded tokens
214              
215             # untaint the string for paranoia, making sure not to allow \n \0 \' \"
216 0 0         if ($hc =~ /^[-A-Za-z0-9\xA0-\xFF:_\/\%\@\.\,\= \*\+\;]+$/) {
217 0           $hc = untaint_var($hc);
218             }
219 0 0         if (!$hc) { return 0; }
  0            
220              
221 0           my ($ver, $bits, $date, $rsrc, $exts, $rand, $trial);
222 0 0         if ($hc =~ /^0:/) {
    0          
223 0           ($ver, $date, $rsrc, $trial) = split (/:/, $hc, 4);
224             }
225             elsif ($hc =~ /^1:/) {
226 0           ($ver, $bits, $date, $rsrc, $exts, $rand, $trial) =
227             split (/:/, $hc, 7);
228             # extensions are, as yet, unused by SpamAssassin
229             }
230             else {
231 0           dbg("hashcash: version $ver stamps not yet supported");
232 0           return 0;
233             }
234              
235 0 0         if (!$trial) {
236 0           dbg("hashcash: no trial in stamp '$hc'");
237 0           return 0;
238             }
239              
240 0           my $accept = $scanner->{conf}->{hashcash_accept};
241 0 0         if (!$self->_check_hashcash_resource ($scanner, $accept, $rsrc)) {
242 0           dbg("hashcash: resource $rsrc not accepted here");
243 0           return 0;
244             }
245              
246             # get the hash collision from the token. Computing the hash collision
247             # is very easy (great!) -- just get SHA1(token) and count the 0 bits at
248             # the start of the SHA1 hash, according to the draft at
249             # http://www.hashcash.org/draft-hashcash.txt .
250 0           my $value = 0;
251 0           my $bitstring = unpack ("B*", sha1($hc));
252 0 0         $bitstring =~ /^(0+)/ and $value = length $1;
253              
254             # hashcash v1 tokens: if the "claimed value" of the token is less than
255             # what the token actually contains (ie. token was accidentally generated
256             # with 24 bits instead of the claimed 20), then cut it down to just the
257             # claimed value. that way it's a bit tidier and more deterministic.
258 0 0 0       if ($bits && $value > $bits) {
259 0           $value = $bits;
260             }
261              
262 0           dbg("hashcash: token value: $value");
263              
264 0 0         if ($self->was_hashcash_token_double_spent ($scanner, $hc)) {
265 0           $scanner->{hashcash_double_spent} = 1;
266 0           return 0;
267             }
268              
269 0           $scanner->{hashcash_value} = $value;
270 0           return $value;
271             }
272              
273             sub was_hashcash_token_double_spent {
274 0     0 0   my ($self, $scanner, $token) = @_;
275              
276 0           my $main = $self->{main};
277 0 0         if (!$main->{conf}->{hashcash_doublespend_path}) {
278 0           dbg("hashcash: hashcash_doublespend_path not defined or empty");
279 0           return 0;
280             }
281 0 0         if (!HAS_DB_FILE) {
282 0           dbg("hashcash: DB_File module not installed, cannot use double-spend db");
283 0           return 0;
284             }
285              
286 0           my $path = $main->sed_path ($main->{conf}->{hashcash_doublespend_path});
287 0           my $parentdir = dirname ($path);
288 0 0         my $stat_errn = stat($parentdir) ? 0 : 0+$!;
289 0 0 0       if ($stat_errn == 0 && !-d _) {
    0          
290 0           dbg("hashcash: parent dir $parentdir exists but is not a directory");
291             } elsif ($stat_errn == ENOENT) {
292             # run in an eval(); if mkpath has no perms, it calls die()
293 0           eval {
294 0           mkpath ($parentdir, 0, (oct ($main->{conf}->{hashcash_doublespend_file_mode}) & 0777));
295             };
296             }
297              
298 0           my %spenddb;
299 0 0         if (!tie %spenddb, "DB_File", $path, O_RDWR|O_CREAT,
300             (oct ($main->{conf}->{hashcash_doublespend_file_mode}) & 0666))
301             {
302 0           dbg("hashcash: failed to tie to $path: $@ $!");
303             # not a serious error. TODO?
304 0           return 0;
305             }
306              
307 0 0         if (exists $spenddb{$token}) {
308 0           untie %spenddb;
309 0           dbg("hashcash: token '$token' spent already");
310 0           return 1;
311             }
312              
313 0           $spenddb{$token} = time;
314 0           dbg("hashcash: marking token '$token' as spent");
315              
316             # TODO: expiry?
317              
318 0           untie %spenddb;
319              
320 0           return 0;
321             }
322              
323             sub _check_hashcash_resource {
324 0     0     my ($self, $scanner, $list, $addr) = @_;
325 0           $addr = lc $addr;
326 0 0         if (defined ($list->{$addr})) { return 1; }
  0            
327 0           study $addr; # study is a no-op since perl 5.16.0, eliminating related bugs
328              
329 0           foreach my $regexp (values %{$list})
  0            
330             {
331             # allow %u == current username
332             # \\ is added by $conf->add_to_addrlist()
333 0           $regexp =~ s/\\\%u/$scanner->{main}->{username}/gs;
334              
335 0 0         if ($addr =~ /$regexp/i) {
336 0           return 1;
337             }
338             }
339              
340             # TODO: use "To" and "Cc" addresses gleaned from the mails in the Bayes
341             # database trained as ham, as well.
342              
343 0           return 0;
344             }
345              
346             ############################################################################
347              
348             1;
349              
350             =back
351              
352             =cut