Skip to content

Commit

Permalink
Migrate MailScanner perl scripts to DBD:MariaDB
Browse files Browse the repository at this point in the history
  • Loading branch information
endelwar committed May 17, 2024
1 parent f84d6b0 commit c6e8eee
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 108 deletions.
37 changes: 13 additions & 24 deletions MailScanner_perl_scripts/MailWatch.pm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ package MailScanner::CustomConfig;

use strict;
use DBI;
use DBD::MariaDB;
use utf8;
use Sys::Hostname;
use Storable(qw[freeze thaw]);
Expand Down Expand Up @@ -119,20 +120,20 @@ sub InitMailWatchLogging {
sub CheckSQLVersion {
# Prevent Logger from dying if connection fails
eval {
$dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host",
$dbh = DBI->connect("DBI:MariaDB:database=$db_name;host=$db_host",
$db_user, $db_pass,
{ PrintError => 0, AutoCommit => 1, RaiseError => 1, mysql_enable_utf8 => 1 }
{ PrintError => 0, AutoCommit => 1, RaiseError => 1 }
);
};
if ($@ || !$dbh) {
LogMessage("warn", "Unable to initialise database connection: $DBI::errstr");
close(SERVER);
return 1;
}
$SQLversion = $dbh->{mysql_serverversion};
$SQLversion = $dbh->{mariadb_serverversion};
$dbh->disconnect;
return $SQLversion;

return $SQLversion;
}

sub LogMessage {
Expand Down Expand Up @@ -177,29 +178,17 @@ sub InitDB {
return 1;
}

if (CheckSQLVersion() >= 50503 ) {
eval { $dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host",
$db_user, $db_pass,
{ PrintError => 0, AutoCommit => 1, RaiseError => 1, mysql_enable_utf8mb4 => 1 }
);
};
if ($@ || !$dbh) {
LogMessage('warn', "Unable to initialise database connection: $DBI::errstr");
return 1;
}
$dbh->do('SET NAMES utf8mb4');
} else {
eval { $dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host",
eval { $dbh = DBI->connect("DBI:MariaDB:database=$db_name;host=$db_host",
$db_user, $db_pass,
{ PrintError => 0, AutoCommit => 1, RaiseError => 1, mysql_enable_utf8 => 1 }
{ PrintError => 0, AutoCommit => 1, RaiseError => 1 }
);
};
if ($@ || !$dbh) {
LogMessage('warn', "Unable to initialise database connection: $DBI::errstr");
return 1;
}
$dbh->do('SET NAMES utf8');
};
if ($@ || !$dbh) {
LogMessage('warn', "Unable to initialise database connection: $DBI::errstr");
return 1;
}
$dbh->do('SET NAMES utf8mb4');

$sth = $dbh->prepare("INSERT INTO maillog (timestamp, id, size, from_address, from_domain, to_address, to_domain, subject, clientip, archive, isspam, ishighspam, issaspam, isrblspam, spamwhitelisted, spamblacklisted, sascore, spamreport, virusinfected, nameinfected, otherinfected, report, ismcp, ishighmcp, issamcp, mcpwhitelisted, mcpblacklisted, mcpsascore, mcpreport, hostname, date, time, headers, quarantined, rblspamreport, token, messageid) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
if (!$sth) {
LogMessage('warn', "Error: $DBI::errstr" );
Expand Down
47 changes: 17 additions & 30 deletions MailScanner_perl_scripts/SQLBlackWhiteList.pm
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use vars qw($VERSION);
$VERSION = substr q$Revision: 1.5 $, 10;

use DBI;
use DBD::MariaDB;
my (%Whitelist, %Blacklist);
my ($wtime, $btime);
my ($dbh);
Expand All @@ -61,23 +62,23 @@ my ($db_pass) = mailwatch_get_db_password();
# Get refresh time from from 00MailWatchConf.pm
my ($bwl_refresh_time) = mailwatch_get_BWL_refresh_time();

# Check MySQL version
# Check MySQL/MariaDB version
sub CheckSQLVersion {
# Prevent dying from failed db connection
eval {
$dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host",
$dbh = DBI->connect("DBI:MariaDB:database=$db_name;host=$db_host",
$db_user, $db_pass,
{ PrintError => 0, AutoCommit => 1, RaiseError => 1, mysql_enable_utf8 => 1 }
{ PrintError => 0, AutoCommit => 1, RaiseError => 1 }
);
};
if ($@ || !$dbh) {
MailScanner::Log::WarnLog("MailWatch: SQLBlackWhiteList:: Unable to initialise database connection: %s", $DBI::errstr);
return 1;
}
$SQLversion = $dbh->{mysql_serverversion};
$SQLversion = $dbh->{mariadb_serverversion};
$dbh->disconnect;
return $SQLversion;

return $SQLversion;
}

#
Expand Down Expand Up @@ -135,39 +136,25 @@ sub CreateList {
my ($type, $BlackWhite) = @_;
my ($sql, $to_address, $from_address, $count, $filter);

# Check if MySQL is >= 5.3.3
# Check if MySQL/MariaDB is >= 5.3.3
my $version = CheckSQLVersion();

# Database connection failed, so return 0 (count) and exit early
if ($version == 1) {
return 0;
}

if ($version >= 50503 ) {
eval {
$dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host",
$db_user, $db_pass,
{ PrintError => 0, AutoCommit => 1, RaiseError => 1, mysql_enable_utf8mb4 => 1 }
);
};
if ($@ || !$dbh) {
MailScanner::Log::WarnLog("MailWatch: SQLBlackWhiteList::CreateList::: Unable to initialise database connection: %s", $DBI::errstr);
return 0;
}
$dbh->do('SET NAMES utf8mb4');
} else {
eval {
$dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host",
$db_user, $db_pass,
{ PrintError => 0, AutoCommit => 1, RaiseError => 1, mysql_enable_utf8 => 1 }
);
};
if ($@ || !$dbh) {
MailScanner::Log::WarnLog("MailWatch: SQLBlackWhiteList::CreateList::: Unable to initialise database connection: %s", $DBI::errstr);
return 0;
}
$dbh->do('SET NAMES utf8');
eval {
$dbh = DBI->connect("DBI:MariaDB:database=$db_name;host=$db_host",
$db_user, $db_pass,
{ PrintError => 0, AutoCommit => 1, RaiseError => 1 }
);
};
if ($@ || !$dbh) {
MailScanner::Log::WarnLog("MailWatch: SQLBlackWhiteList::CreateList::: Unable to initialise database connection: %s", $DBI::errstr);
return 0;
}
$dbh->do('SET NAMES utf8mb4');

# Uncommet the folloging line when debugging SQLBlackWhiteList.pm
#MailScanner::Log::WarnLog("MailWatch: DEBUG SQLBlackWhiteList: CreateList: %s", Dumper($BlackWhite));
Expand Down
82 changes: 28 additions & 54 deletions MailScanner_perl_scripts/SQLSpamSettings.pm
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use vars qw($VERSION);
$VERSION = substr q$Revision: 1.5 $, 10;

use DBI;
use DBD::MariaDB;
my ($dbh);
my ($sth);
my ($SQLversion);
Expand All @@ -64,20 +65,22 @@ my ($db_pass) = mailwatch_get_db_password();
# Get refresh time from from 00MailWatchConf.pm
my ($ss_refresh_time) = mailwatch_get_SS_refresh_time();

# Check MySQL version
# Check MySQL/MariaDB version
sub CheckSQLVersion {
# Prevent dying from failed db connection
eval {
$dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host",
$dbh = DBI->connect("DBI:MariaDB:database=$db_name;host=$db_host",
$db_user, $db_pass,
{ PrintError => 0, AutoCommit => 1, RaiseError => 1, mysql_enable_utf8 => 1 }
{ PrintError => 0, AutoCommit => 1, RaiseError => 1 }
);
};
if ($@ || !$dbh) {
MailScanner::Log::WarnLog("MailWatch: SQLSpamSettings:: Unable to initialise database connection: %s", $DBI::errstr);
return 1;
}
$SQLversion = $dbh->{mysql_serverversion};
$SQLversion = $dbh->{mariadb_serverversion};
$dbh->disconnect;

return $SQLversion;
}

Expand Down Expand Up @@ -169,39 +172,25 @@ sub CreateScoreList
my ($type, $UserList) = @_;
my ($sql, $username, $count);

# Check if MySQL is >= 5.3.3
# Check if MySQL/MariaDB is >= 5.3.3
my $version = CheckSQLVersion();

# Cannot get SQL version, bail out with count of 0
if ($version == 1) {
return 0;
}

if ($version >= 50503 ) {
eval {
$dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host",
$db_user, $db_pass,
{ PrintError => 0, AutoCommit => 1, RaiseError => 1, mysql_enable_utf8mb4 => 1 }
);
};
if ($@ || !$dbh) {
MailScanner::Log::WarnLog("MailWatch: SQLSpamSettings:: CreateScoreList::: Unable to initialise database connection: %s", $DBI::errstr);
return 0;
}
$dbh->do('SET NAMES utf8mb4');
} else {
eval {
$dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host",
$db_user, $db_pass,
{ PrintError => 0, AutoCommit => 1, RaiseError => 1, mysql_enable_utf8 => 1 }
);
};
if ($@ || !$dbh) {
MailScanner::Log::WarnLog("MailWatch: SQLSpamSettings::CreateScoreList::: Unable to initialise database connection: %s", $DBI::errstr);
return 0;
}
$dbh->do('SET NAMES utf8');
eval {
$dbh = DBI->connect("DBI:MariaDB:database=$db_name;host=$db_host",
$db_user, $db_pass,
{ PrintError => 0, AutoCommit => 1, RaiseError => 1 }
);
};
if ($@ || !$dbh) {
MailScanner::Log::WarnLog("MailWatch: SQLSpamSettings:: CreateScoreList::: Unable to initialise database connection: %s", $DBI::errstr);
return 0;
}
$dbh->do('SET NAMES utf8mb4');

$sql = "SELECT username, $type FROM users WHERE $type > 0";
$sth = $dbh->prepare($sql);
Expand All @@ -228,32 +217,17 @@ sub CreateNoScanList
my ($type, $NoScanList) = @_;
my ($sql, $username, $count);

# Check if MySQL is >= 5.3.3
if (CheckSQLVersion() >= 50503 ) {
eval {
$dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host",
$db_user, $db_pass,
{ PrintError => 0, AutoCommit => 1, RaiseError => 1, mysql_enable_utf8mb4 => 1 }
);
};
if ($@ || !$dbh) {
MailScanner::Log::WarnLog("MailWatch: SQLSpamSettings::CreateNoScanList::: Unable to initialise database connection: %s", $DBI::errstr);
return 0;
}
$dbh->do('SET NAMES utf8mb4');
} else {
eval {
$dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host",
$db_user, $db_pass,
{ PrintError => 0, AutoCommit => 1, RaiseError => 1, mysql_enable_utf8 => 1 }
);
};
if ($@ || !$dbh) {
MailScanner::Log::WarnLog("MailWatch: SQLSpamSettings::CreateNoScanList::: Unable to initialise database connection: %s", $DBI::errstr);
return 0;
}
$dbh->do('SET NAMES utf8');
eval {
$dbh = DBI->connect("DBI:MariaDB:database=$db_name;host=$db_host",
$db_user, $db_pass,
{ PrintError => 0, AutoCommit => 1, RaiseError => 1 }
);
};
if ($@ || !$dbh) {
MailScanner::Log::WarnLog("MailWatch: SQLSpamSettings::CreateNoScanList::: Unable to initialise database connection: %s", $DBI::errstr);
return 0;
}
$dbh->do('SET NAMES utf8mb4');

$sql = "SELECT username, $type FROM users WHERE $type > 0";
$sth = $dbh->prepare($sql);
Expand Down

0 comments on commit c6e8eee

Please sign in to comment.