1
0

ssh.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  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, PlugLogType type, SockAddr *addr,
  491. int port, const char *error_msg, int error_code)
  492. {
  493. Ssh *ssh = container_of(plug, Ssh, plug);
  494. /*
  495. * While we're attempting connection sharing, don't loudly log
  496. * everything that happens. Real TCP connections need to be logged
  497. * when we _start_ trying to connect, because it might be ages
  498. * before they respond if something goes wrong; but connection
  499. * sharing is local and quick to respond, and it's sufficient to
  500. * simply wait and see whether it worked afterwards.
  501. */
  502. if (!ssh->attempting_connshare)
  503. backend_socket_log(ssh->seat, ssh->logctx, type, addr, port,
  504. error_msg, error_code, ssh->conf,
  505. ssh->session_started);
  506. }
  507. static void ssh_closing(Plug *plug, PlugCloseType type, const char *error_msg)
  508. {
  509. Ssh *ssh = container_of(plug, Ssh, plug);
  510. if (type == PLUGCLOSE_USER_ABORT) {
  511. ssh_user_close(ssh, "%s", error_msg);
  512. } else if (type != PLUGCLOSE_NORMAL) {
  513. ssh_remote_error(ssh, "%s", error_msg);
  514. } else if (ssh->bpp) {
  515. ssh->bpp->input_eof = true;
  516. queue_idempotent_callback(&ssh->bpp->ic_in_raw);
  517. }
  518. }
  519. static void ssh_receive(Plug *plug, int urgent, const char *data, size_t len)
  520. {
  521. Ssh *ssh = container_of(plug, Ssh, plug);
  522. /* Log raw data, if we're in that mode. */
  523. if (ssh->logctx)
  524. log_packet(ssh->logctx, PKT_INCOMING, -1, NULL, data, len,
  525. 0, NULL, NULL, 0, NULL);
  526. bufchain_add(&ssh->in_raw, data, len);
  527. if (!ssh->logically_frozen && ssh->bpp)
  528. queue_idempotent_callback(&ssh->bpp->ic_in_raw);
  529. ssh_check_frozen(ssh);
  530. }
  531. static void ssh_sent(Plug *plug, size_t bufsize)
  532. {
  533. Ssh *ssh = container_of(plug, Ssh, plug);
  534. /*
  535. * If the send backlog on the SSH socket itself clears, we should
  536. * unthrottle the whole world if it was throttled. Also trigger an
  537. * extra call to the consumer of the BPP's output, to try to send
  538. * some more data off its bufchain.
  539. */
  540. if (bufsize < SSH_MAX_BACKLOG) {
  541. ssh_throttle_all(ssh, false, bufsize);
  542. queue_idempotent_callback(&ssh->ic_out_raw);
  543. ssh_sendbuffer_changed(ssh);
  544. }
  545. }
  546. static void ssh_hostport_setup(const char *host, int port, Conf *conf,
  547. char **savedhost, int *savedport,
  548. char **loghost_ret)
  549. {
  550. char *loghost = conf_get_str(conf, CONF_loghost);
  551. if (loghost_ret)
  552. *loghost_ret = loghost;
  553. if (*loghost) {
  554. char *tmphost;
  555. char *colon;
  556. tmphost = dupstr(loghost);
  557. *savedport = 22; /* default ssh port */
  558. /*
  559. * A colon suffix on the hostname string also lets us affect
  560. * savedport. (Unless there are multiple colons, in which case
  561. * we assume this is an unbracketed IPv6 literal.)
  562. */
  563. colon = host_strrchr(tmphost, ':');
  564. if (colon && colon == host_strchr(tmphost, ':')) {
  565. *colon++ = '\0';
  566. if (*colon)
  567. *savedport = atoi(colon);
  568. }
  569. *savedhost = host_strduptrim(tmphost);
  570. sfree(tmphost);
  571. } else {
  572. *savedhost = host_strduptrim(host);
  573. if (port < 0)
  574. port = 22; /* default ssh port */
  575. *savedport = port;
  576. }
  577. }
  578. static bool ssh_test_for_upstream(const char *host, int port, Conf *conf)
  579. {
  580. char *savedhost;
  581. int savedport;
  582. bool ret;
  583. random_ref(); /* platform may need this to determine share socket name */
  584. ssh_hostport_setup(host, port, conf, &savedhost, &savedport, NULL);
  585. ret = ssh_share_test_for_upstream(savedhost, savedport, conf);
  586. sfree(savedhost);
  587. random_unref();
  588. return ret;
  589. }
  590. static char *ssh_close_warn_text(Backend *be)
  591. {
  592. Ssh *ssh = container_of(be, Ssh, backend);
  593. if (!ssh->connshare)
  594. return NULL;
  595. { // WINSCP
  596. int ndowns = share_ndownstreams(ssh->connshare);
  597. if (ndowns == 0)
  598. return NULL;
  599. { // WINSCP
  600. char *msg = dupprintf("This will also close %d downstream connection%s.",
  601. ndowns, ndowns==1 ? "" : "s");
  602. return msg;
  603. } // WINSCP
  604. } // WINSCP
  605. }
  606. static const PlugVtable Ssh_plugvt = {
  607. // WINSCP
  608. /*.log =*/ ssh_socket_log,
  609. /*.closing =*/ ssh_closing,
  610. /*.receive =*/ ssh_receive,
  611. /*.sent =*/ ssh_sent,
  612. // NULL
  613. };
  614. static char *ssh_description(Interactor *itr)
  615. {
  616. Ssh *ssh = container_of(itr, Ssh, interactor);
  617. return dupstr(ssh->description);
  618. }
  619. static LogPolicy *ssh_logpolicy(Interactor *itr)
  620. {
  621. Ssh *ssh = container_of(itr, Ssh, interactor);
  622. return log_get_policy(ssh->logctx);
  623. }
  624. static Seat *ssh_get_seat(Interactor *itr)
  625. {
  626. Ssh *ssh = container_of(itr, Ssh, interactor);
  627. return ssh->seat;
  628. }
  629. static void ssh_set_seat(Interactor *itr, Seat *seat)
  630. {
  631. Ssh *ssh = container_of(itr, Ssh, interactor);
  632. ssh->seat = seat;
  633. }
  634. static const InteractorVtable Ssh_interactorvt = {
  635. // WINSCP
  636. /*.description =*/ ssh_description,
  637. /*.logpolicy =*/ ssh_logpolicy,
  638. /*.get_seat =*/ ssh_get_seat,
  639. /*.set_seat =*/ ssh_set_seat,
  640. };
  641. /*
  642. * Connect to specified host and port.
  643. * Returns an error message, or NULL on success.
  644. * Also places the canonical host name into `realhost'. It must be
  645. * freed by the caller.
  646. */
  647. static char *connect_to_host(
  648. Ssh *ssh, const char *host, int port, char *loghost, char **realhost,
  649. bool nodelay, bool keepalive)
  650. {
  651. SockAddr *addr;
  652. const char *err;
  653. int addressfamily;
  654. ssh->plug.vt = &Ssh_plugvt;
  655. #ifdef MPEXT
  656. // make sure the field is initialized, in case lookup below fails
  657. ssh->fullhostname = NULL;
  658. #endif
  659. /*
  660. * Try connection-sharing, in case that means we don't open a
  661. * socket after all. ssh_connection_sharing_init will connect to a
  662. * previously established upstream if it can, and failing that,
  663. * establish a listening socket for _us_ to be the upstream. In
  664. * the latter case it will return NULL just as if it had done
  665. * nothing, because here we only need to care if we're a
  666. * downstream and need to do our connection setup differently.
  667. */
  668. ssh->connshare = NULL;
  669. ssh->attempting_connshare = true; /* affects socket logging behaviour */
  670. ssh->s = ssh_connection_sharing_init(
  671. ssh->savedhost, ssh->savedport, ssh->conf, ssh->logctx,
  672. &ssh->plug, &ssh->connshare);
  673. if (ssh->connshare)
  674. ssh_connshare_provide_connlayer(ssh->connshare, &ssh->cl_dummy);
  675. ssh->attempting_connshare = false;
  676. if (ssh->s != NULL) {
  677. /*
  678. * We are a downstream.
  679. */
  680. ssh->bare_connection = true;
  681. ssh->fullhostname = NULL;
  682. *realhost = dupstr(host); /* best we can do */
  683. if (seat_verbose(ssh->seat) || seat_interactive(ssh->seat)) {
  684. /* In an interactive session, or in verbose mode, announce
  685. * in the console window that we're a sharing downstream,
  686. * to avoid confusing users as to why this session doesn't
  687. * behave in quite the usual way. */
  688. const char *msg =
  689. "Reusing a shared connection to this server.\r\n";
  690. seat_stderr_pl(ssh->seat, ptrlen_from_asciz(msg));
  691. }
  692. } else {
  693. /*
  694. * We're not a downstream, so open a normal socket.
  695. */
  696. /*
  697. * Try to find host.
  698. */
  699. addressfamily = conf_get_int(ssh->conf, CONF_addressfamily);
  700. addr = name_lookup(host, port, realhost, ssh->conf, addressfamily,
  701. ssh->logctx, "SSH connection");
  702. if ((err = sk_addr_error(addr)) != NULL) {
  703. sk_addr_free(addr);
  704. return dupstr(err);
  705. }
  706. ssh->fullhostname = dupstr(*realhost); /* save in case of GSSAPI */
  707. ssh->s = new_connection(addr, *realhost, port,
  708. false, true, nodelay, keepalive,
  709. &ssh->plug, ssh->conf, &ssh->interactor);
  710. if ((err = sk_socket_error(ssh->s)) != NULL) {
  711. ssh->s = NULL;
  712. seat_notify_remote_exit(ssh->seat);
  713. seat_notify_remote_disconnect(ssh->seat);
  714. return dupstr(err);
  715. }
  716. }
  717. /*
  718. * The SSH version number is always fixed (since we no longer support
  719. * fallback between versions), so set it now.
  720. */
  721. /* SSH-2 only */
  722. ssh->version = 2; // WINSCP
  723. /*
  724. * Set up the initial BPP that will do the version string
  725. * exchange, and get it started so that it can send the outgoing
  726. * version string early if it wants to.
  727. */
  728. ssh->version_receiver.got_ssh_version = ssh_got_ssh_version;
  729. ssh->bpp = ssh_verstring_new(
  730. ssh->conf, ssh->logctx, ssh->bare_connection,
  731. ssh->version == 1 ? "1.5" : "2.0", &ssh->version_receiver,
  732. false, ""); // WINSCP (impl_name provided in sshver already)
  733. ssh_connect_bpp(ssh);
  734. queue_idempotent_callback(&ssh->bpp->ic_in_raw);
  735. /*
  736. * loghost, if configured, overrides realhost.
  737. */
  738. if (*loghost) {
  739. sfree(*realhost);
  740. *realhost = dupstr(loghost);
  741. }
  742. return NULL;
  743. }
  744. /*
  745. * Throttle or unthrottle the SSH connection.
  746. */
  747. void ssh_throttle_conn(Ssh *ssh, int adjust)
  748. {
  749. int old_count = ssh->conn_throttle_count;
  750. bool frozen;
  751. ssh->conn_throttle_count += adjust;
  752. assert(ssh->conn_throttle_count >= 0);
  753. if (ssh->conn_throttle_count && !old_count) {
  754. frozen = true;
  755. } else if (!ssh->conn_throttle_count && old_count) {
  756. frozen = false;
  757. } else {
  758. return; /* don't change current frozen state */
  759. }
  760. ssh->logically_frozen = frozen;
  761. ssh_check_frozen(ssh);
  762. }
  763. /*
  764. * Throttle or unthrottle _all_ local data streams (for when sends
  765. * on the SSH connection itself back up).
  766. */
  767. static void ssh_throttle_all(Ssh *ssh, bool enable, size_t bufsize)
  768. {
  769. if (enable == ssh->throttled_all)
  770. return;
  771. ssh->throttled_all = enable;
  772. ssh->overall_bufsize = bufsize;
  773. ssh_throttle_all_channels(ssh->cl, enable);
  774. }
  775. static void ssh_cache_conf_values(Ssh *ssh)
  776. {
  777. ssh->pls.omit_passwords = conf_get_bool(ssh->conf, CONF_logomitpass);
  778. ssh->pls.omit_data = conf_get_bool(ssh->conf, CONF_logomitdata);
  779. }
  780. bool ssh_is_bare(Ssh *ssh)
  781. {
  782. return ssh->backend.vt->protocol == PROT_SSHCONN;
  783. }
  784. /* Dummy connlayer must provide ssh_sharing_no_more_downstreams,
  785. * because it might be called early due to plink -shareexists */
  786. static void dummy_sharing_no_more_downstreams(ConnectionLayer *cl) {}
  787. static const ConnectionLayerVtable dummy_connlayer_vtable = {
  788. // WINSCP
  789. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  790. /*.sharing_no_more_downstreams =*/ dummy_sharing_no_more_downstreams,
  791. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
  792. };
  793. /*
  794. * Called to set up the connection.
  795. *
  796. * Returns an error message, or NULL on success.
  797. */
  798. static char *ssh_init(const BackendVtable *vt, Seat *seat,
  799. Backend **backend_handle, LogContext *logctx,
  800. Conf *conf, const char *host, int port,
  801. char **realhost, bool nodelay, bool keepalive)
  802. {
  803. Ssh *ssh;
  804. ssh = snew(Ssh);
  805. memset(ssh, 0, sizeof(Ssh));
  806. ssh->conf = conf_copy(conf);
  807. ssh_cache_conf_values(ssh);
  808. ssh->exitcode = -1;
  809. ssh->pls.kctx = SSH2_PKTCTX_NOKEX;
  810. ssh->pls.actx = SSH2_PKTCTX_NOAUTH;
  811. bufchain_init(&ssh->in_raw);
  812. bufchain_init(&ssh->out_raw);
  813. bufchain_init(&ssh->user_input);
  814. ssh->ic_out_raw.fn = ssh_bpp_output_raw_data_callback;
  815. ssh->ic_out_raw.ctx = ssh;
  816. ssh->ic_out_raw.set = get_seat_callback_set(seat);
  817. ssh->term_width = conf_get_int(ssh->conf, CONF_width);
  818. ssh->term_height = conf_get_int(ssh->conf, CONF_height);
  819. ssh->backend.vt = vt;
  820. ssh->interactor.vt = &Ssh_interactorvt;
  821. ssh->backend.interactor = &ssh->interactor;
  822. *backend_handle = &ssh->backend;
  823. ssh->bare_connection = (vt->protocol == PROT_SSHCONN);
  824. ssh->seat = seat;
  825. ssh->cl_dummy.vt = &dummy_connlayer_vtable;
  826. ssh->cl_dummy.logctx = ssh->logctx = logctx;
  827. { // WINSCP
  828. char *loghost;
  829. ssh_hostport_setup(host, port, ssh->conf,
  830. &ssh->savedhost, &ssh->savedport, &loghost);
  831. ssh->description = default_description(vt, ssh->savedhost, ssh->savedport);
  832. random_ref(); /* do this now - may be needed by sharing setup code */
  833. ssh->need_random_unref = true;
  834. { // WINSCP
  835. char *conn_err = connect_to_host(
  836. ssh, host, port, loghost, realhost, nodelay, keepalive);
  837. if (conn_err) {
  838. /* Call random_unref now instead of waiting until the caller
  839. * frees this useless Ssh object, in case the caller is
  840. * impatient and just exits without bothering, in which case
  841. * the random seed won't be re-saved. */
  842. ssh->need_random_unref = false;
  843. random_unref();
  844. return conn_err;
  845. }
  846. return NULL;
  847. } // WINSCP
  848. } // WINSCP
  849. }
  850. static void ssh_free(Backend *be)
  851. {
  852. Ssh *ssh = container_of(be, Ssh, backend);
  853. bool need_random_unref;
  854. ssh_shutdown(ssh);
  855. if (is_tempseat(ssh->seat))
  856. {
  857. Seat * realseat = tempseat_get_real(ssh->seat); // WINSCP
  858. tempseat_free(ssh->seat);
  859. ssh->seat = realseat; // WINSCP
  860. }
  861. conf_free(ssh->conf);
  862. if (ssh->connshare)
  863. sharestate_free(ssh->connshare);
  864. sfree(ssh->savedhost);
  865. sfree(ssh->fullhostname);
  866. sfree(ssh->specials);
  867. #ifndef NO_GSSAPI
  868. if (ssh->gss_state.srv_name)
  869. ssh->gss_state.lib->release_name(
  870. ssh->gss_state.lib, &ssh->gss_state.srv_name);
  871. if (ssh->gss_state.ctx != NULL)
  872. ssh->gss_state.lib->release_cred(
  873. ssh->gss_state.lib, &ssh->gss_state.ctx);
  874. if (ssh->gss_state.libs)
  875. ssh_gss_cleanup(ssh->gss_state.libs);
  876. #endif
  877. sfree(ssh->deferred_abort_message);
  878. sfree(ssh->description);
  879. delete_callbacks_for_context(get_seat_callback_set(ssh->seat), ssh); /* likely to catch ic_out_raw */ // WINSCP (seat)
  880. need_random_unref = ssh->need_random_unref;
  881. sfree(ssh);
  882. if (need_random_unref)
  883. random_unref();
  884. }
  885. /*
  886. * Reconfigure the SSH backend.
  887. */
  888. static void ssh_reconfig(Backend *be, Conf *conf)
  889. {
  890. Ssh *ssh = container_of(be, Ssh, backend);
  891. if (ssh->pinger)
  892. pinger_reconfig(ssh->pinger, ssh->conf, conf);
  893. if (ssh->base_layer)
  894. ssh_ppl_reconfigure(ssh->base_layer, conf);
  895. conf_free(ssh->conf);
  896. ssh->conf = conf_copy(conf);
  897. ssh_cache_conf_values(ssh);
  898. }
  899. /*
  900. * Called to send data down the SSH connection.
  901. */
  902. static void ssh_send(Backend *be, const char *buf, size_t len)
  903. {
  904. Ssh *ssh = container_of(be, Ssh, backend);
  905. if (ssh == NULL || ssh->s == NULL)
  906. return;
  907. bufchain_add(&ssh->user_input, buf, len);
  908. if (ssh->cl)
  909. ssh_got_user_input(ssh->cl);
  910. }
  911. /*
  912. * Called to query the current amount of buffered stdin data.
  913. */
  914. static size_t ssh_sendbuffer(Backend *be)
  915. {
  916. Ssh *ssh = container_of(be, Ssh, backend);
  917. size_t backlog;
  918. if (!ssh || !ssh->s || !ssh->cl)
  919. return 0;
  920. backlog = ssh_stdin_backlog(ssh->cl);
  921. #ifndef WINSCP
  922. // This throttles WinSCP unnecessarily, as it uses its own throttling mechanism.
  923. // This value is used only by WinSCP, never directly by PuTTY code.
  924. if (ssh->base_layer)
  925. backlog += ssh_ppl_queued_data_size(ssh->base_layer);
  926. #endif
  927. /*
  928. * If the SSH socket itself has backed up, add the total backup
  929. * size on that to any individual buffer on the stdin channel.
  930. */
  931. if (ssh->throttled_all)
  932. backlog += ssh->overall_bufsize;
  933. return backlog;
  934. }
  935. void ssh_sendbuffer_changed(Ssh *ssh)
  936. {
  937. seat_sent(ssh->seat, ssh_sendbuffer(&ssh->backend));
  938. }
  939. /*
  940. * Called to set the size of the window from SSH's POV.
  941. */
  942. static void ssh_size(Backend *be, int width, int height)
  943. {
  944. Ssh *ssh = container_of(be, Ssh, backend);
  945. ssh->term_width = width;
  946. ssh->term_height = height;
  947. if (ssh->cl)
  948. ssh_terminal_size(ssh->cl, ssh->term_width, ssh->term_height);
  949. }
  950. struct ssh_add_special_ctx {
  951. SessionSpecial *specials;
  952. size_t nspecials, specials_size;
  953. };
  954. static void ssh_add_special(void *vctx, const char *text,
  955. SessionSpecialCode code, int arg)
  956. {
  957. struct ssh_add_special_ctx *ctx = (struct ssh_add_special_ctx *)vctx;
  958. SessionSpecial *spec;
  959. sgrowarray(ctx->specials, ctx->specials_size, ctx->nspecials);
  960. spec = &ctx->specials[ctx->nspecials++];
  961. spec->name = text;
  962. spec->code = code;
  963. spec->arg = arg;
  964. }
  965. /*
  966. * Return a list of the special codes that make sense in this
  967. * protocol.
  968. */
  969. static const SessionSpecial *ssh_get_specials(Backend *be)
  970. {
  971. Ssh *ssh = container_of(be, Ssh, backend);
  972. /*
  973. * Ask all our active protocol layers what specials they've got,
  974. * and amalgamate the list into one combined one.
  975. */
  976. struct ssh_add_special_ctx ctx[1];
  977. ctx->specials = NULL;
  978. ctx->nspecials = ctx->specials_size = 0;
  979. if (ssh->base_layer)
  980. ssh_ppl_get_specials(ssh->base_layer, ssh_add_special, ctx);
  981. if (ctx->specials) {
  982. /* If the list is non-empty, terminate it with a SS_EXITMENU. */
  983. ssh_add_special(ctx, NULL, SS_EXITMENU, 0);
  984. }
  985. sfree(ssh->specials);
  986. ssh->specials = ctx->specials;
  987. return ssh->specials;
  988. }
  989. /*
  990. * Send special codes.
  991. */
  992. static void ssh_special(Backend *be, SessionSpecialCode code, int arg)
  993. {
  994. Ssh *ssh = container_of(be, Ssh, backend);
  995. if (ssh->base_layer)
  996. ssh_ppl_special_cmd(ssh->base_layer, code, arg);
  997. }
  998. /*
  999. * This is called when the seat's output channel manages to clear some
  1000. * backlog.
  1001. */
  1002. static void ssh_unthrottle(Backend *be, size_t bufsize)
  1003. {
  1004. Ssh *ssh = container_of(be, Ssh, backend);
  1005. if (ssh->cl)
  1006. ssh_stdout_unthrottle(ssh->cl, bufsize);
  1007. }
  1008. static bool ssh_connected(Backend *be)
  1009. {
  1010. Ssh *ssh = container_of(be, Ssh, backend);
  1011. return ssh->s != NULL;
  1012. }
  1013. static bool ssh_sendok(Backend *be)
  1014. {
  1015. Ssh *ssh = container_of(be, Ssh, backend);
  1016. return ssh->cl && ssh_get_wants_user_input(ssh->cl);
  1017. }
  1018. void ssh_check_sendok(Ssh *ssh)
  1019. {
  1020. /* Called when the connection layer might have caused ssh_sendok
  1021. * to start returning true */
  1022. if (ssh->ldisc)
  1023. ; // ldisc_check_sendok(ssh->ldisc); WINSCP
  1024. }
  1025. void ssh_ldisc_update(Ssh *ssh)
  1026. {
  1027. /* Called when the connection layer wants to propagate an update
  1028. * to the line discipline options */
  1029. if (ssh->ldisc)
  1030. ldisc_echoedit_update(ssh->ldisc);
  1031. }
  1032. static bool ssh_ldisc(Backend *be, int option)
  1033. {
  1034. Ssh *ssh = container_of(be, Ssh, backend);
  1035. return ssh->cl ? ssh_ldisc_option(ssh->cl, option) : false;
  1036. }
  1037. static void ssh_provide_ldisc(Backend *be, Ldisc *ldisc)
  1038. {
  1039. Ssh *ssh = container_of(be, Ssh, backend);
  1040. assert(false); // WINSCP
  1041. ssh->ldisc = ldisc;
  1042. }
  1043. void ssh_got_exitcode(Ssh *ssh, int exitcode)
  1044. {
  1045. ssh->exitcode = exitcode;
  1046. }
  1047. static int ssh_return_exitcode(Backend *be)
  1048. {
  1049. Ssh *ssh = container_of(be, Ssh, backend);
  1050. if (ssh->s && (!ssh->session_started || ssh->base_layer))
  1051. return -1;
  1052. else
  1053. return (ssh->exitcode >= 0 ? ssh->exitcode : INT_MAX);
  1054. }
  1055. /*
  1056. * cfg_info for SSH is the protocol running in this session.
  1057. * (1 or 2 for the full SSH-1 or SSH-2 protocol; -1 for the bare
  1058. * SSH-2 connection protocol, i.e. a downstream; 0 for not-decided-yet.)
  1059. */
  1060. static int ssh_cfg_info(Backend *be)
  1061. {
  1062. Ssh *ssh = container_of(be, Ssh, backend);
  1063. if (ssh->version == 0)
  1064. return 0; /* don't know yet */
  1065. else if (ssh->bare_connection)
  1066. return -1;
  1067. else
  1068. return ssh->version;
  1069. }
  1070. /*
  1071. * Gross hack: pscp will try to start SFTP but fall back to scp1 if
  1072. * that fails. This variable is the means by which pscp.c can reach
  1073. * into the SSH code and find out which one it got.
  1074. */
  1075. extern bool ssh_fallback_cmd(Backend *be)
  1076. {
  1077. Ssh *ssh = container_of(be, Ssh, backend);
  1078. return ssh->fallback_cmd;
  1079. }
  1080. void ssh_got_fallback_cmd(Ssh *ssh)
  1081. {
  1082. ssh->fallback_cmd = true;
  1083. }
  1084. const BackendVtable ssh_backend = {
  1085. // WINSCP
  1086. /*.init =*/ ssh_init,
  1087. /*.free =*/ ssh_free,
  1088. /*.reconfig =*/ ssh_reconfig,
  1089. /*.send =*/ ssh_send,
  1090. /*.sendbuffer =*/ ssh_sendbuffer,
  1091. /*.size =*/ ssh_size,
  1092. /*.special =*/ ssh_special,
  1093. /*.get_specials =*/ ssh_get_specials,
  1094. /*.connected =*/ ssh_connected,
  1095. /*.exitcode =*/ ssh_return_exitcode,
  1096. /*.sendok =*/ ssh_sendok,
  1097. /*.ldisc_option_state =*/ ssh_ldisc,
  1098. /*.provide_ldisc =*/ ssh_provide_ldisc,
  1099. /*.unthrottle =*/ ssh_unthrottle,
  1100. /*.cfg_info =*/ ssh_cfg_info,
  1101. /*.test_for_upstream =*/ ssh_test_for_upstream,
  1102. /*.close_warn_text =*/ ssh_close_warn_text,
  1103. /*.id =*/ "ssh",
  1104. /*.displayname_tc =*/ "SSH",
  1105. /*.displayname_lc =*/ "SSH", /* proper name, so capitalise it anyway */
  1106. /*.protocol =*/ PROT_SSH,
  1107. /*.default_port =*/ 22,
  1108. /*.flags =*/ BACKEND_SUPPORTS_NC_HOST | BACKEND_NOTIFIES_SESSION_START,
  1109. 0, 0, // WINSCP
  1110. };
  1111. const BackendVtable sshconn_backend = {
  1112. // WINSCP
  1113. /*.init =*/ ssh_init,
  1114. /*.free =*/ ssh_free,
  1115. /*.reconfig =*/ ssh_reconfig,
  1116. /*.send =*/ ssh_send,
  1117. /*.sendbuffer =*/ ssh_sendbuffer,
  1118. /*.size =*/ ssh_size,
  1119. /*.special =*/ ssh_special,
  1120. /*.get_specials =*/ ssh_get_specials,
  1121. /*.connected =*/ ssh_connected,
  1122. /*.exitcode =*/ ssh_return_exitcode,
  1123. /*.sendok =*/ ssh_sendok,
  1124. /*.ldisc_option_state =*/ ssh_ldisc,
  1125. /*.provide_ldisc =*/ ssh_provide_ldisc,
  1126. /*.unthrottle =*/ ssh_unthrottle,
  1127. /*.cfg_info =*/ ssh_cfg_info,
  1128. /*.test_for_upstream =*/ ssh_test_for_upstream,
  1129. /*.close_warn_text =*/ ssh_close_warn_text,
  1130. /*.id =*/ "ssh-connection",
  1131. /*.displayname_tc =*/ "Bare ssh-connection",
  1132. /*.displayname_lc =*/ "bare ssh-connection",
  1133. /*.protocol =*/ PROT_SSHCONN,
  1134. 0, // WINSCP
  1135. /*.flags =*/ BACKEND_SUPPORTS_NC_HOST | BACKEND_NOTIFIES_SESSION_START,
  1136. 0, 0, // WINSCP
  1137. };
  1138. #ifdef MPEXT
  1139. #include "puttyexp.h"
  1140. int is_ssh(Plug * plug)
  1141. {
  1142. return plug->vt->closing == ssh_closing;
  1143. }
  1144. int get_ssh_version(Backend * be)
  1145. {
  1146. Ssh * ssh = container_of(be, Ssh, backend);
  1147. return ssh->version;
  1148. }
  1149. Seat * get_ssh_seat(Plug * plug)
  1150. {
  1151. return container_of(plug, Ssh, plug)->seat;
  1152. }
  1153. const ssh_cipher * get_cscipher(Backend * be)
  1154. {
  1155. Ssh * ssh = container_of(be, Ssh, backend);
  1156. return ssh2_bpp_get_cscipher(ssh->bpp);
  1157. }
  1158. const ssh_cipher * get_sccipher(Backend * be)
  1159. {
  1160. Ssh * ssh = container_of(be, Ssh, backend);
  1161. return ssh2_bpp_get_sccipher(ssh->bpp);
  1162. }
  1163. const struct ssh_compressor * get_cscomp(Backend * be)
  1164. {
  1165. Ssh * ssh = container_of(be, Ssh, backend);
  1166. return ssh2_bpp_get_cscomp(ssh->bpp);
  1167. }
  1168. const struct ssh_decompressor * get_sccomp(Backend * be)
  1169. {
  1170. Ssh * ssh = container_of(be, Ssh, backend);
  1171. return ssh2_bpp_get_sccomp(ssh->bpp);
  1172. }
  1173. unsigned int winscp_query(Backend * be, int query)
  1174. {
  1175. Ssh * ssh = container_of(be, Ssh, backend);
  1176. if ((ssh->base_layer != NULL) && (ssh->base_layer->vt->winscp_query != NULL))
  1177. {
  1178. return ssh_ppl_winscp_query(ssh->base_layer, query);
  1179. }
  1180. else
  1181. {
  1182. return 0;
  1183. }
  1184. }
  1185. void md5checksum(const char * buffer, int len, unsigned char output[16])
  1186. {
  1187. hash_simple(&ssh_md5, make_ptrlen(buffer, len), output);
  1188. }
  1189. #endif