2 * winstore.c: Windows-specific implementation of the interface
3 * defined in storage.h.
11 static const char *const puttystr
= PUTTY_REG_POS
"\\Sessions";
13 static char seedpath
[2*MAX_PATH
+10] = "\0";
15 static char hex
[16] = "0123456789ABCDEF";
17 static void mungestr(char *in
, char *out
) {
21 if (*in
== ' ' || *in
== '\\' || *in
== '*' || *in
== '?' ||
22 *in
== '%' || *in
< ' ' || *in
> '~' || (*in
== '.' && !candot
)) {
24 *out
++ = hex
[((unsigned char)*in
) >> 4];
25 *out
++ = hex
[((unsigned char)*in
) & 15];
35 static void unmungestr(char *in
, char *out
, int outlen
) {
37 if (*in
== '%' && in
[1] && in
[2]) {
40 i
= in
[1] - '0'; i
-= (i
> 9 ?
7 : 0);
41 j
= in
[2] - '0'; j
-= (j
> 9 ?
7 : 0);
44 if (!--outlen
) return;
48 if (!--outlen
) return;
55 void *open_settings_w(char *sessionname
) {
56 HKEY subkey1
, sesskey
;
60 p
= malloc(3*strlen(sessionname
)+1);
61 mungestr(sessionname
, p
);
63 ret
= RegCreateKey(HKEY_CURRENT_USER
, puttystr
, &subkey1
);
64 if (ret
!= ERROR_SUCCESS
) {
68 ret
= RegCreateKey(subkey1
, p
, &sesskey
);
71 if (ret
!= ERROR_SUCCESS
)
73 return (void *)sesskey
;
76 void write_setting_s(void *handle
, char *key
, char *value
) {
78 RegSetValueEx((HKEY
)handle
, key
, 0, REG_SZ
, value
, 1+strlen(value
));
81 void write_setting_i(void *handle
, char *key
, int value
) {
83 RegSetValueEx((HKEY
)handle
, key
, 0, REG_DWORD
,
84 (CONST BYTE
*)&value
, sizeof(value
));
87 void close_settings_w(void *handle
) {
88 RegCloseKey((HKEY
)handle
);
91 void *open_settings_r(char *sessionname
) {
92 HKEY subkey1
, sesskey
;
95 p
= malloc(3*strlen(sessionname
)+1);
96 mungestr(sessionname
, p
);
98 if (RegOpenKey(HKEY_CURRENT_USER
, puttystr
, &subkey1
) != ERROR_SUCCESS
) {
101 if (RegOpenKey(subkey1
, p
, &sesskey
) != ERROR_SUCCESS
) {
104 RegCloseKey(subkey1
);
109 return (void *)sesskey
;
112 char *read_setting_s(void *handle
, char *key
, char *buffer
, int buflen
) {
117 RegQueryValueEx((HKEY
)handle
, key
, 0,
118 &type
, buffer
, &size
) != ERROR_SUCCESS
||
125 int read_setting_i(void *handle
, char *key
, int defvalue
) {
126 DWORD type
, val
, size
;
130 RegQueryValueEx((HKEY
)handle
, key
, 0, &type
,
131 (BYTE
*)&val
, &size
) != ERROR_SUCCESS
||
132 size
!= sizeof(val
) || type
!= REG_DWORD
)
138 void close_settings_r(void *handle
) {
139 RegCloseKey((HKEY
)handle
);
142 void del_settings (char *sessionname
) {
146 if (RegOpenKey(HKEY_CURRENT_USER
, puttystr
, &subkey1
) != ERROR_SUCCESS
)
149 p
= malloc(3*strlen(sessionname
)+1);
150 mungestr(sessionname
, p
);
151 RegDeleteKey(subkey1
, p
);
154 RegCloseKey(subkey1
);
157 static void hostkey_regname(char *buffer
, char *hostname
, char *keytype
) {
158 strcpy(buffer
, keytype
);
160 mungestr(hostname
, buffer
+ strlen(buffer
));
163 struct enumsettings
{
168 void *enum_settings_start(void) {
169 struct enumsettings
*ret
;
172 if (RegCreateKey(HKEY_CURRENT_USER
, puttystr
, &key
) != ERROR_SUCCESS
)
175 ret
= malloc(sizeof(*ret
));
184 char *enum_settings_next(void *handle
, char *buffer
, int buflen
) {
185 struct enumsettings
*e
= (struct enumsettings
*)handle
;
187 otherbuf
= malloc(3*buflen
);
188 if (otherbuf
&& RegEnumKey(e
->key
, e
->i
++, otherbuf
,
189 3*buflen
) == ERROR_SUCCESS
) {
190 unmungestr(otherbuf
, buffer
, buflen
);
198 void enum_settings_finish(void *handle
) {
199 struct enumsettings
*e
= (struct enumsettings
*)handle
;
204 int verify_host_key(char *hostname
, char *keytype
, char *key
) {
205 char *otherstr
, *regname
;
212 len
= 1 + strlen(key
);
215 * Now read a saved key in from the registry and see what it
218 otherstr
= smalloc(len
);
219 regname
= smalloc(3*(strlen(hostname
)+strlen(keytype
))+5);
220 if (!otherstr
|| !regname
)
221 fatalbox("Out of memory");
223 hostkey_regname(regname
, hostname
, keytype
);
225 if (RegCreateKey(HKEY_CURRENT_USER
, PUTTY_REG_POS
"\\SshHostKeys",
226 &rkey
) != ERROR_SUCCESS
)
227 return 1; /* key does not exist in registry */
230 ret
= RegQueryValueEx(rkey
, regname
, NULL
, &type
, otherstr
, &readlen
);
232 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_MORE_DATA
&&
233 !strcmp(keytype
, "rsa")) {
235 * Key didn't exist. If the key type is RSA, we'll try
236 * another trick, which is to look up the _old_ key format
237 * under just the hostname and translate that.
239 char *justhost
= regname
+ 1 + strlen(keytype
);
240 char *oldstyle
= smalloc(len
+ 10); /* safety margin */
242 ret
= RegQueryValueEx(rkey
, justhost
, NULL
, &type
,
245 if (ret
== ERROR_SUCCESS
&& type
== REG_SZ
) {
247 * The old format is two old-style bignums separated by
248 * a slash. An old-style bignum is made of groups of
249 * four hex digits: digits are ordered in sensible
250 * (most to least significant) order within each group,
251 * but groups are ordered in silly (least to most)
252 * order within the bignum. The new format is two
253 * ordinary C-format hex numbers (0xABCDEFG...XYZ, with
254 * A nonzero except in the special case 0x0, which
255 * doesn't appear anyway in RSA keys) separated by a
256 * comma. All hex digits are lowercase in both formats.
262 for (i
= 0; i
< 2; i
++) {
264 *p
++ = '0'; *p
++ = 'x';
265 ndigits
= strcspn(q
, "/"); /* find / or end of string */
266 nwords
= ndigits
/ 4;
267 /* now trim ndigits to remove leading zeros */
268 while (q
[ (ndigits
-1) ^ 3 ] == '0' && ndigits
> 1)
270 /* now move digits over to new string */
271 for (j
= 0; j
< ndigits
; j
++)
272 p
[ndigits
-1-j
] = q
[j
^3];
276 q
++; /* eat the slash */
277 *p
++ = ','; /* add a comma */
279 *p
= '\0'; /* terminate the string */
283 * Now _if_ this key matches, we'll enter it in the new
284 * format. If not, we'll assume something odd went
285 * wrong, and hyper-cautiously do nothing.
287 if (!strcmp(otherstr
, key
))
288 RegSetValueEx(rkey
, regname
, 0, REG_SZ
, otherstr
,
295 compare
= strcmp(otherstr
, key
);
300 if (ret
== ERROR_MORE_DATA
||
301 (ret
== ERROR_SUCCESS
&& type
== REG_SZ
&& compare
))
302 return 2; /* key is different in registry */
303 else if (ret
!= ERROR_SUCCESS
|| type
!= REG_SZ
)
304 return 1; /* key does not exist in registry */
306 return 0; /* key matched OK in registry */
309 void store_host_key(char *hostname
, char *keytype
, char *key
) {
313 regname
= smalloc(3*(strlen(hostname
)+strlen(keytype
))+5);
315 fatalbox("Out of memory");
317 hostkey_regname(regname
, hostname
, keytype
);
319 if (RegCreateKey(HKEY_CURRENT_USER
, PUTTY_REG_POS
"\\SshHostKeys",
320 &rkey
) != ERROR_SUCCESS
)
321 return; /* key does not exist in registry */
322 RegSetValueEx(rkey
, regname
, 0, REG_SZ
, key
,
328 * Find the random seed file path and store it in `seedpath'.
330 static void get_seedpath(void) {
334 size
= sizeof(seedpath
);
336 if (RegOpenKey(HKEY_CURRENT_USER
, PUTTY_REG_POS
, &rkey
)==ERROR_SUCCESS
) {
337 int ret
= RegQueryValueEx(rkey
, "RandSeedFile",
338 0, &type
, seedpath
, &size
);
339 if (ret
!= ERROR_SUCCESS
|| type
!= REG_SZ
)
348 len
= GetEnvironmentVariable("HOMEDRIVE", seedpath
, sizeof(seedpath
));
349 ret
= GetEnvironmentVariable("HOMEPATH", seedpath
+len
,
350 sizeof(seedpath
)-len
);
351 if (ret
== 0) { /* probably win95; store in \WINDOWS */
352 GetWindowsDirectory(seedpath
, sizeof(seedpath
));
353 len
= strlen(seedpath
);
356 strcpy(seedpath
+len
, "\\PUTTY.RND");
360 void read_random_seed(noise_consumer_t consumer
) {
366 seedf
= CreateFile(seedpath
, GENERIC_READ
,
367 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
368 NULL
, OPEN_EXISTING
, 0, NULL
);
370 if (seedf
!= INVALID_HANDLE_VALUE
) {
375 if (ReadFile(seedf
, buf
, sizeof(buf
), &len
, NULL
) && len
)
384 void write_random_seed(void *data
, int len
) {
390 seedf
= CreateFile(seedpath
, GENERIC_WRITE
, 0,
391 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
393 if (seedf
!= INVALID_HANDLE_VALUE
) {
396 WriteFile(seedf
, data
, len
, &lenwritten
, NULL
);
402 * Recursively delete a registry key and everything under it.
404 static void registry_recursive_remove(HKEY key
) {
406 char name
[MAX_PATH
+1];
410 while (RegEnumKey(key
, i
, name
, sizeof(name
)) == ERROR_SUCCESS
) {
411 if (RegOpenKey(key
, name
, &subkey
) == ERROR_SUCCESS
) {
412 registry_recursive_remove(subkey
);
415 RegDeleteKey(key
, name
);
419 void cleanup_all(void) {
422 char name
[MAX_PATH
+1];
424 /* ------------------------------------------------------------
425 * Wipe out the random seed file.
431 /* ------------------------------------------------------------
432 * Destroy all registry information associated with PuTTY.
436 * Open the main PuTTY registry key and remove everything in it.
438 if (RegOpenKey(HKEY_CURRENT_USER
, PUTTY_REG_POS
, &key
) == ERROR_SUCCESS
) {
439 registry_recursive_remove(key
);
443 * Now open the parent key and remove the PuTTY main key. Once
444 * we've done that, see if the parent key has any other
447 if (RegOpenKey(HKEY_CURRENT_USER
, PUTTY_REG_PARENT
,
448 &key
) == ERROR_SUCCESS
) {
449 RegDeleteKey(key
, PUTTY_REG_PARENT_CHILD
);
450 ret
= RegEnumKey(key
, 0, name
, sizeof(name
));
453 * If the parent key had no other children, we must delete
454 * it in its turn. That means opening the _grandparent_
457 if (ret
!= ERROR_SUCCESS
) {
458 if (RegOpenKey(HKEY_CURRENT_USER
, PUTTY_REG_GPARENT
,
459 &key
) == ERROR_SUCCESS
) {
460 RegDeleteKey(key
, PUTTY_REG_GPARENT_CHILD
);