ssh.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. /*
  2. * SSH backend.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stdarg.h>
  7. #include <assert.h>
  8. #include <limits.h>
  9. #include <signal.h>
  10. #include "putty.h"
  11. #include "pageant.h" /* for AGENT_MAX_MSGLEN */
  12. #include "tree234.h"
  13. #include "storage.h"
  14. #include "marshal.h"
  15. #include "ssh.h"
  16. #include "sshcr.h"
  17. #include "sshbpp.h"
  18. #include "sshppl.h"
  19. #include "sshchan.h"
  20. #ifndef NO_GSSAPI
  21. #include "sshgssc.h"
  22. #include "sshgss.h"
  23. #define MIN_CTXT_LIFETIME 5 /* Avoid rekey with short lifetime (seconds) */
  24. #define GSS_KEX_CAPABLE (1<<0) /* Can do GSS KEX */
  25. #define GSS_CRED_UPDATED (1<<1) /* Cred updated since previous delegation */
  26. #define GSS_CTXT_EXPIRES (1<<2) /* Context expires before next timer */
  27. #define GSS_CTXT_MAYFAIL (1<<3) /* Context may expire during handshake */
  28. #endif
  29. struct Ssh {
  30. Socket *s;
  31. Seat *seat;
  32. Conf *conf;
  33. struct ssh_version_receiver version_receiver;
  34. int remote_bugs;
  35. Plug plug;
  36. Backend backend;
  37. Ldisc *ldisc;
  38. LogContext *logctx;
  39. /* The last list returned from get_specials. */
  40. SessionSpecial *specials;
  41. bool bare_connection;
  42. ssh_sharing_state *connshare;
  43. bool attempting_connshare;
  44. #ifndef NO_GSSAPI
  45. struct ssh_connection_shared_gss_state gss_state;
  46. #endif
  47. char *savedhost;
  48. int savedport;
  49. char *fullhostname;
  50. bool fallback_cmd;
  51. int exitcode;
  52. int version;
  53. int conn_throttle_count;
  54. size_t overall_bufsize;
  55. bool throttled_all;
  56. /*
  57. * logically_frozen is true if we're not currently _processing_
  58. * data from the SSH socket (e.g. because a higher layer has asked
  59. * us not to due to ssh_throttle_conn). socket_frozen is true if
  60. * we're not even _reading_ data from the socket (i.e. it should
  61. * always match the value we last passed to sk_set_frozen).
  62. *
  63. * The two differ in that socket_frozen can also become
  64. * temporarily true because of a large backlog in the in_raw
  65. * bufchain, to force no further plug_receive events until the BPP
  66. * input function has had a chance to run. (Some front ends, like
  67. * GTK, can persistently call the network and never get round to
  68. * the toplevel callbacks.) If we've stopped reading from the
  69. * socket for that reason, we absolutely _do_ want to carry on
  70. * processing our input bufchain, because that's the only way
  71. * it'll ever get cleared!
  72. *
  73. * ssh_check_frozen() resets socket_frozen, and should be called
  74. * whenever either of logically_frozen and the bufchain size
  75. * changes.
  76. */
  77. bool logically_frozen, socket_frozen;
  78. /* in case we find these out before we have a ConnectionLayer to tell */
  79. int term_width, term_height;
  80. bufchain in_raw, out_raw, user_input;
  81. bool pending_close;
  82. IdempotentCallback ic_out_raw;
  83. PacketLogSettings pls;
  84. struct DataTransferStats stats;
  85. BinaryPacketProtocol *bpp;
  86. /*
  87. * base_layer identifies the bottommost packet protocol layer, the
  88. * one connected directly to the BPP's packet queues. Any
  89. * operation that needs to talk to all layers (e.g. free, or
  90. * get_specials) will do it by talking to this, which will
  91. * recursively propagate it if necessary.
  92. */
  93. PacketProtocolLayer *base_layer;
  94. /*
  95. * The ConnectionLayer vtable from our connection layer.
  96. */
  97. ConnectionLayer *cl;
  98. /*
  99. * A dummy ConnectionLayer that can be used for logging sharing
  100. * downstreams that connect before the real one is ready.
  101. */
  102. ConnectionLayer cl_dummy;
  103. /*
  104. * session_started is false until we initialise the main protocol
  105. * layers. So it distinguishes between base_layer==NULL meaning
  106. * that the SSH protocol hasn't been set up _yet_, and
  107. * base_layer==NULL meaning the SSH protocol has run and finished.
  108. * It's also used to mark the point where we stop counting proxy
  109. * command diagnostics as pre-session-startup.
  110. */
  111. bool session_started;
  112. Pinger *pinger;
  113. char *deferred_abort_message;
  114. bool need_random_unref;
  115. };
  116. #define ssh_logevent(params) ( \
  117. logevent_and_free((ssh)->logctx, dupprintf params))
  118. static void ssh_shutdown(Ssh *ssh);
  119. static void ssh_throttle_all(Ssh *ssh, bool enable, size_t bufsize);
  120. static void ssh_bpp_output_raw_data_callback(void *vctx);
  121. LogContext *ssh_get_logctx(Ssh *ssh)
  122. {
  123. return ssh->logctx;
  124. }
  125. static void ssh_connect_bpp(Ssh *ssh)
  126. {
  127. ssh->bpp->ssh = ssh;
  128. ssh->bpp->in_raw = &ssh->in_raw;
  129. ssh->bpp->out_raw = &ssh->out_raw;
  130. bufchain_set_callback(ssh->bpp->out_raw, &ssh->ic_out_raw);
  131. ssh->bpp->pls = &ssh->pls;
  132. ssh->bpp->logctx = ssh->logctx;
  133. ssh->bpp->remote_bugs = ssh->remote_bugs;
  134. }
  135. static void ssh_connect_ppl(Ssh *ssh, PacketProtocolLayer *ppl)
  136. {
  137. ppl->bpp = ssh->bpp;
  138. ppl->user_input = &ssh->user_input;
  139. ppl->seat = ssh->seat;
  140. ppl->ssh = ssh;
  141. ppl->logctx = ssh->logctx;
  142. ppl->remote_bugs = ssh->remote_bugs;
  143. }
  144. static void ssh_got_ssh_version(struct ssh_version_receiver *rcv,
  145. int major_version)
  146. {
  147. Ssh *ssh = container_of(rcv, Ssh, version_receiver);
  148. BinaryPacketProtocol *old_bpp;
  149. PacketProtocolLayer *connection_layer;
  150. ssh->session_started = true;
  151. /*
  152. * We don't support choosing a major protocol version dynamically,
  153. * so this should always be the same value we set up in
  154. * connect_to_host().
  155. */
  156. assert(ssh->version == major_version);
  157. assert(ssh->version == 2);
  158. old_bpp = ssh->bpp;
  159. ssh->remote_bugs = ssh_verstring_get_bugs(old_bpp);
  160. if (!ssh->bare_connection) {
  161. /*WINSCP if (ssh->version == 2)*/ {
  162. PacketProtocolLayer *userauth_layer, *transport_child_layer;
  163. /*
  164. * We use the 'simple' variant of the SSH protocol if
  165. * we're asked to, except not if we're also doing
  166. * connection-sharing (either tunnelling our packets over
  167. * an upstream or expecting to be tunnelled over
  168. * ourselves), since then the assumption that we have only
  169. * one channel to worry about is not true after all.
  170. */
  171. bool is_simple =
  172. (conf_get_bool(ssh->conf, CONF_ssh_simple) && !ssh->connshare);
  173. ssh->bpp = ssh2_bpp_new(ssh->logctx, &ssh->stats, false);
  174. ssh_connect_bpp(ssh);
  175. #ifndef NO_GSSAPI
  176. /* Load and pick the highest GSS library on the preference
  177. * list. */
  178. if (!ssh->gss_state.libs)
  179. ssh->gss_state.libs = ssh_gss_setup(ssh->conf, ssh->logctx); // WINSCP
  180. ssh->gss_state.lib = NULL;
  181. if (ssh->gss_state.libs->nlibraries > 0) {
  182. int i, j;
  183. for (i = 0; i < ngsslibs; i++) {
  184. int want_id = conf_get_int_int(ssh->conf,
  185. CONF_ssh_gsslist, i);
  186. for (j = 0; j < ssh->gss_state.libs->nlibraries; j++)
  187. if (ssh->gss_state.libs->libraries[j].id == want_id) {
  188. ssh->gss_state.lib =
  189. &ssh->gss_state.libs->libraries[j];
  190. goto got_gsslib; /* double break */
  191. }
  192. }
  193. got_gsslib:
  194. /*
  195. * We always expect to have found something in
  196. * the above loop: we only came here if there
  197. * was at least one viable GSS library, and the
  198. * preference list should always mention
  199. * everything and only change the order.
  200. */
  201. assert(ssh->gss_state.lib);
  202. }
  203. #endif
  204. connection_layer = ssh2_connection_new(
  205. ssh, ssh->connshare, is_simple, ssh->conf,
  206. ssh_verstring_get_remote(old_bpp), &ssh->cl);
  207. ssh_connect_ppl(ssh, connection_layer);
  208. if (conf_get_bool(ssh->conf, CONF_ssh_no_userauth)) {
  209. userauth_layer = NULL;
  210. transport_child_layer = connection_layer;
  211. } else {
  212. char *username = get_remote_username(ssh->conf);
  213. userauth_layer = ssh2_userauth_new(
  214. connection_layer, ssh->savedhost, ssh->fullhostname,
  215. conf_get_filename(ssh->conf, CONF_keyfile),
  216. conf_get_bool(ssh->conf, CONF_ssh_show_banner),
  217. conf_get_bool(ssh->conf, CONF_tryagent), username,
  218. conf_get_bool(ssh->conf, CONF_change_username),
  219. conf_get_bool(ssh->conf, CONF_try_ki_auth),
  220. #ifndef NO_GSSAPI
  221. conf_get_bool(ssh->conf, CONF_try_gssapi_auth),
  222. conf_get_bool(ssh->conf, CONF_try_gssapi_kex),
  223. conf_get_bool(ssh->conf, CONF_gssapifwd),
  224. &ssh->gss_state
  225. #else
  226. false,
  227. false,
  228. false,
  229. NULL
  230. #endif
  231. ,conf_get_str(ssh->conf, CONF_loghost),
  232. conf_get_bool(ssh->conf, CONF_change_password) // WINSCP
  233. );
  234. ssh_connect_ppl(ssh, userauth_layer);
  235. transport_child_layer = userauth_layer;
  236. sfree(username);
  237. }
  238. ssh->base_layer = ssh2_transport_new(
  239. ssh->conf, ssh->savedhost, ssh->savedport,
  240. ssh->fullhostname,
  241. ssh_verstring_get_local(old_bpp),
  242. ssh_verstring_get_remote(old_bpp),
  243. #ifndef NO_GSSAPI
  244. &ssh->gss_state,
  245. #else
  246. NULL,
  247. #endif
  248. &ssh->stats, transport_child_layer, NULL);
  249. ssh_connect_ppl(ssh, ssh->base_layer);
  250. if (userauth_layer)
  251. ssh2_userauth_set_transport_layer(userauth_layer,
  252. ssh->base_layer);
  253. } // WINSCP
  254. } else {
  255. ssh->bpp = ssh2_bare_bpp_new(ssh->logctx);
  256. ssh_connect_bpp(ssh);
  257. connection_layer = ssh2_connection_new(
  258. ssh, ssh->connshare, false, ssh->conf,
  259. ssh_verstring_get_remote(old_bpp), &ssh->cl);
  260. ssh_connect_ppl(ssh, connection_layer);
  261. ssh->base_layer = connection_layer;
  262. }
  263. /* Connect the base layer - whichever it is - to the BPP, and set
  264. * up its selfptr. */
  265. ssh->base_layer->selfptr = &ssh->base_layer;
  266. ssh_ppl_setup_queues(ssh->base_layer, &ssh->bpp->in_pq, &ssh->bpp->out_pq);
  267. seat_update_specials_menu(ssh->seat);
  268. ssh->pinger = pinger_new(ssh->conf, &ssh->backend);
  269. queue_idempotent_callback(&ssh->bpp->ic_in_raw);
  270. ssh_ppl_process_queue(ssh->base_layer);
  271. /* Pass in the initial terminal size, if we knew it already. */
  272. ssh_terminal_size(ssh->cl, ssh->term_width, ssh->term_height);
  273. ssh_bpp_free(old_bpp);
  274. }
  275. void ssh_check_frozen(Ssh *ssh)
  276. {
  277. if (!ssh->s)
  278. return;
  279. { // WINSCP
  280. bool prev_frozen = ssh->socket_frozen;
  281. ssh->socket_frozen = (ssh->logically_frozen ||
  282. bufchain_size(&ssh->in_raw) > SSH_MAX_BACKLOG);
  283. sk_set_frozen(ssh->s, ssh->socket_frozen);
  284. if (prev_frozen && !ssh->socket_frozen && ssh->bpp) {
  285. /*
  286. * If we've just unfrozen, process any SSH connection data
  287. * that was stashed in our queue while we were frozen.
  288. */
  289. queue_idempotent_callback(&ssh->bpp->ic_in_raw);
  290. }
  291. } // WINSCP
  292. }
  293. void ssh_conn_processed_data(Ssh *ssh)
  294. {
  295. ssh_check_frozen(ssh);
  296. }
  297. static void ssh_bpp_output_raw_data_callback(void *vctx)
  298. {
  299. Ssh *ssh = (Ssh *)vctx;
  300. if (!ssh->s)
  301. return;
  302. while (bufchain_size(&ssh->out_raw) > 0) {
  303. size_t backlog;
  304. ptrlen data = bufchain_prefix(&ssh->out_raw);
  305. if (ssh->logctx)
  306. log_packet(ssh->logctx, PKT_OUTGOING, -1, NULL, data.ptr, data.len,
  307. 0, NULL, NULL, 0, NULL);
  308. backlog = sk_write(ssh->s, data.ptr, data.len);
  309. bufchain_consume(&ssh->out_raw, data.len);
  310. if (backlog > SSH_MAX_BACKLOG) {
  311. ssh_throttle_all(ssh, true, backlog);
  312. return;
  313. }
  314. }
  315. ssh_check_frozen(ssh);
  316. if (ssh->pending_close) {
  317. sk_close(ssh->s);
  318. ssh->s = NULL;
  319. }
  320. }
  321. static void ssh_shutdown_internal(Ssh *ssh)
  322. {
  323. expire_timer_context(ssh);
  324. if (ssh->connshare) {
  325. sharestate_free(ssh->connshare);
  326. ssh->connshare = NULL;
  327. }
  328. if (ssh->pinger) {
  329. pinger_free(ssh->pinger);
  330. ssh->pinger = NULL;
  331. }
  332. /*
  333. * We only need to free the base PPL, which will free the others
  334. * (if any) transitively.
  335. */
  336. if (ssh->base_layer) {
  337. ssh_ppl_free(ssh->base_layer);
  338. ssh->base_layer = NULL;
  339. }
  340. ssh->cl = NULL;
  341. }
  342. static void ssh_shutdown(Ssh *ssh)
  343. {
  344. ssh_shutdown_internal(ssh);
  345. if (ssh->bpp) {
  346. ssh_bpp_free(ssh->bpp);
  347. ssh->bpp = NULL;
  348. }
  349. if (ssh->s) {
  350. sk_close(ssh->s);
  351. ssh->s = NULL;
  352. }
  353. bufchain_clear(&ssh->in_raw);
  354. bufchain_clear(&ssh->out_raw);
  355. bufchain_clear(&ssh->user_input);
  356. }
  357. static void ssh_initiate_connection_close(Ssh *ssh)
  358. {
  359. /* Wind up everything above the BPP. */
  360. ssh_shutdown_internal(ssh);
  361. /* Force any remaining queued SSH packets through the BPP, and
  362. * schedule closing the network socket after they go out. */
  363. ssh_bpp_handle_output(ssh->bpp);
  364. ssh->pending_close = true;
  365. queue_idempotent_callback(&ssh->ic_out_raw);
  366. /* Now we expect the other end to close the connection too in
  367. * response, so arrange that we'll receive notification of that
  368. * via ssh_remote_eof. */
  369. ssh->bpp->expect_close = true;
  370. }
  371. #define GET_FORMATTED_MSG \
  372. char *msg; \
  373. va_list ap; \
  374. va_start(ap, fmt); \
  375. msg = dupvprintf(fmt, ap); \
  376. va_end(ap); \
  377. ((void)0) /* eat trailing semicolon */
  378. void ssh_remote_error(Ssh *ssh, const char *fmt, ...)
  379. {
  380. if (ssh->base_layer || !ssh->session_started) {
  381. GET_FORMATTED_MSG;
  382. /* Error messages sent by the remote don't count as clean exits */
  383. ssh->exitcode = 128;
  384. /* Close the socket immediately, since the server has already
  385. * closed its end (or is about to). */
  386. ssh_shutdown(ssh);
  387. logevent(ssh->logctx, msg);
  388. seat_connection_fatal(ssh->seat, "%s", msg);
  389. sfree(msg);
  390. }
  391. }
  392. void ssh_remote_eof(Ssh *ssh, const char *fmt, ...)
  393. {
  394. if (ssh->base_layer || !ssh->session_started) {
  395. GET_FORMATTED_MSG;
  396. /* EOF from the remote, if we were expecting it, does count as
  397. * a clean exit */
  398. ssh->exitcode = 0;
  399. /* Close the socket immediately, since the server has already
  400. * closed its end. */
  401. ssh_shutdown(ssh);
  402. logevent(ssh->logctx, msg);
  403. sfree(msg);
  404. seat_notify_remote_exit(ssh->seat);
  405. } else {
  406. /* This is responding to EOF after we've already seen some
  407. * other reason for terminating the session. */
  408. ssh_shutdown(ssh);
  409. }
  410. }
  411. void ssh_proto_error(Ssh *ssh, const char *fmt, ...)
  412. {
  413. if (ssh->base_layer || !ssh->session_started) {
  414. GET_FORMATTED_MSG;
  415. ssh->exitcode = 128;
  416. ssh_bpp_queue_disconnect(ssh->bpp, msg,
  417. SSH2_DISCONNECT_PROTOCOL_ERROR);
  418. ssh_initiate_connection_close(ssh);
  419. logevent(ssh->logctx, msg);
  420. seat_connection_fatal(ssh->seat, "%s", msg);
  421. sfree(msg);
  422. }
  423. }
  424. void ssh_sw_abort(Ssh *ssh, const char *fmt, ...)
  425. {
  426. if (ssh->base_layer || !ssh->session_started) {
  427. GET_FORMATTED_MSG;
  428. ssh->exitcode = 128;
  429. ssh_initiate_connection_close(ssh);
  430. logevent(ssh->logctx, msg);
  431. seat_connection_fatal(ssh->seat, "%s", msg);
  432. sfree(msg);
  433. seat_notify_remote_exit(ssh->seat);
  434. }
  435. }
  436. void ssh_user_close(Ssh *ssh, const char *fmt, ...)
  437. {
  438. if (ssh->base_layer || !ssh->session_started) {
  439. GET_FORMATTED_MSG;
  440. /* Closing the connection due to user action, even if the
  441. * action is the user aborting during authentication prompts,
  442. * does count as a clean exit - except that this is also how
  443. * we signal ordinary session termination, in which case we
  444. * should use the exit status already sent from the main
  445. * session (if any). */
  446. if (ssh->exitcode < 0)
  447. ssh->exitcode = 0;
  448. ssh_initiate_connection_close(ssh);
  449. logevent(ssh->logctx, msg);
  450. sfree(msg);
  451. seat_notify_remote_exit(ssh->seat);
  452. }
  453. }
  454. static void ssh_deferred_abort_callback(void *vctx)
  455. {
  456. Ssh *ssh = (Ssh *)vctx;
  457. char *msg = ssh->deferred_abort_message;
  458. ssh->deferred_abort_message = NULL;
  459. ssh_sw_abort(ssh, "%s", msg);
  460. sfree(msg);
  461. }
  462. void ssh_sw_abort_deferred(Ssh *ssh, const char *fmt, ...)
  463. {
  464. if (!ssh->deferred_abort_message) {
  465. GET_FORMATTED_MSG;
  466. ssh->deferred_abort_message = msg;
  467. queue_toplevel_callback(get_seat_callback_set(ssh->seat), ssh_deferred_abort_callback, ssh);
  468. }
  469. }
  470. static void ssh_socket_log(Plug *plug, PlugLogType type, SockAddr *addr,
  471. int port, const char *error_msg, int error_code)
  472. {
  473. Ssh *ssh = container_of(plug, Ssh, plug);
  474. /*
  475. * While we're attempting connection sharing, don't loudly log
  476. * everything that happens. Real TCP connections need to be logged
  477. * when we _start_ trying to connect, because it might be ages
  478. * before they respond if something goes wrong; but connection
  479. * sharing is local and quick to respond, and it's sufficient to
  480. * simply wait and see whether it worked afterwards.
  481. */
  482. if (!ssh->attempting_connshare)
  483. backend_socket_log(ssh->seat, ssh->logctx, type, addr, port,
  484. error_msg, error_code, ssh->conf,
  485. ssh->session_started);
  486. }
  487. static void ssh_closing(Plug *plug, const char *error_msg, int error_code,
  488. bool calling_back)
  489. {
  490. Ssh *ssh = container_of(plug, Ssh, plug);
  491. if (error_msg) {
  492. ssh_remote_error(ssh, "%s", error_msg);
  493. } else if (ssh->bpp) {
  494. ssh->bpp->input_eof = true;
  495. queue_idempotent_callback(&ssh->bpp->ic_in_raw);
  496. }
  497. }
  498. static void ssh_receive(Plug *plug, int urgent, const char *data, size_t len)
  499. {
  500. Ssh *ssh = container_of(plug, Ssh, plug);
  501. /* Log raw data, if we're in that mode. */
  502. if (ssh->logctx)
  503. log_packet(ssh->logctx, PKT_INCOMING, -1, NULL, data, len,
  504. 0, NULL, NULL, 0, NULL);
  505. bufchain_add(&ssh->in_raw, data, len);
  506. if (!ssh->logically_frozen && ssh->bpp)
  507. queue_idempotent_callback(&ssh->bpp->ic_in_raw);
  508. ssh_check_frozen(ssh);
  509. }
  510. static void ssh_sent(Plug *plug, size_t bufsize)
  511. {
  512. Ssh *ssh = container_of(plug, Ssh, plug);
  513. /*
  514. * If the send backlog on the SSH socket itself clears, we should
  515. * unthrottle the whole world if it was throttled. Also trigger an
  516. * extra call to the consumer of the BPP's output, to try to send
  517. * some more data off its bufchain.
  518. */
  519. if (bufsize < SSH_MAX_BACKLOG) {
  520. ssh_throttle_all(ssh, false, bufsize);
  521. queue_idempotent_callback(&ssh->ic_out_raw);
  522. }
  523. }
  524. static void ssh_hostport_setup(const char *host, int port, Conf *conf,
  525. char **savedhost, int *savedport,
  526. char **loghost_ret)
  527. {
  528. char *loghost = conf_get_str(conf, CONF_loghost);
  529. if (loghost_ret)
  530. *loghost_ret = loghost;
  531. if (*loghost) {
  532. char *tmphost;
  533. char *colon;
  534. tmphost = dupstr(loghost);
  535. *savedport = 22; /* default ssh port */
  536. /*
  537. * A colon suffix on the hostname string also lets us affect
  538. * savedport. (Unless there are multiple colons, in which case
  539. * we assume this is an unbracketed IPv6 literal.)
  540. */
  541. colon = host_strrchr(tmphost, ':');
  542. if (colon && colon == host_strchr(tmphost, ':')) {
  543. *colon++ = '\0';
  544. if (*colon)
  545. *savedport = atoi(colon);
  546. }
  547. *savedhost = host_strduptrim(tmphost);
  548. sfree(tmphost);
  549. } else {
  550. *savedhost = host_strduptrim(host);
  551. if (port < 0)
  552. port = 22; /* default ssh port */
  553. *savedport = port;
  554. }
  555. }
  556. static bool ssh_test_for_upstream(const char *host, int port, Conf *conf)
  557. {
  558. char *savedhost;
  559. int savedport;
  560. bool ret;
  561. random_ref(); /* platform may need this to determine share socket name */
  562. ssh_hostport_setup(host, port, conf, &savedhost, &savedport, NULL);
  563. ret = ssh_share_test_for_upstream(savedhost, savedport, conf);
  564. sfree(savedhost);
  565. random_unref();
  566. return ret;
  567. }
  568. static char *ssh_close_warn_text(Backend *be)
  569. {
  570. Ssh *ssh = container_of(be, Ssh, backend);
  571. if (!ssh->connshare)
  572. return NULL;
  573. { // WINSCP
  574. int ndowns = share_ndownstreams(ssh->connshare);
  575. if (ndowns == 0)
  576. return NULL;
  577. { // WINSCP
  578. char *msg = dupprintf("This will also close %d downstream connection%s.",
  579. ndowns, ndowns==1 ? "" : "s");
  580. return msg;
  581. } // WINSCP
  582. } // WINSCP
  583. }
  584. static const PlugVtable Ssh_plugvt = {
  585. // WINSCP
  586. /*.log =*/ ssh_socket_log,
  587. /*.closing =*/ ssh_closing,
  588. /*.receive =*/ ssh_receive,
  589. /*.sent =*/ ssh_sent,
  590. // NULL
  591. };
  592. /*
  593. * Connect to specified host and port.
  594. * Returns an error message, or NULL on success.
  595. * Also places the canonical host name into `realhost'. It must be
  596. * freed by the caller.
  597. */
  598. static char *connect_to_host(
  599. Ssh *ssh, const char *host, int port, char **realhost,
  600. bool nodelay, bool keepalive)
  601. {
  602. SockAddr *addr;
  603. const char *err;
  604. char *loghost;
  605. int addressfamily;
  606. ssh_hostport_setup(host, port, ssh->conf,
  607. &ssh->savedhost, &ssh->savedport, &loghost);
  608. ssh->plug.vt = &Ssh_plugvt;
  609. #ifdef MPEXT
  610. // make sure the field is initialized, in case lookup below fails
  611. ssh->fullhostname = NULL;
  612. #endif
  613. /*
  614. * Try connection-sharing, in case that means we don't open a
  615. * socket after all. ssh_connection_sharing_init will connect to a
  616. * previously established upstream if it can, and failing that,
  617. * establish a listening socket for _us_ to be the upstream. In
  618. * the latter case it will return NULL just as if it had done
  619. * nothing, because here we only need to care if we're a
  620. * downstream and need to do our connection setup differently.
  621. */
  622. ssh->connshare = NULL;
  623. ssh->attempting_connshare = true; /* affects socket logging behaviour */
  624. ssh->s = ssh_connection_sharing_init(
  625. ssh->savedhost, ssh->savedport, ssh->conf, ssh->logctx,
  626. &ssh->plug, &ssh->connshare);
  627. if (ssh->connshare)
  628. ssh_connshare_provide_connlayer(ssh->connshare, &ssh->cl_dummy);
  629. ssh->attempting_connshare = false;
  630. if (ssh->s != NULL) {
  631. /*
  632. * We are a downstream.
  633. */
  634. ssh->bare_connection = true;
  635. ssh->fullhostname = NULL;
  636. *realhost = dupstr(host); /* best we can do */
  637. if (seat_verbose(ssh->seat) || seat_interactive(ssh->seat)) {
  638. /* In an interactive session, or in verbose mode, announce
  639. * in the console window that we're a sharing downstream,
  640. * to avoid confusing users as to why this session doesn't
  641. * behave in quite the usual way. */
  642. const char *msg =
  643. "Reusing a shared connection to this server.\r\n";
  644. seat_stderr_pl(ssh->seat, ptrlen_from_asciz(msg));
  645. }
  646. } else {
  647. /*
  648. * We're not a downstream, so open a normal socket.
  649. */
  650. /*
  651. * Try to find host.
  652. */
  653. addressfamily = conf_get_int(ssh->conf, CONF_addressfamily);
  654. addr = name_lookup(host, port, realhost, ssh->conf, addressfamily,
  655. ssh->logctx, "SSH connection");
  656. if ((err = sk_addr_error(addr)) != NULL) {
  657. sk_addr_free(addr);
  658. return dupstr(err);
  659. }
  660. ssh->fullhostname = dupstr(*realhost); /* save in case of GSSAPI */
  661. ssh->s = new_connection(addr, *realhost, port,
  662. false, true, nodelay, keepalive,
  663. &ssh->plug, ssh->conf);
  664. if ((err = sk_socket_error(ssh->s)) != NULL) {
  665. ssh->s = NULL;
  666. seat_notify_remote_exit(ssh->seat);
  667. return dupstr(err);
  668. }
  669. }
  670. /*
  671. * The SSH version number is always fixed (since we no longer support
  672. * fallback between versions), so set it now.
  673. */
  674. /* SSH-2 only */
  675. ssh->version = 2; // WINSCP
  676. /*
  677. * Set up the initial BPP that will do the version string
  678. * exchange, and get it started so that it can send the outgoing
  679. * version string early if it wants to.
  680. */
  681. ssh->version_receiver.got_ssh_version = ssh_got_ssh_version;
  682. ssh->bpp = ssh_verstring_new(
  683. ssh->conf, ssh->logctx, ssh->bare_connection,
  684. ssh->version == 1 ? "1.5" : "2.0", &ssh->version_receiver,
  685. false, ""); // WINSCP (impl_name provided in sshver already)
  686. ssh_connect_bpp(ssh);
  687. queue_idempotent_callback(&ssh->bpp->ic_in_raw);
  688. /*
  689. * loghost, if configured, overrides realhost.
  690. */
  691. if (*loghost) {
  692. sfree(*realhost);
  693. *realhost = dupstr(loghost);
  694. }
  695. return NULL;
  696. }
  697. /*
  698. * Throttle or unthrottle the SSH connection.
  699. */
  700. void ssh_throttle_conn(Ssh *ssh, int adjust)
  701. {
  702. int old_count = ssh->conn_throttle_count;
  703. bool frozen;
  704. ssh->conn_throttle_count += adjust;
  705. assert(ssh->conn_throttle_count >= 0);
  706. if (ssh->conn_throttle_count && !old_count) {
  707. frozen = true;
  708. } else if (!ssh->conn_throttle_count && old_count) {
  709. frozen = false;
  710. } else {
  711. return; /* don't change current frozen state */
  712. }
  713. ssh->logically_frozen = frozen;
  714. ssh_check_frozen(ssh);
  715. }
  716. /*
  717. * Throttle or unthrottle _all_ local data streams (for when sends
  718. * on the SSH connection itself back up).
  719. */
  720. static void ssh_throttle_all(Ssh *ssh, bool enable, size_t bufsize)
  721. {
  722. if (enable == ssh->throttled_all)
  723. return;
  724. ssh->throttled_all = enable;
  725. ssh->overall_bufsize = bufsize;
  726. ssh_throttle_all_channels(ssh->cl, enable);
  727. }
  728. static void ssh_cache_conf_values(Ssh *ssh)
  729. {
  730. ssh->pls.omit_passwords = conf_get_bool(ssh->conf, CONF_logomitpass);
  731. ssh->pls.omit_data = conf_get_bool(ssh->conf, CONF_logomitdata);
  732. }
  733. bool ssh_is_bare(Ssh *ssh)
  734. {
  735. return ssh->backend.vt->protocol == PROT_SSHCONN;
  736. }
  737. /* Dummy connlayer must provide ssh_sharing_no_more_downstreams,
  738. * because it might be called early due to plink -shareexists */
  739. static void dummy_sharing_no_more_downstreams(ConnectionLayer *cl) {}
  740. static const ConnectionLayerVtable dummy_connlayer_vtable = {
  741. // WINSCP
  742. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  743. /*.sharing_no_more_downstreams =*/ dummy_sharing_no_more_downstreams,
  744. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
  745. };
  746. /*
  747. * Called to set up the connection.
  748. *
  749. * Returns an error message, or NULL on success.
  750. */
  751. static char *ssh_init(const BackendVtable *vt, Seat *seat,
  752. Backend **backend_handle, LogContext *logctx,
  753. Conf *conf, const char *host, int port,
  754. char **realhost, bool nodelay, bool keepalive)
  755. {
  756. Ssh *ssh;
  757. ssh = snew(Ssh);
  758. memset(ssh, 0, sizeof(Ssh));
  759. ssh->conf = conf_copy(conf);
  760. ssh_cache_conf_values(ssh);
  761. ssh->exitcode = -1;
  762. ssh->pls.kctx = SSH2_PKTCTX_NOKEX;
  763. ssh->pls.actx = SSH2_PKTCTX_NOAUTH;
  764. bufchain_init(&ssh->in_raw);
  765. bufchain_init(&ssh->out_raw);
  766. bufchain_init(&ssh->user_input);
  767. ssh->ic_out_raw.fn = ssh_bpp_output_raw_data_callback;
  768. ssh->ic_out_raw.ctx = ssh;
  769. ssh->ic_out_raw.set = get_seat_callback_set(seat);
  770. ssh->term_width = conf_get_int(ssh->conf, CONF_width);
  771. ssh->term_height = conf_get_int(ssh->conf, CONF_height);
  772. ssh->backend.vt = vt;
  773. *backend_handle = &ssh->backend;
  774. ssh->bare_connection = (vt->protocol == PROT_SSHCONN);
  775. ssh->seat = seat;
  776. ssh->cl_dummy.vt = &dummy_connlayer_vtable;
  777. ssh->cl_dummy.logctx = ssh->logctx = logctx;
  778. random_ref(); /* do this now - may be needed by sharing setup code */
  779. ssh->need_random_unref = true;
  780. { // WINSCP
  781. char *conn_err = connect_to_host(
  782. ssh, host, port, realhost, nodelay, keepalive);
  783. if (conn_err) {
  784. /* Call random_unref now instead of waiting until the caller
  785. * frees this useless Ssh object, in case the caller is
  786. * impatient and just exits without bothering, in which case
  787. * the random seed won't be re-saved. */
  788. ssh->need_random_unref = false;
  789. random_unref();
  790. return conn_err;
  791. }
  792. } // WINSCP
  793. return NULL;
  794. }
  795. static void ssh_free(Backend *be)
  796. {
  797. Ssh *ssh = container_of(be, Ssh, backend);
  798. bool need_random_unref;
  799. ssh_shutdown(ssh);
  800. conf_free(ssh->conf);
  801. if (ssh->connshare)
  802. sharestate_free(ssh->connshare);
  803. sfree(ssh->savedhost);
  804. sfree(ssh->fullhostname);
  805. sfree(ssh->specials);
  806. #ifndef NO_GSSAPI
  807. if (ssh->gss_state.srv_name)
  808. ssh->gss_state.lib->release_name(
  809. ssh->gss_state.lib, &ssh->gss_state.srv_name);
  810. if (ssh->gss_state.ctx != NULL)
  811. ssh->gss_state.lib->release_cred(
  812. ssh->gss_state.lib, &ssh->gss_state.ctx);
  813. if (ssh->gss_state.libs)
  814. ssh_gss_cleanup(ssh->gss_state.libs);
  815. #endif
  816. sfree(ssh->deferred_abort_message);
  817. delete_callbacks_for_context(get_seat_callback_set(ssh->seat), ssh); /* likely to catch ic_out_raw */ // WINSCP (seat)
  818. need_random_unref = ssh->need_random_unref;
  819. sfree(ssh);
  820. if (need_random_unref)
  821. random_unref();
  822. }
  823. /*
  824. * Reconfigure the SSH backend.
  825. */
  826. static void ssh_reconfig(Backend *be, Conf *conf)
  827. {
  828. Ssh *ssh = container_of(be, Ssh, backend);
  829. if (ssh->pinger)
  830. pinger_reconfig(ssh->pinger, ssh->conf, conf);
  831. ssh_ppl_reconfigure(ssh->base_layer, conf);
  832. conf_free(ssh->conf);
  833. ssh->conf = conf_copy(conf);
  834. ssh_cache_conf_values(ssh);
  835. }
  836. /*
  837. * Called to send data down the SSH connection.
  838. */
  839. static size_t ssh_send(Backend *be, const char *buf, size_t len)
  840. {
  841. Ssh *ssh = container_of(be, Ssh, backend);
  842. if (ssh == NULL || ssh->s == NULL)
  843. return 0;
  844. bufchain_add(&ssh->user_input, buf, len);
  845. if (ssh->base_layer)
  846. ssh_ppl_got_user_input(ssh->base_layer);
  847. return backend_sendbuffer(&ssh->backend);
  848. }
  849. /*
  850. * Called to query the current amount of buffered stdin data.
  851. */
  852. static size_t ssh_sendbuffer(Backend *be)
  853. {
  854. Ssh *ssh = container_of(be, Ssh, backend);
  855. size_t backlog;
  856. if (!ssh || !ssh->s || !ssh->cl)
  857. return 0;
  858. backlog = ssh_stdin_backlog(ssh->cl);
  859. #ifndef WINSCP
  860. // This throttles WinSCP unnecessarily, as it uses its own throttling mechanism.
  861. // This value is used only by WinSCP, never directly by PuTTY code.
  862. if (ssh->base_layer)
  863. backlog += ssh_ppl_queued_data_size(ssh->base_layer);
  864. #endif
  865. /*
  866. * If the SSH socket itself has backed up, add the total backup
  867. * size on that to any individual buffer on the stdin channel.
  868. */
  869. if (ssh->throttled_all)
  870. backlog += ssh->overall_bufsize;
  871. return backlog;
  872. }
  873. /*
  874. * Called to set the size of the window from SSH's POV.
  875. */
  876. static void ssh_size(Backend *be, int width, int height)
  877. {
  878. Ssh *ssh = container_of(be, Ssh, backend);
  879. ssh->term_width = width;
  880. ssh->term_height = height;
  881. if (ssh->cl)
  882. ssh_terminal_size(ssh->cl, ssh->term_width, ssh->term_height);
  883. }
  884. struct ssh_add_special_ctx {
  885. SessionSpecial *specials;
  886. size_t nspecials, specials_size;
  887. };
  888. static void ssh_add_special(void *vctx, const char *text,
  889. SessionSpecialCode code, int arg)
  890. {
  891. struct ssh_add_special_ctx *ctx = (struct ssh_add_special_ctx *)vctx;
  892. SessionSpecial *spec;
  893. sgrowarray(ctx->specials, ctx->specials_size, ctx->nspecials);
  894. spec = &ctx->specials[ctx->nspecials++];
  895. spec->name = text;
  896. spec->code = code;
  897. spec->arg = arg;
  898. }
  899. /*
  900. * Return a list of the special codes that make sense in this
  901. * protocol.
  902. */
  903. static const SessionSpecial *ssh_get_specials(Backend *be)
  904. {
  905. Ssh *ssh = container_of(be, Ssh, backend);
  906. /*
  907. * Ask all our active protocol layers what specials they've got,
  908. * and amalgamate the list into one combined one.
  909. */
  910. struct ssh_add_special_ctx ctx[1];
  911. ctx->specials = NULL;
  912. ctx->nspecials = ctx->specials_size = 0;
  913. if (ssh->base_layer)
  914. ssh_ppl_get_specials(ssh->base_layer, ssh_add_special, ctx);
  915. if (ctx->specials) {
  916. /* If the list is non-empty, terminate it with a SS_EXITMENU. */
  917. ssh_add_special(ctx, NULL, SS_EXITMENU, 0);
  918. }
  919. sfree(ssh->specials);
  920. ssh->specials = ctx->specials;
  921. return ssh->specials;
  922. }
  923. /*
  924. * Send special codes.
  925. */
  926. static void ssh_special(Backend *be, SessionSpecialCode code, int arg)
  927. {
  928. Ssh *ssh = container_of(be, Ssh, backend);
  929. if (ssh->base_layer)
  930. ssh_ppl_special_cmd(ssh->base_layer, code, arg);
  931. }
  932. /*
  933. * This is called when the seat's output channel manages to clear some
  934. * backlog.
  935. */
  936. static void ssh_unthrottle(Backend *be, size_t bufsize)
  937. {
  938. Ssh *ssh = container_of(be, Ssh, backend);
  939. if (ssh->cl)
  940. ssh_stdout_unthrottle(ssh->cl, bufsize);
  941. }
  942. static bool ssh_connected(Backend *be)
  943. {
  944. Ssh *ssh = container_of(be, Ssh, backend);
  945. return ssh->s != NULL;
  946. }
  947. static bool ssh_sendok(Backend *be)
  948. {
  949. Ssh *ssh = container_of(be, Ssh, backend);
  950. return ssh->base_layer && ssh_ppl_want_user_input(ssh->base_layer);
  951. }
  952. void ssh_ldisc_update(Ssh *ssh)
  953. {
  954. /* Called when the connection layer wants to propagate an update
  955. * to the line discipline options */
  956. if (ssh->ldisc)
  957. ldisc_echoedit_update(ssh->ldisc);
  958. }
  959. static bool ssh_ldisc(Backend *be, int option)
  960. {
  961. Ssh *ssh = container_of(be, Ssh, backend);
  962. return ssh->cl ? ssh_ldisc_option(ssh->cl, option) : false;
  963. }
  964. static void ssh_provide_ldisc(Backend *be, Ldisc *ldisc)
  965. {
  966. Ssh *ssh = container_of(be, Ssh, backend);
  967. ssh->ldisc = ldisc;
  968. }
  969. void ssh_got_exitcode(Ssh *ssh, int exitcode)
  970. {
  971. ssh->exitcode = exitcode;
  972. }
  973. static int ssh_return_exitcode(Backend *be)
  974. {
  975. Ssh *ssh = container_of(be, Ssh, backend);
  976. if (ssh->s && (!ssh->session_started || ssh->base_layer))
  977. return -1;
  978. else
  979. return (ssh->exitcode >= 0 ? ssh->exitcode : INT_MAX);
  980. }
  981. /*
  982. * cfg_info for SSH is the protocol running in this session.
  983. * (1 or 2 for the full SSH-1 or SSH-2 protocol; -1 for the bare
  984. * SSH-2 connection protocol, i.e. a downstream; 0 for not-decided-yet.)
  985. */
  986. static int ssh_cfg_info(Backend *be)
  987. {
  988. Ssh *ssh = container_of(be, Ssh, backend);
  989. if (ssh->version == 0)
  990. return 0; /* don't know yet */
  991. else if (ssh->bare_connection)
  992. return -1;
  993. else
  994. return ssh->version;
  995. }
  996. /*
  997. * Gross hack: pscp will try to start SFTP but fall back to scp1 if
  998. * that fails. This variable is the means by which scp.c can reach
  999. * into the SSH code and find out which one it got.
  1000. */
  1001. extern bool ssh_fallback_cmd(Backend *be)
  1002. {
  1003. Ssh *ssh = container_of(be, Ssh, backend);
  1004. return ssh->fallback_cmd;
  1005. }
  1006. void ssh_got_fallback_cmd(Ssh *ssh)
  1007. {
  1008. ssh->fallback_cmd = true;
  1009. }
  1010. const BackendVtable ssh_backend = {
  1011. // WINSCP
  1012. /*.init =*/ ssh_init,
  1013. /*.free =*/ ssh_free,
  1014. /*.reconfig =*/ ssh_reconfig,
  1015. /*.send =*/ ssh_send,
  1016. /*.sendbuffer =*/ ssh_sendbuffer,
  1017. /*.size =*/ ssh_size,
  1018. /*.special =*/ ssh_special,
  1019. /*.get_specials =*/ ssh_get_specials,
  1020. /*.connected =*/ ssh_connected,
  1021. /*.exitcode =*/ ssh_return_exitcode,
  1022. /*.sendok =*/ ssh_sendok,
  1023. /*.ldisc_option_state =*/ ssh_ldisc,
  1024. /*.provide_ldisc =*/ ssh_provide_ldisc,
  1025. /*.unthrottle =*/ ssh_unthrottle,
  1026. /*.cfg_info =*/ ssh_cfg_info,
  1027. /*.test_for_upstream =*/ ssh_test_for_upstream,
  1028. /*.close_warn_text =*/ ssh_close_warn_text,
  1029. /*.id =*/ "ssh",
  1030. /*.displayname =*/ "SSH",
  1031. /*.protocol =*/ PROT_SSH,
  1032. /*.default_port =*/ 22,
  1033. 0, 0, 0, // WINSCP
  1034. };
  1035. const BackendVtable sshconn_backend = {
  1036. // WINSCP
  1037. /*.init =*/ ssh_init,
  1038. /*.free =*/ ssh_free,
  1039. /*.reconfig =*/ ssh_reconfig,
  1040. /*.send =*/ ssh_send,
  1041. /*.sendbuffer =*/ ssh_sendbuffer,
  1042. /*.size =*/ ssh_size,
  1043. /*.special =*/ ssh_special,
  1044. /*.get_specials =*/ ssh_get_specials,
  1045. /*.connected =*/ ssh_connected,
  1046. /*.exitcode =*/ ssh_return_exitcode,
  1047. /*.sendok =*/ ssh_sendok,
  1048. /*.ldisc_option_state =*/ ssh_ldisc,
  1049. /*.provide_ldisc =*/ ssh_provide_ldisc,
  1050. /*.unthrottle =*/ ssh_unthrottle,
  1051. /*.cfg_info =*/ ssh_cfg_info,
  1052. /*.test_for_upstream =*/ ssh_test_for_upstream,
  1053. /*.close_warn_text =*/ ssh_close_warn_text,
  1054. /*.id =*/ "ssh-connection",
  1055. /*.displayname =*/ "Bare ssh-connection",
  1056. /*.protocol =*/ PROT_SSHCONN,
  1057. 0, 0, 0, 0, // WINSCP
  1058. };
  1059. #ifdef MPEXT
  1060. #include "puttyexp.h"
  1061. int is_ssh(Plug * plug)
  1062. {
  1063. return plug->vt->closing == ssh_closing;
  1064. }
  1065. int get_ssh_version(Backend * be)
  1066. {
  1067. Ssh * ssh = container_of(be, Ssh, backend);
  1068. return ssh->version;
  1069. }
  1070. Seat * get_ssh_seat(Plug * plug)
  1071. {
  1072. return container_of(plug, Ssh, plug)->seat;
  1073. }
  1074. const ssh_cipher * get_cscipher(Backend * be)
  1075. {
  1076. Ssh * ssh = container_of(be, Ssh, backend);
  1077. return ssh2_bpp_get_cscipher(ssh->bpp);
  1078. }
  1079. const ssh_cipher * get_sccipher(Backend * be)
  1080. {
  1081. Ssh * ssh = container_of(be, Ssh, backend);
  1082. return ssh2_bpp_get_sccipher(ssh->bpp);
  1083. }
  1084. const struct ssh_compressor * get_cscomp(Backend * be)
  1085. {
  1086. Ssh * ssh = container_of(be, Ssh, backend);
  1087. return ssh2_bpp_get_cscomp(ssh->bpp);
  1088. }
  1089. const struct ssh_decompressor * get_sccomp(Backend * be)
  1090. {
  1091. Ssh * ssh = container_of(be, Ssh, backend);
  1092. return ssh2_bpp_get_sccomp(ssh->bpp);
  1093. }
  1094. unsigned int winscp_query(Backend * be, int query)
  1095. {
  1096. Ssh * ssh = container_of(be, Ssh, backend);
  1097. if ((ssh->base_layer != NULL) && (ssh->base_layer->vt->winscp_query != NULL))
  1098. {
  1099. return ssh_ppl_winscp_query(ssh->base_layer, query);
  1100. }
  1101. else
  1102. {
  1103. return 0;
  1104. }
  1105. }
  1106. void md5checksum(const char * buffer, int len, unsigned char output[16])
  1107. {
  1108. hash_simple(&ssh_md5, make_ptrlen(buffer, len), output);
  1109. }
  1110. #endif