2 * Packet protocol layer for the SSH-2 connection protocol (RFC 4254).
13 #include "connection2.h"
15 static void ssh2_connection_free(PacketProtocolLayer
*);
16 static void ssh2_connection_process_queue(PacketProtocolLayer
*);
17 static bool ssh2_connection_get_specials(
18 PacketProtocolLayer
*ppl
, add_special_fn_t add_special
, void *ctx
);
19 static void ssh2_connection_special_cmd(PacketProtocolLayer
*ppl
,
20 SessionSpecialCode code
, int arg
);
21 static void ssh2_connection_reconfigure(PacketProtocolLayer
*ppl
, Conf
*conf
);
23 static const PacketProtocolLayerVtable ssh2_connection_vtable
= {
24 .free
= ssh2_connection_free
,
25 .process_queue
= ssh2_connection_process_queue
,
26 .get_specials
= ssh2_connection_get_specials
,
27 .special_cmd
= ssh2_connection_special_cmd
,
28 .reconfigure
= ssh2_connection_reconfigure
,
29 .queued_data_size
= ssh_ppl_default_queued_data_size
,
30 .final_output
= ssh_ppl_default_final_output
,
31 .name
= "ssh-connection",
34 static SshChannel
*ssh2_lportfwd_open(
35 ConnectionLayer
*cl
, const char *hostname
, int port
,
36 const char *description
, const SocketPeerInfo
*pi
, Channel
*chan
);
37 static struct X11FakeAuth
*ssh2_add_x11_display(
38 ConnectionLayer
*cl
, int authtype
, struct X11Display
*x11disp
);
39 static struct X11FakeAuth
*ssh2_add_sharing_x11_display(
40 ConnectionLayer
*cl
, int authtype
, ssh_sharing_connstate
*share_cs
,
41 share_channel
*share_chan
);
42 static void ssh2_remove_sharing_x11_display(ConnectionLayer
*cl
,
43 struct X11FakeAuth
*auth
);
44 static void ssh2_send_packet_from_downstream(
45 ConnectionLayer
*cl
, unsigned id
, int type
,
46 const void *pkt
, int pktlen
, const char *additional_log_text
);
47 static unsigned ssh2_alloc_sharing_channel(
48 ConnectionLayer
*cl
, ssh_sharing_connstate
*connstate
);
49 static void ssh2_delete_sharing_channel(
50 ConnectionLayer
*cl
, unsigned localid
);
51 static void ssh2_sharing_queue_global_request(
52 ConnectionLayer
*cl
, ssh_sharing_connstate
*share_ctx
);
53 static void ssh2_sharing_no_more_downstreams(ConnectionLayer
*cl
);
54 static bool ssh2_agent_forwarding_permitted(ConnectionLayer
*cl
);
55 static void ssh2_terminal_size(ConnectionLayer
*cl
, int width
, int height
);
56 static void ssh2_stdout_unthrottle(ConnectionLayer
*cl
, size_t bufsize
);
57 static size_t ssh2_stdin_backlog(ConnectionLayer
*cl
);
58 static void ssh2_throttle_all_channels(ConnectionLayer
*cl
, bool throttled
);
59 static bool ssh2_ldisc_option(ConnectionLayer
*cl
, int option
);
60 static void ssh2_set_ldisc_option(ConnectionLayer
*cl
, int option
, bool value
);
61 static void ssh2_enable_x_fwd(ConnectionLayer
*cl
);
62 static void ssh2_set_wants_user_input(ConnectionLayer
*cl
, bool wanted
);
63 static bool ssh2_get_wants_user_input(ConnectionLayer
*cl
);
64 static void ssh2_got_user_input(ConnectionLayer
*cl
);
66 static const ConnectionLayerVtable ssh2_connlayer_vtable
= {
67 .rportfwd_alloc
= ssh2_rportfwd_alloc
,
68 .rportfwd_remove
= ssh2_rportfwd_remove
,
69 .lportfwd_open
= ssh2_lportfwd_open
,
70 .session_open
= ssh2_session_open
,
71 .serverside_x11_open
= ssh2_serverside_x11_open
,
72 .serverside_agent_open
= ssh2_serverside_agent_open
,
73 .add_x11_display
= ssh2_add_x11_display
,
74 .add_sharing_x11_display
= ssh2_add_sharing_x11_display
,
75 .remove_sharing_x11_display
= ssh2_remove_sharing_x11_display
,
76 .send_packet_from_downstream
= ssh2_send_packet_from_downstream
,
77 .alloc_sharing_channel
= ssh2_alloc_sharing_channel
,
78 .delete_sharing_channel
= ssh2_delete_sharing_channel
,
79 .sharing_queue_global_request
= ssh2_sharing_queue_global_request
,
80 .sharing_no_more_downstreams
= ssh2_sharing_no_more_downstreams
,
81 .agent_forwarding_permitted
= ssh2_agent_forwarding_permitted
,
82 .terminal_size
= ssh2_terminal_size
,
83 .stdout_unthrottle
= ssh2_stdout_unthrottle
,
84 .stdin_backlog
= ssh2_stdin_backlog
,
85 .throttle_all_channels
= ssh2_throttle_all_channels
,
86 .ldisc_option
= ssh2_ldisc_option
,
87 .set_ldisc_option
= ssh2_set_ldisc_option
,
88 .enable_x_fwd
= ssh2_enable_x_fwd
,
89 .set_wants_user_input
= ssh2_set_wants_user_input
,
90 .get_wants_user_input
= ssh2_get_wants_user_input
,
91 .got_user_input
= ssh2_got_user_input
,
94 static char *ssh2_channel_open_failure_error_text(PktIn
*pktin
)
96 static const char *const reasons
[] = {
98 "Administratively prohibited",
100 "Unknown channel type",
103 unsigned reason_code
;
104 const char *reason_code_string
;
105 char reason_code_buf
[256];
108 reason_code
= get_uint32(pktin
);
109 if (reason_code
< lenof(reasons
) && reasons
[reason_code
]) {
110 reason_code_string
= reasons
[reason_code
];
112 reason_code_string
= reason_code_buf
;
113 sprintf(reason_code_buf
, "unknown reason code %#x", reason_code
);
116 reason
= get_string(pktin
);
118 return dupprintf("%s [%.*s]", reason_code_string
, PTRLEN_PRINTF(reason
));
121 static size_t ssh2channel_write(
122 SshChannel
*c
, bool is_stderr
, const void *buf
, size_t len
);
123 static void ssh2channel_write_eof(SshChannel
*c
);
124 static void ssh2channel_initiate_close(SshChannel
*c
, const char *err
);
125 static void ssh2channel_unthrottle(SshChannel
*c
, size_t bufsize
);
126 static Conf
*ssh2channel_get_conf(SshChannel
*c
);
127 static void ssh2channel_window_override_removed(SshChannel
*c
);
128 static void ssh2channel_x11_sharing_handover(
129 SshChannel
*c
, ssh_sharing_connstate
*share_cs
, share_channel
*share_chan
,
130 const char *peer_addr
, int peer_port
, int endian
,
131 int protomajor
, int protominor
, const void *initial_data
, int initial_len
);
132 static void ssh2channel_hint_channel_is_simple(SshChannel
*c
);
134 static const SshChannelVtable ssh2channel_vtable
= {
135 .write
= ssh2channel_write
,
136 .write_eof
= ssh2channel_write_eof
,
137 .initiate_close
= ssh2channel_initiate_close
,
138 .unthrottle
= ssh2channel_unthrottle
,
139 .get_conf
= ssh2channel_get_conf
,
140 .window_override_removed
= ssh2channel_window_override_removed
,
141 .x11_sharing_handover
= ssh2channel_x11_sharing_handover
,
142 .send_exit_status
= ssh2channel_send_exit_status
,
143 .send_exit_signal
= ssh2channel_send_exit_signal
,
144 .send_exit_signal_numeric
= ssh2channel_send_exit_signal_numeric
,
145 .request_x11_forwarding
= ssh2channel_request_x11_forwarding
,
146 .request_agent_forwarding
= ssh2channel_request_agent_forwarding
,
147 .request_pty
= ssh2channel_request_pty
,
148 .send_env_var
= ssh2channel_send_env_var
,
149 .start_shell
= ssh2channel_start_shell
,
150 .start_command
= ssh2channel_start_command
,
151 .start_subsystem
= ssh2channel_start_subsystem
,
152 .send_serial_break
= ssh2channel_send_serial_break
,
153 .send_signal
= ssh2channel_send_signal
,
154 .send_terminal_size_change
= ssh2channel_send_terminal_size_change
,
155 .hint_channel_is_simple
= ssh2channel_hint_channel_is_simple
,
158 static void ssh2_channel_check_close(struct ssh2_channel
*c
);
159 static void ssh2_channel_try_eof(struct ssh2_channel
*c
);
160 static void ssh2_set_window(struct ssh2_channel
*c
, int newwin
);
161 static size_t ssh2_try_send(struct ssh2_channel
*c
);
162 static void ssh2_try_send_and_unthrottle(struct ssh2_channel
*c
);
163 static void ssh2_channel_check_throttle(struct ssh2_channel
*c
);
164 static void ssh2_channel_close_local(struct ssh2_channel
*c
,
166 static void ssh2_channel_destroy(struct ssh2_channel
*c
);
168 static void ssh2_check_termination(struct ssh2_connection_state
*s
);
170 struct outstanding_global_request
{
171 gr_handler_fn_t handler
;
173 struct outstanding_global_request
*next
;
175 void ssh2_queue_global_request_handler(
176 struct ssh2_connection_state
*s
, gr_handler_fn_t handler
, void *ctx
)
178 struct outstanding_global_request
*ogr
=
179 snew(struct outstanding_global_request
);
180 ogr
->handler
= handler
;
184 s
->globreq_tail
->next
= ogr
;
186 s
->globreq_head
= ogr
;
187 s
->globreq_tail
= ogr
;
190 static int ssh2_channelcmp(void *av
, void *bv
)
192 const struct ssh2_channel
*a
= (const struct ssh2_channel
*) av
;
193 const struct ssh2_channel
*b
= (const struct ssh2_channel
*) bv
;
194 if (a
->localid
< b
->localid
)
196 if (a
->localid
> b
->localid
)
201 static int ssh2_channelfind(void *av
, void *bv
)
203 const unsigned *a
= (const unsigned *) av
;
204 const struct ssh2_channel
*b
= (const struct ssh2_channel
*) bv
;
213 * Each channel has a queue of outstanding CHANNEL_REQUESTS and their
216 struct outstanding_channel_request
{
217 cr_handler_fn_t handler
;
219 struct outstanding_channel_request
*next
;
222 static void ssh2_channel_free(struct ssh2_channel
*c
)
224 bufchain_clear(&c
->outbuffer
);
225 bufchain_clear(&c
->errbuffer
);
226 while (c
->chanreq_head
) {
227 struct outstanding_channel_request
*chanreq
= c
->chanreq_head
;
228 c
->chanreq_head
= c
->chanreq_head
->next
;
232 struct ssh2_connection_state
*s
= c
->connlayer
;
233 if (s
->mainchan_sc
== &c
->sc
) {
235 s
->mainchan_sc
= NULL
;
242 PacketProtocolLayer
*ssh2_connection_new(
243 Ssh
*ssh
, ssh_sharing_state
*connshare
, bool is_simple
,
244 Conf
*conf
, const char *peer_verstring
, bufchain
*user_input
,
245 ConnectionLayer
**cl_out
)
247 struct ssh2_connection_state
*s
= snew(struct ssh2_connection_state
);
248 memset(s
, 0, sizeof(*s
));
249 s
->ppl
.vt
= &ssh2_connection_vtable
;
251 s
->conf
= conf_copy(conf
);
253 s
->ssh_is_simple
= is_simple
;
256 * If the ssh_no_shell option is enabled, we disable the usual
257 * termination check, so that we persist even in the absence of
258 * any at all channels (because our purpose is probably to be a
259 * background port forwarder).
261 s
->persistent
= conf_get_bool(s
->conf
, CONF_ssh_no_shell
);
263 s
->connshare
= connshare
;
264 s
->peer_verstring
= dupstr(peer_verstring
);
266 s
->channels
= newtree234(ssh2_channelcmp
);
268 s
->x11authtree
= newtree234(x11_authcmp
);
270 s
->user_input
= user_input
;
272 /* Need to get the log context for s->cl now, because we won't be
273 * helpfully notified when a copy is written into s->ppl by our
275 s
->cl
.vt
= &ssh2_connlayer_vtable
;
276 s
->cl
.logctx
= ssh_get_logctx(ssh
);
278 s
->portfwdmgr
= portfwdmgr_new(&s
->cl
);
282 ssh_connshare_provide_connlayer(s
->connshare
, &s
->cl
);
287 static void ssh2_connection_free(PacketProtocolLayer
*ppl
)
289 struct ssh2_connection_state
*s
=
290 container_of(ppl
, struct ssh2_connection_state
, ppl
);
291 struct X11FakeAuth
*auth
;
292 struct ssh2_channel
*c
;
293 struct ssh_rportfwd
*rpf
;
295 sfree(s
->peer_verstring
);
299 while ((c
= delpos234(s
->channels
, 0)) != NULL
)
300 ssh2_channel_free(c
);
301 freetree234(s
->channels
);
303 while ((auth
= delpos234(s
->x11authtree
, 0)) != NULL
) {
305 x11_free_display(auth
->disp
);
306 x11_free_fake_auth(auth
);
308 freetree234(s
->x11authtree
);
311 while ((rpf
= delpos234(s
->rportfwds
, 0)) != NULL
)
313 freetree234(s
->rportfwds
);
315 portfwdmgr_free(s
->portfwdmgr
);
317 if (s
->antispoof_prompt
)
318 free_prompts(s
->antispoof_prompt
);
320 delete_callbacks_for_context(s
);
325 static bool ssh2_connection_filter_queue(struct ssh2_connection_state
*s
)
330 struct ssh2_channel
*c
;
331 struct outstanding_channel_request
*ocr
;
332 unsigned localid
, remid
, winsize
, pktsize
, ext_type
;
333 bool want_reply
, reply_success
, expect_halfopen
;
334 ChanopenResult chanopen_result
;
335 PacketProtocolLayer
*ppl
= &s
->ppl
; /* for ppl_logevent */
338 if (ssh2_common_filter_queue(&s
->ppl
))
340 if ((pktin
= pq_peek(s
->ppl
.in_pq
)) == NULL
)
343 switch (pktin
->type
) {
344 case SSH2_MSG_GLOBAL_REQUEST
:
345 type
= get_string(pktin
);
346 want_reply
= get_bool(pktin
);
348 reply_success
= ssh2_connection_parse_global_request(
352 int type
= (reply_success
? SSH2_MSG_REQUEST_SUCCESS
:
353 SSH2_MSG_REQUEST_FAILURE
);
354 pktout
= ssh_bpp_new_pktout(s
->ppl
.bpp
, type
);
355 pq_push(s
->ppl
.out_pq
, pktout
);
357 pq_pop(s
->ppl
.in_pq
);
360 case SSH2_MSG_REQUEST_SUCCESS
:
361 case SSH2_MSG_REQUEST_FAILURE
:
362 if (!s
->globreq_head
) {
365 "Received %s with no outstanding global request",
366 ssh2_pkt_type(s
->ppl
.bpp
->pls
->kctx
, s
->ppl
.bpp
->pls
->actx
,
371 s
->globreq_head
->handler(s
, pktin
, s
->globreq_head
->ctx
);
373 struct outstanding_global_request
*tmp
= s
->globreq_head
;
374 s
->globreq_head
= s
->globreq_head
->next
;
377 if (!s
->globreq_head
)
378 s
->globreq_tail
= NULL
;
380 pq_pop(s
->ppl
.in_pq
);
383 case SSH2_MSG_CHANNEL_OPEN
:
384 type
= get_string(pktin
);
385 c
= snew(struct ssh2_channel
);
389 remid
= get_uint32(pktin
);
390 winsize
= get_uint32(pktin
);
391 pktsize
= get_uint32(pktin
);
393 chanopen_result
= ssh2_connection_parse_channel_open(
394 s
, type
, pktin
, &c
->sc
);
396 if (chanopen_result
.outcome
== CHANOPEN_RESULT_DOWNSTREAM
) {
398 * This channel-open request needs to go to a
399 * connection-sharing downstream, so abandon our own
400 * channel-open procedure and just pass the message on
403 share_got_pkt_from_server(
404 chanopen_result
.u
.downstream
.share_ctx
, pktin
->type
,
405 BinarySource_UPCAST(pktin
)->data
,
406 BinarySource_UPCAST(pktin
)->len
);
413 if (chanopen_result
.outcome
== CHANOPEN_RESULT_FAILURE
) {
414 pktout
= ssh_bpp_new_pktout(
415 s
->ppl
.bpp
, SSH2_MSG_CHANNEL_OPEN_FAILURE
);
416 put_uint32(pktout
, c
->remoteid
);
417 put_uint32(pktout
, chanopen_result
.u
.failure
.reason_code
);
418 put_stringz(pktout
, chanopen_result
.u
.failure
.wire_message
);
419 put_stringz(pktout
, "en"); /* language tag */
420 pq_push(s
->ppl
.out_pq
, pktout
);
421 ppl_logevent("Rejected channel open: %s",
422 chanopen_result
.u
.failure
.wire_message
);
423 sfree(chanopen_result
.u
.failure
.wire_message
);
426 c
->chan
= chanopen_result
.u
.success
.channel
;
427 ssh2_channel_init(c
);
428 c
->remwindow
= winsize
;
429 c
->remmaxpkt
= pktsize
;
430 if (c
->remmaxpkt
> s
->ppl
.bpp
->vt
->packet_size_limit
)
431 c
->remmaxpkt
= s
->ppl
.bpp
->vt
->packet_size_limit
;
432 if (c
->chan
->initial_fixed_window_size
) {
433 c
->locwindow
= c
->locmaxwin
= c
->remlocwin
=
434 c
->chan
->initial_fixed_window_size
;
436 pktout
= ssh_bpp_new_pktout(
437 s
->ppl
.bpp
, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
);
438 put_uint32(pktout
, c
->remoteid
);
439 put_uint32(pktout
, c
->localid
);
440 put_uint32(pktout
, c
->locwindow
);
441 put_uint32(pktout
, OUR_V2_MAXPKT
); /* our max pkt size */
442 pq_push(s
->ppl
.out_pq
, pktout
);
445 pq_pop(s
->ppl
.in_pq
);
448 case SSH2_MSG_CHANNEL_DATA
:
449 case SSH2_MSG_CHANNEL_EXTENDED_DATA
:
450 case SSH2_MSG_CHANNEL_WINDOW_ADJUST
:
451 case SSH2_MSG_CHANNEL_REQUEST
:
452 case SSH2_MSG_CHANNEL_EOF
:
453 case SSH2_MSG_CHANNEL_CLOSE
:
454 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
:
455 case SSH2_MSG_CHANNEL_OPEN_FAILURE
:
456 case SSH2_MSG_CHANNEL_SUCCESS
:
457 case SSH2_MSG_CHANNEL_FAILURE
:
459 * Common preliminary code for all the messages from the
460 * server that cite one of our channel ids: look up that
461 * channel id, check it exists, and if it's for a sharing
462 * downstream, pass it on.
464 localid
= get_uint32(pktin
);
465 c
= find234(s
->channels
, &localid
, ssh2_channelfind
);
467 if (c
&& c
->sharectx
) {
468 share_got_pkt_from_server(c
->sharectx
, pktin
->type
,
469 BinarySource_UPCAST(pktin
)->data
,
470 BinarySource_UPCAST(pktin
)->len
);
471 pq_pop(s
->ppl
.in_pq
);
476 pktin
->type
== SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
||
477 pktin
->type
== SSH2_MSG_CHANNEL_OPEN_FAILURE
);
479 if (!c
|| c
->halfopen
!= expect_halfopen
) {
480 ssh_proto_error(s
->ppl
.ssh
,
481 "Received %s for %s channel %u",
482 ssh2_pkt_type(s
->ppl
.bpp
->pls
->kctx
,
483 s
->ppl
.bpp
->pls
->actx
,
485 (!c
? "nonexistent" :
486 c
->halfopen
? "half-open" : "open"),
491 switch (pktin
->type
) {
492 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
:
494 c
->remoteid
= get_uint32(pktin
);
496 c
->remwindow
= get_uint32(pktin
);
497 c
->remmaxpkt
= get_uint32(pktin
);
498 if (c
->remmaxpkt
> s
->ppl
.bpp
->vt
->packet_size_limit
)
499 c
->remmaxpkt
= s
->ppl
.bpp
->vt
->packet_size_limit
;
501 chan_open_confirmation(c
->chan
);
504 * Now that the channel is fully open, it's possible
505 * in principle to immediately close it. Check whether
508 * This can occur if a local socket error occurred
509 * between us sending out CHANNEL_OPEN and receiving
510 * OPEN_CONFIRMATION. If that happens, all we can do
511 * is immediately initiate close proceedings now that
512 * we know the server's id to put in the close
513 * message. We'll have handled that in this code by
514 * having already turned c->chan into a zombie, so its
515 * want_close method (which ssh2_channel_check_close
516 * will consult) will already be returning true.
518 ssh2_channel_check_close(c
);
521 ssh2_channel_try_eof(c
); /* in case we had a pending EOF */
524 case SSH2_MSG_CHANNEL_OPEN_FAILURE
: {
527 char *err
= ssh2_channel_open_failure_error_text(pktin
);
528 chan_open_failed(c
->chan
, err
);
531 del234(s
->channels
, c
);
532 ssh2_channel_free(c
);
537 case SSH2_MSG_CHANNEL_DATA
:
538 case SSH2_MSG_CHANNEL_EXTENDED_DATA
:
539 ext_type
= (pktin
->type
== SSH2_MSG_CHANNEL_DATA
? 0 :
541 data
= get_string(pktin
);
542 if (!get_err(pktin
)) {
544 c
->locwindow
-= data
.len
;
545 c
->remlocwin
-= data
.len
;
546 if (ext_type
!= 0 && ext_type
!= SSH2_EXTENDED_DATA_STDERR
)
547 data
.len
= 0; /* ignore unknown extended data */
549 c
->chan
, ext_type
== SSH2_EXTENDED_DATA_STDERR
,
553 * The channel may have turned into a connection-
554 * shared one as a result of that chan_send, e.g.
555 * if the data we just provided completed the X11
556 * auth phase and caused a callback to
557 * x11_sharing_handover. If so, do nothing
564 * If it looks like the remote end hit the end of
565 * its window, and we didn't want it to do that,
566 * think about using a larger window.
568 if (c
->remlocwin
<= 0 &&
569 c
->throttle_state
== UNTHROTTLED
&&
570 c
->locmaxwin
< 0x40000000)
571 c
->locmaxwin
+= OUR_V2_WINSIZE
;
574 * If we are not buffering too much data, enlarge
575 * the window again at the remote side. If we are
576 * buffering too much, we may still need to adjust
577 * the window if the server's sent excess data.
579 if (bufsize
< c
->locmaxwin
)
580 ssh2_set_window(c
, c
->locmaxwin
- bufsize
);
583 * If we're either buffering way too much data, or
584 * if we're buffering anything at all and we're in
585 * "simple" mode, throttle the whole channel.
587 if ((bufsize
> c
->locmaxwin
||
588 (s
->ssh_is_simple
&& bufsize
>0)) &&
589 !c
->throttling_conn
) {
590 c
->throttling_conn
= true;
591 ssh_throttle_conn(s
->ppl
.ssh
, +1);
596 case SSH2_MSG_CHANNEL_WINDOW_ADJUST
:
597 if (!(c
->closes
& CLOSES_SENT_EOF
)) {
598 c
->remwindow
+= get_uint32(pktin
);
599 ssh2_try_send_and_unthrottle(c
);
603 case SSH2_MSG_CHANNEL_REQUEST
:
604 type
= get_string(pktin
);
605 want_reply
= get_bool(pktin
);
607 reply_success
= false;
609 if (c
->closes
& CLOSES_SENT_CLOSE
) {
611 * We don't reply to channel requests after we've
612 * sent CHANNEL_CLOSE for the channel, because our
613 * reply might cross in the network with the other
614 * side's CHANNEL_CLOSE and arrive after they have
615 * wound the channel up completely.
621 * Try every channel request name we recognise, no
622 * matter what the channel, and see if the Channel
623 * instance will accept it.
625 if (ptrlen_eq_string(type
, "exit-status")) {
626 int exitcode
= toint(get_uint32(pktin
));
627 reply_success
= chan_rcvd_exit_status(c
->chan
, exitcode
);
628 } else if (ptrlen_eq_string(type
, "exit-signal")) {
636 * ICK: older versions of OpenSSH (e.g. 3.4p1)
637 * provide an `int' for the signal, despite its
638 * having been a `string' in the drafts of RFC
639 * 4254 since at least 2001. (Fixed in session.c
640 * 1.147.) Try to infer which we can safely parse
644 size_t startpos
= BinarySource_UPCAST(pktin
)->pos
;
646 for (format
= 0; format
< 2; format
++) {
647 BinarySource_UPCAST(pktin
)->pos
= startpos
;
648 BinarySource_UPCAST(pktin
)->err
= BSE_NO_ERROR
;
650 /* placate compiler warnings about unin */
651 signame
= make_ptrlen(NULL
, 0);
654 if (format
== 0) /* standard string-based format */
655 signame
= get_string(pktin
);
656 else /* nonstandard integer format */
657 signum
= toint(get_uint32(pktin
));
659 core
= get_bool(pktin
);
660 errmsg
= get_string(pktin
); /* error message */
661 get_string(pktin
); /* language tag */
663 if (!get_err(pktin
) && get_avail(pktin
) == 0)
664 break; /* successful parse */
669 reply_success
= chan_rcvd_exit_signal(
670 c
->chan
, signame
, core
, errmsg
);
673 reply_success
= chan_rcvd_exit_signal_numeric(
674 c
->chan
, signum
, core
, errmsg
);
677 /* Couldn't parse this message in either format */
678 reply_success
= false;
681 } else if (ptrlen_eq_string(type
, "shell")) {
682 reply_success
= chan_run_shell(c
->chan
);
683 } else if (ptrlen_eq_string(type
, "exec")) {
684 ptrlen command
= get_string(pktin
);
685 reply_success
= chan_run_command(c
->chan
, command
);
686 } else if (ptrlen_eq_string(type
, "subsystem")) {
687 ptrlen subsys
= get_string(pktin
);
688 reply_success
= chan_run_subsystem(c
->chan
, subsys
);
689 } else if (ptrlen_eq_string(type
, "x11-req")) {
690 bool oneshot
= get_bool(pktin
);
691 ptrlen authproto
= get_string(pktin
);
692 ptrlen authdata
= get_string(pktin
);
693 unsigned screen_number
= get_uint32(pktin
);
694 reply_success
= chan_enable_x11_forwarding(
695 c
->chan
, oneshot
, authproto
, authdata
, screen_number
);
696 } else if (ptrlen_eq_string(type
,
697 "auth-agent-req@openssh.com")) {
698 reply_success
= chan_enable_agent_forwarding(c
->chan
);
699 } else if (ptrlen_eq_string(type
, "pty-req")) {
700 ptrlen termtype
= get_string(pktin
);
701 unsigned width
= get_uint32(pktin
);
702 unsigned height
= get_uint32(pktin
);
703 unsigned pixwidth
= get_uint32(pktin
);
704 unsigned pixheight
= get_uint32(pktin
);
705 ptrlen encoded_modes
= get_string(pktin
);
706 BinarySource bs_modes
[1];
707 struct ssh_ttymodes modes
;
709 BinarySource_BARE_INIT_PL(bs_modes
, encoded_modes
);
710 modes
= read_ttymodes_from_packet(bs_modes
, 2);
711 if (get_err(bs_modes
) || get_avail(bs_modes
) > 0) {
712 ppl_logevent("Unable to decode terminal mode string");
713 reply_success
= false;
715 reply_success
= chan_allocate_pty(
716 c
->chan
, termtype
, width
, height
,
717 pixwidth
, pixheight
, modes
);
719 } else if (ptrlen_eq_string(type
, "env")) {
720 ptrlen var
= get_string(pktin
);
721 ptrlen value
= get_string(pktin
);
723 reply_success
= chan_set_env(c
->chan
, var
, value
);
724 } else if (ptrlen_eq_string(type
, "break")) {
725 unsigned length
= get_uint32(pktin
);
727 reply_success
= chan_send_break(c
->chan
, length
);
728 } else if (ptrlen_eq_string(type
, "signal")) {
729 ptrlen signame
= get_string(pktin
);
731 reply_success
= chan_send_signal(c
->chan
, signame
);
732 } else if (ptrlen_eq_string(type
, "window-change")) {
733 unsigned width
= get_uint32(pktin
);
734 unsigned height
= get_uint32(pktin
);
735 unsigned pixwidth
= get_uint32(pktin
);
736 unsigned pixheight
= get_uint32(pktin
);
737 reply_success
= chan_change_window_size(
738 c
->chan
, width
, height
, pixwidth
, pixheight
);
741 int type
= (reply_success
? SSH2_MSG_CHANNEL_SUCCESS
:
742 SSH2_MSG_CHANNEL_FAILURE
);
743 pktout
= ssh_bpp_new_pktout(s
->ppl
.bpp
, type
);
744 put_uint32(pktout
, c
->remoteid
);
745 pq_push(s
->ppl
.out_pq
, pktout
);
749 case SSH2_MSG_CHANNEL_SUCCESS
:
750 case SSH2_MSG_CHANNEL_FAILURE
:
751 ocr
= c
->chanreq_head
;
755 "Received %s for channel %d with no outstanding "
757 ssh2_pkt_type(s
->ppl
.bpp
->pls
->kctx
,
758 s
->ppl
.bpp
->pls
->actx
, pktin
->type
),
762 ocr
->handler(c
, pktin
, ocr
->ctx
);
763 c
->chanreq_head
= ocr
->next
;
766 * We may now initiate channel-closing procedures, if
767 * that CHANNEL_REQUEST was the last thing outstanding
768 * before we send CHANNEL_CLOSE.
770 ssh2_channel_check_close(c
);
773 case SSH2_MSG_CHANNEL_EOF
:
774 if (!(c
->closes
& CLOSES_RCVD_EOF
)) {
775 c
->closes
|= CLOSES_RCVD_EOF
;
776 chan_send_eof(c
->chan
);
777 ssh2_channel_check_close(c
);
781 case SSH2_MSG_CHANNEL_CLOSE
:
783 * When we receive CLOSE on a channel, we assume it
784 * comes with an implied EOF if we haven't seen EOF
787 if (!(c
->closes
& CLOSES_RCVD_EOF
)) {
788 c
->closes
|= CLOSES_RCVD_EOF
;
789 chan_send_eof(c
->chan
);
792 if (!(s
->ppl
.remote_bugs
& BUG_SENDS_LATE_REQUEST_REPLY
)) {
794 * It also means we stop expecting to see replies
795 * to any outstanding channel requests, so clean
796 * those up too. (ssh_chanreq_init will enforce by
797 * assertion that we don't subsequently put
798 * anything back on this list.)
800 while (c
->chanreq_head
) {
801 struct outstanding_channel_request
*ocr
=
803 ocr
->handler(c
, NULL
, ocr
->ctx
);
804 c
->chanreq_head
= ocr
->next
;
810 * And we also send an outgoing EOF, if we haven't
811 * already, on the assumption that CLOSE is a pretty
812 * forceful announcement that the remote side is doing
813 * away with the entire channel. (If it had wanted to
814 * send us EOF and continue receiving data from us, it
815 * would have just sent CHANNEL_EOF.)
817 if (!(c
->closes
& CLOSES_SENT_EOF
)) {
819 * Abandon any buffered data we still wanted to
820 * send to this channel. Receiving a CHANNEL_CLOSE
821 * is an indication that the server really wants
822 * to get on and _destroy_ this channel, and it
823 * isn't going to send us any further
824 * WINDOW_ADJUSTs to permit us to send pending
827 bufchain_clear(&c
->outbuffer
);
828 bufchain_clear(&c
->errbuffer
);
833 sshfwd_write_eof(&c
->sc
);
836 * Make sure we don't read any more from whatever
837 * our local data source is for this channel.
838 * (This will pick up on the changes made by
841 ssh2_channel_check_throttle(c
);
845 * Now process the actual close.
847 if (!(c
->closes
& CLOSES_RCVD_CLOSE
)) {
848 c
->closes
|= CLOSES_RCVD_CLOSE
;
849 ssh2_channel_check_close(c
);
855 pq_pop(s
->ppl
.in_pq
);
864 static void ssh2_handle_winadj_response(struct ssh2_channel
*c
,
865 PktIn
*pktin
, void *ctx
)
867 unsigned *sizep
= ctx
;
870 * Winadj responses should always be failures. However, at least
871 * one server ("boks_sshd") is known to return SUCCESS for channel
872 * requests it's never heard of, such as "winadj@putty". Raised
873 * with foxt.com as bug 090916-090424, but for the sake of a quiet
874 * life, we don't worry about what kind of response we got.
877 c
->remlocwin
+= *sizep
;
880 * winadj messages are only sent when the window is fully open, so
881 * if we get an ack of one, we know any pending unthrottle is
884 if (c
->throttle_state
== UNTHROTTLING
)
885 c
->throttle_state
= UNTHROTTLED
;
888 static void ssh2_set_window(struct ssh2_channel
*c
, int newwin
)
890 struct ssh2_connection_state
*s
= c
->connlayer
;
893 * Never send WINDOW_ADJUST for a channel that the remote side has
894 * already sent EOF on; there's no point, since it won't be
895 * sending any more data anyway. Ditto if _we've_ already sent
898 if (c
->closes
& (CLOSES_RCVD_EOF
| CLOSES_SENT_CLOSE
))
902 * If the client-side Channel is in an initial setup phase with a
903 * fixed window size, e.g. for an X11 channel when we're still
904 * waiting to see its initial auth and may yet hand it off to a
905 * downstream, don't send any WINDOW_ADJUST either.
907 if (c
->chan
->initial_fixed_window_size
)
911 * If the remote end has a habit of ignoring maxpkt, limit the
912 * window so that it has no choice (assuming it doesn't ignore the
915 if ((s
->ppl
.remote_bugs
& BUG_SSH2_MAXPKT
) && newwin
> OUR_V2_MAXPKT
)
916 newwin
= OUR_V2_MAXPKT
;
919 * Only send a WINDOW_ADJUST if there's significantly more window
920 * available than the other end thinks there is. This saves us
921 * sending a WINDOW_ADJUST for every character in a shell session.
923 * "Significant" is arbitrarily defined as half the window size.
925 if (newwin
/ 2 >= c
->locwindow
) {
930 * In order to keep track of how much window the client
931 * actually has available, we'd like it to acknowledge each
932 * WINDOW_ADJUST. We can't do that directly, so we accompany
933 * it with a CHANNEL_REQUEST that has to be acknowledged.
935 * This is only necessary if we're opening the window wide.
936 * If we're not, then throughput is being constrained by
937 * something other than the maximum window size anyway.
939 if (newwin
== c
->locmaxwin
&&
940 !(s
->ppl
.remote_bugs
& BUG_CHOKES_ON_WINADJ
)) {
942 *up
= newwin
- c
->locwindow
;
943 pktout
= ssh2_chanreq_init(c
, "winadj@putty.projects.tartarus.org",
944 ssh2_handle_winadj_response
, up
);
945 pq_push(s
->ppl
.out_pq
, pktout
);
947 if (c
->throttle_state
!= UNTHROTTLED
)
948 c
->throttle_state
= UNTHROTTLING
;
950 /* Pretend the WINDOW_ADJUST was acked immediately. */
951 c
->remlocwin
= newwin
;
952 c
->throttle_state
= THROTTLED
;
954 pktout
= ssh_bpp_new_pktout(s
->ppl
.bpp
, SSH2_MSG_CHANNEL_WINDOW_ADJUST
);
955 put_uint32(pktout
, c
->remoteid
);
956 put_uint32(pktout
, newwin
- c
->locwindow
);
957 pq_push(s
->ppl
.out_pq
, pktout
);
958 c
->locwindow
= newwin
;
962 static PktIn
*ssh2_connection_pop(struct ssh2_connection_state
*s
)
964 ssh2_connection_filter_queue(s
);
965 return pq_pop(s
->ppl
.in_pq
);
968 static void ssh2_connection_process_queue(PacketProtocolLayer
*ppl
)
970 struct ssh2_connection_state
*s
=
971 container_of(ppl
, struct ssh2_connection_state
, ppl
);
974 if (ssh2_connection_filter_queue(s
)) /* no matter why we were called */
980 share_activate(s
->connshare
, s
->peer_verstring
);
983 * Signal the seat that authentication is done, so that it can
984 * deploy spoofing defences. If it doesn't have any, deploy our
987 * We do this here rather than at the end of userauth, because we
988 * might not have gone through userauth at all (if we're a
989 * connection-sharing downstream).
991 if (ssh2_connection_need_antispoof_prompt(s
)) {
992 s
->antispoof_prompt
= ssh_ppl_new_prompts(&s
->ppl
);
993 s
->antispoof_prompt
->to_server
= false;
994 s
->antispoof_prompt
->from_server
= false;
995 s
->antispoof_prompt
->name
= dupstr("Authentication successful");
998 dupstr("Access granted. Press Return to begin session. "), false);
999 s
->antispoof_ret
= seat_get_userpass_input(
1000 ppl_get_iseat(&s
->ppl
), s
->antispoof_prompt
);
1001 while (s
->antispoof_ret
.kind
== SPRK_INCOMPLETE
) {
1003 s
->antispoof_ret
= seat_get_userpass_input(
1004 ppl_get_iseat(&s
->ppl
), s
->antispoof_prompt
);
1006 free_prompts(s
->antispoof_prompt
);
1007 s
->antispoof_prompt
= NULL
;
1011 * Enable port forwardings.
1013 portfwdmgr_config(s
->portfwdmgr
, s
->conf
);
1014 s
->portfwdmgr_configured
= true;
1017 * Create the main session channel, if any.
1019 s
->mainchan
= mainchan_new(
1020 &s
->ppl
, &s
->cl
, s
->conf
, s
->term_width
, s
->term_height
,
1021 s
->ssh_is_simple
, &s
->mainchan_sc
);
1029 if ((pktin
= ssh2_connection_pop(s
)) != NULL
) {
1032 * _All_ the connection-layer packets we expect to
1033 * receive are now handled by the dispatch table.
1034 * Anything that reaches here must be bogus.
1037 ssh_proto_error(s
->ppl
.ssh
, "Received unexpected connection-layer "
1038 "packet, type %d (%s)", pktin
->type
,
1039 ssh2_pkt_type(s
->ppl
.bpp
->pls
->kctx
,
1040 s
->ppl
.bpp
->pls
->actx
,
1050 static void ssh2_channel_check_close(struct ssh2_channel
*c
)
1052 struct ssh2_connection_state
*s
= c
->connlayer
;
1057 * If we've sent out our own CHANNEL_OPEN but not yet seen
1058 * either OPEN_CONFIRMATION or OPEN_FAILURE in response, then
1059 * it's too early to be sending close messages of any kind.
1064 if (chan_want_close(c
->chan
, (c
->closes
& CLOSES_SENT_EOF
),
1065 (c
->closes
& CLOSES_RCVD_EOF
)) &&
1067 !(c
->closes
& CLOSES_SENT_CLOSE
)) {
1069 * We have both sent and received EOF (or the channel is a
1070 * zombie), and we have no outstanding channel requests, which
1071 * means the channel is in final wind-up. But we haven't sent
1072 * CLOSE, so let's do so now.
1074 pktout
= ssh_bpp_new_pktout(s
->ppl
.bpp
, SSH2_MSG_CHANNEL_CLOSE
);
1075 put_uint32(pktout
, c
->remoteid
);
1076 pq_push(s
->ppl
.out_pq
, pktout
);
1077 c
->closes
|= CLOSES_SENT_EOF
| CLOSES_SENT_CLOSE
;
1080 if (!((CLOSES_SENT_CLOSE
| CLOSES_RCVD_CLOSE
) & ~c
->closes
)) {
1081 assert(c
->chanreq_head
== NULL
);
1083 * We have both sent and received CLOSE, which means we're
1084 * completely done with the channel.
1086 ssh2_channel_destroy(c
);
1090 static void ssh2_channel_try_eof(struct ssh2_channel
*c
)
1092 struct ssh2_connection_state
*s
= c
->connlayer
;
1094 assert(c
->pending_eof
); /* precondition for calling us */
1096 return; /* can't close: not even opened yet */
1097 if (bufchain_size(&c
->outbuffer
) > 0 || bufchain_size(&c
->errbuffer
) > 0)
1098 return; /* can't send EOF: pending outgoing data */
1100 c
->pending_eof
= false; /* we're about to send it */
1102 pktout
= ssh_bpp_new_pktout(s
->ppl
.bpp
, SSH2_MSG_CHANNEL_EOF
);
1103 put_uint32(pktout
, c
->remoteid
);
1104 pq_push(s
->ppl
.out_pq
, pktout
);
1105 c
->closes
|= CLOSES_SENT_EOF
;
1106 ssh2_channel_check_close(c
);
1110 * Attempt to send data on an SSH-2 channel.
1112 static size_t ssh2_try_send(struct ssh2_channel
*c
)
1114 struct ssh2_connection_state
*s
= c
->connlayer
;
1119 while (c
->remwindow
> 0 &&
1120 (bufchain_size(&c
->outbuffer
) > 0 ||
1121 bufchain_size(&c
->errbuffer
) > 0)) {
1122 bufchain
*buf
= (bufchain_size(&c
->errbuffer
) > 0 ?
1123 &c
->errbuffer
: &c
->outbuffer
);
1125 ptrlen data
= bufchain_prefix(buf
);
1126 if (data
.len
> c
->remwindow
)
1127 data
.len
= c
->remwindow
;
1128 if (data
.len
> c
->remmaxpkt
)
1129 data
.len
= c
->remmaxpkt
;
1130 if (buf
== &c
->errbuffer
) {
1131 pktout
= ssh_bpp_new_pktout(
1132 s
->ppl
.bpp
, SSH2_MSG_CHANNEL_EXTENDED_DATA
);
1133 put_uint32(pktout
, c
->remoteid
);
1134 put_uint32(pktout
, SSH2_EXTENDED_DATA_STDERR
);
1136 pktout
= ssh_bpp_new_pktout(s
->ppl
.bpp
, SSH2_MSG_CHANNEL_DATA
);
1137 put_uint32(pktout
, c
->remoteid
);
1139 put_stringpl(pktout
, data
);
1140 pq_push(s
->ppl
.out_pq
, pktout
);
1141 bufchain_consume(buf
, data
.len
);
1142 c
->remwindow
-= data
.len
;
1147 * After having sent as much data as we can, return the amount
1150 bufsize
= bufchain_size(&c
->outbuffer
) + bufchain_size(&c
->errbuffer
);
1153 * And if there's no data pending but we need to send an EOF, send
1156 if (!bufsize
&& c
->pending_eof
)
1157 ssh2_channel_try_eof(c
);
1159 ssh_sendbuffer_changed(s
->ppl
.ssh
);
1163 static void ssh2_try_send_and_unthrottle(struct ssh2_channel
*c
)
1166 if (c
->closes
& CLOSES_SENT_EOF
)
1167 return; /* don't send on channels we've EOFed */
1168 bufsize
= ssh2_try_send(c
);
1170 c
->throttled_by_backlog
= false;
1171 ssh2_channel_check_throttle(c
);
1175 static void ssh2_channel_check_throttle(struct ssh2_channel
*c
)
1178 * We don't want this channel to read further input if this
1179 * particular channel has a backed-up SSH window, or if the
1180 * outgoing side of the whole SSH connection is currently
1181 * throttled, or if this channel already has an outgoing EOF
1182 * either sent or pending.
1184 chan_set_input_wanted(c
->chan
,
1185 !c
->throttled_by_backlog
&&
1186 !c
->connlayer
->all_channels_throttled
&&
1188 !(c
->closes
& CLOSES_SENT_EOF
));
1192 * Close any local socket and free any local resources associated with
1193 * a channel. This converts the channel into a zombie.
1195 static void ssh2_channel_close_local(struct ssh2_channel
*c
,
1198 struct ssh2_connection_state
*s
= c
->connlayer
;
1199 PacketProtocolLayer
*ppl
= &s
->ppl
; /* for ppl_logevent */
1205 msg
= chan_log_close_msg(c
->chan
);
1208 ppl_logevent("%s%s%s", msg
, reason
? " " : "", reason
? reason
: "");
1213 c
->chan
= zombiechan_new();
1216 static void ssh2_check_termination_callback(void *vctx
)
1218 struct ssh2_connection_state
*s
= (struct ssh2_connection_state
*)vctx
;
1219 ssh2_check_termination(s
);
1222 static void ssh2_channel_destroy(struct ssh2_channel
*c
)
1224 struct ssh2_connection_state
*s
= c
->connlayer
;
1226 assert(c
->chanreq_head
== NULL
);
1228 ssh2_channel_close_local(c
, NULL
);
1229 del234(s
->channels
, c
);
1230 ssh2_channel_free(c
);
1233 * If that was the last channel left open, we might need to
1234 * terminate. But we'll be a bit cautious, by doing that in a
1235 * toplevel callback, just in case anything on the current call
1236 * stack objects to this entire PPL being freed.
1238 queue_toplevel_callback(ssh2_check_termination_callback
, s
);
1241 static void ssh2_check_termination(struct ssh2_connection_state
*s
)
1244 * Decide whether we should terminate the SSH connection now.
1245 * Called after a channel or a downstream goes away. The general
1246 * policy is that we terminate when none of either is left.
1250 return; /* persistent mode: never proactively terminate */
1253 /* At startup, we don't have any channels open because we
1254 * haven't got round to opening the main one yet. In that
1255 * situation, we don't want to terminate, even if a sharing
1256 * connection opens and closes and causes a call to this
1261 if (count234(s
->channels
) == 0 &&
1262 !(s
->connshare
&& share_ndownstreams(s
->connshare
) > 0)) {
1264 * We used to send SSH_MSG_DISCONNECT here, because I'd
1265 * believed that _every_ conforming SSH-2 connection had to
1266 * end with a disconnect being sent by at least one side;
1267 * apparently I was wrong and it's perfectly OK to
1268 * unceremoniously slam the connection shut when you're done,
1269 * and indeed OpenSSH feels this is more polite than sending a
1270 * DISCONNECT. So now we don't.
1272 ssh_user_close(s
->ppl
.ssh
, "All channels closed");
1278 * Set up most of a new ssh2_channel. Nulls out sharectx, but leaves
1279 * chan untouched (since it will sometimes have been filled in before
1282 void ssh2_channel_init(struct ssh2_channel
*c
)
1284 struct ssh2_connection_state
*s
= c
->connlayer
;
1286 c
->pending_eof
= false;
1287 c
->throttling_conn
= false;
1288 c
->throttled_by_backlog
= false;
1290 c
->locwindow
= c
->locmaxwin
= c
->remlocwin
=
1291 s
->ssh_is_simple
? OUR_V2_BIGWIN
: OUR_V2_WINSIZE
;
1292 c
->chanreq_head
= NULL
;
1293 c
->throttle_state
= UNTHROTTLED
;
1294 bufchain_init(&c
->outbuffer
);
1295 bufchain_init(&c
->errbuffer
);
1296 c
->sc
.vt
= &ssh2channel_vtable
;
1298 c
->localid
= alloc_channel_id(s
->channels
, struct ssh2_channel
);
1299 add234(s
->channels
, c
);
1303 * Construct the common parts of a CHANNEL_OPEN.
1305 PktOut
*ssh2_chanopen_init(struct ssh2_channel
*c
, const char *type
)
1307 struct ssh2_connection_state
*s
= c
->connlayer
;
1310 pktout
= ssh_bpp_new_pktout(s
->ppl
.bpp
, SSH2_MSG_CHANNEL_OPEN
);
1311 put_stringz(pktout
, type
);
1312 put_uint32(pktout
, c
->localid
);
1313 put_uint32(pktout
, c
->locwindow
); /* our window size */
1314 put_uint32(pktout
, OUR_V2_MAXPKT
); /* our max pkt size */
1319 * Construct the common parts of a CHANNEL_REQUEST. If handler is not
1320 * NULL then a reply will be requested and the handler will be called
1321 * when it arrives. The returned packet is ready to have any
1322 * request-specific data added and be sent. Note that if a handler is
1323 * provided, it's essential that the request actually be sent.
1325 * The handler will usually be passed the response packet in pktin. If
1326 * pktin is NULL, this means that no reply will ever be forthcoming
1327 * (e.g. because the entire connection is being destroyed, or because
1328 * the server initiated channel closure before we saw the response)
1329 * and the handler should free any storage it's holding.
1331 PktOut
*ssh2_chanreq_init(struct ssh2_channel
*c
, const char *type
,
1332 cr_handler_fn_t handler
, void *ctx
)
1334 struct ssh2_connection_state
*s
= c
->connlayer
;
1337 assert(!(c
->closes
& (CLOSES_SENT_CLOSE
| CLOSES_RCVD_CLOSE
)));
1338 pktout
= ssh_bpp_new_pktout(s
->ppl
.bpp
, SSH2_MSG_CHANNEL_REQUEST
);
1339 put_uint32(pktout
, c
->remoteid
);
1340 put_stringz(pktout
, type
);
1341 put_bool(pktout
, handler
!= NULL
);
1342 if (handler
!= NULL
) {
1343 struct outstanding_channel_request
*ocr
=
1344 snew(struct outstanding_channel_request
);
1346 ocr
->handler
= handler
;
1349 if (!c
->chanreq_head
)
1350 c
->chanreq_head
= ocr
;
1352 c
->chanreq_tail
->next
= ocr
;
1353 c
->chanreq_tail
= ocr
;
1358 static Conf
*ssh2channel_get_conf(SshChannel
*sc
)
1360 struct ssh2_channel
*c
= container_of(sc
, struct ssh2_channel
, sc
);
1361 struct ssh2_connection_state
*s
= c
->connlayer
;
1365 static void ssh2channel_write_eof(SshChannel
*sc
)
1367 struct ssh2_channel
*c
= container_of(sc
, struct ssh2_channel
, sc
);
1369 if (c
->closes
& CLOSES_SENT_EOF
)
1372 c
->pending_eof
= true;
1373 ssh2_channel_try_eof(c
);
1376 static void ssh2channel_initiate_close(SshChannel
*sc
, const char *err
)
1378 struct ssh2_channel
*c
= container_of(sc
, struct ssh2_channel
, sc
);
1381 reason
= err
? dupprintf("due to local error: %s", err
) : NULL
;
1382 ssh2_channel_close_local(c
, reason
);
1384 c
->pending_eof
= false; /* this will confuse a zombie channel */
1386 ssh2_channel_check_close(c
);
1389 static void ssh2channel_unthrottle(SshChannel
*sc
, size_t bufsize
)
1391 struct ssh2_channel
*c
= container_of(sc
, struct ssh2_channel
, sc
);
1392 struct ssh2_connection_state
*s
= c
->connlayer
;
1395 buflimit
= s
->ssh_is_simple
? 0 : c
->locmaxwin
;
1396 if (bufsize
< buflimit
)
1397 ssh2_set_window(c
, buflimit
- bufsize
);
1399 if (c
->throttling_conn
&& bufsize
<= buflimit
) {
1400 c
->throttling_conn
= false;
1401 ssh_throttle_conn(s
->ppl
.ssh
, -1);
1405 static size_t ssh2channel_write(
1406 SshChannel
*sc
, bool is_stderr
, const void *buf
, size_t len
)
1408 struct ssh2_channel
*c
= container_of(sc
, struct ssh2_channel
, sc
);
1409 assert(!(c
->closes
& CLOSES_SENT_EOF
));
1410 bufchain_add(is_stderr
? &c
->errbuffer
: &c
->outbuffer
, buf
, len
);
1411 return ssh2_try_send(c
);
1414 static void ssh2channel_x11_sharing_handover(
1415 SshChannel
*sc
, ssh_sharing_connstate
*share_cs
, share_channel
*share_chan
,
1416 const char *peer_addr
, int peer_port
, int endian
,
1417 int protomajor
, int protominor
, const void *initial_data
, int initial_len
)
1419 struct ssh2_channel
*c
= container_of(sc
, struct ssh2_channel
, sc
);
1421 * This function is called when we've just discovered that an X
1422 * forwarding channel on which we'd been handling the initial auth
1423 * ourselves turns out to be destined for a connection-sharing
1424 * downstream. So we turn the channel into a sharing one, meaning
1425 * that we completely stop tracking windows and buffering data and
1426 * just pass more or less unmodified SSH messages back and forth.
1428 c
->sharectx
= share_cs
;
1429 share_setup_x11_channel(share_cs
, share_chan
,
1430 c
->localid
, c
->remoteid
, c
->remwindow
,
1431 c
->remmaxpkt
, c
->locwindow
,
1432 peer_addr
, peer_port
, endian
,
1433 protomajor
, protominor
,
1434 initial_data
, initial_len
);
1439 static void ssh2channel_window_override_removed(SshChannel
*sc
)
1441 struct ssh2_channel
*c
= container_of(sc
, struct ssh2_channel
, sc
);
1442 struct ssh2_connection_state
*s
= c
->connlayer
;
1445 * This function is called when a client-side Channel has just
1446 * stopped requiring an initial fixed-size window.
1448 assert(!c
->chan
->initial_fixed_window_size
);
1449 ssh2_set_window(c
, s
->ssh_is_simple
? OUR_V2_BIGWIN
: OUR_V2_WINSIZE
);
1452 static void ssh2channel_hint_channel_is_simple(SshChannel
*sc
)
1454 struct ssh2_channel
*c
= container_of(sc
, struct ssh2_channel
, sc
);
1455 struct ssh2_connection_state
*s
= c
->connlayer
;
1457 PktOut
*pktout
= ssh2_chanreq_init(
1458 c
, "simple@putty.projects.tartarus.org", NULL
, NULL
);
1459 pq_push(s
->ppl
.out_pq
, pktout
);
1462 static SshChannel
*ssh2_lportfwd_open(
1463 ConnectionLayer
*cl
, const char *hostname
, int port
,
1464 const char *description
, const SocketPeerInfo
*pi
, Channel
*chan
)
1466 struct ssh2_connection_state
*s
=
1467 container_of(cl
, struct ssh2_connection_state
, cl
);
1468 struct ssh2_channel
*c
= snew(struct ssh2_channel
);
1472 ssh2_channel_init(c
);
1476 pktout
= ssh2_portfwd_chanopen(s
, c
, hostname
, port
, description
, pi
);
1477 pq_push(s
->ppl
.out_pq
, pktout
);
1482 static void ssh2_sharing_globreq_response(
1483 struct ssh2_connection_state
*s
, PktIn
*pktin
, void *ctx
)
1485 ssh_sharing_connstate
*cs
= (ssh_sharing_connstate
*)ctx
;
1486 share_got_pkt_from_server(cs
, pktin
->type
,
1487 BinarySource_UPCAST(pktin
)->data
,
1488 BinarySource_UPCAST(pktin
)->len
);
1491 static void ssh2_sharing_queue_global_request(
1492 ConnectionLayer
*cl
, ssh_sharing_connstate
*cs
)
1494 struct ssh2_connection_state
*s
=
1495 container_of(cl
, struct ssh2_connection_state
, cl
);
1496 ssh2_queue_global_request_handler(s
, ssh2_sharing_globreq_response
, cs
);
1499 static void ssh2_sharing_no_more_downstreams(ConnectionLayer
*cl
)
1501 struct ssh2_connection_state
*s
=
1502 container_of(cl
, struct ssh2_connection_state
, cl
);
1503 queue_toplevel_callback(ssh2_check_termination_callback
, s
);
1506 static struct X11FakeAuth
*ssh2_add_x11_display(
1507 ConnectionLayer
*cl
, int authtype
, struct X11Display
*disp
)
1509 struct ssh2_connection_state
*s
=
1510 container_of(cl
, struct ssh2_connection_state
, cl
);
1511 struct X11FakeAuth
*auth
= x11_invent_fake_auth(s
->x11authtree
, authtype
);
1516 static struct X11FakeAuth
*ssh2_add_sharing_x11_display(
1517 ConnectionLayer
*cl
, int authtype
, ssh_sharing_connstate
*share_cs
,
1518 share_channel
*share_chan
)
1520 struct ssh2_connection_state
*s
=
1521 container_of(cl
, struct ssh2_connection_state
, cl
);
1522 struct X11FakeAuth
*auth
;
1525 * Make up a new set of fake X11 auth data, and add it to the tree
1526 * of currently valid ones with an indication of the sharing
1527 * context that it's relevant to.
1529 auth
= x11_invent_fake_auth(s
->x11authtree
, authtype
);
1530 auth
->share_cs
= share_cs
;
1531 auth
->share_chan
= share_chan
;
1536 static void ssh2_remove_sharing_x11_display(
1537 ConnectionLayer
*cl
, struct X11FakeAuth
*auth
)
1539 struct ssh2_connection_state
*s
=
1540 container_of(cl
, struct ssh2_connection_state
, cl
);
1541 del234(s
->x11authtree
, auth
);
1542 x11_free_fake_auth(auth
);
1545 static unsigned ssh2_alloc_sharing_channel(
1546 ConnectionLayer
*cl
, ssh_sharing_connstate
*connstate
)
1548 struct ssh2_connection_state
*s
=
1549 container_of(cl
, struct ssh2_connection_state
, cl
);
1550 struct ssh2_channel
*c
= snew(struct ssh2_channel
);
1553 ssh2_channel_init(c
);
1555 c
->sharectx
= connstate
;
1559 static void ssh2_delete_sharing_channel(ConnectionLayer
*cl
, unsigned localid
)
1561 struct ssh2_connection_state
*s
=
1562 container_of(cl
, struct ssh2_connection_state
, cl
);
1563 struct ssh2_channel
*c
= find234(s
->channels
, &localid
, ssh2_channelfind
);
1565 ssh2_channel_destroy(c
);
1568 static void ssh2_send_packet_from_downstream(
1569 ConnectionLayer
*cl
, unsigned id
, int type
,
1570 const void *data
, int datalen
, const char *additional_log_text
)
1572 struct ssh2_connection_state
*s
=
1573 container_of(cl
, struct ssh2_connection_state
, cl
);
1574 PktOut
*pkt
= ssh_bpp_new_pktout(s
->ppl
.bpp
, type
);
1575 pkt
->downstream_id
= id
;
1576 pkt
->additional_log_text
= additional_log_text
;
1577 put_data(pkt
, data
, datalen
);
1578 pq_push(s
->ppl
.out_pq
, pkt
);
1581 static bool ssh2_agent_forwarding_permitted(ConnectionLayer
*cl
)
1583 struct ssh2_connection_state
*s
=
1584 container_of(cl
, struct ssh2_connection_state
, cl
);
1585 return conf_get_bool(s
->conf
, CONF_agentfwd
) && agent_exists();
1588 static bool ssh2_connection_get_specials(
1589 PacketProtocolLayer
*ppl
, add_special_fn_t add_special
, void *ctx
)
1591 struct ssh2_connection_state
*s
=
1592 container_of(ppl
, struct ssh2_connection_state
, ppl
);
1596 mainchan_get_specials(s
->mainchan
, add_special
, ctx
);
1601 * Don't bother offering IGNORE if we've decided the remote
1602 * won't cope with it, since we wouldn't bother sending it if
1605 if (!(s
->ppl
.remote_bugs
& BUG_CHOKES_ON_SSH2_IGNORE
)) {
1607 add_special(ctx
, NULL
, SS_SEP
, 0);
1609 add_special(ctx
, "IGNORE message", SS_NOP
, 0);
1616 static void ssh2_connection_special_cmd(PacketProtocolLayer
*ppl
,
1617 SessionSpecialCode code
, int arg
)
1619 struct ssh2_connection_state
*s
=
1620 container_of(ppl
, struct ssh2_connection_state
, ppl
);
1623 if (code
== SS_PING
|| code
== SS_NOP
) {
1624 if (!(s
->ppl
.remote_bugs
& BUG_CHOKES_ON_SSH2_IGNORE
)) {
1625 pktout
= ssh_bpp_new_pktout(s
->ppl
.bpp
, SSH2_MSG_IGNORE
);
1626 put_stringz(pktout
, "");
1627 pq_push(s
->ppl
.out_pq
, pktout
);
1629 } else if (s
->mainchan
) {
1630 mainchan_special_cmd(s
->mainchan
, code
, arg
);
1634 static void ssh2_terminal_size(ConnectionLayer
*cl
, int width
, int height
)
1636 struct ssh2_connection_state
*s
=
1637 container_of(cl
, struct ssh2_connection_state
, cl
);
1639 s
->term_width
= width
;
1640 s
->term_height
= height
;
1642 mainchan_terminal_size(s
->mainchan
, width
, height
);
1645 static void ssh2_stdout_unthrottle(ConnectionLayer
*cl
, size_t bufsize
)
1647 struct ssh2_connection_state
*s
=
1648 container_of(cl
, struct ssh2_connection_state
, cl
);
1651 sshfwd_unthrottle(s
->mainchan_sc
, bufsize
);
1654 static size_t ssh2_stdin_backlog(ConnectionLayer
*cl
)
1656 struct ssh2_connection_state
*s
=
1657 container_of(cl
, struct ssh2_connection_state
, cl
);
1658 struct ssh2_channel
*c
;
1662 c
= container_of(s
->mainchan_sc
, struct ssh2_channel
, sc
);
1663 return s
->mainchan
?
1664 bufchain_size(&c
->outbuffer
) + bufchain_size(&c
->errbuffer
) : 0;
1667 static void ssh2_throttle_all_channels(ConnectionLayer
*cl
, bool throttled
)
1669 struct ssh2_connection_state
*s
=
1670 container_of(cl
, struct ssh2_connection_state
, cl
);
1671 struct ssh2_channel
*c
;
1674 s
->all_channels_throttled
= throttled
;
1676 for (i
= 0; NULL
!= (c
= index234(s
->channels
, i
)); i
++)
1678 ssh2_channel_check_throttle(c
);
1681 static bool ssh2_ldisc_option(ConnectionLayer
*cl
, int option
)
1683 struct ssh2_connection_state
*s
=
1684 container_of(cl
, struct ssh2_connection_state
, cl
);
1686 return s
->ldisc_opts
[option
];
1689 static void ssh2_set_ldisc_option(ConnectionLayer
*cl
, int option
, bool value
)
1691 struct ssh2_connection_state
*s
=
1692 container_of(cl
, struct ssh2_connection_state
, cl
);
1694 s
->ldisc_opts
[option
] = value
;
1697 static void ssh2_enable_x_fwd(ConnectionLayer
*cl
)
1699 struct ssh2_connection_state
*s
=
1700 container_of(cl
, struct ssh2_connection_state
, cl
);
1702 s
->X11_fwd_enabled
= true;
1705 static void ssh2_set_wants_user_input(ConnectionLayer
*cl
, bool wanted
)
1707 struct ssh2_connection_state
*s
=
1708 container_of(cl
, struct ssh2_connection_state
, cl
);
1710 s
->want_user_input
= wanted
;
1712 ssh_check_sendok(s
->ppl
.ssh
);
1715 static bool ssh2_get_wants_user_input(ConnectionLayer
*cl
)
1717 struct ssh2_connection_state
*s
=
1718 container_of(cl
, struct ssh2_connection_state
, cl
);
1719 return s
->want_user_input
;
1722 static void ssh2_got_user_input(ConnectionLayer
*cl
)
1724 struct ssh2_connection_state
*s
=
1725 container_of(cl
, struct ssh2_connection_state
, cl
);
1727 while (s
->mainchan
&& bufchain_size(s
->user_input
) > 0) {
1729 * Add user input to the main channel's buffer.
1731 ptrlen data
= bufchain_prefix(s
->user_input
);
1732 sshfwd_write(s
->mainchan_sc
, data
.ptr
, data
.len
);
1733 bufchain_consume(s
->user_input
, data
.len
);
1737 static void ssh2_connection_reconfigure(PacketProtocolLayer
*ppl
, Conf
*conf
)
1739 struct ssh2_connection_state
*s
=
1740 container_of(ppl
, struct ssh2_connection_state
, ppl
);
1743 s
->conf
= conf_copy(conf
);
1745 if (s
->portfwdmgr_configured
)
1746 portfwdmgr_config(s
->portfwdmgr
, s
->conf
);