annotate src/ch/ethz/ssh2/Session.java @ 312:1442be38051b ganymed

add ecdsa key support everywhere
author Carl Byington <carl@five-ten-sg.com>
date Wed, 30 Jul 2014 16:03:21 -0700
parents 071eccdff8ea
children
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 package ch.ethz.ssh2;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
6
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
7 import java.io.IOException;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
8 import java.io.InputStream;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
9 import java.io.OutputStream;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
10 import java.security.SecureRandom;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
11
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
12 import ch.ethz.ssh2.channel.Channel;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
13 import ch.ethz.ssh2.channel.ChannelManager;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
14 import ch.ethz.ssh2.channel.X11ServerData;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
15
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
16 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
17 * A <code>Session</code> is a remote execution of a program. "Program" means
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
18 * in this context either a shell, an application or a system command. The
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
19 * program may or may not have a tty. Only one single program can be started on
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
20 * a session. However, multiple sessions can be active simultaneously.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
21 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
22 * @author Christian Plattner
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
23 * @version $Id: Session.java 96 2014-04-08 15:14:37Z dkocher@sudo.ch $
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
24 */
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
25 public class Session {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
26 private ChannelManager cm;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
27 private Channel cn;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
28
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
29 private boolean flag_pty_requested = false;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
30 private boolean flag_x11_requested = false;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
31 private boolean flag_execution_started = false;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
32 private boolean flag_closed = false;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
33
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
34 private String x11FakeCookie = null;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
35
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
36 private final SecureRandom rnd;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
37
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
38 protected Session(ChannelManager cm, SecureRandom rnd) throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
39 this.cm = cm;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
40 this.cn = cm.openSessionChannel();
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
41 this.rnd = rnd;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
42 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
43
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
44 /**
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
45 * Basically just a wrapper for lazy people - identical to calling
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
46 * <code>requestPTY("dumb", 0, 0, 0, 0, null)</code>.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
47 *
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
48 * @throws IOException
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
49 */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
50 public void requestDumbPTY() throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
51 requestPTY("dumb", 0, 0, 0, 0, null);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
52 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
53
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
54 /**
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
55 * Basically just another wrapper for lazy people - identical to calling
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
56 * <code>requestPTY(term, 0, 0, 0, 0, null)</code>.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
57 *
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
58 * @throws IOException
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
59 */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
60 public void requestPTY(String term) throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
61 requestPTY(term, 0, 0, 0, 0, null);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
62 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
63
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
64 /**
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
65 * Allocate a pseudo-terminal for this session.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
66 * <p/>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
67 * This method may only be called before a program or shell is started in
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
68 * this session.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
69 * <p/>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
70 * Different aspects can be specified:
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
71 * <p/>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
72 * <ul>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
73 * <li>The TERM environment variable value (e.g., vt100)</li>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
74 * <li>The terminal's dimensions.</li>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
75 * <li>The encoded terminal modes.</li>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
76 * </ul>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
77 * Zero dimension parameters are ignored. The character/row dimensions
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
78 * override the pixel dimensions (when nonzero). Pixel dimensions refer to
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
79 * the drawable area of the window. The dimension parameters are only
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
80 * informational. The encoding of terminal modes (parameter
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
81 * <code>terminal_modes</code>) is described in RFC4254.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
82 *
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
83 * @param term The TERM environment variable value (e.g., vt100)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
84 * @param term_width_characters terminal width, characters (e.g., 80)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
85 * @param term_height_characters terminal height, rows (e.g., 24)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
86 * @param term_width_pixels terminal width, pixels (e.g., 640)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
87 * @param term_height_pixels terminal height, pixels (e.g., 480)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
88 * @param terminal_modes encoded terminal modes (may be <code>null</code>)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
89 * @throws IOException
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
90 */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
91 public void requestPTY(String term, int term_width_characters, int term_height_characters, int term_width_pixels,
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
92 int term_height_pixels, byte[] terminal_modes) throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
93 if (term == null)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
94 throw new IllegalArgumentException("TERM cannot be null.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
95
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
96 if ((terminal_modes != null) && (terminal_modes.length > 0)) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
97 if (terminal_modes[terminal_modes.length - 1] != 0)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
98 throw new IOException("Illegal terminal modes description, does not end in zero byte");
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
99 }
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
100 else
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
101 terminal_modes = new byte[] {0};
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
102
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
103 synchronized (this) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
104 /* The following is just a nicer error, we would catch it anyway later in the channel code */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
105 if (flag_closed)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
106 throw new IOException("This session is closed.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
107
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
108 if (flag_pty_requested)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
109 throw new IOException("A PTY was already requested.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
110
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
111 if (flag_execution_started)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
112 throw new IOException(
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
113 "Cannot request PTY at this stage anymore, a remote execution has already started.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
114
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
115 flag_pty_requested = true;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
116 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
117
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
118 cm.requestPTY(cn, term, term_width_characters, term_height_characters, term_width_pixels, term_height_pixels,
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
119 terminal_modes);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
120 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
121
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 * Tells the server that the size of the terminal has changed.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
124 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
125 * See {@link #requestPTY(String, int, int, int, int, byte[])} for more details about how parameters are interpreted.
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
126 *
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
127 * @param term_width_characters
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
128 * terminal width, characters (e.g., 80)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
129 * @param term_height_characters
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
130 * terminal height, rows (e.g., 24)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
131 * @param term_width_pixels
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
132 * terminal width, pixels (e.g., 640)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
133 * @param term_height_pixels
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
134 * terminal height, pixels (e.g., 480)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
135 * @throws IOException
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
136 */
290
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
137 public void resizePTY(int term_width_characters, int term_height_characters, int term_width_pixels, int term_height_pixels) throws IOException {
291
9c202256d102 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 290
diff changeset
138 requestWindowChange(term_width_characters, term_height_characters, term_width_pixels, term_height_pixels);
290
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
139 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
140
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
141 public void requestWindowChange(int term_width_characters, int term_height_characters, int term_width_pixels,
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
142 int term_height_pixels) throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
143 synchronized (this) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
144 /* The following is just a nicer error, we would catch it anyway later in the channel code */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
145 if (flag_closed)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
146 throw new IOException("This session is closed.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
147
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
148 if (!flag_pty_requested)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
149 throw new IOException("A PTY was not requested.");
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
150 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
151
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
152 cm.requestWindowChange(cn, term_width_characters, term_height_characters, term_width_pixels, term_height_pixels);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
153 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
154
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
155 /**
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
156 * Request X11 forwarding for the current session.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
157 * <p/>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
158 * You have to supply the name and port of your X-server.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
159 * <p/>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
160 * This method may only be called before a program or shell is started in
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
161 * this session.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
162 *
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
163 * @param hostname the hostname of the real (target) X11 server (e.g., 127.0.0.1)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
164 * @param port the port of the real (target) X11 server (e.g., 6010)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
165 * @param cookie if non-null, then present this cookie to the real X11 server
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
166 * @param singleConnection if true, then the server is instructed to only forward one single
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
167 * connection, no more connections shall be forwarded after first, or after the session
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
168 * channel has been closed
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
169 * @throws IOException
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
170 */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
171 public void requestX11Forwarding(String hostname, int port, byte[] cookie, boolean singleConnection)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
172 throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
173 if (hostname == null)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
174 throw new IllegalArgumentException("hostname argument may not be null");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
175
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
176 synchronized (this) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
177 /* The following is just a nicer error, we would catch it anyway later in the channel code */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
178 if (flag_closed)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
179 throw new IOException("This session is closed.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
180
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
181 if (flag_x11_requested)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
182 throw new IOException("X11 forwarding was already requested.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
183
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
184 if (flag_execution_started)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
185 throw new IOException(
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
186 "Cannot request X11 forwarding at this stage anymore, a remote execution has already started.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
187
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
188 flag_x11_requested = true;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
189 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
190
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
191 /* X11ServerData - used to store data about the target X11 server */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
192 X11ServerData x11data = new X11ServerData();
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
193 x11data.hostname = hostname;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
194 x11data.port = port;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
195 x11data.x11_magic_cookie = cookie; /* if non-null, then present this cookie to the real X11 server */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
196 /* Generate fake cookie - this one is used between remote clients and the ganymed proxy */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
197 byte[] fakeCookie = new byte[16];
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
198 String hexEncodedFakeCookie;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
199
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
200 /* Make sure that this fake cookie is unique for this connection */
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
201
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
202 while (true) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
203 rnd.nextBytes(fakeCookie);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
204 /* Generate also hex representation of fake cookie */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
205 StringBuilder tmp = new StringBuilder(32);
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
206
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
207 for (int i = 0; i < fakeCookie.length; i++) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
208 String digit2 = Integer.toHexString(fakeCookie[i] & 0xff);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
209 tmp.append((digit2.length() == 2) ? digit2 : "0" + digit2);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
210 }
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
211
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
212 hexEncodedFakeCookie = tmp.toString();
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
213
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
214 /* Well, yes, chances are low, but we want to be on the safe side */
273
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: 291
diff changeset
216 if (cm.checkX11Cookie(hexEncodedFakeCookie) == null)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
217 break;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
218 }
273
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: 291
diff changeset
220 /* Ask for X11 forwarding */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
221 cm.requestX11(cn, singleConnection, "MIT-MAGIC-COOKIE-1", hexEncodedFakeCookie, 0);
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
222
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
223 /* OK, that went fine, get ready to accept X11 connections... */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
224 /* ... but only if the user has not called close() in the meantime =) */
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
225
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
226 synchronized (this) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
227 if (flag_closed == false) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
228 this.x11FakeCookie = hexEncodedFakeCookie;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
229 cm.registerX11Cookie(hexEncodedFakeCookie, x11data);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
230 }
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
231 }
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
232
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
233 /* Now it is safe to start remote X11 programs */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
234 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
235
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
236 /**
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
237 * Execute a command on the remote machine.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
238 *
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
239 * @param cmd The command to execute on the remote host.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
240 * @throws IOException
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
241 */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
242 public void execCommand(String cmd) throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
243 this.execCommand(cmd, null);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
244 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
245
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
246 /**
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
247 * Execute a command on the remote machine.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
248 *
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
249 * @param cmd The command to execute on the remote host.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
250 * @param charsetName The charset used to convert between Java Unicode Strings and byte encodings
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
251 * @throws IOException
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
252 */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
253 public void execCommand(String cmd, String charsetName) throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
254 if (cmd == null)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
255 throw new IllegalArgumentException("cmd argument may not be null");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
256
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
257 synchronized (this) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
258 /* The following is just a nicer error, we would catch it anyway later in the channel code */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
259 if (flag_closed)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
260 throw new IOException("This session is closed.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
261
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
262 if (flag_execution_started)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
263 throw new IOException("A remote execution has already started.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
264
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
265 flag_execution_started = true;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
266 }
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
267
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
268 cm.requestExecCommand(cn, cmd, charsetName);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
269 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
270
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
271 /**
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
272 * Start a shell on the remote machine.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
273 *
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
274 * @throws IOException
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
275 */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
276 public void startShell() throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
277 synchronized (this) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
278 /* The following is just a nicer error, we would catch it anyway later in the channel code */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
279 if (flag_closed)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
280 throw new IOException("This session is closed.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
281
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
282 if (flag_execution_started)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
283 throw new IOException("A remote execution has already started.");
273
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: 291
diff changeset
285 flag_execution_started = true;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
286 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
287
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
288 cm.requestShell(cn);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
289 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
290
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
291 /**
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
292 * Start a subsystem on the remote machine.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
293 * Unless you know what you are doing, you will never need this.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
294 *
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
295 * @param name the name of the subsystem.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
296 * @throws IOException
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
297 */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
298 public void startSubSystem(String name) throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
299 if (name == null)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
300 throw new IllegalArgumentException("name argument may not be null");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
301
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
302 synchronized (this) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
303 /* The following is just a nicer error, we would catch it anyway later in the channel code */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
304 if (flag_closed)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
305 throw new IOException("This session is closed.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
306
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
307 if (flag_execution_started)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
308 throw new IOException("A remote execution has already started.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
309
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
310 flag_execution_started = true;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
311 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
312
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
313 cm.requestSubSystem(cn, name);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
314 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
315
287
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
316 /**
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
317 * Request authentication agent forwarding.
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
318 * @param agent object that implements the callbacks
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
319 *
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
320 * @throws IOException in case of any problem or when the session is closed
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
321 */
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
322
290
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
323 public synchronized void requestAuthAgentForwarding(AuthAgentCallback agent) throws IOException {
287
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
324 synchronized (this) {
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
325 /*
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
326 * The following is just a nicer error, we would catch it anyway
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
327 * later in the channel code
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
328 */
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
329 if (flag_closed)
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
330 throw new IOException("This session is closed.");
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
331 }
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
332
290
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 287
diff changeset
333 cm.requestChannelAgentForwarding(cn, agent);
287
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
334 }
db9b028016de start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
335
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
336 public int getState() {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
337 return cn.getState();
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
338 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
339
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
340 public InputStream getStdout() {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
341 return cn.getStdoutStream();
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
342 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
343
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
344 public InputStream getStderr() {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
345 return cn.getStderrStream();
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
346 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
347
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
348 public OutputStream getStdin() {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
349 return cn.getStdinStream();
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
350 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
351
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
352 /**
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
353 * This method blocks until there is more data available on either the
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
354 * stdout or stderr InputStream of this <code>Session</code>. Very useful
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
355 * if you do not want to use two parallel threads for reading from the two
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
356 * InputStreams. One can also specify a timeout. NOTE: do NOT call this
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
357 * method if you use concurrent threads that operate on either of the two
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
358 * InputStreams of this <code>Session</code> (otherwise this method may
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
359 * block, even though more data is available).
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
360 *
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
361 * @param timeout The (non-negative) timeout in <code>ms</code>. <code>0</code> means no
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
362 * timeout, the call may block forever.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
363 * @return <ul>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
364 * <li><code>0</code> if no more data will arrive.</li>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
365 * <li><code>1</code> if more data is available.</li>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
366 * <li><code>-1</code> if a timeout occurred.</li>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
367 * </ul>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
368 * @throws IOException
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
369 * @deprecated This method has been replaced with a much more powerful wait-for-condition
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
370 * interface and therefore acts only as a wrapper.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
371 */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
372 public int waitUntilDataAvailable(long timeout) throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
373 if (timeout < 0)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
374 throw new IllegalArgumentException("timeout must not be negative!");
273
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: 291
diff changeset
376 int conditions = cm.waitForCondition(cn, timeout, ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
377 | ChannelCondition.EOF);
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
378
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
379 if ((conditions & ChannelCondition.TIMEOUT) != 0)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
380 return -1;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
381
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
382 if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) != 0)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
383 return 1;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
384
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
385 /* Here we do not need to check separately for CLOSED, since CLOSED implies EOF */
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
386
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
387 if ((conditions & ChannelCondition.EOF) != 0)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
388 return 0;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
389
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
390 throw new IllegalStateException("Unexpected condition result (" + conditions + ")");
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
391 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
392
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
393 /**
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
394 * This method blocks until certain conditions hold true on the underlying SSH-2 channel.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
395 * <p/>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
396 * This method returns as soon as one of the following happens:
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
397 * <ul>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
398 * <li>at least of the specified conditions (see {@link ChannelCondition}) holds true</li>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
399 * <li>timeout > 0 and a timeout occured (TIMEOUT will be set in result conditions)</a>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
400 * <li>the underlying channel was closed (CLOSED will be set in result conditions)</a>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
401 * </ul>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
402 * <p/>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
403 * In any case, the result value contains ALL current conditions, which may be more
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
404 * than the specified condition set (i.e., never use the "==" operator to test for conditions
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
405 * in the bitmask, see also comments in {@link ChannelCondition}).
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
406 * <p/>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
407 * Note: do NOT call this method if you want to wait for STDOUT_DATA or STDERR_DATA and
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
408 * there are concurrent threads (e.g., StreamGobblers) that operate on either of the two
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
409 * InputStreams of this <code>Session</code> (otherwise this method may
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
410 * block, even though more data is available in the StreamGobblers).
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
411 *
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
412 * @param condition_set a bitmask based on {@link ChannelCondition} values
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
413 * @param timeout non-negative timeout in ms, <code>0</code> means no timeout
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
414 * @return all bitmask specifying all current conditions that are true
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
415 */
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
416
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
417 public int waitForCondition(int condition_set, long timeout) throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
418 if (timeout < 0)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
419 throw new IllegalArgumentException("timeout must be non-negative!");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
420
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
421 return cm.waitForCondition(cn, timeout, condition_set);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
422 }
273
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: 291
diff changeset
424 /**
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
425 * Get the exit code/status from the remote command - if available. Be
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
426 * careful - not all server implementations return this value. It is
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
427 * generally a good idea to call this method only when all data from the
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
428 * remote side has been consumed (see also the <code<WaitForCondition</code> method).
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
429 *
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
430 * @return An <code>Integer</code> holding the exit code, or
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
431 * <code>null</code> if no exit code is (yet) available.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
432 */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
433 public Integer getExitStatus() {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
434 return cn.getExitStatus();
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
435 }
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
436
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
437 /**
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
438 * Get the name of the signal by which the process on the remote side was
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
439 * stopped - if available and applicable. Be careful - not all server
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
440 * implementations return this value.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
441 *
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
442 * @return An <code>String</code> holding the name of the signal, or
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
443 * <code>null</code> if the process exited normally or is still
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
444 * running (or if the server forgot to send this information).
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
445 */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
446 public String getExitSignal() {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
447 return cn.getExitSignal();
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
448 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
449
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
450 /**
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
451 * Close this session. NEVER forget to call this method to free up resources -
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
452 * even if you got an exception from one of the other methods (or when
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
453 * getting an Exception on the Input- or OutputStreams). Sometimes these other
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
454 * methods may throw an exception, saying that the underlying channel is
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
455 * closed (this can happen, e.g., if the other server sent a close message.)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
456 * However, as long as you have not called the <code>close()</code>
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
457 * method, you may be wasting (local) resources.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
458 */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
459 public void close() {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
460 synchronized (this) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
461 if (flag_closed)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
462 return;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
463
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
464 flag_closed = true;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
465
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
466 if (x11FakeCookie != null)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
467 cm.unRegisterX11Cookie(x11FakeCookie, true);
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
468
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
469 try {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
470 cm.closeChannel(cn, "Closed due to user request", true);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
471 }
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
472 catch (IOException ignored) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
473 }
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
474 }
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 291
diff changeset
475 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
476 }