MailWatch does not log messages

Report bugs and workarounds
Post Reply
gh0stwizard
Posts: 4
Joined: 04 Jul 2019 08:54

MailWatch does not log messages

Post by gh0stwizard »

Hello,
I have found a bug in the Perl module DBD::mysql when working with MariaDB. The issue caused by incorrect MySQL version check in the file /etc/MailScanner/custom/MailWatch.pm Here is the snippet of the function:

Code: Select all

sub CheckSQLVersion {
    $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: Unable to initialise database connection: %s", $DBI::errstr);
    }
    $SQLversion = $dbh->{mysql_serverversion};
    $dbh->disconnect;
    return $SQLversion
}
The value of $dbh->{mysql_serverversion} in my case is empty, so the check CheckSQLVersion() >= 50503 in the function InitConnection() has failed.
Because I am using a fresh install of EFA 3.0.2.6, I did a workaround to force using the option mysql_enable_utf8mb4. The patch is below:

Code: Select all

--- /etc/MailScanner/custom/MailWatch.pm.orig   2019-07-04 12:30:40.223164077 +0300
+++ /etc/MailScanner/custom/MailWatch.pm        2019-07-04 12:31:31.656283396 +0300
@@ -115,7 +115,7 @@
     listen(SERVER, SOMAXCONN) or exit;

     # Our reason for existence - the persistent connection to the database
-    if (CheckSQLVersion() >= 50503 ) {
+    if (CheckSQLVersion() >= 50503 || !CheckSQLVersion()) {
         $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 }
Why is this important?

If a mysql (MariaDB) client is connected using a simple utf8 encoding then trying to insert utf8 characters with 4 bytes will fail. An example output (got by tracing output of /etc/MailScanner/custom/MailWatch.pm):

Code: Select all

!! ERROR: '1366' 'Incorrect string value: '\xF0\x9F\x99\x80 \xD0...' for column `mailscanner`.`maillog`.`subject` at row 1' (err#0)
The "\xF0\x9F\x99\x80" is the "weary cat face" emoticon in unicode (https://codepoints.net/U+1F640?lang=en). See also https://medium.com/@adamhooper/in-mysql ... 761243e434

To check your EFA against this problem you may use the command below. The subject value got from real email to be clear.

Code: Select all

swaks -s <EFA_SERVER> --header 'Subject: =?UTF-8?B?8J+ZgCDQp9GC0L4g0YLQsNC60L7QtSDQutC10LnRgdGLPw==?=' --to <TO_ADDRESS>
Change <EFA_SERVER> to localhost if running the command from the host where EFA installed or type your EFA server.
Change <TO_ADDRESS> to your email address.

The root cause why the value of $dbh->{mysql_serverversion} is empty unclear to me. Folk say that is well known issue: https://github.com/perl5-dbi/DBD-mysql/issues/248 I guess this is because of updating to MariaDB.

I have two servers:
1. With MariaDB 10.1.38-1: $dbh->{mysql_serverversion} is empty, but there are no insert errors, the message correctly logs to DB.
2. With MariaDB 10.1.40-1: $dbh->{mysql_serverversion} is empty, but there is an error as described above.

Also, I have tried to recompile and reinstall perl-DBD-MySQL rpm on the server with MariaDB 10.1.40-1 myself, but this not helped.

Hope this help. Thanks.
User avatar
shawniverson
Posts: 3640
Joined: 13 Jan 2014 23:30
Location: Indianapolis, Indiana USA
Contact:

Re: MailWatch does not log messages

Post by shawniverson »

v4 does not have this issue, which I'm working on some more today :)
Post Reply