settings.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /*
  2. * settings.c: read and write saved sessions. (platform-independent)
  3. */
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "putty.h"
  8. #include "storage.h"
  9. #ifndef NO_GSSAPI
  10. #include "ssh/gssc.h"
  11. #include "ssh/gss.h"
  12. #endif
  13. /* The cipher order given here is the default order. */
  14. static const struct keyvalwhere ciphernames[] = {
  15. { "aes", CIPHER_AES, -1, -1 },
  16. { "chacha20", CIPHER_CHACHA20, CIPHER_AES, +1 },
  17. { "aesgcm", CIPHER_AESGCM, CIPHER_CHACHA20, +1 },
  18. { "3des", CIPHER_3DES, -1, -1 },
  19. { "WARN", CIPHER_WARN, -1, -1 },
  20. { "des", CIPHER_DES, -1, -1 },
  21. { "blowfish", CIPHER_BLOWFISH, -1, -1 },
  22. { "arcfour", CIPHER_ARCFOUR, -1, -1 },
  23. };
  24. /* The default order here is sometimes overridden by the backward-
  25. * compatibility warts in load_open_settings(), and should be kept
  26. * in sync with those. */
  27. static const struct keyvalwhere kexnames[] = {
  28. { "ntru-curve25519", KEX_NTRU_HYBRID, -1, +1 },
  29. { "ecdh", KEX_ECDH, -1, +1 },
  30. /* This name is misleading: it covers both SHA-256 and SHA-1 variants */
  31. { "dh-gex-sha1", KEX_DHGEX, -1, -1 },
  32. /* Again, this covers both SHA-256 and SHA-1, despite the name: */
  33. { "dh-group14-sha1", KEX_DHGROUP14, -1, -1 },
  34. /* This one really is only SHA-1, though: */
  35. { "dh-group1-sha1", KEX_DHGROUP1, KEX_WARN, +1 },
  36. { "rsa", KEX_RSA, KEX_WARN, -1 },
  37. /* Larger fixed DH groups: prefer the larger 15 and 16 over 14,
  38. * but by default the even larger 17 and 18 go below 16.
  39. * Rationale: diminishing returns of improving the DH strength are
  40. * outweighed by increased CPU cost. Group 18 is painful on a slow
  41. * machine. Users can override if they need to. */
  42. { "dh-group15-sha512", KEX_DHGROUP15, KEX_DHGROUP14, -1 },
  43. { "dh-group16-sha512", KEX_DHGROUP16, KEX_DHGROUP15, -1 },
  44. { "dh-group17-sha512", KEX_DHGROUP17, KEX_DHGROUP16, +1 },
  45. { "dh-group18-sha512", KEX_DHGROUP18, KEX_DHGROUP17, +1 },
  46. { "WARN", KEX_WARN, -1, -1 }
  47. };
  48. static const struct keyvalwhere hknames[] = {
  49. { "ed25519", HK_ED25519, -1, +1 },
  50. { "ed448", HK_ED448, -1, +1 },
  51. { "ecdsa", HK_ECDSA, -1, -1 },
  52. { "dsa", HK_DSA, -1, -1 },
  53. { "rsa", HK_RSA, -1, -1 },
  54. { "WARN", HK_WARN, -1, -1 },
  55. };
  56. /*
  57. * All the terminal modes that we know about for the "TerminalModes"
  58. * setting. (Also used by config.c for the drop-down list.)
  59. * This is currently precisely the same as the set in
  60. * ssh/ttymode-list.h, but could in principle differ if other backends
  61. * started to support tty modes (e.g., the pty backend).
  62. * The set of modes in this array is currently significant for
  63. * settings migration from old versions; if they change, review the
  64. * gppmap() invocation for "TerminalModes".
  65. */
  66. const char *const ttymodes[] = {
  67. "INTR", "QUIT", "ERASE", "KILL", "EOF",
  68. "EOL", "EOL2", "START", "STOP", "SUSP",
  69. "DSUSP", "REPRINT", "WERASE", "LNEXT", "FLUSH",
  70. "SWTCH", "STATUS", "DISCARD", "IGNPAR", "PARMRK",
  71. "INPCK", "ISTRIP", "INLCR", "IGNCR", "ICRNL",
  72. "IUCLC", "IXON", "IXANY", "IXOFF", "IMAXBEL",
  73. "IUTF8", "ISIG", "ICANON", "XCASE", "ECHO",
  74. "ECHOE", "ECHOK", "ECHONL", "NOFLSH", "TOSTOP",
  75. "IEXTEN", "ECHOCTL", "ECHOKE", "PENDIN", "OPOST",
  76. "OLCUC", "ONLCR", "OCRNL", "ONOCR", "ONLRET",
  77. "CS7", "CS8", "PARENB", "PARODD", NULL
  78. };
  79. static int default_protocol, default_port;
  80. void settings_set_default_protocol(int newval) { default_protocol = newval; }
  81. void settings_set_default_port(int newval) { default_port = newval; }
  82. /*
  83. * Convenience functions to access the backends[] array
  84. * (which is only present in tools that manage settings).
  85. */
  86. const struct BackendVtable *backend_vt_from_name(const char *name)
  87. {
  88. const struct BackendVtable *const *p;
  89. for (p = backends; *p != NULL; p++)
  90. if (!strcmp((*p)->id, name))
  91. return *p;
  92. return NULL;
  93. }
  94. const struct BackendVtable *backend_vt_from_proto(int proto)
  95. {
  96. const struct BackendVtable *const *p;
  97. for (p = backends; *p != NULL; p++)
  98. if ((*p)->protocol == proto)
  99. return *p;
  100. return NULL;
  101. }
  102. char *get_remote_username(Conf *conf)
  103. {
  104. /* We don't worry about whether the username is stored as UTF-8,
  105. * because SSH wants it as UTF-8 */
  106. char *username = conf_get_str_ambi(conf, CONF_username, NULL);
  107. if (*username) {
  108. return dupstr(username);
  109. } else if (conf_get_bool(conf, CONF_username_from_env)) {
  110. /* Use local username. */
  111. return get_username(); /* might still be NULL */
  112. } else {
  113. return NULL;
  114. }
  115. }
  116. static char *gpps_raw(settings_r *sesskey, const char *name, const char *def)
  117. {
  118. char *ret = read_setting_s(sesskey, name);
  119. if (!ret)
  120. ret = platform_default_s(name);
  121. if (!ret)
  122. ret = def ? dupstr(def) : NULL; /* permit NULL as final fallback */
  123. return ret;
  124. }
  125. static void gpps(settings_r *sesskey, const char *name, const char *def,
  126. Conf *conf, int primary)
  127. {
  128. char *val = gpps_raw(sesskey, name, def);
  129. conf_set_str(conf, primary, val);
  130. sfree(val);
  131. }
  132. /*
  133. * gppfont and gppfile cannot have local defaults, since the very
  134. * format of a Filename or FontSpec is platform-dependent. So the
  135. * platform-dependent functions MUST return some sort of value.
  136. */
  137. static void gppfont(settings_r *sesskey, const char *name,
  138. Conf *conf, int primary)
  139. {
  140. FontSpec *result = read_setting_fontspec(sesskey, name);
  141. if (!result)
  142. result = platform_default_fontspec(name);
  143. conf_set_fontspec(conf, primary, result);
  144. fontspec_free(result);
  145. }
  146. static void gppfile(settings_r *sesskey, const char *name,
  147. Conf *conf, int primary)
  148. {
  149. Filename *result = read_setting_filename(sesskey, name);
  150. if (!result)
  151. result = platform_default_filename(name);
  152. conf_set_filename(conf, primary, result);
  153. filename_free(result);
  154. }
  155. static bool gppb_raw(settings_r *sesskey, const char *name, bool def)
  156. {
  157. def = platform_default_b(name, def);
  158. return read_setting_i(sesskey, name, def) != 0;
  159. }
  160. static void gppb(settings_r *sesskey, const char *name, bool def,
  161. Conf *conf, int primary)
  162. {
  163. conf_set_bool(conf, primary, gppb_raw(sesskey, name, def));
  164. }
  165. static int gppi_raw(settings_r *sesskey, const char *name, int def)
  166. {
  167. def = platform_default_i(name, def);
  168. return read_setting_i(sesskey, name, def);
  169. }
  170. static void gppi(settings_r *sesskey, const char *name, int def,
  171. Conf *conf, int primary)
  172. {
  173. conf_set_int(conf, primary, gppi_raw(sesskey, name, def));
  174. }
  175. /*
  176. * Read a set of name-value pairs in the format we occasionally use:
  177. * NAME\tVALUE\0NAME\tVALUE\0\0 in memory
  178. * NAME=VALUE,NAME=VALUE, in storage
  179. * If there's no "=VALUE" (e.g. just NAME,NAME,NAME) then those keys
  180. * are mapped to the empty string.
  181. */
  182. static bool gppmap(settings_r *sesskey, const char *name,
  183. Conf *conf, int primary)
  184. {
  185. char *buf, *p, *q, *key, *val;
  186. /*
  187. * Start by clearing any existing subkeys of this key from conf.
  188. */
  189. while ((key = conf_get_str_nthstrkey(conf, primary, 0)) != NULL)
  190. conf_del_str_str(conf, primary, key);
  191. /*
  192. * Now read a serialised list from the settings and unmarshal it
  193. * into its components.
  194. */
  195. buf = gpps_raw(sesskey, name, NULL);
  196. if (!buf)
  197. return false;
  198. p = buf;
  199. while (*p) {
  200. q = buf;
  201. val = NULL;
  202. while (*p && *p != ',') {
  203. int c = *p++;
  204. if (c == '=')
  205. c = '\0';
  206. if (c == '\\')
  207. c = *p++;
  208. *q++ = c;
  209. if (!c)
  210. val = q;
  211. }
  212. if (*p == ',')
  213. p++;
  214. if (!val)
  215. val = q;
  216. *q = '\0';
  217. if (primary == CONF_portfwd && strchr(buf, 'D') != NULL) {
  218. /*
  219. * Backwards-compatibility hack: dynamic forwardings are
  220. * indexed in the data store as a third type letter in the
  221. * key, 'D' alongside 'L' and 'R' - but really, they
  222. * should be filed under 'L' with a special _value_,
  223. * because local and dynamic forwardings both involve
  224. * _listening_ on a local port, and are hence mutually
  225. * exclusive on the same port number. So here we translate
  226. * the legacy storage format into the sensible internal
  227. * form, by finding the D and turning it into a L.
  228. */
  229. char *newkey = dupstr(buf);
  230. *strchr(newkey, 'D') = 'L';
  231. conf_set_str_str(conf, primary, newkey, "D");
  232. sfree(newkey);
  233. } else {
  234. conf_set_str_str(conf, primary, buf, val);
  235. }
  236. }
  237. sfree(buf);
  238. return true;
  239. }
  240. /*
  241. * Write a set of name/value pairs in the above format, or just the
  242. * names if include_values is false.
  243. */
  244. static void wmap(settings_w *sesskey, char const *outkey, Conf *conf,
  245. int primary, bool include_values)
  246. {
  247. char *key, *realkey;
  248. const char *val, *q;
  249. strbuf *sb = strbuf_new();
  250. for (val = conf_get_str_strs(conf, primary, NULL, &key);
  251. val != NULL;
  252. val = conf_get_str_strs(conf, primary, key, &key)) {
  253. if (primary == CONF_portfwd && !strcmp(val, "D")) {
  254. /*
  255. * Backwards-compatibility hack, as above: translate from
  256. * the sensible internal representation of dynamic
  257. * forwardings (key "L<port>", value "D") to the
  258. * conceptually incoherent legacy storage format (key
  259. * "D<port>", value empty).
  260. */
  261. char *L;
  262. realkey = key; /* restore it at end of loop */
  263. val = "";
  264. key = dupstr(key);
  265. L = strchr(key, 'L');
  266. if (L) *L = 'D';
  267. } else {
  268. realkey = NULL;
  269. }
  270. if (sb->len)
  271. put_byte(sb, ',');
  272. for (q = key; *q; q++) {
  273. if (*q == '=' || *q == ',' || *q == '\\')
  274. put_byte(sb, '\\');
  275. put_byte(sb, *q);
  276. }
  277. if (include_values) {
  278. put_byte(sb, '=');
  279. for (q = val; *q; q++) {
  280. if (*q == '=' || *q == ',' || *q == '\\')
  281. put_byte(sb, '\\');
  282. put_byte(sb, *q);
  283. }
  284. }
  285. if (realkey) {
  286. free(key);
  287. key = realkey;
  288. }
  289. }
  290. write_setting_s(sesskey, outkey, sb->s);
  291. strbuf_free(sb);
  292. }
  293. static int key2val(const struct keyvalwhere *mapping,
  294. int nmaps, char *key)
  295. {
  296. int i;
  297. for (i = 0; i < nmaps; i++)
  298. if (!strcmp(mapping[i].s, key)) return mapping[i].v;
  299. return -1;
  300. }
  301. static const char *val2key(const struct keyvalwhere *mapping,
  302. int nmaps, int val)
  303. {
  304. int i;
  305. for (i = 0; i < nmaps; i++)
  306. if (mapping[i].v == val) return mapping[i].s;
  307. return NULL;
  308. }
  309. /*
  310. * Helper function to parse a comma-separated list of strings into
  311. * a preference list array of values. Any missing values are added
  312. * to the end and duplicates are weeded.
  313. * XXX: assumes vals in 'mapping' are small +ve integers
  314. */
  315. static void gprefs_from_str(const char *str,
  316. const struct keyvalwhere *mapping, int nvals,
  317. Conf *conf, int primary)
  318. {
  319. char *commalist = dupstr(str);
  320. char *p, *q;
  321. int i, j, n, v, pos;
  322. unsigned long seen = 0; /* bitmap for weeding dups etc */
  323. /*
  324. * Go through that list and convert it into values.
  325. */
  326. n = 0;
  327. p = commalist;
  328. while (1) {
  329. while (*p && *p == ',') p++;
  330. if (!*p)
  331. break; /* no more words */
  332. q = p;
  333. while (*p && *p != ',') p++;
  334. if (*p) *p++ = '\0';
  335. v = key2val(mapping, nvals, q);
  336. if (v != -1 && !(seen & (1 << v))) {
  337. seen |= (1 << v);
  338. conf_set_int_int(conf, primary, n, v);
  339. n++;
  340. }
  341. }
  342. sfree(commalist);
  343. /*
  344. * Now go through 'mapping' and add values that weren't mentioned
  345. * in the list we fetched. We may have to loop over it multiple
  346. * times so that we add values before other values whose default
  347. * positions depend on them.
  348. */
  349. while (n < nvals) {
  350. for (i = 0; i < nvals; i++) {
  351. assert(mapping[i].v >= 0);
  352. assert(mapping[i].v < 32);
  353. if (!(seen & (1 << mapping[i].v))) {
  354. /*
  355. * This element needs adding. But can we add it yet?
  356. */
  357. if (mapping[i].vrel != -1 && !(seen & (1 << mapping[i].vrel)))
  358. continue; /* nope */
  359. /*
  360. * OK, we can work out where to add this element, so
  361. * do so.
  362. */
  363. if (mapping[i].vrel == -1) {
  364. pos = (mapping[i].where < 0 ? n : 0);
  365. } else {
  366. for (j = 0; j < n; j++)
  367. if (conf_get_int_int(conf, primary, j) ==
  368. mapping[i].vrel)
  369. break;
  370. assert(j < n); /* implied by (seen & (1<<vrel)) */
  371. pos = (mapping[i].where < 0 ? j : j+1);
  372. }
  373. /*
  374. * And add it.
  375. */
  376. for (j = n-1; j >= pos; j--)
  377. conf_set_int_int(conf, primary, j+1,
  378. conf_get_int_int(conf, primary, j));
  379. conf_set_int_int(conf, primary, pos, mapping[i].v);
  380. seen |= (1 << mapping[i].v);
  381. n++;
  382. }
  383. }
  384. }
  385. }
  386. /*
  387. * Read a preference list.
  388. */
  389. static void gprefs(settings_r *sesskey, const char *name, const char *def,
  390. const struct keyvalwhere *mapping, int nvals,
  391. Conf *conf, int primary)
  392. {
  393. /*
  394. * Fetch the string which we'll parse as a comma-separated list.
  395. */
  396. char *value = gpps_raw(sesskey, name, def);
  397. gprefs_from_str(value, mapping, nvals, conf, primary);
  398. sfree(value);
  399. }
  400. /*
  401. * Write out a preference list.
  402. */
  403. static void wprefs(settings_w *sesskey, const char *name,
  404. const struct keyvalwhere *mapping, int nvals,
  405. Conf *conf, int primary)
  406. {
  407. strbuf *sb = strbuf_new();
  408. for (int i = 0; i < nvals; i++) {
  409. const char *s = val2key(mapping, nvals,
  410. conf_get_int_int(conf, primary, i));
  411. if (s)
  412. put_fmt(sb, "%s%s", (sb->len ? "," : ""), s);
  413. }
  414. write_setting_s(sesskey, name, sb->s);
  415. strbuf_free(sb);
  416. }
  417. static void write_setting_b(settings_w *handle, const char *key, bool value)
  418. {
  419. write_setting_i(handle, key, value ? 1 : 0);
  420. }
  421. static void write_clip_setting(settings_w *sesskey, const char *savekey,
  422. Conf *conf, int confkey, int strconfkey)
  423. {
  424. int val = conf_get_int(conf, confkey);
  425. switch (val) {
  426. case CLIPUI_NONE:
  427. default:
  428. write_setting_s(sesskey, savekey, "none");
  429. break;
  430. case CLIPUI_IMPLICIT:
  431. write_setting_s(sesskey, savekey, "implicit");
  432. break;
  433. case CLIPUI_EXPLICIT:
  434. write_setting_s(sesskey, savekey, "explicit");
  435. break;
  436. case CLIPUI_CUSTOM: {
  437. char *sval = dupcat("custom:", conf_get_str(conf, strconfkey));
  438. write_setting_s(sesskey, savekey, sval);
  439. sfree(sval);
  440. break;
  441. }
  442. }
  443. }
  444. static void read_clip_setting(settings_r *sesskey, char *savekey,
  445. int def, Conf *conf, int confkey, int strconfkey)
  446. {
  447. char *setting = read_setting_s(sesskey, savekey);
  448. int val;
  449. conf_set_str(conf, strconfkey, "");
  450. if (!setting) {
  451. val = def;
  452. } else if (!strcmp(setting, "implicit")) {
  453. val = CLIPUI_IMPLICIT;
  454. } else if (!strcmp(setting, "explicit")) {
  455. val = CLIPUI_EXPLICIT;
  456. } else if (!strncmp(setting, "custom:", 7)) {
  457. val = CLIPUI_CUSTOM;
  458. conf_set_str(conf, strconfkey, setting + 7);
  459. } else {
  460. val = CLIPUI_NONE;
  461. }
  462. conf_set_int(conf, confkey, val);
  463. sfree(setting);
  464. }
  465. char *save_settings(const char *section, Conf *conf)
  466. {
  467. struct settings_w *sesskey;
  468. char *errmsg;
  469. sesskey = open_settings_w(section, &errmsg);
  470. if (!sesskey)
  471. return errmsg;
  472. save_open_settings(sesskey, conf);
  473. close_settings_w(sesskey);
  474. return NULL;
  475. }
  476. /* Declare extern references to conf_enum_* types */
  477. #define CONF_ENUM(name, ...) extern const ConfSaveEnumType conf_enum_##name;
  478. #include "conf-enums.h"
  479. #undef CONF_ENUM
  480. void save_open_settings(settings_w *sesskey, Conf *conf)
  481. {
  482. int i;
  483. const char *p;
  484. /* Save the settings simple enough to handle automatically */
  485. for (size_t key = 0; key < N_CONFIG_OPTIONS; key++) {
  486. const ConfKeyInfo *info = &conf_key_info[key];
  487. if (!info->save_custom && !info->not_saved) {
  488. /* Mappings are handled individually below */
  489. assert(info->subkey_type == CONF_TYPE_NONE);
  490. switch (info->value_type) {
  491. case CONF_TYPE_STR:
  492. write_setting_s(sesskey, info->save_keyword,
  493. conf_get_str(conf, key));
  494. break;
  495. case CONF_TYPE_STR_AMBI: {
  496. bool orig_is_utf8;
  497. const char *orig = conf_get_str_ambi(conf, key, &orig_is_utf8);
  498. int cp_from, cp_to;
  499. if (orig_is_utf8) {
  500. cp_from = CP_UTF8;
  501. cp_to = DEFAULT_CODEPAGE;
  502. } else {
  503. cp_from = DEFAULT_CODEPAGE;
  504. cp_to = CP_UTF8;
  505. }
  506. size_t wlen;
  507. wchar_t *wide = dup_mb_to_wc_c(
  508. cp_from, orig, strlen(orig), &wlen);
  509. size_t clen;
  510. char *converted = dup_wc_to_mb_c(
  511. cp_to, wide, wlen, "", &clen);
  512. const char *native, *utf8;
  513. if (orig_is_utf8) {
  514. utf8 = orig;
  515. native = converted;
  516. } else {
  517. native = orig;
  518. utf8 = converted;
  519. }
  520. write_setting_s(sesskey, info->save_keyword, native);
  521. (void)utf8; /* FIXME: also save the UTF-8 version */
  522. burnwcs(wide);
  523. burnstr(converted);
  524. break;
  525. }
  526. case CONF_TYPE_INT: {
  527. int ival = conf_get_int(conf, key);
  528. if (info->storage_enum) {
  529. bool success = conf_enum_map_to_storage(
  530. info->storage_enum, ival, &ival);
  531. assert(success && "unmapped integer value");
  532. }
  533. write_setting_i(sesskey, info->save_keyword, ival);
  534. break;
  535. }
  536. case CONF_TYPE_BOOL:
  537. write_setting_b(sesskey, info->save_keyword,
  538. conf_get_bool(conf, key));
  539. break;
  540. case CONF_TYPE_FILENAME:
  541. write_setting_filename(sesskey, info->save_keyword,
  542. conf_get_filename(conf, key));
  543. break;
  544. case CONF_TYPE_FONT:
  545. write_setting_fontspec(sesskey, info->save_keyword,
  546. conf_get_fontspec(conf, key));
  547. break;
  548. default:
  549. unreachable("bad key type in save_open_settings");
  550. }
  551. }
  552. }
  553. write_setting_i(sesskey, "Present", 1);
  554. p = "raw";
  555. {
  556. const struct BackendVtable *vt =
  557. backend_vt_from_proto(conf_get_int(conf, CONF_protocol));
  558. if (vt)
  559. p = vt->id;
  560. }
  561. write_setting_s(sesskey, "Protocol", p);
  562. write_setting_i(sesskey, "PingInterval", conf_get_int(conf, CONF_ping_interval) / 60); /* minutes */
  563. write_setting_i(sesskey, "PingIntervalSecs", conf_get_int(conf, CONF_ping_interval) % 60); /* seconds */
  564. wmap(sesskey, "TerminalModes", conf, CONF_ttymodes, true);
  565. /* proxy settings */
  566. wmap(sesskey, "Environment", conf, CONF_environmt, true);
  567. #ifndef NO_GSSAPI
  568. write_setting_b(sesskey, "GssapiFwd", conf_get_bool(conf, CONF_gssapifwd));
  569. #endif
  570. wprefs(sesskey, "Cipher", ciphernames, CIPHER_MAX, conf, CONF_ssh_cipherlist);
  571. wprefs(sesskey, "KEX", kexnames, KEX_MAX, conf, CONF_ssh_kexlist);
  572. wprefs(sesskey, "HostKey", hknames, HK_MAX, conf, CONF_ssh_hklist);
  573. #ifndef NO_GSSAPI
  574. write_setting_i(sesskey, "GssapiRekey", conf_get_int(conf, CONF_gssapirekey));
  575. #endif
  576. #ifndef NO_GSSAPI
  577. write_setting_b(sesskey, "AuthGSSAPI", conf_get_bool(conf, CONF_try_gssapi_auth));
  578. write_setting_b(sesskey, "AuthGSSAPIKEX", conf_get_bool(conf, CONF_try_gssapi_kex));
  579. wprefs(sesskey, "GSSLibs", gsslibkeywords, ngsslibs, conf, CONF_ssh_gsslist);
  580. write_setting_filename(sesskey, "GSSCustom", conf_get_filename(conf, CONF_ssh_gss_custom));
  581. #endif
  582. #ifdef OSX_META_KEY_CONFIG
  583. write_setting_b(sesskey, "OSXOptionMeta", conf_get_bool(conf, CONF_osx_option_meta));
  584. write_setting_b(sesskey, "OSXCommandMeta", conf_get_bool(conf, CONF_osx_command_meta));
  585. #endif
  586. write_setting_i(sesskey, "BellOverloadT", conf_get_int(conf, CONF_bellovl_t)
  587. #ifdef PUTTY_UNIX_PLATFORM_H
  588. * 1000
  589. #endif
  590. );
  591. write_setting_i(sesskey, "BellOverloadS", conf_get_int(conf, CONF_bellovl_s)
  592. #ifdef PUTTY_UNIX_PLATFORM_H
  593. * 1000
  594. #endif
  595. );
  596. for (i = 0; i < 22; i++) {
  597. char buf[20], buf2[30];
  598. sprintf(buf, "Colour%d", i);
  599. sprintf(buf2, "%d,%d,%d",
  600. conf_get_int_int(conf, CONF_colours, i*3+0),
  601. conf_get_int_int(conf, CONF_colours, i*3+1),
  602. conf_get_int_int(conf, CONF_colours, i*3+2));
  603. write_setting_s(sesskey, buf, buf2);
  604. }
  605. for (i = 0; i < 256; i += 32) {
  606. char buf[20], buf2[256];
  607. int j;
  608. sprintf(buf, "Wordness%d", i);
  609. *buf2 = '\0';
  610. for (j = i; j < i + 32; j++) {
  611. sprintf(buf2 + strlen(buf2), "%s%d",
  612. (*buf2 ? "," : ""),
  613. conf_get_int_int(conf, CONF_wordness, j));
  614. }
  615. write_setting_s(sesskey, buf, buf2);
  616. }
  617. write_clip_setting(sesskey, "MousePaste", conf,
  618. CONF_mousepaste, CONF_mousepaste_custom);
  619. write_clip_setting(sesskey, "CtrlShiftIns", conf,
  620. CONF_ctrlshiftins, CONF_ctrlshiftins_custom);
  621. write_clip_setting(sesskey, "CtrlShiftCV", conf,
  622. CONF_ctrlshiftcv, CONF_ctrlshiftcv_custom);
  623. wmap(sesskey, "PortForwardings", conf, CONF_portfwd, true);
  624. wmap(sesskey, "SSHManualHostKeys", conf, CONF_ssh_manual_hostkeys, false);
  625. }
  626. bool load_settings(const char *section, Conf *conf)
  627. {
  628. settings_r *sesskey;
  629. sesskey = open_settings_r(section);
  630. bool exists = (sesskey != NULL);
  631. load_open_settings(sesskey, conf);
  632. close_settings_r(sesskey);
  633. if (exists && conf_launchable(conf))
  634. add_session_to_jumplist(section);
  635. return exists;
  636. }
  637. void load_open_settings(settings_r *sesskey, Conf *conf)
  638. {
  639. int i;
  640. char *prot;
  641. /* Load the settings simple enough to handle automatically */
  642. for (size_t key = 0; key < N_CONFIG_OPTIONS; key++) {
  643. const ConfKeyInfo *info = &conf_key_info[key];
  644. if (info->not_saved) {
  645. /* Mappings are assumed to default to empty */
  646. if (info->subkey_type == CONF_TYPE_NONE) {
  647. switch (info->value_type) {
  648. case CONF_TYPE_STR:
  649. case CONF_TYPE_STR_AMBI:
  650. conf_set_str(conf, key, info->default_value.sval);
  651. break;
  652. case CONF_TYPE_INT:
  653. conf_set_int(conf, key, info->default_value.ival);
  654. break;
  655. case CONF_TYPE_BOOL:
  656. conf_set_bool(conf, key, info->default_value.bval);
  657. break;
  658. default:
  659. unreachable("bad key type in load_open_settings");
  660. }
  661. }
  662. } else if (!info->load_custom) {
  663. /* Mappings are handled individually below */
  664. assert(info->subkey_type == CONF_TYPE_NONE);
  665. switch (info->value_type) {
  666. case CONF_TYPE_STR:
  667. case CONF_TYPE_STR_AMBI:
  668. gpps(sesskey, info->save_keyword, info->default_value.sval,
  669. conf, key);
  670. break;
  671. case CONF_TYPE_INT:
  672. if (!info->storage_enum) {
  673. gppi(sesskey, info->save_keyword,
  674. info->default_value.ival, conf, key);
  675. } else {
  676. /*
  677. * Because our internal defaults are stored as the
  678. * value we want in Conf, but our API for
  679. * retrieving integers from storage requires a
  680. * default value to fill in if no record is found,
  681. * we must first figure out the default _storage_
  682. * value, ugh.
  683. */
  684. int defstorage;
  685. bool success = conf_enum_map_to_storage(
  686. info->storage_enum, info->default_value.ival,
  687. &defstorage);
  688. assert(success && "unmapped default");
  689. /* Now retrieve the stored value */
  690. int storageval = gppi_raw(sesskey, info->save_keyword,
  691. defstorage);
  692. /* And translate it back to Conf representation,
  693. * replacing it with our Conf-rep default on failure */
  694. int confval;
  695. if (!conf_enum_map_from_storage(
  696. info->storage_enum, storageval, &confval))
  697. confval = info->default_value.ival;
  698. conf_set_int(conf, key, confval);
  699. }
  700. break;
  701. case CONF_TYPE_BOOL:
  702. gppb(sesskey, info->save_keyword, info->default_value.bval,
  703. conf, key);
  704. break;
  705. case CONF_TYPE_FILENAME:
  706. gppfile(sesskey, info->save_keyword, conf, key);
  707. break;
  708. case CONF_TYPE_FONT:
  709. gppfont(sesskey, info->save_keyword, conf, key);
  710. break;
  711. default:
  712. unreachable("bad key type in load_open_settings");
  713. }
  714. }
  715. }
  716. prot = gpps_raw(sesskey, "Protocol", "default");
  717. conf_set_int(conf, CONF_protocol, default_protocol);
  718. conf_set_int(conf, CONF_port, default_port);
  719. {
  720. const struct BackendVtable *vt = backend_vt_from_name(prot);
  721. if (vt) {
  722. conf_set_int(conf, CONF_protocol, vt->protocol);
  723. gppi(sesskey, "PortNumber", default_port, conf, CONF_port);
  724. }
  725. }
  726. sfree(prot);
  727. {
  728. /* This is two values for backward compatibility with 0.50/0.51 */
  729. int pingmin, pingsec;
  730. pingmin = gppi_raw(sesskey, "PingInterval", 0);
  731. pingsec = gppi_raw(sesskey, "PingIntervalSecs", 0);
  732. conf_set_int(conf, CONF_ping_interval, pingmin * 60 + pingsec);
  733. }
  734. if (gppmap(sesskey, "TerminalModes", conf, CONF_ttymodes)) {
  735. /*
  736. * Backwards compatibility with old saved settings.
  737. *
  738. * From the invention of this setting through 0.67, the set of
  739. * terminal modes was fixed, and absence of a mode from this
  740. * setting meant the user had explicitly removed it from the
  741. * UI and we shouldn't send it.
  742. *
  743. * In 0.68, the IUTF8 mode was added, and in handling old
  744. * settings we inadvertently removed the ability to not send
  745. * a mode. Any mode not mentioned was treated as if it was
  746. * set to 'auto' (A).
  747. *
  748. * After 0.68, we added explicit notation to the setting format
  749. * when the user removes a known terminal mode from the list.
  750. *
  751. * So: if any of the modes from the original set is missing, we
  752. * assume this was an intentional removal by the user and add
  753. * an explicit removal ('N'); but if IUTF8 (or any other mode
  754. * added after 0.67) is missing, we assume that its absence is
  755. * due to the setting being old rather than intentional, and
  756. * add it with its default setting.
  757. *
  758. * (This does mean that if a 0.68 user explicitly removed IUTF8,
  759. * we add it back; but removing IUTF8 had no effect in 0.68, so
  760. * we're preserving behaviour, which is the best we can do.)
  761. */
  762. for (i = 0; ttymodes[i]; i++) {
  763. if (!conf_get_str_str_opt(conf, CONF_ttymodes, ttymodes[i])) {
  764. /* Mode not mentioned in setting. */
  765. const char *def;
  766. if (!strcmp(ttymodes[i], "IUTF8")) {
  767. /* Any new modes we add in future should be treated
  768. * this way too. */
  769. def = "A"; /* same as new-setting default below */
  770. } else {
  771. /* One of the original modes. Absence is probably
  772. * deliberate. */
  773. def = "N"; /* don't send */
  774. }
  775. conf_set_str_str(conf, CONF_ttymodes, ttymodes[i], def);
  776. }
  777. }
  778. } else {
  779. /* This hardcodes a big set of defaults in any new saved
  780. * sessions. Let's hope we don't change our mind. */
  781. for (i = 0; ttymodes[i]; i++)
  782. conf_set_str_str(conf, CONF_ttymodes, ttymodes[i], "A");
  783. }
  784. /* proxy settings */
  785. {
  786. int storageval = gppi_raw(sesskey, "ProxyMethod", -1);
  787. int confval;
  788. if (!conf_enum_map_from_storage(&conf_enum_proxy_type,
  789. storageval, &confval)) {
  790. /*
  791. * Fall back to older ProxyType and ProxySOCKSVersion format
  792. */
  793. storageval = gppi_raw(sesskey, "ProxyType", 0);
  794. if (conf_enum_map_from_storage(&conf_enum_old_proxy_type,
  795. storageval, &confval)) {
  796. if (confval == PROXY_SOCKS5 &&
  797. gppi_raw(sesskey, "ProxySOCKSVersion", 5) == 4)
  798. confval = PROXY_SOCKS4;
  799. }
  800. }
  801. conf_set_int(conf, CONF_proxy_type, confval);
  802. }
  803. gppmap(sesskey, "Environment", conf, CONF_environmt);
  804. #ifndef NO_GSSAPI
  805. gppb(sesskey, "GssapiFwd", false, conf, CONF_gssapifwd);
  806. #endif
  807. gprefs(sesskey, "Cipher", "\0",
  808. ciphernames, CIPHER_MAX, conf, CONF_ssh_cipherlist);
  809. {
  810. /* Backward-compatibility: before 0.58 (when the "KEX"
  811. * preference was first added), we had an option to
  812. * disable gex under the "bugs" panel after one report of
  813. * a server which offered it then choked, but we never got
  814. * a server version string or any other reports. */
  815. const char *default_kexes,
  816. *normal_default = "ecdh,dh-gex-sha1,dh-group18-sha512,dh-group17-sha512,dh-group16-sha512,dh-group15-sha512,dh-group14-sha1,rsa,"
  817. "WARN,dh-group1-sha1",
  818. *bugdhgex2_default = "ecdh,dh-group18-sha512,dh-group17-sha512,dh-group16-sha512,dh-group15-sha512,dh-group14-sha1,rsa,"
  819. "WARN,dh-group1-sha1,dh-gex-sha1";
  820. char *raw;
  821. i = 2 - gppi_raw(sesskey, "BugDHGEx2", 0);
  822. if (i == FORCE_ON)
  823. default_kexes = bugdhgex2_default;
  824. else
  825. default_kexes = normal_default;
  826. /* Migration: after 0.67 we decided we didn't like
  827. * dh-group1-sha1. If it looks like the user never changed
  828. * the defaults, quietly upgrade their settings to demote it.
  829. * (If they did, they're on their own.) */
  830. raw = gpps_raw(sesskey, "KEX", default_kexes);
  831. assert(raw != NULL);
  832. /* Lack of 'ecdh' tells us this was saved by 0.58-0.67
  833. * inclusive. If it was saved by a later version, we need
  834. * to leave it alone. */
  835. if (strcmp(raw, "dh-group14-sha1,dh-group1-sha1,rsa,"
  836. "WARN,dh-gex-sha1") == 0) {
  837. /* Previously migrated from BugDHGEx2. */
  838. sfree(raw);
  839. raw = dupstr(bugdhgex2_default);
  840. } else if (strcmp(raw, "dh-gex-sha1,dh-group14-sha1,"
  841. "dh-group1-sha1,rsa,WARN") == 0) {
  842. /* Untouched old default setting. */
  843. sfree(raw);
  844. raw = dupstr(normal_default);
  845. }
  846. /* (For the record: after 0.70, the default algorithm list
  847. * very briefly contained the string 'gss-sha1-krb5'; this was
  848. * never used in any committed version of code, but was left
  849. * over from a pre-commit version of GSS key exchange.
  850. * Mentioned here as it is remotely possible that it will turn
  851. * up in someone's saved settings in future.) */
  852. gprefs_from_str(raw, kexnames, KEX_MAX, conf, CONF_ssh_kexlist);
  853. sfree(raw);
  854. }
  855. gprefs(sesskey, "HostKey", "ed25519,ecdsa,rsa,dsa,WARN",
  856. hknames, HK_MAX, conf, CONF_ssh_hklist);
  857. #ifndef NO_GSSAPI
  858. gppi(sesskey, "GssapiRekey", GSS_DEF_REKEY_MINS, conf, CONF_gssapirekey);
  859. gppb(sesskey, "AuthGSSAPI", true, conf, CONF_try_gssapi_auth);
  860. gppb(sesskey, "AuthGSSAPIKEX", true, conf, CONF_try_gssapi_kex);
  861. gprefs(sesskey, "GSSLibs", "\0",
  862. gsslibkeywords, ngsslibs, conf, CONF_ssh_gsslist);
  863. gppfile(sesskey, "GSSCustom", conf, CONF_ssh_gss_custom);
  864. #endif
  865. {
  866. int storageval = gppi_raw(sesskey, "RemoteQTitleAction", -1);
  867. int confval;
  868. if (!conf_enum_map_from_storage(&conf_enum_remote_qtitle_action,
  869. storageval, &confval)) {
  870. /*
  871. * Fall back to older NoRemoteQTitle format
  872. */
  873. storageval = gppi_raw(sesskey, "NoRemoteQTitle", 1);
  874. /* We deliberately interpret the old setting of "no response" as
  875. * "empty string". This changes the behaviour, but hopefully for
  876. * the better; the user can always recover the old behaviour. */
  877. confval = storageval ? TITLE_EMPTY : TITLE_REAL;
  878. }
  879. conf_set_int(conf, CONF_remote_qtitle_action, confval);
  880. }
  881. #ifdef OSX_META_KEY_CONFIG
  882. gppb(sesskey, "OSXOptionMeta", true, conf, CONF_osx_option_meta);
  883. gppb(sesskey, "OSXCommandMeta", false, conf, CONF_osx_command_meta);
  884. #endif
  885. i = gppi_raw(sesskey, "BellOverloadT", 2*TICKSPERSEC
  886. #ifdef PUTTY_UNIX_PLATFORM_H
  887. *1000
  888. #endif
  889. );
  890. conf_set_int(conf, CONF_bellovl_t, i
  891. #ifdef PUTTY_UNIX_PLATFORM_H
  892. / 1000
  893. #endif
  894. );
  895. i = gppi_raw(sesskey, "BellOverloadS", 5*TICKSPERSEC
  896. #ifdef PUTTY_UNIX_PLATFORM_H
  897. *1000
  898. #endif
  899. );
  900. conf_set_int(conf, CONF_bellovl_s, i
  901. #ifdef PUTTY_UNIX_PLATFORM_H
  902. / 1000
  903. #endif
  904. );
  905. for (i = 0; i < 22; i++) {
  906. static const char *const defaults[] = {
  907. "187,187,187", "255,255,255", "0,0,0", "85,85,85", "0,0,0",
  908. "0,255,0", "0,0,0", "85,85,85", "187,0,0", "255,85,85",
  909. "0,187,0", "85,255,85", "187,187,0", "255,255,85", "0,0,187",
  910. "85,85,255", "187,0,187", "255,85,255", "0,187,187",
  911. "85,255,255", "187,187,187", "255,255,255"
  912. };
  913. char buf[20], *buf2;
  914. int c0, c1, c2;
  915. sprintf(buf, "Colour%d", i);
  916. buf2 = gpps_raw(sesskey, buf, defaults[i]);
  917. if (sscanf(buf2, "%d,%d,%d", &c0, &c1, &c2) == 3) {
  918. conf_set_int_int(conf, CONF_colours, i*3+0, c0);
  919. conf_set_int_int(conf, CONF_colours, i*3+1, c1);
  920. conf_set_int_int(conf, CONF_colours, i*3+2, c2);
  921. }
  922. sfree(buf2);
  923. }
  924. for (i = 0; i < 256; i += 32) {
  925. static const char *const defaults[] = {
  926. "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",
  927. "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",
  928. "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",
  929. "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",
  930. "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",
  931. "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",
  932. "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",
  933. "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"
  934. };
  935. char buf[20], *buf2, *p;
  936. int j;
  937. sprintf(buf, "Wordness%d", i);
  938. buf2 = gpps_raw(sesskey, buf, defaults[i / 32]);
  939. p = buf2;
  940. for (j = i; j < i + 32; j++) {
  941. char *q = p;
  942. while (*p && *p != ',')
  943. p++;
  944. if (*p == ',')
  945. *p++ = '\0';
  946. conf_set_int_int(conf, CONF_wordness, j, atoi(q));
  947. }
  948. sfree(buf2);
  949. }
  950. read_clip_setting(sesskey, "MousePaste", CLIPUI_DEFAULT_MOUSE,
  951. conf, CONF_mousepaste, CONF_mousepaste_custom);
  952. read_clip_setting(sesskey, "CtrlShiftIns", CLIPUI_DEFAULT_INS,
  953. conf, CONF_ctrlshiftins, CONF_ctrlshiftins_custom);
  954. read_clip_setting(sesskey, "CtrlShiftCV", CLIPUI_NONE,
  955. conf, CONF_ctrlshiftcv, CONF_ctrlshiftcv_custom);
  956. /*
  957. * The empty default for LineCodePage will be converted later
  958. * into a plausible default for the locale.
  959. */
  960. gppmap(sesskey, "PortForwardings", conf, CONF_portfwd);
  961. {
  962. int i;
  963. i = gppi_raw(sesskey, "BugHMAC2", 0); conf_set_int(conf, CONF_sshbug_hmac2, 2-i);
  964. if (2-i == AUTO) {
  965. i = gppi_raw(sesskey, "BuggyMAC", 0);
  966. if (i == 1)
  967. conf_set_int(conf, CONF_sshbug_hmac2, FORCE_ON);
  968. }
  969. }
  970. gppmap(sesskey, "SSHManualHostKeys", conf, CONF_ssh_manual_hostkeys);
  971. }
  972. bool do_defaults(const char *session, Conf *conf)
  973. {
  974. return load_settings(session, conf);
  975. }
  976. static int sessioncmp(const void *av, const void *bv)
  977. {
  978. const char *a = *(const char *const *) av;
  979. const char *b = *(const char *const *) bv;
  980. /*
  981. * Alphabetical order, except that "Default Settings" is a
  982. * special case and comes first.
  983. */
  984. if (!strcmp(a, "Default Settings"))
  985. return -1; /* a comes first */
  986. if (!strcmp(b, "Default Settings"))
  987. return +1; /* b comes first */
  988. /*
  989. * FIXME: perhaps we should ignore the first & in determining
  990. * sort order.
  991. */
  992. return strcmp(a, b); /* otherwise, compare normally */
  993. }
  994. bool sesslist_demo_mode = false;
  995. void get_sesslist(struct sesslist *list, bool allocate)
  996. {
  997. int i;
  998. char *p;
  999. settings_e *handle;
  1000. if (allocate) {
  1001. strbuf *sb = strbuf_new();
  1002. if (sesslist_demo_mode) {
  1003. put_asciz(sb, "demo-server");
  1004. put_asciz(sb, "demo-server-2");
  1005. } else {
  1006. if ((handle = enum_settings_start()) != NULL) {
  1007. while (enum_settings_next(handle, sb))
  1008. put_byte(sb, '\0');
  1009. enum_settings_finish(handle);
  1010. }
  1011. put_byte(sb, '\0');
  1012. }
  1013. list->buffer = strbuf_to_str(sb);
  1014. /*
  1015. * Now set up the list of sessions. Note that "Default
  1016. * Settings" must always be claimed to exist, even if it
  1017. * doesn't really.
  1018. */
  1019. p = list->buffer;
  1020. list->nsessions = 1; /* "Default Settings" counts as one */
  1021. while (*p) {
  1022. if (strcmp(p, "Default Settings"))
  1023. list->nsessions++;
  1024. while (*p)
  1025. p++;
  1026. p++;
  1027. }
  1028. list->sessions = snewn(list->nsessions + 1, const char *);
  1029. list->sessions[0] = "Default Settings";
  1030. p = list->buffer;
  1031. i = 1;
  1032. while (*p) {
  1033. if (strcmp(p, "Default Settings"))
  1034. list->sessions[i++] = p;
  1035. while (*p)
  1036. p++;
  1037. p++;
  1038. }
  1039. qsort(list->sessions, i, sizeof(const char *), sessioncmp);
  1040. } else {
  1041. sfree(list->buffer);
  1042. sfree(list->sessions);
  1043. list->buffer = NULL;
  1044. list->sessions = NULL;
  1045. }
  1046. }