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