winstore.c 28 KB

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