2 * winstore.c: Windows-specific implementation of the interface
\r
3 * defined in storage.h.
\r
10 #include "storage.h"
\r
13 #ifndef CSIDL_APPDATA
\r
14 #define CSIDL_APPDATA 0x001a
\r
16 #ifndef CSIDL_LOCAL_APPDATA
\r
17 #define CSIDL_LOCAL_APPDATA 0x001c
\r
20 static const char *const puttystr = PUTTY_REG_POS "\\Sessions";
\r
22 static const char hex[16] = "0123456789ABCDEF";
\r
24 static int tried_shgetfolderpath = FALSE;
\r
25 static HMODULE shell32_module = NULL;
\r
26 typedef HRESULT (WINAPI *p_SHGetFolderPath_t)
\r
27 (HWND, int, HANDLE, DWORD, LPTSTR);
\r
28 static p_SHGetFolderPath_t p_SHGetFolderPath = NULL;
\r
30 static void mungestr(const char *in, char *out)
\r
35 if (*in == ' ' || *in == '\\' || *in == '*' || *in == '?' ||
\r
36 *in == '%' || *in < ' ' || *in > '~' || (*in == '.'
\r
39 *out++ = hex[((unsigned char) *in) >> 4];
\r
40 *out++ = hex[((unsigned char) *in) & 15];
\r
50 static void unmungestr(const char *in, char *out, int outlen)
\r
53 if (*in == '%' && in[1] && in[2]) {
\r
57 i -= (i > 9 ? 7 : 0);
\r
59 j -= (j > 9 ? 7 : 0);
\r
61 *out++ = (i << 4) + j;
\r
75 void *open_settings_w(const char *sessionname, char **errmsg)
\r
77 HKEY subkey1, sesskey;
\r
83 if (!sessionname || !*sessionname)
\r
84 sessionname = "Default Settings";
\r
86 p = snewn(3 * strlen(sessionname) + 1, char);
\r
87 mungestr(sessionname, p);
\r
89 ret = RegCreateKey(HKEY_CURRENT_USER, puttystr, &subkey1);
\r
90 if (ret != ERROR_SUCCESS) {
\r
92 *errmsg = dupprintf("Unable to create registry key\n"
\r
93 "HKEY_CURRENT_USER\\%s", puttystr);
\r
96 ret = RegCreateKey(subkey1, p, &sesskey);
\r
97 RegCloseKey(subkey1);
\r
98 if (ret != ERROR_SUCCESS) {
\r
99 *errmsg = dupprintf("Unable to create registry key\n"
\r
100 "HKEY_CURRENT_USER\\%s\\%s", puttystr, p);
\r
105 return (void *) sesskey;
\r
108 void write_setting_s(void *handle, const char *key, const char *value)
\r
111 RegSetValueEx((HKEY) handle, key, 0, REG_SZ, value,
\r
112 1 + strlen(value));
\r
115 void write_setting_i(void *handle, const char *key, int value)
\r
118 RegSetValueEx((HKEY) handle, key, 0, REG_DWORD,
\r
119 (CONST BYTE *) &value, sizeof(value));
\r
122 void close_settings_w(void *handle)
\r
124 RegCloseKey((HKEY) handle);
\r
127 void *open_settings_r(const char *sessionname)
\r
129 HKEY subkey1, sesskey;
\r
132 if (!sessionname || !*sessionname)
\r
133 sessionname = "Default Settings";
\r
135 p = snewn(3 * strlen(sessionname) + 1, char);
\r
136 mungestr(sessionname, p);
\r
138 if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS) {
\r
141 if (RegOpenKey(subkey1, p, &sesskey) != ERROR_SUCCESS) {
\r
144 RegCloseKey(subkey1);
\r
149 return (void *) sesskey;
\r
152 char *read_setting_s(void *handle, const char *key, char *buffer, int buflen)
\r
158 RegQueryValueEx((HKEY) handle, key, 0,
\r
159 &type, buffer, &size) != ERROR_SUCCESS ||
\r
160 type != REG_SZ) return NULL;
\r
165 int read_setting_i(void *handle, const char *key, int defvalue)
\r
167 DWORD type, val, size;
\r
168 size = sizeof(val);
\r
171 RegQueryValueEx((HKEY) handle, key, 0, &type,
\r
172 (BYTE *) &val, &size) != ERROR_SUCCESS ||
\r
173 size != sizeof(val) || type != REG_DWORD)
\r
179 int read_setting_fontspec(void *handle, const char *name, FontSpec *result)
\r
184 if (!read_setting_s(handle, name, ret.name, sizeof(ret.name)))
\r
186 settingname = dupcat(name, "IsBold", NULL);
\r
187 ret.isbold = read_setting_i(handle, settingname, -1);
\r
188 sfree(settingname);
\r
189 if (ret.isbold == -1) return 0;
\r
190 settingname = dupcat(name, "CharSet", NULL);
\r
191 ret.charset = read_setting_i(handle, settingname, -1);
\r
192 sfree(settingname);
\r
193 if (ret.charset == -1) return 0;
\r
194 settingname = dupcat(name, "Height", NULL);
\r
195 ret.height = read_setting_i(handle, settingname, INT_MIN);
\r
196 sfree(settingname);
\r
197 if (ret.height == INT_MIN) return 0;
\r
202 void write_setting_fontspec(void *handle, const char *name, FontSpec font)
\r
206 write_setting_s(handle, name, font.name);
\r
207 settingname = dupcat(name, "IsBold", NULL);
\r
208 write_setting_i(handle, settingname, font.isbold);
\r
209 sfree(settingname);
\r
210 settingname = dupcat(name, "CharSet", NULL);
\r
211 write_setting_i(handle, settingname, font.charset);
\r
212 sfree(settingname);
\r
213 settingname = dupcat(name, "Height", NULL);
\r
214 write_setting_i(handle, settingname, font.height);
\r
215 sfree(settingname);
\r
218 int read_setting_filename(void *handle, const char *name, Filename *result)
\r
220 return !!read_setting_s(handle, name, result->path, sizeof(result->path));
\r
223 void write_setting_filename(void *handle, const char *name, Filename result)
\r
225 write_setting_s(handle, name, result.path);
\r
228 void close_settings_r(void *handle)
\r
230 RegCloseKey((HKEY) handle);
\r
233 void del_settings(const char *sessionname)
\r
238 if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS)
\r
241 p = snewn(3 * strlen(sessionname) + 1, char);
\r
242 mungestr(sessionname, p);
\r
243 RegDeleteKey(subkey1, p);
\r
246 RegCloseKey(subkey1);
\r
249 struct enumsettings {
\r
254 void *enum_settings_start(void)
\r
256 struct enumsettings *ret;
\r
259 if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &key) != ERROR_SUCCESS)
\r
262 ret = snew(struct enumsettings);
\r
271 char *enum_settings_next(void *handle, char *buffer, int buflen)
\r
273 struct enumsettings *e = (struct enumsettings *) handle;
\r
275 otherbuf = snewn(3 * buflen, char);
\r
276 if (RegEnumKey(e->key, e->i++, otherbuf, 3 * buflen) == ERROR_SUCCESS) {
\r
277 unmungestr(otherbuf, buffer, buflen);
\r
286 void enum_settings_finish(void *handle)
\r
288 struct enumsettings *e = (struct enumsettings *) handle;
\r
289 RegCloseKey(e->key);
\r
293 static void hostkey_regname(char *buffer, const char *hostname,
\r
294 int port, const char *keytype)
\r
297 strcpy(buffer, keytype);
\r
298 strcat(buffer, "@");
\r
299 len = strlen(buffer);
\r
300 len += sprintf(buffer + len, "%d:", port);
\r
301 mungestr(hostname, buffer + strlen(buffer));
\r
304 int verify_host_key(const char *hostname, int port,
\r
305 const char *keytype, const char *key)
\r
307 char *otherstr, *regname;
\r
314 len = 1 + strlen(key);
\r
317 * Now read a saved key in from the registry and see what it
\r
320 otherstr = snewn(len, char);
\r
321 regname = snewn(3 * (strlen(hostname) + strlen(keytype)) + 15, char);
\r
323 hostkey_regname(regname, hostname, port, keytype);
\r
325 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys",
\r
326 &rkey) != ERROR_SUCCESS)
\r
327 return 1; /* key does not exist in registry */
\r
330 ret = RegQueryValueEx(rkey, regname, NULL, &type, otherstr, &readlen);
\r
332 if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA &&
\r
333 !strcmp(keytype, "rsa")) {
\r
335 * Key didn't exist. If the key type is RSA, we'll try
\r
336 * another trick, which is to look up the _old_ key format
\r
337 * under just the hostname and translate that.
\r
339 char *justhost = regname + 1 + strcspn(regname, ":");
\r
340 char *oldstyle = snewn(len + 10, char); /* safety margin */
\r
342 ret = RegQueryValueEx(rkey, justhost, NULL, &type,
\r
343 oldstyle, &readlen);
\r
345 if (ret == ERROR_SUCCESS && type == REG_SZ) {
\r
347 * The old format is two old-style bignums separated by
\r
348 * a slash. An old-style bignum is made of groups of
\r
349 * four hex digits: digits are ordered in sensible
\r
350 * (most to least significant) order within each group,
\r
351 * but groups are ordered in silly (least to most)
\r
352 * order within the bignum. The new format is two
\r
353 * ordinary C-format hex numbers (0xABCDEFG...XYZ, with
\r
354 * A nonzero except in the special case 0x0, which
\r
355 * doesn't appear anyway in RSA keys) separated by a
\r
356 * comma. All hex digits are lowercase in both formats.
\r
358 char *p = otherstr;
\r
359 char *q = oldstyle;
\r
362 for (i = 0; i < 2; i++) {
\r
363 int ndigits, nwords;
\r
366 ndigits = strcspn(q, "/"); /* find / or end of string */
\r
367 nwords = ndigits / 4;
\r
368 /* now trim ndigits to remove leading zeros */
\r
369 while (q[(ndigits - 1) ^ 3] == '0' && ndigits > 1)
\r
371 /* now move digits over to new string */
\r
372 for (j = 0; j < ndigits; j++)
\r
373 p[ndigits - 1 - j] = q[j ^ 3];
\r
377 q++; /* eat the slash */
\r
378 *p++ = ','; /* add a comma */
\r
380 *p = '\0'; /* terminate the string */
\r
384 * Now _if_ this key matches, we'll enter it in the new
\r
385 * format. If not, we'll assume something odd went
\r
386 * wrong, and hyper-cautiously do nothing.
\r
388 if (!strcmp(otherstr, key))
\r
389 RegSetValueEx(rkey, regname, 0, REG_SZ, otherstr,
\r
390 strlen(otherstr) + 1);
\r
396 compare = strcmp(otherstr, key);
\r
401 if (ret == ERROR_MORE_DATA ||
\r
402 (ret == ERROR_SUCCESS && type == REG_SZ && compare))
\r
403 return 2; /* key is different in registry */
\r
404 else if (ret != ERROR_SUCCESS || type != REG_SZ)
\r
405 return 1; /* key does not exist in registry */
\r
407 return 0; /* key matched OK in registry */
\r
410 void store_host_key(const char *hostname, int port,
\r
411 const char *keytype, const char *key)
\r
416 regname = snewn(3 * (strlen(hostname) + strlen(keytype)) + 15, char);
\r
418 hostkey_regname(regname, hostname, port, keytype);
\r
420 if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys",
\r
421 &rkey) == ERROR_SUCCESS) {
\r
422 RegSetValueEx(rkey, regname, 0, REG_SZ, key, strlen(key) + 1);
\r
424 } /* else key does not exist in registry */
\r
430 * Open (or delete) the random seed file.
\r
432 enum { DEL, OPEN_R, OPEN_W };
\r
433 static int try_random_seed(char const *path, int action, HANDLE *ret)
\r
435 if (action == DEL) {
\r
437 *ret = INVALID_HANDLE_VALUE;
\r
438 return FALSE; /* so we'll do the next ones too */
\r
441 *ret = CreateFile(path,
\r
442 action == OPEN_W ? GENERIC_WRITE : GENERIC_READ,
\r
443 action == OPEN_W ? 0 : (FILE_SHARE_READ |
\r
446 action == OPEN_W ? CREATE_ALWAYS : OPEN_EXISTING,
\r
447 action == OPEN_W ? FILE_ATTRIBUTE_NORMAL : 0,
\r
450 return (*ret != INVALID_HANDLE_VALUE);
\r
453 static HANDLE access_random_seed(int action)
\r
458 char seedpath[2 * MAX_PATH + 10] = "\0";
\r
461 * Iterate over a selection of possible random seed paths until
\r
462 * we find one that works.
\r
464 * We do this iteration separately for reading and writing,
\r
465 * meaning that we will automatically migrate random seed files
\r
466 * if a better location becomes available (by reading from the
\r
467 * best location in which we actually find one, and then
\r
468 * writing to the best location in which we can _create_ one).
\r
472 * First, try the location specified by the user in the
\r
473 * Registry, if any.
\r
475 size = sizeof(seedpath);
\r
476 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &rkey) ==
\r
478 int ret = RegQueryValueEx(rkey, "RandSeedFile",
\r
479 0, &type, seedpath, &size);
\r
480 if (ret != ERROR_SUCCESS || type != REG_SZ)
\r
481 seedpath[0] = '\0';
\r
484 if (*seedpath && try_random_seed(seedpath, action, &rethandle))
\r
489 * Next, try the user's local Application Data directory,
\r
490 * followed by their non-local one. This is found using the
\r
491 * SHGetFolderPath function, which won't be present on all
\r
492 * versions of Windows.
\r
494 if (!tried_shgetfolderpath) {
\r
495 /* This is likely only to bear fruit on systems with IE5+
\r
496 * installed, or WinMe/2K+. There is some faffing with
\r
497 * SHFOLDER.DLL we could do to try to find an equivalent
\r
498 * on older versions of Windows if we cared enough.
\r
499 * However, the invocation below requires IE5+ anyway,
\r
500 * so stuff that. */
\r
501 shell32_module = LoadLibrary("SHELL32.DLL");
\r
502 if (shell32_module) {
\r
503 p_SHGetFolderPath = (p_SHGetFolderPath_t)
\r
504 GetProcAddress(shell32_module, "SHGetFolderPathA");
\r
507 if (p_SHGetFolderPath) {
\r
508 if (SUCCEEDED(p_SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA,
\r
509 NULL, SHGFP_TYPE_CURRENT, seedpath))) {
\r
510 strcat(seedpath, "\\PUTTY.RND");
\r
511 if (try_random_seed(seedpath, action, &rethandle))
\r
515 if (SUCCEEDED(p_SHGetFolderPath(NULL, CSIDL_APPDATA,
\r
516 NULL, SHGFP_TYPE_CURRENT, seedpath))) {
\r
517 strcat(seedpath, "\\PUTTY.RND");
\r
518 if (try_random_seed(seedpath, action, &rethandle))
\r
524 * Failing that, try %HOMEDRIVE%%HOMEPATH% as a guess at the
\r
525 * user's home directory.
\r
531 GetEnvironmentVariable("HOMEDRIVE", seedpath,
\r
534 GetEnvironmentVariable("HOMEPATH", seedpath + len,
\r
535 sizeof(seedpath) - len);
\r
537 strcat(seedpath, "\\PUTTY.RND");
\r
538 if (try_random_seed(seedpath, action, &rethandle))
\r
544 * And finally, fall back to C:\WINDOWS.
\r
546 GetWindowsDirectory(seedpath, sizeof(seedpath));
\r
547 strcat(seedpath, "\\PUTTY.RND");
\r
548 if (try_random_seed(seedpath, action, &rethandle))
\r
552 * If even that failed, give up.
\r
554 return INVALID_HANDLE_VALUE;
\r
557 void read_random_seed(noise_consumer_t consumer)
\r
559 HANDLE seedf = access_random_seed(OPEN_R);
\r
561 if (seedf != INVALID_HANDLE_VALUE) {
\r
566 if (ReadFile(seedf, buf, sizeof(buf), &len, NULL) && len)
\r
567 consumer(buf, len);
\r
571 CloseHandle(seedf);
\r
575 void write_random_seed(void *data, int len)
\r
577 HANDLE seedf = access_random_seed(OPEN_W);
\r
579 if (seedf != INVALID_HANDLE_VALUE) {
\r
582 WriteFile(seedf, data, len, &lenwritten, NULL);
\r
583 CloseHandle(seedf);
\r
588 * Recursively delete a registry key and everything under it.
\r
590 static void registry_recursive_remove(HKEY key)
\r
593 char name[MAX_PATH + 1];
\r
597 while (RegEnumKey(key, i, name, sizeof(name)) == ERROR_SUCCESS) {
\r
598 if (RegOpenKey(key, name, &subkey) == ERROR_SUCCESS) {
\r
599 registry_recursive_remove(subkey);
\r
600 RegCloseKey(subkey);
\r
602 RegDeleteKey(key, name);
\r
606 void cleanup_all(void)
\r
610 char name[MAX_PATH + 1];
\r
612 /* ------------------------------------------------------------
\r
613 * Wipe out the random seed file, in all of its possible
\r
616 access_random_seed(DEL);
\r
618 /* ------------------------------------------------------------
\r
619 * Destroy all registry information associated with PuTTY.
\r
623 * Open the main PuTTY registry key and remove everything in it.
\r
625 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &key) ==
\r
627 registry_recursive_remove(key);
\r
631 * Now open the parent key and remove the PuTTY main key. Once
\r
632 * we've done that, see if the parent key has any other
\r
635 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_PARENT,
\r
636 &key) == ERROR_SUCCESS) {
\r
637 RegDeleteKey(key, PUTTY_REG_PARENT_CHILD);
\r
638 ret = RegEnumKey(key, 0, name, sizeof(name));
\r
641 * If the parent key had no other children, we must delete
\r
642 * it in its turn. That means opening the _grandparent_
\r
645 if (ret != ERROR_SUCCESS) {
\r
646 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_GPARENT,
\r
647 &key) == ERROR_SUCCESS) {
\r
648 RegDeleteKey(key, PUTTY_REG_GPARENT_CHILD);
\r