Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use address literal as default EHLO argument #30

Merged
merged 1 commit into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/smtp/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ mod test {
#[test]
fn test_display() {
let id = ClientId::Domain("localhost".to_string());
let id_ipv4 = ClientId::Ipv4(std::net::Ipv4Addr::new(127, 0, 0, 1));
let email = EmailAddress::new("[email protected]".to_string()).unwrap();
let mail_parameter = MailParameter::Other {
keyword: "TEST".to_string(),
Expand All @@ -345,6 +346,10 @@ mod test {
value: Some("value".to_string()),
};
assert_eq!(format!("{}", EhloCommand::new(id)), "EHLO localhost\r\n");
assert_eq!(
format!("{}", EhloCommand::new(id_ipv4)),
"EHLO [127.0.0.1]\r\n"
);
assert_eq!(
format!("{}", MailCommand::new(Some(email.clone()), vec![])),
"MAIL FROM:<[email protected]>\r\n"
Expand Down
30 changes: 24 additions & 6 deletions src/smtp/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ use std::fmt::{self, Display, Formatter};
use std::net::{Ipv4Addr, Ipv6Addr};
use std::result::Result;

/// Default client id
const DEFAULT_DOMAIN_CLIENT_ID: &str = "localhost";
/// Default client id.
///
/// It passes
/// `smtpd_helo_restrictions = reject_non_fqdn_helo_hostname`
/// Postfix check, but not `reject_unknown_helo_hostname`.
const DEFAULT_DOMAIN_CLIENT_ID: &str = "localhost.localdomain";

/// Client identifier, the parameter to `EHLO`
#[derive(PartialEq, Eq, Clone, Debug)]
Expand All @@ -28,12 +32,26 @@ pub enum ClientId {
Ipv6(Ipv6Addr),
}

impl Default for ClientId {
fn default() -> Self {
// The most compatible address.
//
// It passes Postfix checks
// ```
// smtpd_helo_restrictions = reject_invalid_helo_hostname, reject_non_fqdn_helo_hostname, reject_unknown_helo_hostname
// smtpd_helo_required = yes
// smtpd_delay_reject = no
// ```
Self::Ipv4(Ipv4Addr::new(127, 0, 0, 1))
}
}

impl Display for ClientId {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self {
ClientId::Domain(ref value) => f.write_str(value),
ClientId::Ipv4(ref value) => write!(f, "{}", value),
ClientId::Ipv6(ref value) => write!(f, "{}", value),
ClientId::Ipv4(ref value) => write!(f, "[{}]", value),
ClientId::Ipv6(ref value) => write!(f, "[IPv6:{}]", value),
}
}
}
Expand All @@ -44,8 +62,8 @@ impl ClientId {
ClientId::Domain(domain)
}

/// Defines a `ClientId` with the current hostname, of `localhost` if hostname could not be
/// found
/// Defines a `ClientId` with the current hostname, or
/// `localhost.localdomain` if hostname could not be found.
pub fn hostname() -> ClientId {
ClientId::Domain(get_hostname().unwrap_or_else(|| DEFAULT_DOMAIN_CLIENT_ID.to_string()))
}
Expand Down
2 changes: 1 addition & 1 deletion src/smtp/smtp_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl SmtpClient {
smtp_utf8: false,
credentials: None,
connection_reuse: ConnectionReuseParameters::NoReuse,
hello_name: ClientId::new("localhost".to_string()),
hello_name: Default::default(),
authentication_mechanism: None,
force_set_auth: false,
timeout: Some(Duration::new(60, 0)),
Expand Down