You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2024/01/22 15:40:48 UTC
(james-project) branch master updated: [FIX] SMTP stack should recompute relaying rights upon PROXY message (#1933)
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 3445c5a1f0 [FIX] SMTP stack should recompute relaying rights upon PROXY message (#1933)
3445c5a1f0 is described below
commit 3445c5a1f0466769e357819ed8d5c2cca8a414a1
Author: Benoit TELLIER <bt...@linagora.com>
AuthorDate: Mon Jan 22 16:40:43 2024 +0100
[FIX] SMTP stack should recompute relaying rights upon PROXY message (#1933)
Otherwise, the relaying information is taken from the
proxy and not from the source.
---
.../james/protocols/smtp/SMTPSessionImpl.java | 15 +++-
.../james/smtpserver/SMTPProxyProtocolTest.java | 87 ++++++++++++++++++++++
.../src/test/resources/smtpserver-proxy.xml | 52 +++++++++++++
3 files changed, 152 insertions(+), 2 deletions(-)
diff --git a/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/SMTPSessionImpl.java b/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/SMTPSessionImpl.java
index f2302a2e8f..b5b35ac5c9 100644
--- a/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/SMTPSessionImpl.java
+++ b/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/SMTPSessionImpl.java
@@ -23,6 +23,7 @@ import java.util.Optional;
import org.apache.james.protocols.api.ProtocolSessionImpl;
import org.apache.james.protocols.api.ProtocolTransport;
+import org.apache.james.protocols.api.ProxyInformation;
import org.apache.james.protocols.api.Response;
/**
@@ -41,8 +42,12 @@ public class SMTPSessionImpl extends ProtocolSessionImpl implements SMTPSession
public SMTPSessionImpl(ProtocolTransport transport, SMTPConfiguration config) {
super(transport, config);
- relayingAllowed = Optional.ofNullable(getRemoteAddress())
- .flatMap(addres -> Optional.ofNullable(addres.getAddress()))
+ relayingAllowed = computeRelayingAllowed(config);
+ }
+
+ private Boolean computeRelayingAllowed(SMTPConfiguration config) {
+ return Optional.ofNullable(getRemoteAddress())
+ .flatMap(address -> Optional.ofNullable(address.getAddress()))
.flatMap(address -> Optional.ofNullable(address.getHostAddress()))
.map(config::isRelayingAllowed)
.orElse(false);
@@ -53,6 +58,12 @@ public class SMTPSessionImpl extends ProtocolSessionImpl implements SMTPSession
return relayingAllowed;
}
+ @Override
+ public void setProxyInformation(ProxyInformation proxyInformation) {
+ super.setProxyInformation(proxyInformation);
+ relayingAllowed = computeRelayingAllowed((SMTPConfiguration) config);
+ }
+
@Override
public void resetState() {
// remember the ehlo mode between resets
diff --git a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPProxyProtocolTest.java b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPProxyProtocolTest.java
new file mode 100644
index 0000000000..9adbcd466c
--- /dev/null
+++ b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPProxyProtocolTest.java
@@ -0,0 +1,87 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.smtpserver;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.apache.james.jmap.JMAPTestingConstants.DOMAIN;
+import static org.apache.james.jmap.JMAPTestingConstants.LOCALHOST_IP;
+import static org.apache.james.smtpserver.SMTPServerTestSystem.BOB;
+import static org.apache.james.smtpserver.SMTPServerTestSystem.DATE;
+import static org.apache.james.smtpserver.SMTPServerTestSystem.PASSWORD;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+import java.nio.channels.SocketChannel;
+import java.nio.charset.StandardCharsets;
+import java.time.Instant;
+import java.time.chrono.ChronoZonedDateTime;
+import java.util.Base64;
+
+import org.apache.commons.net.smtp.SMTPClient;
+import org.apache.james.queue.api.ManageableMailQueue;
+import org.assertj.core.api.SoftAssertions;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class SMTPProxyProtocolTest {
+ private final SMTPServerTestSystem testSystem = new SMTPServerTestSystem();
+
+ @BeforeEach
+ void setUp() throws Exception {
+ testSystem.setUp("smtpserver-proxy.xml");
+ }
+
+ @AfterEach
+ void tearDown() {
+ testSystem.smtpServer.destroy();
+ }
+
+ @Test
+ void rejectFutureReleaseUsageWhenUnauthenticated() throws Exception {
+ String protocol = "TCP4";
+ String source = "255.255.255.254";
+ String destination = "255.255.255.255";
+
+ SocketChannel server = SocketChannel.open();
+ server.connect(testSystem.getBindedAddress());
+ readBytes(server);
+
+ String proxyMessage = String.format("PROXY %s %s %s %d %d\r\n", protocol, source, destination, 65535, 65535);
+ server.write(ByteBuffer.wrap((proxyMessage).getBytes(StandardCharsets.UTF_8)));
+ server.write(ByteBuffer.wrap(("EHLO <" + DOMAIN + ">\r\n").getBytes(StandardCharsets.UTF_8)));
+ readBytes(server);
+ server.write(ByteBuffer.wrap(("MAIL FROM: <user@" + DOMAIN + ">\r\n").getBytes(StandardCharsets.UTF_8)));
+ readBytes(server);
+ server.write(ByteBuffer.wrap(("RCPT TO: <user@" + DOMAIN + ">\r\n").getBytes(StandardCharsets.UTF_8)));
+
+ assertThat(new String(readBytes(server))).contains("530 5.7.1 Authentication Required");
+ }
+
+ private byte[] readBytes(SocketChannel channel) throws IOException {
+ ByteBuffer line = ByteBuffer.allocate(1024);
+ channel.read(line);
+ line.rewind();
+ byte[] bline = new byte[line.remaining()];
+ line.get(bline);
+ return bline;
+ }
+}
diff --git a/server/protocols/protocols-smtp/src/test/resources/smtpserver-proxy.xml b/server/protocols/protocols-smtp/src/test/resources/smtpserver-proxy.xml
new file mode 100644
index 0000000000..671b7963ba
--- /dev/null
+++ b/server/protocols/protocols-smtp/src/test/resources/smtpserver-proxy.xml
@@ -0,0 +1,52 @@
+<?xml version="1.0"?>
+
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ -->
+
+<!-- Read https://james.apache.org/server/config-smtp-lmtp.html#SMTP_Configuration for further details -->
+
+ <smtpserver enabled="true">
+ <bind>0.0.0.0:0</bind>
+ <connectionBacklog>200</connectionBacklog>
+ <tls socketTLS="false" startTLS="false">
+ <keystore>file://conf/keystore</keystore>
+ <secret>james72laBalle</secret>
+ <provider>org.bouncycastle.jce.provider.BouncyCastleProvider</provider>
+ <algorithm>SunX509</algorithm>
+ </tls>
+ <connectiontimeout>360</connectiontimeout>
+ <connectionLimit>0</connectionLimit>
+ <connectionLimitPerIP>0</connectionLimitPerIP>
+ <auth>
+ <announce>forUnauthorizedAddresses</announce>
+ <requireSSL>false</requireSSL>
+ </auth>
+ <authorizedAddresses>127.0.0.0/8</authorizedAddresses>
+ <verifyIdentity>true</verifyIdentity>
+ <maxmessagesize>0</maxmessagesize>
+ <addressBracketsEnforcement>true</addressBracketsEnforcement>
+ <smtpGreeting>Apache JAMES awesome SMTP Server</smtpGreeting>
+ <handlerchain>
+ <handler class="org.apache.james.smtpserver.CoreCmdHandlerLoader"/>
+ </handlerchain>
+ <gracefulShutdown>false</gracefulShutdown>
+ <proxyRequired>true</proxyRequired>
+ </smtpserver>
+
+
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org