diff options
author | Matt Johnston <matt@ucc.asn.au> | 2014-11-08 22:15:16 +0800 |
---|---|---|
committer | Matt Johnston <matt@ucc.asn.au> | 2014-11-08 22:15:16 +0800 |
commit | 4ba830fc31c056aaada774ce29bb7d4e136b5dcd (patch) | |
tree | eb8b8b6710dbe183bad0ebc252f0278943049cf1 /svr-kex.c | |
parent | 3022a460394c811fb3256c56db696b8ad1a0ad6e (diff) |
Make sure hostkeys are flushed to disk to avoid empty files if the power
fails. Based on patch from Peter Korsgaard
Diffstat (limited to 'svr-kex.c')
-rw-r--r-- | svr-kex.c | 22 |
1 files changed, 22 insertions, 0 deletions
@@ -84,7 +84,25 @@ void recv_msg_kexdh_init() { TRACE(("leave recv_msg_kexdh_init")) } + #ifdef DROPBEAR_DELAY_HOSTKEY + +static void fsync_parent_dir(const char* fn) { +#ifdef HAVE_LIBGEN_H + char *fn_dir = m_strdup(fn); + char *dir = dirname(fn_dir); + /* some OSes need the fd to be writable for fsync */ + int dirfd = open(dir, O_RDWR); + + if (dirfd != -1) { + fsync(dirfd); + m_close(dirfd); + } + + free(fn_dir); +#endif +} + static void svr_ensure_hostkey() { const char* fn = NULL; @@ -142,6 +160,10 @@ static void svr_ensure_hostkey() { } } + /* ensure directory update is flushed to disk, otherwise we can end up + with zero-byte hostkey files if the power goes off */ + fsync_parent_dir(fn); + ret = readhostkey(fn, svr_opts.hostkey, &type); if (ret == DROPBEAR_SUCCESS) { |