ssh2connection.h 8.1 KB

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