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