Class OpenSshConfigFile
java.lang.Object
org.eclipse.jgit.internal.transport.ssh.OpenSshConfigFile
- All Implemented Interfaces:
SshConfigStore
Fairly complete configuration parser for the openssh ~/.ssh/config file.
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:
- This parser does not handle Match or Include keywords.
- This parser does not do host name canonicalization.
Note that openssh's readconf.c is a validating parser; this parser does not validate entries.
This config does %-substitutions for the following tokens:
- %% - single %
- %C - short-hand for %l%h%p%r.
- %d - home directory path
- %h - remote host name
- %L - local host name without domain
- %l - FQDN of the local host
- %n - host name as specified in
lookup(String, int, String) - %p - port number; if not given in
lookup(String, int, String)replaced only if set in the config - %r - remote user name; if not given in
lookup(String, int, String)replaced only if set in the config - %u - local user name
%i is not handled; Java has no concept of a "user ID". %T is always replaced by NONE.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA host entry from the ssh config file.private static classprivate static classEncapsulates entries read out of the configuration file, and a cache of fully resolved entries created from that.Nested classes/interfaces inherited from interface org.eclipse.jgit.transport.SshConfigStore
SshConfigStore.HostConfig -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final FileThe .ssh/config file we read and monitor for updates.private final FileThe user's home directory, as key files may be relative to here.private InstantModification time ofconfigFilewhen it was last loaded.private final StringUser name of the user on the host OS.private OpenSshConfigFile.StateState read from the config file, plus the cache.Fields inherited from interface org.eclipse.jgit.transport.SshConfigStore
EMPTY_CONFIG -
Constructor Summary
ConstructorsConstructorDescriptionOpenSshConfigFile(File home, File config, String localUserName) Creates a newOpenSshConfigFilethat will read the config from fileconfiguse the given filehomeas "home" directory. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanConverts a ssh config flag value (yes/true/on - no/false/off) into anboolean.Retrieves the local user name as given in the constructor.Locate the configuration for a specific host request.private OpenSshConfigFile.HostEntrylookupDefault(String hostName, int port, String userName) Locate the configuration for a specific host request and if the configuration has no values forSshConstants.HOST_NAME,SshConstants.PORT,SshConstants.USER, orSshConstants.CONNECTION_ATTEMPTS, fill those values with defaults from the arguments: ssh config key value from argumentHostNamehostNamePortport > 0 ? port : 22UseruserNameConnectionAttempts1private List<OpenSshConfigFile.HostEntry>parse(BufferedReader reader) Splits the argument into a list of whitespace-separated elements.private static intparseToken(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.private static booleanpatternMatchesHost(String pattern, String name) static intConverts a positive value into anint.private OpenSshConfigFile.Staterefresh()private static StringstripWhitespace(String value) static intConverts an OpenSSH time value into a number of seconds.private StringtoCacheKey(String hostName, int port, String userName) private static FiletoString()protected StringHook to perform validation on a single value, or to sanitize it.Hook to perform validation on values, or to sanitize them.
-
Field Details
-
home
The user's home directory, as key files may be relative to here. -
configFile
The .ssh/config file we read and monitor for updates. -
localUserName
User name of the user on the host OS. -
lastModified
Modification time ofconfigFilewhen it was last loaded. -
state
State read from the config file, plus the cache.
-
-
Constructor Details
-
OpenSshConfigFile
Creates a newOpenSshConfigFilethat will read the config from fileconfiguse the given filehomeas "home" directory.- Parameters:
home- user's home directory for the purpose of ~ replacementconfig- file to load.localUserName- user name of the current user on the local host OS
-
-
Method Details
-
lookup
@NonNull public OpenSshConfigFile.HostEntry lookup(@NonNull String hostName, int port, String userName) Locate the configuration for a specific host request.- Specified by:
lookupin interfaceSshConfigStore- Parameters:
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.port- the user supplied; <= 0 if noneuserName- the user supplied, may benullor empty if none given- Returns:
- the configuration for the requested name.
-
lookupDefault
@NonNull public OpenSshConfigFile.HostEntry lookupDefault(@NonNull String hostName, int port, String userName) Description copied from interface:SshConfigStoreLocate the configuration for a specific host request and if the configuration has no values forSshConstants.HOST_NAME,SshConstants.PORT,SshConstants.USER, orSshConstants.CONNECTION_ATTEMPTS, fill those values with defaults from the arguments:ssh config key value from argument HostNamehostNamePortport > 0 ? port : 22UseruserNameConnectionAttempts1- Specified by:
lookupDefaultin interfaceSshConfigStore- Parameters:
hostName- host name to look upport- port number; <= 0 if noneuserName- the user name, may benullor empty if none given- Returns:
- the configuration for the requested name.
-
lookup
private OpenSshConfigFile.HostEntry lookup(@NonNull String hostName, int port, String userName, boolean fillDefaults) -
toCacheKey
-
refresh
-
parse
- Throws:
IOException
-
parseList
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.- Parameters:
argument- argument part of the configuration line as read from the config file- Returns:
- a
Listof elements, possibly empty and possibly containing empty elements, but not containingnull
-
parseToken
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 ' '.- Parameters:
argument- to parse the token out offrom- index at the beginning of the tokento- index one after the last character to look atresult- a list collecting tokens to which the parsed token is added- Returns:
- the index after the token
-
validate
Hook to perform validation on a single value, or to sanitize it. If this throws an (unchecked) exception, parsing of the file is abandoned.- Parameters:
key- of the entryvalue- as read from the config file- Returns:
- the validated and possibly sanitized value
-
validate
Hook to perform validation on values, or to sanitize them. If this throws an (unchecked) exception, parsing of the file is abandoned.- Parameters:
key- of the entryvalue- list of arguments as read from the config file- Returns:
- a
Listof values, possibly empty and possibly containing empty elements, but not containingnull
-
patternMatchesHost
-
stripWhitespace
-
toFile
-
positive
Converts a positive value into anint.- Parameters:
value- to convert- Returns:
- the value, or -1 if it wasn't a positive integral value
-
flag
Converts a ssh config flag value (yes/true/on - no/false/off) into anboolean.- Parameters:
value- to convert- Returns:
trueifvalueis "yes", "on", or "true";falseotherwise
-
timeSpec
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.- Parameters:
value- to convert- Returns:
- the parsed value as a number of seconds, or -1 if the value is not a valid OpenSSH time value
- See Also:
-
getLocalUserName
Retrieves the local user name as given in the constructor.- Returns:
- the user name
-
toString
-