com.sshtools.publickey
Class AbstractKnownHostsKeyVerification

java.lang.Object
  extended bycom.sshtools.publickey.AbstractKnownHostsKeyVerification
All Implemented Interfaces:
HostKeyVerification
Direct Known Subclasses:
ConsoleKnownHostsKeyVerification

public abstract class AbstractKnownHostsKeyVerification
extends java.lang.Object
implements HostKeyVerification

An abstract HostKeyVerification class implementation providing validation against the known_hosts format.

Author:
Lee David Painter

Constructor Summary
AbstractKnownHostsKeyVerification()
          Construct a known_hosts database based on the default path of ~/.ssh/known_hosts.
AbstractKnownHostsKeyVerification(java.lang.String knownhosts)
           Constructs a known_hosts database based on the path provided.
 
Method Summary
 java.util.Hashtable allowedHosts()
           Returns a Map of the allowed hosts.
 void allowHost(java.lang.String host, SshPublicKey pk, boolean always)
           Allows a host key, optionally recording the key to the known_hosts file.
 boolean isHostFileWriteable()
           Determines whether the host file is writable.
abstract  void onHostKeyMismatch(java.lang.String host, SshPublicKey allowedHostKey, SshPublicKey actualHostKey)
           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  void onUnknownHost(java.lang.String host, SshPublicKey key)
           Called by the verifyHost method when the host key supplied is not recorded in the known_hosts file.
 void removeAllowedHost(java.lang.String host)
           Removes an allowed host.
 void saveHostFile()
           Save's the host key file to be saved.
 java.lang.String toString()
           Outputs the allowed hosts in the known_hosts file format.
 boolean verifyHost(java.lang.String host, SshPublicKey pk)
           Verifies a host key against the list of known_hosts.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractKnownHostsKeyVerification

public AbstractKnownHostsKeyVerification()
                                  throws SshException
Construct a known_hosts database based on the default path of ~/.ssh/known_hosts.


AbstractKnownHostsKeyVerification

public AbstractKnownHostsKeyVerification(java.lang.String knownhosts)
                                  throws SshException

Constructs a known_hosts database based on the path provided.

Parameters:
knownhosts - the path of the known_hosts file
Throws:
InvalidHostFileException - if the known_hosts file is invalid
SshException
Since:
0.2.0
Method Detail

isHostFileWriteable

public boolean isHostFileWriteable()

Determines whether the host file is writable.

Returns:
true if the host file is writable, otherwise false
Since:
0.2.0

onHostKeyMismatch

public abstract void onHostKeyMismatch(java.lang.String host,
                                       SshPublicKey allowedHostKey,
                                       SshPublicKey actualHostKey)
                                throws SshException

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.

Parameters:
host - the name of the host
allowedHostKey - the current key recorded in the known_hosts file.
actualHostKey - the actual key supplied by the user
Throws:
SshException - if an error occurs
Since:
0.2.0

onUnknownHost

public abstract void onUnknownHost(java.lang.String host,
                                   SshPublicKey key)
                            throws SshException

Called by the verifyHost method when the host key supplied is not recorded in the known_hosts file.

Parameters:
host - the name of the host
key - the public key supplied by the host
Throws:
SshException - if an error occurs
Since:
0.2.0

allowHost

public void allowHost(java.lang.String host,
                      SshPublicKey pk,
                      boolean always)
               throws SshException

Allows a host key, optionally recording the key to the known_hosts file.

Parameters:
host - the name of the host
pk - the public key to allow
always - true if the key should be written to the known_hosts file
Throws:
InvalidHostFileException - if the host file cannot be written
SshException
Since:
0.2.0

allowedHosts

public java.util.Hashtable 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.

Returns:
the allowed hosts
Since:
0.2.0

removeAllowedHost

public void removeAllowedHost(java.lang.String host)

Removes an allowed host.

Parameters:
host - the host to remove
Since:
0.2.0

verifyHost

public boolean verifyHost(java.lang.String host,
                          SshPublicKey pk)
                   throws SshException

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.

Specified by:
verifyHost in interface HostKeyVerification
Parameters:
host - the name of the host
pk - the host key supplied
Returns:
true if the host is accepted, otherwise false
Throws:
SshException - if an error occurs
Since:
0.2.0

saveHostFile

public void saveHostFile()
                  throws java.io.IOException

Save's the host key file to be saved.

Throws:
InvalidHostFileException - if the host file is invalid
java.io.IOException
Since:
0.2.0

toString

public java.lang.String toString()

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.....

Returns:
Since:
0.2.0


Copyright © 2003 3SP LTD. All Rights Reserved.