sshcommon.c 31 KB

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