]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - settings.c
Oops - try increasing some more buffer sizes as well. Bah.
[PuTTY.git] / settings.c
1 /*
2  * settings.c: read and write saved sessions.
3  */
4
5 #include <windows.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include "putty.h"
9 #include "storage.h"
10
11 /*
12  * Tables of string <-> enum value mappings
13  */
14 struct keyval { char *s; int v; };
15
16 static const struct keyval ciphernames[] = {
17     { "aes",        CIPHER_AES },
18     { "blowfish",   CIPHER_BLOWFISH },
19     { "3des",       CIPHER_3DES },
20     { "WARN",       CIPHER_WARN },
21     { "des",        CIPHER_DES }
22 };
23
24 static void gpps(void *handle, char *name, char *def, char *val, int len)
25 {
26     if (!read_setting_s(handle, name, val, len)) {
27         strncpy(val, def, len);
28         val[len - 1] = '\0';
29     }
30 }
31
32 static void gppi(void *handle, char *name, int def, int *i)
33 {
34     *i = read_setting_i(handle, name, def);
35 }
36
37 static int key2val(const struct keyval *mapping, int nmaps, char *key)
38 {
39     int i;
40     for (i = 0; i < nmaps; i++)
41         if (!strcmp(mapping[i].s, key)) return mapping[i].v;
42     return -1;
43 }
44
45 static const char *val2key(const struct keyval *mapping, int nmaps, int val)
46 {
47     int i;
48     for (i = 0; i < nmaps; i++)
49         if (mapping[i].v == val) return mapping[i].s;
50     return NULL;
51 }
52
53 /*
54  * Helper function to parse a comma-separated list of strings into
55  * a preference list array of values. Any missing values are added
56  * to the end and duplicates are weeded.
57  * XXX: assumes vals in 'mapping' are small +ve integers
58  */
59 static void gprefs(void *sesskey, char *name, char *def,
60                    const struct keyval *mapping, int nvals,
61                    int *array)
62 {
63     char commalist[80];
64     int n;
65     unsigned long seen = 0;            /* bitmap for weeding dups etc */
66     gpps(sesskey, name, def, commalist, sizeof(commalist));
67
68     /* Grotty parsing of commalist. */
69     n = 0;
70     do {
71         int v;
72         char *key;
73         key = strtok(n==0 ? commalist : NULL, ","); /* sorry */
74         if (!key) break;
75         if (((v = key2val(mapping, nvals, key)) != -1) &&
76             !(seen & 1<<v)) {
77             array[n] = v;
78             n++;
79             seen |= 1<<v;
80         }
81     } while (n < nvals);
82     /* Add any missing values (backward compatibility ect). */
83     {
84         int i;
85         for (i = 0; i < nvals; i++) {
86             if (!(seen & 1<<mapping[i].v)) {
87                 array[n] = mapping[i].v;
88                 n++;
89             }
90         }
91     }
92 }
93
94 /* 
95  * Write out a preference list.
96  */
97 static void wprefs(void *sesskey, char *name,
98                    const struct keyval *mapping, int nvals,
99                    int *array)
100 {
101     char buf[80] = "";  /* XXX assumed big enough */
102     int l = sizeof(buf)-1, i;
103     buf[l] = '\0';
104     for (i = 0; l > 0 && i < nvals; i++) {
105         const char *s = val2key(mapping, nvals, array[i]);
106         if (s) {
107             int sl = strlen(s);
108             if (i > 0) {
109                 strncat(buf, ",", l);
110                 l--;
111             }
112             strncat(buf, s, l);
113             l -= sl;
114         }
115     }
116     write_setting_s(sesskey, name, buf);
117 }
118
119 void save_settings(char *section, int do_host, Config * cfg)
120 {
121     int i;
122     char *p;
123     void *sesskey;
124
125     sesskey = open_settings_w(section);
126     if (!sesskey)
127         return;
128
129     write_setting_i(sesskey, "Present", 1);
130     if (do_host) {
131         write_setting_s(sesskey, "HostName", cfg->host);
132         write_setting_s(sesskey, "LogFileName", cfg->logfilename);
133         write_setting_i(sesskey, "LogType", cfg->logtype);
134         write_setting_i(sesskey, "LogFileClash", cfg->logxfovr);
135     }
136     p = "raw";
137     for (i = 0; backends[i].name != NULL; i++)
138         if (backends[i].protocol == cfg->protocol) {
139             p = backends[i].name;
140             break;
141         }
142     write_setting_s(sesskey, "Protocol", p);
143     write_setting_i(sesskey, "PortNumber", cfg->port);
144     write_setting_i(sesskey, "CloseOnExit", cfg->close_on_exit);
145     write_setting_i(sesskey, "WarnOnClose", !!cfg->warn_on_close);
146     write_setting_i(sesskey, "PingInterval", cfg->ping_interval / 60);  /* minutes */
147     write_setting_i(sesskey, "PingIntervalSecs", cfg->ping_interval % 60);      /* seconds */
148     write_setting_i(sesskey, "TCPNoDelay", cfg->tcp_nodelay);
149     write_setting_s(sesskey, "TerminalType", cfg->termtype);
150     write_setting_s(sesskey, "TerminalSpeed", cfg->termspeed);
151
152     /* proxy settings */
153     write_setting_s(sesskey, "ProxyExcludeList", cfg->proxy_exclude_list);
154     write_setting_i(sesskey, "ProxyType", cfg->proxy_type);
155     write_setting_s(sesskey, "ProxyHost", cfg->proxy_host);
156     write_setting_i(sesskey, "ProxyPort", cfg->proxy_port);
157     write_setting_s(sesskey, "ProxyUsername", cfg->proxy_username);
158     write_setting_s(sesskey, "ProxyPassword", cfg->proxy_password);
159     write_setting_s(sesskey, "ProxyTelnetCommand", cfg->proxy_telnet_command);
160     write_setting_i(sesskey, "ProxySOCKSVersion", cfg->proxy_socks_version);
161
162     {
163         char buf[2 * sizeof(cfg->environmt)], *p, *q;
164         p = buf;
165         q = cfg->environmt;
166         while (*q) {
167             while (*q) {
168                 int c = *q++;
169                 if (c == '=' || c == ',' || c == '\\')
170                     *p++ = '\\';
171                 if (c == '\t')
172                     c = '=';
173                 *p++ = c;
174             }
175             *p++ = ',';
176             q++;
177         }
178         *p = '\0';
179         write_setting_s(sesskey, "Environment", buf);
180     }
181     write_setting_s(sesskey, "UserName", cfg->username);
182     write_setting_s(sesskey, "LocalUserName", cfg->localusername);
183     write_setting_i(sesskey, "NoPTY", cfg->nopty);
184     write_setting_i(sesskey, "Compression", cfg->compression);
185     write_setting_i(sesskey, "AgentFwd", cfg->agentfwd);
186     write_setting_i(sesskey, "ChangeUsername", cfg->change_username);
187     wprefs(sesskey, "Cipher", ciphernames, CIPHER_MAX,
188            cfg->ssh_cipherlist);
189     write_setting_i(sesskey, "AuthTIS", cfg->try_tis_auth);
190     write_setting_i(sesskey, "AuthKI", cfg->try_ki_auth);
191     write_setting_i(sesskey, "SshProt", cfg->sshprot);
192     write_setting_i(sesskey, "SSH2DES", cfg->ssh2_des_cbc);
193     write_setting_s(sesskey, "PublicKeyFile", cfg->keyfile);
194     write_setting_s(sesskey, "RemoteCommand", cfg->remote_cmd);
195     write_setting_i(sesskey, "RFCEnviron", cfg->rfc_environ);
196     write_setting_i(sesskey, "PassiveTelnet", cfg->passive_telnet);
197     write_setting_i(sesskey, "BackspaceIsDelete", cfg->bksp_is_delete);
198     write_setting_i(sesskey, "RXVTHomeEnd", cfg->rxvt_homeend);
199     write_setting_i(sesskey, "LinuxFunctionKeys", cfg->funky_type);
200     write_setting_i(sesskey, "NoApplicationKeys", cfg->no_applic_k);
201     write_setting_i(sesskey, "NoApplicationCursors", cfg->no_applic_c);
202     write_setting_i(sesskey, "NoMouseReporting", cfg->no_mouse_rep);
203     write_setting_i(sesskey, "NoRemoteResize", cfg->no_remote_resize);
204     write_setting_i(sesskey, "NoAltScreen", cfg->no_alt_screen);
205     write_setting_i(sesskey, "NoRemoteWinTitle", cfg->no_remote_wintitle);
206     write_setting_i(sesskey, "NoDBackspace", cfg->no_dbackspace);
207     write_setting_i(sesskey, "NoRemoteCharset", cfg->no_remote_charset);
208     write_setting_i(sesskey, "ApplicationCursorKeys", cfg->app_cursor);
209     write_setting_i(sesskey, "ApplicationKeypad", cfg->app_keypad);
210     write_setting_i(sesskey, "NetHackKeypad", cfg->nethack_keypad);
211     write_setting_i(sesskey, "AltF4", cfg->alt_f4);
212     write_setting_i(sesskey, "AltSpace", cfg->alt_space);
213     write_setting_i(sesskey, "AltOnly", cfg->alt_only);
214     write_setting_i(sesskey, "ComposeKey", cfg->compose_key);
215     write_setting_i(sesskey, "CtrlAltKeys", cfg->ctrlaltkeys);
216     write_setting_i(sesskey, "TelnetKey", cfg->telnet_keyboard);
217     write_setting_i(sesskey, "TelnetRet", cfg->telnet_newline);
218     write_setting_i(sesskey, "LocalEcho", cfg->localecho);
219     write_setting_i(sesskey, "LocalEdit", cfg->localedit);
220     write_setting_s(sesskey, "Answerback", cfg->answerback);
221     write_setting_i(sesskey, "AlwaysOnTop", cfg->alwaysontop);
222     write_setting_i(sesskey, "FullScreenOnAltEnter", cfg->fullscreenonaltenter);
223     write_setting_i(sesskey, "HideMousePtr", cfg->hide_mouseptr);
224     write_setting_i(sesskey, "SunkenEdge", cfg->sunken_edge);
225     write_setting_i(sesskey, "WindowBorder", cfg->window_border);
226     write_setting_i(sesskey, "CurType", cfg->cursor_type);
227     write_setting_i(sesskey, "BlinkCur", cfg->blink_cur);
228     write_setting_i(sesskey, "Beep", cfg->beep);
229     write_setting_i(sesskey, "BeepInd", cfg->beep_ind);
230     write_setting_s(sesskey, "BellWaveFile", cfg->bell_wavefile);
231     write_setting_i(sesskey, "BellOverload", cfg->bellovl);
232     write_setting_i(sesskey, "BellOverloadN", cfg->bellovl_n);
233     write_setting_i(sesskey, "BellOverloadT", cfg->bellovl_t);
234     write_setting_i(sesskey, "BellOverloadS", cfg->bellovl_s);
235     write_setting_i(sesskey, "ScrollbackLines", cfg->savelines);
236     write_setting_i(sesskey, "DECOriginMode", cfg->dec_om);
237     write_setting_i(sesskey, "AutoWrapMode", cfg->wrap_mode);
238     write_setting_i(sesskey, "LFImpliesCR", cfg->lfhascr);
239     write_setting_i(sesskey, "WinNameAlways", cfg->win_name_always);
240     write_setting_s(sesskey, "WinTitle", cfg->wintitle);
241     write_setting_i(sesskey, "TermWidth", cfg->width);
242     write_setting_i(sesskey, "TermHeight", cfg->height);
243     write_setting_s(sesskey, "Font", cfg->font);
244     write_setting_i(sesskey, "FontIsBold", cfg->fontisbold);
245     write_setting_i(sesskey, "FontCharSet", cfg->fontcharset);
246     write_setting_i(sesskey, "FontHeight", cfg->fontheight);
247     write_setting_i(sesskey, "FontVTMode", cfg->vtmode);
248     write_setting_i(sesskey, "TryPalette", cfg->try_palette);
249     write_setting_i(sesskey, "BoldAsColour", cfg->bold_colour);
250     for (i = 0; i < 22; i++) {
251         char buf[20], buf2[30];
252         sprintf(buf, "Colour%d", i);
253         sprintf(buf2, "%d,%d,%d", cfg->colours[i][0],
254                 cfg->colours[i][1], cfg->colours[i][2]);
255         write_setting_s(sesskey, buf, buf2);
256     }
257     write_setting_i(sesskey, "RawCNP", cfg->rawcnp);
258     write_setting_i(sesskey, "PasteRTF", cfg->rtf_paste);
259     write_setting_i(sesskey, "MouseIsXterm", cfg->mouse_is_xterm);
260     write_setting_i(sesskey, "RectSelect", cfg->rect_select);
261     write_setting_i(sesskey, "MouseOverride", cfg->mouse_override);
262     for (i = 0; i < 256; i += 32) {
263         char buf[20], buf2[256];
264         int j;
265         sprintf(buf, "Wordness%d", i);
266         *buf2 = '\0';
267         for (j = i; j < i + 32; j++) {
268             sprintf(buf2 + strlen(buf2), "%s%d",
269                     (*buf2 ? "," : ""), cfg->wordness[j]);
270         }
271         write_setting_s(sesskey, buf, buf2);
272     }
273     write_setting_s(sesskey, "LineCodePage", cfg->line_codepage);
274     write_setting_s(sesskey, "Printer", cfg->printer);
275     write_setting_i(sesskey, "CapsLockCyr", cfg->xlat_capslockcyr);
276     write_setting_i(sesskey, "ScrollBar", cfg->scrollbar);
277     write_setting_i(sesskey, "ScrollBarFullScreen", cfg->scrollbar_in_fullscreen);
278     write_setting_i(sesskey, "ScrollOnKey", cfg->scroll_on_key);
279     write_setting_i(sesskey, "ScrollOnDisp", cfg->scroll_on_disp);
280     write_setting_i(sesskey, "LockSize", cfg->resize_action);
281     write_setting_i(sesskey, "BCE", cfg->bce);
282     write_setting_i(sesskey, "BlinkText", cfg->blinktext);
283     write_setting_i(sesskey, "X11Forward", cfg->x11_forward);
284     write_setting_s(sesskey, "X11Display", cfg->x11_display);
285     write_setting_i(sesskey, "LocalPortAcceptAll", cfg->lport_acceptall);
286     write_setting_i(sesskey, "RemotePortAcceptAll", cfg->rport_acceptall);
287     {
288         char buf[2 * sizeof(cfg->portfwd)], *p, *q;
289         p = buf;
290         q = cfg->portfwd;
291         while (*q) {
292             while (*q) {
293                 int c = *q++;
294                 if (c == '=' || c == ',' || c == '\\')
295                     *p++ = '\\';
296                 if (c == '\t')
297                     c = '=';
298                 *p++ = c;
299             }
300             *p++ = ',';
301             q++;
302         }
303         *p = '\0';
304         write_setting_s(sesskey, "PortForwardings", buf);
305     }
306     write_setting_i(sesskey, "BugIgnore1", cfg->sshbug_ignore1);
307     write_setting_i(sesskey, "BugPlainPW1", cfg->sshbug_plainpw1);
308     write_setting_i(sesskey, "BugRSA1", cfg->sshbug_rsa1);
309     write_setting_i(sesskey, "BugHMAC2", cfg->sshbug_hmac2);
310     write_setting_i(sesskey, "BugDeriveKey2", cfg->sshbug_derivekey2);
311     write_setting_i(sesskey, "BugRSAPad2", cfg->sshbug_rsapad2);
312     write_setting_i(sesskey, "BugDHGEx2", cfg->sshbug_dhgex2);
313     close_settings_w(sesskey);
314 }
315
316 void load_settings(char *section, int do_host, Config * cfg)
317 {
318     int i;
319     char prot[10];
320     void *sesskey;
321
322     sesskey = open_settings_r(section);
323
324     cfg->ssh_subsys = 0;               /* FIXME: load this properly */
325     cfg->remote_cmd_ptr = cfg->remote_cmd;
326     cfg->remote_cmd_ptr2 = NULL;
327
328     if (do_host) {
329         gpps(sesskey, "HostName", "", cfg->host, sizeof(cfg->host));
330     } else {
331         cfg->host[0] = '\0';           /* blank hostname */
332     }
333     gpps(sesskey, "LogFileName", "putty.log",
334          cfg->logfilename, sizeof(cfg->logfilename));
335     gppi(sesskey, "LogType", 0, &cfg->logtype);
336     gppi(sesskey, "LogFileClash", LGXF_ASK, &cfg->logxfovr);
337
338     gpps(sesskey, "Protocol", "default", prot, 10);
339     cfg->protocol = default_protocol;
340     cfg->port = default_port;
341     for (i = 0; backends[i].name != NULL; i++)
342         if (!strcmp(prot, backends[i].name)) {
343             cfg->protocol = backends[i].protocol;
344             gppi(sesskey, "PortNumber", default_port, &cfg->port);
345             break;
346         }
347
348     gppi(sesskey, "CloseOnExit", COE_NORMAL, &cfg->close_on_exit);
349     gppi(sesskey, "WarnOnClose", 1, &cfg->warn_on_close);
350     {
351         /* This is two values for backward compatibility with 0.50/0.51 */
352         int pingmin, pingsec;
353         gppi(sesskey, "PingInterval", 0, &pingmin);
354         gppi(sesskey, "PingIntervalSecs", 0, &pingsec);
355         cfg->ping_interval = pingmin * 60 + pingsec;
356     }
357     gppi(sesskey, "TCPNoDelay", 1, &cfg->tcp_nodelay);
358     gpps(sesskey, "TerminalType", "xterm", cfg->termtype,
359          sizeof(cfg->termtype));
360     gpps(sesskey, "TerminalSpeed", "38400,38400", cfg->termspeed,
361          sizeof(cfg->termspeed));
362
363     /* proxy settings */
364     gpps(sesskey, "ProxyExcludeList", "", cfg->proxy_exclude_list,
365          sizeof(cfg->proxy_exclude_list));
366     gppi(sesskey, "ProxyType", PROXY_NONE, &cfg->proxy_type);
367     gpps(sesskey, "ProxyHost", "proxy", cfg->proxy_host,
368          sizeof(cfg->proxy_host));
369     gppi(sesskey, "ProxyPort", 80, &cfg->proxy_port);
370     gpps(sesskey, "ProxyUsername", "", cfg->proxy_username,
371          sizeof(cfg->proxy_username));
372     gpps(sesskey, "ProxyPassword", "", cfg->proxy_password,
373          sizeof(cfg->proxy_password));
374     gpps(sesskey, "ProxyTelnetCommand", "connect %host %port\\n",
375          cfg->proxy_telnet_command, sizeof(cfg->proxy_telnet_command));
376     gppi(sesskey, "ProxySOCKSVersion", 5, &cfg->proxy_socks_version);
377
378     {
379         char buf[2 * sizeof(cfg->environmt)], *p, *q;
380         gpps(sesskey, "Environment", "", buf, sizeof(buf));
381         p = buf;
382         q = cfg->environmt;
383         while (*p) {
384             while (*p && *p != ',') {
385                 int c = *p++;
386                 if (c == '=')
387                     c = '\t';
388                 if (c == '\\')
389                     c = *p++;
390                 *q++ = c;
391             }
392             if (*p == ',')
393                 p++;
394             *q++ = '\0';
395         }
396         *q = '\0';
397     }
398     gpps(sesskey, "UserName", "", cfg->username, sizeof(cfg->username));
399     gpps(sesskey, "LocalUserName", "", cfg->localusername,
400          sizeof(cfg->localusername));
401     gppi(sesskey, "NoPTY", 0, &cfg->nopty);
402     gppi(sesskey, "Compression", 0, &cfg->compression);
403     gppi(sesskey, "AgentFwd", 0, &cfg->agentfwd);
404     gppi(sesskey, "ChangeUsername", 0, &cfg->change_username);
405     gprefs(sesskey, "Cipher", "\0",
406            ciphernames, CIPHER_MAX, cfg->ssh_cipherlist);
407     gppi(sesskey, "SshProt", 1, &cfg->sshprot);
408     gppi(sesskey, "SSH2DES", 0, &cfg->ssh2_des_cbc);
409     gppi(sesskey, "AuthTIS", 0, &cfg->try_tis_auth);
410     gppi(sesskey, "AuthKI", 1, &cfg->try_ki_auth);
411     gpps(sesskey, "PublicKeyFile", "", cfg->keyfile, sizeof(cfg->keyfile));
412     gpps(sesskey, "RemoteCommand", "", cfg->remote_cmd,
413          sizeof(cfg->remote_cmd));
414     gppi(sesskey, "RFCEnviron", 0, &cfg->rfc_environ);
415     gppi(sesskey, "PassiveTelnet", 0, &cfg->passive_telnet);
416     gppi(sesskey, "BackspaceIsDelete", 1, &cfg->bksp_is_delete);
417     gppi(sesskey, "RXVTHomeEnd", 0, &cfg->rxvt_homeend);
418     gppi(sesskey, "LinuxFunctionKeys", 0, &cfg->funky_type);
419     gppi(sesskey, "NoApplicationKeys", 0, &cfg->no_applic_k);
420     gppi(sesskey, "NoApplicationCursors", 0, &cfg->no_applic_c);
421     gppi(sesskey, "NoMouseReporting", 0, &cfg->no_mouse_rep);
422     gppi(sesskey, "NoRemoteResize", 0, &cfg->no_remote_resize);
423     gppi(sesskey, "NoAltScreen", 0, &cfg->no_alt_screen);
424     gppi(sesskey, "NoRemoteWinTitle", 0, &cfg->no_remote_wintitle);
425     gppi(sesskey, "NoDBackspace", 0, &cfg->no_dbackspace);
426     gppi(sesskey, "NoRemoteCharset", 0, &cfg->no_remote_charset);
427     gppi(sesskey, "ApplicationCursorKeys", 0, &cfg->app_cursor);
428     gppi(sesskey, "ApplicationKeypad", 0, &cfg->app_keypad);
429     gppi(sesskey, "NetHackKeypad", 0, &cfg->nethack_keypad);
430     gppi(sesskey, "AltF4", 1, &cfg->alt_f4);
431     gppi(sesskey, "AltSpace", 0, &cfg->alt_space);
432     gppi(sesskey, "AltOnly", 0, &cfg->alt_only);
433     gppi(sesskey, "ComposeKey", 0, &cfg->compose_key);
434     gppi(sesskey, "CtrlAltKeys", 1, &cfg->ctrlaltkeys);
435     gppi(sesskey, "TelnetKey", 0, &cfg->telnet_keyboard);
436     gppi(sesskey, "TelnetRet", 1, &cfg->telnet_newline);
437     gppi(sesskey, "LocalEcho", LD_BACKEND, &cfg->localecho);
438     gppi(sesskey, "LocalEdit", LD_BACKEND, &cfg->localedit);
439     gpps(sesskey, "Answerback", "PuTTY", cfg->answerback,
440          sizeof(cfg->answerback));
441     gppi(sesskey, "AlwaysOnTop", 0, &cfg->alwaysontop);
442     gppi(sesskey, "FullScreenOnAltEnter", 0, &cfg->fullscreenonaltenter);
443     gppi(sesskey, "HideMousePtr", 0, &cfg->hide_mouseptr);
444     gppi(sesskey, "SunkenEdge", 0, &cfg->sunken_edge);
445     gppi(sesskey, "WindowBorder", 1, &cfg->window_border);
446     gppi(sesskey, "CurType", 0, &cfg->cursor_type);
447     gppi(sesskey, "BlinkCur", 0, &cfg->blink_cur);
448     /* pedantic compiler tells me I can't use &cfg->beep as an int * :-) */
449     gppi(sesskey, "Beep", 1, &i); cfg->beep = i;
450     gppi(sesskey, "BeepInd", 0, &i); cfg->beep_ind = i;
451     gpps(sesskey, "BellWaveFile", "", cfg->bell_wavefile,
452          sizeof(cfg->bell_wavefile));
453     gppi(sesskey, "BellOverload", 1, &cfg->bellovl);
454     gppi(sesskey, "BellOverloadN", 5, &cfg->bellovl_n);
455     gppi(sesskey, "BellOverloadT", 2000, &cfg->bellovl_t);
456     gppi(sesskey, "BellOverloadS", 5000, &cfg->bellovl_s);
457     gppi(sesskey, "ScrollbackLines", 200, &cfg->savelines);
458     gppi(sesskey, "DECOriginMode", 0, &cfg->dec_om);
459     gppi(sesskey, "AutoWrapMode", 1, &cfg->wrap_mode);
460     gppi(sesskey, "LFImpliesCR", 0, &cfg->lfhascr);
461     gppi(sesskey, "WinNameAlways", 0, &cfg->win_name_always);
462     gpps(sesskey, "WinTitle", "", cfg->wintitle, sizeof(cfg->wintitle));
463     gppi(sesskey, "TermWidth", 80, &cfg->width);
464     gppi(sesskey, "TermHeight", 24, &cfg->height);
465     gpps(sesskey, "Font", "Courier New", cfg->font, sizeof(cfg->font));
466     gppi(sesskey, "FontIsBold", 0, &cfg->fontisbold);
467     gppi(sesskey, "FontCharSet", ANSI_CHARSET, &cfg->fontcharset);
468     gppi(sesskey, "FontHeight", 10, &cfg->fontheight);
469     if (cfg->fontheight < 0) {
470         int oldh, newh;
471         HDC hdc = GetDC(NULL);
472         int logpix = GetDeviceCaps(hdc, LOGPIXELSY);
473         ReleaseDC(NULL, hdc);
474
475         oldh = -cfg->fontheight;
476         newh = MulDiv(oldh, 72, logpix) + 1;
477         if (MulDiv(newh, logpix, 72) > oldh)
478             newh--;
479         cfg->fontheight = newh;
480     }
481     gppi(sesskey, "FontVTMode", VT_UNICODE, (int *) &cfg->vtmode);
482     gppi(sesskey, "TryPalette", 0, &cfg->try_palette);
483     gppi(sesskey, "BoldAsColour", 1, &cfg->bold_colour);
484     for (i = 0; i < 22; i++) {
485         static char *defaults[] = {
486             "187,187,187", "255,255,255", "0,0,0", "85,85,85", "0,0,0",
487             "0,255,0", "0,0,0", "85,85,85", "187,0,0", "255,85,85",
488             "0,187,0", "85,255,85", "187,187,0", "255,255,85", "0,0,187",
489             "85,85,255", "187,0,187", "255,85,255", "0,187,187",
490             "85,255,255", "187,187,187", "255,255,255"
491         };
492         char buf[20], buf2[30];
493         int c0, c1, c2;
494         sprintf(buf, "Colour%d", i);
495         gpps(sesskey, buf, defaults[i], buf2, sizeof(buf2));
496         if (sscanf(buf2, "%d,%d,%d", &c0, &c1, &c2) == 3) {
497             cfg->colours[i][0] = c0;
498             cfg->colours[i][1] = c1;
499             cfg->colours[i][2] = c2;
500         }
501     }
502     gppi(sesskey, "RawCNP", 0, &cfg->rawcnp);
503     gppi(sesskey, "PasteRTF", 0, &cfg->rtf_paste);
504     gppi(sesskey, "MouseIsXterm", 0, &cfg->mouse_is_xterm);
505     gppi(sesskey, "RectSelect", 0, &cfg->rect_select);
506     gppi(sesskey, "MouseOverride", 1, &cfg->mouse_override);
507     for (i = 0; i < 256; i += 32) {
508         static char *defaults[] = {
509             "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0",
510             "0,1,2,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1",
511             "1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,2",
512             "1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1",
513             "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1",
514             "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1",
515             "2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2",
516             "2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2"
517         };
518         char buf[20], buf2[256], *p;
519         int j;
520         sprintf(buf, "Wordness%d", i);
521         gpps(sesskey, buf, defaults[i / 32], buf2, sizeof(buf2));
522         p = buf2;
523         for (j = i; j < i + 32; j++) {
524             char *q = p;
525             while (*p && *p != ',')
526                 p++;
527             if (*p == ',')
528                 *p++ = '\0';
529             cfg->wordness[j] = atoi(q);
530         }
531     }
532     /*
533      * The empty default for LineCodePage will be converted later
534      * into a plausible default for the locale.
535      */
536     gpps(sesskey, "LineCodePage", "", cfg->line_codepage,
537          sizeof(cfg->line_codepage));
538     gpps(sesskey, "Printer", "", cfg->printer, sizeof(cfg->printer));
539     gppi (sesskey, "CapsLockCyr", 0, &cfg->xlat_capslockcyr);
540     gppi(sesskey, "ScrollBar", 1, &cfg->scrollbar);
541     gppi(sesskey, "ScrollBarFullScreen", 0, &cfg->scrollbar_in_fullscreen);
542     gppi(sesskey, "ScrollOnKey", 0, &cfg->scroll_on_key);
543     gppi(sesskey, "ScrollOnDisp", 1, &cfg->scroll_on_disp);
544     gppi(sesskey, "LockSize", 0, &cfg->resize_action);
545     gppi(sesskey, "BCE", 0, &cfg->bce);
546     gppi(sesskey, "BlinkText", 0, &cfg->blinktext);
547     gppi(sesskey, "X11Forward", 0, &cfg->x11_forward);
548     gpps(sesskey, "X11Display", "localhost:0", cfg->x11_display,
549          sizeof(cfg->x11_display));
550
551     gppi(sesskey, "LocalPortAcceptAll", 0, &cfg->lport_acceptall);
552     gppi(sesskey, "RemotePortAcceptAll", 0, &cfg->rport_acceptall);
553     {
554         char buf[2 * sizeof(cfg->portfwd)], *p, *q;
555         gpps(sesskey, "PortForwardings", "", buf, sizeof(buf));
556         p = buf;
557         q = cfg->portfwd;
558         while (*p) {
559             while (*p && *p != ',') {
560                 int c = *p++;
561                 if (c == '=')
562                     c = '\t';
563                 if (c == '\\')
564                     c = *p++;
565                 *q++ = c;
566             }
567             if (*p == ',')
568                 p++;
569             *q++ = '\0';
570         }
571         *q = '\0';
572     }
573     gppi(sesskey, "BugIgnore1", BUG_AUTO, &cfg->sshbug_ignore1);
574     gppi(sesskey, "BugPlainPW1", BUG_AUTO, &cfg->sshbug_plainpw1);
575     gppi(sesskey, "BugRSA1", BUG_AUTO, &cfg->sshbug_rsa1);
576     {
577         int i;
578         gppi(sesskey, "BugHMAC2", BUG_AUTO, &cfg->sshbug_hmac2);
579         if (cfg->sshbug_hmac2 == BUG_AUTO) {
580             gppi(sesskey, "BuggyMAC", 0, &i);
581             if (i == 1)
582                 cfg->sshbug_hmac2 = BUG_ON;
583         }
584     }
585     gppi(sesskey, "BugDeriveKey2", BUG_AUTO, &cfg->sshbug_derivekey2);
586     gppi(sesskey, "BugRSAPad2", BUG_AUTO, &cfg->sshbug_rsapad2);
587     gppi(sesskey, "BugDHGEx2", BUG_AUTO, &cfg->sshbug_dhgex2);
588
589     close_settings_r(sesskey);
590 }
591
592 void do_defaults(char *session, Config * cfg)
593 {
594     if (session)
595         load_settings(session, TRUE, cfg);
596     else
597         load_settings("Default Settings", FALSE, cfg);
598 }
599
600 static int sessioncmp(const void *av, const void *bv)
601 {
602     const char *a = *(const char *const *) av;
603     const char *b = *(const char *const *) bv;
604
605     /*
606      * Alphabetical order, except that "Default Settings" is a
607      * special case and comes first.
608      */
609     if (!strcmp(a, "Default Settings"))
610         return -1;                     /* a comes first */
611     if (!strcmp(b, "Default Settings"))
612         return +1;                     /* b comes first */
613     /*
614      * FIXME: perhaps we should ignore the first & in determining
615      * sort order.
616      */
617     return strcmp(a, b);               /* otherwise, compare normally */
618 }
619
620 void get_sesslist(int allocate)
621 {
622     static char otherbuf[2048];
623     static char *buffer;
624     int buflen, bufsize, i;
625     char *p, *ret;
626     void *handle;
627
628     if (allocate) {
629
630         buflen = bufsize = 0;
631         buffer = NULL;
632         if ((handle = enum_settings_start())) {
633             do {
634                 ret = enum_settings_next(handle, otherbuf, sizeof(otherbuf));
635                 if (ret) {
636                     int len = strlen(otherbuf) + 1;
637                     if (bufsize < buflen + len) {
638                         bufsize = buflen + len + 2048;
639                         buffer = srealloc(buffer, bufsize);
640                     }
641                     strcpy(buffer + buflen, otherbuf);
642                     buflen += strlen(buffer + buflen) + 1;
643                 }
644             } while (ret);
645             enum_settings_finish(handle);
646         }
647         buffer = srealloc(buffer, buflen + 1);
648         buffer[buflen] = '\0';
649
650         /*
651          * Now set up the list of sessions. Note that "Default
652          * Settings" must always be claimed to exist, even if it
653          * doesn't really.
654          */
655
656         p = buffer;
657         nsessions = 1;                 /* "Default Settings" counts as one */
658         while (*p) {
659             if (strcmp(p, "Default Settings"))
660                 nsessions++;
661             while (*p)
662                 p++;
663             p++;
664         }
665
666         sessions = smalloc((nsessions + 1) * sizeof(char *));
667         sessions[0] = "Default Settings";
668         p = buffer;
669         i = 1;
670         while (*p) {
671             if (strcmp(p, "Default Settings"))
672                 sessions[i++] = p;
673             while (*p)
674                 p++;
675             p++;
676         }
677
678         qsort(sessions, i, sizeof(char *), sessioncmp);
679     } else {
680         sfree(buffer);
681         sfree(sessions);
682     }
683 }