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 EDEBA9B150 for ; Wed, 24 May 2023 15:57:33 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id CF0BC1F5E2 for ; Wed, 24 May 2023 15:57:33 +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 ; Wed, 24 May 2023 15:57:29 +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 82B2D46E29 for ; Wed, 24 May 2023 15:57:29 +0200 (CEST) From: Lukas Wagner To: pve-devel@lists.proxmox.com Date: Wed, 24 May 2023 15:56:17 +0200 Message-Id: <20230524135649.934881-11-l.wagner@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230524135649.934881-1-l.wagner@proxmox.com> References: <20230524135649.934881-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.171 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 v2 proxmox 10/42] 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: Wed, 24 May 2023 13:57:34 -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 | 17 +++++ 5 files changed, 126 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 3064065b..2ef237fa 100644 --- a/proxmox-notify/src/config.rs +++ b/proxmox-notify/src/config.rs @@ -14,6 +14,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, + )); + } + const CHANNEL_SCHEMA: &ObjectSchema = ChannelConfig::API_SCHEMA.unwrap_object_schema(); config.register_plugin(SectionConfigPlugin::new( 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..f9b3df83 --- /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, + }, + recipient: { + type: Array, + items: { + schema: EMAIL_SCHEMA, + }, + }, + comment: { + optional: true, + schema: COMMENT_SCHEMA, + }, + }, +)] +#[derive(Debug, Serialize, Deserialize, Updater)] +#[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 recipient: 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.recipient.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 3b881e26..ee89d100 100644
--- a/proxmox-notify/src/lib.rs
+++ b/proxmox-notify/src/lib.rs
@@ -212,6 +212,23 @@ impl Bus {
     pub fn from_config(config: &Config) -> Result {
         let mut endpoints = Vec::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(),
+            );
+        }
+
         let channels = config
             .config
             .convert_to_typed_array(CHANNEL_TYPENAME)
-- 
2.30.2