annotate src/ch/ethz/ssh2/Connection.java @ 308:42b15aaa7ac7 ganymed

merge
author Carl Byington <carl@five-ten-sg.com>
date Wed, 30 Jul 2014 14:21:50 -0700
parents 071eccdff8ea
children cdb3c9215a9b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1 /*
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
3 * Please refer to the LICENSE.txt for licensing details.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
4 */
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
5
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
6 package ch.ethz.ssh2;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
7
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
8 import java.io.CharArrayWriter;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
9 import java.io.File;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
10 import java.io.FileReader;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
11 import java.io.IOException;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
12 import java.net.InetSocketAddress;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
13 import java.net.Socket;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
14 import java.net.SocketTimeoutException;
305
d2b303406d63 remove extra override annotations that generate eclipse compiler errors
Carl Byington <carl@five-ten-sg.com>
parents: 302
diff changeset
15 import java.security.KeyPair;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
16 import java.security.SecureRandom;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
17 import java.util.ArrayList;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
18 import java.util.List;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
19 import java.util.Set;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
20
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
21 import ch.ethz.ssh2.auth.AgentProxy;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
22 import ch.ethz.ssh2.auth.AuthenticationManager;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
23 import ch.ethz.ssh2.channel.ChannelManager;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
24 import ch.ethz.ssh2.compression.CompressionFactory;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
25 import ch.ethz.ssh2.crypto.CryptoWishList;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
26 import ch.ethz.ssh2.crypto.cipher.BlockCipherFactory;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
27 import ch.ethz.ssh2.crypto.digest.MAC;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
28 import ch.ethz.ssh2.packets.PacketIgnore;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
29 import ch.ethz.ssh2.transport.ClientTransportManager;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
30 import ch.ethz.ssh2.transport.HTTPProxyClientTransportManager;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
31 import ch.ethz.ssh2.transport.KexManager;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
32 import ch.ethz.ssh2.util.TimeoutService;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
33 import ch.ethz.ssh2.util.TimeoutService.TimeoutToken;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
34
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
35 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
36 * A <code>Connection</code> is used to establish an encrypted TCP/IP
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
37 * connection to a SSH-2 server.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
38 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
39 * Typically, one
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
40 * <ol>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
41 * <li>creates a {@link #Connection(String) Connection} object.</li>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
42 * <li>calls the {@link #connect() connect()} method.</li>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
43 * <li>calls some of the authentication methods (e.g., {@link #authenticateWithPublicKey(String, File, String) authenticateWithPublicKey()}).</li>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
44 * <li>calls one or several times the {@link #openSession() openSession()} method.</li>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
45 * <li>finally, one must close the connection and release resources with the {@link #close() close()} method.</li>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
46 * </ol>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
47 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
48 * @author Christian Plattner
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
49 * @version $Id: Connection.java 160 2014-05-01 14:30:26Z dkocher@sudo.ch $
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
50 */
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
51
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
52 public class Connection {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
53 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
54 * The identifier presented to the SSH-2 server. This is the same
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
55 * as the "softwareversion" defined in RFC 4253.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
56 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
57 * <b>NOTE: As per the RFC, the "softwareversion" string MUST consist of printable
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
58 * US-ASCII characters, with the exception of whitespace characters and the minus sign (-).</b>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
59 */
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
60 private String softwareversion
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
61 = String.format("Ganymed_%s", Version.getSpecification());
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
62
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
63 /* Will be used to generate all random data needed for the current connection.
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
64 * Note: SecureRandom.nextBytes() is thread safe.
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
65 */
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
66
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
67 private SecureRandom generator;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
68
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
69 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
70 * Unless you know what you are doing, you will never need this.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
71 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
72 * @return The list of supported cipher algorithms by this implementation.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
73 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
74
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
75 public static synchronized String[] getAvailableCiphers() {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
76 return BlockCipherFactory.getDefaultCipherList();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
77 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
78
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
79 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
80 * Unless you know what you are doing, you will never need this.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
81 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
82 * @return The list of supported MAC algorthims by this implementation.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
83 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
84
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
85 public static synchronized String[] getAvailableMACs() {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
86 return MAC.getMacList();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
87 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
88
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
89 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
90 * Unless you know what you are doing, you will never need this.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
91 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
92 * @return The list of supported server host key algorthims by this implementation.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
93 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
94
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
95 public static synchronized String[] getAvailableServerHostKeyAlgorithms() {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
96 return KexManager.getDefaultServerHostkeyAlgorithmList();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
97 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
98
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
99 private AuthenticationManager am;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
100
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
101 private boolean authenticated;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
102 private ChannelManager cm;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
103
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
104 private CryptoWishList cryptoWishList
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
105 = new CryptoWishList();
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
106
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
107 private DHGexParameters dhgexpara
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
108 = new DHGexParameters();
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
109
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
110 private final String hostname;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
111
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
112 private final int port;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
113
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
114 private ClientTransportManager tm;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
115
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
116 private boolean tcpNoDelay = false;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
117
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
118 private HTTPProxyData proxy;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
119
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
120 private List<ConnectionMonitor> connectionMonitors
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
121 = new ArrayList<ConnectionMonitor>();
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
122
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
123 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
124 * Prepares a fresh <code>Connection</code> object which can then be used
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
125 * to establish a connection to the specified SSH-2 server.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
126 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
127 * Same as {@link #Connection(String, int) Connection(hostname, 22)}.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
128 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
129 * @param hostname the hostname of the SSH-2 server.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
130 */
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
131 public Connection(String hostname) {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
132 this(hostname, 22);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
133 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
134
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
135 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
136 * Prepares a fresh <code>Connection</code> object which can then be used
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
137 * to establish a connection to the specified SSH-2 server.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
138 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
139 * @param hostname the host where we later want to connect to.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
140 * @param port port on the server, normally 22.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
141 */
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
142 public Connection(String hostname, int port) {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
143 this.hostname = hostname;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
144 this.port = port;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
145 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
146
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
147 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
148 * Prepares a fresh <code>Connection</code> object which can then be used
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
149 * to establish a connection to the specified SSH-2 server.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
150 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
151 * @param hostname the host where we later want to connect to.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
152 * @param port port on the server, normally 22.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
153 * @param softwareversion Allows you to set a custom "softwareversion" string as defined in RFC 4253.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
154 * <b>NOTE: As per the RFC, the "softwareversion" string MUST consist of printable
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
155 * US-ASCII characters, with the exception of whitespace characters and the minus sign (-).</b>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
156 */
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
157 public Connection(String hostname, int port, String softwareversion) {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
158 this.hostname = hostname;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
159 this.port = port;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
160 this.softwareversion = softwareversion;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
161 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
162
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
163 public Connection(String hostname, int port, final HTTPProxyData proxy) {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
164 this.hostname = hostname;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
165 this.port = port;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
166 this.proxy = proxy;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
167 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
168
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
169 public Connection(String hostname, int port, String softwareversion, final HTTPProxyData proxy) {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
170 this.hostname = hostname;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
171 this.port = port;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
172 this.softwareversion = softwareversion;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
173 this.proxy = proxy;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
174 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
175
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
176 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
177 * After a successful connect, one has to authenticate oneself. This method
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
178 * is based on DSA (it uses DSA to sign a challenge sent by the server).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
179 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
180 * If the authentication phase is complete, <code>true</code> will be
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
181 * returned. If the server does not accept the request (or if further
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
182 * authentication steps are needed), <code>false</code> is returned and
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
183 * one can retry either by using this or any other authentication method
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
184 * (use the <code>getRemainingAuthMethods</code> method to get a list of
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
185 * the remaining possible methods).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
186 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
187 * @param user A <code>String</code> holding the username.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
188 * @param pem A <code>String</code> containing the DSA private key of the
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
189 * user in OpenSSH key format (PEM, you can't miss the
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
190 * "-----BEGIN DSA PRIVATE KEY-----" tag). The string may contain
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
191 * linefeeds.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
192 * @param password If the PEM string is 3DES encrypted ("DES-EDE3-CBC"), then you
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
193 * must specify the password. Otherwise, this argument will be
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
194 * ignored and can be set to <code>null</code>.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
195 * @return whether the connection is now authenticated.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
196 * @throws IOException
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
197 * @deprecated You should use one of the {@link #authenticateWithPublicKey(String, File, String) authenticateWithPublicKey()}
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
198 * methods, this method is just a wrapper for it and will
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
199 * disappear in future builds.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
200 */
305
d2b303406d63 remove extra override annotations that generate eclipse compiler errors
Carl Byington <carl@five-ten-sg.com>
parents: 302
diff changeset
201 @Deprecated
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
202
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
203 public synchronized boolean authenticateWithDSA(String user, String pem, String password) throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
204 if (tm == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
205 throw new IllegalStateException("Connection is not established!");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
206 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
207
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
208 if (authenticated) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
209 throw new IllegalStateException("Connection is already authenticated!");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
210 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
211
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
212 if (am == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
213 am = new AuthenticationManager(tm);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
214 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
215
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
216 if (cm == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
217 cm = new ChannelManager(tm);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
218 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
219
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
220 if (user == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
221 throw new IllegalArgumentException("user argument is null");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
222 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
223
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
224 if (pem == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
225 throw new IllegalArgumentException("pem argument is null");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
226 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
227
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
228 authenticated = am.authenticatePublicKey(user, pem.toCharArray(), password, getOrCreateSecureRND());
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
229 return authenticated;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
230 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
231
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
232 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
233 * A wrapper that calls {@link #authenticateWithKeyboardInteractive(String, String[], InteractiveCallback)
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
234 * authenticateWithKeyboardInteractivewith} a <code>null</code> submethod list.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
235 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
236 * @param user A <code>String</code> holding the username.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
237 * @param cb An <code>InteractiveCallback</code> which will be used to
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
238 * determine the responses to the questions asked by the server.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
239 * @return whether the connection is now authenticated.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
240 * @throws IOException
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
241 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
242
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
243 public synchronized boolean authenticateWithKeyboardInteractive(String user, InteractiveCallback cb)
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
244 throws IOException {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
245 return authenticateWithKeyboardInteractive(user, null, cb);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
246 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
247
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
248 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
249 * After a successful connect, one has to authenticate oneself. This method
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
250 * is based on "keyboard-interactive", specified in
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
251 * draft-ietf-secsh-auth-kbdinteract-XX. Basically, you have to define a
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
252 * callback object which will be feeded with challenges generated by the
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
253 * server. Answers are then sent back to the server. It is possible that the
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
254 * callback will be called several times during the invocation of this
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
255 * method (e.g., if the server replies to the callback's answer(s) with
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
256 * another challenge...)
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
257 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
258 * If the authentication phase is complete, <code>true</code> will be
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
259 * returned. If the server does not accept the request (or if further
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
260 * authentication steps are needed), <code>false</code> is returned and
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
261 * one can retry either by using this or any other authentication method
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
262 * (use the <code>getRemainingAuthMethods</code> method to get a list of
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
263 * the remaining possible methods).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
264 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
265 * Note: some SSH servers advertise "keyboard-interactive", however, any
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
266 * interactive request will be denied (without having sent any challenge to
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
267 * the client).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
268 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
269 * @param user A <code>String</code> holding the username.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
270 * @param submethods An array of submethod names, see
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
271 * draft-ietf-secsh-auth-kbdinteract-XX. May be <code>null</code>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
272 * to indicate an empty list.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
273 * @param cb An <code>InteractiveCallback</code> which will be used to
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
274 * determine the responses to the questions asked by the server.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
275 * @return whether the connection is now authenticated.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
276 * @throws IOException
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
277 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
278
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
279 public synchronized boolean authenticateWithKeyboardInteractive(String user, String[] submethods,
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
280 InteractiveCallback cb) throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
281 if (cb == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
282 throw new IllegalArgumentException("Callback may not ne NULL!");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
283 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
284
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
285 if (tm == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
286 throw new IllegalStateException("Connection is not established!");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
287 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
288
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
289 if (authenticated) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
290 throw new IllegalStateException("Connection is already authenticated!");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
291 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
292
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
293 if (am == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
294 am = new AuthenticationManager(tm);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
295 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
296
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
297 if (cm == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
298 cm = new ChannelManager(tm);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
299 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
300
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
301 if (user == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
302 throw new IllegalArgumentException("user argument is null");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
303 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
304
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
305 authenticated = am.authenticateInteractive(user, submethods, cb);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
306 return authenticated;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
307 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
308
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
309 public synchronized boolean authenticateWithAgent(String user, AgentProxy proxy) throws IOException {
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
310 if (tm == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
311 throw new IllegalStateException("Connection is not established!");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
312 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
313
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
314 if (authenticated) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
315 throw new IllegalStateException("Connection is already authenticated!");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
316 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
317
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
318 if (am == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
319 am = new AuthenticationManager(tm);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
320 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
321
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
322 if (cm == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
323 cm = new ChannelManager(tm);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
324 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
325
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
326 if (user == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
327 throw new IllegalArgumentException("user argument is null");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
328 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
329
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
330 authenticated = am.authenticatePublicKey(user, proxy);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
331 return authenticated;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
332 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
333
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
334 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
335 * After a successful connect, one has to authenticate oneself. This method
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
336 * sends username and password to the server.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
337 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
338 * If the authentication phase is complete, <code>true</code> will be
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
339 * returned. If the server does not accept the request (or if further
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
340 * authentication steps are needed), <code>false</code> is returned and
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
341 * one can retry either by using this or any other authentication method
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
342 * (use the <code>getRemainingAuthMethods</code> method to get a list of
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
343 * the remaining possible methods).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
344 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
345 * Note: if this method fails, then please double-check that it is actually
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
346 * offered by the server (use {@link #getRemainingAuthMethods(String) getRemainingAuthMethods()}.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
347 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
348 * Often, password authentication is disabled, but users are not aware of it.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
349 * Many servers only offer "publickey" and "keyboard-interactive". However,
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
350 * even though "keyboard-interactive" *feels* like password authentication
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
351 * (e.g., when using the putty or openssh clients) it is *not* the same mechanism.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
352 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
353 * @param user
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
354 * @param password
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
355 * @return if the connection is now authenticated.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
356 * @throws IOException
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
357 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
358
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
359 public synchronized boolean authenticateWithPassword(String user, String password) throws IOException {
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
360 if (tm == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
361 throw new IllegalStateException("Connection is not established!");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
362 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
363
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
364 if (authenticated) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
365 throw new IllegalStateException("Connection is already authenticated!");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
366 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
367
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
368 if (am == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
369 am = new AuthenticationManager(tm);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
370 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
371
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
372 if (cm == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
373 cm = new ChannelManager(tm);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
374 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
375
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
376 if (user == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
377 throw new IllegalArgumentException("user argument is null");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
378 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
379
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
380 if (password == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
381 throw new IllegalArgumentException("password argument is null");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
382 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
383
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
384 authenticated = am.authenticatePassword(user, password);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
385 return authenticated;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
386 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
387
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
388 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
389 * After a successful connect, one has to authenticate oneself.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
390 * This method can be used to explicitly use the special "none"
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
391 * authentication method (where only a username has to be specified).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
392 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
393 * Note 1: The "none" method may always be tried by clients, however as by
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
394 * the specs, the server will not explicitly announce it. In other words,
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
395 * the "none" token will never show up in the list returned by
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
396 * {@link #getRemainingAuthMethods(String)}.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
397 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
398 * Note 2: no matter which one of the authenticateWithXXX() methods
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
399 * you call, the library will always issue exactly one initial "none"
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
400 * authentication request to retrieve the initially allowed list of
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
401 * authentication methods by the server. Please read RFC 4252 for the
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
402 * details.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
403 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
404 * If the authentication phase is complete, <code>true</code> will be
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
405 * returned. If further authentication steps are needed, <code>false</code>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
406 * is returned and one can retry by any other authentication method
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
407 * (use the <code>getRemainingAuthMethods</code> method to get a list of
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
408 * the remaining possible methods).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
409 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
410 * @param user
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
411 * @return if the connection is now authenticated.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
412 * @throws IOException
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
413 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
414
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
415 public synchronized boolean authenticateWithNone(String user) throws IOException {
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
416 if (tm == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
417 throw new IllegalStateException("Connection is not established!");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
418 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
419
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
420 if (authenticated) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
421 throw new IllegalStateException("Connection is already authenticated!");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
422 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
423
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
424 if (am == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
425 am = new AuthenticationManager(tm);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
426 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
427
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
428 if (cm == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
429 cm = new ChannelManager(tm);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
430 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
431
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
432 if (user == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
433 throw new IllegalArgumentException("user argument is null");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
434 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
435
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
436 /* Trigger the sending of the PacketUserauthRequestNone packet */
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
437 /* (if not already done) */
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
438 authenticated = am.authenticateNone(user);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
439 return authenticated;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
440 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
441
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
442 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
443 * After a successful connect, one has to authenticate oneself.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
444 * The authentication method "publickey" works by signing a challenge
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
445 * sent by the server. The signature is either DSA or RSA based - it
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
446 * just depends on the type of private key you specify, either a DSA
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
447 * or RSA private key in PEM format. And yes, this is may seem to be a
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
448 * little confusing, the method is called "publickey" in the SSH-2 protocol
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
449 * specification, however since we need to generate a signature, you
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
450 * actually have to supply a private key =).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
451 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
452 * The private key contained in the PEM file may also be encrypted ("Proc-Type: 4,ENCRYPTED").
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
453 * The library supports DES-CBC and DES-EDE3-CBC encryption, as well
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
454 * as the more exotic PEM encrpytions AES-128-CBC, AES-192-CBC and AES-256-CBC.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
455 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
456 * If the authentication phase is complete, <code>true</code> will be
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
457 * returned. If the server does not accept the request (or if further
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
458 * authentication steps are needed), <code>false</code> is returned and
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
459 * one can retry either by using this or any other authentication method
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
460 * (use the <code>getRemainingAuthMethods</code> method to get a list of
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
461 * the remaining possible methods).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
462 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
463 * NOTE PUTTY USERS: Event though your key file may start with "-----BEGIN..."
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
464 * it is not in the expected format. You have to convert it to the OpenSSH
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
465 * key format by using the "puttygen" tool (can be downloaded from the Putty
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
466 * website). Simply load your key and then use the "Conversions/Export OpenSSH key"
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
467 * functionality to get a proper PEM file.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
468 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
469 * @param user A <code>String</code> holding the username.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
470 * @param pemPrivateKey A <code>char[]</code> containing a DSA or RSA private key of the
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
471 * user in OpenSSH key format (PEM, you can't miss the
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
472 * "-----BEGIN DSA PRIVATE KEY-----" or "-----BEGIN RSA PRIVATE KEY-----"
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
473 * tag). The char array may contain linebreaks/linefeeds.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
474 * @param password If the PEM structure is encrypted ("Proc-Type: 4,ENCRYPTED") then
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
475 * you must specify a password. Otherwise, this argument will be ignored
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
476 * and can be set to <code>null</code>.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
477 * @return whether the connection is now authenticated.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
478 * @throws IOException
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
479 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
480
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
481 public synchronized boolean authenticateWithPublicKey(String user, char[] pemPrivateKey, String password)
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
482 throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
483 if (tm == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
484 throw new IllegalStateException("Connection is not established!");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
485 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
486
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
487 if (authenticated) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
488 throw new IllegalStateException("Connection is already authenticated!");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
489 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
490
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
491 if (am == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
492 am = new AuthenticationManager(tm);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
493 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
494
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
495 if (cm == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
496 cm = new ChannelManager(tm);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
497 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
498
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
499 if (user == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
500 throw new IllegalArgumentException("user argument is null");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
501 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
502
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
503 if (pemPrivateKey == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
504 throw new IllegalArgumentException("pemPrivateKey argument is null");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
505 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
506
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
507 authenticated = am.authenticatePublicKey(user, pemPrivateKey, password, getOrCreateSecureRND());
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
508 return authenticated;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
509 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
510
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
511 /**
285
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
512 * After a successful connect, one has to authenticate oneself. The
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
513 * authentication method "publickey" works by signing a challenge sent by
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
514 * the server. The signature is either DSA or RSA based - it just depends on
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
515 * the type of private key you specify, either a DSA or RSA private key in
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
516 * PEM format. And yes, this is may seem to be a little confusing, the
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
517 * method is called "publickey" in the SSH-2 protocol specification, however
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
518 * since we need to generate a signature, you actually have to supply a
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
519 * private key =).
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
520 * <p>
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
521 * If the authentication phase is complete, <code>true</code> will be
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
522 * returned. If the server does not accept the request (or if further
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
523 * authentication steps are needed), <code>false</code> is returned and
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
524 * one can retry either by using this or any other authentication method
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
525 * (use the <code>getRemainingAuthMethods</code> method to get a list of
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
526 * the remaining possible methods).
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
527 *
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
528 * @param user
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
529 * A <code>String</code> holding the username.
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
530 * @param pair
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
531 * A <code>RSAPrivateKey</code> or <code>DSAPrivateKey</code>
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
532 * containing a DSA or RSA private key of
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
533 * the user in Trilead object format.
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
534 *
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
535 * @return whether the connection is now authenticated.
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
536 * @throws IOException
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
537 */
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
538
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
539 public synchronized boolean authenticateWithPublicKey(String user, KeyPair pair)
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
540 throws IOException {
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
541 if (tm == null)
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
542 throw new IllegalStateException("Connection is not established!");
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
543
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
544 if (authenticated)
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
545 throw new IllegalStateException("Connection is already authenticated!");
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
546
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
547 if (am == null)
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
548 am = new AuthenticationManager(tm);
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
549
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
550 if (cm == null)
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
551 cm = new ChannelManager(tm);
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
552
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
553 if (user == null)
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
554 throw new IllegalArgumentException("user argument is null");
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
555
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
556 if (pair == null)
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
557 throw new IllegalArgumentException("Key pair argument is null");
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
558
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
559 authenticated = am.authenticatePublicKey(user, pair, getOrCreateSecureRND());
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
560 return authenticated;
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
561 }
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
562
486df527ddc5 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
563 /**
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
564 * A convenience wrapper function which reads in a private key (PEM format, either DSA or RSA)
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
565 * and then calls <code>authenticateWithPublicKey(String, char[], String)</code>.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
566 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
567 * NOTE PUTTY USERS: Event though your key file may start with "-----BEGIN..."
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
568 * it is not in the expected format. You have to convert it to the OpenSSH
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
569 * key format by using the "puttygen" tool (can be downloaded from the Putty
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
570 * website). Simply load your key and then use the "Conversions/Export OpenSSH key"
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
571 * functionality to get a proper PEM file.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
572 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
573 * @param user A <code>String</code> holding the username.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
574 * @param pemFile A <code>File</code> object pointing to a file containing a DSA or RSA
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
575 * private key of the user in OpenSSH key format (PEM, you can't miss the
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
576 * "-----BEGIN DSA PRIVATE KEY-----" or "-----BEGIN RSA PRIVATE KEY-----"
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
577 * tag).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
578 * @param password If the PEM file is encrypted then you must specify the password.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
579 * Otherwise, this argument will be ignored and can be set to <code>null</code>.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
580 * @return whether the connection is now authenticated.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
581 * @throws IOException
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
582 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
583
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
584 public synchronized boolean authenticateWithPublicKey(String user, File pemFile, String password)
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
585 throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
586 if (pemFile == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
587 throw new IllegalArgumentException("pemFile argument is null");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
588 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
589
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
590 char[] buff = new char[256];
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
591 CharArrayWriter cw = new CharArrayWriter();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
592 FileReader fr = new FileReader(pemFile);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
593
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
594 while (true) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
595 int len = fr.read(buff);
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
596
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
597 if (len < 0) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
598 break;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
599 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
600
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
601 cw.write(buff, 0, len);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
602 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
603
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
604 fr.close();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
605 return authenticateWithPublicKey(user, cw.toCharArray(), password);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
606 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
607
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
608 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
609 * Add a {@link ConnectionMonitor} to this connection. Can be invoked at any time,
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
610 * but it is best to add connection monitors before invoking
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
611 * <code>connect()</code> to avoid glitches (e.g., you add a connection monitor after
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
612 * a successful connect(), but the connection has died in the mean time. Then,
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
613 * your connection monitor won't be notified.)
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
614 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
615 * You can add as many monitors as you like. If a monitor has already been added, then
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
616 * this method does nothing.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
617 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
618 * @param cmon An object implementing the {@link ConnectionMonitor} interface.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
619 * @see ConnectionMonitor
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
620 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
621
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
622 public synchronized void addConnectionMonitor(ConnectionMonitor cmon) {
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
623 if (!connectionMonitors.contains(cmon)) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
624 connectionMonitors.add(cmon);
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
625
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
626 if (tm != null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
627 tm.setConnectionMonitors(connectionMonitors);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
628 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
629 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
630 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
631
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
632 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
633 * Remove a {@link ConnectionMonitor} from this connection.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
634 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
635 * @param cmon
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
636 * @return whether the monitor could be removed
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
637 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
638
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
639 public synchronized boolean removeConnectionMonitor(ConnectionMonitor cmon) {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
640 boolean existed = connectionMonitors.remove(cmon);
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
641
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
642 if (tm != null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
643 tm.setConnectionMonitors(connectionMonitors);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
644 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
645
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
646 return existed;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
647 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
648
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
649 /**
287
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 285
diff changeset
650 * Controls whether compression is used on the link or not.
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 285
diff changeset
651 * <p>
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 285
diff changeset
652 * Note: This can only be called before connect()
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 285
diff changeset
653 * @param enabled whether to enable compression
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 285
diff changeset
654 * @throws IOException
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 285
diff changeset
655 */
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 285
diff changeset
656
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 285
diff changeset
657 public synchronized void setCompression(boolean enabled) throws IOException {
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 285
diff changeset
658 if (tm != null)
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 285
diff changeset
659 throw new IOException("Connection to " + hostname + " is already in connected state!");
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 285
diff changeset
660
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 285
diff changeset
661 if (enabled) enableCompression();
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 285
diff changeset
662 else disableCompression();
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 285
diff changeset
663 }
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 285
diff changeset
664
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 285
diff changeset
665 /**
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
666 * Close the connection to the SSH-2 server. All assigned sessions will be
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
667 * closed, too. Can be called at any time. Don't forget to call this once
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
668 * you don't need a connection anymore - otherwise the receiver thread may
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
669 * run forever.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
670 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
671
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
672 public synchronized void close() {
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
673 if (cm != null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
674 cm.closeAllChannels();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
675 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
676
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
677 if (tm != null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
678 tm.close();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
679 tm = null;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
680 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
681
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
682 am = null;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
683 cm = null;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
684 authenticated = false;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
685 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
686
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
687 public synchronized void close(IOException t) {
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
688 if (cm != null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
689 cm.closeAllChannels();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
690 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
691
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
692 if (tm != null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
693 tm.close(t);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
694 tm = null;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
695 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
696
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
697 am = null;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
698 cm = null;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
699 authenticated = false;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
700 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
701
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
702 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
703 * Same as {@link #connect(ServerHostKeyVerifier, int, int) connect(null, 0, 0)}.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
704 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
705 * @return see comments for the {@link #connect(ServerHostKeyVerifier, int, int) connect(ServerHostKeyVerifier, int, int)} method.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
706 * @throws IOException
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
707 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
708
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
709 public synchronized ConnectionInfo connect() throws IOException {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
710 return connect(null, 0, 0);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
711 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
712
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
713 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
714 * Same as {@link #connect(ServerHostKeyVerifier, int, int) connect(verifier, 0, 0)}.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
715 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
716 * @return see comments for the {@link #connect(ServerHostKeyVerifier, int, int) connect(ServerHostKeyVerifier, int, int)} method.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
717 * @throws IOException
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
718 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
719
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
720 public synchronized ConnectionInfo connect(ServerHostKeyVerifier verifier) throws IOException {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
721 return connect(verifier, 0, 0);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
722 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
723
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
724 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
725 * Connect to the SSH-2 server and, as soon as the server has presented its
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
726 * host key, use the {@link ServerHostKeyVerifier#verifyServerHostKey(String,
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
727 * int, String, byte[]) ServerHostKeyVerifier.verifyServerHostKey()}
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
728 * method of the <code>verifier</code> to ask for permission to proceed.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
729 * If <code>verifier</code> is <code>null</code>, then any host key will be
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
730 * accepted - this is NOT recommended, since it makes man-in-the-middle attackes
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
731 * VERY easy (somebody could put a proxy SSH server between you and the real server).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
732 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
733 * Note: The verifier will be called before doing any crypto calculations
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
734 * (i.e., diffie-hellman). Therefore, if you don't like the presented host key then
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
735 * no CPU cycles are wasted (and the evil server has less information about us).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
736 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
737 * However, it is still possible that the server presented a fake host key: the server
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
738 * cheated (typically a sign for a man-in-the-middle attack) and is not able to generate
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
739 * a signature that matches its host key. Don't worry, the library will detect such
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
740 * a scenario later when checking the signature (the signature cannot be checked before
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
741 * having completed the diffie-hellman exchange).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
742 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
743 * Note 2: The {@link ServerHostKeyVerifier#verifyServerHostKey(String,
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
744 * int, String, byte[]) ServerHostKeyVerifier.verifyServerHostKey()} method
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
745 * will *NOT* be called from the current thread, the call is being made from a
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
746 * background thread (there is a background dispatcher thread for every
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
747 * established connection).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
748 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
749 * Note 3: This method will block as long as the key exchange of the underlying connection
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
750 * has not been completed (and you have not specified any timeouts).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
751 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
752 * Note 4: If you want to re-use a connection object that was successfully connected,
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
753 * then you must call the {@link #close()} method before invoking <code>connect()</code> again.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
754 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
755 * @param verifier An object that implements the
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
756 * {@link ServerHostKeyVerifier} interface. Pass <code>null</code>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
757 * to accept any server host key - NOT recommended.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
758 * @param connectTimeout Connect the underlying TCP socket to the server with the given timeout
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
759 * value (non-negative, in milliseconds). Zero means no timeout.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
760 * @param kexTimeout Timeout for complete connection establishment (non-negative,
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
761 * in milliseconds). Zero means no timeout. The timeout counts from the
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
762 * moment you invoke the connect() method and is cancelled as soon as the
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
763 * first key-exchange round has finished. It is possible that
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
764 * the timeout event will be fired during the invocation of the
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
765 * <code>verifier</code> callback, but it will only have an effect after
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
766 * the <code>verifier</code> returns.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
767 * @return A {@link ConnectionInfo} object containing the details of
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
768 * the established connection.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
769 * @throws IOException If any problem occurs, e.g., the server's host key is not
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
770 * accepted by the <code>verifier</code> or there is problem during
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
771 * the initial crypto setup (e.g., the signature sent by the server is wrong).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
772 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
773 * In case of a timeout (either connectTimeout or kexTimeout)
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
774 * a SocketTimeoutException is thrown.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
775 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
776 * An exception may also be thrown if the connection was already successfully
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
777 * connected (no matter if the connection broke in the mean time) and you invoke
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
778 * <code>connect()</code> again without having called {@link #close()} first.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
779 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
780 * If a HTTP proxy is being used and the proxy refuses the connection,
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
781 * then a {@link HTTPProxyException} may be thrown, which
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
782 * contains the details returned by the proxy. If the proxy is buggy and does
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
783 * not return a proper HTTP response, then a normal IOException is thrown instead.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
784 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
785
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
786 public synchronized ConnectionInfo connect(ServerHostKeyVerifier verifier, int connectTimeout, int kexTimeout)
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
787 throws IOException {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
788 final class TimeoutState {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
789 boolean isCancelled = false;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
790 boolean timeoutSocketClosed = false;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
791 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
792
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
793 if (tm != null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
794 throw new IllegalStateException(String.format("Connection to %s is already in connected state", hostname));
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
795 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
796
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
797 if (connectTimeout < 0) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
798 throw new IllegalArgumentException("connectTimeout must be non-negative!");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
799 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
800
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
801 if (kexTimeout < 0) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
802 throw new IllegalArgumentException("kexTimeout must be non-negative!");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
803 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
804
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
805 final TimeoutState state = new TimeoutState();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
806
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
807 if (null == proxy) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
808 tm = new ClientTransportManager(new Socket());
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
809 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
810 else {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
811 tm = new HTTPProxyClientTransportManager(new Socket(), proxy);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
812 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
813
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
814 tm.setSoTimeout(connectTimeout);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
815 tm.setTcpNoDelay(tcpNoDelay);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
816 tm.setConnectionMonitors(connectionMonitors);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
817
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
818 try {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
819 TimeoutToken token = null;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
820
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
821 if (kexTimeout > 0) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
822 final Runnable timeoutHandler = new Runnable() {
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
823 public void run() {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
824 synchronized (state) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
825 if (state.isCancelled) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
826 return;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
827 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
828
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
829 state.timeoutSocketClosed = true;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
830 tm.close(new SocketTimeoutException("The connect timeout expired"));
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
831 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
832 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
833 };
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
834 long timeoutHorizont = System.currentTimeMillis() + kexTimeout;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
835 token = TimeoutService.addTimeoutHandler(timeoutHorizont, timeoutHandler);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
836 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
837
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
838 tm.connect(hostname, port, softwareversion, cryptoWishList, verifier, dhgexpara, connectTimeout,
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
839 getOrCreateSecureRND());
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
840 /* Wait until first KEX has finished */
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
841 ConnectionInfo ci = tm.getConnectionInfo(1);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
842
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
843 /* Now try to cancel the timeout, if needed */
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
844
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
845 if (token != null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
846 TimeoutService.cancelTimeoutHandler(token);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
847
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
848 /* Were we too late? */
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
849
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
850 synchronized (state) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
851 if (state.timeoutSocketClosed) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
852 throw new IOException("This exception will be replaced by the one below =)");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
853 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
854
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
855 /* Just in case the "cancelTimeoutHandler" invocation came just a little bit
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
856 * too late but the handler did not enter the semaphore yet - we can
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
857 * still stop it.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
858 */
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
859 state.isCancelled = true;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
860 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
861 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
862
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
863 return ci;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
864 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
865 catch (SocketTimeoutException e) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
866 throw e;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
867 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
868 catch (HTTPProxyException e) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
869 throw e;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
870 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
871 catch (IOException e) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
872 // This will also invoke any registered connection monitors
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
873 close(e);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
874
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
875 synchronized (state) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
876 /* Show a clean exception, not something like "the socket is closed!?!" */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
877 if (state.timeoutSocketClosed) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
878 throw new SocketTimeoutException(String.format("The kexTimeout (%d ms) expired.", kexTimeout));
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
879 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
880 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
881
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
882 throw e;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
883 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
884 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
885
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
886 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
887 * Creates a new {@link LocalPortForwarder}.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
888 * A <code>LocalPortForwarder</code> forwards TCP/IP connections that arrive at a local
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
889 * port via the secure tunnel to another host (which may or may not be
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
890 * identical to the remote SSH-2 server).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
891 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
892 * This method must only be called after one has passed successfully the authentication step.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
893 * There is no limit on the number of concurrent forwardings.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
894 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
895 * @param local_port the local port the LocalPortForwarder shall bind to.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
896 * @param host_to_connect target address (IP or hostname)
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
897 * @param port_to_connect target port
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
898 * @return A {@link LocalPortForwarder} object.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
899 * @throws IOException
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
900 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
901
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
902 public synchronized LocalPortForwarder createLocalPortForwarder(int local_port, String host_to_connect,
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
903 int port_to_connect) throws IOException {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
904 this.checkConnection();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
905 return new LocalPortForwarder(cm, local_port, host_to_connect, port_to_connect);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
906 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
907
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
908 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
909 * Creates a new {@link LocalPortForwarder}.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
910 * A <code>LocalPortForwarder</code> forwards TCP/IP connections that arrive at a local
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
911 * port via the secure tunnel to another host (which may or may not be
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
912 * identical to the remote SSH-2 server).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
913 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
914 * This method must only be called after one has passed successfully the authentication step.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
915 * There is no limit on the number of concurrent forwardings.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
916 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
917 * @param addr specifies the InetSocketAddress where the local socket shall be bound to.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
918 * @param host_to_connect target address (IP or hostname)
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
919 * @param port_to_connect target port
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
920 * @return A {@link LocalPortForwarder} object.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
921 * @throws IOException
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
922 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
923
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
924 public synchronized LocalPortForwarder createLocalPortForwarder(InetSocketAddress addr, String host_to_connect,
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
925 int port_to_connect) throws IOException {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
926 this.checkConnection();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
927 return new LocalPortForwarder(cm, addr, host_to_connect, port_to_connect);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
928 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
929
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
930 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
931 * Creates a new {@link LocalStreamForwarder}.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
932 * A <code>LocalStreamForwarder</code> manages an Input/Outputstream pair
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
933 * that is being forwarded via the secure tunnel into a TCP/IP connection to another host
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
934 * (which may or may not be identical to the remote SSH-2 server).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
935 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
936 * @param host_to_connect
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
937 * @param port_to_connect
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
938 * @return A {@link LocalStreamForwarder} object.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
939 * @throws IOException
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
940 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
941
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
942 public synchronized LocalStreamForwarder createLocalStreamForwarder(String host_to_connect, int port_to_connect)
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
943 throws IOException {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
944 this.checkConnection();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
945 return new LocalStreamForwarder(cm, host_to_connect, port_to_connect);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
946 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
947
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
948 /**
290
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
949 * Creates a new {@link DynamicPortForwarder}. A
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
950 * <code>DynamicPortForwarder</code> forwards TCP/IP connections that arrive
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
951 * at a local port via the secure tunnel to another host that is chosen via
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
952 * the SOCKS protocol.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
953 * <p>
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
954 * This method must only be called after one has passed successfully the
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
955 * authentication step. There is no limit on the number of concurrent
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
956 * forwardings.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
957 *
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
958 * @param local_port
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
959 * @return A {@link DynamicPortForwarder} object.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
960 * @throws IOException
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
961 */
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
962
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
963 public synchronized DynamicPortForwarder createDynamicPortForwarder(int local_port) throws IOException {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
964 if (tm == null)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
965 throw new IllegalStateException("Cannot forward ports, you need to establish a connection first.");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
966
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
967 if (!authenticated)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
968 throw new IllegalStateException("Cannot forward ports, connection is not authenticated.");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
969
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
970 return new DynamicPortForwarder(cm, local_port);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
971 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
972
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
973 /**
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
974 * Creates a new {@link DynamicPortForwarder}. A
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
975 * <code>DynamicPortForwarder</code> forwards TCP/IP connections that arrive
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
976 * at a local port via the secure tunnel to another host that is chosen via
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
977 * the SOCKS protocol.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
978 * <p>
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
979 * This method must only be called after one has passed successfully the
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
980 * authentication step. There is no limit on the number of concurrent
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
981 * forwardings.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
982 *
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
983 * @param addr
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
984 * specifies the InetSocketAddress where the local socket shall
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
985 * be bound to.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
986 * @return A {@link DynamicPortForwarder} object.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
987 * @throws IOException
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
988 */
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
989
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
990 public synchronized DynamicPortForwarder createDynamicPortForwarder(InetSocketAddress addr) throws IOException {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
991 if (tm == null)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
992 throw new IllegalStateException("Cannot forward ports, you need to establish a connection first.");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
993
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
994 if (!authenticated)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
995 throw new IllegalStateException("Cannot forward ports, connection is not authenticated.");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
996
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
997 return new DynamicPortForwarder(cm, addr);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
998 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
999
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 289
diff changeset
1000 /**
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1001 * Create a very basic {@link SCPClient} that can be used to copy
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1002 * files from/to the SSH-2 server.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1003 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1004 * Works only after one has passed successfully the authentication step.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1005 * There is no limit on the number of concurrent SCP clients.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1006 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1007 * Note: This factory method will probably disappear in the future.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1008 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1009 * @return A {@link SCPClient} object.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1010 * @throws IOException
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1011 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1012
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1013 public synchronized SCPClient createSCPClient() throws IOException {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1014 this.checkConnection();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1015 return new SCPClient(this);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1016 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1017
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1018 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1019 * Force an asynchronous key re-exchange (the call does not block). The
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1020 * latest values set for MAC, Cipher and DH group exchange parameters will
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1021 * be used. If a key exchange is currently in progress, then this method has
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1022 * the only effect that the so far specified parameters will be used for the
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1023 * next (server driven) key exchange.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1024 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1025 * Note: This implementation will never start a key exchange (other than the initial one)
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1026 * unless you or the SSH-2 server ask for it.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1027 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1028 * @throws IOException In case of any failure behind the scenes.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1029 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1030
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1031 public synchronized void forceKeyExchange() throws IOException {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1032 this.checkConnection();
302
beaccc9df37b add ecdsa key support everywhere
Carl Byington <carl@five-ten-sg.com>
parents: 290
diff changeset
1033 tm.forceKeyExchange(cryptoWishList, dhgexpara, null, null, null);
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1034 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1035
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1036 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1037 * Returns the hostname that was passed to the constructor.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1038 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1039 * @return the hostname
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1040 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1041
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1042 public synchronized String getHostname() {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1043 return hostname;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1044 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1045
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1046 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1047 * Returns the port that was passed to the constructor.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1048 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1049 * @return the TCP port
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1050 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1051
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1052 public synchronized int getPort() {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1053 return port;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1054 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1055
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1056 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1057 * Returns a {@link ConnectionInfo} object containing the details of
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1058 * the connection. Can be called as soon as the connection has been
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1059 * established (successfully connected).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1060 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1061 * @return A {@link ConnectionInfo} object.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1062 * @throws IOException In case of any failure behind the scenes.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1063 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1064
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1065 public synchronized ConnectionInfo getConnectionInfo() throws IOException {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1066 this.checkConnection();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1067 return tm.getConnectionInfo(1);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1068 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1069
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1070 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1071 * After a successful connect, one has to authenticate oneself. This method
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1072 * can be used to tell which authentication methods are supported by the
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1073 * server at a certain stage of the authentication process (for the given
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1074 * username).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1075 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1076 * Note 1: the username will only be used if no authentication step was done
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1077 * so far (it will be used to ask the server for a list of possible
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1078 * authentication methods by sending the initial "none" request). Otherwise,
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1079 * this method ignores the user name and returns a cached method list
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1080 * (which is based on the information contained in the last negative server response).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1081 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1082 * Note 2: the server may return method names that are not supported by this
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1083 * implementation.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1084 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1085 * After a successful authentication, this method must not be called
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1086 * anymore.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1087 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1088 * @param user A <code>String</code> holding the username.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1089 * @return a (possibly emtpy) array holding authentication method names.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1090 * @throws IOException
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1091 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1092
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1093 public synchronized String[] getRemainingAuthMethods(String user) throws IOException {
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1094 if (user == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1095 throw new IllegalArgumentException("user argument may not be NULL!");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1096 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1097
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1098 if (tm == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1099 throw new IllegalStateException("Connection is not established!");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1100 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1101
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1102 if (authenticated) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1103 throw new IllegalStateException("Connection is already authenticated!");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1104 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1105
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1106 if (am == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1107 am = new AuthenticationManager(tm);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1108 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1109
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1110 if (cm == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1111 cm = new ChannelManager(tm);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1112 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1113
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1114 final Set<String> remainingMethods = am.getRemainingMethods(user);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1115 return remainingMethods.toArray(new String[remainingMethods.size()]);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1116 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1117
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1118 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1119 * Determines if the authentication phase is complete. Can be called at any
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1120 * time.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1121 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1122 * @return <code>true</code> if no further authentication steps are
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1123 * needed.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1124 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1125
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1126 public synchronized boolean isAuthenticationComplete() {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1127 return authenticated;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1128 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1129
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1130 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1131 * Returns true if there was at least one failed authentication request and
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1132 * the last failed authentication request was marked with "partial success"
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1133 * by the server. This is only needed in the rare case of SSH-2 server setups
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1134 * that cannot be satisfied with a single successful authentication request
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1135 * (i.e., multiple authentication steps are needed.)
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1136 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1137 * If you are interested in the details, then have a look at RFC4252.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1138 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1139 * @return if the there was a failed authentication step and the last one
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1140 * was marked as a "partial success".
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1141 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1142
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1143 public synchronized boolean isAuthenticationPartialSuccess() {
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1144 if (am == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1145 return false;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1146 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1147
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1148 return am.getPartialSuccess();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1149 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1150
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1151 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1152 * Checks if a specified authentication method is available. This method is
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1153 * actually just a wrapper for {@link #getRemainingAuthMethods(String)
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1154 * getRemainingAuthMethods()}.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1155 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1156 * @param user A <code>String</code> holding the username.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1157 * @param method An authentication method name (e.g., "publickey", "password",
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1158 * "keyboard-interactive") as specified by the SSH-2 standard.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1159 * @return if the specified authentication method is currently available.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1160 * @throws IOException
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1161 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1162
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1163 public synchronized boolean isAuthMethodAvailable(String user, String method) throws IOException {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1164 String methods[] = getRemainingAuthMethods(user);
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1165
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1166 for (final String m : methods) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1167 if (m.compareTo(method) == 0) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1168 return true;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1169 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1170 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1171
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1172 return false;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1173 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1174
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1175 private SecureRandom getOrCreateSecureRND() {
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1176 if (generator == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1177 generator = new SecureRandom();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1178 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1179
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1180 return generator;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1181 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1182
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1183 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1184 * Open a new {@link Session} on this connection. Works only after one has passed
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1185 * successfully the authentication step. There is no limit on the number of
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1186 * concurrent sessions.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1187 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1188 * @return A {@link Session} object.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1189 * @throws IOException
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1190 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1191
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1192 public synchronized Session openSession() throws IOException {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1193 this.checkConnection();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1194 return new Session(cm, getOrCreateSecureRND());
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1195 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1196
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1197 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1198 * Send an SSH_MSG_IGNORE packet. This method will generate a random data attribute
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1199 * (length between 0 (invlusive) and 16 (exclusive) bytes, contents are random bytes).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1200 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1201 * This method must only be called once the connection is established.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1202 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1203 * @throws IOException
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1204 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1205
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1206 public synchronized void sendIgnorePacket() throws IOException {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1207 SecureRandom rnd = getOrCreateSecureRND();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1208 byte[] data = new byte[rnd.nextInt(16)];
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1209 rnd.nextBytes(data);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1210 sendIgnorePacket(data);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1211 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1212
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1213 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1214 * Send an SSH_MSG_IGNORE packet with the given data attribute.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1215 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1216 * This method must only be called once the connection is established.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1217 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1218 * @throws IOException
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1219 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1220
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1221 public synchronized void sendIgnorePacket(byte[] data) throws IOException {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1222 this.checkConnection();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1223 PacketIgnore pi = new PacketIgnore(data);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1224 tm.sendMessage(pi.getPayload());
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1225 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1226
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1227 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1228 * Controls whether compression is used on the link or not.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1229 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1230
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1231 public synchronized void setCompression(String[] algorithms) {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1232 CompressionFactory.checkCompressorList(algorithms);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1233 cryptoWishList.c2s_comp_algos = algorithms;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1234 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1235
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1236 public synchronized void enableCompression() {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1237 cryptoWishList.c2s_comp_algos = CompressionFactory.getDefaultCompressorList();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1238 cryptoWishList.s2c_comp_algos = CompressionFactory.getDefaultCompressorList();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1239 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1240
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1241 public synchronized void disableCompression() {
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1242 cryptoWishList.c2s_comp_algos = new String[] {"none"};
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1243 cryptoWishList.s2c_comp_algos = new String[] {"none"};
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1244 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1245
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1246 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1247 * Unless you know what you are doing, you will never need this.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1248 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1249
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1250 public synchronized void setClient2ServerCiphers(final String[] ciphers) {
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1251 if ((ciphers == null) || (ciphers.length == 0)) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1252 throw new IllegalArgumentException();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1253 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1254
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1255 BlockCipherFactory.checkCipherList(ciphers);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1256 cryptoWishList.c2s_enc_algos = ciphers;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1257 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1258
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1259 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1260 * Unless you know what you are doing, you will never need this.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1261 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1262
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1263 public synchronized void setClient2ServerMACs(final String[] macs) {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1264 MAC.checkMacList(macs);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1265 cryptoWishList.c2s_mac_algos = macs;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1266 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1267
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1268 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1269 * Sets the parameters for the diffie-hellman group exchange. Unless you
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1270 * know what you are doing, you will never need this. Default values are
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1271 * defined in the {@link DHGexParameters} class.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1272 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1273 * @param dgp {@link DHGexParameters}, non null.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1274 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1275
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1276 public synchronized void setDHGexParameters(DHGexParameters dgp) {
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1277 if (dgp == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1278 throw new IllegalArgumentException();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1279 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1280
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1281 dhgexpara = dgp;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1282 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1283
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1284 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1285 * Unless you know what you are doing, you will never need this.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1286 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1287
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1288 public synchronized void setServer2ClientCiphers(final String[] ciphers) {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1289 BlockCipherFactory.checkCipherList(ciphers);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1290 cryptoWishList.s2c_enc_algos = ciphers;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1291 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1292
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1293 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1294 * Unless you know what you are doing, you will never need this.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1295 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1296
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1297 public synchronized void setServer2ClientMACs(final String[] macs) {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1298 MAC.checkMacList(macs);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1299 cryptoWishList.s2c_mac_algos = macs;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1300 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1301
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1302 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1303 * Define the set of allowed server host key algorithms to be used for
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1304 * the following key exchange operations.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1305 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1306 * Unless you know what you are doing, you will never need this.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1307 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1308 * @param algos An array of allowed server host key algorithms.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1309 * SSH-2 defines <code>ssh-dss</code> and <code>ssh-rsa</code>.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1310 * The entries of the array must be ordered after preference, i.e.,
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1311 * the entry at index 0 is the most preferred one. You must specify
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1312 * at least one entry.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1313 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1314
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1315 public synchronized void setServerHostKeyAlgorithms(final String[] algos) {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1316 KexManager.checkServerHostkeyAlgorithmsList(algos);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1317 cryptoWishList.serverHostKeyAlgorithms = algos;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1318 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1319
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1320 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1321 * Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm) on the underlying socket.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1322 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1323 * Can be called at any time. If the connection has not yet been established
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1324 * then the passed value will be stored and set after the socket has been set up.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1325 * The default value that will be used is <code>false</code>.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1326 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1327 * @param enable the argument passed to the <code>Socket.setTCPNoDelay()</code> method.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1328 * @throws IOException
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1329 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1330
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1331 public synchronized void setTCPNoDelay(boolean enable) throws IOException {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1332 tcpNoDelay = enable;
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1333
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1334 if (tm != null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1335 tm.setTcpNoDelay(enable);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1336 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1337 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1338
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1339 /**
289
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1340 * Used to tell the library that the connection shall be established through
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1341 * a proxy server. It only makes sense to call this method before calling
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1342 * the {@link #connect() connect()} method.
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1343 * <p>
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1344 * At the moment, only HTTP proxies are supported.
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1345 * <p>
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1346 * Note: This method can be called any number of times. The
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1347 * {@link #connect() connect()} method will use the value set in the last
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1348 * preceding invocation of this method.
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1349 *
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1350 * @see HTTPProxyData
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1351 *
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1352 * @param proxy
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1353 * Connection information about the proxy. If <code>null</code>,
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1354 * then no proxy will be used (non surprisingly, this is also the
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1355 * default).
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1356 */
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1357
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1358 public synchronized void setProxyData(HTTPProxyData proxy) {
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1359 this.proxy = proxy;
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1360 }
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1361
d2ee20d9dff1 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
1362 /**
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1363 * Request a remote port forwarding.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1364 * If successful, then forwarded connections will be redirected to the given target address.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1365 * You can cancle a requested remote port forwarding by calling
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1366 * {@link #cancelRemotePortForwarding(int) cancelRemotePortForwarding()}.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1367 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1368 * A call of this method will block until the peer either agreed or disagreed to your request-
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1369 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1370 * Note 1: this method typically fails if you
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1371 * <ul>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1372 * <li>pass a port number for which the used remote user has not enough permissions (i.e., port
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1373 * &lt; 1024)</li>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1374 * <li>or pass a port number that is already in use on the remote server</li>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1375 * <li>or if remote port forwarding is disabled on the server.</li>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1376 * </ul>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1377 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1378 * Note 2: (from the openssh man page): By default, the listening socket on the server will be
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1379 * bound to the loopback interface only. This may be overriden by specifying a bind address.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1380 * Specifying a remote bind address will only succeed if the server's <b>GatewayPorts</b> option
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1381 * is enabled (see sshd_config(5)).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1382 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1383 * @param bindAddress address to bind to on the server:
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1384 * <ul>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1385 * <li>"" means that connections are to be accepted on all protocol families
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1386 * supported by the SSH implementation</li>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1387 * <li>"0.0.0.0" means to listen on all IPv4 addresses</li>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1388 * <li>"::" means to listen on all IPv6 addresses</li>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1389 * <li>"localhost" means to listen on all protocol families supported by the SSH
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1390 * implementation on loopback addresses only, [RFC3330] and RFC3513]</li>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1391 * <li>"127.0.0.1" and "::1" indicate listening on the loopback interfaces for
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1392 * IPv4 and IPv6 respectively</li>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1393 * </ul>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1394 * @param bindPort port number to bind on the server (must be &gt; 0)
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1395 * @param targetAddress the target address (IP or hostname)
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1396 * @param targetPort the target port
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1397 * @throws IOException
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1398 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1399
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1400 public synchronized void requestRemotePortForwarding(String bindAddress, int bindPort, String targetAddress,
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1401 int targetPort) throws IOException {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1402 this.checkConnection();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1403
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1404 if ((bindAddress == null) || (targetAddress == null) || (bindPort <= 0) || (targetPort <= 0)) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1405 throw new IllegalArgumentException();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1406 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1407
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1408 cm.requestGlobalForward(bindAddress, bindPort, targetAddress, targetPort);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1409 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1410
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1411 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1412 * Cancel an earlier requested remote port forwarding.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1413 * Currently active forwardings will not be affected (e.g., disrupted).
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1414 * Note that further connection forwarding requests may be received until
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1415 * this method has returned.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1416 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1417 * @param bindPort the allocated port number on the server
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1418 * @throws IOException if the remote side refuses the cancel request or another low
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1419 * level error occurs (e.g., the underlying connection is closed)
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1420 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1421
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1422 public synchronized void cancelRemotePortForwarding(int bindPort) throws IOException {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1423 this.checkConnection();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1424 cm.requestCancelGlobalForward(bindPort);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1425 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1426
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1427 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1428 * Provide your own instance of SecureRandom. Can be used, e.g., if you
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1429 * want to seed the used SecureRandom generator manually.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1430 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1431 * The SecureRandom instance is used during key exchanges, public key authentication,
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1432 * x11 cookie generation and the like.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1433 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1434 * @param rnd a SecureRandom instance
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1435 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1436
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1437 public synchronized void setSecureRandom(SecureRandom rnd) {
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1438 if (rnd == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1439 throw new IllegalArgumentException();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1440 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1441
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1442 this.generator = rnd;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1443 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1444
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1445 private void checkConnection() throws IllegalStateException {
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1446 if (tm == null) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1447 throw new IllegalStateException("You need to establish a connection first.");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1448 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1449
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 305
diff changeset
1450 if (!authenticated) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1451 throw new IllegalStateException("The connection is not authenticated.");
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1452 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1453 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1454 }