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