winstore.c 26 KB

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