|
Lines 1-1471
Link Here
|
| 1 |
--- lib/Mail/SpamAssassin/Plugin/DCC.pm 2011-06-06 19:59:17.000000000 -0400 |
|
|
| 2 |
+++ lib/Mail/SpamAssassin/Plugin/DCC.pm 2011-11-26 07:22:36.000000000 -0500 |
| 3 |
@@ -15,6 +15,20 @@ |
| 4 |
# limitations under the License. |
| 5 |
# </@LICENSE> |
| 6 |
|
| 7 |
+# Changes since SpamAssassin 3.3.2: |
| 8 |
+# support for DCC learning. See dcc_learn_score. |
| 9 |
+# deal with orphan dccifd sockets |
| 10 |
+# use `cdcc -q` to not stall waiting to find a DCC server when deciding |
| 11 |
+# whether DCC checks are enabled |
| 12 |
+# use dccproc -Q or dccifd query if a pre-existing X-DCC header shows |
| 13 |
+# the message has already been reported |
| 14 |
+# dccproc now uses -w /var/dcc/whiteclnt so it acts more like dccifd |
| 15 |
+# warn about the use of ancient versions of dccproc and dccifd |
| 16 |
+# turn off dccifd greylisting |
| 17 |
+# query instead of reporting mail messages that contain X-DCC headers and |
| 18 |
+# and so has probably already been reported |
| 19 |
+# try harder to find dccproc and cdcc when not explicitly configured |
| 20 |
+ |
| 21 |
=head1 NAME |
| 22 |
|
| 23 |
Mail::SpamAssassin::Plugin::DCC - perform DCC check of messages |
| 24 |
@@ -30,30 +44,31 @@ |
| 25 |
|
| 26 |
The DCC or Distributed Checksum Clearinghouse is a system of servers |
| 27 |
collecting and counting checksums of millions of mail messages. |
| 28 |
-TheSpamAssassin.pm counts can be used by SpamAssassin to detect and |
| 29 |
-reject or filter spam. |
| 30 |
- |
| 31 |
-Because simplistic checksums of spam can be easily defeated, the main |
| 32 |
-DCC checksums are fuzzy and ignore aspects of messages. The fuzzy |
| 33 |
-checksums are changed as spam evolves. |
| 34 |
+The counts can be used by SpamAssassin to detect and filter spam. |
| 35 |
|
| 36 |
-Note that DCC is disabled by default in C<init.pre> because it is not |
| 37 |
-open source. See the DCC license for more details. |
| 38 |
+See http://www.dcc-servers.net/dcc/ for more information about DCC. |
| 39 |
|
| 40 |
-See http://www.rhyolite.com/anti-spam/dcc/ for more information about |
| 41 |
-DCC. |
| 42 |
+Note that DCC is disabled by default in C<v310.pre> because its use requires |
| 43 |
+software that is not distributed with SpamAssassin and that has license |
| 44 |
+restrictions for certain commercial uses. |
| 45 |
+See the DCC license at http://www.dcc-servers.net/dcc/LICENSE for details. |
| 46 |
+ |
| 47 |
+Enable it by uncommenting the "loadplugin Mail::SpamAssassin::Plugin::DCC" |
| 48 |
+confdir/v310.pre or by adding this line to your local.pre. It might also |
| 49 |
+be necessary to install a DCC package, port, rpm, or equivalent from your |
| 50 |
+operating system distributor or a tarball from the primary DCC source |
| 51 |
+at http://www.dcc-servers.net/dcc/#download |
| 52 |
+See also http://www.dcc-servers.net/dcc/INSTALL.html |
| 53 |
|
| 54 |
=head1 TAGS |
| 55 |
|
| 56 |
The following tags are added to the set, available for use in reports, |
| 57 |
header fields, other plugins, etc.: |
| 58 |
|
| 59 |
- _DCCB_ DCC server ID in a response |
| 60 |
- _DCCR_ response from DCC - header field body in X-DCC-*-Metrics |
| 61 |
- _DCCREP_ response from DCC - DCC reputation in percents (0..100) |
| 62 |
- |
| 63 |
-Tag _DCCREP_ provides a nonempty value only with commercial DCC systems. |
| 64 |
-This is the percentage of spam vs. ham sent from the first untrusted relay. |
| 65 |
+ _DCCB_ DCC server ID in X-DCC-*-Metrics header field name |
| 66 |
+ _DCCR_ X-DCC-*-Metrics header field body |
| 67 |
+ _DCCREP_ DCC Reputation or percent bulk mail (0..100) from |
| 68 |
+ commercial DCC software |
| 69 |
|
| 70 |
=cut |
| 71 |
|
| 72 |
@@ -75,8 +90,6 @@ |
| 73 |
use vars qw(@ISA); |
| 74 |
@ISA = qw(Mail::SpamAssassin::Plugin); |
| 75 |
|
| 76 |
-use vars qw($have_inet6); |
| 77 |
- |
| 78 |
sub new { |
| 79 |
my $class = shift; |
| 80 |
my $mailsaobject = shift; |
| 81 |
@@ -87,7 +100,7 @@ |
| 82 |
|
| 83 |
# are network tests enabled? |
| 84 |
if ($mailsaobject->{local_tests_only}) { |
| 85 |
- $self->{dcc_disabled} = 1; |
| 86 |
+ $self->{use_dcc} = 0; |
| 87 |
dbg("dcc: local tests only, disabling DCC"); |
| 88 |
} |
| 89 |
else { |
| 90 |
@@ -128,20 +141,23 @@ |
| 91 |
|
| 92 |
=item dcc_fuz2_max NUMBER |
| 93 |
|
| 94 |
-This option sets how often a message's body/fuz1/fuz2 checksum must have been |
| 95 |
-reported to the DCC server before SpamAssassin will consider the DCC check as |
| 96 |
-matched. |
| 97 |
- |
| 98 |
-As nearly all DCC clients are auto-reporting these checksums, you should set |
| 99 |
-this to a relatively high value, e.g. C<999999> (this is DCC's MANY count). |
| 100 |
+Sets how often a message's body/fuz1/fuz2 checksum must have been reported |
| 101 |
+to the DCC server before SpamAssassin will consider the DCC check hit. |
| 102 |
+C<999999> is DCC's MANY count. |
| 103 |
|
| 104 |
The default is C<999999> for all these options. |
| 105 |
|
| 106 |
=item dcc_rep_percent NUMBER |
| 107 |
|
| 108 |
-Only commercial DCC systems provide DCC reputation information. This is the |
| 109 |
-percentage of spam vs. ham sent from the first untrusted relay. It will hit |
| 110 |
-on new spam from spam sources. Default is C<90>. |
| 111 |
+Only the commercial DCC software provides DCC Reputations. A DCC Reputation |
| 112 |
+is the percentage of bulk mail received from the last untrusted relay in the |
| 113 |
+path taken by a mail message as measured by all commercial DCC installations. |
| 114 |
+See http://www.rhyolite.com/dcc/reputations.html |
| 115 |
+You C<must> whitelist your trusted relays or MX servers with MX or |
| 116 |
+MXDCC lines in /var/dcc/whiteclnt as described in the main DCC man page |
| 117 |
+to avoid seeing your own MX servers as sources of bulk mail. |
| 118 |
+See http://www.dcc-servers.net/dcc/dcc-tree/dcc.html#White-and-Blacklists |
| 119 |
+The default is C<90>. |
| 120 |
|
| 121 |
=cut |
| 122 |
|
| 123 |
@@ -189,13 +205,9 @@ |
| 124 |
=item dcc_home STRING |
| 125 |
|
| 126 |
This option tells SpamAssassin where to find the dcc homedir. |
| 127 |
-If not given, it will try to get dcc to specify one, and if that fails it |
| 128 |
-will try dcc's own default homedir of '/var/dcc'. |
| 129 |
-If C<dcc_path> is not specified, it will default to looking in |
| 130 |
-C<dcc_home/bin> for dcc client instead of relying on SpamAssassin to find it |
| 131 |
-in the current PATH. If it isn't found there, it will look in the current |
| 132 |
-PATH. If a C<dccifd> socket is found in C<dcc_home> or specified explicitly, |
| 133 |
-it will use that interface instead of C<dccproc>. |
| 134 |
+If not specified, try to use the locally configured directory |
| 135 |
+from the C<cdcc homedir> command. |
| 136 |
+Try /var/dcc if that command fails. |
| 137 |
|
| 138 |
=cut |
| 139 |
|
| 140 |
@@ -205,7 +217,7 @@ |
| 141 |
type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING, |
| 142 |
code => sub { |
| 143 |
my ($self, $key, $value, $line) = @_; |
| 144 |
- if (!defined $value || !length $value) { |
| 145 |
+ if (!defined $value || $value eq '') { |
| 146 |
return $Mail::SpamAssassin::Conf::MISSING_REQUIRED_VALUE; |
| 147 |
} |
| 148 |
$value = untaint_file_path($value); |
| 149 |
@@ -223,14 +235,16 @@ |
| 150 |
|
| 151 |
=item dcc_dccifd_path STRING |
| 152 |
|
| 153 |
-This option tells SpamAssassin where to find the dccifd socket. If |
| 154 |
-C<dcc_dccifd_path> is not specified, it will default to looking for a socket |
| 155 |
-named C<dccifd> in a directory C<dcc_home>. The C<dcc_dccifd_path> can be |
| 156 |
-a Unix socket name (absolute path), or an INET socket specification in a form |
| 157 |
-C<[host]:port> or C<host:port>, where a host can be an IPv4 or IPv6 address |
| 158 |
-or a host name, and port is a TCP port number. In case of an IPv6 address the |
| 159 |
-brackets are required syntax. If a C<dccifd> socket is found, the plugin will |
| 160 |
-use it instead of C<dccproc>. |
| 161 |
+This option tells SpamAssassin where to find the dccifd socket instead |
| 162 |
+of a local Unix socket named C<dccifd> in the C<dcc_home> directory. |
| 163 |
+If a socket is specified or found, use it instead of C<dccproc>. |
| 164 |
+ |
| 165 |
+If specifed, C<dcc_dccifd_path> is the absolute path of local Unix socket |
| 166 |
+or an INET socket specified as C<[Host]:Port> or C<Host:Port>. |
| 167 |
+Host can be an IPv4 or IPv6 address or a host name |
| 168 |
+Port is a TCP port number. The brackets are required for an IPv6 address. |
| 169 |
+ |
| 170 |
+The default is C<undef>. |
| 171 |
|
| 172 |
=cut |
| 173 |
|
| 174 |
@@ -240,45 +254,60 @@ |
| 175 |
type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING, |
| 176 |
code => sub { |
| 177 |
my ($self, $key, $value, $line) = @_; |
| 178 |
- $value = '' if !defined $value; |
| 179 |
- $self->{dcc_dccifd_path_raw} = $value; # for logging purposes |
| 180 |
- undef $self->{dcc_dccifd_host}; |
| 181 |
- undef $self->{dcc_dccifd_port}; |
| 182 |
- undef $self->{dcc_dccifd_socket}; |
| 183 |
- local($1,$2,$3); |
| 184 |
- if ($value eq '') { |
| 185 |
+ |
| 186 |
+ if (!defined $value || $value eq '') { |
| 187 |
return $Mail::SpamAssassin::Conf::MISSING_REQUIRED_VALUE; |
| 188 |
- } elsif ($value =~ m{^ (?: \[ ([^\]]*) \] | ([^:]*) ) : ([^:]*) \z}sx) { |
| 189 |
- # "[host]:port" or "host:port", where a host can be an IPv4 or IPv6 |
| 190 |
- # address or a host name, and port is a TCP port number or service name |
| 191 |
- my $host = defined $1 ? $1 : $2; |
| 192 |
- my $port = $3; |
| 193 |
- $self->{dcc_dccifd_host} = untaint_var($host); |
| 194 |
- $self->{dcc_dccifd_port} = untaint_var($port); |
| 195 |
- dbg("config: dcc_dccifd_path set to [%s]:%s", $host,$port); |
| 196 |
- } else { # assume a unix socket |
| 197 |
+ } |
| 198 |
+ |
| 199 |
+ local($1,$2,$3); |
| 200 |
+ if ($value =~ m{^ (?: \[ ([^\]]*) \] | ([^:]*) ) : ([^:]*) \z}sx) { |
| 201 |
+ my $host = untaint_var(defined $1 ? $1 : $2); |
| 202 |
+ my $port = untaint_var($3); |
| 203 |
+ if (!$host) { |
| 204 |
+ info("config: missing or bad host name in dcc_dccifd_path '$value'"); |
| 205 |
+ return $Mail::SpamAssassin::Conf::INVALID_VALUE; |
| 206 |
+ } |
| 207 |
+ if (!$port || $port !~ /^\d+\z/ || $port < 1 || $port > 65535) { |
| 208 |
+ info("config: bad TCP port number in dcc_dccifd_path '$value'"); |
| 209 |
+ return $Mail::SpamAssassin::Conf::INVALID_VALUE; |
| 210 |
+ } |
| 211 |
+ |
| 212 |
+ $self->{dcc_dccifd_host} = $host; |
| 213 |
+ $self->{dcc_dccifd_port} = $port; |
| 214 |
+ if ($host !~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/) { |
| 215 |
+ # remember to try IPv6 if we can with a host name or non-IPv4 address |
| 216 |
+ $self->{dcc_dccifd_IPv6} = eval { require IO::Socket::INET6 }; |
| 217 |
+ } |
| 218 |
+ dbg("config: dcc_dccifd_path set to [%s]:%s", $host, $port); |
| 219 |
+ |
| 220 |
+ } else { |
| 221 |
+ # assume a unix socket |
| 222 |
if ($value !~ m{^/}) { |
| 223 |
- info("config: dcc_dccifd_path should be an absolute socket path"); |
| 224 |
+ info("config: dcc_dccifd_path '$value' is not an absolute path"); |
| 225 |
# return $Mail::SpamAssassin::Conf::INVALID_VALUE; # abort or accept? |
| 226 |
} |
| 227 |
$value = untaint_file_path($value); |
| 228 |
- # test disabled, dccifd may not yet be running at spamd startup time |
| 229 |
- # if (!-S $value) { |
| 230 |
- # info("config: dcc_dccifd_path '$value' isn't a local socket"); |
| 231 |
- # return $Mail::SpamAssassin::Conf::INVALID_VALUE; |
| 232 |
- # } |
| 233 |
+ |
| 234 |
$self->{dcc_dccifd_socket} = $value; |
| 235 |
dbg("config: dcc_dccifd_path set to local socket %s", $value); |
| 236 |
+ dbg("dcc: dcc_dccifd_path set to local socket %s", $value); |
| 237 |
} |
| 238 |
+ |
| 239 |
+ $self->{dcc_dccifd_path_raw} = $value; |
| 240 |
} |
| 241 |
}); |
| 242 |
|
| 243 |
=item dcc_path STRING |
| 244 |
|
| 245 |
-This option tells SpamAssassin specifically where to find the C<dccproc> |
| 246 |
-client instead of relying on SpamAssassin to find it in the current PATH. |
| 247 |
-Note that if I<taint mode> is enabled in the Perl interpreter, you should |
| 248 |
-use this, as the current PATH will have been cleared. |
| 249 |
+Where to find the C<dccproc> client program instead of relying on SpamAssassin |
| 250 |
+to find it in the current PATH or C<dcc_home/bin>. This must often be set, |
| 251 |
+because the current PATH is cleared by I<taint mode> in the Perl interpreter, |
| 252 |
+ |
| 253 |
+If a C<dccifd> socket is found in C<dcc_home> or specified explicitly |
| 254 |
+with C<dcc_dccifd_path>, use the C<dccifd(8)> interface instead of C<dccproc>. |
| 255 |
+ |
| 256 |
+The default is C<undef>. |
| 257 |
+ |
| 258 |
|
| 259 |
=cut |
| 260 |
|
| 261 |
@@ -289,12 +318,12 @@ |
| 262 |
type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING, |
| 263 |
code => sub { |
| 264 |
my ($self, $key, $value, $line) = @_; |
| 265 |
- if (!defined $value || !length $value) { |
| 266 |
+ if (!defined $value || $value eq '') { |
| 267 |
return $Mail::SpamAssassin::Conf::MISSING_REQUIRED_VALUE; |
| 268 |
} |
| 269 |
$value = untaint_file_path($value); |
| 270 |
if (!-x $value) { |
| 271 |
- info("config: dcc_path '$value' isn't an executable"); |
| 272 |
+ info("config: dcc_path '$value' is not executable"); |
| 273 |
return $Mail::SpamAssassin::Conf::INVALID_VALUE; |
| 274 |
} |
| 275 |
|
| 276 |
@@ -304,7 +333,7 @@ |
| 277 |
|
| 278 |
=item dcc_options options |
| 279 |
|
| 280 |
-Specify additional options to the dccproc(8) command. Please note that only |
| 281 |
+Specify additional options to the dccproc(8) command. Only |
| 282 |
characters in the range [0-9A-Za-z ,._/-] are allowed for security reasons. |
| 283 |
|
| 284 |
The default is C<undef>. |
| 285 |
@@ -319,6 +348,7 @@ |
| 286 |
code => sub { |
| 287 |
my ($self, $key, $value, $line) = @_; |
| 288 |
if ($value !~ m{^([0-9A-Za-z ,._/-]+)$}) { |
| 289 |
+ info("config: dcc_options '$value' contains impermissible characters"); |
| 290 |
return $Mail::SpamAssassin::Conf::INVALID_VALUE; |
| 291 |
} |
| 292 |
$self->{dcc_options} = $1; |
| 293 |
@@ -327,8 +357,9 @@ |
| 294 |
|
| 295 |
=item dccifd_options options |
| 296 |
|
| 297 |
-Specify additional options to send to the dccifd(8) daemon. Please note that only |
| 298 |
-characters in the range [0-9A-Za-z ,._/-] are allowed for security reasons. |
| 299 |
+Specify additional options to send to the dccifd daemon with |
| 300 |
+the ASCII protocol described on the dccifd(8) man page. |
| 301 |
+Only characters in the range [0-9A-Za-z ,._/-] are allowed for security reasons. |
| 302 |
|
| 303 |
The default is C<undef>. |
| 304 |
|
| 305 |
@@ -342,265 +373,306 @@ |
| 306 |
code => sub { |
| 307 |
my ($self, $key, $value, $line) = @_; |
| 308 |
if ($value !~ m{^([0-9A-Za-z ,._/-]+)$}) { |
| 309 |
+ info("config: dccifd_options '$value' contains impermissible characters"); |
| 310 |
return $Mail::SpamAssassin::Conf::INVALID_VALUE; |
| 311 |
} |
| 312 |
$self->{dccifd_options} = $1; |
| 313 |
} |
| 314 |
}); |
| 315 |
|
| 316 |
+=item dcc_learn_score n (default: undef) |
| 317 |
+ |
| 318 |
+Report messages with total scores this much larger than the |
| 319 |
+SpamAssassin spam threshold to DCC as spam. |
| 320 |
+ |
| 321 |
+=cut |
| 322 |
+ |
| 323 |
+ push (@cmds, { |
| 324 |
+ setting => 'dcc_learn_score', |
| 325 |
+ is_admin => 1, |
| 326 |
+ default => undef, |
| 327 |
+ type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC, |
| 328 |
+ }); |
| 329 |
+ |
| 330 |
$conf->{parser}->register_commands(\@cmds); |
| 331 |
} |
| 332 |
|
| 333 |
+ |
| 334 |
+ |
| 335 |
+ |
| 336 |
+sub ck_dir { |
| 337 |
+ my ($self, $dir, $tgt, $src) = @_; |
| 338 |
+ |
| 339 |
+ $dir = untaint_file_path($dir); |
| 340 |
+ if (!stat($dir)) { |
| 341 |
+ my $dir_errno = 0+$!; |
| 342 |
+ if ($dir_errno == ENOENT) { |
| 343 |
+ dbg("dcc: $tgt $dir from $src does not exist"); |
| 344 |
+ } else { |
| 345 |
+ dbg("dcc: $tgt $dir from $src is not accessible: $!"); |
| 346 |
+ } |
| 347 |
+ return; |
| 348 |
+ } |
| 349 |
+ if (!-d _) { |
| 350 |
+ dbg("dcc: $tgt $dir from $src is not a directory"); |
| 351 |
+ return; |
| 352 |
+ } |
| 353 |
+ |
| 354 |
+ $self->{main}->{conf}->{$tgt} = $dir; |
| 355 |
+ dbg("dcc: use '$tgt $dir' from $src"); |
| 356 |
+} |
| 357 |
+ |
| 358 |
sub find_dcc_home { |
| 359 |
my ($self) = @_; |
| 360 |
+ my $dcc_libexec; |
| 361 |
+ |
| 362 |
+ # just once |
| 363 |
+ return if defined $self->{dcc_version}; |
| 364 |
+ $self->{dcc_version} = '?'; |
| 365 |
|
| 366 |
my $conf = $self->{main}->{conf}; |
| 367 |
- return if !$conf->{use_dcc}; |
| 368 |
|
| 369 |
- my $dcchome = $conf->{dcc_home} || ''; |
| 370 |
|
| 371 |
- # If we're not given the DCC homedir, try getting DCC to tell us it. |
| 372 |
- # If that fails, try the DCC default homedir of '/var/dcc'. |
| 373 |
- if ($dcchome eq '') { |
| 374 |
+ # Get the DCC software version for talking to dccifd and formating the |
| 375 |
+ # dccifd options and the built-in DCC homedir. Use -q to prevent delays. |
| 376 |
+ my $cdcc_home; |
| 377 |
+ my $cdcc = $self->dcc_pgm_path('cdcc'); |
| 378 |
+ my $cmd = '-qV homedir libexecdir'; |
| 379 |
+ if ($cdcc && open(CDCC, "$cdcc $cmd 2>&1 |")) { |
| 380 |
+ my $cdcc_output = do { local $/ = undef; <CDCC> }; |
| 381 |
+ close CDCC; |
| 382 |
|
| 383 |
- my $cdcc = Mail::SpamAssassin::Util::find_executable_in_env_path('cdcc'); |
| 384 |
+ $cdcc_output =~ s/\n/ /g; # everything in 1 line for debugging |
| 385 |
+ dbg("dcc: `%s %s` reports '%s'", $cdcc, $cmd, $cdcc_output); |
| 386 |
+ $self->{dcc_version} = ($cdcc_output =~ /^(\d+\.\d+\.\d+)/) ? $1 : ''; |
| 387 |
+ $cdcc_home = ($cdcc_output =~ /\s+homedir=(\S+)/) ? $1 : ''; |
| 388 |
+ if ($cdcc_output =~ /\s+libexecdir=(\S+)/) { |
| 389 |
+ $self->ck_dir($1, 'dcc_libexec', 'cdcc'); |
| 390 |
+ } |
| 391 |
+ } |
| 392 |
|
| 393 |
- my $cdcc_home = ''; |
| 394 |
- if ($cdcc && -x $cdcc && open(CDCC, "$cdcc homedir 2>&1|")) { |
| 395 |
- dbg("dcc: dcc_home not set, querying cdcc utility"); |
| 396 |
- $cdcc_home = <CDCC> || ''; |
| 397 |
- close CDCC; |
| 398 |
+ # without a home, try the homedir from cdcc |
| 399 |
+ if (!$conf->{dcc_home} && $cdcc_home) { |
| 400 |
+ $self->ck_dir($cdcc_home, 'dcc_home', 'cdcc'); |
| 401 |
+ } |
| 402 |
+ # finally fall back to /var/dcc |
| 403 |
+ if (!$conf->{dcc_home}) { |
| 404 |
+ $self->ck_dir($conf->{dcc_home} = '/var/dcc', 'dcc_home', 'default') |
| 405 |
+ } |
| 406 |
|
| 407 |
- chomp $cdcc_home; |
| 408 |
- $cdcc_home =~ s/\s+homedir=//; |
| 409 |
- dbg("dcc: cdcc reports homedir as '%s'", $cdcc_home); |
| 410 |
- } |
| 411 |
- |
| 412 |
- # try first with whatever the cdcc utility reported |
| 413 |
- my $cdcc_home_errno = 0; |
| 414 |
- if ($cdcc_home eq '') { |
| 415 |
- $cdcc_home_errno = ENOENT; |
| 416 |
- } elsif (!stat($cdcc_home)) { |
| 417 |
- $cdcc_home_errno = 0+$!; |
| 418 |
- } |
| 419 |
- if ($cdcc_home_errno == ENOENT) { |
| 420 |
- # no such file |
| 421 |
- } elsif ($cdcc_home_errno != 0) { |
| 422 |
- dbg("dcc: cdcc reported homedir $cdcc_home is not accessible: $!"); |
| 423 |
- } elsif (!-d _) { |
| 424 |
- dbg("dcc: cdcc reported homedir $cdcc_home is not a directory"); |
| 425 |
- } else { # ok |
| 426 |
- dbg("dcc: cdcc reported homedir $cdcc_home exists, using it"); |
| 427 |
- $dcchome = untaint_var($cdcc_home); |
| 428 |
- } |
| 429 |
- |
| 430 |
- # try falling back to /var/dcc |
| 431 |
- if ($dcchome eq '') { |
| 432 |
- my $var_dcc_errno = stat('/var/dcc') ? 0 : 0+$!; |
| 433 |
- if ($var_dcc_errno == ENOENT) { |
| 434 |
- # no such file |
| 435 |
- } elsif ($var_dcc_errno != 0) { |
| 436 |
- dbg("dcc: dcc_home not set and dcc default homedir /var/dcc ". |
| 437 |
- "is not accessible: $!"); |
| 438 |
- } elsif (!-d _) { |
| 439 |
- dbg("dcc: dcc_home not set and dcc default homedir /var/dcc ". |
| 440 |
- "is not a directory"); |
| 441 |
- } else { # ok |
| 442 |
- dbg("dcc: dcc_home not set but dcc default homedir /var/dcc exists, ". |
| 443 |
- "using it"); |
| 444 |
- $dcchome = '/var/dcc'; |
| 445 |
+ # fall back to $conf->{dcc_home}/libexec or /var/dcc/libexec for dccsight |
| 446 |
+ if (!$conf->{dcc_libexec}) { |
| 447 |
+ $self->ck_dir($conf->{dcc_home} . '/libexec', 'dcc_libexec', 'dcc_home'); |
| 448 |
} |
| 449 |
+ if (!$conf->{dcc_libexec}) { |
| 450 |
+ $self->ck_dir('/var/dcc/libexec', 'dcc_libexec', 'dcc_home'); |
| 451 |
} |
| 452 |
|
| 453 |
- if ($dcchome eq '') { |
| 454 |
- dbg("dcc: unable to get homedir from cdcc ". |
| 455 |
- "and the dcc default homedir was not found"); |
| 456 |
- } |
| 457 |
- |
| 458 |
- # Remember found homedir path |
| 459 |
- dbg("dcc: using '%s' as DCC homedir", $dcchome); |
| 460 |
- $conf->{dcc_home} = $dcchome; |
| 461 |
+ # format options for dccifd |
| 462 |
+ my $opts = ($conf->{dccifd_options} || '') . "\n"; |
| 463 |
+ if ($self->{dcc_version} =~ /\d+\.(\d+)\.(\d+)$/ && |
| 464 |
+ ($1 < 3 || ($1 == 3 && $2 < 123))) { |
| 465 |
+ if ($1 < 3 || ($1 == 3 && $2 < 50)) { |
| 466 |
+ info("dcc: DCC version $self->{dcc_version} is years old, ". |
| 467 |
+ "obsolete, and likely to cause problems. ". |
| 468 |
+ "See http://www.dcc-servers.net/dcc/old-versions.html"); |
| 469 |
+ } |
| 470 |
+ $self->{dccifd_lookup_options} = "header " . $opts; |
| 471 |
+ $self->{dccifd_report_options} = "header spam " . $opts; |
| 472 |
+ } else { |
| 473 |
+ # dccifd after version 1.2.123 understands "cksums" and "no-grey" |
| 474 |
+ $self->{dccifd_lookup_options} = "cksums grey-off " . $opts; |
| 475 |
+ $self->{dccifd_report_options} = "header spam grey-off " . $opts; |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
-sub is_dccifd_available { |
| 480 |
- my ($self) = @_; |
| 481 |
- |
| 482 |
+sub dcc_pgm_path { |
| 483 |
+ my ($self, $pgm) = @_; |
| 484 |
+ my $pgmpath; |
| 485 |
my $conf = $self->{main}->{conf}; |
| 486 |
- $self->{dccifd_available} = 0; |
| 487 |
|
| 488 |
- if (!$conf->{use_dcc}) { |
| 489 |
- dbg("dcc: dccifd is not available: use_dcc is false"); |
| 490 |
- } elsif (defined $conf->{dcc_dccifd_host}) { |
| 491 |
- dbg("dcc: dccifd inet socket chosen: [%s]:%s", |
| 492 |
- $conf->{dcc_dccifd_host}, $conf->{dcc_dccifd_port}); |
| 493 |
- $self->{dccifd_available} = 1; |
| 494 |
- } else { |
| 495 |
- my $sockpath = $conf->{dcc_dccifd_socket}; |
| 496 |
- my $dcchome = $conf->{dcc_home}; |
| 497 |
- if (defined $sockpath) { |
| 498 |
- dbg("dcc: dccifd local socket chosen: %s", $sockpath); |
| 499 |
- } elsif (defined $conf->{dcc_dccifd_path_raw}) { |
| 500 |
- # avoid falling back to defaults if explicitly provided but wrong |
| 501 |
- } elsif (defined $dcchome && $dcchome ne '' && -S "$dcchome/dccifd") { |
| 502 |
- $sockpath = "$dcchome/dccifd"; |
| 503 |
- $conf->{dcc_dccifd_socket} = $sockpath; |
| 504 |
- dbg("dcc: dccifd default local socket chosen: %s", $sockpath); |
| 505 |
+ $pgmpath = $conf->{dcc_path}; |
| 506 |
+ if (defined $pgmpath && $pgmpath ne '') { |
| 507 |
+ # accept explicit setting for dccproc |
| 508 |
+ return $pgmpath if $pgm eq 'dccproc'; |
| 509 |
+ # try adapting it for cdcc and everything else |
| 510 |
+ if ($pgmpath =~ s{[^/]+\z}{$pgm}s) { |
| 511 |
+ $pgmpath = untaint_file_path($pgmpath); |
| 512 |
+ if (-x $pgmpath) { |
| 513 |
+ dbg("dcc: dcc_pgm_path, found %s in dcc_path: %s", $pgm,$pgmpath); |
| 514 |
+ return $pgmpath; |
| 515 |
} |
| 516 |
- if (defined $sockpath && -S $sockpath && -w _ && -r _) { |
| 517 |
- $self->{dccifd_available} = 1; |
| 518 |
- } elsif (!defined $conf->{dcc_dccifd_path_raw}) { |
| 519 |
- dbg("dcc: dccifd is not available: no r/w dccifd socket found"); |
| 520 |
- } else { |
| 521 |
- dbg("dcc: dccifd is not available: no r/w dccifd socket found: %s", |
| 522 |
- $conf->{dcc_dccifd_path_raw}); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
- return $self->{dccifd_available}; |
| 527 |
+ $pgmpath = Mail::SpamAssassin::Util::find_executable_in_env_path($pgm); |
| 528 |
+ if (defined $pgmpath) { |
| 529 |
+ dbg("dcc: dcc_pgm_path, found %s in env.path: %s", $pgm,$pgmpath); |
| 530 |
+ return $pgmpath; |
| 531 |
+ } |
| 532 |
+ |
| 533 |
+ # try dcc_home/bin, dcc_libexec, and some desperate last attempts |
| 534 |
+ foreach my $dir ($conf->{dcc_home}.'/bin', $conf->{dcc_libexec}, |
| 535 |
+ '/usr/local/bin', '/usr/local/dcc', '/var/dcc') { |
| 536 |
+ $pgmpath = $dir . '/' . $pgm; |
| 537 |
+ if (-x $pgmpath) { |
| 538 |
+ dbg("dcc: dcc_pgm_path, found %s in %s: %s", $pgm,$dir,$pgmpath); |
| 539 |
+ return $pgmpath; |
| 540 |
+ } |
| 541 |
+ } |
| 542 |
+ |
| 543 |
+ return; |
| 544 |
} |
| 545 |
|
| 546 |
-sub is_dccproc_available { |
| 547 |
+sub is_dccifd_available { |
| 548 |
my ($self) = @_; |
| 549 |
my $conf = $self->{main}->{conf}; |
| 550 |
|
| 551 |
- $self->{dccproc_available} = 0; |
| 552 |
+ # dccifd remains available until it breaks |
| 553 |
+ return $self->{dccifd_available} if $self->{dccifd_available}; |
| 554 |
|
| 555 |
- if (!$conf->{use_dcc}) { |
| 556 |
- dbg("dcc: dccproc is not available: use_dcc is false"); |
| 557 |
- return 0; |
| 558 |
+ # deal with configured INET socket |
| 559 |
+ if (defined $conf->{dcc_dccifd_host}) { |
| 560 |
+ dbg("dcc: dccifd is available via INET socket [%s]:%s", |
| 561 |
+ $conf->{dcc_dccifd_host}, $conf->{dcc_dccifd_port}); |
| 562 |
+ return ($self->{dccifd_available} = 1); |
| 563 |
} |
| 564 |
- my $dcchome = $conf->{dcc_home} || ''; |
| 565 |
- my $dccproc = $conf->{dcc_path} || ''; |
| 566 |
|
| 567 |
- if ($dccproc eq '' && ($dcchome ne '' && -x "$dcchome/bin/dccproc")) { |
| 568 |
- $dccproc = "$dcchome/bin/dccproc"; |
| 569 |
+ # the first time here, compute a default local socket based on DCC home |
| 570 |
+ # from self->find_dcc_home() called elsewhere |
| 571 |
+ my $sockpath = $conf->{dcc_dccifd_socket}; |
| 572 |
+ if (!$sockpath) { |
| 573 |
+ if ($conf->{dcc_dccifd_path_raw}) { |
| 574 |
+ $sockpath = $conf->{dcc_dccifd_path_raw}; |
| 575 |
+ } else { |
| 576 |
+ $sockpath = "$conf->{dcc_home}/dccifd"; |
| 577 |
} |
| 578 |
- if ($dccproc eq '') { |
| 579 |
- $dccproc = Mail::SpamAssassin::Util::find_executable_in_env_path('dccproc'); |
| 580 |
+ $conf->{dcc_dccifd_socket} = $sockpath; |
| 581 |
} |
| 582 |
|
| 583 |
- unless (defined $dccproc && $dccproc ne '' && -x $dccproc) { |
| 584 |
- dbg("dcc: dccproc is not available: no dccproc executable found"); |
| 585 |
- return 0; |
| 586 |
- } |
| 587 |
+ # check the socket every time because it can appear and disappear |
| 588 |
+ return ($self->{dccifd_available} = 1) if (-S $sockpath && -w _ && -r _); |
| 589 |
|
| 590 |
- # remember any found dccproc |
| 591 |
+ dbg("dcc: dccifd is not available; no r/w socket at %s", $sockpath); |
| 592 |
+ return ($self->{dccifd_available} = 0); |
| 593 |
+} |
| 594 |
+ |
| 595 |
+sub is_dccproc_available { |
| 596 |
+ my ($self) = @_; |
| 597 |
+ my $conf = $self->{main}->{conf}; |
| 598 |
+ |
| 599 |
+ # dccproc remains (un)available so check only once |
| 600 |
+ return $self->{dccproc_available} if defined $self->{dccproc_available}; |
| 601 |
+ |
| 602 |
+ my $dccproc = $conf->{dcc_path}; |
| 603 |
+ if (!defined $dccproc || $dccproc eq '') { |
| 604 |
+ $dccproc = $self->dcc_pgm_path('dccproc'); |
| 605 |
$conf->{dcc_path} = $dccproc; |
| 606 |
+ if (!$dccproc || ! -x $dccproc) { |
| 607 |
+ dbg("dcc: dccproc is not available: no dccproc executable found"); |
| 608 |
+ return ($self->{dccproc_available} = 0); |
| 609 |
+ } |
| 610 |
+ } |
| 611 |
|
| 612 |
- dbg("dcc: dccproc is available: %s", $conf->{dcc_path}); |
| 613 |
- $self->{dccproc_available} = 1; |
| 614 |
- return 1; |
| 615 |
+ dbg("dcc: %s is available", $conf->{dcc_path}); |
| 616 |
+ return ($self->{dccproc_available} = 1); |
| 617 |
} |
| 618 |
|
| 619 |
sub dccifd_connect { |
| 620 |
- my($self) = @_; |
| 621 |
+ my($self, $tag) = @_; |
| 622 |
my $conf = $self->{main}->{conf}; |
| 623 |
my $sockpath = $conf->{dcc_dccifd_socket}; |
| 624 |
- my $host = $conf->{dcc_dccifd_host}; |
| 625 |
- my $port = $conf->{dcc_dccifd_port}; |
| 626 |
my $sock; |
| 627 |
+ |
| 628 |
if (defined $sockpath) { |
| 629 |
- dbg("dcc: connecting to a local socket %s", $sockpath); |
| 630 |
- $sock = IO::Socket::UNIX->new( |
| 631 |
- Type => SOCK_STREAM, Peer => $sockpath); |
| 632 |
- $sock or die "dcc: failed to connect to a socket $sockpath: $!\n"; |
| 633 |
- } elsif (defined $host) { |
| 634 |
- my $specified_path = $conf->{dcc_dccifd_path_raw}; |
| 635 |
- if ($host eq '') { |
| 636 |
- die "dcc: empty host specification: $specified_path\n"; |
| 637 |
- } |
| 638 |
- if (!defined $port || $port !~ /^\d+\z/ || $port < 1 || $port > 65535) { |
| 639 |
- die "dcc: bad TCP port number: $specified_path\n"; |
| 640 |
- } |
| 641 |
- my $is_inet4 = $host =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/; |
| 642 |
- if ($is_inet4) { # inet4 socket (IPv4 address) |
| 643 |
- dbg("dcc: connecting to inet4 socket [%s]:%s", $host,$port); |
| 644 |
- $sock = IO::Socket::INET->new( |
| 645 |
- Proto => 'tcp', PeerAddr => $host, PeerPort => $port); |
| 646 |
- } else { |
| 647 |
- if (!defined $have_inet6) { |
| 648 |
- $have_inet6 = eval { require IO::Socket::INET6 }; |
| 649 |
- $have_inet6 = 0 if !defined $have_inet6; |
| 650 |
+ $sock = IO::Socket::UNIX->new(Type => SOCK_STREAM, Peer => $sockpath); |
| 651 |
+ if ($sock) { |
| 652 |
+ dbg("$tag connected to local socket %s", $sockpath); |
| 653 |
+ return $sock; |
| 654 |
} |
| 655 |
- if (!$have_inet6) { # fallback to an inet4 socket (IPv4) |
| 656 |
- dbg("dcc: connecting(2) to inet4 socket [%s]:%s", $host,$port); |
| 657 |
- $sock = IO::Socket::INET->new( |
| 658 |
- Proto => 'tcp', PeerAddr => $host, PeerPort => $port); |
| 659 |
- } else { # inet6 socket (IPv6) or a host name |
| 660 |
- dbg("dcc: connecting to inet6 socket [%s]:%s", $host,$port); |
| 661 |
+ $self->{dccifd_available} = 0; |
| 662 |
+ info("$tag failed to connect to local socket $sockpath"); |
| 663 |
+ return $sock |
| 664 |
+ } |
| 665 |
+ |
| 666 |
+ # must be TCP/IP |
| 667 |
+ my $host = $conf->{dcc_dccifd_host}; |
| 668 |
+ my $port = $conf->{dcc_dccifd_port}; |
| 669 |
+ |
| 670 |
+ if ($conf->{dcc_dccifd_IPv6}) { |
| 671 |
+ # try IPv6 if we can with a host name or non-IPv4 address |
| 672 |
+ dbg("$tag connecting to inet6 socket [%s]:%s", $host,$port); |
| 673 |
$sock = IO::Socket::INET6->new( |
| 674 |
Proto => 'tcp', PeerAddr => $host, PeerPort => $port); |
| 675 |
+ # fall back to IPv4 if that failed |
| 676 |
} |
| 677 |
+ if (!$sock) { |
| 678 |
+ dbg("$tag connecting to inet4 socket [%s]:%s", $host, $port); |
| 679 |
+ $sock = IO::Socket::INET->new( |
| 680 |
+ Proto => 'tcp', PeerAddr => $host, PeerPort => $port); |
| 681 |
} |
| 682 |
- $sock or die "dcc: failed to connect to [$host]:$port : $!\n"; |
| 683 |
- } else { |
| 684 |
- die "dcc: dccifd socket not provided: $conf->{dcc_dccifd_path_raw}\n"; |
| 685 |
- } |
| 686 |
+ |
| 687 |
+ info("failed to connect to [$host]:$port : $!") if !$sock; |
| 688 |
return $sock; |
| 689 |
} |
| 690 |
|
| 691 |
+# check for dccifd every time in case enough uses of dccproc starts dccifd |
| 692 |
sub get_dcc_interface { |
| 693 |
my ($self) = @_; |
| 694 |
+ my $conf = $self->{main}->{conf}; |
| 695 |
|
| 696 |
- if ($self->is_dccifd_available()) { |
| 697 |
- $self->{dcc_interface} = "dccifd"; |
| 698 |
- $self->{dcc_disabled} = 0; |
| 699 |
- } |
| 700 |
- elsif ($self->is_dccproc_available()) { |
| 701 |
- $self->{dcc_interface} = "dccproc"; |
| 702 |
- $self->{dcc_disabled} = 0; |
| 703 |
+ if (!$conf->{use_dcc}) { |
| 704 |
+ $self->{dcc_disabled} = 1; |
| 705 |
+ return; |
| 706 |
} |
| 707 |
- else { |
| 708 |
- dbg("dcc: dccifd and dccproc are not available, disabling DCC"); |
| 709 |
- $self->{dcc_interface} = "none"; |
| 710 |
+ |
| 711 |
+ $self->find_dcc_home(); |
| 712 |
+ if (!$self->is_dccifd_available() && !$self->is_dccproc_available()) { |
| 713 |
+ dbg("dcc: dccifd and dccproc are not available"); |
| 714 |
$self->{dcc_disabled} = 1; |
| 715 |
} |
| 716 |
+ |
| 717 |
+ $self->{dcc_disabled} = 0; |
| 718 |
} |
| 719 |
|
| 720 |
sub dcc_query { |
| 721 |
- my ($self, $permsgstatus, $full) = @_; |
| 722 |
+ my ($self, $permsgstatus, $fulltext) = @_; |
| 723 |
|
| 724 |
$permsgstatus->{dcc_checked} = 1; |
| 725 |
|
| 726 |
+ if (!$self->{main}->{conf}->{use_dcc}) { |
| 727 |
+ dbg("dcc: DCC is not available: use_dcc is 0"); |
| 728 |
+ return; |
| 729 |
+ } |
| 730 |
+ |
| 731 |
# initialize valid tags |
| 732 |
$permsgstatus->{tag_data}->{DCCB} = ""; |
| 733 |
$permsgstatus->{tag_data}->{DCCR} = ""; |
| 734 |
$permsgstatus->{tag_data}->{DCCREP} = ""; |
| 735 |
|
| 736 |
- # short-circuit if there's already a X-DCC header with value of |
| 737 |
- # "bulk" from an upstream DCC check |
| 738 |
- if ($permsgstatus->get('ALL') =~ |
| 739 |
- /^(X-DCC-([^:]{1,80})?-?Metrics:.*bulk.*)$/m) { |
| 740 |
- $permsgstatus->{dcc_response} = $1; |
| 741 |
+ if ($$fulltext eq '') { |
| 742 |
+ dbg("dcc: empty message; skipping dcc check"); |
| 743 |
return; |
| 744 |
} |
| 745 |
|
| 746 |
- my $timer = $self->{main}->time_method("check_dcc"); |
| 747 |
+ if ($permsgstatus->get('ALL') =~ /^(X-DCC-.*-Metrics:.*)$/m) { |
| 748 |
+ $permsgstatus->{dcc_raw_x_dcc} = $1; |
| 749 |
+ # short-circuit if there is already a X-DCC header with value of |
| 750 |
+ # "bulk" from an upstream DCC check |
| 751 |
+ # require "bulk" because then at least one body checksum will be "many" |
| 752 |
+ # and so we know the X-DCC header is not forged by spammers |
| 753 |
+ return if $permsgstatus->{dcc_raw_x_dcc} =~ / bulk /; |
| 754 |
+ } |
| 755 |
|
| 756 |
- $self->find_dcc_home(); |
| 757 |
+ my $timer = $self->{main}->time_method("check_dcc"); |
| 758 |
|
| 759 |
$self->get_dcc_interface(); |
| 760 |
- my $result; |
| 761 |
- if ($self->{dcc_disabled}) { |
| 762 |
- $result = 0; |
| 763 |
- } elsif ($$full eq '') { |
| 764 |
- dbg("dcc: empty message, skipping dcc check"); |
| 765 |
- $result = 0; |
| 766 |
- } elsif ($self->{dccifd_available}) { |
| 767 |
- my $client = $permsgstatus->{relays_external}->[0]->{ip}; |
| 768 |
- my $clientname = $permsgstatus->{relays_external}->[0]->{rdns}; |
| 769 |
- my $helo = $permsgstatus->{relays_external}->[0]->{helo} || ""; |
| 770 |
- if ($client) { |
| 771 |
- $client = $client . "\r" . $clientname if $clientname; |
| 772 |
- } else { |
| 773 |
- $client = "0.0.0.0"; |
| 774 |
- } |
| 775 |
- $self->dccifd_lookup($permsgstatus, $full, $client, $clientname, $helo); |
| 776 |
- } else { |
| 777 |
- my $client = $permsgstatus->{relays_external}->[0]->{ip}; |
| 778 |
- $self->dccproc_lookup($permsgstatus, $full, $client); |
| 779 |
- } |
| 780 |
+ return if $self->{dcc_disabled}; |
| 781 |
+ |
| 782 |
+ my $envelope = $permsgstatus->{relays_external}->[0]; |
| 783 |
+ ($permsgstatus->{dcc_raw_x_dcc}, |
| 784 |
+ $permsgstatus->{dcc_cksums}) = $self->ask_dcc("dcc:", $permsgstatus, |
| 785 |
+ $fulltext, $envelope); |
| 786 |
} |
| 787 |
|
| 788 |
sub check_dcc { |
| 789 |
@@ -609,28 +681,27 @@ |
| 790 |
|
| 791 |
$self->dcc_query($permsgstatus, $full) if !$permsgstatus->{dcc_checked}; |
| 792 |
|
| 793 |
- my $response = $permsgstatus->{dcc_response}; |
| 794 |
- return 0 if !defined $response || $response eq ''; |
| 795 |
+ my $x_dcc = $permsgstatus->{dcc_raw_x_dcc}; |
| 796 |
+ return 0 if !defined $x_dcc || $x_dcc eq ''; |
| 797 |
|
| 798 |
- local($1,$2); |
| 799 |
- if ($response =~ /^X-DCC-(.*)-Metrics: (.*)$/) { |
| 800 |
- $permsgstatus->{tag_data}->{DCCB} = $1; |
| 801 |
- $permsgstatus->{tag_data}->{DCCR} = $2; |
| 802 |
+ if ($x_dcc =~ /^X-DCC-(.*)-Metrics: (.*)$/) { |
| 803 |
+ $permsgstatus->set_tag('DCCB', $1); |
| 804 |
+ $permsgstatus->set_tag('DCCR', $2); |
| 805 |
} |
| 806 |
- $response =~ s/many/999999/ig; |
| 807 |
- $response =~ s/ok\d?/0/ig; |
| 808 |
+ $x_dcc =~ s/many/999999/ig; |
| 809 |
+ $x_dcc =~ s/ok\d?/0/ig; |
| 810 |
|
| 811 |
my %count = (body => 0, fuz1 => 0, fuz2 => 0, rep => 0); |
| 812 |
- if ($response =~ /\bBody=(\d+)/) { |
| 813 |
+ if ($x_dcc =~ /\bBody=(\d+)/) { |
| 814 |
$count{body} = $1+0; |
| 815 |
} |
| 816 |
- if ($response =~ /\bFuz1=(\d+)/) { |
| 817 |
+ if ($x_dcc =~ /\bFuz1=(\d+)/) { |
| 818 |
$count{fuz1} = $1+0; |
| 819 |
} |
| 820 |
- if ($response =~ /\bFuz2=(\d+)/) { |
| 821 |
+ if ($x_dcc =~ /\bFuz2=(\d+)/) { |
| 822 |
$count{fuz2} = $1+0; |
| 823 |
} |
| 824 |
- if ($response =~ /\brep=(\d+)/) { |
| 825 |
+ if ($x_dcc =~ /\brep=(\d+)/) { |
| 826 |
$count{rep} = $1+0; |
| 827 |
} |
| 828 |
if ($count{body} >= $conf->{dcc_body_max} || |
| 829 |
@@ -651,185 +722,185 @@ |
| 830 |
} |
| 831 |
|
| 832 |
sub check_dcc_reputation_range { |
| 833 |
- my ($self, $permsgstatus, $full, $min, $max) = @_; |
| 834 |
- $self->dcc_query($permsgstatus, $full) if !$permsgstatus->{dcc_checked}; |
| 835 |
+ my ($self, $permsgstatus, $fulltext, $min, $max) = @_; |
| 836 |
+ |
| 837 |
+ # this is called several times per message, so parse the X-DCC header once |
| 838 |
+ my $dcc_rep = $permsgstatus->{dcc_rep}; |
| 839 |
+ if (!defined $dcc_rep) { |
| 840 |
+ $self->dcc_query($permsgstatus, $fulltext) if !$permsgstatus->{dcc_checked}; |
| 841 |
+ my $x_dcc = $permsgstatus->{dcc_raw_x_dcc}; |
| 842 |
+ if (defined $x_dcc && $x_dcc =~ /\brep=(\d+)/) { |
| 843 |
+ $dcc_rep = $1+0; |
| 844 |
+ $permsgstatus->set_tag('DCCREP', $dcc_rep); |
| 845 |
+ } else { |
| 846 |
+ $dcc_rep = -1; |
| 847 |
+ } |
| 848 |
+ $permsgstatus->{dcc_rep} = $dcc_rep; |
| 849 |
+ } |
| 850 |
|
| 851 |
- my $response = $permsgstatus->{dcc_response}; |
| 852 |
- return 0 if !defined $response || $response eq ''; |
| 853 |
+ # no X-DCC header or no reputation in the X-DCC header, perhaps for lack |
| 854 |
+ # of data in the DCC Reputation server |
| 855 |
+ return 0 if $dcc_rep < 0; |
| 856 |
|
| 857 |
+ # cover the entire range of reputations if not told otherwise |
| 858 |
$min = 0 if !defined $min; |
| 859 |
- $max = 999 if !defined $max; |
| 860 |
+ $max = 100 if !defined $max; |
| 861 |
|
| 862 |
- local $1; |
| 863 |
- my $dcc_rep; |
| 864 |
- $dcc_rep = $1+0 if defined $response && $response =~ /\brep=(\d+)/; |
| 865 |
- if (defined $dcc_rep) { |
| 866 |
- $dcc_rep = int($dcc_rep); # just in case, rule ranges are integer percents |
| 867 |
my $result = $dcc_rep >= $min && $dcc_rep <= $max ? 1 : 0; |
| 868 |
dbg("dcc: dcc_rep %s, min %s, max %s => result=%s", |
| 869 |
$dcc_rep, $min, $max, $result?'YES':'no'); |
| 870 |
- $permsgstatus->{tag_data}->{DCCREP} = $dcc_rep; |
| 871 |
- return $dcc_rep >= $min && $dcc_rep <= $max ? 1 : 0; |
| 872 |
+ return $result; |
| 873 |
+} |
| 874 |
+ |
| 875 |
+# get the X-DCC header line and save the checksums from dccifd or dccproc |
| 876 |
+sub parse_dcc_response { |
| 877 |
+ my ($self, $resp) = @_; |
| 878 |
+ my ($raw_x_dcc, $cksums); |
| 879 |
+ |
| 880 |
+ # The first line is the header we want. It uses SMTP folded whitespace |
| 881 |
+ # if it is long. The folded whitespace is always a single \t. |
| 882 |
+ chomp($raw_x_dcc = shift @$resp); |
| 883 |
+ my $v; |
| 884 |
+ while (($v = shift @$resp) && $v =~ s/^\t(.+)\s*\n/ $1/) { |
| 885 |
+ $raw_x_dcc .= $v; |
| 886 |
+ } |
| 887 |
+ |
| 888 |
+ # skip the "reported:" line between the X-DCC header and any checksums |
| 889 |
+ # remove ':' to avoid a bug in versions 1.3.115 - 1.3.122 in dccsight |
| 890 |
+ # with the length of "Message-ID:" |
| 891 |
+ $cksums = ''; |
| 892 |
+ while (($v = shift @$resp) && $v =~ s/^([^:]*):/$1/) { |
| 893 |
+ $cksums .= $v; |
| 894 |
} |
| 895 |
- return 0; |
| 896 |
+ |
| 897 |
+ return ($raw_x_dcc, $cksums); |
| 898 |
} |
| 899 |
|
| 900 |
-sub dccifd_lookup { |
| 901 |
- my ($self, $permsgstatus, $fulltext, $client, $clientname, $helo) = @_; |
| 902 |
+sub ask_dcc { |
| 903 |
+ my ($self, $tag, $permsgstatus, $fulltext, $envelope) = @_; |
| 904 |
my $conf = $self->{main}->{conf}; |
| 905 |
- my $response; |
| 906 |
- my $left; |
| 907 |
- my $right; |
| 908 |
- my $timeout = $conf->{dcc_timeout}; |
| 909 |
- my $opts = $conf->{dccifd_options}; |
| 910 |
- my @opts = !defined $opts ? () : split(' ',$opts); |
| 911 |
+ my ($pgm, $err, $sock, $pid, @resp); |
| 912 |
+ my ($client, $clientname, $helo, $opts); |
| 913 |
|
| 914 |
$permsgstatus->enter_helper_run_mode(); |
| 915 |
|
| 916 |
+ my $timeout = $conf->{dcc_timeout}; |
| 917 |
my $timer = Mail::SpamAssassin::Timeout->new( |
| 918 |
{ secs => $timeout, deadline => $permsgstatus->{master_deadline} }); |
| 919 |
- my $err = $timer->run_and_catch(sub { |
| 920 |
|
| 921 |
+ $err = $timer->run_and_catch(sub { |
| 922 |
local $SIG{PIPE} = sub { die "__brokenpipe__ignore__\n" }; |
| 923 |
|
| 924 |
- my $sock = $self->dccifd_connect(); |
| 925 |
- $sock or die "dcc: failed to connect to a dccifd socket"; |
| 926 |
- |
| 927 |
- # send the options and other parameters to the daemon |
| 928 |
- $sock->print("header " . join(" ",@opts) . "\n") |
| 929 |
- or die "dcc: failed write"; # options |
| 930 |
- $sock->print($client . "\n") or die "dcc: failed write"; # client |
| 931 |
- $sock->print($helo . "\n") or die "dcc: failed write"; # HELO value |
| 932 |
- $sock->print("\n") or die "dcc: failed write"; # sender |
| 933 |
- $sock->print("unknown\r\n") or die "dcc: failed write"; # recipients |
| 934 |
- $sock->print("\n") or die "dcc: failed write"; # recipients |
| 935 |
- |
| 936 |
- $sock->print($$fulltext) or die "dcc: failed write"; |
| 937 |
- |
| 938 |
- $sock->shutdown(1) or die "dcc: failed socket shutdown: $!"; |
| 939 |
- |
| 940 |
- $sock->getline() or die "dcc: failed read status"; |
| 941 |
- $sock->getline() or die "dcc: failed read multistatus"; |
| 942 |
+ # prefer dccifd to dccproc |
| 943 |
+ if ($self->{dccifd_available}) { |
| 944 |
+ $pgm = 'dccifd'; |
| 945 |
|
| 946 |
- my @null = $sock->getlines(); |
| 947 |
- if (!@null) { |
| 948 |
- # no facility prefix on this |
| 949 |
- die "dcc: failed to read header\n"; |
| 950 |
- } |
| 951 |
+ $sock = $self->dccifd_connect($tag); |
| 952 |
+ if (!$sock) { |
| 953 |
+ $self->{dccifd_available} = 0; |
| 954 |
+ die("dccproc not available") if (!$self->is_dccproc_available()); |
| 955 |
|
| 956 |
- # the first line will be the header we want to look at |
| 957 |
- chomp($response = shift @null); |
| 958 |
- # but newer versions of DCC fold the header if it's too long... |
| 959 |
- while (my $v = shift @null) { |
| 960 |
- last unless ($v =~ s/^\s+/ /); # if this line wasn't folded, stop |
| 961 |
- chomp $v; |
| 962 |
- $response .= $v; |
| 963 |
+ # fall back on dccproc if the socket is an orphan from |
| 964 |
+ # a killed dccifd daemon or some other obvious (no timeout) problem |
| 965 |
+ dbg("$tag fall back on dccproc"); |
| 966 |
} |
| 967 |
- |
| 968 |
- dbg("dcc: dccifd got response: %s", $response); |
| 969 |
- |
| 970 |
- }); |
| 971 |
- |
| 972 |
- $permsgstatus->leave_helper_run_mode(); |
| 973 |
- |
| 974 |
- if ($timer->timed_out()) { |
| 975 |
- dbg("dcc: dccifd check timed out after $timeout secs."); |
| 976 |
- return; |
| 977 |
} |
| 978 |
|
| 979 |
- if ($err) { |
| 980 |
- chomp $err; |
| 981 |
- warn("dcc: dccifd -> check skipped: $err\n"); |
| 982 |
- return; |
| 983 |
- } |
| 984 |
+ if ($self->{dccifd_available}) { |
| 985 |
|
| 986 |
- if (!defined $response || $response !~ /^X-DCC/) { |
| 987 |
- dbg("dcc: dccifd check failed - no X-DCC returned: %s", $response); |
| 988 |
- return; |
| 989 |
+ # send the options and other parameters to the daemon |
| 990 |
+ $client = $envelope->{ip}; |
| 991 |
+ $clientname = $envelope->{rdns}; |
| 992 |
+ if (!defined $client) { |
| 993 |
+ $client = ''; |
| 994 |
+ } else { |
| 995 |
+ $client .= ("\r" . $clientname) if defined $clientname; |
| 996 |
} |
| 997 |
+ $helo = $envelope->{helo} || ''; |
| 998 |
+ if ($tag ne "dcc:") { |
| 999 |
+ $opts = $self->{dccifd_report_options} |
| 1000 |
+ } else { |
| 1001 |
+ $opts = $self->{dccifd_lookup_options}; |
| 1002 |
+ # only query if there is an X-DCC header |
| 1003 |
+ $opts =~ s/grey-off/& query/ if defined $permsgstatus->{dcc_raw_x_dcc}; |
| 1004 |
+ } |
| 1005 |
+ $sock->print($opts) or die "failed write options\n"; |
| 1006 |
+ $sock->print($client . "\n") or die "failed write SMTP client\n"; |
| 1007 |
+ $sock->print($helo . "\n") or die "failed write HELO value\n"; |
| 1008 |
+ $sock->print("\n") or die "failed write sender\n"; |
| 1009 |
+ $sock->print("unknown\n\n") or die "failed write 1 recipient\n"; |
| 1010 |
+ $sock->print($$fulltext) or die "failed write mail message\n"; |
| 1011 |
+ $sock->shutdown(1) or die "failed socket shutdown: $!"; |
| 1012 |
|
| 1013 |
- $response =~ s/[ \t]\z//; # strip trailing whitespace |
| 1014 |
- $permsgstatus->{dcc_response} = $response; |
| 1015 |
-} |
| 1016 |
+ $sock->getline() or die "failed read status\n"; |
| 1017 |
+ $sock->getline() or die "failed read multistatus\n"; |
| 1018 |
|
| 1019 |
-sub dccproc_lookup { |
| 1020 |
- my ($self, $permsgstatus, $fulltext, $client) = @_; |
| 1021 |
- my $conf = $self->{main}->{conf}; |
| 1022 |
- my $response; |
| 1023 |
- my %count = (body => 0, fuz1 => 0, fuz2 => 0, rep => 0); |
| 1024 |
- my $timeout = $conf->{dcc_timeout}; |
| 1025 |
+ @resp = $sock->getlines(); |
| 1026 |
+ die "failed to read dccifd response\n" if !@resp; |
| 1027 |
|
| 1028 |
- $permsgstatus->enter_helper_run_mode(); |
| 1029 |
- |
| 1030 |
- # use a temp file here -- open2() is unreliable, buffering-wise, under spamd |
| 1031 |
+ } else { |
| 1032 |
+ $pgm = 'dccproc'; |
| 1033 |
+ # use a temp file -- open2() is unreliable, buffering-wise, under spamd |
| 1034 |
+ # first ensure that we do not hit a stray file from some other filter. |
| 1035 |
+ $permsgstatus->delete_fulltext_tmpfile(); |
| 1036 |
my $tmpf = $permsgstatus->create_fulltext_tmpfile($fulltext); |
| 1037 |
- my $pid; |
| 1038 |
- |
| 1039 |
- my $timer = Mail::SpamAssassin::Timeout->new( |
| 1040 |
- { secs => $timeout, deadline => $permsgstatus->{master_deadline} }); |
| 1041 |
- my $err = $timer->run_and_catch(sub { |
| 1042 |
- |
| 1043 |
- local $SIG{PIPE} = sub { die "__brokenpipe__ignore__\n" }; |
| 1044 |
|
| 1045 |
- # note: not really tainted, this came from system configuration file |
| 1046 |
- my $path = untaint_file_path($conf->{dcc_path}); |
| 1047 |
- |
| 1048 |
- my $opts = $conf->{dcc_options}; |
| 1049 |
+ my $path = $conf->{dcc_path}; |
| 1050 |
+ $opts = $conf->{dcc_options}; |
| 1051 |
my @opts = !defined $opts ? () : split(' ',$opts); |
| 1052 |
untaint_var(\@opts); |
| 1053 |
+ unshift(@opts, '-w', 'whiteclnt'); |
| 1054 |
+ $client = $envelope->{ip}; |
| 1055 |
+ if ($client) { |
| 1056 |
+ unshift(@opts, '-a', untaint_var($client)); |
| 1057 |
+ } else { |
| 1058 |
+ # get external relay IP address from Received: header if not available |
| 1059 |
+ unshift(@opts, '-R'); |
| 1060 |
+ } |
| 1061 |
+ if ($tag eq "dcc:") { |
| 1062 |
+ # query instead of report if there is an X-DCC header from upstream |
| 1063 |
+ unshift(@opts, '-Q') if defined $permsgstatus->{dcc_raw_x_dcc}; |
| 1064 |
+ } else { |
| 1065 |
+ # learn or report spam |
| 1066 |
+ unshift(@opts, '-t', 'many'); |
| 1067 |
+ } |
| 1068 |
|
| 1069 |
- unshift(@opts, "-a", |
| 1070 |
- untaint_var($client)) if defined $client && $client ne ''; |
| 1071 |
- |
| 1072 |
- dbg("dcc: opening pipe: %s", |
| 1073 |
- join(' ', $path, "-H", "-x", "0", @opts, "< $tmpf")); |
| 1074 |
+ dbg("$tag opening pipe to %s", |
| 1075 |
+ join(' ', $path, "-C", "-x", "0", @opts, "<$tmpf")); |
| 1076 |
|
| 1077 |
$pid = Mail::SpamAssassin::Util::helper_app_pipe_open(*DCC, |
| 1078 |
- $tmpf, 1, $path, "-H", "-x", "0", @opts); |
| 1079 |
+ $tmpf, 1, $path, "-C", "-x", "0", @opts); |
| 1080 |
$pid or die "$!\n"; |
| 1081 |
|
| 1082 |
# read+split avoids a Perl I/O bug (Bug 5985) |
| 1083 |
my($inbuf,$nread,$resp); $resp = ''; |
| 1084 |
while ( $nread=read(DCC,$inbuf,8192) ) { $resp .= $inbuf } |
| 1085 |
defined $nread or die "error reading from pipe: $!"; |
| 1086 |
- my @null = split(/^/m, $resp, -1); undef $resp; |
| 1087 |
+ @resp = split(/^/m, $resp, -1); undef $resp; |
| 1088 |
|
| 1089 |
my $errno = 0; close DCC or $errno = $!; |
| 1090 |
proc_status_ok($?,$errno) |
| 1091 |
- or info("dcc: [%s] finished: %s", $pid, exit_status_str($?,$errno)); |
| 1092 |
- |
| 1093 |
- if (!@null) { |
| 1094 |
- # no facility prefix on this |
| 1095 |
- die "failed to read header\n"; |
| 1096 |
- } |
| 1097 |
+ or info("$tag [%s] finished: %s", $pid, exit_status_str($?,$errno)); |
| 1098 |
|
| 1099 |
- # the first line will be the header we want to look at |
| 1100 |
- chomp($response = shift @null); |
| 1101 |
- # but newer versions of DCC fold the header if it's too long... |
| 1102 |
- while (my $v = shift @null) { |
| 1103 |
- last unless ($v =~ s/^\s+/ /); # if this line wasn't folded, stop |
| 1104 |
- chomp $v; |
| 1105 |
- $response .= $v; |
| 1106 |
+ die "failed to read X-DCC header from dccproc\n" if !@resp; |
| 1107 |
} |
| 1108 |
- |
| 1109 |
- unless (defined($response)) { |
| 1110 |
- # no facility prefix on this |
| 1111 |
- die "no response\n"; # yes, this is possible |
| 1112 |
- } |
| 1113 |
- |
| 1114 |
- dbg("dcc: got response: %s", $response); |
| 1115 |
- |
| 1116 |
}); |
| 1117 |
|
| 1118 |
+ if ($pgm eq 'dccproc') { |
| 1119 |
if (defined(fileno(*DCC))) { # still open |
| 1120 |
if ($pid) { |
| 1121 |
- if (kill('TERM',$pid)) { dbg("dcc: killed stale helper [$pid]") } |
| 1122 |
- else { dbg("dcc: killing helper application [$pid] failed: $!") } |
| 1123 |
+ if (kill('TERM',$pid)) { |
| 1124 |
+ dbg("$tag killed stale dccproc process [$pid]") |
| 1125 |
+ } else { |
| 1126 |
+ dbg("$tag killing dccproc process [$pid] failed: $!") |
| 1127 |
+ } |
| 1128 |
} |
| 1129 |
my $errno = 0; close(DCC) or $errno = $!; |
| 1130 |
- proc_status_ok($?,$errno) |
| 1131 |
- or info("dcc: [%s] terminated: %s", $pid, exit_status_str($?,$errno)); |
| 1132 |
+ proc_status_ok($?,$errno) or info("$tag [%s] dccproc terminated: %s", |
| 1133 |
+ $pid, exit_status_str($?,$errno)); |
| 1134 |
+ } |
| 1135 |
} |
| 1136 |
+ |
| 1137 |
$permsgstatus->leave_helper_run_mode(); |
| 1138 |
|
| 1139 |
if ($timer->timed_out()) { |
| 1140 |
@@ -833,204 +904,182 @@ |
| 1141 |
$permsgstatus->leave_helper_run_mode(); |
| 1142 |
|
| 1143 |
if ($timer->timed_out()) { |
| 1144 |
- dbg("dcc: check timed out after $timeout seconds"); |
| 1145 |
- return; |
| 1146 |
+ dbg("$tag $pgm timed out after $timeout seconds"); |
| 1147 |
+ return (undef, undef); |
| 1148 |
} |
| 1149 |
|
| 1150 |
if ($err) { |
| 1151 |
chomp $err; |
| 1152 |
- if ($err eq "__brokenpipe__ignore__") { |
| 1153 |
- dbg("dcc: check failed: broken pipe"); |
| 1154 |
- } elsif ($err eq "no response") { |
| 1155 |
- dbg("dcc: check failed: no response"); |
| 1156 |
- } else { |
| 1157 |
- warn("dcc: check failed: $err\n"); |
| 1158 |
- } |
| 1159 |
- return; |
| 1160 |
+ info("$tag $pgm failed: $err\n"); |
| 1161 |
+ return (undef, undef); |
| 1162 |
} |
| 1163 |
|
| 1164 |
- if (!defined($response) || $response !~ /^X-DCC/) { |
| 1165 |
- $response ||= ''; |
| 1166 |
- dbg("dcc: check failed: no X-DCC returned (did you create a map file?): %s", $response); |
| 1167 |
- return; |
| 1168 |
+ my ($raw_x_dcc, $cksums) = $self->parse_dcc_response(\@resp); |
| 1169 |
+ if (!defined $raw_x_dcc || $raw_x_dcc !~ /^X-DCC/) { |
| 1170 |
+ info("$tag instead of X-DCC header, $pgm returned '%s'", $raw_x_dcc); |
| 1171 |
+ return (undef, undef); |
| 1172 |
} |
| 1173 |
- |
| 1174 |
- $permsgstatus->{dcc_response} = $response; |
| 1175 |
+ dbg("$tag %s responded with '%s'", $pgm, $raw_x_dcc); |
| 1176 |
+ return ($raw_x_dcc, $cksums); |
| 1177 |
} |
| 1178 |
|
| 1179 |
-# only supports dccproc right now |
| 1180 |
-sub plugin_report { |
| 1181 |
+# tell DCC server that the message is spam according to SpamAssassin |
| 1182 |
+sub check_post_learn { |
| 1183 |
my ($self, $options) = @_; |
| 1184 |
|
| 1185 |
- return if $options->{report}->{options}->{dont_report_to_dcc}; |
| 1186 |
- $self->get_dcc_interface(); |
| 1187 |
- return if $self->{dcc_disabled}; |
| 1188 |
- |
| 1189 |
- # get the metadata from the message so we can pass the external relay information |
| 1190 |
- $options->{msg}->extract_message_metadata($options->{report}->{main}); |
| 1191 |
- my $client = $options->{msg}->{metadata}->{relays_external}->[0]->{ip}; |
| 1192 |
- if ($self->{dccifd_available}) { |
| 1193 |
- my $clientname = $options->{msg}->{metadata}->{relays_external}->[0]->{rdns}; |
| 1194 |
- my $helo = $options->{msg}->{metadata}->{relays_external}->[0]->{helo} || ""; |
| 1195 |
- if ($client) { |
| 1196 |
- if ($clientname) { |
| 1197 |
- $client = $client . "\r" . $clientname; |
| 1198 |
- } |
| 1199 |
- } else { |
| 1200 |
- $client = "0.0.0.0"; |
| 1201 |
- } |
| 1202 |
- if ($self->dccifd_report($options, $options->{text}, $client, $helo)) { |
| 1203 |
- $options->{report}->{report_available} = 1; |
| 1204 |
- info("reporter: spam reported to DCC"); |
| 1205 |
- $options->{report}->{report_return} = 1; |
| 1206 |
+ # learn only if allowed |
| 1207 |
+ return if $self->{learn_disabled}; |
| 1208 |
+ my $conf = $self->{main}->{conf}; |
| 1209 |
+ if (!$conf->{use_dcc}) { |
| 1210 |
+ $self->{learn_disabled} = 1; |
| 1211 |
+ return; |
| 1212 |
} |
| 1213 |
- else { |
| 1214 |
- info("reporter: could not report spam to DCC via dccifd"); |
| 1215 |
+ my $learn_score = $conf->{dcc_learn_score}; |
| 1216 |
+ if (!defined $learn_score || $learn_score eq '') { |
| 1217 |
+ dbg("dcc: DCC learning not enabled by dcc_learn_score"); |
| 1218 |
+ $self->{learn_disabled} = 1; |
| 1219 |
+ return; |
| 1220 |
} |
| 1221 |
- } else { |
| 1222 |
- # use temporary file: open2() is unreliable due to buffering under spamd |
| 1223 |
- my $tmpf = $options->{report}->create_fulltext_tmpfile($options->{text}); |
| 1224 |
|
| 1225 |
- if ($self->dcc_report($options, $tmpf, $client)) { |
| 1226 |
- $options->{report}->{report_available} = 1; |
| 1227 |
- info("reporter: spam reported to DCC"); |
| 1228 |
- $options->{report}->{report_return} = 1; |
| 1229 |
+ # and if SpamAssassin concluded that the message is spam |
| 1230 |
+ # worse than our threshold |
| 1231 |
+ my $permsgstatus = $options->{permsgstatus}; |
| 1232 |
+ if ($permsgstatus->is_spam()) { |
| 1233 |
+ my $score = $permsgstatus->get_score(); |
| 1234 |
+ my $required_score = $permsgstatus->get_required_score(); |
| 1235 |
+ if ($score < $required_score + $learn_score) { |
| 1236 |
+ dbg("dcc: score=%d required_score=%d dcc_learn_score=%d", |
| 1237 |
+ $score, $required_score, $learn_score); |
| 1238 |
+ return; |
| 1239 |
} |
| 1240 |
- else { |
| 1241 |
- info("reporter: could not report spam to DCC via dccproc"); |
| 1242 |
} |
| 1243 |
- $options->{report}->delete_fulltext_tmpfile(); |
| 1244 |
+ |
| 1245 |
+ # and if we checked the message |
| 1246 |
+ return if (!defined $permsgstatus->{dcc_raw_x_dcc}); |
| 1247 |
+ |
| 1248 |
+ # and if the DCC server thinks it was not spam |
| 1249 |
+ if ($permsgstatus->{dcc_raw_x_dcc} !~ /\b(Body|Fuz1|Fuz2)=\d/) { |
| 1250 |
+ dbg("dcc: already known as spam; no need to learn"); |
| 1251 |
+ return; |
| 1252 |
} |
| 1253 |
+ |
| 1254 |
+ # dccsight is faster than dccifd or dccproc if we have checksums, |
| 1255 |
+ # which we do not have with dccifd before 1.3.123 |
| 1256 |
+ my $old_cksums = $permsgstatus->{dcc_cksums}; |
| 1257 |
+ return if ($old_cksums && $self->dccsight_learn($permsgstatus, $old_cksums)); |
| 1258 |
+ |
| 1259 |
+ # Fall back on dccifd or dccproc without saved checksums or dccsight. |
| 1260 |
+ # get_dcc_interface() was called when the message was checked |
| 1261 |
+ |
| 1262 |
+ # is getting the full text this way kosher? Is get_pristine() public? |
| 1263 |
+ my $fulltext = $permsgstatus->{msg}->get_pristine(); |
| 1264 |
+ my $envelope = $permsgstatus->{relays_external}->[0]; |
| 1265 |
+ my ($raw_x_dcc, $cksums) = $self->ask_dcc("dcc: learn:", $permsgstatus, |
| 1266 |
+ \$fulltext, $envelope); |
| 1267 |
+ dbg("dcc: learned as spam") if defined $raw_x_dcc; |
| 1268 |
} |
| 1269 |
|
| 1270 |
-sub dccifd_report { |
| 1271 |
- my ($self, $options, $fulltext, $client, $helo) = @_; |
| 1272 |
- my $conf = $self->{main}->{conf}; |
| 1273 |
- my $timeout = $conf->{dcc_timeout}; |
| 1274 |
- # instead of header use whatever the report option is |
| 1275 |
- my $opts = $conf->{dccifd_options}; |
| 1276 |
- my @opts = !defined $opts ? () : split(' ',$opts); |
| 1277 |
+sub dccsight_learn { |
| 1278 |
+ my ($self, $permsgstatus, $old_cksums) = @_; |
| 1279 |
+ my ($raw_x_dcc, $new_cksums); |
| 1280 |
+ |
| 1281 |
+ return 0 if !$old_cksums; |
| 1282 |
+ |
| 1283 |
+ my $dccsight = $self->dcc_pgm_path('dccsight'); |
| 1284 |
+ if (!$dccsight) { |
| 1285 |
+ info("dcc: cannot find dccsight") if $dccsight eq ''; |
| 1286 |
+ return 0; |
| 1287 |
+ } |
| 1288 |
|
| 1289 |
- $options->{report}->enter_helper_run_mode(); |
| 1290 |
- my $timer = Mail::SpamAssassin::Timeout->new({ secs => $timeout }); |
| 1291 |
+ $permsgstatus->enter_helper_run_mode(); |
| 1292 |
|
| 1293 |
- my $err = $timer->run_and_catch(sub { |
| 1294 |
+ # use a temp file here -- open2() is unreliable, buffering-wise, under spamd |
| 1295 |
+ # ensure that we do not hit a stray file from some other filter. |
| 1296 |
+ $permsgstatus->delete_fulltext_tmpfile(); |
| 1297 |
+ my $tmpf = $permsgstatus->create_fulltext_tmpfile(\$old_cksums); |
| 1298 |
+ my $pid; |
| 1299 |
|
| 1300 |
+ my $timeout = $self->{main}->{conf}->{dcc_timeout}; |
| 1301 |
+ my $timer = Mail::SpamAssassin::Timeout->new( |
| 1302 |
+ { secs => $timeout, deadline => $permsgstatus->{master_deadline} }); |
| 1303 |
+ my $err = $timer->run_and_catch(sub { |
| 1304 |
local $SIG{PIPE} = sub { die "__brokenpipe__ignore__\n" }; |
| 1305 |
|
| 1306 |
- my $sock = $self->dccifd_connect(); |
| 1307 |
- $sock or die "report: failed to connect to a dccifd socket"; |
| 1308 |
+ dbg("dcc: opening pipe to %s", |
| 1309 |
+ join(' ', $dccsight, "-t", "many", "<$tmpf")); |
| 1310 |
|
| 1311 |
- # send the options and other parameters to the daemon |
| 1312 |
- $sock->print("spam " . join(" ",@opts) . "\n") |
| 1313 |
- or die "report: dccifd failed write"; # options |
| 1314 |
- $sock->print($client . "\n") |
| 1315 |
- or die "report: dccifd failed write"; # client |
| 1316 |
- $sock->print($helo . "\n") |
| 1317 |
- or die "report: dccifd failed write"; # HELO value |
| 1318 |
- $sock->print("\n") |
| 1319 |
- or die "report: dccifd failed write"; # sender |
| 1320 |
- $sock->print("unknown\r\n") |
| 1321 |
- or die "report: dccifd failed write"; # recipients |
| 1322 |
- $sock->print("\n") |
| 1323 |
- or die "report: dccifd failed write"; # recipients |
| 1324 |
+ $pid = Mail::SpamAssassin::Util::helper_app_pipe_open(*DCC, |
| 1325 |
+ $tmpf, 1, $dccsight, "-t", "many"); |
| 1326 |
+ $pid or die "$!\n"; |
| 1327 |
|
| 1328 |
- $sock->print($$fulltext) or die "report: dccifd failed write"; |
| 1329 |
+ # read+split avoids a Perl I/O bug (Bug 5985) |
| 1330 |
+ my($inbuf,$nread,$resp); $resp = ''; |
| 1331 |
+ while ( $nread=read(DCC,$inbuf,8192) ) { $resp .= $inbuf } |
| 1332 |
+ defined $nread or die "error reading from pipe: $!"; |
| 1333 |
+ my @resp = split(/^/m, $resp, -1); undef $resp; |
| 1334 |
|
| 1335 |
- $sock->shutdown(1) or die "report: dccifd failed socket shutdown: $!"; |
| 1336 |
+ my $errno = 0; close DCC or $errno = $!; |
| 1337 |
+ proc_status_ok($?,$errno) |
| 1338 |
+ or info("dcc: [%s] finished: %s", $pid, exit_status_str($?,$errno)); |
| 1339 |
|
| 1340 |
- $sock->getline() or die "report: dccifd failed read status"; |
| 1341 |
- $sock->getline() or die "report: dccifd failed read multistatus"; |
| 1342 |
+ die "dcc: failed to read learning response\n" if !@resp; |
| 1343 |
|
| 1344 |
- my @ignored = $sock->getlines(); |
| 1345 |
+ ($raw_x_dcc, $new_cksums) = $self->parse_dcc_response(\@resp); |
| 1346 |
}); |
| 1347 |
|
| 1348 |
- $options->{report}->leave_helper_run_mode(); |
| 1349 |
+ if (defined(fileno(*DCC))) { # still open |
| 1350 |
+ if ($pid) { |
| 1351 |
+ if (kill('TERM',$pid)) { |
| 1352 |
+ dbg("dcc: killed stale dccsight process [$pid]") |
| 1353 |
+ } else { |
| 1354 |
+ dbg("dcc: killing stale dccsight process [$pid] failed: $!") } |
| 1355 |
+ } |
| 1356 |
+ my $errno = 0; close(DCC) or $errno = $!; |
| 1357 |
+ proc_status_ok($?,$errno) or info("dcc: dccsight [%s] terminated: %s", |
| 1358 |
+ $pid, exit_status_str($?,$errno)); |
| 1359 |
+ } |
| 1360 |
+ $permsgstatus->delete_fulltext_tmpfile(); |
| 1361 |
+ $permsgstatus->leave_helper_run_mode(); |
| 1362 |
|
| 1363 |
if ($timer->timed_out()) { |
| 1364 |
- dbg("reporter: DCC report via dccifd timed out after $timeout secs."); |
| 1365 |
+ dbg("dcc: dccsight timed out after $timeout seconds"); |
| 1366 |
return 0; |
| 1367 |
} |
| 1368 |
|
| 1369 |
if ($err) { |
| 1370 |
chomp $err; |
| 1371 |
- if ($err eq "__brokenpipe__ignore__") { |
| 1372 |
- dbg("reporter: DCC report via dccifd failed: broken pipe"); |
| 1373 |
- } else { |
| 1374 |
- warn("reporter: DCC report via dccifd failed: $err\n"); |
| 1375 |
- } |
| 1376 |
+ info("dcc: dccsight failed: $err\n"); |
| 1377 |
return 0; |
| 1378 |
} |
| 1379 |
|
| 1380 |
+ if ($raw_x_dcc) { |
| 1381 |
+ dbg("dcc: learned response: %s", $raw_x_dcc); |
| 1382 |
return 1; |
| 1383 |
-} |
| 1384 |
- |
| 1385 |
-sub dcc_report { |
| 1386 |
- my ($self, $options, $tmpf, $client) = @_; |
| 1387 |
- my $conf = $self->{main}->{conf}; |
| 1388 |
- my $timeout = $options->{report}->{conf}->{dcc_timeout}; |
| 1389 |
- |
| 1390 |
- # note: not really tainted, this came from system configuration file |
| 1391 |
- my $path = untaint_file_path($options->{report}->{conf}->{dcc_path}); |
| 1392 |
- my $opts = $conf->{dcc_options}; |
| 1393 |
- my @opts = !defined $opts ? () : split(' ',$opts); |
| 1394 |
- untaint_var(\@opts); |
| 1395 |
- |
| 1396 |
- # get the metadata from the message so we can pass the external relay info |
| 1397 |
- |
| 1398 |
- unshift(@opts, "-a", |
| 1399 |
- untaint_var($client)) if defined $client && $client ne ''; |
| 1400 |
- |
| 1401 |
- my $timer = Mail::SpamAssassin::Timeout->new({ secs => $timeout }); |
| 1402 |
- |
| 1403 |
- $options->{report}->enter_helper_run_mode(); |
| 1404 |
- my $err = $timer->run_and_catch(sub { |
| 1405 |
- |
| 1406 |
- local $SIG{PIPE} = sub { die "__brokenpipe__ignore__\n" }; |
| 1407 |
- |
| 1408 |
- dbg("report: opening pipe: %s", |
| 1409 |
- join(' ', $path, "-H", "-t", "many", "-x", "0", @opts, "< $tmpf")); |
| 1410 |
- |
| 1411 |
- my $pid = Mail::SpamAssassin::Util::helper_app_pipe_open(*DCC, |
| 1412 |
- $tmpf, 1, $path, "-H", "-t", "many", "-x", "0", @opts); |
| 1413 |
- $pid or die "$!\n"; |
| 1414 |
+ } |
| 1415 |
|
| 1416 |
- my($inbuf,$nread,$nread_all); $nread_all = 0; |
| 1417 |
- # response is ignored, just check its existence |
| 1418 |
- while ( $nread=read(DCC,$inbuf,8192) ) { $nread_all += $nread } |
| 1419 |
- defined $nread or die "error reading from pipe: $!"; |
| 1420 |
+ return 0; |
| 1421 |
+} |
| 1422 |
|
| 1423 |
- dbg("dcc: empty response") if $nread_all < 1; |
| 1424 |
+sub plugin_report { |
| 1425 |
+ my ($self, $options) = @_; |
| 1426 |
|
| 1427 |
- my $errno = 0; close DCC or $errno = $!; |
| 1428 |
- # closing a pipe also waits for the process executing on the pipe to |
| 1429 |
- # complete, no need to explicitly call waitpid |
| 1430 |
- # my $child_stat = waitpid($pid,0) > 0 ? $? : undef; |
| 1431 |
- proc_status_ok($?,$errno) |
| 1432 |
- or die "dcc: reporter error: ".exit_status_str($?,$errno)."\n"; |
| 1433 |
- }); |
| 1434 |
- $options->{report}->leave_helper_run_mode(); |
| 1435 |
+ return if $options->{report}->{options}->{dont_report_to_dcc}; |
| 1436 |
+ $self->get_dcc_interface(); |
| 1437 |
+ return if $self->{dcc_disabled}; |
| 1438 |
|
| 1439 |
- if ($timer->timed_out()) { |
| 1440 |
- dbg("reporter: DCC report via dccproc timed out after $timeout seconds"); |
| 1441 |
- return 0; |
| 1442 |
- } |
| 1443 |
+ # get the metadata from the message so we can report the external relay |
| 1444 |
+ $options->{msg}->extract_message_metadata($options->{report}->{main}); |
| 1445 |
+ my $envelope = $options->{msg}->{metadata}->{relays_external}->[0]; |
| 1446 |
+ my ($raw_x_dcc, $cksums) = $self->ask_dcc("reporter:", $options->{report}, |
| 1447 |
+ $options->{text}, $envelope); |
| 1448 |
|
| 1449 |
- if ($err) { |
| 1450 |
- chomp $err; |
| 1451 |
- if ($err eq "__brokenpipe__ignore__") { |
| 1452 |
- dbg("reporter: DCC report via dccproc failed: broken pipe"); |
| 1453 |
+ if (defined $raw_x_dcc) { |
| 1454 |
+ $options->{report}->{report_available} = 1; |
| 1455 |
+ info("reporter: spam reported to DCC"); |
| 1456 |
+ $options->{report}->{report_return} = 1; |
| 1457 |
} else { |
| 1458 |
- warn("reporter: DCC report via dccproc failed: $err\n"); |
| 1459 |
+ info("reporter: could not report spam to DCC"); |
| 1460 |
} |
| 1461 |
- return 0; |
| 1462 |
- } |
| 1463 |
- |
| 1464 |
- return 1; |
| 1465 |
} |
| 1466 |
|
| 1467 |
1; |
| 1468 |
- |
| 1469 |
-=back |
| 1470 |
- |
| 1471 |
-=cut |