]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - settings.c
Don't try SSH-1 by default.
[PuTTY.git] / settings.c
1 /*
2  * settings.c: read and write saved sessions. (platform-independent)
3  */
4
5 #include <assert.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include "putty.h"
9 #include "storage.h"
10
11 /* The cipher order given here is the default order. */
12 static const struct keyvalwhere ciphernames[] = {
13     { "aes",        CIPHER_AES,             -1, -1 },
14     { "blowfish",   CIPHER_BLOWFISH,        -1, -1 },
15     { "3des",       CIPHER_3DES,            -1, -1 },
16     { "WARN",       CIPHER_WARN,            -1, -1 },
17     { "arcfour",    CIPHER_ARCFOUR,         -1, -1 },
18     { "des",        CIPHER_DES,             -1, -1 }
19 };
20
21 static const struct keyvalwhere kexnames[] = {
22     { "dh-gex-sha1",        KEX_DHGEX,      -1, -1 },
23     { "dh-group14-sha1",    KEX_DHGROUP14,  -1, -1 },
24     { "dh-group1-sha1",     KEX_DHGROUP1,   -1, -1 },
25     { "rsa",                KEX_RSA,        KEX_WARN, -1 },
26     { "WARN",               KEX_WARN,       -1, -1 }
27 };
28
29 /*
30  * All the terminal modes that we know about for the "TerminalModes"
31  * setting. (Also used by config.c for the drop-down list.)
32  * This is currently precisely the same as the set in ssh.c, but could
33  * in principle differ if other backends started to support tty modes
34  * (e.g., the pty backend).
35  */
36 const char *const ttymodes[] = {
37     "INTR",     "QUIT",     "ERASE",    "KILL",     "EOF",
38     "EOL",      "EOL2",     "START",    "STOP",     "SUSP",
39     "DSUSP",    "REPRINT",  "WERASE",   "LNEXT",    "FLUSH",
40     "SWTCH",    "STATUS",   "DISCARD",  "IGNPAR",   "PARMRK",
41     "INPCK",    "ISTRIP",   "INLCR",    "IGNCR",    "ICRNL",
42     "IUCLC",    "IXON",     "IXANY",    "IXOFF",    "IMAXBEL",
43     "ISIG",     "ICANON",   "XCASE",    "ECHO",     "ECHOE",
44     "ECHOK",    "ECHONL",   "NOFLSH",   "TOSTOP",   "IEXTEN",
45     "ECHOCTL",  "ECHOKE",   "PENDIN",   "OPOST",    "OLCUC",
46     "ONLCR",    "OCRNL",    "ONOCR",    "ONLRET",   "CS7",
47     "CS8",      "PARENB",   "PARODD",   NULL
48 };
49
50 /*
51  * Convenience functions to access the backends[] array
52  * (which is only present in tools that manage settings).
53  */
54
55 Backend *backend_from_name(const char *name)
56 {
57     Backend **p;
58     for (p = backends; *p != NULL; p++)
59         if (!strcmp((*p)->name, name))
60             return *p;
61     return NULL;
62 }
63
64 Backend *backend_from_proto(int proto)
65 {
66     Backend **p;
67     for (p = backends; *p != NULL; p++)
68         if ((*p)->protocol == proto)
69             return *p;
70     return NULL;
71 }
72
73 char *get_remote_username(Conf *conf)
74 {
75     char *username = conf_get_str(conf, CONF_username);
76     if (*username) {
77         return dupstr(username);
78     } else if (conf_get_int(conf, CONF_username_from_env)) {
79         /* Use local username. */
80         return get_username();     /* might still be NULL */
81     } else {
82         return NULL;
83     }
84 }
85
86 static char *gpps_raw(void *handle, const char *name, const char *def)
87 {
88     char *ret = read_setting_s(handle, name);
89     if (!ret)
90         ret = platform_default_s(name);
91     if (!ret)
92         ret = def ? dupstr(def) : NULL;   /* permit NULL as final fallback */
93     return ret;
94 }
95
96 static void gpps(void *handle, const char *name, const char *def,
97                  Conf *conf, int primary)
98 {
99     char *val = gpps_raw(handle, name, def);
100     conf_set_str(conf, primary, val);
101     sfree(val);
102 }
103
104 /*
105  * gppfont and gppfile cannot have local defaults, since the very
106  * format of a Filename or FontSpec is platform-dependent. So the
107  * platform-dependent functions MUST return some sort of value.
108  */
109 static void gppfont(void *handle, const char *name, Conf *conf, int primary)
110 {
111     FontSpec *result = read_setting_fontspec(handle, name);
112     if (!result)
113         result = platform_default_fontspec(name);
114     conf_set_fontspec(conf, primary, result);
115     fontspec_free(result);
116 }
117 static void gppfile(void *handle, const char *name, Conf *conf, int primary)
118 {
119     Filename *result = read_setting_filename(handle, name);
120     if (!result)
121         result = platform_default_filename(name);
122     conf_set_filename(conf, primary, result);
123     filename_free(result);
124 }
125
126 static int gppi_raw(void *handle, char *name, int def)
127 {
128     def = platform_default_i(name, def);
129     return read_setting_i(handle, name, def);
130 }
131
132 static void gppi(void *handle, char *name, int def, Conf *conf, int primary)
133 {
134     conf_set_int(conf, primary, gppi_raw(handle, name, def));
135 }
136
137 /*
138  * Read a set of name-value pairs in the format we occasionally use:
139  *   NAME\tVALUE\0NAME\tVALUE\0\0 in memory
140  *   NAME=VALUE,NAME=VALUE, in storage
141  * If there's no "=VALUE" (e.g. just NAME,NAME,NAME) then those keys
142  * are mapped to the empty string.
143  */
144 static int gppmap(void *handle, char *name, Conf *conf, int primary)
145 {
146     char *buf, *p, *q, *key, *val;
147
148     /*
149      * Start by clearing any existing subkeys of this key from conf.
150      */
151     while ((key = conf_get_str_nthstrkey(conf, primary, 0)) != NULL)
152         conf_del_str_str(conf, primary, key);
153
154     /*
155      * Now read a serialised list from the settings and unmarshal it
156      * into its components.
157      */
158     buf = gpps_raw(handle, name, NULL);
159     if (!buf)
160         return FALSE;
161
162     p = buf;
163     while (*p) {
164         q = buf;
165         val = NULL;
166         while (*p && *p != ',') {
167             int c = *p++;
168             if (c == '=')
169                 c = '\0';
170             if (c == '\\')
171                 c = *p++;
172             *q++ = c;
173             if (!c)
174                 val = q;
175         }
176         if (*p == ',')
177             p++;
178         if (!val)
179             val = q;
180         *q = '\0';
181
182         if (primary == CONF_portfwd && strchr(buf, 'D') != NULL) {
183             /*
184              * Backwards-compatibility hack: dynamic forwardings are
185              * indexed in the data store as a third type letter in the
186              * key, 'D' alongside 'L' and 'R' - but really, they
187              * should be filed under 'L' with a special _value_,
188              * because local and dynamic forwardings both involve
189              * _listening_ on a local port, and are hence mutually
190              * exclusive on the same port number. So here we translate
191              * the legacy storage format into the sensible internal
192              * form, by finding the D and turning it into a L.
193              */
194             char *newkey = dupstr(buf);
195             *strchr(newkey, 'D') = 'L';
196             conf_set_str_str(conf, primary, newkey, "D");
197             sfree(newkey);
198         } else {
199             conf_set_str_str(conf, primary, buf, val);
200         }
201     }
202     sfree(buf);
203
204     return TRUE;
205 }
206
207 /*
208  * Write a set of name/value pairs in the above format, or just the
209  * names if include_values is FALSE.
210  */
211 static void wmap(void *handle, char const *outkey, Conf *conf, int primary,
212                  int include_values)
213 {
214     char *buf, *p, *q, *key, *realkey, *val;
215     int len;
216
217     len = 1;                           /* allow for NUL */
218
219     for (val = conf_get_str_strs(conf, primary, NULL, &key);
220          val != NULL;
221          val = conf_get_str_strs(conf, primary, key, &key))
222         len += 2 + 2 * (strlen(key) + strlen(val));   /* allow for escaping */
223
224     buf = snewn(len, char);
225     p = buf;
226
227     for (val = conf_get_str_strs(conf, primary, NULL, &key);
228          val != NULL;
229          val = conf_get_str_strs(conf, primary, key, &key)) {
230
231         if (primary == CONF_portfwd && !strcmp(val, "D")) {
232             /*
233              * Backwards-compatibility hack, as above: translate from
234              * the sensible internal representation of dynamic
235              * forwardings (key "L<port>", value "D") to the
236              * conceptually incoherent legacy storage format (key
237              * "D<port>", value empty).
238              */
239             char *L;
240
241             realkey = key;             /* restore it at end of loop */
242             val = "";
243             key = dupstr(key);
244             L = strchr(key, 'L');
245             if (L) *L = 'D';
246         } else {
247             realkey = NULL;
248         }
249
250         if (p != buf)
251             *p++ = ',';
252         for (q = key; *q; q++) {
253             if (*q == '=' || *q == ',' || *q == '\\')
254                 *p++ = '\\';
255             *p++ = *q;
256         }
257         if (include_values) {
258             *p++ = '=';
259             for (q = val; *q; q++) {
260                 if (*q == '=' || *q == ',' || *q == '\\')
261                     *p++ = '\\';
262                 *p++ = *q;
263             }
264         }
265
266         if (realkey) {
267             free(key);
268             key = realkey;
269         }
270     }
271     *p = '\0';
272     write_setting_s(handle, outkey, buf);
273     sfree(buf);
274 }
275
276 static int key2val(const struct keyvalwhere *mapping,
277                    int nmaps, char *key)
278 {
279     int i;
280     for (i = 0; i < nmaps; i++)
281         if (!strcmp(mapping[i].s, key)) return mapping[i].v;
282     return -1;
283 }
284
285 static const char *val2key(const struct keyvalwhere *mapping,
286                            int nmaps, int val)
287 {
288     int i;
289     for (i = 0; i < nmaps; i++)
290         if (mapping[i].v == val) return mapping[i].s;
291     return NULL;
292 }
293
294 /*
295  * Helper function to parse a comma-separated list of strings into
296  * a preference list array of values. Any missing values are added
297  * to the end and duplicates are weeded.
298  * XXX: assumes vals in 'mapping' are small +ve integers
299  */
300 static void gprefs(void *sesskey, char *name, char *def,
301                    const struct keyvalwhere *mapping, int nvals,
302                    Conf *conf, int primary)
303 {
304     char *commalist;
305     char *p, *q;
306     int i, j, n, v, pos;
307     unsigned long seen = 0;            /* bitmap for weeding dups etc */
308
309     /*
310      * Fetch the string which we'll parse as a comma-separated list.
311      */
312     commalist = gpps_raw(sesskey, name, def);
313
314     /*
315      * Go through that list and convert it into values.
316      */
317     n = 0;
318     p = commalist;
319     while (1) {
320         while (*p && *p == ',') p++;
321         if (!*p)
322             break;                     /* no more words */
323
324         q = p;
325         while (*p && *p != ',') p++;
326         if (*p) *p++ = '\0';
327
328         v = key2val(mapping, nvals, q);
329         if (v != -1 && !(seen & (1 << v))) {
330             seen |= (1 << v);
331             conf_set_int_int(conf, primary, n, v);
332             n++;
333         }
334     }
335
336     sfree(commalist);
337
338     /*
339      * Now go through 'mapping' and add values that weren't mentioned
340      * in the list we fetched. We may have to loop over it multiple
341      * times so that we add values before other values whose default
342      * positions depend on them.
343      */
344     while (n < nvals) {
345         for (i = 0; i < nvals; i++) {
346             assert(mapping[i].v < 32);
347
348             if (!(seen & (1 << mapping[i].v))) {
349                 /*
350                  * This element needs adding. But can we add it yet?
351                  */
352                 if (mapping[i].vrel != -1 && !(seen & (1 << mapping[i].vrel)))
353                     continue;          /* nope */
354
355                 /*
356                  * OK, we can work out where to add this element, so
357                  * do so.
358                  */
359                 if (mapping[i].vrel == -1) {
360                     pos = (mapping[i].where < 0 ? n : 0);
361                 } else {
362                     for (j = 0; j < n; j++)
363                         if (conf_get_int_int(conf, primary, j) ==
364                             mapping[i].vrel)
365                             break;
366                     assert(j < n);     /* implied by (seen & (1<<vrel)) */
367                     pos = (mapping[i].where < 0 ? j : j+1);
368                 }
369
370                 /*
371                  * And add it.
372                  */
373                 for (j = n-1; j >= pos; j--)
374                     conf_set_int_int(conf, primary, j+1,
375                                      conf_get_int_int(conf, primary, j));
376                 conf_set_int_int(conf, primary, pos, mapping[i].v);
377                 n++;
378             }
379         }
380     }
381 }
382
383 /* 
384  * Write out a preference list.
385  */
386 static void wprefs(void *sesskey, char *name,
387                    const struct keyvalwhere *mapping, int nvals,
388                    Conf *conf, int primary)
389 {
390     char *buf, *p;
391     int i, maxlen;
392
393     for (maxlen = i = 0; i < nvals; i++) {
394         const char *s = val2key(mapping, nvals,
395                                 conf_get_int_int(conf, primary, i));
396         if (s) {
397             maxlen += (maxlen > 0 ? 1 : 0) + strlen(s);
398         }
399     }
400
401     buf = snewn(maxlen + 1, char);
402     p = buf;
403
404     for (i = 0; i < nvals; i++) {
405         const char *s = val2key(mapping, nvals,
406                                 conf_get_int_int(conf, primary, i));
407         if (s) {
408             p += sprintf(p, "%s%s", (p > buf ? "," : ""), s);
409         }
410     }
411
412     assert(p - buf == maxlen);
413     *p = '\0';
414
415     write_setting_s(sesskey, name, buf);
416
417     sfree(buf);
418 }
419
420 char *save_settings(char *section, Conf *conf)
421 {
422     void *sesskey;
423     char *errmsg;
424
425     sesskey = open_settings_w(section, &errmsg);
426     if (!sesskey)
427         return errmsg;
428     save_open_settings(sesskey, conf);
429     close_settings_w(sesskey);
430     return NULL;
431 }
432
433 void save_open_settings(void *sesskey, Conf *conf)
434 {
435     int i;
436     char *p;
437
438     write_setting_i(sesskey, "Present", 1);
439     write_setting_s(sesskey, "HostName", conf_get_str(conf, CONF_host));
440     write_setting_filename(sesskey, "LogFileName", conf_get_filename(conf, CONF_logfilename));
441     write_setting_i(sesskey, "LogType", conf_get_int(conf, CONF_logtype));
442     write_setting_i(sesskey, "LogFileClash", conf_get_int(conf, CONF_logxfovr));
443     write_setting_i(sesskey, "LogFlush", conf_get_int(conf, CONF_logflush));
444     write_setting_i(sesskey, "SSHLogOmitPasswords", conf_get_int(conf, CONF_logomitpass));
445     write_setting_i(sesskey, "SSHLogOmitData", conf_get_int(conf, CONF_logomitdata));
446     p = "raw";
447     {
448         const Backend *b = backend_from_proto(conf_get_int(conf, CONF_protocol));
449         if (b)
450             p = b->name;
451     }
452     write_setting_s(sesskey, "Protocol", p);
453     write_setting_i(sesskey, "PortNumber", conf_get_int(conf, CONF_port));
454     /* The CloseOnExit numbers are arranged in a different order from
455      * the standard FORCE_ON / FORCE_OFF / AUTO. */
456     write_setting_i(sesskey, "CloseOnExit", (conf_get_int(conf, CONF_close_on_exit)+2)%3);
457     write_setting_i(sesskey, "WarnOnClose", !!conf_get_int(conf, CONF_warn_on_close));
458     write_setting_i(sesskey, "PingInterval", conf_get_int(conf, CONF_ping_interval) / 60);      /* minutes */
459     write_setting_i(sesskey, "PingIntervalSecs", conf_get_int(conf, CONF_ping_interval) % 60);  /* seconds */
460     write_setting_i(sesskey, "TCPNoDelay", conf_get_int(conf, CONF_tcp_nodelay));
461     write_setting_i(sesskey, "TCPKeepalives", conf_get_int(conf, CONF_tcp_keepalives));
462     write_setting_s(sesskey, "TerminalType", conf_get_str(conf, CONF_termtype));
463     write_setting_s(sesskey, "TerminalSpeed", conf_get_str(conf, CONF_termspeed));
464     wmap(sesskey, "TerminalModes", conf, CONF_ttymodes, TRUE);
465
466     /* Address family selection */
467     write_setting_i(sesskey, "AddressFamily", conf_get_int(conf, CONF_addressfamily));
468
469     /* proxy settings */
470     write_setting_s(sesskey, "ProxyExcludeList", conf_get_str(conf, CONF_proxy_exclude_list));
471     write_setting_i(sesskey, "ProxyDNS", (conf_get_int(conf, CONF_proxy_dns)+2)%3);
472     write_setting_i(sesskey, "ProxyLocalhost", conf_get_int(conf, CONF_even_proxy_localhost));
473     write_setting_i(sesskey, "ProxyMethod", conf_get_int(conf, CONF_proxy_type));
474     write_setting_s(sesskey, "ProxyHost", conf_get_str(conf, CONF_proxy_host));
475     write_setting_i(sesskey, "ProxyPort", conf_get_int(conf, CONF_proxy_port));
476     write_setting_s(sesskey, "ProxyUsername", conf_get_str(conf, CONF_proxy_username));
477     write_setting_s(sesskey, "ProxyPassword", conf_get_str(conf, CONF_proxy_password));
478     write_setting_s(sesskey, "ProxyTelnetCommand", conf_get_str(conf, CONF_proxy_telnet_command));
479     wmap(sesskey, "Environment", conf, CONF_environmt, TRUE);
480     write_setting_s(sesskey, "UserName", conf_get_str(conf, CONF_username));
481     write_setting_i(sesskey, "UserNameFromEnvironment", conf_get_int(conf, CONF_username_from_env));
482     write_setting_s(sesskey, "LocalUserName", conf_get_str(conf, CONF_localusername));
483     write_setting_i(sesskey, "NoPTY", conf_get_int(conf, CONF_nopty));
484     write_setting_i(sesskey, "Compression", conf_get_int(conf, CONF_compression));
485     write_setting_i(sesskey, "TryAgent", conf_get_int(conf, CONF_tryagent));
486     write_setting_i(sesskey, "AgentFwd", conf_get_int(conf, CONF_agentfwd));
487     write_setting_i(sesskey, "GssapiFwd", conf_get_int(conf, CONF_gssapifwd));
488     write_setting_i(sesskey, "ChangeUsername", conf_get_int(conf, CONF_change_username));
489     wprefs(sesskey, "Cipher", ciphernames, CIPHER_MAX, conf, CONF_ssh_cipherlist);
490     wprefs(sesskey, "KEX", kexnames, KEX_MAX, conf, CONF_ssh_kexlist);
491     write_setting_i(sesskey, "RekeyTime", conf_get_int(conf, CONF_ssh_rekey_time));
492     write_setting_s(sesskey, "RekeyBytes", conf_get_str(conf, CONF_ssh_rekey_data));
493     write_setting_i(sesskey, "SshNoAuth", conf_get_int(conf, CONF_ssh_no_userauth));
494     write_setting_i(sesskey, "SshBanner", conf_get_int(conf, CONF_ssh_show_banner));
495     write_setting_i(sesskey, "AuthTIS", conf_get_int(conf, CONF_try_tis_auth));
496     write_setting_i(sesskey, "AuthKI", conf_get_int(conf, CONF_try_ki_auth));
497     write_setting_i(sesskey, "AuthGSSAPI", conf_get_int(conf, CONF_try_gssapi_auth));
498 #ifndef NO_GSSAPI
499     wprefs(sesskey, "GSSLibs", gsslibkeywords, ngsslibs, conf, CONF_ssh_gsslist);
500     write_setting_filename(sesskey, "GSSCustom", conf_get_filename(conf, CONF_ssh_gss_custom));
501 #endif
502     write_setting_i(sesskey, "SshNoShell", conf_get_int(conf, CONF_ssh_no_shell));
503     write_setting_i(sesskey, "SshProt", conf_get_int(conf, CONF_sshprot));
504     write_setting_s(sesskey, "LogHost", conf_get_str(conf, CONF_loghost));
505     write_setting_i(sesskey, "SSH2DES", conf_get_int(conf, CONF_ssh2_des_cbc));
506     write_setting_filename(sesskey, "PublicKeyFile", conf_get_filename(conf, CONF_keyfile));
507     write_setting_s(sesskey, "RemoteCommand", conf_get_str(conf, CONF_remote_cmd));
508     write_setting_i(sesskey, "RFCEnviron", conf_get_int(conf, CONF_rfc_environ));
509     write_setting_i(sesskey, "PassiveTelnet", conf_get_int(conf, CONF_passive_telnet));
510     write_setting_i(sesskey, "BackspaceIsDelete", conf_get_int(conf, CONF_bksp_is_delete));
511     write_setting_i(sesskey, "RXVTHomeEnd", conf_get_int(conf, CONF_rxvt_homeend));
512     write_setting_i(sesskey, "LinuxFunctionKeys", conf_get_int(conf, CONF_funky_type));
513     write_setting_i(sesskey, "NoApplicationKeys", conf_get_int(conf, CONF_no_applic_k));
514     write_setting_i(sesskey, "NoApplicationCursors", conf_get_int(conf, CONF_no_applic_c));
515     write_setting_i(sesskey, "NoMouseReporting", conf_get_int(conf, CONF_no_mouse_rep));
516     write_setting_i(sesskey, "NoRemoteResize", conf_get_int(conf, CONF_no_remote_resize));
517     write_setting_i(sesskey, "NoAltScreen", conf_get_int(conf, CONF_no_alt_screen));
518     write_setting_i(sesskey, "NoRemoteWinTitle", conf_get_int(conf, CONF_no_remote_wintitle));
519     write_setting_i(sesskey, "RemoteQTitleAction", conf_get_int(conf, CONF_remote_qtitle_action));
520     write_setting_i(sesskey, "NoDBackspace", conf_get_int(conf, CONF_no_dbackspace));
521     write_setting_i(sesskey, "NoRemoteCharset", conf_get_int(conf, CONF_no_remote_charset));
522     write_setting_i(sesskey, "ApplicationCursorKeys", conf_get_int(conf, CONF_app_cursor));
523     write_setting_i(sesskey, "ApplicationKeypad", conf_get_int(conf, CONF_app_keypad));
524     write_setting_i(sesskey, "NetHackKeypad", conf_get_int(conf, CONF_nethack_keypad));
525     write_setting_i(sesskey, "AltF4", conf_get_int(conf, CONF_alt_f4));
526     write_setting_i(sesskey, "AltSpace", conf_get_int(conf, CONF_alt_space));
527     write_setting_i(sesskey, "AltOnly", conf_get_int(conf, CONF_alt_only));
528     write_setting_i(sesskey, "ComposeKey", conf_get_int(conf, CONF_compose_key));
529     write_setting_i(sesskey, "CtrlAltKeys", conf_get_int(conf, CONF_ctrlaltkeys));
530     write_setting_i(sesskey, "TelnetKey", conf_get_int(conf, CONF_telnet_keyboard));
531     write_setting_i(sesskey, "TelnetRet", conf_get_int(conf, CONF_telnet_newline));
532     write_setting_i(sesskey, "LocalEcho", conf_get_int(conf, CONF_localecho));
533     write_setting_i(sesskey, "LocalEdit", conf_get_int(conf, CONF_localedit));
534     write_setting_s(sesskey, "Answerback", conf_get_str(conf, CONF_answerback));
535     write_setting_i(sesskey, "AlwaysOnTop", conf_get_int(conf, CONF_alwaysontop));
536     write_setting_i(sesskey, "FullScreenOnAltEnter", conf_get_int(conf, CONF_fullscreenonaltenter));
537     write_setting_i(sesskey, "HideMousePtr", conf_get_int(conf, CONF_hide_mouseptr));
538     write_setting_i(sesskey, "SunkenEdge", conf_get_int(conf, CONF_sunken_edge));
539     write_setting_i(sesskey, "WindowBorder", conf_get_int(conf, CONF_window_border));
540     write_setting_i(sesskey, "CurType", conf_get_int(conf, CONF_cursor_type));
541     write_setting_i(sesskey, "BlinkCur", conf_get_int(conf, CONF_blink_cur));
542     write_setting_i(sesskey, "Beep", conf_get_int(conf, CONF_beep));
543     write_setting_i(sesskey, "BeepInd", conf_get_int(conf, CONF_beep_ind));
544     write_setting_filename(sesskey, "BellWaveFile", conf_get_filename(conf, CONF_bell_wavefile));
545     write_setting_i(sesskey, "BellOverload", conf_get_int(conf, CONF_bellovl));
546     write_setting_i(sesskey, "BellOverloadN", conf_get_int(conf, CONF_bellovl_n));
547     write_setting_i(sesskey, "BellOverloadT", conf_get_int(conf, CONF_bellovl_t)
548 #ifdef PUTTY_UNIX_H
549                     * 1000
550 #endif
551                     );
552     write_setting_i(sesskey, "BellOverloadS", conf_get_int(conf, CONF_bellovl_s)
553 #ifdef PUTTY_UNIX_H
554                     * 1000
555 #endif
556                     );
557     write_setting_i(sesskey, "ScrollbackLines", conf_get_int(conf, CONF_savelines));
558     write_setting_i(sesskey, "DECOriginMode", conf_get_int(conf, CONF_dec_om));
559     write_setting_i(sesskey, "AutoWrapMode", conf_get_int(conf, CONF_wrap_mode));
560     write_setting_i(sesskey, "LFImpliesCR", conf_get_int(conf, CONF_lfhascr));
561     write_setting_i(sesskey, "CRImpliesLF", conf_get_int(conf, CONF_crhaslf));
562     write_setting_i(sesskey, "DisableArabicShaping", conf_get_int(conf, CONF_arabicshaping));
563     write_setting_i(sesskey, "DisableBidi", conf_get_int(conf, CONF_bidi));
564     write_setting_i(sesskey, "WinNameAlways", conf_get_int(conf, CONF_win_name_always));
565     write_setting_s(sesskey, "WinTitle", conf_get_str(conf, CONF_wintitle));
566     write_setting_i(sesskey, "TermWidth", conf_get_int(conf, CONF_width));
567     write_setting_i(sesskey, "TermHeight", conf_get_int(conf, CONF_height));
568     write_setting_fontspec(sesskey, "Font", conf_get_fontspec(conf, CONF_font));
569     write_setting_i(sesskey, "FontQuality", conf_get_int(conf, CONF_font_quality));
570     write_setting_i(sesskey, "FontVTMode", conf_get_int(conf, CONF_vtmode));
571     write_setting_i(sesskey, "UseSystemColours", conf_get_int(conf, CONF_system_colour));
572     write_setting_i(sesskey, "TryPalette", conf_get_int(conf, CONF_try_palette));
573     write_setting_i(sesskey, "ANSIColour", conf_get_int(conf, CONF_ansi_colour));
574     write_setting_i(sesskey, "Xterm256Colour", conf_get_int(conf, CONF_xterm_256_colour));
575     write_setting_i(sesskey, "BoldAsColour", conf_get_int(conf, CONF_bold_style)-1);
576
577     for (i = 0; i < 22; i++) {
578         char buf[20], buf2[30];
579         sprintf(buf, "Colour%d", i);
580         sprintf(buf2, "%d,%d,%d",
581                 conf_get_int_int(conf, CONF_colours, i*3+0),
582                 conf_get_int_int(conf, CONF_colours, i*3+1),
583                 conf_get_int_int(conf, CONF_colours, i*3+2));
584         write_setting_s(sesskey, buf, buf2);
585     }
586     write_setting_i(sesskey, "RawCNP", conf_get_int(conf, CONF_rawcnp));
587     write_setting_i(sesskey, "PasteRTF", conf_get_int(conf, CONF_rtf_paste));
588     write_setting_i(sesskey, "MouseIsXterm", conf_get_int(conf, CONF_mouse_is_xterm));
589     write_setting_i(sesskey, "RectSelect", conf_get_int(conf, CONF_rect_select));
590     write_setting_i(sesskey, "MouseOverride", conf_get_int(conf, CONF_mouse_override));
591     for (i = 0; i < 256; i += 32) {
592         char buf[20], buf2[256];
593         int j;
594         sprintf(buf, "Wordness%d", i);
595         *buf2 = '\0';
596         for (j = i; j < i + 32; j++) {
597             sprintf(buf2 + strlen(buf2), "%s%d",
598                     (*buf2 ? "," : ""),
599                     conf_get_int_int(conf, CONF_wordness, j));
600         }
601         write_setting_s(sesskey, buf, buf2);
602     }
603     write_setting_s(sesskey, "LineCodePage", conf_get_str(conf, CONF_line_codepage));
604     write_setting_i(sesskey, "CJKAmbigWide", conf_get_int(conf, CONF_cjk_ambig_wide));
605     write_setting_i(sesskey, "UTF8Override", conf_get_int(conf, CONF_utf8_override));
606     write_setting_s(sesskey, "Printer", conf_get_str(conf, CONF_printer));
607     write_setting_i(sesskey, "CapsLockCyr", conf_get_int(conf, CONF_xlat_capslockcyr));
608     write_setting_i(sesskey, "ScrollBar", conf_get_int(conf, CONF_scrollbar));
609     write_setting_i(sesskey, "ScrollBarFullScreen", conf_get_int(conf, CONF_scrollbar_in_fullscreen));
610     write_setting_i(sesskey, "ScrollOnKey", conf_get_int(conf, CONF_scroll_on_key));
611     write_setting_i(sesskey, "ScrollOnDisp", conf_get_int(conf, CONF_scroll_on_disp));
612     write_setting_i(sesskey, "EraseToScrollback", conf_get_int(conf, CONF_erase_to_scrollback));
613     write_setting_i(sesskey, "LockSize", conf_get_int(conf, CONF_resize_action));
614     write_setting_i(sesskey, "BCE", conf_get_int(conf, CONF_bce));
615     write_setting_i(sesskey, "BlinkText", conf_get_int(conf, CONF_blinktext));
616     write_setting_i(sesskey, "X11Forward", conf_get_int(conf, CONF_x11_forward));
617     write_setting_s(sesskey, "X11Display", conf_get_str(conf, CONF_x11_display));
618     write_setting_i(sesskey, "X11AuthType", conf_get_int(conf, CONF_x11_auth));
619     write_setting_filename(sesskey, "X11AuthFile", conf_get_filename(conf, CONF_xauthfile));
620     write_setting_i(sesskey, "LocalPortAcceptAll", conf_get_int(conf, CONF_lport_acceptall));
621     write_setting_i(sesskey, "RemotePortAcceptAll", conf_get_int(conf, CONF_rport_acceptall));
622     wmap(sesskey, "PortForwardings", conf, CONF_portfwd, TRUE);
623     write_setting_i(sesskey, "BugIgnore1", 2-conf_get_int(conf, CONF_sshbug_ignore1));
624     write_setting_i(sesskey, "BugPlainPW1", 2-conf_get_int(conf, CONF_sshbug_plainpw1));
625     write_setting_i(sesskey, "BugRSA1", 2-conf_get_int(conf, CONF_sshbug_rsa1));
626     write_setting_i(sesskey, "BugIgnore2", 2-conf_get_int(conf, CONF_sshbug_ignore2));
627     write_setting_i(sesskey, "BugHMAC2", 2-conf_get_int(conf, CONF_sshbug_hmac2));
628     write_setting_i(sesskey, "BugDeriveKey2", 2-conf_get_int(conf, CONF_sshbug_derivekey2));
629     write_setting_i(sesskey, "BugRSAPad2", 2-conf_get_int(conf, CONF_sshbug_rsapad2));
630     write_setting_i(sesskey, "BugPKSessID2", 2-conf_get_int(conf, CONF_sshbug_pksessid2));
631     write_setting_i(sesskey, "BugRekey2", 2-conf_get_int(conf, CONF_sshbug_rekey2));
632     write_setting_i(sesskey, "BugMaxPkt2", 2-conf_get_int(conf, CONF_sshbug_maxpkt2));
633     write_setting_i(sesskey, "BugWinadj", 2-conf_get_int(conf, CONF_sshbug_winadj));
634     write_setting_i(sesskey, "BugChanReq", 2-conf_get_int(conf, CONF_sshbug_chanreq));
635     write_setting_i(sesskey, "StampUtmp", conf_get_int(conf, CONF_stamp_utmp));
636     write_setting_i(sesskey, "LoginShell", conf_get_int(conf, CONF_login_shell));
637     write_setting_i(sesskey, "ScrollbarOnLeft", conf_get_int(conf, CONF_scrollbar_on_left));
638     write_setting_fontspec(sesskey, "BoldFont", conf_get_fontspec(conf, CONF_boldfont));
639     write_setting_fontspec(sesskey, "WideFont", conf_get_fontspec(conf, CONF_widefont));
640     write_setting_fontspec(sesskey, "WideBoldFont", conf_get_fontspec(conf, CONF_wideboldfont));
641     write_setting_i(sesskey, "ShadowBold", conf_get_int(conf, CONF_shadowbold));
642     write_setting_i(sesskey, "ShadowBoldOffset", conf_get_int(conf, CONF_shadowboldoffset));
643     write_setting_s(sesskey, "SerialLine", conf_get_str(conf, CONF_serline));
644     write_setting_i(sesskey, "SerialSpeed", conf_get_int(conf, CONF_serspeed));
645     write_setting_i(sesskey, "SerialDataBits", conf_get_int(conf, CONF_serdatabits));
646     write_setting_i(sesskey, "SerialStopHalfbits", conf_get_int(conf, CONF_serstopbits));
647     write_setting_i(sesskey, "SerialParity", conf_get_int(conf, CONF_serparity));
648     write_setting_i(sesskey, "SerialFlowControl", conf_get_int(conf, CONF_serflow));
649     write_setting_s(sesskey, "WindowClass", conf_get_str(conf, CONF_winclass));
650     write_setting_i(sesskey, "ConnectionSharing", conf_get_int(conf, CONF_ssh_connection_sharing));
651     write_setting_i(sesskey, "ConnectionSharingUpstream", conf_get_int(conf, CONF_ssh_connection_sharing_upstream));
652     write_setting_i(sesskey, "ConnectionSharingDownstream", conf_get_int(conf, CONF_ssh_connection_sharing_downstream));
653     wmap(sesskey, "SSHManualHostKeys", conf, CONF_ssh_manual_hostkeys, FALSE);
654 }
655
656 void load_settings(char *section, Conf *conf)
657 {
658     void *sesskey;
659
660     sesskey = open_settings_r(section);
661     load_open_settings(sesskey, conf);
662     close_settings_r(sesskey);
663
664     if (conf_launchable(conf))
665         add_session_to_jumplist(section);
666 }
667
668 void load_open_settings(void *sesskey, Conf *conf)
669 {
670     int i;
671     char *prot;
672
673     conf_set_int(conf, CONF_ssh_subsys, 0);   /* FIXME: load this properly */
674     conf_set_str(conf, CONF_remote_cmd, "");
675     conf_set_str(conf, CONF_remote_cmd2, "");
676     conf_set_str(conf, CONF_ssh_nc_host, "");
677
678     gpps(sesskey, "HostName", "", conf, CONF_host);
679     gppfile(sesskey, "LogFileName", conf, CONF_logfilename);
680     gppi(sesskey, "LogType", 0, conf, CONF_logtype);
681     gppi(sesskey, "LogFileClash", LGXF_ASK, conf, CONF_logxfovr);
682     gppi(sesskey, "LogFlush", 1, conf, CONF_logflush);
683     gppi(sesskey, "SSHLogOmitPasswords", 1, conf, CONF_logomitpass);
684     gppi(sesskey, "SSHLogOmitData", 0, conf, CONF_logomitdata);
685
686     prot = gpps_raw(sesskey, "Protocol", "default");
687     conf_set_int(conf, CONF_protocol, default_protocol);
688     conf_set_int(conf, CONF_port, default_port);
689     {
690         const Backend *b = backend_from_name(prot);
691         if (b) {
692             conf_set_int(conf, CONF_protocol, b->protocol);
693             gppi(sesskey, "PortNumber", default_port, conf, CONF_port);
694         }
695     }
696     sfree(prot);
697
698     /* Address family selection */
699     gppi(sesskey, "AddressFamily", ADDRTYPE_UNSPEC, conf, CONF_addressfamily);
700
701     /* The CloseOnExit numbers are arranged in a different order from
702      * the standard FORCE_ON / FORCE_OFF / AUTO. */
703     i = gppi_raw(sesskey, "CloseOnExit", 1); conf_set_int(conf, CONF_close_on_exit, (i+1)%3);
704     gppi(sesskey, "WarnOnClose", 1, conf, CONF_warn_on_close);
705     {
706         /* This is two values for backward compatibility with 0.50/0.51 */
707         int pingmin, pingsec;
708         pingmin = gppi_raw(sesskey, "PingInterval", 0);
709         pingsec = gppi_raw(sesskey, "PingIntervalSecs", 0);
710         conf_set_int(conf, CONF_ping_interval, pingmin * 60 + pingsec);
711     }
712     gppi(sesskey, "TCPNoDelay", 1, conf, CONF_tcp_nodelay);
713     gppi(sesskey, "TCPKeepalives", 0, conf, CONF_tcp_keepalives);
714     gpps(sesskey, "TerminalType", "xterm", conf, CONF_termtype);
715     gpps(sesskey, "TerminalSpeed", "38400,38400", conf, CONF_termspeed);
716     if (!gppmap(sesskey, "TerminalModes", conf, CONF_ttymodes)) {
717         /* This hardcodes a big set of defaults in any new saved
718          * sessions. Let's hope we don't change our mind. */
719         for (i = 0; ttymodes[i]; i++)
720             conf_set_str_str(conf, CONF_ttymodes, ttymodes[i], "A");
721     }
722
723     /* proxy settings */
724     gpps(sesskey, "ProxyExcludeList", "", conf, CONF_proxy_exclude_list);
725     i = gppi_raw(sesskey, "ProxyDNS", 1); conf_set_int(conf, CONF_proxy_dns, (i+1)%3);
726     gppi(sesskey, "ProxyLocalhost", 0, conf, CONF_even_proxy_localhost);
727     gppi(sesskey, "ProxyMethod", -1, conf, CONF_proxy_type);
728     if (conf_get_int(conf, CONF_proxy_type) == -1) {
729         int i;
730         i = gppi_raw(sesskey, "ProxyType", 0);
731         if (i == 0)
732             conf_set_int(conf, CONF_proxy_type, PROXY_NONE);
733         else if (i == 1)
734             conf_set_int(conf, CONF_proxy_type, PROXY_HTTP);
735         else if (i == 3)
736             conf_set_int(conf, CONF_proxy_type, PROXY_TELNET);
737         else if (i == 4)
738             conf_set_int(conf, CONF_proxy_type, PROXY_CMD);
739         else {
740             i = gppi_raw(sesskey, "ProxySOCKSVersion", 5);
741             if (i == 5)
742                 conf_set_int(conf, CONF_proxy_type, PROXY_SOCKS5);
743             else
744                 conf_set_int(conf, CONF_proxy_type, PROXY_SOCKS4);
745         }
746     }
747     gpps(sesskey, "ProxyHost", "proxy", conf, CONF_proxy_host);
748     gppi(sesskey, "ProxyPort", 80, conf, CONF_proxy_port);
749     gpps(sesskey, "ProxyUsername", "", conf, CONF_proxy_username);
750     gpps(sesskey, "ProxyPassword", "", conf, CONF_proxy_password);
751     gpps(sesskey, "ProxyTelnetCommand", "connect %host %port\\n",
752          conf, CONF_proxy_telnet_command);
753     gppmap(sesskey, "Environment", conf, CONF_environmt);
754     gpps(sesskey, "UserName", "", conf, CONF_username);
755     gppi(sesskey, "UserNameFromEnvironment", 0, conf, CONF_username_from_env);
756     gpps(sesskey, "LocalUserName", "", conf, CONF_localusername);
757     gppi(sesskey, "NoPTY", 0, conf, CONF_nopty);
758     gppi(sesskey, "Compression", 0, conf, CONF_compression);
759     gppi(sesskey, "TryAgent", 1, conf, CONF_tryagent);
760     gppi(sesskey, "AgentFwd", 0, conf, CONF_agentfwd);
761     gppi(sesskey, "ChangeUsername", 0, conf, CONF_change_username);
762     gppi(sesskey, "GssapiFwd", 0, conf, CONF_gssapifwd);
763     gprefs(sesskey, "Cipher", "\0",
764            ciphernames, CIPHER_MAX, conf, CONF_ssh_cipherlist);
765     {
766         /* Backward-compatibility: we used to have an option to
767          * disable gex under the "bugs" panel after one report of
768          * a server which offered it then choked, but we never got
769          * a server version string or any other reports. */
770         char *default_kexes;
771         i = 2 - gppi_raw(sesskey, "BugDHGEx2", 0);
772         if (i == FORCE_ON)
773             default_kexes = "dh-group14-sha1,dh-group1-sha1,rsa,WARN,dh-gex-sha1";
774         else
775             default_kexes = "dh-gex-sha1,dh-group14-sha1,dh-group1-sha1,rsa,WARN";
776         gprefs(sesskey, "KEX", default_kexes,
777                kexnames, KEX_MAX, conf, CONF_ssh_kexlist);
778     }
779     gppi(sesskey, "RekeyTime", 60, conf, CONF_ssh_rekey_time);
780     gpps(sesskey, "RekeyBytes", "1G", conf, CONF_ssh_rekey_data);
781     /* SSH-2 only by default */
782     gppi(sesskey, "SshProt", 3, conf, CONF_sshprot);
783     gpps(sesskey, "LogHost", "", conf, CONF_loghost);
784     gppi(sesskey, "SSH2DES", 0, conf, CONF_ssh2_des_cbc);
785     gppi(sesskey, "SshNoAuth", 0, conf, CONF_ssh_no_userauth);
786     gppi(sesskey, "SshBanner", 1, conf, CONF_ssh_show_banner);
787     gppi(sesskey, "AuthTIS", 0, conf, CONF_try_tis_auth);
788     gppi(sesskey, "AuthKI", 1, conf, CONF_try_ki_auth);
789     gppi(sesskey, "AuthGSSAPI", 1, conf, CONF_try_gssapi_auth);
790 #ifndef NO_GSSAPI
791     gprefs(sesskey, "GSSLibs", "\0",
792            gsslibkeywords, ngsslibs, conf, CONF_ssh_gsslist);
793     gppfile(sesskey, "GSSCustom", conf, CONF_ssh_gss_custom);
794 #endif
795     gppi(sesskey, "SshNoShell", 0, conf, CONF_ssh_no_shell);
796     gppfile(sesskey, "PublicKeyFile", conf, CONF_keyfile);
797     gpps(sesskey, "RemoteCommand", "", conf, CONF_remote_cmd);
798     gppi(sesskey, "RFCEnviron", 0, conf, CONF_rfc_environ);
799     gppi(sesskey, "PassiveTelnet", 0, conf, CONF_passive_telnet);
800     gppi(sesskey, "BackspaceIsDelete", 1, conf, CONF_bksp_is_delete);
801     gppi(sesskey, "RXVTHomeEnd", 0, conf, CONF_rxvt_homeend);
802     gppi(sesskey, "LinuxFunctionKeys", 0, conf, CONF_funky_type);
803     gppi(sesskey, "NoApplicationKeys", 0, conf, CONF_no_applic_k);
804     gppi(sesskey, "NoApplicationCursors", 0, conf, CONF_no_applic_c);
805     gppi(sesskey, "NoMouseReporting", 0, conf, CONF_no_mouse_rep);
806     gppi(sesskey, "NoRemoteResize", 0, conf, CONF_no_remote_resize);
807     gppi(sesskey, "NoAltScreen", 0, conf, CONF_no_alt_screen);
808     gppi(sesskey, "NoRemoteWinTitle", 0, conf, CONF_no_remote_wintitle);
809     {
810         /* Backward compatibility */
811         int no_remote_qtitle = gppi_raw(sesskey, "NoRemoteQTitle", 1);
812         /* We deliberately interpret the old setting of "no response" as
813          * "empty string". This changes the behaviour, but hopefully for
814          * the better; the user can always recover the old behaviour. */
815         gppi(sesskey, "RemoteQTitleAction",
816              no_remote_qtitle ? TITLE_EMPTY : TITLE_REAL,
817              conf, CONF_remote_qtitle_action);
818     }
819     gppi(sesskey, "NoDBackspace", 0, conf, CONF_no_dbackspace);
820     gppi(sesskey, "NoRemoteCharset", 0, conf, CONF_no_remote_charset);
821     gppi(sesskey, "ApplicationCursorKeys", 0, conf, CONF_app_cursor);
822     gppi(sesskey, "ApplicationKeypad", 0, conf, CONF_app_keypad);
823     gppi(sesskey, "NetHackKeypad", 0, conf, CONF_nethack_keypad);
824     gppi(sesskey, "AltF4", 1, conf, CONF_alt_f4);
825     gppi(sesskey, "AltSpace", 0, conf, CONF_alt_space);
826     gppi(sesskey, "AltOnly", 0, conf, CONF_alt_only);
827     gppi(sesskey, "ComposeKey", 0, conf, CONF_compose_key);
828     gppi(sesskey, "CtrlAltKeys", 1, conf, CONF_ctrlaltkeys);
829     gppi(sesskey, "TelnetKey", 0, conf, CONF_telnet_keyboard);
830     gppi(sesskey, "TelnetRet", 1, conf, CONF_telnet_newline);
831     gppi(sesskey, "LocalEcho", AUTO, conf, CONF_localecho);
832     gppi(sesskey, "LocalEdit", AUTO, conf, CONF_localedit);
833     gpps(sesskey, "Answerback", "PuTTY", conf, CONF_answerback);
834     gppi(sesskey, "AlwaysOnTop", 0, conf, CONF_alwaysontop);
835     gppi(sesskey, "FullScreenOnAltEnter", 0, conf, CONF_fullscreenonaltenter);
836     gppi(sesskey, "HideMousePtr", 0, conf, CONF_hide_mouseptr);
837     gppi(sesskey, "SunkenEdge", 0, conf, CONF_sunken_edge);
838     gppi(sesskey, "WindowBorder", 1, conf, CONF_window_border);
839     gppi(sesskey, "CurType", 0, conf, CONF_cursor_type);
840     gppi(sesskey, "BlinkCur", 0, conf, CONF_blink_cur);
841     /* pedantic compiler tells me I can't use conf, CONF_beep as an int * :-) */
842     gppi(sesskey, "Beep", 1, conf, CONF_beep);
843     gppi(sesskey, "BeepInd", 0, conf, CONF_beep_ind);
844     gppfile(sesskey, "BellWaveFile", conf, CONF_bell_wavefile);
845     gppi(sesskey, "BellOverload", 1, conf, CONF_bellovl);
846     gppi(sesskey, "BellOverloadN", 5, conf, CONF_bellovl_n);
847     i = gppi_raw(sesskey, "BellOverloadT", 2*TICKSPERSEC
848 #ifdef PUTTY_UNIX_H
849                                    *1000
850 #endif
851                                    );
852     conf_set_int(conf, CONF_bellovl_t, i
853 #ifdef PUTTY_UNIX_H
854                  / 1000
855 #endif
856                  );
857     i = gppi_raw(sesskey, "BellOverloadS", 5*TICKSPERSEC
858 #ifdef PUTTY_UNIX_H
859                                    *1000
860 #endif
861                                    );
862     conf_set_int(conf, CONF_bellovl_s, i
863 #ifdef PUTTY_UNIX_H
864                  / 1000
865 #endif
866                  );
867     gppi(sesskey, "ScrollbackLines", 2000, conf, CONF_savelines);
868     gppi(sesskey, "DECOriginMode", 0, conf, CONF_dec_om);
869     gppi(sesskey, "AutoWrapMode", 1, conf, CONF_wrap_mode);
870     gppi(sesskey, "LFImpliesCR", 0, conf, CONF_lfhascr);
871     gppi(sesskey, "CRImpliesLF", 0, conf, CONF_crhaslf);
872     gppi(sesskey, "DisableArabicShaping", 0, conf, CONF_arabicshaping);
873     gppi(sesskey, "DisableBidi", 0, conf, CONF_bidi);
874     gppi(sesskey, "WinNameAlways", 1, conf, CONF_win_name_always);
875     gpps(sesskey, "WinTitle", "", conf, CONF_wintitle);
876     gppi(sesskey, "TermWidth", 80, conf, CONF_width);
877     gppi(sesskey, "TermHeight", 24, conf, CONF_height);
878     gppfont(sesskey, "Font", conf, CONF_font);
879     gppi(sesskey, "FontQuality", FQ_DEFAULT, conf, CONF_font_quality);
880     gppi(sesskey, "FontVTMode", VT_UNICODE, conf, CONF_vtmode);
881     gppi(sesskey, "UseSystemColours", 0, conf, CONF_system_colour);
882     gppi(sesskey, "TryPalette", 0, conf, CONF_try_palette);
883     gppi(sesskey, "ANSIColour", 1, conf, CONF_ansi_colour);
884     gppi(sesskey, "Xterm256Colour", 1, conf, CONF_xterm_256_colour);
885     i = gppi_raw(sesskey, "BoldAsColour", 1); conf_set_int(conf, CONF_bold_style, i+1);
886
887     for (i = 0; i < 22; i++) {
888         static const char *const defaults[] = {
889             "187,187,187", "255,255,255", "0,0,0", "85,85,85", "0,0,0",
890             "0,255,0", "0,0,0", "85,85,85", "187,0,0", "255,85,85",
891             "0,187,0", "85,255,85", "187,187,0", "255,255,85", "0,0,187",
892             "85,85,255", "187,0,187", "255,85,255", "0,187,187",
893             "85,255,255", "187,187,187", "255,255,255"
894         };
895         char buf[20], *buf2;
896         int c0, c1, c2;
897         sprintf(buf, "Colour%d", i);
898         buf2 = gpps_raw(sesskey, buf, defaults[i]);
899         if (sscanf(buf2, "%d,%d,%d", &c0, &c1, &c2) == 3) {
900             conf_set_int_int(conf, CONF_colours, i*3+0, c0);
901             conf_set_int_int(conf, CONF_colours, i*3+1, c1);
902             conf_set_int_int(conf, CONF_colours, i*3+2, c2);
903         }
904         sfree(buf2);
905     }
906     gppi(sesskey, "RawCNP", 0, conf, CONF_rawcnp);
907     gppi(sesskey, "PasteRTF", 0, conf, CONF_rtf_paste);
908     gppi(sesskey, "MouseIsXterm", 0, conf, CONF_mouse_is_xterm);
909     gppi(sesskey, "RectSelect", 0, conf, CONF_rect_select);
910     gppi(sesskey, "MouseOverride", 1, conf, CONF_mouse_override);
911     for (i = 0; i < 256; i += 32) {
912         static const char *const defaults[] = {
913             "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",
914             "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",
915             "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",
916             "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",
917             "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",
918             "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",
919             "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",
920             "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"
921         };
922         char buf[20], *buf2, *p;
923         int j;
924         sprintf(buf, "Wordness%d", i);
925         buf2 = gpps_raw(sesskey, buf, defaults[i / 32]);
926         p = buf2;
927         for (j = i; j < i + 32; j++) {
928             char *q = p;
929             while (*p && *p != ',')
930                 p++;
931             if (*p == ',')
932                 *p++ = '\0';
933             conf_set_int_int(conf, CONF_wordness, j, atoi(q));
934         }
935         sfree(buf2);
936     }
937     /*
938      * The empty default for LineCodePage will be converted later
939      * into a plausible default for the locale.
940      */
941     gpps(sesskey, "LineCodePage", "", conf, CONF_line_codepage);
942     gppi(sesskey, "CJKAmbigWide", 0, conf, CONF_cjk_ambig_wide);
943     gppi(sesskey, "UTF8Override", 1, conf, CONF_utf8_override);
944     gpps(sesskey, "Printer", "", conf, CONF_printer);
945     gppi(sesskey, "CapsLockCyr", 0, conf, CONF_xlat_capslockcyr);
946     gppi(sesskey, "ScrollBar", 1, conf, CONF_scrollbar);
947     gppi(sesskey, "ScrollBarFullScreen", 0, conf, CONF_scrollbar_in_fullscreen);
948     gppi(sesskey, "ScrollOnKey", 0, conf, CONF_scroll_on_key);
949     gppi(sesskey, "ScrollOnDisp", 1, conf, CONF_scroll_on_disp);
950     gppi(sesskey, "EraseToScrollback", 1, conf, CONF_erase_to_scrollback);
951     gppi(sesskey, "LockSize", 0, conf, CONF_resize_action);
952     gppi(sesskey, "BCE", 1, conf, CONF_bce);
953     gppi(sesskey, "BlinkText", 0, conf, CONF_blinktext);
954     gppi(sesskey, "X11Forward", 0, conf, CONF_x11_forward);
955     gpps(sesskey, "X11Display", "", conf, CONF_x11_display);
956     gppi(sesskey, "X11AuthType", X11_MIT, conf, CONF_x11_auth);
957     gppfile(sesskey, "X11AuthFile", conf, CONF_xauthfile);
958
959     gppi(sesskey, "LocalPortAcceptAll", 0, conf, CONF_lport_acceptall);
960     gppi(sesskey, "RemotePortAcceptAll", 0, conf, CONF_rport_acceptall);
961     gppmap(sesskey, "PortForwardings", conf, CONF_portfwd);
962     i = gppi_raw(sesskey, "BugIgnore1", 0); conf_set_int(conf, CONF_sshbug_ignore1, 2-i);
963     i = gppi_raw(sesskey, "BugPlainPW1", 0); conf_set_int(conf, CONF_sshbug_plainpw1, 2-i);
964     i = gppi_raw(sesskey, "BugRSA1", 0); conf_set_int(conf, CONF_sshbug_rsa1, 2-i);
965     i = gppi_raw(sesskey, "BugIgnore2", 0); conf_set_int(conf, CONF_sshbug_ignore2, 2-i);
966     {
967         int i;
968         i = gppi_raw(sesskey, "BugHMAC2", 0); conf_set_int(conf, CONF_sshbug_hmac2, 2-i);
969         if (2-i == AUTO) {
970             i = gppi_raw(sesskey, "BuggyMAC", 0);
971             if (i == 1)
972                 conf_set_int(conf, CONF_sshbug_hmac2, FORCE_ON);
973         }
974     }
975     i = gppi_raw(sesskey, "BugDeriveKey2", 0); conf_set_int(conf, CONF_sshbug_derivekey2, 2-i);
976     i = gppi_raw(sesskey, "BugRSAPad2", 0); conf_set_int(conf, CONF_sshbug_rsapad2, 2-i);
977     i = gppi_raw(sesskey, "BugPKSessID2", 0); conf_set_int(conf, CONF_sshbug_pksessid2, 2-i);
978     i = gppi_raw(sesskey, "BugRekey2", 0); conf_set_int(conf, CONF_sshbug_rekey2, 2-i);
979     i = gppi_raw(sesskey, "BugMaxPkt2", 0); conf_set_int(conf, CONF_sshbug_maxpkt2, 2-i);
980     i = gppi_raw(sesskey, "BugWinadj", 0); conf_set_int(conf, CONF_sshbug_winadj, 2-i);
981     i = gppi_raw(sesskey, "BugChanReq", 0); conf_set_int(conf, CONF_sshbug_chanreq, 2-i);
982     conf_set_int(conf, CONF_ssh_simple, FALSE);
983     gppi(sesskey, "StampUtmp", 1, conf, CONF_stamp_utmp);
984     gppi(sesskey, "LoginShell", 1, conf, CONF_login_shell);
985     gppi(sesskey, "ScrollbarOnLeft", 0, conf, CONF_scrollbar_on_left);
986     gppi(sesskey, "ShadowBold", 0, conf, CONF_shadowbold);
987     gppfont(sesskey, "BoldFont", conf, CONF_boldfont);
988     gppfont(sesskey, "WideFont", conf, CONF_widefont);
989     gppfont(sesskey, "WideBoldFont", conf, CONF_wideboldfont);
990     gppi(sesskey, "ShadowBoldOffset", 1, conf, CONF_shadowboldoffset);
991     gpps(sesskey, "SerialLine", "", conf, CONF_serline);
992     gppi(sesskey, "SerialSpeed", 9600, conf, CONF_serspeed);
993     gppi(sesskey, "SerialDataBits", 8, conf, CONF_serdatabits);
994     gppi(sesskey, "SerialStopHalfbits", 2, conf, CONF_serstopbits);
995     gppi(sesskey, "SerialParity", SER_PAR_NONE, conf, CONF_serparity);
996     gppi(sesskey, "SerialFlowControl", SER_FLOW_XONXOFF, conf, CONF_serflow);
997     gpps(sesskey, "WindowClass", "", conf, CONF_winclass);
998     gppi(sesskey, "ConnectionSharing", 0, conf, CONF_ssh_connection_sharing);
999     gppi(sesskey, "ConnectionSharingUpstream", 1, conf, CONF_ssh_connection_sharing_upstream);
1000     gppi(sesskey, "ConnectionSharingDownstream", 1, conf, CONF_ssh_connection_sharing_downstream);
1001     gppmap(sesskey, "SSHManualHostKeys", conf, CONF_ssh_manual_hostkeys);
1002 }
1003
1004 void do_defaults(char *session, Conf *conf)
1005 {
1006     load_settings(session, conf);
1007 }
1008
1009 static int sessioncmp(const void *av, const void *bv)
1010 {
1011     const char *a = *(const char *const *) av;
1012     const char *b = *(const char *const *) bv;
1013
1014     /*
1015      * Alphabetical order, except that "Default Settings" is a
1016      * special case and comes first.
1017      */
1018     if (!strcmp(a, "Default Settings"))
1019         return -1;                     /* a comes first */
1020     if (!strcmp(b, "Default Settings"))
1021         return +1;                     /* b comes first */
1022     /*
1023      * FIXME: perhaps we should ignore the first & in determining
1024      * sort order.
1025      */
1026     return strcmp(a, b);               /* otherwise, compare normally */
1027 }
1028
1029 void get_sesslist(struct sesslist *list, int allocate)
1030 {
1031     char otherbuf[2048];
1032     int buflen, bufsize, i;
1033     char *p, *ret;
1034     void *handle;
1035
1036     if (allocate) {
1037
1038         buflen = bufsize = 0;
1039         list->buffer = NULL;
1040         if ((handle = enum_settings_start()) != NULL) {
1041             do {
1042                 ret = enum_settings_next(handle, otherbuf, sizeof(otherbuf));
1043                 if (ret) {
1044                     int len = strlen(otherbuf) + 1;
1045                     if (bufsize < buflen + len) {
1046                         bufsize = buflen + len + 2048;
1047                         list->buffer = sresize(list->buffer, bufsize, char);
1048                     }
1049                     strcpy(list->buffer + buflen, otherbuf);
1050                     buflen += strlen(list->buffer + buflen) + 1;
1051                 }
1052             } while (ret);
1053             enum_settings_finish(handle);
1054         }
1055         list->buffer = sresize(list->buffer, buflen + 1, char);
1056         list->buffer[buflen] = '\0';
1057
1058         /*
1059          * Now set up the list of sessions. Note that "Default
1060          * Settings" must always be claimed to exist, even if it
1061          * doesn't really.
1062          */
1063
1064         p = list->buffer;
1065         list->nsessions = 1;           /* "Default Settings" counts as one */
1066         while (*p) {
1067             if (strcmp(p, "Default Settings"))
1068                 list->nsessions++;
1069             while (*p)
1070                 p++;
1071             p++;
1072         }
1073
1074         list->sessions = snewn(list->nsessions + 1, char *);
1075         list->sessions[0] = "Default Settings";
1076         p = list->buffer;
1077         i = 1;
1078         while (*p) {
1079             if (strcmp(p, "Default Settings"))
1080                 list->sessions[i++] = p;
1081             while (*p)
1082                 p++;
1083             p++;
1084         }
1085
1086         qsort(list->sessions, i, sizeof(char *), sessioncmp);
1087     } else {
1088         sfree(list->buffer);
1089         sfree(list->sessions);
1090         list->buffer = NULL;
1091         list->sessions = NULL;
1092     }
1093 }