storage.c 27 KB

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