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