C
C
SSH Keyboard-Interactive Authentication
See more SSH Examples
Demonstrates keyboard-interactive authentication with an SSH server. StartKeyboardAuth returns XML describing the server's prompts, and ContinueKeyboardAuth submits each response. Authentication is complete when the returned XML contains either a success or an error node.
Background: Keyboard-interactive is SSH's flexible, prompt-driven method: rather than assuming a single password, the server asks one or more questions — a password, a one-time code, a security question — and the client answers each. This is how SSH supports two-factor and other challenge-response schemes. The prompt XML also indicates whether each response should be echoed, so a client knows when to mask input. A server may issue several rounds, so a robust implementation loops until it sees success or error rather than assuming one exchange is enough.
Chilkat C Downloads
#include <C_CkSsh.h>
#include <C_CkXml.h>
void ChilkatSample(void)
{
BOOL success;
HCkSsh ssh;
const char *hostname;
int port;
const char *xmlResponse;
HCkXml xml;
const char *password;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Demonstrates keyboard-interactive authentication with an SSH server. The server sends one or
// more prompts as XML, and the application answers each with ContinueKeyboardAuth.
ssh = CkSsh_Create();
CkSsh_putConnectTimeoutMs(ssh,5000);
CkSsh_putReadTimeoutMs(ssh,15000);
hostname = "ssh.example.com";
port = 22;
success = CkSsh_Connect(ssh,hostname,port);
if (success == FALSE) {
printf("%s\n",CkSsh_lastErrorText(ssh));
CkSsh_Dispose(ssh);
return;
}
// Begin keyboard-interactive authentication. The returned XML describes the server's prompts.
xmlResponse = CkSsh_startKeyboardAuth(ssh,"mySshLogin");
if (CkSsh_getLastMethodSuccess(ssh) == FALSE) {
printf("%s\n",CkSsh_lastErrorText(ssh));
CkSsh_Dispose(ssh);
return;
}
// If the server sent a user authentication banner, an application may display it before
// prompting.
printf("UserAuthBanner: %s\n",CkSsh_userAuthBanner(ssh));
xml = CkXml_Create();
success = CkXml_LoadXml(xml,xmlResponse);
if (success == FALSE) {
printf("%s\n",CkXml_lastErrorText(xml));
CkSsh_Dispose(ssh);
CkXml_Dispose(xml);
return;
}
// Authentication is complete when the XML contains either a "success" or an "error" node.
if (CkXml_HasChildWithTag(xml,"success")) {
printf("No password required, already authenticated.\n");
CkSsh_Dispose(ssh);
CkXml_Dispose(xml);
return;
}
if (CkXml_HasChildWithTag(xml,"error")) {
printf("Authentication failed.\n");
CkSsh_Dispose(ssh);
CkXml_Dispose(xml);
return;
}
// Normally you would not hard-code the password in source. You should instead obtain it
// from an interactive prompt, environment variable, or a secrets vault.
password = "mySshPassword";
// Answer the prompt. Typically one call is enough, but a server may issue several rounds of
// prompts, so a robust client loops until it sees "success" or "error".
xmlResponse = CkSsh_continueKeyboardAuth(ssh,password);
if (CkSsh_getLastMethodSuccess(ssh) == FALSE) {
printf("%s\n",CkSsh_lastErrorText(ssh));
CkSsh_Dispose(ssh);
CkXml_Dispose(xml);
return;
}
success = CkXml_LoadXml(xml,xmlResponse);
if (success == FALSE) {
printf("%s\n",CkXml_lastErrorText(xml));
CkSsh_Dispose(ssh);
CkXml_Dispose(xml);
return;
}
if (CkXml_HasChildWithTag(xml,"success")) {
printf("SSH keyboard-interactive authentication successful.\n");
CkSsh_Dispose(ssh);
CkXml_Dispose(xml);
return;
}
if (CkXml_HasChildWithTag(xml,"error")) {
printf("Authentication failed.\n");
}
CkSsh_Dispose(ssh);
CkXml_Dispose(xml);
}