| 1 | /* |
| 2 | * winstore.c: Windows-specific implementation of the interface |
| 3 | * defined in storage.h. |
| 4 | */ |
| 5 | |
| 6 | #include <windows.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <limits.h> |
| 10 | #include "putty.h" |
| 11 | #include "storage.h" |
| 12 | |
| 13 | static const char *const puttystr = PUTTY_REG_POS "\\Sessions"; |
| 14 | |
| 15 | static char seedpath[2 * MAX_PATH + 10] = "\0"; |
| 16 | |
| 17 | static const char hex[16] = "0123456789ABCDEF"; |
| 18 | |
| 19 | static void mungestr(const char *in, char *out) |
| 20 | { |
| 21 | int candot = 0; |
| 22 | |
| 23 | while (*in) { |
| 24 | if (*in == ' ' || *in == '\\' || *in == '*' || *in == '?' || |
| 25 | *in == '%' || *in < ' ' || *in > '~' || (*in == '.' |
| 26 | && !candot)) { |
| 27 | *out++ = '%'; |
| 28 | *out++ = hex[((unsigned char) *in) >> 4]; |
| 29 | *out++ = hex[((unsigned char) *in) & 15]; |
| 30 | } else |
| 31 | *out++ = *in; |
| 32 | in++; |
| 33 | candot = 1; |
| 34 | } |
| 35 | *out = '\0'; |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | static void unmungestr(const char *in, char *out, int outlen) |
| 40 | { |
| 41 | while (*in) { |
| 42 | if (*in == '%' && in[1] && in[2]) { |
| 43 | int i, j; |
| 44 | |
| 45 | i = in[1] - '0'; |
| 46 | i -= (i > 9 ? 7 : 0); |
| 47 | j = in[2] - '0'; |
| 48 | j -= (j > 9 ? 7 : 0); |
| 49 | |
| 50 | *out++ = (i << 4) + j; |
| 51 | if (!--outlen) |
| 52 | return; |
| 53 | in += 3; |
| 54 | } else { |
| 55 | *out++ = *in++; |
| 56 | if (!--outlen) |
| 57 | return; |
| 58 | } |
| 59 | } |
| 60 | *out = '\0'; |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | void *open_settings_w(const char *sessionname) |
| 65 | { |
| 66 | HKEY subkey1, sesskey; |
| 67 | int ret; |
| 68 | char *p; |
| 69 | |
| 70 | if (!sessionname || !*sessionname) |
| 71 | sessionname = "Default Settings"; |
| 72 | |
| 73 | p = smalloc(3 * strlen(sessionname) + 1); |
| 74 | mungestr(sessionname, p); |
| 75 | |
| 76 | ret = RegCreateKey(HKEY_CURRENT_USER, puttystr, &subkey1); |
| 77 | if (ret != ERROR_SUCCESS) { |
| 78 | sfree(p); |
| 79 | return NULL; |
| 80 | } |
| 81 | ret = RegCreateKey(subkey1, p, &sesskey); |
| 82 | sfree(p); |
| 83 | RegCloseKey(subkey1); |
| 84 | if (ret != ERROR_SUCCESS) |
| 85 | return NULL; |
| 86 | return (void *) sesskey; |
| 87 | } |
| 88 | |
| 89 | void write_setting_s(void *handle, const char *key, const char *value) |
| 90 | { |
| 91 | if (handle) |
| 92 | RegSetValueEx((HKEY) handle, key, 0, REG_SZ, value, |
| 93 | 1 + strlen(value)); |
| 94 | } |
| 95 | |
| 96 | void write_setting_i(void *handle, const char *key, int value) |
| 97 | { |
| 98 | if (handle) |
| 99 | RegSetValueEx((HKEY) handle, key, 0, REG_DWORD, |
| 100 | (CONST BYTE *) & value, sizeof(value)); |
| 101 | } |
| 102 | |
| 103 | void close_settings_w(void *handle) |
| 104 | { |
| 105 | RegCloseKey((HKEY) handle); |
| 106 | } |
| 107 | |
| 108 | void *open_settings_r(const char *sessionname) |
| 109 | { |
| 110 | HKEY subkey1, sesskey; |
| 111 | char *p; |
| 112 | |
| 113 | if (!sessionname || !*sessionname) |
| 114 | sessionname = "Default Settings"; |
| 115 | |
| 116 | p = smalloc(3 * strlen(sessionname) + 1); |
| 117 | mungestr(sessionname, p); |
| 118 | |
| 119 | if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS) { |
| 120 | sesskey = NULL; |
| 121 | } else { |
| 122 | if (RegOpenKey(subkey1, p, &sesskey) != ERROR_SUCCESS) { |
| 123 | sesskey = NULL; |
| 124 | } |
| 125 | RegCloseKey(subkey1); |
| 126 | } |
| 127 | |
| 128 | sfree(p); |
| 129 | |
| 130 | return (void *) sesskey; |
| 131 | } |
| 132 | |
| 133 | char *read_setting_s(void *handle, const char *key, char *buffer, int buflen) |
| 134 | { |
| 135 | DWORD type, size; |
| 136 | size = buflen; |
| 137 | |
| 138 | if (!handle || |
| 139 | RegQueryValueEx((HKEY) handle, key, 0, |
| 140 | &type, buffer, &size) != ERROR_SUCCESS || |
| 141 | type != REG_SZ) return NULL; |
| 142 | else |
| 143 | return buffer; |
| 144 | } |
| 145 | |
| 146 | int read_setting_i(void *handle, const char *key, int defvalue) |
| 147 | { |
| 148 | DWORD type, val, size; |
| 149 | size = sizeof(val); |
| 150 | |
| 151 | if (!handle || |
| 152 | RegQueryValueEx((HKEY) handle, key, 0, &type, |
| 153 | (BYTE *) & val, &size) != ERROR_SUCCESS || |
| 154 | size != sizeof(val) || type != REG_DWORD) |
| 155 | return defvalue; |
| 156 | else |
| 157 | return val; |
| 158 | } |
| 159 | |
| 160 | int read_setting_fontspec(void *handle, const char *name, FontSpec *result) |
| 161 | { |
| 162 | char *settingname; |
| 163 | FontSpec ret; |
| 164 | |
| 165 | if (!read_setting_s(handle, name, ret.name, sizeof(ret.name))) |
| 166 | return 0; |
| 167 | settingname = dupcat(name, "IsBold", NULL); |
| 168 | ret.isbold = read_setting_i(handle, settingname, -1); |
| 169 | sfree(settingname); |
| 170 | if (ret.isbold == -1) return 0; |
| 171 | settingname = dupcat(name, "CharSet", NULL); |
| 172 | ret.charset = read_setting_i(handle, settingname, -1); |
| 173 | sfree(settingname); |
| 174 | if (ret.charset == -1) return 0; |
| 175 | settingname = dupcat(name, "Height", NULL); |
| 176 | ret.height = read_setting_i(handle, settingname, INT_MIN); |
| 177 | sfree(settingname); |
| 178 | if (ret.height == INT_MIN) return 0; |
| 179 | if (ret.height < 0) { |
| 180 | int oldh, newh; |
| 181 | HDC hdc = GetDC(NULL); |
| 182 | int logpix = GetDeviceCaps(hdc, LOGPIXELSY); |
| 183 | ReleaseDC(NULL, hdc); |
| 184 | |
| 185 | oldh = -ret.height; |
| 186 | newh = MulDiv(oldh, 72, logpix) + 1; |
| 187 | if (MulDiv(newh, logpix, 72) > oldh) |
| 188 | newh--; |
| 189 | ret.height = newh; |
| 190 | } |
| 191 | *result = ret; |
| 192 | return 1; |
| 193 | } |
| 194 | |
| 195 | void write_setting_fontspec(void *handle, const char *name, FontSpec font) |
| 196 | { |
| 197 | char *settingname; |
| 198 | |
| 199 | write_setting_s(handle, name, font.name); |
| 200 | settingname = dupcat(name, "IsBold", NULL); |
| 201 | write_setting_i(handle, settingname, font.isbold); |
| 202 | sfree(settingname); |
| 203 | settingname = dupcat(name, "CharSet", NULL); |
| 204 | write_setting_i(handle, settingname, font.charset); |
| 205 | sfree(settingname); |
| 206 | settingname = dupcat(name, "Height", NULL); |
| 207 | write_setting_i(handle, settingname, font.height); |
| 208 | sfree(settingname); |
| 209 | } |
| 210 | |
| 211 | int read_setting_filename(void *handle, const char *name, Filename *result) |
| 212 | { |
| 213 | return !!read_setting_s(handle, name, result->path, sizeof(result->path)); |
| 214 | } |
| 215 | |
| 216 | void write_setting_filename(void *handle, const char *name, Filename result) |
| 217 | { |
| 218 | write_setting_s(handle, name, result.path); |
| 219 | } |
| 220 | |
| 221 | void close_settings_r(void *handle) |
| 222 | { |
| 223 | RegCloseKey((HKEY) handle); |
| 224 | } |
| 225 | |
| 226 | void del_settings(const char *sessionname) |
| 227 | { |
| 228 | HKEY subkey1; |
| 229 | char *p; |
| 230 | |
| 231 | if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS) |
| 232 | return; |
| 233 | |
| 234 | p = smalloc(3 * strlen(sessionname) + 1); |
| 235 | mungestr(sessionname, p); |
| 236 | RegDeleteKey(subkey1, p); |
| 237 | sfree(p); |
| 238 | |
| 239 | RegCloseKey(subkey1); |
| 240 | } |
| 241 | |
| 242 | struct enumsettings { |
| 243 | HKEY key; |
| 244 | int i; |
| 245 | }; |
| 246 | |
| 247 | void *enum_settings_start(void) |
| 248 | { |
| 249 | struct enumsettings *ret; |
| 250 | HKEY key; |
| 251 | |
| 252 | if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &key) != ERROR_SUCCESS) |
| 253 | return NULL; |
| 254 | |
| 255 | ret = smalloc(sizeof(*ret)); |
| 256 | if (ret) { |
| 257 | ret->key = key; |
| 258 | ret->i = 0; |
| 259 | } |
| 260 | |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | char *enum_settings_next(void *handle, char *buffer, int buflen) |
| 265 | { |
| 266 | struct enumsettings *e = (struct enumsettings *) handle; |
| 267 | char *otherbuf; |
| 268 | otherbuf = smalloc(3 * buflen); |
| 269 | if (RegEnumKey(e->key, e->i++, otherbuf, 3 * buflen) == ERROR_SUCCESS) { |
| 270 | unmungestr(otherbuf, buffer, buflen); |
| 271 | sfree(otherbuf); |
| 272 | return buffer; |
| 273 | } else { |
| 274 | sfree(otherbuf); |
| 275 | return NULL; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | void enum_settings_finish(void *handle) |
| 280 | { |
| 281 | struct enumsettings *e = (struct enumsettings *) handle; |
| 282 | RegCloseKey(e->key); |
| 283 | sfree(e); |
| 284 | } |
| 285 | |
| 286 | static void hostkey_regname(char *buffer, const char *hostname, |
| 287 | int port, const char *keytype) |
| 288 | { |
| 289 | int len; |
| 290 | strcpy(buffer, keytype); |
| 291 | strcat(buffer, "@"); |
| 292 | len = strlen(buffer); |
| 293 | len += sprintf(buffer + len, "%d:", port); |
| 294 | mungestr(hostname, buffer + strlen(buffer)); |
| 295 | } |
| 296 | |
| 297 | int verify_host_key(const char *hostname, int port, |
| 298 | const char *keytype, const char *key) |
| 299 | { |
| 300 | char *otherstr, *regname; |
| 301 | int len; |
| 302 | HKEY rkey; |
| 303 | DWORD readlen; |
| 304 | DWORD type; |
| 305 | int ret, compare; |
| 306 | |
| 307 | len = 1 + strlen(key); |
| 308 | |
| 309 | /* |
| 310 | * Now read a saved key in from the registry and see what it |
| 311 | * says. |
| 312 | */ |
| 313 | otherstr = smalloc(len); |
| 314 | regname = smalloc(3 * (strlen(hostname) + strlen(keytype)) + 15); |
| 315 | |
| 316 | hostkey_regname(regname, hostname, port, keytype); |
| 317 | |
| 318 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys", |
| 319 | &rkey) != ERROR_SUCCESS) |
| 320 | return 1; /* key does not exist in registry */ |
| 321 | |
| 322 | readlen = len; |
| 323 | ret = RegQueryValueEx(rkey, regname, NULL, &type, otherstr, &readlen); |
| 324 | |
| 325 | if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA && |
| 326 | !strcmp(keytype, "rsa")) { |
| 327 | /* |
| 328 | * Key didn't exist. If the key type is RSA, we'll try |
| 329 | * another trick, which is to look up the _old_ key format |
| 330 | * under just the hostname and translate that. |
| 331 | */ |
| 332 | char *justhost = regname + 1 + strcspn(regname, ":"); |
| 333 | char *oldstyle = smalloc(len + 10); /* safety margin */ |
| 334 | readlen = len; |
| 335 | ret = RegQueryValueEx(rkey, justhost, NULL, &type, |
| 336 | oldstyle, &readlen); |
| 337 | |
| 338 | if (ret == ERROR_SUCCESS && type == REG_SZ) { |
| 339 | /* |
| 340 | * The old format is two old-style bignums separated by |
| 341 | * a slash. An old-style bignum is made of groups of |
| 342 | * four hex digits: digits are ordered in sensible |
| 343 | * (most to least significant) order within each group, |
| 344 | * but groups are ordered in silly (least to most) |
| 345 | * order within the bignum. The new format is two |
| 346 | * ordinary C-format hex numbers (0xABCDEFG...XYZ, with |
| 347 | * A nonzero except in the special case 0x0, which |
| 348 | * doesn't appear anyway in RSA keys) separated by a |
| 349 | * comma. All hex digits are lowercase in both formats. |
| 350 | */ |
| 351 | char *p = otherstr; |
| 352 | char *q = oldstyle; |
| 353 | int i, j; |
| 354 | |
| 355 | for (i = 0; i < 2; i++) { |
| 356 | int ndigits, nwords; |
| 357 | *p++ = '0'; |
| 358 | *p++ = 'x'; |
| 359 | ndigits = strcspn(q, "/"); /* find / or end of string */ |
| 360 | nwords = ndigits / 4; |
| 361 | /* now trim ndigits to remove leading zeros */ |
| 362 | while (q[(ndigits - 1) ^ 3] == '0' && ndigits > 1) |
| 363 | ndigits--; |
| 364 | /* now move digits over to new string */ |
| 365 | for (j = 0; j < ndigits; j++) |
| 366 | p[ndigits - 1 - j] = q[j ^ 3]; |
| 367 | p += ndigits; |
| 368 | q += nwords * 4; |
| 369 | if (*q) { |
| 370 | q++; /* eat the slash */ |
| 371 | *p++ = ','; /* add a comma */ |
| 372 | } |
| 373 | *p = '\0'; /* terminate the string */ |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * Now _if_ this key matches, we'll enter it in the new |
| 378 | * format. If not, we'll assume something odd went |
| 379 | * wrong, and hyper-cautiously do nothing. |
| 380 | */ |
| 381 | if (!strcmp(otherstr, key)) |
| 382 | RegSetValueEx(rkey, regname, 0, REG_SZ, otherstr, |
| 383 | strlen(otherstr) + 1); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | RegCloseKey(rkey); |
| 388 | |
| 389 | compare = strcmp(otherstr, key); |
| 390 | |
| 391 | sfree(otherstr); |
| 392 | sfree(regname); |
| 393 | |
| 394 | if (ret == ERROR_MORE_DATA || |
| 395 | (ret == ERROR_SUCCESS && type == REG_SZ && compare)) |
| 396 | return 2; /* key is different in registry */ |
| 397 | else if (ret != ERROR_SUCCESS || type != REG_SZ) |
| 398 | return 1; /* key does not exist in registry */ |
| 399 | else |
| 400 | return 0; /* key matched OK in registry */ |
| 401 | } |
| 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 | |
| 409 | regname = smalloc(3 * (strlen(hostname) + strlen(keytype)) + 15); |
| 410 | |
| 411 | hostkey_regname(regname, hostname, port, keytype); |
| 412 | |
| 413 | if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys", |
| 414 | &rkey) != ERROR_SUCCESS) |
| 415 | return; /* key does not exist in registry */ |
| 416 | RegSetValueEx(rkey, regname, 0, REG_SZ, key, strlen(key) + 1); |
| 417 | RegCloseKey(rkey); |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * Find the random seed file path and store it in `seedpath'. |
| 422 | */ |
| 423 | static void get_seedpath(void) |
| 424 | { |
| 425 | HKEY rkey; |
| 426 | DWORD type, size; |
| 427 | |
| 428 | size = sizeof(seedpath); |
| 429 | |
| 430 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &rkey) == |
| 431 | ERROR_SUCCESS) { |
| 432 | int ret = RegQueryValueEx(rkey, "RandSeedFile", |
| 433 | 0, &type, seedpath, &size); |
| 434 | if (ret != ERROR_SUCCESS || type != REG_SZ) |
| 435 | seedpath[0] = '\0'; |
| 436 | RegCloseKey(rkey); |
| 437 | } else |
| 438 | seedpath[0] = '\0'; |
| 439 | |
| 440 | if (!seedpath[0]) { |
| 441 | int len, ret; |
| 442 | |
| 443 | len = |
| 444 | GetEnvironmentVariable("HOMEDRIVE", seedpath, |
| 445 | sizeof(seedpath)); |
| 446 | ret = |
| 447 | GetEnvironmentVariable("HOMEPATH", seedpath + len, |
| 448 | sizeof(seedpath) - len); |
| 449 | if (ret == 0) { /* probably win95; store in \WINDOWS */ |
| 450 | GetWindowsDirectory(seedpath, sizeof(seedpath)); |
| 451 | len = strlen(seedpath); |
| 452 | } else |
| 453 | len += ret; |
| 454 | strcpy(seedpath + len, "\\PUTTY.RND"); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | void read_random_seed(noise_consumer_t consumer) |
| 459 | { |
| 460 | HANDLE seedf; |
| 461 | |
| 462 | if (!seedpath[0]) |
| 463 | get_seedpath(); |
| 464 | |
| 465 | seedf = CreateFile(seedpath, GENERIC_READ, |
| 466 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 467 | NULL, OPEN_EXISTING, 0, NULL); |
| 468 | |
| 469 | if (seedf != INVALID_HANDLE_VALUE) { |
| 470 | while (1) { |
| 471 | char buf[1024]; |
| 472 | DWORD len; |
| 473 | |
| 474 | if (ReadFile(seedf, buf, sizeof(buf), &len, NULL) && len) |
| 475 | consumer(buf, len); |
| 476 | else |
| 477 | break; |
| 478 | } |
| 479 | CloseHandle(seedf); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | void write_random_seed(void *data, int len) |
| 484 | { |
| 485 | HANDLE seedf; |
| 486 | |
| 487 | if (!seedpath[0]) |
| 488 | get_seedpath(); |
| 489 | |
| 490 | seedf = CreateFile(seedpath, GENERIC_WRITE, 0, |
| 491 | NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 492 | |
| 493 | if (seedf != INVALID_HANDLE_VALUE) { |
| 494 | DWORD lenwritten; |
| 495 | |
| 496 | WriteFile(seedf, data, len, &lenwritten, NULL); |
| 497 | CloseHandle(seedf); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * Recursively delete a registry key and everything under it. |
| 503 | */ |
| 504 | static void registry_recursive_remove(HKEY key) |
| 505 | { |
| 506 | DWORD i; |
| 507 | char name[MAX_PATH + 1]; |
| 508 | HKEY subkey; |
| 509 | |
| 510 | i = 0; |
| 511 | while (RegEnumKey(key, i, name, sizeof(name)) == ERROR_SUCCESS) { |
| 512 | if (RegOpenKey(key, name, &subkey) == ERROR_SUCCESS) { |
| 513 | registry_recursive_remove(subkey); |
| 514 | RegCloseKey(subkey); |
| 515 | } |
| 516 | RegDeleteKey(key, name); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | void cleanup_all(void) |
| 521 | { |
| 522 | HKEY key; |
| 523 | int ret; |
| 524 | char name[MAX_PATH + 1]; |
| 525 | |
| 526 | /* ------------------------------------------------------------ |
| 527 | * Wipe out the random seed file. |
| 528 | */ |
| 529 | if (!seedpath[0]) |
| 530 | get_seedpath(); |
| 531 | remove(seedpath); |
| 532 | |
| 533 | /* ------------------------------------------------------------ |
| 534 | * Destroy all registry information associated with PuTTY. |
| 535 | */ |
| 536 | |
| 537 | /* |
| 538 | * Open the main PuTTY registry key and remove everything in it. |
| 539 | */ |
| 540 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &key) == |
| 541 | ERROR_SUCCESS) { |
| 542 | registry_recursive_remove(key); |
| 543 | RegCloseKey(key); |
| 544 | } |
| 545 | /* |
| 546 | * Now open the parent key and remove the PuTTY main key. Once |
| 547 | * we've done that, see if the parent key has any other |
| 548 | * children. |
| 549 | */ |
| 550 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_PARENT, |
| 551 | &key) == ERROR_SUCCESS) { |
| 552 | RegDeleteKey(key, PUTTY_REG_PARENT_CHILD); |
| 553 | ret = RegEnumKey(key, 0, name, sizeof(name)); |
| 554 | RegCloseKey(key); |
| 555 | /* |
| 556 | * If the parent key had no other children, we must delete |
| 557 | * it in its turn. That means opening the _grandparent_ |
| 558 | * key. |
| 559 | */ |
| 560 | if (ret != ERROR_SUCCESS) { |
| 561 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_GPARENT, |
| 562 | &key) == ERROR_SUCCESS) { |
| 563 | RegDeleteKey(key, PUTTY_REG_GPARENT_CHILD); |
| 564 | RegCloseKey(key); |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | /* |
| 569 | * Now we're done. |
| 570 | */ |
| 571 | } |