The Unified and Autonomous Development Platform
feat: support implicit SSL for SMTP and IMAP mail setting (#1508)
Robin Shen0606e550b7d4
diff --git a/server-core/src/main/java/io/onedev/server/data/migration/DataMigrator.java b/server-core/src/main/java/io/onedev/server/data/migration/DataMigrator.javaindex 1eb10d13a4..acb67c1879 100644--- a/server-core/src/main/java/io/onedev/server/data/migration/DataMigrator.java+++ b/server-core/src/main/java/io/onedev/server/data/migration/DataMigrator.java@@ -5635,5 +5635,52 @@ public class DataMigrator {}}-++ private void migrate134(File dataDir, Stack<Integer> versions) {+ for (File file : dataDir.listFiles()) {+ if (file.getName().startsWith("Settings.xml")) {+ VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);+ for (Element element : dom.getRootElement().elements()) {+ if (element.elementTextTrim("key").equals("MAIL")) {+ Element valueElement = element.element("value");+ if (valueElement != null) {+ if (valueElement.attributeValue("class").contains("OtherMailSetting")) {+ var enableStartTLSElement = valueElement.element("enableStartTLS");+ var sslSettingElement = valueElement.addElement("sslSetting");+ if (enableStartTLSElement.getTextTrim().equals("true")) {+ sslSettingElement.addAttribute("class", "io.onedev.server.model.support.administration.mailsetting.SmtpExplicitSsl");+ sslSettingElement.addElement("trustAll").setText("false");+ } else {+ sslSettingElement.addAttribute("class", "io.onedev.server.model.support.administration.mailsetting.SmtpWithoutSsl");+ }+ enableStartTLSElement.detach();+ var portElement = valueElement.element("smtpPort");+ portElement.detach();+ portElement.setName("port");+ sslSettingElement.add(portElement);++ var pollSettingElement = valueElement.element("otherInboxPollSetting");+ if (pollSettingElement != null) {+ var enableSSLElement = pollSettingElement.element("enableSSL");+ sslSettingElement = pollSettingElement.addElement("sslSetting");+ if (enableSSLElement.getTextTrim().equals("true")) {+ sslSettingElement.addAttribute("class", "io.onedev.server.model.support.administration.mailsetting.ImapImplicitSsl");+ sslSettingElement.addElement("trustAll").setText("false");+ } else {+ sslSettingElement.addAttribute("class", "io.onedev.server.model.support.administration.mailsetting.ImapWithoutSsl");+ }+ enableSSLElement.detach();+ portElement = pollSettingElement.element("imapPort");+ portElement.detach();+ portElement.setName("port");+ sslSettingElement.add(portElement);+ }+ }+ }+ }+ }+ dom.writeToFile(file, false);+ }+ }+ }}diff --git a/server-core/src/main/java/io/onedev/server/mail/DefaultMailManager.java b/server-core/src/main/java/io/onedev/server/mail/DefaultMailManager.javaindex a5f27a999e..63186334bf 100644--- a/server-core/src/main/java/io/onedev/server/mail/DefaultMailManager.java+++ b/server-core/src/main/java/io/onedev/server/mail/DefaultMailManager.java@@ -236,13 +236,11 @@ public class DefaultMailManager implements MailManager, Serializable {if (sendSetting != null) {Properties properties = new Properties();properties.setProperty("mail.smtp.host", sendSetting.getSmtpHost());- properties.setProperty("mail.smtp.port", String.valueOf(sendSetting.getSmtpPort()));- properties.setProperty("mail.transport.protocol", "smtp");++ sendSetting.getSslSetting().configure(properties);properties.setProperty("mail.smtp.connectiontimeout", String.valueOf(Bootstrap.SOCKET_CONNECT_TIMEOUT));properties.setProperty("mail.smtp.timeout", String.valueOf(sendSetting.getTimeout()*1000));- properties.setProperty("mail.smtp.starttls.enable", String.valueOf(sendSetting.isEnableStartTLS()));- properties.setProperty("mail.smtp.starttls.required", "false");Authenticator authenticator;if (sendSetting.getSmtpUser() != null) {@@ -884,8 +882,8 @@ public class DefaultMailManager implements MailManager, Serializable {return executorService.submit(new Runnable() {private void processMessages(IMAPFolder inbox, AtomicInteger messageNumber) throws MessagingException {- int currentMessageNumber = inbox.getMessageCount();- for (int i=messageNumber.get()+1; i<=currentMessageNumber; i++) {+ int messageCount = inbox.getMessageCount();+ for (int i=messageNumber.get()+1; i<=messageCount; i++) {Message message = inbox.getMessage(i);lastPosition.setUid(inbox.getUID(message));logger.trace("Processing inbox messge (subject: {}, uid: {}, seq: {})",@@ -916,7 +914,7 @@ public class DefaultMailManager implements MailManager, Serializable {logger.error("Error processing message", e);}}- messageNumber.set(currentMessageNumber);+ messageNumber.set(messageCount);}private long getUid(IMAPFolder inbox, int messageNumber) throws MessagingException {@@ -931,12 +929,7 @@ public class DefaultMailManager implements MailManager, Serializable {Properties properties = new Properties();properties.setProperty("mail.imap.host", checkSetting.getImapHost());- properties.setProperty("mail.imap.port", String.valueOf(checkSetting.getImapPort()));- properties.setProperty("mail.imap.ssl.enable", String.valueOf(checkSetting.isEnableSSL()));-- properties.setProperty("mail.imap.socketFactory.fallback", "false");- properties.setProperty("mail.imap.socketFactory.port", String.valueOf(checkSetting.getImapPort()));-+ checkSetting.getSslSetting().configure(properties);properties.setProperty("mail.imap.connectiontimeout", String.valueOf(Bootstrap.SOCKET_CONNECT_TIMEOUT));properties.setProperty("mail.imap.timeout", String.valueOf(checkSetting.getTimeout()*1000));if (checkSetting.getImapCredential() instanceof OAuthAccessToken)@@ -973,7 +966,11 @@ public class DefaultMailManager implements MailManager, Serializable {}} else {lastPosition.setUidValidity(uidValidity);- messageNumber.set(inbox.getMessageCount());+ // Check back for 5 emails as otherwise we may not be able to receive the just sent+ // email when test mail check setting+ messageNumber.set(inbox.getMessageCount() - 5);+ if (messageNumber.get() < 0)+ messageNumber.set(0);lastPosition.setUid(getUid(inbox, messageNumber.get()));logger.trace("Inbox uid validity changed (uid reset to: {})", lastPosition.getUid());}diff --git a/server-core/src/main/java/io/onedev/server/mail/MailCheckSetting.java b/server-core/src/main/java/io/onedev/server/mail/MailCheckSetting.javaindex 8b7a829664..bbc28b7ee9 100644--- a/server-core/src/main/java/io/onedev/server/mail/MailCheckSetting.java+++ b/server-core/src/main/java/io/onedev/server/mail/MailCheckSetting.java@@ -1,69 +1,65 @@-package io.onedev.server.mail;--import java.io.Serializable;--public class MailCheckSetting implements Serializable {-- private static final long serialVersionUID = 1L;-- private final String imapHost;-- private final int imapPort;-- private final String imapUser;-- private final MailCredential imapCredential;-- private final String checkAddress;-- private final boolean enableSSL;-- private final int pollInterval;-- private final int timeout;-- public MailCheckSetting(String imapHost, int imapPort, String imapUser, MailCredential imapCredential,- String checkAddress, boolean enableSSL, int pollInterval, int timeout) {- this.imapHost = imapHost;- this.imapPort = imapPort;- this.imapUser = imapUser;- this.imapCredential = imapCredential;- this.checkAddress = checkAddress;- this.enableSSL = enableSSL;- this.pollInterval = pollInterval;- this.timeout = timeout;- }-- public String getImapHost() {- return imapHost;- }-- public int getImapPort() {- return imapPort;- }-- public String getImapUser() {- return imapUser;- }-- public MailCredential getImapCredential() {- return imapCredential;- }-- public String getCheckAddress() {- return checkAddress;- }-- public boolean isEnableSSL() {- return enableSSL;- }-- public int getPollInterval() {- return pollInterval;- }-- public int getTimeout() {- return timeout;- }--}+package io.onedev.server.mail;++import io.onedev.server.model.support.administration.mailsetting.ImapSslSetting;++import java.io.Serializable;++public class MailCheckSetting implements Serializable {++ private static final long serialVersionUID = 1L;++ private final String imapHost;++ private final ImapSslSetting sslSetting;++ private final String imapUser;++ private final MailCredential imapCredential;++ private final String checkAddress;++ private final int pollInterval;++ private final int timeout;++ public MailCheckSetting(String imapHost, ImapSslSetting sslSetting, String imapUser,+ MailCredential imapCredential, String checkAddress,+ int pollInterval, int timeout) {+ this.imapHost = imapHost;+ this.sslSetting = sslSetting;+ this.imapUser = imapUser;+ this.imapCredential = imapCredential;+ this.checkAddress = checkAddress;+ this.pollInterval = pollInterval;+ this.timeout = timeout;+ }++ public String getImapHost() {+ return imapHost;+ }++ public ImapSslSetting getSslSetting() {+ return sslSetting;+ }++ public String getImapUser() {+ return imapUser;+ }++ public MailCredential getImapCredential() {+ return imapCredential;+ }++ public String getCheckAddress() {+ return checkAddress;+ }++ public int getPollInterval() {+ return pollInterval;+ }++ public int getTimeout() {+ return timeout;+ }++}diff --git a/server-core/src/main/java/io/onedev/server/mail/MailManager.java b/server-core/src/main/java/io/onedev/server/mail/MailManager.javaindex baea02d833..bca0998ddb 100644--- a/server-core/src/main/java/io/onedev/server/mail/MailManager.java+++ b/server-core/src/main/java/io/onedev/server/mail/MailManager.java@@ -41,6 +41,7 @@ public interface MailManager {String toPlainText(String mailContent);- Future<?> monitorInbox(MailCheckSetting checkSetting, MessageListener listener, MailPosition MailPosition);+ Future<?> monitorInbox(MailCheckSetting checkSetting, MessageListener listener,+ MailPosition lastPosition);}diff --git a/server-core/src/main/java/io/onedev/server/mail/MailSendSetting.java b/server-core/src/main/java/io/onedev/server/mail/MailSendSetting.javaindex bc98d745c7..00e6da6f4f 100644--- a/server-core/src/main/java/io/onedev/server/mail/MailSendSetting.java+++ b/server-core/src/main/java/io/onedev/server/mail/MailSendSetting.java@@ -1,62 +1,57 @@-package io.onedev.server.mail;--import java.io.Serializable;--public class MailSendSetting implements Serializable {-- private static final long serialVersionUID = 1L;-- private final String smtpHost;-- private final int smtpPort;-- private final String smtpUser;-- private final MailCredential smtpCredential;-- private final String senderAddress;-- private final boolean enableStartTLS;-- private final int timeout;-- public MailSendSetting(String smtpHost, int smtpPort, String smtpUser, MailCredential smtpCredential,- String senderAddress, boolean enableStartTLS, int timeout) {- this.smtpHost = smtpHost;- this.smtpPort = smtpPort;- this.smtpUser = smtpUser;- this.smtpCredential = smtpCredential;- this.senderAddress = senderAddress;- this.enableStartTLS = enableStartTLS;- this.timeout = timeout;- }-- public String getSmtpHost() {- return smtpHost;- }-- public int getSmtpPort() {- return smtpPort;- }-- public String getSmtpUser() {- return smtpUser;- }-- public MailCredential getSmtpCredential() {- return smtpCredential;- }-- public String getSenderAddress() {- return senderAddress;- }-- public boolean isEnableStartTLS() {- return enableStartTLS;- }-- public int getTimeout() {- return timeout;- }--}+package io.onedev.server.mail;++import io.onedev.server.model.support.administration.mailsetting.SmtpSslSetting;++import java.io.Serializable;++public class MailSendSetting implements Serializable {++ private static final long serialVersionUID = 1L;++ private final String smtpHost;++ private final SmtpSslSetting sslSetting;++ private final String smtpUser;++ private final MailCredential smtpCredential;++ private final String senderAddress;++ private final int timeout;++ public MailSendSetting(String smtpHost, SmtpSslSetting sslSetting, String smtpUser,+ MailCredential smtpCredential, String senderAddress, int timeout) {+ this.smtpHost = smtpHost;+ this.sslSetting = sslSetting;+ this.smtpUser = smtpUser;+ this.smtpCredential = smtpCredential;+ this.senderAddress = senderAddress;+ this.timeout = timeout;+ }++ public String getSmtpHost() {+ return smtpHost;+ }++ public SmtpSslSetting getSslSetting() {+ return sslSetting;+ }++ public String getSmtpUser() {+ return smtpUser;+ }++ public MailCredential getSmtpCredential() {+ return smtpCredential;+ }++ public String getSenderAddress() {+ return senderAddress;+ }++ public int getTimeout() {+ return timeout;+ }++}diff --git a/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/GmailSetting.java b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/GmailSetting.javaindex a73384ff92..7025c80053 100644--- a/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/GmailSetting.java+++ b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/GmailSetting.java@@ -154,8 +154,8 @@ public class GmailSetting extends MailSetting {public MailSendSetting getSendSetting() {MailCredential smtpCredential = new OAuthAccessToken(TOKEN_ENDPOINT, clientId, clientSecret, refreshToken);- return new MailSendSetting("smtp.gmail.com", 587, accountName, smtpCredential,- accountName, true, getTimeout());+ return new MailSendSetting("smtp.gmail.com", new SmtpExplicitSsl(), accountName, smtpCredential,+ accountName, getTimeout());}@Override@@ -164,8 +164,8 @@ public class GmailSetting extends MailSetting {MailCredential imapCredential = new OAuthAccessToken(TOKEN_ENDPOINT, clientId, clientSecret, refreshToken);- return new MailCheckSetting("imap.gmail.com", 993,- accountName, imapCredential, accountName, true,+ return new MailCheckSetting("imap.gmail.com",+ new ImapImplicitSsl(), accountName, imapCredential, accountName,inboxPollSetting.getPollInterval(), getTimeout());} else {return null;diff --git a/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/ImapExplicitSsl.java b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/ImapExplicitSsl.javanew file mode 100644index 0000000000..11f538c537--- /dev/null+++ b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/ImapExplicitSsl.java@@ -0,0 +1,30 @@+package io.onedev.server.model.support.administration.mailsetting;++import io.onedev.server.annotation.Editable;++import java.util.Properties;++@Editable(order=200, name="Explicit SSL (StartTLS)")+public class ImapExplicitSsl extends ImapWithSsl {++ private static final long serialVersionUID = 1L;++ private int port = 143;++ @Editable+ public int getPort() {+ return port;+ }++ public void setPort(int port) {+ this.port = port;+ }++ @Override+ public void configure(Properties properties) {+ super.configure(properties);+ properties.setProperty("mail.imap.port", String.valueOf(port));+ properties.setProperty("mail.imap.starttls.enable", "true");+ properties.setProperty("mail.imap.starttls.required", "false");+ }+}diff --git a/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/ImapImplicitSsl.java b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/ImapImplicitSsl.javanew file mode 100644index 0000000000..bd6712e810--- /dev/null+++ b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/ImapImplicitSsl.java@@ -0,0 +1,30 @@+package io.onedev.server.model.support.administration.mailsetting;++import io.onedev.server.annotation.Editable;++import java.util.Properties;++@Editable(order=100, name="Implicit SSL")+public class ImapImplicitSsl extends ImapWithSsl {++ private static final long serialVersionUID = 1L;++ private int port = 993;++ @Editable+ public int getPort() {+ return port;+ }++ public void setPort(int port) {+ this.port = port;+ }++ @Override+ public void configure(Properties properties) {+ super.configure(properties);+ properties.setProperty("mail.imap.port", String.valueOf(port));+ properties.setProperty("mail.imap.ssl.enable", "true");+ }++}diff --git a/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/ImapSslSetting.java b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/ImapSslSetting.javanew file mode 100644index 0000000000..0c84aa1be4--- /dev/null+++ b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/ImapSslSetting.java@@ -0,0 +1,13 @@+package io.onedev.server.model.support.administration.mailsetting;++import io.onedev.server.annotation.Editable;++import java.io.Serializable;+import java.util.Properties;++@Editable+public interface ImapSslSetting extends Serializable {++ void configure(Properties properties);++}diff --git a/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/ImapWithSsl.java b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/ImapWithSsl.javanew file mode 100644index 0000000000..f20f842fe0--- /dev/null+++ b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/ImapWithSsl.java@@ -0,0 +1,44 @@+package io.onedev.server.model.support.administration.mailsetting;++import com.sun.mail.util.MailSSLSocketFactory;+import io.onedev.server.annotation.Editable;+import io.onedev.server.security.TrustCertsSSLSocketFactory;++import java.security.GeneralSecurityException;+import java.util.Properties;++@Editable+public abstract class ImapWithSsl implements ImapSslSetting {++ private static final long serialVersionUID = 1L;++ private boolean trustAll;++ @Editable(order=1000, name="Bypass Certificate Check", description = "In case IMAP host certificate is self-signed or its CA root is not accepted, " ++ "you may tell OneDev to bypass certificate check. <b class='text-danger'>WARNING: </b> In " ++ "an untrusted network, this may lead to man-in-the-middle attack, and you should <a href='https://docs.onedev.io/administration-guide/trust-self-signed-certificates#trust-self-signed-certificates-on-server' target='_blank'>import the " ++ "certificate into OneDev</a> instead")+ public boolean isTrustAll() {+ return trustAll;+ }++ public void setTrustAll(boolean trustAll) {+ this.trustAll = trustAll;+ }++ @Override+ public void configure(Properties properties) {+ properties.setProperty("mail.imap.ssl.socketFactory.class",+ TrustCertsSSLSocketFactory.class.getName());+ properties.setProperty("mail.imap.ssl.socketFactory.fallback", "false");+ if (isTrustAll()) {+ try {+ var sf = new MailSSLSocketFactory();+ sf.setTrustAllHosts(true);+ properties.put("mail.imap.ssl.socketFactory", sf);+ } catch (GeneralSecurityException e) {+ throw new RuntimeException(e);+ }+ }+ }+}diff --git a/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/ImapWithoutSsl.java b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/ImapWithoutSsl.javanew file mode 100644index 0000000000..e3b2c26cc9--- /dev/null+++ b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/ImapWithoutSsl.java@@ -0,0 +1,28 @@+package io.onedev.server.model.support.administration.mailsetting;++import io.onedev.server.annotation.Editable;++import java.util.Properties;++@Editable(order=300, name="No SSL")+public class ImapWithoutSsl implements ImapSslSetting {++ private static final long serialVersionUID = 1L;++ private int port = 143;++ @Editable+ public int getPort() {+ return port;+ }++ public void setPort(int port) {+ this.port = port;+ }++ @Override+ public void configure(Properties properties) {+ properties.setProperty("mail.imap.port", String.valueOf(port));+ }++}diff --git a/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/Office365Setting.java b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/Office365Setting.javaindex 73ab8814ad..c6ba4e45fb 100644--- a/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/Office365Setting.java+++ b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/Office365Setting.java@@ -192,8 +192,8 @@ public class Office365Setting extends MailSetting {public MailSendSetting getSendSetting() {MailCredential smtpCredential = new OAuthAccessToken(getTokenEndpoint(tenantId), clientId, clientSecret, refreshToken);- return new MailSendSetting("smtp.office365.com", 587, userPrincipalName, smtpCredential,- emailAddress, true, getTimeout());+ return new MailSendSetting("smtp.office365.com", new SmtpExplicitSsl(), userPrincipalName,+ smtpCredential, emailAddress, getTimeout());}@Override@@ -203,8 +203,8 @@ public class Office365Setting extends MailSetting {MailCredential imapCredential = new OAuthAccessToken(getTokenEndpoint(tenantId), clientId, clientSecret, refreshToken);- return new MailCheckSetting("outlook.office365.com", 993,- imapUser, imapCredential, emailAddress, true,+ return new MailCheckSetting("outlook.office365.com",+ new ImapImplicitSsl(), imapUser, imapCredential, emailAddress,inboxPollSetting.getPollInterval(), getTimeout());} else {return null;diff --git a/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/OtherInboxPollSetting.java b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/OtherInboxPollSetting.javaindex f36d85e50c..ecdd49a42c 100644--- a/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/OtherInboxPollSetting.java+++ b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/OtherInboxPollSetting.java@@ -1,10 +1,11 @@package io.onedev.server.model.support.administration.mailsetting;-import javax.validation.constraints.NotEmpty;-import io.onedev.server.annotation.Editable;import io.onedev.server.annotation.Password;+import javax.validation.constraints.NotEmpty;+import javax.validation.constraints.NotNull;+@Editablepublic class OtherInboxPollSetting extends InboxPollSetting {@@ -12,14 +13,12 @@ public class OtherInboxPollSetting extends InboxPollSetting {private String imapHost;- private int imapPort = 993;+ private ImapSslSetting sslSetting = new ImapImplicitSsl();private String imapUser;private String imapPassword;- private boolean enableSSL = true;-@Editable(order=100, name="IMAP Host")@NotEmptypublic String getImapHost() {@@ -30,13 +29,14 @@ public class OtherInboxPollSetting extends InboxPollSetting {this.imapHost = imapHost;}- @Editable(order=200, name="IMAP Port")- public int getImapPort() {- return imapPort;+ @Editable(order=200)+ @NotNull+ public ImapSslSetting getSslSetting() {+ return sslSetting;}- public void setImapPort(int imapPort) {- this.imapPort = imapPort;+ public void setSslSetting(ImapSslSetting sslSetting) {+ this.sslSetting = sslSetting;}@Editable(order=300, name="IMAP User", description="Specify IMAP user name.<br>"@@ -62,13 +62,4 @@ public class OtherInboxPollSetting extends InboxPollSetting {this.imapPassword = imapPassword;}- @Editable(order=700, name="Enable IMAP SSL", description="Whether or not to enable SSL when connect to IMAP server")- public boolean isEnableSSL() {- return enableSSL;- }-- public void setEnableSSL(boolean enableSSL) {- this.enableSSL = enableSSL;- }-}\ No newline at end of filediff --git a/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/OtherMailSetting.java b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/OtherMailSetting.javaindex 1208c63535..1593491175 100644--- a/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/OtherMailSetting.java+++ b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/OtherMailSetting.java@@ -2,6 +2,7 @@ package io.onedev.server.model.support.administration.mailsetting;import javax.validation.constraints.Email;import javax.validation.constraints.NotEmpty;+import javax.validation.constraints.NotNull;import io.onedev.server.mail.BasicAuthPassword;import io.onedev.server.mail.MailCheckSetting;@@ -17,7 +18,7 @@ public class OtherMailSetting extends MailSetting {private String smtpHost;- private int smtpPort = 587;+ private SmtpSslSetting sslSetting = new SmtpExplicitSsl();private String smtpUser;@@ -26,8 +27,6 @@ public class OtherMailSetting extends MailSetting {private String emailAddress;private OtherInboxPollSetting otherInboxPollSetting;-- private boolean enableStartTLS = true;@Editable(order=100, name="SMTP Host")@NotEmpty@@ -39,13 +38,14 @@ public class OtherMailSetting extends MailSetting {this.smtpHost = smtpHost;}- @Editable(order=200, name="SMTP Port")- public int getSmtpPort() {- return smtpPort;+ @Editable(order=200, name="SSL Setting")+ @NotNull+ public SmtpSslSetting getSslSetting() {+ return sslSetting;}- public void setSmtpPort(int smtpPort) {- this.smtpPort = smtpPort;+ public void setSslSetting(SmtpSslSetting sslSetting) {+ this.sslSetting = sslSetting;}@Editable(order=300, name="SMTP User")@@ -79,17 +79,7 @@ public class OtherMailSetting extends MailSetting {public void setEmailAddress(String emailAddress) {this.emailAddress = emailAddress;}-- @Editable(order=430, name="Enable SMTP StartTLS", description="Whether or not to enable StartTLS "- + "when connect to SMTP server")- public boolean isEnableStartTLS() {- return enableStartTLS;- }-- public void setEnableStartTLS(boolean enableStartTLS) {- this.enableStartTLS = enableStartTLS;- }-+@Editable(order=450, name="Check Incoming Email", description="Enable this to post issue and pull request comments via email. "+ "<b class='text-danger'>NOTE:</b> <a href='https://en.wikipedia.org/wiki/Email_address#Subaddressing' target='_blank'>Sub addressing</a> "+ "needs to be enabled for above email address, as OneDev uses it to track issue and pull request contexts")@@ -108,7 +98,7 @@ public class OtherMailSetting extends MailSetting {smtpCredential = new BasicAuthPassword(smtpPassword);elsesmtpCredential = null;- return new MailSendSetting(smtpHost, smtpPort, smtpUser, smtpCredential, emailAddress, enableStartTLS, getTimeout());+ return new MailSendSetting(smtpHost, sslSetting, smtpUser, smtpCredential, emailAddress, getTimeout());}@Override@@ -116,9 +106,8 @@ public class OtherMailSetting extends MailSetting {if (otherInboxPollSetting != null) {String imapUser = otherInboxPollSetting.getImapUser();MailCredential imapCredential = new BasicAuthPassword(otherInboxPollSetting.getImapPassword());- return new MailCheckSetting(otherInboxPollSetting.getImapHost(), otherInboxPollSetting.getImapPort(),- imapUser, imapCredential, emailAddress, otherInboxPollSetting.isEnableSSL(),- otherInboxPollSetting.getPollInterval(), getTimeout());+ return new MailCheckSetting(otherInboxPollSetting.getImapHost(), otherInboxPollSetting.getSslSetting(),+ imapUser, imapCredential, emailAddress, otherInboxPollSetting.getPollInterval(), getTimeout());} else {return null;}diff --git a/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/SmtpExplicitSsl.java b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/SmtpExplicitSsl.javanew file mode 100644index 0000000000..86cd0fe91d--- /dev/null+++ b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/SmtpExplicitSsl.java@@ -0,0 +1,30 @@+package io.onedev.server.model.support.administration.mailsetting;++import io.onedev.server.annotation.Editable;++import java.util.Properties;++@Editable(order=100, name="Explicit SSL (StartTLS)")+public class SmtpExplicitSsl extends SmtpWithSsl {++ private static final long serialVersionUID = 1L;++ private int port = 587;++ @Editable+ public int getPort() {+ return port;+ }++ public void setPort(int port) {+ this.port = port;+ }++ @Override+ public void configure(Properties properties) {+ super.configure(properties);+ properties.setProperty("mail.smtp.port", String.valueOf(port));+ properties.setProperty("mail.smtp.starttls.enable", "true");+ properties.setProperty("mail.smtp.starttls.required", "false");+ }+}diff --git a/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/SmtpImplicitSsl.java b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/SmtpImplicitSsl.javanew file mode 100644index 0000000000..1e51c1511e--- /dev/null+++ b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/SmtpImplicitSsl.java@@ -0,0 +1,29 @@+package io.onedev.server.model.support.administration.mailsetting;++import io.onedev.server.annotation.Editable;++import java.util.Properties;++@Editable(order=200, name="Implicit SSL")+public class SmtpImplicitSsl extends SmtpWithSsl {++ private static final long serialVersionUID = 1L;++ private int port = 465;++ @Editable+ public int getPort() {+ return port;+ }++ public void setPort(int port) {+ this.port = port;+ }++ @Override+ public void configure(Properties properties) {+ super.configure(properties);+ properties.setProperty("mail.smtp.port", String.valueOf(port));+ properties.setProperty("mail.smtp.ssl.enable", "true");+ }+}diff --git a/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/SmtpSslSetting.java b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/SmtpSslSetting.javanew file mode 100644index 0000000000..a3511aa49e--- /dev/null+++ b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/SmtpSslSetting.java@@ -0,0 +1,13 @@+package io.onedev.server.model.support.administration.mailsetting;++import io.onedev.server.annotation.Editable;++import java.io.Serializable;+import java.util.Properties;++@Editable+public interface SmtpSslSetting extends Serializable {++ void configure(Properties properties);++}diff --git a/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/SmtpWithSsl.java b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/SmtpWithSsl.javanew file mode 100644index 0000000000..36cba926a8--- /dev/null+++ b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/SmtpWithSsl.java@@ -0,0 +1,44 @@+package io.onedev.server.model.support.administration.mailsetting;++import com.sun.mail.util.MailSSLSocketFactory;+import io.onedev.server.annotation.Editable;+import io.onedev.server.security.TrustCertsSSLSocketFactory;++import java.security.GeneralSecurityException;+import java.util.Properties;++@Editable+public abstract class SmtpWithSsl implements SmtpSslSetting {++ private static final long serialVersionUID = 1L;++ private boolean trustAll;++ @Editable(order=1000, name="Bypass Certificate Check", description = "In case SMTP host certificate is self-signed or its CA root is not accepted, " ++ "you may tell OneDev to bypass certificate check. <b class='text-danger'>WARNING: </b> In " ++ "an untrusted network, this may lead to man-in-the-middle attack, and you should <a href='https://docs.onedev.io/administration-guide/trust-self-signed-certificates#trust-self-signed-certificates-on-server' target='_blank'>import the " ++ "certificate into OneDev</a> instead")+ public boolean isTrustAll() {+ return trustAll;+ }++ public void setTrustAll(boolean trustAll) {+ this.trustAll = trustAll;+ }++ @Override+ public void configure(Properties properties) {+ properties.setProperty("mail.smtp.ssl.socketFactory.class",+ TrustCertsSSLSocketFactory.class.getName());+ properties.setProperty("mail.smtp.ssl.socketFactory.fallback", "false");+ if (isTrustAll()) {+ try {+ var sf = new MailSSLSocketFactory();+ sf.setTrustAllHosts(true);+ properties.put("mail.smtp.ssl.socketFactory", sf);+ } catch (GeneralSecurityException e) {+ throw new RuntimeException(e);+ }+ }+ }+}diff --git a/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/SmtpWithoutSsl.java b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/SmtpWithoutSsl.javanew file mode 100644index 0000000000..d2f5f8e4a1--- /dev/null+++ b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/SmtpWithoutSsl.java@@ -0,0 +1,28 @@+package io.onedev.server.model.support.administration.mailsetting;++import io.onedev.server.annotation.Editable;++import java.util.Properties;++@Editable(order=300, name="No SSL")+public class SmtpWithoutSsl implements SmtpSslSetting {++ private static final long serialVersionUID = 1L;++ private int port = 25;++ @Editable+ public int getPort() {+ return port;+ }++ public void setPort(int port) {+ this.port = port;+ }++ @Override+ public void configure(Properties properties) {+ properties.setProperty("mail.smtp.port", String.valueOf(port));+ }++}diff --git a/server-plugin/server-plugin-authenticator-ldap/src/main/java/io/onedev/server/plugin/authenticator/ldap/LdapSSLSocketFactory.java b/server-core/src/main/java/io/onedev/server/security/TrustCertsSSLSocketFactory.javasimilarity index 65%rename from server-plugin/server-plugin-authenticator-ldap/src/main/java/io/onedev/server/plugin/authenticator/ldap/LdapSSLSocketFactory.javarename to server-core/src/main/java/io/onedev/server/security/TrustCertsSSLSocketFactory.javaindex d54f9aac71..39a76248c8 100644--- a/server-plugin/server-plugin-authenticator-ldap/src/main/java/io/onedev/server/plugin/authenticator/ldap/LdapSSLSocketFactory.java+++ b/server-core/src/main/java/io/onedev/server/security/TrustCertsSSLSocketFactory.java@@ -1,11 +1,11 @@-package io.onedev.server.plugin.authenticator.ldap;+package io.onedev.server.security;import io.onedev.server.OneDev;import nl.altindag.ssl.SSLFactory;import javax.net.ssl.SSLSocketFactory;-public abstract class LdapSSLSocketFactory extends SSLSocketFactory {+public abstract class TrustCertsSSLSocketFactory extends SSLSocketFactory {public static SSLSocketFactory getDefault() {return OneDev.getInstance(SSLFactory.class).getSslSocketFactory();diff --git a/server-core/src/main/java/io/onedev/server/web/page/admin/mailsetting/MailSettingPage.java b/server-core/src/main/java/io/onedev/server/web/page/admin/mailsetting/MailSettingPage.javaindex 3dc80bcff0..749d24e3d3 100644--- a/server-core/src/main/java/io/onedev/server/web/page/admin/mailsetting/MailSettingPage.java+++ b/server-core/src/main/java/io/onedev/server/web/page/admin/mailsetting/MailSettingPage.java@@ -84,68 +84,58 @@ public class MailSettingPage extends AdministrationPage {@Overrideprotected String runTask(TaskLogger logger) {- return OneDev.getInstance(SessionManager.class).call(new Callable<String>() {-- @Override- public String call() {- MailManager mailManager = OneDev.getInstance(MailManager.class);- MailSetting mailSetting = mailSettingHolder.getMailSetting();- MailCheckSetting checkSetting = mailSetting.getCheckSetting();- if (checkSetting != null) {- String uuid = UUID.randomUUID().toString();- AtomicReference<Future<?>> futureRef = new AtomicReference<>(null);- MessageListener listener = new MessageListener() {-- @Override- public void onReceived(Message message) throws MessagingException {- if (message.getSubject().contains(uuid))- futureRef.get().cancel(true);- }-- };-- checkSetting = new MailCheckSetting(checkSetting.getImapHost(), checkSetting.getImapPort(),- checkSetting.getImapUser(), checkSetting.getImapCredential(), checkSetting.getCheckAddress(),- checkSetting.isEnableSSL(), 5, checkSetting.getTimeout());- futureRef.set(mailManager.monitorInbox(checkSetting, listener, new MailPosition()));-- ParsedEmailAddress checkAddress = ParsedEmailAddress.parse(checkSetting.getCheckAddress());- String subAddressed = checkAddress.getSubAddressed(MailManager.TEST_SUB_ADDRESS);- logger.log("Sending test mail to " + subAddressed + "...");- mailManager.sendMail(mailSetting.getSendSetting(),- Sets.newHashSet(subAddressed), Lists.newArrayList(), Lists.newArrayList(), uuid,- "[Test] Test Email From OneDev", "This is a test email from OneDev",- null, null, null);-- logger.log("Waiting for test mail to come back...");-- try {- futureRef.get().get();- } catch (CancellationException e) {- } catch (InterruptedException e) {+ return OneDev.getInstance(SessionManager.class).call(() -> {+ MailManager mailManager = OneDev.getInstance(MailManager.class);+ MailSetting mailSetting = mailSettingHolder.getMailSetting();+ MailCheckSetting checkSetting = mailSetting.getCheckSetting();+ if (checkSetting != null) {+ String uuid = UUID.randomUUID().toString();+ AtomicReference<Future<?>> futureRef = new AtomicReference<>(null);+ MessageListener listener = message -> {+ if (message.getSubject().contains(uuid))futureRef.get().cancel(true);- throw new RuntimeException(e);- } catch (ExecutionException e) {- throw new RuntimeException(e);- }-- logger.log("Received test mail");-- return "Great, your mail settings are working";+ };++ checkSetting = new MailCheckSetting(checkSetting.getImapHost(), checkSetting.getSslSetting(),+ checkSetting.getImapUser(), checkSetting.getImapCredential(), checkSetting.getCheckAddress(),+ 5, checkSetting.getTimeout());+ futureRef.set(mailManager.monitorInbox(checkSetting, listener, new MailPosition()));++ ParsedEmailAddress checkAddress = ParsedEmailAddress.parse(checkSetting.getCheckAddress());+ String subAddressed = checkAddress.getSubAddressed(MailManager.TEST_SUB_ADDRESS);+ logger.log("Sending test mail to " + subAddressed + "...");+ mailManager.sendMail(mailSetting.getSendSetting(),+ Sets.newHashSet(subAddressed), Lists.newArrayList(), Lists.newArrayList(), uuid,+ "[Test] Test Email From OneDev", "This is a test email from OneDev",+ null, null, null);++ logger.log("Waiting for test mail to come back...");++ try {+ futureRef.get().get();+ } catch (CancellationException e) {+ } catch (InterruptedException e) {+ futureRef.get().cancel(true);+ throw new RuntimeException(e);+ } catch (ExecutionException e) {+ throw new RuntimeException(e);+ }++ logger.log("Received test mail");++ return "Great, your mail settings are working";+ } else {+ io.onedev.server.model.EmailAddress emailAddress = SecurityUtils.getUser().getPrimaryEmailAddress();+ if (emailAddress != null) {+ String body = "Great, your mail settings are working!";+ mailManager.sendMail(mailSettingHolder.getMailSetting().getSendSetting(), Sets.newHashSet(emailAddress.getValue()),+ Lists.newArrayList(), Lists.newArrayList(), "[Test] Test Email From OneDev",+ body, body, null, null, null);+ return "Test mail has been sent to " + emailAddress.getValue() + ", please check your mail box";} else {- io.onedev.server.model.EmailAddress emailAddress = SecurityUtils.getUser().getPrimaryEmailAddress();- if (emailAddress != null) {- String body = "Great, your mail settings are working!";- mailManager.sendMail(mailSettingHolder.getMailSetting().getSendSetting(), Sets.newHashSet(emailAddress.getValue()),- Lists.newArrayList(), Lists.newArrayList(), "[Test] Test Email From OneDev",- body, body, null, null, null);- return "Test mail has been sent to " + emailAddress.getValue() + ", please check your mail box";- } else {- throw new ExplicitException("Primary email address of your account is not specified yet");- }- }- }-+ throw new ExplicitException("Primary email address of your account is not specified yet");+ }+ }});}diff --git a/server-plugin/server-plugin-authenticator-ldap/src/main/java/io/onedev/server/plugin/authenticator/ldap/LdapAuthenticator.java b/server-plugin/server-plugin-authenticator-ldap/src/main/java/io/onedev/server/plugin/authenticator/ldap/LdapAuthenticator.javaindex 8351dd1bd3..fdf6bfe435 100644--- a/server-plugin/server-plugin-authenticator-ldap/src/main/java/io/onedev/server/plugin/authenticator/ldap/LdapAuthenticator.java+++ b/server-plugin/server-plugin-authenticator-ldap/src/main/java/io/onedev/server/plugin/authenticator/ldap/LdapAuthenticator.java@@ -6,6 +6,7 @@ import io.onedev.server.annotation.Password;import io.onedev.server.annotation.ShowCondition;import io.onedev.server.model.support.administration.authenticator.Authenticated;import io.onedev.server.model.support.administration.authenticator.Authenticator;+import io.onedev.server.security.TrustCertsSSLSocketFactory;import io.onedev.server.util.EditContext;import org.apache.shiro.authc.UnknownAccountException;import org.apache.shiro.authc.UsernamePasswordToken;@@ -210,7 +211,7 @@ public class LdapAuthenticator extends Authenticator {Hashtable<String, String> ldapEnv = new Hashtable<>();if (getLdapUrl().trim().startsWith("ldaps"))- ldapEnv.put("java.naming.ldap.factory.socket", LdapSSLSocketFactory.class.getName());+ ldapEnv.put("java.naming.ldap.factory.socket", TrustCertsSSLSocketFactory.class.getName());ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");ldapEnv.put(Context.PROVIDER_URL, getLdapUrl());ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple");diff --git a/server-product/src/test/java/io/onedev/server/product/Test.java b/server-product/src/test/java/io/onedev/server/product/Test.javaindex bd483080d6..457a49af9a 100644--- a/server-product/src/test/java/io/onedev/server/product/Test.java+++ b/server-product/src/test/java/io/onedev/server/product/Test.java@@ -1,12 +1,9 @@package io.onedev.server.product;-import org.apache.commons.lang3.SystemUtils;-public class Test {@org.junit.Testpublic void test() {- System.out.println(SystemUtils.getHostName());}}\ No newline at end of file