settings.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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. int i; // WINSCP
  409. for (i = 0; i < nvals; i++) {
  410. const char *s = val2key(mapping, nvals,
  411. conf_get_int_int(conf, primary, i));
  412. if (s)
  413. put_fmt(sb, "%s%s", (sb->len ? "," : ""), s);
  414. }
  415. write_setting_s(sesskey, name, sb->s);
  416. strbuf_free(sb);
  417. }
  418. static void write_setting_b(settings_w *handle, const char *key, bool value)
  419. {
  420. write_setting_i(handle, key, value ? 1 : 0);
  421. }
  422. static void write_clip_setting(settings_w *sesskey, const char *savekey,
  423. Conf *conf, int confkey, int strconfkey)
  424. {
  425. int val = conf_get_int(conf, confkey);
  426. switch (val) {
  427. case CLIPUI_NONE:
  428. default:
  429. write_setting_s(sesskey, savekey, "none");
  430. break;
  431. case CLIPUI_IMPLICIT:
  432. write_setting_s(sesskey, savekey, "implicit");
  433. break;
  434. case CLIPUI_EXPLICIT:
  435. write_setting_s(sesskey, savekey, "explicit");
  436. break;
  437. case CLIPUI_CUSTOM: {
  438. char *sval = dupcat("custom:", conf_get_str(conf, strconfkey));
  439. write_setting_s(sesskey, savekey, sval);
  440. sfree(sval);
  441. break;
  442. }
  443. }
  444. }
  445. static void read_clip_setting(settings_r *sesskey, char *savekey,
  446. int def, Conf *conf, int confkey, int strconfkey)
  447. {
  448. char *setting = read_setting_s(sesskey, savekey);
  449. int val;
  450. conf_set_str(conf, strconfkey, "");
  451. if (!setting) {
  452. val = def;
  453. } else if (!strcmp(setting, "implicit")) {
  454. val = CLIPUI_IMPLICIT;
  455. } else if (!strcmp(setting, "explicit")) {
  456. val = CLIPUI_EXPLICIT;
  457. } else if (!strncmp(setting, "custom:", 7)) {
  458. val = CLIPUI_CUSTOM;
  459. conf_set_str(conf, strconfkey, setting + 7);
  460. } else {
  461. val = CLIPUI_NONE;
  462. }
  463. conf_set_int(conf, confkey, val);
  464. sfree(setting);
  465. }
  466. char *save_settings(const char *section, Conf *conf)
  467. {
  468. struct settings_w *sesskey;
  469. char *errmsg;
  470. sesskey = open_settings_w(section, &errmsg);
  471. if (!sesskey)
  472. return errmsg;
  473. save_open_settings(sesskey, conf);
  474. close_settings_w(sesskey);
  475. return NULL;
  476. }
  477. /* Declare extern references to conf_enum_* types */
  478. #define CONF_ENUM(name, ...) extern const ConfSaveEnumType conf_enum_##name;
  479. #include "conf-enums.h"
  480. #undef CONF_ENUM
  481. void save_open_settings(settings_w *sesskey, Conf *conf)
  482. {
  483. int i;
  484. const char *p;
  485. /* Save the settings simple enough to handle automatically */
  486. size_t key; // WINSCP
  487. for (key = 0; key < N_CONFIG_OPTIONS; key++) {
  488. const ConfKeyInfo *info = &conf_key_info[key];
  489. if (!info->save_custom && !info->not_saved) {
  490. /* Mappings are handled individually below */
  491. assert(info->subkey_type == CONF_TYPE_NONE);
  492. switch (info->value_type) {
  493. case CONF_TYPE_STR:
  494. write_setting_s(sesskey, info->save_keyword,
  495. conf_get_str(conf, key));
  496. break;
  497. case CONF_TYPE_STR_AMBI: {
  498. bool orig_is_utf8;
  499. const char *orig = conf_get_str_ambi(conf, key, &orig_is_utf8);
  500. int cp_from, cp_to;
  501. if (orig_is_utf8) {
  502. cp_from = CP_UTF8;
  503. cp_to = DEFAULT_CODEPAGE;
  504. } else {
  505. cp_from = DEFAULT_CODEPAGE;
  506. cp_to = CP_UTF8;
  507. }
  508. { // WINSCP
  509. size_t wlen;
  510. wchar_t *wide = dup_mb_to_wc_c(
  511. cp_from, orig, strlen(orig), &wlen);
  512. size_t clen;
  513. char *converted = dup_wc_to_mb_c(
  514. cp_to, wide, wlen, "", &clen);
  515. const char *native, *utf8;
  516. if (orig_is_utf8) {
  517. utf8 = orig;
  518. native = converted;
  519. } else {
  520. native = orig;
  521. utf8 = converted;
  522. }
  523. write_setting_s(sesskey, info->save_keyword, native);
  524. (void)utf8; /* FIXME: also save the UTF-8 version */
  525. burnwcs(wide);
  526. burnstr(converted);
  527. } // WINSCP
  528. break;
  529. }
  530. case CONF_TYPE_INT: {
  531. int ival = conf_get_int(conf, key);
  532. if (info->storage_enum) {
  533. bool success = conf_enum_map_to_storage(
  534. info->storage_enum, ival, &ival);
  535. assert(success && "unmapped integer value");
  536. }
  537. write_setting_i(sesskey, info->save_keyword, ival);
  538. break;
  539. }
  540. case CONF_TYPE_BOOL:
  541. write_setting_b(sesskey, info->save_keyword,
  542. conf_get_bool(conf, key));
  543. break;
  544. case CONF_TYPE_FILENAME:
  545. write_setting_filename(sesskey, info->save_keyword,
  546. conf_get_filename(conf, key));
  547. break;
  548. case CONF_TYPE_FONT:
  549. write_setting_fontspec(sesskey, info->save_keyword,
  550. conf_get_fontspec(conf, key));
  551. break;
  552. default:
  553. unreachable("bad key type in save_open_settings");
  554. }
  555. }
  556. }
  557. write_setting_i(sesskey, "Present", 1);
  558. p = "raw";
  559. {
  560. const struct BackendVtable *vt =
  561. backend_vt_from_proto(conf_get_int(conf, CONF_protocol));
  562. if (vt)
  563. p = vt->id;
  564. }
  565. write_setting_s(sesskey, "Protocol", p);
  566. write_setting_i(sesskey, "PingInterval", conf_get_int(conf, CONF_ping_interval) / 60); /* minutes */
  567. write_setting_i(sesskey, "PingIntervalSecs", conf_get_int(conf, CONF_ping_interval) % 60); /* seconds */
  568. wmap(sesskey, "TerminalModes", conf, CONF_ttymodes, true);
  569. /* proxy settings */
  570. wmap(sesskey, "Environment", conf, CONF_environmt, true);
  571. #ifndef NO_GSSAPI
  572. write_setting_b(sesskey, "GssapiFwd", conf_get_bool(conf, CONF_gssapifwd));
  573. #endif
  574. wprefs(sesskey, "Cipher", ciphernames, CIPHER_MAX, conf, CONF_ssh_cipherlist);
  575. wprefs(sesskey, "KEX", kexnames, KEX_MAX, conf, CONF_ssh_kexlist);
  576. wprefs(sesskey, "HostKey", hknames, HK_MAX, conf, CONF_ssh_hklist);
  577. #ifndef NO_GSSAPI
  578. write_setting_i(sesskey, "GssapiRekey", conf_get_int(conf, CONF_gssapirekey));
  579. #endif
  580. #ifndef NO_GSSAPI
  581. write_setting_b(sesskey, "AuthGSSAPI", conf_get_bool(conf, CONF_try_gssapi_auth));
  582. write_setting_b(sesskey, "AuthGSSAPIKEX", conf_get_bool(conf, CONF_try_gssapi_kex));
  583. wprefs(sesskey, "GSSLibs", gsslibkeywords, ngsslibs, conf, CONF_ssh_gsslist);
  584. write_setting_filename(sesskey, "GSSCustom", conf_get_filename(conf, CONF_ssh_gss_custom));
  585. #endif
  586. #ifdef OSX_META_KEY_CONFIG
  587. write_setting_b(sesskey, "OSXOptionMeta", conf_get_bool(conf, CONF_osx_option_meta));
  588. write_setting_b(sesskey, "OSXCommandMeta", conf_get_bool(conf, CONF_osx_command_meta));
  589. #endif
  590. write_setting_i(sesskey, "BellOverloadT", conf_get_int(conf, CONF_bellovl_t)
  591. #ifdef PUTTY_UNIX_PLATFORM_H
  592. * 1000
  593. #endif
  594. );
  595. write_setting_i(sesskey, "BellOverloadS", conf_get_int(conf, CONF_bellovl_s)
  596. #ifdef PUTTY_UNIX_PLATFORM_H
  597. * 1000
  598. #endif
  599. );
  600. for (i = 0; i < 22; i++) {
  601. char buf[20], buf2[30];
  602. sprintf(buf, "Colour%d", i);
  603. sprintf(buf2, "%d,%d,%d",
  604. conf_get_int_int(conf, CONF_colours, i*3+0),
  605. conf_get_int_int(conf, CONF_colours, i*3+1),
  606. conf_get_int_int(conf, CONF_colours, i*3+2));
  607. write_setting_s(sesskey, buf, buf2);
  608. }
  609. for (i = 0; i < 256; i += 32) {
  610. char buf[20], buf2[256];
  611. int j;
  612. sprintf(buf, "Wordness%d", i);
  613. *buf2 = '\0';
  614. for (j = i; j < i + 32; j++) {
  615. sprintf(buf2 + strlen(buf2), "%s%d",
  616. (*buf2 ? "," : ""),
  617. conf_get_int_int(conf, CONF_wordness, j));
  618. }
  619. write_setting_s(sesskey, buf, buf2);
  620. }
  621. write_clip_setting(sesskey, "MousePaste", conf,
  622. CONF_mousepaste, CONF_mousepaste_custom);
  623. write_clip_setting(sesskey, "CtrlShiftIns", conf,
  624. CONF_ctrlshiftins, CONF_ctrlshiftins_custom);
  625. write_clip_setting(sesskey, "CtrlShiftCV", conf,
  626. CONF_ctrlshiftcv, CONF_ctrlshiftcv_custom);
  627. wmap(sesskey, "PortForwardings", conf, CONF_portfwd, true);
  628. wmap(sesskey, "SSHManualHostKeys", conf, CONF_ssh_manual_hostkeys, false);
  629. }
  630. bool load_settings(const char *section, Conf *conf)
  631. {
  632. settings_r *sesskey;
  633. sesskey = open_settings_r(section);
  634. { // WINSCP
  635. bool exists = (sesskey != NULL);
  636. load_open_settings(sesskey, conf);
  637. close_settings_r(sesskey);
  638. if (exists && conf_launchable(conf))
  639. add_session_to_jumplist(section);
  640. return exists;
  641. } // WINSCP
  642. }
  643. void load_open_settings(settings_r *sesskey, Conf *conf)
  644. {
  645. int i;
  646. char *prot;
  647. // WINSCP BEGIN Testing that our conf.winscp.h projection is valid
  648. assert(conf_key_info[CONF_host].subkey_type == CONF_TYPE_NONE);
  649. assert(conf_key_info[CONF_host].value_type == CONF_TYPE_STR);
  650. assert(strcmp(conf_key_info[CONF_host].default_value.sval, "") == 0);
  651. assert(!conf_key_info[CONF_host].save_custom);
  652. assert(!conf_key_info[CONF_host].load_custom);
  653. assert(!conf_key_info[CONF_host].not_saved);
  654. assert(strcmp(conf_key_info[CONF_host].save_keyword, "HostName") == 0);
  655. assert(conf_key_info[CONF_host].storage_enum == NULL);
  656. assert(conf_key_info[CONF_close_on_exit].default_value.ival == AUTO);
  657. assert(conf_key_info[CONF_proxy_type].storage_enum == &conf_enum_proxy_type);
  658. assert(conf_key_info[CONF_remote_cmd2].not_saved);
  659. assert(strcmp(conf_key_info[CONF_proxy_host].default_value.sval, "proxy") == 0);
  660. assert(conf_key_info[CONF_ssh_kexlist].subkey_type == CONF_TYPE_INT);
  661. assert(conf_key_info[CONF_ssh_kexlist].save_custom);
  662. assert(conf_key_info[CONF_ssh_kexlist].load_custom);
  663. assert(conf_key_info[CONF_ssh_kexlist].save_keyword == NULL);
  664. assert(conf_key_info[CONF_ssh_prefer_known_hostkeys].default_value.bval == true);
  665. // WINSCP END
  666. { // WINSCP
  667. /* Load the settings simple enough to handle automatically */
  668. size_t key; // WINSCP
  669. for (key = 0; key < N_CONFIG_OPTIONS; key++) {
  670. const ConfKeyInfo *info = &conf_key_info[key];
  671. if (info->not_saved) {
  672. /* Mappings are assumed to default to empty */
  673. if (info->subkey_type == CONF_TYPE_NONE) {
  674. switch (info->value_type) {
  675. case CONF_TYPE_STR:
  676. case CONF_TYPE_STR_AMBI:
  677. conf_set_str(conf, key, info->default_value.sval);
  678. break;
  679. case CONF_TYPE_INT:
  680. conf_set_int(conf, key, info->default_value.ival);
  681. break;
  682. case CONF_TYPE_BOOL:
  683. conf_set_bool(conf, key, info->default_value.bval);
  684. break;
  685. default:
  686. unreachable("bad key type in load_open_settings");
  687. }
  688. }
  689. } else if (!info->load_custom) {
  690. /* Mappings are handled individually below */
  691. assert(info->subkey_type == CONF_TYPE_NONE);
  692. switch (info->value_type) {
  693. case CONF_TYPE_STR:
  694. case CONF_TYPE_STR_AMBI:
  695. gpps(sesskey, info->save_keyword, info->default_value.sval,
  696. conf, key);
  697. break;
  698. case CONF_TYPE_INT:
  699. if (!info->storage_enum) {
  700. gppi(sesskey, info->save_keyword,
  701. info->default_value.ival, conf, key);
  702. } else {
  703. /*
  704. * Because our internal defaults are stored as the
  705. * value we want in Conf, but our API for
  706. * retrieving integers from storage requires a
  707. * default value to fill in if no record is found,
  708. * we must first figure out the default _storage_
  709. * value, ugh.
  710. */
  711. int defstorage;
  712. bool success = conf_enum_map_to_storage(
  713. info->storage_enum, info->default_value.ival,
  714. &defstorage);
  715. assert(success && "unmapped default");
  716. /* Now retrieve the stored value */
  717. { // WINSCP
  718. int storageval = gppi_raw(sesskey, info->save_keyword,
  719. defstorage);
  720. /* And translate it back to Conf representation,
  721. * replacing it with our Conf-rep default on failure */
  722. int confval;
  723. if (!conf_enum_map_from_storage(
  724. info->storage_enum, storageval, &confval))
  725. confval = info->default_value.ival;
  726. conf_set_int(conf, key, confval);
  727. } // WINSCP
  728. }
  729. break;
  730. case CONF_TYPE_BOOL:
  731. gppb(sesskey, info->save_keyword, info->default_value.bval,
  732. conf, key);
  733. break;
  734. case CONF_TYPE_FILENAME:
  735. gppfile(sesskey, info->save_keyword, conf, key);
  736. break;
  737. case CONF_TYPE_FONT:
  738. gppfont(sesskey, info->save_keyword, conf, key);
  739. break;
  740. default:
  741. unreachable("bad key type in load_open_settings");
  742. }
  743. }
  744. }
  745. prot = gpps_raw(sesskey, "Protocol", "default");
  746. conf_set_int(conf, CONF_protocol, default_protocol);
  747. conf_set_int(conf, CONF_port, default_port);
  748. {
  749. const struct BackendVtable *vt = backend_vt_from_name(prot);
  750. if (vt) {
  751. conf_set_int(conf, CONF_protocol, vt->protocol);
  752. gppi(sesskey, "PortNumber", default_port, conf, CONF_port);
  753. }
  754. }
  755. sfree(prot);
  756. {
  757. /* This is two values for backward compatibility with 0.50/0.51 */
  758. int pingmin, pingsec;
  759. pingmin = gppi_raw(sesskey, "PingInterval", 0);
  760. pingsec = gppi_raw(sesskey, "PingIntervalSecs", 0);
  761. conf_set_int(conf, CONF_ping_interval, pingmin * 60 + pingsec);
  762. }
  763. if (gppmap(sesskey, "TerminalModes", conf, CONF_ttymodes)) {
  764. /*
  765. * Backwards compatibility with old saved settings.
  766. *
  767. * From the invention of this setting through 0.67, the set of
  768. * terminal modes was fixed, and absence of a mode from this
  769. * setting meant the user had explicitly removed it from the
  770. * UI and we shouldn't send it.
  771. *
  772. * In 0.68, the IUTF8 mode was added, and in handling old
  773. * settings we inadvertently removed the ability to not send
  774. * a mode. Any mode not mentioned was treated as if it was
  775. * set to 'auto' (A).
  776. *
  777. * After 0.68, we added explicit notation to the setting format
  778. * when the user removes a known terminal mode from the list.
  779. *
  780. * So: if any of the modes from the original set is missing, we
  781. * assume this was an intentional removal by the user and add
  782. * an explicit removal ('N'); but if IUTF8 (or any other mode
  783. * added after 0.67) is missing, we assume that its absence is
  784. * due to the setting being old rather than intentional, and
  785. * add it with its default setting.
  786. *
  787. * (This does mean that if a 0.68 user explicitly removed IUTF8,
  788. * we add it back; but removing IUTF8 had no effect in 0.68, so
  789. * we're preserving behaviour, which is the best we can do.)
  790. */
  791. for (i = 0; ttymodes[i]; i++) {
  792. if (!conf_get_str_str_opt(conf, CONF_ttymodes, ttymodes[i])) {
  793. /* Mode not mentioned in setting. */
  794. const char *def;
  795. if (!strcmp(ttymodes[i], "IUTF8")) {
  796. /* Any new modes we add in future should be treated
  797. * this way too. */
  798. def = "A"; /* same as new-setting default below */
  799. } else {
  800. /* One of the original modes. Absence is probably
  801. * deliberate. */
  802. def = "N"; /* don't send */
  803. }
  804. conf_set_str_str(conf, CONF_ttymodes, ttymodes[i], def);
  805. }
  806. }
  807. } else {
  808. /* This hardcodes a big set of defaults in any new saved
  809. * sessions. Let's hope we don't change our mind. */
  810. for (i = 0; ttymodes[i]; i++)
  811. conf_set_str_str(conf, CONF_ttymodes, ttymodes[i], "A");
  812. }
  813. /* proxy settings */
  814. {
  815. int storageval = gppi_raw(sesskey, "ProxyMethod", -1);
  816. int confval;
  817. if (!conf_enum_map_from_storage(&conf_enum_proxy_type,
  818. storageval, &confval)) {
  819. /*
  820. * Fall back to older ProxyType and ProxySOCKSVersion format
  821. */
  822. storageval = gppi_raw(sesskey, "ProxyType", 0);
  823. if (conf_enum_map_from_storage(&conf_enum_old_proxy_type,
  824. storageval, &confval)) {
  825. if (confval == PROXY_SOCKS5 &&
  826. gppi_raw(sesskey, "ProxySOCKSVersion", 5) == 4)
  827. confval = PROXY_SOCKS4;
  828. }
  829. }
  830. conf_set_int(conf, CONF_proxy_type, confval);
  831. }
  832. gppmap(sesskey, "Environment", conf, CONF_environmt);
  833. #ifndef NO_GSSAPI
  834. gppb(sesskey, "GssapiFwd", false, conf, CONF_gssapifwd);
  835. #endif
  836. gprefs(sesskey, "Cipher", "\0",
  837. ciphernames, CIPHER_MAX, conf, CONF_ssh_cipherlist);
  838. {
  839. /* Backward-compatibility: before 0.58 (when the "KEX"
  840. * preference was first added), we had an option to
  841. * disable gex under the "bugs" panel after one report of
  842. * a server which offered it then choked, but we never got
  843. * a server version string or any other reports. */
  844. const char *default_kexes,
  845. *normal_default = "ecdh,dh-gex-sha1,dh-group18-sha512,dh-group17-sha512,dh-group16-sha512,dh-group15-sha512,dh-group14-sha1,rsa,"
  846. "WARN,dh-group1-sha1",
  847. *bugdhgex2_default = "ecdh,dh-group18-sha512,dh-group17-sha512,dh-group16-sha512,dh-group15-sha512,dh-group14-sha1,rsa,"
  848. "WARN,dh-group1-sha1,dh-gex-sha1";
  849. char *raw;
  850. i = 2 - gppi_raw(sesskey, "BugDHGEx2", 0);
  851. if (i == FORCE_ON)
  852. default_kexes = bugdhgex2_default;
  853. else
  854. default_kexes = normal_default;
  855. /* Migration: after 0.67 we decided we didn't like
  856. * dh-group1-sha1. If it looks like the user never changed
  857. * the defaults, quietly upgrade their settings to demote it.
  858. * (If they did, they're on their own.) */
  859. raw = gpps_raw(sesskey, "KEX", default_kexes);
  860. assert(raw != NULL);
  861. /* Lack of 'ecdh' tells us this was saved by 0.58-0.67
  862. * inclusive. If it was saved by a later version, we need
  863. * to leave it alone. */
  864. if (strcmp(raw, "dh-group14-sha1,dh-group1-sha1,rsa,"
  865. "WARN,dh-gex-sha1") == 0) {
  866. /* Previously migrated from BugDHGEx2. */
  867. sfree(raw);
  868. raw = dupstr(bugdhgex2_default);
  869. } else if (strcmp(raw, "dh-gex-sha1,dh-group14-sha1,"
  870. "dh-group1-sha1,rsa,WARN") == 0) {
  871. /* Untouched old default setting. */
  872. sfree(raw);
  873. raw = dupstr(normal_default);
  874. }
  875. /* (For the record: after 0.70, the default algorithm list
  876. * very briefly contained the string 'gss-sha1-krb5'; this was
  877. * never used in any committed version of code, but was left
  878. * over from a pre-commit version of GSS key exchange.
  879. * Mentioned here as it is remotely possible that it will turn
  880. * up in someone's saved settings in future.) */
  881. gprefs_from_str(raw, kexnames, KEX_MAX, conf, CONF_ssh_kexlist);
  882. sfree(raw);
  883. }
  884. gprefs(sesskey, "HostKey", "ed25519,ecdsa,rsa,dsa,WARN",
  885. hknames, HK_MAX, conf, CONF_ssh_hklist);
  886. #ifndef NO_GSSAPI
  887. gppi(sesskey, "GssapiRekey", GSS_DEF_REKEY_MINS, conf, CONF_gssapirekey);
  888. gppb(sesskey, "AuthGSSAPI", true, conf, CONF_try_gssapi_auth);
  889. gppb(sesskey, "AuthGSSAPIKEX", true, conf, CONF_try_gssapi_kex);
  890. gprefs(sesskey, "GSSLibs", "\0",
  891. gsslibkeywords, ngsslibs, conf, CONF_ssh_gsslist);
  892. gppfile(sesskey, "GSSCustom", conf, CONF_ssh_gss_custom);
  893. #endif
  894. {
  895. int storageval = gppi_raw(sesskey, "RemoteQTitleAction", -1);
  896. int confval;
  897. if (!conf_enum_map_from_storage(&conf_enum_remote_qtitle_action,
  898. storageval, &confval)) {
  899. /*
  900. * Fall back to older NoRemoteQTitle format
  901. */
  902. storageval = gppi_raw(sesskey, "NoRemoteQTitle", 1);
  903. /* We deliberately interpret the old setting of "no response" as
  904. * "empty string". This changes the behaviour, but hopefully for
  905. * the better; the user can always recover the old behaviour. */
  906. confval = storageval ? TITLE_EMPTY : TITLE_REAL;
  907. }
  908. conf_set_int(conf, CONF_remote_qtitle_action, confval);
  909. }
  910. #ifdef OSX_META_KEY_CONFIG
  911. gppb(sesskey, "OSXOptionMeta", true, conf, CONF_osx_option_meta);
  912. gppb(sesskey, "OSXCommandMeta", false, conf, CONF_osx_command_meta);
  913. #endif
  914. i = gppi_raw(sesskey, "BellOverloadT", 2*TICKSPERSEC
  915. #ifdef PUTTY_UNIX_PLATFORM_H
  916. *1000
  917. #endif
  918. );
  919. conf_set_int(conf, CONF_bellovl_t, i
  920. #ifdef PUTTY_UNIX_PLATFORM_H
  921. / 1000
  922. #endif
  923. );
  924. i = gppi_raw(sesskey, "BellOverloadS", 5*TICKSPERSEC
  925. #ifdef PUTTY_UNIX_PLATFORM_H
  926. *1000
  927. #endif
  928. );
  929. conf_set_int(conf, CONF_bellovl_s, i
  930. #ifdef PUTTY_UNIX_PLATFORM_H
  931. / 1000
  932. #endif
  933. );
  934. for (i = 0; i < 22; i++) {
  935. static const char *const defaults[] = {
  936. "187,187,187", "255,255,255", "0,0,0", "85,85,85", "0,0,0",
  937. "0,255,0", "0,0,0", "85,85,85", "187,0,0", "255,85,85",
  938. "0,187,0", "85,255,85", "187,187,0", "255,255,85", "0,0,187",
  939. "85,85,255", "187,0,187", "255,85,255", "0,187,187",
  940. "85,255,255", "187,187,187", "255,255,255"
  941. };
  942. char buf[20], *buf2;
  943. int c0, c1, c2;
  944. sprintf(buf, "Colour%d", i);
  945. buf2 = gpps_raw(sesskey, buf, defaults[i]);
  946. if (sscanf(buf2, "%d,%d,%d", &c0, &c1, &c2) == 3) {
  947. conf_set_int_int(conf, CONF_colours, i*3+0, c0);
  948. conf_set_int_int(conf, CONF_colours, i*3+1, c1);
  949. conf_set_int_int(conf, CONF_colours, i*3+2, c2);
  950. }
  951. sfree(buf2);
  952. }
  953. for (i = 0; i < 256; i += 32) {
  954. static const char *const defaults[] = {
  955. "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",
  956. "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",
  957. "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",
  958. "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",
  959. "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",
  960. "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",
  961. "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",
  962. "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"
  963. };
  964. char buf[20], *buf2, *p;
  965. int j;
  966. sprintf(buf, "Wordness%d", i);
  967. buf2 = gpps_raw(sesskey, buf, defaults[i / 32]);
  968. p = buf2;
  969. for (j = i; j < i + 32; j++) {
  970. char *q = p;
  971. while (*p && *p != ',')
  972. p++;
  973. if (*p == ',')
  974. *p++ = '\0';
  975. conf_set_int_int(conf, CONF_wordness, j, atoi(q));
  976. }
  977. sfree(buf2);
  978. }
  979. read_clip_setting(sesskey, "MousePaste", CLIPUI_DEFAULT_MOUSE,
  980. conf, CONF_mousepaste, CONF_mousepaste_custom);
  981. read_clip_setting(sesskey, "CtrlShiftIns", CLIPUI_DEFAULT_INS,
  982. conf, CONF_ctrlshiftins, CONF_ctrlshiftins_custom);
  983. read_clip_setting(sesskey, "CtrlShiftCV", CLIPUI_NONE,
  984. conf, CONF_ctrlshiftcv, CONF_ctrlshiftcv_custom);
  985. /*
  986. * The empty default for LineCodePage will be converted later
  987. * into a plausible default for the locale.
  988. */
  989. gppmap(sesskey, "PortForwardings", conf, CONF_portfwd);
  990. {
  991. int i;
  992. i = gppi_raw(sesskey, "BugHMAC2", 0); conf_set_int(conf, CONF_sshbug_hmac2, 2-i);
  993. if (2-i == AUTO) {
  994. i = gppi_raw(sesskey, "BuggyMAC", 0);
  995. if (i == 1)
  996. conf_set_int(conf, CONF_sshbug_hmac2, FORCE_ON);
  997. }
  998. }
  999. gppmap(sesskey, "SSHManualHostKeys", conf, CONF_ssh_manual_hostkeys);
  1000. } // WINSCP
  1001. }
  1002. bool do_defaults(const char *session, Conf *conf)
  1003. {
  1004. return load_settings(session, conf);
  1005. }
  1006. static int sessioncmp(const void *av, const void *bv)
  1007. {
  1008. const char *a = *(const char *const *) av;
  1009. const char *b = *(const char *const *) bv;
  1010. /*
  1011. * Alphabetical order, except that "Default Settings" is a
  1012. * special case and comes first.
  1013. */
  1014. if (!strcmp(a, "Default Settings"))
  1015. return -1; /* a comes first */
  1016. if (!strcmp(b, "Default Settings"))
  1017. return +1; /* b comes first */
  1018. /*
  1019. * FIXME: perhaps we should ignore the first & in determining
  1020. * sort order.
  1021. */
  1022. return strcmp(a, b); /* otherwise, compare normally */
  1023. }
  1024. bool sesslist_demo_mode = false;
  1025. #ifndef WINSCP
  1026. void get_sesslist(struct sesslist *list, bool allocate)
  1027. {
  1028. int i;
  1029. char *p;
  1030. settings_e *handle;
  1031. if (allocate) {
  1032. strbuf *sb = strbuf_new();
  1033. if (sesslist_demo_mode) {
  1034. put_asciz(sb, "demo-server");
  1035. put_asciz(sb, "demo-server-2");
  1036. } else {
  1037. if ((handle = enum_settings_start()) != NULL) {
  1038. while (enum_settings_next(handle, sb))
  1039. put_byte(sb, '\0');
  1040. enum_settings_finish(handle);
  1041. }
  1042. put_byte(sb, '\0');
  1043. }
  1044. list->buffer = strbuf_to_str(sb);
  1045. /*
  1046. * Now set up the list of sessions. Note that "Default
  1047. * Settings" must always be claimed to exist, even if it
  1048. * doesn't really.
  1049. */
  1050. p = list->buffer;
  1051. list->nsessions = 1; /* "Default Settings" counts as one */
  1052. while (*p) {
  1053. if (strcmp(p, "Default Settings"))
  1054. list->nsessions++;
  1055. while (*p)
  1056. p++;
  1057. p++;
  1058. }
  1059. list->sessions = snewn(list->nsessions + 1, const char *);
  1060. list->sessions[0] = "Default Settings";
  1061. p = list->buffer;
  1062. i = 1;
  1063. while (*p) {
  1064. if (strcmp(p, "Default Settings"))
  1065. list->sessions[i++] = p;
  1066. while (*p)
  1067. p++;
  1068. p++;
  1069. }
  1070. qsort(list->sessions, i, sizeof(const char *), sessioncmp);
  1071. } else {
  1072. sfree(list->buffer);
  1073. sfree(list->sessions);
  1074. list->buffer = NULL;
  1075. list->sessions = NULL;
  1076. }
  1077. }
  1078. #endif