Introduced wrapper macros snew(), snewn() and sresize() for the
[u/mdw/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
90bf4cce 70 if (!sessionname || !*sessionname)
71 sessionname = "Default Settings";
72
3d88e64d 73 p = snewn(3 * strlen(sessionname) + 1, char);
d1622aed 74 mungestr(sessionname, p);
32874aea 75
d1622aed 76 ret = RegCreateKey(HKEY_CURRENT_USER, puttystr, &subkey1);
77 if (ret != ERROR_SUCCESS) {
32874aea 78 sfree(p);
79 return NULL;
d1622aed 80 }
81 ret = RegCreateKey(subkey1, p, &sesskey);
dcbde236 82 sfree(p);
d1622aed 83 RegCloseKey(subkey1);
84 if (ret != ERROR_SUCCESS)
32874aea 85 return NULL;
86 return (void *) sesskey;
d1622aed 87}
88
c85623f9 89void write_setting_s(void *handle, const char *key, const char *value)
32874aea 90{
d1622aed 91 if (handle)
32874aea 92 RegSetValueEx((HKEY) handle, key, 0, REG_SZ, value,
93 1 + strlen(value));
d1622aed 94}
95
c85623f9 96void write_setting_i(void *handle, const char *key, int value)
32874aea 97{
d1622aed 98 if (handle)
32874aea 99 RegSetValueEx((HKEY) handle, key, 0, REG_DWORD,
100 (CONST BYTE *) & value, sizeof(value));
d1622aed 101}
102
32874aea 103void close_settings_w(void *handle)
104{
105 RegCloseKey((HKEY) handle);
d1622aed 106}
107
c85623f9 108void *open_settings_r(const char *sessionname)
32874aea 109{
d1622aed 110 HKEY subkey1, sesskey;
111 char *p;
112
90bf4cce 113 if (!sessionname || !*sessionname)
114 sessionname = "Default Settings";
115
3d88e64d 116 p = snewn(3 * strlen(sessionname) + 1, char);
d1622aed 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
dcbde236 128 sfree(p);
d1622aed 129
32874aea 130 return (void *) sesskey;
d1622aed 131}
132
c85623f9 133char *read_setting_s(void *handle, const char *key, char *buffer, int buflen)
32874aea 134{
d1622aed 135 DWORD type, size;
136 size = buflen;
137
138 if (!handle ||
32874aea 139 RegQueryValueEx((HKEY) handle, key, 0,
140 &type, buffer, &size) != ERROR_SUCCESS ||
141 type != REG_SZ) return NULL;
d1622aed 142 else
32874aea 143 return buffer;
d1622aed 144}
d5859615 145
c85623f9 146int read_setting_i(void *handle, const char *key, int defvalue)
32874aea 147{
d1622aed 148 DWORD type, val, size;
149 size = sizeof(val);
150
151 if (!handle ||
32874aea 152 RegQueryValueEx((HKEY) handle, key, 0, &type,
153 (BYTE *) & val, &size) != ERROR_SUCCESS ||
d1622aed 154 size != sizeof(val) || type != REG_DWORD)
155 return defvalue;
156 else
157 return val;
158}
159
9a30e26b 160int 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
195void 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
211int 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
216void write_setting_filename(void *handle, const char *name, Filename result)
217{
218 write_setting_s(handle, name, result.path);
219}
220
32874aea 221void close_settings_r(void *handle)
222{
223 RegCloseKey((HKEY) handle);
d1622aed 224}
225
c85623f9 226void del_settings(const char *sessionname)
32874aea 227{
d1622aed 228 HKEY subkey1;
229 char *p;
230
231 if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS)
232 return;
233
3d88e64d 234 p = snewn(3 * strlen(sessionname) + 1, char);
d1622aed 235 mungestr(sessionname, p);
236 RegDeleteKey(subkey1, p);
dcbde236 237 sfree(p);
d1622aed 238
239 RegCloseKey(subkey1);
240}
d5859615 241
d1622aed 242struct enumsettings {
243 HKEY key;
244 int i;
245};
246
32874aea 247void *enum_settings_start(void)
248{
d1622aed 249 struct enumsettings *ret;
250 HKEY key;
251
5f2df4d2 252 if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &key) != ERROR_SUCCESS)
32874aea 253 return NULL;
d1622aed 254
3d88e64d 255 ret = snew(struct enumsettings);
d1622aed 256 if (ret) {
32874aea 257 ret->key = key;
258 ret->i = 0;
d1622aed 259 }
260
261 return ret;
262}
263
32874aea 264char *enum_settings_next(void *handle, char *buffer, int buflen)
265{
266 struct enumsettings *e = (struct enumsettings *) handle;
d1622aed 267 char *otherbuf;
3d88e64d 268 otherbuf = snewn(3 * buflen, char);
60371ab3 269 if (RegEnumKey(e->key, e->i++, otherbuf, 3 * buflen) == ERROR_SUCCESS) {
32874aea 270 unmungestr(otherbuf, buffer, buflen);
271 sfree(otherbuf);
272 return buffer;
60371ab3 273 } else {
274 sfree(otherbuf);
32874aea 275 return NULL;
60371ab3 276 }
d1622aed 277}
278
32874aea 279void enum_settings_finish(void *handle)
280{
281 struct enumsettings *e = (struct enumsettings *) handle;
d1622aed 282 RegCloseKey(e->key);
dcbde236 283 sfree(e);
d1622aed 284}
285
c85623f9 286static void hostkey_regname(char *buffer, const char *hostname,
287 int port, const char *keytype)
32874aea 288{
d4857987 289 int len;
290 strcpy(buffer, keytype);
291 strcat(buffer, "@");
292 len = strlen(buffer);
32874aea 293 len += sprintf(buffer + len, "%d:", port);
d4857987 294 mungestr(hostname, buffer + strlen(buffer));
295}
296
c85623f9 297int verify_host_key(const char *hostname, int port,
298 const char *keytype, const char *key)
32874aea 299{
d5859615 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 */
3d88e64d 313 otherstr = snewn(len, char);
314 regname = snewn(3 * (strlen(hostname) + strlen(keytype)) + 15, char);
d5859615 315
d4857987 316 hostkey_regname(regname, hostname, port, keytype);
d5859615 317
5f2df4d2 318 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys",
319 &rkey) != ERROR_SUCCESS)
32874aea 320 return 1; /* key does not exist in registry */
d5859615 321
322 readlen = len;
323 ret = RegQueryValueEx(rkey, regname, NULL, &type, otherstr, &readlen);
324
325 if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA &&
32874aea 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, ":");
3d88e64d 333 char *oldstyle = snewn(len + 10, char); /* safety margin */
32874aea 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 }
d5859615 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 ||
32874aea 395 (ret == ERROR_SUCCESS && type == REG_SZ && compare))
396 return 2; /* key is different in registry */
d5859615 397 else if (ret != ERROR_SUCCESS || type != REG_SZ)
32874aea 398 return 1; /* key does not exist in registry */
d5859615 399 else
32874aea 400 return 0; /* key matched OK in registry */
d5859615 401}
402
c85623f9 403void store_host_key(const char *hostname, int port,
404 const char *keytype, const char *key)
32874aea 405{
d5859615 406 char *regname;
407 HKEY rkey;
408
3d88e64d 409 regname = snewn(3 * (strlen(hostname) + strlen(keytype)) + 15, char);
d5859615 410
d4857987 411 hostkey_regname(regname, hostname, port, keytype);
d5859615 412
413 if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys",
414 &rkey) != ERROR_SUCCESS)
32874aea 415 return; /* key does not exist in registry */
416 RegSetValueEx(rkey, regname, 0, REG_SZ, key, strlen(key) + 1);
d5859615 417 RegCloseKey(rkey);
418}
419
420/*
421 * Find the random seed file path and store it in `seedpath'.
422 */
32874aea 423static void get_seedpath(void)
424{
d5859615 425 HKEY rkey;
426 DWORD type, size;
427
428 size = sizeof(seedpath);
429
32874aea 430 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &rkey) ==
431 ERROR_SUCCESS) {
d5859615 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
32874aea 443 len =
444 GetEnvironmentVariable("HOMEDRIVE", seedpath,
445 sizeof(seedpath));
446 ret =
447 GetEnvironmentVariable("HOMEPATH", seedpath + len,
448 sizeof(seedpath) - len);
d5859615 449 if (ret == 0) { /* probably win95; store in \WINDOWS */
450 GetWindowsDirectory(seedpath, sizeof(seedpath));
451 len = strlen(seedpath);
452 } else
453 len += ret;
32874aea 454 strcpy(seedpath + len, "\\PUTTY.RND");
d5859615 455 }
456}
457
32874aea 458void read_random_seed(noise_consumer_t consumer)
459{
d5859615 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)
32874aea 475 consumer(buf, len);
d5859615 476 else
477 break;
478 }
479 CloseHandle(seedf);
480 }
481}
482
32874aea 483void write_random_seed(void *data, int len)
484{
d5859615 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 */
32874aea 504static void registry_recursive_remove(HKEY key)
505{
d5859615 506 DWORD i;
32874aea 507 char name[MAX_PATH + 1];
d5859615 508 HKEY subkey;
509
510 i = 0;
511 while (RegEnumKey(key, i, name, sizeof(name)) == ERROR_SUCCESS) {
32874aea 512 if (RegOpenKey(key, name, &subkey) == ERROR_SUCCESS) {
513 registry_recursive_remove(subkey);
514 RegCloseKey(subkey);
515 }
516 RegDeleteKey(key, name);
d5859615 517 }
518}
519
32874aea 520void cleanup_all(void)
521{
d5859615 522 HKEY key;
523 int ret;
32874aea 524 char name[MAX_PATH + 1];
d5859615 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 */
32874aea 540 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &key) ==
541 ERROR_SUCCESS) {
542 registry_recursive_remove(key);
543 RegCloseKey(key);
d5859615 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,
32874aea 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 }
d5859615 567 }
568 /*
569 * Now we're done.
570 */
571}