storage.c 26 KB

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