From e5bef3555741aff3c32271fa1358ea3eb4329bfa Mon Sep 17 00:00:00 2001 From: Eduardo M KALINOWSKI Date: Sat, 4 Jun 2022 17:10:29 -0300 Subject: Adjustments to make insertion in PgSQL work Objects with database-specific definitions for each database type were created to make that possible. --- dbx_Pg.pl | 9 +++++++++ dbx_mysql.pl | 9 +++++++++ dmarcts-report-parser.pl | 10 ++++++++-- 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 dbx_Pg.pl create mode 100644 dbx_mysql.pl diff --git a/dbx_Pg.pl b/dbx_Pg.pl new file mode 100644 index 0000000..46bb7f0 --- /dev/null +++ b/dbx_Pg.pl @@ -0,0 +1,9 @@ +%dbx = ( + epoch_to_timestamp_fn => 'TO_TIMESTAMP', + to_hex_string => sub { + my ($bin) = @_; + return "'\\x" . unpack("H*", $bin) . "'"; + }, +); + +1; diff --git a/dbx_mysql.pl b/dbx_mysql.pl new file mode 100644 index 0000000..3c03bc6 --- /dev/null +++ b/dbx_mysql.pl @@ -0,0 +1,9 @@ +%dbx = ( + epoch_to_timestamp_fn => 'FROM_UNIXTIME', + to_hex_string => sub { + my ($bin) = @_; + return "X'" . unpack("H*", $bin) . "'"; + }, +); + +1; diff --git a/dmarcts-report-parser.pl b/dmarcts-report-parser.pl index 5b240c4..aba9a85 100755 --- a/dmarcts-report-parser.pl +++ b/dmarcts-report-parser.pl @@ -264,6 +264,12 @@ if (exists $options{delete}) {$delete_reports = 1;} if (exists $options{info}) {$processInfo = 1;} # Setup connection to database server. +our %dbx; +my $dbx_file = File::Basename::dirname($0) . "/dbx_$dbtype.pl"; +my $dbx_return = do $dbx_file; +die "$scriptname: couldn't load DB definition for type $dbtype: $@" if $@; +die "$scriptname: couldn't load DB definition for type $dbtype: $!" unless defined $dbx_return; + my $dbh = DBI->connect("DBI:$dbtype:database=$dbname;host=$dbhost;port=$dbport", $dbuser, $dbpass) or die "$scriptname: Cannot connect to database\n"; @@ -833,7 +839,7 @@ sub storeXMLInDatabase { } my $sql = qq{INSERT INTO report(mindate,maxdate,domain,org,reportid,email,extra_contact_info,policy_adkim, policy_aspf, policy_p, policy_sp, policy_pct, raw_xml) - VALUES(FROM_UNIXTIME(?),FROM_UNIXTIME(?),?,?,?,?,?,?,?,?,?,?,?)}; + VALUES($dbx{epoch_to_timestamp_fn}(?),$dbx{epoch_to_timestamp_fn}(?),?,?,?,?,?,?,?,?,?,?,?)}; my $storexml = $xml->{'raw_xml'}; if ($compress_xml) { my $gzipdata; @@ -985,7 +991,7 @@ sub storeXMLInDatabase { $ipval = unpack "N", $nip; $iptype = "ip"; } elsif($nip = inet_pton(AF_INET6, $ip)) { - $ipval = "X'" . unpack("H*",$nip) . "'"; + $ipval = $dbx{to_hex_string}($nip); $iptype = "ip6"; } else { warn "$scriptname: $org: $id: ??? mystery ip $ip\n"; -- cgit v1.2.3