1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/*
* Copyright (C) 2012-2016 Brian P. Hinz. All Rights Reserved.
* Copyright (C) 2000 Const Kaplinsky. All Rights Reserved.
* Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
/*
* tunnel.java - SSH tunneling support
*/
package com.tigervnc.vncviewer;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
import com.tigervnc.rdr.*;
import com.tigervnc.rfb.*;
import com.tigervnc.rfb.Exception;
import com.tigervnc.network.*;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.ConfigRepository;
import com.jcraft.jsch.Logger;
import com.jcraft.jsch.OpenSSHConfig;
import com.jcraft.jsch.Session;
import static com.tigervnc.vncviewer.Parameters.*;
public class Tunnel {
private final static String DEFAULT_TUNNEL_TEMPLATE
= "-f -L %L:localhost:%R %H sleep 20";
private final static String DEFAULT_VIA_TEMPLATE
= "-f -L %L:%H:%R %G sleep 20";
public static void createTunnel(CConn cc, int localPort) throws Exception {
int remotePort;
String gatewayHost;
String remoteHost;
remotePort = cc.getServerPort();
if (tunnel.getValue()) {
gatewayHost = cc.getServerName();
remoteHost = "localhost";
} else {
gatewayHost = getSshHost();
remoteHost = cc.getServerName();
}
String pattern = extSSHArgs.getValue();
if (pattern == null || pattern.isEmpty()) {
if (tunnel.getValue())
pattern = System.getProperty("VNC_TUNNEL_CMD");
else
pattern = System.getProperty("VNC_VIA_CMD");
}
if (extSSH.getValue() ||
(pattern != null && pattern.length() > 0)) {
createTunnelExt(gatewayHost, remoteHost, remotePort, localPort, pattern);
} else {
createTunnelJSch(gatewayHost, remoteHost, remotePort, localPort);
}
}
private static class MyJSchLogger implements Logger {
public boolean isEnabled(int level){
return true;
}
public void log(int level, String msg){
switch (level) {
case Logger.INFO:
vlog.info(msg);
break;
case Logger.ERROR:
vlog.error(msg);
break;
default:
vlog.debug(msg);
}
}
}
public static String getSshHost() {
String sshHost = via.getValue();
if (sshHost.isEmpty())
return vncServerName.getValue();
int end = sshHost.indexOf(":");
if (end < 0)
end = sshHost.length();
sshHost = sshHost.substring(sshHost.indexOf("@")+1, end);
return sshHost;
}
public static String getSshUser() {
String sshUser = (String)System.getProperties().get("user.name");
String viaStr = via.getValue();
if (!viaStr.isEmpty() && viaStr.indexOf("@") > 0)
sshUser = viaStr.substring(0, viaStr.indexOf("@"));
return sshUser;
}
public static int getSshPort() {
String sshPort = "22";
String viaStr = via.getValue();
if (!viaStr.isEmpty() && viaStr.indexOf(":") > 0)
sshPort = viaStr.substring(viaStr.indexOf(":")+1, viaStr.length());
return Integer.parseInt(sshPort);
}
public static String getSshKeyFile() {
if (!sshKeyFile.getValue().isEmpty())
return sshKeyFile.getValue();
String[] ids = { "id_dsa", "id_rsa" };
for (String id : ids) {
File f = new File(FileUtils.getHomeDir()+".ssh/"+id);
if (f.exists() && f.canRead())
return(f.getAbsolutePath());
}
return "";
}
public static String getSshKey() {
if (!sshKey.getValue().isEmpty())
return sshKeyFile.getValue().replaceAll("\\\\n", "\n");
return "";
}
private static void createTunnelJSch(String gatewayHost, String remoteHost,
int remotePort, int localPort) throws Exception {
JSch.setLogger(new MyJSchLogger());
JSch jsch=new JSch();
try {
// NOTE: jsch does not support all ciphers. User may be
// prompted to accept host key authenticy even if
// the key is in the known_hosts file.
File knownHosts = new File(FileUtils.getHomeDir()+".ssh/known_hosts");
if (knownHosts.exists() && knownHosts.canRead())
jsch.setKnownHosts(knownHosts.getAbsolutePath());
ArrayList<File> privateKeys = new ArrayList<File>();
if (!getSshKey().isEmpty()) {
byte[] keyPass = null, key;
if (!sshKeyPass.getValue().isEmpty())
keyPass = sshKeyPass.getValue().getBytes();
jsch.addIdentity("TigerVNC", getSshKey().getBytes(), null, keyPass);
} else if (!getSshKeyFile().isEmpty()) {
File f = new File(getSshKeyFile());
if (!f.exists() || !f.canRead())
throw new Exception("Cannot access SSH key file "+getSshKeyFile());
privateKeys.add(f);
}
for (Iterator<File> i = privateKeys.iterator(); i.hasNext();) {
File privateKey = (File)i.next();
if (privateKey.exists() && privateKey.canRead())
if (!sshKeyPass.getValue().isEmpty())
jsch.addIdentity(privateKey.getAbsolutePath(),
sshKeyPass.getValue());
else
jsch.addIdentity(privateKey.getAbsolutePath());
}
String user = getSshUser();
String label = new String("SSH Authentication");
PasswdDialog dlg =
new PasswdDialog(label, (user == null ? false : true), false);
dlg.userEntry.setText(user != null ? user : "");
File ssh_config = new File(sshConfig.getValue());
if (ssh_config.exists() && ssh_config.canRead()) {
ConfigRepository repo =
OpenSSHConfig.parse(ssh_config.getAbsolutePath());
jsch.setConfigRepository(repo);
}
Session session=jsch.getSession(user, gatewayHost, getSshPort());
session.setUserInfo(dlg);
// OpenSSHConfig doesn't recognize StrictHostKeyChecking
if (session.getConfig("StrictHostKeyChecking") == null)
session.setConfig("StrictHostKeyChecking", "ask");
session.connect();
session.setPortForwardingL(localPort, remoteHost, remotePort);
} catch (java.lang.Exception e) {
throw new Exception(e.getMessage());
}
}
private static void createTunnelExt(String gatewayHost, String remoteHost,
int remotePort, int localPort,
String pattern) throws Exception {
if (pattern == null || pattern.length() < 1) {
if (tunnel.getValue())
pattern = DEFAULT_TUNNEL_TEMPLATE;
else
pattern = DEFAULT_VIA_TEMPLATE;
}
String cmd = fillCmdPattern(pattern, gatewayHost, remoteHost,
remotePort, localPort);
try {
Thread t = new Thread(new ExtProcess(cmd, vlog, true));
t.start();
// wait for the ssh process to start
Thread.sleep(1000);
} catch (java.lang.Exception e) {
throw new Exception(e.getMessage());
}
}
private static String fillCmdPattern(String pattern, String gatewayHost,
String remoteHost, int remotePort,
int localPort) {
boolean H_found = false, G_found = false, R_found = false, L_found = false;
boolean P_found = false;
String cmd = extSSHClient.getValue() + " ";
pattern.replaceAll("^\\s+", "");
String user = getSshUser();
int sshPort = getSshPort();
gatewayHost = user + "@" + gatewayHost;
for (int i = 0; i < pattern.length(); i++) {
if (pattern.charAt(i) == '%') {
switch (pattern.charAt(++i)) {
case 'H':
cmd += (tunnel.getValue() ? gatewayHost : remoteHost);
H_found = true;
continue;
case 'G':
cmd += gatewayHost;
G_found = true;
continue;
case 'R':
cmd += remotePort;
R_found = true;
continue;
case 'L':
cmd += localPort;
L_found = true;
continue;
case 'P':
cmd += sshPort;
P_found = true;
continue;
}
}
cmd += pattern.charAt(i);
}
if (pattern.length() > 1024)
throw new Exception("Tunneling command is too long.");
if (!H_found || !R_found || !L_found)
throw new Exception("%H, %R or %L absent in tunneling command template.");
if (!tunnel.getValue() && !G_found)
throw new Exception("%G pattern absent in tunneling command template.");
vlog.info("SSH command line: "+cmd);
if (VncViewer.os.startsWith("windows"))
cmd.replaceAll("\\\\", "\\\\\\\\");
return cmd;
}
static LogWriter vlog = new LogWriter("Tunnel");
}
|