]> git.proxmox.com Git - pmg-api.git/blobdiff - src/PMG/Utils.pm
utils: postgres_admin_cmd chdir to / before running
[pmg-api.git] / src / PMG / Utils.pm
index 20686ad87bc6b1e34369b27505fd50ee24442921..52701a3bfe2605aff63bc53c06424a7ee30d0eec 100644 (file)
@@ -2,6 +2,7 @@ package PMG::Utils;
 
 use strict;
 use warnings;
+use Cwd;
 use DBI;
 use Net::Cmd;
 use Net::SMTP;
@@ -203,7 +204,7 @@ sub subst_values {
 }
 
 sub reinject_mail {
-    my ($entity, $sender, $targets, $xforward, $me, $nodsn) = @_;
+    my ($entity, $sender, $targets, $xforward, $me, $params) = @_;
 
     my $smtp;
     my $resid;
@@ -244,15 +245,28 @@ sub reinject_mail {
            $mail_opts .= " SMTPUTF8" if $has_utf8_targets;
        }
 
+       if (defined($params->{mail})) {
+           my $mailparams = $params->{mail};
+           for my $p (keys %$mailparams) {
+               $mail_opts .= " $p=$mailparams->{$p}";
+           }
+       }
+
        if (!$smtp->_MAIL("FROM:" . $sender_addr . $mail_opts)) {
            syslog('err', "smtp error - got: %s %s", $smtp->code, scalar ($smtp->message));
            die "smtp from: ERROR";
        }
 
-       my $rcpt_opts = $nodsn ? " NOTIFY=NEVER" : "";
-
        foreach my $target (@$targets) {
            my $rcpt_addr;
+           my $rcpt_opts = '';
+           if (defined($params->{rcpt}->{$target})) {
+               my $rcptparams = $params->{rcpt}->{$target};
+               for my $p (keys %$rcptparams) {
+                   $rcpt_opts .= " $p=$rcptparams->{$p}";
+               }
+           }
+
            if (utf8::is_utf8($target)) {
                $rcpt_addr = encode('UTF-8', $smtp->_addr($target));
            } else {
@@ -1370,6 +1384,10 @@ sub postgres_admin_cmd {
     my $save_uid = POSIX::getuid();
     my $pg_uid = getpwnam('postgres') || die "getpwnam postgres failed\n";
 
+    #cd to / to prevent warnings on EPERM (e.g. when running in /root)
+    my $cwd = getcwd() || die "getcwd failed\n";
+    ($cwd) = ($cwd =~ m|^(/.*)$|); #untaint
+    chdir('/') || die "could not chdir to '/'\n";
     PVE::Tools::setresuid(-1, $pg_uid, -1) ||
        die "setresuid postgres ($pg_uid) failed - $!\n";
 
@@ -1377,6 +1395,8 @@ sub postgres_admin_cmd {
 
     PVE::Tools::setresuid(-1, $save_uid, -1) ||
        die "setresuid back failed - $!\n";
+
+    chdir("$cwd") || die "could not chdir back to $cwd\n";
 }
 
 sub get_pg_server_version {
@@ -1505,4 +1525,21 @@ sub update_local_spamassassin_channels {
     return $fresh_updates
 }
 
+sub get_existing_object_id {
+    my ($dbh, $obj_id, $obj_type, $value) = @_;
+
+    my $sth = $dbh->prepare("SELECT id FROM Object WHERE ".
+       "Objectgroup_ID = ? AND ".
+       "ObjectType = ? AND ".
+       "Value = ?"
+    );
+    $sth->execute($obj_id, $obj_type, $value);
+
+    if (my $ref = $sth->fetchrow_hashref()) {
+       return $ref->{id};
+    }
+
+    return;
+}
+
 1;