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
);
87 if (ret
!= ERROR_SUCCESS
) {
88 *errmsg
= dupprintf("Unable to create registry key\n"
89 "HKEY_CURRENT_USER\\%s\\%s", puttystr
, p
);
92 return (void *) sesskey
;
95 void write_setting_s(void *handle
, const char *key
, const char *value
)
98 RegSetValueEx((HKEY
) handle
, key
, 0, REG_SZ
, value
,
102 void write_setting_i(void *handle
, const char *key
, int value
)
105 RegSetValueEx((HKEY
) handle
, key
, 0, REG_DWORD
,
106 (CONST BYTE
*) & value
, sizeof(value
));
109 void close_settings_w(void *handle
)
111 RegCloseKey((HKEY
) handle
);
114 void *open_settings_r(const char *sessionname
)
116 HKEY subkey1
, sesskey
;
119 if (!sessionname
|| !*sessionname
)
120 sessionname
= "Default Settings";
122 p
= snewn(3 * strlen(sessionname
) + 1, char);
123 mungestr(sessionname
, p
);
125 if (RegOpenKey(HKEY_CURRENT_USER
, puttystr
, &subkey1
) != ERROR_SUCCESS
) {
128 if (RegOpenKey(subkey1
, p
, &sesskey
) != ERROR_SUCCESS
) {
131 RegCloseKey(subkey1
);
136 return (void *) sesskey
;
139 char *read_setting_s(void *handle
, const char *key
, char *buffer
, int buflen
)
145 RegQueryValueEx((HKEY
) handle
, key
, 0,
146 &type
, buffer
, &size
) != ERROR_SUCCESS
||
147 type
!= REG_SZ
) return NULL
;
152 int read_setting_i(void *handle
, const char *key
, int defvalue
)
154 DWORD type
, val
, size
;
158 RegQueryValueEx((HKEY
) handle
, key
, 0, &type
,
159 (BYTE
*) & val
, &size
) != ERROR_SUCCESS
||
160 size
!= sizeof(val
) || type
!= REG_DWORD
)
166 int read_setting_fontspec(void *handle
, const char *name
, FontSpec
*result
)
171 if (!read_setting_s(handle
, name
, ret
.name
, sizeof(ret
.name
)))
173 settingname
= dupcat(name
, "IsBold", NULL
);
174 ret
.isbold
= read_setting_i(handle
, settingname
, -1);
176 if (ret
.isbold
== -1) return 0;
177 settingname
= dupcat(name
, "CharSet", NULL
);
178 ret
.charset
= read_setting_i(handle
, settingname
, -1);
180 if (ret
.charset
== -1) return 0;
181 settingname
= dupcat(name
, "Height", NULL
);
182 ret
.height
= read_setting_i(handle
, settingname
, INT_MIN
);
184 if (ret
.height
== INT_MIN
) return 0;
189 void write_setting_fontspec(void *handle
, const char *name
, FontSpec font
)
193 write_setting_s(handle
, name
, font
.name
);
194 settingname
= dupcat(name
, "IsBold", NULL
);
195 write_setting_i(handle
, settingname
, font
.isbold
);
197 settingname
= dupcat(name
, "CharSet", NULL
);
198 write_setting_i(handle
, settingname
, font
.charset
);
200 settingname
= dupcat(name
, "Height", NULL
);
201 write_setting_i(handle
, settingname
, font
.height
);
205 int read_setting_filename(void *handle
, const char *name
, Filename
*result
)
207 return !!read_setting_s(handle
, name
, result
->path
, sizeof(result
->path
));
210 void write_setting_filename(void *handle
, const char *name
, Filename result
)
212 write_setting_s(handle
, name
, result
.path
);
215 void close_settings_r(void *handle
)
217 RegCloseKey((HKEY
) handle
);
220 void del_settings(const char *sessionname
)
225 if (RegOpenKey(HKEY_CURRENT_USER
, puttystr
, &subkey1
) != ERROR_SUCCESS
)
228 p
= snewn(3 * strlen(sessionname
) + 1, char);
229 mungestr(sessionname
, p
);
230 RegDeleteKey(subkey1
, p
);
233 RegCloseKey(subkey1
);
236 struct enumsettings
{
241 void *enum_settings_start(void)
243 struct enumsettings
*ret
;
246 if (RegOpenKey(HKEY_CURRENT_USER
, puttystr
, &key
) != ERROR_SUCCESS
)
249 ret
= snew(struct enumsettings
);
258 char *enum_settings_next(void *handle
, char *buffer
, int buflen
)
260 struct enumsettings
*e
= (struct enumsettings
*) handle
;
262 otherbuf
= snewn(3 * buflen
, char);
263 if (RegEnumKey(e
->key
, e
->i
++, otherbuf
, 3 * buflen
) == ERROR_SUCCESS
) {
264 unmungestr(otherbuf
, buffer
, buflen
);
273 void enum_settings_finish(void *handle
)
275 struct enumsettings
*e
= (struct enumsettings
*) handle
;
280 static void hostkey_regname(char *buffer
, const char *hostname
,
281 int port
, const char *keytype
)
284 strcpy(buffer
, keytype
);
286 len
= strlen(buffer
);
287 len
+= sprintf(buffer
+ len
, "%d:", port
);
288 mungestr(hostname
, buffer
+ strlen(buffer
));
291 int verify_host_key(const char *hostname
, int port
,
292 const char *keytype
, const char *key
)
294 char *otherstr
, *regname
;
301 len
= 1 + strlen(key
);
304 * Now read a saved key in from the registry and see what it
307 otherstr
= snewn(len
, char);
308 regname
= snewn(3 * (strlen(hostname
) + strlen(keytype
)) + 15, char);
310 hostkey_regname(regname
, hostname
, port
, keytype
);
312 if (RegOpenKey(HKEY_CURRENT_USER
, PUTTY_REG_POS
"\\SshHostKeys",
313 &rkey
) != ERROR_SUCCESS
)
314 return 1; /* key does not exist in registry */
317 ret
= RegQueryValueEx(rkey
, regname
, NULL
, &type
, otherstr
, &readlen
);
319 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_MORE_DATA
&&
320 !strcmp(keytype
, "rsa")) {
322 * Key didn't exist. If the key type is RSA, we'll try
323 * another trick, which is to look up the _old_ key format
324 * under just the hostname and translate that.
326 char *justhost
= regname
+ 1 + strcspn(regname
, ":");
327 char *oldstyle
= snewn(len
+ 10, char); /* safety margin */
329 ret
= RegQueryValueEx(rkey
, justhost
, NULL
, &type
,
332 if (ret
== ERROR_SUCCESS
&& type
== REG_SZ
) {
334 * The old format is two old-style bignums separated by
335 * a slash. An old-style bignum is made of groups of
336 * four hex digits: digits are ordered in sensible
337 * (most to least significant) order within each group,
338 * but groups are ordered in silly (least to most)
339 * order within the bignum. The new format is two
340 * ordinary C-format hex numbers (0xABCDEFG...XYZ, with
341 * A nonzero except in the special case 0x0, which
342 * doesn't appear anyway in RSA keys) separated by a
343 * comma. All hex digits are lowercase in both formats.
349 for (i
= 0; i
< 2; i
++) {
353 ndigits
= strcspn(q
, "/"); /* find / or end of string */
354 nwords
= ndigits
/ 4;
355 /* now trim ndigits to remove leading zeros */
356 while (q
[(ndigits
- 1) ^ 3] == '0' && ndigits
> 1)
358 /* now move digits over to new string */
359 for (j
= 0; j
< ndigits
; j
++)
360 p
[ndigits
- 1 - j
] = q
[j
^ 3];
364 q
++; /* eat the slash */
365 *p
++ = ','; /* add a comma */
367 *p
= '\0'; /* terminate the string */
371 * Now _if_ this key matches, we'll enter it in the new
372 * format. If not, we'll assume something odd went
373 * wrong, and hyper-cautiously do nothing.
375 if (!strcmp(otherstr
, key
))
376 RegSetValueEx(rkey
, regname
, 0, REG_SZ
, otherstr
,
377 strlen(otherstr
) + 1);
383 compare
= strcmp(otherstr
, key
);
388 if (ret
== ERROR_MORE_DATA
||
389 (ret
== ERROR_SUCCESS
&& type
== REG_SZ
&& compare
))
390 return 2; /* key is different in registry */
391 else if (ret
!= ERROR_SUCCESS
|| type
!= REG_SZ
)
392 return 1; /* key does not exist in registry */
394 return 0; /* key matched OK in registry */
397 void store_host_key(const char *hostname
, int port
,
398 const char *keytype
, const char *key
)
403 regname
= snewn(3 * (strlen(hostname
) + strlen(keytype
)) + 15, char);
405 hostkey_regname(regname
, hostname
, port
, keytype
);
407 if (RegCreateKey(HKEY_CURRENT_USER
, PUTTY_REG_POS
"\\SshHostKeys",
408 &rkey
) != ERROR_SUCCESS
)
409 return; /* key does not exist in registry */
410 RegSetValueEx(rkey
, regname
, 0, REG_SZ
, key
, strlen(key
) + 1);
415 * Find the random seed file path and store it in `seedpath'.
417 static void get_seedpath(void)
422 size
= sizeof(seedpath
);
424 if (RegOpenKey(HKEY_CURRENT_USER
, PUTTY_REG_POS
, &rkey
) ==
426 int ret
= RegQueryValueEx(rkey
, "RandSeedFile",
427 0, &type
, seedpath
, &size
);
428 if (ret
!= ERROR_SUCCESS
|| type
!= REG_SZ
)
438 GetEnvironmentVariable("HOMEDRIVE", seedpath
,
441 GetEnvironmentVariable("HOMEPATH", seedpath
+ len
,
442 sizeof(seedpath
) - len
);
443 if (ret
== 0) { /* probably win95; store in \WINDOWS */
444 GetWindowsDirectory(seedpath
, sizeof(seedpath
));
445 len
= strlen(seedpath
);
448 strcpy(seedpath
+ len
, "\\PUTTY.RND");
452 void read_random_seed(noise_consumer_t consumer
)
459 seedf
= CreateFile(seedpath
, GENERIC_READ
,
460 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
461 NULL
, OPEN_EXISTING
, 0, NULL
);
463 if (seedf
!= INVALID_HANDLE_VALUE
) {
468 if (ReadFile(seedf
, buf
, sizeof(buf
), &len
, NULL
) && len
)
477 void write_random_seed(void *data
, int len
)
484 seedf
= CreateFile(seedpath
, GENERIC_WRITE
, 0,
485 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
487 if (seedf
!= INVALID_HANDLE_VALUE
) {
490 WriteFile(seedf
, data
, len
, &lenwritten
, NULL
);
496 * Recursively delete a registry key and everything under it.
498 static void registry_recursive_remove(HKEY key
)
501 char name
[MAX_PATH
+ 1];
505 while (RegEnumKey(key
, i
, name
, sizeof(name
)) == ERROR_SUCCESS
) {
506 if (RegOpenKey(key
, name
, &subkey
) == ERROR_SUCCESS
) {
507 registry_recursive_remove(subkey
);
510 RegDeleteKey(key
, name
);
514 void cleanup_all(void)
518 char name
[MAX_PATH
+ 1];
520 /* ------------------------------------------------------------
521 * Wipe out the random seed file.
527 /* ------------------------------------------------------------
528 * Destroy all registry information associated with PuTTY.
532 * Open the main PuTTY registry key and remove everything in it.
534 if (RegOpenKey(HKEY_CURRENT_USER
, PUTTY_REG_POS
, &key
) ==
536 registry_recursive_remove(key
);
540 * Now open the parent key and remove the PuTTY main key. Once
541 * we've done that, see if the parent key has any other
544 if (RegOpenKey(HKEY_CURRENT_USER
, PUTTY_REG_PARENT
,
545 &key
) == ERROR_SUCCESS
) {
546 RegDeleteKey(key
, PUTTY_REG_PARENT_CHILD
);
547 ret
= RegEnumKey(key
, 0, name
, sizeof(name
));
550 * If the parent key had no other children, we must delete
551 * it in its turn. That means opening the _grandparent_
554 if (ret
!= ERROR_SUCCESS
) {
555 if (RegOpenKey(HKEY_CURRENT_USER
, PUTTY_REG_GPARENT
,
556 &key
) == ERROR_SUCCESS
) {
557 RegDeleteKey(key
, PUTTY_REG_GPARENT_CHILD
);