ssh.c 40 KB

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