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