2000-09-27 15:21:04 +00:00
|
|
|
/*
|
|
|
|
* winstore.c: Windows-specific implementation of the interface
|
|
|
|
* defined in storage.h.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2000-10-24 10:47:49 +00:00
|
|
|
#include <stdlib.h>
|
2003-02-01 12:54:40 +00:00
|
|
|
#include <limits.h>
|
2000-09-27 15:21:04 +00:00
|
|
|
#include "putty.h"
|
|
|
|
#include "storage.h"
|
|
|
|
|
2007-01-09 18:05:17 +00:00
|
|
|
#include <shlobj.h>
|
|
|
|
#ifndef CSIDL_APPDATA
|
|
|
|
#define CSIDL_APPDATA 0x001a
|
|
|
|
#endif
|
|
|
|
#ifndef CSIDL_LOCAL_APPDATA
|
|
|
|
#define CSIDL_LOCAL_APPDATA 0x001c
|
|
|
|
#endif
|
2000-09-27 16:21:52 +00:00
|
|
|
|
2007-01-09 18:05:17 +00:00
|
|
|
static const char *const puttystr = PUTTY_REG_POS "\\Sessions";
|
2000-09-27 15:21:04 +00:00
|
|
|
|
2003-01-14 18:43:45 +00:00
|
|
|
static const char hex[16] = "0123456789ABCDEF";
|
2000-09-27 15:21:04 +00:00
|
|
|
|
2007-01-09 18:05:17 +00:00
|
|
|
static int tried_shgetfolderpath = FALSE;
|
|
|
|
static HMODULE shell32_module = NULL;
|
|
|
|
typedef HRESULT (WINAPI *p_SHGetFolderPath_t)
|
|
|
|
(HWND, int, HANDLE, DWORD, LPTSTR);
|
|
|
|
static p_SHGetFolderPath_t p_SHGetFolderPath = NULL;
|
|
|
|
|
2003-01-14 18:43:45 +00:00
|
|
|
static void mungestr(const char *in, char *out)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2000-09-27 15:21:04 +00:00
|
|
|
int candot = 0;
|
|
|
|
|
|
|
|
while (*in) {
|
|
|
|
if (*in == ' ' || *in == '\\' || *in == '*' || *in == '?' ||
|
2001-05-06 14:35:20 +00:00
|
|
|
*in == '%' || *in < ' ' || *in > '~' || (*in == '.'
|
|
|
|
&& !candot)) {
|
2000-09-27 15:21:04 +00:00
|
|
|
*out++ = '%';
|
2001-05-06 14:35:20 +00:00
|
|
|
*out++ = hex[((unsigned char) *in) >> 4];
|
|
|
|
*out++ = hex[((unsigned char) *in) & 15];
|
2000-09-27 15:21:04 +00:00
|
|
|
} else
|
|
|
|
*out++ = *in;
|
|
|
|
in++;
|
|
|
|
candot = 1;
|
|
|
|
}
|
|
|
|
*out = '\0';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-01-14 18:43:45 +00:00
|
|
|
static void unmungestr(const char *in, char *out, int outlen)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2000-09-27 15:21:04 +00:00
|
|
|
while (*in) {
|
|
|
|
if (*in == '%' && in[1] && in[2]) {
|
|
|
|
int i, j;
|
|
|
|
|
2001-05-06 14:35:20 +00:00
|
|
|
i = in[1] - '0';
|
|
|
|
i -= (i > 9 ? 7 : 0);
|
|
|
|
j = in[2] - '0';
|
|
|
|
j -= (j > 9 ? 7 : 0);
|
2000-09-27 15:21:04 +00:00
|
|
|
|
2001-05-06 14:35:20 +00:00
|
|
|
*out++ = (i << 4) + j;
|
|
|
|
if (!--outlen)
|
|
|
|
return;
|
2000-09-27 15:21:04 +00:00
|
|
|
in += 3;
|
2000-09-27 16:21:52 +00:00
|
|
|
} else {
|
2000-09-27 15:21:04 +00:00
|
|
|
*out++ = *in++;
|
2001-05-06 14:35:20 +00:00
|
|
|
if (!--outlen)
|
|
|
|
return;
|
|
|
|
}
|
2000-09-27 15:21:04 +00:00
|
|
|
}
|
|
|
|
*out = '\0';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-04-01 18:10:25 +00:00
|
|
|
void *open_settings_w(const char *sessionname, char **errmsg)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2000-09-27 16:21:52 +00:00
|
|
|
HKEY subkey1, sesskey;
|
|
|
|
int ret;
|
|
|
|
char *p;
|
|
|
|
|
2003-04-01 18:10:25 +00:00
|
|
|
*errmsg = NULL;
|
|
|
|
|
2003-03-22 09:49:20 +00:00
|
|
|
if (!sessionname || !*sessionname)
|
|
|
|
sessionname = "Default Settings";
|
|
|
|
|
2003-03-29 16:14:26 +00:00
|
|
|
p = snewn(3 * strlen(sessionname) + 1, char);
|
2000-09-27 16:21:52 +00:00
|
|
|
mungestr(sessionname, p);
|
2001-05-06 14:35:20 +00:00
|
|
|
|
2000-09-27 16:21:52 +00:00
|
|
|
ret = RegCreateKey(HKEY_CURRENT_USER, puttystr, &subkey1);
|
|
|
|
if (ret != ERROR_SUCCESS) {
|
2001-05-06 14:35:20 +00:00
|
|
|
sfree(p);
|
2003-04-01 18:10:25 +00:00
|
|
|
*errmsg = dupprintf("Unable to create registry key\n"
|
2004-11-20 19:07:34 +00:00
|
|
|
"HKEY_CURRENT_USER\\%s", puttystr);
|
2001-05-06 14:35:20 +00:00
|
|
|
return NULL;
|
2000-09-27 16:21:52 +00:00
|
|
|
}
|
|
|
|
ret = RegCreateKey(subkey1, p, &sesskey);
|
|
|
|
RegCloseKey(subkey1);
|
2003-04-01 18:10:25 +00:00
|
|
|
if (ret != ERROR_SUCCESS) {
|
|
|
|
*errmsg = dupprintf("Unable to create registry key\n"
|
2004-11-20 19:07:34 +00:00
|
|
|
"HKEY_CURRENT_USER\\%s\\%s", puttystr, p);
|
2004-12-22 23:17:02 +00:00
|
|
|
sfree(p);
|
2001-05-06 14:35:20 +00:00
|
|
|
return NULL;
|
2003-04-01 18:10:25 +00:00
|
|
|
}
|
2004-12-22 23:17:02 +00:00
|
|
|
sfree(p);
|
2001-05-06 14:35:20 +00:00
|
|
|
return (void *) sesskey;
|
2000-09-27 16:21:52 +00:00
|
|
|
}
|
|
|
|
|
2003-01-14 18:43:45 +00:00
|
|
|
void write_setting_s(void *handle, const char *key, const char *value)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2000-09-27 16:21:52 +00:00
|
|
|
if (handle)
|
2001-05-06 14:35:20 +00:00
|
|
|
RegSetValueEx((HKEY) handle, key, 0, REG_SZ, value,
|
|
|
|
1 + strlen(value));
|
2000-09-27 16:21:52 +00:00
|
|
|
}
|
|
|
|
|
2003-01-14 18:43:45 +00:00
|
|
|
void write_setting_i(void *handle, const char *key, int value)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2000-09-27 16:21:52 +00:00
|
|
|
if (handle)
|
2001-05-06 14:35:20 +00:00
|
|
|
RegSetValueEx((HKEY) handle, key, 0, REG_DWORD,
|
2005-03-02 15:53:50 +00:00
|
|
|
(CONST BYTE *) &value, sizeof(value));
|
2000-09-27 16:21:52 +00:00
|
|
|
}
|
|
|
|
|
2001-05-06 14:35:20 +00:00
|
|
|
void close_settings_w(void *handle)
|
|
|
|
{
|
|
|
|
RegCloseKey((HKEY) handle);
|
2000-09-27 16:21:52 +00:00
|
|
|
}
|
|
|
|
|
2003-01-14 18:43:45 +00:00
|
|
|
void *open_settings_r(const char *sessionname)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2000-09-27 16:21:52 +00:00
|
|
|
HKEY subkey1, sesskey;
|
|
|
|
char *p;
|
|
|
|
|
2003-03-22 09:49:20 +00:00
|
|
|
if (!sessionname || !*sessionname)
|
|
|
|
sessionname = "Default Settings";
|
|
|
|
|
2003-03-29 16:14:26 +00:00
|
|
|
p = snewn(3 * strlen(sessionname) + 1, char);
|
2000-09-27 16:21:52 +00:00
|
|
|
mungestr(sessionname, p);
|
|
|
|
|
|
|
|
if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS) {
|
|
|
|
sesskey = NULL;
|
|
|
|
} else {
|
|
|
|
if (RegOpenKey(subkey1, p, &sesskey) != ERROR_SUCCESS) {
|
|
|
|
sesskey = NULL;
|
|
|
|
}
|
|
|
|
RegCloseKey(subkey1);
|
|
|
|
}
|
|
|
|
|
2000-12-12 10:33:13 +00:00
|
|
|
sfree(p);
|
2000-09-27 16:21:52 +00:00
|
|
|
|
2001-05-06 14:35:20 +00:00
|
|
|
return (void *) sesskey;
|
2000-09-27 16:21:52 +00:00
|
|
|
}
|
|
|
|
|
2003-01-14 18:43:45 +00:00
|
|
|
char *read_setting_s(void *handle, const char *key, char *buffer, int buflen)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2000-09-27 16:21:52 +00:00
|
|
|
DWORD type, size;
|
|
|
|
size = buflen;
|
|
|
|
|
|
|
|
if (!handle ||
|
2001-05-06 14:35:20 +00:00
|
|
|
RegQueryValueEx((HKEY) handle, key, 0,
|
|
|
|
&type, buffer, &size) != ERROR_SUCCESS ||
|
|
|
|
type != REG_SZ) return NULL;
|
2000-09-27 16:21:52 +00:00
|
|
|
else
|
2001-05-06 14:35:20 +00:00
|
|
|
return buffer;
|
2000-09-27 16:21:52 +00:00
|
|
|
}
|
2000-09-27 15:21:04 +00:00
|
|
|
|
2003-01-14 18:43:45 +00:00
|
|
|
int read_setting_i(void *handle, const char *key, int defvalue)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2000-09-27 16:21:52 +00:00
|
|
|
DWORD type, val, size;
|
|
|
|
size = sizeof(val);
|
|
|
|
|
|
|
|
if (!handle ||
|
2001-05-06 14:35:20 +00:00
|
|
|
RegQueryValueEx((HKEY) handle, key, 0, &type,
|
2005-03-02 15:53:50 +00:00
|
|
|
(BYTE *) &val, &size) != ERROR_SUCCESS ||
|
2000-09-27 16:21:52 +00:00
|
|
|
size != sizeof(val) || type != REG_DWORD)
|
|
|
|
return defvalue;
|
|
|
|
else
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2003-02-01 12:54:40 +00:00
|
|
|
int read_setting_fontspec(void *handle, const char *name, FontSpec *result)
|
|
|
|
{
|
|
|
|
char *settingname;
|
|
|
|
FontSpec ret;
|
|
|
|
|
|
|
|
if (!read_setting_s(handle, name, ret.name, sizeof(ret.name)))
|
|
|
|
return 0;
|
|
|
|
settingname = dupcat(name, "IsBold", NULL);
|
|
|
|
ret.isbold = read_setting_i(handle, settingname, -1);
|
|
|
|
sfree(settingname);
|
|
|
|
if (ret.isbold == -1) return 0;
|
|
|
|
settingname = dupcat(name, "CharSet", NULL);
|
|
|
|
ret.charset = read_setting_i(handle, settingname, -1);
|
|
|
|
sfree(settingname);
|
|
|
|
if (ret.charset == -1) return 0;
|
|
|
|
settingname = dupcat(name, "Height", NULL);
|
|
|
|
ret.height = read_setting_i(handle, settingname, INT_MIN);
|
|
|
|
sfree(settingname);
|
|
|
|
if (ret.height == INT_MIN) return 0;
|
|
|
|
*result = ret;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_setting_fontspec(void *handle, const char *name, FontSpec font)
|
|
|
|
{
|
|
|
|
char *settingname;
|
|
|
|
|
|
|
|
write_setting_s(handle, name, font.name);
|
|
|
|
settingname = dupcat(name, "IsBold", NULL);
|
|
|
|
write_setting_i(handle, settingname, font.isbold);
|
|
|
|
sfree(settingname);
|
|
|
|
settingname = dupcat(name, "CharSet", NULL);
|
|
|
|
write_setting_i(handle, settingname, font.charset);
|
|
|
|
sfree(settingname);
|
|
|
|
settingname = dupcat(name, "Height", NULL);
|
|
|
|
write_setting_i(handle, settingname, font.height);
|
|
|
|
sfree(settingname);
|
|
|
|
}
|
|
|
|
|
|
|
|
int read_setting_filename(void *handle, const char *name, Filename *result)
|
|
|
|
{
|
|
|
|
return !!read_setting_s(handle, name, result->path, sizeof(result->path));
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_setting_filename(void *handle, const char *name, Filename result)
|
|
|
|
{
|
|
|
|
write_setting_s(handle, name, result.path);
|
|
|
|
}
|
|
|
|
|
2001-05-06 14:35:20 +00:00
|
|
|
void close_settings_r(void *handle)
|
|
|
|
{
|
|
|
|
RegCloseKey((HKEY) handle);
|
2000-09-27 16:21:52 +00:00
|
|
|
}
|
|
|
|
|
2003-01-14 18:43:45 +00:00
|
|
|
void del_settings(const char *sessionname)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2000-09-27 16:21:52 +00:00
|
|
|
HKEY subkey1;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS)
|
|
|
|
return;
|
|
|
|
|
2003-03-29 16:14:26 +00:00
|
|
|
p = snewn(3 * strlen(sessionname) + 1, char);
|
2000-09-27 16:21:52 +00:00
|
|
|
mungestr(sessionname, p);
|
|
|
|
RegDeleteKey(subkey1, p);
|
2000-12-12 10:33:13 +00:00
|
|
|
sfree(p);
|
2000-09-27 16:21:52 +00:00
|
|
|
|
|
|
|
RegCloseKey(subkey1);
|
|
|
|
}
|
2000-09-27 15:21:04 +00:00
|
|
|
|
2000-09-27 16:21:52 +00:00
|
|
|
struct enumsettings {
|
|
|
|
HKEY key;
|
|
|
|
int i;
|
|
|
|
};
|
|
|
|
|
2001-05-06 14:35:20 +00:00
|
|
|
void *enum_settings_start(void)
|
|
|
|
{
|
2000-09-27 16:21:52 +00:00
|
|
|
struct enumsettings *ret;
|
|
|
|
HKEY key;
|
|
|
|
|
2001-12-29 14:18:51 +00:00
|
|
|
if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &key) != ERROR_SUCCESS)
|
2001-05-06 14:35:20 +00:00
|
|
|
return NULL;
|
2000-09-27 16:21:52 +00:00
|
|
|
|
2003-03-29 16:14:26 +00:00
|
|
|
ret = snew(struct enumsettings);
|
2000-09-27 16:21:52 +00:00
|
|
|
if (ret) {
|
2001-05-06 14:35:20 +00:00
|
|
|
ret->key = key;
|
|
|
|
ret->i = 0;
|
2000-09-27 16:21:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-05-06 14:35:20 +00:00
|
|
|
char *enum_settings_next(void *handle, char *buffer, int buflen)
|
|
|
|
{
|
|
|
|
struct enumsettings *e = (struct enumsettings *) handle;
|
2000-09-27 16:21:52 +00:00
|
|
|
char *otherbuf;
|
2003-03-29 16:14:26 +00:00
|
|
|
otherbuf = snewn(3 * buflen, char);
|
2002-11-17 15:06:16 +00:00
|
|
|
if (RegEnumKey(e->key, e->i++, otherbuf, 3 * buflen) == ERROR_SUCCESS) {
|
2001-05-06 14:35:20 +00:00
|
|
|
unmungestr(otherbuf, buffer, buflen);
|
|
|
|
sfree(otherbuf);
|
|
|
|
return buffer;
|
2002-11-17 15:06:16 +00:00
|
|
|
} else {
|
|
|
|
sfree(otherbuf);
|
2001-05-06 14:35:20 +00:00
|
|
|
return NULL;
|
2002-11-17 15:06:16 +00:00
|
|
|
}
|
2000-09-27 16:21:52 +00:00
|
|
|
}
|
|
|
|
|
2001-05-06 14:35:20 +00:00
|
|
|
void enum_settings_finish(void *handle)
|
|
|
|
{
|
|
|
|
struct enumsettings *e = (struct enumsettings *) handle;
|
2000-09-27 16:21:52 +00:00
|
|
|
RegCloseKey(e->key);
|
2000-12-12 10:33:13 +00:00
|
|
|
sfree(e);
|
2000-09-27 16:21:52 +00:00
|
|
|
}
|
|
|
|
|
2003-01-14 18:43:45 +00:00
|
|
|
static void hostkey_regname(char *buffer, const char *hostname,
|
|
|
|
int port, const char *keytype)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2000-09-28 08:37:10 +00:00
|
|
|
int len;
|
|
|
|
strcpy(buffer, keytype);
|
|
|
|
strcat(buffer, "@");
|
|
|
|
len = strlen(buffer);
|
2001-05-06 14:35:20 +00:00
|
|
|
len += sprintf(buffer + len, "%d:", port);
|
2000-09-28 08:37:10 +00:00
|
|
|
mungestr(hostname, buffer + strlen(buffer));
|
|
|
|
}
|
|
|
|
|
2003-01-14 18:43:45 +00:00
|
|
|
int verify_host_key(const char *hostname, int port,
|
|
|
|
const char *keytype, const char *key)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2000-09-27 15:21:04 +00:00
|
|
|
char *otherstr, *regname;
|
|
|
|
int len;
|
|
|
|
HKEY rkey;
|
|
|
|
DWORD readlen;
|
|
|
|
DWORD type;
|
|
|
|
int ret, compare;
|
|
|
|
|
|
|
|
len = 1 + strlen(key);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now read a saved key in from the registry and see what it
|
|
|
|
* says.
|
|
|
|
*/
|
2003-03-29 16:14:26 +00:00
|
|
|
otherstr = snewn(len, char);
|
|
|
|
regname = snewn(3 * (strlen(hostname) + strlen(keytype)) + 15, char);
|
2000-09-27 15:21:04 +00:00
|
|
|
|
2000-09-28 08:37:10 +00:00
|
|
|
hostkey_regname(regname, hostname, port, keytype);
|
2000-09-27 15:21:04 +00:00
|
|
|
|
2001-12-29 14:18:51 +00:00
|
|
|
if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys",
|
|
|
|
&rkey) != ERROR_SUCCESS)
|
2001-05-06 14:35:20 +00:00
|
|
|
return 1; /* key does not exist in registry */
|
2000-09-27 15:21:04 +00:00
|
|
|
|
|
|
|
readlen = len;
|
|
|
|
ret = RegQueryValueEx(rkey, regname, NULL, &type, otherstr, &readlen);
|
|
|
|
|
|
|
|
if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA &&
|
2001-05-06 14:35:20 +00:00
|
|
|
!strcmp(keytype, "rsa")) {
|
|
|
|
/*
|
|
|
|
* Key didn't exist. If the key type is RSA, we'll try
|
|
|
|
* another trick, which is to look up the _old_ key format
|
|
|
|
* under just the hostname and translate that.
|
|
|
|
*/
|
|
|
|
char *justhost = regname + 1 + strcspn(regname, ":");
|
2003-03-29 16:14:26 +00:00
|
|
|
char *oldstyle = snewn(len + 10, char); /* safety margin */
|
2001-05-06 14:35:20 +00:00
|
|
|
readlen = len;
|
|
|
|
ret = RegQueryValueEx(rkey, justhost, NULL, &type,
|
|
|
|
oldstyle, &readlen);
|
|
|
|
|
|
|
|
if (ret == ERROR_SUCCESS && type == REG_SZ) {
|
|
|
|
/*
|
|
|
|
* The old format is two old-style bignums separated by
|
|
|
|
* a slash. An old-style bignum is made of groups of
|
|
|
|
* four hex digits: digits are ordered in sensible
|
|
|
|
* (most to least significant) order within each group,
|
|
|
|
* but groups are ordered in silly (least to most)
|
|
|
|
* order within the bignum. The new format is two
|
|
|
|
* ordinary C-format hex numbers (0xABCDEFG...XYZ, with
|
|
|
|
* A nonzero except in the special case 0x0, which
|
|
|
|
* doesn't appear anyway in RSA keys) separated by a
|
|
|
|
* comma. All hex digits are lowercase in both formats.
|
|
|
|
*/
|
|
|
|
char *p = otherstr;
|
|
|
|
char *q = oldstyle;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
int ndigits, nwords;
|
|
|
|
*p++ = '0';
|
|
|
|
*p++ = 'x';
|
|
|
|
ndigits = strcspn(q, "/"); /* find / or end of string */
|
|
|
|
nwords = ndigits / 4;
|
|
|
|
/* now trim ndigits to remove leading zeros */
|
|
|
|
while (q[(ndigits - 1) ^ 3] == '0' && ndigits > 1)
|
|
|
|
ndigits--;
|
|
|
|
/* now move digits over to new string */
|
|
|
|
for (j = 0; j < ndigits; j++)
|
|
|
|
p[ndigits - 1 - j] = q[j ^ 3];
|
|
|
|
p += ndigits;
|
|
|
|
q += nwords * 4;
|
|
|
|
if (*q) {
|
|
|
|
q++; /* eat the slash */
|
|
|
|
*p++ = ','; /* add a comma */
|
|
|
|
}
|
|
|
|
*p = '\0'; /* terminate the string */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now _if_ this key matches, we'll enter it in the new
|
|
|
|
* format. If not, we'll assume something odd went
|
|
|
|
* wrong, and hyper-cautiously do nothing.
|
|
|
|
*/
|
|
|
|
if (!strcmp(otherstr, key))
|
|
|
|
RegSetValueEx(rkey, regname, 0, REG_SZ, otherstr,
|
|
|
|
strlen(otherstr) + 1);
|
|
|
|
}
|
2000-09-27 15:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RegCloseKey(rkey);
|
|
|
|
|
|
|
|
compare = strcmp(otherstr, key);
|
|
|
|
|
|
|
|
sfree(otherstr);
|
|
|
|
sfree(regname);
|
|
|
|
|
|
|
|
if (ret == ERROR_MORE_DATA ||
|
2001-05-06 14:35:20 +00:00
|
|
|
(ret == ERROR_SUCCESS && type == REG_SZ && compare))
|
|
|
|
return 2; /* key is different in registry */
|
2000-09-27 15:21:04 +00:00
|
|
|
else if (ret != ERROR_SUCCESS || type != REG_SZ)
|
2001-05-06 14:35:20 +00:00
|
|
|
return 1; /* key does not exist in registry */
|
2000-09-27 15:21:04 +00:00
|
|
|
else
|
2001-05-06 14:35:20 +00:00
|
|
|
return 0; /* key matched OK in registry */
|
2000-09-27 15:21:04 +00:00
|
|
|
}
|
|
|
|
|
2003-01-14 18:43:45 +00:00
|
|
|
void store_host_key(const char *hostname, int port,
|
|
|
|
const char *keytype, const char *key)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2000-09-27 15:21:04 +00:00
|
|
|
char *regname;
|
|
|
|
HKEY rkey;
|
|
|
|
|
2003-03-29 16:14:26 +00:00
|
|
|
regname = snewn(3 * (strlen(hostname) + strlen(keytype)) + 15, char);
|
2000-09-27 15:21:04 +00:00
|
|
|
|
2000-09-28 08:37:10 +00:00
|
|
|
hostkey_regname(regname, hostname, port, keytype);
|
2000-09-27 15:21:04 +00:00
|
|
|
|
|
|
|
if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys",
|
2005-05-20 21:52:07 +00:00
|
|
|
&rkey) == ERROR_SUCCESS) {
|
|
|
|
RegSetValueEx(rkey, regname, 0, REG_SZ, key, strlen(key) + 1);
|
|
|
|
RegCloseKey(rkey);
|
|
|
|
} /* else key does not exist in registry */
|
|
|
|
|
|
|
|
sfree(regname);
|
2000-09-27 15:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-01-09 18:05:17 +00:00
|
|
|
* Open (or delete) the random seed file.
|
2000-09-27 15:21:04 +00:00
|
|
|
*/
|
2007-01-09 18:05:17 +00:00
|
|
|
enum { DEL, OPEN_R, OPEN_W };
|
|
|
|
static int try_random_seed(char const *path, int action, HANDLE *ret)
|
|
|
|
{
|
|
|
|
if (action == DEL) {
|
|
|
|
remove(path);
|
|
|
|
*ret = INVALID_HANDLE_VALUE;
|
|
|
|
return FALSE; /* so we'll do the next ones too */
|
|
|
|
}
|
|
|
|
|
|
|
|
*ret = CreateFile(path,
|
|
|
|
action == OPEN_W ? GENERIC_WRITE : GENERIC_READ,
|
|
|
|
action == OPEN_W ? 0 : (FILE_SHARE_READ |
|
|
|
|
FILE_SHARE_WRITE),
|
|
|
|
NULL,
|
|
|
|
action == OPEN_W ? CREATE_ALWAYS : OPEN_EXISTING,
|
|
|
|
action == OPEN_W ? FILE_ATTRIBUTE_NORMAL : 0,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
return (*ret != INVALID_HANDLE_VALUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static HANDLE access_random_seed(int action)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2000-09-27 15:21:04 +00:00
|
|
|
HKEY rkey;
|
|
|
|
DWORD type, size;
|
2007-01-09 18:05:17 +00:00
|
|
|
HANDLE rethandle;
|
|
|
|
char seedpath[2 * MAX_PATH + 10] = "\0";
|
2000-09-27 15:21:04 +00:00
|
|
|
|
2007-01-09 18:05:17 +00:00
|
|
|
/*
|
|
|
|
* Iterate over a selection of possible random seed paths until
|
|
|
|
* we find one that works.
|
|
|
|
*
|
|
|
|
* We do this iteration separately for reading and writing,
|
|
|
|
* meaning that we will automatically migrate random seed files
|
|
|
|
* if a better location becomes available (by reading from the
|
|
|
|
* best location in which we actually find one, and then
|
|
|
|
* writing to the best location in which we can _create_ one).
|
|
|
|
*/
|
2000-09-27 15:21:04 +00:00
|
|
|
|
2007-01-09 18:05:17 +00:00
|
|
|
/*
|
|
|
|
* First, try the location specified by the user in the
|
|
|
|
* Registry, if any.
|
|
|
|
*/
|
|
|
|
size = sizeof(seedpath);
|
2001-05-06 14:35:20 +00:00
|
|
|
if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &rkey) ==
|
|
|
|
ERROR_SUCCESS) {
|
2000-09-27 15:21:04 +00:00
|
|
|
int ret = RegQueryValueEx(rkey, "RandSeedFile",
|
|
|
|
0, &type, seedpath, &size);
|
|
|
|
if (ret != ERROR_SUCCESS || type != REG_SZ)
|
|
|
|
seedpath[0] = '\0';
|
|
|
|
RegCloseKey(rkey);
|
|
|
|
|
2007-01-09 18:05:17 +00:00
|
|
|
if (*seedpath && try_random_seed(seedpath, action, &rethandle))
|
|
|
|
return rethandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Next, try the user's local Application Data directory,
|
|
|
|
* followed by their non-local one. This is found using the
|
|
|
|
* SHGetFolderPath function, which won't be present on all
|
|
|
|
* versions of Windows.
|
|
|
|
*/
|
|
|
|
if (!tried_shgetfolderpath) {
|
2007-01-09 23:47:15 +00:00
|
|
|
/* This is likely only to bear fruit on systems with IE5+
|
|
|
|
* installed, or WinMe/2K+. There is some faffing with
|
|
|
|
* SHFOLDER.DLL we could do to try to find an equivalent
|
|
|
|
* on older versions of Windows if we cared enough.
|
|
|
|
* However, the invocation below requires IE5+ anyway,
|
|
|
|
* so stuff that. */
|
2007-01-09 18:05:17 +00:00
|
|
|
shell32_module = LoadLibrary("SHELL32.DLL");
|
|
|
|
if (shell32_module) {
|
|
|
|
p_SHGetFolderPath = (p_SHGetFolderPath_t)
|
|
|
|
GetProcAddress(shell32_module, "SHGetFolderPathA");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p_SHGetFolderPath) {
|
|
|
|
if (SUCCEEDED(p_SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA,
|
|
|
|
NULL, SHGFP_TYPE_CURRENT, seedpath))) {
|
|
|
|
strcat(seedpath, "\\PUTTY.RND");
|
|
|
|
if (try_random_seed(seedpath, action, &rethandle))
|
|
|
|
return rethandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SUCCEEDED(p_SHGetFolderPath(NULL, CSIDL_APPDATA,
|
|
|
|
NULL, SHGFP_TYPE_CURRENT, seedpath))) {
|
|
|
|
strcat(seedpath, "\\PUTTY.RND");
|
|
|
|
if (try_random_seed(seedpath, action, &rethandle))
|
|
|
|
return rethandle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Failing that, try %HOMEDRIVE%%HOMEPATH% as a guess at the
|
|
|
|
* user's home directory.
|
|
|
|
*/
|
|
|
|
{
|
2000-09-27 15:21:04 +00:00
|
|
|
int len, ret;
|
|
|
|
|
2001-05-06 14:35:20 +00:00
|
|
|
len =
|
|
|
|
GetEnvironmentVariable("HOMEDRIVE", seedpath,
|
|
|
|
sizeof(seedpath));
|
|
|
|
ret =
|
|
|
|
GetEnvironmentVariable("HOMEPATH", seedpath + len,
|
|
|
|
sizeof(seedpath) - len);
|
2007-01-09 18:05:17 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
strcat(seedpath, "\\PUTTY.RND");
|
|
|
|
if (try_random_seed(seedpath, action, &rethandle))
|
|
|
|
return rethandle;
|
|
|
|
}
|
2000-09-27 15:21:04 +00:00
|
|
|
}
|
2007-01-09 18:05:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* And finally, fall back to C:\WINDOWS.
|
|
|
|
*/
|
|
|
|
GetWindowsDirectory(seedpath, sizeof(seedpath));
|
|
|
|
strcat(seedpath, "\\PUTTY.RND");
|
|
|
|
if (try_random_seed(seedpath, action, &rethandle))
|
|
|
|
return rethandle;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If even that failed, give up.
|
|
|
|
*/
|
|
|
|
return INVALID_HANDLE_VALUE;
|
2000-09-27 15:21:04 +00:00
|
|
|
}
|
|
|
|
|
2001-05-06 14:35:20 +00:00
|
|
|
void read_random_seed(noise_consumer_t consumer)
|
|
|
|
{
|
2007-01-09 18:05:17 +00:00
|
|
|
HANDLE seedf = access_random_seed(OPEN_R);
|
2000-09-27 15:21:04 +00:00
|
|
|
|
|
|
|
if (seedf != INVALID_HANDLE_VALUE) {
|
|
|
|
while (1) {
|
|
|
|
char buf[1024];
|
|
|
|
DWORD len;
|
|
|
|
|
|
|
|
if (ReadFile(seedf, buf, sizeof(buf), &len, NULL) && len)
|
2001-05-06 14:35:20 +00:00
|
|
|
consumer(buf, len);
|
2000-09-27 15:21:04 +00:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
CloseHandle(seedf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-06 14:35:20 +00:00
|
|
|
void write_random_seed(void *data, int len)
|
|
|
|
{
|
2007-01-09 18:05:17 +00:00
|
|
|
HANDLE seedf = access_random_seed(OPEN_W);
|
2000-09-27 15:21:04 +00:00
|
|
|
|
|
|
|
if (seedf != INVALID_HANDLE_VALUE) {
|
|
|
|
DWORD lenwritten;
|
|
|
|
|
|
|
|
WriteFile(seedf, data, len, &lenwritten, NULL);
|
|
|
|
CloseHandle(seedf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recursively delete a registry key and everything under it.
|
|
|
|
*/
|
2001-05-06 14:35:20 +00:00
|
|
|
static void registry_recursive_remove(HKEY key)
|
|
|
|
{
|
2000-09-27 15:21:04 +00:00
|
|
|
DWORD i;
|
2001-05-06 14:35:20 +00:00
|
|
|
char name[MAX_PATH + 1];
|
2000-09-27 15:21:04 +00:00
|
|
|
HKEY subkey;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (RegEnumKey(key, i, name, sizeof(name)) == ERROR_SUCCESS) {
|
2001-05-06 14:35:20 +00:00
|
|
|
if (RegOpenKey(key, name, &subkey) == ERROR_SUCCESS) {
|
|
|
|
registry_recursive_remove(subkey);
|
|
|
|
RegCloseKey(subkey);
|
|
|
|
}
|
|
|
|
RegDeleteKey(key, name);
|
2000-09-27 15:21:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-06 14:35:20 +00:00
|
|
|
void cleanup_all(void)
|
|
|
|
{
|
2000-09-27 15:21:04 +00:00
|
|
|
HKEY key;
|
|
|
|
int ret;
|
2001-05-06 14:35:20 +00:00
|
|
|
char name[MAX_PATH + 1];
|
2000-09-27 15:21:04 +00:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------
|
2007-01-09 18:05:17 +00:00
|
|
|
* Wipe out the random seed file, in all of its possible
|
|
|
|
* locations.
|
2000-09-27 15:21:04 +00:00
|
|
|
*/
|
2007-01-09 18:05:17 +00:00
|
|
|
access_random_seed(DEL);
|
2000-09-27 15:21:04 +00:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------
|
|
|
|
* Destroy all registry information associated with PuTTY.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the main PuTTY registry key and remove everything in it.
|
|
|
|
*/
|
2001-05-06 14:35:20 +00:00
|
|
|
if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &key) ==
|
|
|
|
ERROR_SUCCESS) {
|
|
|
|
registry_recursive_remove(key);
|
|
|
|
RegCloseKey(key);
|
2000-09-27 15:21:04 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Now open the parent key and remove the PuTTY main key. Once
|
|
|
|
* we've done that, see if the parent key has any other
|
|
|
|
* children.
|
|
|
|
*/
|
|
|
|
if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_PARENT,
|
2001-05-06 14:35:20 +00:00
|
|
|
&key) == ERROR_SUCCESS) {
|
|
|
|
RegDeleteKey(key, PUTTY_REG_PARENT_CHILD);
|
|
|
|
ret = RegEnumKey(key, 0, name, sizeof(name));
|
|
|
|
RegCloseKey(key);
|
|
|
|
/*
|
|
|
|
* If the parent key had no other children, we must delete
|
|
|
|
* it in its turn. That means opening the _grandparent_
|
|
|
|
* key.
|
|
|
|
*/
|
|
|
|
if (ret != ERROR_SUCCESS) {
|
|
|
|
if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_GPARENT,
|
|
|
|
&key) == ERROR_SUCCESS) {
|
|
|
|
RegDeleteKey(key, PUTTY_REG_GPARENT_CHILD);
|
|
|
|
RegCloseKey(key);
|
|
|
|
}
|
|
|
|
}
|
2000-09-27 15:21:04 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Now we're done.
|
|
|
|
*/
|
|
|
|
}
|