2 * winstore.c: Windows-specific implementation of the interface
3 * defined in storage.h.
18 static const char *const puttystr
= PUTTY_REG_POS
"\\Sessions";
20 static char seedpath
[2*MAX_PATH
+10] = "\0";
22 static char hex
[16] = "0123456789ABCDEF";
24 static void mungestr(char *in
, char *out
) {
28 if (*in
== ' ' || *in
== '\\' || *in
== '*' || *in
== '?' ||
29 *in
== '%' || *in
< ' ' || *in
> '~' || (*in
== '.' && !candot
)) {
31 *out
++ = hex
[((unsigned char)*in
) >> 4];
32 *out
++ = hex
[((unsigned char)*in
) & 15];
42 static void unmungestr(char *in
, char *out
, int outlen
) {
44 if (*in
== '%' && in
[1] && in
[2]) {
47 i
= in
[1] - '0'; i
-= (i
> 9 ?
7 : 0);
48 j
= in
[2] - '0'; j
-= (j
> 9 ?
7 : 0);
51 if (!--outlen
) return;
55 if (!--outlen
) return;
62 void *open_settings_w(char *sessionname
) {
63 HKEY subkey1
, sesskey
;
67 p
= malloc(3*strlen(sessionname
)+1);
68 mungestr(sessionname
, p
);
70 ret
= RegCreateKey(HKEY_CURRENT_USER
, puttystr
, &subkey1
);
71 if (ret
!= ERROR_SUCCESS
) {
75 ret
= RegCreateKey(subkey1
, p
, &sesskey
);
78 if (ret
!= ERROR_SUCCESS
)
80 return (void *)sesskey
;
83 void write_setting_s(void *handle
, char *key
, char *value
) {
85 RegSetValueEx((HKEY
)handle
, key
, 0, REG_SZ
, value
, 1+strlen(value
));
88 void write_setting_i(void *handle
, char *key
, int value
) {
90 RegSetValueEx((HKEY
)handle
, key
, 0, REG_DWORD
,
91 (CONST BYTE
*)&value
, sizeof(value
));
94 void close_settings_w(void *handle
) {
95 RegCloseKey((HKEY
)handle
);
98 void *open_settings_r(char *sessionname
) {
99 HKEY subkey1
, sesskey
;
102 p
= malloc(3*strlen(sessionname
)+1);
103 mungestr(sessionname
, p
);
105 if (RegOpenKey(HKEY_CURRENT_USER
, puttystr
, &subkey1
) != ERROR_SUCCESS
) {
108 if (RegOpenKey(subkey1
, p
, &sesskey
) != ERROR_SUCCESS
) {
111 RegCloseKey(subkey1
);
116 return (void *)sesskey
;
119 char *read_setting_s(void *handle
, char *key
, char *buffer
, int buflen
) {
124 RegQueryValueEx((HKEY
)handle
, key
, 0,
125 &type
, buffer
, &size
) != ERROR_SUCCESS
||
132 int read_setting_i(void *handle
, char *key
, int defvalue
) {
133 DWORD type
, val
, size
;
137 RegQueryValueEx((HKEY
)handle
, key
, 0, &type
,
138 (BYTE
*)&val
, &size
) != ERROR_SUCCESS
||
139 size
!= sizeof(val
) || type
!= REG_DWORD
)
145 void close_settings_r(void *handle
) {
146 RegCloseKey((HKEY
)handle
);
149 void del_settings (char *sessionname
) {
153 if (RegOpenKey(HKEY_CURRENT_USER
, puttystr
, &subkey1
) != ERROR_SUCCESS
)
156 p
= malloc(3*strlen(sessionname
)+1);
157 mungestr(sessionname
, p
);
158 RegDeleteKey(subkey1
, p
);
161 RegCloseKey(subkey1
);
164 struct enumsettings
{
169 void *enum_settings_start(void) {
170 struct enumsettings
*ret
;
173 if (RegCreateKey(HKEY_CURRENT_USER
, puttystr
, &key
) != ERROR_SUCCESS
)
176 ret
= malloc(sizeof(*ret
));
185 char *enum_settings_next(void *handle
, char *buffer
, int buflen
) {
186 struct enumsettings
*e
= (struct enumsettings
*)handle
;
188 otherbuf
= malloc(3*buflen
);
189 if (otherbuf
&& RegEnumKey(e
->key
, e
->i
++, otherbuf
,
190 3*buflen
) == ERROR_SUCCESS
) {
191 unmungestr(otherbuf
, buffer
, buflen
);
199 void enum_settings_finish(void *handle
) {
200 struct enumsettings
*e
= (struct enumsettings
*)handle
;
205 static void hostkey_regname(char *buffer
, char *hostname
,
206 int port
, char *keytype
) {
208 strcpy(buffer
, keytype
);
210 len
= strlen(buffer
);
211 len
+= sprintf(buffer
+len
, "%d:", port
);
212 mungestr(hostname
, buffer
+ strlen(buffer
));
215 int verify_host_key(char *hostname
, int port
, char *keytype
, char *key
) {
216 char *otherstr
, *regname
;
223 len
= 1 + strlen(key
);
226 * Now read a saved key in from the registry and see what it
229 otherstr
= smalloc(len
);
230 regname
= smalloc(3*(strlen(hostname
)+strlen(keytype
))+15);
232 hostkey_regname(regname
, hostname
, port
, keytype
);
234 if (RegCreateKey(HKEY_CURRENT_USER
, PUTTY_REG_POS
"\\SshHostKeys",
235 &rkey
) != ERROR_SUCCESS
)
236 return 1; /* key does not exist in registry */
239 ret
= RegQueryValueEx(rkey
, regname
, NULL
, &type
, otherstr
, &readlen
);
241 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_MORE_DATA
&&
242 !strcmp(keytype
, "rsa")) {
244 * Key didn't exist. If the key type is RSA, we'll try
245 * another trick, which is to look up the _old_ key format
246 * under just the hostname and translate that.
248 char *justhost
= regname
+ 1 + strcspn(regname
, ":");
249 char *oldstyle
= smalloc(len
+ 10); /* safety margin */
251 ret
= RegQueryValueEx(rkey
, justhost
, NULL
, &type
,
254 if (ret
== ERROR_SUCCESS
&& type
== REG_SZ
) {
256 * The old format is two old-style bignums separated by
257 * a slash. An old-style bignum is made of groups of
258 * four hex digits: digits are ordered in sensible
259 * (most to least significant) order within each group,
260 * but groups are ordered in silly (least to most)
261 * order within the bignum. The new format is two
262 * ordinary C-format hex numbers (0xABCDEFG...XYZ, with
263 * A nonzero except in the special case 0x0, which
264 * doesn't appear anyway in RSA keys) separated by a
265 * comma. All hex digits are lowercase in both formats.
271 for (i
= 0; i
< 2; i
++) {
273 *p
++ = '0'; *p
++ = 'x';
274 ndigits
= strcspn(q
, "/"); /* find / or end of string */
275 nwords
= ndigits
/ 4;
276 /* now trim ndigits to remove leading zeros */
277 while (q
[ (ndigits
-1) ^ 3 ] == '0' && ndigits
> 1)
279 /* now move digits over to new string */
280 for (j
= 0; j
< ndigits
; j
++)
281 p
[ndigits
-1-j
] = q
[j
^3];
285 q
++; /* eat the slash */
286 *p
++ = ','; /* add a comma */
288 *p
= '\0'; /* terminate the string */
292 * Now _if_ this key matches, we'll enter it in the new
293 * format. If not, we'll assume something odd went
294 * wrong, and hyper-cautiously do nothing.
296 if (!strcmp(otherstr
, key
))
297 RegSetValueEx(rkey
, regname
, 0, REG_SZ
, otherstr
,
304 compare
= strcmp(otherstr
, key
);
309 if (ret
== ERROR_MORE_DATA
||
310 (ret
== ERROR_SUCCESS
&& type
== REG_SZ
&& compare
))
311 return 2; /* key is different in registry */
312 else if (ret
!= ERROR_SUCCESS
|| type
!= REG_SZ
)
313 return 1; /* key does not exist in registry */
315 return 0; /* key matched OK in registry */
318 void store_host_key(char *hostname
, int port
, char *keytype
, char *key
) {
322 regname
= smalloc(3*(strlen(hostname
)+strlen(keytype
))+15);
324 hostkey_regname(regname
, hostname
, port
, keytype
);
326 if (RegCreateKey(HKEY_CURRENT_USER
, PUTTY_REG_POS
"\\SshHostKeys",
327 &rkey
) != ERROR_SUCCESS
)
328 return; /* key does not exist in registry */
329 RegSetValueEx(rkey
, regname
, 0, REG_SZ
, key
,
335 * Find the random seed file path and store it in `seedpath'.
337 static void get_seedpath(void) {
341 size
= sizeof(seedpath
);
343 if (RegOpenKey(HKEY_CURRENT_USER
, PUTTY_REG_POS
, &rkey
)==ERROR_SUCCESS
) {
344 int ret
= RegQueryValueEx(rkey
, "RandSeedFile",
345 0, &type
, seedpath
, &size
);
346 if (ret
!= ERROR_SUCCESS
|| type
!= REG_SZ
)
355 len
= GetEnvironmentVariable("HOMEDRIVE", seedpath
, sizeof(seedpath
));
356 ret
= GetEnvironmentVariable("HOMEPATH", seedpath
+len
,
357 sizeof(seedpath
)-len
);
358 if (ret
== 0) { /* probably win95; store in \WINDOWS */
359 GetWindowsDirectory(seedpath
, sizeof(seedpath
));
360 len
= strlen(seedpath
);
363 strcpy(seedpath
+len
, "\\PUTTY.RND");
367 void read_random_seed(noise_consumer_t consumer
) {
373 seedf
= CreateFile(seedpath
, GENERIC_READ
,
374 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
375 NULL
, OPEN_EXISTING
, 0, NULL
);
377 if (seedf
!= INVALID_HANDLE_VALUE
) {
382 if (ReadFile(seedf
, buf
, sizeof(buf
), &len
, NULL
) && len
)
391 void write_random_seed(void *data
, int len
) {
397 seedf
= CreateFile(seedpath
, GENERIC_WRITE
, 0,
398 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
400 if (seedf
!= INVALID_HANDLE_VALUE
) {
403 WriteFile(seedf
, data
, len
, &lenwritten
, NULL
);
409 * Recursively delete a registry key and everything under it.
411 static void registry_recursive_remove(HKEY key
) {
413 char name
[MAX_PATH
+1];
417 while (RegEnumKey(key
, i
, name
, sizeof(name
)) == ERROR_SUCCESS
) {
418 if (RegOpenKey(key
, name
, &subkey
) == ERROR_SUCCESS
) {
419 registry_recursive_remove(subkey
);
422 RegDeleteKey(key
, name
);
426 void cleanup_all(void) {
429 char name
[MAX_PATH
+1];
431 /* ------------------------------------------------------------
432 * Wipe out the random seed file.
438 /* ------------------------------------------------------------
439 * Destroy all registry information associated with PuTTY.
443 * Open the main PuTTY registry key and remove everything in it.
445 if (RegOpenKey(HKEY_CURRENT_USER
, PUTTY_REG_POS
, &key
) == ERROR_SUCCESS
) {
446 registry_recursive_remove(key
);
450 * Now open the parent key and remove the PuTTY main key. Once
451 * we've done that, see if the parent key has any other
454 if (RegOpenKey(HKEY_CURRENT_USER
, PUTTY_REG_PARENT
,
455 &key
) == ERROR_SUCCESS
) {
456 RegDeleteKey(key
, PUTTY_REG_PARENT_CHILD
);
457 ret
= RegEnumKey(key
, 0, name
, sizeof(name
));
460 * If the parent key had no other children, we must delete
461 * it in its turn. That means opening the _grandparent_
464 if (ret
!= ERROR_SUCCESS
) {
465 if (RegOpenKey(HKEY_CURRENT_USER
, PUTTY_REG_GPARENT
,
466 &key
) == ERROR_SUCCESS
) {
467 RegDeleteKey(key
, PUTTY_REG_GPARENT_CHILD
);