sshcommon.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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_frontend_callback_set(pqb->frontend);
  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, Frontend * frontend) // WINSCP
  114. {
  115. pq->pqb.ic = NULL;
  116. pq->pqb.frontend = frontend;
  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, Frontend * frontend) // WINSCP
  121. {
  122. pq->pqb.ic = NULL;
  123. pq->pqb.frontend = frontend;
  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. };
  259. Channel *zombiechan_new(void)
  260. {
  261. Channel *chan = snew(Channel);
  262. chan->vt = &zombiechan_channelvt;
  263. chan->initial_fixed_window_size = 0;
  264. return chan;
  265. }
  266. static void zombiechan_free(Channel *chan)
  267. {
  268. assert(chan->vt == &zombiechan_channelvt);
  269. sfree(chan);
  270. }
  271. static void zombiechan_do_nothing(Channel *chan)
  272. {
  273. assert(chan->vt == &zombiechan_channelvt);
  274. }
  275. static void zombiechan_open_failure(Channel *chan, const char *errtext)
  276. {
  277. assert(chan->vt == &zombiechan_channelvt);
  278. }
  279. static int zombiechan_send(Channel *chan, int is_stderr,
  280. const void *data, int length)
  281. {
  282. assert(chan->vt == &zombiechan_channelvt);
  283. return 0;
  284. }
  285. static void zombiechan_set_input_wanted(Channel *chan, int enable)
  286. {
  287. assert(chan->vt == &zombiechan_channelvt);
  288. }
  289. static int zombiechan_want_close(Channel *chan, int sent_eof, int rcvd_eof)
  290. {
  291. return TRUE;
  292. }
  293. /* ----------------------------------------------------------------------
  294. * Centralised standard methods for other channel implementations to
  295. * borrow.
  296. */
  297. void chan_remotely_opened_confirmation(Channel *chan)
  298. {
  299. assert(0 && "this channel type should never receive OPEN_CONFIRMATION");
  300. }
  301. void chan_remotely_opened_failure(Channel *chan, const char *errtext)
  302. {
  303. assert(0 && "this channel type should never receive OPEN_FAILURE");
  304. }
  305. int chan_no_eager_close(Channel *chan, int sent_local_eof, int rcvd_remote_eof)
  306. {
  307. return FALSE; /* default: never proactively ask for a close */
  308. }
  309. /* ----------------------------------------------------------------------
  310. * Common routine to marshal tty modes into an SSH packet.
  311. */
  312. void write_ttymodes_to_packet_from_conf(
  313. BinarySink *bs, Seat *seat, Conf *conf,
  314. int ssh_version, int ospeed, int ispeed)
  315. {
  316. int i;
  317. /*
  318. * Codes for terminal modes.
  319. * Most of these are the same in SSH-1 and SSH-2.
  320. * This list is derived from RFC 4254 and
  321. * SSH-1 RFC-1.2.31.
  322. */
  323. static const struct ssh_ttymode {
  324. const char *mode;
  325. int opcode;
  326. enum { TTY_OP_CHAR, TTY_OP_BOOL } type;
  327. } ssh_ttymodes[] = {
  328. /* "V" prefix discarded for special characters relative to SSH specs */
  329. { "INTR", 1, TTY_OP_CHAR },
  330. { "QUIT", 2, TTY_OP_CHAR },
  331. { "ERASE", 3, TTY_OP_CHAR },
  332. { "KILL", 4, TTY_OP_CHAR },
  333. { "EOF", 5, TTY_OP_CHAR },
  334. { "EOL", 6, TTY_OP_CHAR },
  335. { "EOL2", 7, TTY_OP_CHAR },
  336. { "START", 8, TTY_OP_CHAR },
  337. { "STOP", 9, TTY_OP_CHAR },
  338. { "SUSP", 10, TTY_OP_CHAR },
  339. { "DSUSP", 11, TTY_OP_CHAR },
  340. { "REPRINT", 12, TTY_OP_CHAR },
  341. { "WERASE", 13, TTY_OP_CHAR },
  342. { "LNEXT", 14, TTY_OP_CHAR },
  343. { "FLUSH", 15, TTY_OP_CHAR },
  344. { "SWTCH", 16, TTY_OP_CHAR },
  345. { "STATUS", 17, TTY_OP_CHAR },
  346. { "DISCARD", 18, TTY_OP_CHAR },
  347. { "IGNPAR", 30, TTY_OP_BOOL },
  348. { "PARMRK", 31, TTY_OP_BOOL },
  349. { "INPCK", 32, TTY_OP_BOOL },
  350. { "ISTRIP", 33, TTY_OP_BOOL },
  351. { "INLCR", 34, TTY_OP_BOOL },
  352. { "IGNCR", 35, TTY_OP_BOOL },
  353. { "ICRNL", 36, TTY_OP_BOOL },
  354. { "IUCLC", 37, TTY_OP_BOOL },
  355. { "IXON", 38, TTY_OP_BOOL },
  356. { "IXANY", 39, TTY_OP_BOOL },
  357. { "IXOFF", 40, TTY_OP_BOOL },
  358. { "IMAXBEL", 41, TTY_OP_BOOL },
  359. { "IUTF8", 42, TTY_OP_BOOL },
  360. { "ISIG", 50, TTY_OP_BOOL },
  361. { "ICANON", 51, TTY_OP_BOOL },
  362. { "XCASE", 52, TTY_OP_BOOL },
  363. { "ECHO", 53, TTY_OP_BOOL },
  364. { "ECHOE", 54, TTY_OP_BOOL },
  365. { "ECHOK", 55, TTY_OP_BOOL },
  366. { "ECHONL", 56, TTY_OP_BOOL },
  367. { "NOFLSH", 57, TTY_OP_BOOL },
  368. { "TOSTOP", 58, TTY_OP_BOOL },
  369. { "IEXTEN", 59, TTY_OP_BOOL },
  370. { "ECHOCTL", 60, TTY_OP_BOOL },
  371. { "ECHOKE", 61, TTY_OP_BOOL },
  372. { "PENDIN", 62, TTY_OP_BOOL }, /* XXX is this a real mode? */
  373. { "OPOST", 70, TTY_OP_BOOL },
  374. { "OLCUC", 71, TTY_OP_BOOL },
  375. { "ONLCR", 72, TTY_OP_BOOL },
  376. { "OCRNL", 73, TTY_OP_BOOL },
  377. { "ONOCR", 74, TTY_OP_BOOL },
  378. { "ONLRET", 75, TTY_OP_BOOL },
  379. { "CS7", 90, TTY_OP_BOOL },
  380. { "CS8", 91, TTY_OP_BOOL },
  381. { "PARENB", 92, TTY_OP_BOOL },
  382. { "PARODD", 93, TTY_OP_BOOL }
  383. };
  384. /* Miscellaneous other tty-related constants. */
  385. enum {
  386. /* The opcodes for ISPEED/OSPEED differ between SSH-1 and SSH-2. */
  387. SSH1_TTY_OP_ISPEED = 192,
  388. SSH1_TTY_OP_OSPEED = 193,
  389. SSH2_TTY_OP_ISPEED = 128,
  390. SSH2_TTY_OP_OSPEED = 129,
  391. SSH_TTY_OP_END = 0
  392. };
  393. for (i = 0; i < lenof(ssh_ttymodes); i++) {
  394. const struct ssh_ttymode *mode = ssh_ttymodes + i;
  395. const char *sval = conf_get_str_str(conf, CONF_ttymodes, mode->mode);
  396. char *to_free = NULL;
  397. /* Every mode known to the current version of the code should be
  398. * mentioned; this was ensured when settings were loaded. */
  399. /*
  400. * sval[0] can be
  401. * - 'V', indicating that an explicit value follows it;
  402. * - 'A', indicating that we should pass the value through from
  403. * the local environment via get_ttymode; or
  404. * - 'N', indicating that we should explicitly not send this
  405. * mode.
  406. */
  407. if (sval[0] == 'A') {
  408. sval = to_free = seat_get_ttymode(seat, mode->mode);
  409. } else if (sval[0] == 'V') {
  410. sval++; /* skip the 'V' */
  411. } else {
  412. /* else 'N', or something from the future we don't understand */
  413. continue;
  414. }
  415. if (sval) {
  416. /*
  417. * Parse the string representation of the tty mode
  418. * into the integer value it will take on the wire.
  419. */
  420. unsigned ival = 0;
  421. switch (mode->type) {
  422. case TTY_OP_CHAR:
  423. if (*sval) {
  424. char *next = NULL;
  425. /* We know ctrlparse won't write to the string, so
  426. * casting away const is ugly but allowable. */
  427. ival = ctrlparse((char *)sval, &next);
  428. if (!next)
  429. ival = sval[0];
  430. } else {
  431. ival = 255; /* special value meaning "don't set" */
  432. }
  433. break;
  434. case TTY_OP_BOOL:
  435. if (stricmp(sval, "yes") == 0 ||
  436. stricmp(sval, "on") == 0 ||
  437. stricmp(sval, "true") == 0 ||
  438. stricmp(sval, "+") == 0)
  439. ival = 1; /* true */
  440. else if (stricmp(sval, "no") == 0 ||
  441. stricmp(sval, "off") == 0 ||
  442. stricmp(sval, "false") == 0 ||
  443. stricmp(sval, "-") == 0)
  444. ival = 0; /* false */
  445. else
  446. ival = (atoi(sval) != 0);
  447. break;
  448. default:
  449. assert(0 && "Bad mode->type");
  450. }
  451. /*
  452. * And write it into the output packet. The parameter
  453. * value is formatted as a byte in SSH-1, but a uint32
  454. * in SSH-2.
  455. */
  456. put_byte(bs, mode->opcode);
  457. if (ssh_version == 1)
  458. put_byte(bs, ival);
  459. else
  460. put_uint32(bs, ival);
  461. }
  462. sfree(to_free);
  463. }
  464. /*
  465. * Finish off with the terminal speeds (which are formatted as
  466. * uint32 in both protocol versions) and the end marker.
  467. */
  468. put_byte(bs, ssh_version == 1 ? SSH1_TTY_OP_ISPEED : SSH2_TTY_OP_ISPEED);
  469. put_uint32(bs, ispeed);
  470. put_byte(bs, ssh_version == 1 ? SSH1_TTY_OP_OSPEED : SSH2_TTY_OP_OSPEED);
  471. put_uint32(bs, ospeed);
  472. put_byte(bs, SSH_TTY_OP_END);
  473. }
  474. /* ----------------------------------------------------------------------
  475. * Routine for allocating a new channel ID, given a means of finding
  476. * the index field in a given channel structure.
  477. */
  478. unsigned alloc_channel_id_general(tree234 *channels, size_t localid_offset)
  479. {
  480. const unsigned CHANNEL_NUMBER_OFFSET = 256;
  481. search234_state ss;
  482. /*
  483. * First-fit allocation of channel numbers: we always pick the
  484. * lowest unused one.
  485. *
  486. * Every channel before that, and no channel after it, has an ID
  487. * exactly equal to its tree index plus CHANNEL_NUMBER_OFFSET. So
  488. * we can use the search234 system to identify the length of that
  489. * initial sequence, in a single log-time pass down the channels
  490. * tree.
  491. */
  492. search234_start(&ss, channels);
  493. while (ss.element) {
  494. unsigned localid = *(unsigned *)((char *)ss.element + localid_offset);
  495. if (localid == ss.index + CHANNEL_NUMBER_OFFSET)
  496. search234_step(&ss, +1);
  497. else
  498. search234_step(&ss, -1);
  499. }
  500. /*
  501. * Now ss.index gives exactly the number of channels in that
  502. * initial sequence. So adding CHANNEL_NUMBER_OFFSET to it must
  503. * give precisely the lowest unused channel number.
  504. */
  505. return ss.index + CHANNEL_NUMBER_OFFSET;
  506. }
  507. /* ----------------------------------------------------------------------
  508. * Functions for handling the comma-separated strings used to store
  509. * lists of protocol identifiers in SSH-2.
  510. */
  511. int first_in_commasep_string(char const *needle, char const *haystack,
  512. int haylen)
  513. {
  514. int needlen;
  515. if (!needle || !haystack) /* protect against null pointers */
  516. return 0;
  517. needlen = strlen(needle);
  518. if (haylen >= needlen && /* haystack is long enough */
  519. !memcmp(needle, haystack, needlen) && /* initial match */
  520. (haylen == needlen || haystack[needlen] == ',')
  521. /* either , or EOS follows */
  522. )
  523. return 1;
  524. return 0;
  525. }
  526. int in_commasep_string(char const *needle, char const *haystack, int haylen)
  527. {
  528. char *p;
  529. if (!needle || !haystack) /* protect against null pointers */
  530. return FALSE;
  531. /*
  532. * Is it at the start of the string?
  533. */
  534. if (first_in_commasep_string(needle, haystack, haylen))
  535. return TRUE;
  536. /*
  537. * If not, search for the next comma and resume after that.
  538. * If no comma found, terminate.
  539. */
  540. p = memchr(haystack, ',', haylen);
  541. if (!p)
  542. return FALSE;
  543. /* + 1 to skip over comma */
  544. return in_commasep_string(needle, p + 1, haylen - (p + 1 - haystack));
  545. }
  546. void add_to_commasep(strbuf *buf, const char *data)
  547. {
  548. if (buf->len > 0)
  549. put_byte(buf, ',');
  550. put_data(buf, data, strlen(data));
  551. }
  552. /* ----------------------------------------------------------------------
  553. * Functions for translating SSH packet type codes into their symbolic
  554. * string names.
  555. */
  556. #define TRANSLATE_UNIVERSAL(y, name, value) \
  557. if (type == value) return #name;
  558. #define TRANSLATE_KEX(y, name, value, ctx) \
  559. if (type == value && pkt_kctx == ctx) return #name;
  560. #define TRANSLATE_AUTH(y, name, value, ctx) \
  561. if (type == value && pkt_actx == ctx) return #name;
  562. const char *ssh1_pkt_type(int type)
  563. {
  564. SSH1_MESSAGE_TYPES(TRANSLATE_UNIVERSAL, y);
  565. return "unknown";
  566. }
  567. const char *ssh2_pkt_type(Pkt_KCtx pkt_kctx, Pkt_ACtx pkt_actx, int type)
  568. {
  569. SSH2_MESSAGE_TYPES(TRANSLATE_UNIVERSAL, TRANSLATE_KEX, TRANSLATE_AUTH, y);
  570. return "unknown";
  571. }
  572. #undef TRANSLATE_UNIVERSAL
  573. #undef TRANSLATE_KEX
  574. #undef TRANSLATE_AUTH
  575. /* ----------------------------------------------------------------------
  576. * Common helper function for clients and implementations of
  577. * PacketProtocolLayer.
  578. */
  579. void ssh_ppl_replace(PacketProtocolLayer *old, PacketProtocolLayer *new)
  580. {
  581. new->bpp = old->bpp;
  582. ssh_ppl_setup_queues(new, old->in_pq, old->out_pq);
  583. new->selfptr = old->selfptr;
  584. new->user_input = old->user_input;
  585. new->seat = old->seat;
  586. new->ssh = old->ssh;
  587. *new->selfptr = new;
  588. ssh_ppl_free(old);
  589. /* The new layer might need to be the first one that sends a
  590. * packet, so trigger a call to its main coroutine immediately. If
  591. * it doesn't need to go first, the worst that will do is return
  592. * straight away. */
  593. queue_idempotent_callback(&new->ic_process_queue);
  594. }
  595. void ssh_ppl_free(PacketProtocolLayer *ppl)
  596. {
  597. delete_callbacks_for_context(get_frontend_callback_set(ppl->frontend), ppl); // WINSCP
  598. ppl->vt->free(ppl);
  599. }
  600. static void ssh_ppl_ic_process_queue_callback(void *context)
  601. {
  602. PacketProtocolLayer *ppl = (PacketProtocolLayer *)context;
  603. ssh_ppl_process_queue(ppl);
  604. }
  605. void ssh_ppl_setup_queues(PacketProtocolLayer *ppl,
  606. PktInQueue *inq, PktOutQueue *outq)
  607. {
  608. ppl->in_pq = inq;
  609. ppl->out_pq = outq;
  610. ppl->in_pq->pqb.ic = &ppl->ic_process_queue;
  611. ppl->ic_process_queue.fn = ssh_ppl_ic_process_queue_callback;
  612. ppl->ic_process_queue.ctx = ppl;
  613. ppl->ic_process_queue.set = get_frontend_callback_set(ppl->frontend);
  614. /* If there's already something on the input queue, it will want
  615. * handling immediately. */
  616. if (pq_peek(ppl->in_pq))
  617. queue_idempotent_callback(&ppl->ic_process_queue);
  618. }
  619. void ssh_ppl_user_output_string_and_free(PacketProtocolLayer *ppl, char *text)
  620. {
  621. /* Messages sent via this function are from the SSH layer, not
  622. * from the server-side process, so they always have the stderr
  623. * flag set. */
  624. seat_stderr(ppl->seat, text, strlen(text));
  625. sfree(text);
  626. }
  627. /* ----------------------------------------------------------------------
  628. * Common helper functions for clients and implementations of
  629. * BinaryPacketProtocol.
  630. */
  631. static void ssh_bpp_input_raw_data_callback(void *context)
  632. {
  633. BinaryPacketProtocol *bpp = (BinaryPacketProtocol *)context;
  634. ssh_bpp_handle_input(bpp);
  635. }
  636. static void ssh_bpp_output_packet_callback(void *context)
  637. {
  638. BinaryPacketProtocol *bpp = (BinaryPacketProtocol *)context;
  639. ssh_bpp_handle_output(bpp);
  640. }
  641. void ssh_bpp_common_setup(BinaryPacketProtocol *bpp)
  642. {
  643. pq_in_init(&bpp->in_pq, log_get_frontend(bpp->logctx)); // WINSCP
  644. pq_out_init(&bpp->out_pq, log_get_frontend(bpp->logctx)); // WINSCP
  645. bpp->input_eof = FALSE;
  646. bpp->ic_in_raw.fn = ssh_bpp_input_raw_data_callback;
  647. bpp->ic_in_raw.set = get_frontend_callback_set(log_get_frontend(bpp->logctx));
  648. bpp->ic_in_raw.ctx = bpp;
  649. bpp->ic_out_pq.fn = ssh_bpp_output_packet_callback;
  650. bpp->ic_out_pq.set = get_frontend_callback_set(log_get_frontend(bpp->logctx));
  651. bpp->ic_out_pq.ctx = bpp;
  652. bpp->out_pq.pqb.ic = &bpp->ic_out_pq;
  653. }
  654. void ssh_bpp_free(BinaryPacketProtocol *bpp)
  655. {
  656. // WINSCP
  657. delete_callbacks_for_context(get_frontend_callback_set(log_get_frontend(bpp->logctx)), bpp);
  658. bpp->vt->free(bpp);
  659. }
  660. void ssh2_bpp_queue_disconnect(BinaryPacketProtocol *bpp,
  661. const char *msg, int category)
  662. {
  663. PktOut *pkt = ssh_bpp_new_pktout(bpp, SSH2_MSG_DISCONNECT);
  664. put_uint32(pkt, category);
  665. put_stringz(pkt, msg);
  666. put_stringz(pkt, "en"); /* language tag */
  667. pq_push(&bpp->out_pq, pkt);
  668. }
  669. #define BITMAP_UNIVERSAL(y, name, value) \
  670. | (value >= y && value < y+32 ? 1UL << (value-y) : 0)
  671. #define BITMAP_CONDITIONAL(y, name, value, ctx) \
  672. BITMAP_UNIVERSAL(y, name, value)
  673. #define SSH2_BITMAP_WORD(y) \
  674. (0 SSH2_MESSAGE_TYPES(BITMAP_UNIVERSAL, BITMAP_CONDITIONAL, \
  675. BITMAP_CONDITIONAL, (32*y)))
  676. int ssh2_bpp_check_unimplemented(BinaryPacketProtocol *bpp, PktIn *pktin)
  677. {
  678. #pragma warn -osh
  679. static const unsigned valid_bitmap[] = {
  680. SSH2_BITMAP_WORD(0),
  681. SSH2_BITMAP_WORD(1),
  682. SSH2_BITMAP_WORD(2),
  683. SSH2_BITMAP_WORD(3),
  684. SSH2_BITMAP_WORD(4),
  685. SSH2_BITMAP_WORD(5),
  686. SSH2_BITMAP_WORD(6),
  687. SSH2_BITMAP_WORD(7),
  688. };
  689. #pragma warn +osh
  690. if (pktin->type < 0x100 &&
  691. !((valid_bitmap[pktin->type >> 5] >> (pktin->type & 0x1F)) & 1)) {
  692. PktOut *pkt = ssh_bpp_new_pktout(bpp, SSH2_MSG_UNIMPLEMENTED);
  693. put_uint32(pkt, pktin->sequence);
  694. pq_push(&bpp->out_pq, pkt);
  695. return TRUE;
  696. }
  697. return FALSE;
  698. }
  699. #undef BITMAP_UNIVERSAL
  700. #undef BITMAP_CONDITIONAL
  701. #undef SSH1_BITMAP_WORD
  702. /* ----------------------------------------------------------------------
  703. * Function to check a host key against any manually configured in Conf.
  704. */
  705. int verify_ssh_manual_host_key(
  706. Conf *conf, const char *fingerprint, ssh_key *key)
  707. {
  708. if (!conf_get_str_nthstrkey(conf, CONF_ssh_manual_hostkeys, 0))
  709. return -1; /* no manual keys configured */
  710. if (fingerprint) {
  711. /*
  712. * The fingerprint string we've been given will have things
  713. * like 'ssh-rsa 2048' at the front of it. Strip those off and
  714. * narrow down to just the colon-separated hex block at the
  715. * end of the string.
  716. */
  717. const char *p = strrchr(fingerprint, ' ');
  718. fingerprint = p ? p+1 : fingerprint;
  719. /* Quick sanity checks, including making sure it's in lowercase */
  720. assert(strlen(fingerprint) == 16*3 - 1);
  721. assert(fingerprint[2] == ':');
  722. assert(fingerprint[strspn(fingerprint, "0123456789abcdef:")] == 0);
  723. if (conf_get_str_str_opt(conf, CONF_ssh_manual_hostkeys, fingerprint))
  724. return 1; /* success */
  725. }
  726. if (key) {
  727. /*
  728. * Construct the base64-encoded public key blob and see if
  729. * that's listed.
  730. */
  731. strbuf *binblob;
  732. char *base64blob;
  733. int atoms, i;
  734. binblob = strbuf_new();
  735. ssh_key_public_blob(key, BinarySink_UPCAST(binblob));
  736. atoms = (binblob->len + 2) / 3;
  737. base64blob = snewn(atoms * 4 + 1, char);
  738. for (i = 0; i < atoms; i++)
  739. base64_encode_atom(binblob->u + 3*i,
  740. binblob->len - 3*i, base64blob + 4*i);
  741. base64blob[atoms * 4] = '\0';
  742. strbuf_free(binblob);
  743. if (conf_get_str_str_opt(conf, CONF_ssh_manual_hostkeys, base64blob)) {
  744. sfree(base64blob);
  745. return 1; /* success */
  746. }
  747. sfree(base64blob);
  748. }
  749. return 0;
  750. }
  751. /* ----------------------------------------------------------------------
  752. * Common get_specials function for the two SSH-1 layers.
  753. */
  754. int ssh1_common_get_specials(
  755. PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx)
  756. {
  757. /*
  758. * Don't bother offering IGNORE if we've decided the remote
  759. * won't cope with it, since we wouldn't bother sending it if
  760. * asked anyway.
  761. */
  762. if (!(ppl->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) {
  763. add_special(ctx, "IGNORE message", SS_NOP, 0);
  764. return TRUE;
  765. }
  766. return FALSE;
  767. }
  768. /* ----------------------------------------------------------------------
  769. * Other miscellaneous utility functions.
  770. */
  771. void free_rportfwd(struct ssh_rportfwd *rpf)
  772. {
  773. if (rpf) {
  774. sfree(rpf->log_description);
  775. sfree(rpf->shost);
  776. sfree(rpf->dhost);
  777. sfree(rpf);
  778. }
  779. }