trAvis - MANAGER
Edit File: CloudmarkClient.pm
# $Id: CMAE.pm 36091 2008-11-14 18:35:52Z jrobin $ package Mail::SpamAssassin::Plugin::CloudmarkClient; use Mail::SpamAssassin::Plugin; use Mail::SpamAssassin::Logger; use LWP::UserAgent; use JSON; use strict; use warnings; use bytes; use vars qw(@ISA); @ISA = qw(Mail::SpamAssassin::Plugin); #Makefile will extract this my $VERSION = "2.0.0.5"; #install script will extract these my $CONFIG_DIR = "/etc/mail/spamassassin"; my $CONFIG_FILE = "sa_authority_plugin.cfg"; sub new { my $class = shift; my $mailsaobject = shift; # configuration file for this filter my $CONFIG = "$CONFIG_DIR/$CONFIG_FILE"; $class = ref($class) || $class; my $self = $class->SUPER::new($mailsaobject); bless ($self, $class); $self->register_eval_rule("check_msg"); $self->read_conf($CONFIG); dbg("CloudmarkClient: Authority SpamAssassin " . $VERSION . " started up."); return $self; } sub check_msg() { my ($self, $permsgstatus, $full) = @_; ################## # Config Options # ################## my $HOSTURL = $$self{conf}{'auth-server url'} || 'http://localhost:2780'; my $THRESHOLD = $$self{conf}{'spam score threshold'} || 0; my $SHOW_CATS = $$self{conf}{'show categories'} || 'no'; my $LOG_ANAYLSIS = $$self{conf}{'log analysis string'} || 'no'; my $formuri = $HOSTURL."/v1/score_message"; my $msgszuri = $HOSTURL."/v1/max_message_size"; # create user agent object my $ua = new LWP::UserAgent; my $response; my $content; my $json; my $decoded; #get max message size $response = $ua->get($msgszuri); # parse response; if successful, decode json and extract max message size if (!$response->is_success) { dbg("CloudmarkClient: Server error: " . $response->status_line ); return 0; } $content = $response->decoded_content; # parse JSON response $json = new JSON; $decoded = eval { $json->decode($content); }; if ($@) { dbg("CloudmarkClient: JSON error: " . $@ ); return 0; } my $max_message_size = $decoded->{"max_message_size"}; if (!$max_message_size) { dbg("CloudmarkClient: No max message size returned"); return 0; } my $ref_msg = (length($$full) >= $max_message_size) ? \(substr($$full, 0, $max_message_size-1)) : $full; #my $score = 0; #my $category = 0; #my $sub_category = 0; #my $rescan = 0; #my $analysis = ''; # send request to server my $form; $form = { rfc822 => $$ref_msg }; # Note that we are using multipart/form-data content type, # to comply with Authority HTTP Server's handling of form uploads. # See RFC2388 (http://www.ietf.org/rfc/rfc2388.txt) for more info. $response = $ua->post($formuri, Content_Type => 'form-data', Content => $form); # # parse response; if successful, decode json and extract analysis if (!$response->is_success) { dbg("CloudmarkClient: Server error: " . $response->status_line ); return 0; } $content = $response->decoded_content; # parse JSON response $json = new JSON; $decoded = eval { $json->decode($content); }; if ($@) { dbg("CloudmarkClient: JSON error: " . $@ ); return 0; } my $analysis = $decoded->{"analysis"}; my $score = $decoded->{"score"}; if (not defined($analysis)) { dbg("CloudmarkClient: No Analysis String returned"); return 0; } if (not defined($score)) { dbg("CloudmarkClient: No Score returned"); return 0; } my $header = "$analysis"; if ($SHOW_CATS eq 'yes') { my $result_attributes = $decoded->{"result_attributes"}; if ($result_attributes) { foreach my $row (@{ $result_attributes}) { if ($row->{"attribute"} eq "category:sub-category pair") { my $catstr = $row->{"value"}; $catstr =~ /([^:]*)(?::(.*))?/; my $cat = $1 || ''; my $subcat = $2 || ''; $subcat =~ s/[[:punct:]\s]/_/g; $header .= " xcat=$cat/$subcat"; last; } } } } if ($LOG_ANAYLSIS eq 'yes') { $permsgstatus->set_spamd_result_item( sub { "cmae_analysis=$analysis"; } ); } # Add a X header with analysis header. $permsgstatus->set_tag("CMCLTAG", $header); if ($score >= $THRESHOLD) { return $score; } else { return 0; } } sub read_conf() { my ($self, $file) = @_; if (! open CFILE, "<$file") { dbg("CloudmarkClient: Couldn't read config file: " . $file . ". Defaults will be used\n"); return 0; } for (<CFILE>) { next if /^\s*#/; # ignore comments next unless /=/; # ignore if not a config option chomp; # remove trailing [\r\n] s/#.*$//g; # remove trailing comments my ($option, $value) = split /\s*=\s*/, $_, 2; if ($option) { $option =~ s/\s*$//g; # remove trailing spaces $value =~ s/\s*$//g; # remove trailing spaces $$self{conf}{$option} = $value; } } } ########################################################################### 1; # Copyright (c) 2015, Cloudmark, Inc. # All rights reserved. # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of Cloudmark, Inc. nor the # names of its contributors may be used to endorse or promote products # derived from this software without specific prior written permission. # THIS SOFTWARE IS PROVIDED BY CLOUDMARK, INC. ``AS IS'' AND ANY # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL CLOUDMARK, INC. BE LIABLE FOR ANY # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # EXPORT LAWS: THIS LICENSE ADDS NO RESTRICTIONS TO THE EXPORT LAWS OF YOUR JURISDICTION. It is licensee's responsibility to comply with any export regulations applicable in licensee's jurisdiction. Under CURRENT (May 2015) U.S. export regulations this software is eligible for export from the U.S. and can be downloaded by or otherwise exported or reexported worldwide EXCEPT to U.S. embargoed destinations which include Cuba, Iraq, Libya, North Korea, Iran, Syria, Sudan, Afghanistan and any other country to which the U.S. has embargoed goods and services.