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