Alon Amir
Alon Amir

Reputation: 5035

Apache Mina SSHD 1.0.0 set user directory & mapping

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);
    }
});

Upvotes: 2

Views: 3653

Answers (2)

Alix Lourme
Alix Lourme

Reputation: 1135

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)));

Upvotes: 0

Alon Amir
Alon Amir

Reputation: 5035

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.

Upvotes: 4

Related Questions