sshppl.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Abstraction of the various layers of SSH packet-level protocol,
  3. * general enough to take in all three of the main SSH-2 layers and
  4. * both of the SSH-1 phases.
  5. */
  6. #ifndef PUTTY_SSHPPL_H
  7. #define PUTTY_SSHPPL_H
  8. typedef void (*packet_handler_fn_t)(PacketProtocolLayer *ppl, PktIn *pktin);
  9. struct PacketProtocolLayerVtable {
  10. void (*free)(PacketProtocolLayer *);
  11. void (*process_queue)(PacketProtocolLayer *ppl);
  12. bool (*get_specials)(
  13. PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx);
  14. void (*special_cmd)(
  15. PacketProtocolLayer *ppl, SessionSpecialCode code, int arg);
  16. bool (*want_user_input)(PacketProtocolLayer *ppl);
  17. void (*got_user_input)(PacketProtocolLayer *ppl);
  18. void (*reconfigure)(PacketProtocolLayer *ppl, Conf *conf);
  19. size_t (*queued_data_size)(PacketProtocolLayer *ppl);
  20. /* Protocol-level name of this layer. */
  21. const char *name;
  22. unsigned int (*winscp_query)(PacketProtocolLayer *ppl, int query);
  23. };
  24. struct PacketProtocolLayer {
  25. const struct PacketProtocolLayerVtable *vt;
  26. /* Link to the underlying SSH BPP. */
  27. BinaryPacketProtocol *bpp;
  28. /* Queue from which the layer receives its input packets, and one
  29. * to put its output packets on. */
  30. PktInQueue *in_pq;
  31. PktOutQueue *out_pq;
  32. /* Idempotent callback that in_pq will be linked to, causing a
  33. * call to the process_queue method. in_pq points to this, so it
  34. * will be automatically triggered by pushing things on the
  35. * layer's input queue, but it can also be triggered on purpose. */
  36. IdempotentCallback ic_process_queue;
  37. /* Owner's pointer to this layer. Permits a layer to unilaterally
  38. * abdicate in favour of a replacement, by overwriting this
  39. * pointer and then freeing itself. */
  40. PacketProtocolLayer **selfptr;
  41. /* Bufchain of keyboard input from the user, for login prompts and
  42. * similar. */
  43. bufchain *user_input;
  44. /* Logging and error-reporting facilities. */
  45. LogContext *logctx;
  46. Seat *seat; /* for dialog boxes, session output etc */
  47. Ssh *ssh; /* for session termination + assorted connection-layer ops */
  48. /* Known bugs in the remote implementation. */
  49. unsigned remote_bugs;
  50. };
  51. static inline void ssh_ppl_process_queue(PacketProtocolLayer *ppl)
  52. { ppl->vt->process_queue(ppl); }
  53. static inline bool ssh_ppl_get_specials(
  54. PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx)
  55. { return ppl->vt->get_specials(ppl, add_special, ctx); }
  56. static inline void ssh_ppl_special_cmd(
  57. PacketProtocolLayer *ppl, SessionSpecialCode code, int arg)
  58. { ppl->vt->special_cmd(ppl, code, arg); }
  59. static inline bool ssh_ppl_want_user_input(PacketProtocolLayer *ppl)
  60. { return ppl->vt->want_user_input(ppl); }
  61. static inline void ssh_ppl_got_user_input(PacketProtocolLayer *ppl)
  62. { ppl->vt->got_user_input(ppl); }
  63. static inline void ssh_ppl_reconfigure(PacketProtocolLayer *ppl, Conf *conf)
  64. { ppl->vt->reconfigure(ppl, conf); }
  65. static inline size_t ssh_ppl_queued_data_size(PacketProtocolLayer *ppl)
  66. { return ppl->vt->queued_data_size(ppl); }
  67. static inline unsigned int ssh_ppl_winscp_query(PacketProtocolLayer *ppl, int query)
  68. { return ppl->vt->winscp_query(ppl, query); }
  69. /* ssh_ppl_free is more than just a macro wrapper on the vtable; it
  70. * does centralised parts of the freeing too. */
  71. void ssh_ppl_free(PacketProtocolLayer *ppl);
  72. /* Helper routine to point a PPL at its input and output queues. Also
  73. * sets up the IdempotentCallback on the input queue to trigger a call
  74. * to process_queue whenever packets are added to it. */
  75. void ssh_ppl_setup_queues(PacketProtocolLayer *ppl,
  76. PktInQueue *inq, PktOutQueue *outq);
  77. /* Routine a PPL can call to abdicate in favour of a replacement, by
  78. * overwriting ppl->selfptr. Has the side effect of freeing 'old', so
  79. * if 'old' actually called this (which is likely) then it should
  80. * avoid dereferencing itself on return from this function! */
  81. void ssh_ppl_replace(PacketProtocolLayer *old, PacketProtocolLayer *new);
  82. /* Default implementation of queued_data_size, which just adds up the
  83. * sizes of all the packets in pq_out. A layer can override this if it
  84. * has other things to take into account as well. */
  85. size_t ssh_ppl_default_queued_data_size(PacketProtocolLayer *ppl);
  86. PacketProtocolLayer *ssh1_login_new(
  87. Conf *conf, const char *host, int port,
  88. PacketProtocolLayer *successor_layer);
  89. PacketProtocolLayer *ssh1_connection_new(
  90. Ssh *ssh, Conf *conf, ConnectionLayer **cl_out);
  91. struct DataTransferStats;
  92. struct ssh_connection_shared_gss_state;
  93. PacketProtocolLayer *ssh2_transport_new(
  94. Conf *conf, const char *host, int port, const char *fullhostname,
  95. const char *client_greeting, const char *server_greeting,
  96. struct ssh_connection_shared_gss_state *shgss,
  97. struct DataTransferStats *stats, PacketProtocolLayer *higher_layer,
  98. const SshServerConfig *ssc);
  99. PacketProtocolLayer *ssh2_userauth_new(
  100. PacketProtocolLayer *successor_layer,
  101. const char *hostname, const char *fullhostname,
  102. Filename *keyfile, bool show_banner, bool tryagent,
  103. const char *default_username, bool change_username,
  104. bool try_ki_auth,
  105. bool try_gssapi_auth, bool try_gssapi_kex_auth,
  106. bool gssapi_fwd, struct ssh_connection_shared_gss_state *shgss,
  107. const char * loghost, bool change_password); // WINSCP
  108. PacketProtocolLayer *ssh2_connection_new(
  109. Ssh *ssh, ssh_sharing_state *connshare, bool is_simple,
  110. Conf *conf, const char *peer_verstring, ConnectionLayer **cl_out);
  111. /* Can't put this in the userauth constructor without having a
  112. * dependency loop at setup time (transport and userauth can't _both_
  113. * be constructed second and given a pointer to the other). */
  114. void ssh2_userauth_set_transport_layer(PacketProtocolLayer *userauth,
  115. PacketProtocolLayer *transport);
  116. /* Convenience macro for protocol layers to send formatted strings to
  117. * the Event Log. Assumes a function parameter called 'ppl' is in
  118. * scope. */
  119. #define ppl_logevent(...) ( \
  120. logevent_and_free((ppl)->logctx, dupprintf(__VA_ARGS__)))
  121. /* Convenience macro for protocol layers to send formatted strings to
  122. * the terminal. Also expects 'ppl' to be in scope. */
  123. #define ppl_printf(...) \
  124. ssh_ppl_user_output_string_and_free(ppl, dupprintf(__VA_ARGS__))
  125. void ssh_ppl_user_output_string_and_free(PacketProtocolLayer *ppl, char *text);
  126. /* Methods for userauth to communicate back to the transport layer */
  127. ptrlen ssh2_transport_get_session_id(PacketProtocolLayer *ssh2_transport_ptr);
  128. void ssh2_transport_notify_auth_done(PacketProtocolLayer *ssh2_transport_ptr);
  129. /* Shared method between ssh2 layers (defined in ssh2transport.c) to
  130. * handle the common packets between login and connection: DISCONNECT,
  131. * DEBUG and IGNORE. Those messages are handled by the ssh2transport
  132. * layer if we have one, but in bare ssh2-connection mode they have to
  133. * be handled by ssh2connection. */
  134. bool ssh2_common_filter_queue(PacketProtocolLayer *ppl);
  135. /* Methods for ssh1login to pass protocol flags to ssh1connection */
  136. void ssh1_connection_set_protoflags(
  137. PacketProtocolLayer *ppl, int local, int remote);
  138. /* Shared get_specials method between the two ssh1 layers */
  139. bool ssh1_common_get_specials(PacketProtocolLayer *, add_special_fn_t, void *);
  140. /* Other shared functions between ssh1 layers */
  141. bool ssh1_common_filter_queue(PacketProtocolLayer *ppl);
  142. void ssh1_compute_session_id(
  143. unsigned char *session_id, const unsigned char *cookie,
  144. RSAKey *hostkey, RSAKey *servkey);
  145. /* Method used by the SSH server */
  146. void ssh2_transport_provide_hostkeys(PacketProtocolLayer *ssh2_transport_ptr,
  147. ssh_key *const *hostkeys, int nhostkeys);
  148. #endif /* PUTTY_SSHPPL_H */