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