From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 6AE0C1FF13A for ; Wed, 01 Apr 2026 09:55:28 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 625FA10875; Wed, 1 Apr 2026 09:55:49 +0200 (CEST) From: Christian Ebner To: pbs-devel@lists.proxmox.com Subject: [PATCH proxmox-backup 03/20] pbs-key-config: introduce store_with() for KeyConfig Date: Wed, 1 Apr 2026 09:55:04 +0200 Message-ID: <20260401075521.176354-4-c.ebner@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260401075521.176354-1-c.ebner@proxmox.com> References: <20260401075521.176354-1-c.ebner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1775030086371 X-SPAM-LEVEL: Spam detection results: 0 AWL -1.438 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 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 1 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 1 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 1 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: LTYA36UH4D5ENILG6PEK5VOIMTGZIUR5 X-Message-ID-Hash: LTYA36UH4D5ENILG6PEK5VOIMTGZIUR5 X-MailFrom: c.ebner@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox Backup Server development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Extends the behavior of KeyConfig::store() to allow optionally specifying the mode and ownership of the file the key is stored with. Default to the same behavior as KeyConfig::store() if none of the optional parameters are set, therefore the same implementation is reused for it as well. Signed-off-by: Christian Ebner --- pbs-key-config/src/lib.rs | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/pbs-key-config/src/lib.rs b/pbs-key-config/src/lib.rs index 0bcd5338c..258fb197b 100644 --- a/pbs-key-config/src/lib.rs +++ b/pbs-key-config/src/lib.rs @@ -1,7 +1,10 @@ use std::io::Write; +use std::os::fd::AsRawFd; use std::path::Path; use anyhow::{bail, format_err, Context, Error}; +use nix::sys::stat::Mode; +use nix::unistd::{Gid, Uid}; use serde::{Deserialize, Serialize}; use proxmox_lang::try_block; @@ -236,24 +239,49 @@ impl KeyConfig { /// Store a KeyConfig to path pub fn store>(&self, path: P, replace: bool) -> Result<(), Error> { + self.store_with(path, replace, None, None, None) + } + + /// Store a KeyConfig to path with given ownership and mode. + /// Requires the process to run with permissions to do so. + pub fn store_with>( + &self, + path: P, + replace: bool, + mode: Option, + owner: Option, + group: Option, + ) -> Result<(), Error> { let path: &Path = path.as_ref(); let data = serde_json::to_string(self)?; try_block!({ if replace { - let mode = nix::sys::stat::Mode::S_IRUSR | nix::sys::stat::Mode::S_IWUSR; - replace_file(path, data.as_bytes(), CreateOptions::new().perm(mode), true)?; + let mode = + mode.unwrap_or(nix::sys::stat::Mode::S_IRUSR | nix::sys::stat::Mode::S_IWUSR); + let mut create_options = CreateOptions::new().perm(mode); + if let Some(owner) = owner { + create_options = create_options.owner(owner); + } + if let Some(group) = group { + create_options = create_options.group(group); + } + replace_file(path, data.as_bytes(), create_options, true)?; } else { use std::os::unix::fs::OpenOptionsExt; - + let mode = mode.map(|m| m.bits()).unwrap_or(0o0600); let mut file = std::fs::OpenOptions::new() .write(true) - .mode(0o0600) + .mode(mode) .create_new(true) .open(path)?; file.write_all(data.as_bytes())?; + + let fd = file.as_raw_fd(); + nix::unistd::fchown(fd, owner, group)?; + nix::unistd::fsync(fd)?; } Ok(()) -- 2.47.3