]> asedeno.scripts.mit.edu Git - PuTTY.git/blobdiff - windows/winstore.c
A bunch of further warning fixes in the Windows code.
[PuTTY.git] / windows / winstore.c
index 76c73d096d5d89b946e0d3a67e8c75a2b8ffc6be..26cbf63496e9fdbb2c04d439c810144c2738738a 100644 (file)
@@ -6,6 +6,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <limits.h>
+#include <assert.h>
 #include "putty.h"
 #include "storage.h"
 
@@ -109,7 +110,7 @@ void *open_settings_w(const char *sessionname, char **errmsg)
 void write_setting_s(void *handle, const char *key, const char *value)
 {
     if (handle)
-       RegSetValueEx((HKEY) handle, key, 0, REG_SZ, value,
+       RegSetValueEx((HKEY) handle, key, 0, REG_SZ, (CONST BYTE *)value,
                      1 + strlen(value));
 }
 
@@ -152,7 +153,7 @@ void *open_settings_r(const char *sessionname)
 
 char *read_setting_s(void *handle, const char *key)
 {
-    DWORD type, size;
+    DWORD type, allocsize, size;
     char *ret;
 
     if (!handle)
@@ -164,10 +165,17 @@ char *read_setting_s(void *handle, const char *key)
        type != REG_SZ)
        return NULL;
 
-    ret = snewn(size+1, char);
+    allocsize = size+1;         /* allow for an extra NUL if needed */
+    ret = snewn(allocsize, char);
     if (RegQueryValueEx((HKEY) handle, key, 0,
-                       &type, ret, &size) != ERROR_SUCCESS ||
-       type != REG_SZ) return NULL;
+                       &type, (BYTE *)ret, &size) != ERROR_SUCCESS ||
+       type != REG_SZ) {
+        sfree(ret);
+        return NULL;
+    }
+    assert(size < allocsize);
+    ret[size] = '\0'; /* add an extra NUL in case RegQueryValueEx
+                       * didn't supply one */
 
     return ret;
 }
@@ -190,6 +198,7 @@ FontSpec *read_setting_fontspec(void *handle, const char *name)
 {
     char *settingname;
     char *fontname;
+    FontSpec *ret;
     int isbold, height, charset;
 
     fontname = read_setting_s(handle, name);
@@ -199,19 +208,30 @@ FontSpec *read_setting_fontspec(void *handle, const char *name)
     settingname = dupcat(name, "IsBold", NULL);
     isbold = read_setting_i(handle, settingname, -1);
     sfree(settingname);
-    if (isbold == -1) return NULL;
+    if (isbold == -1) {
+        sfree(fontname);
+        return NULL;
+    }
 
     settingname = dupcat(name, "CharSet", NULL);
     charset = read_setting_i(handle, settingname, -1);
     sfree(settingname);
-    if (charset == -1) return NULL;
+    if (charset == -1) {
+        sfree(fontname);
+        return NULL;
+    }
 
     settingname = dupcat(name, "Height", NULL);
     height = read_setting_i(handle, settingname, INT_MIN);
     sfree(settingname);
-    if (height == INT_MIN) return NULL;
+    if (height == INT_MIN) {
+        sfree(fontname);
+        return NULL;
+    }
 
-    return fontspec_new(fontname, isbold, height, charset);
+    ret = fontspec_new(fontname, isbold, height, charset);
+    sfree(fontname);
+    return ret;
 }
 
 void write_setting_fontspec(void *handle, const char *name, FontSpec *font)
@@ -340,17 +360,20 @@ int verify_host_key(const char *hostname, int port,
      * Now read a saved key in from the registry and see what it
      * says.
      */
-    otherstr = snewn(len, char);
     regname = snewn(3 * (strlen(hostname) + strlen(keytype)) + 15, char);
 
     hostkey_regname(regname, hostname, port, keytype);
 
     if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys",
-                  &rkey) != ERROR_SUCCESS)
+                  &rkey) != ERROR_SUCCESS) {
+        sfree(regname);
        return 1;                      /* key does not exist in registry */
+    }
 
     readlen = len;
