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
45
46
47
48
49
50
51
52
53
54
55
56
57
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
|
/*
* Copyright (C) 2008, 2017, Google Inc.
* Copyright (C) 2017, 2021, Thomas Wolf <thomas.wolf@paranor.ch> and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.internal.transport.ssh;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.errors.InvalidPatternException;
import org.eclipse.jgit.fnmatch.FileNameMatcher;
import org.eclipse.jgit.transport.SshConfigStore;
import org.eclipse.jgit.transport.SshConstants;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.StringUtils;
import org.eclipse.jgit.util.SystemReader;
/**
* Fairly complete configuration parser for the openssh ~/.ssh/config file.
* <p>
* Both JSch 0.1.54 and Apache MINA sshd 2.1.0 have parsers for this, but both
* are buggy. Therefore we implement our own parser to read an openssh
* configuration file.
* </p>
* <p>
* Limitations compared to the full openssh 7.5 parser:
* </p>
* <ul>
* <li>This parser does not handle Match or Include keywords.
* <li>This parser does not do host name canonicalization.
* </ul>
* <p>
* Note that openssh's readconf.c is a validating parser; this parser does not
* validate entries.
* </p>
* <p>
* This config does %-substitutions for the following tokens:
* </p>
* <ul>
* <li>%% - single %
* <li>%C - short-hand for %l%h%p%r.
* <li>%d - home directory path
* <li>%h - remote host name
* <li>%L - local host name without domain
* <li>%l - FQDN of the local host
* <li>%n - host name as specified in {@link #lookup(String, int, String)}
* <li>%p - port number; if not given in {@link #lookup(String, int, String)}
* replaced only if set in the config
* <li>%r - remote user name; if not given in
* {@link #lookup(String, int, String)} replaced only if set in the config
* <li>%u - local user name
* </ul>
* <p>
* %i is not handled; Java has no concept of a "user ID". %T is always replaced
* by NONE.
* </p>
*
* @see <a href="http://man.openbsd.org/OpenBSD-current/man5/ssh_config.5">man
* ssh-config</a>
*/
public class OpenSshConfigFile implements SshConfigStore {
/** The user's home directory, as key files may be relative to here. */
private final File home;
/** The .ssh/config file we read and monitor for updates. */
private final File configFile;
/** User name of the user on the host OS. */
private final String localUserName;
/** Modification time of {@link #configFile} when it was last loaded. */
private Instant lastModified;
/**
* Encapsulates entries read out of the configuration file, and a cache of
* fully resolved entries created from that.
*/
private static class State {
List<HostEntry> entries = new ArrayList<>();
// Previous lookups, keyed by user@hostname:port
Map<String, HostEntry> hosts = new HashMap<>();
@Override
@SuppressWarnings("nls")
public String toString() {
return "State [entries=" + entries + ", hosts=" + hosts + "]";
}
}
/** State read from the config file, plus the cache. */
private State state;
/**
* Creates a new {@link OpenSshConfigFile} that will read the config from
* file {@code config} use the given file {@code home} as "home" directory.
*
* @param home
* user's home directory for the purpose of ~ replacement
* @param config
* file to load.
* @param localUserName
* user name of the current user on the local host OS
*/
public OpenSshConfigFile(@NonNull File home, @NonNull File config,
@NonNull String localUserName) {
this.home = home;
this.configFile = config;
this.localUserName = localUserName;
state = new State();
}
/**
* Locate the configuration for a specific host request.
*
* @param hostName
* the name the user has supplied to the SSH tool. This may be a
* real host name, or it may just be a "Host" block in the
* configuration file.
* @param port
* the user supplied; <= 0 if none
* @param userName
* the user supplied, may be {@code null} or empty if none given
* @return the configuration for the requested name.
*/
@Override
@NonNull
public HostEntry lookup(@NonNull String hostName, int port,
String userName) {
return lookup(hostName, port, userName, false);
}
@Override
@NonNull
public HostEntry lookupDefault(@NonNull String hostName, int port,
String userName) {
return lookup(hostName, port, userName, true);
}
private HostEntry lookup(@NonNull String hostName, int port,
String userName, boolean fillDefaults) {
final State cache = refresh();
String cacheKey = toCacheKey(hostName, port, userName);
HostEntry h = cache.hosts.get(cacheKey);
if (h != null) {
return h;
}
HostEntry fullConfig = new HostEntry();
Iterator<HostEntry> entries = cache.entries.iterator();
if (entries.hasNext()) {
// Should always have at least the first top entry containing
// key-value pairs before the first Host block
fullConfig.merge(entries.next());
entries.forEachRemaining(entry -> {
if (entry.matches(hostName)) {
fullConfig.merge(entry);
}
});
}
fullConfig.substitute(hostName, port, userName, localUserName, home,
fillDefaults);
cache.hosts.put(cacheKey, fullConfig);
return fullConfig;
}
@NonNull
private String toCacheKey(@NonNull String hostName, int port,
String userName) {
String key = hostName;
if (port > 0) {
key = key + ':' + Integer.toString(port);
}
if (userName != null && !userName.isEmpty()) {
key = userName + '@' + key;
}
return key;
}
private synchronized State refresh() {
final Instant mtime = FS.DETECTED.lastModifiedInstant(configFile);
if (!mtime.equals(lastModified)) {
State newState = new State();
try (BufferedReader br = Files
.newBufferedReader(configFile.toPath(), UTF_8)) {
newState.entries = parse(br);
} catch (IOException | RuntimeException none) {
// Ignore -- we'll set and return an empty state
}
lastModified = mtime;
state = newState;
}
return state;
}
private List<HostEntry> parse(BufferedReader reader)
throws IOException {
final List<HostEntry> entries = new ArrayList<>();
// The man page doesn't say so, but the openssh parser (readconf.c)
// starts out in active mode and thus always applies any lines that
// occur before the first host block. We gather those options in a
// HostEntry.
HostEntry defaults = new HostEntry();
HostEntry current = defaults;
entries.add(defaults);
String line;
while ((line = reader.readLine()) != null) {
line = line.strip();
if (line.isEmpty()) {
continue;
}
String[] parts = line.split("[ \t]*[= \t]", 2); //$NON-NLS-1$
String keyword = parts[0].strip();
if (keyword.isEmpty()) {
continue;
}
switch (keyword.charAt(0)) {
case '#':
continue;
case '"':
// Although the ssh-config man page doesn't say so, the openssh
// parser does allow quoted keywords.
List<String> dequoted = parseList(keyword);
keyword = dequoted.isEmpty() ? "" : dequoted.get(0); //$NON-NLS-1$
break;
default:
// Keywords never contain hashes, nor whitespace
int i = keyword.indexOf('#');
if (i >= 0) {
keyword = keyword.substring(0, i);
}
break;
}
if (keyword.isEmpty()) {
continue;
}
// man 5 ssh-config says lines had the format "keyword arguments",
// with no indication that arguments were optional. However, let's
// not crap out on missing arguments. See bug 444319.
String argValue = parts.length > 1 ? parts[1].strip() : ""; //$NON-NLS-1$
if (StringUtils.equalsIgnoreCase(SshConstants.HOST, keyword)) {
current = new HostEntry(parseList(argValue));
entries.add(current);
continue;
}
if (HostEntry.isListKey(keyword)) {
List<String> args = validate(keyword, parseList(argValue));
current.setValue(keyword, args);
} else if (!argValue.isEmpty()) {
List<String> args = parseList(argValue);
String arg = args.isEmpty() ? "" : args.get(0); //$NON-NLS-1$
argValue = validate(keyword, arg);
current.setValue(keyword, argValue);
}
}
return entries;
}
/**
* Splits the argument into a list of whitespace-separated elements.
* Elements containing whitespace must be quoted and will be de-quoted.
* Backslash-escapes are handled for quotes and blanks.
*
* @param argument
* argument part of the configuration line as read from the
* config file
* @return a {@link List} of elements, possibly empty and possibly
* containing empty elements, but not containing {@code null}
*/
private static List<String> parseList(String argument) {
List<String> result = new ArrayList<>(4);
int start = 0;
int length = argument.length();
while (start < length) {
// Skip whitespace
char ch = argument.charAt(start);
if (Character.isWhitespace(ch)) {
start++;
} else if (ch == '#') {
break; // Comment start
} else {
// Parse one token now.
start = parseToken(argument, start, length, result);
}
}
return result;
}
/**
* Parses a token up to the next whitespace not inside a string quoted by
* single or double quotes. Inside a string, quotes can be escaped by
* backslash characters. Outside of a string, "\ " can be used to include a
* space in a token; inside a string "\ " is taken literally as '\' followed
* by ' '.
*
* @param argument
* to parse the token out of
* @param from
* index at the beginning of the token
* @param to
* index one after the last character to look at
* @param result
* a list collecting tokens to which the parsed token is added
* @return the index after the token
*/
private static int parseToken(String argument, int from, int to,
List<String> result) {
StringBuilder b = new StringBuilder();
int i = from;
char quote = 0;
boolean escaped = false;
SCAN: while (i < to) {
char ch = argument.charAt(i);
switch (ch) {
case '"':
case '\'':
if (quote == 0) {
if (escaped) {
b.append(ch);
} else {
quote = ch;
}
} else if (!escaped && quote == ch) {
quote = 0;
} else {
b.append(ch);
}
escaped = false;
break;
case '\\':
if (escaped) {
b.append(ch);
}
escaped = !escaped;
break;
case ' ':
if (quote == 0) {
if (escaped) {
b.append(ch);
escaped = false;
} else {
break SCAN;
}
} else {
if (escaped) {
b.append('\\');
}
b.append(ch);
escaped = false;
}
break;
default:
if (escaped) {
b.append('\\');
}
if (quote == 0 && Character.isWhitespace(ch)) {
break SCAN;
}
b.append(ch);
escaped = false;
break;
}
i++;
}
if (b.length() > 0) {
result.add(b.toString());
}
return i;
}
/**
* Hook to perform validation on a single value, or to sanitize it. If this
* throws an (unchecked) exception, parsing of the file is abandoned.
*
* @param key
* of the entry
* @param value
* as read from the config file
* @return the validated and possibly sanitized value
*/
protected String validate(String key, String value) {
if (SshConstants.PREFERRED_AUTHENTICATIONS.equalsIgnoreCase(key)) {
return stripWhitespace(value);
}
return value;
}
/**
* Hook to perform validation on values, or to sanitize them. If this throws
* an (unchecked) exception, parsing of the file is abandoned.
*
* @param key
* of the entry
* @param value
* list of arguments as read from the config file
* @return a {@link List} of values, possibly empty and possibly containing
* empty elements, but not containing {@code null}
*/
protected List<String> validate(String key, List<String> value) {
return value;
}
/**
* Tells whether a given {@code name} matches the given list of patterns,
* accounting for negative matches.
*
* @param patterns
* to test {@code name} against; any pattern starting with an
* exclamation mark is a negative pattern
* @param name
* to test
* @return {@code true} if the {@code name} matches at least one of the
* non-negative patterns and none of the negative patterns,
* {@code false} otherwise
* @since 7.1
*/
public static boolean patternMatch(Iterable<String> patterns, String name) {
boolean doesMatch = false;
for (String pattern : patterns) {
if (pattern.startsWith("!")) { //$NON-NLS-1$
if (patternMatches(pattern.substring(1), name)) {
return false;
}
} else if (!doesMatch && patternMatches(pattern, name)) {
doesMatch = true;
}
}
return doesMatch;
}
private static boolean patternMatches(String pattern, String name) {
if (pattern.indexOf('*') >= 0 || pattern.indexOf('?') >= 0) {
final FileNameMatcher fn;
try {
fn = new FileNameMatcher(pattern, null);
} catch (InvalidPatternException e) {
return false;
}
fn.append(name);
return fn.isMatch();
}
// Not a pattern but a full host name
return pattern.equals(name);
}
private static String stripWhitespace(String value) {
final StringBuilder b = new StringBuilder();
int length = value.length();
for (int i = 0; i < length; i++) {
char ch = value.charAt(i);
if (!Character.isWhitespace(ch)) {
b.append(ch);
}
}
return b.toString();
}
private static File toFile(String path, File home) {
if (path.startsWith("~/") || path.startsWith("~" + File.separator)) { //$NON-NLS-1$ //$NON-NLS-2$
return new File(home, path.substring(2));
}
File ret = new File(path);
if (ret.isAbsolute()) {
return ret;
}
return new File(home, path);
}
/**
* Converts a positive value into an {@code int}.
*
* @param value
* to convert
* @return the value, or -1 if it wasn't a positive integral value
*/
public static int positive(String value) {
if (value != null) {
try {
return Integer.parseUnsignedInt(value);
} catch (NumberFormatException e) {
// Ignore
}
}
return -1;
}
/**
* Converts a ssh config flag value (yes/true/on - no/false/off) into an
* {@code boolean}.
*
* @param value
* to convert
* @return {@code true} if {@code value} is "yes", "on", or "true";
* {@code false} otherwise
*/
public static boolean flag(String value) {
if (value == null) {
return false;
}
return SshConstants.YES.equals(value) || SshConstants.ON.equals(value)
|| SshConstants.TRUE.equals(value);
}
/**
* Converts an OpenSSH time value into a number of seconds. The format is
* defined by OpenSSH as a sequence of (positive) integers with suffixes for
* seconds, minutes, hours, days, and weeks.
*
* @param value
* to convert
* @return the parsed value as a number of seconds, or -1 if the value is
* not a valid OpenSSH time value
* @see <a href="https://man.openbsd.org/sshd_config.5#TIME_FORMATS">OpenBSD
* man 5 sshd_config, section TIME FORMATS</a>
*/
public static int timeSpec(String value) {
if (value == null) {
return -1;
}
try {
int length = value.length();
int i = 0;
int seconds = 0;
boolean valueSeen = false;
while (i < length) {
// Skip whitespace
char ch = value.charAt(i);
if (Character.isWhitespace(ch)) {
i++;
continue;
}
if (ch == '+') {
// OpenSSH uses strtol with base 10: a leading plus sign is
// allowed.
i++;
}
int val = 0;
int j = i;
while (j < length) {
ch = value.charAt(j++);
if (ch >= '0' && ch <= '9') {
val = Math.addExact(Math.multiplyExact(val, 10),
ch - '0');
} else {
j--;
break;
}
}
if (i == j) {
// No digits seen
return -1;
}
i = j;
int multiplier = 1;
if (i < length) {
ch = value.charAt(i++);
switch (ch) {
case 's':
case 'S':
break;
case 'm':
case 'M':
multiplier = 60;
break;
case 'h':
case 'H':
multiplier = 3600;
break;
case 'd':
case 'D':
multiplier = 24 * 3600;
break;
case 'w':
case 'W':
multiplier = 7 * 24 * 3600;
break;
default:
if (Character.isWhitespace(ch)) {
break;
}
// Invalid time spec
return -1;
}
}
seconds = Math.addExact(seconds,
Math.multiplyExact(val, multiplier));
valueSeen = true;
}
return valueSeen ? seconds : -1;
} catch (ArithmeticException e) {
// Overflow
return -1;
}
}
/**
* Retrieves the local user name as given in the constructor.
*
* @return the user name
*/
public String getLocalUserName() {
return localUserName;
}
/**
* A host entry from the ssh config file. Any merging of global values and
* of several matching host entries, %-substitutions, and ~ replacement have
* all been done.
*/
public static class HostEntry implements SshConfigStore.HostConfig {
/**
* Keys that can be specified multiple times, building up a list. (I.e.,
* those are the keys that do not follow the general rule of "first
* occurrence wins".)
*/
private static final Set<String> MULTI_KEYS = new TreeSet<>(
String.CASE_INSENSITIVE_ORDER);
static {
MULTI_KEYS.add(SshConstants.CERTIFICATE_FILE);
MULTI_KEYS.add(SshConstants.IDENTITY_FILE);
MULTI_KEYS.add(SshConstants.LOCAL_FORWARD);
MULTI_KEYS.add(SshConstants.REMOTE_FORWARD);
MULTI_KEYS.add(SshConstants.SEND_ENV);
}
/**
* Keys that take a whitespace-separated list of elements as argument.
* Because the dequote-handling is different, we must handle those in
* the parser. There are a few other keys that take comma-separated
* lists as arguments, but for the parser those are single arguments
* that must be quoted if they contain whitespace, and taking them apart
* is the responsibility of the user of those keys.
*/
private static final Set<String> LIST_KEYS = new TreeSet<>(
String.CASE_INSENSITIVE_ORDER);
static {
LIST_KEYS.add(SshConstants.CANONICAL_DOMAINS);
LIST_KEYS.add(SshConstants.GLOBAL_KNOWN_HOSTS_FILE);
LIST_KEYS.add(SshConstants.SEND_ENV);
LIST_KEYS.add(SshConstants.USER_KNOWN_HOSTS_FILE);
LIST_KEYS.add(SshConstants.ADD_KEYS_TO_AGENT); // confirm timeSpec
}
/**
* OpenSSH has renamed some config keys. This maps old names to new
* names.
*/
private static final Map<String, String> ALIASES = new TreeMap<>(
String.CASE_INSENSITIVE_ORDER);
static {
// See https://github.com/openssh/openssh-portable/commit/ee9c0da80
ALIASES.put("PubkeyAcceptedKeyTypes", //$NON-NLS-1$
SshConstants.PUBKEY_ACCEPTED_ALGORITHMS);
}
private Map<String, String> options;
private Map<String, List<String>> multiOptions;
private Map<String, List<String>> listOptions;
private final List<String> patterns;
/**
* Constructor used to build the merged entry; never matches anything
*/
public HostEntry() {
this.patterns = Collections.emptyList();
}
/**
* @param patterns
* to be used in matching against host name.
*/
public HostEntry(List<String> patterns) {
this.patterns = patterns;
}
boolean matches(String hostName) {
return patternMatch(patterns, hostName);
}
private static String toKey(String key) {
String k = ALIASES.get(key);
return k != null ? k : key;
}
/**
* Retrieves the value of a single-valued key, or the first if the key
* has multiple values. Keys are case-insensitive, so
* {@code getValue("HostName") == getValue("HOSTNAME")}.
*
* @param key
* to get the value of
* @return the value, or {@code null} if none
*/
@Override
public String getValue(String key) {
String k = toKey(key);
String result = options != null ? options.get(k) : null;
if (result == null) {
// Let's be lenient and return at least the first value from
// a list-valued or multi-valued key.
List<String> values = listOptions != null ? listOptions.get(k)
: null;
if (values == null) {
values = multiOptions != null ? multiOptions.get(k) : null;
}
if (values != null && !values.isEmpty()) {
result = values.get(0);
}
}
return result;
}
/**
* Retrieves the values of a multi or list-valued key. Keys are
* case-insensitive, so
* {@code getValue("HostName") == getValue("HOSTNAME")}.
*
* @param key
* to get the values of
* @return a possibly empty list of values
*/
@Override
public List<String> getValues(String key) {
String k = toKey(key);
List<String> values = listOptions != null ? listOptions.get(k)
: null;
if (values == null) {
values = multiOptions != null ? multiOptions.get(k) : null;
}
if (values == null || values.isEmpty()) {
return new ArrayList<>();
}
return new ArrayList<>(values);
}
/**
* Sets the value of a single-valued key if it not set yet, or adds a
* value to a multi-valued key. If the value is {@code null}, the key is
* removed altogether, whether it is single-, list-, or multi-valued.
*
* @param key
* to modify
* @param value
* to set or add
*/
public void setValue(String key, String value) {
String k = toKey(key);
if (value == null) {
if (multiOptions != null) {
multiOptions.remove(k);
}
if (listOptions != null) {
listOptions.remove(k);
}
if (options != null) {
options.remove(k);
}
return;
}
if (MULTI_KEYS.contains(k)) {
if (multiOptions == null) {
multiOptions = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
}
List<String> values = multiOptions.get(k);
if (values == null) {
values = new ArrayList<>(4);
multiOptions.put(k, values);
}
values.add(value);
} else {
if (options == null) {
options = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
}
if (!options.containsKey(k)) {
options.put(k, value);
}
}
}
/**
* Sets the values of a multi- or list-valued key.
*
* @param key
* to set
* @param values
* a non-empty list of values
*/
public void setValue(String key, List<String> values) {
if (values.isEmpty()) {
return;
}
String k = toKey(key);
// Check multi-valued keys first; because of the replacement
// strategy, they must take precedence over list-valued keys
// which always follow the "first occurrence wins" strategy.
//
// Note that SendEnv is a multi-valued list-valued key. (It's
// rather immaterial for JGit, though.)
if (MULTI_KEYS.contains(k)) {
if (multiOptions == null) {
multiOptions = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
}
List<String> items = multiOptions.get(k);
if (items == null) {
items = new ArrayList<>(values);
multiOptions.put(k, items);
} else {
items.addAll(values);
}
} else {
if (listOptions == null) {
listOptions = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
}
if (!listOptions.containsKey(k)) {
listOptions.put(k, values);
}
}
}
/**
* Does the key take a whitespace-separated list of values?
*
* @param key
* to check
* @return {@code true} if the key is a list-valued key.
*/
public static boolean isListKey(String key) {
return LIST_KEYS.contains(toKey(key));
}
void merge(HostEntry entry) {
if (entry == null) {
// Can occur if we could not read the config file
return;
}
if (entry.options != null) {
if (options == null) {
options = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
}
for (Map.Entry<String, String> item : entry.options
.entrySet()) {
if (!options.containsKey(item.getKey())) {
options.put(item.getKey(), item.getValue());
}
}
}
if (entry.listOptions != null) {
if (listOptions == null) {
listOptions = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
}
for (Map.Entry<String, List<String>> item : entry.listOptions
.entrySet()) {
if (!listOptions.containsKey(item.getKey())) {
listOptions.put(item.getKey(), item.getValue());
}
}
}
if (entry.multiOptions != null) {
if (multiOptions == null) {
multiOptions = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
}
for (Map.Entry<String, List<String>> item : entry.multiOptions
.entrySet()) {
List<String> values = multiOptions.get(item.getKey());
if (values == null) {
values = new ArrayList<>(item.getValue());
multiOptions.put(item.getKey(), values);
} else {
values.addAll(item.getValue());
}
}
}
}
private List<String> substitute(List<String> values, String allowed,
Replacer r, boolean withEnv) {
List<String> result = new ArrayList<>(values.size());
for (String value : values) {
result.add(r.substitute(value, allowed, withEnv));
}
return result;
}
private List<String> replaceTilde(List<String> values, File home) {
List<String> result = new ArrayList<>(values.size());
for (String value : values) {
result.add(toFile(value, home).getPath());
}
return result;
}
void substitute(String originalHostName, int port, String userName,
String localUserName, File home, boolean fillDefaults) {
int p = port > 0 ? port : positive(getValue(SshConstants.PORT));
if (p <= 0) {
p = SshConstants.SSH_DEFAULT_PORT;
}
String u = !StringUtils.isEmptyOrNull(userName) ? userName
: getValue(SshConstants.USER);
if (u == null || u.isEmpty()) {
u = localUserName;
}
Replacer r = new Replacer(originalHostName, p, u, localUserName,
home);
if (options != null) {
// HOSTNAME first
String hostName = options.get(SshConstants.HOST_NAME);
if (hostName == null || hostName.isEmpty()) {
options.put(SshConstants.HOST_NAME, originalHostName);
} else {
hostName = r.substitute(hostName, "h", false); //$NON-NLS-1$
options.put(SshConstants.HOST_NAME, hostName);
r.update('h', hostName);
}
} else if (fillDefaults) {
setValue(SshConstants.HOST_NAME, originalHostName);
}
if (multiOptions != null) {
List<String> values = multiOptions
.get(SshConstants.IDENTITY_FILE);
if (values != null) {
values = substitute(values, Replacer.DEFAULT_TOKENS, r,
true);
values = replaceTilde(values, home);
multiOptions.put(SshConstants.IDENTITY_FILE, values);
}
values = multiOptions.get(SshConstants.CERTIFICATE_FILE);
if (values != null) {
values = substitute(values, Replacer.DEFAULT_TOKENS, r,
true);
values = replaceTilde(values, home);
multiOptions.put(SshConstants.CERTIFICATE_FILE, values);
}
}
if (listOptions != null) {
List<String> values = listOptions
.get(SshConstants.USER_KNOWN_HOSTS_FILE);
if (values != null) {
values = substitute(values, Replacer.DEFAULT_TOKENS, r,
true);
values = replaceTilde(values, home);
listOptions.put(SshConstants.USER_KNOWN_HOSTS_FILE, values);
}
}
if (options != null) {
// HOSTNAME already done above
String value = options.get(SshConstants.IDENTITY_AGENT);
if (value != null && !SshConstants.NONE.equals(value)
&& !SshConstants.ENV_SSH_AUTH_SOCKET.equals(value)) {
value = r.substitute(value, Replacer.DEFAULT_TOKENS, true);
value = toFile(value, home).getPath();
options.put(SshConstants.IDENTITY_AGENT, value);
}
value = options.get(SshConstants.CONTROL_PATH);
if (value != null) {
value = r.substitute(value, Replacer.DEFAULT_TOKENS, true);
value = toFile(value, home).getPath();
options.put(SshConstants.CONTROL_PATH, value);
}
value = options.get(SshConstants.LOCAL_COMMAND);
if (value != null) {
value = r.substitute(value, "CdhLlnprTu", false); //$NON-NLS-1$
options.put(SshConstants.LOCAL_COMMAND, value);
}
value = options.get(SshConstants.REMOTE_COMMAND);
if (value != null) {
value = r.substitute(value, Replacer.DEFAULT_TOKENS, false);
options.put(SshConstants.REMOTE_COMMAND, value);
}
value = options.get(SshConstants.PROXY_COMMAND);
if (value != null) {
value = r.substitute(value, "hnpr", false); //$NON-NLS-1$
options.put(SshConstants.PROXY_COMMAND, value);
}
}
// Match is not implemented and would need to be done elsewhere
// anyway.
if (fillDefaults) {
String s = options.get(SshConstants.USER);
if (StringUtils.isEmptyOrNull(s)) {
options.put(SshConstants.USER, u);
}
if (positive(options.get(SshConstants.PORT)) <= 0) {
options.put(SshConstants.PORT, Integer.toString(p));
}
if (positive(
options.get(SshConstants.CONNECTION_ATTEMPTS)) <= 0) {
options.put(SshConstants.CONNECTION_ATTEMPTS, "1"); //$NON-NLS-1$
}
}
}
/**
* Retrieves an unmodifiable map of all single-valued options, with
* case-insensitive lookup by keys.
*
* @return all single-valued options
*/
@Override
@NonNull
public Map<String, String> getOptions() {
if (options == null) {
return Collections.emptyMap();
}
return Collections.unmodifiableMap(options);
}
/**
* Retrieves an unmodifiable map of all multi-valued options, with
* case-insensitive lookup by keys.
*
* @return all multi-valued options
*/
@Override
@NonNull
public Map<String, List<String>> getMultiValuedOptions() {
if (listOptions == null && multiOptions == null) {
return Collections.emptyMap();
}
Map<String, List<String>> allValues = new TreeMap<>(
String.CASE_INSENSITIVE_ORDER);
if (multiOptions != null) {
allValues.putAll(multiOptions);
}
if (listOptions != null) {
allValues.putAll(listOptions);
}
return Collections.unmodifiableMap(allValues);
}
@Override
@SuppressWarnings("nls")
public String toString() {
return "HostEntry [options=" + options + ", multiOptions="
+ multiOptions + ", listOptions=" + listOptions + "]";
}
}
private static class Replacer {
/**
* Tokens applicable to most keys.
*
* @see <a href="https://man.openbsd.org/ssh_config.5#TOKENS">man
* ssh_config</a>
*/
public static final String DEFAULT_TOKENS = "CdhLlnpru"; //$NON-NLS-1$
private final Map<Character, String> replacements = new HashMap<>();
public Replacer(String host, int port, String user,
String localUserName, File home) {
replacements.put(Character.valueOf('%'), "%"); //$NON-NLS-1$
replacements.put(Character.valueOf('d'), home.getPath());
replacements.put(Character.valueOf('h'), host);
String localhost = SystemReader.getInstance().getHostname();
replacements.put(Character.valueOf('l'), localhost);
int period = localhost.indexOf('.');
if (period > 0) {
localhost = localhost.substring(0, period);
}
replacements.put(Character.valueOf('L'), localhost);
replacements.put(Character.valueOf('n'), host);
replacements.put(Character.valueOf('p'), Integer.toString(port));
replacements.put(Character.valueOf('r'), user == null ? "" : user); //$NON-NLS-1$
replacements.put(Character.valueOf('u'), localUserName);
replacements.put(Character.valueOf('C'),
substitute("%l%h%p%r", "hlpr", false)); //$NON-NLS-1$ //$NON-NLS-2$
replacements.put(Character.valueOf('T'), "NONE"); //$NON-NLS-1$
}
public void update(char key, String value) {
replacements.put(Character.valueOf(key), value);
if ("lhpr".indexOf(key) >= 0) { //$NON-NLS-1$
replacements.put(Character.valueOf('C'),
substitute("%l%h%p%r", "hlpr", false)); //$NON-NLS-1$ //$NON-NLS-2$
}
}
public String substitute(String input, String allowed,
boolean withEnv) {
if (input == null || input.length() <= 1
|| (input.indexOf('%') < 0
&& (!withEnv || input.indexOf("${") < 0))) { //$NON-NLS-1$
return input;
}
StringBuilder builder = new StringBuilder();
int start = 0;
int length = input.length();
while (start < length) {
char ch = input.charAt(start);
switch (ch) {
case '%':
if (start + 1 >= length) {
break;
}
String replacement = null;
ch = input.charAt(start + 1);
if (ch == '%' || allowed.indexOf(ch) >= 0) {
replacement = replacements.get(Character.valueOf(ch));
}
if (replacement == null) {
builder.append('%').append(ch);
} else {
builder.append(replacement);
}
start += 2;
continue;
case '$':
if (!withEnv || start + 2 >= length) {
break;
}
ch = input.charAt(start + 1);
if (ch == '{') {
int close = input.indexOf('}', start + 2);
if (close > start + 2) {
String variable = SystemReader.getInstance()
.getenv(input.substring(start + 2, close));
if (!StringUtils.isEmptyOrNull(variable)) {
builder.append(variable);
}
start = close + 1;
continue;
}
}
ch = '$';
break;
default:
break;
}
builder.append(ch);
start++;
}
return builder.toString();
}
}
@Override
@SuppressWarnings("nls")
public String toString() {
return "OpenSshConfig [home=" + home + ", configFile=" + configFile
+ ", lastModified=" + lastModified + ", state=" + state + "]";
}
}
|