public class OpenSshConfigFile
extends java.lang.Object
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 |
---|---|
static class |
OpenSshConfigFile.HostEntry
A host entry from the ssh config file.
|
private static class |
OpenSshConfigFile.Replacer |
private static class |
OpenSshConfigFile.State
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 java.io.File |
configFile
The .ssh/config file we read and monitor for updates.
|
private static java.lang.String |
DEFAULT_NAME
"Host" name of the HostEntry for the default options before the first
host block in a config file.
|
private java.io.File |
home
The user's home directory, as key files may be relative to here.
|
private java.time.Instant |
lastModified
Modification time of
configFile when it was last loaded. |
private java.lang.String |
localUserName
User name of the user on the host OS.
|
private OpenSshConfigFile.State |
state
State read from the config file, plus the cache.
|
Constructor and Description |
---|
OpenSshConfigFile(java.io.File home,
java.io.File config,
java.lang.String localUserName)
Creates a new
OpenSshConfigFile that will read the config from
file config use the given file home as "home" directory. |
Modifier and Type | Method and Description |
---|---|
private static java.lang.String |
dequote(java.lang.String value) |
static boolean |
flag(java.lang.String value)
Converts a ssh config flag value (yes/true/on - no/false/off) into an
boolean . |
java.lang.String |
getLocalUserName()
Retrieves the local user name as given in the constructor.
|
private static boolean |
isHostMatch(java.lang.String pattern,
java.lang.String name) |
OpenSshConfigFile.HostEntry |
lookup(java.lang.String hostName,
int port,
java.lang.String userName)
Locate the configuration for a specific host request.
|
private java.util.Map<java.lang.String,OpenSshConfigFile.HostEntry> |
parse(java.io.BufferedReader reader) |
private java.util.List<java.lang.String> |
parseList(java.lang.String argument)
Splits the argument into a list of whitespace-separated elements.
|
private static boolean |
patternMatchesHost(java.lang.String pattern,
java.lang.String name) |
static int |
positive(java.lang.String value)
Converts a positive value into an
int . |
private OpenSshConfigFile.State |
refresh() |
private static java.lang.String |
stripWhitespace(java.lang.String value) |
private java.lang.String |
toCacheKey(java.lang.String hostName,
int port,
java.lang.String userName) |
private static java.io.File |
toFile(java.lang.String path,
java.io.File home) |
java.lang.String |
toString() |
protected java.util.List<java.lang.String> |
validate(java.lang.String key,
java.util.List<java.lang.String> value)
Hook to perform validation on values, or to sanitize them.
|
protected java.lang.String |
validate(java.lang.String key,
java.lang.String value)
Hook to perform validation on a single value, or to sanitize it.
|
private static final java.lang.String DEFAULT_NAME
private final java.io.File home
private final java.io.File configFile
private final java.lang.String localUserName
private java.time.Instant lastModified
configFile
when it was last loaded.private OpenSshConfigFile.State state
public OpenSshConfigFile(@NonNull java.io.File home, @NonNull java.io.File config, @NonNull java.lang.String localUserName)
OpenSshConfigFile
that will read the config from
file config
use the given file home
as "home" directory.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@NonNull public OpenSshConfigFile.HostEntry lookup(@NonNull java.lang.String hostName, int port, java.lang.String userName)
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 be null
or empty if none given@NonNull private java.lang.String toCacheKey(@NonNull java.lang.String hostName, int port, java.lang.String userName)
private OpenSshConfigFile.State refresh()
private java.util.Map<java.lang.String,OpenSshConfigFile.HostEntry> parse(java.io.BufferedReader reader) throws java.io.IOException
java.io.IOException
private java.util.List<java.lang.String> parseList(java.lang.String argument)
argument
- argument part of the configuration line as read from the
config fileList
of elements, possibly empty and possibly
containing empty elements, but not containing null
protected java.lang.String validate(java.lang.String key, java.lang.String value)
key
- of the entryvalue
- as read from the config fileprotected java.util.List<java.lang.String> validate(java.lang.String key, java.util.List<java.lang.String> value)
key
- of the entryvalue
- list of arguments as read from the config fileList
of values, possibly empty and possibly containing
empty elements, but not containing null
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 stripWhitespace(java.lang.String value)
private static java.io.File toFile(java.lang.String path, java.io.File home)
public static int positive(java.lang.String value)
int
.value
- to convertpublic static boolean flag(java.lang.String value)
boolean
.value
- to converttrue
if value
is "yes", "on", or "true";
false
otherwisepublic java.lang.String getLocalUserName()
public java.lang.String toString()
toString
in class java.lang.Object