X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.core%2Fsrc%2Forg%2Fargeo%2Fssh%2FAbstractSsh.java;h=ae1d6a0d4acabf446948ccbfce60026140c1ef8b;hb=bc5e10532e11e384a47264fec26e8afe531654b6;hp=9c4ec567acf4206c1d1b6eed58e0ab0b7f9ee7f4;hpb=5c6333d04de4985c349197852414faa0f4ee33ee;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.core/src/org/argeo/ssh/AbstractSsh.java b/org.argeo.core/src/org/argeo/ssh/AbstractSsh.java index 9c4ec567a..ae1d6a0d4 100644 --- a/org.argeo.core/src/org/argeo/ssh/AbstractSsh.java +++ b/org.argeo.core/src/org/argeo/ssh/AbstractSsh.java @@ -1,9 +1,12 @@ package org.argeo.ssh; +import java.io.Console; import java.io.IOException; import java.net.URI; import java.security.GeneralSecurityException; import java.security.KeyPair; +import java.util.Arrays; +import java.util.Scanner; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -11,7 +14,7 @@ import org.apache.sshd.client.SshClient; import org.apache.sshd.client.config.keys.ClientIdentityLoader; import org.apache.sshd.client.future.ConnectFuture; import org.apache.sshd.client.session.ClientSession; -import org.apache.sshd.client.subsystem.sftp.SftpFileSystemProvider; +import org.apache.sshd.client.subsystem.sftp.fs.SftpFileSystemProvider; import org.apache.sshd.common.config.keys.FilePasswordProvider; abstract class AbstractSsh { @@ -35,9 +38,9 @@ abstract class AbstractSsh { } return sshClient; } - + synchronized SftpFileSystemProvider getSftpFileSystemProvider() { - if(sftpFileSystemProvider==null) { + if (sftpFileSystemProvider == null) { sftpFileSystemProvider = new SftpFileSystemProvider(sshClient); } return sftpFileSystemProvider; @@ -45,6 +48,22 @@ abstract class AbstractSsh { void authenticate() { try { + if (!passwordSet) { + String password; + Console console = System.console(); + if (console == null) {// IDE + System.out.print("Password: "); + Scanner s = new Scanner(System.in); + password = s.next(); + } else { + console.printf("Password: "); + char[] pwd = console.readPassword(); + password = new String(pwd); + Arrays.fill(pwd, ' '); + } + session.addPasswordIdentity(password); + passwordSet = true; + } session.auth().verify(1000l); } catch (IOException e) { throw new IllegalStateException(e); @@ -60,13 +79,13 @@ abstract class AbstractSsh { } void loadKey(String password, String keyPath) { - try { - KeyPair keyPair = ClientIdentityLoader.DEFAULT.loadClientIdentity(keyPath, - FilePasswordProvider.of(password)); - session.addPublicKeyIdentity(keyPair); - } catch (IOException | GeneralSecurityException e) { - throw new IllegalStateException(e); - } +// try { +// KeyPair keyPair = ClientIdentityLoader.DEFAULT.loadClientIdentity(keyPath, +// FilePasswordProvider.of(password)); +// session.addPublicKeyIdentity(keyPair); +// } catch (IOException | GeneralSecurityException e) { +// throw new IllegalStateException(e); +// } } void openSession(URI uri) { @@ -107,7 +126,7 @@ abstract class AbstractSsh { } void closeSession() { - if (session != null) + if (session == null) throw new IllegalStateException("No session is open"); try { session.close();