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