Add proper error reports in write_random_seed, via the new 'nonfatal'
[sgt/putty] / unix / uxstore.c
index 5da6ed6..785e1e3 100644 (file)
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
 #include <ctype.h>
+#include <limits.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <dirent.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <pwd.h>
 #include "putty.h"
 #include "storage.h"
 #include "tree234.h"
 
-/*
- * For the moment, the only existing Unix utility is pterm and that
- * has no GUI configuration at all, so our write routines need do
- * nothing. Eventually I suppose these will read and write an rc
- * file somewhere or other.
- */
+#ifdef PATH_MAX
+#define FNLEN PATH_MAX
+#else
+#define FNLEN 1024 /* XXX */
+#endif
 
-void *open_settings_w(const char *sessionname)
+enum {
+    INDEX_DIR, INDEX_HOSTKEYS, INDEX_HOSTKEYS_TMP, INDEX_RANDSEED,
+    INDEX_SESSIONDIR, INDEX_SESSION,
+};
+
+static const char hex[16] = "0123456789ABCDEF";
+
+static char *mungestr(const char *in)
 {
-    return NULL;
+    char *out, *ret;
+
+    if (!in || !*in)
+        in = "Default Settings";
+
+    ret = out = snewn(3*strlen(in)+1, char);
+
+    while (*in) {
+        /*
+         * There are remarkably few punctuation characters that
+         * aren't shell-special in some way or likely to be used as
+         * separators in some file format or another! Hence we use
+         * opt-in for safe characters rather than opt-out for
+         * specific unsafe ones...
+         */
+       if (*in!='+' && *in!='-' && *in!='.' && *in!='@' && *in!='_' &&
+            !(*in >= '0' && *in <= '9') &&
+            !(*in >= 'A' && *in <= 'Z') &&
+            !(*in >= 'a' && *in <= 'z')) {
+           *out++ = '%';
+           *out++ = hex[((unsigned char) *in) >> 4];
+           *out++ = hex[((unsigned char) *in) & 15];
+       } else
+           *out++ = *in;
+       in++;
+    }
+    *out = '\0';
+    return ret;
+}
+
+static char *unmungestr(const char *in)
+{
+    char *out, *ret;
+    out = ret = snewn(strlen(in)+1, char);
+    while (*in) {
+       if (*in == '%' && in[1] && in[2]) {
+           int i, j;
+
+           i = in[1] - '0';
+           i -= (i > 9 ? 7 : 0);
+           j = in[2] - '0';
+           j -= (j > 9 ? 7 : 0);
+
+           *out++ = (i << 4) + j;
+           in += 3;
+       } else {
+           *out++ = *in++;
+       }
+    }
+    *out = '\0';
+    return ret;
+}
+
+static char *make_filename(int index, const char *subname)
+{
+    char *env, *tmp, *ret;
+
+    /*
+     * Allow override of the PuTTY configuration location, and of
+     * specific subparts of it, by means of environment variables.
+     */
+    if (index == INDEX_DIR) {
+       struct passwd *pwd;
+
+       env = getenv("PUTTYDIR");
+       if (env)
+           return dupstr(env);
+       env = getenv("HOME");
+       if (env)
+           return dupprintf("%s/.putty", env);
+       pwd = getpwuid(getuid());
+       if (pwd && pwd->pw_dir)
+           return dupprintf("%s/.putty", pwd->pw_dir);
+       return dupstr("/.putty");
+    }
+    if (index == INDEX_SESSIONDIR) {
+       env = getenv("PUTTYSESSIONS");
+       if (env)
+           return dupstr(env);
+       tmp = make_filename(INDEX_DIR, NULL);
+       ret = dupprintf("%s/sessions", tmp);
+       sfree(tmp);
+       return ret;
+    }
+    if (index == INDEX_SESSION) {
+        char *munged = mungestr(subname);
+       tmp = make_filename(INDEX_SESSIONDIR, NULL);
+       ret = dupprintf("%s/%s", tmp, munged);
+       sfree(tmp);
+       sfree(munged);
+       return ret;
+    }
+    if (index == INDEX_HOSTKEYS) {
+       env = getenv("PUTTYSSHHOSTKEYS");
+       if (env)
+           return dupstr(env);
+       tmp = make_filename(INDEX_DIR, NULL);
+       ret = dupprintf("%s/sshhostkeys", tmp);
+       sfree(tmp);
+       return ret;
+    }
+    if (index == INDEX_HOSTKEYS_TMP) {
+       tmp = make_filename(INDEX_HOSTKEYS, NULL);
+       ret = dupprintf("%s.tmp", tmp);
+       sfree(tmp);
+       return ret;
+    }
+    if (index == INDEX_RANDSEED) {
+       env = getenv("PUTTYRANDOMSEED");
+       if (env)
+           return dupstr(env);
+       tmp = make_filename(INDEX_DIR, NULL);
+       ret = dupprintf("%s/randomseed", tmp);
+       sfree(tmp);
+       return ret;
+    }
+    tmp = make_filename(INDEX_DIR, NULL);
+    ret = dupprintf("%s/ERROR", tmp);
+    sfree(tmp);
+    return ret;
+}
+
+void *open_settings_w(const char *sessionname, char **errmsg)
+{
+    char *filename;
+    FILE *fp;
+
+    *errmsg = NULL;
+
+    /*
+     * Start by making sure the .putty directory and its sessions
+     * subdir actually exist. Ignore error returns from mkdir since
+     * they're perfectly likely to be `already exists', and any
+     * other error will trip us up later on so there's no real need
+     * to catch it now.
+     */
+    filename = make_filename(INDEX_SESSIONDIR, NULL);
+    if (mkdir(filename, 0700) != 0) {
+       char *filename2 = make_filename(INDEX_DIR, NULL);
+       mkdir(filename2, 0700);
+       sfree(filename2);
+       mkdir(filename, 0700);
+    }
+    sfree(filename);
+
+    filename = make_filename(INDEX_SESSION, sessionname);
+    fp = fopen(filename, "w");
+    if (!fp) {
+        *errmsg = dupprintf("Unable to create %s: %s",
+                            filename, strerror(errno));
+       sfree(filename);
+       return NULL;                   /* can't open */
+    }
+    sfree(filename);
+    return fp;
 }
 
 void write_setting_s(void *handle, const char *key, const char *value)
 {
+    FILE *fp = (FILE *)handle;
+    fprintf(fp, "%s=%s\n", key, value);
 }
 
 void write_setting_i(void *handle, const char *key, int value)
 {
+    FILE *fp = (FILE *)handle;
+    fprintf(fp, "%s=%d\n", key, value);
 }
 
 void close_settings_w(void *handle)
 {
+    FILE *fp = (FILE *)handle;
+    fclose(fp);
 }
 
 /*
@@ -45,26 +217,27 @@ void close_settings_w(void *handle)
  * PuTTY's inbuilt defaults, but that the disk files will then
  * override those. This isn't optimal, but it's the best I can
  * immediately work out.
+ * FIXME: the above comment is a bit out of date. Did it happen?
  */
 
