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