Quarantine/Spam.pm untested

filter: Statementstmt 0.0% 0 / 158 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 282 CC 28 SCAR 45.9
CC
28
Cov
0%
check_message_for_spam
210.0
results_in_headers
56.0
clamav
30.0
CRAP 98.5
1 # Copyright(C) 2006 David Muir Sharnoff <muir@idiom.com>
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 #
17 # This software is available without the GPL: please write if you need
18 # a non-GPL license. All submissions of patches must come with a
19 # copyright grant so that David Sharnoff remains able to change the
20 # license at will.
21  
22  
23 package Qpsmtpd::Plugin::Quarantine::Spam;
24  
25 use Qpsmtpd::Constants;
26 use Qpsmtpd::DSN;
27 use File::Slurp;
28 use Qpsmtpd::Plugin::Quarantine::Common;
29  
30 our @ISA = qw(Exporter);
31 our @EXPORT = qw(check_message_for_spam spam_init);
32  
33 use strict;
34  
35 my @spamd3;
36 my $headerrx = qr/\G(\S+):((?:.+(?=\n)|\n(?=\s))+)?\n/;
37 my %spamc_headers = (
38 'X-Spam-Status' => 1,
39 'X-Spam-Report' => 1,
40 );
41  
42 # -----------------------------------------------------------------------
43  
44  
45 tie my %q_shell, 'FuncT', sub {
46 my $file = shift;
47 return $file if $file =~ /^[-_\.+=:\/0-9a-zA-Z]+$/;
48 $file =~ s/'/'\\''/g;
49 "'$file'";
50 };
51  
52 my $myhostname = $Qpsmtpd::Plugin::Quarantine::Spam::myhostname;
53  
54 # -----------------------------------------------------------------------
55  
56 sub spam_init
57 {
58 my $total = 0;
59 map { $total += $defaults{spamd3}{$_} } keys %{$defaults{spamd3}};
60 my (%map) = map { $_ => rand($total)+$defaults{spamd3}{$_} } keys %{$defaults{spamd3}};
61 @spamd3 = sort { $map{$b} <=> $map{$a} } keys %map;
62 }
63  
64 sub check_message_for_spam
65 {
66 my ($qp, $transaction) = @_;
67  
68 my $headers = $transaction->header();
69 my $file = $transaction->body_filename();
70  
71 my $x = read_file($file);
72  
73 my %results;
74  
75 TEST:
76 for(;;) {
77 if (($defaults{clamd} || $defaults{clamav}) &&
78 ($headers->get('Content-Type') =~ /$defaults{virus_content}/
79 || $headers->get('Content-Disposition') =~ /$defaults{virus_content}/
80 || ($headers->get('Content-Type') =~ /multipart/
81 && $transaction->body_as_string() =~ /^Content-Type: $defaults{virus_content}/m)))
82 {
83 last if
84 clamav($qp, $transaction, \%results, ($defaults{clamd} || $defaults{clamav}), 'stream');
85 unless (exists $results{clamav}) {
86 last if
87 clamav($qp, $transaction, \%results, ($defaults{clamd} || $defaults{clamav}), $file);
88 }
89 }
90  
91 for my $spamd3 (@spamd3) {
92 last TEST if
93 spamc($qp, $transaction, \%results, $spamd3, 'SPAMASSASSIN3');
94 last if $results{spamc};
95 }
96  
97 last if $results{filtered};
98  
99 if ($defaults{clamd}) {
100 unless (exists $results{clamav}) {
101 last if
102 clamav($qp, $transaction, \%results, $defaults{clamd}, 'stream');
103 }
104 }
105  
106 if ($defaults{clamav}) {
107 unless (exists $results{clamav}) {
108 last if
109 clamav($qp, $transaction, \%results, $defaults{clamav}, $file);
110 }
111 }
112
113 last;
114 }
115 $qp->log(LOGDEBUG, "Filtered? $results{filterd}");
116 $qp->log(LOGDEBUG, "Details: $results{hlines}");
117 return $results{filtered};
118 }
119  
120 sub clamav
121 {
122 my ($qp, $transaction, $r, $command, $input) = @_;
123 my $results;
124 my $file = $transaction->body_filename();
125 eval {
126 local($SIG{__DIE__}) = 'DEFAULT';
127 local($SIG{ALRM}) = sub {
128 $qp->log(LOGWARN, "ClamAV timeout");
129 die "timeout\n";
130 };
131 alarm($defaults{subcommand_timeout}) if $defaults{subcommand_timeout};
132 $results = `$command $file`;
133 };
134 unless ($results =~ /-- SCAN SUMMARY --/ && $results =~ /^\Q$input\E: (OK|.*FOUND)\n/) {
135 $qp->log(LOGINFO, "ClamAV failed: $results");
136 $r->{clamav} = undef;
137 return 0;
138 }
139 my $return = 0;
140 my $status = $1;
141 if ($status =~ /\n\n/) {
142 $qp->log(LOGINFO, "ClamAV Failed: $results");
143 $r->{clamav} = undef;
144 return 0;
145 } elsif ($status eq "OK") {
146 $status = "X-ClamAV-Status: No\n";
147 } else {
148 $status = "X-ClamAV-Status: Yes, $status\n";
149 $r->{filtered} .= " CLAMAV";
150 $return = 1;
151 }
152 $r->{clamav} = $status;
153 $r->{hlines} .= $status;
154 return $return;
155 }
156  
157 sub results_in_headers
158 {
159 my ($qp, $transaction, $r, $command, $lookfor) = @_;
160 $qp->log(LOGDEBUG, "running $command...");
161 local($/) = "\n\n";
162 my $file = $transaction->body_filename();
163 unless (open(RESULTS, "$command < $file|")) {
164 $qp->log(LOGWARN, "Could not open $command: $!");
165 return '';
166 }
167 my $h = <RESULTS>;
168 $/ = "\n";
169 chomp($h);
170 close(RESULTS);
171  
172 my $fileheader = $transaction->header()->as_string();
173 my $fileheaderlen = length($fileheader);
174  
175 if (substr($h, 0, $fileheaderlen) eq $fileheader) {
176 substr($h, 0, $fileheaderlen) = '';
177 } elsif ($h =~ /^(From .*\n)/g) {
178 # weird, with qpsmtpd we don't have a "From " line
179 } elsif ($h =~ /^[A-Z][-\w]*:/) {
180 # that's good
181 } else {
182 $qp->log(LOGWARN, "Could not parse output from $command");
183 return '';
184 }
185 my $found = '';
186 my %found;
187 pos($h) = 0;
188 while ($h =~ /$headerrx/gco) {
189 my ($k, $v) = ($1, $2);
190 next unless $lookfor->{$k};
191 $found{$k} = $v;
192 $found .= "$k:$v\n";
193 }
194 if (pos($h) != length($h)) {
195 $qp->log(LOGWARN, "Unparsed header from $command");
196 return '';
197 }
198 return ($found, %found);
199 }
200  
201 sub spamc
202 {
203 my ($qp, $transaction, $r, $command, $tag) = @_;
204 my $file = $transaction->body_filename();
205 $qp->log(LOGDEBUG, "running $command...");
206 my $results;
207 eval {
208 local($SIG{__DIE__}) = 'DEFAULT';
209 local($SIG{ALRM}) = sub {
210 $qp->log(LOGWARN, "TIMEOUT $command $file");
211 die "timeout\n";
212 };
213 alarm($defaults{subcommand_timeout}) if $defaults{subcommand_timeout};
214 $results = `$command $file`;
215 };
216 $results =~ m!^(-?\d+\.\d+)/(\d+\.\d+)\n!;
217 my ($score, $thresh) = ($1 || '', $2 || '');
218 unless ($thresh && $thresh > 0 && ($results =~ s/.*^[- ]{25,300}\n//sm)) {
219 $results = substr($results, 0, 30);
220 $results =~ s/\n/\\n/g;
221 $qp->log(LOGDEBUG, "'$command $file' failed $thresh: $results");
222 return '';
223 }
224 my @tests;
225 my $report = '';
226 $thresh = 5.0;
227 while ($results =~ /\G {0,5}(-?\d+(?:\.\d+)?) (\S+)\s+(\S(?:.+(?=\n)|\n(?= {6}))+)\n/g) {
228 my ($pts, $rule, $desc) = ($1, $2, $3);
229 $desc =~ s/\n {5,60}(\S)/\n\t* $1/g;
230 push(@tests, $rule);
231 $report .= sprintf("\t* % 4s %s %s\n", $pts, $rule, $desc);
232 }
233 my $xss;
234 my $tests = join(',', @tests);
235 if ($score >= $thresh) {
236 $xss = "X-Spam-Status: Yes, hits=$score required=$thresh tests=$tests\n";
237 $r->{filtered} .= " $tag";
238 $report = "X-Spam-Report:\n$report";
239 } else {
240 $xss = "X-Spam-Status: No, hits=$score required=$thresh tests=$tests\n";
241 $report = "";
242 }
243 1 while $xss =~ s/^(.{60}.*?,)(?=.)/$1\n\t/m;
244 my $rout = "$xss$report";
245 if ($rout =~ /\n\n/) {
246 $qp->log(LOGWARN, "$command $file: failed doubleNL $results");
247 return '';
248 }
249 $r->{spamc} = $rout;
250 $r->{hlines} .= $rout;
251 return $report;
252 }
253  
254 1;