summaryrefslogtreecommitdiffstats
path: root/src/legacy.c
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2011-08-28 14:19:07 +0200
committerAndreas Schneider <asn@cryptomilk.org>2011-08-28 14:19:07 +0200
commit29ecccb96d495c5a99481fea19a4906d596ed925 (patch)
tree394a14b08e8d25cbf72f8fe855b1b2a7313d30b6 /src/legacy.c
parent16b47496c1a91dbdf139528caf39326d456cd7d3 (diff)
downloadlibssh-29ecccb96d495c5a99481fea19a4906d596ed925.tar.gz
libssh-29ecccb96d495c5a99481fea19a4906d596ed925.tar.xz
libssh-29ecccb96d495c5a99481fea19a4906d596ed925.zip
keyfiles: Make ssh_try_publickey_from_file() legacy.
Diffstat (limited to 'src/legacy.c')
-rw-r--r--src/legacy.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/legacy.c b/src/legacy.c
index 2789b1c..9550834 100644
--- a/src/legacy.c
+++ b/src/legacy.c
@@ -26,6 +26,7 @@
#include "config.h"
+#include <errno.h>
#include <stdio.h>
#include <libssh/priv.h>
@@ -34,6 +35,7 @@
#include <libssh/buffer.h>
#include <libssh/pki.h>
#include "libssh/pki_priv.h"
+#include <libssh/misc.h>
#include <libssh/keys.h>
#include <libssh/keyfiles.h>
@@ -502,6 +504,74 @@ ssh_string publickey_to_string(ssh_public_key pubkey) {
return key_blob;
}
+int ssh_publickey_to_file(ssh_session session,
+ const char *file,
+ ssh_string pubkey,
+ int type)
+{
+ FILE *fp;
+ char *user;
+ char buffer[1024];
+ char host[256];
+ unsigned char *pubkey_64;
+ size_t len;
+ int rc;
+ if(session==NULL)
+ return SSH_ERROR;
+ if(file==NULL || pubkey==NULL){
+ ssh_set_error(session, SSH_FATAL, "Invalid parameters");
+ return SSH_ERROR;
+ }
+ pubkey_64 = bin_to_base64(string_data(pubkey), ssh_string_len(pubkey));
+ if (pubkey_64 == NULL) {
+ return SSH_ERROR;
+ }
+
+ user = ssh_get_local_username();
+ if (user == NULL) {
+ SAFE_FREE(pubkey_64);
+ return SSH_ERROR;
+ }
+
+ rc = gethostname(host, sizeof(host));
+ if (rc < 0) {
+ SAFE_FREE(user);
+ SAFE_FREE(pubkey_64);
+ return SSH_ERROR;
+ }
+
+ snprintf(buffer, sizeof(buffer), "%s %s %s@%s\n",
+ ssh_type_to_char(type),
+ pubkey_64,
+ user,
+ host);
+
+ SAFE_FREE(pubkey_64);
+ SAFE_FREE(user);
+
+ ssh_log(session, SSH_LOG_RARE, "Trying to write public key file: %s", file);
+ ssh_log(session, SSH_LOG_PACKET, "public key file content: %s", buffer);
+
+ fp = fopen(file, "w+");
+ if (fp == NULL) {
+ ssh_set_error(session, SSH_REQUEST_DENIED,
+ "Error opening %s: %s", file, strerror(errno));
+ return SSH_ERROR;
+ }
+
+ len = strlen(buffer);
+ if (fwrite(buffer, len, 1, fp) != 1 || ferror(fp)) {
+ ssh_set_error(session, SSH_REQUEST_DENIED,
+ "Unable to write to %s", file);
+ fclose(fp);
+ unlink(file);
+ return SSH_ERROR;
+ }
+
+ fclose(fp);
+ return SSH_OK;
+}
+
/****************************************************************************
* SERVER SUPPORT
****************************************************************************/