]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - settings.c
In get_sesslist(), when freeing, set freed members to NULL on general
[PuTTY.git] / settings.c
1 /*
2  * settings.c: read and write saved sessions. (platform-independent)
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "putty.h"
8 #include "storage.h"
9
10 /*
11  * Tables of string <-> enum value mappings
12  */
13 struct keyval { char *s; int v; };
14
15 /* The cipher order given here is the default order. */
16 static const struct keyval ciphernames[] = {
17     { "aes",        CIPHER_AES },
18     { "blowfish",   CIPHER_BLOWFISH },
19     { "3des",       CIPHER_3DES },
20     { "WARN",       CIPHER_WARN },
21     { "des",        CIPHER_DES }
22 };
23
24 static const struct keyval kexnames[] = {
25     { "dh-gex-sha1",        KEX_DHGEX },
26     { "dh-group14-sha1",    KEX_DHGROUP14 },
27     { "dh-group1-sha1",     KEX_DHGROUP1 },
28     { "WARN",               KEX_WARN }
29 };
30
31 static void gpps(void *handle, const char *name, const char *def,
32                  char *val, int len)
33 {
34     if (!read_setting_s(handle, name, val, len)) {
35         char *pdef;
36
37         pdef = platform_default_s(name);
38         if (pdef) {
39             strncpy(val, pdef, len);
40             sfree(pdef);
41         } else {
42             strncpy(val, def, len);
43         }
44
45         val[len - 1] = '\0';
46     }
47 }
48
49 /*
50  * gppfont and gppfile cannot have local defaults, since the very
51  * format of a Filename or Font is platform-dependent. So the
52  * platform-dependent functions MUST return some sort of value.
53  */
54 static void gppfont(void *handle, const char *name, FontSpec *result)
55 {
56     if (!read_setting_fontspec(handle, name, result))
57         *result = platform_default_fontspec(name);
58 }
59 static void gppfile(void *handle, const char *name, Filename *result)
60 {
61     if (!read_setting_filename(handle, name, result))
62         *result = platform_default_filename(name);
63 }
64
65 static void gppi(void *handle, char *name, int def, int *i)
66 {
67     def = platform_default_i(name, def);
68     *i = read_setting_i(handle, name, def);
69 }
70
71 static int key2val(const struct keyval *mapping, int nmaps, char *key)
72 {
73     int i;
74     for (i = 0; i < nmaps; i++)
75         if (!strcmp(mapping[i].s, key)) return mapping[i].v;
76     return -1;
77 }
78
79 static const char *val2key(const struct keyval *mapping, int nmaps, int val)
80 {
81     int i;
82     for (i = 0; i < nmaps; i++)
83         if (mapping[i].v == val) return mapping[i].s;
84     return NULL;
85 }
86
87 /*
88  * Helper function to parse a comma-separated list of strings into
89  * a preference list array of values. Any missing values are added
90  * to the end and duplicates are weeded.
91  * XXX: assumes vals in 'mapping' are small +ve integers
92  */
93 static void gprefs(void *sesskey, char *name, char *def,
94                    const struct keyval *mapping, int nvals,
95                    int *array)
96 {
97     char commalist[80];
98     int n;
99     unsigned long seen = 0;            /* bitmap for weeding dups etc */
100     gpps(sesskey, name, def, commalist, sizeof(commalist));
101
102     /* Grotty parsing of commalist. */
103     n = 0;
104     do {
105         int v;
106         char *key;
107         key = strtok(n==0 ? commalist : NULL, ","); /* sorry */
108         if (!key) break;
109         if (((v = key2val(mapping, nvals, key)) != -1) &&
110             !(seen & 1<<v)) {
111             array[n] = v;
112             n++;
113             seen |= 1<<v;
114         }
115     } while (n < nvals);
116     /* Add any missing values (backward compatibility ect). */
117     {
118         int i;
119         for (i = 0; i < nvals; i++) {
120             if (!(seen & 1<<mapping[i].v)) {
121                 array[n] = mapping[i].v;
122                 n++;
123             }
124         }
125     }
126 }
127
128 /* 
129  * Write out a preference list.
130  */
131 static void wprefs(void *sesskey, char *name,
132                    const struct keyval *mapping, int nvals,
133                    int *array)
134 {
135     char buf[80] = "";  /* XXX assumed big enough */
136     int l = sizeof(buf)-1, i;
137     buf[l] = '\0';
138     for (i = 0; l > 0 && i < nvals; i++) {
139         const char *s = val2key(mapping, nvals, array[i]);
140         if (s) {
141             int sl = strlen(s);
142             if (i > 0) {
143                 strncat(buf, ",", l);
144                 l--;
145             }
146             strncat(buf, s, l);
147             l -= sl;
148         }
149     }
150     write_setting_s(sesskey, name, buf);
151 }
152
153 char *save_settings(char *section, int do_host, Config * cfg)
154 {
155     void *sesskey;
156     char *errmsg;
157
158     sesskey = open_settings_w(section, &errmsg);
159     if (!sesskey)
160         return errmsg;
161     save_open_settings(sesskey, do_host, cfg);
162     close_settings_w(sesskey);
163     return NULL;
164 }
165
166 void save_open_settings(void *sesskey, int do_host, Config *cfg)
167 {
168     int i;
169     char *p;
170
171     write_setting_i(sesskey, "Present", 1);
172     if (do_host) {
173         write_setting_s(sesskey, "HostName", cfg->host);
174     }
175     write_setting_filename(sesskey, "LogFileName", cfg->logfilename);
176     write_setting_i(sesskey, "LogType", cfg->logtype);
177     write_setting_i(sesskey, "LogFileClash", cfg->logxfovr);
178     write_setting_i(sesskey, "LogFlush", cfg->logflush);
179     write_setting_i(sesskey, "SSHLogOmitPasswords", cfg->logomitpass);
180     write_setting_i(sesskey, "SSHLogOmitData", cfg->logomitdata);
181     p = "raw";
182     for (i = 0; backends[i].name != NULL; i++)
183         if (backends[i].protocol == cfg->protocol) {
184             p = backends[i].name;
185             break;
186         }
187     write_setting_s(sesskey, "Protocol", p);
188     write_setting_i(sesskey, "PortNumber", cfg->port);
189     /* The CloseOnExit numbers are arranged in a different order from
190      * the standard FORCE_ON / FORCE_OFF / AUTO. */
191     write_setting_i(sesskey, "CloseOnExit", (cfg->close_on_exit+2)%3);
192     write_setting_i(sesskey, "WarnOnClose", !!cfg->warn_on_close);
193     write_setting_i(sesskey, "PingInterval", cfg->ping_interval / 60);  /* minutes */
194     write_setting_i(sesskey, "PingIntervalSecs", cfg->ping_interval % 60);      /* seconds */
195     write_setting_i(sesskey, "TCPNoDelay", cfg->tcp_nodelay);
196     write_setting_i(sesskey, "TCPKeepalives", cfg->tcp_keepalives);
197     write_setting_s(sesskey, "TerminalType", cfg->termtype);
198     write_setting_s(sesskey, "TerminalSpeed", cfg->termspeed);
199
200     /* Address family selection */
201     write_setting_i(sesskey, "AddressFamily", cfg->addressfamily);
202
203     /* proxy settings */
204     write_setting_s(sesskey, "ProxyExcludeList", cfg->proxy_exclude_list);
205     write_setting_i(sesskey, "ProxyDNS", (cfg->proxy_dns+2)%3);
206     write_setting_i(sesskey, "ProxyLocalhost", cfg->even_proxy_localhost);
207     write_setting_i(sesskey, "ProxyMethod", cfg->proxy_type);
208     write_setting_s(sesskey, "ProxyHost", cfg->proxy_host);
209     write_setting_i(sesskey, "ProxyPort", cfg->proxy_port);
210     write_setting_s(sesskey, "ProxyUsername", cfg->proxy_username);
211     write_setting_s(sesskey, "ProxyPassword", cfg->proxy_password);
212     write_setting_s(sesskey, "ProxyTelnetCommand", cfg->proxy_telnet_command);
213
214     {
215         char buf[2 * sizeof(cfg->environmt)], *p, *q;
216         p = buf;
217         q = cfg->environmt;
218         while (*q) {
219             while (*q) {
220                 int c = *q++;
221                 if (c == '=' || c == ',' || c == '\\')
222                     *p++ = '\\';
223                 if (c == '\t')
224                     c = '=';
225                 *p++ = c;
226             }
227             *p++ = ',';
228             q++;
229         }
230         *p = '\0';
231         write_setting_s(sesskey, "Environment", buf);
232     }
233     write_setting_s(sesskey, "UserName", cfg->username);
234     write_setting_s(sesskey, "LocalUserName", cfg->localusername);
235     write_setting_i(sesskey, "NoPTY", cfg->nopty);
236     write_setting_i(sesskey, "Compression", cfg->compression);
237     write_setting_i(sesskey, "AgentFwd", cfg->agentfwd);
238     write_setting_i(sesskey, "ChangeUsername", cfg->change_username);
239     wprefs(sesskey, "Cipher", ciphernames, CIPHER_MAX,
240            cfg->ssh_cipherlist);
241     wprefs(sesskey, "KEX", kexnames, KEX_MAX, cfg->ssh_kexlist);
242     write_setting_i(sesskey, "RekeyTime", cfg->ssh_rekey_time);
243     write_setting_s(sesskey, "RekeyBytes", cfg->ssh_rekey_data);
244     write_setting_i(sesskey, "AuthTIS", cfg->try_tis_auth);
245     write_setting_i(sesskey, "AuthKI", cfg->try_ki_auth);
246     write_setting_i(sesskey, "SshNoShell", cfg->ssh_no_shell);
247     write_setting_i(sesskey, "SshProt", cfg->sshprot);
248     write_setting_i(sesskey, "SSH2DES", cfg->ssh2_des_cbc);
249     write_setting_filename(sesskey, "PublicKeyFile", cfg->keyfile);
250     write_setting_s(sesskey, "RemoteCommand", cfg->remote_cmd);
251     write_setting_i(sesskey, "RFCEnviron", cfg->rfc_environ);
252     write_setting_i(sesskey, "PassiveTelnet", cfg->passive_telnet);
253     write_setting_i(sesskey, "BackspaceIsDelete", cfg->bksp_is_delete);
254     write_setting_i(sesskey, "RXVTHomeEnd", cfg->rxvt_homeend);
255     write_setting_i(sesskey, "LinuxFunctionKeys", cfg->funky_type);
256     write_setting_i(sesskey, "NoApplicationKeys", cfg->no_applic_k);
257     write_setting_i(sesskey, "NoApplicationCursors", cfg->no_applic_c);
258     write_setting_i(sesskey, "NoMouseReporting", cfg->no_mouse_rep);
259     write_setting_i(sesskey, "NoRemoteResize", cfg->no_remote_resize);
260     write_setting_i(sesskey, "NoAltScreen", cfg->no_alt_screen);
261     write_setting_i(sesskey, "NoRemoteWinTitle", cfg->no_remote_wintitle);
262     write_setting_i(sesskey, "NoRemoteQTitle", cfg->no_remote_qtitle);
263     write_setting_i(sesskey, "NoDBackspace", cfg->no_dbackspace);
264     write_setting_i(sesskey, "NoRemoteCharset", cfg->no_remote_charset);
265     write_setting_i(sesskey, "ApplicationCursorKeys", cfg->app_cursor);
266     write_setting_i(sesskey, "ApplicationKeypad", cfg->app_keypad);
267     write_setting_i(sesskey, "NetHackKeypad", cfg->nethack_keypad);
268     write_setting_i(sesskey, "AltF4", cfg->alt_f4);
269     write_setting_i(sesskey, "AltSpace", cfg->alt_space);
270     write_setting_i(sesskey, "AltOnly", cfg->alt_only);
271     write_setting_i(sesskey, "ComposeKey", cfg->compose_key);
272     write_setting_i(sesskey, "CtrlAltKeys", cfg->ctrlaltkeys);
273     write_setting_i(sesskey, "TelnetKey", cfg->telnet_keyboard);
274     write_setting_i(sesskey, "TelnetRet", cfg->telnet_newline);
275     write_setting_i(sesskey, "LocalEcho", cfg->localecho);
276     write_setting_i(sesskey, "LocalEdit", cfg->localedit);
277     write_setting_s(sesskey, "Answerback", cfg->answerback);
278     write_setting_i(sesskey, "AlwaysOnTop", cfg->alwaysontop);
279     write_setting_i(sesskey, "FullScreenOnAltEnter", cfg->fullscreenonaltenter);
280     write_setting_i(sesskey, "HideMousePtr", cfg->hide_mouseptr);
281     write_setting_i(sesskey, "SunkenEdge", cfg->sunken_edge);
282     write_setting_i(sesskey, "WindowBorder", cfg->window_border);
283     write_setting_i(sesskey, "CurType", cfg->cursor_type);
284     write_setting_i(sesskey, "BlinkCur", cfg->blink_cur);
285     write_setting_i(sesskey, "Beep", cfg->beep);
286     write_setting_i(sesskey, "BeepInd", cfg->beep_ind);
287     write_setting_filename(sesskey, "BellWaveFile", cfg->bell_wavefile);
288     write_setting_i(sesskey, "BellOverload", cfg->bellovl);
289     write_setting_i(sesskey, "BellOverloadN", cfg->bellovl_n);
290     write_setting_i(sesskey, "BellOverloadT", cfg->bellovl_t
291 #ifdef PUTTY_UNIX_H
292                     * 1000
293 #endif
294                     );
295     write_setting_i(sesskey, "BellOverloadS", cfg->bellovl_s
296 #ifdef PUTTY_UNIX_H
297                     * 1000
298 #endif
299                     );
300     write_setting_i(sesskey, "ScrollbackLines", cfg->savelines);
301     write_setting_i(sesskey, "DECOriginMode", cfg->dec_om);
302     write_setting_i(sesskey, "AutoWrapMode", cfg->wrap_mode);
303     write_setting_i(sesskey, "LFImpliesCR", cfg->lfhascr);
304     write_setting_i(sesskey, "DisableArabicShaping", cfg->arabicshaping);
305     write_setting_i(sesskey, "DisableBidi", cfg->bidi);
306     write_setting_i(sesskey, "WinNameAlways", cfg->win_name_always);
307     write_setting_s(sesskey, "WinTitle", cfg->wintitle);
308     write_setting_i(sesskey, "TermWidth", cfg->width);
309     write_setting_i(sesskey, "TermHeight", cfg->height);
310     write_setting_fontspec(sesskey, "Font", cfg->font);
311     write_setting_i(sesskey, "FontVTMode", cfg->vtmode);
312     write_setting_i(sesskey, "UseSystemColours", cfg->system_colour);
313     write_setting_i(sesskey, "TryPalette", cfg->try_palette);
314     write_setting_i(sesskey, "ANSIColour", cfg->ansi_colour);
315     write_setting_i(sesskey, "Xterm256Colour", cfg->xterm_256_colour);
316     write_setting_i(sesskey, "BoldAsColour", cfg->bold_colour);
317
318     for (i = 0; i < 22; i++) {
319         char buf[20], buf2[30];
320         sprintf(buf, "Colour%d", i);
321         sprintf(buf2, "%d,%d,%d", cfg->colours[i][0],
322                 cfg->colours[i][1], cfg->colours[i][2]);
323         write_setting_s(sesskey, buf, buf2);
324     }
325     write_setting_i(sesskey, "RawCNP", cfg->rawcnp);
326     write_setting_i(sesskey, "PasteRTF", cfg->rtf_paste);
327     write_setting_i(sesskey, "MouseIsXterm", cfg->mouse_is_xterm);
328     write_setting_i(sesskey, "RectSelect", cfg->rect_select);
329     write_setting_i(sesskey, "MouseOverride", cfg->mouse_override);
330     for (i = 0; i < 256; i += 32) {
331         char buf[20], buf2[256];
332         int j;
333         sprintf(buf, "Wordness%d", i);
334         *buf2 = '\0';
335         for (j = i; j < i + 32; j++) {
336             sprintf(buf2 + strlen(buf2), "%s%d",
337                     (*buf2 ? "," : ""), cfg->wordness[j]);
338         }
339         write_setting_s(sesskey, buf, buf2);
340     }
341     write_setting_s(sesskey, "LineCodePage", cfg->line_codepage);
342     write_setting_i(sesskey, "CJKAmbigWide", cfg->cjk_ambig_wide);
343     write_setting_i(sesskey, "UTF8Override", cfg->utf8_override);
344     write_setting_s(sesskey, "Printer", cfg->printer);
345     write_setting_i(sesskey, "CapsLockCyr", cfg->xlat_capslockcyr);
346     write_setting_i(sesskey, "ScrollBar", cfg->scrollbar);
347     write_setting_i(sesskey, "ScrollBarFullScreen", cfg->scrollbar_in_fullscreen);
348     write_setting_i(sesskey, "ScrollOnKey", cfg->scroll_on_key);
349     write_setting_i(sesskey, "ScrollOnDisp", cfg->scroll_on_disp);
350     write_setting_i(sesskey, "EraseToScrollback", cfg->erase_to_scrollback);
351     write_setting_i(sesskey, "LockSize", cfg->resize_action);
352     write_setting_i(sesskey, "BCE", cfg->bce);
353     write_setting_i(sesskey, "BlinkText", cfg->blinktext);
354     write_setting_i(sesskey, "X11Forward", cfg->x11_forward);
355     write_setting_s(sesskey, "X11Display", cfg->x11_display);
356     write_setting_i(sesskey, "X11AuthType", cfg->x11_auth);
357     write_setting_i(sesskey, "LocalPortAcceptAll", cfg->lport_acceptall);
358     write_setting_i(sesskey, "RemotePortAcceptAll", cfg->rport_acceptall);
359     {
360         char buf[2 * sizeof(cfg->portfwd)], *p, *q;
361         p = buf;
362         q = cfg->portfwd;
363         while (*q) {
364             while (*q) {
365                 int c = *q++;
366                 if (c == '=' || c == ',' || c == '\\')
367                     *p++ = '\\';
368                 if (c == '\t')
369                     c = '=';
370                 *p++ = c;
371             }
372             *p++ = ',';
373             q++;
374         }
375         *p = '\0';
376         write_setting_s(sesskey, "PortForwardings", buf);
377     }
378     write_setting_i(sesskey, "BugIgnore1", 2-cfg->sshbug_ignore1);
379     write_setting_i(sesskey, "BugPlainPW1", 2-cfg->sshbug_plainpw1);
380     write_setting_i(sesskey, "BugRSA1", 2-cfg->sshbug_rsa1);
381     write_setting_i(sesskey, "BugHMAC2", 2-cfg->sshbug_hmac2);
382     write_setting_i(sesskey, "BugDeriveKey2", 2-cfg->sshbug_derivekey2);
383     write_setting_i(sesskey, "BugRSAPad2", 2-cfg->sshbug_rsapad2);
384     write_setting_i(sesskey, "BugPKSessID2", 2-cfg->sshbug_pksessid2);
385     write_setting_i(sesskey, "StampUtmp", cfg->stamp_utmp);
386     write_setting_i(sesskey, "LoginShell", cfg->login_shell);
387     write_setting_i(sesskey, "ScrollbarOnLeft", cfg->scrollbar_on_left);
388     write_setting_fontspec(sesskey, "BoldFont", cfg->boldfont);
389     write_setting_fontspec(sesskey, "WideFont", cfg->widefont);
390     write_setting_fontspec(sesskey, "WideBoldFont", cfg->wideboldfont);
391     write_setting_i(sesskey, "ShadowBold", cfg->shadowbold);
392     write_setting_i(sesskey, "ShadowBoldOffset", cfg->shadowboldoffset);
393 }
394
395 void load_settings(char *section, int do_host, Config * cfg)
396 {
397     void *sesskey;
398
399     sesskey = open_settings_r(section);
400     load_open_settings(sesskey, do_host, cfg);
401     close_settings_r(sesskey);
402 }
403
404 void load_open_settings(void *sesskey, int do_host, Config *cfg)
405 {
406     int i;
407     char prot[10];
408
409     cfg->ssh_subsys = 0;               /* FIXME: load this properly */
410     cfg->remote_cmd_ptr = NULL;
411     cfg->remote_cmd_ptr2 = NULL;
412
413     if (do_host) {
414         gpps(sesskey, "HostName", "", cfg->host, sizeof(cfg->host));
415     } else {
416         cfg->host[0] = '\0';           /* blank hostname */
417     }
418     gppfile(sesskey, "LogFileName", &cfg->logfilename);
419     gppi(sesskey, "LogType", 0, &cfg->logtype);
420     gppi(sesskey, "LogFileClash", LGXF_ASK, &cfg->logxfovr);
421     gppi(sesskey, "LogFlush", 1, &cfg->logflush);
422     gppi(sesskey, "SSHLogOmitPasswords", 1, &cfg->logomitpass);
423     gppi(sesskey, "SSHLogOmitData", 0, &cfg->logomitdata);
424
425     gpps(sesskey, "Protocol", "default", prot, 10);
426     cfg->protocol = default_protocol;
427     cfg->port = default_port;
428     for (i = 0; backends[i].name != NULL; i++)
429         if (!strcmp(prot, backends[i].name)) {
430             cfg->protocol = backends[i].protocol;
431             gppi(sesskey, "PortNumber", default_port, &cfg->port);
432             break;
433         }
434
435     /* Address family selection */
436     gppi(sesskey, "AddressFamily", ADDRTYPE_UNSPEC, &cfg->addressfamily);
437
438     /* The CloseOnExit numbers are arranged in a different order from
439      * the standard FORCE_ON / FORCE_OFF / AUTO. */
440     gppi(sesskey, "CloseOnExit", 1, &i); cfg->close_on_exit = (i+1)%3;
441     gppi(sesskey, "WarnOnClose", 1, &cfg->warn_on_close);
442     {
443         /* This is two values for backward compatibility with 0.50/0.51 */
444         int pingmin, pingsec;
445         gppi(sesskey, "PingInterval", 0, &pingmin);
446         gppi(sesskey, "PingIntervalSecs", 0, &pingsec);
447         cfg->ping_interval = pingmin * 60 + pingsec;
448     }
449     gppi(sesskey, "TCPNoDelay", 1, &cfg->tcp_nodelay);
450     gppi(sesskey, "TCPKeepalives", 0, &cfg->tcp_keepalives);
451     gpps(sesskey, "TerminalType", "xterm", cfg->termtype,
452          sizeof(cfg->termtype));
453     gpps(sesskey, "TerminalSpeed", "38400,38400", cfg->termspeed,
454          sizeof(cfg->termspeed));
455
456     /* proxy settings */
457     gpps(sesskey, "ProxyExcludeList", "", cfg->proxy_exclude_list,
458          sizeof(cfg->proxy_exclude_list));
459     gppi(sesskey, "ProxyDNS", 1, &i); cfg->proxy_dns = (i+1)%3;
460     gppi(sesskey, "ProxyLocalhost", 0, &cfg->even_proxy_localhost);
461     gppi(sesskey, "ProxyMethod", -1, &cfg->proxy_type);
462     if (cfg->proxy_type == -1) {
463         int i;
464         gppi(sesskey, "ProxyType", 0, &i);
465         if (i == 0)
466             cfg->proxy_type = PROXY_NONE;
467         else if (i == 1)
468             cfg->proxy_type = PROXY_HTTP;
469         else if (i == 3)
470             cfg->proxy_type = PROXY_TELNET;
471         else if (i == 4)
472             cfg->proxy_type = PROXY_CMD;
473         else {
474             gppi(sesskey, "ProxySOCKSVersion", 5, &i);
475             if (i == 5)
476                 cfg->proxy_type = PROXY_SOCKS5;
477             else
478                 cfg->proxy_type = PROXY_SOCKS4;
479         }
480     }
481     gpps(sesskey, "ProxyHost", "proxy", cfg->proxy_host,
482          sizeof(cfg->proxy_host));
483     gppi(sesskey, "ProxyPort", 80, &cfg->proxy_port);
484     gpps(sesskey, "ProxyUsername", "", cfg->proxy_username,
485          sizeof(cfg->proxy_username));
486     gpps(sesskey, "ProxyPassword", "", cfg->proxy_password,
487          sizeof(cfg->proxy_password));
488     gpps(sesskey, "ProxyTelnetCommand", "connect %host %port\\n",
489          cfg->proxy_telnet_command, sizeof(cfg->proxy_telnet_command));
490
491     {
492         char buf[2 * sizeof(cfg->environmt)], *p, *q;
493         gpps(sesskey, "Environment", "", buf, sizeof(buf));
494         p = buf;
495         q = cfg->environmt;
496         while (*p) {
497             while (*p && *p != ',') {
498                 int c = *p++;
499                 if (c == '=')
500                     c = '\t';
501                 if (c == '\\')
502                     c = *p++;
503                 *q++ = c;
504             }
505             if (*p == ',')
506                 p++;
507             *q++ = '\0';
508         }
509         *q = '\0';
510     }
511     gpps(sesskey, "UserName", "", cfg->username, sizeof(cfg->username));
512     gpps(sesskey, "LocalUserName", "", cfg->localusername,
513          sizeof(cfg->localusername));
514     gppi(sesskey, "NoPTY", 0, &cfg->nopty);
515     gppi(sesskey, "Compression", 0, &cfg->compression);
516     gppi(sesskey, "AgentFwd", 0, &cfg->agentfwd);
517     gppi(sesskey, "ChangeUsername", 0, &cfg->change_username);
518     gprefs(sesskey, "Cipher", "\0",
519            ciphernames, CIPHER_MAX, cfg->ssh_cipherlist);
520     {
521         /* Backward-compatibility: we used to have an option to
522          * disable gex under the "bugs" panel after one report of
523          * a server which offered it then choked, but we never got
524          * a server version string or any other reports. */
525         char *default_kexes;
526         gppi(sesskey, "BugDHGEx2", 0, &i); i = 2-i;
527         if (i == FORCE_ON)
528             default_kexes = "dh-group14-sha1,dh-group1-sha1,WARN,dh-gex-sha1";
529         else
530             default_kexes = "dh-gex-sha1,dh-group14-sha1,dh-group1-sha1,WARN";
531         gprefs(sesskey, "KEX", default_kexes,
532                kexnames, KEX_MAX, cfg->ssh_kexlist);
533     }
534     gppi(sesskey, "RekeyTime", 60, &cfg->ssh_rekey_time);
535     gpps(sesskey, "RekeyBytes", "1G", cfg->ssh_rekey_data,
536          sizeof(cfg->ssh_rekey_data));
537     gppi(sesskey, "SshProt", 2, &cfg->sshprot);
538     gppi(sesskey, "SSH2DES", 0, &cfg->ssh2_des_cbc);
539     gppi(sesskey, "AuthTIS", 0, &cfg->try_tis_auth);
540     gppi(sesskey, "AuthKI", 1, &cfg->try_ki_auth);
541     gppi(sesskey, "SshNoShell", 0, &cfg->ssh_no_shell);
542     gppfile(sesskey, "PublicKeyFile", &cfg->keyfile);
543     gpps(sesskey, "RemoteCommand", "", cfg->remote_cmd,
544          sizeof(cfg->remote_cmd));
545     gppi(sesskey, "RFCEnviron", 0, &cfg->rfc_environ);
546     gppi(sesskey, "PassiveTelnet", 0, &cfg->passive_telnet);
547     gppi(sesskey, "BackspaceIsDelete", 1, &cfg->bksp_is_delete);
548     gppi(sesskey, "RXVTHomeEnd", 0, &cfg->rxvt_homeend);
549     gppi(sesskey, "LinuxFunctionKeys", 0, &cfg->funky_type);
550     gppi(sesskey, "NoApplicationKeys", 0, &cfg->no_applic_k);
551     gppi(sesskey, "NoApplicationCursors", 0, &cfg->no_applic_c);
552     gppi(sesskey, "NoMouseReporting", 0, &cfg->no_mouse_rep);
553     gppi(sesskey, "NoRemoteResize", 0, &cfg->no_remote_resize);
554     gppi(sesskey, "NoAltScreen", 0, &cfg->no_alt_screen);
555     gppi(sesskey, "NoRemoteWinTitle", 0, &cfg->no_remote_wintitle);
556     gppi(sesskey, "NoRemoteQTitle", 1, &cfg->no_remote_qtitle);
557     gppi(sesskey, "NoDBackspace", 0, &cfg->no_dbackspace);
558     gppi(sesskey, "NoRemoteCharset", 0, &cfg->no_remote_charset);
559     gppi(sesskey, "ApplicationCursorKeys", 0, &cfg->app_cursor);
560     gppi(sesskey, "ApplicationKeypad", 0, &cfg->app_keypad);
561     gppi(sesskey, "NetHackKeypad", 0, &cfg->nethack_keypad);
562     gppi(sesskey, "AltF4", 1, &cfg->alt_f4);
563     gppi(sesskey, "AltSpace", 0, &cfg->alt_space);
564     gppi(sesskey, "AltOnly", 0, &cfg->alt_only);
565     gppi(sesskey, "ComposeKey", 0, &cfg->compose_key);
566     gppi(sesskey, "CtrlAltKeys", 1, &cfg->ctrlaltkeys);
567     gppi(sesskey, "TelnetKey", 0, &cfg->telnet_keyboard);
568     gppi(sesskey, "TelnetRet", 1, &cfg->telnet_newline);
569     gppi(sesskey, "LocalEcho", AUTO, &cfg->localecho);
570     gppi(sesskey, "LocalEdit", AUTO, &cfg->localedit);
571     gpps(sesskey, "Answerback", "PuTTY", cfg->answerback,
572          sizeof(cfg->answerback));
573     gppi(sesskey, "AlwaysOnTop", 0, &cfg->alwaysontop);
574     gppi(sesskey, "FullScreenOnAltEnter", 0, &cfg->fullscreenonaltenter);
575     gppi(sesskey, "HideMousePtr", 0, &cfg->hide_mouseptr);
576     gppi(sesskey, "SunkenEdge", 0, &cfg->sunken_edge);
577     gppi(sesskey, "WindowBorder", 1, &cfg->window_border);
578     gppi(sesskey, "CurType", 0, &cfg->cursor_type);
579     gppi(sesskey, "BlinkCur", 0, &cfg->blink_cur);
580     /* pedantic compiler tells me I can't use &cfg->beep as an int * :-) */
581     gppi(sesskey, "Beep", 1, &cfg->beep);
582     gppi(sesskey, "BeepInd", 0, &cfg->beep_ind);
583     gppfile(sesskey, "BellWaveFile", &cfg->bell_wavefile);
584     gppi(sesskey, "BellOverload", 1, &cfg->bellovl);
585     gppi(sesskey, "BellOverloadN", 5, &cfg->bellovl_n);
586     gppi(sesskey, "BellOverloadT", 2*TICKSPERSEC, &i);
587     cfg->bellovl_t = i
588 #ifdef PUTTY_UNIX_H
589                     / 1000
590 #endif
591         ;
592     gppi(sesskey, "BellOverloadS", 5*TICKSPERSEC, &i);
593     cfg->bellovl_s = i
594 #ifdef PUTTY_UNIX_H
595                     / 1000
596 #endif
597         ;
598     gppi(sesskey, "ScrollbackLines", 200, &cfg->savelines);
599     gppi(sesskey, "DECOriginMode", 0, &cfg->dec_om);
600     gppi(sesskey, "AutoWrapMode", 1, &cfg->wrap_mode);
601     gppi(sesskey, "LFImpliesCR", 0, &cfg->lfhascr);
602     gppi(sesskey, "DisableArabicShaping", 0, &cfg->arabicshaping);
603     gppi(sesskey, "DisableBidi", 0, &cfg->bidi);
604     gppi(sesskey, "WinNameAlways", 1, &cfg->win_name_always);
605     gpps(sesskey, "WinTitle", "", cfg->wintitle, sizeof(cfg->wintitle));
606     gppi(sesskey, "TermWidth", 80, &cfg->width);
607     gppi(sesskey, "TermHeight", 24, &cfg->height);
608     gppfont(sesskey, "Font", &cfg->font);
609     gppi(sesskey, "FontVTMode", VT_UNICODE, (int *) &cfg->vtmode);
610     gppi(sesskey, "UseSystemColours", 0, &cfg->system_colour);
611     gppi(sesskey, "TryPalette", 0, &cfg->try_palette);
612     gppi(sesskey, "ANSIColour", 1, &cfg->ansi_colour);
613     gppi(sesskey, "Xterm256Colour", 1, &cfg->xterm_256_colour);
614     gppi(sesskey, "BoldAsColour", 1, &cfg->bold_colour);
615
616     for (i = 0; i < 22; i++) {
617         static const char *const defaults[] = {
618             "187,187,187", "255,255,255", "0,0,0", "85,85,85", "0,0,0",
619             "0,255,0", "0,0,0", "85,85,85", "187,0,0", "255,85,85",
620             "0,187,0", "85,255,85", "187,187,0", "255,255,85", "0,0,187",
621             "85,85,255", "187,0,187", "255,85,255", "0,187,187",
622             "85,255,255", "187,187,187", "255,255,255"
623         };
624         char buf[20], buf2[30];
625         int c0, c1, c2;
626         sprintf(buf, "Colour%d", i);
627         gpps(sesskey, buf, defaults[i], buf2, sizeof(buf2));
628         if (sscanf(buf2, "%d,%d,%d", &c0, &c1, &c2) == 3) {
629             cfg->colours[i][0] = c0;
630             cfg->colours[i][1] = c1;
631             cfg->colours[i][2] = c2;
632         }
633     }
634     gppi(sesskey, "RawCNP", 0, &cfg->rawcnp);
635     gppi(sesskey, "PasteRTF", 0, &cfg->rtf_paste);
636     gppi(sesskey, "MouseIsXterm", 0, &cfg->mouse_is_xterm);
637     gppi(sesskey, "RectSelect", 0, &cfg->rect_select);
638     gppi(sesskey, "MouseOverride", 1, &cfg->mouse_override);
639     for (i = 0; i < 256; i += 32) {
640         static const char *const defaults[] = {
641             "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",
642             "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",
643             "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",
644             "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",
645             "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",
646             "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",
647             "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",
648             "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"
649         };
650         char buf[20], buf2[256], *p;
651         int j;
652         sprintf(buf, "Wordness%d", i);
653         gpps(sesskey, buf, defaults[i / 32], buf2, sizeof(buf2));
654         p = buf2;
655         for (j = i; j < i + 32; j++) {
656             char *q = p;
657             while (*p && *p != ',')
658                 p++;
659             if (*p == ',')
660                 *p++ = '\0';
661             cfg->wordness[j] = atoi(q);
662         }
663     }
664     /*
665      * The empty default for LineCodePage will be converted later
666      * into a plausible default for the locale.
667      */
668     gpps(sesskey, "LineCodePage", "", cfg->line_codepage,
669          sizeof(cfg->line_codepage));
670     gppi(sesskey, "CJKAmbigWide", 0, &cfg->cjk_ambig_wide);
671     gppi(sesskey, "UTF8Override", 1, &cfg->utf8_override);
672     gpps(sesskey, "Printer", "", cfg->printer, sizeof(cfg->printer));
673     gppi (sesskey, "CapsLockCyr", 0, &cfg->xlat_capslockcyr);
674     gppi(sesskey, "ScrollBar", 1, &cfg->scrollbar);
675     gppi(sesskey, "ScrollBarFullScreen", 0, &cfg->scrollbar_in_fullscreen);
676     gppi(sesskey, "ScrollOnKey", 0, &cfg->scroll_on_key);
677     gppi(sesskey, "ScrollOnDisp", 1, &cfg->scroll_on_disp);
678     gppi(sesskey, "EraseToScrollback", 1, &cfg->erase_to_scrollback);
679     gppi(sesskey, "LockSize", 0, &cfg->resize_action);
680     gppi(sesskey, "BCE", 1, &cfg->bce);
681     gppi(sesskey, "BlinkText", 0, &cfg->blinktext);
682     gppi(sesskey, "X11Forward", 0, &cfg->x11_forward);
683     gpps(sesskey, "X11Display", "", cfg->x11_display,
684          sizeof(cfg->x11_display));
685     gppi(sesskey, "X11AuthType", X11_MIT, &cfg->x11_auth);
686
687     gppi(sesskey, "LocalPortAcceptAll", 0, &cfg->lport_acceptall);
688     gppi(sesskey, "RemotePortAcceptAll", 0, &cfg->rport_acceptall);
689     {
690         char buf[2 * sizeof(cfg->portfwd)], *p, *q;
691         gpps(sesskey, "PortForwardings", "", buf, sizeof(buf));
692         p = buf;
693         q = cfg->portfwd;
694         while (*p) {
695             while (*p && *p != ',') {
696                 int c = *p++;
697                 if (c == '=')
698                     c = '\t';
699                 if (c == '\\')
700                     c = *p++;
701                 *q++ = c;
702             }
703             if (*p == ',')
704                 p++;
705             *q++ = '\0';
706         }
707         *q = '\0';
708     }
709     gppi(sesskey, "BugIgnore1", 0, &i); cfg->sshbug_ignore1 = 2-i;
710     gppi(sesskey, "BugPlainPW1", 0, &i); cfg->sshbug_plainpw1 = 2-i;
711     gppi(sesskey, "BugRSA1", 0, &i); cfg->sshbug_rsa1 = 2-i;
712     {
713         int i;
714         gppi(sesskey, "BugHMAC2", 0, &i); cfg->sshbug_hmac2 = 2-i;
715         if (cfg->sshbug_hmac2 == AUTO) {
716             gppi(sesskey, "BuggyMAC", 0, &i);
717             if (i == 1)
718                 cfg->sshbug_hmac2 = FORCE_ON;
719         }
720     }
721     gppi(sesskey, "BugDeriveKey2", 0, &i); cfg->sshbug_derivekey2 = 2-i;
722     gppi(sesskey, "BugRSAPad2", 0, &i); cfg->sshbug_rsapad2 = 2-i;
723     gppi(sesskey, "BugPKSessID2", 0, &i); cfg->sshbug_pksessid2 = 2-i;
724     gppi(sesskey, "BugRekey2", 0, &i); cfg->sshbug_rekey2 = 2-i;
725     gppi(sesskey, "StampUtmp", 1, &cfg->stamp_utmp);
726     gppi(sesskey, "LoginShell", 1, &cfg->login_shell);
727     gppi(sesskey, "ScrollbarOnLeft", 0, &cfg->scrollbar_on_left);
728     gppi(sesskey, "ShadowBold", 0, &cfg->shadowbold);
729     gppfont(sesskey, "BoldFont", &cfg->boldfont);
730     gppfont(sesskey, "WideFont", &cfg->widefont);
731     gppfont(sesskey, "WideBoldFont", &cfg->wideboldfont);
732     gppi(sesskey, "ShadowBoldOffset", 1, &cfg->shadowboldoffset);
733 }
734
735 void do_defaults(char *session, Config * cfg)
736 {
737     load_settings(session, (session != NULL && *session), cfg);
738 }
739
740 static int sessioncmp(const void *av, const void *bv)
741 {
742     const char *a = *(const char *const *) av;
743     const char *b = *(const char *const *) bv;
744
745     /*
746      * Alphabetical order, except that "Default Settings" is a
747      * special case and comes first.
748      */
749     if (!strcmp(a, "Default Settings"))
750         return -1;                     /* a comes first */
751     if (!strcmp(b, "Default Settings"))
752         return +1;                     /* b comes first */
753     /*
754      * FIXME: perhaps we should ignore the first & in determining
755      * sort order.
756      */
757     return strcmp(a, b);               /* otherwise, compare normally */
758 }
759
760 void get_sesslist(struct sesslist *list, int allocate)
761 {
762     char otherbuf[2048];
763     int buflen, bufsize, i;
764     char *p, *ret;
765     void *handle;
766
767     if (allocate) {
768
769         buflen = bufsize = 0;
770         list->buffer = NULL;
771         if ((handle = enum_settings_start()) != NULL) {
772             do {
773                 ret = enum_settings_next(handle, otherbuf, sizeof(otherbuf));
774                 if (ret) {
775                     int len = strlen(otherbuf) + 1;
776                     if (bufsize < buflen + len) {
777                         bufsize = buflen + len + 2048;
778                         list->buffer = sresize(list->buffer, bufsize, char);
779                     }
780                     strcpy(list->buffer + buflen, otherbuf);
781                     buflen += strlen(list->buffer + buflen) + 1;
782                 }
783             } while (ret);
784             enum_settings_finish(handle);
785         }
786         list->buffer = sresize(list->buffer, buflen + 1, char);
787         list->buffer[buflen] = '\0';
788
789         /*
790          * Now set up the list of sessions. Note that "Default
791          * Settings" must always be claimed to exist, even if it
792          * doesn't really.
793          */
794
795         p = list->buffer;
796         list->nsessions = 1;           /* "Default Settings" counts as one */
797         while (*p) {
798             if (strcmp(p, "Default Settings"))
799                 list->nsessions++;
800             while (*p)
801                 p++;
802             p++;
803         }
804
805         list->sessions = snewn(list->nsessions + 1, char *);
806         list->sessions[0] = "Default Settings";
807         p = list->buffer;
808         i = 1;
809         while (*p) {
810             if (strcmp(p, "Default Settings"))
811                 list->sessions[i++] = p;
812             while (*p)
813                 p++;
814             p++;
815         }
816
817         qsort(list->sessions, i, sizeof(char *), sessioncmp);
818     } else {
819         sfree(list->buffer);
820         sfree(list->sessions);
821         list->buffer = NULL;
822         list->sessions = NULL;
823     }
824 }