winstore.c 27 KB

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