sshcommon.c 28 KB

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