sshcommon.c 25 KB

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