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