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.
Limitations compared to the full openssh 7.5 parser:
Note that openssh's readconf.c is a validating parser; this parser does not validate entries.
This config does %-substitutions for the following tokens:
lookup(String, int, String)
lookup(String, int, String)
replaced only if set in the config
lookup(String, int, String) replaced only if set in the config
%i is not handled; Java has no concept of a "user ID". %T is always replaced by NONE.
| Modifier and Type | Class and Description |
|---|---|
| public static class | OpenSshConfigFile.
A host entry from the ssh config file. |
| private static class | |
| private static class | OpenSshConfigFile.
Encapsulates entries read out of the configuration file, and a cache of fully resolved entries created from that. |
| Modifier and Type | Field and Description |
|---|---|
| private final File | configFile
The .ssh/config file we read and monitor for updates. |
| private final File | home
The user's home directory, as key files may be relative to here. |
| private Instant | lastModified
Modification time of |
| private final String | localUserName
User name of the user on the host OS. |
| private OpenSshConfigFile. | state
State read from the config file, plus the cache. |
| Access | Constructor and Description |
|---|---|
| public | OpenSshConfigFile(File
user's home directory for the purpose of ~ replacement home, File file to load. config, String user name of the current user on the local host OS localUserName)Creates a new |
| Modifier and Type | Method and Description |
|---|---|
| public static boolean | |
| public String | |
| public OpenSshConfigFile. | Returns: the configuration for the requested name.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. hostName, int the user supplied; <= 0 if none port, String the user supplied, may be userName)null or empty if none givenImplements org. Locate the configuration for a specific host request. |
| private OpenSshConfigFile. | |
| public OpenSshConfigFile. | lookupDefault(String
host name to look up hostName, int port number; <= 0 if none port, String the user name, may be userName)null or empty if none givenImplements org. Locate the configuration for a specific host request and if the
configuration has no values for |
| private List | |
| private static List | |
| private static int | Returns: the index after the tokento parse the token out of argument, int index at the beginning of the token from, int index one after the last character to look at to, List<String> a list collecting tokens to which the parsed token is added result)Parses a token up to the next whitespace not inside a string quoted by single or double quotes. |
| public static boolean | Returns: true if the name matches at least one of the
non-negative patterns and none of the negative patterns,
false otherwiseto test patterns, String name against; any pattern starting with an
exclamation mark is a negative patternto test name)Tells whether a given |
| private static boolean | |
| public static int | |
| private synchronized OpenSshConfigFile. | |
| private static String | |
| public static int | |
| private String | |
| private static File | |
| public String | |
| protected String | |
| protected List |
| configFile | back to summary |
|---|---|
| private final File configFile The .ssh/config file we read and monitor for updates. | |
| home | back to summary |
|---|---|
| private final File home The user's home directory, as key files may be relative to here. | |
| lastModified | back to summary |
|---|---|
| private Instant lastModified Modification time of | |
| localUserName | back to summary |
|---|---|
| private final String localUserName User name of the user on the host OS. | |
| state | back to summary |
|---|---|
| private OpenSshConfigFile. State read from the config file, plus the cache. | |
| OpenSshConfigFile | back to summary |
|---|---|
| public OpenSshConfigFile(File home, File config, String localUserName) Creates a new | |
| flag | back to summary |
|---|---|
| public static boolean flag(String value) Converts a ssh config flag value (yes/true/on - no/false/off) into an
| |
| getLocalUserName | back to summary |
|---|---|
| public String getLocalUserName() Retrieves the local user name as given in the constructor.
| |
| lookup | back to summary |
|---|---|
| public OpenSshConfigFile. Implements org. Locate the configuration for a specific host request.
| |
| lookup | back to summary |
|---|---|
| private OpenSshConfigFile. | |
| lookupDefault | back to summary | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| public OpenSshConfigFile. Implements org. Doc from org. Locate the configuration for a specific host request and if the
configuration has no values for
| |||||||||||
| parse | back to summary |
|---|---|
| private List | |
| parseList | back to summary |
|---|---|
| private static List 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. | |
| parseToken | back to summary |
|---|---|
| private static int parseToken(String argument, int from, int to, List<String> 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 ' '. | |
| patternMatch | back to summary |
|---|---|
| public static boolean patternMatch(Iterable<String> patterns, String name) Tells whether a given | |
| patternMatches | back to summary |
|---|---|
| private static boolean patternMatches(String pattern, String name) | |
| positive | back to summary |
|---|---|
| public static int positive(String value) Converts a positive value into an
| |
| refresh | back to summary |
|---|---|
| private synchronized OpenSshConfigFile. | |
| stripWhitespace | back to summary |
|---|---|
| private static String stripWhitespace(String value) | |
| timeSpec | back to summary |
|---|---|
| public static int timeSpec(String 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.
| |
| toCacheKey | back to summary |
|---|---|
private String toCacheKey(String hostName, int port, String userName)
| |
| toFile | back to summary |
|---|---|
| private static File toFile(String path, File home) | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non-
| |
| validate | back to summary |
|---|---|
| protected String validate(String key, String value) Hook to perform validation on a single value, or to sanitize it. If this throws an (unchecked) exception, parsing of the file is abandoned. | |
| validate | back to summary |
|---|---|
| protected List Hook to perform validation on values, or to sanitize them. If this throws an (unchecked) exception, parsing of the file is abandoned. | |
| Modifier and Type | Field and Description |
|---|---|
| private static final Map | ALIASES
OpenSSH has renamed some config keys. |
| private static final Set | LIST_KEYS
Keys that take a whitespace-separated list of elements as argument. |
| private Map | |
| private static final Set | MULTI_KEYS
Keys that can be specified multiple times, building up a list. |
| private Map | |
| private Map | |
| private final List |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public Map | Returns: all multi-valued optionsImplements org. Retrieves an unmodifiable map of all multi-valued options, with case-insensitive lookup by keys. |
| public Map | Returns: all single-valued optionsImplements org. Retrieves an unmodifiable map of all single-valued options, with case-insensitive lookup by keys. |
| public String | Returns: the value, ornull if noneto get the value of key)Implements org. Retrieves the value of a single-valued key, or the first if the key has multiple values. |
| public List | Returns: a possibly empty list of valuesto get the values of key)Implements org. Retrieves the values of a multi or list-valued key. |
| public static boolean | |
| pack-priv boolean | |
| pack-priv void | |
| private List | |
| public void | |
| public void | |
| private List | |
| pack-priv void | substitute(String originalHostName, int port, String userName, String localUserName, File home, boolean fillDefaults)
|
| private static String | |
| public String |
| ALIASES | back to summary |
|---|---|
| private static final Map<String, String> ALIASES OpenSSH has renamed some config keys. This maps old names to new names. | |
| LIST_KEYS | back to summary |
|---|---|
| private static final Set<String> LIST_KEYS 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. | |
| listOptions | back to summary |
|---|---|
| private Map<String, List<String>> listOptions | |
| MULTI_KEYS | back to summary |
|---|---|
| private static final Set<String> MULTI_KEYS 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".) | |
| multiOptions | back to summary |
|---|---|
| private Map<String, List<String>> multiOptions | |
| options | back to summary |
|---|---|
| private Map<String, String> options | |
| patterns | back to summary |
|---|---|
| private final List<String> patterns | |
| HostEntry | back to summary |
|---|---|
| public HostEntry() Constructor used to build the merged entry; never matches anything | |
| HostEntry | back to summary |
|---|---|
| public HostEntry(List<String> patterns) | |
| getMultiValuedOptions | back to summary |
|---|---|
| public Map Implements org. Retrieves an unmodifiable map of all multi-valued options, with case-insensitive lookup by keys. | |
| getOptions | back to summary |
|---|---|
| public Map Implements org. Retrieves an unmodifiable map of all single-valued options, with case-insensitive lookup by keys. | |
| getValue | back to summary |
|---|---|
| public String getValue(String key) Implements org. Retrieves the value of a single-valued key, or the first if the key
has multiple values. Keys are case-insensitive, so
| |
| getValues | back to summary |
|---|---|
| public List Implements org. Retrieves the values of a multi or list-valued key. Keys are
case-insensitive, so
| |
| isListKey | back to summary |
|---|---|
| public static boolean isListKey(String key) Does the key take a whitespace-separated list of values?
| |
| matches | back to summary |
|---|---|
| pack-priv boolean matches(String hostName) | |
| merge | back to summary |
|---|---|
| pack-priv void merge(OpenSshConfigFile. | |
| replaceTilde | back to summary |
|---|---|
| private List | |
| setValue | back to summary |
|---|---|
| public void setValue(String key, String value) 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 | |
| setValue | back to summary |
|---|---|
| public void setValue(String key, List<String> values) Sets the values of a multi- or list-valued key. | |
| substitute | back to summary |
|---|---|
| private List | |
| substitute | back to summary |
|---|---|
| pack-priv void substitute(String originalHostName, int port, String userName, String localUserName, File home, boolean fillDefaults) | |
| toKey | back to summary |
|---|---|
| private static String toKey(String key) | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non-
| |
| Modifier and Type | Field and Description |
|---|---|
| public static final String | DEFAULT_TOKENS
Tokens applicable to most keys. |
| private final Map |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public String | |
| public void |
| DEFAULT_TOKENS | back to summary |
|---|---|
| public static final String DEFAULT_TOKENS Tokens applicable to most keys.
| |
| replacements | back to summary |
|---|---|
| private final Map<Character, String> replacements | |
| Replacer | back to summary |
|---|---|
| public Replacer(String host, int port, String user, String localUserName, File home) | |
| substitute | back to summary |
|---|---|
| public String substitute(String input, String allowed, boolean withEnv) | |
| update | back to summary |
|---|---|
| public void update(char key, String value) | |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv List | |
| pack-priv Map |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public String |
| entries | back to summary |
|---|---|
| pack-priv List<OpenSshConfigFile. | |
| hosts | back to summary |
|---|---|
| pack-priv Map<String, OpenSshConfigFile. | |
| State | back to summary |
|---|---|
| private State() | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non-
| |