} 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 {
if( keyCheck == Check.NOT_INCLUDED ) {
throw new JSchException("UnknownHostKey: "+chost+". "+kex._hostKeyType+" key fingerprint is "+Util.getFingerPrint(kex.K_S));
} else {
throw new JSchException("HostKey has been changed (StrictHostKeyChecking:ask): " + chost);
}
}
}
if( "no".equals(shkc) && keyCheck == Check.NOT_INCLUDED ) {
insert = true;
}
if( keyCheck == Check.OK && JSch.getLogger().isEnabled(Logger.Level.INFO) ) {
JSch.getLogger().log(Logger.Level.INFO, "Host '"+chost+"' is known and matches the "+kex._hostKeyType+" host key");
}
if( insert && JSch.getLogger().isEnabled(Logger.Level.WARN) ) {
JSch.getLogger().log(Logger.Level.WARN, "Permanently added '"+chost+"' ("+kex._hostKeyType+") to the list of known hosts.");
}
// Create host key instance
_hostKey = HostKey.createHostKey(chost, kex.K_S, _session.getConfig().getBoolean(SessionConfig.HASH_KNOWN_HOSTS));
if( insert ) {
synchronized( hkr ) {
hkr.add(_hostKey, _userinfo);
}
}
}