sshcommon.c 30 KB

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