sshcommon.c 32 KB

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