Mercurial > 510Connectbot
annotate src/com/five_ten_sg/connectbot/transport/SSH.java @ 39:69bc44954bb5 tn5250
start tn5250 integration
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Tue, 10 Jun 2014 17:06:53 -0700 |
parents | 139394237973 |
children | 77ac18bc1b2f |
rev | line source |
---|---|
0 | 1 /* |
2 * ConnectBot: simple, powerful, open-source SSH client for Android | |
3 * Copyright 2007 Kenny Root, Jeffrey Sharkey | |
4 * | |
5 * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 * you may not use this file except in compliance with the License. | |
7 * You may obtain a copy of the License at | |
8 * | |
9 * http://www.apache.org/licenses/LICENSE-2.0 | |
10 * | |
11 * Unless required by applicable law or agreed to in writing, software | |
12 * distributed under the License is distributed on an "AS IS" BASIS, | |
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 * See the License for the specific language governing permissions and | |
15 * limitations under the License. | |
16 */ | |
17 | |
18 package com.five_ten_sg.connectbot.transport; | |
19 | |
20 import java.io.File; | |
21 import java.io.IOException; | |
22 import java.io.InputStream; | |
23 import java.io.OutputStream; | |
24 import java.net.InetAddress; | |
25 import java.net.InetSocketAddress; | |
26 import java.net.URL; | |
27 import java.net.MalformedURLException; | |
28 import java.security.KeyPair; | |
29 import java.security.NoSuchAlgorithmException; | |
30 import java.security.PrivateKey; | |
31 import java.security.PublicKey; | |
32 import java.security.interfaces.DSAPrivateKey; | |
33 import java.security.interfaces.DSAPublicKey; | |
34 import java.security.interfaces.RSAPrivateKey; | |
35 import java.security.interfaces.RSAPublicKey; | |
36 import java.security.spec.InvalidKeySpecException; | |
37 import java.util.Arrays; | |
38 import java.util.HashMap; | |
39 import java.util.LinkedList; | |
40 import java.util.List; | |
41 import java.util.Locale; | |
42 import java.util.Map; | |
43 import java.util.Map.Entry; | |
44 import java.util.regex.Matcher; | |
45 import java.util.regex.Pattern; | |
46 | |
47 import com.five_ten_sg.connectbot.R; | |
48 import com.five_ten_sg.connectbot.bean.HostBean; | |
49 import com.five_ten_sg.connectbot.bean.PortForwardBean; | |
50 import com.five_ten_sg.connectbot.bean.PubkeyBean; | |
51 import com.five_ten_sg.connectbot.service.TerminalBridge; | |
52 import com.five_ten_sg.connectbot.service.TerminalManager; | |
53 import com.five_ten_sg.connectbot.service.TerminalManager.KeyHolder; | |
54 import com.five_ten_sg.connectbot.util.HostDatabase; | |
55 import com.five_ten_sg.connectbot.util.PubkeyDatabase; | |
56 import com.five_ten_sg.connectbot.util.PubkeyUtils; | |
57 import android.content.Context; | |
58 import android.net.Uri; | |
59 import android.os.Environment; | |
60 import android.util.Log; | |
61 | |
62 import com.trilead.ssh2.AuthAgentCallback; | |
63 import com.trilead.ssh2.ChannelCondition; | |
64 import com.trilead.ssh2.Connection; | |
65 import com.trilead.ssh2.ConnectionInfo; | |
66 import com.trilead.ssh2.ConnectionMonitor; | |
67 import com.trilead.ssh2.DynamicPortForwarder; | |
68 import com.trilead.ssh2.InteractiveCallback; | |
69 import com.trilead.ssh2.KnownHosts; | |
70 import com.trilead.ssh2.LocalPortForwarder; | |
71 import com.trilead.ssh2.SCPClient; | |
72 import com.trilead.ssh2.ServerHostKeyVerifier; | |
73 import com.trilead.ssh2.Session; | |
74 import com.trilead.ssh2.HTTPProxyData; | |
75 import com.trilead.ssh2.HTTPProxyException; | |
76 import com.trilead.ssh2.crypto.PEMDecoder; | |
77 import com.trilead.ssh2.signature.DSASHA1Verify; | |
78 import com.trilead.ssh2.signature.RSASHA1Verify; | |
79 | |
80 /** | |
81 * @author Kenny Root | |
82 * | |
83 */ | |
84 public class SSH extends AbsTransport implements ConnectionMonitor, InteractiveCallback, AuthAgentCallback { | |
85 private static final String PROTOCOL = "ssh"; | |
86 private static final String TAG = "ConnectBot.SSH"; | |
87 private static final int DEFAULT_PORT = 22; | |
88 | |
89 private static final String AUTH_PUBLICKEY = "publickey", | |
90 AUTH_PASSWORD = "password", | |
91 AUTH_KEYBOARDINTERACTIVE = "keyboard-interactive"; | |
92 | |
93 private final static int AUTH_TRIES = 20; | |
94 | |
95 static final Pattern hostmask; | |
96 static { | |
97 hostmask = Pattern.compile("^(.+)@([0-9a-z.-]+)(:(\\d+))?$", Pattern.CASE_INSENSITIVE); | |
98 } | |
99 | |
100 private boolean compression = false; | |
101 private String httpproxy = null; | |
102 private volatile boolean authenticated = false; | |
103 private volatile boolean connected = false; | |
104 private volatile boolean sessionOpen = false; | |
105 | |
106 private boolean pubkeysExhausted = false; | |
107 private boolean interactiveCanContinue = true; | |
108 | |
109 private Connection connection; | |
110 private Session session; | |
111 private ConnectionInfo connectionInfo; | |
112 | |
113 private OutputStream stdin; | |
114 private InputStream stdout; | |
115 private InputStream stderr; | |
116 | |
117 private static final int conditions = ChannelCondition.STDOUT_DATA | |
118 | ChannelCondition.STDERR_DATA | |
119 | ChannelCondition.CLOSED | |
120 | ChannelCondition.EOF; | |
121 | |
122 private List<PortForwardBean> portForwards = new LinkedList<PortForwardBean>(); | |
123 | |
124 private int columns; | |
125 private int rows; | |
126 | |
127 private int width; | |
128 private int height; | |
129 | |
130 private String useAuthAgent = HostDatabase.AUTHAGENT_NO; | |
131 private String agentLockPassphrase; | |
132 | |
133 public class HostKeyVerifier implements ServerHostKeyVerifier { | |
134 public boolean verifyServerHostKey(String hostname, int port, | |
135 String serverHostKeyAlgorithm, byte[] serverHostKey) throws IOException { | |
136 // read in all known hosts from hostdb | |
137 KnownHosts hosts = manager.hostdb.getKnownHosts(); | |
138 Boolean result; | |
139 String matchName = String.format(Locale.US, "%s:%d", hostname, port); | |
140 String fingerprint = KnownHosts.createHexFingerprint(serverHostKeyAlgorithm, serverHostKey); | |
141 String algorithmName; | |
142 | |
143 if ("ssh-rsa".equals(serverHostKeyAlgorithm)) | |
144 algorithmName = "RSA"; | |
145 else if ("ssh-dss".equals(serverHostKeyAlgorithm)) | |
146 algorithmName = "DSA"; | |
147 else if (serverHostKeyAlgorithm.startsWith("ecdsa-")) | |
148 algorithmName = "EC"; | |
149 else | |
150 algorithmName = serverHostKeyAlgorithm; | |
151 | |
152 switch (hosts.verifyHostkey(matchName, serverHostKeyAlgorithm, serverHostKey)) { | |
153 case KnownHosts.HOSTKEY_IS_OK: | |
154 bridge.outputLine(manager.res.getString(R.string.terminal_sucess, algorithmName, fingerprint)); | |
155 return true; | |
156 | |
157 case KnownHosts.HOSTKEY_IS_NEW: | |
158 // prompt user | |
159 bridge.outputLine(manager.res.getString(R.string.host_authenticity_warning, hostname)); | |
160 bridge.outputLine(manager.res.getString(R.string.host_fingerprint, algorithmName, fingerprint)); | |
161 result = bridge.promptHelper.requestBooleanPrompt(null, manager.res.getString(R.string.prompt_continue_connecting)); | |
162 | |
163 if (result == null) return false; | |
164 | |
165 if (result.booleanValue()) { | |
166 // save this key in known database | |
167 manager.hostdb.saveKnownHost(hostname, port, serverHostKeyAlgorithm, serverHostKey); | |
168 } | |
169 | |
170 return result.booleanValue(); | |
171 | |
172 case KnownHosts.HOSTKEY_HAS_CHANGED: | |
173 String header = String.format("@ %s @", | |
174 manager.res.getString(R.string.host_verification_failure_warning_header)); | |
175 char[] atsigns = new char[header.length()]; | |
176 Arrays.fill(atsigns, '@'); | |
177 String border = new String(atsigns); | |
178 bridge.outputLine(border); | |
179 bridge.outputLine(manager.res.getString(R.string.host_verification_failure_warning)); | |
180 bridge.outputLine(border); | |
181 bridge.outputLine(String.format(manager.res.getString(R.string.host_fingerprint), | |
182 algorithmName, fingerprint)); | |
183 // Users have no way to delete keys, so we'll prompt them for now. | |
184 result = bridge.promptHelper.requestBooleanPrompt(null, manager.res.getString(R.string.prompt_continue_connecting)); | |
185 | |
186 if (result == null) return false; | |
187 | |
188 if (result.booleanValue()) { | |
189 // save this key in known database | |
190 manager.hostdb.saveKnownHost(hostname, port, serverHostKeyAlgorithm, serverHostKey); | |
191 } | |
192 | |
193 return result.booleanValue(); | |
194 | |
195 default: | |
196 return false; | |
197 } | |
198 } | |
199 | |
200 } | |
201 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
202 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
203 public SSH() { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
204 super(); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
205 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
206 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
207 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
208 /** |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
209 * @return protocol part of the URI |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
210 */ |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
211 public static String getProtocolName() { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
212 return PROTOCOL; |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
213 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
214 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
215 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
216 public Uri getUri(String input) { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
217 Matcher matcher = hostmask.matcher(input); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
218 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
219 if (!matcher.matches()) |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
220 return null; |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
221 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
222 StringBuilder sb = new StringBuilder(); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
223 sb.append(PROTOCOL) |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
224 .append("://") |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
225 .append(Uri.encode(matcher.group(1))) |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
226 .append('@') |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
227 .append(matcher.group(2)); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
228 String portString = matcher.group(4); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
229 int port = DEFAULT_PORT; |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
230 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
231 if (portString != null) { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
232 try { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
233 port = Integer.parseInt(portString); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
234 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
235 if (port < 1 || port > 65535) { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
236 port = DEFAULT_PORT; |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
237 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
238 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
239 catch (NumberFormatException nfe) { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
240 // Keep the default port |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
241 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
242 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
243 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
244 if (port != DEFAULT_PORT) { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
245 sb.append(':') |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
246 .append(port); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
247 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
248 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
249 sb.append("/#") |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
250 .append(Uri.encode(input)); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
251 Uri uri = Uri.parse(sb.toString()); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
252 return uri; |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
253 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
254 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
255 |
0 | 256 private void authenticate() { |
257 try { | |
258 if (connection.authenticateWithNone(host.getUsername())) { | |
259 finishConnection(); | |
260 return; | |
261 } | |
262 } | |
263 catch (Exception e) { | |
264 Log.d(TAG, "Host does not support 'none' authentication."); | |
265 } | |
266 | |
267 bridge.outputLine(manager.res.getString(R.string.terminal_auth)); | |
268 | |
269 try { | |
270 long pubkeyId = host.getPubkeyId(); | |
271 | |
272 if (!pubkeysExhausted && | |
273 pubkeyId != HostDatabase.PUBKEYID_NEVER && | |
274 connection.isAuthMethodAvailable(host.getUsername(), AUTH_PUBLICKEY)) { | |
275 // if explicit pubkey defined for this host, then prompt for password as needed | |
276 // otherwise just try all in-memory keys held in terminalmanager | |
277 if (pubkeyId == HostDatabase.PUBKEYID_ANY) { | |
278 // try each of the in-memory keys | |
279 bridge.outputLine(manager.res | |
280 .getString(R.string.terminal_auth_pubkey_any)); | |
281 | |
282 for (Entry<String, KeyHolder> entry : manager.loadedKeypairs.entrySet()) { | |
283 if (entry.getValue().bean.isConfirmUse() | |
284 && !promptForPubkeyUse(entry.getKey())) | |
285 continue; | |
286 | |
287 if (this.tryPublicKey(host.getUsername(), entry.getKey(), | |
288 entry.getValue().pair)) { | |
289 finishConnection(); | |
290 break; | |
291 } | |
292 } | |
293 } | |
294 else { | |
295 bridge.outputLine(manager.res.getString(R.string.terminal_auth_pubkey_specific)); | |
296 // use a specific key for this host, as requested | |
297 PubkeyBean pubkey = manager.pubkeydb.findPubkeyById(pubkeyId); | |
298 | |
299 if (pubkey == null) | |
300 bridge.outputLine(manager.res.getString(R.string.terminal_auth_pubkey_invalid)); | |
301 else if (tryPublicKey(pubkey)) | |
302 finishConnection(); | |
303 } | |
304 | |
305 pubkeysExhausted = true; | |
306 } | |
307 else if (interactiveCanContinue && | |
308 connection.isAuthMethodAvailable(host.getUsername(), AUTH_KEYBOARDINTERACTIVE)) { | |
309 // this auth method will talk with us using InteractiveCallback interface | |
310 // it blocks until authentication finishes | |
311 bridge.outputLine(manager.res.getString(R.string.terminal_auth_ki)); | |
312 interactiveCanContinue = false; | |
313 | |
314 if (connection.authenticateWithKeyboardInteractive(host.getUsername(), this)) { | |
315 finishConnection(); | |
316 } | |
317 else { | |
318 bridge.outputLine(manager.res.getString(R.string.terminal_auth_ki_fail)); | |
319 } | |
320 } | |
321 else if (connection.isAuthMethodAvailable(host.getUsername(), AUTH_PASSWORD)) { | |
322 bridge.outputLine(manager.res.getString(R.string.terminal_auth_pass)); | |
323 String password = bridge.getPromptHelper().requestPasswordPrompt(null, | |
324 manager.res.getString(R.string.prompt_password)); | |
325 | |
326 if (password != null | |
327 && connection.authenticateWithPassword(host.getUsername(), password)) { | |
328 finishConnection(); | |
329 } | |
330 else { | |
331 bridge.outputLine(manager.res.getString(R.string.terminal_auth_pass_fail)); | |
332 } | |
333 } | |
334 else { | |
335 bridge.outputLine(manager.res.getString(R.string.terminal_auth_fail)); | |
336 } | |
337 } | |
338 catch (IllegalStateException e) { | |
339 Log.e(TAG, "Connection went away while we were trying to authenticate", e); | |
340 return; | |
341 } | |
342 catch (Exception e) { | |
343 Log.e(TAG, "Problem during handleAuthentication()", e); | |
344 } | |
345 } | |
346 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
347 |
0 | 348 /** |
349 * Attempt connection with database row pointed to by cursor. | |
350 * @param cursor | |
351 * @return true for successful authentication | |
352 * @throws NoSuchAlgorithmException | |
353 * @throws InvalidKeySpecException | |
354 * @throws IOException | |
355 */ | |
356 private boolean tryPublicKey(PubkeyBean pubkey) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException { | |
357 KeyPair pair = null; | |
358 | |
359 if (manager.isKeyLoaded(pubkey.getNickname())) { | |
360 // load this key from memory if its already there | |
361 Log.d(TAG, String.format("Found unlocked key '%s' already in-memory", pubkey.getNickname())); | |
362 | |
363 if (pubkey.isConfirmUse()) { | |
364 if (!promptForPubkeyUse(pubkey.getNickname())) | |
365 return false; | |
366 } | |
367 | |
368 pair = manager.getKey(pubkey.getNickname()); | |
369 } | |
370 else { | |
371 // otherwise load key from database and prompt for password as needed | |
372 String password = null; | |
373 | |
374 if (pubkey.isEncrypted()) { | |
375 password = bridge.getPromptHelper().requestPasswordPrompt(null, | |
376 manager.res.getString(R.string.prompt_pubkey_password, pubkey.getNickname())); | |
377 | |
378 // Something must have interrupted the prompt. | |
379 if (password == null) | |
380 return false; | |
381 } | |
382 | |
383 if (PubkeyDatabase.KEY_TYPE_IMPORTED.equals(pubkey.getType())) { | |
384 // load specific key using pem format | |
385 pair = PEMDecoder.decode(new String(pubkey.getPrivateKey()).toCharArray(), password); | |
386 } | |
387 else { | |
388 // load using internal generated format | |
389 PrivateKey privKey; | |
390 | |
391 try { | |
392 privKey = PubkeyUtils.decodePrivate(pubkey.getPrivateKey(), | |
393 pubkey.getType(), password); | |
394 } | |
395 catch (Exception e) { | |
396 String message = String.format("Bad password for key '%s'. Authentication failed.", pubkey.getNickname()); | |
397 Log.e(TAG, message, e); | |
398 bridge.outputLine(message); | |
399 return false; | |
400 } | |
401 | |
402 PublicKey pubKey = PubkeyUtils.decodePublic(pubkey.getPublicKey(), pubkey.getType()); | |
403 // convert key to trilead format | |
404 pair = new KeyPair(pubKey, privKey); | |
405 Log.d(TAG, "Unlocked key " + PubkeyUtils.formatKey(pubKey)); | |
406 } | |
407 | |
408 Log.d(TAG, String.format("Unlocked key '%s'", pubkey.getNickname())); | |
409 // save this key in memory | |
410 manager.addKey(pubkey, pair); | |
411 } | |
412 | |
413 return tryPublicKey(host.getUsername(), pubkey.getNickname(), pair); | |
414 } | |
415 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
416 |
0 | 417 private boolean tryPublicKey(String username, String keyNickname, KeyPair pair) throws IOException { |
418 //bridge.outputLine(String.format("Attempting 'publickey' with key '%s' [%s]...", keyNickname, trileadKey.toString())); | |
419 boolean success = connection.authenticateWithPublicKey(username, pair); | |
420 | |
421 if (!success) | |
422 bridge.outputLine(manager.res.getString(R.string.terminal_auth_pubkey_fail, keyNickname)); | |
423 | |
424 return success; | |
425 } | |
426 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
427 |
0 | 428 /** |
429 * Internal method to request actual PTY terminal once we've finished | |
430 * authentication. If called before authenticated, it will just fail. | |
431 */ | |
432 private void finishConnection() { | |
433 authenticated = true; | |
434 | |
435 for (PortForwardBean portForward : portForwards) { | |
436 try { | |
437 enablePortForward(portForward); | |
438 bridge.outputLine(manager.res.getString(R.string.terminal_enable_portfoward, portForward.getDescription())); | |
439 } | |
440 catch (Exception e) { | |
441 Log.e(TAG, "Error setting up port forward during connect", e); | |
442 } | |
443 } | |
444 | |
445 if (!host.getWantSession()) { | |
446 bridge.outputLine(manager.res.getString(R.string.terminal_no_session)); | |
447 bridge.onConnected(); | |
448 return; | |
449 } | |
450 | |
451 try { | |
452 session = connection.openSession(); | |
453 | |
454 if (!useAuthAgent.equals(HostDatabase.AUTHAGENT_NO)) | |
455 session.requestAuthAgentForwarding(this); | |
456 | |
457 if (host.getWantX11Forward()) { | |
458 try { | |
459 session.requestX11Forwarding(host.getX11Host(), host.getX11Port(), null, false); | |
460 } | |
461 catch (IOException e2) { | |
462 Log.e(TAG, "Problem while trying to setup X11 forwarding in finishConnection()", e2); | |
463 } | |
464 } | |
465 | |
466 session.requestPTY(getEmulation(), columns, rows, width, height, null); | |
467 session.startShell(); | |
468 stdin = session.getStdin(); | |
469 stdout = session.getStdout(); | |
470 stderr = session.getStderr(); | |
471 sessionOpen = true; | |
472 bridge.onConnected(); | |
473 } | |
474 catch (IOException e1) { | |
475 Log.e(TAG, "Problem while trying to create PTY in finishConnection()", e1); | |
476 } | |
477 } | |
478 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
479 |
0 | 480 @Override |
481 public void connect() { | |
482 connection = new Connection(host.getHostname(), host.getPort()); | |
483 connection.addConnectionMonitor(this); | |
484 | |
485 try { | |
486 connection.setCompression(compression); | |
487 } | |
488 catch (IOException e) { | |
489 Log.e(TAG, "Could not enable compression!", e); | |
490 } | |
491 | |
492 if (httpproxy != null && httpproxy.length() > 0) { | |
493 Log.d(TAG, "Want HTTP Proxy: " + httpproxy, null); | |
494 | |
495 try { | |
496 URL u; | |
497 | |
498 if (httpproxy.startsWith("http://")) { | |
499 u = new URL(httpproxy); | |
500 } | |
501 else { | |
502 u = new URL("http://" + httpproxy); | |
503 } | |
504 | |
505 connection.setProxyData(new HTTPProxyData( | |
506 u.getHost(), | |
507 u.getPort(), | |
508 u.getUserInfo(), | |
509 u.getAuthority())); | |
510 bridge.outputLine("Connecting via proxy: " + httpproxy); | |
511 } | |
512 catch (MalformedURLException e) { | |
513 Log.e(TAG, "Could not parse proxy " + httpproxy, e); | |
514 // Display the reason in the text. | |
515 bridge.outputLine("Bad proxy URL: " + httpproxy); | |
516 onDisconnect(); | |
517 return; | |
518 } | |
519 } | |
520 | |
521 try { | |
522 /* Uncomment when debugging SSH protocol: | |
523 DebugLogger logger = new DebugLogger() { | |
524 | |
525 public void log(int level, String className, String message) { | |
526 Log.d("SSH", message); | |
527 } | |
528 | |
529 }; | |
530 Logger.enabled = true; | |
531 Logger.logger = logger; | |
532 */ | |
533 connectionInfo = connection.connect(new HostKeyVerifier()); | |
534 connected = true; | |
535 | |
536 if (connectionInfo.clientToServerCryptoAlgorithm | |
537 .equals(connectionInfo.serverToClientCryptoAlgorithm) | |
538 && connectionInfo.clientToServerMACAlgorithm | |
539 .equals(connectionInfo.serverToClientMACAlgorithm)) { | |
540 bridge.outputLine(manager.res.getString(R.string.terminal_using_algorithm, | |
541 connectionInfo.clientToServerCryptoAlgorithm, | |
542 connectionInfo.clientToServerMACAlgorithm)); | |
543 } | |
544 else { | |
545 bridge.outputLine(manager.res.getString( | |
546 R.string.terminal_using_c2s_algorithm, | |
547 connectionInfo.clientToServerCryptoAlgorithm, | |
548 connectionInfo.clientToServerMACAlgorithm)); | |
549 bridge.outputLine(manager.res.getString( | |
550 R.string.terminal_using_s2c_algorithm, | |
551 connectionInfo.serverToClientCryptoAlgorithm, | |
552 connectionInfo.serverToClientMACAlgorithm)); | |
553 } | |
554 } | |
555 catch (HTTPProxyException e) { | |
556 Log.e(TAG, "Failed to connect to HTTP Proxy", e); | |
557 // Display the reason in the text. | |
558 bridge.outputLine("Failed to connect to HTTP Proxy."); | |
559 onDisconnect(); | |
560 return; | |
561 } | |
562 catch (IOException e) { | |
563 Log.e(TAG, "Problem in SSH connection thread during authentication", e); | |
564 // Display the reason in the text. | |
565 bridge.outputLine(e.getCause().getMessage()); | |
566 onDisconnect(); | |
567 return; | |
568 } | |
569 | |
570 try { | |
571 // enter a loop to keep trying until authentication | |
572 int tries = 0; | |
573 | |
574 while (connected && !connection.isAuthenticationComplete() && tries++ < AUTH_TRIES) { | |
575 authenticate(); | |
576 // sleep to make sure we dont kill system | |
577 Thread.sleep(1000); | |
578 } | |
579 } | |
580 catch (Exception e) { | |
581 Log.e(TAG, "Problem in SSH connection thread during authentication", e); | |
582 } | |
583 } | |
584 | |
585 | |
586 @Override | |
587 public boolean willBlock() { | |
588 if (stdout == null) return true; | |
589 try { | |
590 return stdout.available() == 0; | |
591 } catch (Exception e) { | |
592 return true; | |
593 } | |
594 } | |
595 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
596 |
0 | 597 @Override |
598 public int read(byte[] buffer, int start, int len) throws IOException { | |
599 int bytesRead = 0; | |
600 | |
601 if (session == null) | |
602 return 0; | |
603 | |
604 int newConditions = session.waitForCondition(conditions, 0); | |
605 | |
606 if ((newConditions & ChannelCondition.STDOUT_DATA) != 0) { | |
607 bytesRead = stdout.read(buffer, start, len); | |
608 } | |
609 | |
610 if ((newConditions & ChannelCondition.STDERR_DATA) != 0) { | |
611 byte discard[] = new byte[256]; | |
612 | |
613 while (stderr.available() > 0) { | |
614 stderr.read(discard); | |
615 } | |
616 } | |
617 | |
618 if ((newConditions & ChannelCondition.EOF) != 0) { | |
619 onDisconnect(); | |
620 throw new IOException("Remote end closed connection"); | |
621 } | |
622 | |
623 return bytesRead; | |
624 } | |
625 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
626 |
0 | 627 @Override |
628 public void write(byte[] buffer) throws IOException { | |
629 if (stdin != null) | |
630 stdin.write(buffer); | |
631 } | |
632 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
633 |
0 | 634 @Override |
635 public void write(int c) throws IOException { | |
636 if (stdin != null) | |
637 stdin.write(c); | |
638 } | |
639 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
640 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
641 @Override |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
642 public void flush() throws IOException { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
643 if (stdin != null) |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
644 stdin.flush(); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
645 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
646 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
647 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
648 public void connectionLost(Throwable reason) { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
649 onDisconnect(); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
650 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
651 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
652 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
653 private void onDisconnect() { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
654 close(); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
655 bridge.dispatchDisconnect(false); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
656 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
657 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
658 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
659 @Override |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
660 public void close() { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
661 connected = false; |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
662 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
663 if (session != null) { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
664 session.close(); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
665 session = null; |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
666 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
667 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
668 if (connection != null) { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
669 connection.close(); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
670 connection = null; |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
671 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
672 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
673 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
674 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
675 @Override |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
676 public void setDimensions(int columns, int rows, int width, int height) { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
677 this.columns = columns; |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
678 this.rows = rows; |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
679 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
680 if (sessionOpen) { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
681 try { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
682 session.resizePTY(columns, rows, width, height); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
683 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
684 catch (IOException e) { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
685 Log.e(TAG, "Couldn't send resize PTY packet", e); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
686 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
687 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
688 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
689 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
690 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
691 @Override |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
692 public void setOptions(Map<String, String> options) { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
693 if (options.containsKey("compression")) |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
694 compression = Boolean.parseBoolean(options.get("compression")); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
695 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
696 if (options.containsKey("httpproxy")) |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
697 httpproxy = options.get("httpproxy"); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
698 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
699 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
700 |
0 | 701 @Override |
702 public Map<String, String> getOptions() { | |
703 Map<String, String> options = new HashMap<String, String>(); | |
704 options.put("compression", Boolean.toString(compression)); | |
705 | |
706 if (httpproxy != null) | |
707 options.put("httpproxy", httpproxy); | |
708 | |
709 return options; | |
710 } | |
711 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
712 |
0 | 713 @Override |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
714 public void setCompression(boolean compression) { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
715 this.compression = compression; |
0 | 716 } |
717 | |
718 | |
719 @Override | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
720 public void setHttpproxy(String httpproxy) { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
721 this.httpproxy = httpproxy; |
0 | 722 } |
723 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
724 |
0 | 725 @Override |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
726 public void setUseAuthAgent(String useAuthAgent) { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
727 this.useAuthAgent = useAuthAgent; |
0 | 728 } |
729 | |
730 @Override | |
731 public boolean canForwardPorts() { | |
732 return true; | |
733 } | |
734 | |
735 @Override | |
736 public List<PortForwardBean> getPortForwards() { | |
737 return portForwards; | |
738 } | |
739 | |
740 @Override | |
741 public boolean addPortForward(PortForwardBean portForward) { | |
742 return portForwards.add(portForward); | |
743 } | |
744 | |
745 @Override | |
746 public boolean removePortForward(PortForwardBean portForward) { | |
747 // Make sure we don't have a phantom forwarder. | |
748 disablePortForward(portForward); | |
749 return portForwards.remove(portForward); | |
750 } | |
751 | |
752 @Override | |
753 public boolean enablePortForward(PortForwardBean portForward) { | |
754 if (!portForwards.contains(portForward)) { | |
755 Log.e(TAG, "Attempt to enable port forward not in list"); | |
756 return false; | |
757 } | |
758 | |
759 if (!authenticated) | |
760 return false; | |
761 | |
762 if (HostDatabase.PORTFORWARD_LOCAL.equals(portForward.getType())) { | |
763 LocalPortForwarder lpf = null; | |
764 | |
765 try { | |
766 lpf = connection.createLocalPortForwarder( | |
767 new InetSocketAddress(InetAddress.getLocalHost(), portForward.getSourcePort()), | |
768 portForward.getDestAddr(), portForward.getDestPort()); | |
769 } | |
770 catch (Exception e) { | |
771 Log.e(TAG, "Could not create local port forward", e); | |
772 return false; | |
773 } | |
774 | |
775 if (lpf == null) { | |
776 Log.e(TAG, "returned LocalPortForwarder object is null"); | |
777 return false; | |
778 } | |
779 | |
780 portForward.setIdentifier(lpf); | |
781 portForward.setEnabled(true); | |
782 return true; | |
783 } | |
784 else if (HostDatabase.PORTFORWARD_REMOTE.equals(portForward.getType())) { | |
785 try { | |
786 connection.requestRemotePortForwarding("", portForward.getSourcePort(), portForward.getDestAddr(), portForward.getDestPort()); | |
787 } | |
788 catch (Exception e) { | |
789 Log.e(TAG, "Could not create remote port forward", e); | |
790 return false; | |
791 } | |
792 | |
793 portForward.setEnabled(true); | |
794 return true; | |
795 } | |
796 else if (HostDatabase.PORTFORWARD_DYNAMIC5.equals(portForward.getType())) { | |
797 DynamicPortForwarder dpf = null; | |
798 | |
799 try { | |
800 dpf = connection.createDynamicPortForwarder( | |
801 new InetSocketAddress(InetAddress.getLocalHost(), portForward.getSourcePort())); | |
802 } | |
803 catch (Exception e) { | |
804 Log.e(TAG, "Could not create dynamic port forward", e); | |
805 return false; | |
806 } | |
807 | |
808 portForward.setIdentifier(dpf); | |
809 portForward.setEnabled(true); | |
810 return true; | |
811 } | |
812 else { | |
813 // Unsupported type | |
814 Log.e(TAG, String.format("attempt to forward unknown type %s", portForward.getType())); | |
815 return false; | |
816 } | |
817 } | |
818 | |
819 @Override | |
820 public boolean disablePortForward(PortForwardBean portForward) { | |
821 if (!portForwards.contains(portForward)) { | |
822 Log.e(TAG, "Attempt to disable port forward not in list"); | |
823 return false; | |
824 } | |
825 | |
826 if (!authenticated) | |
827 return false; | |
828 | |
829 if (HostDatabase.PORTFORWARD_LOCAL.equals(portForward.getType())) { | |
830 LocalPortForwarder lpf = null; | |
831 lpf = (LocalPortForwarder)portForward.getIdentifier(); | |
832 | |
833 if (!portForward.isEnabled() || lpf == null) { | |
834 Log.d(TAG, String.format("Could not disable %s; it appears to be not enabled or have no handler", portForward.getNickname())); | |
835 return false; | |
836 } | |
837 | |
838 portForward.setEnabled(false); | |
839 | |
840 try { | |
841 lpf.close(); | |
842 } | |
843 catch (IOException e) { | |
844 Log.e(TAG, "Could not stop local port forwarder, setting enabled to false", e); | |
845 return false; | |
846 } | |
847 | |
848 return true; | |
849 } | |
850 else if (HostDatabase.PORTFORWARD_REMOTE.equals(portForward.getType())) { | |
851 portForward.setEnabled(false); | |
852 | |
853 try { | |
854 connection.cancelRemotePortForwarding(portForward.getSourcePort()); | |
855 } | |
856 catch (IOException e) { | |
857 Log.e(TAG, "Could not stop remote port forwarding, setting enabled to false", e); | |
858 return false; | |
859 } | |
860 | |
861 return true; | |
862 } | |
863 else if (HostDatabase.PORTFORWARD_DYNAMIC5.equals(portForward.getType())) { | |
864 DynamicPortForwarder dpf = null; | |
865 dpf = (DynamicPortForwarder)portForward.getIdentifier(); | |
866 | |
867 if (!portForward.isEnabled() || dpf == null) { | |
868 Log.d(TAG, String.format("Could not disable %s; it appears to be not enabled or have no handler", portForward.getNickname())); | |
869 return false; | |
870 } | |
871 | |
872 portForward.setEnabled(false); | |
873 | |
874 try { | |
875 dpf.close(); | |
876 } | |
877 catch (IOException e) { | |
878 Log.e(TAG, "Could not stop dynamic port forwarder, setting enabled to false", e); | |
879 return false; | |
880 } | |
881 | |
882 return true; | |
883 } | |
884 else { | |
885 // Unsupported type | |
886 Log.e(TAG, String.format("attempt to forward unknown type %s", portForward.getType())); | |
887 return false; | |
888 } | |
889 } | |
890 | |
891 @Override | |
892 public boolean canTransferFiles() { | |
893 return true; | |
894 } | |
895 | |
896 @Override | |
897 public boolean downloadFile(String remoteFile, String localFolder) { | |
898 try { | |
899 SCPClient client = new SCPClient(connection); | |
900 | |
901 if (localFolder == null || localFolder == "") | |
902 localFolder = Environment.getExternalStorageDirectory().getAbsolutePath(); | |
903 | |
904 File dir = new File(localFolder); | |
905 dir.mkdirs(); | |
906 client.get(remoteFile, localFolder); | |
907 return true; | |
908 } | |
909 catch (IOException e) { | |
910 Log.e(TAG, "Could not download remote file", e); | |
911 return false; | |
912 } | |
913 } | |
914 | |
915 @Override | |
916 public boolean uploadFile(String localFile, String remoteFile, | |
917 String remoteFolder, String mode) { | |
918 try { | |
919 SCPClient client = new SCPClient(connection); | |
920 | |
921 if (remoteFolder == null) | |
922 remoteFolder = ""; | |
923 | |
924 if (remoteFile == null || remoteFile == "") | |
925 client.put(localFile, remoteFolder, mode); | |
926 else | |
927 client.put(localFile, remoteFile, remoteFolder, mode); | |
928 | |
929 return true; | |
930 } | |
931 catch (IOException e) { | |
932 Log.e(TAG, "Could not upload local file", e); | |
933 return false; | |
934 } | |
935 } | |
936 | |
937 | |
938 @Override | |
939 public int getDefaultPort() { | |
940 return DEFAULT_PORT; | |
941 } | |
942 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
943 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
944 @Override |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
945 public boolean isConnected() { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
946 return connected; |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
947 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
948 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
949 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
950 @Override |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
951 public boolean isSessionOpen() { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
952 return sessionOpen; |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
953 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
954 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
955 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
956 @Override |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
957 public boolean isAuthenticated() { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
958 return authenticated; |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
959 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
960 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
961 |
0 | 962 @Override |
963 public String getDefaultNickname(String username, String hostname, int port) { | |
964 if (port == DEFAULT_PORT) { | |
965 return String.format(Locale.US, "%s@%s", username, hostname); | |
966 } | |
967 else { | |
968 return String.format(Locale.US, "%s@%s:%d", username, hostname, port); | |
969 } | |
970 } | |
971 | |
972 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
973 @Override |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
974 public void getSelectionArgs(Uri uri, Map<String, String> selection) { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
975 selection.put(HostDatabase.FIELD_HOST_PROTOCOL, PROTOCOL); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
976 selection.put(HostDatabase.FIELD_HOST_NICKNAME, uri.getFragment()); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
977 selection.put(HostDatabase.FIELD_HOST_HOSTNAME, uri.getHost()); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
978 int port = uri.getPort(); |
0 | 979 |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
980 if (port < 0) |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
981 port = DEFAULT_PORT; |
0 | 982 |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
983 selection.put(HostDatabase.FIELD_HOST_PORT, Integer.toString(port)); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
984 selection.put(HostDatabase.FIELD_HOST_USERNAME, uri.getUserInfo()); |
0 | 985 } |
986 | |
987 | |
988 @Override | |
989 public HostBean createHost(Uri uri) { | |
990 HostBean host = new HostBean(); | |
991 host.setProtocol(PROTOCOL); | |
992 host.setHostname(uri.getHost()); | |
993 int port = uri.getPort(); | |
994 | |
995 if (port < 0) | |
996 port = DEFAULT_PORT; | |
997 | |
998 host.setPort(port); | |
999 host.setUsername(uri.getUserInfo()); | |
1000 String nickname = uri.getFragment(); | |
1001 | |
1002 if (nickname == null || nickname.length() == 0) { | |
1003 host.setNickname(getDefaultNickname(host.getUsername(), | |
1004 host.getHostname(), host.getPort())); | |
1005 } | |
1006 else { | |
1007 host.setNickname(uri.getFragment()); | |
1008 } | |
1009 | |
1010 return host; | |
1011 } | |
1012 | |
1013 | |
12 | 1014 public String getFormatHint(Context context) { |
0 | 1015 return String.format("%s@%s:%s", |
1016 context.getString(R.string.format_username), | |
1017 context.getString(R.string.format_hostname), | |
1018 context.getString(R.string.format_port)); | |
1019 } | |
1020 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1021 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1022 /** |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1023 * @return do we use the network |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1024 */ |
0 | 1025 @Override |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1026 public boolean usesNetwork() { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1027 return true; |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1028 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1029 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1030 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1031 /** |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1032 * Handle challenges from keyboard-interactive authentication mode. |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1033 */ |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1034 public String[] replyToChallenge(String name, String instruction, int numPrompts, String[] prompt, boolean[] echo) { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1035 interactiveCanContinue = true; |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1036 String[] responses = new String[numPrompts]; |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1037 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1038 for (int i = 0; i < numPrompts; i++) { |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1039 // request response from user for each prompt |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1040 responses[i] = bridge.promptHelper.requestPasswordPrompt(instruction, prompt[i]); |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1041 } |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1042 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
12
diff
changeset
|
1043 return responses; |
0 | 1044 } |
1045 | |
1046 public Map<String, byte[]> retrieveIdentities() { | |
1047 Map<String, byte[]> pubKeys = new HashMap<String, byte[]> (manager.loadedKeypairs.size()); | |
1048 | |
1049 for (Entry<String, KeyHolder> entry : manager.loadedKeypairs.entrySet()) { | |
1050 KeyPair pair = entry.getValue().pair; | |
1051 | |
1052 try { | |
1053 PrivateKey privKey = pair.getPrivate(); | |
1054 | |
1055 if (privKey instanceof RSAPrivateKey) { | |
1056 RSAPublicKey pubkey = (RSAPublicKey) pair.getPublic(); | |
1057 pubKeys.put(entry.getKey(), RSASHA1Verify.encodeSSHRSAPublicKey(pubkey)); | |
1058 } | |
1059 else if (privKey instanceof DSAPrivateKey) { | |
1060 DSAPublicKey pubkey = (DSAPublicKey) pair.getPublic(); | |
1061 pubKeys.put(entry.getKey(), DSASHA1Verify.encodeSSHDSAPublicKey(pubkey)); | |
1062 } | |
1063 else | |
1064 continue; | |
1065 } | |
1066 catch (IOException e) { | |
1067 continue; | |
1068 } | |
1069 } | |
1070 | |
1071 return pubKeys; | |
1072 } | |
1073 | |
1074 public KeyPair getKeyPair(byte[] publicKey) { | |
1075 String nickname = manager.getKeyNickname(publicKey); | |
1076 | |
1077 if (nickname == null) | |
1078 return null; | |
1079 | |
1080 if (useAuthAgent.equals(HostDatabase.AUTHAGENT_NO)) { | |
1081 Log.e(TAG, ""); | |
1082 return null; | |
1083 } | |
1084 else if (useAuthAgent.equals(HostDatabase.AUTHAGENT_CONFIRM) || | |
1085 manager.loadedKeypairs.get(nickname).bean.isConfirmUse()) { | |
1086 if (!promptForPubkeyUse(nickname)) | |
1087 return null; | |
1088 } | |
1089 | |
1090 return manager.getKey(nickname); | |
1091 } | |
1092 | |
1093 private boolean promptForPubkeyUse(String nickname) { | |
1094 Boolean result = bridge.promptHelper.requestBooleanPrompt(null, | |
1095 manager.res.getString(R.string.prompt_allow_agent_to_use_key, | |
1096 nickname)); | |
1097 return result; | |
1098 } | |
1099 | |
1100 public boolean addIdentity(KeyPair pair, String comment, boolean confirmUse, int lifetime) { | |
1101 PubkeyBean pubkey = new PubkeyBean(); | |
1102 // pubkey.setType(PubkeyDatabase.KEY_TYPE_IMPORTED); | |
1103 pubkey.setNickname(comment); | |
1104 pubkey.setConfirmUse(confirmUse); | |
1105 pubkey.setLifetime(lifetime); | |
1106 manager.addKey(pubkey, pair); | |
1107 return true; | |
1108 } | |
1109 | |
1110 public boolean removeAllIdentities() { | |
1111 manager.loadedKeypairs.clear(); | |
1112 return true; | |
1113 } | |
1114 | |
1115 public boolean removeIdentity(byte[] publicKey) { | |
1116 return manager.removeKey(publicKey); | |
1117 } | |
1118 | |
1119 public boolean isAgentLocked() { | |
1120 return agentLockPassphrase != null; | |
1121 } | |
1122 | |
1123 public boolean requestAgentUnlock(String unlockPassphrase) { | |
1124 if (agentLockPassphrase == null) | |
1125 return false; | |
1126 | |
1127 if (agentLockPassphrase.equals(unlockPassphrase)) | |
1128 agentLockPassphrase = null; | |
1129 | |
1130 return agentLockPassphrase == null; | |
1131 } | |
1132 | |
1133 public boolean setAgentLock(String lockPassphrase) { | |
1134 if (agentLockPassphrase != null) | |
1135 return false; | |
1136 | |
1137 agentLockPassphrase = lockPassphrase; | |
1138 return true; | |
1139 } | |
1140 | |
1141 } |