1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00
putty-source/ssh2connection.h

223 lines
7.4 KiB
C
Raw Normal View History

Move client-specific SSH code into new files. This is a major code reorganisation in preparation for making this code base into one that can build an SSH server as well as a client. (Mostly for purposes of using the server as a regression test suite for the client, though I have some other possible uses in mind too. However, it's currently no part of my plan to harden the server to the point where it can sensibly be deployed in a hostile environment.) In this preparatory commit, I've broken up the SSH-2 transport and connection layers, and the SSH-1 connection layer, into multiple source files, with each layer having its own header file containing the shared type definitions. In each case, the new source file contains code that's specific to the client side of the protocol, so that a new file can be swapped in in its place when building the server. Mostly this is just a straightforward moving of code without changing it very much, but there are a couple of actual changes in the process: The parsing of SSH-2 global-request and channel open-messages is now done by a new pair of functions in the client module. For channel opens, I've invented a new union data type to be the return value from that function, representing either failure (plus error message), success (plus Channel instance to manage the new channel), or an instruction to hand the channel over to a sharing downstream (plus a pointer to the downstream in question). Also, the tree234 of remote port forwardings in ssh2connection is now initialised on first use by the client-specific code, so that's where its compare function lives. The shared ssh2connection_free() still takes responsibility for freeing it, but now has to check if it's non-null first. The outer shell of the ssh2_lportfwd_open method, for making a local-to-remote port forwarding, is still centralised in ssh2connection.c, but the part of it that actually constructs the outgoing channel-open message has moved into the client code, because that will have to change depending on whether the channel-open has to have type direct-tcpip or forwarded-tcpip. In the SSH-1 connection layer, half the filter_queue method has moved out into the new client-specific code, but not all of it - bidirectional channel maintenance messages are still handled centrally. One exception is SSH_MSG_PORT_OPEN, which can be sent in both directions, but with subtly different semantics - from server to client, it's referring to a previously established remote forwarding (and must be rejected if there isn't one that matches it), but from client to server it's just a "direct-tcpip" request with no prior context. So that one is in the client-specific module, and when I add the server code it will have its own different handler.
2018-10-20 16:57:37 +00:00
#ifndef PUTTY_SSH2CONNECTION_H
#define PUTTY_SSH2CONNECTION_H
struct outstanding_channel_request;
struct outstanding_global_request;
struct ssh2_connection_state {
int crState;
Ssh *ssh;
ssh_sharing_state *connshare;
char *peer_verstring;
mainchan *mainchan;
SshChannel *mainchan_sc;
int ldisc_opts[LD_N_OPTIONS];
int session_attempt, session_status;
int term_width, term_height;
int want_user_input;
int ssh_is_simple;
int persistent;
Conf *conf;
tree234 *channels; /* indexed by local id */
int all_channels_throttled;
int X11_fwd_enabled;
tree234 *x11authtree;
int got_pty;
int agent_fwd_enabled;
tree234 *rportfwds;
PortFwdManager *portfwdmgr;
int portfwdmgr_configured;
/*
* These store the list of global requests that we're waiting for
* replies to. (REQUEST_FAILURE doesn't come with any indication
* of what message caused it, so we have to keep track of the
* queue ourselves.)
*/
struct outstanding_global_request *globreq_head, *globreq_tail;
ConnectionLayer cl;
PacketProtocolLayer ppl;
};
typedef void (*gr_handler_fn_t)(struct ssh2_connection_state *s,
PktIn *pktin, void *ctx);
void ssh2_queue_global_request_handler(
struct ssh2_connection_state *s, gr_handler_fn_t handler, void *ctx);
struct ssh2_channel {
struct ssh2_connection_state *connlayer;
unsigned remoteid, localid;
int type;
/* True if we opened this channel but server hasn't confirmed. */
int halfopen;
/* Bitmap of whether we've sent/received CHANNEL_EOF and
* CHANNEL_CLOSE. */
#define CLOSES_SENT_EOF 1
#define CLOSES_SENT_CLOSE 2
#define CLOSES_RCVD_EOF 4
#define CLOSES_RCVD_CLOSE 8
int closes;
/*
* This flag indicates that an EOF is pending on the outgoing side
* of the channel: that is, wherever we're getting the data for
* this channel has sent us some data followed by EOF. We can't
* actually send the EOF until we've finished sending the data, so
* we set this flag instead to remind us to do so once our buffer
* is clear.
*/
int pending_eof;
/*
* True if this channel is causing the underlying connection to be
* throttled.
*/
int throttling_conn;
/*
* True if we currently have backed-up data on the direction of
* this channel pointing out of the SSH connection, and therefore
* would prefer the 'Channel' implementation not to read further
* local input if possible.
*/
int throttled_by_backlog;
bufchain outbuffer;
unsigned remwindow, remmaxpkt;
/* locwindow is signed so we can cope with excess data. */
int locwindow, locmaxwin;
/*
* remlocwin is the amount of local window that we think
* the remote end had available to it after it sent the
* last data packet or window adjust ack.
*/
int remlocwin;
/*
* These store the list of channel requests that we're waiting for
* replies to. (CHANNEL_FAILURE doesn't come with any indication
* of what message caused it, so we have to keep track of the
* queue ourselves.)
*/
struct outstanding_channel_request *chanreq_head, *chanreq_tail;
enum { THROTTLED, UNTHROTTLING, UNTHROTTLED } throttle_state;
ssh_sharing_connstate *sharectx; /* sharing context, if this is a
* downstream channel */
Channel *chan; /* handle the client side of this channel, if not */
SshChannel sc; /* entry point for chan to talk back to */
};
typedef void (*cr_handler_fn_t)(struct ssh2_channel *, PktIn *, void *);
void ssh2_channel_init(struct ssh2_channel *c);
PktOut *ssh2_chanreq_init(struct ssh2_channel *c, const char *type,
cr_handler_fn_t handler, void *ctx);
typedef enum ChanopenOutcome {
CHANOPEN_RESULT_FAILURE,
CHANOPEN_RESULT_SUCCESS,
CHANOPEN_RESULT_DOWNSTREAM,
} ChanopenOutcome;
typedef struct ChanopenResult {
ChanopenOutcome outcome;
union {
struct {
char *wire_message; /* must be freed by recipient */
unsigned reason_code;
} failure;
struct {
Channel *channel;
} success;
struct {
ssh_sharing_connstate *share_ctx;
} downstream;
} u;
} ChanopenResult;
PktOut *ssh2_chanopen_init(struct ssh2_channel *c, const char *type);
PktOut *ssh2_portfwd_chanopen(
struct ssh2_connection_state *s, struct ssh2_channel *c,
const char *hostname, int port,
const char *description, const SocketPeerInfo *peerinfo);
struct ssh_rportfwd *ssh2_rportfwd_alloc(
ConnectionLayer *cl,
const char *shost, int sport, const char *dhost, int dport,
int addressfamily, const char *log_description, PortFwdRecord *pfr,
ssh_sharing_connstate *share_ctx);
void ssh2_rportfwd_remove(
ConnectionLayer *cl, struct ssh_rportfwd *rpf);
SshChannel *ssh2_session_open(ConnectionLayer *cl, Channel *chan);
void ssh2channel_request_x11_forwarding(
SshChannel *c, int want_reply, const char *authproto,
const char *authdata, int screen_number, int oneshot);
void ssh2channel_request_agent_forwarding(SshChannel *c, int want_reply);
void ssh2channel_request_pty(
SshChannel *c, int want_reply, Conf *conf, int w, int h);
int ssh2channel_send_env_var(
SshChannel *c, int want_reply, const char *var, const char *value);
void ssh2channel_start_shell(SshChannel *c, int want_reply);
void ssh2channel_start_command(
SshChannel *c, int want_reply, const char *command);
int ssh2channel_start_subsystem(
SshChannel *c, int want_reply, const char *subsystem);
int ssh2channel_send_env_var(
SshChannel *c, int want_reply, const char *var, const char *value);
int ssh2channel_send_serial_break(
SshChannel *c, int want_reply, int length);
int ssh2channel_send_signal(
SshChannel *c, int want_reply, const char *signame);
void ssh2channel_send_terminal_size_change(SshChannel *c, int w, int h);
#define CHANOPEN_RETURN_FAILURE(code, msgparams) do \
{ \
ChanopenResult toret; \
toret.outcome = CHANOPEN_RESULT_FAILURE; \
toret.u.failure.reason_code = code; \
toret.u.failure.wire_message = dupprintf msgparams; \
return toret; \
} while (0)
#define CHANOPEN_RETURN_SUCCESS(chan) do \
{ \
ChanopenResult toret; \
toret.outcome = CHANOPEN_RESULT_SUCCESS; \
toret.u.success.channel = chan; \
return toret; \
} while (0)
#define CHANOPEN_RETURN_DOWNSTREAM(shctx) do \
{ \
ChanopenResult toret; \
toret.outcome = CHANOPEN_RESULT_DOWNSTREAM; \
toret.u.downstream.share_ctx = shctx; \
return toret; \
} while (0)
ChanopenResult ssh2_connection_parse_channel_open(
struct ssh2_connection_state *s, ptrlen type,
PktIn *pktin, SshChannel *sc);
int ssh2_connection_parse_global_request(
struct ssh2_connection_state *s, ptrlen type, PktIn *pktin);
#endif /* PUTTY_SSH2CONNECTION_H */