]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - unix/uxstore.c
Post-release destabilisation! Completely remove the struct type
[PuTTY.git] / unix / uxstore.c
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>
8 #include <string.h>
9 #include <assert.h>
10 #include <errno.h>
11 #include <ctype.h>
12 #include <limits.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <dirent.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <pwd.h>
19 #include "putty.h"
20 #include "storage.h"
21 #include "tree234.h"
22
23 #ifdef PATH_MAX
24 #define FNLEN PATH_MAX
25 #else
26 #define FNLEN 1024 /* XXX */
27 #endif
28
29 enum {
30     INDEX_DIR, INDEX_HOSTKEYS, INDEX_HOSTKEYS_TMP, INDEX_RANDSEED,
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
40     if (!in || !*in)
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
91 static char *make_filename(int index, const char *subname)
92 {
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     }
122     if (index == INDEX_SESSION) {
123         char *munged = mungestr(subname);
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;
153     }
154     tmp = make_filename(INDEX_DIR, NULL);
155     ret = dupprintf("%s/ERROR", tmp);
156     sfree(tmp);
157     return ret;
158 }
159
160 void *open_settings_w(const char *sessionname, char **errmsg)
161 {
162     char *filename;
163     FILE *fp;
164
165     *errmsg = NULL;
166
167     /*
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.
173      */
174     filename = make_filename(INDEX_SESSIONDIR, NULL);
175     if (mkdir(filename, 0700) != 0) {
176         char *filename2 = make_filename(INDEX_DIR, NULL);
177         mkdir(filename2, 0700);
178         sfree(filename2);
179         mkdir(filename, 0700);
180     }
181     sfree(filename);
182
183     filename = make_filename(INDEX_SESSION, sessionname);
184     fp = fopen(filename, "w");
185     if (!fp) {
186         *errmsg = dupprintf("Unable to create %s: %s",
187                             filename, strerror(errno));
188         sfree(filename);
189         return NULL;                   /* can't open */
190     }
191     sfree(filename);
192     return fp;
193 }
194
195 void write_setting_s(void *handle, const char *key, const char *value)
196 {
197     FILE *fp = (FILE *)handle;
198     fprintf(fp, "%s=%s\n", key, value);
199 }
200
201 void write_setting_i(void *handle, const char *key, int value)
202 {
203     FILE *fp = (FILE *)handle;
204     fprintf(fp, "%s=%d\n", key, value);
205 }
206
207 void close_settings_w(void *handle)
208 {
209     FILE *fp = (FILE *)handle;
210     fclose(fp);
211 }
212
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.
220  * FIXME: the above comment is a bit out of date. Did it happen?
221  */
222
223 struct skeyval {
224     const char *key;
225     const char *value;
226 };
227
228 static tree234 *xrmtree = NULL;
229
230 int keycmp(void *av, void *bv)
231 {
232     struct skeyval *a = (struct skeyval *)av;
233     struct skeyval *b = (struct skeyval *)bv;
234     return strcmp(a->key, b->key);
235 }
236
237 void provide_xrm_string(char *string)
238 {
239     char *p, *q, *key;
240     struct skeyval *xrms, *ret;
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--;
251     xrms = snew(struct skeyval);
252     key = snewn(q-p, char);
253     memcpy(key, p, q-p);
254     key[q-p-1] = '\0';
255     xrms->key = key;
256     while (*q && isspace((unsigned char)*q))
257         q++;
258     xrms->value = dupstr(q);
259
260     if (!xrmtree)
261         xrmtree = newtree234(keycmp);
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
271 const char *get_setting(const char *key)
272 {
273     struct skeyval tmp, *ret;
274     tmp.key = key;
275     if (xrmtree) {
276         ret = find234(xrmtree, &tmp, NULL);
277         if (ret)
278             return ret->value;
279     }
280     return x_get_default(key);
281 }
282
283 void *open_settings_r(const char *sessionname)
284 {
285     char *filename;
286     FILE *fp;
287     char *line;
288     tree234 *ret;
289
290     filename = make_filename(INDEX_SESSION, sessionname);
291     fp = fopen(filename, "r");
292     sfree(filename);
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, '=');
300         struct skeyval *kv;
301
302         if (!value)
303             continue;
304         *value++ = '\0';
305         value[strcspn(value, "\r\n")] = '\0';   /* trim trailing NL */
306
307         kv = snew(struct skeyval);
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;
318 }
319
320 char *read_setting_s(void *handle, const char *key)
321 {
322     tree234 *tree = (tree234 *)handle;
323     const char *val;
324     struct skeyval tmp, *kv;
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
334     if (!val)
335         return NULL;
336     else
337         return dupstr(val);
338 }
339
340 int read_setting_i(void *handle, const char *key, int defvalue)
341 {
342     tree234 *tree = (tree234 *)handle;
343     const char *val;
344     struct skeyval tmp, *kv;
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
354     if (!val)
355         return defvalue;
356     else
357         return atoi(val);
358 }
359
360 int read_setting_fontspec(void *handle, const char *name, FontSpec *result)
361 {
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);
375     char *tmp;
376
377     if ((tmp = read_setting_s(handle, suffname)) != NULL) {
378         strncpy(result->name, tmp, sizeof(result->name)-1);
379         result->name[sizeof(result->name)-1] = '\0';
380         sfree(suffname);
381         sfree(tmp);
382         return TRUE;                   /* got new-style name */
383     }
384     sfree(suffname);
385
386     /* Fall back to old-style name. */
387     tmp = read_setting_s(handle, name);
388     if (tmp && *tmp) {
389         strcpy(result->name, "server:");
390         strncpy(result->name + 7, tmp, sizeof(result->name) - 8);
391         result->name[sizeof(result->name)-1] = '\0';
392         sfree(tmp);
393         return TRUE;
394     } else {
395         sfree(tmp);
396         return FALSE;
397     }
398 }
399 int read_setting_filename(void *handle, const char *name, Filename *result)
400 {
401     char *tmp = read_setting_s(handle, name);
402     if (tmp) {
403         strncpy(result->path, tmp, sizeof(result->path)-1);
404         result->path[sizeof(result->path)-1] = '\0';
405         sfree(tmp);
406         return TRUE;
407     } else
408         return FALSE;
409 }
410
411 void write_setting_fontspec(void *handle, const char *name, FontSpec result)
412 {
413     /*
414      * read_setting_fontspec had to handle two cases, but when
415      * writing our settings back out we simply always generate the
416      * new-style name.
417      */
418     char *suffname = dupcat(name, "Name", NULL);
419     write_setting_s(handle, suffname, result.name);
420     sfree(suffname);
421 }
422 void write_setting_filename(void *handle, const char *name, Filename result)
423 {
424     write_setting_s(handle, name, result.path);
425 }
426
427 void close_settings_r(void *handle)
428 {
429     tree234 *tree = (tree234 *)handle;
430     struct skeyval *kv;
431
432     if (!tree)
433         return;
434
435     while ( (kv = index234(tree, 0)) != NULL) {
436         del234(tree, kv);
437         sfree((char *)kv->key);
438         sfree((char *)kv->value);
439         sfree(kv);
440     }
441
442     freetree234(tree);
443 }
444
445 void del_settings(const char *sessionname)
446 {
447     char *filename;
448     filename = make_filename(INDEX_SESSION, sessionname);
449     unlink(filename);
450     sfree(filename);
451 }
452
453 void *enum_settings_start(void)
454 {
455     DIR *dp;
456     char *filename;
457
458     filename = make_filename(INDEX_SESSIONDIR, NULL);
459     dp = opendir(filename);
460     sfree(filename);
461
462     return dp;
463 }
464
465 char *enum_settings_next(void *handle, char *buffer, int buflen)
466 {
467     DIR *dp = (DIR *)handle;
468     struct dirent *de;
469     struct stat st;
470     char *fullpath;
471     int maxlen, thislen, len;
472     char *unmunged;
473
474     fullpath = make_filename(INDEX_SESSIONDIR, NULL);
475     maxlen = len = strlen(fullpath);
476
477     while ( (de = readdir(dp)) != NULL ) {
478         thislen = len + 1 + strlen(de->d_name);
479         if (maxlen < thislen) {
480             maxlen = thislen;
481             fullpath = sresize(fullpath, maxlen+1, char);
482         }
483         fullpath[len] = '/';
484         strncpy(fullpath+len+1, de->d_name, thislen - (len+1));
485         fullpath[thislen] = '\0';
486
487         if (stat(fullpath, &st) < 0 || !S_ISREG(st.st_mode))
488             continue;                  /* try another one */
489
490         unmunged = unmungestr(de->d_name);
491         strncpy(buffer, unmunged, buflen);
492         buffer[buflen-1] = '\0';
493         sfree(unmunged);
494         sfree(fullpath);
495         return buffer;
496     }
497
498     sfree(fullpath);
499     return NULL;
500 }
501
502 void enum_settings_finish(void *handle)
503 {
504     DIR *dp = (DIR *)handle;
505     closedir(dp);
506 }
507
508 /*
509  * Lines in the host keys file are of the form
510  * 
511  *   type@port:hostname keydata
512  * 
513  * e.g.
514  * 
515  *   rsa@22:foovax.example.org 0x23,0x293487364395345345....2343
516  */
517 int verify_host_key(const char *hostname, int port,
518                     const char *keytype, const char *key)
519 {
520     FILE *fp;
521     char *filename;
522     char *line;
523     int ret;
524
525     filename = make_filename(INDEX_HOSTKEYS, NULL);
526     fp = fopen(filename, "r");
527     sfree(filename);
528     if (!fp)
529         return 1;                      /* key does not exist */
530
531     ret = 1;
532     while ( (line = fgetline(fp)) ) {
533         int i;
534         char *p = line;
535         char porttext[20];
536
537         line[strcspn(line, "\n")] = '\0';   /* strip trailing newline */
538
539         i = strlen(keytype);
540         if (strncmp(p, keytype, i))
541             goto done;
542         p += i;
543
544         if (*p != '@')
545             goto done;
546         p++;
547
548         sprintf(porttext, "%d", port);
549         i = strlen(porttext);
550         if (strncmp(p, porttext, i))
551             goto done;
552         p += i;
553
554         if (*p != ':')
555             goto done;
556         p++;
557
558         i = strlen(hostname);
559         if (strncmp(p, hostname, i))
560             goto done;
561         p += i;
562
563         if (*p != ' ')
564             goto done;
565         p++;
566
567         /*
568          * Found the key. Now just work out whether it's the right
569          * one or not.
570          */
571         if (!strcmp(p, key))
572             ret = 0;                   /* key matched OK */
573         else
574             ret = 2;                   /* key mismatch */
575
576         done:
577         sfree(line);
578         if (ret != 1)
579             break;
580     }
581
582     fclose(fp);
583     return ret;
584 }
585
586 void store_host_key(const char *hostname, int port,
587                     const char *keytype, const char *key)
588 {
589     FILE *rfp, *wfp;
590     char *newtext, *line;
591     int headerlen;
592     char *filename, *tmpfilename;
593
594     newtext = dupprintf("%s@%d:%s %s\n", keytype, port, hostname, key);
595     headerlen = 1 + strcspn(newtext, " ");   /* count the space too */
596
597     /*
598      * Open both the old file and a new file.
599      */
600     tmpfilename = make_filename(INDEX_HOSTKEYS_TMP, NULL);
601     wfp = fopen(tmpfilename, "w");
602     if (!wfp) {
603         char *dir;
604
605         dir = make_filename(INDEX_DIR, NULL);
606         mkdir(dir, 0700);
607         sfree(dir);
608
609         wfp = fopen(tmpfilename, "w");
610     }
611     if (!wfp) {
612         sfree(tmpfilename);
613         return;
614     }
615     filename = make_filename(INDEX_HOSTKEYS, NULL);
616     rfp = fopen(filename, "r");
617
618     /*
619      * Copy all lines from the old file to the new one that _don't_
620      * involve the same host key identifier as the one we're adding.
621      */
622     if (rfp) {
623         while ( (line = fgetline(rfp)) ) {
624             if (strncmp(line, newtext, headerlen))
625                 fputs(line, wfp);
626         }
627         fclose(rfp);
628     }
629
630     /*
631      * Now add the new line at the end.
632      */
633     fputs(newtext, wfp);
634
635     fclose(wfp);
636
637     rename(tmpfilename, filename);
638
639     sfree(tmpfilename);
640     sfree(filename);
641     sfree(newtext);
642 }
643
644 void read_random_seed(noise_consumer_t consumer)
645 {
646     int fd;
647     char *fname;
648
649     fname = make_filename(INDEX_RANDSEED, NULL);
650     fd = open(fname, O_RDONLY);
651     sfree(fname);
652     if (fd >= 0) {
653         char buf[512];
654         int ret;
655         while ( (ret = read(fd, buf, sizeof(buf))) > 0)
656             consumer(buf, ret);
657         close(fd);
658     }
659 }
660
661 void write_random_seed(void *data, int len)
662 {
663     int fd;
664     char *fname;
665
666     fname = make_filename(INDEX_RANDSEED, NULL);
667     /*
668      * Don't truncate the random seed file if it already exists; if
669      * something goes wrong half way through writing it, it would
670      * be better to leave the old data there than to leave it empty.
671      */
672     fd = open(fname, O_CREAT | O_WRONLY, 0600);
673     if (fd < 0) {
674         char *dir;
675
676         dir = make_filename(INDEX_DIR, NULL);
677         mkdir(dir, 0700);
678         sfree(dir);
679
680         fd = open(fname, O_CREAT | O_WRONLY, 0600);
681     }
682
683     while (len > 0) {
684         int ret = write(fd, data, len);
685         if (ret <= 0) break;
686         len -= ret;
687         data = (char *)data + len;
688     }
689
690     close(fd);
691     sfree(fname);
692 }
693
694 void cleanup_all(void)
695 {
696 }