@Override public void afterPropertiesSet() throws Exception { final PublicKey allowedKey = decodePublicKey(); this.server.setPublickeyAuthenticator((username, key, session) -> key.equals(allowedKey)); this.server.setPort(this.port); this.server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser"))); this.server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); final String pathname = System.getProperty("java.io.tmpdir") + File.separator + "sftptest" + File.separator; new File(pathname).mkdirs(); server.setFileSystemFactory(new VirtualFileSystemFactory(Paths.get(pathname))); }
protected void signalHandleOpening(SftpSubsystem subsystem) throws IOException { SftpEventListener listener = subsystem.getSftpEventListenerProxy(); ServerSession session = subsystem.getServerSession(); listener.opening(session, handle, this); }
protected void signalHandleOpen(SftpSubsystem subsystem) throws IOException { SftpEventListener listener = subsystem.getSftpEventListenerProxy(); ServerSession session = subsystem.getServerSession(); listener.open(session, handle, this); }
@Override public SftpSubsystemFactory build() { SftpSubsystemFactory factory = new SftpSubsystemFactory(); factory.setExecutorService(executors); factory.setUnsupportedAttributePolicy(policy); factory.setFileSystemAccessor(fileSystemAccessor); factory.setErrorStatusDataHandler(errorStatusDataHandler); GenericUtils.forEach(getRegisteredListeners(), factory::addSftpEventListener); return factory; } }
@Override protected void doOpenSSHFsync(int id, String handle) throws IOException { Handle h = handles.get(handle); ServerSession session = getServerSession(); if (log.isDebugEnabled()) { log.debug("doOpenSSHFsync({})[id={}] {}[{}]", session, id, handle, h); } FileHandle fileHandle = validateHandle(handle, h, FileHandle.class); SftpFileSystemAccessor accessor = getFileSystemAccessor(); accessor.syncFileData(session, this, fileHandle.getFile(), fileHandle.getFileHandle(), fileHandle.getFileChannel()); }
@Override public Command create() { SftpSubsystem subsystem = new SftpSubsystem(getExecutorService(), getUnsupportedAttributePolicy(), getFileSystemAccessor(), getErrorStatusDataHandler()); GenericUtils.forEach(getRegisteredListeners(), subsystem::addSftpEventListener); return subsystem; } }
protected void doSetAttributes(Path file, Map<String, ?> attributes) throws IOException { SftpEventListener listener = getSftpEventListenerProxy(); ServerSession session = getServerSession(); listener.modifyingAttributes(session, file, attributes); try { setFileAttributes(file, attributes, IoUtils.getLinkOptions(false)); } catch (IOException | RuntimeException e) { listener.modifiedAttributes(session, file, attributes, e); throw e; } listener.modifiedAttributes(session, file, attributes, null); }
protected Path resolveNormalizedLocation(String remotePath) throws IOException, InvalidPathException { Path resolvedPath = resolveFile(remotePath); return normalize(resolvedPath); }
@Override public void close() throws IOException { super.close(); SeekableByteChannel channel = getFileChannel(); if (channel.isOpen()) { channel.close(); } }
@Override public String toString() { return getName(); } }
protected void doLink(int id, String targetPath, String linkPath, boolean symLink) throws IOException { createLink(id, targetPath, linkPath, symLink); }
protected SimpleImmutableEntry<Path, Boolean> doRealPathV345(int id, String path, Path p, LinkOption... options) throws IOException { return validateRealPath(id, path, p, options); }
protected void sendStatus(Buffer buffer, int id, int substatus, String msg) throws IOException { sendStatus(buffer, id, substatus, (msg != null) ? msg : "", ""); }
public void startServer() throws IOException { sshd = SshServer.setUpDefaultServer(); sshd.setHost("localhost"); sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider()); //Accept all keys for authentication sshd.setPublickeyAuthenticator((s, publicKey, serverSession) -> true); //Allow username/password authentication using pre-defined credentials sshd.setPasswordAuthenticator((username, password, serverSession) -> this.username.equals(username) && this.password.equals(password)); //Setup Virtual File System (VFS) //Ensure VFS folder exists Path dir = Paths.get(getVirtualFileSystemPath()); Files.createDirectories(dir); sshd.setFileSystemFactory(new VirtualFileSystemFactory(dir.toAbsolutePath())); //Add SFTP support List<NamedFactory<Command>> sftpCommandFactory = new ArrayList<>(); sftpCommandFactory.add(new SftpSubsystemFactory()); sshd.setSubsystemFactories(sftpCommandFactory); sshd.start(); }
private SshServer defaultSshServer() { SshServer sshServer = SshServer.setUpDefaultServer(); sshServer.setPort(randomPort()); ClassLoadableResourceKeyPairProvider fileKeyPairProvider = new ClassLoadableResourceKeyPairProvider(hostkey); sshServer.setKeyPairProvider(fileKeyPairProvider); sshServer.setPasswordAuthenticator(new PasswordAuthenticator() { @Override public boolean authenticate(String username, String password, ServerSession session) { return username.equals(password); } }); sshServer.setGSSAuthenticator(new BogusGSSAuthenticator()); sshServer.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystemFactory())); ScpCommandFactory commandFactory = new ScpCommandFactory(); commandFactory.setDelegateCommandFactory(new CommandFactory() { @Override public Command createCommand(String command) { return new ProcessShellFactory(command.split(" ")).create(); } }); sshServer.setCommandFactory(commandFactory); sshServer.setShellFactory(new ProcessShellFactory("ls")); return sshServer; }
private DefaultSftpSessionFactory createServerAndClient(SshServer server) throws IOException { server.setPublickeyAuthenticator((username, key, session) -> true); server.setPort(0); server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath())); server.start(); DefaultSftpSessionFactory f = new DefaultSftpSessionFactory(); f.setHost("localhost"); f.setPort(server.getPort()); f.setUser("user"); Resource privateKey = new ClassPathResource("id_rsa"); f.setPrivateKey(privateKey); return f; }
@BeforeClass public static void createServer() throws Exception { server = SshServer.setUpDefaultServer(); server.setPasswordAuthenticator((username, password, session) -> true); server.setPort(0); server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath())); server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); server.setFileSystemFactory(new VirtualFileSystemFactory(remoteTemporaryFolder.getRoot().toPath())); server.start(); port = server.getPort(); }
@Test public void testUcPw() throws Exception { SshServer server = SshServer.setUpDefaultServer(); try { server.setPasswordAuthenticator((arg0, arg1, arg2) -> true); server.setPort(0); server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath())); server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); final String pathname = System.getProperty("java.io.tmpdir") + File.separator + "sftptest" + File.separator; new File(pathname).mkdirs(); server.setFileSystemFactory(new VirtualFileSystemFactory(Paths.get(pathname))); server.start(); DefaultSftpSessionFactory f = new DefaultSftpSessionFactory(); f.setHost("localhost"); f.setPort(server.getPort()); f.setUser("user"); f.setPassword("pass"); f.setAllowUnknownKeys(true); Session<LsEntry> session = f.getSession(); doTest(server, session); } finally { server.stop(true); } }
private void testKeyExchange(String pubKey, String privKey, String passphrase) throws Exception { SshServer server = SshServer.setUpDefaultServer(); final PublicKey allowedKey = decodePublicKey(pubKey); try { server.setPublickeyAuthenticator((username, key, session) -> key.equals(allowedKey)); server.setPort(0); server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath())); server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); final String pathname = System.getProperty("java.io.tmpdir") + File.separator + "sftptest" + File.separator; new File(pathname).mkdirs(); server.setFileSystemFactory(new VirtualFileSystemFactory(Paths.get(pathname))); server.start(); DefaultSftpSessionFactory f = new DefaultSftpSessionFactory(); f.setHost("localhost"); f.setPort(server.getPort()); f.setUser("user"); f.setAllowUnknownKeys(true); InputStream stream = new ClassPathResource(privKey).getInputStream(); f.setPrivateKey(new ByteArrayResource(FileCopyUtils.copyToByteArray(stream))); f.setPrivateKeyPassphrase(passphrase); Session<LsEntry> session = f.getSession(); doTest(server, session); } finally { server.stop(true); } }