]> git.proxmox.com Git - pmg-api.git/blobdiff - PMG/DBTools.pm
node: add journal api to index too
[pmg-api.git] / PMG / DBTools.pm
index 30a6aedb8771439e9c8cdb7ef69b3ea29c06c4e9..464b0136644470fde12394a532a9efd30a417904 100644 (file)
@@ -4,7 +4,7 @@ use strict;
 use warnings;
 
 use POSIX ":sys_wait_h";
-use POSIX ':signal_h';
+use POSIX qw(:signal_h getuid);
 use DBI;
 use Time::Local;
 
@@ -80,12 +80,17 @@ sub postgres_admin_cmd {
     my ($cmd, $options, @params) = @_;
 
     $cmd = ref($cmd) ? $cmd : [ $cmd ];
-    my $uid = getpwnam('postgres') || die "getpwnam postgres failed\n";
 
-    local $> = $uid;
-    $! &&  die "setuid postgres ($uid) failed - $!\n";
+    my $save_uid = POSIX::getuid();
+    my $pg_uid = getpwnam('postgres') || die "getpwnam postgres failed\n";
+
+    PVE::Tools::setresuid(-1, $pg_uid, -1) ||
+       die "setresuid postgres ($pg_uid) failed - $!\n";
 
     PVE::Tools::run_command([@$cmd, '-U', 'postgres', @params], %$options);
+
+    PVE::Tools::setresuid(-1, $save_uid, -1) ||
+       die "setresuid back failed - $!\n";
 }
 
 sub delete_ruledb {
@@ -146,7 +151,7 @@ __EOD
 
 my $local_stat_ctablecmd =  <<__EOD;
     CREATE TABLE LocalStat
-    (Time INTEGER NOT NULL UNIQUE,
+    (Time INTEGER NOT NULL,
      RBLCount INTEGER DEFAULT 0 NOT NULL,
      PregreetCount INTEGER DEFAULT 0 NOT NULL,
      CID INTEGER NOT NULL,
@@ -341,6 +346,19 @@ sub database_column_exists {
     return defined($res);
 }
 
+my $createdb = sub {
+    my ($dbname) = @_;
+    postgres_admin_cmd(
+       'createdb',
+       undef,
+       '-E', 'sql_ascii',
+       '-T', 'template0',
+       '--lc-collate=C',
+       '--lc-ctype=C',
+       $dbname,
+    );
+};
+
 sub create_ruledb {
     my ($dbname) = @_;
 
@@ -355,8 +373,7 @@ sub create_ruledb {
     # use sql_ascii to avoid any character set conversions, and be compatible with
     # older postgres versions (update from 8.1 must be possible)
 
-    postgres_admin_cmd('createdb', undef, '-E', 'sql_ascii', '-T', 'template0',
-                      '--lc-collate=C', '--lc-ctype=C', $dbname);
+    $createdb->($dbname);
 
     my $dbh = open_ruledb($dbname);
 
@@ -491,6 +508,10 @@ sub upgradedb {
                 "PregreetCount INTEGER DEFAULT 0 NOT NULL");
     }
 
+    eval { $dbh->do("ALTER TABLE LocalStat DROP CONSTRAINT localstat_time_key"); };
+    # ignore errors here
+
+
     # add missing TicketID to CMSReceivers
     if (!database_column_exists($dbh, 'CMSReceivers', 'TicketID')) {
        eval {
@@ -782,7 +803,7 @@ sub init_ruledb {
     }
 
     # Quarantine/Mark Spam (Level 5)
-    $rule = PMG::RuleDB::Rule->new ('Quarantine/Mark Spam (Level 5)', 79, 0, 0);
+    $rule = PMG::RuleDB::Rule->new ('Quarantine/Mark Spam (Level 5)', 81, 0, 0);
     $ruledb->save_rule ($rule);
 
     $ruledb->rule_add_what_group ($rule, $spam5);
@@ -790,7 +811,7 @@ sub init_ruledb {
     $ruledb->rule_add_action ($rule, $quarantine);
 
     ## Block Spam Level 10
-    $rule = PMG::RuleDB::Rule->new ('Block Spam (Level 10)', 78, 0, 0);
+    $rule = PMG::RuleDB::Rule->new ('Block Spam (Level 10)', 82, 0, 0);
     $ruledb->save_rule ($rule);
 
     $ruledb->rule_add_what_group ($rule, $spam10);
@@ -1142,7 +1163,7 @@ sub init_nodedb {
 
        print STDERR "create new local database\n";
 
-       postgres_admin_cmd('createdb', undef, $dbname);
+       $createdb->($dbname);
 
        print STDERR "insert received data into local database\n";
 
@@ -1223,20 +1244,19 @@ sub cluster_sync_status {
 }
 
 sub load_mail_data {
-    my ($dbh, $cid, $rid, $pmail) = @_;
+    my ($dbh, $cid, $rid, $ticketid) = @_;
 
     my $sth = $dbh->prepare(
        "SELECT * FROM CMailStore, CMSReceivers WHERE " .
-       ($pmail ? 'pmail = ' . $dbh->quote($pmail) . " AND " : '') .
-       "CID = $cid and RID = $rid AND " .
+       "CID = ? AND RID = ? AND TicketID = ? AND " .
        "CID = CMailStore_CID AND RID = CMailStore_RID");
-    $sth->execute ();
+    $sth->execute($cid, $rid, $ticketid);
 
     my $res = $sth->fetchrow_hashref();
 
     $sth->finish();
 
-    die "no such mail (C${cid}R${rid})\n" if !defined($res);
+    die "no such mail (C${cid}R${rid}T${ticketid})\n" if !defined($res);
 
     return $res;
 }