2 * winstore.c: Windows-specific implementation of the interface
3 * defined in storage.h.
12 static const char *const puttystr = PUTTY_REG_POS "\\Sessions";
14 static char seedpath[2 * MAX_PATH + 10] = "\0";
16 static char hex[16] = "0123456789ABCDEF";
18 static void mungestr(char *in, char *out)
23 if (*in == ' ' || *in == '\\' || *in == '*' || *in == '?' ||
24 *in == '%' || *in < ' ' || *in > '~' || (*in == '.'
27 *out++ = hex[((unsigned char) *in) >> 4];
28 *out++ = hex[((unsigned char) *in) & 15];
38 static void unmungestr(char *in, char *out, int outlen)
41 if (*in == '%' && in[1] && in[2]) {
49 *out++ = (i << 4) + j;
63 void *open_settings_w(char *sessionname)
65 HKEY subkey1, sesskey;
69 p = smalloc(3 * strlen(sessionname) + 1);
70 mungestr(sessionname, p);
72 ret = RegCreateKey(HKEY_CURRENT_USER, puttystr, &subkey1);
73 if (ret != ERROR_SUCCESS) {
77 ret = RegCreateKey(subkey1, p, &sesskey);
80 if (ret != ERROR_SUCCESS)
82 return (void *) sesskey;
85 void write_setting_s(void *handle, char *key, char *value)
88 RegSetValueEx((HKEY) handle, key, 0, REG_SZ, value,
92 void write_setting_i(void *handle, char *key, int value)
95 RegSetValueEx((HKEY) handle, key, 0, REG_DWORD,
96 (CONST BYTE *) & value, sizeof(value));
99 void close_settings_w(void *handle)
101 RegCloseKey((HKEY) handle);
104 void *open_settings_r(char *sessionname)
106 HKEY subkey1, sesskey;
109 p = smalloc(3 * strlen(sessionname) + 1);
110 mungestr(sessionname, p);
112 if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS) {
115 if (RegOpenKey(subkey1, p, &sesskey) != ERROR_SUCCESS) {
118 RegCloseKey(subkey1);
123 return (void *) sesskey;
126 char *read_setting_s(void *handle, char *key, char *buffer, int buflen)
132 RegQueryValueEx((HKEY) handle, key, 0,
133 &type, buffer, &size) != ERROR_SUCCESS ||
134 type != REG_SZ) return NULL;
139 int read_setting_i(void *handle, char *key, int defvalue)
141 DWORD type, val, size;
145 RegQueryValueEx((HKEY) handle, key, 0, &type,
146 (BYTE *) & val, &size) != ERROR_SUCCESS ||
147 size != sizeof(val) || type != REG_DWORD)
153 void close_settings_r(void *handle)
155 RegCloseKey((HKEY) handle);
158 void del_settings(char *sessionname)
163 if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS)
166 p = smalloc(3 * strlen(sessionname) + 1);
167 mungestr(sessionname, p);
168 RegDeleteKey(subkey1, p);
171 RegCloseKey(subkey1);
174 struct enumsettings {
179 void *enum_settings_start(void)
181 struct enumsettings *ret;
184 if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &key) != ERROR_SUCCESS)
187 ret = smalloc(sizeof(*ret));
196 char *enum_settings_next(void *handle, char *buffer, int buflen)
198 struct enumsettings *e = (struct enumsettings *) handle;
200 otherbuf = smalloc(3 * buflen);
201 if (otherbuf && RegEnumKey(e->key, e->i++, otherbuf,
202 3 * buflen) == ERROR_SUCCESS) {
203 unmungestr(otherbuf, buffer, buflen);
211 void enum_settings_finish(void *handle)
213 struct enumsettings *e = (struct enumsettings *) handle;
218 static void hostkey_regname(char *buffer, char *hostname,
219 int port, char *keytype)
222 strcpy(buffer, keytype);
224 len = strlen(buffer);
225 len += sprintf(buffer + len, "%d:", port);
226 mungestr(hostname, buffer + strlen(buffer));
229 int verify_host_key(char *hostname, int port, char *keytype, char *key)
231 char *otherstr, *regname;
238 len = 1 + strlen(key);
241 * Now read a saved key in from the registry and see what it
244 otherstr = smalloc(len);
245 regname = smalloc(3 * (strlen(hostname) + strlen(keytype)) + 15);
247 hostkey_regname(regname, hostname, port, keytype);
249 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys",
250 &rkey) != ERROR_SUCCESS)
251 return 1; /* key does not exist in registry */
254 ret = RegQueryValueEx(rkey, regname, NULL, &type, otherstr, &readlen);
256 if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA &&
257 !strcmp(keytype, "rsa")) {
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.
263 char *justhost = regname + 1 + strcspn(regname, ":");
264 char *oldstyle = smalloc(len + 10); /* safety margin */
266 ret = RegQueryValueEx(rkey, justhost, NULL, &type,
269 if (ret == ERROR_SUCCESS && type == REG_SZ) {
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.
286 for (i = 0; i < 2; i++) {
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)
295 /* now move digits over to new string */
296 for (j = 0; j < ndigits; j++)
297 p[ndigits - 1 - j] = q[j ^ 3];
301 q++; /* eat the slash */
302 *p++ = ','; /* add a comma */
304 *p = '\0'; /* terminate the string */
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.
312 if (!strcmp(otherstr, key))
313 RegSetValueEx(rkey, regname, 0, REG_SZ, otherstr,
314 strlen(otherstr) + 1);
320 compare = strcmp(otherstr, key);
325 if (ret == ERROR_MORE_DATA ||
326 (ret == ERROR_SUCCESS && type == REG_SZ && compare))
327 return 2; /* key is different in registry */
328 else if (ret != ERROR_SUCCESS || type != REG_SZ)
329 return 1; /* key does not exist in registry */
331 return 0; /* key matched OK in registry */
334 void store_host_key(char *hostname, int port, char *keytype, char *key)
339 regname = smalloc(3 * (strlen(hostname) + strlen(keytype)) + 15);
341 hostkey_regname(regname, hostname, port, keytype);
343 if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys",
344 &rkey) != ERROR_SUCCESS)
345 return; /* key does not exist in registry */
346 RegSetValueEx(rkey, regname, 0, REG_SZ, key, strlen(key) + 1);
351 * Find the random seed file path and store it in `seedpath'.
353 static void get_seedpath(void)
358 size = sizeof(seedpath);
360 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &rkey) ==
362 int ret = RegQueryValueEx(rkey, "RandSeedFile",
363 0, &type, seedpath, &size);
364 if (ret != ERROR_SUCCESS || type != REG_SZ)
374 GetEnvironmentVariable("HOMEDRIVE", seedpath,
377 GetEnvironmentVariable("HOMEPATH", seedpath + len,
378 sizeof(seedpath) - len);
379 if (ret == 0) { /* probably win95; store in \WINDOWS */
380 GetWindowsDirectory(seedpath, sizeof(seedpath));
381 len = strlen(seedpath);
384 strcpy(seedpath + len, "\\PUTTY.RND");
388 void read_random_seed(noise_consumer_t consumer)
395 seedf = CreateFile(seedpath, GENERIC_READ,
396 FILE_SHARE_READ | FILE_SHARE_WRITE,
397 NULL, OPEN_EXISTING, 0, NULL);
399 if (seedf != INVALID_HANDLE_VALUE) {
404 if (ReadFile(seedf, buf, sizeof(buf), &len, NULL) && len)
413 void write_random_seed(void *data, int len)
420 seedf = CreateFile(seedpath, GENERIC_WRITE, 0,
421 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
423 if (seedf != INVALID_HANDLE_VALUE) {
426 WriteFile(seedf, data, len, &lenwritten, NULL);
432 * Recursively delete a registry key and everything under it.
434 static void registry_recursive_remove(HKEY key)
437 char name[MAX_PATH + 1];
441 while (RegEnumKey(key, i, name, sizeof(name)) == ERROR_SUCCESS) {
442 if (RegOpenKey(key, name, &subkey) == ERROR_SUCCESS) {
443 registry_recursive_remove(subkey);
446 RegDeleteKey(key, name);
450 void cleanup_all(void)
454 char name[MAX_PATH + 1];
456 /* ------------------------------------------------------------
457 * Wipe out the random seed file.
463 /* ------------------------------------------------------------
464 * Destroy all registry information associated with PuTTY.
468 * Open the main PuTTY registry key and remove everything in it.
470 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &key) ==
472 registry_recursive_remove(key);
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
480 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_PARENT,
481 &key) == ERROR_SUCCESS) {
482 RegDeleteKey(key, PUTTY_REG_PARENT_CHILD);
483 ret = RegEnumKey(key, 0, name, sizeof(name));
486 * If the parent key had no other children, we must delete
487 * it in its turn. That means opening the _grandparent_
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);