settings.c 39 KB

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