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 2017/05/26 18:02:17 UTC

mina-sshd git commit: [SSHD-749] SFTP client sends wrong open flags for protocol version 4

Repository: mina-sshd
Updated Branches:
  refs/heads/master d874b11c5 -> 89b60a883


[SSHD-749] SFTP client sends wrong open flags for protocol version 4


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/89b60a88
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/89b60a88
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/89b60a88

Branch: refs/heads/master
Commit: 89b60a883a58c006bb52fc8819c099fae9096cb9
Parents: d874b11
Author: Christian Leiner <ch...@gmx.de>
Authored: Fri May 26 21:01:21 2017 +0300
Committer: Lyor Goldstein <ly...@gmail.com>
Committed: Fri May 26 21:02:04 2017 +0300

----------------------------------------------------------------------
 .../subsystem/sftp/AbstractSftpClient.java      | 24 +++++++++----------
 .../client/subsystem/sftp/SftpVersionsTest.java | 25 ++++++++++++++++++++
 2 files changed, 36 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/89b60a88/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/AbstractSftpClient.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/AbstractSftpClient.java b/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/AbstractSftpClient.java
index a9b7b67..fb1aed8 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/AbstractSftpClient.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/AbstractSftpClient.java
@@ -564,7 +564,7 @@ public abstract class AbstractSftpClient extends AbstractSubsystemClient impleme
         buffer.putString(path);
         int version = getVersion();
         int mode = 0;
-        if (version == SftpConstants.SFTP_V3) {
+        if (version < SftpConstants.SFTP_V5) {
             for (OpenMode m : options) {
                 switch (m) {
                     case Read:
@@ -589,19 +589,17 @@ public abstract class AbstractSftpClient extends AbstractSubsystemClient impleme
                 }
             }
         } else {
-            if (version >= SftpConstants.SFTP_V5) {
-                int access = 0;
-                if (options.contains(OpenMode.Read)) {
-                    access |= SftpConstants.ACE4_READ_DATA | SftpConstants.ACE4_READ_ATTRIBUTES;
-                }
-                if (options.contains(OpenMode.Write)) {
-                    access |= SftpConstants.ACE4_WRITE_DATA | SftpConstants.ACE4_WRITE_ATTRIBUTES;
-                }
-                if (options.contains(OpenMode.Append)) {
-                    access |= SftpConstants.ACE4_APPEND_DATA;
-                }
-                buffer.putInt(access);
+            int access = 0;
+            if (options.contains(OpenMode.Read)) {
+                access |= SftpConstants.ACE4_READ_DATA | SftpConstants.ACE4_READ_ATTRIBUTES;
+            }
+            if (options.contains(OpenMode.Write)) {
+                access |= SftpConstants.ACE4_WRITE_DATA | SftpConstants.ACE4_WRITE_ATTRIBUTES;
+            }
+            if (options.contains(OpenMode.Append)) {
+                access |= SftpConstants.ACE4_APPEND_DATA;
             }
+            buffer.putInt(access);
 
             if (options.contains(OpenMode.Create) && options.contains(OpenMode.Exclusive)) {
                 mode |= SftpConstants.SSH_FXF_CREATE_NEW;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/89b60a88/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpVersionsTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpVersionsTest.java b/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpVersionsTest.java
index 5c53825..4d61a26 100644
--- a/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpVersionsTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpVersionsTest.java
@@ -20,6 +20,7 @@
 package org.apache.sshd.client.subsystem.sftp;
 
 import java.io.IOException;
+import java.io.OutputStream;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.LinkOption;
@@ -46,6 +47,7 @@ import org.apache.sshd.client.session.ClientSession;
 import org.apache.sshd.client.subsystem.sftp.SftpClient.Attributes;
 import org.apache.sshd.client.subsystem.sftp.SftpClient.CloseableHandle;
 import org.apache.sshd.client.subsystem.sftp.SftpClient.DirEntry;
+import org.apache.sshd.client.subsystem.sftp.SftpClient.OpenMode;
 import org.apache.sshd.common.NamedFactory;
 import org.apache.sshd.common.subsystem.sftp.SftpConstants;
 import org.apache.sshd.common.subsystem.sftp.SftpHelper;
@@ -100,6 +102,29 @@ public class SftpVersionsTest extends AbstractSftpClientTestSupport {
         return testVersion;
     }
 
+    @Test   // See SSHD-749
+    public void testSftpOpenFlags() throws Exception {
+        Path targetPath = detectTargetFolder();
+        Path lclSftp = Utils.resolve(targetPath, SftpConstants.SFTP_SUBSYSTEM_NAME, getClass().getSimpleName());
+        Path lclParent = assertHierarchyTargetFolderExists(lclSftp);
+        Path lclFile = lclParent.resolve(getCurrentTestName() + "-" + getTestedVersion() + ".txt");
+        Files.deleteIfExists(lclFile);
+
+        Path parentPath = targetPath.getParent();
+        String remotePath = Utils.resolveRelativeRemotePath(parentPath, lclFile);
+        try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) {
+            session.addPasswordIdentity(getCurrentTestName());
+            session.auth().verify(5L, TimeUnit.SECONDS);
+            try (SftpClient sftp = session.createSftpClient(getTestedVersion())) {
+                try (OutputStream out = sftp.write(remotePath, OpenMode.Create, OpenMode.Write)) {
+                    out.write(getCurrentTestName().getBytes(StandardCharsets.UTF_8));
+                }
+                assertTrue("File should exist on disk: " + lclFile, Files.exists(lclFile));
+                sftp.remove(remotePath);
+            }
+        }
+    }
+
     @Test
     public void testSftpVersionSelector() throws Exception {
         try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) {