- SmtpServer: STARTTLS (RFC 3207) — SslHandler ставится сразу после того, как ответ 220 ушёл открытым текстом; сессия начинается заново; строки, присланные открытым текстом вместе со STARTTLS, не выполняются, соединение закрывается (CVE-2011-0411); свежий декодер строк без остатков открытого текста - неявный TLS на SMTPS_PORT (RFC 8314); AUTH — только внутри TLS (или SMTP_AUTH_WITHOUT_TLS); Received: ESMTPS/ESMTPSA - SmtpTls: свой сертификат (MAIL_TLS_CERT/KEY) или самоподписанный, создаётся один раз в MAIL_TLS_DIR (в образе /app/data/tls); TLS 1.2/1.3 - тесты: STARTTLS + AUTH, подмешанный открытый текст, JavaMail со starttls.required, порт 465, переиспользование сертификата (38 всего) - проверено вживую: openssl s_client -starttls smtp и порт 465, TLSv1.3 α Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
87 lines
5.1 KiB
Java
87 lines
5.1 KiB
Java
package com.mailserver.config;
|
|
|
|
import com.mailserver.delivery.SmtpDeliveryClient;
|
|
import com.mailserver.smtp.MailRouter;
|
|
import com.mailserver.smtp.SmtpServer;
|
|
import com.mailserver.smtp.SmtpTls;
|
|
import io.netty.handler.ssl.SslContext;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
|
import java.nio.file.Path;
|
|
import java.time.Duration;
|
|
import java.util.Locale;
|
|
|
|
/** SMTP server, outbound delivery client and the queue scheduler. */
|
|
@Configuration
|
|
@EnableScheduling
|
|
public class MailServerConfig {
|
|
|
|
/**
|
|
* Certificate for SMTP TLS: mailserver.tls.cert-file + key-file (PEM), or a self-signed one in mailserver.tls.dir.
|
|
* mailserver.tls.enabled=false — no TLS at all.
|
|
*/
|
|
@Bean
|
|
public SmtpTlsContext smtpTlsContext(@Value("${mailserver.tls.enabled:true}") boolean enabled,
|
|
@Value("${mailserver.tls.cert-file:}") String certFile,
|
|
@Value("${mailserver.tls.key-file:}") String keyFile,
|
|
@Value("${mailserver.tls.dir:./tls}") String dir,
|
|
@Value("${mailserver.smtp.hostname:localhost}") String hostname) throws Exception {
|
|
return new SmtpTlsContext(enabled ? SmtpTls.serverContext(certFile, keyFile, Path.of(dir), hostname) : null);
|
|
}
|
|
|
|
public record SmtpTlsContext(SslContext context) {
|
|
}
|
|
|
|
@Bean(initMethod = "start", destroyMethod = "stop")
|
|
@ConditionalOnProperty(name = "mailserver.smtp.enabled", havingValue = "true", matchIfMissing = true)
|
|
public SmtpServer smtpServer(MailRouter router, SmtpTlsContext tls,
|
|
@Value("${mailserver.smtp.host:0.0.0.0}") String host,
|
|
@Value("${mailserver.smtp.port:3025}") int port,
|
|
@Value("${mailserver.smtp.hostname:localhost}") String hostname,
|
|
@Value("${mailserver.smtp.max-message-bytes:26214400}") long maxBytes,
|
|
@Value("${mailserver.smtp.auth-without-tls:false}") boolean authWithoutTls,
|
|
@Value("${mailserver.smtp.idle-timeout:5m}") Duration idleTimeout) {
|
|
return new SmtpServer(new SmtpServer.Settings(host, port, hostname, maxBytes, authWithoutTls, idleTimeout,
|
|
tls.context(), false), router);
|
|
}
|
|
|
|
/** Implicit TLS (RFC 8314, usually port 465) — only when mailserver.smtps.port is set and TLS is on. */
|
|
@Bean(initMethod = "start", destroyMethod = "stop")
|
|
@ConditionalOnExpression("${mailserver.smtps.port:0} > 0 and ${mailserver.tls.enabled:true}")
|
|
public SmtpServer smtpsServer(MailRouter router, SmtpTlsContext tls,
|
|
@Value("${mailserver.smtp.host:0.0.0.0}") String host,
|
|
@Value("${mailserver.smtps.port}") int port,
|
|
@Value("${mailserver.smtp.hostname:localhost}") String hostname,
|
|
@Value("${mailserver.smtp.max-message-bytes:26214400}") long maxBytes,
|
|
@Value("${mailserver.smtp.idle-timeout:5m}") Duration idleTimeout) {
|
|
return new SmtpServer(new SmtpServer.Settings(host, port, hostname, maxBytes, false, idleTimeout,
|
|
tls.context(), true), router);
|
|
}
|
|
|
|
/**
|
|
* mailserver.outbound.mode: off | mx | smarthost. For smarthost — host, port, username, password,
|
|
* tls (starttls | ssl | none).
|
|
*/
|
|
@Bean
|
|
public SmtpDeliveryClient smtpDeliveryClient(@Value("${mailserver.outbound.mode:off}") String mode,
|
|
@Value("${mailserver.outbound.host:}") String host,
|
|
@Value("${mailserver.outbound.port:587}") int port,
|
|
@Value("${mailserver.outbound.username:}") String username,
|
|
@Value("${mailserver.outbound.password:}") String password,
|
|
@Value("${mailserver.outbound.tls:starttls}") String tls,
|
|
@Value("${mailserver.smtp.hostname:localhost}") String hostname,
|
|
@Value("${mailserver.outbound.timeout-ms:30000}") int timeoutMs) {
|
|
SmtpDeliveryClient.Mode m = SmtpDeliveryClient.Mode.valueOf(mode.trim().toUpperCase(Locale.ROOT));
|
|
if (m == SmtpDeliveryClient.Mode.SMARTHOST && host.isBlank()) {
|
|
throw new IllegalStateException("mailserver.outbound.mode=smarthost needs mailserver.outbound.host");
|
|
}
|
|
return new SmtpDeliveryClient(new SmtpDeliveryClient.Settings(m, host, port, username, password,
|
|
SmtpDeliveryClient.Tls.valueOf(tls.trim().toUpperCase(Locale.ROOT)), hostname, timeoutMs, 25));
|
|
}
|
|
}
|