MOON
Server: Apache/2.2.31 (Unix) mod_ssl/2.2.31 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4
System: Linux csr818.wilogic.com 2.6.18-419.el5xen #1 SMP Fri Feb 24 22:50:37 UTC 2017 x86_64
User: digitals (531)
PHP: 5.4.45
Disabled: NONE
Upload Files
File: //scripts.20110531.215904.25158/update_sa_rules
#!/usr/bin/perl
# cpanel - update_sa_rules                        Copyright(c) 2010 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

BEGIN { unshift @INC, '/usr/local/cpanel', '/usr/local/cpanel/spamassassin_sandbox'; }

eval 'local $SIG{__DIE__}; use Cpanel::SpamAssassinSandBox;';    #issafe #nomunge
use Cpanel::SafeRun::Errors      ();
use Cpanel::StringFunc           ();
use Cpanel::FindBin              ();
use Cpanel::FileUtils::TouchFile ();
use Cpanel::LoadFile             ();
use Cpanel::Logger               ();
use Cpanel::SafeFile             ();
use Cpanel::Rand                 ();
use Cpanel::SafeDir              ();
use Cpanel::SafeDir::MK          ();
use Getopt::Long;
eval 'use Mail::SpamAssassin;';

my $help    = 0;
my $verbose = 0;

GetOptions( 'help' => \$help, 'verbose' => \$verbose );

usage() if $help;

my $logger = Cpanel::Logger->new();

my $envtype = Cpanel::LoadFile::loadfile('/var/cpanel/envtype');
if ( $envtype && $envtype ne 'standard' ) {
    Cpanel::StringFunc::regsrep( '/etc/mail/spamassassin/v310.pre', '^\s*loadplugin Mail::SpamAssassin::Plugin::SpamCop', '#loadplugin Mail::SpamAssassin::Plugin::SpamCop' );
}

my $re2c_bin  = Cpanel::FindBin::findbin( 're2c',  'path' => [ '/usr/sbin', '/usr/local/sbin', '/usr/bin', '/usr/local/bin' ] );
my $spamd_bin = Cpanel::FindBin::findbin( 'spamd', 'path' => [ '/usr/sbin', '/usr/local/sbin', '/usr/bin', '/usr/local/bin' ] );
if ( !$spamd_bin ) {
    print "Spamd binary not found.  Is SpamAssassin installed?\n" if $verbose;
    exit;
}

my @SPAMD_CODE;
my $hassandbox  = 0;
my $haspodusage = 0;
my $block       = Cpanel::SafeFile::safeopen( \*SB, '+<', $spamd_bin );
if ( !$block ) {
    $logger->die("Could not edit $spamd_bin");
}
while ( readline(SB) ) {
    push @SPAMD_CODE, $_;
    if (/^\s*\S+\s*Pod::Usage/) {
        $haspodusage = 1;
    }
    if (/spamassassin_sandbox/) {
        $hassandbox = 1;
    }
}
if ( !$hassandbox || $haspodusage ) {
    my $added_inc = 0;
    seek( SB, 0, 0 );
    foreach (@SPAMD_CODE) {
        if (/^\s*#/) {
            print SB $_;
        }
        elsif (/^\s*\S+\s*Pod::Usage;/) {
            next;
        }
        elsif (/pod2usage\s*\([^\)]*\)/) {
            next;
        }
        elsif (/pod2usage\s*\(/) {
            $find_end_sub = 1;
            next;
        }
        elsif ( $find_end_sub && /^\s+\);\s+$/ ) {
            $find_end_sub = 0;
            next;
        }
        elsif ($find_end_sub) {
            next;
        }
        elsif ( !$added_inc && /^\s*BEGIN\s*{\s*$/ ) {
            $added_inc = 1;
            print SB $_;
            print SB 'unshift(@INC, \'/usr/local/cpanel/spamassassin_sandbox\');' . "\n";
            print "Applied Cpanel::SpamAssasinSandBox memory reduction to spamd\n" if $verbose;    #issafe
        }
        else {
            print SB $_;
        }
    }
    truncate( SB, tell(SB) );
}
Cpanel::SafeFile::safeclose( \*SB, $block );

if ( -e '/usr/local/cpanel/src/spamassassin/ResourceLimits.pm' ) {
    my $rslimit = 0;
    eval 'use Mail::SpamAssassin::Plugin::ResourceLimits; $rslimit=1; ';
    if ( !$rslimit ) {
        my $module_path = $INC{'Mail/SpamAssassin.pm'};
        $module_path =~ s/\.pm$//g;
        if ( !-e $module_path . '/Plugin/ResourceLimits.pm' ) {
            system( 'cp', '-f', '--', '/usr/local/cpanel/src/spamassassin/ResourceLimits.pm', $module_path . '/Plugin/ResourceLimits.pm' );
        }
    }

    eval 'use Mail::SpamAssassin::Plugin::ResourceLimits; $rslimit=1; ';
    if ($rslimit) {
        my $block = Cpanel::SafeFile::safeopen( \*SB, '+<', '/etc/mail/spamassassin/local.cf' );
        if ( !$block ) {
            $logger->die("Could not edit /etc/mail/spamassassin/local.cf");
        }
        my @local_cf = <SB>;
        if ( !grep( /ResourceLimits/, @local_cf ) ) {
            print "Enabled SpamAssassin Resource Limits Plugin\n" if $verbose;
            print SB "\nloadplugin Mail::SpamAssassin::Plugin::ResourceLimits\n";
            print SB "resource_limit_mem 268435456\n\n";
        }
        #268435456 is required for 3.3.1 or later
        elsif ( grep(/resource_limit_mem 134217728/, @local_cf)) {
            @local_cf = map { s/resource_limit_mem 134217728/resource_limit_mem 268435456/; $_ } @local_cf;
            seek(SB,0,0);
            print SB join('',@local_cf);
            truncate(SB,tell(SB));
        }
        Cpanel::SafeFile::safeclose( \*SB, $block );
    }
}

