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 19     19   148 use strict;
  19         43  
  19         691  
87 19     19   117 use warnings;
  19         61  
  19         601  
88             # use bytes;
89 19     19   109 use re 'taint';
  19         51  
  19         568  
90              
91 19     19   113 use Mail::SpamAssassin::Plugin;
  19         45  
  19         419  
92 19     19   96 use Mail::SpamAssassin::Logger;
  19         51  
  19         1200  
93 19     19   135 use Mail::SpamAssassin::Util qw(untaint_var);
  19         54  
  19         982  
94              
95 19     19   128 use Errno qw(ENOENT EACCES);
  19         37  
  19         1155  
96 19     19   145 use Fcntl;
  19         43  
  19         5868  
97 19     19   147 use File::Path;
  19         46  
  19         1361  
98 19     19   140 use File::Basename;
  19         37  
  19         2260  
99              
100             BEGIN {
101 19         117 eval { require Digest::SHA; import Digest::SHA qw(sha1); 1 }
  19         582  
  19         1138  
102 19 50   19   80 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 19     19   152 use constant HAS_DB_FILE => eval { require DB_File; };
  19         46  
  19         42  
  19         29945  
108              
109             # constructor: register the eval rule
110             sub new {
111 60     60 1 257 my $class = shift;
112 60         174 my $mailsaobject = shift;
113              
114             # some boilerplate...
115 60   33     506 $class = ref($class) || $class;
116 60         366 my $self = $class->SUPER::new($mailsaobject);
117 60         180 bless ($self, $class);
118              
119 60         322 $self->register_eval_rule ("check_hashcash_value");
120 60         279 $self->register_eval_rule ("check_hashcash_double_spend");
121              
122 60         318 $self->set_config($mailsaobject->{conf});
123              
124 60         662 return $self;
125             }
126              
127             ###########################################################################
128              
129             sub set_config {
130 60     60 0 213 my($self, $conf) = @_;
131 60         149 my @cmds;
132              
133 60         351 push(@cmds, {
134             setting => 'use_hashcash',
135             default => 1,
136             type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
137             });
138              
139 60         296 push(@cmds, {
140             setting => 'hashcash_doublespend_path',
141             default => '__userstate__/hashcash_seen',
142             type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
143             });
144              
145 60         247 push(@cmds, {
146             setting => 'hashcash_doublespend_file_mode',
147             default => "0700",
148             type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
149             });
150              
151 60         287 push(@cmds, {
152             setting => 'hashcash_accept',
153             default => {},
154             type => $Mail::SpamAssassin::Conf::CONF_TYPE_ADDRLIST,
155             });
156              
157 60         300 $conf->{parser}->register_commands(\@cmds);
158             }
159              
160             ###########################################################################
161              
162             sub check_hashcash_value {
163 567     567 0 2684 my ($self, $scanner, $valmin, $valmax) = @_;
164 567         1604 my $val = $self->_run_hashcash($scanner);
165 567   33     10784 return ($val >= $valmin && $val < $valmax);
166             }
167              
168             sub check_hashcash_double_spend {
169 81     81 0 251 my ($self, $scanner) = @_;
170 81         303 $self->_run_hashcash($scanner);
171 81         1306 return ($scanner->{hashcash_double_spent});
172             }
173              
174             ############################################################################
175              
176             sub _run_hashcash {
177 648     648   1247 my ($self, $scanner) = @_;
178              
179 648 100       1668 if (defined $scanner->{hashcash_value}) { return $scanner->{hashcash_value}; }
  567         1297  
180              
181 81         228 $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 81         354 my @hdrs = $scanner->{msg}->get_header ("X-Hashcash");
192 81 50       371 if (scalar @hdrs == 0) {
193 81         333 @hdrs = $scanner->{msg}->get_header ("Hashcash");
194             }
195              
196 81         350 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 81         228 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