ssh.c 42 KB

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