]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - unix/uxstore.c
Tweak the icon script, and the generated icons, to more closely
[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 "putty.h"
19 #include "storage.h"
20 #include "tree234.h"
21
22 #ifdef PATH_MAX
23 #define FNLEN PATH_MAX
24 #else
25 #define FNLEN 1024 /* XXX */
26 #endif
27
28 enum {
29     INDEX_DIR, INDEX_HOSTKEYS, INDEX_HOSTKEYS_TMP, INDEX_RANDSEED,
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
39     if (!in || !*in)
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");
95     if (!home)
96         home="/";
97     strncpy(filename, home, FNLEN);
98     len = strlen(filename);
99     if (index == INDEX_SESSION) {
100         char *munged = mungestr(subname);
101         char *fn = dupprintf("/.putty/sessions/%s", munged);
102         strncpy(filename + len, fn, FNLEN - len);
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" :
110                 index == INDEX_HOSTKEYS_TMP ? "/.putty/sshhostkeys.tmp" :
111                 index == INDEX_RANDSEED ? "/.putty/randomseed" :
112                 "/.putty/ERROR", FNLEN - len);
113     }
114     filename[FNLEN-1] = '\0';
115 }
116
117 void *open_settings_w(const char *sessionname, char **errmsg)
118 {
119     char filename[FNLEN];
120     FILE *fp;
121
122     *errmsg = NULL;
123
124     /*
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.
130      */
131     make_filename(filename, INDEX_DIR, sessionname);
132     mkdir(filename, 0700);
133     make_filename(filename, INDEX_SESSIONDIR, sessionname);
134     mkdir(filename, 0700);
135
136     make_filename(filename, INDEX_SESSION, sessionname);
137     fp = fopen(filename, "w");
138     if (!fp) {
139         *errmsg = dupprintf("Unable to create %s: %s",
140                             filename, strerror(errno));
141         return NULL;                   /* can't open */
142     }
143     return fp;
144 }
145
146 void write_setting_s(void *handle, const char *key, const char *value)
147 {
148     FILE *fp = (FILE *)handle;
149     fprintf(fp, "%s=%s\n", key, value);
150 }
151
152 void write_setting_i(void *handle, const char *key, int value)
153 {
154     FILE *fp = (FILE *)handle;
155     fprintf(fp, "%s=%d\n", key, value);
156 }
157
158 void close_settings_w(void *handle)
159 {
160     FILE *fp = (FILE *)handle;
161     fclose(fp);
162 }
163
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.
171  * FIXME: the above comment is a bit out of date. Did it happen?
172  */
173
174 struct keyval {
175     const char *key;
176     const char *value;
177 };
178
179 static tree234 *xrmtree = NULL;
180
181 int keycmp(void *av, void *bv)
182 {
183     struct keyval *a = (struct keyval *)av;
184     struct keyval *b = (struct keyval *)bv;
185     return strcmp(a->key, b->key);
186 }
187
188 void provide_xrm_string(char *string)
189 {
190     char *p, *q, *key;
191     struct keyval *xrms, *ret;
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--;
202     xrms = snew(struct keyval);
203     key = snewn(q-p, char);
204     memcpy(key, p, q-p);
205     key[q-p-1] = '\0';
206     xrms->key = key;
207     while (*q && isspace((unsigned char)*q))
208         q++;
209     xrms->value = dupstr(q);
210
211     if (!xrmtree)
212         xrmtree = newtree234(keycmp);
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
222 const char *get_setting(const char *key)
223 {
224     struct keyval tmp, *ret;
225     tmp.key = key;
226     if (xrmtree) {
227         ret = find234(xrmtree, &tmp, NULL);
228         if (ret)
229             return ret->value;
230     }
231     return x_get_default(key);
232 }
233
234 void *open_settings_r(const char *sessionname)
235 {
236     char filename[FNLEN];
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;
268 }
269
270 char *read_setting_s(void *handle, const char *key, char *buffer, int buflen)
271 {
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
284     if (!val)
285         return NULL;
286     else {
287         strncpy(buffer, val, buflen);
288         buffer[buflen-1] = '\0';
289         return buffer;
290     }
291 }
292
293 int read_setting_i(void *handle, const char *key, int defvalue)
294 {
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
307     if (!val)
308         return defvalue;
309     else
310         return atoi(val);
311 }
312
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
331 void close_settings_r(void *handle)
332 {
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);
347 }
348
349 void del_settings(const char *sessionname)
350 {
351     char filename[FNLEN];
352     make_filename(filename, INDEX_SESSION, sessionname);
353     unlink(filename);
354 }
355
356 void *enum_settings_start(void)
357 {
358     DIR *dp;
359     char filename[FNLEN];
360
361     make_filename(filename, INDEX_SESSIONDIR, NULL);
362     dp = opendir(filename);
363
364     return dp;
365 }
366
367 char *enum_settings_next(void *handle, char *buffer, int buflen)
368 {
369     DIR *dp = (DIR *)handle;
370     struct dirent *de;
371     struct stat st;
372     char fullpath[FNLEN];
373     int len;
374     char *unmunged;
375
376     make_filename(fullpath, INDEX_SESSIONDIR, NULL);
377     len = strlen(fullpath);
378
379     while ( (de = readdir(dp)) != NULL ) {
380         if (len < FNLEN) {
381             fullpath[len] = '/';
382             strncpy(fullpath+len+1, de->d_name, FNLEN-(len+1));
383             fullpath[FNLEN-1] = '\0';
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;
397 }
398
399 void enum_settings_finish(void *handle)
400 {
401     DIR *dp = (DIR *)handle;
402     closedir(dp);
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  */
414 int verify_host_key(const char *hostname, int port,
415                     const char *keytype, const char *key)
416 {
417     FILE *fp;
418     char filename[FNLEN];
419     char *line;
420     int ret;
421
422     make_filename(filename, INDEX_HOSTKEYS, NULL);
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
478     fclose(fp);
479     return ret;
480 }
481
482 void store_host_key(const char *hostname, int port,
483                     const char *keytype, const char *key)
484 {
485     FILE *rfp, *wfp;
486     char *newtext, *line;
487     int headerlen;
488     char filename[FNLEN], tmpfilename[FNLEN];
489
490     newtext = dupprintf("%s@%d:%s %s\n", keytype, port, hostname, key);
491     headerlen = 1 + strcspn(newtext, " ");   /* count the space too */
492
493     /*
494      * Open both the old file and a new file.
495      */
496     make_filename(tmpfilename, INDEX_HOSTKEYS_TMP, NULL);
497     wfp = fopen(tmpfilename, "w");
498     if (!wfp) {
499         char dir[FNLEN];
500
501         make_filename(dir, INDEX_DIR, NULL);
502         mkdir(dir, 0700);
503         wfp = fopen(tmpfilename, "w");
504     }
505     if (!wfp)
506         return;
507     make_filename(filename, INDEX_HOSTKEYS, NULL);
508     rfp = fopen(filename, "r");
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      */
514     if (rfp) {
515         while ( (line = fgetline(rfp)) ) {
516             if (strncmp(line, newtext, headerlen))
517                 fputs(line, wfp);
518         }
519         fclose(rfp);
520     }
521
522     /*
523      * Now add the new line at the end.
524      */
525     fputs(newtext, wfp);
526
527     fclose(wfp);
528
529     rename(tmpfilename, filename);
530
531     sfree(newtext);
532 }
533
534 void read_random_seed(noise_consumer_t consumer)
535 {
536     int fd;
537     char fname[FNLEN];
538
539     make_filename(fname, INDEX_RANDSEED, NULL);
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     }
548 }
549
550 void write_random_seed(void *data, int len)
551 {
552     int fd;
553     char fname[FNLEN];
554
555     make_filename(fname, INDEX_RANDSEED, NULL);
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);
562     if (fd < 0) {
563         char dir[FNLEN];
564
565         make_filename(dir, INDEX_DIR, NULL);
566         mkdir(dir, 0700);
567         fd = open(fname, O_CREAT | O_WRONLY, 0600);
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);
578 }
579
580 void cleanup_all(void)
581 {
582 }