2 * winstore.c: Windows-specific implementation of the interface
3 * defined in storage.h.
13 static const char *const puttystr = PUTTY_REG_POS "\\Sessions";
15 static char seedpath[2 * MAX_PATH + 10] = "\0";
17 static const char hex[16] = "0123456789ABCDEF";
19 static void mungestr(const char *in, char *out)
24 if (*in == ' ' || *in == '\\' || *in == '*' || *in == '?' ||
25 *in == '%' || *in < ' ' || *in > '~' || (*in == '.'
28 *out++ = hex[((unsigned char) *in) >> 4];
29 *out++ = hex[((unsigned char) *in) & 15];
39 static void unmungestr(const char *in, char *out, int outlen)
42 if (*in == '%' && in[1] && in[2]) {
50 *out++ = (i << 4) + j;
64 void *open_settings_w(const char *sessionname)
66 HKEY subkey1, sesskey;
70 if (!sessionname || !*sessionname)
71 sessionname = "Default Settings";
73 p = smalloc(3 * strlen(sessionname) + 1);
74 mungestr(sessionname, p);
76 ret = RegCreateKey(HKEY_CURRENT_USER, puttystr, &subkey1);
77 if (ret != ERROR_SUCCESS) {
81 ret = RegCreateKey(subkey1, p, &sesskey);
84 if (ret != ERROR_SUCCESS)
86 return (void *) sesskey;
89 void write_setting_s(void *handle, const char *key, const char *value)
92 RegSetValueEx((HKEY) handle, key, 0, REG_SZ, value,
96 void write_setting_i(void *handle, const char *key, int value)
99 RegSetValueEx((HKEY) handle, key, 0, REG_DWORD,
100 (CONST BYTE *) & value, sizeof(value));
103 void close_settings_w(void *handle)
105 RegCloseKey((HKEY) handle);
108 void *open_settings_r(const char *sessionname)
110 HKEY subkey1, sesskey;
113 if (!sessionname || !*sessionname)
114 sessionname = "Default Settings";
116 p = smalloc(3 * strlen(sessionname) + 1);
117 mungestr(sessionname, p);
119 if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS) {
122 if (RegOpenKey(subkey1, p, &sesskey) != ERROR_SUCCESS) {
125 RegCloseKey(subkey1);
130 return (void *) sesskey;
133 char *read_setting_s(void *handle, const char *key, char *buffer, int buflen)
139 RegQueryValueEx((HKEY) handle, key, 0,
140 &type, buffer, &size) != ERROR_SUCCESS ||
141 type != REG_SZ) return NULL;
146 int read_setting_i(void *handle, const char *key, int defvalue)
148 DWORD type, val, size;
152 RegQueryValueEx((HKEY) handle, key, 0, &type,
153 (BYTE *) & val, &size) != ERROR_SUCCESS ||
154 size != sizeof(val) || type != REG_DWORD)
160 int read_setting_fontspec(void *handle, const char *name, FontSpec *result)
165 if (!read_setting_s(handle, name, ret.name, sizeof(ret.name)))
167 settingname = dupcat(name, "IsBold", NULL);
168 ret.isbold = read_setting_i(handle, settingname, -1);
170 if (ret.isbold == -1) return 0;
171 settingname = dupcat(name, "CharSet", NULL);
172 ret.charset = read_setting_i(handle, settingname, -1);
174 if (ret.charset == -1) return 0;
175 settingname = dupcat(name, "Height", NULL);
176 ret.height = read_setting_i(handle, settingname, INT_MIN);
178 if (ret.height == INT_MIN) return 0;
179 if (ret.height < 0) {
181 HDC hdc = GetDC(NULL);
182 int logpix = GetDeviceCaps(hdc, LOGPIXELSY);
183 ReleaseDC(NULL, hdc);
186 newh = MulDiv(oldh, 72, logpix) + 1;
187 if (MulDiv(newh, logpix, 72) > oldh)
195 void write_setting_fontspec(void *handle, const char *name, FontSpec font)
199 write_setting_s(handle, name, font.name);
200 settingname = dupcat(name, "IsBold", NULL);
201 write_setting_i(handle, settingname, font.isbold);
203 settingname = dupcat(name, "CharSet", NULL);
204 write_setting_i(handle, settingname, font.charset);
206 settingname = dupcat(name, "Height", NULL);
207 write_setting_i(handle, settingname, font.height);
211 int read_setting_filename(void *handle, const char *name, Filename *result)
213 return !!read_setting_s(handle, name, result->path, sizeof(result->path));
216 void write_setting_filename(void *handle, const char *name, Filename result)
218 write_setting_s(handle, name, result.path);
221 void close_settings_r(void *handle)
223 RegCloseKey((HKEY) handle);
226 void del_settings(const char *sessionname)
231 if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS)
234 p = smalloc(3 * strlen(sessionname) + 1);
235 mungestr(sessionname, p);
236 RegDeleteKey(subkey1, p);
239 RegCloseKey(subkey1);
242 struct enumsettings {
247 void *enum_settings_start(void)
249 struct enumsettings *ret;
252 if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &key) != ERROR_SUCCESS)
255 ret = smalloc(sizeof(*ret));
264 char *enum_settings_next(void *handle, char *buffer, int buflen)
266 struct enumsettings *e = (struct enumsettings *) handle;
268 otherbuf = smalloc(3 * buflen);
269 if (RegEnumKey(e->key, e->i++, otherbuf, 3 * buflen) == ERROR_SUCCESS) {
270 unmungestr(otherbuf, buffer, buflen);
279 void enum_settings_finish(void *handle)
281 struct enumsettings *e = (struct enumsettings *) handle;
286 static void hostkey_regname(char *buffer, const char *hostname,
287 int port, const char *keytype)
290 strcpy(buffer, keytype);
292 len = strlen(buffer);
293 len += sprintf(buffer + len, "%d:", port);
294 mungestr(hostname, buffer + strlen(buffer));
297 int verify_host_key(const char *hostname, int port,
298 const char *keytype, const char *key)
300 char *otherstr, *regname;
307 len = 1 + strlen(key);
310 * Now read a saved key in from the registry and see what it
313 otherstr = smalloc(len);
314 regname = smalloc(3 * (strlen(hostname) + strlen(keytype)) + 15);
316 hostkey_regname(regname, hostname, port, keytype);
318 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys",
319 &rkey) != ERROR_SUCCESS)
320 return 1; /* key does not exist in registry */
323 ret = RegQueryValueEx(rkey, regname, NULL, &type, otherstr, &readlen);
325 if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA &&
326 !strcmp(keytype, "rsa")) {
328 * Key didn't exist. If the key type is RSA, we'll try
329 * another trick, which is to look up the _old_ key format
330 * under just the hostname and translate that.
332 char *justhost = regname + 1 + strcspn(regname, ":");
333 char *oldstyle = smalloc(len + 10); /* safety margin */
335 ret = RegQueryValueEx(rkey, justhost, NULL, &type,
338 if (ret == ERROR_SUCCESS && type == REG_SZ) {
340 * The old format is two old-style bignums separated by
341 * a slash. An old-style bignum is made of groups of
342 * four hex digits: digits are ordered in sensible
343 * (most to least significant) order within each group,
344 * but groups are ordered in silly (least to most)
345 * order within the bignum. The new format is two
346 * ordinary C-format hex numbers (0xABCDEFG...XYZ, with
347 * A nonzero except in the special case 0x0, which
348 * doesn't appear anyway in RSA keys) separated by a
349 * comma. All hex digits are lowercase in both formats.
355 for (i = 0; i < 2; i++) {
359 ndigits = strcspn(q, "/"); /* find / or end of string */
360 nwords = ndigits / 4;
361 /* now trim ndigits to remove leading zeros */
362 while (q[(ndigits - 1) ^ 3] == '0' && ndigits > 1)
364 /* now move digits over to new string */
365 for (j = 0; j < ndigits; j++)
366 p[ndigits - 1 - j] = q[j ^ 3];
370 q++; /* eat the slash */
371 *p++ = ','; /* add a comma */
373 *p = '\0'; /* terminate the string */
377 * Now _if_ this key matches, we'll enter it in the new
378 * format. If not, we'll assume something odd went
379 * wrong, and hyper-cautiously do nothing.
381 if (!strcmp(otherstr, key))
382 RegSetValueEx(rkey, regname, 0, REG_SZ, otherstr,
383 strlen(otherstr) + 1);
389 compare = strcmp(otherstr, key);
394 if (ret == ERROR_MORE_DATA ||
395 (ret == ERROR_SUCCESS && type == REG_SZ && compare))
396 return 2; /* key is different in registry */
397 else if (ret != ERROR_SUCCESS || type != REG_SZ)
398 return 1; /* key does not exist in registry */
400 return 0; /* key matched OK in registry */
403 void store_host_key(const char *hostname, int port,
404 const char *keytype, const char *key)
409 regname = smalloc(3 * (strlen(hostname) + strlen(keytype)) + 15);
411 hostkey_regname(regname, hostname, port, keytype);
413 if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys",
414 &rkey) != ERROR_SUCCESS)
415 return; /* key does not exist in registry */
416 RegSetValueEx(rkey, regname, 0, REG_SZ, key, strlen(key) + 1);
421 * Find the random seed file path and store it in `seedpath'.
423 static void get_seedpath(void)
428 size = sizeof(seedpath);
430 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &rkey) ==
432 int ret = RegQueryValueEx(rkey, "RandSeedFile",
433 0, &type, seedpath, &size);
434 if (ret != ERROR_SUCCESS || type != REG_SZ)
444 GetEnvironmentVariable("HOMEDRIVE", seedpath,
447 GetEnvironmentVariable("HOMEPATH", seedpath + len,
448 sizeof(seedpath) - len);
449 if (ret == 0) { /* probably win95; store in \WINDOWS */
450 GetWindowsDirectory(seedpath, sizeof(seedpath));
451 len = strlen(seedpath);
454 strcpy(seedpath + len, "\\PUTTY.RND");
458 void read_random_seed(noise_consumer_t consumer)
465 seedf = CreateFile(seedpath, GENERIC_READ,
466 FILE_SHARE_READ | FILE_SHARE_WRITE,
467 NULL, OPEN_EXISTING, 0, NULL);
469 if (seedf != INVALID_HANDLE_VALUE) {
474 if (ReadFile(seedf, buf, sizeof(buf), &len, NULL) && len)
483 void write_random_seed(void *data, int len)
490 seedf = CreateFile(seedpath, GENERIC_WRITE, 0,
491 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
493 if (seedf != INVALID_HANDLE_VALUE) {
496 WriteFile(seedf, data, len, &lenwritten, NULL);
502 * Recursively delete a registry key and everything under it.
504 static void registry_recursive_remove(HKEY key)
507 char name[MAX_PATH + 1];
511 while (RegEnumKey(key, i, name, sizeof(name)) == ERROR_SUCCESS) {
512 if (RegOpenKey(key, name, &subkey) == ERROR_SUCCESS) {
513 registry_recursive_remove(subkey);
516 RegDeleteKey(key, name);
520 void cleanup_all(void)
524 char name[MAX_PATH + 1];
526 /* ------------------------------------------------------------
527 * Wipe out the random seed file.
533 /* ------------------------------------------------------------
534 * Destroy all registry information associated with PuTTY.
538 * Open the main PuTTY registry key and remove everything in it.
540 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &key) ==
542 registry_recursive_remove(key);
546 * Now open the parent key and remove the PuTTY main key. Once
547 * we've done that, see if the parent key has any other
550 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_PARENT,
551 &key) == ERROR_SUCCESS) {
552 RegDeleteKey(key, PUTTY_REG_PARENT_CHILD);
553 ret = RegEnumKey(key, 0, name, sizeof(name));
556 * If the parent key had no other children, we must delete
557 * it in its turn. That means opening the _grandparent_
560 if (ret != ERROR_SUCCESS) {
561 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_GPARENT,
562 &key) == ERROR_SUCCESS) {
563 RegDeleteKey(key, PUTTY_REG_GPARENT_CHILD);