storage.c 25 KB

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