d5859615 |
1 | /* |
2 | * winstore.c: Windows-specific implementation of the interface |
3 | * defined in storage.h. |
4 | */ |
5 | |
d5859615 |
6 | #include <stdio.h> |
49bad831 |
7 | #include <stdlib.h> |
9a30e26b |
8 | #include <limits.h> |
d5859615 |
9 | #include "putty.h" |
10 | #include "storage.h" |
11 | |
7bccf0fe |
12 | #include <shlobj.h> |
13 | #ifndef CSIDL_APPDATA |
14 | #define CSIDL_APPDATA 0x001a |
15 | #endif |
16 | #ifndef CSIDL_LOCAL_APPDATA |
17 | #define CSIDL_LOCAL_APPDATA 0x001c |
18 | #endif |
d1622aed |
19 | |
7bccf0fe |
20 | static const char *const puttystr = PUTTY_REG_POS "\\Sessions"; |
d5859615 |
21 | |
c85623f9 |
22 | static const char hex[16] = "0123456789ABCDEF"; |
d5859615 |
23 | |
7bccf0fe |
24 | static int tried_shgetfolderpath = FALSE; |
25 | static HMODULE shell32_module = NULL; |
9099600a |
26 | DECL_WINDOWS_FUNCTION(static, HRESULT, SHGetFolderPathA, |
27 | (HWND, int, HANDLE, DWORD, LPSTR)); |
7bccf0fe |
28 | |
c85623f9 |
29 | static void mungestr(const char *in, char *out) |
32874aea |
30 | { |
d5859615 |
31 | int candot = 0; |
32 | |
33 | while (*in) { |
34 | if (*in == ' ' || *in == '\\' || *in == '*' || *in == '?' || |
32874aea |
35 | *in == '%' || *in < ' ' || *in > '~' || (*in == '.' |
36 | && !candot)) { |
d5859615 |
37 | *out++ = '%'; |
32874aea |
38 | *out++ = hex[((unsigned char) *in) >> 4]; |
39 | *out++ = hex[((unsigned char) *in) & 15]; |
d5859615 |
40 | } else |
41 | *out++ = *in; |
42 | in++; |
43 | candot = 1; |
44 | } |
45 | *out = '\0'; |
46 | return; |
47 | } |
48 | |
c85623f9 |
49 | static void unmungestr(const char *in, char *out, int outlen) |
32874aea |
50 | { |
d5859615 |
51 | while (*in) { |
52 | if (*in == '%' && in[1] && in[2]) { |
53 | int i, j; |
54 | |
32874aea |
55 | i = in[1] - '0'; |
56 | i -= (i > 9 ? 7 : 0); |
57 | j = in[2] - '0'; |
58 | j -= (j > 9 ? 7 : 0); |
d5859615 |
59 | |
32874aea |
60 | *out++ = (i << 4) + j; |
61 | if (!--outlen) |
62 | return; |
d5859615 |
63 | in += 3; |
d1622aed |
64 | } else { |
d5859615 |
65 | *out++ = *in++; |
32874aea |
66 | if (!--outlen) |
67 | return; |
68 | } |
d5859615 |
69 | } |
70 | *out = '\0'; |
71 | return; |
72 | } |
73 | |
3f935d5b |
74 | void *open_settings_w(const char *sessionname, char **errmsg) |
32874aea |
75 | { |
d1622aed |
76 | HKEY subkey1, sesskey; |
77 | int ret; |
78 | char *p; |
79 | |
3f935d5b |
80 | *errmsg = NULL; |
81 | |
90bf4cce |
82 | if (!sessionname || !*sessionname) |
83 | sessionname = "Default Settings"; |
84 | |
3d88e64d |
85 | p = snewn(3 * strlen(sessionname) + 1, char); |
d1622aed |
86 | mungestr(sessionname, p); |
32874aea |
87 | |
d1622aed |
88 | ret = RegCreateKey(HKEY_CURRENT_USER, puttystr, &subkey1); |
89 | if (ret != ERROR_SUCCESS) { |
32874aea |
90 | sfree(p); |
3f935d5b |
91 | *errmsg = dupprintf("Unable to create registry key\n" |
c19a50ed |
92 | "HKEY_CURRENT_USER\\%s", puttystr); |
32874aea |
93 | return NULL; |
d1622aed |
94 | } |
95 | ret = RegCreateKey(subkey1, p, &sesskey); |
d1622aed |
96 | RegCloseKey(subkey1); |
3f935d5b |
97 | if (ret != ERROR_SUCCESS) { |
98 | *errmsg = dupprintf("Unable to create registry key\n" |
c19a50ed |
99 | "HKEY_CURRENT_USER\\%s\\%s", puttystr, p); |
2d3f15fa |
100 | sfree(p); |
32874aea |
101 | return NULL; |
3f935d5b |
102 | } |
2d3f15fa |
103 | sfree(p); |
32874aea |
104 | return (void *) sesskey; |
d1622aed |
105 | } |
106 | |
c85623f9 |
107 | void write_setting_s(void *handle, const char *key, const char *value) |
32874aea |
108 | { |
d1622aed |
109 | if (handle) |
32874aea |
110 | RegSetValueEx((HKEY) handle, key, 0, REG_SZ, value, |
111 | 1 + strlen(value)); |
d1622aed |
112 | } |
113 | |
c85623f9 |
114 | void write_setting_i(void *handle, const char *key, int value) |
32874aea |
115 | { |
d1622aed |
116 | if (handle) |
32874aea |
117 | RegSetValueEx((HKEY) handle, key, 0, REG_DWORD, |
e66c3912 |
118 | (CONST BYTE *) &value, sizeof(value)); |
d1622aed |
119 | } |
120 | |
32874aea |
121 | void close_settings_w(void *handle) |
122 | { |
123 | RegCloseKey((HKEY) handle); |
d1622aed |
124 | } |
125 | |
c85623f9 |
126 | void *open_settings_r(const char *sessionname) |
32874aea |
127 | { |
d1622aed |
128 | HKEY subkey1, sesskey; |
129 | char *p; |
130 | |
90bf4cce |
131 | if (!sessionname || !*sessionname) |
132 | sessionname = "Default Settings"; |
133 | |
3d88e64d |
134 | p = snewn(3 * strlen(sessionname) + 1, char); |
d1622aed |
135 | mungestr(sessionname, p); |
136 | |
137 | if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS) { |
138 | sesskey = NULL; |
139 | } else { |
140 | if (RegOpenKey(subkey1, p, &sesskey) != ERROR_SUCCESS) { |
141 | sesskey = NULL; |
142 | } |
143 | RegCloseKey(subkey1); |
144 | } |
145 | |
dcbde236 |
146 | sfree(p); |
d1622aed |
147 | |
32874aea |
148 | return (void *) sesskey; |
d1622aed |
149 | } |
150 | |
c85623f9 |
151 | char *read_setting_s(void *handle, const char *key, char *buffer, int buflen) |
32874aea |
152 | { |
d1622aed |
153 | DWORD type, size; |
154 | size = buflen; |
155 | |
156 | if (!handle || |
32874aea |
157 | RegQueryValueEx((HKEY) handle, key, 0, |
158 | &type, buffer, &size) != ERROR_SUCCESS || |
159 | type != REG_SZ) return NULL; |
d1622aed |
160 | else |
32874aea |
161 | return buffer; |
d1622aed |
162 | } |
d5859615 |
163 | |
c85623f9 |
164 | int read_setting_i(void *handle, const char *key, int defvalue) |
32874aea |
165 | { |
d1622aed |
166 | DWORD type, val, size; |
167 | size = sizeof(val); |
168 | |
169 | if (!handle || |
32874aea |
170 | RegQueryValueEx((HKEY) handle, key, 0, &type, |
e66c3912 |
171 | (BYTE *) &val, &size) != ERROR_SUCCESS || |
d1622aed |
172 | size != sizeof(val) || type != REG_DWORD) |
173 | return defvalue; |
174 | else |
175 | return val; |
176 | } |
177 | |
9a30e26b |
178 | int read_setting_fontspec(void *handle, const char *name, FontSpec *result) |
179 | { |
180 | char *settingname; |
181 | FontSpec ret; |
182 | |
183 | if (!read_setting_s(handle, name, ret.name, sizeof(ret.name))) |
184 | return 0; |
185 | settingname = dupcat(name, "IsBold", NULL); |
186 | ret.isbold = read_setting_i(handle, settingname, -1); |
187 | sfree(settingname); |
188 | if (ret.isbold == -1) return 0; |
189 | settingname = dupcat(name, "CharSet", NULL); |
190 | ret.charset = read_setting_i(handle, settingname, -1); |
191 | sfree(settingname); |
192 | if (ret.charset == -1) return 0; |
193 | settingname = dupcat(name, "Height", NULL); |
194 | ret.height = read_setting_i(handle, settingname, INT_MIN); |
195 | sfree(settingname); |
196 | if (ret.height == INT_MIN) return 0; |
9a30e26b |
197 | *result = ret; |
198 | return 1; |
199 | } |
200 | |
201 | void write_setting_fontspec(void *handle, const char *name, FontSpec font) |
202 | { |
203 | char *settingname; |
204 | |
205 | write_setting_s(handle, name, font.name); |
206 | settingname = dupcat(name, "IsBold", NULL); |
207 | write_setting_i(handle, settingname, font.isbold); |
208 | sfree(settingname); |
209 | settingname = dupcat(name, "CharSet", NULL); |
210 | write_setting_i(handle, settingname, font.charset); |
211 | sfree(settingname); |
212 | settingname = dupcat(name, "Height", NULL); |
213 | write_setting_i(handle, settingname, font.height); |
214 | sfree(settingname); |
215 | } |
216 | |
217 | int read_setting_filename(void *handle, const char *name, Filename *result) |
218 | { |
219 | return !!read_setting_s(handle, name, result->path, sizeof(result->path)); |
220 | } |
221 | |
222 | void write_setting_filename(void *handle, const char *name, Filename result) |
223 | { |
224 | write_setting_s(handle, name, result.path); |
225 | } |
226 | |
32874aea |
227 | void close_settings_r(void *handle) |
228 | { |
229 | RegCloseKey((HKEY) handle); |
d1622aed |
230 | } |
231 | |
c85623f9 |
232 | void del_settings(const char *sessionname) |
32874aea |
233 | { |
d1622aed |
234 | HKEY subkey1; |
235 | char *p; |
236 | |
237 | if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS) |
238 | return; |
239 | |
3d88e64d |
240 | p = snewn(3 * strlen(sessionname) + 1, char); |
d1622aed |
241 | mungestr(sessionname, p); |
242 | RegDeleteKey(subkey1, p); |
dcbde236 |
243 | sfree(p); |
d1622aed |
244 | |
245 | RegCloseKey(subkey1); |
246 | } |
d5859615 |
247 | |
d1622aed |
248 | struct enumsettings { |
249 | HKEY key; |
250 | int i; |
251 | }; |
252 | |
32874aea |
253 | void *enum_settings_start(void) |
254 | { |
d1622aed |
255 | struct enumsettings *ret; |
256 | HKEY key; |
257 | |
5f2df4d2 |
258 | if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &key) != ERROR_SUCCESS) |
32874aea |
259 | return NULL; |
d1622aed |
260 | |
3d88e64d |
261 | ret = snew(struct enumsettings); |
d1622aed |
262 | if (ret) { |
32874aea |
263 | ret->key = key; |
264 | ret->i = 0; |
d1622aed |
265 | } |
266 | |
267 | return ret; |
268 | } |
269 | |
32874aea |
270 | char *enum_settings_next(void *handle, char *buffer, int buflen) |
271 | { |
272 | struct enumsettings *e = (struct enumsettings *) handle; |
d1622aed |
273 | char *otherbuf; |
3d88e64d |
274 | otherbuf = snewn(3 * buflen, char); |
60371ab3 |
275 | if (RegEnumKey(e->key, e->i++, otherbuf, 3 * buflen) == ERROR_SUCCESS) { |
32874aea |
276 | unmungestr(otherbuf, buffer, buflen); |
277 | sfree(otherbuf); |
278 | return buffer; |
60371ab3 |
279 | } else { |
280 | sfree(otherbuf); |
32874aea |
281 | return NULL; |
60371ab3 |
282 | } |
d1622aed |
283 | } |
284 | |
32874aea |
285 | void enum_settings_finish(void *handle) |
286 | { |
287 | struct enumsettings *e = (struct enumsettings *) handle; |
d1622aed |
288 | RegCloseKey(e->key); |
dcbde236 |
289 | sfree(e); |
d1622aed |
290 | } |
291 | |
c85623f9 |
292 | static void hostkey_regname(char *buffer, const char *hostname, |
293 | int port, const char *keytype) |
32874aea |
294 | { |
d4857987 |
295 | int len; |
296 | strcpy(buffer, keytype); |
297 | strcat(buffer, "@"); |
298 | len = strlen(buffer); |
32874aea |
299 | len += sprintf(buffer + len, "%d:", port); |
d4857987 |
300 | mungestr(hostname, buffer + strlen(buffer)); |
301 | } |
302 | |
c85623f9 |
303 | int verify_host_key(const char *hostname, int port, |
304 | const char *keytype, const char *key) |
32874aea |
305 | { |
d5859615 |
306 | char *otherstr, *regname; |
307 | int len; |
308 | HKEY rkey; |
309 | DWORD readlen; |
310 | DWORD type; |
311 | int ret, compare; |
312 | |
313 | len = 1 + strlen(key); |
314 | |
315 | /* |
316 | * Now read a saved key in from the registry and see what it |
317 | * says. |
318 | */ |
3d88e64d |
319 | otherstr = snewn(len, char); |
320 | regname = snewn(3 * (strlen(hostname) + strlen(keytype)) + 15, char); |
d5859615 |
321 | |
d4857987 |
322 | hostkey_regname(regname, hostname, port, keytype); |
d5859615 |
323 | |
5f2df4d2 |
324 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys", |
325 | &rkey) != ERROR_SUCCESS) |
32874aea |
326 | return 1; /* key does not exist in registry */ |
d5859615 |
327 | |
328 | readlen = len; |
329 | ret = RegQueryValueEx(rkey, regname, NULL, &type, otherstr, &readlen); |
330 | |
331 | if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA && |
32874aea |
332 | !strcmp(keytype, "rsa")) { |
333 | /* |
334 | * Key didn't exist. If the key type is RSA, we'll try |
335 | * another trick, which is to look up the _old_ key format |
336 | * under just the hostname and translate that. |
337 | */ |
338 | char *justhost = regname + 1 + strcspn(regname, ":"); |
3d88e64d |
339 | char *oldstyle = snewn(len + 10, char); /* safety margin */ |
32874aea |
340 | readlen = len; |
341 | ret = RegQueryValueEx(rkey, justhost, NULL, &type, |
342 | oldstyle, &readlen); |
343 | |
344 | if (ret == ERROR_SUCCESS && type == REG_SZ) { |
345 | /* |
346 | * The old format is two old-style bignums separated by |
347 | * a slash. An old-style bignum is made of groups of |
348 | * four hex digits: digits are ordered in sensible |
349 | * (most to least significant) order within each group, |
350 | * but groups are ordered in silly (least to most) |
351 | * order within the bignum. The new format is two |
352 | * ordinary C-format hex numbers (0xABCDEFG...XYZ, with |
353 | * A nonzero except in the special case 0x0, which |
354 | * doesn't appear anyway in RSA keys) separated by a |
355 | * comma. All hex digits are lowercase in both formats. |
356 | */ |
357 | char *p = otherstr; |
358 | char *q = oldstyle; |
359 | int i, j; |
360 | |
361 | for (i = 0; i < 2; i++) { |
362 | int ndigits, nwords; |
363 | *p++ = '0'; |
364 | *p++ = 'x'; |
365 | ndigits = strcspn(q, "/"); /* find / or end of string */ |
366 | nwords = ndigits / 4; |
367 | /* now trim ndigits to remove leading zeros */ |
368 | while (q[(ndigits - 1) ^ 3] == '0' && ndigits > 1) |
369 | ndigits--; |
370 | /* now move digits over to new string */ |
371 | for (j = 0; j < ndigits; j++) |
372 | p[ndigits - 1 - j] = q[j ^ 3]; |
373 | p += ndigits; |
374 | q += nwords * 4; |
375 | if (*q) { |
376 | q++; /* eat the slash */ |
377 | *p++ = ','; /* add a comma */ |
378 | } |
379 | *p = '\0'; /* terminate the string */ |
380 | } |
381 | |
382 | /* |
383 | * Now _if_ this key matches, we'll enter it in the new |
384 | * format. If not, we'll assume something odd went |
385 | * wrong, and hyper-cautiously do nothing. |
386 | */ |
387 | if (!strcmp(otherstr, key)) |
388 | RegSetValueEx(rkey, regname, 0, REG_SZ, otherstr, |
389 | strlen(otherstr) + 1); |
390 | } |
d5859615 |
391 | } |
392 | |
393 | RegCloseKey(rkey); |
394 | |
395 | compare = strcmp(otherstr, key); |
396 | |
397 | sfree(otherstr); |
398 | sfree(regname); |
399 | |
400 | if (ret == ERROR_MORE_DATA || |
32874aea |
401 | (ret == ERROR_SUCCESS && type == REG_SZ && compare)) |
402 | return 2; /* key is different in registry */ |
d5859615 |
403 | else if (ret != ERROR_SUCCESS || type != REG_SZ) |
32874aea |
404 | return 1; /* key does not exist in registry */ |
d5859615 |
405 | else |
32874aea |
406 | return 0; /* key matched OK in registry */ |
d5859615 |
407 | } |
408 | |
c85623f9 |
409 | void store_host_key(const char *hostname, int port, |
410 | const char *keytype, const char *key) |
32874aea |
411 | { |
d5859615 |
412 | char *regname; |
413 | HKEY rkey; |
414 | |
3d88e64d |
415 | regname = snewn(3 * (strlen(hostname) + strlen(keytype)) + 15, char); |
d5859615 |
416 | |
d4857987 |
417 | hostkey_regname(regname, hostname, port, keytype); |
d5859615 |
418 | |
419 | if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys", |
fc2464c6 |
420 | &rkey) == ERROR_SUCCESS) { |
421 | RegSetValueEx(rkey, regname, 0, REG_SZ, key, strlen(key) + 1); |
422 | RegCloseKey(rkey); |
423 | } /* else key does not exist in registry */ |
424 | |
425 | sfree(regname); |
d5859615 |
426 | } |
427 | |
428 | /* |
7bccf0fe |
429 | * Open (or delete) the random seed file. |
d5859615 |
430 | */ |
7bccf0fe |
431 | enum { DEL, OPEN_R, OPEN_W }; |
432 | static int try_random_seed(char const *path, int action, HANDLE *ret) |
433 | { |
434 | if (action == DEL) { |
435 | remove(path); |
436 | *ret = INVALID_HANDLE_VALUE; |
437 | return FALSE; /* so we'll do the next ones too */ |
438 | } |
439 | |
440 | *ret = CreateFile(path, |
441 | action == OPEN_W ? GENERIC_WRITE : GENERIC_READ, |
442 | action == OPEN_W ? 0 : (FILE_SHARE_READ | |
443 | FILE_SHARE_WRITE), |
444 | NULL, |
445 | action == OPEN_W ? CREATE_ALWAYS : OPEN_EXISTING, |
446 | action == OPEN_W ? FILE_ATTRIBUTE_NORMAL : 0, |
447 | NULL); |
448 | |
449 | return (*ret != INVALID_HANDLE_VALUE); |
450 | } |
451 | |
452 | static HANDLE access_random_seed(int action) |
32874aea |
453 | { |
d5859615 |
454 | HKEY rkey; |
455 | DWORD type, size; |
7bccf0fe |
456 | HANDLE rethandle; |
457 | char seedpath[2 * MAX_PATH + 10] = "\0"; |
d5859615 |
458 | |
7bccf0fe |
459 | /* |
460 | * Iterate over a selection of possible random seed paths until |
461 | * we find one that works. |
462 | * |
463 | * We do this iteration separately for reading and writing, |
464 | * meaning that we will automatically migrate random seed files |
465 | * if a better location becomes available (by reading from the |
466 | * best location in which we actually find one, and then |
467 | * writing to the best location in which we can _create_ one). |
468 | */ |
d5859615 |
469 | |
7bccf0fe |
470 | /* |
471 | * First, try the location specified by the user in the |
472 | * Registry, if any. |
473 | */ |
474 | size = sizeof(seedpath); |
32874aea |
475 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &rkey) == |
476 | ERROR_SUCCESS) { |
d5859615 |
477 | int ret = RegQueryValueEx(rkey, "RandSeedFile", |
478 | 0, &type, seedpath, &size); |
479 | if (ret != ERROR_SUCCESS || type != REG_SZ) |
480 | seedpath[0] = '\0'; |
481 | RegCloseKey(rkey); |
d5859615 |
482 | |
7bccf0fe |
483 | if (*seedpath && try_random_seed(seedpath, action, &rethandle)) |
484 | return rethandle; |
485 | } |
486 | |
487 | /* |
488 | * Next, try the user's local Application Data directory, |
489 | * followed by their non-local one. This is found using the |
490 | * SHGetFolderPath function, which won't be present on all |
491 | * versions of Windows. |
492 | */ |
493 | if (!tried_shgetfolderpath) { |
8f5f26d2 |
494 | /* This is likely only to bear fruit on systems with IE5+ |
495 | * installed, or WinMe/2K+. There is some faffing with |
496 | * SHFOLDER.DLL we could do to try to find an equivalent |
497 | * on older versions of Windows if we cared enough. |
498 | * However, the invocation below requires IE5+ anyway, |
499 | * so stuff that. */ |
7bccf0fe |
500 | shell32_module = LoadLibrary("SHELL32.DLL"); |
9099600a |
501 | GET_WINDOWS_FUNCTION(shell32_module, SHGetFolderPathA); |
a6c15e85 |
502 | tried_shgetfolderpath = TRUE; |
7bccf0fe |
503 | } |
9099600a |
504 | if (p_SHGetFolderPathA) { |
505 | if (SUCCEEDED(p_SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, |
506 | NULL, SHGFP_TYPE_CURRENT, seedpath))) { |
7bccf0fe |
507 | strcat(seedpath, "\\PUTTY.RND"); |
508 | if (try_random_seed(seedpath, action, &rethandle)) |
509 | return rethandle; |
510 | } |
511 | |
9099600a |
512 | if (SUCCEEDED(p_SHGetFolderPathA(NULL, CSIDL_APPDATA, |
513 | NULL, SHGFP_TYPE_CURRENT, seedpath))) { |
7bccf0fe |
514 | strcat(seedpath, "\\PUTTY.RND"); |
515 | if (try_random_seed(seedpath, action, &rethandle)) |
516 | return rethandle; |
517 | } |
518 | } |
519 | |
520 | /* |
521 | * Failing that, try %HOMEDRIVE%%HOMEPATH% as a guess at the |
522 | * user's home directory. |
523 | */ |
524 | { |
d5859615 |
525 | int len, ret; |
526 | |
32874aea |
527 | len = |
528 | GetEnvironmentVariable("HOMEDRIVE", seedpath, |
529 | sizeof(seedpath)); |
530 | ret = |
531 | GetEnvironmentVariable("HOMEPATH", seedpath + len, |
532 | sizeof(seedpath) - len); |
7bccf0fe |
533 | if (ret != 0) { |
534 | strcat(seedpath, "\\PUTTY.RND"); |
535 | if (try_random_seed(seedpath, action, &rethandle)) |
536 | return rethandle; |
537 | } |
d5859615 |
538 | } |
7bccf0fe |
539 | |
540 | /* |
541 | * And finally, fall back to C:\WINDOWS. |
542 | */ |
543 | GetWindowsDirectory(seedpath, sizeof(seedpath)); |
544 | strcat(seedpath, "\\PUTTY.RND"); |
545 | if (try_random_seed(seedpath, action, &rethandle)) |
546 | return rethandle; |
547 | |
548 | /* |
549 | * If even that failed, give up. |
550 | */ |
551 | return INVALID_HANDLE_VALUE; |
d5859615 |
552 | } |
553 | |
32874aea |
554 | void read_random_seed(noise_consumer_t consumer) |
555 | { |
7bccf0fe |
556 | HANDLE seedf = access_random_seed(OPEN_R); |
d5859615 |
557 | |
558 | if (seedf != INVALID_HANDLE_VALUE) { |
559 | while (1) { |
560 | char buf[1024]; |
561 | DWORD len; |
562 | |
563 | if (ReadFile(seedf, buf, sizeof(buf), &len, NULL) && len) |
32874aea |
564 | consumer(buf, len); |
d5859615 |
565 | else |
566 | break; |
567 | } |
568 | CloseHandle(seedf); |
569 | } |
570 | } |
571 | |
32874aea |
572 | void write_random_seed(void *data, int len) |
573 | { |
7bccf0fe |
574 | HANDLE seedf = access_random_seed(OPEN_W); |
d5859615 |
575 | |
576 | if (seedf != INVALID_HANDLE_VALUE) { |
577 | DWORD lenwritten; |
578 | |
579 | WriteFile(seedf, data, len, &lenwritten, NULL); |
580 | CloseHandle(seedf); |
581 | } |
582 | } |
583 | |
584 | /* |
585 | * Recursively delete a registry key and everything under it. |
586 | */ |
32874aea |
587 | static void registry_recursive_remove(HKEY key) |
588 | { |
d5859615 |
589 | DWORD i; |
32874aea |
590 | char name[MAX_PATH + 1]; |
d5859615 |
591 | HKEY subkey; |
592 | |
593 | i = 0; |
594 | while (RegEnumKey(key, i, name, sizeof(name)) == ERROR_SUCCESS) { |
32874aea |
595 | if (RegOpenKey(key, name, &subkey) == ERROR_SUCCESS) { |
596 | registry_recursive_remove(subkey); |
597 | RegCloseKey(subkey); |
598 | } |
599 | RegDeleteKey(key, name); |
d5859615 |
600 | } |
601 | } |
602 | |
32874aea |
603 | void cleanup_all(void) |
604 | { |
d5859615 |
605 | HKEY key; |
606 | int ret; |
32874aea |
607 | char name[MAX_PATH + 1]; |
d5859615 |
608 | |
609 | /* ------------------------------------------------------------ |
7bccf0fe |
610 | * Wipe out the random seed file, in all of its possible |
611 | * locations. |
d5859615 |
612 | */ |
7bccf0fe |
613 | access_random_seed(DEL); |
d5859615 |
614 | |
615 | /* ------------------------------------------------------------ |
616 | * Destroy all registry information associated with PuTTY. |
617 | */ |
618 | |
619 | /* |
620 | * Open the main PuTTY registry key and remove everything in it. |
621 | */ |
32874aea |
622 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &key) == |
623 | ERROR_SUCCESS) { |
624 | registry_recursive_remove(key); |
625 | RegCloseKey(key); |
d5859615 |
626 | } |
627 | /* |
628 | * Now open the parent key and remove the PuTTY main key. Once |
629 | * we've done that, see if the parent key has any other |
630 | * children. |
631 | */ |
632 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_PARENT, |
32874aea |
633 | &key) == ERROR_SUCCESS) { |
634 | RegDeleteKey(key, PUTTY_REG_PARENT_CHILD); |
635 | ret = RegEnumKey(key, 0, name, sizeof(name)); |
636 | RegCloseKey(key); |
637 | /* |
638 | * If the parent key had no other children, we must delete |
639 | * it in its turn. That means opening the _grandparent_ |
640 | * key. |
641 | */ |
642 | if (ret != ERROR_SUCCESS) { |
643 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_GPARENT, |
644 | &key) == ERROR_SUCCESS) { |
645 | RegDeleteKey(key, PUTTY_REG_GPARENT_CHILD); |
646 | RegCloseKey(key); |
647 | } |
648 | } |
d5859615 |
649 | } |
650 | /* |
651 | * Now we're done. |
652 | */ |
653 | } |