Skip to content

Commit

Permalink
Add config for additional SMTP TLS root certs
Browse files Browse the repository at this point in the history
  • Loading branch information
JosefSchoenberger authored and BlackDex committed May 19, 2024
1 parent 0fe93ed commit d4e66d3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,10 @@
## Only use this as a last resort if you are not able to use a valid certificate.
# SMTP_ACCEPT_INVALID_HOSTNAMES=false

## Accept additional root certs
## Paths to PEM files, separated by semicolons
# SMTP_ADDITIONAL_ROOT_CERTS=

##########################
### Rocket settings ###
##########################
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,8 @@ make_config! {
smtp_accept_invalid_certs: bool, true, def, false;
/// Accept Invalid Hostnames (Know the risks!) |> DANGEROUS: Allow invalid hostnames. This option introduces significant vulnerabilities to man-in-the-middle attacks!
smtp_accept_invalid_hostnames: bool, true, def, false;
/// Accept additional root certs |> Paths to PEM files, separated by semicolons
smtp_additional_root_certs: String, true, option;
},

/// Email 2FA Settings
Expand Down
23 changes: 22 additions & 1 deletion src/mail.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::str::FromStr;

use chrono::NaiveDateTime;
use once_cell::sync::Lazy;
use percent_encoding::{percent_encode, NON_ALPHANUMERIC};

use lettre::{
message::{Attachment, Body, Mailbox, Message, MultiPart, SinglePart},
transport::smtp::authentication::{Credentials, Mechanism as SmtpAuthMechanism},
transport::smtp::client::{Tls, TlsParameters},
transport::smtp::client::{Certificate, Tls, TlsParameters},
transport::smtp::extension::ClientId,
Address, AsyncSendmailTransport, AsyncSmtpTransport, AsyncTransport, Tokio1Executor,
};
Expand All @@ -29,6 +30,21 @@ fn sendmail_transport() -> AsyncSendmailTransport<Tokio1Executor> {
}
}

static SMTP_ADDITIONAL_ROOT_CERTS: Lazy<Option<Vec<Certificate>>> = Lazy::new(|| {
Some(
CONFIG
.smtp_additional_root_certs()?
.split(';')
.filter(|path| !path.is_empty())
.map(|path| {
let cert = std::fs::read(path)
.unwrap_or_else(|e| panic!("Error loading additional SMTP root certificate file {path}.\n{e}"));
Certificate::from_pem(&cert).unwrap_or_else(|e| panic!("Error decoding certificate file {path}.\n{e}"))
})
.collect(),
)
});

fn smtp_transport() -> AsyncSmtpTransport<Tokio1Executor> {
use std::time::Duration;
let host = CONFIG.smtp_host().unwrap();
Expand All @@ -46,6 +62,11 @@ fn smtp_transport() -> AsyncSmtpTransport<Tokio1Executor> {
if CONFIG.smtp_accept_invalid_certs() {
tls_parameters = tls_parameters.dangerous_accept_invalid_certs(true);
}
if let Some(ref certs) = *SMTP_ADDITIONAL_ROOT_CERTS {
for cert in certs {
tls_parameters = tls_parameters.add_root_certificate(cert.clone());
}
}
let tls_parameters = tls_parameters.build().unwrap();

if CONFIG.smtp_security() == *"force_tls" {
Expand Down

0 comments on commit d4e66d3

Please sign in to comment.