#include "putty.h"
#include "storage.h"
+#include <shlobj.h>
+#ifndef CSIDL_APPDATA
+#define CSIDL_APPDATA 0x001a
+#endif
+#ifndef CSIDL_LOCAL_APPDATA
+#define CSIDL_LOCAL_APPDATA 0x001c
+#endif
+
+static const char *const reg_jumplist_key = PUTTY_REG_POS "\\Jumplist";
+static const char *const reg_jumplist_value = "Recent sessions";
static const char *const puttystr = PUTTY_REG_POS "\\Sessions";
-static char seedpath[2 * MAX_PATH + 10] = "\0";
-
static const char hex[16] = "0123456789ABCDEF";
+static int tried_shgetfolderpath = FALSE;
+static HMODULE shell32_module = NULL;
+DECL_WINDOWS_FUNCTION(static, HRESULT, SHGetFolderPathA,
+ (HWND, int, HANDLE, DWORD, LPSTR));
+
static void mungestr(const char *in, char *out)
{
int candot = 0;
{
if (handle)
RegSetValueEx((HKEY) handle, key, 0, REG_DWORD,
- (CONST BYTE *) & value, sizeof(value));
+ (CONST BYTE *) &value, sizeof(value));
}
void close_settings_w(void *handle)
return (void *) sesskey;
}
-char *read_setting_s(void *handle, const char *key, char *buffer, int buflen)
+char *read_setting_s(void *handle, const char *key)
{
DWORD type, size;
- size = buflen;
+ char *ret;
- if (!handle ||
- RegQueryValueEx((HKEY) handle, key, 0,
- &type, buffer, &size) != ERROR_SUCCESS ||
+ if (!handle)
+ return NULL;
+
+ /* Find out the type and size of the data. */
+ if (RegQueryValueEx((HKEY) handle, key, 0,
+ &type, NULL, &size) != ERROR_SUCCESS ||
+ type != REG_SZ)
+ return NULL;
+
+ ret = snewn(size+1, char);
+ if (RegQueryValueEx((HKEY) handle, key, 0,
+ &type, ret, &size) != ERROR_SUCCESS ||
type != REG_SZ) return NULL;
- else
- return buffer;
+
+ return ret;
}
int read_setting_i(void *handle, const char *key, int defvalue)
if (!handle ||
RegQueryValueEx((HKEY) handle, key, 0, &type,
- (BYTE *) & val, &size) != ERROR_SUCCESS ||
+ (BYTE *) &val, &size) != ERROR_SUCCESS ||
size != sizeof(val) || type != REG_DWORD)
return defvalue;
else
return val;
}
-int read_setting_fontspec(void *handle, const char *name, FontSpec *result)
+FontSpec *read_setting_fontspec(void *handle, const char *name)
{
char *settingname;
- FontSpec ret;
+ char *fontname;
+ int isbold, height, charset;
+
+ fontname = read_setting_s(handle, name);
+ if (!fontname)
+ return NULL;
- 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);
+ isbold = read_setting_i(handle, settingname, -1);
sfree(settingname);
- if (ret.isbold == -1) return 0;
+ if (isbold == -1) return NULL;
+
settingname = dupcat(name, "CharSet", NULL);
- ret.charset = read_setting_i(handle, settingname, -1);
+ charset = read_setting_i(handle, settingname, -1);
sfree(settingname);
- if (ret.charset == -1) return 0;
+ if (charset == -1) return NULL;
+
settingname = dupcat(name, "Height", NULL);
- ret.height = read_setting_i(handle, settingname, INT_MIN);
+ height = read_setting_i(handle, settingname, INT_MIN);
sfree(settingname);
- if (ret.height == INT_MIN) return 0;
- *result = ret;
- return 1;
+ if (height == INT_MIN) return NULL;
+
+ return fontspec_new(fontname, isbold, height, charset);
}
-void write_setting_fontspec(void *handle, const char *name, FontSpec font)
+void write_setting_fontspec(void *handle, const char *name, FontSpec *font)
{
char *settingname;
- write_setting_s(handle, name, font.name);
+ write_setting_s(handle, name, font->name);
settingname = dupcat(name, "IsBold", NULL);
- write_setting_i(handle, settingname, font.isbold);
+ write_setting_i(handle, settingname, font->isbold);
sfree(settingname);
settingname = dupcat(name, "CharSet", NULL);
- write_setting_i(handle, settingname, font.charset);
+ write_setting_i(handle, settingname, font->charset);
sfree(settingname);
settingname = dupcat(name, "Height", NULL);
- write_setting_i(handle, settingname, font.height);
+ write_setting_i(handle, settingname, font->height);
sfree(settingname);
}
-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_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)
sfree(p);
RegCloseKey(subkey1);
+
+ remove_session_from_jumplist(sessionname);
}
struct enumsettings {
hostkey_regname(regname, hostname, port, keytype);
if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys",
- &rkey) != ERROR_SUCCESS)
- return; /* key does not exist in registry */
- RegSetValueEx(rkey, regname, 0, REG_SZ, key, strlen(key) + 1);
- RegCloseKey(rkey);
+ &rkey) == ERROR_SUCCESS) {
+ RegSetValueEx(rkey, regname, 0, REG_SZ, key, strlen(key) + 1);
+ RegCloseKey(rkey);
+ } /* else key does not exist in registry */
+
+ sfree(regname);
}
/*
- * Find the random seed file path and store it in `seedpath'.
+ * Open (or delete) the random seed file.
*/
-static void get_seedpath(void)
+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)
{
HKEY rkey;
DWORD type, size;
+ HANDLE rethandle;
+ char seedpath[2 * MAX_PATH + 10] = "\0";
- size = sizeof(seedpath);
+ /*
+ * 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).
+ */
+ /*
+ * First, try the location specified by the user in the
+ * Registry, if any.
+ */
+ size = sizeof(seedpath);
if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &rkey) ==
ERROR_SUCCESS) {
int ret = RegQueryValueEx(rkey, "RandSeedFile",
if (ret != ERROR_SUCCESS || type != REG_SZ)
seedpath[0] = '\0';
RegCloseKey(rkey);
- } else
- seedpath[0] = '\0';
- if (!seedpath[0]) {
+ 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) {
+ /* 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. */
+ shell32_module = load_system32_dll("shell32.dll");
+ GET_WINDOWS_FUNCTION(shell32_module, SHGetFolderPathA);
+ tried_shgetfolderpath = TRUE;
+ }
+ if (p_SHGetFolderPathA) {
+ if (SUCCEEDED(p_SHGetFolderPathA(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_SHGetFolderPathA(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.
+ */
+ {
int len, ret;
len =
ret =
GetEnvironmentVariable("HOMEPATH", seedpath + len,
sizeof(seedpath) - len);
- if (ret == 0) { /* probably win95; store in \WINDOWS */
- GetWindowsDirectory(seedpath, sizeof(seedpath));
- len = strlen(seedpath);
- } else
- len += ret;
- strcpy(seedpath + len, "\\PUTTY.RND");
+ if (ret != 0) {
+ strcat(seedpath, "\\PUTTY.RND");
+ if (try_random_seed(seedpath, action, &rethandle))
+ return rethandle;
+ }
}
+
+ /*
+ * 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;
}
void read_random_seed(noise_consumer_t consumer)
{
- HANDLE seedf;
-
- if (!seedpath[0])
- get_seedpath();
-
- seedf = CreateFile(seedpath, GENERIC_READ,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- NULL, OPEN_EXISTING, 0, NULL);
+ HANDLE seedf = access_random_seed(OPEN_R);
if (seedf != INVALID_HANDLE_VALUE) {
while (1) {
void write_random_seed(void *data, int len)
{
- HANDLE seedf;
-
- if (!seedpath[0])
- get_seedpath();
-
- seedf = CreateFile(seedpath, GENERIC_WRITE, 0,
- NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+ HANDLE seedf = access_random_seed(OPEN_W);
if (seedf != INVALID_HANDLE_VALUE) {
DWORD lenwritten;
}
}
+/*
+ * Internal function supporting the jump list registry code. All the
+ * functions to add, remove and read the list have substantially
+ * similar content, so this is a generalisation of all of them which
+ * transforms the list in the registry by prepending 'add' (if
+ * non-null), removing 'rem' from what's left (if non-null), and
+ * returning the resulting concatenated list of strings in 'out' (if
+ * non-null).
+ */
+static int transform_jumplist_registry
+ (const char *add, const char *rem, char **out)
+{
+ int ret;
+ HKEY pjumplist_key, psettings_tmp;
+ DWORD type;
+ int value_length;
+ char *old_value, *new_value;
+ char *piterator_old, *piterator_new, *piterator_tmp;
+
+ ret = RegCreateKeyEx(HKEY_CURRENT_USER, reg_jumplist_key, 0, NULL,
+ REG_OPTION_NON_VOLATILE, (KEY_READ | KEY_WRITE), NULL,
+ &pjumplist_key, NULL);
+ if (ret != ERROR_SUCCESS) {
+ return JUMPLISTREG_ERROR_KEYOPENCREATE_FAILURE;
+ }
+
+ /* Get current list of saved sessions in the registry. */
+ value_length = 200;
+ old_value = snewn(value_length, char);
+ ret = RegQueryValueEx(pjumplist_key, reg_jumplist_value, NULL, &type,
+ 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. */
+ if (ret == ERROR_MORE_DATA) {
+ sfree(old_value);
+ old_value = snewn(value_length, char);
+ ret = RegQueryValueEx(pjumplist_key, reg_jumplist_value, NULL, &type,
+ old_value, &value_length);
+ }
+
+ if (ret == ERROR_FILE_NOT_FOUND) {
+ /* Value doesn't exist yet. Start from an empty value. */
+ *old_value = '\0';
+ *(old_value + 1) = '\0';
+ } else if (ret != ERROR_SUCCESS) {
+ /* Some non-recoverable error occurred. */
+ sfree(old_value);
+ RegCloseKey(pjumplist_key);
+ return JUMPLISTREG_ERROR_VALUEREAD_FAILURE;
+ } else if (type != REG_MULTI_SZ) {
+ /* The value present in the registry has the wrong type: we
+ * try to delete it and start from an empty value. */
+ ret = RegDeleteValue(pjumplist_key, reg_jumplist_value);
+ if (ret != ERROR_SUCCESS) {
+ sfree(old_value);
+ RegCloseKey(pjumplist_key);
+ return JUMPLISTREG_ERROR_VALUEREAD_FAILURE;
+ }
+
+ *old_value = '\0';
+ *(old_value + 1) = '\0';
+ }
+
+ /* Check validity of registry data: REG_MULTI_SZ value must end
+ * with \0\0. */
+ piterator_tmp = old_value;
+ while (((piterator_tmp - old_value) < (value_length - 1)) &&
+ !(*piterator_tmp == '\0' && *(piterator_tmp+1) == '\0')) {
+ ++piterator_tmp;
+ }
+
+ if ((piterator_tmp - old_value) >= (value_length-1)) {
+ /* Invalid value. Start from an empty value. */
+ *old_value = '\0';
+ *(old_value + 1) = '\0';
+ }
+
+ /*
+ * Modify the list, if we're modifying.
+ */
+ if (add || rem) {
+ /* Walk through the existing list and construct the new list of
+ * saved sessions. */
+ new_value = snewn(value_length + (add ? strlen(add) + 1 : 0), char);
+ piterator_new = new_value;
+ piterator_old = old_value;
+
+ /* First add the new item to the beginning of the list. */
+ if (add) {
+ strcpy(piterator_new, add);
+ piterator_new += strlen(piterator_new) + 1;
+ }
+ /* Now add the existing list, taking care to leave out the removed
+ * item, if it was already in the existing list. */
+ while (*piterator_old != '\0') {
+ if (!rem || strcmp(piterator_old, rem) != 0) {
+ /* Check if this is a valid session, otherwise don't add. */
+ psettings_tmp = open_settings_r(piterator_old);
+ if (psettings_tmp != NULL) {
+ close_settings_r(psettings_tmp);
+ strcpy(piterator_new, piterator_old);
+ piterator_new += strlen(piterator_new) + 1;
+ }
+ }
+ piterator_old += strlen(piterator_old) + 1;
+ }
+ *piterator_new = '\0';
+ ++piterator_new;
+
+ /* Save the new list to the registry. */
+ ret = RegSetValueEx(pjumplist_key, reg_jumplist_value, 0, REG_MULTI_SZ,
+ new_value, piterator_new - new_value);
+
+ sfree(old_value);
+ old_value = new_value;
+ } else
+ ret = ERROR_SUCCESS;
+
+ /*
+ * Either return or free the result.
+ */
+ if (out)
+ *out = old_value;
+ else
+ sfree(old_value);
+
+ /* Clean up and return. */
+ RegCloseKey(pjumplist_key);
+
+ if (ret != ERROR_SUCCESS) {
+ return JUMPLISTREG_ERROR_VALUEWRITE_FAILURE;
+ } else {
+ return JUMPLISTREG_OK;
+ }
+}
+
+/* Adds a new entry to the jumplist entries in the registry. */
+int add_to_jumplist_registry(const char *item)
+{
+ return transform_jumplist_registry(item, item, NULL);
+}
+
+/* Removes an item from the jumplist entries in the registry. */
+int remove_from_jumplist_registry(const char *item)
+{
+ return transform_jumplist_registry(NULL, item, NULL);
+}
+
+/* Returns the jumplist entries from the registry. Caller must free
+ * the returned pointer. */
+char *get_jumplist_registry_entries (void)
+{
+ char *list_value;
+
+ if (transform_jumplist_registry(NULL,NULL,&list_value) != ERROR_SUCCESS) {
+ list_value = snewn(2, char);
+ *list_value = '\0';
+ *(list_value + 1) = '\0';
+ }
+ return list_value;
+}
+
/*
* Recursively delete a registry key and everything under it.
*/
char name[MAX_PATH + 1];
/* ------------------------------------------------------------
- * Wipe out the random seed file.
+ * Wipe out the random seed file, in all of its possible
+ * locations.
+ */
+ access_random_seed(DEL);
+
+ /* ------------------------------------------------------------
+ * Ask Windows to delete any jump list information associated
+ * with this installation of PuTTY.
*/
- if (!seedpath[0])
- get_seedpath();
- remove(seedpath);
+ clear_jumplist();
/* ------------------------------------------------------------
* Destroy all registry information associated with PuTTY.