[pmg-devel] [PATCH pmg-api v2] utils: postgres_admin_cmd chdir to / before running

Stoiko Ivanov s.ivanov at proxmox.com
Wed Dec 1 19:08:26 CET 2021


postgres_admin_cmd switches user to the postgres user.

When running a cli command in `/root` (or any other directory not
accessible by the postgres user) this causes:

`could not change directory to "/root": Permission denied`

to be printed multiple times on stderr for those invocations, which is
confusing and has caused quite a few support requests.

modifying the postgres_admin_cmd invocation only should not cause any
future surprises

quickly tested with `pmgconfig sync`

Reported-by: Oguz Bektas <o.bektas at proxmox.com>
Signed-off-by: Stoiko Ivanov <s.ivanov at proxmox.com>
---
v1->v2:
* changed to only cd'ing for the problematic postgres_admin_cmd, after
  a chat with Thomas - huge thanks!

 src/PMG/Utils.pm | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/src/PMG/Utils.pm b/src/PMG/Utils.pm
index 4eebfa5..52701a3 100644
--- a/src/PMG/Utils.pm
+++ b/src/PMG/Utils.pm
@@ -2,6 +2,7 @@ package PMG::Utils;
 
 use strict;
 use warnings;
+use Cwd;
 use DBI;
 use Net::Cmd;
 use Net::SMTP;
@@ -1383,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";
 
@@ -1390,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 {
-- 
2.30.2





More information about the pmg-devel mailing list