winstore.c 24 KB

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