diff options
author | Andreas Schneider <asn@cryptomilk.org> | 2018-02-04 19:02:49 +0100 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2018-06-04 11:20:28 +0200 |
commit | f23dbe6f4221fd59337087298ed9536c1a05ea63 (patch) | |
tree | 79a5291a97d7db888a3239b8abbdc4c1823cb047 /src | |
parent | 974e1831a0ecc09a56135892c2f272781eafc3b7 (diff) | |
download | libssh-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>
Diffstat (limited to 'src')
-rw-r--r-- | src/knownhosts.c | 74 |
1 files changed, 74 insertions, 0 deletions
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; +} |