feat(transport-smtp): Allow building without tls support

This commit is contained in:
Alexis Mousset
2020-05-02 20:58:40 +02:00
parent 4115652695
commit 18a89d4407
6 changed files with 25 additions and 6 deletions

View File

@@ -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

View File

@@ -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 = []

View File

@@ -186,6 +186,7 @@ impl SmtpConnection {
&& self.server_info.supports_feature(Extension::StartTls)
}
#[allow(unused_variables)]
pub fn starttls(
&mut self,
tls_parameters: &TlsParameters,

View File

@@ -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<Duration>) -> 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<Duration>) -> 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(()),
}

View File

@@ -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)
}
}

View File

@@ -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<Self, Error> {
#[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,
},