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