comparison cli-kex.c @ 80:07bcdccb3577

create known_hosts properly
author Matt Johnston <matt@ucc.asn.au>
date Mon, 16 Aug 2004 14:46:13 +0000
parents 5a55bd66707f
children 29a5c7c62350
comparison
equal deleted inserted replaced
79:5a55bd66707f 80:07bcdccb3577
145 145
146 snprintf(filename, len+18, "%s/.ssh", pw->pw_dir); 146 snprintf(filename, len+18, "%s/.ssh", pw->pw_dir);
147 /* Check that ~/.ssh exists - easiest way is just to mkdir */ 147 /* Check that ~/.ssh exists - easiest way is just to mkdir */
148 if (mkdir(filename, S_IRWXU) != 0) { 148 if (mkdir(filename, S_IRWXU) != 0) {
149 if (errno != EEXIST) { 149 if (errno != EEXIST) {
150 dropbear_log(LOG_INFO, "Warning: failed creating ~/.ssh: %s",
151 strerror(errno));
152 TRACE(("mkdir didn't work: %s", strerror(errno)));
150 ask_to_confirm(keyblob, keybloblen); 153 ask_to_confirm(keyblob, keybloblen);
151 goto out; /* only get here on success */ 154 goto out; /* only get here on success */
152 } 155 }
153 } 156 }
154 157
155 snprintf(filename, len+18, "%s/.ssh/known_hosts", pw->pw_dir); 158 snprintf(filename, len+18, "%s/.ssh/known_hosts", pw->pw_dir);
156 hostsfile = fopen(filename, "r+"); 159 hostsfile = fopen(filename, "a+");
160 fseek(hostsfile, 0, SEEK_SET);
157 161
158 /* We mightn't have been able to open it if it was read-only */ 162 /* We mightn't have been able to open it if it was read-only */
159 if (hostsfile == NULL && (errno == EACCES || errno == EROFS)) { 163 if (hostsfile == NULL && (errno == EACCES || errno == EROFS)) {
164 TRACE(("trying readonly: %s", strerror(errno)));
160 readonly = 1; 165 readonly = 1;
161 hostsfile = fopen(filename, "r"); 166 hostsfile = fopen(filename, "r");
162 } 167 }
163 168
164 if (hostsfile == NULL) { 169 if (hostsfile == NULL) {
170 TRACE(("hostsfile didn't open: %s", strerror(errno)));
165 ask_to_confirm(keyblob, keybloblen); 171 ask_to_confirm(keyblob, keybloblen);
166 goto out; /* We only get here on success */ 172 goto out; /* We only get here on success */
167 } 173 }
168 174
169 line = buf_new(MAX_KNOWNHOSTS_LINE); 175 line = buf_new(MAX_KNOWNHOSTS_LINE);
226 ask_to_confirm(keyblob, keybloblen); 232 ask_to_confirm(keyblob, keybloblen);
227 233
228 /* If we get here, they said yes */ 234 /* If we get here, they said yes */
229 235
230 if (readonly) { 236 if (readonly) {
237 TRACE(("readonly"));
231 goto out; 238 goto out;
232 } 239 }
233 240
234 /* put the new entry in the file */ 241 /* put the new entry in the file */
235 fseek(hostsfile, 0, SEEK_END); 242 fseek(hostsfile, 0, SEEK_END); /* In case it wasn't opened append */
236 buf_setpos(line, 0); 243 buf_setpos(line, 0);
237 buf_setlen(line, 0); 244 buf_setlen(line, 0);
238 buf_putbytes(line, ses.remotehost, hostlen); 245 buf_putbytes(line, ses.remotehost, hostlen);
239 buf_putbyte(line, ' '); 246 buf_putbyte(line, ' ');
240 buf_putbytes(line, algoname, algolen); 247 buf_putbytes(line, algoname, algolen);