connection2.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #ifndef PUTTY_SSH2CONNECTION_H
  2. #define PUTTY_SSH2CONNECTION_H
  3. struct outstanding_channel_request;
  4. struct outstanding_global_request;
  5. struct ssh2_connection_state {
  6. int crState;
  7. ssh_sharing_state *connshare;
  8. char *peer_verstring;
  9. mainchan *mainchan;
  10. SshChannel *mainchan_sc;
  11. bool ldisc_opts[LD_N_OPTIONS];
  12. int session_attempt, session_status;
  13. int term_width, term_height;
  14. bool want_user_input;
  15. bufchain *user_input;
  16. bool ssh_is_simple;
  17. bool persistent;
  18. bool started;
  19. Conf *conf;
  20. tree234 *channels; /* indexed by local id */
  21. bool all_channels_throttled;
  22. bool X11_fwd_enabled;
  23. tree234 *x11authtree;
  24. bool got_pty;
  25. tree234 *rportfwds;
  26. PortFwdManager *portfwdmgr;
  27. bool portfwdmgr_configured;
  28. prompts_t *antispoof_prompt;
  29. SeatPromptResult antispoof_ret;
  30. const SftpServerVtable *sftpserver_vt;
  31. const SshServerConfig *ssc;
  32. /*
  33. * These store the list of global requests that we're waiting for
  34. * replies to. (REQUEST_FAILURE doesn't come with any indication
  35. * of what message caused it, so we have to keep track of the
  36. * queue ourselves.)
  37. */
  38. struct outstanding_global_request *globreq_head, *globreq_tail;
  39. ConnectionLayer cl;
  40. PacketProtocolLayer ppl;
  41. };
  42. typedef void (*gr_handler_fn_t)(struct ssh2_connection_state *s,
  43. PktIn *pktin, void *ctx);
  44. void ssh2_queue_global_request_handler(
  45. struct ssh2_connection_state *s, gr_handler_fn_t handler, void *ctx);
  46. struct ssh2_channel {
  47. struct ssh2_connection_state *connlayer;
  48. unsigned remoteid, localid;
  49. int type;
  50. /* True if we opened this channel but server hasn't confirmed. */
  51. bool halfopen;
  52. /* Bitmap of whether we've sent/received CHANNEL_EOF and
  53. * CHANNEL_CLOSE. */
  54. #define CLOSES_SENT_EOF 1
  55. #define CLOSES_SENT_CLOSE 2
  56. #define CLOSES_RCVD_EOF 4
  57. #define CLOSES_RCVD_CLOSE 8
  58. int closes;
  59. /*
  60. * This flag indicates that an EOF is pending on the outgoing side
  61. * of the channel: that is, wherever we're getting the data for
  62. * this channel has sent us some data followed by EOF. We can't
  63. * actually send the EOF until we've finished sending the data, so
  64. * we set this flag instead to remind us to do so once our buffer
  65. * is clear.
  66. */
  67. bool pending_eof;
  68. /*
  69. * True if this channel is causing the underlying connection to be
  70. * throttled.
  71. */
  72. bool throttling_conn;
  73. /*
  74. * True if we currently have backed-up data on the direction of
  75. * this channel pointing out of the SSH connection, and therefore
  76. * would prefer the 'Channel' implementation not to read further
  77. * local input if possible.
  78. */
  79. bool throttled_by_backlog;
  80. bufchain outbuffer, errbuffer;
  81. unsigned remwindow, remmaxpkt;
  82. /* locwindow is signed so we can cope with excess data. */
  83. int locwindow, locmaxwin;
  84. /*
  85. * remlocwin is the amount of local window that we think
  86. * the remote end had available to it after it sent the
  87. * last data packet or window adjust ack.
  88. */
  89. int remlocwin;
  90. /*
  91. * These store the list of channel requests that we're waiting for
  92. * replies to. (CHANNEL_FAILURE doesn't come with any indication
  93. * of what message caused it, so we have to keep track of the
  94. * queue ourselves.)
  95. */
  96. struct outstanding_channel_request *chanreq_head, *chanreq_tail;
  97. enum { THROTTLED, UNTHROTTLING, UNTHROTTLED } throttle_state;
  98. ssh_sharing_connstate *sharectx; /* sharing context, if this is a
  99. * downstream channel */
  100. Channel *chan; /* handle the client side of this channel, if not */
  101. SshChannel sc; /* entry point for chan to talk back to */
  102. };
  103. typedef void (*cr_handler_fn_t)(struct ssh2_channel *, PktIn *, void *);
  104. void ssh2_channel_init(struct ssh2_channel *c);
  105. PktOut *ssh2_chanreq_init(struct ssh2_channel *c, const char *type,
  106. cr_handler_fn_t handler, void *ctx);
  107. typedef enum ChanopenOutcome {
  108. CHANOPEN_RESULT_FAILURE,
  109. CHANOPEN_RESULT_SUCCESS,
  110. CHANOPEN_RESULT_DOWNSTREAM,
  111. } ChanopenOutcome;
  112. typedef struct ChanopenResult {
  113. ChanopenOutcome outcome;
  114. union {
  115. struct {
  116. char *wire_message; /* must be freed by recipient */
  117. unsigned reason_code;
  118. } failure;
  119. struct {
  120. Channel *channel;
  121. } success;
  122. struct {
  123. ssh_sharing_connstate *share_ctx;
  124. } downstream;
  125. } u;
  126. } ChanopenResult;
  127. PktOut *ssh2_chanopen_init(struct ssh2_channel *c, const char *type);
  128. PktOut *ssh2_portfwd_chanopen(
  129. struct ssh2_connection_state *s, struct ssh2_channel *c,
  130. const char *hostname, int port,
  131. const char *description, const SocketEndpointInfo *peerinfo);
  132. struct ssh_rportfwd *ssh2_rportfwd_alloc(
  133. ConnectionLayer *cl,
  134. const char *shost, int sport, const char *dhost, int dport,
  135. int addressfamily, const char *log_description, PortFwdRecord *pfr,
  136. ssh_sharing_connstate *share_ctx);
  137. void ssh2_rportfwd_remove(
  138. ConnectionLayer *cl, struct ssh_rportfwd *rpf);
  139. SshChannel *ssh2_session_open(ConnectionLayer *cl, Channel *chan);
  140. SshChannel *ssh2_serverside_x11_open(
  141. ConnectionLayer *cl, Channel *chan, const SocketEndpointInfo *pi);
  142. SshChannel *ssh2_serverside_agent_open(ConnectionLayer *cl, Channel *chan);
  143. void ssh2channel_send_exit_status(SshChannel *c, int status);
  144. void ssh2channel_send_exit_signal(
  145. SshChannel *c, ptrlen signame, bool core_dumped, ptrlen msg);
  146. void ssh2channel_send_exit_signal_numeric(
  147. SshChannel *c, int signum, bool core_dumped, ptrlen msg);
  148. void ssh2channel_request_x11_forwarding(
  149. SshChannel *c, bool want_reply, const char *authproto,
  150. const char *authdata, int screen_number, bool oneshot);
  151. void ssh2channel_request_agent_forwarding(SshChannel *c, bool want_reply);
  152. void ssh2channel_request_pty(
  153. SshChannel *c, bool want_reply, Conf *conf, int w, int h);
  154. bool ssh2channel_send_env_var(
  155. SshChannel *c, bool want_reply, const char *var, const char *value);
  156. void ssh2channel_start_shell(SshChannel *c, bool want_reply);
  157. void ssh2channel_start_command(
  158. SshChannel *c, bool want_reply, const char *command);
  159. bool ssh2channel_start_subsystem(
  160. SshChannel *c, bool want_reply, const char *subsystem);
  161. bool ssh2channel_send_env_var(
  162. SshChannel *c, bool want_reply, const char *var, const char *value);
  163. bool ssh2channel_send_serial_break(
  164. SshChannel *c, bool want_reply, int length);
  165. bool ssh2channel_send_signal(
  166. SshChannel *c, bool want_reply, const char *signame);
  167. void ssh2channel_send_terminal_size_change(SshChannel *c, int w, int h);
  168. #define CHANOPEN_RETURN_FAILURE(code, msgparams) do \
  169. { \
  170. ChanopenResult toret; \
  171. toret.outcome = CHANOPEN_RESULT_FAILURE; \
  172. toret.u.failure.reason_code = code; \
  173. toret.u.failure.wire_message = dupprintf msgparams; \
  174. return toret; \
  175. } while (0)
  176. #define CHANOPEN_RETURN_SUCCESS(chan) do \
  177. { \
  178. ChanopenResult toret; \
  179. toret.outcome = CHANOPEN_RESULT_SUCCESS; \
  180. toret.u.success.channel = chan; \
  181. return toret; \
  182. } while (0)
  183. #define CHANOPEN_RETURN_DOWNSTREAM(shctx) do \
  184. { \
  185. ChanopenResult toret; \
  186. toret.outcome = CHANOPEN_RESULT_DOWNSTREAM; \
  187. toret.u.downstream.share_ctx = shctx; \
  188. return toret; \
  189. } while (0)
  190. ChanopenResult ssh2_connection_parse_channel_open(
  191. struct ssh2_connection_state *s, ptrlen type,
  192. PktIn *pktin, SshChannel *sc);
  193. bool ssh2_connection_parse_global_request(
  194. struct ssh2_connection_state *s, ptrlen type, PktIn *pktin);
  195. bool ssh2_connection_need_antispoof_prompt(struct ssh2_connection_state *s);
  196. #endif /* PUTTY_SSH2CONNECTION_H */