From 18a89d4407e184f10d0269c0357c61f3e32d6d6a Mon Sep 17 00:00:00 2001 From: Alexis Mousset Date: Sat, 2 May 2020 20:58:40 +0200 Subject: [PATCH] feat(transport-smtp): Allow building without tls support --- .github/workflows/test.yml | 5 +++++ Cargo.toml | 2 +- src/transport/smtp/client/mod.rs | 1 + src/transport/smtp/client/net.rs | 12 +++++++++++- src/transport/smtp/commands.rs | 2 -- src/transport/smtp/mod.rs | 9 +++++++-- 6 files changed, 25 insertions(+), 6 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2dce1ec..31fa4b5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -29,6 +29,11 @@ jobs: - uses: actions-rs/cargo@v1 with: command: test + - run: rm target/debug/deps/liblettre-* + - uses: actions-rs/cargo@v1 + with: + command: test + args: --no-default-features --features=builder,smtp-transport,file-transport,sendmail-transport check: name: Check diff --git a/Cargo.toml b/Cargo.toml index a98df95..04b1f7c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -51,7 +51,7 @@ name = "transport_smtp" [features] builder = ["mime", "base64", "hyperx", "textnonce", "quoted_printable"] -default = ["file-transport", "smtp-transport", "hostname", "sendmail-transport", "rustls-tls", "builder", "r2d2"] +default = ["file-transport", "smtp-transport", "hostname", "sendmail-transport", "builder", "r2d2"] file-transport = ["serde", "serde_json"] rustls-tls = ["webpki", "webpki-roots", "rustls"] sendmail-transport = [] diff --git a/src/transport/smtp/client/mod.rs b/src/transport/smtp/client/mod.rs index 7cde3f7..5af1c9c 100644 --- a/src/transport/smtp/client/mod.rs +++ b/src/transport/smtp/client/mod.rs @@ -186,6 +186,7 @@ impl SmtpConnection { && self.server_info.supports_feature(Extension::StartTls) } + #[allow(unused_variables)] pub fn starttls( &mut self, tls_parameters: &TlsParameters, diff --git a/src/transport/smtp/client/net.rs b/src/transport/smtp/client/net.rs index f82e9ab..e0c20d6 100644 --- a/src/transport/smtp/client/net.rs +++ b/src/transport/smtp/client/net.rs @@ -114,10 +114,13 @@ impl NetworkStream { tcp_stream, )))) } + #[cfg(not(any(feature = "native-tls", feature = "rustls")))] + Some(_) => panic!("TLS configuration without support"), None => Ok(NetworkStream::Tcp(tcp_stream)), } } + #[allow(unused_variables, unreachable_code)] pub fn upgrade_tls(&mut self, tls_parameters: &TlsParameters) -> Result<(), Error> { *self = match *self { #[cfg(feature = "native-tls")] @@ -137,7 +140,11 @@ impl NetworkStream { stream.try_clone().unwrap(), ))) } - NetworkStream::Tls(_) | NetworkStream::Mock(_) => return Ok(()), + #[cfg(not(any(feature = "native-tls", feature = "rustls")))] + NetworkStream::Tcp(_) => panic!("STARTTLS without TLS support"), + #[cfg(any(feature = "native-tls", feature = "rustls"))] + NetworkStream::Tls(_) => return Ok(()), + NetworkStream::Mock(_) => return Ok(()), }; Ok(()) @@ -146,6 +153,7 @@ impl NetworkStream { pub fn is_encrypted(&self) -> bool { match *self { NetworkStream::Tcp(_) | NetworkStream::Mock(_) => false, + #[cfg(any(feature = "native-tls", feature = "rustls"))] NetworkStream::Tls(_) => true, } } @@ -153,6 +161,7 @@ impl NetworkStream { pub fn set_read_timeout(&mut self, duration: Option) -> io::Result<()> { match *self { NetworkStream::Tcp(ref mut stream) => stream.set_read_timeout(duration), + #[cfg(any(feature = "native-tls", feature = "rustls"))] NetworkStream::Tls(ref mut stream) => stream.get_ref().set_read_timeout(duration), NetworkStream::Mock(_) => Ok(()), } @@ -162,6 +171,7 @@ impl NetworkStream { pub fn set_write_timeout(&mut self, duration: Option) -> io::Result<()> { match *self { NetworkStream::Tcp(ref mut stream) => stream.set_write_timeout(duration), + #[cfg(any(feature = "native-tls", feature = "rustls"))] NetworkStream::Tls(ref mut stream) => stream.get_ref().set_write_timeout(duration), NetworkStream::Mock(_) => Ok(()), } diff --git a/src/transport/smtp/commands.rs b/src/transport/smtp/commands.rs index a4eb498..6cee1eb 100644 --- a/src/transport/smtp/commands.rs +++ b/src/transport/smtp/commands.rs @@ -169,7 +169,6 @@ pub struct Vrfy { impl Display for Vrfy { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - #[allow(clippy::write_with_newline)] write!(f, "VRFY {}\r\n", self.argument) } } @@ -190,7 +189,6 @@ pub struct Expn { impl Display for Expn { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - #[allow(clippy::write_with_newline)] write!(f, "EXPN {}\r\n", self.argument) } } diff --git a/src/transport/smtp/mod.rs b/src/transport/smtp/mod.rs index 70d7c98..c6766f7 100644 --- a/src/transport/smtp/mod.rs +++ b/src/transport/smtp/mod.rs @@ -12,10 +12,12 @@ //! * STARTTLS ([RFC 2487](http://tools.ietf.org/html/rfc2487)) //! +#[cfg(any(feature = "native-tls", feature = "rustls"))] +use crate::transport::smtp::net::TlsParameters; use crate::{ transport::smtp::{ authentication::{Credentials, Mechanism, DEFAULT_MECHANISMS}, - client::{net::TlsParameters, SmtpConnection}, + client::SmtpConnection, error::{Error, SmtpResult}, extension::ClientId, }, @@ -61,7 +63,7 @@ const DEFAULT_TLS_MIN_PROTOCOL: Protocol = Protocol::Tlsv12; /// How to apply TLS to a client connection #[derive(Clone)] -#[allow(missing_debug_implementations)] +#[allow(missing_debug_implementations, missing_copy_implementations)] pub enum Tls { /// Insecure connection only (for testing purposes) None, @@ -128,6 +130,7 @@ impl SmtpTransport { /// Simple and secure transport, should be used when possible. /// Creates an encrypted transport over submissions port, using the provided domain /// to validate TLS certificates. + #[cfg(any(feature = "native-tls", feature = "rustls"))] pub fn relay(relay: &str) -> Result { #[cfg(feature = "native-tls")] let mut tls_builder = TlsConnector::builder(); @@ -193,6 +196,7 @@ impl SmtpTransport { } /// Set the TLS settings to use + #[cfg(any(feature = "native-tls", feature = "rustls"))] pub fn tls(mut self, tls: Tls) -> Self { self.tls = tls; self @@ -214,6 +218,7 @@ impl SmtpTransport { self.timeout, &self.hello_name, match self.tls { + #[cfg(any(feature = "native-tls", feature = "rustls"))] Tls::Wrapper(ref tls_parameters) => Some(tls_parameters), _ => None, },