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 package com.mindtree.techworks.insight.download;
26
27 import java.awt.Component;
28 import java.io.File;
29 import java.text.MessageFormat;
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.Map;
33
34 import javax.swing.JOptionPane;
35
36 import com.mindtree.techworks.insight.InsightConstants;
37 import com.mindtree.techworks.insight.gui.widgets.StatusBar;
38 import com.sshtools.j2ssh.transport.AbstractKnownHostsKeyVerification;
39 import com.sshtools.j2ssh.transport.InvalidHostFileException;
40 import com.sshtools.j2ssh.transport.TransportProtocolException;
41 import com.sshtools.j2ssh.transport.publickey.SshPublicKey;
42
43
44
45
46
47
48
49
50
51
52
53 public class SshKnownHostKeyVerification extends
54 AbstractKnownHostsKeyVerification {
55
56
57
58
59
60
61
62
63 Component parentComponent;
64
65
66
67
68
69
70
71
72
73 private static Map sessionAllowedHostStore = new HashMap();
74
75
76
77
78
79
80
81
82
83
84 public SshKnownHostKeyVerification () throws InvalidHostFileException {
85
86 super(new File(System.getProperty("user.home"), ".ssh" + File.separator
87 + "known_hosts").getAbsolutePath());
88
89
90
91
92
93 if (!sessionAllowedHostStore.isEmpty()) {
94 synchronized (sessionAllowedHostStore) {
95 for (Iterator hosts = sessionAllowedHostStore.keySet()
96 .iterator(); hosts.hasNext();) {
97 String host = (String) hosts.next();
98 allowHost(host, (SshPublicKey) sessionAllowedHostStore
99 .get(host), false);
100 }
101 }
102 }
103 }
104
105
106
107
108
109
110
111
112 public SshKnownHostKeyVerification (Component parentComponent)
113 throws InvalidHostFileException {
114
115 this();
116 this.parentComponent = parentComponent;
117 }
118
119
120
121
122
123
124 public void onHostKeyMismatch (String host, SshPublicKey allowedHostKey,
125 SshPublicKey actualHostKey) throws TransportProtocolException {
126
127
128 StringBuffer message = new StringBuffer();
129 message.append(MessageFormat.format(InsightConstants
130 .getLiteral("SSH_KEY_MISMATCH_MSG_FORMAT"), new String [] {
131 host, actualHostKey.getFingerprint(),
132 allowedHostKey.getFingerprint() }));
133 getResponse(host, allowedHostKey, message);
134
135 }
136
137
138
139
140
141 public void onUnknownHost (String host, SshPublicKey key)
142 throws TransportProtocolException {
143
144
145 StringBuffer message = new StringBuffer();
146 message.append(MessageFormat.format(InsightConstants
147 .getLiteral("SSH_KEY_UNKNOWN_HOST_MSG_FORMAT"), new String [] {
148 host, key.getFingerprint() }));
149 getResponse(host, key, message);
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163 private void getResponse (String host, SshPublicKey pk, StringBuffer message)
164 throws TransportProtocolException {
165
166
167 String permYes = InsightConstants.getLiteral("SSH_KEY_ALLOW_YES");
168 String permNo = InsightConstants.getLiteral("SSH_KEY_ALLOW_NO");
169 String permAlways = InsightConstants.getLiteral("SSH_KEY_ALLOW_ALWAYS");
170
171 Object [] options = (isHostFileWriteable() ? new String [] { permYes,
172 permNo, permAlways } : new String [] { permYes, permNo });
173
174 if (!isHostFileWriteable()) {
175 message.append("\n");
176 message.append(InsightConstants
177 .getLiteral("SSH_KEY_HOST_FILE_NOT_WRITABLE"));
178 }
179
180 message.append("\n\n");
181 message.append(InsightConstants
182 .getLiteral("SSH_KEY_ALLOW_HOST_QUESTION"));
183
184
185
186 String response = (String) JOptionPane.showInputDialog(parentComponent,
187 message.toString(), InsightConstants
188 .getLiteral("SSH_KEY_DIALOG_TITLE"),
189 JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
190
191 if (null == response || response.equalsIgnoreCase(permNo)) {
192
193
194
195 StatusBar.getInstance().clearDisplay(1);
196 JOptionPane.showMessageDialog(parentComponent, InsightConstants
197 .getLiteral("SSH_KEY_CANNOT_CONTINUE"), InsightConstants
198 .getLiteral("ERROR"), JOptionPane.ERROR_MESSAGE);
199 throw new TransportProtocolException("Host not allowed!");
200 } else if (response.equalsIgnoreCase(permYes)) {
201 allowHost(host, pk, false);
202
203
204
205
206 synchronized (sessionAllowedHostStore) {
207 sessionAllowedHostStore.put(host, pk);
208 }
209 } else if (response.equalsIgnoreCase(permAlways)) {
210 if (isHostFileWriteable()) {
211 allowHost(host, pk, true);
212 } else {
213 allowHost(host, pk, false);
214 }
215
216 }
217 }
218
219 }