sshcommon.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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 "sshbpp.h"
  11. #include "sshppl.h"
  12. #include "sshchan.h"
  13. /* ----------------------------------------------------------------------
  14. * Implementation of PacketQueue.
  15. */
  16. static void pq_ensure_unlinked(PacketQueueNode *node)
  17. {
  18. if (node->on_free_queue) {
  19. node->next->prev = node->prev;
  20. node->prev->next = node->next;
  21. } else {
  22. assert(!node->next);
  23. assert(!node->prev);
  24. }
  25. }
  26. void pq_base_push(PacketQueueBase *pqb, PacketQueueNode *node)
  27. {
  28. pq_ensure_unlinked(node);
  29. node->next = &pqb->end;
  30. node->prev = pqb->end.prev;
  31. node->next->prev = node;
  32. node->prev->next = node;
  33. if (pqb->ic)
  34. queue_idempotent_callback(pqb->ic);
  35. }
  36. void pq_base_push_front(PacketQueueBase *pqb, PacketQueueNode *node)
  37. {
  38. pq_ensure_unlinked(node);
  39. node->prev = &pqb->end;
  40. node->next = pqb->end.next;
  41. node->next->prev = node;
  42. node->prev->next = node;
  43. if (pqb->ic)
  44. queue_idempotent_callback(pqb->ic);
  45. }
  46. #ifndef WINSCP
  47. static PacketQueueNode pktin_freeq_head = {
  48. &pktin_freeq_head, &pktin_freeq_head, true
  49. };
  50. #endif
  51. /*WINSCP static*/ void pktin_free_queue_callback(void *vctx)
  52. {
  53. struct callback_set * set = (struct callback_set *)vctx;
  54. while (set->pktin_freeq_head->next != set->pktin_freeq_head) {
  55. PacketQueueNode *node = set->pktin_freeq_head->next;
  56. PktIn *pktin = container_of(node, PktIn, qnode);
  57. set->pktin_freeq_head->next = node->next;
  58. sfree(pktin);
  59. }
  60. set->pktin_freeq_head->prev = set->pktin_freeq_head;
  61. }
  62. #ifndef WINSCP
  63. static IdempotentCallback ic_pktin_free = {
  64. pktin_free_queue_callback, NULL, false
  65. };
  66. #endif
  67. static PktIn *pq_in_after(PacketQueueBase *pqb,
  68. PacketQueueNode *prev, bool pop)
  69. {
  70. PacketQueueNode *node = prev->next;
  71. if (node == &pqb->end)
  72. return NULL;
  73. if (pop) {
  74. #ifdef WINSCP
  75. struct callback_set * set = get_seat_callback_set(pqb->seat);
  76. assert(set != NULL);
  77. if (set->ic_pktin_free == NULL)
  78. {
  79. set->pktin_freeq_head = snew(PacketQueueNode);
  80. set->pktin_freeq_head->next = set->pktin_freeq_head;
  81. set->pktin_freeq_head->prev = set->pktin_freeq_head;
  82. set->pktin_freeq_head->on_free_queue = TRUE;
  83. set->ic_pktin_free = snew(IdempotentCallback);
  84. set->ic_pktin_free->fn = pktin_free_queue_callback;
  85. set->ic_pktin_free->ctx = set;
  86. set->ic_pktin_free->queued = FALSE;
  87. set->ic_pktin_free->set = set;
  88. }
  89. #endif
  90. node->next->prev = node->prev;
  91. node->prev->next = node->next;
  92. node->prev = set->pktin_freeq_head->prev; // WINSCP
  93. node->next = set->pktin_freeq_head; // WINSCP
  94. node->next->prev = node;
  95. node->prev->next = node;
  96. node->on_free_queue = true;
  97. queue_idempotent_callback(set->ic_pktin_free); // WINSCP
  98. }
  99. return container_of(node, PktIn, qnode);
  100. }
  101. static PktOut *pq_out_after(PacketQueueBase *pqb,
  102. PacketQueueNode *prev, bool pop)
  103. {
  104. PacketQueueNode *node = prev->next;
  105. if (node == &pqb->end)
  106. return NULL;
  107. if (pop) {
  108. node->next->prev = node->prev;
  109. node->prev->next = node->next;
  110. node->prev = node->next = NULL;
  111. }
  112. return container_of(node, PktOut, qnode);
  113. }
  114. void pq_in_init(PktInQueue *pq, Seat * seat) // WINSCP
  115. {
  116. pq->pqb.ic = NULL;
  117. pq->pqb.seat = seat;
  118. pq->pqb.end.next = pq->pqb.end.prev = &pq->pqb.end;
  119. pq->after = pq_in_after;
  120. }
  121. void pq_out_init(PktOutQueue *pq, Seat * seat) // WINSCP
  122. {
  123. pq->pqb.ic = NULL;
  124. pq->pqb.seat = seat;
  125. pq->pqb.end.next = pq->pqb.end.prev = &pq->pqb.end;
  126. pq->after = pq_out_after;
  127. }
  128. void pq_in_clear(PktInQueue *pq)
  129. {
  130. PktIn *pkt;
  131. pq->pqb.ic = NULL;
  132. while ((pkt = pq_pop(pq)) != NULL) {
  133. /* No need to actually free these packets: pq_pop on a
  134. * PktInQueue will automatically move them to the free
  135. * queue. */
  136. }
  137. }
  138. void pq_out_clear(PktOutQueue *pq)
  139. {
  140. PktOut *pkt;
  141. pq->pqb.ic = NULL;
  142. while ((pkt = pq_pop(pq)) != NULL)
  143. ssh_free_pktout(pkt);
  144. }
  145. /*
  146. * Concatenate the contents of the two queues q1 and q2, and leave the
  147. * result in qdest. qdest must be either empty, or one of the input
  148. * queues.
  149. */
  150. void pq_base_concatenate(PacketQueueBase *qdest,
  151. PacketQueueBase *q1, PacketQueueBase *q2)
  152. {
  153. struct PacketQueueNode *head1, *tail1, *head2, *tail2;
  154. /*
  155. * Extract the contents from both input queues, and empty them.
  156. */
  157. head1 = (q1->end.next == &q1->end ? NULL : q1->end.next);
  158. tail1 = (q1->end.prev == &q1->end ? NULL : q1->end.prev);
  159. head2 = (q2->end.next == &q2->end ? NULL : q2->end.next);
  160. tail2 = (q2->end.prev == &q2->end ? NULL : q2->end.prev);
  161. q1->end.next = q1->end.prev = &q1->end;
  162. q2->end.next = q2->end.prev = &q2->end;
  163. /*
  164. * Link the two lists together, handling the case where one or
  165. * both is empty.
  166. */
  167. if (tail1)
  168. tail1->next = head2;
  169. else
  170. head1 = head2;
  171. if (head2)
  172. head2->prev = tail1;
  173. else
  174. tail2 = tail1;
  175. /*
  176. * Check the destination queue is currently empty. (If it was one
  177. * of the input queues, then it will be, because we emptied both
  178. * of those just a moment ago.)
  179. */
  180. assert(qdest->end.next == &qdest->end);
  181. assert(qdest->end.prev == &qdest->end);
  182. /*
  183. * If our concatenated list has anything in it, then put it in
  184. * dest.
  185. */
  186. if (!head1) {
  187. assert(!tail2);
  188. } else {
  189. assert(tail2);
  190. qdest->end.next = head1;
  191. qdest->end.prev = tail2;
  192. head1->prev = &qdest->end;
  193. tail2->next = &qdest->end;
  194. if (qdest->ic)
  195. queue_idempotent_callback(qdest->ic);
  196. }
  197. }
  198. /* ----------------------------------------------------------------------
  199. * Low-level functions for the packet structures themselves.
  200. */
  201. static void ssh_pkt_BinarySink_write(BinarySink *bs,
  202. const void *data, size_t len);
  203. PktOut *ssh_new_packet(void)
  204. {
  205. PktOut *pkt = snew(PktOut);
  206. BinarySink_INIT(pkt, ssh_pkt_BinarySink_write);
  207. pkt->data = NULL;
  208. pkt->length = 0;
  209. pkt->maxlen = 0;
  210. pkt->downstream_id = 0;
  211. pkt->additional_log_text = NULL;
  212. pkt->qnode.next = pkt->qnode.prev = NULL;
  213. pkt->qnode.on_free_queue = false;
  214. return pkt;
  215. }
  216. static void ssh_pkt_adddata(PktOut *pkt, const void *data, int len)
  217. {
  218. sgrowarrayn_nm(pkt->data, pkt->maxlen, pkt->length, len);
  219. memcpy(pkt->data + pkt->length, data, len);
  220. pkt->length += len;
  221. }
  222. static void ssh_pkt_BinarySink_write(BinarySink *bs,
  223. const void *data, size_t len)
  224. {
  225. PktOut *pkt = BinarySink_DOWNCAST(bs, PktOut);
  226. ssh_pkt_adddata(pkt, data, len);
  227. }
  228. void ssh_free_pktout(PktOut *pkt)
  229. {
  230. sfree(pkt->data);
  231. sfree(pkt);
  232. }
  233. /* ----------------------------------------------------------------------
  234. * Implement zombiechan_new() and its trivial vtable.
  235. */
  236. static void zombiechan_free(Channel *chan);
  237. static size_t zombiechan_send(
  238. Channel *chan, bool is_stderr, const void *, size_t);
  239. static void zombiechan_set_input_wanted(Channel *chan, bool wanted);
  240. static void zombiechan_do_nothing(Channel *chan);
  241. static void zombiechan_open_failure(Channel *chan, const char *);
  242. static bool zombiechan_want_close(Channel *chan, bool sent_eof, bool rcvd_eof);
  243. static char *zombiechan_log_close_msg(Channel *chan) { return NULL; }
  244. static const struct ChannelVtable zombiechan_channelvt = {
  245. zombiechan_free,
  246. zombiechan_do_nothing, /* open_confirmation */
  247. zombiechan_open_failure,
  248. zombiechan_send,
  249. zombiechan_do_nothing, /* send_eof */
  250. zombiechan_set_input_wanted,
  251. zombiechan_log_close_msg,
  252. zombiechan_want_close,
  253. chan_no_exit_status,
  254. chan_no_exit_signal,
  255. chan_no_exit_signal_numeric,
  256. chan_no_run_shell,
  257. chan_no_run_command,
  258. chan_no_run_subsystem,
  259. chan_no_enable_x11_forwarding,
  260. chan_no_enable_agent_forwarding,
  261. chan_no_allocate_pty,
  262. chan_no_set_env,
  263. chan_no_send_break,
  264. chan_no_send_signal,
  265. chan_no_change_window_size,
  266. chan_no_request_response,
  267. };
  268. Channel *zombiechan_new(void)
  269. {
  270. Channel *chan = snew(Channel);
  271. chan->vt = &zombiechan_channelvt;
  272. chan->initial_fixed_window_size = 0;
  273. return chan;
  274. }
  275. static void zombiechan_free(Channel *chan)
  276. {
  277. assert(chan->vt == &zombiechan_channelvt);
  278. sfree(chan);
  279. }
  280. static void zombiechan_do_nothing(Channel *chan)
  281. {
  282. assert(chan->vt == &zombiechan_channelvt);
  283. }
  284. static void zombiechan_open_failure(Channel *chan, const char *errtext)
  285. {
  286. assert(chan->vt == &zombiechan_channelvt);
  287. }
  288. static size_t zombiechan_send(Channel *chan, bool is_stderr,
  289. const void *data, size_t length)
  290. {
  291. assert(chan->vt == &zombiechan_channelvt);
  292. return 0;
  293. }
  294. static void zombiechan_set_input_wanted(Channel *chan, bool enable)
  295. {
  296. assert(chan->vt == &zombiechan_channelvt);
  297. }
  298. static bool zombiechan_want_close(Channel *chan, bool sent_eof, bool rcvd_eof)
  299. {
  300. return true;
  301. }
  302. /* ----------------------------------------------------------------------
  303. * Centralised standard methods for other channel implementations to
  304. * borrow.
  305. */
  306. void chan_remotely_opened_confirmation(Channel *chan)
  307. {
  308. unreachable("this channel type should never receive OPEN_CONFIRMATION");
  309. }
  310. void chan_remotely_opened_failure(Channel *chan, const char *errtext)
  311. {
  312. unreachable("this channel type should never receive OPEN_FAILURE");
  313. }
  314. bool chan_default_want_close(
  315. Channel *chan, bool sent_local_eof, bool rcvd_remote_eof)
  316. {
  317. /*
  318. * Default close policy: we start initiating the CHANNEL_CLOSE
  319. * procedure as soon as both sides of the channel have seen EOF.
  320. */
  321. return sent_local_eof && rcvd_remote_eof;
  322. }
  323. bool chan_no_exit_status(Channel *chan, int status)
  324. {
  325. return false;
  326. }
  327. bool chan_no_exit_signal(
  328. Channel *chan, ptrlen signame, bool core_dumped, ptrlen msg)
  329. {
  330. return false;
  331. }
  332. bool chan_no_exit_signal_numeric(
  333. Channel *chan, int signum, bool core_dumped, ptrlen msg)
  334. {
  335. return false;
  336. }
  337. bool chan_no_run_shell(Channel *chan)
  338. {
  339. return false;
  340. }
  341. bool chan_no_run_command(Channel *chan, ptrlen command)
  342. {
  343. return false;
  344. }
  345. bool chan_no_run_subsystem(Channel *chan, ptrlen subsys)
  346. {
  347. return false;
  348. }
  349. bool chan_no_enable_x11_forwarding(
  350. Channel *chan, bool oneshot, ptrlen authproto, ptrlen authdata,
  351. unsigned screen_number)
  352. {
  353. return false;
  354. }
  355. bool chan_no_enable_agent_forwarding(Channel *chan)
  356. {
  357. return false;
  358. }
  359. bool chan_no_allocate_pty(
  360. Channel *chan, ptrlen termtype, unsigned width, unsigned height,
  361. unsigned pixwidth, unsigned pixheight, struct ssh_ttymodes modes)
  362. {
  363. return false;
  364. }
  365. bool chan_no_set_env(Channel *chan, ptrlen var, ptrlen value)
  366. {
  367. return false;
  368. }
  369. bool chan_no_send_break(Channel *chan, unsigned length)
  370. {
  371. return false;
  372. }
  373. bool chan_no_send_signal(Channel *chan, ptrlen signame)
  374. {
  375. return false;
  376. }
  377. bool chan_no_change_window_size(
  378. Channel *chan, unsigned width, unsigned height,
  379. unsigned pixwidth, unsigned pixheight)
  380. {
  381. return false;
  382. }
  383. void chan_no_request_response(Channel *chan, bool success)
  384. {
  385. unreachable("this channel type should never send a want-reply request");
  386. }
  387. /* ----------------------------------------------------------------------
  388. * Common routines for handling SSH tty modes.
  389. */
  390. static unsigned real_ttymode_opcode(unsigned our_opcode, int ssh_version)
  391. {
  392. switch (our_opcode) {
  393. case TTYMODE_ISPEED:
  394. return ssh_version == 1 ? TTYMODE_ISPEED_SSH1 : TTYMODE_ISPEED_SSH2;
  395. case TTYMODE_OSPEED:
  396. return ssh_version == 1 ? TTYMODE_OSPEED_SSH1 : TTYMODE_OSPEED_SSH2;
  397. default:
  398. return our_opcode;
  399. }
  400. }
  401. static unsigned our_ttymode_opcode(unsigned real_opcode, int ssh_version)
  402. {
  403. if (ssh_version == 1) {
  404. switch (real_opcode) {
  405. case TTYMODE_ISPEED_SSH1:
  406. return TTYMODE_ISPEED;
  407. case TTYMODE_OSPEED_SSH1:
  408. return TTYMODE_OSPEED;
  409. default:
  410. return real_opcode;
  411. }
  412. } else {
  413. switch (real_opcode) {
  414. case TTYMODE_ISPEED_SSH2:
  415. return TTYMODE_ISPEED;
  416. case TTYMODE_OSPEED_SSH2:
  417. return TTYMODE_OSPEED;
  418. default:
  419. return real_opcode;
  420. }
  421. }
  422. }
  423. struct ssh_ttymodes get_ttymodes_from_conf(Seat *seat, Conf *conf)
  424. {
  425. struct ssh_ttymodes modes;
  426. size_t i;
  427. static const struct mode_name_type {
  428. const char *mode;
  429. int opcode;
  430. enum { TYPE_CHAR, TYPE_BOOL } type;
  431. } modes_names_types[] = {
  432. #define TTYMODE_CHAR(name, val, index) { #name, val, TYPE_CHAR },
  433. #define TTYMODE_FLAG(name, val, field, mask) { #name, val, TYPE_BOOL },
  434. #include "sshttymodes.h"
  435. #undef TTYMODE_CHAR
  436. #undef TTYMODE_FLAG
  437. };
  438. memset(&modes, 0, sizeof(modes));
  439. for (i = 0; i < lenof(modes_names_types); i++) {
  440. const struct mode_name_type *mode = &modes_names_types[i];
  441. const char *sval = conf_get_str_str(conf, CONF_ttymodes, mode->mode);
  442. char *to_free = NULL;
  443. if (!sval)
  444. sval = "N"; /* just in case */
  445. /*
  446. * sval[0] can be
  447. * - 'V', indicating that an explicit value follows it;
  448. * - 'A', indicating that we should pass the value through from
  449. * the local environment via get_ttymode; or
  450. * - 'N', indicating that we should explicitly not send this
  451. * mode.
  452. */
  453. if (sval[0] == 'A') {
  454. sval = to_free = seat_get_ttymode(seat, mode->mode);
  455. } else if (sval[0] == 'V') {
  456. sval++; /* skip the 'V' */
  457. } else {
  458. /* else 'N', or something from the future we don't understand */
  459. continue;
  460. }
  461. if (sval) {
  462. /*
  463. * Parse the string representation of the tty mode
  464. * into the integer value it will take on the wire.
  465. */
  466. unsigned ival = 0;
  467. switch (mode->type) {
  468. case TYPE_CHAR:
  469. if (*sval) {
  470. char *next = NULL;
  471. /* We know ctrlparse won't write to the string, so
  472. * casting away const is ugly but allowable. */
  473. ival = ctrlparse((char *)sval, &next);
  474. if (!next)
  475. ival = sval[0];
  476. } else {
  477. ival = 255; /* special value meaning "don't set" */
  478. }
  479. break;
  480. case TYPE_BOOL:
  481. if (stricmp(sval, "yes") == 0 ||
  482. stricmp(sval, "on") == 0 ||
  483. stricmp(sval, "true") == 0 ||
  484. stricmp(sval, "+") == 0)
  485. ival = 1; /* true */
  486. else if (stricmp(sval, "no") == 0 ||
  487. stricmp(sval, "off") == 0 ||
  488. stricmp(sval, "false") == 0 ||
  489. stricmp(sval, "-") == 0)
  490. ival = 0; /* false */
  491. else
  492. ival = (atoi(sval) != 0);
  493. break;
  494. default:
  495. unreachable("Bad mode->type");
  496. }
  497. modes.have_mode[mode->opcode] = true;
  498. modes.mode_val[mode->opcode] = ival;
  499. }
  500. sfree(to_free);
  501. }
  502. {
  503. unsigned ospeed, ispeed;
  504. /* Unpick the terminal-speed config string. */
  505. ospeed = ispeed = 38400; /* last-resort defaults */
  506. sscanf(conf_get_str(conf, CONF_termspeed), "%u,%u", &ospeed, &ispeed);
  507. /* Currently we unconditionally set these */
  508. modes.have_mode[TTYMODE_ISPEED] = true;
  509. modes.mode_val[TTYMODE_ISPEED] = ispeed;
  510. modes.have_mode[TTYMODE_OSPEED] = true;
  511. modes.mode_val[TTYMODE_OSPEED] = ospeed;
  512. }
  513. return modes;
  514. }
  515. struct ssh_ttymodes read_ttymodes_from_packet(
  516. BinarySource *bs, int ssh_version)
  517. {
  518. struct ssh_ttymodes modes;
  519. memset(&modes, 0, sizeof(modes));
  520. while (1) {
  521. unsigned real_opcode, our_opcode;
  522. real_opcode = get_byte(bs);
  523. if (real_opcode == TTYMODE_END_OF_LIST)
  524. break;
  525. if (real_opcode >= 160) {
  526. /*
  527. * RFC 4254 (and the SSH 1.5 spec): "Opcodes 160 to 255
  528. * are not yet defined, and cause parsing to stop (they
  529. * should only be used after any other data)."
  530. *
  531. * My interpretation of this is that if one of these
  532. * opcodes appears, it's not a parse _error_, but it is
  533. * something that we don't know how to parse even well
  534. * enough to step over it to find the next opcode, so we
  535. * stop parsing now and assume that the rest of the string
  536. * is composed entirely of things we don't understand and
  537. * (as usual for unsupported terminal modes) silently
  538. * ignore.
  539. */
  540. return modes;
  541. }
  542. our_opcode = our_ttymode_opcode(real_opcode, ssh_version);
  543. assert(our_opcode < TTYMODE_LIMIT);
  544. modes.have_mode[our_opcode] = true;
  545. if (ssh_version == 1 && real_opcode >= 1 && real_opcode <= 127)
  546. modes.mode_val[our_opcode] = get_byte(bs);
  547. else
  548. modes.mode_val[our_opcode] = get_uint32(bs);
  549. }
  550. return modes;
  551. }
  552. void write_ttymodes_to_packet(BinarySink *bs, int ssh_version,
  553. struct ssh_ttymodes modes)
  554. {
  555. unsigned i;
  556. for (i = 0; i < TTYMODE_LIMIT; i++) {
  557. if (modes.have_mode[i]) {
  558. unsigned val = modes.mode_val[i];
  559. unsigned opcode = real_ttymode_opcode(i, ssh_version);
  560. put_byte(bs, opcode);
  561. if (ssh_version == 1 && opcode >= 1 && opcode <= 127)
  562. put_byte(bs, val);
  563. else
  564. put_uint32(bs, val);
  565. }
  566. }
  567. put_byte(bs, TTYMODE_END_OF_LIST);
  568. }
  569. /* ----------------------------------------------------------------------
  570. * Routine for allocating a new channel ID, given a means of finding
  571. * the index field in a given channel structure.
  572. */
  573. unsigned alloc_channel_id_general(tree234 *channels, size_t localid_offset)
  574. {
  575. const unsigned CHANNEL_NUMBER_OFFSET = 256;
  576. search234_state ss;
  577. /*
  578. * First-fit allocation of channel numbers: we always pick the
  579. * lowest unused one.
  580. *
  581. * Every channel before that, and no channel after it, has an ID
  582. * exactly equal to its tree index plus CHANNEL_NUMBER_OFFSET. So
  583. * we can use the search234 system to identify the length of that
  584. * initial sequence, in a single log-time pass down the channels
  585. * tree.
  586. */
  587. search234_start(&ss, channels);
  588. while (ss.element) {
  589. unsigned localid = *(unsigned *)((char *)ss.element + localid_offset);
  590. if (localid == ss.index + CHANNEL_NUMBER_OFFSET)
  591. search234_step(&ss, +1);
  592. else
  593. search234_step(&ss, -1);
  594. }
  595. /*
  596. * Now ss.index gives exactly the number of channels in that
  597. * initial sequence. So adding CHANNEL_NUMBER_OFFSET to it must
  598. * give precisely the lowest unused channel number.
  599. */
  600. return ss.index + CHANNEL_NUMBER_OFFSET;
  601. }
  602. /* ----------------------------------------------------------------------
  603. * Functions for handling the comma-separated strings used to store
  604. * lists of protocol identifiers in SSH-2.
  605. */
  606. void add_to_commasep(strbuf *buf, const char *data)
  607. {
  608. if (buf->len > 0)
  609. put_byte(buf, ',');
  610. put_data(buf, data, strlen(data));
  611. }
  612. bool get_commasep_word(ptrlen *list, ptrlen *word)
  613. {
  614. const char *comma;
  615. /*
  616. * Discard empty list elements, should there be any, because we
  617. * never want to return one as if it was a real string. (This
  618. * introduces a mild tolerance of badly formatted data in lists we
  619. * receive, but I think that's acceptable.)
  620. */
  621. while (list->len > 0 && *(const char *)list->ptr == ',') {
  622. list->ptr = (const char *)list->ptr + 1;
  623. list->len--;
  624. }
  625. if (!list->len)
  626. return false;
  627. comma = memchr(list->ptr, ',', list->len);
  628. if (!comma) {
  629. *word = *list;
  630. list->len = 0;
  631. } else {
  632. size_t wordlen = comma - (const char *)list->ptr;
  633. word->ptr = list->ptr;
  634. word->len = wordlen;
  635. list->ptr = (const char *)list->ptr + wordlen + 1;
  636. list->len -= wordlen + 1;
  637. }
  638. return true;
  639. }
  640. /* ----------------------------------------------------------------------
  641. * Functions for translating SSH packet type codes into their symbolic
  642. * string names.
  643. */
  644. #define TRANSLATE_UNIVERSAL(y, name, value) \
  645. if (type == value) return #name;
  646. #define TRANSLATE_KEX(y, name, value, ctx) \
  647. if (type == value && pkt_kctx == ctx) return #name;
  648. #define TRANSLATE_AUTH(y, name, value, ctx) \
  649. if (type == value && pkt_actx == ctx) return #name;
  650. const char *ssh1_pkt_type(int type)
  651. {
  652. SSH1_MESSAGE_TYPES(TRANSLATE_UNIVERSAL, y);
  653. return "unknown";
  654. }
  655. const char *ssh2_pkt_type(Pkt_KCtx pkt_kctx, Pkt_ACtx pkt_actx, int type)
  656. {
  657. SSH2_MESSAGE_TYPES(TRANSLATE_UNIVERSAL, TRANSLATE_KEX, TRANSLATE_AUTH, y);
  658. return "unknown";
  659. }
  660. #undef TRANSLATE_UNIVERSAL
  661. #undef TRANSLATE_KEX
  662. #undef TRANSLATE_AUTH
  663. /* ----------------------------------------------------------------------
  664. * Common helper function for clients and implementations of
  665. * PacketProtocolLayer.
  666. */
  667. void ssh_ppl_replace(PacketProtocolLayer *old, PacketProtocolLayer *new)
  668. {
  669. new->bpp = old->bpp;
  670. ssh_ppl_setup_queues(new, old->in_pq, old->out_pq);
  671. new->selfptr = old->selfptr;
  672. new->user_input = old->user_input;
  673. new->seat = old->seat;
  674. new->ssh = old->ssh;
  675. *new->selfptr = new;
  676. ssh_ppl_free(old);
  677. /* The new layer might need to be the first one that sends a
  678. * packet, so trigger a call to its main coroutine immediately. If
  679. * it doesn't need to go first, the worst that will do is return
  680. * straight away. */
  681. queue_idempotent_callback(&new->ic_process_queue);
  682. }
  683. void ssh_ppl_free(PacketProtocolLayer *ppl)
  684. {
  685. delete_callbacks_for_context(get_seat_callback_set(ppl->seat), ppl); // WINSCP
  686. ppl->vt->free(ppl);
  687. }
  688. static void ssh_ppl_ic_process_queue_callback(void *context)
  689. {
  690. PacketProtocolLayer *ppl = (PacketProtocolLayer *)context;
  691. ssh_ppl_process_queue(ppl);
  692. }
  693. void ssh_ppl_setup_queues(PacketProtocolLayer *ppl,
  694. PktInQueue *inq, PktOutQueue *outq)
  695. {
  696. ppl->in_pq = inq;
  697. ppl->out_pq = outq;
  698. ppl->in_pq->pqb.ic = &ppl->ic_process_queue;
  699. ppl->ic_process_queue.fn = ssh_ppl_ic_process_queue_callback;
  700. ppl->ic_process_queue.ctx = ppl;
  701. ppl->ic_process_queue.set = get_seat_callback_set(ppl->seat);
  702. /* If there's already something on the input queue, it will want
  703. * handling immediately. */
  704. if (pq_peek(ppl->in_pq))
  705. queue_idempotent_callback(&ppl->ic_process_queue);
  706. }
  707. void ssh_ppl_user_output_string_and_free(PacketProtocolLayer *ppl, char *text)
  708. {
  709. /* Messages sent via this function are from the SSH layer, not
  710. * from the server-side process, so they always have the stderr
  711. * flag set. */
  712. int stderrflag = -1; // WINSCP
  713. seat_output(ppl->seat, *((bool*)&stderrflag), text, strlen(text)); // WINSCP
  714. sfree(text);
  715. }
  716. /* ----------------------------------------------------------------------
  717. * Common helper functions for clients and implementations of
  718. * BinaryPacketProtocol.
  719. */
  720. static void ssh_bpp_input_raw_data_callback(void *context)
  721. {
  722. BinaryPacketProtocol *bpp = (BinaryPacketProtocol *)context;
  723. Ssh *ssh = bpp->ssh; /* in case bpp is about to get freed */
  724. ssh_bpp_handle_input(bpp);
  725. /* If we've now cleared enough backlog on the input connection, we
  726. * may need to unfreeze it. */
  727. ssh_conn_processed_data(ssh);
  728. }
  729. static void ssh_bpp_output_packet_callback(void *context)
  730. {
  731. BinaryPacketProtocol *bpp = (BinaryPacketProtocol *)context;
  732. ssh_bpp_handle_output(bpp);
  733. }
  734. void ssh_bpp_common_setup(BinaryPacketProtocol *bpp)
  735. {
  736. pq_in_init(&bpp->in_pq, get_log_seat(bpp->logctx)); // WINSCP
  737. pq_out_init(&bpp->out_pq, get_log_seat(bpp->logctx)); // WINSCP
  738. bpp->input_eof = false;
  739. bpp->ic_in_raw.fn = ssh_bpp_input_raw_data_callback;
  740. bpp->ic_in_raw.set = get_log_callback_set(bpp->logctx);
  741. bpp->ic_in_raw.ctx = bpp;
  742. bpp->ic_out_pq.fn = ssh_bpp_output_packet_callback;
  743. bpp->ic_out_pq.set = get_log_callback_set(bpp->logctx);
  744. bpp->ic_out_pq.ctx = bpp;
  745. bpp->out_pq.pqb.ic = &bpp->ic_out_pq;
  746. }
  747. void ssh_bpp_free(BinaryPacketProtocol *bpp)
  748. {
  749. // WINSCP
  750. delete_callbacks_for_context(get_log_callback_set(bpp->logctx), bpp);
  751. bpp->vt->free(bpp);
  752. }
  753. void ssh2_bpp_queue_disconnect(BinaryPacketProtocol *bpp,
  754. const char *msg, int category)
  755. {
  756. PktOut *pkt = ssh_bpp_new_pktout(bpp, SSH2_MSG_DISCONNECT);
  757. put_uint32(pkt, category);
  758. put_stringz(pkt, msg);
  759. put_stringz(pkt, "en"); /* language tag */
  760. pq_push(&bpp->out_pq, pkt);
  761. }
  762. #define BITMAP_UNIVERSAL(y, name, value) \
  763. | (value >= y && value < y+32 ? 1UL << (value-y) : 0)
  764. #define BITMAP_CONDITIONAL(y, name, value, ctx) \
  765. BITMAP_UNIVERSAL(y, name, value)
  766. #define SSH2_BITMAP_WORD(y) \
  767. (0 SSH2_MESSAGE_TYPES(BITMAP_UNIVERSAL, BITMAP_CONDITIONAL, \
  768. BITMAP_CONDITIONAL, (32*y)))
  769. bool ssh2_bpp_check_unimplemented(BinaryPacketProtocol *bpp, PktIn *pktin)
  770. {
  771. #pragma warn -osh
  772. static const unsigned valid_bitmap[] = {
  773. SSH2_BITMAP_WORD(0),
  774. SSH2_BITMAP_WORD(1),
  775. SSH2_BITMAP_WORD(2),
  776. SSH2_BITMAP_WORD(3),
  777. SSH2_BITMAP_WORD(4),
  778. SSH2_BITMAP_WORD(5),
  779. SSH2_BITMAP_WORD(6),
  780. SSH2_BITMAP_WORD(7),
  781. };
  782. #pragma warn +osh
  783. if (pktin->type < 0x100 &&
  784. !((valid_bitmap[pktin->type >> 5] >> (pktin->type & 0x1F)) & 1)) {
  785. PktOut *pkt = ssh_bpp_new_pktout(bpp, SSH2_MSG_UNIMPLEMENTED);
  786. put_uint32(pkt, pktin->sequence);
  787. pq_push(&bpp->out_pq, pkt);
  788. return true;
  789. }
  790. return false;
  791. }
  792. #undef BITMAP_UNIVERSAL
  793. #undef BITMAP_CONDITIONAL
  794. #undef SSH1_BITMAP_WORD
  795. /* ----------------------------------------------------------------------
  796. * Function to check a host key against any manually configured in Conf.
  797. */
  798. int verify_ssh_manual_host_key(
  799. Conf *conf, const char *fingerprint, ssh_key *key)
  800. {
  801. if (!conf_get_str_nthstrkey(conf, CONF_ssh_manual_hostkeys, 0))
  802. return -1; /* no manual keys configured */
  803. if (fingerprint) {
  804. /*
  805. * The fingerprint string we've been given will have things
  806. * like 'ssh-rsa 2048' at the front of it. Strip those off and
  807. * narrow down to just the colon-separated hex block at the
  808. * end of the string.
  809. */
  810. const char *p = strrchr(fingerprint, ' ');
  811. fingerprint = p ? p+1 : fingerprint;
  812. /* Quick sanity checks, including making sure it's in lowercase */
  813. assert(strlen(fingerprint) == 16*3 - 1);
  814. assert(fingerprint[2] == ':');
  815. assert(fingerprint[strspn(fingerprint, "0123456789abcdef:")] == 0);
  816. if (conf_get_str_str_opt(conf, CONF_ssh_manual_hostkeys, fingerprint))
  817. return 1; /* success */
  818. }
  819. if (key) {
  820. /*
  821. * Construct the base64-encoded public key blob and see if
  822. * that's listed.
  823. */
  824. strbuf *binblob;
  825. char *base64blob;
  826. int atoms, i;
  827. binblob = strbuf_new();
  828. ssh_key_public_blob(key, BinarySink_UPCAST(binblob));
  829. atoms = (binblob->len + 2) / 3;
  830. base64blob = snewn(atoms * 4 + 1, char);
  831. for (i = 0; i < atoms; i++)
  832. base64_encode_atom(binblob->u + 3*i,
  833. binblob->len - 3*i, base64blob + 4*i);
  834. base64blob[atoms * 4] = '\0';
  835. strbuf_free(binblob);
  836. if (conf_get_str_str_opt(conf, CONF_ssh_manual_hostkeys, base64blob)) {
  837. sfree(base64blob);
  838. return 1; /* success */
  839. }
  840. sfree(base64blob);
  841. }
  842. return 0;
  843. }
  844. /* ----------------------------------------------------------------------
  845. * Common functions shared between SSH-1 layers.
  846. */
  847. bool ssh1_common_get_specials(
  848. PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx)
  849. {
  850. /*
  851. * Don't bother offering IGNORE if we've decided the remote
  852. * won't cope with it, since we wouldn't bother sending it if
  853. * asked anyway.
  854. */
  855. if (!(ppl->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) {
  856. add_special(ctx, "IGNORE message", SS_NOP, 0);
  857. return true;
  858. }
  859. return false;
  860. }
  861. bool ssh1_common_filter_queue(PacketProtocolLayer *ppl)
  862. {
  863. PktIn *pktin;
  864. ptrlen msg;
  865. while ((pktin = pq_peek(ppl->in_pq)) != NULL) {
  866. switch (pktin->type) {
  867. case SSH1_MSG_DISCONNECT:
  868. msg = get_string(pktin);
  869. ssh_remote_error(ppl->ssh,
  870. "Remote side sent disconnect message:\n\"%.*s\"",
  871. PTRLEN_PRINTF(msg));
  872. pq_pop(ppl->in_pq);
  873. return true; /* indicate that we've been freed */
  874. case SSH1_MSG_DEBUG:
  875. msg = get_string(pktin);
  876. ppl_logevent("Remote debug message: %.*s", PTRLEN_PRINTF(msg));
  877. pq_pop(ppl->in_pq);
  878. break;
  879. case SSH1_MSG_IGNORE:
  880. /* Do nothing, because we're ignoring it! Duhh. */
  881. pq_pop(ppl->in_pq);
  882. break;
  883. default:
  884. return false;
  885. }
  886. }
  887. return false;
  888. }
  889. void ssh1_compute_session_id(
  890. unsigned char *session_id, const unsigned char *cookie,
  891. RSAKey *hostkey, RSAKey *servkey)
  892. {
  893. ssh_hash *hash = ssh_hash_new(&ssh_md5);
  894. size_t i; // WINSCP
  895. for (i = (mp_get_nbits(hostkey->modulus) + 7) / 8; i-- ;)
  896. put_byte(hash, mp_get_byte(hostkey->modulus, i));
  897. for (i = (mp_get_nbits(servkey->modulus) + 7) / 8; i-- ;)
  898. put_byte(hash, mp_get_byte(servkey->modulus, i));
  899. put_data(hash, cookie, 8);
  900. ssh_hash_final(hash, session_id);
  901. }
  902. /* ----------------------------------------------------------------------
  903. * Other miscellaneous utility functions.
  904. */
  905. void free_rportfwd(struct ssh_rportfwd *rpf)
  906. {
  907. if (rpf) {
  908. sfree(rpf->log_description);
  909. sfree(rpf->shost);
  910. sfree(rpf->dhost);
  911. sfree(rpf);
  912. }
  913. }