You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by gn...@apache.org on 2014/03/17 15:09:33 UTC

[1/2] git commit: [SSHD-294] Dubious SSH_FXP_REALPATH handling

Repository: mina-sshd
Updated Branches:
  refs/heads/master 6d32bbc18 -> a2e38bb57


[SSHD-294] Dubious SSH_FXP_REALPATH handling

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

Branch: refs/heads/master
Commit: 290554d7a5fa14072981dc8a5389e192e32673b3
Parents: 6d32bbc
Author: Guillaume Nodet <gn...@apache.org>
Authored: Mon Mar 17 14:12:51 2014 +0100
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Mon Mar 17 14:12:51 2014 +0100

----------------------------------------------------------------------
 .../apache/sshd/server/sftp/SftpSubsystem.java  | 27 +++++++++++++++++---
 .../src/test/java/org/apache/sshd/SftpTest.java | 13 ++++++++++
 2 files changed, 37 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/290554d7/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java b/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java
index 94f50f3..f96e4b2 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java
@@ -712,7 +712,7 @@ public class SftpSubsystem implements Command, Runnable, SessionAware, FileSyste
                 }
                 try {
                     SshFile p = resolveFile(path);
-                    sendPath(id, p);
+                    sendPath(id, p, false);
                 } catch (FileNotFoundException e) {
                     e.printStackTrace();
                     sendStatus(id, SSH_FX_NO_SUCH_FILE, e.getMessage());
@@ -795,6 +795,10 @@ public class SftpSubsystem implements Command, Runnable, SessionAware, FileSyste
     }
 
     protected void sendPath(int id, SshFile f) throws IOException {
+        sendPath(id, f, true);
+    }
+
+    protected void sendPath(int id, SshFile f, boolean sendAttrs) throws IOException {
         Buffer buffer = new Buffer();
         buffer.putByte((byte) SSH_FXP_NAME);
         buffer.putInt(id);
@@ -809,7 +813,7 @@ public class SftpSubsystem implements Command, Runnable, SessionAware, FileSyste
         if (f.getName().length() == 0) {
             f = resolveFile(".");
         }
-        buffer.putString(getLongName(f)); // Format specified in the specs
+        buffer.putString(getLongName(f, sendAttrs)); // Format specified in the specs
         buffer.putInt(0);
         send(buffer);
     }