-struct xrm_string {
+struct skeyval {
     const char *key;
     const char *value;
 };
 
 static tree234 *xrmtree = NULL;
 
-int xrmcmp(void *av, void *bv)
+int keycmp(void *av, void *bv)
 {
-    struct xrm_string *a = (struct xrm_string *)av;
-    struct xrm_string *b = (struct xrm_string *)bv;
+    struct skeyval *a = (struct skeyval *)av;
+    struct skeyval *b = (struct skeyval *)bv;
     return strcmp(a->key, b->key);
 }
 
 void provide_xrm_string(char *string)
 {
     char *p, *q, *key;
-    struct xrm_string *xrms, *ret;
+    struct skeyval *xrms, *ret;
 
     p = q = strchr(string, ':');
     if (!q) {
@@ -75,8 +248,8 @@ void provide_xrm_string(char *string)
     q++;
     while (p > string && p[-1] != '.' && p[-1] != '*')
        p--;
-    xrms = smalloc(sizeof(struct xrm_string));
-    key = smalloc(q-p);
+    xrms = snew(struct skeyval);
+    key = snewn(q-p, char);
     memcpy(key, p, q-p);
     key[q-p-1] = '\0';
     xrms->key = key;
@@ -85,7 +258,7 @@ void provide_xrm_string(char *string)
     xrms->value = dupstr(q);
 
     if (!xrmtree)
-       xrmtree = newtree234(xrmcmp);
+       xrmtree = newtree234(keycmp);
 
     ret = add234(xrmtree, xrms);
     if (ret) {
@@ -97,7 +270,7 @@ void provide_xrm_string(char *string)
 
 const char *get_setting(const char *key)
 {
-    struct xrm_string tmp, *ret;
+    struct skeyval tmp, *ret;
     tmp.key = key;
     if (xrmtree) {
        ret = find234(xrmtree, &tmp, NULL);
@@ -109,111 +282,227 @@ const char *get_setting(const char *key)
 
 void *open_settings_r(const char *sessionname)
 {
-    static int thing_to_return_an_arbitrary_non_null_pointer_to;
-    return &thing_to_return_an_arbitrary_non_null_pointer_to;
+    char *filename;
+    FILE *fp;
+    char *line;
+    tree234 *ret;
+
+    filename = make_filename(INDEX_SESSION, sessionname);
+    fp = fopen(filename, "r");
+    sfree(filename);
+    if (!fp)
+       return NULL;                   /* can't open */
+
+    ret = newtree234(keycmp);
+
+    while ( (line = fgetline(fp)) ) {
+        char *value = strchr(line, '=');
+        struct skeyval *kv;
+
+        if (!value) {
+            sfree(line);
+            continue;
+        }
+        *value++ = '\0';
+        value[strcspn(value, "\r\n")] = '\0';   /* trim trailing NL */
+
+        kv = snew(struct skeyval);
+        kv->key = dupstr(line);
+        kv->value = dupstr(value);
+        add234(ret, kv);
+
+        sfree(line);
+    }
+
+    fclose(fp);
+
+    return ret;
 }
 
-char *read_setting_s(void *handle, const char *key, char *buffer, int buflen)
+char *read_setting_s(void *handle, const char *key)
 {
-    const char *val = get_setting(key);
+    tree234 *tree = (tree234 *)handle;
+    const char *val;
+    struct skeyval tmp, *kv;
+
+    tmp.key = key;
+    if (tree != NULL &&
+        (kv = find234(tree, &tmp, NULL)) != NULL) {
+        val = kv->value;
+        assert(val != NULL);
+    } else
+        val = get_setting(key);
+
     if (!val)
        return NULL;
-    else {
-       strncpy(buffer, val, buflen);
-       buffer[buflen-1] = '\0';
-       return buffer;
-    }
+    else
+       return dupstr(val);
 }
 
 int read_setting_i(void *handle, const char *key, int defvalue)
 {
-    const char *val = get_setting(key);
+    tree234 *tree = (tree234 *)handle;
+    const char *val;
+    struct skeyval tmp, *kv;
+
+    tmp.key = key;
+    if (tree != NULL &&
+        (kv = find234(tree, &tmp, NULL)) != NULL) {
+        val = kv->value;
+        assert(val != NULL);
+    } else
+        val = get_setting(key);
+
     if (!val)
        return defvalue;
     else
        return atoi(val);
 }
 
-int read_setting_fontspec(void *handle, const char *name, FontSpec *result)
+FontSpec *read_setting_fontspec(void *handle, const char *name)
 {
-    return !!read_setting_s(handle, name, result->name, sizeof(result->name));
+    /*
+     * In GTK1-only PuTTY, we used to store font names simply as a
+     * valid X font description string (logical or alias), under a
+     * bare key such as "Font".
+     * 
+     * In GTK2 PuTTY, we have a prefix system where "client:"
+     * indicates a Pango font and "server:" an X one; existing
+     * configuration needs to be reinterpreted as having the
+     * "server:" prefix, so we change the storage key from the
+     * provided name string (e.g. "Font") to a suffixed one
+     * ("FontName").
+     */
+    char *suffname = dupcat(name, "Name", NULL);
+    char *tmp;
+
+    if ((tmp = read_setting_s(handle, suffname)) != NULL) {
+        FontSpec *fs = fontspec_new(tmp);
+       sfree(suffname);
+       sfree(tmp);
+       return fs;                     /* got new-style name */
+    }
+    sfree(suffname);
+
+    /* Fall back to old-style name. */
+    tmp = read_setting_s(handle, name);
+    if (tmp && *tmp) {
+        char *tmp2 = dupcat("server:", tmp, NULL);
+        FontSpec *fs = fontspec_new(tmp2);
+       sfree(tmp2);
+       sfree(tmp);
+       return fs;
+    } else {
+       sfree(tmp);
+       return NULL;
+    }
 }
-int read_setting_filename(void *handle, const char *name, Filename *result)
+Filename *read_setting_filename(void *handle, const char *name)
 {
-    return !!read_setting_s(handle, name, result->path, sizeof(result->path));
+    char *tmp = read_setting_s(handle, name);
+    if (tmp) {
+        Filename *ret = filename_from_str(tmp);
+       sfree(tmp);
+       return ret;
+    } else
+       return NULL;
 }
 
-void write_setting_fontspec(void *handle, const char *name, FontSpec result)
+void write_setting_fontspec(void *handle, const char *name, FontSpec *fs)
 {
-    write_setting_s(handle, name, result.name);
+    /*
+     * read_setting_fontspec had to handle two cases, but when
+     * writing our settings back out we simply always generate the
+     * new-style name.
+     */
+    char *suffname = dupcat(name, "Name", NULL);
+    write_setting_s(handle, suffname, fs->name);
+    sfree(suffname);
 }
-void write_setting_filename(void *handle, const char *name, Filename result)
+void write_setting_filename(void *handle, const char *name, Filename *result)
 {
-    write_setting_s(handle, name, result.path);
+    write_setting_s(handle, name, result->path);
 }
 
 void close_settings_r(void *handle)
 {
+    tree234 *tree = (tree234 *)handle;
+    struct skeyval *kv;
+
+    if (!tree)
+        return;
+
+    while ( (kv = index234(tree, 0)) != NULL) {
+        del234(tree, kv);
+        sfree((char *)kv->key);
+        sfree((char *)kv->value);
+        sfree(kv);
+    }
+
+    freetree234(tree);
 }
 
 void del_settings(const char *sessionname)
 {
+    char *filename;
+    filename = make_filename(INDEX_SESSION, sessionname);
+    unlink(filename);
+    sfree(filename);
 }
 
 void *enum_settings_start(void)
 {
-    return NULL;
+    DIR *dp;
+    char *filename;
+
+    filename = make_filename(INDEX_SESSIONDIR, NULL);
+    dp = opendir(filename);
+    sfree(filename);
+
+    return dp;
 }
 
 char *enum_settings_next(void *handle, char *buffer, int buflen)
 {
+    DIR *dp = (DIR *)handle;
+    struct dirent *de;
+    struct stat st;
+    char *fullpath;
+    int maxlen, thislen, len;
+    char *unmunged;
+
+    fullpath = make_filename(INDEX_SESSIONDIR, NULL);
+    maxlen = len = strlen(fullpath);
+
+    while ( (de = readdir(dp)) != NULL ) {
+        thislen = len + 1 + strlen(de->d_name);
+       if (maxlen < thislen) {
+           maxlen = thislen;
+           fullpath = sresize(fullpath, maxlen+1, char);
+       }
+       fullpath[len] = '/';
+       strncpy(fullpath+len+1, de->d_name, thislen - (len+1));
+       fullpath[thislen] = '\0';
+
+        if (stat(fullpath, &st) < 0 || !S_ISREG(st.st_mode))
+            continue;                  /* try another one */
+
+        unmunged = unmungestr(de->d_name);
+        strncpy(buffer, unmunged, buflen);
+        buffer[buflen-1] = '\0';
+        sfree(unmunged);
+       sfree(fullpath);
+        return buffer;
+    }
+
+    sfree(fullpath);
     return NULL;
 }
 
 void enum_settings_finish(void *handle)
 {
-}
-
-enum {
-    INDEX_DIR, INDEX_HOSTKEYS, INDEX_RANDSEED
-};
-
-static void make_filename(char *filename, int index)
-{
-    char *home;
-    int len;
-    home = getenv("HOME");
-    strncpy(filename, home, FILENAME_MAX);
-    len = strlen(filename);
-    strncpy(filename + len,
-           index == INDEX_DIR ? "/.putty" :
-           index == INDEX_HOSTKEYS ? "/.putty/sshhostkeys" :
-           index == INDEX_RANDSEED ? "/.putty/randomseed" :
-           "/.putty/ERROR", FILENAME_MAX - len);
-    filename[FILENAME_MAX-1] = '\0';
-}
-
-/*
- * Read an entire line of text from a file. Return a buffer
- * malloced to be as big as necessary (caller must free).
- */
-static char *fgetline(FILE *fp)
-{
-    char *ret = smalloc(512);
-    int size = 512, len = 0;
-    while (fgets(ret + len, size - len, fp)) {
-       len += strlen(ret + len);
-       if (ret[len-1] == '\n')
-           break;                     /* got a newline, we're done */
-       size = len + 512;
-       ret = srealloc(ret, size);
-    }
-    if (len == 0) {                   /* first fgets returned NULL */
-       sfree(ret);
-       return NULL;
-    }
-    ret[len] = '\0';
-    return ret;
+    DIR *dp = (DIR *)handle;
+    closedir(dp);
 }
 
 /*
@@ -229,12 +518,13 @@ int verify_host_key(const char *hostname, int port,
                    const char *keytype, const char *key)
 {
     FILE *fp;
-    char filename[FILENAME_MAX];
+    char *filename;
     char *line;
     int ret;
 
-    make_filename(filename, INDEX_HOSTKEYS);
+    filename = make_filename(INDEX_HOSTKEYS, NULL);
     fp = fopen(filename, "r");
+    sfree(filename);
     if (!fp)
        return 1;                      /* key does not exist */
 
@@ -289,42 +579,78 @@ int verify_host_key(const char *hostname, int port,
            break;
     }
 
+    fclose(fp);
     return ret;
 }
 
 void store_host_key(const char *hostname, int port,
                    const char *keytype, const char *key)
 {
-    FILE *fp;
-    int fd;
-    char filename[FILENAME_MAX];
+    FILE *rfp, *wfp;
+    char *newtext, *line;
+    int headerlen;
+    char *filename, *tmpfilename;
 
-    make_filename(filename, INDEX_HOSTKEYS);
-    fd = open(filename, O_CREAT | O_APPEND | O_RDWR, 0600);
-    if (fd < 0) {
-       char dir[FILENAME_MAX];
+    /*
+     * Open both the old file and a new file.
+     */
+    tmpfilename = make_filename(INDEX_HOSTKEYS_TMP, NULL);
+    wfp = fopen(tmpfilename, "w");
+    if (!wfp) {
+        char *dir;
+
+        dir = make_filename(INDEX_DIR, NULL);
+        mkdir(dir, 0700);
+       sfree(dir);
 
-       make_filename(dir, INDEX_DIR);
-       mkdir(dir, 0700);
-       fd = open(filename, O_CREAT | O_APPEND | O_RDWR, 0600);
+        wfp = fopen(tmpfilename, "w");
     }
-    if (fd < 0) {
-       perror(filename);
-       exit(1);
+    if (!wfp) {
+       sfree(tmpfilename);
+       return;
     }
-    fp = fdopen(fd, "a");
-    fprintf(fp, "%s@%d:%s %s\n", keytype, port, hostname, key);
-    fclose(fp);
+    filename = make_filename(INDEX_HOSTKEYS, NULL);
+    rfp = fopen(filename, "r");
+
+    newtext = dupprintf("%s@%d:%s %s\n", keytype, port, hostname, key);
+    headerlen = 1 + strcspn(newtext, " ");   /* count the space too */
+
+    /*
+     * Copy all lines from the old file to the new one that _don't_
+     * involve the same host key identifier as the one we're adding.
+     */
+    if (rfp) {
+        while ( (line = fgetline(rfp)) ) {
+            if (strncmp(line, newtext, headerlen))
+                fputs(line, wfp);
+            sfree(line);
+        }
+        fclose(rfp);
+    }
+
+    /*
+     * Now add the new line at the end.
+     */
+    fputs(newtext, wfp);
+
+    fclose(wfp);
+
+    rename(tmpfilename, filename);
+
+    sfree(tmpfilename);
+    sfree(filename);
+    sfree(newtext);
 }
 
 void read_random_seed(noise_consumer_t consumer)
 {
     int fd;
-    char fname[FILENAME_MAX];
+    char *fname;
 
-    make_filename(fname, INDEX_RANDSEED);
+    fname = make_filename(INDEX_RANDSEED, NULL);
     fd = open(fname, O_RDONLY);
-    if (fd) {
+    sfree(fname);
+    if (fd >= 0) {
        char buf[512];
        int ret;
        while ( (ret = read(fd, buf, sizeof(buf))) > 0)
@@ -336,9 +662,9 @@ void read_random_seed(noise_consumer_t consumer)
 void write_random_seed(void *data, int len)
 {
     int fd;
-    char fname[FILENAME_MAX];
+    char *fname;
 
-    make_filename(fname, INDEX_RANDSEED);
+    fname = make_filename(INDEX_RANDSEED, NULL);
     /*
      * Don't truncate the random seed file if it already exists; if
      * something goes wrong half way through writing it, it would
@@ -346,21 +672,51 @@ void write_random_seed(void *data, int len)
      */
     fd = open(fname, O_CREAT | O_WRONLY, 0600);
     if (fd < 0) {
-       char dir[FILENAME_MAX];
+        if (errno != ENOENT) {
+            char *msg = dupprintf("Unable to write random seed: open(\"%s\") "
+                                  "returned '%s'", fname, strerror(errno));
+            nonfatal(msg);
+            sfree(msg);
+            return;
+        }
+       char *dir;
+
+       dir = make_filename(INDEX_DIR, NULL);
+       if (mkdir(dir, 0700) < 0) {
+            char *msg = dupprintf("Unable to write random seed: mkdir(\"%s\") "
+                                  "returned '%s'", dir, strerror(errno));
+            nonfatal(msg);
+            sfree(msg);
+            sfree(dir);
+            return;
+        }
+       sfree(dir);
 
-       make_filename(dir, INDEX_DIR);
-       mkdir(dir, 0700);
        fd = open(fname, O_CREAT | O_WRONLY, 0600);
+        if (errno != ENOENT) {
+            char *msg = dupprintf("Unable to write random seed: open(\"%s\") "
+                                  "returned '%s'", fname, strerror(errno));
+            nonfatal(msg);
+            sfree(msg);
+            return;
+        }
     }
 
     while (len > 0) {
        int ret = write(fd, data, len);
-       if (ret <= 0) break;
+       if (ret < 0) {
+            char *msg = dupprintf("Unable to write random seed: write "
+                                  "returned '%s'", strerror(errno));
+            nonfatal(msg);
+            sfree(msg);
+            break;
+        }
        len -= ret;
        data = (char *)data + len;
     }
 
     close(fd);
+    sfree(fname);
 }
 
 void cleanup_all(void)