my $spamassassin_bin = Cpanel::FindBin::findbin( 'spamassassin', 'path' => [ '/usr/sbin', '/usr/local/sbin', '/usr/bin', '/usr/local/bin' ] );
my $has_rules = 1;
if ( -x $spamassassin_bin ) {
    my @testout = Cpanel::SafeRun::Errors::saferunallerrors( $spamassassin_bin, '--lint', '-D' );
    if ( grep ( /no rules were found/, @testout ) ) {
        $has_rules = 0;
    }
}

if ( !$has_rules ) {
    print "spamassassin rule update has not yet run.\n";
    system '/scripts/sa-update_wrapper';
}

my ($perl_version) = ( $] =~ /^(\d\.\d\d\d)/ );

my $dir_sa_compiled = (
    -e "/var/lib/spamassassin/compiled/$perl_version/$Mail::SpamAssassin::VERSION"
    ? "/var/lib/spamassassin/compiled/$perl_version/$Mail::SpamAssassin::VERSION"
    : "/var/lib/spamassassin/compiled/$Mail::SpamAssassin::VERSION"
);

my $saxtest = Cpanel::SafeRun::Errors::saferunnoerror( '/scripts/test_sa_compiled', $dir_sa_compiled );
my $saxsok = ( $saxtest =~ /ok/ ? 1 : 0 );

# This module can give us up to 20% more cpu time free on a loaded mail server
Cpanel::StringFunc::regsrep( '/etc/mail/spamassassin/v320.pre', '^\s*#\s*loadplugin Mail::SpamAssassin::Plugin::Shortcircuit', 'loadplugin Mail::SpamAssassin::Plugin::Shortcircuit' );
if ( $re2c_bin && -x $re2c_bin ) {

    my $lastver       = Cpanel::LoadFile::loadfile('/var/cpanel/sa-compile-time');
    my $compile_mtime = ( stat('/var/cpanel/sa-compile-time') )[9] || 0;
    my $sa_rule_mtime = 0;
    opendir( my $sa_dh, '/usr/share/spamassassin' );
    while ( my $file = readdir($sa_dh) ) {
        my $this_mtime = ( stat( '/usr/share/spamassassin/' . $file ) )[9];
        if ( $this_mtime > $sa_rule_mtime ) {
            $sa_rule_mtime = $this_mtime;
        }
    }

    if ( !$saxsok || $sa_rule_mtime >= $compile_mtime || $lastver != $Mail::SpamAssassin::VERSION ) {
        my $sa_compile_bin = Cpanel::FindBin::findbin( 'sa-compile', 'path' => [ '/usr/sbin', '/usr/local/sbin', '/usr/bin', '/usr/local/bin' ] );
        if ($sa_compile_bin) {
            system_verbose_aware( '/scripts/fix_sa_compile', $sa_compile_bin ) if ( -x '/scripts/fix_sa_compile' );
            my $tmpdir = Cpanel::Rand::gettmpfile();
            if ( $tmpdir && Cpanel::SafeDir::MK::safemkdir( $tmpdir, 0700 ) ) {
                local $ENV{'TMP'}    = $tmpdir;
                local $ENV{'TMPDIR'} = $tmpdir;
                system_verbose_aware($sa_compile_bin);
                Cpanel::SafeDir::safermdir($tmpdir);
            }
            else {
                system_verbose_aware($sa_compile_bin);
            }
        }
    }

    system_verbose_aware( '/scripts/patch_mail_spamassassin_compiledregexps_body_0', $dir_sa_compiled );

    $saxtest = Cpanel::SafeRun::Errors::saferunnoerror( '/scripts/test_sa_compiled', $dir_sa_compiled );
    $saxsok = ( $saxtest =~ /ok/ ? 1 : 0 );
}

if ($saxsok) {
    open( my $sa_fh, '>', '/var/cpanel/sa-compile-time' );
    print $sa_fh $Mail::SpamAssassin::VERSION;
    close($sa_fh);
    print "Mail::SpamAssassin::Plugin::Rule2XSBody test passed: enabled\n" if $verbose;
    Cpanel::StringFunc::regsrep( '/etc/mail/spamassassin/v320.pre', '^\s*#\s*loadplugin Mail::SpamAssassin::Plugin::Rule2XSBody', 'loadplugin Mail::SpamAssassin::Plugin::Rule2XSBody' );
}
else {
    print "Mail::SpamAssassin::Plugin::Rule2XSBody is not available: disabled\n" if $verbose;
    Cpanel::StringFunc::regsrep( '/etc/mail/spamassassin/v320.pre', '^\s*loadplugin Mail::SpamAssassin::Plugin::Rule2XSBody', '#loadplugin Mail::SpamAssassin::Plugin::Rule2XSBody' );
}

sub system_verbose_aware {
    if ($verbose) {
        system(@_);
    }
    else {
        Cpanel::SafeRun::Errors::saferunnoerror(@_);
    }
}

sub usage {
    print q{
update_sa_rules [options]

    Options:
      --help       Brief help message
      --verbose    Display verbose output from each test preformed

};
}

1;

__END__

=head1 NAME

update_sa_rules - Optimize SpamAssassin if possible and update compiled rulesets

=head1 SYNOPSIS

update_sa_rules [options]

    Options:
      --help       Brief help message
      --verbose    Display verbose output from each test preformed

=cut