[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] examples: add public key auth for specific key
[Thread Prev] | [Thread Next]
- Subject: [PATCH] examples: add public key auth for specific key
- From: Eric Bentley <ebentley66@xxxxxxxxx>
- Reply-to: libssh@xxxxxxxxxx
- Date: Mon, 27 Nov 2017 13:08:47 -0500
- To: libssh@xxxxxxxxxx
- Cc: Eric Bentley <ebentley66@xxxxxxxxx>
There was no example of using a specific key for authentication so I added one. Eric Bentley (1): examples: add public key auth for specific key examples/authentication.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) -- 2.6.0.GITFrom 044f825eb5480a6f0d32f3f83836230e577589a4 Mon Sep 17 00:00:00 2001 From: Eric Bentley <ebentley66@xxxxxxxxx> Date: Mon, 27 Nov 2017 12:56:29 -0500 Subject: [PATCH] examples: add public key auth for specific key Signed-off-by: ebentley66@xxxxxxxxx --- examples/authentication.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/examples/authentication.c b/examples/authentication.c index b9f70f5..89459e4 100644 --- a/examples/authentication.c +++ b/examples/authentication.c @@ -99,6 +99,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]; // +".pub" + int rc; + + snprintf(pubkey, 132, "%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)); } @@ -136,6 +169,34 @@ int authenticate_console(ssh_session session){ } else if (rc == SSH_AUTH_SUCCESS) { break; } + { + char buffer[128] = {0}; + char *p = NULL; + + printf("auto 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"; -- 2.6.0.GIT
Archive administrator: postmaster@lists.cynapses.org