This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git


The following commit(s) were added to refs/heads/master by this push:
     new bf5d0be43c JAMES-4038 Relax EHLO validation for EMclient (#2262)
bf5d0be43c is described below

commit bf5d0be43c922a671410f57cc4356e97ced84c5f
Author: Benoit TELLIER <[email protected]>
AuthorDate: Fri May 31 10:23:55 2024 +0200

    JAMES-4038 Relax EHLO validation for EMclient (#2262)
---
 .../protocols/smtp/core/esmtp/EhloCmdHandler.java  | 28 ++++++++++++++++++++--
 .../apache/james/smtpserver/SMTPServerTest.java    | 16 +++++++++++++
 2 files changed, 42 insertions(+), 2 deletions(-)

diff --git 
a/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/esmtp/EhloCmdHandler.java
 
b/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/esmtp/EhloCmdHandler.java
index 347a569e99..01e3b4a5e3 100644
--- 
a/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/esmtp/EhloCmdHandler.java
+++ 
b/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/esmtp/EhloCmdHandler.java
@@ -100,8 +100,32 @@ public class EhloCmdHandler extends 
AbstractHookableCmdHandler<HeloHook> impleme
         // Without [] Guava attempt to parse IPV4
         return InetAddresses.isUriInetAddress(hostname)
             // Guava tries parsing IPv6 if and only if wrapped by []
-            || InetAddresses.isUriInetAddress("[" + hostname + "]")
-            || InternetDomainName.isValid(hostname);
+            || InetAddresses.isUriInetAddress("[" + 
removeEmIPV6Prefix(hostname) + "]")
+            || InternetDomainName.isValid(hostname)
+            || emClientCompatibility(hostname);
+    }
+
+    // CF JAMES-4040 IPv6v4-full https://datatracker.ietf.org/doc/html/rfc5321
+    private boolean emClientCompatibility(String hostname) {
+        int separator = hostname.lastIndexOf(':');
+        if (separator == -1 || separator == hostname.length() - 1) {
+            return false;
+        }
+        String ipv4 = hostname.substring(separator + 1);
+        String ipv6 = removeEmIPV6Prefix(hostname.substring(0, separator));
+
+        boolean isIPv6 = InetAddresses.isInetAddress(ipv6)
+            || InetAddresses.isUriInetAddress(ipv6)
+            || InetAddresses.isUriInetAddress("[" + ipv6 + "]");
+        return InetAddresses.isInetAddress(ipv4)
+            && isIPv6;
+    }
+
+    private static String removeEmIPV6Prefix(String ipv6) {
+        if (ipv6.startsWith("IPv6:")) {
+            ipv6 = ipv6.substring(5);
+        }
+        return ipv6;
     }
 
     private String unquote(String argument) {
diff --git 
a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java
 
b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java
index ac4c0a0e4c..4ad2487a08 100644
--- 
a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java
+++ 
b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java
@@ -1407,6 +1407,22 @@ public class SMTPServerTest {
             .isNotNull();
     }
 
+
+    // CF JAMES-4040
+    @Test
+    public void shouldBeCompatibleWithEMClient() throws Exception {
+        smtpConfiguration.setAuthorizedAddresses("128.0.0.1/8");
+        smtpConfiguration.setAuthorizingAnnounce();
+        init(smtpConfiguration);
+
+        SMTPClient smtpProtocol = new SMTPClient();
+        InetSocketAddress bindedAddress = testSystem.getBindedAddress();
+        smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), 
bindedAddress.getPort());
+
+        smtpProtocol.sendCommand("ehlo", "[IPv6:::ffff:172.16.149.220]");
+        assertThat(smtpProtocol.getReplyString()).contains("250");
+    }
+
     @Test
     public void testAuthSendMailFromDomainAlias() throws Exception {
         smtpConfiguration.setAuthorizedAddresses("128.0.0.1/8");


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to