1
0

sshchan.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Abstraction of the various ways to handle the local end of an SSH
  3. * connection-layer channel.
  4. */
  5. #ifndef PUTTY_SSHCHAN_H
  6. #define PUTTY_SSHCHAN_H
  7. struct ChannelVtable {
  8. void (*free)(Channel *);
  9. /* Called for channel types that were created at the same time as
  10. * we sent an outgoing CHANNEL_OPEN, when the confirmation comes
  11. * back from the server indicating that the channel has been
  12. * opened, or the failure message indicating that it hasn't,
  13. * respectively. In the latter case, this must _not_ free the
  14. * Channel structure - the client will call the free method
  15. * separately. But it might do logging or other local cleanup. */
  16. void (*open_confirmation)(Channel *);
  17. void (*open_failed)(Channel *, const char *error_text);
  18. size_t (*send)(Channel *, bool is_stderr, const void *buf, size_t len);
  19. void (*send_eof)(Channel *);
  20. void (*set_input_wanted)(Channel *, bool wanted);
  21. char *(*log_close_msg)(Channel *);
  22. bool (*want_close)(Channel *, bool sent_local_eof, bool rcvd_remote_eof);
  23. /* A method for every channel request we know of. All of these
  24. * return true for success or false for failure. */
  25. bool (*rcvd_exit_status)(Channel *, int status);
  26. bool (*rcvd_exit_signal)(
  27. Channel *chan, ptrlen signame, bool core_dumped, ptrlen msg);
  28. bool (*rcvd_exit_signal_numeric)(
  29. Channel *chan, int signum, bool core_dumped, ptrlen msg);
  30. bool (*run_shell)(Channel *chan);
  31. bool (*run_command)(Channel *chan, ptrlen command);
  32. bool (*run_subsystem)(Channel *chan, ptrlen subsys);
  33. bool (*enable_x11_forwarding)(
  34. Channel *chan, bool oneshot, ptrlen authproto, ptrlen authdata,
  35. unsigned screen_number);
  36. bool (*enable_agent_forwarding)(Channel *chan);
  37. bool (*allocate_pty)(
  38. Channel *chan, ptrlen termtype, unsigned width, unsigned height,
  39. unsigned pixwidth, unsigned pixheight, struct ssh_ttymodes modes);
  40. bool (*set_env)(Channel *chan, ptrlen var, ptrlen value);
  41. bool (*send_break)(Channel *chan, unsigned length);
  42. bool (*send_signal)(Channel *chan, ptrlen signame);
  43. bool (*change_window_size)(
  44. Channel *chan, unsigned width, unsigned height,
  45. unsigned pixwidth, unsigned pixheight);
  46. /* A method for signalling success/failure responses to channel
  47. * requests initiated from the SshChannel vtable with want_reply
  48. * true. */
  49. void (*request_response)(Channel *, bool success);
  50. };
  51. struct Channel {
  52. const struct ChannelVtable *vt;
  53. unsigned initial_fixed_window_size;
  54. };
  55. static inline void chan_free(Channel *ch)
  56. { ch->vt->free(ch); }
  57. static inline void chan_open_confirmation(Channel *ch)
  58. { ch->vt->open_confirmation(ch); }
  59. static inline void chan_open_failed(Channel *ch, const char *err)
  60. { ch->vt->open_failed(ch, err); }
  61. static inline size_t chan_send(
  62. Channel *ch, bool err, const void *buf, size_t len)
  63. { return ch->vt->send(ch, err, buf, len); }
  64. static inline void chan_send_eof(Channel *ch)
  65. { ch->vt->send_eof(ch); }
  66. static inline void chan_set_input_wanted(Channel *ch, bool wanted)
  67. { ch->vt->set_input_wanted(ch, wanted); }
  68. static inline char *chan_log_close_msg(Channel *ch)
  69. { return ch->vt->log_close_msg(ch); }
  70. static inline bool chan_want_close(Channel *ch, bool leof, bool reof)
  71. { return ch->vt->want_close(ch, leof, reof); }
  72. static inline bool chan_rcvd_exit_status(Channel *ch, int status)
  73. { return ch->vt->rcvd_exit_status(ch, status); }
  74. static inline bool chan_rcvd_exit_signal(
  75. Channel *ch, ptrlen sig, bool core, ptrlen msg)
  76. { return ch->vt->rcvd_exit_signal(ch, sig, core, msg); }
  77. static inline bool chan_rcvd_exit_signal_numeric(
  78. Channel *ch, int sig, bool core, ptrlen msg)
  79. { return ch->vt->rcvd_exit_signal_numeric(ch, sig, core, msg); }
  80. static inline bool chan_run_shell(Channel *ch)
  81. { return ch->vt->run_shell(ch); }
  82. static inline bool chan_run_command(Channel *ch, ptrlen cmd)
  83. { return ch->vt->run_command(ch, cmd); }
  84. static inline bool chan_run_subsystem(Channel *ch, ptrlen subsys)
  85. { return ch->vt->run_subsystem(ch, subsys); }
  86. static inline bool chan_enable_x11_forwarding(
  87. Channel *ch, bool oneshot, ptrlen ap, ptrlen ad, unsigned scr)
  88. { return ch->vt->enable_x11_forwarding(ch, oneshot, ap, ad, scr); }
  89. static inline bool chan_enable_agent_forwarding(Channel *ch)
  90. { return ch->vt->enable_agent_forwarding(ch); }
  91. static inline bool chan_allocate_pty(
  92. Channel *ch, ptrlen termtype, unsigned w, unsigned h,
  93. unsigned pw, unsigned ph, struct ssh_ttymodes modes)
  94. { return ch->vt->allocate_pty(ch, termtype, w, h, pw, ph, modes); }
  95. static inline bool chan_set_env(Channel *ch, ptrlen var, ptrlen value)
  96. { return ch->vt->set_env(ch, var, value); }
  97. static inline bool chan_send_break(Channel *ch, unsigned length)
  98. { return ch->vt->send_break(ch, length); }
  99. static inline bool chan_send_signal(Channel *ch, ptrlen signame)
  100. { return ch->vt->send_signal(ch, signame); }
  101. static inline bool chan_change_window_size(
  102. Channel *ch, unsigned w, unsigned h, unsigned pw, unsigned ph)
  103. { return ch->vt->change_window_size(ch, w, h, pw, ph); }
  104. static inline void chan_request_response(Channel *ch, bool success)
  105. { ch->vt->request_response(ch, success); }
  106. /*
  107. * Reusable methods you can put in vtables to give default handling of
  108. * some of those functions.
  109. */
  110. /* open_confirmation / open_failed for any channel it doesn't apply to */
  111. void chan_remotely_opened_confirmation(Channel *chan);
  112. void chan_remotely_opened_failure(Channel *chan, const char *errtext);
  113. /* want_close for any channel that wants the default behaviour of not
  114. * closing until both directions have had an EOF */
  115. bool chan_default_want_close(Channel *, bool, bool);
  116. /* default implementations that refuse all the channel requests */
  117. bool chan_no_exit_status(Channel *, int);
  118. bool chan_no_exit_signal(Channel *, ptrlen, bool, ptrlen);
  119. bool chan_no_exit_signal_numeric(Channel *, int, bool, ptrlen);
  120. bool chan_no_run_shell(Channel *chan);
  121. bool chan_no_run_command(Channel *chan, ptrlen command);
  122. bool chan_no_run_subsystem(Channel *chan, ptrlen subsys);
  123. bool chan_no_enable_x11_forwarding(
  124. Channel *chan, bool oneshot, ptrlen authproto, ptrlen authdata,
  125. unsigned screen_number);
  126. bool chan_no_enable_agent_forwarding(Channel *chan);
  127. bool chan_no_allocate_pty(
  128. Channel *chan, ptrlen termtype, unsigned width, unsigned height,
  129. unsigned pixwidth, unsigned pixheight, struct ssh_ttymodes modes);
  130. bool chan_no_set_env(Channel *chan, ptrlen var, ptrlen value);
  131. bool chan_no_send_break(Channel *chan, unsigned length);
  132. bool chan_no_send_signal(Channel *chan, ptrlen signame);
  133. bool chan_no_change_window_size(
  134. Channel *chan, unsigned width, unsigned height,
  135. unsigned pixwidth, unsigned pixheight);
  136. /* default implementation that never expects to receive a response */
  137. void chan_no_request_response(Channel *, bool);
  138. /*
  139. * Constructor for a trivial do-nothing implementation of
  140. * ChannelVtable. Used for 'zombie' channels, i.e. channels whose
  141. * proper local source of data has been shut down or otherwise stopped
  142. * existing, but the SSH side is still there and needs some kind of a
  143. * Channel implementation to talk to. In particular, the want_close
  144. * method for this channel always returns 'yes, please close this
  145. * channel asap', regardless of whether local and/or remote EOF have
  146. * been sent - indeed, even if _neither_ has.
  147. */
  148. Channel *zombiechan_new(void);
  149. /* ----------------------------------------------------------------------
  150. * This structure is owned by an SSH connection layer, and identifies
  151. * the connection layer's end of the channel, for the Channel
  152. * implementation to talk back to.
  153. */
  154. struct SshChannelVtable {
  155. size_t (*write)(SshChannel *c, bool is_stderr, const void *, size_t);
  156. void (*write_eof)(SshChannel *c);
  157. void (*initiate_close)(SshChannel *c, const char *err);
  158. void (*unthrottle)(SshChannel *c, size_t bufsize);
  159. Conf *(*get_conf)(SshChannel *c);
  160. void (*window_override_removed)(SshChannel *c);
  161. void (*x11_sharing_handover)(SshChannel *c,
  162. ssh_sharing_connstate *share_cs,
  163. share_channel *share_chan,
  164. const char *peer_addr, int peer_port,
  165. int endian, int protomajor, int protominor,
  166. const void *initial_data, int initial_len);
  167. /*
  168. * All the outgoing channel requests we support. Each one has a
  169. * want_reply flag, which will cause a callback to
  170. * chan_request_response when the result is available.
  171. *
  172. * The ones that return 'bool' use it to indicate that the SSH
  173. * protocol in use doesn't support this request at all.
  174. *
  175. * (It's also intentional that not all of them have a want_reply
  176. * flag: the ones that don't are because SSH-1 has no method for
  177. * signalling success or failure of that request, or because we
  178. * wouldn't do anything usefully different with the reply in any
  179. * case.)
  180. */
  181. void (*send_exit_status)(SshChannel *c, int status);
  182. void (*send_exit_signal)(
  183. SshChannel *c, ptrlen signame, bool core_dumped, ptrlen msg);
  184. void (*send_exit_signal_numeric)(
  185. SshChannel *c, int signum, bool core_dumped, ptrlen msg);
  186. void (*request_x11_forwarding)(
  187. SshChannel *c, bool want_reply, const char *authproto,
  188. const char *authdata, int screen_number, bool oneshot);
  189. void (*request_agent_forwarding)(
  190. SshChannel *c, bool want_reply);
  191. void (*request_pty)(
  192. SshChannel *c, bool want_reply, Conf *conf, int w, int h);
  193. bool (*send_env_var)(
  194. SshChannel *c, bool want_reply, const char *var, const char *value);
  195. void (*start_shell)(
  196. SshChannel *c, bool want_reply);
  197. void (*start_command)(
  198. SshChannel *c, bool want_reply, const char *command);
  199. bool (*start_subsystem)(
  200. SshChannel *c, bool want_reply, const char *subsystem);
  201. bool (*send_serial_break)(
  202. SshChannel *c, bool want_reply, int length); /* length=0 for default */
  203. bool (*send_signal)(
  204. SshChannel *c, bool want_reply, const char *signame);
  205. void (*send_terminal_size_change)(
  206. SshChannel *c, int w, int h);
  207. void (*hint_channel_is_simple)(SshChannel *c);
  208. };
  209. struct SshChannel {
  210. const struct SshChannelVtable *vt;
  211. ConnectionLayer *cl;
  212. };
  213. static inline size_t sshfwd_write_ext(
  214. SshChannel *c, bool is_stderr, const void *data, size_t len)
  215. { return c->vt->write(c, is_stderr, data, len); }
  216. static inline size_t sshfwd_write(SshChannel *c, const void *data, size_t len)
  217. { return sshfwd_write_ext(c, false, data, len); }
  218. static inline void sshfwd_write_eof(SshChannel *c)
  219. { c->vt->write_eof(c); }
  220. static inline void sshfwd_initiate_close(SshChannel *c, const char *err)
  221. { c->vt->initiate_close(c, err); }
  222. static inline void sshfwd_unthrottle(SshChannel *c, size_t bufsize)
  223. { c->vt->unthrottle(c, bufsize); }
  224. static inline Conf *sshfwd_get_conf(SshChannel *c)
  225. { return c->vt->get_conf(c); }
  226. static inline void sshfwd_window_override_removed(SshChannel *c)
  227. { c->vt->window_override_removed(c); }
  228. static inline void sshfwd_x11_sharing_handover(
  229. SshChannel *c, ssh_sharing_connstate *cs, share_channel *sch,
  230. const char *addr, int port, int endian, int maj, int min,
  231. const void *idata, int ilen)
  232. { /*WINSCP return*/ c->vt->x11_sharing_handover(c, cs, sch, addr, port, endian,
  233. maj, min, idata, ilen); }
  234. static inline void sshfwd_send_exit_status(SshChannel *c, int status)
  235. { c->vt->send_exit_status(c, status); }
  236. static inline void sshfwd_send_exit_signal(
  237. SshChannel *c, ptrlen signame, bool core_dumped, ptrlen msg)
  238. { c->vt->send_exit_signal(c, signame, core_dumped, msg); }
  239. static inline void sshfwd_send_exit_signal_numeric(
  240. SshChannel *c, int signum, bool core_dumped, ptrlen msg)
  241. { c->vt->send_exit_signal_numeric(c, signum, core_dumped, msg); }
  242. static inline void sshfwd_request_x11_forwarding(
  243. SshChannel *c, bool want_reply, const char *proto,
  244. const char *data, int scr, bool once)
  245. { c->vt->request_x11_forwarding(c, want_reply, proto, data, scr, once); }
  246. static inline void sshfwd_request_agent_forwarding(
  247. SshChannel *c, bool want_reply)
  248. { c->vt->request_agent_forwarding(c, want_reply); }
  249. static inline void sshfwd_request_pty(
  250. SshChannel *c, bool want_reply, Conf *conf, int w, int h)
  251. { c->vt->request_pty(c, want_reply, conf, w, h); }
  252. static inline bool sshfwd_send_env_var(
  253. SshChannel *c, bool want_reply, const char *var, const char *value)
  254. { return c->vt->send_env_var(c, want_reply, var, value); }
  255. static inline void sshfwd_start_shell(
  256. SshChannel *c, bool want_reply)
  257. { c->vt->start_shell(c, want_reply); }
  258. static inline void sshfwd_start_command(
  259. SshChannel *c, bool want_reply, const char *command)
  260. { c->vt->start_command(c, want_reply, command); }
  261. static inline bool sshfwd_start_subsystem(
  262. SshChannel *c, bool want_reply, const char *subsystem)
  263. { return c->vt->start_subsystem(c, want_reply, subsystem); }
  264. static inline bool sshfwd_send_serial_break(
  265. SshChannel *c, bool want_reply, int length)
  266. { return c->vt->send_serial_break(c, want_reply, length); }
  267. static inline bool sshfwd_send_signal(
  268. SshChannel *c, bool want_reply, const char *signame)
  269. { return c->vt->send_signal(c, want_reply, signame); }
  270. static inline void sshfwd_send_terminal_size_change(
  271. SshChannel *c, int w, int h)
  272. { c->vt->send_terminal_size_change(c, w, h); }
  273. static inline void sshfwd_hint_channel_is_simple(SshChannel *c)
  274. { c->vt->hint_channel_is_simple(c); }
  275. /* ----------------------------------------------------------------------
  276. * The 'main' or primary channel of the SSH connection is special,
  277. * because it's the one that's connected directly to parts of the
  278. * frontend such as the terminal and the specials menu. So it exposes
  279. * a richer API.
  280. */
  281. mainchan *mainchan_new(
  282. PacketProtocolLayer *ppl, ConnectionLayer *cl, Conf *conf,
  283. int term_width, int term_height, bool is_simple, SshChannel **sc_out);
  284. void mainchan_get_specials(
  285. mainchan *mc, add_special_fn_t add_special, void *ctx);
  286. void mainchan_special_cmd(mainchan *mc, SessionSpecialCode code, int arg);
  287. void mainchan_terminal_size(mainchan *mc, int width, int height);
  288. #endif /* PUTTY_SSHCHAN_H */