ppl.h 7.8 KB

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