2

Trying to set a home directory for a user using Apache Mina SSHD embedded in Java.

Both solutions are deprecated in 1.0 in-
How to Set Root Directory in Apache Mina Sshd Server in Java
How to override getVirtualUserDir() in Apache Mina sshd-core version 0.14.0

In 0.14.0 the following worked fine:

sshd.setFileSystemFactory(new NativeFileSystemFactory() {
   @Override
   public FileSystemView createFileSystem(final Session session) {
      HashMap<String,String> map = new HashMap<String,String>();
      map.put("/", "/Users/someone/Documents");
      return new NativeFileSystemView(session.getUsername(), map, "/");
   };
});

This is as far as I got:

sshd.setFileSystemFactory(new NativeFileSystemFactory() {
    @Override
    public FileSystem createFileSystem(Session session) {
        // What should I do here?
        return super.createFileSystem(session);
    }
});
Community
  • 1
  • 1
Alon Amir
  • 4,913
  • 9
  • 47
  • 86

2 Answers2

4

Found it.
I had to use the VirtualFileSystemFactory class.

This is the result:

VirtualFileSystemFactory fsFactory = new VirtualFileSystemFactory();
fsFactory.setUserHomeDir(userName, realDirectory);
sshd.setFileSystemFactory(fsFactory);

Note: If you're using OS X or linux, don't forget to chmod your path first.

Alon Amir
  • 4,913
  • 9
  • 47
  • 86
0

For v1.2.0 (if Java 7) / v1.3.0 (if Java 8) and java.nio.file.Path usage, the solution could be :

sshServer.setFileSystemFactory(new VirtualFileSystemFactory(FileSystems.getDefault().getPath(rootDir)));
Alix Lourme
  • 1,135
  • 1
  • 11
  • 21