From 83d86ef6a5d0d755f2d3f2e453f61cba8a7c0d9a Mon Sep 17 00:00:00 2001 From: Eric Bentley Date: Mon, 27 Nov 2017 13:08:47 -0500 Subject: [PATCH] examples: add public key auth for specific key There was no example of using a specific key for authentication so I added one. Signed-off-by: Eric Bentley Reviewed-by: Andreas Schneider --- examples/authentication.c | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/examples/authentication.c b/examples/authentication.c index 9e5b94c9..375987af 100644 --- a/examples/authentication.c +++ b/examples/authentication.c @@ -100,6 +100,39 @@ int authenticate_kbdint(ssh_session session, const char *password) return err; } +static int auth_keyfile(ssh_session session, char* keyfile) +{ + ssh_key key = NULL; + char pubkey[132] = {0}; // +".pub" + int rc; + + snprintf(pubkey, sizeof(pubkey), "%s.pub", keyfile); + + rc = ssh_pki_import_pubkey_file( pubkey, &key); + + if (rc != SSH_OK) + return SSH_AUTH_DENIED; + + rc = ssh_userauth_try_publickey(session, NULL, key); + + ssh_key_free(key); + + if (rc!=SSH_AUTH_SUCCESS) + return SSH_AUTH_DENIED; + + rc = ssh_pki_import_privkey_file(keyfile, NULL, NULL, NULL, &key); + + if (rc != SSH_OK) + return SSH_AUTH_DENIED; + + rc = ssh_userauth_publickey(session, NULL, key); + + ssh_key_free(key); + + return rc; +} + + static void error(ssh_session session) { fprintf(stderr,"Authentication failed: %s\n",ssh_get_error(session)); @@ -140,6 +173,35 @@ int authenticate_console(ssh_session session) break; } } + { + char buffer[128] = {0}; + char *p = NULL; + + printf("Automatic pubkey failed. " + "Do you want to try a specific key? (y/n)\n"); + if (fgets(buffer, sizeof(buffer), stdin) == NULL) { + break; + } + if ((buffer[0]=='Y') || (buffer[0]=='y')) { + printf("private key filename: "); + + if (fgets(buffer, sizeof(buffer), stdin) == NULL) { + return SSH_AUTH_ERROR; + } + + buffer[sizeof(buffer) - 1] = '\0'; + if ((p = strchr(buffer, '\n'))) { + *p = '\0'; + } + + rc = auth_keyfile(session, buffer); + + if(rc == SSH_AUTH_SUCCESS) { + break; + } + fprintf(stderr, "failed with key\n"); + } + } // Try to authenticate with keyboard interactive"; if (method & SSH_AUTH_METHOD_INTERACTIVE) {