sshcommon.c 32 KB

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