aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2018-02-04 19:02:49 +0100
committerAndreas Schneider <asn@cryptomilk.org>2018-06-04 11:20:28 +0200
commitf23dbe6f4221fd59337087298ed9536c1a05ea63 (patch)
tree79a5291a97d7db888a3239b8abbdc4c1823cb047
parent974e1831a0ecc09a56135892c2f272781eafc3b7 (diff)
downloadlibssh-f23dbe6f4221fd59337087298ed9536c1a05ea63.tar.gz
libssh-f23dbe6f4221fd59337087298ed9536c1a05ea63.tar.xz
libssh-f23dbe6f4221fd59337087298ed9536c1a05ea63.zip
knownhosts: Add ssh_session_update_known_hosts()
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
-rw-r--r--include/libssh/libssh.h2
-rw-r--r--src/knownhosts.c74
2 files changed, 76 insertions, 0 deletions
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;
+}