From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 08D5B82EE7 for ; Wed, 1 Dec 2021 19:08:38 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id F37D7111C6 for ; Wed, 1 Dec 2021 19:08:37 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 9D44C111BA for ; Wed, 1 Dec 2021 19:08:36 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 6B1B645F31 for ; Wed, 1 Dec 2021 19:08:36 +0100 (CET) From: Stoiko Ivanov To: pmg-devel@lists.proxmox.com Date: Wed, 1 Dec 2021 19:08:26 +0100 Message-Id: <20211201180826.68298-1-s.ivanov@proxmox.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.285 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [utils.pm] Subject: [pmg-devel] [PATCH pmg-api v2] utils: postgres_admin_cmd chdir to / before running X-BeenThere: pmg-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Mail Gateway development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 01 Dec 2021 18:08:38 -0000 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 Signed-off-by: Stoiko Ivanov --- 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