Miscellaneous fixes to finish up `remove-statics'. rlogin.c had a
[u/mdw/putty] / unix / uxstore.c
index 9047988..10e33c0 100644 (file)
  * file somewhere or other.
  */
 
-void *open_settings_w(char *sessionname)
+void *open_settings_w(const char *sessionname)
 {
     return NULL;
 }
 
-void write_setting_s(void *handle, char *key, char *value)
+void write_setting_s(void *handle, const char *key, const char *value)
 {
 }
 
-void write_setting_i(void *handle, char *key, int value)
+void write_setting_i(void *handle, const char *key, int value)
 {
 }
 
@@ -48,8 +48,8 @@ void close_settings_w(void *handle)
  */
 
 struct xrm_string {
-    char *key;
-    char *value;
+    const char *key;
+    const char *value;
 };
 
 static tree234 *xrmtree = NULL;
@@ -63,7 +63,7 @@ int xrmcmp(void *av, void *bv)
 
 void provide_xrm_string(char *string)
 {
-    char *p, *q;
+    char *p, *q, *key;
     struct xrm_string *xrms, *ret;
 
     p = q = strchr(string, ':');
@@ -76,9 +76,10 @@ void provide_xrm_string(char *string)
     while (p > string && p[-1] != '.' && p[-1] != '*')
        p--;
     xrms = smalloc(sizeof(struct xrm_string));
-    xrms->key = smalloc(q-p);
-    memcpy(xrms->key, p, q-p);
-    xrms->key[q-p-1] = '\0';
+    key = smalloc(q-p);
+    memcpy(key, p, q-p);
+    key[q-p-1] = '\0';
+    xrms->key = key;
     while (*q && isspace(*q))
        q++;
     xrms->value = dupstr(q);
@@ -94,7 +95,7 @@ void provide_xrm_string(char *string)
     }
 }
 
-char *get_setting(char *key)
+const char *get_setting(const char *key)
 {
     struct xrm_string tmp, *ret;
     tmp.key = key;
@@ -106,15 +107,15 @@ char *get_setting(char *key)
     return x_get_default(key);
 }
 
-void *open_settings_r(char *sessionname)
+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 *read_setting_s(void *handle, char *key, char *buffer, int buflen)
+char *read_setting_s(void *handle, const char *key, char *buffer, int buflen)
 {
-    char *val = get_setting(key);
+    const char *val = get_setting(key);
     if (!val)
        return NULL;
     else {
@@ -124,9 +125,9 @@ char *read_setting_s(void *handle, char *key, char *buffer, int buflen)
     }
 }
 
-int read_setting_i(void *handle, char *key, int defvalue)
+int read_setting_i(void *handle, const char *key, int defvalue)
 {
-    char *val = get_setting(key);
+    const char *val = get_setting(key);
     if (!val)
        return defvalue;
     else
@@ -137,7 +138,7 @@ void close_settings_r(void *handle)
 {
 }
 
-void del_settings(char *sessionname)
+void del_settings(const char *sessionname)
 {
 }
 
@@ -156,7 +157,7 @@ void enum_settings_finish(void *handle)
 }
 
 enum {
-    INDEX_DIR, INDEX_HOSTKEYS
+    INDEX_DIR, INDEX_HOSTKEYS, INDEX_RANDSEED
 };
 
 static void make_filename(char *filename, int index)
@@ -169,6 +170,7 @@ static void make_filename(char *filename, int index)
     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';
 }
@@ -205,7 +207,8 @@ static char *fgetline(FILE *fp)
  * 
  *   rsa@22:foovax.example.org 0x23,0x293487364395345345....2343
  */
-int verify_host_key(char *hostname, int port, char *keytype, char *key)
+int verify_host_key(const char *hostname, int port,
+                   const char *keytype, const char *key)
 {
     FILE *fp;
     char filename[FILENAME_MAX];
@@ -271,7 +274,8 @@ int verify_host_key(char *hostname, int port, char *keytype, char *key)
     return ret;
 }
 
-void store_host_key(char *hostname, int port, char *keytype, char *key)
+void store_host_key(const char *hostname, int port,
+                   const char *keytype, const char *key)
 {
     FILE *fp;
     int fd;
@@ -297,10 +301,48 @@ void store_host_key(char *hostname, int port, char *keytype, char *key)
 
 void read_random_seed(noise_consumer_t consumer)
 {
+    int fd;
+    char fname[FILENAME_MAX];
+
+    make_filename(fname, INDEX_RANDSEED);
+    fd = open(fname, O_RDONLY);
+    if (fd) {
+       char buf[512];
+       int ret;
+       while ( (ret = read(fd, buf, sizeof(buf))) > 0)
+           consumer(buf, ret);
+       close(fd);
+    }
 }
 
 void write_random_seed(void *data, int len)
 {
+    int fd;
+    char fname[FILENAME_MAX];
+
+    make_filename(fname, INDEX_RANDSEED);
+    /*
+     * Don't truncate the random seed file if it already exists; if
+     * something goes wrong half way through writing it, it would
+     * be better to leave the old data there than to leave it empty.
+     */
+    fd = open(fname, O_CREAT | O_WRONLY, 0600);
+    if (fd < 0) {
+       char dir[FILENAME_MAX];
+
+       make_filename(dir, INDEX_DIR);
+       mkdir(dir, 0700);
+       fd = open(fname, O_CREAT | O_WRONLY, 0600);
+    }
+
+    while (len > 0) {
+       int ret = write(fd, data, len);
+       if (ret <= 0) break;
+       len -= ret;
+       data = (char *)data + len;
+    }
+
+    close(fd);
 }
 
 void cleanup_all(void)