winstore.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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", NULL);
  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", NULL);
  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", NULL);
  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", NULL);
  177. write_setting_i(handle, settingname, font->isbold);
  178. sfree(settingname);
  179. settingname = dupcat(name, "CharSet", NULL);
  180. write_setting_i(handle, settingname, font->charset);
  181. sfree(settingname);
  182. settingname = dupcat(name, "Height", NULL);
  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. (const char *)NULL),
  473. action, &rethandle))
  474. return rethandle;
  475. if (SUCCEEDED(p_SHGetFolderPathA(NULL, CSIDL_APPDATA,
  476. NULL, SHGFP_TYPE_CURRENT, profile)) &&
  477. try_random_seed_and_free(dupcat(profile, "\\PUTTY.RND",
  478. (const char *)NULL),
  479. action, &rethandle))
  480. return rethandle;
  481. }
  482. /*
  483. * Failing that, try %HOMEDRIVE%%HOMEPATH% as a guess at the
  484. * user's home directory.
  485. */
  486. {
  487. char drv[MAX_PATH], path[MAX_PATH];
  488. DWORD drvlen = GetEnvironmentVariable("HOMEDRIVE", drv, sizeof(drv));
  489. DWORD pathlen = GetEnvironmentVariable("HOMEPATH", path, sizeof(path));
  490. /* We permit %HOMEDRIVE% to expand to an empty string, but if
  491. * %HOMEPATH% does that, we abort the attempt. Same if either
  492. * variable overflows its buffer. */
  493. if (drvlen == 0)
  494. drv[0] = '\0';
  495. if (drvlen < lenof(drv) && pathlen < lenof(path) && pathlen > 0 &&
  496. try_random_seed_and_free(
  497. dupcat(drv, path, "\\PUTTY.RND", (const char *)NULL),
  498. action, &rethandle))
  499. return rethandle;
  500. }
  501. /*
  502. * And finally, fall back to C:\WINDOWS.
  503. */
  504. {
  505. char windir[MAX_PATH];
  506. DWORD len = GetWindowsDirectory(windir, sizeof(windir));
  507. if (len < lenof(windir) &&
  508. try_random_seed_and_free(
  509. dupcat(windir, "\\PUTTY.RND", (const char *)NULL),
  510. action, &rethandle))
  511. return rethandle;
  512. }
  513. /*
  514. * If even that failed, give up.
  515. */
  516. return INVALID_HANDLE_VALUE;
  517. }
  518. void read_random_seed(noise_consumer_t consumer)
  519. {
  520. HANDLE seedf = access_random_seed(OPEN_R);
  521. if (seedf != INVALID_HANDLE_VALUE) {
  522. while (1) {
  523. char buf[1024];
  524. DWORD len;
  525. if (ReadFile(seedf, buf, sizeof(buf), &len, NULL) && len)
  526. consumer(buf, len);
  527. else
  528. break;
  529. }
  530. CloseHandle(seedf);
  531. }
  532. }
  533. void write_random_seed(void *data, int len)
  534. {
  535. HANDLE seedf = access_random_seed(OPEN_W);
  536. if (seedf != INVALID_HANDLE_VALUE) {
  537. DWORD lenwritten;
  538. WriteFile(seedf, data, len, &lenwritten, NULL);
  539. CloseHandle(seedf);
  540. }
  541. }
  542. /*
  543. * Internal function supporting the jump list registry code. All the
  544. * functions to add, remove and read the list have substantially
  545. * similar content, so this is a generalisation of all of them which
  546. * transforms the list in the registry by prepending 'add' (if
  547. * non-null), removing 'rem' from what's left (if non-null), and
  548. * returning the resulting concatenated list of strings in 'out' (if
  549. * non-null).
  550. */
  551. static int transform_jumplist_registry
  552. (const char *add, const char *rem, char **out)
  553. {
  554. int ret;
  555. HKEY pjumplist_key;
  556. DWORD type;
  557. DWORD value_length;
  558. char *old_value, *new_value;
  559. char *piterator_old, *piterator_new, *piterator_tmp;
  560. ret = RegCreateKeyEx(HKEY_CURRENT_USER, reg_jumplist_key, 0, NULL,
  561. REG_OPTION_NON_VOLATILE, (KEY_READ | KEY_WRITE), NULL,
  562. &pjumplist_key, NULL);
  563. if (ret != ERROR_SUCCESS) {
  564. return JUMPLISTREG_ERROR_KEYOPENCREATE_FAILURE;
  565. }
  566. /* Get current list of saved sessions in the registry. */
  567. value_length = 200;
  568. old_value = snewn(value_length, char);
  569. ret = RegQueryValueEx(pjumplist_key, reg_jumplist_value, NULL, &type,
  570. (BYTE *)old_value, &value_length);
  571. /* When the passed buffer is too small, ERROR_MORE_DATA is
  572. * returned and the required size is returned in the length
  573. * argument. */
  574. if (ret == ERROR_MORE_DATA) {
  575. sfree(old_value);
  576. old_value = snewn(value_length, char);
  577. ret = RegQueryValueEx(pjumplist_key, reg_jumplist_value, NULL, &type,
  578. (BYTE *)old_value, &value_length);
  579. }
  580. if (ret == ERROR_FILE_NOT_FOUND) {
  581. /* Value doesn't exist yet. Start from an empty value. */
  582. *old_value = '\0';
  583. *(old_value + 1) = '\0';
  584. } else if (ret != ERROR_SUCCESS) {
  585. /* Some non-recoverable error occurred. */
  586. sfree(old_value);
  587. RegCloseKey(pjumplist_key);
  588. return JUMPLISTREG_ERROR_VALUEREAD_FAILURE;
  589. } else if (type != REG_MULTI_SZ) {
  590. /* The value present in the registry has the wrong type: we
  591. * try to delete it and start from an empty value. */
  592. ret = RegDeleteValue(pjumplist_key, reg_jumplist_value);
  593. if (ret != ERROR_SUCCESS) {
  594. sfree(old_value);
  595. RegCloseKey(pjumplist_key);
  596. return JUMPLISTREG_ERROR_VALUEREAD_FAILURE;
  597. }
  598. *old_value = '\0';
  599. *(old_value + 1) = '\0';
  600. }
  601. /* Check validity of registry data: REG_MULTI_SZ value must end
  602. * with \0\0. */
  603. piterator_tmp = old_value;
  604. while (((piterator_tmp - old_value) < (value_length - 1)) &&
  605. !(*piterator_tmp == '\0' && *(piterator_tmp+1) == '\0')) {
  606. ++piterator_tmp;
  607. }
  608. if ((piterator_tmp - old_value) >= (value_length-1)) {
  609. /* Invalid value. Start from an empty value. */
  610. *old_value = '\0';
  611. *(old_value + 1) = '\0';
  612. }
  613. /*
  614. * Modify the list, if we're modifying.
  615. */
  616. if (add || rem) {
  617. /* Walk through the existing list and construct the new list of
  618. * saved sessions. */
  619. new_value = snewn(value_length + (add ? strlen(add) + 1 : 0), char);
  620. piterator_new = new_value;
  621. piterator_old = old_value;
  622. /* First add the new item to the beginning of the list. */
  623. if (add) {
  624. strcpy(piterator_new, add);
  625. piterator_new += strlen(piterator_new) + 1;
  626. }
  627. /* Now add the existing list, taking care to leave out the removed
  628. * item, if it was already in the existing list. */
  629. while (*piterator_old != '\0') {
  630. if (!rem || strcmp(piterator_old, rem) != 0) {
  631. /* Check if this is a valid session, otherwise don't add. */
  632. settings_r *psettings_tmp = open_settings_r(piterator_old);
  633. if (psettings_tmp != NULL) {
  634. close_settings_r(psettings_tmp);
  635. strcpy(piterator_new, piterator_old);
  636. piterator_new += strlen(piterator_new) + 1;
  637. }
  638. }
  639. piterator_old += strlen(piterator_old) + 1;
  640. }
  641. *piterator_new = '\0';
  642. ++piterator_new;
  643. /* Save the new list to the registry. */
  644. ret = RegSetValueEx(pjumplist_key, reg_jumplist_value, 0, REG_MULTI_SZ,
  645. (BYTE *)new_value, piterator_new - new_value);
  646. sfree(old_value);
  647. old_value = new_value;
  648. } else
  649. ret = ERROR_SUCCESS;
  650. /*
  651. * Either return or free the result.
  652. */
  653. if (out && ret == ERROR_SUCCESS)
  654. *out = old_value;
  655. else
  656. sfree(old_value);
  657. /* Clean up and return. */
  658. RegCloseKey(pjumplist_key);
  659. if (ret != ERROR_SUCCESS) {
  660. return JUMPLISTREG_ERROR_VALUEWRITE_FAILURE;
  661. } else {
  662. return JUMPLISTREG_OK;
  663. }
  664. }
  665. /* Adds a new entry to the jumplist entries in the registry. */
  666. int add_to_jumplist_registry(const char *item)
  667. {
  668. return transform_jumplist_registry(item, item, NULL);
  669. }
  670. /* Removes an item from the jumplist entries in the registry. */
  671. int remove_from_jumplist_registry(const char *item)
  672. {
  673. return transform_jumplist_registry(NULL, item, NULL);
  674. }
  675. /* Returns the jumplist entries from the registry. Caller must free
  676. * the returned pointer. */
  677. char *get_jumplist_registry_entries (void)
  678. {
  679. char *list_value;
  680. if (transform_jumplist_registry(NULL,NULL,&list_value) != JUMPLISTREG_OK) {
  681. list_value = snewn(2, char);
  682. *list_value = '\0';
  683. *(list_value + 1) = '\0';
  684. }
  685. return list_value;
  686. }
  687. /*
  688. * Recursively delete a registry key and everything under it.
  689. */
  690. static void registry_recursive_remove(HKEY key)
  691. {
  692. DWORD i;
  693. char name[MAX_PATH + 1];
  694. HKEY subkey;
  695. i = 0;
  696. while (RegEnumKey(key, i, name, sizeof(name)) == ERROR_SUCCESS) {
  697. if (RegOpenKey(key, name, &subkey) == ERROR_SUCCESS) {
  698. registry_recursive_remove(subkey);
  699. RegCloseKey(subkey);
  700. }
  701. RegDeleteKey(key, name);
  702. }
  703. }
  704. void cleanup_all(void)
  705. {
  706. HKEY key;
  707. int ret;
  708. char name[MAX_PATH + 1];
  709. /* ------------------------------------------------------------
  710. * Wipe out the random seed file, in all of its possible
  711. * locations.
  712. */
  713. access_random_seed(DEL);
  714. /* ------------------------------------------------------------
  715. * Ask Windows to delete any jump list information associated
  716. * with this installation of PuTTY.
  717. */
  718. clear_jumplist();
  719. /* ------------------------------------------------------------
  720. * Destroy all registry information associated with PuTTY.
  721. */
  722. /*
  723. * Open the main PuTTY registry key and remove everything in it.
  724. */
  725. if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &key) ==
  726. ERROR_SUCCESS) {
  727. registry_recursive_remove(key);
  728. RegCloseKey(key);
  729. }
  730. /*
  731. * Now open the parent key and remove the PuTTY main key. Once
  732. * we've done that, see if the parent key has any other
  733. * children.
  734. */
  735. if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_PARENT,
  736. &key) == ERROR_SUCCESS) {
  737. RegDeleteKey(key, PUTTY_REG_PARENT_CHILD);
  738. ret = RegEnumKey(key, 0, name, sizeof(name));
  739. RegCloseKey(key);
  740. /*
  741. * If the parent key had no other children, we must delete
  742. * it in its turn. That means opening the _grandparent_
  743. * key.
  744. */
  745. if (ret != ERROR_SUCCESS) {
  746. if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_GPARENT,
  747. &key) == ERROR_SUCCESS) {
  748. RegDeleteKey(key, PUTTY_REG_GPARENT_CHILD);
  749. RegCloseKey(key);
  750. }
  751. }
  752. }
  753. /*
  754. * Now we're done.
  755. */
  756. }