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