2 * uxstore.c: Unix-specific implementation of the interface defined
\r
16 #include <sys/stat.h>
\r
17 #include <sys/types.h>
\r
20 #include "storage.h"
\r
21 #include "tree234.h"
\r
24 #define FNLEN PATH_MAX
\r
26 #define FNLEN 1024 /* XXX */
\r
30 INDEX_DIR, INDEX_HOSTKEYS, INDEX_HOSTKEYS_TMP, INDEX_RANDSEED,
\r
31 INDEX_SESSIONDIR, INDEX_SESSION,
\r
34 static const char hex[16] = "0123456789ABCDEF";
\r
36 static char *mungestr(const char *in)
\r
41 in = "Default Settings";
\r
43 ret = out = snewn(3*strlen(in)+1, char);
\r
47 * There are remarkably few punctuation characters that
\r
48 * aren't shell-special in some way or likely to be used as
\r
49 * separators in some file format or another! Hence we use
\r
50 * opt-in for safe characters rather than opt-out for
\r
51 * specific unsafe ones...
\r
53 if (*in!='+' && *in!='-' && *in!='.' && *in!='@' && *in!='_' &&
\r
54 !(*in >= '0' && *in <= '9') &&
\r
55 !(*in >= 'A' && *in <= 'Z') &&
\r
56 !(*in >= 'a' && *in <= 'z')) {
\r
58 *out++ = hex[((unsigned char) *in) >> 4];
\r
59 *out++ = hex[((unsigned char) *in) & 15];
\r
68 static char *unmungestr(const char *in)
\r
71 out = ret = snewn(strlen(in)+1, char);
\r
73 if (*in == '%' && in[1] && in[2]) {
\r
77 i -= (i > 9 ? 7 : 0);
\r
79 j -= (j > 9 ? 7 : 0);
\r
81 *out++ = (i << 4) + j;
\r
91 static char *make_filename(int index, const char *subname)
\r
93 char *env, *tmp, *ret;
\r
96 * Allow override of the PuTTY configuration location, and of
\r
97 * specific subparts of it, by means of environment variables.
\r
99 if (index == INDEX_DIR) {
\r
100 struct passwd *pwd;
\r
102 env = getenv("PUTTYDIR");
\r
104 return dupstr(env);
\r
105 env = getenv("HOME");
\r
107 return dupprintf("%s/.putty", env);
\r
108 pwd = getpwuid(getuid());
\r
109 if (pwd && pwd->pw_dir)
\r
110 return dupprintf("%s/.putty", pwd->pw_dir);
\r
111 return dupstr("/.putty");
\r
113 if (index == INDEX_SESSIONDIR) {
\r
114 env = getenv("PUTTYSESSIONS");
\r
116 return dupstr(env);
\r
117 tmp = make_filename(INDEX_DIR, NULL);
\r
118 ret = dupprintf("%s/sessions", tmp);
\r
122 if (index == INDEX_SESSION) {
\r
123 char *munged = mungestr(subname);
\r
124 tmp = make_filename(INDEX_SESSIONDIR, NULL);
\r
125 ret = dupprintf("%s/%s", tmp, munged);
\r
130 if (index == INDEX_HOSTKEYS) {
\r
131 env = getenv("PUTTYSSHHOSTKEYS");
\r
133 return dupstr(env);
\r
134 tmp = make_filename(INDEX_DIR, NULL);
\r
135 ret = dupprintf("%s/sshhostkeys", tmp);
\r
139 if (index == INDEX_HOSTKEYS_TMP) {
\r
140 tmp = make_filename(INDEX_HOSTKEYS, NULL);
\r
141 ret = dupprintf("%s.tmp", tmp);
\r
145 if (index == INDEX_RANDSEED) {
\r
146 env = getenv("PUTTYRANDOMSEED");
\r
148 return dupstr(env);
\r
149 tmp = make_filename(INDEX_DIR, NULL);
\r
150 ret = dupprintf("%s/randomseed", tmp);
\r
154 tmp = make_filename(INDEX_DIR, NULL);
\r
155 ret = dupprintf("%s/ERROR", tmp);
\r
160 void *open_settings_w(const char *sessionname, char **errmsg)
\r
168 * Start by making sure the .putty directory and its sessions
\r
169 * subdir actually exist. Ignore error returns from mkdir since
\r
170 * they're perfectly likely to be `already exists', and any
\r
171 * other error will trip us up later on so there's no real need
\r
174 filename = make_filename(INDEX_SESSIONDIR, NULL);
\r
175 if (mkdir(filename, 0700) != 0) {
\r
176 char *filename2 = make_filename(INDEX_DIR, NULL);
\r
177 mkdir(filename2, 0700);
\r
179 mkdir(filename, 0700);
\r
183 filename = make_filename(INDEX_SESSION, sessionname);
\r
184 fp = fopen(filename, "w");
\r
186 *errmsg = dupprintf("Unable to create %s: %s",
\r
187 filename, strerror(errno));
\r
189 return NULL; /* can't open */
\r
195 void write_setting_s(void *handle, const char *key, const char *value)
\r
197 FILE *fp = (FILE *)handle;
\r
198 fprintf(fp, "%s=%s\n", key, value);
\r
201 void write_setting_i(void *handle, const char *key, int value)
\r
203 FILE *fp = (FILE *)handle;
\r
204 fprintf(fp, "%s=%d\n", key, value);
\r
207 void close_settings_w(void *handle)
\r
209 FILE *fp = (FILE *)handle;
\r
214 * Reading settings, for the moment, is done by retrieving X
\r
215 * resources from the X display. When we introduce disk files, I
\r
216 * think what will happen is that the X resources will override
\r
217 * PuTTY's inbuilt defaults, but that the disk files will then
\r
218 * override those. This isn't optimal, but it's the best I can
\r
219 * immediately work out.
\r
220 * FIXME: the above comment is a bit out of date. Did it happen?
\r
228 static tree234 *xrmtree = NULL;
\r
230 int keycmp(void *av, void *bv)
\r
232 struct skeyval *a = (struct skeyval *)av;
\r
233 struct skeyval *b = (struct skeyval *)bv;
\r
234 return strcmp(a->key, b->key);
\r
237 void provide_xrm_string(char *string)
\r
240 struct skeyval *xrms, *ret;
\r
242 p = q = strchr(string, ':');
\r
244 fprintf(stderr, "pterm: expected a colon in resource string"
\r
245 " \"%s\"\n", string);
\r
249 while (p > string && p[-1] != '.' && p[-1] != '*')
\r
251 xrms = snew(struct skeyval);
\r
252 key = snewn(q-p, char);
\r
253 memcpy(key, p, q-p);
\r
256 while (*q && isspace((unsigned char)*q))
\r
258 xrms->value = dupstr(q);
\r
261 xrmtree = newtree234(keycmp);
\r
263 ret = add234(xrmtree, xrms);
\r
265 /* Override an existing string. */
\r
266 del234(xrmtree, ret);
\r
267 add234(xrmtree, xrms);
\r
271 const char *get_setting(const char *key)
\r
273 struct skeyval tmp, *ret;
\r
276 ret = find234(xrmtree, &tmp, NULL);
\r
280 return x_get_default(key);
\r
283 void *open_settings_r(const char *sessionname)
\r
290 filename = make_filename(INDEX_SESSION, sessionname);
\r
291 fp = fopen(filename, "r");
\r
294 return NULL; /* can't open */
\r
296 ret = newtree234(keycmp);
\r
298 while ( (line = fgetline(fp)) ) {
\r
299 char *value = strchr(line, '=');
\r
300 struct skeyval *kv;
\r
305 value[strcspn(value, "\r\n")] = '\0'; /* trim trailing NL */
\r
307 kv = snew(struct skeyval);
\r
308 kv->key = dupstr(line);
\r
309 kv->value = dupstr(value);
\r
320 char *read_setting_s(void *handle, const char *key, char *buffer, int buflen)
\r
322 tree234 *tree = (tree234 *)handle;
\r
324 struct skeyval tmp, *kv;
\r
327 if (tree != NULL &&
\r
328 (kv = find234(tree, &tmp, NULL)) != NULL) {
\r
330 assert(val != NULL);
\r
332 val = get_setting(key);
\r
337 strncpy(buffer, val, buflen);
\r
338 buffer[buflen-1] = '\0';
\r
343 int read_setting_i(void *handle, const char *key, int defvalue)
\r
345 tree234 *tree = (tree234 *)handle;
\r
347 struct skeyval tmp, *kv;
\r
350 if (tree != NULL &&
\r
351 (kv = find234(tree, &tmp, NULL)) != NULL) {
\r
353 assert(val != NULL);
\r
355 val = get_setting(key);
\r
363 int read_setting_fontspec(void *handle, const char *name, FontSpec *result)
\r
366 * In GTK1-only PuTTY, we used to store font names simply as a
\r
367 * valid X font description string (logical or alias), under a
\r
368 * bare key such as "Font".
\r
370 * In GTK2 PuTTY, we have a prefix system where "client:"
\r
371 * indicates a Pango font and "server:" an X one; existing
\r
372 * configuration needs to be reinterpreted as having the
\r
373 * "server:" prefix, so we change the storage key from the
\r
374 * provided name string (e.g. "Font") to a suffixed one
\r
377 char *suffname = dupcat(name, "Name", NULL);
\r
378 if (read_setting_s(handle, suffname, result->name, sizeof(result->name))) {
\r
380 return TRUE; /* got new-style name */
\r
384 /* Fall back to old-style name. */
\r
385 memcpy(result->name, "server:", 7);
\r
386 if (!read_setting_s(handle, name,
\r
387 result->name + 7, sizeof(result->name) - 7) ||
\r
388 !result->name[7]) {
\r
389 result->name[0] = '\0';
\r
395 int read_setting_filename(void *handle, const char *name, Filename *result)
\r
397 return !!read_setting_s(handle, name, result->path, sizeof(result->path));
\r
400 void write_setting_fontspec(void *handle, const char *name, FontSpec result)
\r
403 * read_setting_fontspec had to handle two cases, but when
\r
404 * writing our settings back out we simply always generate the
\r
407 char *suffname = dupcat(name, "Name", NULL);
\r
408 write_setting_s(handle, suffname, result.name);
\r
411 void write_setting_filename(void *handle, const char *name, Filename result)
\r
413 write_setting_s(handle, name, result.path);
\r
416 void close_settings_r(void *handle)
\r
418 tree234 *tree = (tree234 *)handle;
\r
419 struct skeyval *kv;
\r
424 while ( (kv = index234(tree, 0)) != NULL) {
\r
426 sfree((char *)kv->key);
\r
427 sfree((char *)kv->value);
\r
434 void del_settings(const char *sessionname)
\r
437 filename = make_filename(INDEX_SESSION, sessionname);
\r
442 void *enum_settings_start(void)
\r
447 filename = make_filename(INDEX_SESSIONDIR, NULL);
\r
448 dp = opendir(filename);
\r
454 char *enum_settings_next(void *handle, char *buffer, int buflen)
\r
456 DIR *dp = (DIR *)handle;
\r
460 int maxlen, thislen, len;
\r
463 fullpath = make_filename(INDEX_SESSIONDIR, NULL);
\r
464 maxlen = len = strlen(fullpath);
\r
466 while ( (de = readdir(dp)) != NULL ) {
\r
467 thislen = len + 1 + strlen(de->d_name);
\r
468 if (maxlen < thislen) {
\r
470 fullpath = sresize(fullpath, maxlen+1, char);
\r
472 fullpath[len] = '/';
\r
473 strncpy(fullpath+len+1, de->d_name, thislen - (len+1));
\r
474 fullpath[thislen] = '\0';
\r
476 if (stat(fullpath, &st) < 0 || !S_ISREG(st.st_mode))
\r
477 continue; /* try another one */
\r
479 unmunged = unmungestr(de->d_name);
\r
480 strncpy(buffer, unmunged, buflen);
\r
481 buffer[buflen-1] = '\0';
\r
491 void enum_settings_finish(void *handle)
\r
493 DIR *dp = (DIR *)handle;
\r
498 * Lines in the host keys file are of the form
\r
500 * type@port:hostname keydata
\r
504 * rsa@22:foovax.example.org 0x23,0x293487364395345345....2343
\r
506 int verify_host_key(const char *hostname, int port,
\r
507 const char *keytype, const char *key)
\r
514 filename = make_filename(INDEX_HOSTKEYS, NULL);
\r
515 fp = fopen(filename, "r");
\r
518 return 1; /* key does not exist */
\r
521 while ( (line = fgetline(fp)) ) {
\r
526 line[strcspn(line, "\n")] = '\0'; /* strip trailing newline */
\r
528 i = strlen(keytype);
\r
529 if (strncmp(p, keytype, i))
\r
537 sprintf(porttext, "%d", port);
\r
538 i = strlen(porttext);
\r
539 if (strncmp(p, porttext, i))
\r
547 i = strlen(hostname);
\r
548 if (strncmp(p, hostname, i))
\r
557 * Found the key. Now just work out whether it's the right
\r
560 if (!strcmp(p, key))
\r
561 ret = 0; /* key matched OK */
\r
563 ret = 2; /* key mismatch */
\r
575 void store_host_key(const char *hostname, int port,
\r
576 const char *keytype, const char *key)
\r
579 char *newtext, *line;
\r
581 char *filename, *tmpfilename;
\r
583 newtext = dupprintf("%s@%d:%s %s\n", keytype, port, hostname, key);
\r
584 headerlen = 1 + strcspn(newtext, " "); /* count the space too */
\r
587 * Open both the old file and a new file.
\r
589 tmpfilename = make_filename(INDEX_HOSTKEYS_TMP, NULL);
\r
590 wfp = fopen(tmpfilename, "w");
\r
594 dir = make_filename(INDEX_DIR, NULL);
\r
598 wfp = fopen(tmpfilename, "w");
\r
601 sfree(tmpfilename);
\r
604 filename = make_filename(INDEX_HOSTKEYS, NULL);
\r
605 rfp = fopen(filename, "r");
\r
608 * Copy all lines from the old file to the new one that _don't_
\r
609 * involve the same host key identifier as the one we're adding.
\r
612 while ( (line = fgetline(rfp)) ) {
\r
613 if (strncmp(line, newtext, headerlen))
\r
620 * Now add the new line at the end.
\r
622 fputs(newtext, wfp);
\r
626 rename(tmpfilename, filename);
\r
628 sfree(tmpfilename);
\r
633 void read_random_seed(noise_consumer_t consumer)
\r
638 fname = make_filename(INDEX_RANDSEED, NULL);
\r
639 fd = open(fname, O_RDONLY);
\r
644 while ( (ret = read(fd, buf, sizeof(buf))) > 0)
\r
645 consumer(buf, ret);
\r
650 void write_random_seed(void *data, int len)
\r
655 fname = make_filename(INDEX_RANDSEED, NULL);
\r
657 * Don't truncate the random seed file if it already exists; if
\r
658 * something goes wrong half way through writing it, it would
\r
659 * be better to leave the old data there than to leave it empty.
\r
661 fd = open(fname, O_CREAT | O_WRONLY, 0600);
\r
665 dir = make_filename(INDEX_DIR, NULL);
\r
669 fd = open(fname, O_CREAT | O_WRONLY, 0600);
\r
673 int ret = write(fd, data, len);
\r
674 if (ret <= 0) break;
\r
676 data = (char *)data + len;
\r
683 void cleanup_all(void)
\r