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