comparison svr-kex.c @ 982:fd2e8bbb0333

Make sure hostkeys are flushed to disk to avoid empty files if the power fails. Based on patch from Peter Korsgaard
author Matt Johnston <matt@ucc.asn.au>
date Sat, 08 Nov 2014 22:15:16 +0800
parents cbc73a5aefb0
children 2b62f26cf808
comparison
equal deleted inserted replaced
981:b2fc6607b530 982:fd2e8bbb0333
82 send_msg_newkeys(); 82 send_msg_newkeys();
83 ses.requirenext = SSH_MSG_NEWKEYS; 83 ses.requirenext = SSH_MSG_NEWKEYS;
84 TRACE(("leave recv_msg_kexdh_init")) 84 TRACE(("leave recv_msg_kexdh_init"))
85 } 85 }
86 86
87
87 #ifdef DROPBEAR_DELAY_HOSTKEY 88 #ifdef DROPBEAR_DELAY_HOSTKEY
89
90 static void fsync_parent_dir(const char* fn) {
91 #ifdef HAVE_LIBGEN_H
92 char *fn_dir = m_strdup(fn);
93 char *dir = dirname(fn_dir);
94 /* some OSes need the fd to be writable for fsync */
95 int dirfd = open(dir, O_RDWR);
96
97 if (dirfd != -1) {
98 fsync(dirfd);
99 m_close(dirfd);
100 }
101
102 free(fn_dir);
103 #endif
104 }
105
88 static void svr_ensure_hostkey() { 106 static void svr_ensure_hostkey() {
89 107
90 const char* fn = NULL; 108 const char* fn = NULL;
91 char *fn_temp = NULL; 109 char *fn_temp = NULL;
92 enum signkey_type type = ses.newkeys->algo_hostkey; 110 enum signkey_type type = ses.newkeys->algo_hostkey;
139 strerror(errno)); 157 strerror(errno));
140 /* XXX fallback to non-atomic copy for some filesystems? */ 158 /* XXX fallback to non-atomic copy for some filesystems? */
141 goto out; 159 goto out;
142 } 160 }
143 } 161 }
162
163 /* ensure directory update is flushed to disk, otherwise we can end up
164 with zero-byte hostkey files if the power goes off */
165 fsync_parent_dir(fn);
144 166
145 ret = readhostkey(fn, svr_opts.hostkey, &type); 167 ret = readhostkey(fn, svr_opts.hostkey, &type);
146 168
147 if (ret == DROPBEAR_SUCCESS) { 169 if (ret == DROPBEAR_SUCCESS) {
148 char *fp = NULL; 170 char *fp = NULL;