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 | |
157 | static void hostkey_regname(char *buffer, char *hostname, char *keytype) { |
158 | strcpy(buffer, keytype); |
159 | strcat(buffer, "@"); |
160 | mungestr(hostname, buffer + strlen(buffer)); |
161 | } |
162 | |
d1622aed |
163 | struct enumsettings { |
164 | HKEY key; |
165 | int i; |
166 | }; |
167 | |
168 | void *enum_settings_start(void) { |
169 | struct enumsettings *ret; |
170 | HKEY key; |
171 | |
172 | if (RegCreateKey(HKEY_CURRENT_USER, puttystr, &key) != ERROR_SUCCESS) |
173 | return NULL; |
174 | |
175 | ret = malloc(sizeof(*ret)); |
176 | if (ret) { |
177 | ret->key = key; |
178 | ret->i = 0; |
179 | } |
180 | |
181 | return ret; |
182 | } |
183 | |
184 | char *enum_settings_next(void *handle, char *buffer, int buflen) { |
185 | struct enumsettings *e = (struct enumsettings *)handle; |
186 | char *otherbuf; |
187 | otherbuf = malloc(3*buflen); |
188 | if (otherbuf && RegEnumKey(e->key, e->i++, otherbuf, |
189 | 3*buflen) == ERROR_SUCCESS) { |
190 | unmungestr(otherbuf, buffer, buflen); |
191 | free(otherbuf); |
192 | return buffer; |
193 | } else |
194 | return NULL; |
195 | |
196 | } |
197 | |
198 | void enum_settings_finish(void *handle) { |
199 | struct enumsettings *e = (struct enumsettings *)handle; |
200 | RegCloseKey(e->key); |
201 | free(e); |
202 | } |
203 | |
d5859615 |
204 | int verify_host_key(char *hostname, char *keytype, char *key) { |
205 | char *otherstr, *regname; |
206 | int len; |
207 | HKEY rkey; |
208 | DWORD readlen; |
209 | DWORD type; |
210 | int ret, compare; |
211 | |
212 | len = 1 + strlen(key); |
213 | |
214 | /* |
215 | * Now read a saved key in from the registry and see what it |
216 | * says. |
217 | */ |
218 | otherstr = smalloc(len); |
219 | regname = smalloc(3*(strlen(hostname)+strlen(keytype))+5); |
220 | if (!otherstr || !regname) |
221 | fatalbox("Out of memory"); |
222 | |
223 | hostkey_regname(regname, hostname, keytype); |
224 | |
225 | if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys", |
226 | &rkey) != ERROR_SUCCESS) |
227 | return 1; /* key does not exist in registry */ |
228 | |
229 | readlen = len; |
230 | ret = RegQueryValueEx(rkey, regname, NULL, &type, otherstr, &readlen); |
231 | |
232 | if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA && |
233 | !strcmp(keytype, "rsa")) { |
234 | /* |
235 | * Key didn't exist. If the key type is RSA, we'll try |
236 | * another trick, which is to look up the _old_ key format |
237 | * under just the hostname and translate that. |
238 | */ |
239 | char *justhost = regname + 1 + strlen(keytype); |
240 | char *oldstyle = smalloc(len + 10); /* safety margin */ |
241 | readlen = len; |
242 | ret = RegQueryValueEx(rkey, justhost, NULL, &type, |
243 | oldstyle, &readlen); |
244 | |
245 | if (ret == ERROR_SUCCESS && type == REG_SZ) { |
246 | /* |
247 | * The old format is two old-style bignums separated by |
248 | * a slash. An old-style bignum is made of groups of |
249 | * four hex digits: digits are ordered in sensible |
250 | * (most to least significant) order within each group, |
251 | * but groups are ordered in silly (least to most) |
252 | * order within the bignum. The new format is two |
253 | * ordinary C-format hex numbers (0xABCDEFG...XYZ, with |
254 | * A nonzero except in the special case 0x0, which |
255 | * doesn't appear anyway in RSA keys) separated by a |
256 | * comma. All hex digits are lowercase in both formats. |
257 | */ |
258 | char *p = otherstr; |
259 | char *q = oldstyle; |
260 | int i, j; |
261 | |
262 | for (i = 0; i < 2; i++) { |
263 | int ndigits, nwords; |
264 | *p++ = '0'; *p++ = 'x'; |
265 | ndigits = strcspn(q, "/"); /* find / or end of string */ |
266 | nwords = ndigits / 4; |
267 | /* now trim ndigits to remove leading zeros */ |
268 | while (q[ (ndigits-1) ^ 3 ] == '0' && ndigits > 1) |
269 | ndigits--; |
270 | /* now move digits over to new string */ |
271 | for (j = 0; j < ndigits; j++) |
272 | p[ndigits-1-j] = q[j^3]; |
273 | p += ndigits; |
274 | q += nwords*4; |
275 | if (*q) { |
276 | q++; /* eat the slash */ |
277 | *p++ = ','; /* add a comma */ |
278 | } |
279 | *p = '\0'; /* terminate the string */ |
280 | } |
281 | |
282 | /* |
283 | * Now _if_ this key matches, we'll enter it in the new |
284 | * format. If not, we'll assume something odd went |
285 | * wrong, and hyper-cautiously do nothing. |
286 | */ |
287 | if (!strcmp(otherstr, key)) |
288 | RegSetValueEx(rkey, regname, 0, REG_SZ, otherstr, |
289 | strlen(otherstr)+1); |
290 | } |
291 | } |
292 | |
293 | RegCloseKey(rkey); |
294 | |
295 | compare = strcmp(otherstr, key); |
296 | |
297 | sfree(otherstr); |
298 | sfree(regname); |
299 | |
300 | if (ret == ERROR_MORE_DATA || |
301 | (ret == ERROR_SUCCESS && type == REG_SZ && compare)) |
302 | return 2; /* key is different in registry */ |
303 | else if (ret != ERROR_SUCCESS || type != REG_SZ) |
304 | return 1; /* key does not exist in registry */ |
305 | else |
306 | return 0; /* key matched OK in registry */ |
307 | } |
308 | |
309 | void store_host_key(char *hostname, char *keytype, char *key) { |
310 | char *regname; |
311 | HKEY rkey; |
312 | |
313 | regname = smalloc(3*(strlen(hostname)+strlen(keytype))+5); |
314 | if (!regname) |
315 | fatalbox("Out of memory"); |
316 | |
317 | hostkey_regname(regname, hostname, keytype); |
318 | |
319 | if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys", |
320 | &rkey) != ERROR_SUCCESS) |
321 | return; /* key does not exist in registry */ |
322 | RegSetValueEx(rkey, regname, 0, REG_SZ, key, |
323 | strlen(key)+1); |
324 | RegCloseKey(rkey); |
325 | } |
326 | |
327 | /* |
328 | * Find the random seed file path and store it in `seedpath'. |
329 | */ |
330 | static void get_seedpath(void) { |
331 | HKEY rkey; |
332 | DWORD type, size; |
333 | |
334 | size = sizeof(seedpath); |
335 | |
336 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &rkey)==ERROR_SUCCESS) { |
337 | int ret = RegQueryValueEx(rkey, "RandSeedFile", |
338 | 0, &type, seedpath, &size); |
339 | if (ret != ERROR_SUCCESS || type != REG_SZ) |
340 | seedpath[0] = '\0'; |
341 | RegCloseKey(rkey); |
342 | } else |
343 | seedpath[0] = '\0'; |
344 | |
345 | if (!seedpath[0]) { |
346 | int len, ret; |
347 | |
348 | len = GetEnvironmentVariable("HOMEDRIVE", seedpath, sizeof(seedpath)); |
349 | ret = GetEnvironmentVariable("HOMEPATH", seedpath+len, |
350 | sizeof(seedpath)-len); |
351 | if (ret == 0) { /* probably win95; store in \WINDOWS */ |
352 | GetWindowsDirectory(seedpath, sizeof(seedpath)); |
353 | len = strlen(seedpath); |
354 | } else |
355 | len += ret; |
356 | strcpy(seedpath+len, "\\PUTTY.RND"); |
357 | } |
358 | } |
359 | |
360 | void read_random_seed(noise_consumer_t consumer) { |
361 | HANDLE seedf; |
362 | |
363 | if (!seedpath[0]) |
364 | get_seedpath(); |
365 | |
366 | seedf = CreateFile(seedpath, GENERIC_READ, |
367 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
368 | NULL, OPEN_EXISTING, 0, NULL); |
369 | |
370 | if (seedf != INVALID_HANDLE_VALUE) { |
371 | while (1) { |
372 | char buf[1024]; |
373 | DWORD len; |
374 | |
375 | if (ReadFile(seedf, buf, sizeof(buf), &len, NULL) && len) |
376 | consumer(buf, len); |
377 | else |
378 | break; |
379 | } |
380 | CloseHandle(seedf); |
381 | } |
382 | } |
383 | |
d1622aed |
384 | void write_random_seed(void *data, int len) { |
d5859615 |
385 | HANDLE seedf; |
386 | |
387 | if (!seedpath[0]) |
388 | get_seedpath(); |
389 | |
390 | seedf = CreateFile(seedpath, GENERIC_WRITE, 0, |
391 | NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
392 | |
393 | if (seedf != INVALID_HANDLE_VALUE) { |
394 | DWORD lenwritten; |
395 | |
396 | WriteFile(seedf, data, len, &lenwritten, NULL); |
397 | CloseHandle(seedf); |
398 | } |
399 | } |
400 | |
401 | /* |
402 | * Recursively delete a registry key and everything under it. |
403 | */ |
404 | static void registry_recursive_remove(HKEY key) { |
405 | DWORD i; |
406 | char name[MAX_PATH+1]; |
407 | HKEY subkey; |
408 | |
409 | i = 0; |
410 | while (RegEnumKey(key, i, name, sizeof(name)) == ERROR_SUCCESS) { |
411 | if (RegOpenKey(key, name, &subkey) == ERROR_SUCCESS) { |
412 | registry_recursive_remove(subkey); |
413 | RegCloseKey(subkey); |
414 | } |
415 | RegDeleteKey(key, name); |
416 | } |
417 | } |
418 | |
419 | void cleanup_all(void) { |
420 | HKEY key; |
421 | int ret; |
422 | char name[MAX_PATH+1]; |
423 | |
424 | /* ------------------------------------------------------------ |
425 | * Wipe out the random seed file. |
426 | */ |
427 | if (!seedpath[0]) |
428 | get_seedpath(); |
429 | remove(seedpath); |
430 | |
431 | /* ------------------------------------------------------------ |
432 | * Destroy all registry information associated with PuTTY. |
433 | */ |
434 | |
435 | /* |
436 | * Open the main PuTTY registry key and remove everything in it. |
437 | */ |
438 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &key) == ERROR_SUCCESS) { |
439 | registry_recursive_remove(key); |
440 | RegCloseKey(key); |
441 | } |
442 | /* |
443 | * Now open the parent key and remove the PuTTY main key. Once |
444 | * we've done that, see if the parent key has any other |
445 | * children. |
446 | */ |
447 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_PARENT, |
448 | &key) == ERROR_SUCCESS) { |
449 | RegDeleteKey(key, PUTTY_REG_PARENT_CHILD); |
450 | ret = RegEnumKey(key, 0, name, sizeof(name)); |
451 | RegCloseKey(key); |
452 | /* |
453 | * If the parent key had no other children, we must delete |
454 | * it in its turn. That means opening the _grandparent_ |
455 | * key. |
456 | */ |
457 | if (ret != ERROR_SUCCESS) { |
458 | if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_GPARENT, |
459 | &key) == ERROR_SUCCESS) { |
460 | RegDeleteKey(key, PUTTY_REG_GPARENT_CHILD); |
461 | RegCloseKey(key); |
462 | } |
463 | } |
464 | } |
465 | /* |
466 | * Now we're done. |
467 | */ |
468 | } |