public class OpenSshConfig
extends java.lang.Object
implements com.jcraft.jsch.ConfigRepository
JSch does have its own config file parser
OpenSSHConfig
since version 0.1.50, but it has a
number of problems:
Therefore implement our own parser to read an OpenSSH configuration file. It
makes the critical options available to
SshSessionFactory
via
OpenSshConfig.Host
objects returned by
lookup(String)
, and implements a fully conforming
ConfigRepository
providing
ConfigRepository.Config
s via
getConfig(String)
.
Limitations compared to the full OpenSSH 7.5 parser:
Note that OpenSSH's readconf.c is a validating parser; Jsch's
ConfigRepository OTOH treats all option values as plain strings, so any
validation must happen in Jsch outside of the parser. Thus this parser does
not validate option values, except for a few options when constructing a
OpenSshConfig.Host
object.
This config does %-substitutions for the following tokens:
lookup(String)
If the config doesn't set the port or the remote user name, %p and %r remain un-substituted. It's the caller's responsibility to replace them with values obtained from the connection URI. %i is not handled; Java has no concept of a "user ID".
Modifier and Type | Class and Description |
---|---|
static class |
OpenSshConfig.Host
Configuration of one "Host" block in the configuration file.
|
private static class |
OpenSshConfig.HostEntry |
private static class |
OpenSshConfig.State
Encapsulates entries read out of the configuration file, and
OpenSshConfig.Host s created from that. |
Modifier and Type | Field and Description |
---|---|
private java.io.File |
configFile
The .ssh/config file we read and monitor for updates.
|
private java.io.File |
home
The user's home directory, as key files may be relative to here.
|
private long |
lastModified
Modification time of
configFile when it was last loaded. |
(package private) static int |
SSH_PORT
IANA assigned port number for SSH.
|
private OpenSshConfig.State |
state
State read from the config file, plus
OpenSshConfig.Host s created from it. |
Constructor and Description |
---|
OpenSshConfig(java.io.File h,
java.io.File cfg) |
Modifier and Type | Method and Description |
---|---|
private static java.lang.String |
dequote(java.lang.String value) |
static OpenSshConfig |
get(FS fs)
Obtain the user's configuration data.
|
com.jcraft.jsch.ConfigRepository.Config |
getConfig(java.lang.String hostName) |
private static boolean |
isHostMatch(java.lang.String pattern,
java.lang.String name) |
OpenSshConfig.Host |
lookup(java.lang.String hostName)
Locate the configuration for a specific host request.
|
private static java.lang.String |
nows(java.lang.String value) |
private java.util.Map<java.lang.String,OpenSshConfig.HostEntry> |
parse(java.io.InputStream in) |
private static boolean |
patternMatchesHost(java.lang.String pattern,
java.lang.String name) |
private static int |
positive(java.lang.String value) |
private OpenSshConfig.State |
refresh() |
private static java.io.File |
toFile(java.lang.String path,
java.io.File home) |
java.lang.String |
toString() |
(package private) static java.lang.String |
userName() |
private static java.lang.Boolean |
yesno(java.lang.String value) |
static final int SSH_PORT
private final java.io.File home
private final java.io.File configFile
private long lastModified
configFile
when it was last loaded.private OpenSshConfig.State state
OpenSshConfig.Host
s created from it.public static OpenSshConfig get(FS fs)
The configuration file is always returned to the caller, even if no file exists in the user's home directory at the time the call was made. Lookup requests are cached and are automatically updated if the user modifies the configuration file since the last time it was cached.
fs
- the file system abstraction which will be necessary to
perform certain file system operations.public OpenSshConfig.Host lookup(java.lang.String hostName)
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.private OpenSshConfig.State refresh()
private java.util.Map<java.lang.String,OpenSshConfig.HostEntry> parse(java.io.InputStream in) throws java.io.IOException
java.io.IOException
private static boolean isHostMatch(java.lang.String pattern, java.lang.String name)
private static boolean patternMatchesHost(java.lang.String pattern, java.lang.String name)
private static java.lang.String dequote(java.lang.String value)
private static java.lang.String nows(java.lang.String value)
private static java.lang.Boolean yesno(java.lang.String value)
private static java.io.File toFile(java.lang.String path, java.io.File home)
private static int positive(java.lang.String value)
static java.lang.String userName()
public com.jcraft.jsch.ConfigRepository.Config getConfig(java.lang.String hostName)
Retrieves the full Config
for the given host name. Should be called only by Jsch and tests.
getConfig
in interface com.jcraft.jsch.ConfigRepository
public java.lang.String toString()
toString
in class java.lang.Object