STARTTLS for non-465 ports, 587 default, real mail errors on the page
Browse files at this commit993ef227305aa17e7962bcb05305835e5d707b4eParent: 2d7813a- fix
- STARTTLS for non-465 ports, 587 default, real mail errors on the page
4 changed files +29 -7
- crates/im-core/src/settings.rs +2 -1
- crates/im-web/src/admin.rs +14 -3
- crates/im-web/src/mailer.rs +12 -3
- crates/im-web/src/pages.rs +1 -0
crates/im-core/src/settings.rsmodified+2 -1
| @@ -59,7 +59,7 @@ pub async fn smtp(store: &Store) -> Result<Smtp> { | |||
| 59 | let port = get(store, "smtp_port") | 59 | let port = get(store, "smtp_port") |
| 60 | .await? | 60 | .await? |
| 61 | .and_then(|raw| raw.parse().ok()) | 61 | .and_then(|raw| raw.parse().ok()) |
| 62 | - .unwrap_or(465); | 62 | + .unwrap_or(587); |
| 63 | let username = get(store, "smtp_username").await?.unwrap_or_default(); | 63 | let username = get(store, "smtp_username").await?.unwrap_or_default(); |
| 64 | let from = get(store, "smtp_from").await?.unwrap_or_default(); | 64 | let from = get(store, "smtp_from").await?.unwrap_or_default(); |
| 65 | let password = get(store, "smtp_password") | 65 | let password = get(store, "smtp_password") |
| @@ -120,6 +120,7 @@ mod tests { | |||
| 120 | async fn smtp_roundtrip_with_sealed_password() { | 120 | async fn smtp_roundtrip_with_sealed_password() { |
| 121 | let store = Store::open(Path::new(":memory:")).await.unwrap(); | 121 | let store = Store::open(Path::new(":memory:")).await.unwrap(); |
| 122 | assert!(!smtp(&store).await.unwrap().configured()); | 122 | assert!(!smtp(&store).await.unwrap().configured()); |
| 123 | + assert_eq!(smtp(&store).await.unwrap().port, 587); | ||
| 123 | 124 | ||
| 124 | let value = Smtp { | 125 | let value = Smtp { |
| 125 | host: "smtp.example.com".into(), | 126 | host: "smtp.example.com".into(), |
crates/im-web/src/admin.rsmodified+14 -3
| @@ -70,6 +70,7 @@ async fn admin_page(cx: &Cx) -> Result<Response> { | |||
| 70 | let section = query_value(&query, "section").unwrap_or_else(|| "users".to_string()); | 70 | let section = query_value(&query, "section").unwrap_or_else(|| "users".to_string()); |
| 71 | let error = query_value(&query, "error"); | 71 | let error = query_value(&query, "error"); |
| 72 | let ok = query_value(&query, "ok"); | 72 | let ok = query_value(&query, "ok"); |
| 73 | + let why = query_value(&query, "why"); | ||
| 73 | let invited = query_value(&query, "invited"); | 74 | let invited = query_value(&query, "invited"); |
| 74 | 75 | ||
| 75 | let nav = |current: &str| { | 76 | let nav = |current: &str| { |
| @@ -104,8 +105,11 @@ async fn admin_page(cx: &Cx) -> Result<Response> { | |||
| 104 | } | 105 | } |
| 105 | )), | 106 | )), |
| 106 | (_, Some(code)) => Some(format!( | 107 | (_, Some(code)) => Some(format!( |
| 107 | - r#"<div class="auth-problem">{}</div>"#, | 108 | + r#"<div class="auth-problem">{}{}</div>"#, |
| 108 | - error_text(code) | 109 | + error_text(code), |
| 110 | + why.as_deref() | ||
| 111 | + .map(|detail| format!("<div class=\"muted\">{}</div>", escape(detail))) | ||
| 112 | + .unwrap_or_default(), | ||
| 109 | )), | 113 | )), |
| 110 | _ => None, | 114 | _ => None, |
| 111 | }; | 115 | }; |
| @@ -432,7 +436,14 @@ async fn smtp_test(cx: &Cx) -> Result<Response> { | |||
| 432 | Some(&e.to_string()), | 436 | Some(&e.to_string()), |
| 433 | ) | 437 | ) |
| 434 | .await; | 438 | .await; |
| 435 | - back(cx, "mail", "&error=smtp_test") | 439 | + back( |
| 440 | + cx, | ||
| 441 | + "mail", | ||
| 442 | + &format!( | ||
| 443 | + "&error=smtp_test&why={}", | ||
| 444 | + crate::oidc::urlencode(&e.to_string()) | ||
| 445 | + ), | ||
| 446 | + ) | ||
| 436 | } | 447 | } |
| 437 | } | 448 | } |
| 438 | } | 449 | } |
crates/im-web/src/mailer.rsmodified+12 -3
| @@ -21,9 +21,18 @@ fn transport(smtp: &Smtp) -> Result<lettre::AsyncSmtpTransport<lettre::Tokio1Exe | |||
| 21 | if !smtp.configured() { | 21 | if !smtp.configured() { |
| 22 | return Err(MailError::NotConfigured); | 22 | return Err(MailError::NotConfigured); |
| 23 | } | 23 | } |
| 24 | - let mut builder = lettre::AsyncSmtpTransport::<lettre::Tokio1Executor>::relay(&smtp.host) | 24 | + // 465 is implicit TLS — the handshake wraps the connection from the first |
| 25 | - .map_err(|e| MailError::Backend(e.to_string()))? | 25 | + // byte. Everything else (587 above all) is submission: the session starts |
| 26 | - .port(smtp.port); | 26 | + // in the clear and STARTTLS upgrades it. Picking the wrong one is the |
| 27 | + // classic "something went wrong": relay() against a 587 port hangs the TLS | ||
| 28 | + // handshake on a server waiting for plaintext EHLO. | ||
| 29 | + let mut builder = if smtp.port == 465 { | ||
| 30 | + lettre::AsyncSmtpTransport::<lettre::Tokio1Executor>::relay(&smtp.host) | ||
| 31 | + } else { | ||
| 32 | + lettre::AsyncSmtpTransport::<lettre::Tokio1Executor>::starttls_relay(&smtp.host) | ||
| 33 | + } | ||
| 34 | + .map_err(|e| MailError::Backend(e.to_string()))? | ||
| 35 | + .port(smtp.port); | ||
| 27 | if !smtp.username.is_empty() { | 36 | if !smtp.username.is_empty() { |
| 28 | builder = builder.credentials(lettre::transport::smtp::authentication::Credentials::new( | 37 | builder = builder.credentials(lettre::transport::smtp::authentication::Credentials::new( |
| 29 | smtp.username.clone(), | 38 | smtp.username.clone(), |
crates/im-web/src/pages.rsmodified+1 -0
| @@ -30,6 +30,7 @@ pub fn error_text(code: &str) -> &'static str { | |||
| 30 | "password_personal" => "The password can't contain your address or your name.", | 30 | "password_personal" => "The password can't contain your address or your name.", |
| 31 | "passwords_differ" => "The two passwords don't match.", | 31 | "passwords_differ" => "The two passwords don't match.", |
| 32 | "enroll_first" => "Set up your second factor first.", | 32 | "enroll_first" => "Set up your second factor first.", |
| 33 | + "smtp_test" => "The test mail could not be sent.", | ||
| 33 | _ => "Something went wrong. Try again.", | 34 | _ => "Something went wrong. Try again.", |
| 34 | } | 35 | } |
| 35 | } | 36 | } |