ssh.c 39 KB

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