winstore.c 24 KB

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