diff options
author | Andreas Schneider <mail@cynapses.org> | 2009-08-26 09:48:35 +0200 |
---|---|---|
committer | Andreas Schneider <mail@cynapses.org> | 2009-08-26 09:48:35 +0200 |
commit | a501d63c8a76e2a737cf06228300dcf4198c6479 (patch) | |
tree | 041b46473ff2125a5cacd38a78753e2c8f83df2a /libssh | |
parent | 160053bc3912bd204156c229ceed836795b1959b (diff) | |
download | libssh-a501d63c8a76e2a737cf06228300dcf4198c6479.tar.gz libssh-a501d63c8a76e2a737cf06228300dcf4198c6479.tar.xz libssh-a501d63c8a76e2a737cf06228300dcf4198c6479.zip |
Fix ssh_write_knownhost() if ~/.ssh doesn't exist.
Diffstat (limited to 'libssh')
-rw-r--r-- | libssh/keyfiles.c | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/libssh/keyfiles.c b/libssh/keyfiles.c index bac0ea2f..d219dc10 100644 --- a/libssh/keyfiles.c +++ b/libssh/keyfiles.c @@ -22,13 +22,15 @@ * MA 02111-1307, USA. */ +#include <ctype.h> +#include <errno.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/types.h> #include <stdio.h> #include <string.h> -#include <errno.h> #include <unistd.h> #include <stdlib.h> -#include <fcntl.h> -#include <ctype.h> #ifndef _WIN32 #include <arpa/inet.h> @@ -1447,6 +1449,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) { @@ -1460,6 +1463,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, |