From af7255bac4318ec96b0b36a57ea87ea893ba7b02 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 4 Aug 2016 10:42:33 +0100 Subject: [PATCH] [Fix] Parse CGP envelope data --- utils/cgp_rspamd.pl | 46 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/utils/cgp_rspamd.pl b/utils/cgp_rspamd.pl index f9e0bb4d7..482bbd3bb 100644 --- a/utils/cgp_rspamd.pl +++ b/utils/cgp_rspamd.pl @@ -20,7 +20,6 @@ my $reject_message = "Spam message rejected"; GetOptions( "host=s" => \$rspamd_host, "header=s" => \$header, - "local" => \$local, "reject-message=s" => \$reject_message, "help|?" => \$help, "man" => \$man @@ -100,13 +99,51 @@ sub rspamd_scan { if ($local) { # Use file scan + # XXX: not implemented now due to CGP queue format http_get("http://$rspamd_host/symbols?file=$file", $http_callback); } else { aio_load($file, sub { my ($data) = @_ or return print "* Cannot open $file: $!\n$tag FAILURE\n"; - http_post("http://$rspamd_host/symbols", $data, $http_callback); + # Parse CGP format + $data =~ s/^((?:[^\n]*\n)*?)\n(.*)$/$2/ms; + my @envelope = split /\n/, $1; + chomp(@envelope); + my $from; + my @rcpts; + my $ip; + + foreach my $elt (@envelope) { + if ($elt =~ /^P\s[^<]*(<[^>]*>).*$/) { + $from = $1; + } + elsif ($elt =~ /^R\s[^<]*(<[^>]*>).*$/) { + push @rcpts, $1; + } + elsif ($elt =~ /^S .*\[(.+)\]/) { + $ip = $1; + } + } + + my $headers = {}; + if ($file =~ /\/([^\/.]+)\.msg$/) { + $headers->{'Queue-ID'} = $1; + } + if ($from) { + $headers->{From} = $from; + } + if (scalar(@rcpts) > 0) { + # XXX: Anyevent cannot parse headers with multiple values + foreach (@rcpts) { + $headers->{Rcpt} = $_; + } + } + if ($ip) { + $headers->{IP} = $ip; + } + + http_post("http://$rspamd_host/symbols", $data, headers => $headers, $http_callback); }); } } @@ -154,7 +191,6 @@ cgp_rspamd [options] Options: --host=hostport Rspamd host to connect (localhost:11333 by default) - --local Rspamd runs locally and can access CGP files (false by default) --header Add specific header for a spam message ("X-Spam: yes" by default) --reject-message Rejection message for spam mail ("Spam message rejected" by default) --help brief help message @@ -168,10 +204,6 @@ cgp_rspamd [options] Specifies Rspamd host to use for scanning -=item B<--local> - -Should be used if Rspamd runs on the same machine and can access CGP files - =item B<--header> Specifies the header that should be added when Rspamd action is B -- 2.39.5