From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 6C4231FF0C0 for ; Wed, 18 Nov 2026 10:52:11 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 6FF7B21E41; Wed, 18 Nov 2026 10:45:13 +0100 (CET) From: Lukas Wagner To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox-mail-forward v2 61/63] add crate for code shared between the mail-forwarder and the worker Date: Wed, 18 Nov 2026 10:43:45 +0100 Message-ID: <20261118094348.273646-62-l.wagner@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261118094348.273646-1-l.wagner@proxmox.com> References: <20261118094348.273646-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794995042811 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.186 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust 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: 56PVIC2X44Y6BDNGFNHTTX6Z3KXOCPJF X-Message-ID-Hash: 56PVIC2X44Y6BDNGFNHTTX6Z3KXOCPJF X-MailFrom: l.wagner@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 VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Signed-off-by: Lukas Wagner --- Cargo.toml | 3 ++ Makefile | 2 +- common/Cargo.toml | 13 ++++++ common/src/lib.rs | 78 ++++++++++++++++++++++++++++++++ proxmox-mail-forward/Cargo.toml | 2 + proxmox-mail-forward/src/main.rs | 74 +----------------------------- 6 files changed, 99 insertions(+), 73 deletions(-) create mode 100644 common/Cargo.toml create mode 100644 common/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index e6e70c6..27e4d8e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = [ + "common", "proxmox-mail-forward", ] resolver = "3" @@ -22,3 +23,5 @@ nix = "0.29" proxmox-log = "1" proxmox-notify = "2" proxmox-sys = "1" + +common = { path = "common" } diff --git a/Makefile b/Makefile index 72ae89a..0c7dc72 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ CARGO ?= cargo $(BUILDDIR): rm -rf $@ $@.tmp && mkdir $@.tmp - cp -a debian Cargo.toml proxmox-mail-forward Makefile .cargo $@.tmp + cp -a debian Cargo.toml common proxmox-mail-forward Makefile .cargo $@.tmp rm -f $@.tmp/Cargo.lock find $@.tmp/debian -name "*.hint" -delete mv $@.tmp $@ diff --git a/common/Cargo.toml b/common/Cargo.toml new file mode 100644 index 0000000..e66ec5e --- /dev/null +++ b/common/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "common" +description = "Shared definitions for the Proxmox mail forwarder and notification worker" +version.workspace = true +authors.workspace = true +edition.workspace = true +license.workspace = true +homepage.workspace = true +publish = false + +[dependencies] +anyhow.workspace = true +nix = { workspace = true, features = [ "user" ] } diff --git a/common/src/lib.rs b/common/src/lib.rs new file mode 100644 index 0000000..fe03064 --- /dev/null +++ b/common/src/lib.rs @@ -0,0 +1,78 @@ +//! Definitions shared between the Proxmox mail forwarder and the notification worker. + +#![deny(unsafe_code)] +#![deny(missing_docs)] + +use std::path::Path; + +use anyhow::{Context, Error}; +use nix::unistd::{Gid, Group, Uid, User}; + +/// A Proxmox product whose notifications are processed via a notification queue. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Product { + /// Proxmox VE + Pve, + /// Proxmox Backup Server + Pbs, +} + +impl Product { + /// All supported products, in order of precedence. + pub const ALL: &[Product] = &[Product::Pve, Product::Pbs]; + + /// Directory of the product's notification queue. + pub fn queue_dir(self) -> &'static str { + match self { + Self::Pve => "/var/lib/pve-manager/notifications/queue", + Self::Pbs => "/var/lib/proxmox-backup/notifications/queue", + } + } + + /// Check whether the product is installed on this host. + /// + /// A product counts as installed if its notification queue directory exists. + pub fn is_installed(self) -> bool { + Path::new(self.queue_dir()).exists() + } + + /// Name of the user that owns the notification queue. + pub fn user(self) -> &'static str { + match self { + Self::Pve => "www-data", + Self::Pbs => "backup", + } + } + + /// Name of the group that owns the notification queue. + pub fn group(self) -> &'static str { + match self { + Self::Pve => "www-data", + Self::Pbs => "backup", + } + } + + /// Look up the ID of the user that owns the notification queue. + pub fn uid(self) -> Result { + let user = self.user(); + + let uid = User::from_name(user) + .with_context(|| format!("could not look up username {user}"))? + .with_context(|| format!("user {user} does not exist"))? + .uid; + + Ok(uid) + } + + /// Look up the ID of the group that owns the notification queue. + pub fn gid(self) -> Result { + let group = self.group(); + + let gid = Group::from_name(group) + .with_context(|| format!("could not look up group {group}"))? + .with_context(|| format!("group {group} does not exist"))? + .gid; + + Ok(gid) + } +} diff --git a/proxmox-mail-forward/Cargo.toml b/proxmox-mail-forward/Cargo.toml index c3f7f7d..ef3e4a7 100644 --- a/proxmox-mail-forward/Cargo.toml +++ b/proxmox-mail-forward/Cargo.toml @@ -11,6 +11,8 @@ homepage.workspace = true anyhow.workspace = true nix.workspace = true +common.workspace = true + proxmox-log.workspace = true proxmox-notify = { workspace = true, features = ["queue-submitter"] } proxmox-sys.workspace = true diff --git a/proxmox-mail-forward/src/main.rs b/proxmox-mail-forward/src/main.rs index 60c8336..0673728 100644 --- a/proxmox-mail-forward/src/main.rs +++ b/proxmox-mail-forward/src/main.rs @@ -23,7 +23,6 @@ //! reading the mail from stdin. use std::io::Read; -use std::path::Path; use std::process::ExitCode; use anyhow::Context; @@ -31,9 +30,9 @@ use anyhow::Error; use anyhow::bail; use nix::sys::stat::Mode; use nix::unistd::Gid; -use nix::unistd::Group; use nix::unistd::Uid; -use nix::unistd::User; + +use common::Product; use proxmox_log::LevelFilter; use proxmox_log::Logger; @@ -74,75 +73,6 @@ impl std::fmt::Display for ForwardError { } } -/// A Proxmox product whose notifications are processed via a notification queue. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -enum Product { - /// Proxmox VE - Pve, - /// Proxmox Backup Server - Pbs, -} - -impl Product { - /// All supported products, in order of precedence. - const ALL: &[Product] = &[Product::Pve, Product::Pbs]; - - /// Directory of the product's notification queue. - fn queue_dir(self) -> &'static str { - match self { - Self::Pve => "/var/lib/pve-manager/notifications/queue", - Self::Pbs => "/var/lib/proxmox-backup/notifications/queue", - } - } - - /// Check whether the product is installed on this host. - /// - /// A product counts as installed if its notification queue directory exists. - fn is_installed(self) -> bool { - Path::new(self.queue_dir()).exists() - } - - /// Name of the user that owns the notification queue. - fn user(self) -> &'static str { - match self { - Self::Pve => "www-data", - Self::Pbs => "backup", - } - } - - /// Name of the group that owns the notification queue. - fn group(self) -> &'static str { - match self { - Self::Pve => "www-data", - Self::Pbs => "backup", - } - } - - /// Look up the ID of the user that owns the notification queue. - fn uid(self) -> Result { - let user = self.user(); - - let uid = User::from_name(user) - .with_context(|| format!("could not look up username {user}"))? - .with_context(|| format!("user {user} does not exist"))? - .uid; - - Ok(uid) - } - - /// Look up the ID of the group that owns the notification queue. - fn gid(self) -> Result { - let group = self.group(); - - let gid = Group::from_name(group) - .with_context(|| format!("could not look up group {group}"))? - .with_context(|| format!("group {group} does not exist"))? - .gid; - - Ok(gid) - } -} - /// Maximum number of bytes read from stdin, as a safeguard against excessively large mails. const MAX_MAIL_SIZE: u64 = 1024 * 1024; -- 2.47.3