connection1-server.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Server-specific parts of the SSH-1 connection layer.
  3. */
  4. #include <assert.h>
  5. #include "putty.h"
  6. #include "ssh.h"
  7. #include "bpp.h"
  8. #include "ppl.h"
  9. #include "channel.h"
  10. #include "sshcr.h"
  11. #include "connection1.h"
  12. #include "server.h"
  13. static size_t ssh1sesschan_write(SshChannel *c, bool is_stderr,
  14. const void *, size_t);
  15. static void ssh1sesschan_write_eof(SshChannel *c);
  16. static void ssh1sesschan_initiate_close(SshChannel *c, const char *err);
  17. static void ssh1sesschan_send_exit_status(SshChannel *c, int status);
  18. static void ssh1sesschan_send_exit_signal(
  19. SshChannel *c, ptrlen signame, bool core_dumped, ptrlen msg);
  20. static const SshChannelVtable ssh1sesschan_vtable = {
  21. .write = ssh1sesschan_write,
  22. .write_eof = ssh1sesschan_write_eof,
  23. .initiate_close = ssh1sesschan_initiate_close,
  24. .send_exit_status = ssh1sesschan_send_exit_status,
  25. .send_exit_signal = ssh1sesschan_send_exit_signal,
  26. /* everything else is NULL */
  27. };
  28. void ssh1connection_server_configure(
  29. PacketProtocolLayer *ppl, const SshServerConfig *ssc)
  30. {
  31. struct ssh1_connection_state *s =
  32. container_of(ppl, struct ssh1_connection_state, ppl);
  33. s->ssc = ssc;
  34. }
  35. void ssh1_connection_direction_specific_setup(
  36. struct ssh1_connection_state *s)
  37. {
  38. if (!s->mainchan_chan) {
  39. s->mainchan_sc.vt = &ssh1sesschan_vtable;
  40. s->mainchan_sc.cl = &s->cl;
  41. s->mainchan_chan = sesschan_new(
  42. &s->mainchan_sc, s->ppl.logctx, NULL, s->ssc);
  43. }
  44. }
  45. bool ssh1_handle_direction_specific_packet(
  46. struct ssh1_connection_state *s, PktIn *pktin)
  47. {
  48. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  49. PktOut *pktout;
  50. struct ssh1_channel *c;
  51. unsigned remid;
  52. ptrlen host, cmd, data;
  53. char *host_str, *err;
  54. int port, listenport;
  55. bool success;
  56. switch (pktin->type) {
  57. case SSH1_CMSG_EXEC_SHELL:
  58. if (s->finished_setup)
  59. goto unexpected_setup_packet;
  60. ppl_logevent("Client requested a shell");
  61. chan_run_shell(s->mainchan_chan);
  62. s->finished_setup = true;
  63. return true;
  64. case SSH1_CMSG_EXEC_CMD:
  65. if (s->finished_setup)
  66. goto unexpected_setup_packet;
  67. cmd = get_string(pktin);
  68. ppl_logevent("Client sent command '%.*s'", PTRLEN_PRINTF(cmd));
  69. chan_run_command(s->mainchan_chan, cmd);
  70. s->finished_setup = true;
  71. return true;
  72. case SSH1_CMSG_REQUEST_COMPRESSION:
  73. if (s->compressing || !s->ssc->ssh1_allow_compression) {
  74. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_SMSG_FAILURE);
  75. pq_push(s->ppl.out_pq, pktout);
  76. } else {
  77. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_SMSG_SUCCESS);
  78. pq_push(s->ppl.out_pq, pktout);
  79. /* Synchronous run of output formatting, to ensure that
  80. * success packet is converted into wire format before we
  81. * start compressing */
  82. ssh_bpp_handle_output(s->ppl.bpp);
  83. /* And now ensure that the _next_ packet will be the first
  84. * compressed one. */
  85. ssh1_bpp_start_compression(s->ppl.bpp);
  86. s->compressing = true;
  87. }
  88. return true;
  89. case SSH1_CMSG_REQUEST_PTY: {
  90. if (s->finished_setup)
  91. goto unexpected_setup_packet;
  92. ptrlen termtype = get_string(pktin);
  93. unsigned height = get_uint32(pktin);
  94. unsigned width = get_uint32(pktin);
  95. unsigned pixwidth = get_uint32(pktin);
  96. unsigned pixheight = get_uint32(pktin);
  97. struct ssh_ttymodes modes = read_ttymodes_from_packet(
  98. BinarySource_UPCAST(pktin), 1);
  99. if (get_err(pktin)) {
  100. ppl_logevent("Unable to decode pty request packet");
  101. success = false;
  102. } else if (!chan_allocate_pty(
  103. s->mainchan_chan, termtype, width, height,
  104. pixwidth, pixheight, modes)) {
  105. ppl_logevent("Unable to allocate a pty");
  106. success = false;
  107. } else {
  108. success = true;
  109. }
  110. pktout = ssh_bpp_new_pktout(
  111. s->ppl.bpp, (success ? SSH1_SMSG_SUCCESS : SSH1_SMSG_FAILURE));
  112. pq_push(s->ppl.out_pq, pktout);
  113. return true;
  114. }
  115. case SSH1_CMSG_PORT_FORWARD_REQUEST:
  116. if (s->finished_setup)
  117. goto unexpected_setup_packet;
  118. listenport = toint(get_uint32(pktin));
  119. host = get_string(pktin);
  120. port = toint(get_uint32(pktin));
  121. ppl_logevent("Client requested port %d forward to %.*s:%d",
  122. listenport, PTRLEN_PRINTF(host), port);
  123. host_str = mkstr(host);
  124. success = portfwdmgr_listen(
  125. s->portfwdmgr, NULL, listenport, host_str, port, s->conf);
  126. sfree(host_str);
  127. pktout = ssh_bpp_new_pktout(
  128. s->ppl.bpp, (success ? SSH1_SMSG_SUCCESS : SSH1_SMSG_FAILURE));
  129. pq_push(s->ppl.out_pq, pktout);
  130. return true;
  131. case SSH1_CMSG_X11_REQUEST_FORWARDING: {
  132. if (s->finished_setup)
  133. goto unexpected_setup_packet;
  134. ptrlen authproto = get_string(pktin);
  135. ptrlen authdata = get_string(pktin);
  136. unsigned screen_number = 0;
  137. if (s->remote_protoflags & SSH1_PROTOFLAG_SCREEN_NUMBER)
  138. screen_number = get_uint32(pktin);
  139. success = chan_enable_x11_forwarding(
  140. s->mainchan_chan, false, authproto, authdata, screen_number);
  141. pktout = ssh_bpp_new_pktout(
  142. s->ppl.bpp, (success ? SSH1_SMSG_SUCCESS : SSH1_SMSG_FAILURE));
  143. pq_push(s->ppl.out_pq, pktout);
  144. return true;
  145. }
  146. case SSH1_CMSG_AGENT_REQUEST_FORWARDING:
  147. if (s->finished_setup)
  148. goto unexpected_setup_packet;
  149. success = chan_enable_agent_forwarding(s->mainchan_chan);
  150. pktout = ssh_bpp_new_pktout(
  151. s->ppl.bpp, (success ? SSH1_SMSG_SUCCESS : SSH1_SMSG_FAILURE));
  152. pq_push(s->ppl.out_pq, pktout);
  153. return true;
  154. case SSH1_CMSG_STDIN_DATA:
  155. data = get_string(pktin);
  156. chan_send(s->mainchan_chan, false, data.ptr, data.len);
  157. return true;
  158. case SSH1_CMSG_EOF:
  159. chan_send_eof(s->mainchan_chan);
  160. return true;
  161. case SSH1_CMSG_WINDOW_SIZE:
  162. return true;
  163. case SSH1_MSG_PORT_OPEN:
  164. remid = get_uint32(pktin);
  165. host = get_string(pktin);
  166. port = toint(get_uint32(pktin));
  167. host_str = mkstr(host);
  168. ppl_logevent("Received request to connect to port %s:%d",
  169. host_str, port);
  170. c = snew(struct ssh1_channel);
  171. c->connlayer = s;
  172. err = portfwdmgr_connect(
  173. s->portfwdmgr, &c->chan, host_str, port,
  174. &c->sc, ADDRTYPE_UNSPEC);
  175. sfree(host_str);
  176. if (err) {
  177. ppl_logevent("Port open failed: %s", err);
  178. sfree(err);
  179. ssh1_channel_free(c);
  180. pktout = ssh_bpp_new_pktout(
  181. s->ppl.bpp, SSH1_MSG_CHANNEL_OPEN_FAILURE);
  182. put_uint32(pktout, remid);
  183. pq_push(s->ppl.out_pq, pktout);
  184. } else {
  185. ssh1_channel_init(c);
  186. c->remoteid = remid;
  187. c->halfopen = false;
  188. pktout = ssh_bpp_new_pktout(
  189. s->ppl.bpp, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION);
  190. put_uint32(pktout, c->remoteid);
  191. put_uint32(pktout, c->localid);
  192. pq_push(s->ppl.out_pq, pktout);
  193. ppl_logevent("Forwarded port opened successfully");
  194. }
  195. return true;
  196. case SSH1_CMSG_EXIT_CONFIRMATION:
  197. if (!s->sent_exit_status) {
  198. ssh_proto_error(s->ppl.ssh, "Received SSH1_CMSG_EXIT_CONFIRMATION"
  199. " without having sent SSH1_SMSG_EXIT_STATUS");
  200. return true;
  201. }
  202. ppl_logevent("Client sent exit confirmation");
  203. return true;
  204. default:
  205. return false;
  206. }
  207. unexpected_setup_packet:
  208. ssh_proto_error(s->ppl.ssh, "Received unexpected setup packet after the "
  209. "setup phase, type %d (%s)", pktin->type,
  210. ssh1_pkt_type(pktin->type));
  211. /* FIXME: ensure caller copes with us just having freed the whole layer */
  212. return true;
  213. }
  214. SshChannel *ssh1_session_open(ConnectionLayer *cl, Channel *chan)
  215. {
  216. unreachable("Should never be called in the server");
  217. }
  218. struct ssh_rportfwd *ssh1_rportfwd_alloc(
  219. ConnectionLayer *cl,
  220. const char *shost, int sport, const char *dhost, int dport,
  221. int addressfamily, const char *log_description, PortFwdRecord *pfr,
  222. ssh_sharing_connstate *share_ctx)
  223. {
  224. unreachable("Should never be called in the server");
  225. }
  226. static size_t ssh1sesschan_write(SshChannel *sc, bool is_stderr,
  227. const void *data, size_t len)
  228. {
  229. struct ssh1_connection_state *s =
  230. container_of(sc, struct ssh1_connection_state, mainchan_sc);
  231. PktOut *pktout;
  232. pktout = ssh_bpp_new_pktout(
  233. s->ppl.bpp,
  234. (is_stderr ? SSH1_SMSG_STDERR_DATA : SSH1_SMSG_STDOUT_DATA));
  235. put_string(pktout, data, len);
  236. pq_push(s->ppl.out_pq, pktout);
  237. return 0;
  238. }
  239. static void ssh1sesschan_write_eof(SshChannel *sc)
  240. {
  241. /* SSH-1 can't represent server-side EOF */
  242. /* FIXME: some kind of check-termination system, whereby once this has been called _and_ we've had an exit status _and_ we've got no other channels open, we send the actual EXIT_STATUS message */
  243. }
  244. static void ssh1sesschan_initiate_close(SshChannel *sc, const char *err)
  245. {
  246. /* SSH-1 relies on the client to close the connection in the end */
  247. }
  248. static void ssh1sesschan_send_exit_status(SshChannel *sc, int status)
  249. {
  250. struct ssh1_connection_state *s =
  251. container_of(sc, struct ssh1_connection_state, mainchan_sc);
  252. PktOut *pktout;
  253. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_SMSG_EXIT_STATUS);
  254. put_uint32(pktout, status);
  255. pq_push(s->ppl.out_pq, pktout);
  256. s->sent_exit_status = true;
  257. }
  258. static void ssh1sesschan_send_exit_signal(
  259. SshChannel *sc, ptrlen signame, bool core_dumped, ptrlen msg)
  260. {
  261. /* SSH-1 has no separate representation for signals */
  262. ssh1sesschan_send_exit_status(sc, 128);
  263. }
  264. SshChannel *ssh1_serverside_x11_open(
  265. ConnectionLayer *cl, Channel *chan, const SocketEndpointInfo *pi)
  266. {
  267. struct ssh1_connection_state *s =
  268. container_of(cl, struct ssh1_connection_state, cl);
  269. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  270. struct ssh1_channel *c = snew(struct ssh1_channel);
  271. PktOut *pktout;
  272. c->connlayer = s;
  273. ssh1_channel_init(c);
  274. c->halfopen = true;
  275. c->chan = chan;
  276. ppl_logevent("Forwarding X11 connection to client");
  277. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_SMSG_X11_OPEN);
  278. put_uint32(pktout, c->localid);
  279. pq_push(s->ppl.out_pq, pktout);
  280. return &c->sc;
  281. }
  282. SshChannel *ssh1_serverside_agent_open(ConnectionLayer *cl, Channel *chan)
  283. {
  284. struct ssh1_connection_state *s =
  285. container_of(cl, struct ssh1_connection_state, cl);
  286. PacketProtocolLayer *ppl = &s->ppl; /* for ppl_logevent */
  287. struct ssh1_channel *c = snew(struct ssh1_channel);
  288. PktOut *pktout;
  289. c->connlayer = s;
  290. ssh1_channel_init(c);
  291. c->halfopen = true;
  292. c->chan = chan;
  293. ppl_logevent("Forwarding agent connection to client");
  294. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH1_SMSG_AGENT_OPEN);
  295. put_uint32(pktout, c->localid);
  296. pq_push(s->ppl.out_pq, pktout);
  297. return &c->sc;
  298. }
  299. bool ssh1_connection_need_antispoof_prompt(struct ssh1_connection_state *s)
  300. {
  301. return false;
  302. }