this.username = username;
}
public void run()
{
Connection conn = new Connection(hostname);
try
{
/*
*
* CONNECT AND VERIFY SERVER HOST KEY (with callback)
*
*/
String[] hostkeyAlgos = database.getPreferredServerHostkeyAlgorithmOrder(hostname);
if (hostkeyAlgos != null)
conn.setServerHostKeyAlgorithms(hostkeyAlgos);
conn.connect(new AdvancedVerifier());
/*
*
* AUTHENTICATION PHASE
*
*/
boolean enableKeyboardInteractive = true;
boolean enableDSA = true;
boolean enableRSA = true;
String lastError = null;
while (true)
{
if ((enableDSA || enableRSA) && conn.isAuthMethodAvailable(username, "publickey"))
{
if (enableDSA)
{
File key = new File(idDSAPath);
if (key.exists())
{
EnterSomethingDialog esd = new EnterSomethingDialog(loginFrame, "DSA Authentication",
new String[] { lastError, "Enter DSA private key password:" }, true);
esd.setVisible(true);
boolean res = conn.authenticateWithPublicKey(username, key, esd.answer);
if (res == true)
break;
lastError = "DSA authentication failed.";
}
enableDSA = false; // do not try again
}
if (enableRSA)
{
File key = new File(idRSAPath);
if (key.exists())
{
EnterSomethingDialog esd = new EnterSomethingDialog(loginFrame, "RSA Authentication",
new String[] { lastError, "Enter RSA private key password:" }, true);
esd.setVisible(true);
boolean res = conn.authenticateWithPublicKey(username, key, esd.answer);
if (res == true)
break;
lastError = "RSA authentication failed.";
}
enableRSA = false; // do not try again
}
continue;
}
if (enableKeyboardInteractive && conn.isAuthMethodAvailable(username, "keyboard-interactive"))
{
InteractiveLogic il = new InteractiveLogic(lastError);
boolean res = conn.authenticateWithKeyboardInteractive(username, il);
if (res == true)
break;
if (il.getPromptCount() == 0)
{
// aha. the server announced that it supports "keyboard-interactive", but when
// we asked for it, it just denied the request without sending us any prompt.
// That happens with some server versions/configurations.
// We just disable the "keyboard-interactive" method and notify the user.
lastError = "Keyboard-interactive does not work.";
enableKeyboardInteractive = false; // do not try this again
}
else
{
lastError = "Keyboard-interactive auth failed."; // try again, if possible
}
continue;
}
if (conn.isAuthMethodAvailable(username, "password"))
{
final EnterSomethingDialog esd = new EnterSomethingDialog(loginFrame,
"Password Authentication",
new String[] { lastError, "Enter password for " + username }, true);
esd.setVisible(true);
if (esd.answer == null)
throw new IOException("Login aborted by user");
boolean res = conn.authenticateWithPassword(username, esd.answer);
if (res == true)
break;
lastError = "Password authentication failed."; // try again, if possible
continue;
}
throw new IOException("No supported authentication methods available.");
}
/*
*
* AUTHENTICATION OK. DO SOMETHING.
*
*/
Session sess = conn.openSession();
int x_width = 90;
int y_width = 30;
sess.requestPTY("dumb", x_width, y_width, 0, 0, null);
sess.startShell();
TerminalDialog td = new TerminalDialog(loginFrame, username + "@" + hostname, sess, x_width, y_width);
/* The following call blocks until the dialog has been closed */
td.setVisible(true);
}
catch (IOException e)
{
//e.printStackTrace();
JOptionPane.showMessageDialog(loginFrame, "Exception: " + e.getMessage());
}
/*
*
* CLOSE THE CONNECTION.
*
*/
conn.close();
/*
*
* CLOSE THE LOGIN FRAME - APPLICATION WILL BE EXITED (no more frames)
*