diff options
-rw-r--r-- | include/libssh/legacy.h | 2 | ||||
-rw-r--r-- | include/libssh/libssh.h | 2 | ||||
-rw-r--r-- | src/auth.c | 69 | ||||
-rw-r--r-- | src/legacy.c | 46 |
4 files changed, 49 insertions, 70 deletions
diff --git a/include/libssh/legacy.h b/include/libssh/legacy.h index c8225a2..db151f8 100644 --- a/include/libssh/legacy.h +++ b/include/libssh/legacy.h @@ -38,6 +38,8 @@ LIBSSH_API int ssh_userauth_agent_pubkey(ssh_session session, const char *userna ssh_public_key publickey); #endif LIBSSH_API int ssh_userauth_autopubkey(ssh_session session, const char *passphrase); +LIBSSH_API int ssh_userauth_privatekey_file(ssh_session session, const char *username, + const char *filename, const char *passphrase); LIBSSH_API void buffer_free(ssh_buffer buffer); LIBSSH_API void *buffer_get(ssh_buffer buffer); diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h index 843cf2d..62a210a 100644 --- a/include/libssh/libssh.h +++ b/include/libssh/libssh.h @@ -518,8 +518,6 @@ LIBSSH_API int ssh_userauth_kbdint_getnanswers(ssh_session session); LIBSSH_API const char *ssh_userauth_kbdint_getanswer(ssh_session session, unsigned int i); LIBSSH_API int ssh_userauth_kbdint_setanswer(ssh_session session, unsigned int i, const char *answer); -LIBSSH_API int ssh_userauth_privatekey_file(ssh_session session, const char *username, - const char *filename, const char *passphrase); LIBSSH_API const char *ssh_version(int req_version); LIBSSH_API int ssh_write_knownhost(ssh_session session); @@ -1341,76 +1341,9 @@ fail: return SSH_AUTH_ERROR; } -/** - * @brief Try to authenticate through a private key file. - * - * @param[in] session The ssh session to use. - * - * @param[in] username The username to authenticate. You can specify NULL if - * ssh_option_set_username() has been used. You cannot try - * two different logins in a row. - * - * @param[in] filename Filename containing the private key. - * - * @param[in] passphrase Passphrase to decrypt the private key. Set to null if - * none is needed or it is unknown. - * - * @returns SSH_AUTH_ERROR: A serious error happened.\n - * SSH_AUTH_DENIED: Authentication failed: use another method.\n - * SSH_AUTH_PARTIAL: You've been partially authenticated, you still - * have to use another method.\n - * SSH_AUTH_SUCCESS: Authentication successful.\n - * SSH_AUTH_AGAIN: In nonblocking mode, you've got to call this again - * later. - * - * @see publickey_from_file() - * @see privatekey_from_file() - * @see privatekey_free() - * @see ssh_userauth_pubkey() - */ -int ssh_userauth_privatekey_file(ssh_session session, const char *username, - const char *filename, const char *passphrase) { - char *pubkeyfile = NULL; - ssh_string pubkey = NULL; - ssh_private_key privkey = NULL; - int type = 0; - int rc = SSH_AUTH_ERROR; - - enter_function(); - - pubkeyfile = malloc(strlen(filename) + 1 + 4); - if (pubkeyfile == NULL) { - ssh_set_error_oom(session); - leave_function(); - return SSH_AUTH_ERROR; - } - sprintf(pubkeyfile, "%s.pub", filename); - - pubkey = publickey_from_file(session, pubkeyfile, &type); - if (pubkey == NULL) { - ssh_log(session, SSH_LOG_RARE, "Public key file %s not found. Trying to generate it.", pubkeyfile); - /* auto-detect the key type with type=0 */ - privkey = privatekey_from_file(session, filename, 0, passphrase); - } else { - ssh_log(session, SSH_LOG_RARE, "Public key file %s loaded.", pubkeyfile); - privkey = privatekey_from_file(session, filename, type, passphrase); - } - if (privkey == NULL) { - goto error; - } - /* ssh_userauth_pubkey is responsible for taking care of null-pubkey */ - rc = ssh_userauth_pubkey(session, username, pubkey, privkey); - privatekey_free(privkey); - -error: - SAFE_FREE(pubkeyfile); - ssh_string_free(pubkey); - - leave_function(); - return rc; -} #ifndef _WIN32 +/* LEGACY */ int ssh_userauth_agent_pubkey(ssh_session session, const char *username, ssh_public_key publickey) diff --git a/src/legacy.c b/src/legacy.c index 5abe18e..c223fa3 100644 --- a/src/legacy.c +++ b/src/legacy.c @@ -26,6 +26,8 @@ #include "config.h" +#include <stdio.h> + #include <libssh/priv.h> #include <libssh/session.h> #include <libssh/server.h> @@ -93,6 +95,50 @@ int ssh_userauth_autopubkey(ssh_session session, const char *passphrase) { return ssh_userauth_publickey_auto(session, NULL, passphrase); } +int ssh_userauth_privatekey_file(ssh_session session, + const char *username, + const char *filename, + const char *passphrase) { + char *pubkeyfile = NULL; + ssh_string pubkey = NULL; + ssh_private_key privkey = NULL; + int type = 0; + int rc = SSH_AUTH_ERROR; + + enter_function(); + + pubkeyfile = malloc(strlen(filename) + 1 + 4); + if (pubkeyfile == NULL) { + ssh_set_error_oom(session); + leave_function(); + return SSH_AUTH_ERROR; + } + sprintf(pubkeyfile, "%s.pub", filename); + + pubkey = publickey_from_file(session, pubkeyfile, &type); + if (pubkey == NULL) { + ssh_log(session, SSH_LOG_RARE, "Public key file %s not found. Trying to generate it.", pubkeyfile); + /* auto-detect the key type with type=0 */ + privkey = privatekey_from_file(session, filename, 0, passphrase); + } else { + ssh_log(session, SSH_LOG_RARE, "Public key file %s loaded.", pubkeyfile); + privkey = privatekey_from_file(session, filename, type, passphrase); + } + if (privkey == NULL) { + goto error; + } + /* ssh_userauth_pubkey is responsible for taking care of null-pubkey */ + rc = ssh_userauth_pubkey(session, username, pubkey, privkey); + privatekey_free(privkey); + +error: + SAFE_FREE(pubkeyfile); + ssh_string_free(pubkey); + + leave_function(); + return rc; +} + /* BUFFER FUNCTIONS */ void buffer_free(ssh_buffer buffer){ |