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