diff options
author | Andreas Schneider <asn@cryptomilk.org> | 2011-08-28 14:21:56 +0200 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2011-08-28 14:21:56 +0200 |
commit | bce2c22e454504aa5586e815f01c5989b84ea9f8 (patch) | |
tree | ba1505a1218f4d0ac67e77e03c3efe1fbf976e4a /src/legacy.c | |
parent | 29ecccb96d495c5a99481fea19a4906d596ed925 (diff) | |
download | libssh-bce2c22e454504aa5586e815f01c5989b84ea9f8.tar.gz libssh-bce2c22e454504aa5586e815f01c5989b84ea9f8.tar.xz libssh-bce2c22e454504aa5586e815f01c5989b84ea9f8.zip |
keyfiles: Make ssh_try_publickey_from_file() legacy.
Diffstat (limited to 'src/legacy.c')
-rw-r--r-- | src/legacy.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/legacy.c b/src/legacy.c index 9550834..ac98478 100644 --- a/src/legacy.c +++ b/src/legacy.c @@ -572,6 +572,71 @@ int ssh_publickey_to_file(ssh_session session, return SSH_OK; } +int ssh_try_publickey_from_file(ssh_session session, + const char *keyfile, + ssh_string *publickey, + int *type) { + char *pubkey_file; + size_t len; + ssh_string pubkey_string; + int pubkey_type; + + if (session == NULL || keyfile == NULL || publickey == NULL || type == NULL) { + return -1; + } + + if (session->sshdir == NULL) { + if (ssh_options_apply(session) < 0) { + return -1; + } + } + + ssh_log(session, SSH_LOG_PACKET, "Trying to open privatekey %s", keyfile); + if (!ssh_file_readaccess_ok(keyfile)) { + ssh_log(session, SSH_LOG_PACKET, "Failed to open privatekey %s", keyfile); + return -1; + } + + len = strlen(keyfile) + 5; + pubkey_file = malloc(len); + if (pubkey_file == NULL) { + return -1; + } + snprintf(pubkey_file, len, "%s.pub", keyfile); + + ssh_log(session, SSH_LOG_PACKET, "Trying to open publickey %s", + pubkey_file); + if (!ssh_file_readaccess_ok(pubkey_file)) { + ssh_log(session, SSH_LOG_PACKET, "Failed to open publickey %s", + pubkey_file); + SAFE_FREE(pubkey_file); + return 1; + } + + ssh_log(session, SSH_LOG_PACKET, "Success opening public and private key"); + + /* + * We are sure both the private and public key file is readable. We return + * the public as a string, and the private filename as an argument + */ + pubkey_string = publickey_from_file(session, pubkey_file, &pubkey_type); + if (pubkey_string == NULL) { + ssh_log(session, SSH_LOG_PACKET, + "Wasn't able to open public key file %s: %s", + pubkey_file, + ssh_get_error(session)); + SAFE_FREE(pubkey_file); + return -1; + } + + SAFE_FREE(pubkey_file); + + *publickey = pubkey_string; + *type = pubkey_type; + + return 0; +} + /**************************************************************************** * SERVER SUPPORT ****************************************************************************/ |