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