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 DB674DB48 for ; Mon, 17 Jul 2023 17:01:14 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id C2D10F4E0 for ; Mon, 17 Jul 2023 17:01:14 +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 ; Mon, 17 Jul 2023 17:01:13 +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 8078A42B5B for ; Mon, 17 Jul 2023 17:01:13 +0200 (CEST) From: Lukas Wagner To: pve-devel@lists.proxmox.com Date: Mon, 17 Jul 2023 16:59:50 +0200 Message-Id: <20230717150051.710464-6-l.wagner@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230717150051.710464-1-l.wagner@proxmox.com> References: <20230717150051.710464-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.136 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: [pve-devel] [PATCH v3 proxmox 05/66] notify: add sendmail plugin X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 17 Jul 2023 15:01:14 -0000 This plugin uses the 'sendmail' command to send an email to one or more recipients. Signed-off-by: Lukas Wagner --- proxmox-notify/Cargo.toml | 9 ++- proxmox-notify/src/config.rs | 12 ++++ proxmox-notify/src/endpoints/mod.rs | 2 + proxmox-notify/src/endpoints/sendmail.rs | 88 ++++++++++++++++++++++++ proxmox-notify/src/lib.rs | 18 +++++ 5 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 proxmox-notify/src/endpoints/sendmail.rs diff --git a/proxmox-notify/Cargo.toml b/proxmox-notify/Cargo.toml index 37d175f0..f7329295 100644 --- a/proxmox-notify/Cargo.toml +++ b/proxmox-notify/Cargo.toml @@ -8,12 +8,17 @@ repository.workspace = true exclude.workspace = true [dependencies] +handlebars = { workspace = true, optional = true } lazy_static.workspace = true log.workspace = true openssl.workspace = true proxmox-schema = { workspace = true, features = ["api-macro"]} proxmox-section-config = { workspace = true } -proxmox-sys.workspace = true +proxmox-sys = { workspace = true, optional = true } regex.workspace = true -serde.workspace = true +serde = { workspace = true, features = ["derive"]} serde_json.workspace = true + +[features] +default = ["sendmail"] +sendmail = ["dep:handlebars", "dep:proxmox-sys"] \ No newline at end of file diff --git a/proxmox-notify/src/config.rs b/proxmox-notify/src/config.rs index 362ca0fc..6269ec3c 100644 --- a/proxmox-notify/src/config.rs +++ b/proxmox-notify/src/config.rs @@ -13,6 +13,18 @@ lazy_static! { fn config_init() -> SectionConfig { let mut config = SectionConfig::new(&BACKEND_NAME_SCHEMA); + #[cfg(feature = "sendmail")] + { + use crate::endpoints::sendmail::{SendmailConfig, SENDMAIL_TYPENAME}; + + const SENDMAIL_SCHEMA: &ObjectSchema = SendmailConfig::API_SCHEMA.unwrap_object_schema(); + config.register_plugin(SectionConfigPlugin::new( + SENDMAIL_TYPENAME.to_string(), + Some(String::from("name")), + SENDMAIL_SCHEMA, + )); + } + config } diff --git a/proxmox-notify/src/endpoints/mod.rs b/proxmox-notify/src/endpoints/mod.rs index e69de29b..dd80d9bc 100644 --- a/proxmox-notify/src/endpoints/mod.rs +++ b/proxmox-notify/src/endpoints/mod.rs @@ -0,0 +1,2 @@ +#[cfg(feature = "sendmail")] +pub mod sendmail; diff --git a/proxmox-notify/src/endpoints/sendmail.rs b/proxmox-notify/src/endpoints/sendmail.rs new file mode 100644 index 00000000..dd971438 --- /dev/null +++ b/proxmox-notify/src/endpoints/sendmail.rs @@ -0,0 +1,88 @@ +use crate::schema::{COMMENT_SCHEMA, EMAIL_SCHEMA, ENTITY_NAME_SCHEMA}; +use crate::{Endpoint, Error, Notification}; + +use proxmox_schema::{api, Updater}; +use serde::{Deserialize, Serialize}; + +pub(crate) const SENDMAIL_TYPENAME: &str = "sendmail"; + +#[api( + properties: { + name: { + schema: ENTITY_NAME_SCHEMA, + }, + mailto: { + type: Array, + items: { + schema: EMAIL_SCHEMA, + }, + }, + comment: { + optional: true, + schema: COMMENT_SCHEMA, + }, + }, +)] +#[derive(Debug, Serialize, Deserialize, Updater, Default)] +#[serde(rename_all = "kebab-case")] +/// Config for Sendmail notification endpoints +pub struct SendmailConfig { + /// Name of the endpoint + #[updater(skip)] + pub name: String, + /// Mail recipients + pub mailto: Vec, + /// `From` address for the mail + #[serde(skip_serializing_if = "Option::is_none")] + pub from_address: Option, + /// Author of the mail + #[serde(skip_serializing_if = "Option::is_none")] + pub author: Option, + /// Comment + #[serde(skip_serializing_if = "Option::is_none")] + pub comment: Option, +} + +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum DeleteableSendmailProperty { + FromAddress, + Author, + Comment, +} + +/// A sendmail notification endpoint. +pub struct SendmailEndpoint { + pub config: SendmailConfig, +} + +impl Endpoint for SendmailEndpoint { + fn send(&self, notification: &Notification) -> Result<(), Error> { + let recipients: Vec<&str> = self.config.mailto.iter().map(String::as_str).collect(); + + // Note: OX has serious problems displaying text mails, + // so we include html as well + let html = format!( + "
\n{}\n
",
+            handlebars::html_escape(¬ification.body)
+        );
+
+        // proxmox_sys::email::sendmail will set the author to
+        // "Proxmox Backup Server" if it is not set.
+        let author = self.config.author.as_deref().or(Some(""));
+
+        proxmox_sys::email::sendmail(
+            &recipients,
+            ¬ification.title,
+            Some(¬ification.body),
+            Some(&html),
+            self.config.from_address.as_deref(),
+            author,
+        )
+        .map_err(|err| Error::NotifyFailed(self.config.name.clone(), err.into()))
+    }
+
+    fn name(&self) -> &str {
+        &self.config.name
+    }
+}
diff --git a/proxmox-notify/src/lib.rs b/proxmox-notify/src/lib.rs
index 43feac25..041a4d7a 100644
--- a/proxmox-notify/src/lib.rs
+++ b/proxmox-notify/src/lib.rs
@@ -212,6 +212,24 @@ impl Bus {
     pub fn from_config(config: &Config) -> Result {
         let mut endpoints = HashMap::new();
 
+        // Instantiate endpoints
+
+        #[cfg(feature = "sendmail")]
+        {
+            use endpoints::sendmail::SENDMAIL_TYPENAME;
+            use endpoints::sendmail::{SendmailConfig, SendmailEndpoint};
+            endpoints.extend(
+                parse_endpoints_without_private_config!(
+                    config,
+                    SendmailConfig,
+                    SendmailEndpoint,
+                    SENDMAIL_TYPENAME
+                )?
+                .into_iter()
+                .map(|e| (e.name().into(), e)),
+            );
+        }
+
         Ok(Bus { endpoints })
     }
 
-- 
2.39.2