You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2019/02/01 17:24:04 UTC

[mina-sshd] branch master updated: [SSHD-886] Do not send SSH_MSG_UNIMPLEMENTED reply if registered ReservedSessionMessagesHandler signals that it has handled the unknown packet type

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

lgoldstein pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mina-sshd.git


The following commit(s) were added to refs/heads/master by this push:
     new 4fb51ad  [SSHD-886] Do not send SSH_MSG_UNIMPLEMENTED reply if registered ReservedSessionMessagesHandler signals that it has handled the unknown packet type
4fb51ad is described below

commit 4fb51ad574d1102832e0f9bd51365819d276ec86
Author: Lyor Goldstein <lg...@apache.org>
AuthorDate: Fri Feb 1 18:54:33 2019 +0200

    [SSHD-886] Do not send SSH_MSG_UNIMPLEMENTED reply if registered ReservedSessionMessagesHandler signals that it has handled the unknown packet type
---
 CHANGES.md                                               |  3 +++
 README.md                                                | 16 ++++++++++++++++
 .../java/org/apache/sshd/common/io/IoWriteFuture.java    |  1 -
 .../common/session/ReservedSessionMessagesHandler.java   |  6 ++++--
 .../sshd/common/session/helpers/AbstractSession.java     |  9 +++++++--
 .../helpers/ReservedSessionMessagesHandlerAdapter.java   |  8 ++++++--
 .../sshd/common/session/helpers/AbstractSessionTest.java |  3 ++-
 7 files changed, 38 insertions(+), 8 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index b5a3abe..c1a02e4 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -168,6 +168,9 @@ for exposing the cipher's block size. **Note:** for the time being we declare a
 implement `AttributeStore` interface - which means that `SftpEventListener`(s) can now attach user-defined attributes
 to the generated handle(s).
 
