sshcommon.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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. static PacketQueueNode pktin_freeq_head = {
  46. &pktin_freeq_head, &pktin_freeq_head, TRUE
  47. };
  48. static void pktin_free_queue_callback(void *vctx)
  49. {
  50. while (pktin_freeq_head.next != &pktin_freeq_head) {
  51. PacketQueueNode *node = pktin_freeq_head.next;
  52. PktIn *pktin = container_of(node, PktIn, qnode);
  53. pktin_freeq_head.next = node->next;
  54. sfree(pktin);
  55. }
  56. pktin_freeq_head.prev = &pktin_freeq_head;
  57. }
  58. static IdempotentCallback ic_pktin_free = {
  59. pktin_free_queue_callback, NULL, FALSE
  60. };
  61. static PktIn *pq_in_after(PacketQueueBase *pqb,
  62. PacketQueueNode *prev, int pop)
  63. {
  64. PacketQueueNode *node = prev->next;
  65. if (node == &pqb->end)
  66. return NULL;
  67. if (pop) {
  68. node->next->prev = node->prev;
  69. node->prev->next = node->next;
  70. node->prev = pktin_freeq_head.prev;
  71. node->next = &pktin_freeq_head;
  72. node->next->prev = node;
  73. node->prev->next = node;
  74. node->on_free_queue = TRUE;
  75. queue_idempotent_callback(&ic_pktin_free);
  76. }
  77. return container_of(node, PktIn, qnode);
  78. }
  79. static PktOut *pq_out_after(PacketQueueBase *pqb,
  80. PacketQueueNode *prev, int pop)
  81. {
  82. PacketQueueNode *node = prev->next;
  83. if (node == &pqb->end)
  84. return NULL;
  85. if (pop) {
  86. node->next->prev = node->prev;
  87. node->prev->next = node->next;
  88. node->prev = node->next = NULL;
  89. }
  90. return container_of(node, PktOut, qnode);
  91. }
  92. void pq_in_init(PktInQueue *pq)
  93. {
  94. pq->pqb.ic = NULL;
  95. pq->pqb.end.next = pq->pqb.end.prev = &pq->pqb.end;
  96. pq->after = pq_in_after;
  97. }
  98. void pq_out_init(PktOutQueue *pq)
  99. {
  100. pq->pqb.ic = NULL;
  101. pq->pqb.end.next = pq->pqb.end.prev = &pq->pqb.end;
  102. pq->after = pq_out_after;
  103. }
  104. void pq_in_clear(PktInQueue *pq)
  105. {
  106. PktIn *pkt;
  107. pq->pqb.ic = NULL;
  108. while ((pkt = pq_pop(pq)) != NULL) {
  109. /* No need to actually free these packets: pq_pop on a
  110. * PktInQueue will automatically move them to the free
  111. * queue. */
  112. }
  113. }
  114. void pq_out_clear(PktOutQueue *pq)
  115. {
  116. PktOut *pkt;
  117. pq->pqb.ic = NULL;
  118. while ((pkt = pq_pop(pq)) != NULL)
  119. ssh_free_pktout(pkt);
  120. }
  121. /*
  122. * Concatenate the contents of the two queues q1 and q2, and leave the
  123. * result in qdest. qdest must be either empty, or one of the input
  124. * queues.
  125. */
  126. void pq_base_concatenate(PacketQueueBase *qdest,
  127. PacketQueueBase *q1, PacketQueueBase *q2)
  128. {
  129. struct PacketQueueNode *head1, *tail1, *head2, *tail2;
  130. /*
  131. * Extract the contents from both input queues, and empty them.
  132. */
  133. head1 = (q1->end.next == &q1->end ? NULL : q1->end.next);
  134. tail1 = (q1->end.prev == &q1->end ? NULL : q1->end.prev);
  135. head2 = (q2->end.next == &q2->end ? NULL : q2->end.next);
  136. tail2 = (q2->end.prev == &q2->end ? NULL : q2->end.prev);
  137. q1->end.next = q1->end.prev = &q1->end;
  138. q2->end.next = q2->end.prev = &q2->end;
  139. /*
  140. * Link the two lists together, handling the case where one or
  141. * both is empty.
  142. */
  143. if (tail1)
  144. tail1->next = head2;
  145. else
  146. head1 = head2;
  147. if (head2)
  148. head2->prev = tail1;
  149. else
  150. tail2 = tail1;
  151. /*
  152. * Check the destination queue is currently empty. (If it was one
  153. * of the input queues, then it will be, because we emptied both
  154. * of those just a moment ago.)
  155. */
  156. assert(qdest->end.next == &qdest->end);
  157. assert(qdest->end.prev == &qdest->end);
  158. /*
  159. * If our concatenated list has anything in it, then put it in
  160. * dest.
  161. */
  162. if (!head1) {
  163. assert(!tail2);
  164. } else {
  165. assert(tail2);
  166. qdest->end.next = head1;
  167. qdest->end.prev = tail2;
  168. head1->prev = &qdest->end;
  169. tail2->next = &qdest->end;
  170. if (qdest->ic)
  171. queue_idempotent_callback(qdest->ic);
  172. }
  173. }
  174. /* ----------------------------------------------------------------------
  175. * Low-level functions for the packet structures themselves.
  176. */
  177. static void ssh_pkt_BinarySink_write(BinarySink *bs,
  178. const void *data, size_t len);
  179. PktOut *ssh_new_packet(void)
  180. {
  181. PktOut *pkt = snew(PktOut);
  182. BinarySink_INIT(pkt, ssh_pkt_BinarySink_write);
  183. pkt->data = NULL;
  184. pkt->length = 0;
  185. pkt->maxlen = 0;
  186. pkt->downstream_id = 0;
  187. pkt->additional_log_text = NULL;
  188. pkt->qnode.next = pkt->qnode.prev = NULL;
  189. pkt->qnode.on_free_queue = FALSE;
  190. return pkt;
  191. }
  192. static void ssh_pkt_ensure(PktOut *pkt, int length)
  193. {
  194. if (pkt->maxlen < length) {
  195. pkt->maxlen = length + 256;
  196. pkt->data = sresize(pkt->data, pkt->maxlen, unsigned char);
  197. }
  198. }
  199. static void ssh_pkt_adddata(PktOut *pkt, const void *data, int len)
  200. {
  201. pkt->length += len;
  202. ssh_pkt_ensure(pkt, pkt->length);
  203. memcpy(pkt->data + pkt->length - len, data, len);
  204. }
  205. static void ssh_pkt_BinarySink_write(BinarySink *bs,
  206. const void *data, size_t len)
  207. {
  208. PktOut *pkt = BinarySink_DOWNCAST(bs, PktOut);
  209. ssh_pkt_adddata(pkt, data, len);
  210. }
  211. void ssh_free_pktout(PktOut *pkt)
  212. {
  213. sfree(pkt->data);
  214. sfree(pkt);
  215. }
  216. /* ----------------------------------------------------------------------
  217. * Implement zombiechan_new() and its trivial vtable.
  218. */
  219. static void zombiechan_free(Channel *chan);
  220. static int zombiechan_send(Channel *chan, int is_stderr, const void *, int);
  221. static void zombiechan_set_input_wanted(Channel *chan, int wanted);
  222. static void zombiechan_do_nothing(Channel *chan);
  223. static void zombiechan_open_failure(Channel *chan, const char *);
  224. static int zombiechan_want_close(Channel *chan, int sent_eof, int rcvd_eof);
  225. static char *zombiechan_log_close_msg(Channel *chan) { return NULL; }
  226. static const struct ChannelVtable zombiechan_channelvt = {
  227. zombiechan_free,
  228. zombiechan_do_nothing, /* open_confirmation */
  229. zombiechan_open_failure,
  230. zombiechan_send,
  231. zombiechan_do_nothing, /* send_eof */
  232. zombiechan_set_input_wanted,
  233. zombiechan_log_close_msg,
  234. zombiechan_want_close,
  235. };
  236. Channel *zombiechan_new(void)
  237. {
  238. Channel *chan = snew(Channel);
  239. chan->vt = &zombiechan_channelvt;
  240. chan->initial_fixed_window_size = 0;
  241. return chan;
  242. }
  243. static void zombiechan_free(Channel *chan)
  244. {
  245. assert(chan->vt == &zombiechan_channelvt);
  246. sfree(chan);
  247. }
  248. static void zombiechan_do_nothing(Channel *chan)
  249. {
  250. assert(chan->vt == &zombiechan_channelvt);
  251. }
  252. static void zombiechan_open_failure(Channel *chan, const char *errtext)
  253. {
  254. assert(chan->vt == &zombiechan_channelvt);
  255. }
  256. static int zombiechan_send(Channel *chan, int is_stderr,
  257. const void *data, int length)
  258. {
  259. assert(chan->vt == &zombiechan_channelvt);
  260. return 0;
  261. }
  262. static void zombiechan_set_input_wanted(Channel *chan, int enable)
  263. {
  264. assert(chan->vt == &zombiechan_channelvt);
  265. }
  266. static int zombiechan_want_close(Channel *chan, int sent_eof, int rcvd_eof)
  267. {
  268. return TRUE;
  269. }
  270. /* ----------------------------------------------------------------------
  271. * Centralised standard methods for other channel implementations to
  272. * borrow.
  273. */
  274. void chan_remotely_opened_confirmation(Channel *chan)
  275. {
  276. assert(0 && "this channel type should never receive OPEN_CONFIRMATION");
  277. }
  278. void chan_remotely_opened_failure(Channel *chan, const char *errtext)
  279. {
  280. assert(0 && "this channel type should never receive OPEN_FAILURE");
  281. }
  282. int chan_no_eager_close(Channel *chan, int sent_local_eof, int rcvd_remote_eof)
  283. {
  284. return FALSE; /* default: never proactively ask for a close */
  285. }
  286. /* ----------------------------------------------------------------------
  287. * Common routine to marshal tty modes into an SSH packet.
  288. */
  289. void write_ttymodes_to_packet_from_conf(
  290. BinarySink *bs, Frontend *frontend, Conf *conf,
  291. int ssh_version, int ospeed, int ispeed)
  292. {
  293. int i;
  294. /*
  295. * Codes for terminal modes.
  296. * Most of these are the same in SSH-1 and SSH-2.
  297. * This list is derived from RFC 4254 and
  298. * SSH-1 RFC-1.2.31.
  299. */
  300. static const struct ssh_ttymode {
  301. const char *mode;
  302. int opcode;
  303. enum { TTY_OP_CHAR, TTY_OP_BOOL } type;
  304. } ssh_ttymodes[] = {
  305. /* "V" prefix discarded for special characters relative to SSH specs */
  306. { "INTR", 1, TTY_OP_CHAR },
  307. { "QUIT", 2, TTY_OP_CHAR },
  308. { "ERASE", 3, TTY_OP_CHAR },
  309. { "KILL", 4, TTY_OP_CHAR },
  310. { "EOF", 5, TTY_OP_CHAR },
  311. { "EOL", 6, TTY_OP_CHAR },
  312. { "EOL2", 7, TTY_OP_CHAR },
  313. { "START", 8, TTY_OP_CHAR },
  314. { "STOP", 9, TTY_OP_CHAR },
  315. { "SUSP", 10, TTY_OP_CHAR },
  316. { "DSUSP", 11, TTY_OP_CHAR },
  317. { "REPRINT", 12, TTY_OP_CHAR },
  318. { "WERASE", 13, TTY_OP_CHAR },
  319. { "LNEXT", 14, TTY_OP_CHAR },
  320. { "FLUSH", 15, TTY_OP_CHAR },
  321. { "SWTCH", 16, TTY_OP_CHAR },
  322. { "STATUS", 17, TTY_OP_CHAR },
  323. { "DISCARD", 18, TTY_OP_CHAR },
  324. { "IGNPAR", 30, TTY_OP_BOOL },
  325. { "PARMRK", 31, TTY_OP_BOOL },
  326. { "INPCK", 32, TTY_OP_BOOL },
  327. { "ISTRIP", 33, TTY_OP_BOOL },
  328. { "INLCR", 34, TTY_OP_BOOL },
  329. { "IGNCR", 35, TTY_OP_BOOL },
  330. { "ICRNL", 36, TTY_OP_BOOL },
  331. { "IUCLC", 37, TTY_OP_BOOL },
  332. { "IXON", 38, TTY_OP_BOOL },
  333. { "IXANY", 39, TTY_OP_BOOL },
  334. { "IXOFF", 40, TTY_OP_BOOL },
  335. { "IMAXBEL", 41, TTY_OP_BOOL },
  336. { "IUTF8", 42, TTY_OP_BOOL },
  337. { "ISIG", 50, TTY_OP_BOOL },
  338. { "ICANON", 51, TTY_OP_BOOL },
  339. { "XCASE", 52, TTY_OP_BOOL },
  340. { "ECHO", 53, TTY_OP_BOOL },
  341. { "ECHOE", 54, TTY_OP_BOOL },
  342. { "ECHOK", 55, TTY_OP_BOOL },
  343. { "ECHONL", 56, TTY_OP_BOOL },
  344. { "NOFLSH", 57, TTY_OP_BOOL },
  345. { "TOSTOP", 58, TTY_OP_BOOL },
  346. { "IEXTEN", 59, TTY_OP_BOOL },
  347. { "ECHOCTL", 60, TTY_OP_BOOL },
  348. { "ECHOKE", 61, TTY_OP_BOOL },
  349. { "PENDIN", 62, TTY_OP_BOOL }, /* XXX is this a real mode? */
  350. { "OPOST", 70, TTY_OP_BOOL },
  351. { "OLCUC", 71, TTY_OP_BOOL },
  352. { "ONLCR", 72, TTY_OP_BOOL },
  353. { "OCRNL", 73, TTY_OP_BOOL },
  354. { "ONOCR", 74, TTY_OP_BOOL },
  355. { "ONLRET", 75, TTY_OP_BOOL },
  356. { "CS7", 90, TTY_OP_BOOL },
  357. { "CS8", 91, TTY_OP_BOOL },
  358. { "PARENB", 92, TTY_OP_BOOL },
  359. { "PARODD", 93, TTY_OP_BOOL }
  360. };
  361. /* Miscellaneous other tty-related constants. */
  362. enum {
  363. /* The opcodes for ISPEED/OSPEED differ between SSH-1 and SSH-2. */
  364. SSH1_TTY_OP_ISPEED = 192,
  365. SSH1_TTY_OP_OSPEED = 193,
  366. SSH2_TTY_OP_ISPEED = 128,
  367. SSH2_TTY_OP_OSPEED = 129,
  368. SSH_TTY_OP_END = 0
  369. };
  370. for (i = 0; i < lenof(ssh_ttymodes); i++) {
  371. const struct ssh_ttymode *mode = ssh_ttymodes + i;
  372. const char *sval = conf_get_str_str(conf, CONF_ttymodes, mode->mode);
  373. char *to_free = NULL;
  374. /* Every mode known to the current version of the code should be
  375. * mentioned; this was ensured when settings were loaded. */
  376. /*
  377. * sval[0] can be
  378. * - 'V', indicating that an explicit value follows it;
  379. * - 'A', indicating that we should pass the value through from
  380. * the local environment via get_ttymode; or
  381. * - 'N', indicating that we should explicitly not send this
  382. * mode.
  383. */
  384. if (sval[0] == 'A') {
  385. sval = to_free = get_ttymode(frontend, mode->mode);
  386. } else if (sval[0] == 'V') {
  387. sval++; /* skip the 'V' */
  388. } else {
  389. /* else 'N', or something from the future we don't understand */
  390. continue;
  391. }
  392. if (sval) {
  393. /*
  394. * Parse the string representation of the tty mode
  395. * into the integer value it will take on the wire.
  396. */
  397. unsigned ival = 0;
  398. switch (mode->type) {
  399. case TTY_OP_CHAR:
  400. if (*sval) {
  401. char *next = NULL;
  402. /* We know ctrlparse won't write to the string, so
  403. * casting away const is ugly but allowable. */
  404. ival = ctrlparse((char *)sval, &next);
  405. if (!next)
  406. ival = sval[0];
  407. } else {
  408. ival = 255; /* special value meaning "don't set" */
  409. }
  410. break;
  411. case TTY_OP_BOOL:
  412. if (stricmp(sval, "yes") == 0 ||
  413. stricmp(sval, "on") == 0 ||
  414. stricmp(sval, "true") == 0 ||
  415. stricmp(sval, "+") == 0)
  416. ival = 1; /* true */
  417. else if (stricmp(sval, "no") == 0 ||
  418. stricmp(sval, "off") == 0 ||
  419. stricmp(sval, "false") == 0 ||
  420. stricmp(sval, "-") == 0)
  421. ival = 0; /* false */
  422. else
  423. ival = (atoi(sval) != 0);
  424. break;
  425. default:
  426. assert(0 && "Bad mode->type");
  427. }
  428. /*
  429. * And write it into the output packet. The parameter
  430. * value is formatted as a byte in SSH-1, but a uint32
  431. * in SSH-2.
  432. */
  433. put_byte(bs, mode->opcode);
  434. if (ssh_version == 1)
  435. put_byte(bs, ival);
  436. else
  437. put_uint32(bs, ival);
  438. }
  439. sfree(to_free);
  440. }
  441. /*
  442. * Finish off with the terminal speeds (which are formatted as
  443. * uint32 in both protocol versions) and the end marker.
  444. */
  445. put_byte(bs, ssh_version == 1 ? SSH1_TTY_OP_ISPEED : SSH2_TTY_OP_ISPEED);
  446. put_uint32(bs, ispeed);
  447. put_byte(bs, ssh_version == 1 ? SSH1_TTY_OP_OSPEED : SSH2_TTY_OP_OSPEED);
  448. put_uint32(bs, ospeed);
  449. put_byte(bs, SSH_TTY_OP_END);
  450. }
  451. /* ----------------------------------------------------------------------
  452. * Routine for allocating a new channel ID, given a means of finding
  453. * the index field in a given channel structure.
  454. */
  455. unsigned alloc_channel_id_general(tree234 *channels, size_t localid_offset)
  456. {
  457. const unsigned CHANNEL_NUMBER_OFFSET = 256;
  458. search234_state ss;
  459. /*
  460. * First-fit allocation of channel numbers: we always pick the
  461. * lowest unused one.
  462. *
  463. * Every channel before that, and no channel after it, has an ID
  464. * exactly equal to its tree index plus CHANNEL_NUMBER_OFFSET. So
  465. * we can use the search234 system to identify the length of that
  466. * initial sequence, in a single log-time pass down the channels
  467. * tree.
  468. */
  469. search234_start(&ss, channels);
  470. while (ss.element) {
  471. unsigned localid = *(unsigned *)((char *)ss.element + localid_offset);
  472. if (localid == ss.index + CHANNEL_NUMBER_OFFSET)
  473. search234_step(&ss, +1);
  474. else
  475. search234_step(&ss, -1);
  476. }
  477. /*
  478. * Now ss.index gives exactly the number of channels in that
  479. * initial sequence. So adding CHANNEL_NUMBER_OFFSET to it must
  480. * give precisely the lowest unused channel number.
  481. */
  482. return ss.index + CHANNEL_NUMBER_OFFSET;
  483. }
  484. /* ----------------------------------------------------------------------
  485. * Functions for handling the comma-separated strings used to store
  486. * lists of protocol identifiers in SSH-2.
  487. */
  488. int first_in_commasep_string(char const *needle, char const *haystack,
  489. int haylen)
  490. {
  491. int needlen;
  492. if (!needle || !haystack) /* protect against null pointers */
  493. return 0;
  494. needlen = strlen(needle);
  495. if (haylen >= needlen && /* haystack is long enough */
  496. !memcmp(needle, haystack, needlen) && /* initial match */
  497. (haylen == needlen || haystack[needlen] == ',')
  498. /* either , or EOS follows */
  499. )
  500. return 1;
  501. return 0;
  502. }
  503. int in_commasep_string(char const *needle, char const *haystack, int haylen)
  504. {
  505. char *p;
  506. if (!needle || !haystack) /* protect against null pointers */
  507. return FALSE;
  508. /*
  509. * Is it at the start of the string?
  510. */
  511. if (first_in_commasep_string(needle, haystack, haylen))
  512. return TRUE;
  513. /*
  514. * If not, search for the next comma and resume after that.
  515. * If no comma found, terminate.
  516. */
  517. p = memchr(haystack, ',', haylen);
  518. if (!p)
  519. return FALSE;
  520. /* + 1 to skip over comma */
  521. return in_commasep_string(needle, p + 1, haylen - (p + 1 - haystack));
  522. }
  523. void add_to_commasep(strbuf *buf, const char *data)
  524. {
  525. if (buf->len > 0)
  526. put_byte(buf, ',');
  527. put_data(buf, data, strlen(data));
  528. }
  529. /* ----------------------------------------------------------------------
  530. * Functions for translating SSH packet type codes into their symbolic
  531. * string names.
  532. */
  533. #define TRANSLATE_UNIVERSAL(y, name, value) \
  534. if (type == value) return #name;
  535. #define TRANSLATE_KEX(y, name, value, ctx) \
  536. if (type == value && pkt_kctx == ctx) return #name;
  537. #define TRANSLATE_AUTH(y, name, value, ctx) \
  538. if (type == value && pkt_actx == ctx) return #name;
  539. const char *ssh1_pkt_type(int type)
  540. {
  541. SSH1_MESSAGE_TYPES(TRANSLATE_UNIVERSAL, y);
  542. return "unknown";
  543. }
  544. const char *ssh2_pkt_type(Pkt_KCtx pkt_kctx, Pkt_ACtx pkt_actx, int type)
  545. {
  546. SSH2_MESSAGE_TYPES(TRANSLATE_UNIVERSAL, TRANSLATE_KEX, TRANSLATE_AUTH, y);
  547. return "unknown";
  548. }
  549. #undef TRANSLATE_UNIVERSAL
  550. #undef TRANSLATE_KEX
  551. #undef TRANSLATE_AUTH
  552. /* ----------------------------------------------------------------------
  553. * Common helper function for clients and implementations of
  554. * PacketProtocolLayer.
  555. */
  556. void ssh_logevent_and_free(void *frontend, char *message)
  557. {
  558. logevent(frontend, message);
  559. sfree(message);
  560. }
  561. void ssh_ppl_replace(PacketProtocolLayer *old, PacketProtocolLayer *new)
  562. {
  563. new->bpp = old->bpp;
  564. ssh_ppl_setup_queues(new, old->in_pq, old->out_pq);
  565. new->selfptr = old->selfptr;
  566. new->user_input = old->user_input;
  567. new->frontend = old->frontend;
  568. new->ssh = old->ssh;
  569. *new->selfptr = new;
  570. ssh_ppl_free(old);
  571. /* The new layer might need to be the first one that sends a
  572. * packet, so trigger a call to its main coroutine immediately. If
  573. * it doesn't need to go first, the worst that will do is return
  574. * straight away. */
  575. queue_idempotent_callback(&new->ic_process_queue);
  576. }
  577. void ssh_ppl_free(PacketProtocolLayer *ppl)
  578. {
  579. delete_callbacks_for_context(ppl);
  580. ppl->vt->free(ppl);
  581. }
  582. static void ssh_ppl_ic_process_queue_callback(void *context)
  583. {
  584. PacketProtocolLayer *ppl = (PacketProtocolLayer *)context;
  585. ssh_ppl_process_queue(ppl);
  586. }
  587. void ssh_ppl_setup_queues(PacketProtocolLayer *ppl,
  588. PktInQueue *inq, PktOutQueue *outq)
  589. {
  590. ppl->in_pq = inq;
  591. ppl->out_pq = outq;
  592. ppl->in_pq->pqb.ic = &ppl->ic_process_queue;
  593. ppl->ic_process_queue.fn = ssh_ppl_ic_process_queue_callback;
  594. ppl->ic_process_queue.ctx = ppl;
  595. /* If there's already something on the input queue, it will want
  596. * handling immediately. */
  597. if (pq_peek(ppl->in_pq))
  598. queue_idempotent_callback(&ppl->ic_process_queue);
  599. }
  600. void ssh_ppl_user_output_string_and_free(PacketProtocolLayer *ppl, char *text)
  601. {
  602. /* Messages sent via this function are from the SSH layer, not
  603. * from the server-side process, so they always have the stderr
  604. * flag set. */
  605. from_backend(ppl->frontend, TRUE, text, strlen(text));
  606. sfree(text);
  607. }
  608. /* ----------------------------------------------------------------------
  609. * Common helper functions for clients and implementations of
  610. * BinaryPacketProtocol.
  611. */
  612. static void ssh_bpp_input_raw_data_callback(void *context)
  613. {
  614. BinaryPacketProtocol *bpp = (BinaryPacketProtocol *)context;
  615. ssh_bpp_handle_input(bpp);
  616. }
  617. static void ssh_bpp_output_packet_callback(void *context)
  618. {
  619. BinaryPacketProtocol *bpp = (BinaryPacketProtocol *)context;
  620. ssh_bpp_handle_output(bpp);
  621. }
  622. void ssh_bpp_common_setup(BinaryPacketProtocol *bpp)
  623. {
  624. pq_in_init(&bpp->in_pq);
  625. pq_out_init(&bpp->out_pq);
  626. bpp->input_eof = FALSE;
  627. bpp->ic_in_raw.fn = ssh_bpp_input_raw_data_callback;
  628. bpp->ic_in_raw.ctx = bpp;
  629. bpp->ic_out_pq.fn = ssh_bpp_output_packet_callback;
  630. bpp->ic_out_pq.ctx = bpp;
  631. bpp->out_pq.pqb.ic = &bpp->ic_out_pq;
  632. }
  633. void ssh_bpp_free(BinaryPacketProtocol *bpp)
  634. {
  635. delete_callbacks_for_context(bpp);
  636. bpp->vt->free(bpp);
  637. }
  638. void ssh2_bpp_queue_disconnect(BinaryPacketProtocol *bpp,
  639. const char *msg, int category)
  640. {
  641. PktOut *pkt = ssh_bpp_new_pktout(bpp, SSH2_MSG_DISCONNECT);
  642. put_uint32(pkt, category);
  643. put_stringz(pkt, msg);
  644. put_stringz(pkt, "en"); /* language tag */
  645. pq_push(&bpp->out_pq, pkt);
  646. }
  647. #define BITMAP_UNIVERSAL(y, name, value) \
  648. | (value >= y && value < y+32 ? 1UL << (value-y) : 0)
  649. #define BITMAP_CONDITIONAL(y, name, value, ctx) \
  650. BITMAP_UNIVERSAL(y, name, value)
  651. #define SSH2_BITMAP_WORD(y) \
  652. (0 SSH2_MESSAGE_TYPES(BITMAP_UNIVERSAL, BITMAP_CONDITIONAL, \
  653. BITMAP_CONDITIONAL, (32*y)))
  654. int ssh2_bpp_check_unimplemented(BinaryPacketProtocol *bpp, PktIn *pktin)
  655. {
  656. static const unsigned valid_bitmap[] = {
  657. SSH2_BITMAP_WORD(0),
  658. SSH2_BITMAP_WORD(1),
  659. SSH2_BITMAP_WORD(2),
  660. SSH2_BITMAP_WORD(3),
  661. SSH2_BITMAP_WORD(4),
  662. SSH2_BITMAP_WORD(5),
  663. SSH2_BITMAP_WORD(6),
  664. SSH2_BITMAP_WORD(7),
  665. };
  666. if (pktin->type < 0x100 &&
  667. !((valid_bitmap[pktin->type >> 5] >> (pktin->type & 0x1F)) & 1)) {
  668. PktOut *pkt = ssh_bpp_new_pktout(bpp, SSH2_MSG_UNIMPLEMENTED);
  669. put_uint32(pkt, pktin->sequence);
  670. pq_push(&bpp->out_pq, pkt);
  671. return TRUE;
  672. }
  673. return FALSE;
  674. }
  675. #undef BITMAP_UNIVERSAL
  676. #undef BITMAP_CONDITIONAL
  677. #undef SSH1_BITMAP_WORD
  678. /* ----------------------------------------------------------------------
  679. * Function to check a host key against any manually configured in Conf.
  680. */
  681. int verify_ssh_manual_host_key(
  682. Conf *conf, const char *fingerprint, ssh_key *key)
  683. {
  684. if (!conf_get_str_nthstrkey(conf, CONF_ssh_manual_hostkeys, 0))
  685. return -1; /* no manual keys configured */
  686. if (fingerprint) {
  687. /*
  688. * The fingerprint string we've been given will have things
  689. * like 'ssh-rsa 2048' at the front of it. Strip those off and
  690. * narrow down to just the colon-separated hex block at the
  691. * end of the string.
  692. */
  693. const char *p = strrchr(fingerprint, ' ');
  694. fingerprint = p ? p+1 : fingerprint;
  695. /* Quick sanity checks, including making sure it's in lowercase */
  696. assert(strlen(fingerprint) == 16*3 - 1);
  697. assert(fingerprint[2] == ':');
  698. assert(fingerprint[strspn(fingerprint, "0123456789abcdef:")] == 0);
  699. if (conf_get_str_str_opt(conf, CONF_ssh_manual_hostkeys, fingerprint))
  700. return 1; /* success */
  701. }
  702. if (key) {
  703. /*
  704. * Construct the base64-encoded public key blob and see if
  705. * that's listed.
  706. */
  707. strbuf *binblob;
  708. char *base64blob;
  709. int atoms, i;
  710. binblob = strbuf_new();
  711. ssh_key_public_blob(key, BinarySink_UPCAST(binblob));
  712. atoms = (binblob->len + 2) / 3;
  713. base64blob = snewn(atoms * 4 + 1, char);
  714. for (i = 0; i < atoms; i++)
  715. base64_encode_atom(binblob->u + 3*i,
  716. binblob->len - 3*i, base64blob + 4*i);
  717. base64blob[atoms * 4] = '\0';
  718. strbuf_free(binblob);
  719. if (conf_get_str_str_opt(conf, CONF_ssh_manual_hostkeys, base64blob)) {
  720. sfree(base64blob);
  721. return 1; /* success */
  722. }
  723. sfree(base64blob);
  724. }
  725. return 0;
  726. }
  727. /* ----------------------------------------------------------------------
  728. * Common get_specials function for the two SSH-1 layers.
  729. */
  730. int ssh1_common_get_specials(
  731. PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx)
  732. {
  733. /*
  734. * Don't bother offering IGNORE if we've decided the remote
  735. * won't cope with it, since we wouldn't bother sending it if
  736. * asked anyway.
  737. */
  738. if (!(ppl->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) {
  739. add_special(ctx, "IGNORE message", SS_NOP, 0);
  740. return TRUE;
  741. }
  742. return FALSE;
  743. }
  744. /* ----------------------------------------------------------------------
  745. * Other miscellaneous utility functions.
  746. */
  747. void free_rportfwd(struct ssh_rportfwd *rpf)
  748. {
  749. if (rpf) {
  750. sfree(rpf->log_description);
  751. sfree(rpf->shost);
  752. sfree(rpf->dhost);
  753. sfree(rpf);
  754. }
  755. }