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