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 package org.eclipse.jgit.transport;
45
46 import static org.eclipse.jgit.internal.transport.ssh.OpenSshConfigFile.positive;
47
48 import java.io.File;
49 import java.util.List;
50 import java.util.Map;
51 import java.util.TreeMap;
52
53 import org.eclipse.jgit.internal.transport.ssh.OpenSshConfigFile;
54 import org.eclipse.jgit.internal.transport.ssh.OpenSshConfigFile.HostEntry;
55 import org.eclipse.jgit.util.FS;
56
57 import com.jcraft.jsch.ConfigRepository;
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 public class OpenSshConfig implements ConfigRepository {
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 public static OpenSshConfig get(FS fs) {
100 File home = fs.userHome();
101 if (home == null)
102 home = new File(".").getAbsoluteFile();
103
104 final File config = new File(new File(home, SshConstants.SSH_DIR),
105 SshConstants.CONFIG);
106 return new OpenSshConfig(home, config);
107 }
108
109
110 private OpenSshConfigFile configFile;
111
112 OpenSshConfig(File h, File cfg) {
113 configFile = new OpenSshConfigFile(h, cfg,
114 SshSessionFactory.getLocalUserName());
115 }
116
117
118
119
120
121
122
123
124
125
126 public Host lookup(String hostName) {
127 HostEntry entry = configFile.lookup(hostName, -1, null);
128 return new Host(entry, hostName, configFile.getLocalUserName());
129 }
130
131
132
133
134
135
136
137
138
139
140
141
142 public static class Host {
143 String hostName;
144
145 int port;
146
147 File identityFile;
148
149 String user;
150
151 String preferredAuthentications;
152
153 Boolean batchMode;
154
155 String strictHostKeyChecking;
156
157 int connectionAttempts;
158
159 private HostEntry entry;
160
161 private Config config;
162
163
164
165 private static final Map<String, String> KEY_MAP = new TreeMap<>(
166 String.CASE_INSENSITIVE_ORDER);
167
168 static {
169 KEY_MAP.put("kex", SshConstants.KEX_ALGORITHMS);
170 KEY_MAP.put("server_host_key", SshConstants.HOST_KEY_ALGORITHMS);
171 KEY_MAP.put("cipher.c2s", SshConstants.CIPHERS);
172 KEY_MAP.put("cipher.s2c", SshConstants.CIPHERS);
173 KEY_MAP.put("mac.c2s", SshConstants.MACS);
174 KEY_MAP.put("mac.s2c", SshConstants.MACS);
175 KEY_MAP.put("compression.s2c", SshConstants.COMPRESSION);
176 KEY_MAP.put("compression.c2s", SshConstants.COMPRESSION);
177 KEY_MAP.put("compression_level", "CompressionLevel");
178 KEY_MAP.put("MaxAuthTries",
179 SshConstants.NUMBER_OF_PASSWORD_PROMPTS);
180 }
181
182 private static String mapKey(String key) {
183 String k = KEY_MAP.get(key);
184 return k != null ? k : key;
185 }
186
187
188
189
190 public Host() {
191
192 }
193
194 Host(HostEntry entry, String hostName, String localUserName) {
195 this.entry = entry;
196 complete(hostName, localUserName);
197 }
198
199
200
201
202
203
204
205 public String getStrictHostKeyChecking() {
206 return strictHostKeyChecking;
207 }
208
209
210
211
212 public String getHostName() {
213 return hostName;
214 }
215
216
217
218
219 public int getPort() {
220 return port;
221 }
222
223
224
225
226
227 public File getIdentityFile() {
228 return identityFile;
229 }
230
231
232
233
234 public String getUser() {
235 return user;
236 }
237
238
239
240
241
242 public String getPreferredAuthentications() {
243 return preferredAuthentications;
244 }
245
246
247
248
249
250 public boolean isBatchMode() {
251 return batchMode != null && batchMode.booleanValue();
252 }
253
254
255
256
257
258
259
260
261 public int getConnectionAttempts() {
262 return connectionAttempts;
263 }
264
265
266 private void complete(String initialHostName, String localUserName) {
267
268 hostName = entry.getValue(SshConstants.HOST_NAME);
269 user = entry.getValue(SshConstants.USER);
270 port = positive(entry.getValue(SshConstants.PORT));
271 connectionAttempts = positive(
272 entry.getValue(SshConstants.CONNECTION_ATTEMPTS));
273 strictHostKeyChecking = entry
274 .getValue(SshConstants.STRICT_HOST_KEY_CHECKING);
275 batchMode = Boolean.valueOf(OpenSshConfigFile
276 .flag(entry.getValue(SshConstants.BATCH_MODE)));
277 preferredAuthentications = entry
278 .getValue(SshConstants.PREFERRED_AUTHENTICATIONS);
279
280 if (hostName == null || hostName.isEmpty()) {
281 hostName = initialHostName;
282 }
283 if (user == null || user.isEmpty()) {
284 user = localUserName;
285 }
286 if (port <= 0) {
287 port = SshConstants.SSH_DEFAULT_PORT;
288 }
289 if (connectionAttempts <= 0) {
290 connectionAttempts = 1;
291 }
292 List<String> identityFiles = entry
293 .getValues(SshConstants.IDENTITY_FILE);
294 if (identityFiles != null && !identityFiles.isEmpty()) {
295 identityFile = new File(identityFiles.get(0));
296 }
297 }
298
299 Config getConfig() {
300 if (config == null) {
301 config = new Config() {
302
303 @Override
304 public String getHostname() {
305 return Host.this.getHostName();
306 }
307
308 @Override
309 public String getUser() {
310 return Host.this.getUser();
311 }
312
313 @Override
314 public int getPort() {
315 return Host.this.getPort();
316 }
317
318 @Override
319 public String getValue(String key) {
320
321
322 if (key.equals("compression.s2c")
323 || key.equals("compression.c2s")) {
324 if (!OpenSshConfigFile.flag(
325 Host.this.entry.getValue(mapKey(key)))) {
326 return "none,zlib@openssh.com,zlib";
327 }
328 return "zlib@openssh.com,zlib,none";
329 }
330 return Host.this.entry.getValue(mapKey(key));
331 }
332
333 @Override
334 public String[] getValues(String key) {
335 List<String> values = Host.this.entry
336 .getValues(mapKey(key));
337 if (values == null) {
338 return new String[0];
339 }
340 return values.toArray(new String[0]);
341 }
342 };
343 }
344 return config;
345 }
346
347 @Override
348 @SuppressWarnings("nls")
349 public String toString() {
350 return "Host [hostName=" + hostName + ", port=" + port
351 + ", identityFile=" + identityFile + ", user=" + user
352 + ", preferredAuthentications=" + preferredAuthentications
353 + ", batchMode=" + batchMode + ", strictHostKeyChecking="
354 + strictHostKeyChecking + ", connectionAttempts="
355 + connectionAttempts + ", entry=" + entry + "]";
356 }
357 }
358
359
360
361
362
363
364
365
366
367 @Override
368 public Config getConfig(String hostName) {
369 Host host = lookup(hostName);
370 return host.getConfig();
371 }
372
373
374 @Override
375 public String toString() {
376 return "OpenSshConfig [configFile=" + configFile + ']';
377 }
378 }