1
2
3
4
5
6
7
8
9
10 package org.gwe.drivers.netAccess.handles;
11
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Properties;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.gwe.drivers.netAccess.ConnectorException;
21 import org.gwe.drivers.netAccess.HostHandle;
22 import org.gwe.utils.security.AccessControl;
23 import org.gwe.utils.security.AccountInfo;
24 import org.gwe.utils.security.KeyStore;
25 import org.gwe.utils.security.Realm;
26 import org.gwe.utils.security.ResourceLink;
27
28 import com.jcraft.jsch.JSch;
29 import com.jcraft.jsch.JSchException;
30 import com.jcraft.jsch.Session;
31
32
33
34
35
36 public class JSchConnection {
37
38 private static Log log = LogFactory.getLog(JSchConnection.class);
39
40 public static List<Exception> test(KeyStore keys) {
41 List<Exception> result = new ArrayList<Exception>();
42 for (AccessControl ac : keys.getAccessControls()) {
43 for (Realm realm : ac.getRealms()) {
44 try {
45
46 } catch (Exception e) {
47 result.add(e);
48 }
49 }
50 }
51 return result;
52 }
53
54 private ResourceLink<HostHandle> link;
55 private JSch jschObj;
56 private Session sessionObj;
57
58 public JSchConnection(ResourceLink<HostHandle> link) throws JSchException, UnknownHostException {
59 this.link = link;
60 AccountInfo acct = link.getAccountInfo();
61 log.debug("Creating ssh connector for " + acct.getUser() + "@" + link.getURI());
62
63 jschObj = createJSchObject(acct.getPrivateKey(), acct.getPublicKey(), acct.getPassphrase());
64 createJSchSession();
65 }
66
67 private JSch createJSchObject(byte[] privateKey, byte[] publicKey, String passphrase) throws JSchException {
68 JSch result = new JSch();
69
70 result.setKnownHosts(System.getProperty("user.home") + "/.ssh/known_hosts");
71 if (privateKey != null) result.addIdentity("byte-array", privateKey, publicKey, passphrase.getBytes());
72 return result;
73 }
74
75 private void createJSchSession() throws JSchException, UnknownHostException {
76 AccountInfo acct = link.getAccountInfo();
77 String host = (link != null && link.getURI() != null) ? link.getURI().getHost() : InetAddress.getLocalHost().getHostName();
78 log.info("Connecting to " + acct.getUser() + "@" + host);
79 sessionObj = jschObj.getSession(acct.getUser(), host);
80
81
82 Properties config = new Properties();
83 config.setProperty("StrictHostKeyChecking", "ask");
84 sessionObj.setConfig(config);
85
86
87
88 sessionObj.setUserInfo(new JSchUserInfo(acct));
89 sessionObj.connect(30000);
90 log.info("Successfully connected");
91 }
92
93 public JSch getJschObj() {
94 return jschObj;
95 }
96
97 public Session getSessionObj() {
98 return sessionObj;
99 }
100
101 public void close() throws ConnectorException {
102 if (sessionObj != null) {
103 log.info("Closing SSH connection to " + link);
104 sessionObj.disconnect();
105 sessionObj = null;
106 }
107 }
108
109 @Override
110 protected void finalize() throws Throwable {
111 close();
112 }
113 }