winstore.c 26 KB

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