From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <f.ebner@proxmox.com>
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 44F87CADD
 for <pmg-devel@lists.proxmox.com>; Mon, 10 Jul 2023 13:37:22 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 277BB3E597
 for <pmg-devel@lists.proxmox.com>; Mon, 10 Jul 2023 13:36:52 +0200 (CEST)
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))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pmg-devel@lists.proxmox.com>; Mon, 10 Jul 2023 13:36:51 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 400B842875
 for <pmg-devel@lists.proxmox.com>; Mon, 10 Jul 2023 13:36:51 +0200 (CEST)
From: Fiona Ebner <f.ebner@proxmox.com>
To: pmg-devel@lists.proxmox.com
Date: Mon, 10 Jul 2023 13:36:47 +0200
Message-Id: <20230710113647.53879-2-f.ebner@proxmox.com>
X-Mailer: git-send-email 2.39.2
In-Reply-To: <20230710113647.53879-1-f.ebner@proxmox.com>
References: <20230710113647.53879-1-f.ebner@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.045 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 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
 T_SCC_BODY_TEXT_LINE    -0.01 -
Subject: [pmg-devel] [PATCH pmg-api 2/2] tree-wide: make slurp mode as local
 as possible for future-proofing
X-BeenThere: pmg-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Mail Gateway development discussion
 <pmg-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pmg-devel>, 
 <mailto:pmg-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pmg-devel/>
List-Post: <mailto:pmg-devel@lists.proxmox.com>
List-Help: <mailto:pmg-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pmg-devel>, 
 <mailto:pmg-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Mon, 10 Jul 2023 11:37:22 -0000

similar to what PMG/TFAConfig.pm already does.

Otherwise, sub-routine calls would still be affected leading to
unexpected results, like the issue fixed by commit "cluster config:
restrict slurp scope to avoid issue parsing network interfaces".

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
 src/PMG/API2/ACMEPlugin.pm |  3 +--
 src/PMG/Config.pm          |  4 +---
 src/PMG/LDAPConfig.pm      |  4 +---
 src/PMG/NodeConfig.pm      |  3 +--
 src/PMG/PBSConfig.pm       |  4 +---
 src/PMG/Ticket.pm          | 12 +++---------
 6 files changed, 8 insertions(+), 22 deletions(-)

diff --git a/src/PMG/API2/ACMEPlugin.pm b/src/PMG/API2/ACMEPlugin.pm
index e2004bf..25d3a04 100644
--- a/src/PMG/API2/ACMEPlugin.pm
+++ b/src/PMG/API2/ACMEPlugin.pm
@@ -30,8 +30,7 @@ PVE::JSONSchema::register_standard_option('pmg-acme-pluginid', {
 
 sub read_pmg_acme_challenge_config {
     my ($filename, $fh) = @_;
-    local $/ = undef; # slurp mode
-    my $raw = defined($fh) ? <$fh> : '';
+    my $raw = defined($fh) ? do { local $/ = undef; <$fh> } : '';
     return PVE::ACME::Challenge->parse_config($filename, $raw);
 }
 
diff --git a/src/PMG/Config.pm b/src/PMG/Config.pm
index fe89e11..7339e0d 100644
--- a/src/PMG/Config.pm
+++ b/src/PMG/Config.pm
@@ -939,10 +939,8 @@ sub get_config {
 sub read_pmg_conf {
     my ($filename, $fh) = @_;
 
-    local $/ = undef; # slurp mode
-
     my $raw;
-    $raw = <$fh> if defined($fh);
+    $raw = do { local $/ = undef; <$fh> } if defined($fh);
 
     return  PMG::Config::Base->parse_config($filename, $raw);
 }
diff --git a/src/PMG/LDAPConfig.pm b/src/PMG/LDAPConfig.pm
index a6cd6ef..e5b3388 100644
--- a/src/PMG/LDAPConfig.pm
+++ b/src/PMG/LDAPConfig.pm
@@ -221,9 +221,7 @@ __PACKAGE__->init();
 sub read_pmg_ldap_conf {
     my ($filename, $fh) = @_;
 
-    local $/ = undef; # slurp mode
-
-    my $raw = defined($fh) ? <$fh> : '';
+    my $raw = defined($fh) ? do { local $/ = undef; <$fh> } : '';
 
     return __PACKAGE__->parse_config($filename, $raw);
 }
diff --git a/src/PMG/NodeConfig.pm b/src/PMG/NodeConfig.pm
index 42139e4..6303979 100644
--- a/src/PMG/NodeConfig.pm
+++ b/src/PMG/NodeConfig.pm
@@ -120,8 +120,7 @@ sub print_domain : prototype($) {
 
 sub read_pmg_node_config {
     my ($filename, $fh) = @_;
-    local $/ = undef; # slurp mode
-    my $raw = defined($fh) ? <$fh> : '';
+    my $raw = defined($fh) ? do { local $/ = undef; <$fh> } : '';
     my $digest = Digest::SHA::sha1_hex($raw);
     my $conf = PVE::JSONSchema::parse_config($config_schema, $filename, $raw);
     $conf->{digest} = $digest;
diff --git a/src/PMG/PBSConfig.pm b/src/PMG/PBSConfig.pm
index 3417123..ee506f1 100644
--- a/src/PMG/PBSConfig.pm
+++ b/src/PMG/PBSConfig.pm
@@ -194,9 +194,7 @@ __PACKAGE__->init();
 sub read_pmg_pbs_conf {
     my ($filename, $fh) = @_;
 
-    local $/ = undef; # slurp mode
-
-    my $raw = defined($fh) ? <$fh> : '';
+    my $raw = defined($fh) ? do { local $/ = undef; <$fh> } : '';
 
     return __PACKAGE__->parse_config($filename, $raw);
 }
diff --git a/src/PMG/Ticket.pm b/src/PMG/Ticket.pm
index 0c2ec0b..fc2ac77 100644
--- a/src/PMG/Ticket.pm
+++ b/src/PMG/Ticket.pm
@@ -106,9 +106,7 @@ sub generate_auth_key {
 my $read_rsa_priv_key = sub {
    my ($filename, $fh) = @_;
 
-   local $/ = undef; # slurp mode
-
-   my $input = <$fh>;
+   my $input = do { local $/ = undef; <$fh> };
 
    return Crypt::OpenSSL::RSA->new_private_key($input);
 
@@ -121,9 +119,7 @@ PVE::INotify::register_file('auth_priv_key', $authprivkeyfn,
 my $read_rsa_pub_key = sub {
    my ($filename, $fh) = @_;
 
-   local $/ = undef; # slurp mode
-
-   my $input = <$fh>;
+   my $input = do { local $/ = undef; <$fh> };
 
    return Crypt::OpenSSL::RSA->new_public_key($input);
 };
@@ -135,9 +131,7 @@ PVE::INotify::register_file('auth_pub_key', $authpubkeyfn,
 my $read_csrf_secret = sub {
    my ($filename, $fh) = @_;
 
-   local $/ = undef; # slurp mode
-
-   my $input = <$fh>;
+   my $input = do { local $/ = undef; <$fh> };
 
    return Digest::SHA::hmac_sha256_base64($input);
 };
-- 
2.39.2