1709795f |
1 | /* |
2 | * uxstore.c: Unix-specific implementation of the interface defined |
3 | * in storage.h. |
4 | */ |
5 | |
6 | #include <stdio.h> |
7 | #include <stdlib.h> |
3f935d5b |
8 | #include <string.h> |
8dacc30e |
9 | #include <assert.h> |
3f935d5b |
10 | #include <errno.h> |
0ac15bdc |
11 | #include <ctype.h> |
4afa6d82 |
12 | #include <limits.h> |
c5e438ec |
13 | #include <unistd.h> |
14 | #include <fcntl.h> |
8dacc30e |
15 | #include <dirent.h> |
c5e438ec |
16 | #include <sys/stat.h> |
17 | #include <sys/types.h> |
c18ed097 |
18 | #include <pwd.h> |
1709795f |
19 | #include "putty.h" |
20 | #include "storage.h" |
0ac15bdc |
21 | #include "tree234.h" |
1709795f |
22 | |
4afa6d82 |
23 | #ifdef PATH_MAX |
24 | #define FNLEN PATH_MAX |
25 | #else |
26 | #define FNLEN 1024 /* XXX */ |
27 | #endif |
28 | |
8dacc30e |
29 | enum { |
be57bcef |
30 | INDEX_DIR, INDEX_HOSTKEYS, INDEX_HOSTKEYS_TMP, INDEX_RANDSEED, |
8dacc30e |
31 | INDEX_SESSIONDIR, INDEX_SESSION, |
32 | }; |
33 | |
34 | static const char hex[16] = "0123456789ABCDEF"; |
35 | |
36 | static char *mungestr(const char *in) |
37 | { |
38 | char *out, *ret; |
39 | |
797f6ff3 |
40 | if (!in || !*in) |
8dacc30e |
41 | in = "Default Settings"; |
42 | |
43 | ret = out = snewn(3*strlen(in)+1, char); |
44 | |
45 | while (*in) { |
46 | /* |
47 | * There are remarkably few punctuation characters that |
48 | * aren't shell-special in some way or likely to be used as |
49 | * separators in some file format or another! Hence we use |
50 | * opt-in for safe characters rather than opt-out for |
51 | * specific unsafe ones... |
52 | */ |
53 | if (*in!='+' && *in!='-' && *in!='.' && *in!='@' && *in!='_' && |
54 | !(*in >= '0' && *in <= '9') && |
55 | !(*in >= 'A' && *in <= 'Z') && |
56 | !(*in >= 'a' && *in <= 'z')) { |
57 | *out++ = '%'; |
58 | *out++ = hex[((unsigned char) *in) >> 4]; |
59 | *out++ = hex[((unsigned char) *in) & 15]; |
60 | } else |
61 | *out++ = *in; |
62 | in++; |
63 | } |
64 | *out = '\0'; |
65 | return ret; |
66 | } |
67 | |
68 | static char *unmungestr(const char *in) |
69 | { |
70 | char *out, *ret; |
71 | out = ret = snewn(strlen(in)+1, char); |
72 | while (*in) { |
73 | if (*in == '%' && in[1] && in[2]) { |
74 | int i, j; |
75 | |
76 | i = in[1] - '0'; |
77 | i -= (i > 9 ? 7 : 0); |
78 | j = in[2] - '0'; |
79 | j -= (j > 9 ? 7 : 0); |
80 | |
81 | *out++ = (i << 4) + j; |
82 | in += 3; |
83 | } else { |
84 | *out++ = *in++; |
85 | } |
86 | } |
87 | *out = '\0'; |
88 | return ret; |
89 | } |
90 | |
c18ed097 |
91 | static char *make_filename(int index, const char *subname) |
8dacc30e |
92 | { |
c18ed097 |
93 | char *env, *tmp, *ret; |
94 | |
95 | /* |
96 | * Allow override of the PuTTY configuration location, and of |
97 | * specific subparts of it, by means of environment variables. |
98 | */ |
99 | if (index == INDEX_DIR) { |
100 | struct passwd *pwd; |
101 | |
102 | env = getenv("PUTTYDIR"); |
103 | if (env) |
104 | return dupstr(env); |
105 | env = getenv("HOME"); |
106 | if (env) |
107 | return dupprintf("%s/.putty", env); |
108 | pwd = getpwuid(getuid()); |
109 | if (pwd && pwd->pw_dir) |
110 | return dupprintf("%s/.putty", pwd->pw_dir); |
111 | return dupstr("/.putty"); |
112 | } |
113 | if (index == INDEX_SESSIONDIR) { |
114 | env = getenv("PUTTYSESSIONS"); |
115 | if (env) |
116 | return dupstr(env); |
117 | tmp = make_filename(INDEX_DIR, NULL); |
118 | ret = dupprintf("%s/sessions", tmp); |
119 | sfree(tmp); |
120 | return ret; |
121 | } |
8dacc30e |
122 | if (index == INDEX_SESSION) { |
123 | char *munged = mungestr(subname); |
c18ed097 |
124 | tmp = make_filename(INDEX_SESSIONDIR, NULL); |
125 | ret = dupprintf("%s/%s", tmp, munged); |
126 | sfree(tmp); |
127 | sfree(munged); |
128 | return ret; |
129 | } |
130 | if (index == INDEX_HOSTKEYS) { |
131 | env = getenv("PUTTYSSHHOSTKEYS"); |
132 | if (env) |
133 | return dupstr(env); |
134 | tmp = make_filename(INDEX_DIR, NULL); |
135 | ret = dupprintf("%s/sshhostkeys", tmp); |
136 | sfree(tmp); |
137 | return ret; |
138 | } |
139 | if (index == INDEX_HOSTKEYS_TMP) { |
140 | tmp = make_filename(INDEX_HOSTKEYS, NULL); |
141 | ret = dupprintf("%s.tmp", tmp); |
142 | sfree(tmp); |
143 | return ret; |
144 | } |
145 | if (index == INDEX_RANDSEED) { |
146 | env = getenv("PUTTYRANDOMSEED"); |
147 | if (env) |
148 | return dupstr(env); |
149 | tmp = make_filename(INDEX_DIR, NULL); |
150 | ret = dupprintf("%s/randomseed", tmp); |
151 | sfree(tmp); |
152 | return ret; |
8dacc30e |
153 | } |
c18ed097 |
154 | tmp = make_filename(INDEX_DIR, NULL); |
155 | ret = dupprintf("%s/ERROR", tmp); |
156 | sfree(tmp); |
157 | return ret; |
8dacc30e |
158 | } |
159 | |
3f935d5b |
160 | void *open_settings_w(const char *sessionname, char **errmsg) |
1709795f |
161 | { |
c18ed097 |
162 | char *filename; |
8dacc30e |
163 | FILE *fp; |
164 | |
3f935d5b |
165 | *errmsg = NULL; |
166 | |
8dacc30e |
167 | /* |
3f935d5b |
168 | * Start by making sure the .putty directory and its sessions |
169 | * subdir actually exist. Ignore error returns from mkdir since |
170 | * they're perfectly likely to be `already exists', and any |
171 | * other error will trip us up later on so there's no real need |
172 | * to catch it now. |
8dacc30e |
173 | */ |
c18ed097 |
174 | filename = make_filename(INDEX_SESSIONDIR, NULL); |
0e27cb60 |
175 | if (mkdir(filename, 0700) != 0) { |
c18ed097 |
176 | char *filename2 = make_filename(INDEX_DIR, NULL); |
177 | mkdir(filename2, 0700); |
178 | sfree(filename2); |
179 | mkdir(filename, 0700); |
180 | } |
181 | sfree(filename); |
8dacc30e |
182 | |
c18ed097 |
183 | filename = make_filename(INDEX_SESSION, sessionname); |
8dacc30e |
184 | fp = fopen(filename, "w"); |
3f935d5b |
185 | if (!fp) { |
186 | *errmsg = dupprintf("Unable to create %s: %s", |
187 | filename, strerror(errno)); |
c18ed097 |
188 | sfree(filename); |
3f935d5b |
189 | return NULL; /* can't open */ |
190 | } |
c18ed097 |
191 | sfree(filename); |
8dacc30e |
192 | return fp; |
1709795f |
193 | } |
194 | |
c85623f9 |
195 | void write_setting_s(void *handle, const char *key, const char *value) |
1709795f |
196 | { |
8dacc30e |
197 | FILE *fp = (FILE *)handle; |
198 | fprintf(fp, "%s=%s\n", key, value); |
1709795f |
199 | } |
200 | |
c85623f9 |
201 | void write_setting_i(void *handle, const char *key, int value) |
1709795f |
202 | { |
8dacc30e |
203 | FILE *fp = (FILE *)handle; |
204 | fprintf(fp, "%s=%d\n", key, value); |
1709795f |
205 | } |
206 | |
207 | void close_settings_w(void *handle) |
208 | { |
8dacc30e |
209 | FILE *fp = (FILE *)handle; |
210 | fclose(fp); |
1709795f |
211 | } |
212 | |
faec60ed |
213 | /* |
214 | * Reading settings, for the moment, is done by retrieving X |
215 | * resources from the X display. When we introduce disk files, I |
216 | * think what will happen is that the X resources will override |
217 | * PuTTY's inbuilt defaults, but that the disk files will then |
218 | * override those. This isn't optimal, but it's the best I can |
219 | * immediately work out. |
e50f98bc |
220 | * FIXME: the above comment is a bit out of date. Did it happen? |
faec60ed |
221 | */ |
222 | |
b3d375b2 |
223 | struct skeyval { |
c85623f9 |
224 | const char *key; |
225 | const char *value; |
0ac15bdc |
226 | }; |
227 | |
228 | static tree234 *xrmtree = NULL; |
229 | |
8dacc30e |
230 | int keycmp(void *av, void *bv) |
0ac15bdc |
231 | { |
b3d375b2 |
232 | struct skeyval *a = (struct skeyval *)av; |
233 | struct skeyval *b = (struct skeyval *)bv; |
0ac15bdc |
234 | return strcmp(a->key, b->key); |
235 | } |
236 | |
237 | void provide_xrm_string(char *string) |
238 | { |
c85623f9 |
239 | char *p, *q, *key; |
b3d375b2 |
240 | struct skeyval *xrms, *ret; |
0ac15bdc |
241 | |
242 | p = q = strchr(string, ':'); |
243 | if (!q) { |
244 | fprintf(stderr, "pterm: expected a colon in resource string" |
245 | " \"%s\"\n", string); |
246 | return; |
247 | } |
248 | q++; |
249 | while (p > string && p[-1] != '.' && p[-1] != '*') |
250 | p--; |
b3d375b2 |
251 | xrms = snew(struct skeyval); |
3d88e64d |
252 | key = snewn(q-p, char); |
c85623f9 |
253 | memcpy(key, p, q-p); |
254 | key[q-p-1] = '\0'; |
255 | xrms->key = key; |
e93ed432 |
256 | while (*q && isspace((unsigned char)*q)) |
0ac15bdc |
257 | q++; |
258 | xrms->value = dupstr(q); |
259 | |
260 | if (!xrmtree) |
8dacc30e |
261 | xrmtree = newtree234(keycmp); |
0ac15bdc |
262 | |
263 | ret = add234(xrmtree, xrms); |
264 | if (ret) { |
265 | /* Override an existing string. */ |
266 | del234(xrmtree, ret); |
267 | add234(xrmtree, xrms); |
268 | } |
269 | } |
270 | |
c85623f9 |
271 | const char *get_setting(const char *key) |
0ac15bdc |
272 | { |
b3d375b2 |
273 | struct skeyval tmp, *ret; |
0ac15bdc |
274 | tmp.key = key; |
275 | if (xrmtree) { |
276 | ret = find234(xrmtree, &tmp, NULL); |
277 | if (ret) |
278 | return ret->value; |
279 | } |
c5e438ec |
280 | return x_get_default(key); |
0ac15bdc |
281 | } |
282 | |
c85623f9 |
283 | void *open_settings_r(const char *sessionname) |
1709795f |
284 | { |
c18ed097 |
285 | char *filename; |
8dacc30e |
286 | FILE *fp; |
287 | char *line; |
288 | tree234 *ret; |
289 | |
c18ed097 |
290 | filename = make_filename(INDEX_SESSION, sessionname); |
8dacc30e |
291 | fp = fopen(filename, "r"); |
c18ed097 |
292 | sfree(filename); |
8dacc30e |
293 | if (!fp) |
294 | return NULL; /* can't open */ |
295 | |
296 | ret = newtree234(keycmp); |
297 | |
298 | while ( (line = fgetline(fp)) ) { |
299 | char *value = strchr(line, '='); |
b3d375b2 |
300 | struct skeyval *kv; |
8dacc30e |
301 | |
302 | if (!value) |
303 | continue; |
304 | *value++ = '\0'; |
305 | value[strcspn(value, "\r\n")] = '\0'; /* trim trailing NL */ |
306 | |
b3d375b2 |
307 | kv = snew(struct skeyval); |
8dacc30e |
308 | kv->key = dupstr(line); |
309 | kv->value = dupstr(value); |
310 | add234(ret, kv); |
311 | |
312 | sfree(line); |
313 | } |
314 | |
315 | fclose(fp); |
316 | |
317 | return ret; |
1709795f |
318 | } |
319 | |
4a693cfc |
320 | char *read_setting_s(void *handle, const char *key) |
1709795f |
321 | { |
8dacc30e |
322 | tree234 *tree = (tree234 *)handle; |
323 | const char *val; |
b3d375b2 |
324 | struct skeyval tmp, *kv; |
8dacc30e |
325 | |
326 | tmp.key = key; |
327 | if (tree != NULL && |
328 | (kv = find234(tree, &tmp, NULL)) != NULL) { |
329 | val = kv->value; |
330 | assert(val != NULL); |
331 | } else |
332 | val = get_setting(key); |
333 | |
faec60ed |
334 | if (!val) |
335 | return NULL; |
4a693cfc |
336 | else |
337 | return dupstr(val); |
1709795f |
338 | } |
339 | |
c85623f9 |
340 | int read_setting_i(void *handle, const char *key, int defvalue) |
1709795f |
341 | { |
8dacc30e |
342 | tree234 *tree = (tree234 *)handle; |
343 | const char *val; |
b3d375b2 |
344 | struct skeyval tmp, *kv; |
8dacc30e |
345 | |
346 | tmp.key = key; |
347 | if (tree != NULL && |
348 | (kv = find234(tree, &tmp, NULL)) != NULL) { |
349 | val = kv->value; |
350 | assert(val != NULL); |
351 | } else |
352 | val = get_setting(key); |
353 | |
faec60ed |
354 | if (!val) |
355 | return defvalue; |
356 | else |
357 | return atoi(val); |
1709795f |
358 | } |
359 | |
ae62eaeb |
360 | FontSpec *read_setting_fontspec(void *handle, const char *name) |
9a30e26b |
361 | { |
f160b7b8 |
362 | /* |
363 | * In GTK1-only PuTTY, we used to store font names simply as a |
364 | * valid X font description string (logical or alias), under a |
365 | * bare key such as "Font". |
366 | * |
367 | * In GTK2 PuTTY, we have a prefix system where "client:" |
368 | * indicates a Pango font and "server:" an X one; existing |
369 | * configuration needs to be reinterpreted as having the |
370 | * "server:" prefix, so we change the storage key from the |
371 | * provided name string (e.g. "Font") to a suffixed one |
372 | * ("FontName"). |
373 | */ |
374 | char *suffname = dupcat(name, "Name", NULL); |
4a693cfc |
375 | char *tmp; |
376 | |
377 | if ((tmp = read_setting_s(handle, suffname)) != NULL) { |
ae62eaeb |
378 | FontSpec *fs = fontspec_new(tmp); |
f160b7b8 |
379 | sfree(suffname); |
4a693cfc |
380 | sfree(tmp); |
ae62eaeb |
381 | return fs; /* got new-style name */ |
f160b7b8 |
382 | } |
383 | sfree(suffname); |
384 | |
385 | /* Fall back to old-style name. */ |
4a693cfc |
386 | tmp = read_setting_s(handle, name); |
387 | if (tmp && *tmp) { |
ae62eaeb |
388 | char *tmp2 = dupcat("server:", tmp, NULL); |
389 | FontSpec *fs = fontspec_new(tmp2); |
390 | sfree(tmp2); |
4a693cfc |
391 | sfree(tmp); |
ae62eaeb |
392 | return fs; |
4a693cfc |
393 | } else { |
394 | sfree(tmp); |
ae62eaeb |
395 | return NULL; |
f160b7b8 |
396 | } |
9a30e26b |
397 | } |
962468d4 |
398 | Filename *read_setting_filename(void *handle, const char *name) |
9a30e26b |
399 | { |
4a693cfc |
400 | char *tmp = read_setting_s(handle, name); |
401 | if (tmp) { |
962468d4 |
402 | Filename *ret = filename_from_str(tmp); |
4a693cfc |
403 | sfree(tmp); |
962468d4 |
404 | return ret; |
4a693cfc |
405 | } else |
962468d4 |
406 | return NULL; |
9a30e26b |
407 | } |
408 | |
ae62eaeb |
409 | void write_setting_fontspec(void *handle, const char *name, FontSpec *fs) |
9a30e26b |
410 | { |
f160b7b8 |
411 | /* |
412 | * read_setting_fontspec had to handle two cases, but when |
413 | * writing our settings back out we simply always generate the |
414 | * new-style name. |
415 | */ |
416 | char *suffname = dupcat(name, "Name", NULL); |
ae62eaeb |
417 | write_setting_s(handle, suffname, fs->name); |
f160b7b8 |
418 | sfree(suffname); |
9a30e26b |
419 | } |
962468d4 |
420 | void write_setting_filename(void *handle, const char *name, Filename *result) |
9a30e26b |
421 | { |
962468d4 |
422 | write_setting_s(handle, name, result->path); |
9a30e26b |
423 | } |
424 | |
1709795f |
425 | void close_settings_r(void *handle) |
426 | { |
8dacc30e |
427 | tree234 *tree = (tree234 *)handle; |
b3d375b2 |
428 | struct skeyval *kv; |
8dacc30e |
429 | |
430 | if (!tree) |
431 | return; |
432 | |
433 | while ( (kv = index234(tree, 0)) != NULL) { |
434 | del234(tree, kv); |
435 | sfree((char *)kv->key); |
436 | sfree((char *)kv->value); |
437 | sfree(kv); |
438 | } |
439 | |
440 | freetree234(tree); |
1709795f |
441 | } |
442 | |
c85623f9 |
443 | void del_settings(const char *sessionname) |
1709795f |
444 | { |
c18ed097 |
445 | char *filename; |
446 | filename = make_filename(INDEX_SESSION, sessionname); |
8dacc30e |
447 | unlink(filename); |
c18ed097 |
448 | sfree(filename); |
1709795f |
449 | } |
450 | |
451 | void *enum_settings_start(void) |
452 | { |
8dacc30e |
453 | DIR *dp; |
c18ed097 |
454 | char *filename; |
1709795f |
455 | |
c18ed097 |
456 | filename = make_filename(INDEX_SESSIONDIR, NULL); |
8dacc30e |
457 | dp = opendir(filename); |
c18ed097 |
458 | sfree(filename); |
1709795f |
459 | |
8dacc30e |
460 | return dp; |
1709795f |
461 | } |
462 | |
8dacc30e |
463 | char *enum_settings_next(void *handle, char *buffer, int buflen) |
c5e438ec |
464 | { |
8dacc30e |
465 | DIR *dp = (DIR *)handle; |
466 | struct dirent *de; |
467 | struct stat st; |
c18ed097 |
468 | char *fullpath; |
469 | int maxlen, thislen, len; |
8dacc30e |
470 | char *unmunged; |
471 | |
c18ed097 |
472 | fullpath = make_filename(INDEX_SESSIONDIR, NULL); |
473 | maxlen = len = strlen(fullpath); |
8dacc30e |
474 | |
475 | while ( (de = readdir(dp)) != NULL ) { |
c18ed097 |
476 | thislen = len + 1 + strlen(de->d_name); |
477 | if (maxlen < thislen) { |
478 | maxlen = thislen; |
479 | fullpath = sresize(fullpath, maxlen+1, char); |
480 | } |
481 | fullpath[len] = '/'; |
482 | strncpy(fullpath+len+1, de->d_name, thislen - (len+1)); |
483 | fullpath[thislen] = '\0'; |
8dacc30e |
484 | |
485 | if (stat(fullpath, &st) < 0 || !S_ISREG(st.st_mode)) |
486 | continue; /* try another one */ |
487 | |
488 | unmunged = unmungestr(de->d_name); |
489 | strncpy(buffer, unmunged, buflen); |
490 | buffer[buflen-1] = '\0'; |
491 | sfree(unmunged); |
c18ed097 |
492 | sfree(fullpath); |
8dacc30e |
493 | return buffer; |
494 | } |
495 | |
c18ed097 |
496 | sfree(fullpath); |
8dacc30e |
497 | return NULL; |
c5e438ec |
498 | } |
499 | |
8dacc30e |
500 | void enum_settings_finish(void *handle) |
c5e438ec |
501 | { |
8dacc30e |
502 | DIR *dp = (DIR *)handle; |
503 | closedir(dp); |
c5e438ec |
504 | } |
505 | |
506 | /* |
507 | * Lines in the host keys file are of the form |
508 | * |
509 | * type@port:hostname keydata |
510 | * |
511 | * e.g. |
512 | * |
513 | * rsa@22:foovax.example.org 0x23,0x293487364395345345....2343 |
514 | */ |
c85623f9 |
515 | int verify_host_key(const char *hostname, int port, |
516 | const char *keytype, const char *key) |
1709795f |
517 | { |
c5e438ec |
518 | FILE *fp; |
c18ed097 |
519 | char *filename; |
c5e438ec |
520 | char *line; |
521 | int ret; |
522 | |
c18ed097 |
523 | filename = make_filename(INDEX_HOSTKEYS, NULL); |
c5e438ec |
524 | fp = fopen(filename, "r"); |
c18ed097 |
525 | sfree(filename); |
c5e438ec |
526 | if (!fp) |
527 | return 1; /* key does not exist */ |
528 | |
529 | ret = 1; |
530 | while ( (line = fgetline(fp)) ) { |
531 | int i; |
532 | char *p = line; |
533 | char porttext[20]; |
534 | |
535 | line[strcspn(line, "\n")] = '\0'; /* strip trailing newline */ |
536 | |
537 | i = strlen(keytype); |
538 | if (strncmp(p, keytype, i)) |
539 | goto done; |
540 | p += i; |
541 | |
542 | if (*p != '@') |
543 | goto done; |
544 | p++; |
545 | |
546 | sprintf(porttext, "%d", port); |
547 | i = strlen(porttext); |
548 | if (strncmp(p, porttext, i)) |
549 | goto done; |
550 | p += i; |
551 | |
552 | if (*p != ':') |
553 | goto done; |
554 | p++; |
555 | |
556 | i = strlen(hostname); |
557 | if (strncmp(p, hostname, i)) |
558 | goto done; |
559 | p += i; |
560 | |
561 | if (*p != ' ') |
562 | goto done; |
563 | p++; |
564 | |
565 | /* |
566 | * Found the key. Now just work out whether it's the right |
567 | * one or not. |
568 | */ |
569 | if (!strcmp(p, key)) |
570 | ret = 0; /* key matched OK */ |
571 | else |
572 | ret = 2; /* key mismatch */ |
573 | |
574 | done: |
575 | sfree(line); |
576 | if (ret != 1) |
577 | break; |
578 | } |
579 | |
1957695c |
580 | fclose(fp); |
c5e438ec |
581 | return ret; |
1709795f |
582 | } |
583 | |
c85623f9 |
584 | void store_host_key(const char *hostname, int port, |
585 | const char *keytype, const char *key) |
1709795f |
586 | { |
be57bcef |
587 | FILE *rfp, *wfp; |
588 | char *newtext, *line; |
589 | int headerlen; |
c18ed097 |
590 | char *filename, *tmpfilename; |
c5e438ec |
591 | |
be57bcef |
592 | newtext = dupprintf("%s@%d:%s %s\n", keytype, port, hostname, key); |
593 | headerlen = 1 + strcspn(newtext, " "); /* count the space too */ |
c5e438ec |
594 | |
be57bcef |
595 | /* |
596 | * Open both the old file and a new file. |
597 | */ |
c18ed097 |
598 | tmpfilename = make_filename(INDEX_HOSTKEYS_TMP, NULL); |
be57bcef |
599 | wfp = fopen(tmpfilename, "w"); |
600 | if (!wfp) { |
c18ed097 |
601 | char *dir; |
11cd814b |
602 | |
c18ed097 |
603 | dir = make_filename(INDEX_DIR, NULL); |
11cd814b |
604 | mkdir(dir, 0700); |
c18ed097 |
605 | sfree(dir); |
606 | |
11cd814b |
607 | wfp = fopen(tmpfilename, "w"); |
c5e438ec |
608 | } |
c18ed097 |
609 | if (!wfp) { |
610 | sfree(tmpfilename); |
11cd814b |
611 | return; |
c18ed097 |
612 | } |
613 | filename = make_filename(INDEX_HOSTKEYS, NULL); |
11cd814b |
614 | rfp = fopen(filename, "r"); |
be57bcef |
615 | |
616 | /* |
617 | * Copy all lines from the old file to the new one that _don't_ |
618 | * involve the same host key identifier as the one we're adding. |
619 | */ |
11cd814b |
620 | if (rfp) { |
621 | while ( (line = fgetline(rfp)) ) { |
622 | if (strncmp(line, newtext, headerlen)) |
623 | fputs(line, wfp); |
624 | } |
625 | fclose(rfp); |
c5e438ec |
626 | } |
be57bcef |
627 | |
628 | /* |
629 | * Now add the new line at the end. |
630 | */ |
631 | fputs(newtext, wfp); |
632 | |
be57bcef |
633 | fclose(wfp); |
634 | |
635 | rename(tmpfilename, filename); |
636 | |
c18ed097 |
637 | sfree(tmpfilename); |
638 | sfree(filename); |
be57bcef |
639 | sfree(newtext); |
1709795f |
640 | } |
641 | |
642 | void read_random_seed(noise_consumer_t consumer) |
643 | { |
d9c40fd6 |
644 | int fd; |
c18ed097 |
645 | char *fname; |
d9c40fd6 |
646 | |
c18ed097 |
647 | fname = make_filename(INDEX_RANDSEED, NULL); |
d9c40fd6 |
648 | fd = open(fname, O_RDONLY); |
c18ed097 |
649 | sfree(fname); |
0720879b |
650 | if (fd >= 0) { |
d9c40fd6 |
651 | char buf[512]; |
652 | int ret; |
653 | while ( (ret = read(fd, buf, sizeof(buf))) > 0) |
654 | consumer(buf, ret); |
655 | close(fd); |
656 | } |
1709795f |
657 | } |
658 | |
659 | void write_random_seed(void *data, int len) |
660 | { |
d9c40fd6 |
661 | int fd; |
c18ed097 |
662 | char *fname; |
d9c40fd6 |
663 | |
c18ed097 |
664 | fname = make_filename(INDEX_RANDSEED, NULL); |
e3ac3c05 |
665 | /* |
666 | * Don't truncate the random seed file if it already exists; if |
667 | * something goes wrong half way through writing it, it would |
668 | * be better to leave the old data there than to leave it empty. |
669 | */ |
670 | fd = open(fname, O_CREAT | O_WRONLY, 0600); |
d9c40fd6 |
671 | if (fd < 0) { |
c18ed097 |
672 | char *dir; |
d9c40fd6 |
673 | |
c18ed097 |
674 | dir = make_filename(INDEX_DIR, NULL); |
d9c40fd6 |
675 | mkdir(dir, 0700); |
c18ed097 |
676 | sfree(dir); |
677 | |
e3ac3c05 |
678 | fd = open(fname, O_CREAT | O_WRONLY, 0600); |
d9c40fd6 |
679 | } |
680 | |
681 | while (len > 0) { |
682 | int ret = write(fd, data, len); |
683 | if (ret <= 0) break; |
684 | len -= ret; |
685 | data = (char *)data + len; |
686 | } |
687 | |
688 | close(fd); |
c18ed097 |
689 | sfree(fname); |
1709795f |
690 | } |
691 | |
692 | void cleanup_all(void) |
693 | { |
694 | } |