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