{
uIKeyboardInteractive = null;
setInteractiveUserInfo( new NullInteractiveUserInfo() );
}
JSch sch = new JSch();
File privateKey;
try
{
privateKey = ScpHelper.getPrivateKey( authenticationInfo );
}
catch ( FileNotFoundException e )
{
throw new AuthenticationException( e.getMessage() );
}
if ( privateKey != null && privateKey.exists() )
{
fireSessionDebug( "Using private key: " + privateKey );
try
{
sch.addIdentity( privateKey.getAbsolutePath(), authenticationInfo.getPassphrase() );
}
catch ( JSchException e )
{
throw new AuthenticationException( "Cannot connect. Reason: " + e.getMessage(), e );
}
}
String host = getRepository().getHost();
int port =
repository.getPort() == WagonConstants.UNKNOWN_PORT ? ScpHelper.DEFAULT_SSH_PORT : repository.getPort();
try
{
String userName = authenticationInfo.getUserName();
if ( userName == null )
{
userName = System.getProperty( "user.name" );
}
session = sch.getSession( userName, host, port );
session.setTimeout( getTimeout() );
}
catch ( JSchException e )
{
throw new AuthenticationException( "Cannot connect. Reason: " + e.getMessage(), e );
}
Proxy proxy = null;
ProxyInfo proxyInfo = getProxyInfo( ProxyInfo.PROXY_SOCKS5, getRepository().getHost() );
if ( proxyInfo != null && proxyInfo.getHost() != null )
{
proxy = new ProxySOCKS5( proxyInfo.getHost(), proxyInfo.getPort() );
( (ProxySOCKS5) proxy ).setUserPasswd( proxyInfo.getUserName(), proxyInfo.getPassword() );
}
else
{
proxyInfo = getProxyInfo( ProxyInfo.PROXY_HTTP, getRepository().getHost() );
if ( proxyInfo != null && proxyInfo.getHost() != null )
{
proxy = new ProxyHTTP( proxyInfo.getHost(), proxyInfo.getPort() );
( (ProxyHTTP) proxy ).setUserPasswd( proxyInfo.getUserName(), proxyInfo.getPassword() );
}
else
{
// Backwards compatibility
proxyInfo = getProxyInfo( getRepository().getProtocol(), getRepository().getHost() );
if ( proxyInfo != null && proxyInfo.getHost() != null )
{
// if port == 1080 we will use SOCKS5 Proxy, otherwise will use HTTP Proxy
if ( proxyInfo.getPort() == SOCKS5_PROXY_PORT )
{
proxy = new ProxySOCKS5( proxyInfo.getHost(), proxyInfo.getPort() );
( (ProxySOCKS5) proxy ).setUserPasswd( proxyInfo.getUserName(), proxyInfo.getPassword() );
}
else
{
proxy = new ProxyHTTP( proxyInfo.getHost(), proxyInfo.getPort() );
( (ProxyHTTP) proxy ).setUserPasswd( proxyInfo.getUserName(), proxyInfo.getPassword() );
}
}
}
}
session.setProxy( proxy );
// username and password will be given via UserInfo interface.
UserInfo ui = new WagonUserInfo( authenticationInfo, getInteractiveUserInfo() );
if ( uIKeyboardInteractive != null )
{
ui = new UserInfoUIKeyboardInteractiveProxy( ui, uIKeyboardInteractive );
}
Properties config = new Properties();
if ( getKnownHostsProvider() != null )
{
try
{
String contents = getKnownHostsProvider().getContents();
if ( contents != null )
{
sch.setKnownHosts( new StringInputStream( contents ) );
}
}
catch ( JSchException e )
{
// continue without known_hosts
}
config.setProperty( "StrictHostKeyChecking", getKnownHostsProvider().getHostKeyChecking() );
}
if ( authenticationInfo.getPassword() != null )
{
config.setProperty( "PreferredAuthentications", "gssapi-with-mic,publickey,password,keyboard-interactive" );
}
config.setProperty( "BatchMode", interactive ? "no" : "yes" );
session.setConfig( config );
session.setUserInfo( ui );
StringWriter stringWriter = new StringWriter();
try
{
session.connect();
if ( getKnownHostsProvider() != null )
{
PrintWriter w = new PrintWriter( stringWriter );
HostKeyRepository hkr = sch.getHostKeyRepository();
HostKey[] keys = hkr.getHostKey();
for ( int i = 0; keys != null && i < keys.length; i++ )
{
HostKey key = keys[i];