-    ret = RegQueryValueEx(rkey, regname, NULL, &type, otherstr, &readlen);
+    otherstr = snewn(len, char);
+    ret = RegQueryValueEx(rkey, regname, NULL,
+                          &type, (BYTE *)otherstr, &readlen);
 
     if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA &&
        !strcmp(keytype, "rsa")) {
@@ -363,7 +386,7 @@ int verify_host_key(const char *hostname, int port,
        char *oldstyle = snewn(len + 10, char); /* safety margin */
        readlen = len;
        ret = RegQueryValueEx(rkey, justhost, NULL, &type,
-                             oldstyle, &readlen);
+                             (BYTE *)oldstyle, &readlen);
 
        if (ret == ERROR_SUCCESS && type == REG_SZ) {
            /*
@@ -409,9 +432,11 @@ int verify_host_key(const char *hostname, int port,
             * wrong, and hyper-cautiously do nothing.
             */
            if (!strcmp(otherstr, key))
-               RegSetValueEx(rkey, regname, 0, REG_SZ, otherstr,
+               RegSetValueEx(rkey, regname, 0, REG_SZ, (BYTE *)otherstr,
                              strlen(otherstr) + 1);
        }
+
+        sfree(oldstyle);
     }
 
     RegCloseKey(rkey);
@@ -430,6 +455,16 @@ int verify_host_key(const char *hostname, int port,
        return 0;                      /* key matched OK in registry */
 }
 
+int have_ssh_host_key(const char *hostname, int port,
+                     const char *keytype)
+{
+    /*
+     * If we have a host key, verify_host_key will return 0 or 2.
+     * If we don't have one, it'll return 1.
+     */
+    return verify_host_key(hostname, port, keytype, "") != 1;
+}
+
 void store_host_key(const char *hostname, int port,
                    const char *keytype, const char *key)
 {
@@ -442,7 +477,7 @@ void store_host_key(const char *hostname, int port,
 
     if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys",
                     &rkey) == ERROR_SUCCESS) {
-       RegSetValueEx(rkey, regname, 0, REG_SZ, key, strlen(key) + 1);
+       RegSetValueEx(rkey, regname, 0, REG_SZ, (BYTE *)key, strlen(key) + 1);
        RegCloseKey(rkey);
     } /* else key does not exist in registry */
 
@@ -456,7 +491,10 @@ enum { DEL, OPEN_R, OPEN_W };
 static int try_random_seed(char const *path, int action, HANDLE *ret)
 {
     if (action == DEL) {
-       remove(path);
+        if (!DeleteFile(path) && GetLastError() != ERROR_FILE_NOT_FOUND) {
+            nonfatal("Unable to delete '%s': %s", path,
+                     win_strerror(GetLastError()));
+        }
        *ret = INVALID_HANDLE_VALUE;
        return FALSE;                  /* so we'll do the next ones too */
     }
@@ -499,7 +537,7 @@ static HANDLE access_random_seed(int action)
     if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &rkey) ==
        ERROR_SUCCESS) {
        int ret = RegQueryValueEx(rkey, "RandSeedFile",
-                                 0, &type, seedpath, &size);
+                                 0, &type, (BYTE *)seedpath, &size);
        if (ret != ERROR_SUCCESS || type != REG_SZ)
            seedpath[0] = '\0';
        RegCloseKey(rkey);
@@ -620,7 +658,7 @@ static int transform_jumplist_registry
     int ret;
     HKEY pjumplist_key, psettings_tmp;
     DWORD type;
-    int value_length;
+    DWORD value_length;
     char *old_value, *new_value;
     char *piterator_old, *piterator_new, *piterator_tmp;
 
@@ -635,7 +673,7 @@ static int transform_jumplist_registry
     value_length = 200;
     old_value = snewn(value_length, char);
     ret = RegQueryValueEx(pjumplist_key, reg_jumplist_value, NULL, &type,
-                          old_value, &value_length);
+                          (BYTE *)old_value, &value_length);
     /* When the passed buffer is too small, ERROR_MORE_DATA is
      * returned and the required size is returned in the length
      * argument. */
@@ -643,7 +681,7 @@ static int transform_jumplist_registry
         sfree(old_value);
         old_value = snewn(value_length, char);
         ret = RegQueryValueEx(pjumplist_key, reg_jumplist_value, NULL, &type,
-                              old_value, &value_length);
+                              (BYTE *)old_value, &value_length);
     }
 
     if (ret == ERROR_FILE_NOT_FOUND) {
@@ -717,7 +755,7 @@ static int transform_jumplist_registry
 
         /* Save the new list to the registry. */
         ret = RegSetValueEx(pjumplist_key, reg_jumplist_value, 0, REG_MULTI_SZ,
-                            new_value, piterator_new - new_value);
+                            (BYTE *)new_value, piterator_new - new_value);
 
         sfree(old_value);
         old_value = new_value;
@@ -727,7 +765,7 @@ static int transform_jumplist_registry
     /*
      * Either return or free the result.
      */
-    if (out)
+    if (out && ret == ERROR_SUCCESS)
         *out = old_value;
     else
         sfree(old_value);
@@ -760,7 +798,7 @@ char *get_jumplist_registry_entries (void)
 {
     char *list_value;
 
-    if (transform_jumplist_registry(NULL,NULL,&list_value) != ERROR_SUCCESS) {
+    if (transform_jumplist_registry(NULL,NULL,&list_value) != JUMPLISTREG_OK) {
        list_value = snewn(2, char);
         *list_value = '\0';
         *(list_value + 1) = '\0';