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