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