From f23dbe6f4221fd59337087298ed9536c1a05ea63 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Sun, 4 Feb 2018 19:02:49 +0100 Subject: knownhosts: Add ssh_session_update_known_hosts() Signed-off-by: Andreas Schneider --- include/libssh/libssh.h | 2 ++ src/knownhosts.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h index 92ca5938..336e31da 100644 --- a/include/libssh/libssh.h +++ b/include/libssh/libssh.h @@ -541,6 +541,8 @@ LIBSSH_API enum ssh_known_hosts_e ssh_session_has_known_hosts_entry(ssh_session LIBSSH_API int ssh_session_export_known_hosts_entry(ssh_session session, char **pentry_string); +LIBSSH_API int ssh_session_update_known_hosts(ssh_session session); + /* LOGGING */ LIBSSH_API int ssh_set_log_level(int level); LIBSSH_API int ssh_get_log_level(void); diff --git a/src/knownhosts.c b/src/knownhosts.c index da96102d..bc789afc 100644 --- a/src/knownhosts.c +++ b/src/knownhosts.c @@ -560,3 +560,77 @@ int ssh_session_export_known_hosts_entry(ssh_session session, return SSH_OK; } + +/** + * @brief Add the current connected server to the known_hosts file. + * + * This adds the currently connected server to the known_hosts file by + * appending a new line at the end. + * + * @param[in] session The session to use to write the entry. + * + * @return SSH_OK on success, SSH_ERROR otherwise. + */ +int ssh_session_update_known_hosts(ssh_session session) +{ + FILE *fp = NULL; + char *entry = NULL; + char *dir = NULL; + size_t nwritten; + size_t len; + int rc; + + if (session->opts.knownhosts == NULL) { + rc = ssh_options_apply(session); + if (rc != SSH_OK) { + ssh_set_error(session, SSH_FATAL, "Can't find a known_hosts file"); + return SSH_ERROR; + } + } + + /* Check if directory exists and create it if not */ + dir = ssh_dirname(session->opts.knownhosts); + if (dir == NULL) { + ssh_set_error(session, SSH_FATAL, "%s", strerror(errno)); + return SSH_ERROR; + } + + rc = ssh_file_readaccess_ok(dir); + if (rc == 0) { + rc = ssh_mkdir(dir, 0700); + } else { + rc = 0; + } + SAFE_FREE(dir); + if (rc != 0) { + ssh_set_error(session, SSH_FATAL, + "Cannot create %s directory.", dir); + return SSH_ERROR; + } + + fp = fopen(session->opts.knownhosts, "a"); + if (fp == NULL) { + ssh_set_error(session, SSH_FATAL, + "Couldn't open known_hosts file %s for appending: %s", + session->opts.knownhosts, strerror(errno)); + return SSH_ERROR; + } + + rc = ssh_session_export_known_hosts_entry(session, &entry); + if (rc != SSH_OK) { + return rc; + } + + len = strlen(entry); + nwritten = fwrite(entry, sizeof(char), len, fp); + SAFE_FREE(entry); + fclose(fp); + if (nwritten != len || ferror(fp)) { + ssh_set_error(session, SSH_FATAL, + "Couldn't append to known_hosts file %s: %s", + session->opts.knownhosts, strerror(errno)); + return SSH_ERROR; + } + + return SSH_OK; +} -- cgit v1.2.3