aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Bentley <ebentley66@gmail.com>2017-11-27 13:08:47 -0500
committerAndreas Schneider <asn@cryptomilk.org>2019-01-24 17:48:08 +0100
commit83d86ef6a5d0d755f2d3f2e453f61cba8a7c0d9a (patch)
tree85aae5e95cf64016b63611ed3c2d23816fbe32bd
parentdb67fcbe8885369365239a837220f453979e6d20 (diff)
downloadlibssh-83d86ef6a5d0d755f2d3f2e453f61cba8a7c0d9a.tar.gz
libssh-83d86ef6a5d0d755f2d3f2e453f61cba8a7c0d9a.tar.xz
libssh-83d86ef6a5d0d755f2d3f2e453f61cba8a7c0d9a.zip
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 <ebentley66@gmail.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
-rw-r--r--examples/authentication.c62
1 files changed, 62 insertions, 0 deletions
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) {