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 struct enumsettings
{
162 void *enum_settings_start(void) {
163 struct enumsettings
*ret
;
166 if (RegCreateKey(HKEY_CURRENT_USER
, puttystr
, &key
) != ERROR_SUCCESS
)
169 ret
= malloc(sizeof(*ret
));
178 char *enum_settings_next(void *handle
, char *buffer
, int buflen
) {
179 struct enumsettings
*e
= (struct enumsettings
*)handle
;
181 otherbuf
= malloc(3*buflen
);
182 if (otherbuf
&& RegEnumKey(e
->key
, e
->i
++, otherbuf
,
183 3*buflen
) == ERROR_SUCCESS
) {
184 unmungestr(otherbuf
, buffer
, buflen
);
192 void enum_settings_finish(void *handle
) {
193 struct enumsettings
*e
= (struct enumsettings
*)handle
;
198 static void hostkey_regname(char *buffer
, char *hostname
,
199 int port
, char *keytype
) {
201 strcpy(buffer
, keytype
);
203 len
= strlen(buffer
);
204 len
+= sprintf(buffer
+len
, "%d:", port
);
205 mungestr(hostname
, buffer
+ strlen(buffer
));
208 int verify_host_key(char *hostname
, int port
, char *keytype
, char *key
) {
209 char *otherstr
, *regname
;
216 len
= 1 + strlen(key
);
219 * Now read a saved key in from the registry and see what it
222 otherstr
= smalloc(len
);
223 regname
= smalloc(3*(strlen(hostname
)+strlen(keytype
))+15);
225 hostkey_regname(regname
, hostname
, port
, keytype
);
227 if (RegCreateKey(HKEY_CURRENT_USER
, PUTTY_REG_POS
"\\SshHostKeys",
228 &rkey
) != ERROR_SUCCESS
)
229 return 1; /* key does not exist in registry */
232 ret
= RegQueryValueEx(rkey
, regname
, NULL
, &type
, otherstr
, &readlen
);
234 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_MORE_DATA
&&
235 !strcmp(keytype
, "rsa")) {
237 * Key didn't exist. If the key type is RSA, we'll try
238 * another trick, which is to look up the _old_ key format
239 * under just the hostname and translate that.
241 char *justhost
= regname
+ 1 + strcspn(regname
, ":");
242 char *oldstyle
= smalloc(len
+ 10); /* safety margin */
244 ret
= RegQueryValueEx(rkey
, justhost
, NULL
, &type
,
247 if (ret
== ERROR_SUCCESS
&& type
== REG_SZ
) {
249 * The old format is two old-style bignums separated by
250 * a slash. An old-style bignum is made of groups of
251 * four hex digits: digits are ordered in sensible
252 * (most to least significant) order within each group,
253 * but groups are ordered in silly (least to most)
254 * order within the bignum. The new format is two
255 * ordinary C-format hex numbers (0xABCDEFG...XYZ, with
256 * A nonzero except in the special case 0x0, which
257 * doesn't appear anyway in RSA keys) separated by a
258 * comma. All hex digits are lowercase in both formats.
264 for (i
= 0; i
< 2; i
++) {
266 *p
++ = '0'; *p
++ = 'x';
267 ndigits
= strcspn(q
, "/"); /* find / or end of string */
268 nwords
= ndigits
/ 4;
269 /* now trim ndigits to remove leading zeros */
270 while (q
[ (ndigits
-1) ^ 3 ] == '0' && ndigits
> 1)
272 /* now move digits over to new string */
273 for (j
= 0; j
< ndigits
; j
++)
274 p
[ndigits
-1-j
] = q
[j
^3];
278 q
++; /* eat the slash */
279 *p
++ = ','; /* add a comma */
281 *p
= '\0'; /* terminate the string */
285 * Now _if_ this key matches, we'll enter it in the new
286 * format. If not, we'll assume something odd went
287 * wrong, and hyper-cautiously do nothing.
289 if (!strcmp(otherstr
, key
))
290 RegSetValueEx(rkey
, regname
, 0, REG_SZ
, otherstr
,
297 compare
= strcmp(otherstr
, key
);
302 if (ret
== ERROR_MORE_DATA
||
303 (ret
== ERROR_SUCCESS
&& type
== REG_SZ
&& compare
))
304 return 2; /* key is different in registry */
305 else if (ret
!= ERROR_SUCCESS
|| type
!= REG_SZ
)
306 return 1; /* key does not exist in registry */
308 return 0; /* key matched OK in registry */
311 void store_host_key(char *hostname
, int port
, char *keytype
, char *key
) {
315 regname
= smalloc(3*(strlen(hostname
)+strlen(keytype
))+15);
317 hostkey_regname(regname
, hostname
, port
, 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
);