2 * winstore.c: Windows-specific implementation of the interface
3 * defined in storage.h.
12 static const char *const puttystr
= PUTTY_REG_POS
"\\Sessions";
14 static char seedpath
[2 * MAX_PATH
+ 10] = "\0";
16 static const char hex
[16] = "0123456789ABCDEF";
18 static void mungestr(const char *in
, char *out
)
23 if (*in
== ' ' || *in
== '\\' || *in
== '*' || *in
== '?' ||
24 *in
== '%' || *in
< ' ' || *in
> '~' || (*in
== '.'
27 *out
++ = hex
[((unsigned char) *in
) >> 4];
28 *out
++ = hex
[((unsigned char) *in
) & 15];
38 static void unmungestr(const char *in
, char *out
, int outlen
)
41 if (*in
== '%' && in
[1] && in
[2]) {
49 *out
++ = (i
<< 4) + j
;
63 void *open_settings_w(const char *sessionname
, char **errmsg
)
65 HKEY subkey1
, sesskey
;
71 if (!sessionname
|| !*sessionname
)
72 sessionname
= "Default Settings";
74 p
= snewn(3 * strlen(sessionname
) + 1, char);
75 mungestr(sessionname
, p
);
77 ret
= RegCreateKey(HKEY_CURRENT_USER
, puttystr
, &subkey1
);
78 if (ret
!= ERROR_SUCCESS
) {
80 *errmsg
= dupprintf("Unable to create registry key\n"
81 "HKEY_CURRENT_USER\\%s", puttystr
);
84 ret
= RegCreateKey(subkey1
, p
, &sesskey
);
86 if (ret
!= ERROR_SUCCESS
) {
87 *errmsg
= dupprintf("Unable to create registry key\n"
88 "HKEY_CURRENT_USER\\%s\\%s", puttystr
, p
);
93 return (void *) sesskey
;
96 void write_setting_s(void *handle
, const char *key
, const char *value
)
99 RegSetValueEx((HKEY
) handle
, key
, 0, REG_SZ
, value
,
103 void write_setting_i(void *handle
, const char *key
, int value
)
106 RegSetValueEx((HKEY
) handle
, key
, 0, REG_DWORD
,
107 (CONST BYTE
*) &value
, sizeof(value
));
110 void close_settings_w(void *handle
)
112 RegCloseKey((HKEY
) handle
);
115 void *open_settings_r(const char *sessionname
)
117 HKEY subkey1
, sesskey
;
120 if (!sessionname
|| !*sessionname
)
121 sessionname
= "Default Settings";
123 p
= snewn(3 * strlen(sessionname
) + 1, char);
124 mungestr(sessionname
, p
);
126 if (RegOpenKey(HKEY_CURRENT_USER
, puttystr
, &subkey1
) != ERROR_SUCCESS
) {
129 if (RegOpenKey(subkey1
, p
, &sesskey
) != ERROR_SUCCESS
) {
132 RegCloseKey(subkey1
);
137 return (void *) sesskey
;
140 char *read_setting_s(void *handle
, const char *key
, char *buffer
, int buflen
)
146 RegQueryValueEx((HKEY
) handle
, key
, 0,
147 &type
, buffer
, &size
) != ERROR_SUCCESS
||
148 type
!= REG_SZ
) return NULL
;
153 int read_setting_i(void *handle
, const char *key
, int defvalue
)
155 DWORD type
, val
, size
;
159 RegQueryValueEx((HKEY
) handle
, key
, 0, &type
,
160 (BYTE
*) &val
, &size
) != ERROR_SUCCESS
||
161 size
!= sizeof(val
) || type
!= REG_DWORD
)
167 int read_setting_fontspec(void *handle
, const char *name
, FontSpec
*result
)
172 if (!read_setting_s(handle
, name
, ret
.name
, sizeof(ret
.name
)))
174 settingname
= dupcat(name
, "IsBold", NULL
);
175 ret
.isbold
= read_setting_i(handle
, settingname
, -1);
177 if (ret
.isbold
== -1) return 0;
178 settingname
= dupcat(name
, "CharSet", NULL
);
179 ret
.charset
= read_setting_i(handle
, settingname
, -1);
181 if (ret
.charset
== -1) return 0;
182 settingname
= dupcat(name
, "Height", NULL
);
183 ret
.height
= read_setting_i(handle
, settingname
, INT_MIN
);
185 if (ret
.height
== INT_MIN
) return 0;
190 void write_setting_fontspec(void *handle
, const char *name
, FontSpec font
)
194 write_setting_s(handle
, name
, font
.name
);
195 settingname
= dupcat(name
, "IsBold", NULL
);
196 write_setting_i(handle
, settingname
, font
.isbold
);
198 settingname
= dupcat(name
, "CharSet", NULL
);
199 write_setting_i(handle
, settingname
, font
.charset
);
201 settingname
= dupcat(name
, "Height", NULL
);
202 write_setting_i(handle
, settingname
, font
.height
);
206 int read_setting_filename(void *handle
, const char *name
, Filename
*result
)
208 return !!read_setting_s(handle
, name
, result
->path
, sizeof(result
->path
));
211 void write_setting_filename(void *handle
, const char *name
, Filename result
)
213 write_setting_s(handle
, name
, result
.path
);
216 void close_settings_r(void *handle
)
218 RegCloseKey((HKEY
) handle
);
221 void del_settings(const char *sessionname
)
226 if (RegOpenKey(HKEY_CURRENT_USER
, puttystr
, &subkey1
) != ERROR_SUCCESS
)
229 p
= snewn(3 * strlen(sessionname
) + 1, char);
230 mungestr(sessionname
, p
);
231 RegDeleteKey(subkey1
, p
);
234 RegCloseKey(subkey1
);
237 struct enumsettings
{
242 void *enum_settings_start(void)
244 struct enumsettings
*ret
;
247 if (RegOpenKey(HKEY_CURRENT_USER
, puttystr
, &key
) != ERROR_SUCCESS
)
250 ret
= snew(struct enumsettings
);
259 char *enum_settings_next(void *handle
, char *buffer
, int buflen
)
261 struct enumsettings
*e
= (struct enumsettings
*) handle
;
263 otherbuf
= snewn(3 * buflen
, char);
264 if (RegEnumKey(e
->key
, e
->i
++, otherbuf
, 3 * buflen
) == ERROR_SUCCESS
) {
265 unmungestr(otherbuf
, buffer
, buflen
);
274 void enum_settings_finish(void *handle
)
276 struct enumsettings
*e
= (struct enumsettings
*) handle
;
281 static void hostkey_regname(char *buffer
, const char *hostname
,
282 int port
, const char *keytype
)
285 strcpy(buffer
, keytype
);
287 len
= strlen(buffer
);
288 len
+= sprintf(buffer
+ len
, "%d:", port
);
289 mungestr(hostname
, buffer
+ strlen(buffer
));
292 int verify_host_key(const char *hostname
, int port
,
293 const char *keytype
, const char *key
)
295 char *otherstr
, *regname
;
302 len
= 1 + strlen(key
);
305 * Now read a saved key in from the registry and see what it
308 otherstr
= snewn(len
, char);
309 regname
= snewn(3 * (strlen(hostname
) + strlen(keytype
)) + 15, char);
311 hostkey_regname(regname
, hostname
, port
, keytype
);
313 if (RegOpenKey(HKEY_CURRENT_USER
, PUTTY_REG_POS
"\\SshHostKeys",
314 &rkey
) != ERROR_SUCCESS
)
315 return 1; /* key does not exist in registry */
318 ret
= RegQueryValueEx(rkey
, regname
, NULL
, &type
, otherstr
, &readlen
);
320 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_MORE_DATA
&&
321 !strcmp(keytype
, "rsa")) {
323 * Key didn't exist. If the key type is RSA, we'll try
324 * another trick, which is to look up the _old_ key format
325 * under just the hostname and translate that.
327 char *justhost
= regname
+ 1 + strcspn(regname
, ":");
328 char *oldstyle
= snewn(len
+ 10, char); /* safety margin */
330 ret
= RegQueryValueEx(rkey
, justhost
, NULL
, &type
,
333 if (ret
== ERROR_SUCCESS
&& type
== REG_SZ
) {
335 * The old format is two old-style bignums separated by
336 * a slash. An old-style bignum is made of groups of
337 * four hex digits: digits are ordered in sensible
338 * (most to least significant) order within each group,
339 * but groups are ordered in silly (least to most)
340 * order within the bignum. The new format is two
341 * ordinary C-format hex numbers (0xABCDEFG...XYZ, with
342 * A nonzero except in the special case 0x0, which
343 * doesn't appear anyway in RSA keys) separated by a
344 * comma. All hex digits are lowercase in both formats.
350 for (i
= 0; i
< 2; i
++) {
354 ndigits
= strcspn(q
, "/"); /* find / or end of string */
355 nwords
= ndigits
/ 4;
356 /* now trim ndigits to remove leading zeros */
357 while (q
[(ndigits
- 1) ^ 3] == '0' && ndigits
> 1)
359 /* now move digits over to new string */
360 for (j
= 0; j
< ndigits
; j
++)
361 p
[ndigits
- 1 - j
] = q
[j
^ 3];
365 q
++; /* eat the slash */
366 *p
++ = ','; /* add a comma */
368 *p
= '\0'; /* terminate the string */
372 * Now _if_ this key matches, we'll enter it in the new
373 * format. If not, we'll assume something odd went
374 * wrong, and hyper-cautiously do nothing.
376 if (!strcmp(otherstr
, key
))
377 RegSetValueEx(rkey
, regname
, 0, REG_SZ
, otherstr
,
378 strlen(otherstr
) + 1);
384 compare
= strcmp(otherstr
, key
);
389 if (ret
== ERROR_MORE_DATA
||
390 (ret
== ERROR_SUCCESS
&& type
== REG_SZ
&& compare
))
391 return 2; /* key is different in registry */
392 else if (ret
!= ERROR_SUCCESS
|| type
!= REG_SZ
)
393 return 1; /* key does not exist in registry */
395 return 0; /* key matched OK in registry */
398 void store_host_key(const char *hostname
, int port
,
399 const char *keytype
, const char *key
)
404 regname
= snewn(3 * (strlen(hostname
) + strlen(keytype
)) + 15, char);
406 hostkey_regname(regname
, hostname
, port
, keytype
);
408 if (RegCreateKey(HKEY_CURRENT_USER
, PUTTY_REG_POS
"\\SshHostKeys",
409 &rkey
) == ERROR_SUCCESS
) {
410 RegSetValueEx(rkey
, regname
, 0, REG_SZ
, key
, strlen(key
) + 1);
412 } /* else key does not exist in registry */
418 * Find the random seed file path and store it in `seedpath'.
420 static void get_seedpath(void)
425 size
= sizeof(seedpath
);
427 if (RegOpenKey(HKEY_CURRENT_USER
, PUTTY_REG_POS
, &rkey
) ==
429 int ret
= RegQueryValueEx(rkey
, "RandSeedFile",
430 0, &type
, seedpath
, &size
);
431 if (ret
!= ERROR_SUCCESS
|| type
!= REG_SZ
)
441 GetEnvironmentVariable("HOMEDRIVE", seedpath
,
444 GetEnvironmentVariable("HOMEPATH", seedpath
+ len
,
445 sizeof(seedpath
) - len
);
446 if (ret
== 0) { /* probably win95; store in \WINDOWS */
447 GetWindowsDirectory(seedpath
, sizeof(seedpath
));
448 len
= strlen(seedpath
);
451 strcpy(seedpath
+ len
, "\\PUTTY.RND");
455 void read_random_seed(noise_consumer_t consumer
)
462 seedf
= CreateFile(seedpath
, GENERIC_READ
,
463 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
464 NULL
, OPEN_EXISTING
, 0, NULL
);
466 if (seedf
!= INVALID_HANDLE_VALUE
) {
471 if (ReadFile(seedf
, buf
, sizeof(buf
), &len
, NULL
) && len
)
480 void write_random_seed(void *data
, int len
)
487 seedf
= CreateFile(seedpath
, GENERIC_WRITE
, 0,
488 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
490 if (seedf
!= INVALID_HANDLE_VALUE
) {
493 WriteFile(seedf
, data
, len
, &lenwritten
, NULL
);
499 * Recursively delete a registry key and everything under it.
501 static void registry_recursive_remove(HKEY key
)
504 char name
[MAX_PATH
+ 1];
508 while (RegEnumKey(key
, i
, name
, sizeof(name
)) == ERROR_SUCCESS
) {
509 if (RegOpenKey(key
, name
, &subkey
) == ERROR_SUCCESS
) {
510 registry_recursive_remove(subkey
);
513 RegDeleteKey(key
, name
);
517 void cleanup_all(void)
521 char name
[MAX_PATH
+ 1];
523 /* ------------------------------------------------------------
524 * Wipe out the random seed file.
530 /* ------------------------------------------------------------
531 * Destroy all registry information associated with PuTTY.
535 * Open the main PuTTY registry key and remove everything in it.
537 if (RegOpenKey(HKEY_CURRENT_USER
, PUTTY_REG_POS
, &key
) ==
539 registry_recursive_remove(key
);
543 * Now open the parent key and remove the PuTTY main key. Once
544 * we've done that, see if the parent key has any other
547 if (RegOpenKey(HKEY_CURRENT_USER
, PUTTY_REG_PARENT
,
548 &key
) == ERROR_SUCCESS
) {
549 RegDeleteKey(key
, PUTTY_REG_PARENT_CHILD
);
550 ret
= RegEnumKey(key
, 0, name
, sizeof(name
));
553 * If the parent key had no other children, we must delete
554 * it in its turn. That means opening the _grandparent_
557 if (ret
!= ERROR_SUCCESS
) {
558 if (RegOpenKey(HKEY_CURRENT_USER
, PUTTY_REG_GPARENT
,
559 &key
) == ERROR_SUCCESS
) {
560 RegDeleteKey(key
, PUTTY_REG_GPARENT_CHILD
);