settings.c 43 KB

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