From d9a50f04e705e561be31b2842fc91d3c28c3a4a1 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Wed, 26 Aug 2009 09:39:21 +0200 Subject: Fix ssh_write_knownhost() if ~/.ssh doesn't exist. --- libssh/keyfiles.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'libssh') diff --git a/libssh/keyfiles.c b/libssh/keyfiles.c index ed786e59..55b2c778 100644 --- a/libssh/keyfiles.c +++ b/libssh/keyfiles.c @@ -22,12 +22,14 @@ * MA 02111-1307, USA. */ +#include +#include +#include +#include +#include #include #include -#include #include -#include -#include #ifndef _WIN32 #include @@ -1446,6 +1448,7 @@ int ssh_write_knownhost(SSH_SESSION *session) { unsigned char *pubkey_64; char buffer[4096] = {0}; FILE *file; + char *dir; size_t len = 0; if (ssh_options_default_known_hosts_file(session->options) < 0) { @@ -1459,6 +1462,22 @@ int ssh_write_knownhost(SSH_SESSION *session) { return -1; } + /* Check if ~/.ssh exists and create it if not */ + dir = ssh_dirname(session->options->known_hosts_file); + if (dir == NULL) { + ssh_set_error(session, SSH_FATAL, "%s", strerror(errno)); + return -1; + } + if (! ssh_file_readaccess_ok(dir)) { + if (mkdir(dir, 0700) < 0) { + ssh_set_error(session, SSH_FATAL, + "Cannot create %s directory.", dir); + SAFE_FREE(dir); + return -1; + } + } + SAFE_FREE(dir); + file = fopen(session->options->known_hosts_file, "a"); if (file == NULL) { ssh_set_error(session, SSH_FATAL, -- cgit v1.2.3