sshserver.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * Top-level code for SSH server implementation.
  3. */
  4. #include <assert.h>
  5. #include <stddef.h>
  6. #include "putty.h"
  7. #include "ssh.h"
  8. #include "sshbpp.h"
  9. #include "sshppl.h"
  10. #include "sshchan.h"
  11. #include "sshserver.h"
  12. #ifndef NO_GSSAPI
  13. #include "sshgssc.h"
  14. #include "sshgss.h"
  15. #endif
  16. struct Ssh { int dummy; };
  17. typedef struct server server;
  18. struct server {
  19. bufchain in_raw, out_raw;
  20. IdempotentCallback ic_out_raw;
  21. bool pending_close;
  22. bufchain dummy_user_input; /* we never put anything on this */
  23. PacketLogSettings pls;
  24. LogContext *logctx;
  25. struct DataTransferStats stats;
  26. int remote_bugs;
  27. Socket *socket;
  28. Plug plug;
  29. int conn_throttle_count;
  30. bool frozen;
  31. Conf *conf;
  32. const SshServerConfig *ssc;
  33. ssh_key *const *hostkeys;
  34. int nhostkeys;
  35. RSAKey *hostkey1;
  36. AuthPolicy *authpolicy;
  37. LogPolicy *logpolicy;
  38. const SftpServerVtable *sftpserver_vt;
  39. agentfwd *stunt_agentfwd;
  40. Seat seat;
  41. Ssh ssh;
  42. struct ssh_version_receiver version_receiver;
  43. BinaryPacketProtocol *bpp;
  44. PacketProtocolLayer *base_layer;
  45. ConnectionLayer *cl;
  46. #ifndef NO_GSSAPI
  47. struct ssh_connection_shared_gss_state gss_state;
  48. #endif
  49. };
  50. static void ssh_server_free_callback(void *vsrv);
  51. static void server_got_ssh_version(struct ssh_version_receiver *rcv,
  52. int major_version);
  53. static void server_connect_bpp(server *srv);
  54. static void server_bpp_output_raw_data_callback(void *vctx);
  55. void share_activate(ssh_sharing_state *sharestate,
  56. const char *server_verstring) {}
  57. void ssh_connshare_provide_connlayer(ssh_sharing_state *sharestate,
  58. ConnectionLayer *cl) {}
  59. int share_ndownstreams(ssh_sharing_state *sharestate) { return 0; }
  60. void share_got_pkt_from_server(ssh_sharing_connstate *cs, int type,
  61. const void *vpkt, int pktlen) {}
  62. void share_setup_x11_channel(ssh_sharing_connstate *cs, share_channel *chan,
  63. unsigned upstream_id, unsigned server_id,
  64. unsigned server_currwin, unsigned server_maxpkt,
  65. unsigned client_adjusted_window,
  66. const char *peer_addr, int peer_port, int endian,
  67. int protomajor, int protominor,
  68. const void *initial_data, int initial_len) {}
  69. Channel *agentf_new(SshChannel *c) { return NULL; }
  70. bool agent_exists(void) { return false; }
  71. void ssh_got_exitcode(Ssh *ssh, int exitcode) {}
  72. void ssh_check_frozen(Ssh *ssh) {}
  73. mainchan *mainchan_new(
  74. PacketProtocolLayer *ppl, ConnectionLayer *cl, Conf *conf,
  75. int term_width, int term_height, bool is_simple, SshChannel **sc_out)
  76. { return NULL; }
  77. void mainchan_get_specials(
  78. mainchan *mc, add_special_fn_t add_special, void *ctx) {}
  79. void mainchan_special_cmd(mainchan *mc, SessionSpecialCode code, int arg) {}
  80. void mainchan_terminal_size(mainchan *mc, int width, int height) {}
  81. /* Seat functions to ensure we don't get choosy about crypto - as the
  82. * server, it's not up to us to give user warnings */
  83. static int server_confirm_weak_crypto_primitive(
  84. Seat *seat, const char *algtype, const char *algname,
  85. void (*callback)(void *ctx, int result), void *ctx) { return 1; }
  86. static int server_confirm_weak_cached_hostkey(
  87. Seat *seat, const char *algname, const char *betteralgs,
  88. void (*callback)(void *ctx, int result), void *ctx) { return 1; }
  89. static const SeatVtable server_seat_vt = {
  90. .output = nullseat_output,
  91. .eof = nullseat_eof,
  92. .get_userpass_input = nullseat_get_userpass_input,
  93. .notify_remote_exit = nullseat_notify_remote_exit,
  94. .connection_fatal = nullseat_connection_fatal,
  95. .update_specials_menu = nullseat_update_specials_menu,
  96. .get_ttymode = nullseat_get_ttymode,
  97. .set_busy_status = nullseat_set_busy_status,
  98. .verify_ssh_host_key = nullseat_verify_ssh_host_key,
  99. .confirm_weak_crypto_primitive = server_confirm_weak_crypto_primitive,
  100. .confirm_weak_cached_hostkey = server_confirm_weak_cached_hostkey,
  101. .is_utf8 = nullseat_is_never_utf8,
  102. .echoedit_update = nullseat_echoedit_update,
  103. .get_x_display = nullseat_get_x_display,
  104. .get_windowid = nullseat_get_windowid,
  105. .get_window_pixel_size = nullseat_get_window_pixel_size,
  106. .stripctrl_new = nullseat_stripctrl_new,
  107. .set_trust_status = nullseat_set_trust_status,
  108. .verbose = nullseat_verbose_no,
  109. .interactive = nullseat_interactive_no,
  110. .get_cursor_position = nullseat_get_cursor_position,
  111. };
  112. static void server_socket_log(Plug *plug, PlugLogType type, SockAddr *addr,
  113. int port, const char *error_msg, int error_code)
  114. {
  115. /* server *srv = container_of(plug, server, plug); */
  116. /* FIXME */
  117. }
  118. static void server_closing(Plug *plug, const char *error_msg, int error_code,
  119. bool calling_back)
  120. {
  121. server *srv = container_of(plug, server, plug);
  122. if (error_msg) {
  123. ssh_remote_error(&srv->ssh, "%s", error_msg);
  124. } else if (srv->bpp) {
  125. srv->bpp->input_eof = true;
  126. queue_idempotent_callback(&srv->bpp->ic_in_raw);
  127. }
  128. }
  129. static void server_receive(
  130. Plug *plug, int urgent, const char *data, size_t len)
  131. {
  132. server *srv = container_of(plug, server, plug);
  133. /* Log raw data, if we're in that mode. */
  134. if (srv->logctx)
  135. log_packet(srv->logctx, PKT_INCOMING, -1, NULL, data, len,
  136. 0, NULL, NULL, 0, NULL);
  137. bufchain_add(&srv->in_raw, data, len);
  138. if (!srv->frozen && srv->bpp)
  139. queue_idempotent_callback(&srv->bpp->ic_in_raw);
  140. }
  141. static void server_sent(Plug *plug, size_t bufsize)
  142. {
  143. #ifdef FIXME
  144. server *srv = container_of(plug, server, plug);
  145. /*
  146. * If the send backlog on the SSH socket itself clears, we should
  147. * unthrottle the whole world if it was throttled. Also trigger an
  148. * extra call to the consumer of the BPP's output, to try to send
  149. * some more data off its bufchain.
  150. */
  151. if (bufsize < SSH_MAX_BACKLOG) {
  152. srv_throttle_all(srv, 0, bufsize);
  153. queue_idempotent_callback(&srv->ic_out_raw);
  154. }
  155. #endif
  156. }
  157. LogContext *ssh_get_logctx(Ssh *ssh)
  158. {
  159. server *srv = container_of(ssh, server, ssh);
  160. return srv->logctx;
  161. }
  162. void ssh_throttle_conn(Ssh *ssh, int adjust)
  163. {
  164. server *srv = container_of(ssh, server, ssh);
  165. int old_count = srv->conn_throttle_count;
  166. bool frozen;
  167. srv->conn_throttle_count += adjust;
  168. assert(srv->conn_throttle_count >= 0);
  169. if (srv->conn_throttle_count && !old_count) {
  170. frozen = true;
  171. } else if (!srv->conn_throttle_count && old_count) {
  172. frozen = false;
  173. } else {
  174. return; /* don't change current frozen state */
  175. }
  176. srv->frozen = frozen;
  177. if (srv->socket) {
  178. sk_set_frozen(srv->socket, frozen);
  179. /*
  180. * Now process any SSH connection data that was stashed in our
  181. * queue while we were frozen.
  182. */
  183. queue_idempotent_callback(&srv->bpp->ic_in_raw);
  184. }
  185. }
  186. void ssh_conn_processed_data(Ssh *ssh)
  187. {
  188. /* FIXME: we could add the same check_frozen_state system as we
  189. * have in ssh.c, but because that was originally added to work
  190. * around a peculiarity of the GUI event loop, I haven't yet. */
  191. }
  192. Conf *make_ssh_server_conf(void)
  193. {
  194. Conf *conf = conf_new();
  195. load_open_settings(NULL, conf);
  196. /* In Uppity, we support even the legacy des-cbc cipher by
  197. * default, so that it will be available if the user forces it by
  198. * overriding the KEXINIT strings. If the user wants it _not_
  199. * supported, of course, they can override KEXINIT in the other
  200. * direction. */
  201. conf_set_bool(conf, CONF_ssh2_des_cbc, true);
  202. return conf;
  203. }
  204. static const PlugVtable ssh_server_plugvt = {
  205. .log = server_socket_log,
  206. .closing = server_closing,
  207. .receive = server_receive,
  208. .sent = server_sent,
  209. };
  210. Plug *ssh_server_plug(
  211. Conf *conf, const SshServerConfig *ssc,
  212. ssh_key *const *hostkeys, int nhostkeys,
  213. RSAKey *hostkey1, AuthPolicy *authpolicy, LogPolicy *logpolicy,
  214. const SftpServerVtable *sftpserver_vt)
  215. {
  216. server *srv = snew(server);
  217. memset(srv, 0, sizeof(server));
  218. srv->plug.vt = &ssh_server_plugvt;
  219. srv->conf = conf_copy(conf);
  220. srv->ssc = ssc;
  221. srv->logctx = log_init(logpolicy, conf);
  222. conf_set_bool(srv->conf, CONF_ssh_no_shell, true);
  223. srv->nhostkeys = nhostkeys;
  224. srv->hostkeys = hostkeys;
  225. srv->hostkey1 = hostkey1;
  226. srv->authpolicy = authpolicy;
  227. srv->logpolicy = logpolicy;
  228. srv->sftpserver_vt = sftpserver_vt;
  229. srv->seat.vt = &server_seat_vt;
  230. bufchain_init(&srv->in_raw);
  231. bufchain_init(&srv->out_raw);
  232. bufchain_init(&srv->dummy_user_input);
  233. #ifndef NO_GSSAPI
  234. /* FIXME: replace with sensible */
  235. srv->gss_state.libs = snew(struct ssh_gss_liblist);
  236. srv->gss_state.libs->nlibraries = 0;
  237. #endif
  238. return &srv->plug;
  239. }
  240. void ssh_server_start(Plug *plug, Socket *socket)
  241. {
  242. server *srv = container_of(plug, server, plug);
  243. const char *our_protoversion;
  244. if (srv->ssc->bare_connection) {
  245. our_protoversion = "2.0"; /* SSH-2 only */
  246. } else if (srv->hostkey1 && srv->nhostkeys) {
  247. our_protoversion = "1.99"; /* offer both SSH-1 and SSH-2 */
  248. } else if (srv->hostkey1) {
  249. our_protoversion = "1.5"; /* SSH-1 only */
  250. } else {
  251. assert(srv->nhostkeys);
  252. our_protoversion = "2.0"; /* SSH-2 only */
  253. }
  254. srv->socket = socket;
  255. srv->ic_out_raw.fn = server_bpp_output_raw_data_callback;
  256. srv->ic_out_raw.ctx = srv;
  257. srv->version_receiver.got_ssh_version = server_got_ssh_version;
  258. srv->bpp = ssh_verstring_new(
  259. srv->conf, srv->logctx, srv->ssc->bare_connection,
  260. our_protoversion, &srv->version_receiver,
  261. true, srv->ssc->application_name);
  262. server_connect_bpp(srv);
  263. queue_idempotent_callback(&srv->bpp->ic_in_raw);
  264. }
  265. static void ssh_server_free_callback(void *vsrv)
  266. {
  267. server *srv = (server *)vsrv;
  268. logeventf(srv->logctx, "freeing server instance");
  269. bufchain_clear(&srv->in_raw);
  270. bufchain_clear(&srv->out_raw);
  271. bufchain_clear(&srv->dummy_user_input);
  272. if (srv->socket)
  273. sk_close(srv->socket);
  274. if (srv->stunt_agentfwd)
  275. agentfwd_free(srv->stunt_agentfwd);
  276. if (srv->base_layer)
  277. ssh_ppl_free(srv->base_layer);
  278. if (srv->bpp)
  279. ssh_bpp_free(srv->bpp);
  280. delete_callbacks_for_context(srv);
  281. conf_free(srv->conf);
  282. log_free(srv->logctx);
  283. #ifndef NO_GSSAPI
  284. sfree(srv->gss_state.libs); /* FIXME: replace with sensible */
  285. #endif
  286. LogPolicy *lp = srv->logpolicy;
  287. sfree(srv);
  288. server_instance_terminated(lp);
  289. }
  290. static void server_connect_bpp(server *srv)
  291. {
  292. srv->bpp->ssh = &srv->ssh;
  293. srv->bpp->in_raw = &srv->in_raw;
  294. srv->bpp->out_raw = &srv->out_raw;
  295. bufchain_set_callback(srv->bpp->out_raw, &srv->ic_out_raw);
  296. srv->bpp->pls = &srv->pls;
  297. srv->bpp->logctx = srv->logctx;
  298. srv->bpp->remote_bugs = srv->remote_bugs;
  299. /* Servers don't really have a notion of 'unexpected' connection
  300. * closure. The client is free to close if it likes. */
  301. srv->bpp->expect_close = true;
  302. }
  303. static void server_connect_ppl(server *srv, PacketProtocolLayer *ppl)
  304. {
  305. ppl->bpp = srv->bpp;
  306. ppl->user_input = &srv->dummy_user_input;
  307. ppl->logctx = srv->logctx;
  308. ppl->ssh = &srv->ssh;
  309. ppl->seat = &srv->seat;
  310. ppl->remote_bugs = srv->remote_bugs;
  311. }
  312. static void server_bpp_output_raw_data_callback(void *vctx)
  313. {
  314. server *srv = (server *)vctx;
  315. if (!srv->socket)
  316. return;
  317. while (bufchain_size(&srv->out_raw) > 0) {
  318. size_t backlog;
  319. ptrlen data = bufchain_prefix(&srv->out_raw);
  320. if (srv->logctx)
  321. log_packet(srv->logctx, PKT_OUTGOING, -1, NULL, data.ptr, data.len,
  322. 0, NULL, NULL, 0, NULL);
  323. backlog = sk_write(srv->socket, data.ptr, data.len);
  324. bufchain_consume(&srv->out_raw, data.len);
  325. if (backlog > SSH_MAX_BACKLOG) {
  326. #ifdef FIXME
  327. ssh_throttle_all(ssh, 1, backlog);
  328. #endif
  329. return;
  330. }
  331. }
  332. if (srv->pending_close) {
  333. sk_close(srv->socket);
  334. srv->socket = NULL;
  335. queue_toplevel_callback(ssh_server_free_callback, srv);
  336. }
  337. }
  338. static void server_shutdown_internal(server *srv)
  339. {
  340. /*
  341. * We only need to free the base PPL, which will free the others
  342. * (if any) transitively.
  343. */
  344. if (srv->base_layer) {
  345. ssh_ppl_free(srv->base_layer);
  346. srv->base_layer = NULL;
  347. }
  348. srv->cl = NULL;
  349. }
  350. static void server_initiate_connection_close(server *srv)
  351. {
  352. /* Wind up everything above the BPP. */
  353. server_shutdown_internal(srv);
  354. /* Force any remaining queued SSH packets through the BPP, and
  355. * schedule closing the network socket after they go out. */
  356. ssh_bpp_handle_output(srv->bpp);
  357. srv->pending_close = true;
  358. queue_idempotent_callback(&srv->ic_out_raw);
  359. /* Now we expect the other end to close the connection too in
  360. * response, so arrange that we'll receive notification of that
  361. * via ssh_remote_eof. */
  362. srv->bpp->expect_close = true;
  363. }
  364. #define GET_FORMATTED_MSG(fmt) \
  365. char *msg; \
  366. va_list ap; \
  367. va_start(ap, fmt); \
  368. msg = dupvprintf(fmt, ap); \
  369. va_end(ap);
  370. #define LOG_FORMATTED_MSG(logctx, fmt) do \
  371. { \
  372. va_list ap; \
  373. va_start(ap, fmt); \
  374. logeventvf(logctx, fmt, ap); \
  375. va_end(ap); \
  376. } while (0)
  377. void ssh_remote_error(Ssh *ssh, const char *fmt, ...)
  378. {
  379. server *srv = container_of(ssh, server, ssh);
  380. LOG_FORMATTED_MSG(srv->logctx, fmt);
  381. queue_toplevel_callback(ssh_server_free_callback, srv);
  382. }
  383. void ssh_remote_eof(Ssh *ssh, const char *fmt, ...)
  384. {
  385. server *srv = container_of(ssh, server, ssh);
  386. LOG_FORMATTED_MSG(srv->logctx, fmt);
  387. queue_toplevel_callback(ssh_server_free_callback, srv);
  388. }
  389. void ssh_proto_error(Ssh *ssh, const char *fmt, ...)
  390. {
  391. server *srv = container_of(ssh, server, ssh);
  392. if (srv->base_layer) {
  393. GET_FORMATTED_MSG(fmt);
  394. ssh_bpp_queue_disconnect(srv->bpp, msg,
  395. SSH2_DISCONNECT_PROTOCOL_ERROR);
  396. server_initiate_connection_close(srv);
  397. logeventf(srv->logctx, "Protocol error: %s", msg);
  398. sfree(msg);
  399. }
  400. }
  401. void ssh_sw_abort(Ssh *ssh, const char *fmt, ...)
  402. {
  403. server *srv = container_of(ssh, server, ssh);
  404. LOG_FORMATTED_MSG(srv->logctx, fmt);
  405. queue_toplevel_callback(ssh_server_free_callback, srv);
  406. }
  407. void ssh_user_close(Ssh *ssh, const char *fmt, ...)
  408. {
  409. server *srv = container_of(ssh, server, ssh);
  410. LOG_FORMATTED_MSG(srv->logctx, fmt);
  411. queue_toplevel_callback(ssh_server_free_callback, srv);
  412. }
  413. static void server_got_ssh_version(struct ssh_version_receiver *rcv,
  414. int major_version)
  415. {
  416. server *srv = container_of(rcv, server, version_receiver);
  417. BinaryPacketProtocol *old_bpp;
  418. PacketProtocolLayer *connection_layer;
  419. old_bpp = srv->bpp;
  420. srv->remote_bugs = ssh_verstring_get_bugs(old_bpp);
  421. if (srv->ssc->bare_connection) {
  422. srv->bpp = ssh2_bare_bpp_new(srv->logctx);
  423. server_connect_bpp(srv);
  424. connection_layer = ssh2_connection_new(
  425. &srv->ssh, NULL, false, srv->conf,
  426. ssh_verstring_get_local(old_bpp), &srv->cl);
  427. ssh2connection_server_configure(connection_layer,
  428. srv->sftpserver_vt, srv->ssc);
  429. server_connect_ppl(srv, connection_layer);
  430. srv->base_layer = connection_layer;
  431. } else if (major_version == 2) {
  432. PacketProtocolLayer *userauth_layer, *transport_child_layer;
  433. srv->bpp = ssh2_bpp_new(srv->logctx, &srv->stats, true);
  434. server_connect_bpp(srv);
  435. connection_layer = ssh2_connection_new(
  436. &srv->ssh, NULL, false, srv->conf,
  437. ssh_verstring_get_local(old_bpp), &srv->cl);
  438. ssh2connection_server_configure(connection_layer,
  439. srv->sftpserver_vt, srv->ssc);
  440. server_connect_ppl(srv, connection_layer);
  441. if (conf_get_bool(srv->conf, CONF_ssh_no_userauth)) {
  442. userauth_layer = NULL;
  443. transport_child_layer = connection_layer;
  444. } else {
  445. userauth_layer = ssh2_userauth_server_new(
  446. connection_layer, srv->authpolicy, srv->ssc);
  447. server_connect_ppl(srv, userauth_layer);
  448. transport_child_layer = userauth_layer;
  449. }
  450. srv->base_layer = ssh2_transport_new(
  451. srv->conf, NULL, 0, NULL,
  452. ssh_verstring_get_remote(old_bpp),
  453. ssh_verstring_get_local(old_bpp),
  454. #ifndef NO_GSSAPI
  455. &srv->gss_state,
  456. #else
  457. NULL,
  458. #endif
  459. &srv->stats, transport_child_layer, srv->ssc);
  460. ssh2_transport_provide_hostkeys(
  461. srv->base_layer, srv->hostkeys, srv->nhostkeys);
  462. if (userauth_layer)
  463. ssh2_userauth_server_set_transport_layer(
  464. userauth_layer, srv->base_layer);
  465. server_connect_ppl(srv, srv->base_layer);
  466. } else {
  467. srv->bpp = ssh1_bpp_new(srv->logctx);
  468. server_connect_bpp(srv);
  469. connection_layer = ssh1_connection_new(&srv->ssh, srv->conf, &srv->cl);
  470. ssh1connection_server_configure(connection_layer, srv->ssc);
  471. server_connect_ppl(srv, connection_layer);
  472. srv->base_layer = ssh1_login_server_new(
  473. connection_layer, srv->hostkey1, srv->authpolicy, srv->ssc);
  474. server_connect_ppl(srv, srv->base_layer);
  475. }
  476. /* Connect the base layer - whichever it is - to the BPP, and set
  477. * up its selfptr. */
  478. srv->base_layer->selfptr = &srv->base_layer;
  479. ssh_ppl_setup_queues(srv->base_layer, &srv->bpp->in_pq, &srv->bpp->out_pq);
  480. #ifdef FIXME // we probably will want one of these, in the end
  481. srv->pinger = pinger_new(srv->conf, &srv->backend);
  482. #endif
  483. if (srv->ssc->stunt_open_unconditional_agent_socket) {
  484. char *socketname;
  485. srv->stunt_agentfwd = agentfwd_new(srv->cl, &socketname);
  486. if (srv->stunt_agentfwd) {
  487. logeventf(srv->logctx, "opened unconditional agent socket at %s\n",
  488. socketname);
  489. sfree(socketname);
  490. }
  491. }
  492. queue_idempotent_callback(&srv->bpp->ic_in_raw);
  493. ssh_ppl_process_queue(srv->base_layer);
  494. ssh_bpp_free(old_bpp);
  495. }