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 |
12 | static const char *const puttystr = PUTTY_REG_POS "\\Sessions"; |
13 | |
32874aea |
14 | static char seedpath[2 * MAX_PATH + 10] = "\0"; |
d5859615 |
15 | |
16 | static char hex[16] = "0123456789ABCDEF"; |
17 | |
32874aea |
18 | static void mungestr(char *in, char *out) |
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 | |
32874aea |
38 | static void unmungestr(char *in, char *out, int outlen) |
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 | |
32874aea |
63 | void *open_settings_w(char *sessionname) |
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 | |
32874aea |
85 | void write_setting_s(void *handle, char *key, char *value) |
86 | { |
d1622aed |
87 | if (handle) |
32874aea |
88 | RegSetValueEx((HKEY) handle, key, 0, REG_SZ, value, |
89 | 1 + strlen(value)); |
d1622aed |
90 | } |
91 | |
32874aea |
92 | void write_setting_i(void *handle, char *key, int value) |
93 | { |
d1622aed |
94 | if (handle) |
32874aea |
95 | RegSetValueEx((HKEY) handle, key, 0, REG_DWORD, |
96 | (CONST BYTE *) & value, sizeof(value)); |
d1622aed |
97 | } |
98 | |
32874aea |
99 | void close_settings_w(void *handle) |
100 | { |
101 | RegCloseKey((HKEY) handle); |
d1622aed |
102 | } |
103 | |
32874aea |
104 | void *open_settings_r(char *sessionname) |
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 | |
32874aea |
126 | char *read_setting_s(void *handle, char *key, char *buffer, int buflen) |
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 | |
32874aea |
139 | int read_setting_i(void *handle, char *key, int defvalue) |
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 |
153 | void close_settings_r(void *handle) |
154 | { |
155 | RegCloseKey((HKEY) handle); |
d1622aed |
156 | } |
157 | |
32874aea |
158 | void del_settings(char *sessionname) |
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 |
174 | struct enumsettings { |
175 | HKEY key; |
176 | int i; |
177 | }; |
178 | |
32874aea |
179 | void *enum_settings_start(void) |
180 | { |
d1622aed |
181 | struct enumsettings *ret; |
182 | HKEY key; |
183 | |
184 | if (RegCreateKey(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 |
196 | char *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); |
d1622aed |
201 | if (otherbuf && RegEnumKey(e->key, e->i++, otherbuf, |
32874aea |
202 | 3 * buflen) == ERROR_SUCCESS) { |
203 | unmungestr(otherbuf, buffer, buflen); |
204 | sfree(otherbuf); |
205 | return buffer; |
d1622aed |
206 | } else |
32874aea |
207 | return NULL; |
d1622aed |
208 | |
209 | } |
210 | |
32874aea |
211 | void 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 | |
d4857987 |
218 | static void hostkey_regname(char *buffer, char *hostname, |
32874aea |
219 | int port, char *keytype) |
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 | |
32874aea |
229 | int verify_host_key(char *hostname, int port, char *keytype, char *key) |
230 | { |
d5859615 |
231 | char *otherstr, *regname; |
232 | int len; |
233 | HKEY rkey; |
234 | DWORD readlen; |
235 | DWORD type; |
236 | int ret, compare; |
237 | |
238 | len = 1 + strlen(key); |
239 | |
240 | /* |
241 | * Now read a saved key in from the registry and see what it |
242 | * says. |
243 | */ |
244 | otherstr = smalloc(len); |
32874aea |
245 | regname = smalloc(3 * (strlen(hostname) + strlen(keytype)) + 15); |
d5859615 |
246 | |
d4857987 |
247 | hostkey_regname(regname, hostname, port, keytype); |
d5859615 |
248 | |
249 | if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys", |
250 | &rkey) != ERROR_SUCCESS) |
32874aea |
251 | return 1; /* key does not exist in registry */ |
d5859615 |
252 | |
253 | readlen = len; |
254 | ret = RegQueryValueEx(rkey, regname, NULL, &type, otherstr, &readlen); |
255 | |
256 | if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA && |
32874aea |
257 | !strcmp(keytype, "rsa")) { |
258 | /* |
259 | * Key didn't exist. If the key type is RSA, we'll try |
260 | * another trick, which is to look up the _old_ key format |
261 | * under just the hostname and translate that. |
262 | */ |
263 | char *justhost = regname + 1 + strcspn(regname, ":"); |
264 | char *oldstyle = smalloc(len + 10); /* safety margin */ |
265 | readlen = len; |
266 | ret = RegQueryValueEx(rkey, justhost, NULL, &type, |
267 | oldstyle, &readlen); |
268 | |
269 | if (ret == ERROR_SUCCESS && type == REG_SZ) { |
270 | /* |
271 | * The old format is two old-style bignums separated by |
272 | * a slash. An old-style bignum is made of groups of |
273 | * four hex digits: digits are ordered in sensible |
274 | * (most to least significant) order within each group, |
275 | * but groups are ordered in silly (least to most) |
276 | * order within the bignum. The new format is two |
277 | * ordinary C-format hex numbers (0xABCDEFG...XYZ, with |
278 | * A nonzero except in the special case 0x0, which |
279 | * doesn't appear anyway in RSA keys) separated by a |
280 | * comma. All hex digits are lowercase in both formats. |
281 | */ |
282 | char *p = otherstr; |
283 | char *q = oldstyle; |
284 | int i, j; |
285 | |
286 | for (i = 0; i < 2; i++) { |
287 | int ndigits, nwords; |
288 | *p++ = '0'; |
289 | *p++ = 'x'; |
290 | ndigits = strcspn(q, "/"); /* find / or end of string */ |
291 | nwords = ndigits / 4; |
292 | /* now trim ndigits to remove leading zeros */ |
293 | while (q[(ndigits - 1) ^ 3] == '0' && ndigits > 1) |
294 | ndigits--; |
295 | /* now move digits over to new string */ |
296 | for (j = 0; j < ndigits; j++) |
297 | p[ndigits - 1 - j] = q[j ^ 3]; |
298 | p += ndigits; |
299 | q += nwords * 4; |
300 | if (*q) { |
301 | q++; /* eat the slash */ |
302 | *p++ = ','; /* add a comma */ |
303 | } |
304 | *p = '\0'; /* terminate the string */ |
305 | } |
306 | |
307 | /* |
308 | * Now _if_ this key matches, we'll enter it in the new |
309 | * format. If not, we'll assume something odd went |
310 | * wrong, and hyper-cautiously do nothing. |
311 | */ |
312 | if (!strcmp(otherstr, key)) |
313 | RegSetValueEx(rkey, regname, 0, REG_SZ, otherstr, |
314 | strlen(otherstr) + 1); |
315 | } |
d5859615 |
316 | } |
317 | |
318 | RegCloseKey(rkey); |
319 | |
320 | compare = strcmp(otherstr, key); |
321 | |
322 | sfree(otherstr); |
323 | sfree(regname); |
324 | |
325 | if (ret == ERROR_MORE_DATA || |
32874aea |
326 | (ret == ERROR_SUCCESS && type == REG_SZ && compare)) |
327 | return 2; /* key is different in registry */ |
d5859615 |
328 | else if (ret != ERROR_SUCCESS || type != REG_SZ) |
32874aea |
329 | return 1; /* key does not exist in registry */ |
d5859615 |
330 | else |
32874aea |
331 | return 0; /* key matched OK in registry */ |
d5859615 |
332 | } |
333 | |
32874aea |
334 | void store_host_key(char *hostname, int port, char *keytype, char *key) |
335 | { |
d5859615 |
336 | char *regname; |
337 | HKEY rkey; |
338 | |
32874aea |
339 | regname = smalloc(3 * (strlen(hostname) + strlen(keytype)) + 15); |
d5859615 |
340 | |
d4857987 |
341 | hostkey_regname(regname, hostname, port, keytype); |
d5859615 |
342 | |
343 | if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys", |
344 | &rkey) != ERROR_SUCCESS) |
32874aea |
345 | return; /* key does not exist in registry */ |
346 | RegSetValueEx(rkey, regname, 0, REG_SZ, key, strlen(key) + 1); |
d5859615 |
347 | RegCloseKey(rkey); |
348 | } |
349 | |
350 | /* |
351 | * Find the random seed file path and store it in `seedpath'. |
352 | */ |
32874aea |
353 | static void get_seedpath(void) |
354 | { |
d5859615 |
355 | HKEY rkey; |
356 | DWORD type, size; |
357 | |
358 | size = sizeof(seedpath); |
359 | |
32874aea |
360 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &rkey) == |
361 | ERROR_SUCCESS) { |
d5859615 |
362 | int ret = RegQueryValueEx(rkey, "RandSeedFile", |
363 | 0, &type, seedpath, &size); |
364 | if (ret != ERROR_SUCCESS || type != REG_SZ) |
365 | seedpath[0] = '\0'; |
366 | RegCloseKey(rkey); |
367 | } else |
368 | seedpath[0] = '\0'; |
369 | |
370 | if (!seedpath[0]) { |
371 | int len, ret; |
372 | |
32874aea |
373 | len = |
374 | GetEnvironmentVariable("HOMEDRIVE", seedpath, |
375 | sizeof(seedpath)); |
376 | ret = |
377 | GetEnvironmentVariable("HOMEPATH", seedpath + len, |
378 | sizeof(seedpath) - len); |
d5859615 |
379 | if (ret == 0) { /* probably win95; store in \WINDOWS */ |
380 | GetWindowsDirectory(seedpath, sizeof(seedpath)); |
381 | len = strlen(seedpath); |
382 | } else |
383 | len += ret; |
32874aea |
384 | strcpy(seedpath + len, "\\PUTTY.RND"); |
d5859615 |
385 | } |
386 | } |
387 | |
32874aea |
388 | void read_random_seed(noise_consumer_t consumer) |
389 | { |
d5859615 |
390 | HANDLE seedf; |
391 | |
392 | if (!seedpath[0]) |
393 | get_seedpath(); |
394 | |
395 | seedf = CreateFile(seedpath, GENERIC_READ, |
396 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
397 | NULL, OPEN_EXISTING, 0, NULL); |
398 | |
399 | if (seedf != INVALID_HANDLE_VALUE) { |
400 | while (1) { |
401 | char buf[1024]; |
402 | DWORD len; |
403 | |
404 | if (ReadFile(seedf, buf, sizeof(buf), &len, NULL) && len) |
32874aea |
405 | consumer(buf, len); |
d5859615 |
406 | else |
407 | break; |
408 | } |
409 | CloseHandle(seedf); |
410 | } |
411 | } |
412 | |
32874aea |
413 | void write_random_seed(void *data, int len) |
414 | { |
d5859615 |
415 | HANDLE seedf; |
416 | |
417 | if (!seedpath[0]) |
418 | get_seedpath(); |
419 | |
420 | seedf = CreateFile(seedpath, GENERIC_WRITE, 0, |
421 | NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
422 | |
423 | if (seedf != INVALID_HANDLE_VALUE) { |
424 | DWORD lenwritten; |
425 | |
426 | WriteFile(seedf, data, len, &lenwritten, NULL); |
427 | CloseHandle(seedf); |
428 | } |
429 | } |
430 | |
431 | /* |
432 | * Recursively delete a registry key and everything under it. |
433 | */ |
32874aea |
434 | static void registry_recursive_remove(HKEY key) |
435 | { |
d5859615 |
436 | DWORD i; |
32874aea |
437 | char name[MAX_PATH + 1]; |
d5859615 |
438 | HKEY subkey; |
439 | |
440 | i = 0; |
441 | while (RegEnumKey(key, i, name, sizeof(name)) == ERROR_SUCCESS) { |
32874aea |
442 | if (RegOpenKey(key, name, &subkey) == ERROR_SUCCESS) { |
443 | registry_recursive_remove(subkey); |
444 | RegCloseKey(subkey); |
445 | } |
446 | RegDeleteKey(key, name); |
d5859615 |
447 | } |
448 | } |
449 | |
32874aea |
450 | void cleanup_all(void) |
451 | { |
d5859615 |
452 | HKEY key; |
453 | int ret; |
32874aea |
454 | char name[MAX_PATH + 1]; |
d5859615 |
455 | |
456 | /* ------------------------------------------------------------ |
457 | * Wipe out the random seed file. |
458 | */ |
459 | if (!seedpath[0]) |
460 | get_seedpath(); |
461 | remove(seedpath); |
462 | |
463 | /* ------------------------------------------------------------ |
464 | * Destroy all registry information associated with PuTTY. |
465 | */ |
466 | |
467 | /* |
468 | * Open the main PuTTY registry key and remove everything in it. |
469 | */ |
32874aea |
470 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &key) == |
471 | ERROR_SUCCESS) { |
472 | registry_recursive_remove(key); |
473 | RegCloseKey(key); |
d5859615 |
474 | } |
475 | /* |
476 | * Now open the parent key and remove the PuTTY main key. Once |
477 | * we've done that, see if the parent key has any other |
478 | * children. |
479 | */ |
480 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_PARENT, |
32874aea |
481 | &key) == ERROR_SUCCESS) { |
482 | RegDeleteKey(key, PUTTY_REG_PARENT_CHILD); |
483 | ret = RegEnumKey(key, 0, name, sizeof(name)); |
484 | RegCloseKey(key); |
485 | /* |
486 | * If the parent key had no other children, we must delete |
487 | * it in its turn. That means opening the _grandparent_ |
488 | * key. |
489 | */ |
490 | if (ret != ERROR_SUCCESS) { |
491 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_GPARENT, |
492 | &key) == ERROR_SUCCESS) { |
493 | RegDeleteKey(key, PUTTY_REG_GPARENT_CHILD); |
494 | RegCloseKey(key); |
495 | } |
496 | } |
d5859615 |
497 | } |
498 | /* |
499 | * Now we're done. |
500 | */ |
501 | } |