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