*
* @param kex instance
* @throws JSchException if any errors occur
*/
private void checkHost(KexAlgorithm kex) throws JSchException {
UserInfo _userinfo = _session.getUserInfo();
// Check if host key alias exists and use it, or if it's not present and
// not using default port, set the port in host to check
String chost = _session.getHost();
if( _session.getHostKeyAlias() != null ) {
chost = _session.getHostKeyAlias();
} else if( _session.getPort() != SSHConstants.DEFAULT_SSH_PORT ) {
chost = "[" + chost + "]:" + _session.getPort();
}
// Check host against known hosts repository
HostKeyRepository hkr = JSch.getInstance().getHostKeyRepository();
Check keyCheck;
synchronized( hkr ) {
keyCheck = hkr.check(chost, kex.K_S);
}
boolean insert = false;
String shkc = _session.getConfig().getString(SessionConfig.STRICT_HOST_KEY_CHECKING);
if( ("ask".equals(shkc) || "yes".equals(shkc)) && keyCheck == Check.CHANGED ) {
String file = hkr.getKnownHostsRepositoryID() != null ?
hkr.getKnownHostsRepositoryID() : SSHConstants.KNOWN_HOSTS;
// Notify user host key changed (ask if requested) and throw exception
// if user doesn't accept the new key
if( _userinfo != null ) {
if( "ask".equals(shkc) ) {
if( !_userinfo.promptYesNo(String.format(MessageConstants.PROMPT_REPLACE_KEY,
kex._hostKeyType.DISPLAY_NAME, Util.getFingerPrint(kex.K_S), file)) ) {
throw new JSchException("HostKey has changed (StrictHostKeyChecking:ask): "+chost);
}
} else { // shkc.equals("yes")
_userinfo.showMessage(String.format(MessageConstants.INVALID_SERVER_HOST,
kex._hostKeyType.DISPLAY_NAME, Util.getFingerPrint(kex.K_S), file));
throw new JSchException("HostKey has changed (StrictHostKeyChecking:yes): "+chost);
}
}
// Remove the old key from the repository
synchronized ( hkr ) {
hkr.remove(chost, kex._hostKeyType, null);
insert = true;
}
}
if( ("ask".equals(shkc) || "yes".equals(shkc)) && keyCheck != Check.OK && !insert ) {
if( "yes".equals(shkc) ) {
throw new JSchException("HostKey does not match known hosts (StrictHostKeyChecking:yes): "+chost);
}
if( _userinfo != null ) {
if( !_userinfo.promptYesNo(String.format(MessageConstants.PROMPT_UNKNOWN_KEY,
chost, kex._hostKeyType.DISPLAY_NAME, Util.getFingerPrint(kex.K_S))) ) {
throw new JSchException("HostKey does not match known hosts (StrictHostKeyChecking:ask): "+chost);
}
insert = true;
} else {