]>
Commit | Line | Data |
---|---|---|
0763e16d JW |
1 | /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ |
2 | /* | |
3 | Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved. | |
4 | ||
5 | Redistribution and use in source and binary forms, with or without | |
6 | modification, are permitted provided that the following conditions are met: | |
7 | ||
8 | 1. Redistributions of source code must retain the above copyright notice, | |
9 | this list of conditions and the following disclaimer. | |
10 | ||
11 | 2. Redistributions in binary form must reproduce the above copyright | |
12 | notice, this list of conditions and the following disclaimer in | |
13 | the documentation and/or other materials provided with the distribution. | |
14 | ||
15 | 3. The names of the authors may not be used to endorse or promote products | |
16 | derived from this software without specific prior written permission. | |
17 | ||
18 | THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, | |
19 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
20 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, | |
21 | INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, | |
22 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | |
24 | OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
25 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
26 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | |
27 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | */ | |
29 | ||
30 | package com.jcraft.jsch; | |
31 | ||
32 | import java.util.*; | |
33 | ||
34 | class ChannelSession extends Channel{ | |
35 | private static byte[] _session=Util.str2byte("session"); | |
36 | ||
37 | protected boolean agent_forwarding=false; | |
38 | protected boolean xforwading=false; | |
39 | protected Hashtable env=null; | |
40 | ||
41 | protected boolean pty=false; | |
42 | ||
43 | protected String ttype="vt100"; | |
44 | protected int tcol=80; | |
45 | protected int trow=24; | |
46 | protected int twp=640; | |
47 | protected int thp=480; | |
48 | protected byte[] terminal_mode=null; | |
49 | ||
50 | ChannelSession(){ | |
51 | super(); | |
52 | type=_session; | |
53 | io=new IO(); | |
54 | } | |
55 | ||
56 | /** | |
57 | * Enable the agent forwarding. | |
58 | * | |
59 | * @param enable | |
60 | */ | |
61 | public void setAgentForwarding(boolean enable){ | |
62 | agent_forwarding=enable; | |
63 | } | |
64 | ||
65 | /** | |
66 | * Enable the X11 forwarding. | |
67 | * | |
68 | * @param enable | |
69 | * @see RFC4254 6.3.1. Requesting X11 Forwarding | |
70 | */ | |
71 | public void setXForwarding(boolean enable){ | |
72 | xforwading=enable; | |
73 | } | |
74 | ||
75 | /** | |
76 | * @deprecated Use {@link #setEnv(String, String)} or {@link #setEnv(byte[], byte[])} instead. | |
77 | * @see #setEnv(String, String) | |
78 | * @see #setEnv(byte[], byte[]) | |
79 | */ | |
80 | public void setEnv(Hashtable env){ | |
81 | synchronized(this){ | |
82 | this.env=env; | |
83 | } | |
84 | } | |
85 | ||
86 | /** | |
87 | * Set the environment variable. | |
88 | * If <code>name</code> and <code>value</code> are needed to be passed | |
89 | * to the remote in your faivorite encoding,use | |
90 | * {@link #setEnv(byte[], byte[])}. | |
91 | * | |
92 | * @param name A name for environment variable. | |
93 | * @param value A value for environment variable. | |
94 | * @see RFC4254 6.4 Environment Variable Passing | |
95 | */ | |
96 | public void setEnv(String name, String value){ | |
97 | setEnv(Util.str2byte(name), Util.str2byte(value)); | |
98 | } | |
99 | ||
100 | /** | |
101 | * Set the environment variable. | |
102 | * | |
103 | * @param name A name of environment variable. | |
104 | * @param value A value of environment variable. | |
105 | * @see #setEnv(String, String) | |
106 | * @see RFC4254 6.4 Environment Variable Passing | |
107 | */ | |
108 | public void setEnv(byte[] name, byte[] value){ | |
109 | synchronized(this){ | |
110 | getEnv().put(name, value); | |
111 | } | |
112 | } | |
113 | ||
114 | private Hashtable getEnv(){ | |
115 | if(env==null) | |
116 | env=new Hashtable(); | |
117 | return env; | |
118 | } | |
119 | ||
120 | /** | |
121 | * Allocate a Pseudo-Terminal. | |
122 | * | |
123 | * @param enable | |
124 | * @see RFC4254 6.2. Requesting a Pseudo-Terminal | |
125 | */ | |
126 | public void setPty(boolean enable){ | |
127 | pty=enable; | |
128 | } | |
129 | ||
130 | /** | |
131 | * Set the terminal mode. | |
132 | * | |
133 | * @param terminal_mode | |
134 | */ | |
135 | public void setTerminalMode(byte[] terminal_mode){ | |
136 | this.terminal_mode=terminal_mode; | |
137 | } | |
138 | ||
139 | /** | |
140 | * Change the window dimension interactively. | |
141 | * | |
142 | * @param col terminal width, columns | |
143 | * @param row terminal height, rows | |
144 | * @param wp terminal width, pixels | |
145 | * @param hp terminal height, pixels | |
146 | * @see RFC4254 6.7. Window Dimension Change Message | |
147 | */ | |
148 | public void setPtySize(int col, int row, int wp, int hp){ | |
149 | setPtyType(this.ttype, col, row, wp, hp); | |
150 | if(!pty || !isConnected()){ | |
151 | return; | |
152 | } | |
153 | try{ | |
154 | RequestWindowChange request=new RequestWindowChange(); | |
155 | request.setSize(col, row, wp, hp); | |
156 | request.request(getSession(), this); | |
157 | } | |
158 | catch(Exception e){ | |
159 | //System.err.println("ChannelSessio.setPtySize: "+e); | |
160 | } | |
161 | } | |
162 | ||
163 | /** | |
164 | * Set the terminal type. | |
165 | * This method is not effective after Channel#connect(). | |
166 | * | |
167 | * @param ttype terminal type(for example, "vt100") | |
168 | * @see #setPtyType(String, int, int, int, int) | |
169 | */ | |
170 | public void setPtyType(String ttype){ | |
171 | setPtyType(ttype, 80, 24, 640, 480); | |
172 | } | |
173 | ||
174 | /** | |
175 | * Set the terminal type. | |
176 | * This method is not effective after Channel#connect(). | |
177 | * | |
178 | * @param ttype terminal type(for example, "vt100") | |
179 | * @param col terminal width, columns | |
180 | * @param row terminal height, rows | |
181 | * @param wp terminal width, pixels | |
182 | * @param hp terminal height, pixels | |
183 | */ | |
184 | public void setPtyType(String ttype, int col, int row, int wp, int hp){ | |
185 | this.ttype=ttype; | |
186 | this.tcol=col; | |
187 | this.trow=row; | |
188 | this.twp=wp; | |
189 | this.thp=hp; | |
190 | } | |
191 | ||
192 | protected void sendRequests() throws Exception{ | |
193 | Session _session=getSession(); | |
194 | Request request; | |
195 | if(agent_forwarding){ | |
196 | request=new RequestAgentForwarding(); | |
197 | request.request(_session, this); | |
198 | } | |
199 | ||
200 | if(xforwading){ | |
201 | request=new RequestX11(); | |
202 | request.request(_session, this); | |
203 | } | |
204 | ||
205 | if(pty){ | |
206 | request=new RequestPtyReq(); | |
207 | ((RequestPtyReq)request).setTType(ttype); | |
208 | ((RequestPtyReq)request).setTSize(tcol, trow, twp, thp); | |
209 | if(terminal_mode!=null){ | |
210 | ((RequestPtyReq)request).setTerminalMode(terminal_mode); | |
211 | } | |
212 | request.request(_session, this); | |
213 | } | |
214 | ||
215 | if(env!=null){ | |
216 | for(Enumeration _env=env.keys(); _env.hasMoreElements();){ | |
217 | Object name=_env.nextElement(); | |
218 | Object value=env.get(name); | |
219 | request=new RequestEnv(); | |
220 | ((RequestEnv)request).setEnv(toByteArray(name), | |
221 | toByteArray(value)); | |
222 | request.request(_session, this); | |
223 | } | |
224 | } | |
225 | } | |
226 | ||
227 | private byte[] toByteArray(Object o){ | |
228 | if(o instanceof String){ | |
229 | return Util.str2byte((String)o); | |
230 | } | |
231 | return (byte[])o; | |
232 | } | |
233 | ||
234 | public void run(){ | |
235 | //System.err.println(this+":run >"); | |
236 | ||
237 | Buffer buf=new Buffer(rmpsize); | |
238 | Packet packet=new Packet(buf); | |
239 | int i=-1; | |
240 | try{ | |
241 | while(isConnected() && | |
242 | thread!=null && | |
243 | io!=null && | |
244 | io.in!=null){ | |
245 | i=io.in.read(buf.buffer, | |
246 | 14, | |
247 | buf.buffer.length-14 | |
248 | -32 -20 // padding and mac | |
249 | ); | |
250 | if(i==0)continue; | |
251 | if(i==-1){ | |
252 | eof(); | |
253 | break; | |
254 | } | |
255 | if(close)break; | |
256 | //System.out.println("write: "+i); | |
257 | packet.reset(); | |
258 | buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA); | |
259 | buf.putInt(recipient); | |
260 | buf.putInt(i); | |
261 | buf.skip(i); | |
262 | getSession().write(packet, this, i); | |
263 | } | |
264 | } | |
265 | catch(Exception e){ | |
266 | //System.err.println("# ChannelExec.run"); | |
267 | //e.printStackTrace(); | |
268 | } | |
269 | Thread _thread=thread; | |
270 | if(_thread!=null){ | |
271 | synchronized(_thread){ _thread.notifyAll(); } | |
272 | } | |
273 | thread=null; | |
274 | //System.err.println(this+":run <"); | |
275 | } | |
276 | } |