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