ssh.c 39 KB

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