]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - winstore.c
Cross-reference cleanup: winstore.c should not use fatalbox()
[PuTTY.git] / winstore.c
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
11 static const char *const puttystr = PUTTY_REG_POS "\\Sessions";
12
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
35 static void unmungestr(char *in, char *out, int outlen) {
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;
44             if (!--outlen) return;
45             in += 3;
46         } else {
47             *out++ = *in++;
48             if (!--outlen) return;
49         }
50     }
51     *out = '\0';
52     return;
53 }
54
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 }
124
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 }
156
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
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) {
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);
223     regname = smalloc(3*(strlen(hostname)+strlen(keytype))+15);
224
225     hostkey_regname(regname, hostname, port, keytype);
226
227     if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys",
228                      &rkey) != ERROR_SUCCESS)
229         return 1;                      /* key does not exist in registry */
230
231     readlen = len;
232     ret = RegQueryValueEx(rkey, regname, NULL, &type, otherstr, &readlen);
233
234     if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA &&
235         !strcmp(keytype, "rsa")) {
236         /*
237          * Key didn't exist. If the key type is RSA, we'll try
238          * another trick, which is to look up the _old_ key format
239          * under just the hostname and translate that.
240          */
241         char *justhost = regname + 1 + strcspn(regname, ":");
242         char *oldstyle = smalloc(len + 10);   /* safety margin */
243         readlen = len;
244         ret = RegQueryValueEx(rkey, justhost, NULL, &type,
245                               oldstyle, &readlen);
246         
247         if (ret == ERROR_SUCCESS && type == REG_SZ) {
248             /*
249              * The old format is two old-style bignums separated by
250              * a slash. An old-style bignum is made of groups of
251              * four hex digits: digits are ordered in sensible
252              * (most to least significant) order within each group,
253              * but groups are ordered in silly (least to most)
254              * order within the bignum. The new format is two
255              * ordinary C-format hex numbers (0xABCDEFG...XYZ, with
256              * A nonzero except in the special case 0x0, which
257              * doesn't appear anyway in RSA keys) separated by a
258              * comma. All hex digits are lowercase in both formats.
259              */
260             char *p = otherstr;
261             char *q = oldstyle;
262             int i, j;
263
264             for (i = 0; i < 2; i++) {
265                 int ndigits, nwords;
266                 *p++ = '0'; *p++ = 'x';
267                 ndigits = strcspn(q, "/");   /* find / or end of string */
268                 nwords = ndigits / 4;
269                 /* now trim ndigits to remove leading zeros */
270                 while (q[ (ndigits-1) ^ 3 ] == '0' && ndigits > 1)
271                     ndigits--;
272                 /* now move digits over to new string */
273                 for (j = 0; j < ndigits; j++)
274                     p[ndigits-1-j] = q[j^3];
275                 p += ndigits;
276                 q += nwords*4;
277                 if (*q) {
278                     q++;               /* eat the slash */
279                     *p++ = ',';        /* add a comma */
280                 }
281                 *p = '\0';             /* terminate the string */
282             }
283
284             /*
285              * Now _if_ this key matches, we'll enter it in the new
286              * format. If not, we'll assume something odd went
287              * wrong, and hyper-cautiously do nothing.
288              */
289             if (!strcmp(otherstr, key))
290                 RegSetValueEx(rkey, regname, 0, REG_SZ, otherstr,
291                               strlen(otherstr)+1);
292         }
293     }
294
295     RegCloseKey(rkey);
296
297     compare = strcmp(otherstr, key);
298
299     sfree(otherstr);
300     sfree(regname);
301
302     if (ret == ERROR_MORE_DATA ||
303         (ret == ERROR_SUCCESS && type == REG_SZ && compare))
304         return 2;                      /* key is different in registry */
305     else if (ret != ERROR_SUCCESS || type != REG_SZ)
306         return 1;                      /* key does not exist in registry */
307     else
308         return 0;                      /* key matched OK in registry */
309 }
310
311 void store_host_key(char *hostname, int port, char *keytype, char *key) {
312     char *regname;
313     HKEY rkey;
314
315     regname = smalloc(3*(strlen(hostname)+strlen(keytype))+15);
316
317     hostkey_regname(regname, hostname, port, 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
384 void write_random_seed(void *data, int len) {
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 }