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 | |
073e9f42 |
20 | static const char *const reg_jumplist_key = PUTTY_REG_POS "\\Jumplist"; |
21 | static const char *const reg_jumplist_value = "Recent sessions"; |
7bccf0fe |
22 | static const char *const puttystr = PUTTY_REG_POS "\\Sessions"; |
d5859615 |
23 | |
c85623f9 |
24 | static const char hex[16] = "0123456789ABCDEF"; |
d5859615 |
25 | |
7bccf0fe |
26 | static int tried_shgetfolderpath = FALSE; |
27 | static HMODULE shell32_module = NULL; |
9099600a |
28 | DECL_WINDOWS_FUNCTION(static, HRESULT, SHGetFolderPathA, |
29 | (HWND, int, HANDLE, DWORD, LPSTR)); |
7bccf0fe |
30 | |
c85623f9 |
31 | static void mungestr(const char *in, char *out) |
32874aea |
32 | { |
d5859615 |
33 | int candot = 0; |
34 | |
35 | while (*in) { |
36 | if (*in == ' ' || *in == '\\' || *in == '*' || *in == '?' || |
32874aea |
37 | *in == '%' || *in < ' ' || *in > '~' || (*in == '.' |
38 | && !candot)) { |
d5859615 |
39 | *out++ = '%'; |
32874aea |
40 | *out++ = hex[((unsigned char) *in) >> 4]; |
41 | *out++ = hex[((unsigned char) *in) & 15]; |
d5859615 |
42 | } else |
43 | *out++ = *in; |
44 | in++; |
45 | candot = 1; |
46 | } |
47 | *out = '\0'; |
48 | return; |
49 | } |
50 | |
c85623f9 |
51 | static void unmungestr(const char *in, char *out, int outlen) |
32874aea |
52 | { |
d5859615 |
53 | while (*in) { |
54 | if (*in == '%' && in[1] && in[2]) { |
55 | int i, j; |
56 | |
32874aea |
57 | i = in[1] - '0'; |
58 | i -= (i > 9 ? 7 : 0); |
59 | j = in[2] - '0'; |
60 | j -= (j > 9 ? 7 : 0); |
d5859615 |
61 | |
32874aea |
62 | *out++ = (i << 4) + j; |
63 | if (!--outlen) |
64 | return; |
d5859615 |
65 | in += 3; |
d1622aed |
66 | } else { |
d5859615 |
67 | *out++ = *in++; |
32874aea |
68 | if (!--outlen) |
69 | return; |
70 | } |
d5859615 |
71 | } |
72 | *out = '\0'; |
73 | return; |
74 | } |
75 | |
3f935d5b |
76 | void *open_settings_w(const char *sessionname, char **errmsg) |
32874aea |
77 | { |
d1622aed |
78 | HKEY subkey1, sesskey; |
79 | int ret; |
80 | char *p; |
81 | |
3f935d5b |
82 | *errmsg = NULL; |
83 | |
90bf4cce |
84 | if (!sessionname || !*sessionname) |
85 | sessionname = "Default Settings"; |
86 | |
3d88e64d |
87 | p = snewn(3 * strlen(sessionname) + 1, char); |
d1622aed |
88 | mungestr(sessionname, p); |
32874aea |
89 | |
d1622aed |
90 | ret = RegCreateKey(HKEY_CURRENT_USER, puttystr, &subkey1); |
91 | if (ret != ERROR_SUCCESS) { |
32874aea |
92 | sfree(p); |
3f935d5b |
93 | *errmsg = dupprintf("Unable to create registry key\n" |
c19a50ed |
94 | "HKEY_CURRENT_USER\\%s", puttystr); |
32874aea |
95 | return NULL; |
d1622aed |
96 | } |
97 | ret = RegCreateKey(subkey1, p, &sesskey); |
d1622aed |
98 | RegCloseKey(subkey1); |
3f935d5b |
99 | if (ret != ERROR_SUCCESS) { |
100 | *errmsg = dupprintf("Unable to create registry key\n" |
c19a50ed |
101 | "HKEY_CURRENT_USER\\%s\\%s", puttystr, p); |
2d3f15fa |
102 | sfree(p); |
32874aea |
103 | return NULL; |
3f935d5b |
104 | } |
2d3f15fa |
105 | sfree(p); |
32874aea |
106 | return (void *) sesskey; |
d1622aed |
107 | } |
108 | |
c85623f9 |
109 | void write_setting_s(void *handle, const char *key, const char *value) |
32874aea |
110 | { |
d1622aed |
111 | if (handle) |
32874aea |
112 | RegSetValueEx((HKEY) handle, key, 0, REG_SZ, value, |
113 | 1 + strlen(value)); |
d1622aed |
114 | } |
115 | |
c85623f9 |
116 | void write_setting_i(void *handle, const char *key, int value) |
32874aea |
117 | { |
d1622aed |
118 | if (handle) |
32874aea |
119 | RegSetValueEx((HKEY) handle, key, 0, REG_DWORD, |
e66c3912 |
120 | (CONST BYTE *) &value, sizeof(value)); |
d1622aed |
121 | } |
122 | |
32874aea |
123 | void close_settings_w(void *handle) |
124 | { |
125 | RegCloseKey((HKEY) handle); |
d1622aed |
126 | } |
127 | |
c85623f9 |
128 | void *open_settings_r(const char *sessionname) |
32874aea |
129 | { |
d1622aed |
130 | HKEY subkey1, sesskey; |
131 | char *p; |
132 | |
90bf4cce |
133 | if (!sessionname || !*sessionname) |
134 | sessionname = "Default Settings"; |
135 | |
3d88e64d |
136 | p = snewn(3 * strlen(sessionname) + 1, char); |
d1622aed |
137 | mungestr(sessionname, p); |
138 | |
139 | if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS) { |
140 | sesskey = NULL; |
141 | } else { |
142 | if (RegOpenKey(subkey1, p, &sesskey) != ERROR_SUCCESS) { |
143 | sesskey = NULL; |
144 | } |
145 | RegCloseKey(subkey1); |
146 | } |
147 | |
dcbde236 |
148 | sfree(p); |
d1622aed |
149 | |
32874aea |
150 | return (void *) sesskey; |
d1622aed |
151 | } |
152 | |
c85623f9 |
153 | char *read_setting_s(void *handle, const char *key, char *buffer, int buflen) |
32874aea |
154 | { |
d1622aed |
155 | DWORD type, size; |
156 | size = buflen; |
157 | |
158 | if (!handle || |
32874aea |
159 | RegQueryValueEx((HKEY) handle, key, 0, |
160 | &type, buffer, &size) != ERROR_SUCCESS || |
161 | type != REG_SZ) return NULL; |
d1622aed |
162 | else |
32874aea |
163 | return buffer; |
d1622aed |
164 | } |
d5859615 |
165 | |
c85623f9 |
166 | int read_setting_i(void *handle, const char *key, int defvalue) |
32874aea |
167 | { |
d1622aed |
168 | DWORD type, val, size; |
169 | size = sizeof(val); |
170 | |
171 | if (!handle || |
32874aea |
172 | RegQueryValueEx((HKEY) handle, key, 0, &type, |
e66c3912 |
173 | (BYTE *) &val, &size) != ERROR_SUCCESS || |
d1622aed |
174 | size != sizeof(val) || type != REG_DWORD) |
175 | return defvalue; |
176 | else |
177 | return val; |
178 | } |
179 | |
9a30e26b |
180 | int read_setting_fontspec(void *handle, const char *name, FontSpec *result) |
181 | { |
182 | char *settingname; |
183 | FontSpec ret; |
184 | |
185 | if (!read_setting_s(handle, name, ret.name, sizeof(ret.name))) |
186 | return 0; |
187 | settingname = dupcat(name, "IsBold", NULL); |
188 | ret.isbold = read_setting_i(handle, settingname, -1); |
189 | sfree(settingname); |
190 | if (ret.isbold == -1) return 0; |
191 | settingname = dupcat(name, "CharSet", NULL); |
192 | ret.charset = read_setting_i(handle, settingname, -1); |
193 | sfree(settingname); |
194 | if (ret.charset == -1) return 0; |
195 | settingname = dupcat(name, "Height", NULL); |
196 | ret.height = read_setting_i(handle, settingname, INT_MIN); |
197 | sfree(settingname); |
198 | if (ret.height == INT_MIN) return 0; |
9a30e26b |
199 | *result = ret; |
200 | return 1; |
201 | } |
202 | |
203 | void write_setting_fontspec(void *handle, const char *name, FontSpec font) |
204 | { |
205 | char *settingname; |
206 | |
207 | write_setting_s(handle, name, font.name); |
208 | settingname = dupcat(name, "IsBold", NULL); |
209 | write_setting_i(handle, settingname, font.isbold); |
210 | sfree(settingname); |
211 | settingname = dupcat(name, "CharSet", NULL); |
212 | write_setting_i(handle, settingname, font.charset); |
213 | sfree(settingname); |
214 | settingname = dupcat(name, "Height", NULL); |
215 | write_setting_i(handle, settingname, font.height); |
216 | sfree(settingname); |
217 | } |
218 | |
219 | int read_setting_filename(void *handle, const char *name, Filename *result) |
220 | { |
221 | return !!read_setting_s(handle, name, result->path, sizeof(result->path)); |
222 | } |
223 | |
224 | void write_setting_filename(void *handle, const char *name, Filename result) |
225 | { |
226 | write_setting_s(handle, name, result.path); |
227 | } |
228 | |
32874aea |
229 | void close_settings_r(void *handle) |
230 | { |
231 | RegCloseKey((HKEY) handle); |
d1622aed |
232 | } |
233 | |
c85623f9 |
234 | void del_settings(const char *sessionname) |
32874aea |
235 | { |
d1622aed |
236 | HKEY subkey1; |
237 | char *p; |
238 | |
239 | if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS) |
240 | return; |
241 | |
3d88e64d |
242 | p = snewn(3 * strlen(sessionname) + 1, char); |
d1622aed |
243 | mungestr(sessionname, p); |
244 | RegDeleteKey(subkey1, p); |
dcbde236 |
245 | sfree(p); |
d1622aed |
246 | |
247 | RegCloseKey(subkey1); |
073e9f42 |
248 | |
249 | remove_session_from_jumplist(sessionname); |
d1622aed |
250 | } |
d5859615 |
251 | |
d1622aed |
252 | struct enumsettings { |
253 | HKEY key; |
254 | int i; |
255 | }; |
256 | |
32874aea |
257 | void *enum_settings_start(void) |
258 | { |
d1622aed |
259 | struct enumsettings *ret; |
260 | HKEY key; |
261 | |
5f2df4d2 |
262 | if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &key) != ERROR_SUCCESS) |
32874aea |
263 | return NULL; |
d1622aed |
264 | |
3d88e64d |
265 | ret = snew(struct enumsettings); |
d1622aed |
266 | if (ret) { |
32874aea |
267 | ret->key = key; |
268 | ret->i = 0; |
d1622aed |
269 | } |
270 | |
271 | return ret; |
272 | } |
273 | |
32874aea |
274 | char *enum_settings_next(void *handle, char *buffer, int buflen) |
275 | { |
276 | struct enumsettings *e = (struct enumsettings *) handle; |
d1622aed |
277 | char *otherbuf; |
3d88e64d |
278 | otherbuf = snewn(3 * buflen, char); |
60371ab3 |
279 | if (RegEnumKey(e->key, e->i++, otherbuf, 3 * buflen) == ERROR_SUCCESS) { |
32874aea |
280 | unmungestr(otherbuf, buffer, buflen); |
281 | sfree(otherbuf); |
282 | return buffer; |
60371ab3 |
283 | } else { |
284 | sfree(otherbuf); |
32874aea |
285 | return NULL; |
60371ab3 |
286 | } |
d1622aed |
287 | } |
288 | |
32874aea |
289 | void enum_settings_finish(void *handle) |
290 | { |
291 | struct enumsettings *e = (struct enumsettings *) handle; |
d1622aed |
292 | RegCloseKey(e->key); |
dcbde236 |
293 | sfree(e); |
d1622aed |
294 | } |
295 | |
c85623f9 |
296 | static void hostkey_regname(char *buffer, const char *hostname, |
297 | int port, const char *keytype) |
32874aea |
298 | { |
d4857987 |
299 | int len; |
300 | strcpy(buffer, keytype); |
301 | strcat(buffer, "@"); |
302 | len = strlen(buffer); |
32874aea |
303 | len += sprintf(buffer + len, "%d:", port); |
d4857987 |
304 | mungestr(hostname, buffer + strlen(buffer)); |
305 | } |
306 | |
c85623f9 |
307 | int verify_host_key(const char *hostname, int port, |
308 | const char *keytype, const char *key) |
32874aea |
309 | { |
d5859615 |
310 | char *otherstr, *regname; |
311 | int len; |
312 | HKEY rkey; |
313 | DWORD readlen; |
314 | DWORD type; |
315 | int ret, compare; |
316 | |
317 | len = 1 + strlen(key); |
318 | |
319 | /* |
320 | * Now read a saved key in from the registry and see what it |
321 | * says. |
322 | */ |
3d88e64d |
323 | otherstr = snewn(len, char); |
324 | regname = snewn(3 * (strlen(hostname) + strlen(keytype)) + 15, char); |
d5859615 |
325 | |
d4857987 |
326 | hostkey_regname(regname, hostname, port, keytype); |
d5859615 |
327 | |
5f2df4d2 |
328 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys", |
329 | &rkey) != ERROR_SUCCESS) |
32874aea |
330 | return 1; /* key does not exist in registry */ |
d5859615 |
331 | |
332 | readlen = len; |
333 | ret = RegQueryValueEx(rkey, regname, NULL, &type, otherstr, &readlen); |
334 | |
335 | if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA && |
32874aea |
336 | !strcmp(keytype, "rsa")) { |
337 | /* |
338 | * Key didn't exist. If the key type is RSA, we'll try |
339 | * another trick, which is to look up the _old_ key format |
340 | * under just the hostname and translate that. |
341 | */ |
342 | char *justhost = regname + 1 + strcspn(regname, ":"); |
3d88e64d |
343 | char *oldstyle = snewn(len + 10, char); /* safety margin */ |
32874aea |
344 | readlen = len; |
345 | ret = RegQueryValueEx(rkey, justhost, NULL, &type, |
346 | oldstyle, &readlen); |
347 | |
348 | if (ret == ERROR_SUCCESS && type == REG_SZ) { |
349 | /* |
350 | * The old format is two old-style bignums separated by |
351 | * a slash. An old-style bignum is made of groups of |
352 | * four hex digits: digits are ordered in sensible |
353 | * (most to least significant) order within each group, |
354 | * but groups are ordered in silly (least to most) |
355 | * order within the bignum. The new format is two |
356 | * ordinary C-format hex numbers (0xABCDEFG...XYZ, with |
357 | * A nonzero except in the special case 0x0, which |
358 | * doesn't appear anyway in RSA keys) separated by a |
359 | * comma. All hex digits are lowercase in both formats. |
360 | */ |
361 | char *p = otherstr; |
362 | char *q = oldstyle; |
363 | int i, j; |
364 | |
365 | for (i = 0; i < 2; i++) { |
366 | int ndigits, nwords; |
367 | *p++ = '0'; |
368 | *p++ = 'x'; |
369 | ndigits = strcspn(q, "/"); /* find / or end of string */ |
370 | nwords = ndigits / 4; |
371 | /* now trim ndigits to remove leading zeros */ |
372 | while (q[(ndigits - 1) ^ 3] == '0' && ndigits > 1) |
373 | ndigits--; |
374 | /* now move digits over to new string */ |
375 | for (j = 0; j < ndigits; j++) |
376 | p[ndigits - 1 - j] = q[j ^ 3]; |
377 | p += ndigits; |
378 | q += nwords * 4; |
379 | if (*q) { |
380 | q++; /* eat the slash */ |
381 | *p++ = ','; /* add a comma */ |
382 | } |
383 | *p = '\0'; /* terminate the string */ |
384 | } |
385 | |
386 | /* |
387 | * Now _if_ this key matches, we'll enter it in the new |
388 | * format. If not, we'll assume something odd went |
389 | * wrong, and hyper-cautiously do nothing. |
390 | */ |
391 | if (!strcmp(otherstr, key)) |
392 | RegSetValueEx(rkey, regname, 0, REG_SZ, otherstr, |
393 | strlen(otherstr) + 1); |
394 | } |
d5859615 |
395 | } |
396 | |
397 | RegCloseKey(rkey); |
398 | |
399 | compare = strcmp(otherstr, key); |
400 | |
401 | sfree(otherstr); |
402 | sfree(regname); |
403 | |
404 | if (ret == ERROR_MORE_DATA || |
32874aea |
405 | (ret == ERROR_SUCCESS && type == REG_SZ && compare)) |
406 | return 2; /* key is different in registry */ |
d5859615 |
407 | else if (ret != ERROR_SUCCESS || type != REG_SZ) |
32874aea |
408 | return 1; /* key does not exist in registry */ |
d5859615 |
409 | else |
32874aea |
410 | return 0; /* key matched OK in registry */ |
d5859615 |
411 | } |
412 | |
c85623f9 |
413 | void store_host_key(const char *hostname, int port, |
414 | const char *keytype, const char *key) |
32874aea |
415 | { |
d5859615 |
416 | char *regname; |
417 | HKEY rkey; |
418 | |
3d88e64d |
419 | regname = snewn(3 * (strlen(hostname) + strlen(keytype)) + 15, char); |
d5859615 |
420 | |
d4857987 |
421 | hostkey_regname(regname, hostname, port, keytype); |
d5859615 |
422 | |
423 | if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys", |
fc2464c6 |
424 | &rkey) == ERROR_SUCCESS) { |
425 | RegSetValueEx(rkey, regname, 0, REG_SZ, key, strlen(key) + 1); |
426 | RegCloseKey(rkey); |
427 | } /* else key does not exist in registry */ |
428 | |
429 | sfree(regname); |
d5859615 |
430 | } |
431 | |
432 | /* |
7bccf0fe |
433 | * Open (or delete) the random seed file. |
d5859615 |
434 | */ |
7bccf0fe |
435 | enum { DEL, OPEN_R, OPEN_W }; |
436 | static int try_random_seed(char const *path, int action, HANDLE *ret) |
437 | { |
438 | if (action == DEL) { |
439 | remove(path); |
440 | *ret = INVALID_HANDLE_VALUE; |
441 | return FALSE; /* so we'll do the next ones too */ |
442 | } |
443 | |
444 | *ret = CreateFile(path, |
445 | action == OPEN_W ? GENERIC_WRITE : GENERIC_READ, |
446 | action == OPEN_W ? 0 : (FILE_SHARE_READ | |
447 | FILE_SHARE_WRITE), |
448 | NULL, |
449 | action == OPEN_W ? CREATE_ALWAYS : OPEN_EXISTING, |
450 | action == OPEN_W ? FILE_ATTRIBUTE_NORMAL : 0, |
451 | NULL); |
452 | |
453 | return (*ret != INVALID_HANDLE_VALUE); |
454 | } |
455 | |
456 | static HANDLE access_random_seed(int action) |
32874aea |
457 | { |
d5859615 |
458 | HKEY rkey; |
459 | DWORD type, size; |
7bccf0fe |
460 | HANDLE rethandle; |
461 | char seedpath[2 * MAX_PATH + 10] = "\0"; |
d5859615 |
462 | |
7bccf0fe |
463 | /* |
464 | * Iterate over a selection of possible random seed paths until |
465 | * we find one that works. |
466 | * |
467 | * We do this iteration separately for reading and writing, |
468 | * meaning that we will automatically migrate random seed files |
469 | * if a better location becomes available (by reading from the |
470 | * best location in which we actually find one, and then |
471 | * writing to the best location in which we can _create_ one). |
472 | */ |
d5859615 |
473 | |
7bccf0fe |
474 | /* |
475 | * First, try the location specified by the user in the |
476 | * Registry, if any. |
477 | */ |
478 | size = sizeof(seedpath); |
32874aea |
479 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &rkey) == |
480 | ERROR_SUCCESS) { |
d5859615 |
481 | int ret = RegQueryValueEx(rkey, "RandSeedFile", |
482 | 0, &type, seedpath, &size); |
483 | if (ret != ERROR_SUCCESS || type != REG_SZ) |
484 | seedpath[0] = '\0'; |
485 | RegCloseKey(rkey); |
d5859615 |
486 | |
7bccf0fe |
487 | if (*seedpath && try_random_seed(seedpath, action, &rethandle)) |
488 | return rethandle; |
489 | } |
490 | |
491 | /* |
492 | * Next, try the user's local Application Data directory, |
493 | * followed by their non-local one. This is found using the |
494 | * SHGetFolderPath function, which won't be present on all |
495 | * versions of Windows. |
496 | */ |
497 | if (!tried_shgetfolderpath) { |
8f5f26d2 |
498 | /* This is likely only to bear fruit on systems with IE5+ |
499 | * installed, or WinMe/2K+. There is some faffing with |
500 | * SHFOLDER.DLL we could do to try to find an equivalent |
501 | * on older versions of Windows if we cared enough. |
502 | * However, the invocation below requires IE5+ anyway, |
503 | * so stuff that. */ |
bda368a5 |
504 | shell32_module = load_system32_dll("shell32.dll"); |
9099600a |
505 | GET_WINDOWS_FUNCTION(shell32_module, SHGetFolderPathA); |
a6c15e85 |
506 | tried_shgetfolderpath = TRUE; |
7bccf0fe |
507 | } |
9099600a |
508 | if (p_SHGetFolderPathA) { |
509 | if (SUCCEEDED(p_SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, |
510 | NULL, SHGFP_TYPE_CURRENT, seedpath))) { |
7bccf0fe |
511 | strcat(seedpath, "\\PUTTY.RND"); |
512 | if (try_random_seed(seedpath, action, &rethandle)) |
513 | return rethandle; |
514 | } |
515 | |
9099600a |
516 | if (SUCCEEDED(p_SHGetFolderPathA(NULL, CSIDL_APPDATA, |
517 | NULL, SHGFP_TYPE_CURRENT, seedpath))) { |
7bccf0fe |
518 | strcat(seedpath, "\\PUTTY.RND"); |
519 | if (try_random_seed(seedpath, action, &rethandle)) |
520 | return rethandle; |
521 | } |
522 | } |
523 | |
524 | /* |
525 | * Failing that, try %HOMEDRIVE%%HOMEPATH% as a guess at the |
526 | * user's home directory. |
527 | */ |
528 | { |
d5859615 |
529 | int len, ret; |
530 | |
32874aea |
531 | len = |
532 | GetEnvironmentVariable("HOMEDRIVE", seedpath, |
533 | sizeof(seedpath)); |
534 | ret = |
535 | GetEnvironmentVariable("HOMEPATH", seedpath + len, |
536 | sizeof(seedpath) - len); |
7bccf0fe |
537 | if (ret != 0) { |
538 | strcat(seedpath, "\\PUTTY.RND"); |
539 | if (try_random_seed(seedpath, action, &rethandle)) |
540 | return rethandle; |
541 | } |
d5859615 |
542 | } |
7bccf0fe |
543 | |
544 | /* |
545 | * And finally, fall back to C:\WINDOWS. |
546 | */ |
547 | GetWindowsDirectory(seedpath, sizeof(seedpath)); |
548 | strcat(seedpath, "\\PUTTY.RND"); |
549 | if (try_random_seed(seedpath, action, &rethandle)) |
550 | return rethandle; |
551 | |
552 | /* |
553 | * If even that failed, give up. |
554 | */ |
555 | return INVALID_HANDLE_VALUE; |
d5859615 |
556 | } |
557 | |
32874aea |
558 | void read_random_seed(noise_consumer_t consumer) |
559 | { |
7bccf0fe |
560 | HANDLE seedf = access_random_seed(OPEN_R); |
d5859615 |
561 | |
562 | if (seedf != INVALID_HANDLE_VALUE) { |
563 | while (1) { |
564 | char buf[1024]; |
565 | DWORD len; |
566 | |
567 | if (ReadFile(seedf, buf, sizeof(buf), &len, NULL) && len) |
32874aea |
568 | consumer(buf, len); |
d5859615 |
569 | else |
570 | break; |
571 | } |
572 | CloseHandle(seedf); |
573 | } |
574 | } |
575 | |
32874aea |
576 | void write_random_seed(void *data, int len) |
577 | { |
7bccf0fe |
578 | HANDLE seedf = access_random_seed(OPEN_W); |
d5859615 |
579 | |
580 | if (seedf != INVALID_HANDLE_VALUE) { |
581 | DWORD lenwritten; |
582 | |
583 | WriteFile(seedf, data, len, &lenwritten, NULL); |
584 | CloseHandle(seedf); |
585 | } |
586 | } |
587 | |
588 | /* |
073e9f42 |
589 | * Internal function supporting the jump list registry code. All the |
590 | * functions to add, remove and read the list have substantially |
591 | * similar content, so this is a generalisation of all of them which |
592 | * transforms the list in the registry by prepending 'add' (if |
593 | * non-null), removing 'rem' from what's left (if non-null), and |
594 | * returning the resulting concatenated list of strings in 'out' (if |
595 | * non-null). |
596 | */ |
597 | static int transform_jumplist_registry |
598 | (const char *add, const char *rem, char **out) |
599 | { |
600 | int ret; |
601 | HKEY pjumplist_key, psettings_tmp; |
602 | DWORD type; |
603 | int value_length; |
604 | char *old_value, *new_value; |
605 | char *piterator_old, *piterator_new, *piterator_tmp; |
606 | |
607 | ret = RegCreateKeyEx(HKEY_CURRENT_USER, reg_jumplist_key, 0, NULL, |
608 | REG_OPTION_NON_VOLATILE, (KEY_READ | KEY_WRITE), NULL, |
609 | &pjumplist_key, NULL); |
610 | if (ret != ERROR_SUCCESS) { |
611 | return JUMPLISTREG_ERROR_KEYOPENCREATE_FAILURE; |
612 | } |
613 | |
614 | /* Get current list of saved sessions in the registry. */ |
615 | value_length = 200; |
616 | old_value = snewn(value_length, char); |
617 | ret = RegQueryValueEx(pjumplist_key, reg_jumplist_value, NULL, &type, |
618 | old_value, &value_length); |
619 | /* When the passed buffer is too small, ERROR_MORE_DATA is |
620 | * returned and the required size is returned in the length |
621 | * argument. */ |
622 | if (ret == ERROR_MORE_DATA) { |
623 | sfree(old_value); |
624 | old_value = snewn(value_length, char); |
625 | ret = RegQueryValueEx(pjumplist_key, reg_jumplist_value, NULL, &type, |
626 | old_value, &value_length); |
627 | } |
628 | |
629 | if (ret == ERROR_FILE_NOT_FOUND) { |
630 | /* Value doesn't exist yet. Start from an empty value. */ |
631 | *old_value = '\0'; |
632 | *(old_value + 1) = '\0'; |
633 | } else if (ret != ERROR_SUCCESS) { |
634 | /* Some non-recoverable error occurred. */ |
635 | sfree(old_value); |
636 | RegCloseKey(pjumplist_key); |
637 | return JUMPLISTREG_ERROR_VALUEREAD_FAILURE; |
638 | } else if (type != REG_MULTI_SZ) { |
639 | /* The value present in the registry has the wrong type: we |
640 | * try to delete it and start from an empty value. */ |
641 | ret = RegDeleteValue(pjumplist_key, reg_jumplist_value); |
642 | if (ret != ERROR_SUCCESS) { |
643 | sfree(old_value); |
644 | RegCloseKey(pjumplist_key); |
645 | return JUMPLISTREG_ERROR_VALUEREAD_FAILURE; |
646 | } |
647 | |
648 | *old_value = '\0'; |
649 | *(old_value + 1) = '\0'; |
650 | } |
651 | |
652 | /* Check validity of registry data: REG_MULTI_SZ value must end |
653 | * with \0\0. */ |
654 | piterator_tmp = old_value; |
655 | while (((piterator_tmp - old_value) < (value_length - 1)) && |
656 | !(*piterator_tmp == '\0' && *(piterator_tmp+1) == '\0')) { |
657 | ++piterator_tmp; |
658 | } |
659 | |
660 | if ((piterator_tmp - old_value) >= (value_length-1)) { |
661 | /* Invalid value. Start from an empty value. */ |
662 | *old_value = '\0'; |
663 | *(old_value + 1) = '\0'; |
664 | } |
665 | |
666 | /* |
667 | * Modify the list, if we're modifying. |
668 | */ |
669 | if (add || rem) { |
670 | /* Walk through the existing list and construct the new list of |
671 | * saved sessions. */ |
672 | new_value = snewn(value_length + (add ? strlen(add) + 1 : 0), char); |
673 | piterator_new = new_value; |
674 | piterator_old = old_value; |
675 | |
676 | /* First add the new item to the beginning of the list. */ |
677 | if (add) { |
678 | strcpy(piterator_new, add); |
679 | piterator_new += strlen(piterator_new) + 1; |
680 | } |
681 | /* Now add the existing list, taking care to leave out the removed |
682 | * item, if it was already in the existing list. */ |
683 | while (*piterator_old != '\0') { |
684 | if (!rem || strcmp(piterator_old, rem) != 0) { |
685 | /* Check if this is a valid session, otherwise don't add. */ |
686 | psettings_tmp = open_settings_r(piterator_old); |
687 | if (psettings_tmp != NULL) { |
688 | close_settings_r(psettings_tmp); |
689 | strcpy(piterator_new, piterator_old); |
690 | piterator_new += strlen(piterator_new) + 1; |
691 | } |
692 | } |
693 | piterator_old += strlen(piterator_old) + 1; |
694 | } |
695 | *piterator_new = '\0'; |
696 | ++piterator_new; |
697 | |
698 | /* Save the new list to the registry. */ |
699 | ret = RegSetValueEx(pjumplist_key, reg_jumplist_value, 0, REG_MULTI_SZ, |
700 | new_value, piterator_new - new_value); |
701 | |
5165dd4f |
702 | sfree(old_value); |
073e9f42 |
703 | old_value = new_value; |
073e9f42 |
704 | } else |
705 | ret = ERROR_SUCCESS; |
706 | |
707 | /* |
708 | * Either return or free the result. |
709 | */ |
710 | if (out) |
711 | *out = old_value; |
712 | else |
713 | sfree(old_value); |
714 | |
715 | /* Clean up and return. */ |
716 | RegCloseKey(pjumplist_key); |
717 | |
718 | if (ret != ERROR_SUCCESS) { |
719 | return JUMPLISTREG_ERROR_VALUEWRITE_FAILURE; |
720 | } else { |
721 | return JUMPLISTREG_OK; |
722 | } |
723 | } |
724 | |
725 | /* Adds a new entry to the jumplist entries in the registry. */ |
726 | int add_to_jumplist_registry(const char *item) |
727 | { |
728 | return transform_jumplist_registry(item, item, NULL); |
729 | } |
730 | |
731 | /* Removes an item from the jumplist entries in the registry. */ |
732 | int remove_from_jumplist_registry(const char *item) |
733 | { |
734 | return transform_jumplist_registry(NULL, item, NULL); |
735 | } |
736 | |
737 | /* Returns the jumplist entries from the registry. Caller must free |
738 | * the returned pointer. */ |
739 | char *get_jumplist_registry_entries (void) |
740 | { |
741 | char *list_value; |
742 | |
743 | if (transform_jumplist_registry(NULL,NULL,&list_value) != ERROR_SUCCESS) { |
744 | list_value = snewn(2, char); |
745 | *list_value = '\0'; |
746 | *(list_value + 1) = '\0'; |
747 | } |
748 | return list_value; |
749 | } |
750 | |
751 | /* |
d5859615 |
752 | * Recursively delete a registry key and everything under it. |
753 | */ |
32874aea |
754 | static void registry_recursive_remove(HKEY key) |
755 | { |
d5859615 |
756 | DWORD i; |
32874aea |
757 | char name[MAX_PATH + 1]; |
d5859615 |
758 | HKEY subkey; |
759 | |
760 | i = 0; |
761 | while (RegEnumKey(key, i, name, sizeof(name)) == ERROR_SUCCESS) { |
32874aea |
762 | if (RegOpenKey(key, name, &subkey) == ERROR_SUCCESS) { |
763 | registry_recursive_remove(subkey); |
764 | RegCloseKey(subkey); |
765 | } |
766 | RegDeleteKey(key, name); |
d5859615 |
767 | } |
768 | } |
769 | |
32874aea |
770 | void cleanup_all(void) |
771 | { |
d5859615 |
772 | HKEY key; |
773 | int ret; |
32874aea |
774 | char name[MAX_PATH + 1]; |
d5859615 |
775 | |
776 | /* ------------------------------------------------------------ |
7bccf0fe |
777 | * Wipe out the random seed file, in all of its possible |
778 | * locations. |
d5859615 |
779 | */ |
7bccf0fe |
780 | access_random_seed(DEL); |
d5859615 |
781 | |
782 | /* ------------------------------------------------------------ |
21cdf8c2 |
783 | * Ask Windows to delete any jump list information associated |
784 | * with this installation of PuTTY. |
785 | */ |
786 | clear_jumplist(); |
787 | |
788 | /* ------------------------------------------------------------ |
d5859615 |
789 | * Destroy all registry information associated with PuTTY. |
790 | */ |
791 | |
792 | /* |
793 | * Open the main PuTTY registry key and remove everything in it. |
794 | */ |
32874aea |
795 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &key) == |
796 | ERROR_SUCCESS) { |
797 | registry_recursive_remove(key); |
798 | RegCloseKey(key); |
d5859615 |
799 | } |
800 | /* |
801 | * Now open the parent key and remove the PuTTY main key. Once |
802 | * we've done that, see if the parent key has any other |
803 | * children. |
804 | */ |
805 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_PARENT, |
32874aea |
806 | &key) == ERROR_SUCCESS) { |
807 | RegDeleteKey(key, PUTTY_REG_PARENT_CHILD); |
808 | ret = RegEnumKey(key, 0, name, sizeof(name)); |
809 | RegCloseKey(key); |
810 | /* |
811 | * If the parent key had no other children, we must delete |
812 | * it in its turn. That means opening the _grandparent_ |
813 | * key. |
814 | */ |
815 | if (ret != ERROR_SUCCESS) { |
816 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_GPARENT, |
817 | &key) == ERROR_SUCCESS) { |
818 | RegDeleteKey(key, PUTTY_REG_GPARENT_CHILD); |
819 | RegCloseKey(key); |
820 | } |
821 | } |
d5859615 |
822 | } |
823 | /* |
824 | * Now we're done. |
825 | */ |
826 | } |