ssh.c 41 KB

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