| Method from com.sshtools.j2ssh.transport.AbstractKnownHostsKeyVerification Detail: |
public void allowHost(String host,
SshPublicKey pk,
boolean always) throws InvalidHostFileException {
if (log.isDebugEnabled()) {
log.debug("Allowing " + host + " with fingerprint " +
pk.getFingerprint());
}
// Put the host into the allowed hosts list, overiding any previous
// entry
putAllowedKey(host, pk);
//allowedHosts.put(host, pk);
// If we always want to allow then save the host file with the
// new details
if (always) {
saveHostFile();
}
}
Allows a host key, optionally recording the key to the known_hosts file.
|
public Map allowedHosts() {
return allowedHosts;
}
Returns a Map of the allowed hosts.
The keys of the returned Map are comma separated strings of
"hostname,ipaddress". The value objects are Maps containing a string
key of the public key alogorithm name and the public key as the value.
|
public boolean isHostFileWriteable() {
return hostFileWriteable;
}
|
abstract public void onHostKeyMismatch(String host,
SshPublicKey allowedHostKey,
SshPublicKey actualHostKey) throws TransportProtocolException
Called by the verifyHost method when the host key supplied
by the host does not match the current key recording in the known hosts
file.
|
abstract public void onUnknownHost(String host,
SshPublicKey key) throws TransportProtocolException
|
public void removeAllowedHost(String host) {
Iterator it = allowedHosts.keySet().iterator();
while (it.hasNext()) {
StringTokenizer tokens = new StringTokenizer((String) it.next(), ",");
while (tokens.hasMoreElements()) {
String name = (String) tokens.nextElement();
if (name.equals(host)) {
allowedHosts.remove(name);
}
}
}
}
|
public void saveHostFile() throws InvalidHostFileException {
if (!hostFileWriteable) {
throw new InvalidHostFileException("Host file is not writeable.");
}
log.info("Saving " + defaultHostFile);
try {
File f = new File(knownhosts);
FileOutputStream out = new FileOutputStream(f);
out.write(toString().getBytes());
out.close();
} catch (IOException e) {
throw new InvalidHostFileException("Could not write to " +
knownhosts);
}
}
|
public String toString() {
String knownhosts = "";
Map.Entry entry;
Map.Entry entry2;
Iterator it = allowedHosts.entrySet().iterator();
while (it.hasNext()) {
entry = (Map.Entry) it.next();
Iterator it2 = ((Map) entry.getValue()).entrySet().iterator();
while (it2.hasNext()) {
entry2 = (Map.Entry) it2.next();
SshPublicKey pk = (SshPublicKey) entry2.getValue();
knownhosts += (entry.getKey().toString() + " " +
pk.getAlgorithmName() + " " +
Base64.encodeBytes(pk.getEncoded(), true) + "\n");
}
}
return knownhosts;
}
Outputs the allowed hosts in the known_hosts file format.
The format consists of any number of lines each representing one key for
a single host.
titan,192.168.1.12 ssh-dss AAAAB3NzaC1kc3MAAACBAP1/U4Ed.....
titan,192.168.1.12 ssh-rsa AAAAB3NzaC1kc3MAAACBAP1/U4Ed.....
einstein,192.168.1.40 ssh-dss AAAAB3NzaC1kc3MAAACBAP1/U4Ed.....
|
public boolean verifyHost(String host,
SshPublicKey pk) throws TransportProtocolException {
String fingerprint = pk.getFingerprint();
log.info("Verifying " + host + " host key");
if (log.isDebugEnabled()) {
log.debug("Fingerprint: " + fingerprint);
}
Iterator it = allowedHosts.keySet().iterator();
while (it.hasNext()) {
// Could be a comma delimited string of names/ip addresses
String names = (String) it.next();
if (names.equals(host)) {
return validateHost(names, pk);
}
StringTokenizer tokens = new StringTokenizer(names, ",");
while (tokens.hasMoreElements()) {
// Try the allowed hosts by looking at the allowed hosts map
String name = (String) tokens.nextElement();
if (name.equalsIgnoreCase(host)) {
return validateHost(names, pk);
}
}
}
// The host is unknown os ask the user
onUnknownHost(host, pk);
// Recheck ans return the result
return checkKey(host, pk);
}
Verifies a host key against the list of known_hosts.
If the host unknown or the key does not match the currently allowed host
key the abstract onUnknownHost or
onHostKeyMismatch methods are called so that the caller
may identify and allow the host.
|