+* [SSHD-886](https://issues.apache.org/jira/browse/SSHD-886) - Do not send `SSH_MSG_UNIMPLEMENTED` reply if registered
+`ReservedSessionMessagesHandler` signals that it has handled the unknown packet type.
+
 * `SftpCommandMain` shows by default `get/put` command progress using the hash sign (`#`) marker. The marker
 can be enabled/disabled via the `progress` command:
 
diff --git a/README.md b/README.md
index ee5045f..c5237a3 100644
--- a/README.md
+++ b/README.md
@@ -1637,6 +1637,22 @@ message received in the session as well.
 
 ```java
 
+    class MyClientSideReservedSessionMessagesHandler implements ReservedSessionMessagesHandler {
+        @Override
+        public boolean handleUnimplementedMessage(Session session, int cmd, Buffer buffer) throws Exception {
+            switch(cmd) {
+                case MY_SPECIAL_CMD1:
+                    ....
+                    return true;
+                case MY_SPECIAL_CMD2:
+                    ....
+                    return true;
+                default:
+                    return false;    // send SSH_MSG_UNIMPLEMENTED reply if necessary
+            }
+        }
+    }
+
     // client side
     SshClient client = SshClient.setupDefaultClient();
     // This is the default for ALL sessions unless specifically overridden
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/IoWriteFuture.java b/sshd-core/src/main/java/org/apache/sshd/common/io/IoWriteFuture.java
index fa56044..b72aa4b 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/io/IoWriteFuture.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/io/IoWriteFuture.java
@@ -34,5 +34,4 @@ public interface IoWriteFuture extends SshFuture<IoWriteFuture>, VerifiableFutur
      * between the two.
      */
     Throwable getException();
-
 }
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/session/ReservedSessionMessagesHandler.java b/sshd-core/src/main/java/org/apache/sshd/common/session/ReservedSessionMessagesHandler.java
index c7c6012..48ab970 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/session/ReservedSessionMessagesHandler.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/session/ReservedSessionMessagesHandler.java
@@ -60,10 +60,12 @@ public interface ReservedSessionMessagesHandler extends SshdEventListener {
      * @param session The {@code Session} through which the message was received
      * @param cmd The received (un-implemented) command
      * @param buffer The {@code Buffer} containing the data - positioned just beyond the command
+     * @return {@code true} if message handled internally, {@code false} if should
+     * return a {@code SSH_MSG_UNIMPLEMENTED} reply (default behavior)
      * @throws Exception If failed to handle the message
      * @see <A HREF="https://tools.ietf.org/html/rfc4253#section-11.4">RFC 4253 - section 11.4</A>
      */
-    default void handleUnimplementedMessage(Session session, int cmd, Buffer buffer) throws Exception {
-        // ignored
+    default boolean handleUnimplementedMessage(Session session, int cmd, Buffer buffer) throws Exception {
+        return false;
     }
 }
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSession.java b/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSession.java
index f16a881..e582039 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSession.java
@@ -1368,13 +1368,18 @@ public abstract class AbstractSession extends SessionHelper {
      * @param cmd The un-implemented command value
      * @param buffer The {@link Buffer} that contains the command. <b>Note:</b> the
      * buffer's read position is just beyond the command.
-     * @return An {@link IoWriteFuture} that can be used to wait for packet write completion
+     * @return An {@link IoWriteFuture} that can be used to wait for packet write
+     * completion - {@code null} if the registered {@link ReservedSessionMessagesHandler}
+     * decided to handle the command internally
      * @throws Exception if an error occurred while handling the packet.
      * @see #sendNotImplemented(long)
      */
     protected IoWriteFuture notImplemented(int cmd, Buffer buffer) throws Exception {
         ReservedSessionMessagesHandler handler = resolveReservedSessionMessagesHandler();
-        handler.handleUnimplementedMessage(this, cmd, buffer);
+        if (handler.handleUnimplementedMessage(this, cmd, buffer)) {
+            return null;
+        }
+
         return sendNotImplemented(seqi - 1L);
     }
 
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/ReservedSessionMessagesHandlerAdapter.java b/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/ReservedSessionMessagesHandlerAdapter.java
index 6abd4b3..b88717a 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/ReservedSessionMessagesHandlerAdapter.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/ReservedSessionMessagesHandlerAdapter.java
@@ -61,7 +61,9 @@ public class ReservedSessionMessagesHandlerAdapter
         handleDebugMessage(session, buffer.getBoolean(), buffer.getString(), buffer.getString(), buffer);
     }
 
-    public void handleDebugMessage(Session session, boolean display, String msg, String lang, Buffer buffer) throws Exception {
+    public void handleDebugMessage(
+            Session session, boolean display, String msg, String lang, Buffer buffer)
+                throws Exception {
         if (log.isDebugEnabled()) {
             log.debug("handleDebugMessage({}) SSH_MSG_DEBUG (display={}) [lang={}] '{}'",
                       session, display, lang, msg);
@@ -69,7 +71,7 @@ public class ReservedSessionMessagesHandlerAdapter
     }
 
     @Override
-    public void handleUnimplementedMessage(Session session, int cmd, Buffer buffer) throws Exception {
+    public boolean handleUnimplementedMessage(Session session, int cmd, Buffer buffer) throws Exception {
         boolean debugEnabled = log.isDebugEnabled();
         if (debugEnabled) {
             if (cmd == SshConstants.SSH_MSG_UNIMPLEMENTED) {
@@ -79,5 +81,7 @@ public class ReservedSessionMessagesHandlerAdapter
                 log.debug("handleUnimplementedMessage({}): {}", session, SshConstants.getCommandMessageName(cmd));
             }
         }
+
+        return false;
     }
 }
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/session/helpers/AbstractSessionTest.java b/sshd-core/src/test/java/org/apache/sshd/common/session/helpers/AbstractSessionTest.java
index 22eb511..770dca8 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/session/helpers/AbstractSessionTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/session/helpers/AbstractSessionTest.java
@@ -203,8 +203,9 @@ public class AbstractSessionTest extends BaseTestSupport {
     public void testMalformedUnimplementedMessage() throws Exception {
         session.setReservedSessionMessagesHandler(new ReservedSessionMessagesHandler() {
             @Override
-            public void handleUnimplementedMessage(Session session, int cmd, Buffer buffer) throws Exception {
+            public boolean handleUnimplementedMessage(Session session, int cmd, Buffer buffer) throws Exception {
                 fail("Unexpected invocation: available=" + buffer.available());
+                return false;
             }
         });