common_p.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. /*
  2. * Supporting routines used in common by all the various components of
  3. * the SSH system.
  4. */
  5. #include <assert.h>
  6. #include <stdlib.h>
  7. #include "putty.h"
  8. #include "mpint.h"
  9. #include "ssh.h"
  10. #include "storage.h"
  11. #include "bpp.h"
  12. #include "ppl.h"
  13. #include "channel.h"
  14. /* ----------------------------------------------------------------------
  15. * Implementation of PacketQueue.
  16. */
  17. static void pq_ensure_unlinked(PacketQueueNode *node)
  18. {
  19. if (node->on_free_queue) {
  20. node->next->prev = node->prev;
  21. node->prev->next = node->next;
  22. } else {
  23. assert(!node->next);
  24. assert(!node->prev);
  25. }
  26. }
  27. void pq_base_push(PacketQueueBase *pqb, PacketQueueNode *node)
  28. {
  29. pq_ensure_unlinked(node);
  30. node->next = &pqb->end;
  31. node->prev = pqb->end.prev;
  32. node->next->prev = node;
  33. node->prev->next = node;
  34. pqb->total_size += node->formal_size;
  35. if (pqb->ic)
  36. queue_idempotent_callback(pqb->ic);
  37. }
  38. void pq_base_push_front(PacketQueueBase *pqb, PacketQueueNode *node)
  39. {
  40. pq_ensure_unlinked(node);
  41. node->prev = &pqb->end;
  42. node->next = pqb->end.next;
  43. node->next->prev = node;
  44. node->prev->next = node;
  45. pqb->total_size += node->formal_size;
  46. if (pqb->ic)
  47. queue_idempotent_callback(pqb->ic);
  48. }
  49. #ifndef WINSCP
  50. static PacketQueueNode pktin_freeq_head = {
  51. &pktin_freeq_head, &pktin_freeq_head, true
  52. };
  53. #endif
  54. /*WINSCP static*/ void pktin_free_queue_callback(void *vctx)
  55. {
  56. struct callback_set * set = (struct callback_set *)vctx;
  57. while (set->pktin_freeq_head->next != set->pktin_freeq_head) {
  58. PacketQueueNode *node = set->pktin_freeq_head->next;
  59. PktIn *pktin = container_of(node, PktIn, qnode);
  60. set->pktin_freeq_head->next = node->next;
  61. sfree(pktin);
  62. }
  63. set->pktin_freeq_head->prev = set->pktin_freeq_head;
  64. }
  65. #ifndef WINSCP
  66. static IdempotentCallback ic_pktin_free = {
  67. pktin_free_queue_callback, NULL, false
  68. };
  69. #endif
  70. static inline void pq_unlink_common(PacketQueueBase *pqb,
  71. PacketQueueNode *node)
  72. {
  73. node->next->prev = node->prev;
  74. node->prev->next = node->next;
  75. /* Check total_size doesn't drift out of sync downwards, by
  76. * ensuring it doesn't underflow when we do this subtraction */
  77. assert(pqb->total_size >= node->formal_size);
  78. pqb->total_size -= node->formal_size;
  79. /* Check total_size doesn't drift out of sync upwards, by checking
  80. * that it's returned to exactly zero whenever a queue is
  81. * emptied */
  82. assert(pqb->end.next != &pqb->end || pqb->total_size == 0);
  83. }
  84. static PktIn *pq_in_after(PacketQueueBase *pqb,
  85. PacketQueueNode *prev, bool pop)
  86. {
  87. PacketQueueNode *node = prev->next;
  88. if (node == &pqb->end)
  89. return NULL;
  90. if (pop) {
  91. #ifdef WINSCP
  92. struct callback_set * set = get_seat_callback_set(pqb->seat);
  93. assert(set != NULL);
  94. if (set->ic_pktin_free == NULL)
  95. {
  96. set->pktin_freeq_head = snew(PacketQueueNode);
  97. set->pktin_freeq_head->next = set->pktin_freeq_head;
  98. set->pktin_freeq_head->prev = set->pktin_freeq_head;
  99. set->pktin_freeq_head->on_free_queue = TRUE;
  100. set->ic_pktin_free = snew(IdempotentCallback);
  101. set->ic_pktin_free->fn = pktin_free_queue_callback;
  102. set->ic_pktin_free->ctx = set;
  103. set->ic_pktin_free->queued = FALSE;
  104. set->ic_pktin_free->set = set;
  105. }
  106. #endif
  107. pq_unlink_common(pqb, node);
  108. node->prev = set->pktin_freeq_head->prev; // WINSCP
  109. node->next = set->pktin_freeq_head; // WINSCP
  110. node->next->prev = node;
  111. node->prev->next = node;
  112. node->on_free_queue = true;
  113. queue_idempotent_callback(set->ic_pktin_free); // WINSCP
  114. }
  115. return container_of(node, PktIn, qnode);
  116. }
  117. static PktOut *pq_out_after(PacketQueueBase *pqb,
  118. PacketQueueNode *prev, bool pop)
  119. {
  120. PacketQueueNode *node = prev->next;
  121. if (node == &pqb->end)
  122. return NULL;
  123. if (pop) {
  124. pq_unlink_common(pqb, node);
  125. node->prev = node->next = NULL;
  126. }
  127. return container_of(node, PktOut, qnode);
  128. }
  129. void pq_in_init(PktInQueue *pq, Seat * seat) // WINSCP
  130. {
  131. pq->pqb.ic = NULL;
  132. pq->pqb.seat = seat;
  133. pq->pqb.end.next = pq->pqb.end.prev = &pq->pqb.end;
  134. pq->after = pq_in_after;
  135. pq->pqb.total_size = 0;
  136. }
  137. void pq_out_init(PktOutQueue *pq, Seat * seat) // WINSCP
  138. {
  139. pq->pqb.ic = NULL;
  140. pq->pqb.seat = seat;
  141. pq->pqb.end.next = pq->pqb.end.prev = &pq->pqb.end;
  142. pq->after = pq_out_after;
  143. pq->pqb.total_size = 0;
  144. }
  145. void pq_in_clear(PktInQueue *pq)
  146. {
  147. PktIn *pkt;
  148. pq->pqb.ic = NULL;
  149. while ((pkt = pq_pop(pq)) != NULL) {
  150. /* No need to actually free these packets: pq_pop on a
  151. * PktInQueue will automatically move them to the free
  152. * queue. */
  153. }
  154. }
  155. void pq_out_clear(PktOutQueue *pq)
  156. {
  157. PktOut *pkt;
  158. pq->pqb.ic = NULL;
  159. while ((pkt = pq_pop(pq)) != NULL)
  160. ssh_free_pktout(pkt);
  161. }
  162. /*
  163. * Concatenate the contents of the two queues q1 and q2, and leave the
  164. * result in qdest. qdest must be either empty, or one of the input
  165. * queues.
  166. */
  167. void pq_base_concatenate(PacketQueueBase *qdest,
  168. PacketQueueBase *q1, PacketQueueBase *q2)
  169. {
  170. struct PacketQueueNode *head1, *tail1, *head2, *tail2;
  171. size_t total_size = q1->total_size + q2->total_size;
  172. /*
  173. * Extract the contents from both input queues, and empty them.
  174. */
  175. head1 = (q1->end.next == &q1->end ? NULL : q1->end.next);
  176. tail1 = (q1->end.prev == &q1->end ? NULL : q1->end.prev);
  177. head2 = (q2->end.next == &q2->end ? NULL : q2->end.next);
  178. tail2 = (q2->end.prev == &q2->end ? NULL : q2->end.prev);
  179. q1->end.next = q1->end.prev = &q1->end;
  180. q2->end.next = q2->end.prev = &q2->end;
  181. q1->total_size = q2->total_size = 0;
  182. /*
  183. * Link the two lists together, handling the case where one or
  184. * both is empty.
  185. */
  186. if (tail1)
  187. tail1->next = head2;
  188. else
  189. head1 = head2;
  190. if (head2)
  191. head2->prev = tail1;
  192. else
  193. tail2 = tail1;
  194. /*
  195. * Check the destination queue is currently empty. (If it was one
  196. * of the input queues, then it will be, because we emptied both
  197. * of those just a moment ago.)
  198. */
  199. assert(qdest->end.next == &qdest->end);
  200. assert(qdest->end.prev == &qdest->end);
  201. /*
  202. * If our concatenated list has anything in it, then put it in
  203. * dest.
  204. */
  205. if (!head1) {
  206. assert(!tail2);
  207. } else {
  208. assert(tail2);
  209. qdest->end.next = head1;
  210. qdest->end.prev = tail2;
  211. head1->prev = &qdest->end;
  212. tail2->next = &qdest->end;
  213. if (qdest->ic)
  214. queue_idempotent_callback(qdest->ic);
  215. }
  216. qdest->total_size = total_size;
  217. }
  218. /* ----------------------------------------------------------------------
  219. * Low-level functions for the packet structures themselves.
  220. */
  221. static void ssh_pkt_BinarySink_write(BinarySink *bs,
  222. const void *data, size_t len);
  223. PktOut *ssh_new_packet(void)
  224. {
  225. PktOut *pkt = snew(PktOut);
  226. BinarySink_INIT(pkt, ssh_pkt_BinarySink_write);
  227. pkt->data = NULL;
  228. pkt->length = 0;
  229. pkt->maxlen = 0;
  230. pkt->downstream_id = 0;
  231. pkt->additional_log_text = NULL;
  232. pkt->qnode.next = pkt->qnode.prev = NULL;
  233. pkt->qnode.on_free_queue = false;
  234. return pkt;
  235. }
  236. static void ssh_pkt_adddata(PktOut *pkt, const void *data, int len)
  237. {
  238. sgrowarrayn_nm(pkt->data, pkt->maxlen, pkt->length, len);
  239. memcpy(pkt->data + pkt->length, data, len);
  240. pkt->length += len;
  241. pkt->qnode.formal_size = pkt->length;
  242. }
  243. static void ssh_pkt_BinarySink_write(BinarySink *bs,
  244. const void *data, size_t len)
  245. {
  246. PktOut *pkt = BinarySink_DOWNCAST(bs, PktOut);
  247. ssh_pkt_adddata(pkt, data, len);
  248. }
  249. void ssh_free_pktout(PktOut *pkt)
  250. {
  251. sfree(pkt->data);
  252. sfree(pkt);
  253. }
  254. /* ----------------------------------------------------------------------
  255. * Implement zombiechan_new() and its trivial vtable.
  256. */
  257. static void zombiechan_free(Channel *chan);
  258. static size_t zombiechan_send(
  259. Channel *chan, bool is_stderr, const void *, size_t);
  260. static void zombiechan_set_input_wanted(Channel *chan, bool wanted);
  261. static void zombiechan_do_nothing(Channel *chan);
  262. static void zombiechan_open_failure(Channel *chan, const char *);
  263. static bool zombiechan_want_close(Channel *chan, bool sent_eof, bool rcvd_eof);
  264. static char *zombiechan_log_close_msg(Channel *chan) { return NULL; }
  265. static const ChannelVtable zombiechan_channelvt = {
  266. /*.free =*/ zombiechan_free,
  267. /*.open_confirmation =*/ zombiechan_do_nothing,
  268. /*.open_failed =*/ zombiechan_open_failure,
  269. /*.send =*/ zombiechan_send,
  270. /*.send_eof =*/ zombiechan_do_nothing,
  271. /*.set_input_wanted =*/ zombiechan_set_input_wanted,
  272. /*.log_close_msg =*/ zombiechan_log_close_msg,
  273. /*.want_close =*/ zombiechan_want_close,
  274. /*.rcvd_exit_status =*/ chan_no_exit_status,
  275. /*.rcvd_exit_signal =*/ chan_no_exit_signal,
  276. /*.rcvd_exit_signal_numeric =*/ chan_no_exit_signal_numeric,
  277. /*.run_shell =*/ chan_no_run_shell,
  278. /*.run_command =*/ chan_no_run_command,
  279. /*.run_subsystem =*/ chan_no_run_subsystem,
  280. /*.enable_x11_forwarding =*/ chan_no_enable_x11_forwarding,
  281. /*.enable_agent_forwarding =*/ chan_no_enable_agent_forwarding,
  282. /*.allocate_pty =*/ chan_no_allocate_pty,
  283. /*.set_env =*/ chan_no_set_env,
  284. /*.send_break =*/ chan_no_send_break,
  285. /*.send_signal =*/ chan_no_send_signal,
  286. /*.change_window_size =*/ chan_no_change_window_size,
  287. /*.request_response =*/ chan_no_request_response,
  288. };
  289. Channel *zombiechan_new(void)
  290. {
  291. Channel *chan = snew(Channel);
  292. chan->vt = &zombiechan_channelvt;
  293. chan->initial_fixed_window_size = 0;
  294. return chan;
  295. }
  296. static void zombiechan_free(Channel *chan)
  297. {
  298. assert(chan->vt == &zombiechan_channelvt);
  299. sfree(chan);
  300. }
  301. static void zombiechan_do_nothing(Channel *chan)
  302. {
  303. assert(chan->vt == &zombiechan_channelvt);
  304. }
  305. static void zombiechan_open_failure(Channel *chan, const char *errtext)
  306. {
  307. assert(chan->vt == &zombiechan_channelvt);
  308. }
  309. static size_t zombiechan_send(Channel *chan, bool is_stderr,
  310. const void *data, size_t length)
  311. {
  312. assert(chan->vt == &zombiechan_channelvt);
  313. return 0;
  314. }
  315. static void zombiechan_set_input_wanted(Channel *chan, bool enable)
  316. {
  317. assert(chan->vt == &zombiechan_channelvt);
  318. }
  319. static bool zombiechan_want_close(Channel *chan, bool sent_eof, bool rcvd_eof)
  320. {
  321. return true;
  322. }
  323. /* ----------------------------------------------------------------------
  324. * Common routines for handling SSH tty modes.
  325. */
  326. static unsigned real_ttymode_opcode(unsigned our_opcode, int ssh_version)
  327. {
  328. switch (our_opcode) {
  329. case TTYMODE_ISPEED:
  330. return ssh_version == 1 ? TTYMODE_ISPEED_SSH1 : TTYMODE_ISPEED_SSH2;
  331. case TTYMODE_OSPEED:
  332. return ssh_version == 1 ? TTYMODE_OSPEED_SSH1 : TTYMODE_OSPEED_SSH2;
  333. default:
  334. return our_opcode;
  335. }
  336. }
  337. static unsigned our_ttymode_opcode(unsigned real_opcode, int ssh_version)
  338. {
  339. if (ssh_version == 1) {
  340. switch (real_opcode) {
  341. case TTYMODE_ISPEED_SSH1:
  342. return TTYMODE_ISPEED;
  343. case TTYMODE_OSPEED_SSH1:
  344. return TTYMODE_OSPEED;
  345. default:
  346. return real_opcode;
  347. }
  348. } else {
  349. switch (real_opcode) {
  350. case TTYMODE_ISPEED_SSH2:
  351. return TTYMODE_ISPEED;
  352. case TTYMODE_OSPEED_SSH2:
  353. return TTYMODE_OSPEED;
  354. default:
  355. return real_opcode;
  356. }
  357. }
  358. }
  359. struct ssh_ttymodes get_ttymodes_from_conf(Seat *seat, Conf *conf)
  360. {
  361. struct ssh_ttymodes modes;
  362. size_t i;
  363. static const struct mode_name_type {
  364. const char *mode;
  365. int opcode;
  366. enum { TYPE_CHAR, TYPE_BOOL } type;
  367. } modes_names_types[] = {
  368. #define TTYMODE_CHAR(name, val, index) { #name, val, TYPE_CHAR },
  369. #define TTYMODE_FLAG(name, val, field, mask) { #name, val, TYPE_BOOL },
  370. #include "ttymode-list.h"
  371. #undef TTYMODE_CHAR
  372. #undef TTYMODE_FLAG
  373. };
  374. memset(&modes, 0, sizeof(modes));
  375. for (i = 0; i < lenof(modes_names_types); i++) {
  376. const struct mode_name_type *mode = &modes_names_types[i];
  377. const char *sval = conf_get_str_str(conf, CONF_ttymodes, mode->mode);
  378. char *to_free = NULL;
  379. if (!sval)
  380. sval = "N"; /* just in case */
  381. /*
  382. * sval[0] can be
  383. * - 'V', indicating that an explicit value follows it;
  384. * - 'A', indicating that we should pass the value through from
  385. * the local environment via get_ttymode; or
  386. * - 'N', indicating that we should explicitly not send this
  387. * mode.
  388. */
  389. if (sval[0] == 'A') {
  390. sval = to_free = seat_get_ttymode(seat, mode->mode);
  391. } else if (sval[0] == 'V') {
  392. sval++; /* skip the 'V' */
  393. } else {
  394. /* else 'N', or something from the future we don't understand */
  395. continue;
  396. }
  397. if (sval) {
  398. /*
  399. * Parse the string representation of the tty mode
  400. * into the integer value it will take on the wire.
  401. */
  402. unsigned ival = 0;
  403. switch (mode->type) {
  404. case TYPE_CHAR:
  405. if (*sval) {
  406. char *next = NULL;
  407. /* We know ctrlparse won't write to the string, so
  408. * casting away const is ugly but allowable. */
  409. ival = ctrlparse((char *)sval, &next);
  410. if (!next)
  411. ival = sval[0];
  412. } else {
  413. ival = 255; /* special value meaning "don't set" */
  414. }
  415. break;
  416. case TYPE_BOOL:
  417. if (stricmp(sval, "yes") == 0 ||
  418. stricmp(sval, "on") == 0 ||
  419. stricmp(sval, "true") == 0 ||
  420. stricmp(sval, "+") == 0)
  421. ival = 1; /* true */
  422. else if (stricmp(sval, "no") == 0 ||
  423. stricmp(sval, "off") == 0 ||
  424. stricmp(sval, "false") == 0 ||
  425. stricmp(sval, "-") == 0)
  426. ival = 0; /* false */
  427. else
  428. ival = (atoi(sval) != 0);
  429. break;
  430. default:
  431. unreachable("Bad mode->type");
  432. }
  433. modes.have_mode[mode->opcode] = true;
  434. modes.mode_val[mode->opcode] = ival;
  435. }
  436. sfree(to_free);
  437. }
  438. {
  439. unsigned ospeed, ispeed;
  440. /* Unpick the terminal-speed config string. */
  441. ospeed = ispeed = 38400; /* last-resort defaults */
  442. sscanf(conf_get_str(conf, CONF_termspeed), "%u,%u", &ospeed, &ispeed);
  443. /* Currently we unconditionally set these */
  444. modes.have_mode[TTYMODE_ISPEED] = true;
  445. modes.mode_val[TTYMODE_ISPEED] = ispeed;
  446. modes.have_mode[TTYMODE_OSPEED] = true;
  447. modes.mode_val[TTYMODE_OSPEED] = ospeed;
  448. }
  449. return modes;
  450. }
  451. struct ssh_ttymodes read_ttymodes_from_packet(
  452. BinarySource *bs, int ssh_version)
  453. {
  454. struct ssh_ttymodes modes;
  455. memset(&modes, 0, sizeof(modes));
  456. while (1) {
  457. unsigned real_opcode, our_opcode;
  458. real_opcode = get_byte(bs);
  459. if (real_opcode == TTYMODE_END_OF_LIST)
  460. break;
  461. if (real_opcode >= 160) {
  462. /*
  463. * RFC 4254 (and the SSH 1.5 spec): "Opcodes 160 to 255
  464. * are not yet defined, and cause parsing to stop (they
  465. * should only be used after any other data)."
  466. *
  467. * My interpretation of this is that if one of these
  468. * opcodes appears, it's not a parse _error_, but it is
  469. * something that we don't know how to parse even well
  470. * enough to step over it to find the next opcode, so we
  471. * stop parsing now and assume that the rest of the string
  472. * is composed entirely of things we don't understand and
  473. * (as usual for unsupported terminal modes) silently
  474. * ignore.
  475. */
  476. return modes;
  477. }
  478. our_opcode = our_ttymode_opcode(real_opcode, ssh_version);
  479. assert(our_opcode < TTYMODE_LIMIT);
  480. modes.have_mode[our_opcode] = true;
  481. if (ssh_version == 1 && real_opcode >= 1 && real_opcode <= 127)
  482. modes.mode_val[our_opcode] = get_byte(bs);
  483. else
  484. modes.mode_val[our_opcode] = get_uint32(bs);
  485. }
  486. return modes;
  487. }
  488. void write_ttymodes_to_packet(BinarySink *bs, int ssh_version,
  489. struct ssh_ttymodes modes)
  490. {
  491. unsigned i;
  492. for (i = 0; i < TTYMODE_LIMIT; i++) {
  493. if (modes.have_mode[i]) {
  494. unsigned val = modes.mode_val[i];
  495. unsigned opcode = real_ttymode_opcode(i, ssh_version);
  496. put_byte(bs, opcode);
  497. if (ssh_version == 1 && opcode >= 1 && opcode <= 127)
  498. put_byte(bs, val);
  499. else
  500. put_uint32(bs, val);
  501. }
  502. }
  503. put_byte(bs, TTYMODE_END_OF_LIST);
  504. }
  505. /* ----------------------------------------------------------------------
  506. * Routine for allocating a new channel ID, given a means of finding
  507. * the index field in a given channel structure.
  508. */
  509. unsigned alloc_channel_id_general(tree234 *channels, size_t localid_offset)
  510. {
  511. const unsigned CHANNEL_NUMBER_OFFSET = 256;
  512. search234_state ss;
  513. /*
  514. * First-fit allocation of channel numbers: we always pick the
  515. * lowest unused one.
  516. *
  517. * Every channel before that, and no channel after it, has an ID
  518. * exactly equal to its tree index plus CHANNEL_NUMBER_OFFSET. So
  519. * we can use the search234 system to identify the length of that
  520. * initial sequence, in a single log-time pass down the channels
  521. * tree.
  522. */
  523. search234_start(&ss, channels);
  524. while (ss.element) {
  525. unsigned localid = *(unsigned *)((char *)ss.element + localid_offset);
  526. if (localid == ss.index + CHANNEL_NUMBER_OFFSET)
  527. search234_step(&ss, +1);
  528. else
  529. search234_step(&ss, -1);
  530. }
  531. /*
  532. * Now ss.index gives exactly the number of channels in that
  533. * initial sequence. So adding CHANNEL_NUMBER_OFFSET to it must
  534. * give precisely the lowest unused channel number.
  535. */
  536. return ss.index + CHANNEL_NUMBER_OFFSET;
  537. }
  538. /* ----------------------------------------------------------------------
  539. * Functions for handling the comma-separated strings used to store
  540. * lists of protocol identifiers in SSH-2.
  541. */
  542. void add_to_commasep_pl(strbuf *buf, ptrlen data)
  543. {
  544. if (buf->len > 0)
  545. put_byte(buf, ',');
  546. put_datapl(buf, data);
  547. }
  548. void add_to_commasep(strbuf *buf, const char *data)
  549. {
  550. add_to_commasep_pl(buf, ptrlen_from_asciz(data));
  551. }
  552. bool get_commasep_word(ptrlen *list, ptrlen *word)
  553. {
  554. const char *comma;
  555. /*
  556. * Discard empty list elements, should there be any, because we
  557. * never want to return one as if it was a real string. (This
  558. * introduces a mild tolerance of badly formatted data in lists we
  559. * receive, but I think that's acceptable.)
  560. */
  561. while (list->len > 0 && *(const char *)list->ptr == ',') {
  562. list->ptr = (const char *)list->ptr + 1;
  563. list->len--;
  564. }
  565. if (!list->len)
  566. return false;
  567. comma = memchr(list->ptr, ',', list->len);
  568. if (!comma) {
  569. *word = *list;
  570. list->len = 0;
  571. } else {
  572. size_t wordlen = comma - (const char *)list->ptr;
  573. word->ptr = list->ptr;
  574. word->len = wordlen;
  575. list->ptr = (const char *)list->ptr + wordlen + 1;
  576. list->len -= wordlen + 1;
  577. }
  578. return true;
  579. }
  580. /* ----------------------------------------------------------------------
  581. * Functions for translating SSH packet type codes into their symbolic
  582. * string names.
  583. */
  584. #define TRANSLATE_UNIVERSAL(y, name, value) \
  585. if (type == value) return #name;
  586. #define TRANSLATE_KEX(y, name, value, ctx) \
  587. if (type == value && pkt_kctx == ctx) return #name;
  588. #define TRANSLATE_AUTH(y, name, value, ctx) \
  589. if (type == value && pkt_actx == ctx) return #name;
  590. const char *ssh1_pkt_type(int type)
  591. {
  592. SSH1_MESSAGE_TYPES(TRANSLATE_UNIVERSAL, y);
  593. return "unknown";
  594. }
  595. const char *ssh2_pkt_type(Pkt_KCtx pkt_kctx, Pkt_ACtx pkt_actx, int type)
  596. {
  597. SSH2_MESSAGE_TYPES(TRANSLATE_UNIVERSAL, TRANSLATE_KEX, TRANSLATE_AUTH, y);
  598. return "unknown";
  599. }
  600. #undef TRANSLATE_UNIVERSAL
  601. #undef TRANSLATE_KEX
  602. #undef TRANSLATE_AUTH
  603. /* ----------------------------------------------------------------------
  604. * Common helper function for clients and implementations of
  605. * PacketProtocolLayer.
  606. */
  607. void ssh_ppl_replace(PacketProtocolLayer *old, PacketProtocolLayer *new)
  608. {
  609. new->bpp = old->bpp;
  610. ssh_ppl_setup_queues(new, old->in_pq, old->out_pq);
  611. new->selfptr = old->selfptr;
  612. new->seat = old->seat;
  613. new->ssh = old->ssh;
  614. *new->selfptr = new;
  615. ssh_ppl_free(old);
  616. /* The new layer might need to be the first one that sends a
  617. * packet, so trigger a call to its main coroutine immediately. If
  618. * it doesn't need to go first, the worst that will do is return
  619. * straight away. */
  620. queue_idempotent_callback(&new->ic_process_queue);
  621. }
  622. void ssh_ppl_free(PacketProtocolLayer *ppl)
  623. {
  624. delete_callbacks_for_context(get_seat_callback_set(ppl->seat), ppl); // WINSCP
  625. ppl->vt->free(ppl);
  626. }
  627. static void ssh_ppl_ic_process_queue_callback(void *context)
  628. {
  629. PacketProtocolLayer *ppl = (PacketProtocolLayer *)context;
  630. ssh_ppl_process_queue(ppl);
  631. }
  632. void ssh_ppl_setup_queues(PacketProtocolLayer *ppl,
  633. PktInQueue *inq, PktOutQueue *outq)
  634. {
  635. ppl->in_pq = inq;
  636. ppl->out_pq = outq;
  637. ppl->in_pq->pqb.ic = &ppl->ic_process_queue;
  638. ppl->ic_process_queue.fn = ssh_ppl_ic_process_queue_callback;
  639. ppl->ic_process_queue.ctx = ppl;
  640. ppl->ic_process_queue.set = get_seat_callback_set(ppl->seat);
  641. /* If there's already something on the input queue, it will want
  642. * handling immediately. */
  643. if (pq_peek(ppl->in_pq))
  644. queue_idempotent_callback(&ppl->ic_process_queue);
  645. }
  646. void ssh_ppl_user_output_string_and_free(PacketProtocolLayer *ppl, char *text)
  647. {
  648. /* Messages sent via this function are from the SSH layer, not
  649. * from the server-side process, so they always have the stderr
  650. * flag set. */
  651. SeatOutputType stderrflag = (SeatOutputType)-1; // WINSCP
  652. seat_output(ppl->seat, stderrflag, text, strlen(text)); // WINSCP
  653. sfree(text);
  654. }
  655. size_t ssh_ppl_default_queued_data_size(PacketProtocolLayer *ppl)
  656. {
  657. return ppl->out_pq->pqb.total_size;
  658. }
  659. void ssh_ppl_default_final_output(PacketProtocolLayer *ppl)
  660. {
  661. }
  662. static void ssh_ppl_prompts_callback(void *ctx)
  663. {
  664. ssh_ppl_process_queue((PacketProtocolLayer *)ctx);
  665. }
  666. prompts_t *ssh_ppl_new_prompts(PacketProtocolLayer *ppl)
  667. {
  668. prompts_t *p = new_prompts();
  669. p->callback = ssh_ppl_prompts_callback;
  670. p->callback_ctx = ppl;
  671. return p;
  672. }
  673. /* ----------------------------------------------------------------------
  674. * Common helper functions for clients and implementations of
  675. * BinaryPacketProtocol.
  676. */
  677. static void ssh_bpp_input_raw_data_callback(void *context)
  678. {
  679. BinaryPacketProtocol *bpp = (BinaryPacketProtocol *)context;
  680. Ssh *ssh = bpp->ssh; /* in case bpp is about to get freed */
  681. ssh_bpp_handle_input(bpp);
  682. /* If we've now cleared enough backlog on the input connection, we
  683. * may need to unfreeze it. */
  684. ssh_conn_processed_data(ssh);
  685. }
  686. static void ssh_bpp_output_packet_callback(void *context)
  687. {
  688. BinaryPacketProtocol *bpp = (BinaryPacketProtocol *)context;
  689. ssh_bpp_handle_output(bpp);
  690. }
  691. void ssh_bpp_common_setup(BinaryPacketProtocol *bpp)
  692. {
  693. pq_in_init(&bpp->in_pq, get_log_seat(bpp->logctx)); // WINSCP
  694. pq_out_init(&bpp->out_pq, get_log_seat(bpp->logctx)); // WINSCP
  695. bpp->input_eof = false;
  696. bpp->ic_in_raw.fn = ssh_bpp_input_raw_data_callback;
  697. bpp->ic_in_raw.set = get_log_callback_set(bpp->logctx);
  698. bpp->ic_in_raw.ctx = bpp;
  699. bpp->ic_out_pq.fn = ssh_bpp_output_packet_callback;
  700. bpp->ic_out_pq.set = get_log_callback_set(bpp->logctx);
  701. bpp->ic_out_pq.ctx = bpp;
  702. bpp->out_pq.pqb.ic = &bpp->ic_out_pq;
  703. }
  704. void ssh_bpp_free(BinaryPacketProtocol *bpp)
  705. {
  706. // WINSCP
  707. delete_callbacks_for_context(get_log_callback_set(bpp->logctx), bpp);
  708. bpp->vt->free(bpp);
  709. }
  710. void ssh2_bpp_queue_disconnect(BinaryPacketProtocol *bpp,
  711. const char *msg, int category)
  712. {
  713. PktOut *pkt = ssh_bpp_new_pktout(bpp, SSH2_MSG_DISCONNECT);
  714. put_uint32(pkt, category);
  715. put_stringz(pkt, msg);
  716. put_stringz(pkt, "en"); /* language tag */
  717. pq_push(&bpp->out_pq, pkt);
  718. }
  719. #define BITMAP_UNIVERSAL(y, name, value) \
  720. | (value >= y && value < y+32 \
  721. ? 1UL << (value >= y && value < y+32 ? (value-y) : 0) \
  722. : 0)
  723. #define BITMAP_CONDITIONAL(y, name, value, ctx) \
  724. BITMAP_UNIVERSAL(y, name, value)
  725. #define SSH2_BITMAP_WORD(y) \
  726. (0 SSH2_MESSAGE_TYPES(BITMAP_UNIVERSAL, BITMAP_CONDITIONAL, \
  727. BITMAP_CONDITIONAL, (32*y)))
  728. bool ssh2_bpp_check_unimplemented(BinaryPacketProtocol *bpp, PktIn *pktin)
  729. {
  730. #pragma warn -osh
  731. static const unsigned valid_bitmap[] = {
  732. SSH2_BITMAP_WORD(0),
  733. SSH2_BITMAP_WORD(1),
  734. SSH2_BITMAP_WORD(2),
  735. SSH2_BITMAP_WORD(3),
  736. SSH2_BITMAP_WORD(4),
  737. SSH2_BITMAP_WORD(5),
  738. SSH2_BITMAP_WORD(6),
  739. SSH2_BITMAP_WORD(7),
  740. };
  741. #pragma warn +osh
  742. if (pktin->type < 0x100 &&
  743. !((valid_bitmap[pktin->type >> 5] >> (pktin->type & 0x1F)) & 1)) {
  744. PktOut *pkt = ssh_bpp_new_pktout(bpp, SSH2_MSG_UNIMPLEMENTED);
  745. put_uint32(pkt, pktin->sequence);
  746. pq_push(&bpp->out_pq, pkt);
  747. return true;
  748. }
  749. return false;
  750. }
  751. #undef BITMAP_UNIVERSAL
  752. #undef BITMAP_CONDITIONAL
  753. #undef SSH2_BITMAP_WORD
  754. /* ----------------------------------------------------------------------
  755. * Centralised component of SSH host key verification.
  756. *
  757. * verify_ssh_host_key is called from both the SSH-1 and SSH-2
  758. * transport layers, and does the initial work of checking whether the
  759. * host key is already known. If so, it returns success on its own
  760. * account; otherwise, it calls out to the Seat to give an interactive
  761. * prompt (the nature of which varies depending on the Seat itself).
  762. */
  763. SeatPromptResult verify_ssh_host_key(
  764. InteractionReadySeat iseat, Conf *conf, const char *host, int port,
  765. ssh_key *key, const char *keytype, char *keystr, const char *keydisp,
  766. char **fingerprints, int ca_count,
  767. void (*callback)(void *ctx, SeatPromptResult result), void *ctx)
  768. {
  769. /*
  770. * First, check if the Conf includes a manual specification of the
  771. * expected host key. If so, that completely supersedes everything
  772. * else, including the normal host key cache _and_ including
  773. * manual overrides: we return success or failure immediately,
  774. * entirely based on whether the key matches the Conf.
  775. */
  776. if (conf_get_str_nthstrkey(conf, CONF_ssh_manual_hostkeys, 0)) {
  777. if (fingerprints) {
  778. size_t i; // WINSCP
  779. for (i = 0; i < SSH_N_FPTYPES; i++) {
  780. /*
  781. * Each fingerprint string we've been given will have
  782. * things like 'ssh-rsa 2048' at the front of it. Strip
  783. * those off and narrow down to just the hash at the end
  784. * of the string.
  785. */
  786. const char *fingerprint = fingerprints[i];
  787. if (!fingerprint)
  788. continue;
  789. { // WINSCP
  790. const char *p = strrchr(fingerprint, ' ');
  791. fingerprint = p ? p+1 : fingerprint;
  792. if (conf_get_str_str_opt(conf, CONF_ssh_manual_hostkeys,
  793. fingerprint))
  794. return SPR_OK;
  795. } // WINSCP
  796. }
  797. }
  798. if (key) {
  799. /*
  800. * Construct the base64-encoded public key blob and see if
  801. * that's listed.
  802. */
  803. strbuf *binblob;
  804. char *base64blob;
  805. int atoms, i;
  806. binblob = strbuf_new();
  807. ssh_key_public_blob(key, BinarySink_UPCAST(binblob));
  808. atoms = (binblob->len + 2) / 3;
  809. base64blob = snewn(atoms * 4 + 1, char);
  810. for (i = 0; i < atoms; i++)
  811. base64_encode_atom(binblob->u + 3*i,
  812. binblob->len - 3*i, base64blob + 4*i);
  813. base64blob[atoms * 4] = '\0';
  814. strbuf_free(binblob);
  815. if (conf_get_str_str_opt(conf, CONF_ssh_manual_hostkeys,
  816. base64blob)) {
  817. sfree(base64blob);
  818. return SPR_OK;
  819. }
  820. sfree(base64blob);
  821. }
  822. return SPR_SW_ABORT("Host key not in manually configured list");
  823. }
  824. /*
  825. * Next, check the host key cache.
  826. */
  827. { // WINSCP
  828. int storage_status = 1; // WINSCP check_stored_host_key(host, port, keytype, keystr);
  829. if (storage_status == 0) /* matching key was found in the cache */
  830. return SPR_OK;
  831. /*
  832. * The key is either missing from the cache, or does not match.
  833. * Either way, fall back to an interactive prompt from the Seat.
  834. */
  835. { // WINSCP
  836. SeatDialogText *text = seat_dialog_text_new();
  837. const SeatDialogPromptDescriptions *pds =
  838. seat_prompt_descriptions(iseat.seat);
  839. FingerprintType fptype_default =
  840. ssh2_pick_default_fingerprint(fingerprints);
  841. seat_dialog_text_append(
  842. text, SDT_TITLE, "%s Security Alert", appname);
  843. { // WINSCP
  844. HelpCtx helpctx;
  845. if (key && ssh_key_alg(key)->is_certificate) {
  846. seat_dialog_text_append(
  847. text, SDT_SCARY_HEADING, "WARNING - POTENTIAL SECURITY BREACH!");
  848. seat_dialog_text_append(
  849. text, SDT_PARA, "This server presented a certified host key:");
  850. seat_dialog_text_append(
  851. text, SDT_DISPLAY, "%s (port %d)", host, port);
  852. if (ca_count) {
  853. seat_dialog_text_append(
  854. text, SDT_PARA, "which was signed by a different "
  855. "certification authority from the %s %s is configured to "
  856. "trust for this server.", ca_count > 1 ? "ones" : "one",
  857. appname);
  858. if (storage_status == 2) {
  859. seat_dialog_text_append(
  860. text, SDT_PARA, "ALSO, that key does not match the key "
  861. "%s had previously cached for this server.", appname);
  862. seat_dialog_text_append(
  863. text, SDT_PARA, "This means that either another "
  864. "certification authority is operating in this realm AND "
  865. "the server administrator has changed the host key, or "
  866. "you have actually connected to another computer "
  867. "pretending to be the server.");
  868. } else {
  869. seat_dialog_text_append(
  870. text, SDT_PARA, "This means that either another "
  871. "certification authority is operating in this realm, or "
  872. "you have actually connected to another computer "
  873. "pretending to be the server.");
  874. }
  875. } else {
  876. assert(storage_status == 2);
  877. seat_dialog_text_append(
  878. text, SDT_PARA, "which does not match the certified key %s "
  879. "had previously cached for this server.", appname);
  880. seat_dialog_text_append(
  881. text, SDT_PARA, "This means that either the server "
  882. "administrator has changed the host key, or you have actually "
  883. "connected to another computer pretending to be the server.");
  884. }
  885. seat_dialog_text_append(
  886. text, SDT_PARA, "The new %s key fingerprint is:", keytype);
  887. seat_dialog_text_append(
  888. text, SDT_DISPLAY, "%s", fingerprints[fptype_default]);
  889. helpctx = HELPCTX(errors_cert_mismatch);
  890. } else if (storage_status == 1) {
  891. seat_dialog_text_append(
  892. text, SDT_PARA, "The host key is not cached for this server:");
  893. seat_dialog_text_append(
  894. text, SDT_DISPLAY, "%s (port %d)", host, port);
  895. seat_dialog_text_append(
  896. text, SDT_PARA, "You have no guarantee that the server is the "
  897. "computer you think it is.");
  898. seat_dialog_text_append(
  899. text, SDT_PARA, "The server's %s key fingerprint is:", keytype);
  900. seat_dialog_text_append(
  901. text, SDT_DISPLAY, "%s", fingerprints[fptype_default]);
  902. helpctx = HELPCTX(errors_hostkey_absent);
  903. } else {
  904. seat_dialog_text_append(
  905. text, SDT_SCARY_HEADING, "WARNING - POTENTIAL SECURITY BREACH!");
  906. seat_dialog_text_append(
  907. text, SDT_PARA, "The host key does not match the one %s has "
  908. "cached for this server:", appname);
  909. seat_dialog_text_append(
  910. text, SDT_DISPLAY, "%s (port %d)", host, port);
  911. seat_dialog_text_append(
  912. text, SDT_PARA, "This means that either the server administrator "
  913. "has changed the host key, or you have actually connected to "
  914. "another computer pretending to be the server.");
  915. seat_dialog_text_append(
  916. text, SDT_PARA, "The new %s key fingerprint is:", keytype);
  917. seat_dialog_text_append(
  918. text, SDT_DISPLAY, "%s", fingerprints[fptype_default]);
  919. helpctx = HELPCTX(errors_hostkey_changed);
  920. }
  921. /* The above text is printed even in batch mode. Here's where we stop if
  922. * we can't present interactive prompts. */
  923. seat_dialog_text_append(
  924. text, SDT_BATCH_ABORT, "Connection abandoned.");
  925. if (storage_status == 1) {
  926. seat_dialog_text_append(
  927. text, SDT_PARA, "If you trust this host, %s to add the key to "
  928. "%s's cache and carry on connecting.",
  929. pds->hk_accept_action, appname);
  930. if (key && ssh_key_alg(key)->is_certificate) {
  931. seat_dialog_text_append(
  932. text, SDT_PARA, "(Storing this certified key in the cache "
  933. "will NOT cause its certification authority to be trusted "
  934. "for any other key or host.)");
  935. }
  936. seat_dialog_text_append(
  937. text, SDT_PARA, "If you want to carry on connecting just once, "
  938. "without adding the key to the cache, %s.",
  939. pds->hk_connect_once_action);
  940. seat_dialog_text_append(
  941. text, SDT_PARA, "If you do not trust this host, %s to abandon the "
  942. "connection.", pds->hk_cancel_action);
  943. seat_dialog_text_append(
  944. text, SDT_PROMPT, "Store key in cache?");
  945. } else {
  946. seat_dialog_text_append(
  947. text, SDT_PARA, "If you were expecting this change and trust the "
  948. "new key, %s to update %s's cache and carry on connecting.",
  949. pds->hk_accept_action, appname);
  950. if (key && ssh_key_alg(key)->is_certificate) {
  951. seat_dialog_text_append(
  952. text, SDT_PARA, "(Storing this certified key in the cache "
  953. "will NOT cause its certification authority to be trusted "
  954. "for any other key or host.)");
  955. }
  956. seat_dialog_text_append(
  957. text, SDT_PARA, "If you want to carry on connecting but without "
  958. "updating the cache, %s.", pds->hk_connect_once_action);
  959. seat_dialog_text_append(
  960. text, SDT_PARA, "If you want to abandon the connection "
  961. "completely, %s to cancel. %s is the ONLY guaranteed safe choice.",
  962. pds->hk_cancel_action, pds->hk_cancel_action_Participle);
  963. seat_dialog_text_append(
  964. text, SDT_PROMPT, "Update cached key?");
  965. }
  966. seat_dialog_text_append(text, SDT_MORE_INFO_KEY,
  967. "Full text of host's public key");
  968. seat_dialog_text_append(text, SDT_MORE_INFO_VALUE_BLOB, "%s", keydisp);
  969. if (fingerprints[SSH_FPTYPE_SHA256]) {
  970. seat_dialog_text_append(text, SDT_MORE_INFO_KEY, "SHA256 fingerprint");
  971. seat_dialog_text_append(text, SDT_MORE_INFO_VALUE_SHORT, "%s",
  972. fingerprints[SSH_FPTYPE_SHA256]);
  973. }
  974. if (fingerprints[SSH_FPTYPE_MD5]) {
  975. seat_dialog_text_append(text, SDT_MORE_INFO_KEY, "MD5 fingerprint");
  976. seat_dialog_text_append(text, SDT_MORE_INFO_VALUE_SHORT, "%s",
  977. fingerprints[SSH_FPTYPE_MD5]);
  978. }
  979. { // WINSCP
  980. SeatPromptResult toret = seat_confirm_ssh_host_key(
  981. iseat, host, port, keytype, keystr, text, helpctx, callback, ctx,
  982. fingerprints, key && ssh_key_alg(key)->is_certificate); // WINSCP
  983. seat_dialog_text_free(text);
  984. return toret;
  985. } // WINSCP
  986. } // WINSCP
  987. } // WINSCP
  988. } // WINSCP
  989. }
  990. /* ----------------------------------------------------------------------
  991. * Common functions shared between SSH-1 layers.
  992. */
  993. bool ssh1_common_get_specials(
  994. PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx)
  995. {
  996. /*
  997. * Don't bother offering IGNORE if we've decided the remote
  998. * won't cope with it, since we wouldn't bother sending it if
  999. * asked anyway.
  1000. */
  1001. if (!(ppl->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) {
  1002. add_special(ctx, "IGNORE message", SS_NOP, 0);
  1003. return true;
  1004. }
  1005. return false;
  1006. }
  1007. bool ssh1_common_filter_queue(PacketProtocolLayer *ppl)
  1008. {
  1009. PktIn *pktin;
  1010. ptrlen msg;
  1011. while ((pktin = pq_peek(ppl->in_pq)) != NULL) {
  1012. switch (pktin->type) {
  1013. case SSH1_MSG_DISCONNECT:
  1014. msg = get_string(pktin);
  1015. ssh_remote_error(ppl->ssh,
  1016. "Remote side sent disconnect message:\n\"%.*s\"",
  1017. PTRLEN_PRINTF(msg));
  1018. /* don't try to pop the queue, because we've been freed! */
  1019. return true; /* indicate that we've been freed */
  1020. case SSH1_MSG_DEBUG:
  1021. msg = get_string(pktin);
  1022. ppl_logevent("Remote debug message: %.*s", PTRLEN_PRINTF(msg));
  1023. pq_pop(ppl->in_pq);
  1024. break;
  1025. case SSH1_MSG_IGNORE:
  1026. /* Do nothing, because we're ignoring it! Duhh. */
  1027. pq_pop(ppl->in_pq);
  1028. break;
  1029. default:
  1030. return false;
  1031. }
  1032. }
  1033. return false;
  1034. }
  1035. void ssh1_compute_session_id(
  1036. unsigned char *session_id, const unsigned char *cookie,
  1037. RSAKey *hostkey, RSAKey *servkey)
  1038. {
  1039. ssh_hash *hash = ssh_hash_new(&ssh_md5);
  1040. size_t i; // WINSCP
  1041. for (i = (mp_get_nbits(hostkey->modulus) + 7) / 8; i-- ;)
  1042. put_byte(hash, mp_get_byte(hostkey->modulus, i));
  1043. for (i = (mp_get_nbits(servkey->modulus) + 7) / 8; i-- ;)
  1044. put_byte(hash, mp_get_byte(servkey->modulus, i));
  1045. put_data(hash, cookie, 8);
  1046. ssh_hash_final(hash, session_id);
  1047. }
  1048. /* ----------------------------------------------------------------------
  1049. * Wrapper function to handle the abort-connection modes of a
  1050. * SeatPromptResult without a lot of verbiage at every call site.
  1051. *
  1052. * Can become ssh_sw_abort or ssh_user_close, depending on the kind of
  1053. * negative SeatPromptResult.
  1054. */
  1055. void ssh_spr_close(Ssh *ssh, SeatPromptResult spr, const char *context)
  1056. {
  1057. if (spr.kind == SPRK_USER_ABORT) {
  1058. ssh_user_close(ssh, "User aborted at %s", context);
  1059. } else {
  1060. assert(spr.kind == SPRK_SW_ABORT);
  1061. { // WINSCP
  1062. char *err = spr_get_error_message(spr);
  1063. ssh_sw_abort(ssh, "%s", err);
  1064. sfree(err);
  1065. } // WINSCP
  1066. }
  1067. }