winstore.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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 = 256;
  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. 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 HANDLE access_random_seed(int action)
  452. {
  453. HKEY rkey;
  454. DWORD type, size;
  455. HANDLE rethandle;
  456. char seedpath[2 * MAX_PATH + 10] = "\0";
  457. /*
  458. * Iterate over a selection of possible random seed paths until
  459. * we find one that works.
  460. *
  461. * We do this iteration separately for reading and writing,
  462. * meaning that we will automatically migrate random seed files
  463. * if a better location becomes available (by reading from the
  464. * best location in which we actually find one, and then
  465. * writing to the best location in which we can _create_ one).
  466. */
  467. /*
  468. * First, try the location specified by the user in the
  469. * Registry, if any.
  470. */
  471. size = sizeof(seedpath);
  472. if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &rkey) ==
  473. ERROR_SUCCESS) {
  474. int ret = RegQueryValueEx(rkey, "RandSeedFile",
  475. 0, &type, (BYTE *)seedpath, &size);
  476. if (ret != ERROR_SUCCESS || type != REG_SZ)
  477. seedpath[0] = '\0';
  478. RegCloseKey(rkey);
  479. if (*seedpath && try_random_seed(seedpath, action, &rethandle))
  480. return rethandle;
  481. }
  482. /*
  483. * Next, try the user's local Application Data directory,
  484. * followed by their non-local one. This is found using the
  485. * SHGetFolderPath function, which won't be present on all
  486. * versions of Windows.
  487. */
  488. if (!tried_shgetfolderpath) {
  489. /* This is likely only to bear fruit on systems with IE5+
  490. * installed, or WinMe/2K+. There is some faffing with
  491. * SHFOLDER.DLL we could do to try to find an equivalent
  492. * on older versions of Windows if we cared enough.
  493. * However, the invocation below requires IE5+ anyway,
  494. * so stuff that. */
  495. shell32_module = load_system32_dll("shell32.dll");
  496. GET_WINDOWS_FUNCTION(shell32_module, SHGetFolderPathA);
  497. tried_shgetfolderpath = true;
  498. }
  499. if (p_SHGetFolderPathA) {
  500. if (SUCCEEDED(p_SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA,
  501. NULL, SHGFP_TYPE_CURRENT, seedpath))) {
  502. strcat(seedpath, "\\PUTTY.RND");
  503. if (try_random_seed(seedpath, action, &rethandle))
  504. return rethandle;
  505. }
  506. if (SUCCEEDED(p_SHGetFolderPathA(NULL, CSIDL_APPDATA,
  507. NULL, SHGFP_TYPE_CURRENT, seedpath))) {
  508. strcat(seedpath, "\\PUTTY.RND");
  509. if (try_random_seed(seedpath, action, &rethandle))
  510. return rethandle;
  511. }
  512. }
  513. /*
  514. * Failing that, try %HOMEDRIVE%%HOMEPATH% as a guess at the
  515. * user's home directory.
  516. */
  517. {
  518. int len, ret;
  519. len =
  520. GetEnvironmentVariable("HOMEDRIVE", seedpath,
  521. sizeof(seedpath));
  522. ret =
  523. GetEnvironmentVariable("HOMEPATH", seedpath + len,
  524. sizeof(seedpath) - len);
  525. if (ret != 0) {
  526. strcat(seedpath, "\\PUTTY.RND");
  527. if (try_random_seed(seedpath, action, &rethandle))
  528. return rethandle;
  529. }
  530. }
  531. /*
  532. * And finally, fall back to C:\WINDOWS.
  533. */
  534. GetWindowsDirectory(seedpath, sizeof(seedpath));
  535. strcat(seedpath, "\\PUTTY.RND");
  536. if (try_random_seed(seedpath, action, &rethandle))
  537. return rethandle;
  538. /*
  539. * If even that failed, give up.
  540. */
  541. return INVALID_HANDLE_VALUE;
  542. }
  543. void read_random_seed(noise_consumer_t consumer)
  544. {
  545. HANDLE seedf = access_random_seed(OPEN_R);
  546. if (seedf != INVALID_HANDLE_VALUE) {
  547. while (1) {
  548. char buf[1024];
  549. DWORD len;
  550. if (ReadFile(seedf, buf, sizeof(buf), &len, NULL) && len)
  551. consumer(buf, len);
  552. else
  553. break;
  554. }
  555. CloseHandle(seedf);
  556. }
  557. }
  558. void write_random_seed(void *data, int len)
  559. {
  560. HANDLE seedf = access_random_seed(OPEN_W);
  561. if (seedf != INVALID_HANDLE_VALUE) {
  562. DWORD lenwritten;
  563. WriteFile(seedf, data, len, &lenwritten, NULL);
  564. CloseHandle(seedf);
  565. }
  566. }
  567. /*
  568. * Internal function supporting the jump list registry code. All the
  569. * functions to add, remove and read the list have substantially
  570. * similar content, so this is a generalisation of all of them which
  571. * transforms the list in the registry by prepending 'add' (if
  572. * non-null), removing 'rem' from what's left (if non-null), and
  573. * returning the resulting concatenated list of strings in 'out' (if
  574. * non-null).
  575. */
  576. static int transform_jumplist_registry
  577. (const char *add, const char *rem, char **out)
  578. {
  579. int ret;
  580. HKEY pjumplist_key;
  581. DWORD type;
  582. DWORD value_length;
  583. char *old_value, *new_value;
  584. char *piterator_old, *piterator_new, *piterator_tmp;
  585. ret = RegCreateKeyEx(HKEY_CURRENT_USER, reg_jumplist_key, 0, NULL,
  586. REG_OPTION_NON_VOLATILE, (KEY_READ | KEY_WRITE), NULL,
  587. &pjumplist_key, NULL);
  588. if (ret != ERROR_SUCCESS) {
  589. return JUMPLISTREG_ERROR_KEYOPENCREATE_FAILURE;
  590. }
  591. /* Get current list of saved sessions in the registry. */
  592. value_length = 200;
  593. old_value = snewn(value_length, char);
  594. ret = RegQueryValueEx(pjumplist_key, reg_jumplist_value, NULL, &type,
  595. (BYTE *)old_value, &value_length);
  596. /* When the passed buffer is too small, ERROR_MORE_DATA is
  597. * returned and the required size is returned in the length
  598. * argument. */
  599. if (ret == ERROR_MORE_DATA) {
  600. sfree(old_value);
  601. old_value = snewn(value_length, char);
  602. ret = RegQueryValueEx(pjumplist_key, reg_jumplist_value, NULL, &type,
  603. (BYTE *)old_value, &value_length);
  604. }
  605. if (ret == ERROR_FILE_NOT_FOUND) {
  606. /* Value doesn't exist yet. Start from an empty value. */
  607. *old_value = '\0';
  608. *(old_value + 1) = '\0';
  609. } else if (ret != ERROR_SUCCESS) {
  610. /* Some non-recoverable error occurred. */
  611. sfree(old_value);
  612. RegCloseKey(pjumplist_key);
  613. return JUMPLISTREG_ERROR_VALUEREAD_FAILURE;
  614. } else if (type != REG_MULTI_SZ) {
  615. /* The value present in the registry has the wrong type: we
  616. * try to delete it and start from an empty value. */
  617. ret = RegDeleteValue(pjumplist_key, reg_jumplist_value);
  618. if (ret != ERROR_SUCCESS) {
  619. sfree(old_value);
  620. RegCloseKey(pjumplist_key);
  621. return JUMPLISTREG_ERROR_VALUEREAD_FAILURE;
  622. }
  623. *old_value = '\0';
  624. *(old_value + 1) = '\0';
  625. }
  626. /* Check validity of registry data: REG_MULTI_SZ value must end
  627. * with \0\0. */
  628. piterator_tmp = old_value;
  629. while (((piterator_tmp - old_value) < (value_length - 1)) &&
  630. !(*piterator_tmp == '\0' && *(piterator_tmp+1) == '\0')) {
  631. ++piterator_tmp;
  632. }
  633. if ((piterator_tmp - old_value) >= (value_length-1)) {
  634. /* Invalid value. Start from an empty value. */
  635. *old_value = '\0';
  636. *(old_value + 1) = '\0';
  637. }
  638. /*
  639. * Modify the list, if we're modifying.
  640. */
  641. if (add || rem) {
  642. /* Walk through the existing list and construct the new list of
  643. * saved sessions. */
  644. new_value = snewn(value_length + (add ? strlen(add) + 1 : 0), char);
  645. piterator_new = new_value;
  646. piterator_old = old_value;
  647. /* First add the new item to the beginning of the list. */
  648. if (add) {
  649. strcpy(piterator_new, add);
  650. piterator_new += strlen(piterator_new) + 1;
  651. }
  652. /* Now add the existing list, taking care to leave out the removed
  653. * item, if it was already in the existing list. */
  654. while (*piterator_old != '\0') {
  655. if (!rem || strcmp(piterator_old, rem) != 0) {
  656. /* Check if this is a valid session, otherwise don't add. */
  657. settings_r *psettings_tmp = open_settings_r(piterator_old);
  658. if (psettings_tmp != NULL) {
  659. close_settings_r(psettings_tmp);
  660. strcpy(piterator_new, piterator_old);
  661. piterator_new += strlen(piterator_new) + 1;
  662. }
  663. }
  664. piterator_old += strlen(piterator_old) + 1;
  665. }
  666. *piterator_new = '\0';
  667. ++piterator_new;
  668. /* Save the new list to the registry. */
  669. ret = RegSetValueEx(pjumplist_key, reg_jumplist_value, 0, REG_MULTI_SZ,
  670. (BYTE *)new_value, piterator_new - new_value);
  671. sfree(old_value);
  672. old_value = new_value;
  673. } else
  674. ret = ERROR_SUCCESS;
  675. /*
  676. * Either return or free the result.
  677. */
  678. if (out && ret == ERROR_SUCCESS)
  679. *out = old_value;
  680. else
  681. sfree(old_value);
  682. /* Clean up and return. */
  683. RegCloseKey(pjumplist_key);
  684. if (ret != ERROR_SUCCESS) {
  685. return JUMPLISTREG_ERROR_VALUEWRITE_FAILURE;
  686. } else {
  687. return JUMPLISTREG_OK;
  688. }
  689. }
  690. /* Adds a new entry to the jumplist entries in the registry. */
  691. int add_to_jumplist_registry(const char *item)
  692. {
  693. return transform_jumplist_registry(item, item, NULL);
  694. }
  695. /* Removes an item from the jumplist entries in the registry. */
  696. int remove_from_jumplist_registry(const char *item)
  697. {
  698. return transform_jumplist_registry(NULL, item, NULL);
  699. }
  700. /* Returns the jumplist entries from the registry. Caller must free
  701. * the returned pointer. */
  702. char *get_jumplist_registry_entries (void)
  703. {
  704. char *list_value;
  705. if (transform_jumplist_registry(NULL,NULL,&list_value) != JUMPLISTREG_OK) {
  706. list_value = snewn(2, char);
  707. *list_value = '\0';
  708. *(list_value + 1) = '\0';
  709. }
  710. return list_value;
  711. }
  712. /*
  713. * Recursively delete a registry key and everything under it.
  714. */
  715. static void registry_recursive_remove(HKEY key)
  716. {
  717. DWORD i;
  718. char name[MAX_PATH + 1];
  719. HKEY subkey;
  720. i = 0;
  721. while (RegEnumKey(key, i, name, sizeof(name)) == ERROR_SUCCESS) {
  722. if (RegOpenKey(key, name, &subkey) == ERROR_SUCCESS) {
  723. registry_recursive_remove(subkey);
  724. RegCloseKey(subkey);
  725. }
  726. RegDeleteKey(key, name);
  727. }
  728. }
  729. void cleanup_all(void)
  730. {
  731. HKEY key;
  732. int ret;
  733. char name[MAX_PATH + 1];
  734. /* ------------------------------------------------------------
  735. * Wipe out the random seed file, in all of its possible
  736. * locations.
  737. */
  738. access_random_seed(DEL);
  739. /* ------------------------------------------------------------
  740. * Ask Windows to delete any jump list information associated
  741. * with this installation of PuTTY.
  742. */
  743. clear_jumplist();
  744. /* ------------------------------------------------------------
  745. * Destroy all registry information associated with PuTTY.
  746. */
  747. /*
  748. * Open the main PuTTY registry key and remove everything in it.
  749. */
  750. if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &key) ==
  751. ERROR_SUCCESS) {
  752. registry_recursive_remove(key);
  753. RegCloseKey(key);
  754. }
  755. /*
  756. * Now open the parent key and remove the PuTTY main key. Once
  757. * we've done that, see if the parent key has any other
  758. * children.
  759. */
  760. if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_PARENT,
  761. &key) == ERROR_SUCCESS) {
  762. RegDeleteKey(key, PUTTY_REG_PARENT_CHILD);
  763. ret = RegEnumKey(key, 0, name, sizeof(name));
  764. RegCloseKey(key);
  765. /*
  766. * If the parent key had no other children, we must delete
  767. * it in its turn. That means opening the _grandparent_
  768. * key.
  769. */
  770. if (ret != ERROR_SUCCESS) {
  771. if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_GPARENT,
  772. &key) == ERROR_SUCCESS) {
  773. RegDeleteKey(key, PUTTY_REG_GPARENT_CHILD);
  774. RegCloseKey(key);
  775. }
  776. }
  777. }
  778. /*
  779. * Now we're done.
  780. */
  781. }