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