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