@@ -848,7 +852,24 @@ public class SftpSubsystem implements Command, Runnable, SessionAware, FileSyste
     }
 
     private String getLongName(SshFile f) throws IOException {
-        Map<SshFile.Attribute, Object> attributes = f.getAttributes(true);
+        return getLongName(f, true);
+    }
+
+    private String getLongName(SshFile f, boolean sendAttrs) throws IOException {
+        Map<SshFile.Attribute, Object> attributes;
+        if (sendAttrs) {
+            attributes = f.getAttributes(true);
+        } else {
+            attributes = new HashMap<SshFile.Attribute, Object>();
+            attributes.put(SshFile.Attribute.Owner, "owner");
+            attributes.put(SshFile.Attribute.Group, "group");
+            attributes.put(SshFile.Attribute.Size, (long) 0);
+            attributes.put(SshFile.Attribute.IsDirectory, false);
+            attributes.put(SshFile.Attribute.IsSymbolicLink, false);
+            attributes.put(SshFile.Attribute.IsRegularFile, false);
+            attributes.put(SshFile.Attribute.Permissions, EnumSet.noneOf(SshFile.Permission.class));
+            attributes.put(SshFile.Attribute.LastModifiedTime, (long) 0);
+        }
         String username = (String) attributes.get(SshFile.Attribute.Owner);
         if (username.length() > 8) {
             username = username.substring(0, 8);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/290554d7/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/SftpTest.java b/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
index cc0770b..0d687e9 100644
--- a/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
@@ -222,6 +222,19 @@ public class SftpTest extends BaseTest {
         }
     }
 
+    @Test
+    public void testRealPath() throws Exception {
+        ChannelSftp c = (ChannelSftp) session.openChannel("sftp");
+        c.connect();
+
+        URI url = getClass().getClassLoader().getResource(SshClient.class.getName().replace('.', '/') + ".class").toURI();
+        URI base = new File(System.getProperty("user.dir")).getAbsoluteFile().toURI();
+        String path = new File(base.relativize(url).getPath()).getParent() + "/";
+        path = path.replace('\\', '/');
+        String real = c.realpath(path + "/foobar");
+        System.out.println(real);
+    }
+
     protected void assertFileLength(File file, long length, long timeout) throws Exception {
         boolean ok = false;
         while (timeout > 0) {


[2/2] git commit: [SSHD-293] SSH_FXP_SYMLINK not supported in SftpSubsystem

Posted by gn...@apache.org.
[SSHD-293] SSH_FXP_SYMLINK not supported in SftpSubsystem

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

Branch: refs/heads/master
Commit: a2e38bb575057d3d2a31944653ad8773a15180d7
Parents: 290554d
Author: Guillaume Nodet <gn...@apache.org>
Authored: Mon Mar 17 14:50:10 2014 +0100
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Mon Mar 17 14:50:10 2014 +0100

----------------------------------------------------------------------
 .../org/apache/sshd/common/file/SshFile.java    |  2 ++
 .../common/file/nativefs/NativeSshFile.java     |  4 +++
 .../common/file/nativefs/NativeSshFileNio.java  |  9 +++++++
 .../apache/sshd/server/sftp/SftpSubsystem.java  | 16 +++++++++++
 .../src/test/java/org/apache/sshd/SftpTest.java | 28 ++++++++++++++++++++
 5 files changed, 59 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a2e38bb5/sshd-core/src/main/java/org/apache/sshd/common/file/SshFile.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/file/SshFile.java b/sshd-core/src/main/java/org/apache/sshd/common/file/SshFile.java
index 0b204c8..df14bb6 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/file/SshFile.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/file/SshFile.java
@@ -90,6 +90,8 @@ public interface SshFile {
 
     String readSymbolicLink() throws IOException;
 
+    void createSymbolicLink(SshFile destination) throws IOException;
+
 
     /**
      * Get the owner name of the file

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a2e38bb5/sshd-core/src/main/java/org/apache/sshd/common/file/nativefs/NativeSshFile.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/file/nativefs/NativeSshFile.java b/sshd-core/src/main/java/org/apache/sshd/common/file/nativefs/NativeSshFile.java
index 5d2acbe..eef3a73 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/file/nativefs/NativeSshFile.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/file/nativefs/NativeSshFile.java
@@ -648,4 +648,8 @@ public class NativeSshFile implements SshFile {
     public String readSymbolicLink() throws IOException {
         throw new UnsupportedOperationException();
     }
+
+    public void createSymbolicLink(SshFile destination) throws IOException {
+        throw new UnsupportedOperationException();
+    }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a2e38bb5/sshd-core/src/main/java/org/apache/sshd/common/file/nativefs/NativeSshFileNio.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/file/nativefs/NativeSshFileNio.java b/sshd-core/src/main/java/org/apache/sshd/common/file/nativefs/NativeSshFileNio.java
index bc83a97..c4cde42 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/file/nativefs/NativeSshFileNio.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/file/nativefs/NativeSshFileNio.java
@@ -26,6 +26,7 @@ import java.nio.channels.FileChannel;
 import java.nio.file.Files;
 import java.nio.file.LinkOption;
 import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.nio.file.attribute.FileTime;
 import java.nio.file.attribute.GroupPrincipal;
 import java.nio.file.attribute.PosixFilePermission;
@@ -37,6 +38,8 @@ import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.sshd.common.file.SshFile;
+
 /**
  * <strong>Internal class, do not use directly.</strong>
  * 
@@ -108,6 +111,12 @@ public class NativeSshFileNio extends NativeSshFile {
         return link.toString();
     }
 
+    public void createSymbolicLink(SshFile destination) throws IOException {
+        Path link = file.toPath();
+        Path target = Paths.get(destination.getAbsolutePath());
+        Files.createSymbolicLink(target, link);
+    }
+
     private EnumSet<Permission> fromPerms(Set<PosixFilePermission> perms) {
         EnumSet<Permission> p = EnumSet.noneOf(Permission.class);
         for (PosixFilePermission perm : perms) {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a2e38bb5/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java b/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java
index f96e4b2..df56bd4 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java
@@ -770,6 +770,22 @@ public class SftpSubsystem implements Command, Runnable, SessionAware, FileSyste
                 }
                 break;
             }
+            case SSH_FXP_SYMLINK: {
+                String linkpath = buffer.getString();
+                String targetpath = buffer.getString();
+                log.debug("Received SSH_FXP_SYMLINK (linkpath={}, targetpath={})", linkpath, targetpath);
+                try {
+                    SshFile link = resolveFile(linkpath);
+                    SshFile target = resolveFile(targetpath);
+                    link.createSymbolicLink(target);
+                    sendStatus(id, SSH_FX_OK, "");
+                } catch (UnsupportedOperationException e) {
+                    sendStatus(id, SSH_FX_OP_UNSUPPORTED, "Command " + type + " is unsupported or not implemented");
+                } catch (IOException e) {
+                    sendStatus(id, SSH_FX_FAILURE, e.getMessage());
+                }
+                break;
+            }
             default: {
                 log.error("Received: {}", type);
                 sendStatus(id, SSH_FX_OP_UNSUPPORTED, "Command " + type + " is unsupported or not implemented");

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a2e38bb5/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/SftpTest.java b/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
index 0d687e9..256fa85 100644
--- a/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
@@ -235,6 +235,34 @@ public class SftpTest extends BaseTest {
         System.out.println(real);
     }
 
+    @Test
+    public void testCreateSymbolicLink() throws Exception {
+        File root = new File("target/sftp");
+        String unixPath = "target/sftp/out.txt";
+        String linkUnixPath = "target/sftp/link.txt";
+        File target = new File(unixPath);
+        File link = new File(linkUnixPath);
+        Utils.deleteRecursive(root);
+        root.mkdirs();
+        assertTrue(root.exists());
+
+        ChannelSftp c = (ChannelSftp) session.openChannel("sftp");
+        c.connect();
+        c.put(new ByteArrayInputStream("0123456789".getBytes()), unixPath);
+
+        assertTrue(target.exists());
+        assertEquals("0123456789", readFile(unixPath));
+
+        c.symlink(unixPath, linkUnixPath);
+
+        assertTrue(link.exists());
+        assertEquals("0123456789", readFile(linkUnixPath));
+
+        String str1 = c.readlink(linkUnixPath);
+        String str2 = c.realpath(unixPath);
+        assertEquals(str1, str2);
+    }
+
     protected void assertFileLength(File file, long length, long timeout) throws Exception {
         boolean ok = false;
         while (timeout > 0) {