private void checkDefaultConfiguration(SshConfiguration configuration) { if (configuration == null) { if (supportsDefaultConfiguration) { return; } else { throw new UnsupportedOperationException("Default configuration is not supported. You must supply a configuration."); } } configuration.providerHasCapabilities(this); check(configuration.getPreferredClientToServerCipher(), getSupportedCiphers(configuration.getProtocolVersion())); check(configuration.getPreferredServerToClientCipher(), getSupportedCiphers(configuration.getProtocolVersion())); check(configuration.getPreferredClientToServerMAC(), getSupportedMAC()); check(configuration.getPreferredServerToClientMAC(), getSupportedMAC()); check(configuration.getPreferredClientToServerCompression(), getSupportedCompression()); check(configuration.getPreferredServerToClientCompression(), getSupportedCompression()); if (configuration.getProxyServer() != null) { SshProxyServerDetails proxy = configuration.getProxyServer(); if (proxy.getType().equals(SshProxyServerDetails.Type.HTTP) && !getCapabilities().contains(Capability.HTTP_PROXY)) { throw new UnsupportedOperationException("HTTP proxy is not supported."); } if (proxy.getType().equals(SshProxyServerDetails.Type.SOCKS4) && !getCapabilities().contains(Capability.SOCKS4_PROXY)) { throw new UnsupportedOperationException("SOCKS4 proxy is not supported."); } if (proxy.getType().equals(SshProxyServerDetails.Type.SOCKS5) && !getCapabilities().contains(Capability.SOCKS5_PROXY)) { throw new UnsupportedOperationException("SOCKS5 proxy is not supported."); } } }
private void checkFirstConnection() { if (!firstConnection) { SshConfiguration.getLogger().log( Level.WARN, "JSch does not fully support per connection configuration. This second client's configuration " + "may interfere with the first's."); firstConnection = true; } }
public final void onOpen() throws SshException { if (!Util.nullOrTrimmedBlank(configuration.getX11Host())) { boolean singleConnection = Boolean.parseBoolean(configuration.getProperties().getProperty( GanymedSshProvider.CFG_SINGLE_X11_CONNECTION, "false")); try { session.requestX11Forwarding(configuration.getX11Host(), configuration.getX11Port(), configuration.getX11Cookie(), singleConnection); } catch (IOException e) { throw new SshException(SshException.IO_ERROR, e); } } onChannelOpen(); }
public void doSupportsConfiguration(SshConfiguration configuration) { try { Class.forName("ch.ethz.ssh2.Connection", false, getClass().getClassLoader()); } catch (ClassNotFoundException cnfe) { SshConfiguration.getLogger().log(Level.INFO, "Could not find Ganymed class ch.ethz.ssh2.Connection."); throw new UnsupportedOperationException("Ganymed is not on the CLASSPATH"); } if (configuration != null && configuration.getProtocolVersion() == SshConfiguration.SSH1_ONLY) { SshConfiguration.getLogger().log(Level.INFO, "Ganymed does not support SSH1, not usable."); throw new UnsupportedOperationException("SSH1 is not supported."); } }
private void checkForBanner() { try { Field amF = connection.getClass().getDeclaredField("am"); amF.setAccessible(true); AuthenticationManager am = (AuthenticationManager) amF .get(connection); Field bannerF = am.getClass().getDeclaredField("banner"); bannerF.setAccessible(true); String banner = (String) bannerF.get(am); if (banner != null && !banner.equals("") && getConfiguration().getBannerHandler() != null) { getConfiguration().getBannerHandler().banner(banner); } } catch (Exception e) { SshConfiguration.getLogger().log(Level.ERROR, "Failed to access banner", e); } }
private void configureAlgorithms(SshConfiguration configuration) { JSch.setConfig("random", JschRandom.class.getName()); String preferredClientToServerCipher = configuration.getPreferredClientToServerCipher(); if (preferredClientToServerCipher != null) { checkConfig(preferredClientToServerCipher, "cipher", "cipher.c2s"); String preferredServerToClientCipher = configuration.getPreferredServerToClientCipher(); if (preferredServerToClientCipher != null) { checkConfig(preferredServerToClientCipher, "cipher", "cipher.s2c"); String preferredClientToServerMAC = configuration.getPreferredClientToServerMAC(); if (preferredClientToServerMAC != null) { checkConfig(preferredClientToServerMAC, "MAC", "mac.c2s"); String preferredServerToClientMAC = configuration.getPreferredServerToClientMAC(); if (preferredServerToClientMAC != null) { checkConfig(preferredServerToClientMAC, "MAC", "mac.s2c"); String preferredClientToServerCompression = configuration.getPreferredClientToServerCompression(); if (preferredClientToServerCompression != null) { checkConfig(preferredClientToServerCompression, "compression", "compression.c2s"); String preferredServerToClientCompression = configuration.getPreferredServerToClientCompression(); if (preferredServerToClientCompression != null) { checkConfig(preferredServerToClientCompression, "compression", "compression.s2c"); String preferredKeyExchange = configuration.getPreferredKeyExchange(); if (preferredKeyExchange != null) { checkConfig(preferredKeyExchange, "key exchange", "kex");
private void configureAlgorithms(SshConfiguration configuration) { String preferredClientToServerCipher = configuration .getPreferredClientToServerCipher(); if (preferredClientToServerCipher != null) { connection .getPreferredServerToClientCipher(); if (preferredServerToClientCipher != null) { connection .getPreferredClientToServerMAC(); if (preferredClientToServerMAC != null) { connection .getPreferredServerToClientMAC(); if (preferredServerToClientMAC != null) { connection .setServer2ClientMACs(checkMAC(preferredServerToClientMAC)); String preferredKeyExchange = configuration.getPreferredKeyExchange(); if (preferredKeyExchange != null) { try { cwl.kexAlgorithms = (String[]) l.toArray(new String[l.size()]); } catch (Exception e) { SshConfiguration.getLogger().log(Level.ERROR, "Could not set key exchange.", e); String preferredPublicKey = configuration.getPreferredPublicKey(); if (preferredPublicKey != null) {
.ssh_options_set(libSshSession, SshLibrary.ssh_options_e.SSH_OPTIONS_PORT, new IntByReference(port).getPointer()); if (configuration.getPreferredClientToServerCompression() != null) { library.ssh_options_set(libSshSession, SshLibrary.ssh_options_e.SSH_OPTIONS_COMPRESSION_C_S, stringPointer(configuration.getPreferredClientToServerCompression())); if (configuration.getPreferredServerToClientCompression() != null) { library.ssh_options_set(libSshSession, SshLibrary.ssh_options_e.SSH_OPTIONS_COMPRESSION_S_C, stringPointer(configuration.getPreferredServerToClientCompression())); new IntByReference(configuration.getProtocolVersion() == SshConfiguration.SSH2_ONLY ? 0 : 1).getPointer()); library.ssh_options_set(libSshSession, SshLibrary.ssh_options_e.SSH_OPTIONS_SSH2, new IntByReference(configuration.getProtocolVersion() == SshConfiguration.SSH1_ONLY ? 0 : 1).getPointer()); SshHostKeyValidator hostKeyValidator = configuration.getHostKeyValidator(); if (hostKeyValidator != null) { PointerByReference ref = new PointerByReference(new Memory(16));
if (configuration.getProtocolVersion() == SshConfiguration.SSH1_ONLY) { throw new SshException(SshException.UNSUPPORTED_PROTOCOL_VERSION, "Ganymed only supports SSH2, yet SSH1 only was request."); SshProxyServerDetails proxyServer = configuration.getProxyServer(); if (proxyServer != null) { connection = new Connection(hostname, port, new HTTPProxyData( .getHostKeyValidator())); connected = true; } catch (IOException e) {
client.setHostKeyRepository(new HostKeyRepositoryBridge(client.getHostKeyRepository())); session = client.getSession(username, hostname, port); final SocketFactory socketFactory = getConfiguration().getSocketFactory(); if (socketFactory != null) { session.setSocketFactory(new com.jcraft.jsch.SocketFactory() { if (configuration.getX11Host() != null) { session.setX11Host(configuration.getX11Host()); if (configuration.getX11Port() > -1) { session.setX11Port(configuration.getX11Port()); if (configuration.getX11Cookie() != null) { String hexString = Util.formatAsHexString(configuration.getX11Cookie()); session.setX11Cookie(hexString); SshProxyServerDetails proxyServer = configuration.getProxyServer(); if (proxyServer != null) { if (proxyServer.getType().equals(SshProxyServerDetails.Type.HTTP)) {
public void doSupportsConfiguration(SshConfiguration configuration) { try { Class.forName("com.jcraft.jsch.JSch", false, getClass().getClassLoader()); } catch (ClassNotFoundException cnfe) { throw new UnsupportedOperationException("JSch is not on the CLASSPATH"); } if (configuration != null && configuration.getProtocolVersion() == SshConfiguration.SSH1_ONLY) { throw new UnsupportedOperationException("SSH1 is not supported."); } }
/** * Get a value from a configuration object, returning a default value if it * does not exist. This method will also look for the same property in the * current system properties. * * @param configuration configuration object * @param name name of configuration * @param defaultValue * @return value */ public static String getConfigurationValue(SshConfiguration configuration, String name, String defaultValue) { String val = configuration == null ? null : configuration.getProperties().getProperty(name); if (val == null) { val = System.getProperty(SshConfiguration.CFG_KNOWN_HOSTS_PATH); } if (val == null) { val = defaultValue; } return val; }
public int getRemoteProtocolVersion() { if (!isConnected()) { throw new IllegalStateException("Not connected"); } // TODO get the actual remote version return getConfiguration().getProtocolVersion() == SshConfiguration.SSH1_OR_SSH2 ? 2 : getConfiguration() .getProtocolVersion(); }
/** * Get a value from a configuration object, returning a default value if it * does not exist. This method will also look for the same property in the * current system properties. * * @param configuration configuration object * @param name name of configuration * @param defaultValue * @return value */ public static String getConfigurationValue(SshConfiguration configuration, String name, String defaultValue) { String val = configuration == null ? null : configuration.getProperties().getProperty(name); if (val == null) { val = System.getProperty(SshConfiguration.CFG_KNOWN_HOSTS_PATH); } if (val == null) { val = defaultValue; } return val; }
private void checkDefaultConfiguration(SshConfiguration configuration) { if (configuration == null) { if (supportsDefaultConfiguration) { return; } else { throw new UnsupportedOperationException("Default configuration is not supported. You must supply a configuration."); } } configuration.providerHasCapabilities(this); check(configuration.getPreferredClientToServerCipher(), getSupportedCiphers(configuration.getProtocolVersion())); check(configuration.getPreferredServerToClientCipher(), getSupportedCiphers(configuration.getProtocolVersion())); check(configuration.getPreferredClientToServerMAC(), getSupportedMAC()); check(configuration.getPreferredServerToClientMAC(), getSupportedMAC()); check(configuration.getPreferredClientToServerCompression(), getSupportedCompression()); check(configuration.getPreferredServerToClientCompression(), getSupportedCompression()); if (configuration.getProxyServer() != null) { SshProxyServerDetails proxy = configuration.getProxyServer(); if (proxy.getType().equals(SshProxyServerDetails.Type.HTTP) && !getCapabilities().contains(Capability.HTTP_PROXY)) { throw new UnsupportedOperationException("HTTP proxy is not supported."); } if (proxy.getType().equals(SshProxyServerDetails.Type.SOCKS4) && !getCapabilities().contains(Capability.SOCKS4_PROXY)) { throw new UnsupportedOperationException("SOCKS4 proxy is not supported."); } if (proxy.getType().equals(SshProxyServerDetails.Type.SOCKS5) && !getCapabilities().contains(Capability.SOCKS5_PROXY)) { throw new UnsupportedOperationException("SOCKS5 proxy is not supported."); } } }
public SshHostKey[] getKeys() { List keys = new ArrayList(); // Ewwwww :( try { Field field = knownHosts.getClass().getDeclaredField("publicKeys"); field.setAccessible(true); LinkedList publickeys = (LinkedList) field.get(knownHosts); addKeys(keys, publickeys); } catch (Exception e) { SshConfiguration.getLogger().log(Level.ERROR, "Failed to get host keys.", e); } return (SshHostKey[]) keys.toArray(new SshHostKey[0]); }
static void dumpClientInfo(SshClient client) { System.out.println("Provider: " + client.getProvider().getClass().getName()); System.out.println("Client: " + client.getClass().getName()); System.out.println("Capabilities: " + client.getProvider().getCapabilities()); int protocolVersion = client.getConfiguration().getProtocolVersion(); System.out.println("Ciphers: " + client.getProvider().getSupportedCiphers(protocolVersion)); if (protocolVersion != SshConfiguration.SSH1_ONLY) { System.out.println("MAC: " + client.getProvider().getSupportedMAC()); System.out.println("Compression: " + client.getProvider().getSupportedCompression()); System.out.println("Key Exchange: " + client.getProvider().getSupportedKeyExchange()); System.out.println("Public Key: " + client.getProvider().getSupportedPublicKey()); } }
public final SshHostKey[] getKeysForHost(String host, String type) { // Look up first using the plain text hostname SshHostKey[] hk = doGetKeysForHost(host, type); if (hk == null) { // Now try a reverse look up if ("true".equals(configuration.getProperties().getProperty(SshConfiguration.CFG_KNOWN_HOSTS_REVERSE_DNS, "true"))) { try { InetAddress addr = InetAddress.getByName(host); hk = doGetKeysForHost(addr.getHostName(), type); if (hk == null) { hk = doGetKeysForHost(addr.getCanonicalHostName(), type); } if (hk == null) { hk = doGetKeysForHost(addr.getHostAddress(), type); } } catch (UnknownHostException uhe) { } } } return hk == null ? new SshHostKey[0] : hk; }
public void log(int level, String message) { net.sf.sshapi.Logger logger = SshConfiguration.getLogger(); Level sshapiLevel = Level.ERROR; switch (level) { case Logger.DEBUG: sshapiLevel = Level.DEBUG; break; case Logger.INFO: sshapiLevel = Level.INFO; break; case Logger.WARN: sshapiLevel = Level.WARN; break; default: sshapiLevel = Level.ERROR; break; } logger.log(sshapiLevel, message); }
public final SshHostKey[] getKeysForHost(String host, String type) { // Look up first using the plain text hostname SshHostKey[] hk = doGetKeysForHost(host, type); if (hk == null) { // Now try a reverse look up if ("true".equals(configuration.getProperties().getProperty(SshConfiguration.CFG_KNOWN_HOSTS_REVERSE_DNS, "true"))) { try { InetAddress addr = InetAddress.getByName(host); hk = doGetKeysForHost(addr.getHostName(), type); if (hk == null) { hk = doGetKeysForHost(addr.getCanonicalHostName(), type); } if (hk == null) { hk = doGetKeysForHost(addr.getHostAddress(), type); } } catch (UnknownHostException uhe) { } } } return hk == null ? new SshHostKey[0] : hk; }