winstore.c 24 KB

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