winstore.c 25 KB

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