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 .name
= "ssh-connection",
33 static SshChannel
*ssh2_lportfwd_open(
34 ConnectionLayer
*cl
, const char *hostname
, int port
,
35 const char *description
, const SocketPeerInfo
*pi
, Channel
*chan
);
36 static struct X11FakeAuth
*ssh2_add_x11_display(
37 ConnectionLayer
*cl
, int authtype
, struct X11Display
*x11disp
);
38 static struct X11FakeAuth
*ssh2_add_sharing_x11_display(
39 ConnectionLayer
*cl
, int authtype
, ssh_sharing_connstate
*share_cs
,
40 share_channel
*share_chan
);
41 static void ssh2_remove_sharing_x11_display(ConnectionLayer
*cl
,
42 struct X11FakeAuth
*auth
);
43 static void ssh2_send_packet_from_downstream(
44 ConnectionLayer
*cl
, unsigned id
, int type
,
45 const void *pkt
, int pktlen
, const char *additional_log_text
);
46 static unsigned ssh2_alloc_sharing_channel(
47 ConnectionLayer
*cl
, ssh_sharing_connstate
*connstate
);
48 static void ssh2_delete_sharing_channel(
49 ConnectionLayer
*cl
, unsigned localid
);
50 static void ssh2_sharing_queue_global_request(
51 ConnectionLayer
*cl
, ssh_sharing_connstate
*share_ctx
);
52 static void ssh2_sharing_no_more_downstreams(ConnectionLayer
*cl
);
53 static bool ssh2_agent_forwarding_permitted(ConnectionLayer
*cl
);
54 static void ssh2_terminal_size(ConnectionLayer
*cl
, int width
, int height
);
55 static void ssh2_stdout_unthrottle(ConnectionLayer
*cl
, size_t bufsize
);
56 static size_t ssh2_stdin_backlog(ConnectionLayer
*cl
);
57 static void ssh2_throttle_all_channels(ConnectionLayer
*cl
, bool throttled
);
58 static bool ssh2_ldisc_option(ConnectionLayer
*cl
, int option
);
59 static void ssh2_set_ldisc_option(ConnectionLayer
*cl
, int option
, bool value
);
60 static void ssh2_enable_x_fwd(ConnectionLayer
*cl
);
61 static void ssh2_set_wants_user_input(ConnectionLayer
*cl
, bool wanted
);
62 static bool ssh2_get_wants_user_input(ConnectionLayer
*cl
);
63 static void ssh2_got_user_input(ConnectionLayer
*cl
);
65 static const ConnectionLayerVtable ssh2_connlayer_vtable
= {
66 .rportfwd_alloc
= ssh2_rportfwd_alloc
,
67 .rportfwd_remove
= ssh2_rportfwd_remove
,
68 .lportfwd_open
= ssh2_lportfwd_open
,
69 .session_open
= ssh2_session_open
,
70 .serverside_x11_open
= ssh2_serverside_x11_open
,
71 .serverside_agent_open
= ssh2_serverside_agent_open
,
72 .add_x11_display
= ssh2_add_x11_display
,
73 .add_sharing_x11_display
= ssh2_add_sharing_x11_display
,
74 .remove_sharing_x11_display
= ssh2_remove_sharing_x11_display
,
75 .send_packet_from_downstream
= ssh2_send_packet_from_downstream
,
76 .alloc_sharing_channel
= ssh2_alloc_sharing_channel
,
77 .delete_sharing_channel
= ssh2_delete_sharing_channel
,
78 .sharing_queue_global_request
= ssh2_sharing_queue_global_request
,
79 .sharing_no_more_downstreams
= ssh2_sharing_no_more_downstreams
,
80 .agent_forwarding_permitted
= ssh2_agent_forwarding_permitted
,
81 .terminal_size
= ssh2_terminal_size
,
82 .stdout_unthrottle
= ssh2_stdout_unthrottle
,
83 .stdin_backlog
= ssh2_stdin_backlog
,
84 .throttle_all_channels
= ssh2_throttle_all_channels
,
85 .ldisc_option
= ssh2_ldisc_option
,
86 .set_ldisc_option
= ssh2_set_ldisc_option
,
87 .enable_x_fwd
= ssh2_enable_x_fwd
,
88 .set_wants_user_input
= ssh2_set_wants_user_input
,
89 .get_wants_user_input
= ssh2_get_wants_user_input
,
90 .got_user_input
= ssh2_got_user_input
,
93 static char *ssh2_channel_open_failure_error_text(PktIn
*pktin
)
95 static const char *const reasons
[] = {
97 "Administratively prohibited",
99 "Unknown channel type",
102 unsigned reason_code
;
103 const char *reason_code_string
;
104 char reason_code_buf
[256];
107 reason_code
= get_uint32(pktin
);
108 if (reason_code
< lenof(reasons
) && reasons
[reason_code
]) {
109 reason_code_string
= reasons
[reason_code
];
111 reason_code_string
= reason_code_buf
;
112 sprintf(reason_code_buf
, "unknown reason code %#x", reason_code
);
115 reason
= get_string(pktin
);
117 return dupprintf("%s [%.*s]", reason_code_string
, PTRLEN_PRINTF(reason
));
120 static size_t ssh2channel_write(
121 SshChannel
*c
, bool is_stderr
, const void *buf
, size_t len
);
122 static void ssh2channel_write_eof(SshChannel
*c
);
123 static void ssh2channel_initiate_close(SshChannel
*c
, const char *err
);
124 static void ssh2channel_unthrottle(SshChannel
*c
, size_t bufsize
);
125 static Conf
*ssh2channel_get_conf(SshChannel
*c
);
126 static void ssh2channel_window_override_removed(SshChannel
*c
);
127 static void ssh2channel_x11_sharing_handover(
128 SshChannel
*c
, ssh_sharing_connstate
*share_cs
, share_channel
*share_chan
,
129 const char *peer_addr
, int peer_port
, int endian
,
130 int protomajor
, int protominor
, const void *initial_data
, int initial_len
);
131 static void ssh2channel_hint_channel_is_simple(SshChannel
*c
);
133 static const SshChannelVtable ssh2channel_vtable
= {
134 .write
= ssh2channel_write
,
135 .write_eof
= ssh2channel_write_eof
,
136 .initiate_close
= ssh2channel_initiate_close
,
137 .unthrottle
= ssh2channel_unthrottle
,
138 .get_conf
= ssh2channel_get_conf
,
139 .window_override_removed
= ssh2channel_window_override_removed
,
140 .x11_sharing_handover
= ssh2channel_x11_sharing_handover
,
141 .send_exit_status
= ssh2channel_send_exit_status
,
142 .send_exit_signal
= ssh2channel_send_exit_signal
,
143 .send_exit_signal_numeric
= ssh2channel_send_exit_signal_numeric
,
144 .request_x11_forwarding
= ssh2channel_request_x11_forwarding
,
145 .request_agent_forwarding
= ssh2channel_request_agent_forwarding
,
146 .request_pty
= ssh2channel_request_pty
,
147 .send_env_var
= ssh2channel_send_env_var
,
148 .start_shell
= ssh2channel_start_shell
,
149 .start_command
= ssh2channel_start_command
,
150 .start_subsystem
= ssh2channel_start_subsystem
,
151 .send_serial_break
= ssh2channel_send_serial_break
,
152 .send_signal
= ssh2channel_send_signal
,
153 .send_terminal_size_change
= ssh2channel_send_terminal_size_change
,
154 .hint_channel_is_simple
= ssh2channel_hint_channel_is_simple
,
157 static void ssh2_channel_check_close(struct ssh2_channel
*c
);
158 static void ssh2_channel_try_eof(struct ssh2_channel
*c
);
159 static void ssh2_set_window(struct ssh2_channel
*c
, int newwin
);
160 static size_t ssh2_try_send(struct ssh2_channel
*c
);
161 static void ssh2_try_send_and_unthrottle(struct ssh2_channel
*c
);
162 static void ssh2_channel_check_throttle(struct ssh2_channel
*c
);
163 static void ssh2_channel_close_local(struct ssh2_channel
*c
,
165 static void ssh2_channel_destroy(struct ssh2_channel
*c
);
167 static void ssh2_check_termination(struct ssh2_connection_state
*s
);
169 struct outstanding_global_request
{
170 gr_handler_fn_t handler
;
172 struct outstanding_global_request
*next
;
174 void ssh2_queue_global_request_handler(
175 struct ssh2_connection_state
*s
, gr_handler_fn_t handler
, void *ctx
)
177 struct outstanding_global_request
*ogr
=
178 snew(struct outstanding_global_request
);
179 ogr
->handler
= handler
;
183 s
->globreq_tail
->next
= ogr
;
185 s
->globreq_head
= ogr
;
186 s
->globreq_tail
= ogr
;
189 static int ssh2_channelcmp(void *av
, void *bv
)
191 const struct ssh2_channel
*a
= (const struct ssh2_channel
*) av
;
192 const struct ssh2_channel
*b
= (const struct ssh2_channel
*) bv
;
193 if (a
->localid
< b
->localid
)
195 if (a
->localid
> b
->localid
)
200 static int ssh2_channelfind(void *av
, void *bv
)
202 const unsigned *a
= (const unsigned *) av
;
203 const struct ssh2_channel
*b
= (const struct ssh2_channel
*) bv
;
212 * Each channel has a queue of outstanding CHANNEL_REQUESTS and their
215 struct outstanding_channel_request
{
216 cr_handler_fn_t handler
;
218 struct outstanding_channel_request
*next
;
221 static void ssh2_channel_free(struct ssh2_channel
*c
)
223 bufchain_clear(&c
->outbuffer
);
224 bufchain_clear(&c
->errbuffer
);
225 while (c
->chanreq_head
) {
226 struct outstanding_channel_request
*chanreq
= c
->chanreq_head
;
227 c
->chanreq_head
= c
->chanreq_head
->next
;
231 struct ssh2_connection_state
*s
= c
->connlayer
;
232 if (s
->mainchan_sc
== &c
->sc
) {
234 s
->mainchan_sc
= NULL
;
241 PacketProtocolLayer
*ssh2_connection_new(
242 Ssh
*ssh
, ssh_sharing_state
*connshare
, bool is_simple
,
243 Conf
*conf
, const char *peer_verstring
, bufchain
*user_input
,
244 ConnectionLayer
**cl_out
)
246 struct ssh2_connection_state
*s
= snew(struct ssh2_connection_state
);
247 memset(s
, 0, sizeof(*s
));
248 s
->ppl
.vt
= &ssh2_connection_vtable
;
250 s
->conf
= conf_copy(conf
);
252 s
->ssh_is_simple
= is_simple
;
255 * If the ssh_no_shell option is enabled, we disable the usual
256 * termination check, so that we persist even in the absence of
257 * any at all channels (because our purpose is probably to be a
258 * background port forwarder).
260 s
->persistent
= conf_get_bool(s
->conf
, CONF_ssh_no_shell
);
262 s
->connshare
= connshare
;
263 s
->peer_verstring
= dupstr(peer_verstring
);
265 s
->channels
= newtree234(ssh2_channelcmp
);
267 s
->x11authtree
= newtree234(x11_authcmp
);
269 s
->user_input
= user_input
;
271 /* Need to get the log context for s->cl now, because we won't be
272 * helpfully notified when a copy is written into s->ppl by our
274 s
->cl
.vt
= &ssh2_connlayer_vtable
;
275 s
->cl
.logctx
= ssh_get_logctx(ssh
);
277 s
->portfwdmgr
= portfwdmgr_new(&s
->cl
);
281 ssh_connshare_provide_connlayer(s
->connshare
, &s
->cl
);
286 static void ssh2_connection_free(PacketProtocolLayer
*ppl
)
288 struct ssh2_connection_state
*s
=
289 container_of(ppl
, struct ssh2_connection_state
, ppl
);
290 struct X11FakeAuth
*auth
;
291 struct ssh2_channel
*c
;
292 struct ssh_rportfwd
*rpf
;
294 sfree(s
->peer_verstring
);
298 while ((c
= delpos234(s
->channels
, 0)) != NULL
)
299 ssh2_channel_free(c
);
300 freetree234(s
->channels
);
302 while ((auth
= delpos234(s
->x11authtree
, 0)) != NULL
) {
304 x11_free_display(auth
->disp
);
305 x11_free_fake_auth(auth
);
307 freetree234(s
->x11authtree
);
310 while ((rpf
= delpos234(s
->rportfwds
, 0)) != NULL
)
312 freetree234(s
->rportfwds
);
314 portfwdmgr_free(s
->portfwdmgr
);
316 if (s
->antispoof_prompt
)
317 free_prompts(s
->antispoof_prompt
);
319 delete_callbacks_for_context(s
);
324 static bool ssh2_connection_filter_queue(struct ssh2_connection_state
*s
)
329 struct ssh2_channel
*c
;
330 struct outstanding_channel_request
*ocr
;
331 unsigned localid
, remid
, winsize
, pktsize
, ext_type
;
332 bool want_reply
, reply_success
, expect_halfopen
;
333 ChanopenResult chanopen_result
;
334 PacketProtocolLayer
*ppl
= &s
->ppl
; /* for ppl_logevent */
337 if (ssh2_common_filter_queue(&s
->ppl
))
339 if ((pktin
= pq_peek(s
->ppl
.in_pq
)) == NULL
)
342 switch (pktin
->type
) {
343 case SSH2_MSG_GLOBAL_REQUEST
:
344 type
= get_string(pktin
);
345 want_reply
= get_bool(pktin
);
347 reply_success
= ssh2_connection_parse_global_request(
351 int type
= (reply_success
? SSH2_MSG_REQUEST_SUCCESS
:
352 SSH2_MSG_REQUEST_FAILURE
);
353 pktout
= ssh_bpp_new_pktout(s
->ppl
.bpp
, type
);
354 pq_push(s
->ppl
.out_pq
, pktout
);
356 pq_pop(s
->ppl
.in_pq
);
359 case SSH2_MSG_REQUEST_SUCCESS
:
360 case SSH2_MSG_REQUEST_FAILURE
:
361 if (!s
->globreq_head
) {
364 "Received %s with no outstanding global request",
365 ssh2_pkt_type(s
->ppl
.bpp
->pls
->kctx
, s
->ppl
.bpp
->pls
->actx
,
370 s
->globreq_head
->handler(s
, pktin
, s
->globreq_head
->ctx
);
372 struct outstanding_global_request
*tmp
= s
->globreq_head
;
373 s
->globreq_head
= s
->globreq_head
->next
;
376 if (!s
->globreq_head
)
377 s
->globreq_tail
= NULL
;
379 pq_pop(s
->ppl
.in_pq
);
382 case SSH2_MSG_CHANNEL_OPEN
:
383 type
= get_string(pktin
);
384 c
= snew(struct ssh2_channel
);
388 remid
= get_uint32(pktin
);
389 winsize
= get_uint32(pktin
);
390 pktsize
= get_uint32(pktin
);
392 chanopen_result
= ssh2_connection_parse_channel_open(
393 s
, type
, pktin
, &c
->sc
);
395 if (chanopen_result
.outcome
== CHANOPEN_RESULT_DOWNSTREAM
) {
397 * This channel-open request needs to go to a
398 * connection-sharing downstream, so abandon our own
399 * channel-open procedure and just pass the message on
402 share_got_pkt_from_server(
403 chanopen_result
.u
.downstream
.share_ctx
, pktin
->type
,
404 BinarySource_UPCAST(pktin
)->data
,
405 BinarySource_UPCAST(pktin
)->len
);
412 if (chanopen_result
.outcome
== CHANOPEN_RESULT_FAILURE
) {
413 pktout
= ssh_bpp_new_pktout(
414 s
->ppl
.bpp
, SSH2_MSG_CHANNEL_OPEN_FAILURE
);
415 put_uint32(pktout
, c
->remoteid
);
416 put_uint32(pktout
, chanopen_result
.u
.failure
.reason_code
);
417 put_stringz(pktout
, chanopen_result
.u
.failure
.wire_message
);
418 put_stringz(pktout
, "en"); /* language tag */
419 pq_push(s
->ppl
.out_pq
, pktout
);
420 ppl_logevent("Rejected channel open: %s",
421 chanopen_result
.u
.failure
.wire_message
);
422 sfree(chanopen_result
.u
.failure
.wire_message
);
425 c
->chan
= chanopen_result
.u
.success
.channel
;
426 ssh2_channel_init(c
);
427 c
->remwindow
= winsize
;
428 c
->remmaxpkt
= pktsize
;
429 if (c
->remmaxpkt
> s
->ppl
.bpp
->vt
->packet_size_limit
)
430 c
->remmaxpkt
= s
->ppl
.bpp
->vt
->packet_size_limit
;
431 if (c
->chan
->initial_fixed_window_size
) {
432 c
->locwindow
= c
->locmaxwin
= c
->remlocwin
=
433 c
->chan
->initial_fixed_window_size
;
435 pktout
= ssh_bpp_new_pktout(
436 s
->ppl
.bpp
, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
);
437 put_uint32(pktout
, c
->remoteid
);
438 put_uint32(pktout
, c
->localid
);
439 put_uint32(pktout
, c
->locwindow
);
440 put_uint32(pktout
, OUR_V2_MAXPKT
); /* our max pkt size */
441 pq_push(s
->ppl
.out_pq
, pktout
);
444 pq_pop(s
->ppl
.in_pq
);
447 case SSH2_MSG_CHANNEL_DATA
:
448 case SSH2_MSG_CHANNEL_EXTENDED_DATA
:
449 case SSH2_MSG_CHANNEL_WINDOW_ADJUST
:
450 case SSH2_MSG_CHANNEL_REQUEST
:
451 case SSH2_MSG_CHANNEL_EOF
:
452 case SSH2_MSG_CHANNEL_CLOSE
:
453 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
:
454 case SSH2_MSG_CHANNEL_OPEN_FAILURE
:
455 case SSH2_MSG_CHANNEL_SUCCESS
:
456 case SSH2_MSG_CHANNEL_FAILURE
:
458 * Common preliminary code for all the messages from the
459 * server that cite one of our channel ids: look up that
460 * channel id, check it exists, and if it's for a sharing
461 * downstream, pass it on.
463 localid
= get_uint32(pktin
);
464 c
= find234(s
->channels
, &localid
, ssh2_channelfind
);
466 if (c
&& c
->sharectx
) {
467 share_got_pkt_from_server(c
->sharectx
, pktin
->type
,
468 BinarySource_UPCAST(pktin
)->data
,
469 BinarySource_UPCAST(pktin
)->len
);
470 pq_pop(s
->ppl
.in_pq
);
475 pktin
->type
== SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
||
476 pktin
->type
== SSH2_MSG_CHANNEL_OPEN_FAILURE
);
478 if (!c
|| c
->halfopen
!= expect_halfopen
) {
479 ssh_proto_error(s
->ppl
.ssh
,
480 "Received %s for %s channel %u",
481 ssh2_pkt_type(s
->ppl
.bpp
->pls
->kctx
,
482 s
->ppl
.bpp
->pls
->actx
,
484 (!c
? "nonexistent" :
485 c
->halfopen
? "half-open" : "open"),
490 switch (pktin
->type
) {
491 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
:
493 c
->remoteid
= get_uint32(pktin
);
495 c
->remwindow
= get_uint32(pktin
);
496 c
->remmaxpkt
= get_uint32(pktin
);
497 if (c
->remmaxpkt
> s
->ppl
.bpp
->vt
->packet_size_limit
)
498 c
->remmaxpkt
= s
->ppl
.bpp
->vt
->packet_size_limit
;
500 chan_open_confirmation(c
->chan
);
503 * Now that the channel is fully open, it's possible
504 * in principle to immediately close it. Check whether
507 * This can occur if a local socket error occurred
508 * between us sending out CHANNEL_OPEN and receiving
509 * OPEN_CONFIRMATION. If that happens, all we can do
510 * is immediately initiate close proceedings now that
511 * we know the server's id to put in the close
512 * message. We'll have handled that in this code by
513 * having already turned c->chan into a zombie, so its
514 * want_close method (which ssh2_channel_check_close
515 * will consult) will already be returning true.
517 ssh2_channel_check_close(c
);
520 ssh2_channel_try_eof(c
); /* in case we had a pending EOF */
523 case SSH2_MSG_CHANNEL_OPEN_FAILURE
: {
526 char *err
= ssh2_channel_open_failure_error_text(pktin
);
527 chan_open_failed(c
->chan
, err
);
530 del234(s
->channels
, c
);
531 ssh2_channel_free(c
);
536 case SSH2_MSG_CHANNEL_DATA
:
537 case SSH2_MSG_CHANNEL_EXTENDED_DATA
:
538 ext_type
= (pktin
->type
== SSH2_MSG_CHANNEL_DATA
? 0 :
540 data
= get_string(pktin
);
541 if (!get_err(pktin
)) {
543 c
->locwindow
-= data
.len
;
544 c
->remlocwin
-= data
.len
;
545 if (ext_type
!= 0 && ext_type
!= SSH2_EXTENDED_DATA_STDERR
)
546 data
.len
= 0; /* ignore unknown extended data */
548 c
->chan
, ext_type
== SSH2_EXTENDED_DATA_STDERR
,
552 * The channel may have turned into a connection-
553 * shared one as a result of that chan_send, e.g.
554 * if the data we just provided completed the X11
555 * auth phase and caused a callback to
556 * x11_sharing_handover. If so, do nothing
563 * If it looks like the remote end hit the end of
564 * its window, and we didn't want it to do that,
565 * think about using a larger window.
567 if (c
->remlocwin
<= 0 &&
568 c
->throttle_state
== UNTHROTTLED
&&
569 c
->locmaxwin
< 0x40000000)
570 c
->locmaxwin
+= OUR_V2_WINSIZE
;
573 * If we are not buffering too much data, enlarge
574 * the window again at the remote side. If we are
575 * buffering too much, we may still need to adjust
576 * the window if the server's sent excess data.
578 if (bufsize
< c
->locmaxwin
)
579 ssh2_set_window(c
, c
->locmaxwin
- bufsize
);
582 * If we're either buffering way too much data, or
583 * if we're buffering anything at all and we're in
584 * "simple" mode, throttle the whole channel.
586 if ((bufsize
> c
->locmaxwin
||
587 (s
->ssh_is_simple
&& bufsize
>0)) &&
588 !c
->throttling_conn
) {
589 c
->throttling_conn
= true;
590 ssh_throttle_conn(s
->ppl
.ssh
, +1);
595 case SSH2_MSG_CHANNEL_WINDOW_ADJUST
:
596 if (!(c
->closes
& CLOSES_SENT_EOF
)) {
597 c
->remwindow
+= get_uint32(pktin
);
598 ssh2_try_send_and_unthrottle(c
);
602 case SSH2_MSG_CHANNEL_REQUEST
:
603 type
= get_string(pktin
);
604 want_reply
= get_bool(pktin
);
606 reply_success
= false;
608 if (c
->closes
& CLOSES_SENT_CLOSE
) {
610 * We don't reply to channel requests after we've
611 * sent CHANNEL_CLOSE for the channel, because our
612 * reply might cross in the network with the other
613 * side's CHANNEL_CLOSE and arrive after they have
614 * wound the channel up completely.
620 * Try every channel request name we recognise, no
621 * matter what the channel, and see if the Channel
622 * instance will accept it.
624 if (ptrlen_eq_string(type
, "exit-status")) {
625 int exitcode
= toint(get_uint32(pktin
));
626 reply_success
= chan_rcvd_exit_status(c
->chan
, exitcode
);
627 } else if (ptrlen_eq_string(type
, "exit-signal")) {
635 * ICK: older versions of OpenSSH (e.g. 3.4p1)
636 * provide an `int' for the signal, despite its
637 * having been a `string' in the drafts of RFC
638 * 4254 since at least 2001. (Fixed in session.c
639 * 1.147.) Try to infer which we can safely parse
643 size_t startpos
= BinarySource_UPCAST(pktin
)->pos
;
645 for (format
= 0; format
< 2; format
++) {
646 BinarySource_UPCAST(pktin
)->pos
= startpos
;
647 BinarySource_UPCAST(pktin
)->err
= BSE_NO_ERROR
;
649 /* placate compiler warnings about unin */
650 signame
= make_ptrlen(NULL
, 0);
653 if (format
== 0) /* standard string-based format */
654 signame
= get_string(pktin
);
655 else /* nonstandard integer format */
656 signum
= toint(get_uint32(pktin
));
658 core
= get_bool(pktin
);
659 errmsg
= get_string(pktin
); /* error message */
660 get_string(pktin
); /* language tag */
662 if (!get_err(pktin
) && get_avail(pktin
) == 0)
663 break; /* successful parse */
668 reply_success
= chan_rcvd_exit_signal(
669 c
->chan
, signame
, core
, errmsg
);
672 reply_success
= chan_rcvd_exit_signal_numeric(
673 c
->chan
, signum
, core
, errmsg
);
676 /* Couldn't parse this message in either format */
677 reply_success
= false;
680 } else if (ptrlen_eq_string(type
, "shell")) {
681 reply_success
= chan_run_shell(c
->chan
);
682 } else if (ptrlen_eq_string(type
, "exec")) {
683 ptrlen command
= get_string(pktin
);
684 reply_success
= chan_run_command(c
->chan
, command
);
685 } else if (ptrlen_eq_string(type
, "subsystem")) {
686 ptrlen subsys
= get_string(pktin
);
687 reply_success
= chan_run_subsystem(c
->chan
, subsys
);
688 } else if (ptrlen_eq_string(type
, "x11-req")) {
689 bool oneshot
= get_bool(pktin
);
690 ptrlen authproto
= get_string(pktin
);
691 ptrlen authdata
= get_string(pktin
);
692 unsigned screen_number
= get_uint32(pktin
);
693 reply_success
= chan_enable_x11_forwarding(
694 c
->chan
, oneshot
, authproto
, authdata
, screen_number
);
695 } else if (ptrlen_eq_string(type
,
696 "auth-agent-req@openssh.com")) {
697 reply_success
= chan_enable_agent_forwarding(c
->chan
);
698 } else if (ptrlen_eq_string(type
, "pty-req")) {
699 ptrlen termtype
= get_string(pktin
);
700 unsigned width
= get_uint32(pktin
);
701 unsigned height
= get_uint32(pktin
);
702 unsigned pixwidth
= get_uint32(pktin
);
703 unsigned pixheight
= get_uint32(pktin
);
704 ptrlen encoded_modes
= get_string(pktin
);
705 BinarySource bs_modes
[1];
706 struct ssh_ttymodes modes
;
708 BinarySource_BARE_INIT_PL(bs_modes
, encoded_modes
);
709 modes
= read_ttymodes_from_packet(bs_modes
, 2);
710 if (get_err(bs_modes
) || get_avail(bs_modes
) > 0) {
711 ppl_logevent("Unable to decode terminal mode string");
712 reply_success
= false;
714 reply_success
= chan_allocate_pty(
715 c
->chan
, termtype
, width
, height
,
716 pixwidth
, pixheight
, modes
);
718 } else if (ptrlen_eq_string(type
, "env")) {
719 ptrlen var
= get_string(pktin
);
720 ptrlen value
= get_string(pktin
);
722 reply_success
= chan_set_env(c
->chan
, var
, value
);
723 } else if (ptrlen_eq_string(type
, "break")) {
724 unsigned length
= get_uint32(pktin
);
726 reply_success
= chan_send_break(c
->chan
, length
);
727 } else if (ptrlen_eq_string(type
, "signal")) {
728 ptrlen signame
= get_string(pktin
);
730 reply_success
= chan_send_signal(c
->chan
, signame
);
731 } else if (ptrlen_eq_string(type
, "window-change")) {
732 unsigned width
= get_uint32(pktin
);
733 unsigned height
= get_uint32(pktin
);
734 unsigned pixwidth
= get_uint32(pktin
);
735 unsigned pixheight
= get_uint32(pktin
);
736 reply_success
= chan_change_window_size(
737 c
->chan
, width
, height
, pixwidth
, pixheight
);
740 int type
= (reply_success
? SSH2_MSG_CHANNEL_SUCCESS
:
741 SSH2_MSG_CHANNEL_FAILURE
);
742 pktout
= ssh_bpp_new_pktout(s
->ppl
.bpp
, type
);
743 put_uint32(pktout
, c
->remoteid
);
744 pq_push(s
->ppl
.out_pq
, pktout
);
748 case SSH2_MSG_CHANNEL_SUCCESS
:
749 case SSH2_MSG_CHANNEL_FAILURE
:
750 ocr
= c
->chanreq_head
;
754 "Received %s for channel %d with no outstanding "
756 ssh2_pkt_type(s
->ppl
.bpp
->pls
->kctx
,
757 s
->ppl
.bpp
->pls
->actx
, pktin
->type
),
761 ocr
->handler(c
, pktin
, ocr
->ctx
);
762 c
->chanreq_head
= ocr
->next
;
765 * We may now initiate channel-closing procedures, if
766 * that CHANNEL_REQUEST was the last thing outstanding
767 * before we send CHANNEL_CLOSE.
769 ssh2_channel_check_close(c
);
772 case SSH2_MSG_CHANNEL_EOF
:
773 if (!(c
->closes
& CLOSES_RCVD_EOF
)) {
774 c
->closes
|= CLOSES_RCVD_EOF
;
775 chan_send_eof(c
->chan
);
776 ssh2_channel_check_close(c
);
780 case SSH2_MSG_CHANNEL_CLOSE
:
782 * When we receive CLOSE on a channel, we assume it
783 * comes with an implied EOF if we haven't seen EOF
786 if (!(c
->closes
& CLOSES_RCVD_EOF
)) {
787 c
->closes
|= CLOSES_RCVD_EOF
;
788 chan_send_eof(c
->chan
);
791 if (!(s
->ppl
.remote_bugs
& BUG_SENDS_LATE_REQUEST_REPLY
)) {
793 * It also means we stop expecting to see replies
794 * to any outstanding channel requests, so clean
795 * those up too. (ssh_chanreq_init will enforce by
796 * assertion that we don't subsequently put
797 * anything back on this list.)
799 while (c
->chanreq_head
) {
800 struct outstanding_channel_request
*ocr
=
802 ocr
->handler(c
, NULL
, ocr
->ctx
);
803 c
->chanreq_head
= ocr
->next
;
809 * And we also send an outgoing EOF, if we haven't
810 * already, on the assumption that CLOSE is a pretty
811 * forceful announcement that the remote side is doing
812 * away with the entire channel. (If it had wanted to
813 * send us EOF and continue receiving data from us, it
814 * would have just sent CHANNEL_EOF.)
816 if (!(c
->closes
& CLOSES_SENT_EOF
)) {
818 * Abandon any buffered data we still wanted to
819 * send to this channel. Receiving a CHANNEL_CLOSE
820 * is an indication that the server really wants
821 * to get on and _destroy_ this channel, and it
822 * isn't going to send us any further
823 * WINDOW_ADJUSTs to permit us to send pending
826 bufchain_clear(&c
->outbuffer
);
827 bufchain_clear(&c
->errbuffer
);
832 sshfwd_write_eof(&c
->sc
);
835 * Make sure we don't read any more from whatever
836 * our local data source is for this channel.
837 * (This will pick up on the changes made by
840 ssh2_channel_check_throttle(c
);
844 * Now process the actual close.
846 if (!(c
->closes
& CLOSES_RCVD_CLOSE
)) {
847 c
->closes
|= CLOSES_RCVD_CLOSE
;
848 ssh2_channel_check_close(c
);
854 pq_pop(s
->ppl
.in_pq
);
863 static void ssh2_handle_winadj_response(struct ssh2_channel
*c
,
864 PktIn
*pktin
, void *ctx
)
866 unsigned *sizep
= ctx
;
869 * Winadj responses should always be failures. However, at least
870 * one server ("boks_sshd") is known to return SUCCESS for channel
871 * requests it's never heard of, such as "winadj@putty". Raised
872 * with foxt.com as bug 090916-090424, but for the sake of a quiet
873 * life, we don't worry about what kind of response we got.
876 c
->remlocwin
+= *sizep
;
879 * winadj messages are only sent when the window is fully open, so
880 * if we get an ack of one, we know any pending unthrottle is
883 if (c
->throttle_state
== UNTHROTTLING
)
884 c
->throttle_state
= UNTHROTTLED
;
887 static void ssh2_set_window(struct ssh2_channel
*c
, int newwin
)
889 struct ssh2_connection_state
*s
= c
->connlayer
;
892 * Never send WINDOW_ADJUST for a channel that the remote side has
893 * already sent EOF on; there's no point, since it won't be
894 * sending any more data anyway. Ditto if _we've_ already sent
897 if (c
->closes
& (CLOSES_RCVD_EOF
| CLOSES_SENT_CLOSE
))
901 * If the client-side Channel is in an initial setup phase with a
902 * fixed window size, e.g. for an X11 channel when we're still
903 * waiting to see its initial auth and may yet hand it off to a
904 * downstream, don't send any WINDOW_ADJUST either.
906 if (c
->chan
->initial_fixed_window_size
)
910 * If the remote end has a habit of ignoring maxpkt, limit the
911 * window so that it has no choice (assuming it doesn't ignore the
914 if ((s
->ppl
.remote_bugs
& BUG_SSH2_MAXPKT
) && newwin
> OUR_V2_MAXPKT
)
915 newwin
= OUR_V2_MAXPKT
;
918 * Only send a WINDOW_ADJUST if there's significantly more window
919 * available than the other end thinks there is. This saves us
920 * sending a WINDOW_ADJUST for every character in a shell session.
922 * "Significant" is arbitrarily defined as half the window size.
924 if (newwin
/ 2 >= c
->locwindow
) {
929 * In order to keep track of how much window the client
930 * actually has available, we'd like it to acknowledge each
931 * WINDOW_ADJUST. We can't do that directly, so we accompany
932 * it with a CHANNEL_REQUEST that has to be acknowledged.
934 * This is only necessary if we're opening the window wide.
935 * If we're not, then throughput is being constrained by
936 * something other than the maximum window size anyway.
938 if (newwin
== c
->locmaxwin
&&
939 !(s
->ppl
.remote_bugs
& BUG_CHOKES_ON_WINADJ
)) {
941 *up
= newwin
- c
->locwindow
;
942 pktout
= ssh2_chanreq_init(c
, "winadj@putty.projects.tartarus.org",
943 ssh2_handle_winadj_response
, up
);
944 pq_push(s
->ppl
.out_pq
, pktout
);
946 if (c
->throttle_state
!= UNTHROTTLED
)
947 c
->throttle_state
= UNTHROTTLING
;
949 /* Pretend the WINDOW_ADJUST was acked immediately. */
950 c
->remlocwin
= newwin
;
951 c
->throttle_state
= THROTTLED
;
953 pktout
= ssh_bpp_new_pktout(s
->ppl
.bpp
, SSH2_MSG_CHANNEL_WINDOW_ADJUST
);
954 put_uint32(pktout
, c
->remoteid
);
955 put_uint32(pktout
, newwin
- c
->locwindow
);
956 pq_push(s
->ppl
.out_pq
, pktout
);
957 c
->locwindow
= newwin
;
961 static PktIn
*ssh2_connection_pop(struct ssh2_connection_state
*s
)
963 ssh2_connection_filter_queue(s
);
964 return pq_pop(s
->ppl
.in_pq
);
967 static void ssh2_connection_process_queue(PacketProtocolLayer
*ppl
)
969 struct ssh2_connection_state
*s
=
970 container_of(ppl
, struct ssh2_connection_state
, ppl
);
973 if (ssh2_connection_filter_queue(s
)) /* no matter why we were called */
979 share_activate(s
->connshare
, s
->peer_verstring
);
982 * Signal the seat that authentication is done, so that it can
983 * deploy spoofing defences. If it doesn't have any, deploy our
986 * We do this here rather than at the end of userauth, because we
987 * might not have gone through userauth at all (if we're a
988 * connection-sharing downstream).
990 if (ssh2_connection_need_antispoof_prompt(s
)) {
991 s
->antispoof_prompt
= ssh_ppl_new_prompts(&s
->ppl
);
992 s
->antispoof_prompt
->to_server
= false;
993 s
->antispoof_prompt
->from_server
= false;
994 s
->antispoof_prompt
->name
= dupstr("Authentication successful");
997 dupstr("Access granted. Press Return to begin session. "), false);
998 s
->antispoof_ret
= seat_get_userpass_input(
999 ppl_get_iseat(&s
->ppl
), s
->antispoof_prompt
);
1000 while (s
->antispoof_ret
.kind
== SPRK_INCOMPLETE
) {
1002 s
->antispoof_ret
= seat_get_userpass_input(
1003 ppl_get_iseat(&s
->ppl
), s
->antispoof_prompt
);
1005 free_prompts(s
->antispoof_prompt
);
1006 s
->antispoof_prompt
= NULL
;
1010 * Enable port forwardings.
1012 portfwdmgr_config(s
->portfwdmgr
, s
->conf
);
1013 s
->portfwdmgr_configured
= true;
1016 * Create the main session channel, if any.
1018 s
->mainchan
= mainchan_new(
1019 &s
->ppl
, &s
->cl
, s
->conf
, s
->term_width
, s
->term_height
,
1020 s
->ssh_is_simple
, &s
->mainchan_sc
);
1028 if ((pktin
= ssh2_connection_pop(s
)) != NULL
) {
1031 * _All_ the connection-layer packets we expect to
1032 * receive are now handled by the dispatch table.
1033 * Anything that reaches here must be bogus.
1036 ssh_proto_error(s
->ppl
.ssh
, "Received unexpected connection-layer "
1037 "packet, type %d (%s)", pktin
->type
,
1038 ssh2_pkt_type(s
->ppl
.bpp
->pls
->kctx
,
1039 s
->ppl
.bpp
->pls
->actx
,
1049 static void ssh2_channel_check_close(struct ssh2_channel
*c
)
1051 struct ssh2_connection_state
*s
= c
->connlayer
;
1056 * If we've sent out our own CHANNEL_OPEN but not yet seen
1057 * either OPEN_CONFIRMATION or OPEN_FAILURE in response, then
1058 * it's too early to be sending close messages of any kind.
1063 if (chan_want_close(c
->chan
, (c
->closes
& CLOSES_SENT_EOF
),
1064 (c
->closes
& CLOSES_RCVD_EOF
)) &&
1066 !(c
->closes
& CLOSES_SENT_CLOSE
)) {
1068 * We have both sent and received EOF (or the channel is a
1069 * zombie), and we have no outstanding channel requests, which
1070 * means the channel is in final wind-up. But we haven't sent
1071 * CLOSE, so let's do so now.
1073 pktout
= ssh_bpp_new_pktout(s
->ppl
.bpp
, SSH2_MSG_CHANNEL_CLOSE
);
1074 put_uint32(pktout
, c
->remoteid
);
1075 pq_push(s
->ppl
.out_pq
, pktout
);
1076 c
->closes
|= CLOSES_SENT_EOF
| CLOSES_SENT_CLOSE
;
1079 if (!((CLOSES_SENT_CLOSE
| CLOSES_RCVD_CLOSE
) & ~c
->closes
)) {
1080 assert(c
->chanreq_head
== NULL
);
1082 * We have both sent and received CLOSE, which means we're
1083 * completely done with the channel.
1085 ssh2_channel_destroy(c
);
1089 static void ssh2_channel_try_eof(struct ssh2_channel
*c
)
1091 struct ssh2_connection_state
*s
= c
->connlayer
;
1093 assert(c
->pending_eof
); /* precondition for calling us */
1095 return; /* can't close: not even opened yet */
1096 if (bufchain_size(&c
->outbuffer
) > 0 || bufchain_size(&c
->errbuffer
) > 0)
1097 return; /* can't send EOF: pending outgoing data */
1099 c
->pending_eof
= false; /* we're about to send it */
1101 pktout
= ssh_bpp_new_pktout(s
->ppl
.bpp
, SSH2_MSG_CHANNEL_EOF
);
1102 put_uint32(pktout
, c
->remoteid
);
1103 pq_push(s
->ppl
.out_pq
, pktout
);
1104 c
->closes
|= CLOSES_SENT_EOF
;
1105 ssh2_channel_check_close(c
);
1109 * Attempt to send data on an SSH-2 channel.
1111 static size_t ssh2_try_send(struct ssh2_channel
*c
)
1113 struct ssh2_connection_state
*s
= c
->connlayer
;
1118 while (c
->remwindow
> 0 &&
1119 (bufchain_size(&c
->outbuffer
) > 0 ||
1120 bufchain_size(&c
->errbuffer
) > 0)) {
1121 bufchain
*buf
= (bufchain_size(&c
->errbuffer
) > 0 ?
1122 &c
->errbuffer
: &c
->outbuffer
);
1124 ptrlen data
= bufchain_prefix(buf
);
1125 if (data
.len
> c
->remwindow
)
1126 data
.len
= c
->remwindow
;
1127 if (data
.len
> c
->remmaxpkt
)
1128 data
.len
= c
->remmaxpkt
;
1129 if (buf
== &c
->errbuffer
) {
1130 pktout
= ssh_bpp_new_pktout(
1131 s
->ppl
.bpp
, SSH2_MSG_CHANNEL_EXTENDED_DATA
);
1132 put_uint32(pktout
, c
->remoteid
);
1133 put_uint32(pktout
, SSH2_EXTENDED_DATA_STDERR
);
1135 pktout
= ssh_bpp_new_pktout(s
->ppl
.bpp
, SSH2_MSG_CHANNEL_DATA
);
1136 put_uint32(pktout
, c
->remoteid
);
1138 put_stringpl(pktout
, data
);
1139 pq_push(s
->ppl
.out_pq
, pktout
);
1140 bufchain_consume(buf
, data
.len
);
1141 c
->remwindow
-= data
.len
;
1146 * After having sent as much data as we can, return the amount
1149 bufsize
= bufchain_size(&c
->outbuffer
) + bufchain_size(&c
->errbuffer
);
1152 * And if there's no data pending but we need to send an EOF, send
1155 if (!bufsize
&& c
->pending_eof
)
1156 ssh2_channel_try_eof(c
);
1158 ssh_sendbuffer_changed(s
->ppl
.ssh
);
1162 static void ssh2_try_send_and_unthrottle(struct ssh2_channel
*c
)
1165 if (c
->closes
& CLOSES_SENT_EOF
)
1166 return; /* don't send on channels we've EOFed */
1167 bufsize
= ssh2_try_send(c
);
1169 c
->throttled_by_backlog
= false;
1170 ssh2_channel_check_throttle(c
);
1174 static void ssh2_channel_check_throttle(struct ssh2_channel
*c
)
1177 * We don't want this channel to read further input if this
1178 * particular channel has a backed-up SSH window, or if the
1179 * outgoing side of the whole SSH connection is currently
1180 * throttled, or if this channel already has an outgoing EOF
1181 * either sent or pending.
1183 chan_set_input_wanted(c
->chan
,
1184 !c
->throttled_by_backlog
&&
1185 !c
->connlayer
->all_channels_throttled
&&
1187 !(c
->closes
& CLOSES_SENT_EOF
));
1191 * Close any local socket and free any local resources associated with
1192 * a channel. This converts the channel into a zombie.
1194 static void ssh2_channel_close_local(struct ssh2_channel
*c
,
1197 struct ssh2_connection_state
*s
= c
->connlayer
;
1198 PacketProtocolLayer
*ppl
= &s
->ppl
; /* for ppl_logevent */
1204 msg
= chan_log_close_msg(c
->chan
);
1207 ppl_logevent("%s%s%s", msg
, reason
? " " : "", reason
? reason
: "");
1212 c
->chan
= zombiechan_new();
1215 static void ssh2_check_termination_callback(void *vctx
)
1217 struct ssh2_connection_state
*s
= (struct ssh2_connection_state
*)vctx
;
1218 ssh2_check_termination(s
);
1221 static void ssh2_channel_destroy(struct ssh2_channel
*c
)
1223 struct ssh2_connection_state
*s
= c
->connlayer
;
1225 assert(c
->chanreq_head
== NULL
);
1227 ssh2_channel_close_local(c
, NULL
);
1228 del234(s
->channels
, c
);
1229 ssh2_channel_free(c
);
1232 * If that was the last channel left open, we might need to
1233 * terminate. But we'll be a bit cautious, by doing that in a
1234 * toplevel callback, just in case anything on the current call
1235 * stack objects to this entire PPL being freed.
1237 queue_toplevel_callback(ssh2_check_termination_callback
, s
);
1240 static void ssh2_check_termination(struct ssh2_connection_state
*s
)
1243 * Decide whether we should terminate the SSH connection now.
1244 * Called after a channel or a downstream goes away. The general
1245 * policy is that we terminate when none of either is left.
1249 return; /* persistent mode: never proactively terminate */
1252 /* At startup, we don't have any channels open because we
1253 * haven't got round to opening the main one yet. In that
1254 * situation, we don't want to terminate, even if a sharing
1255 * connection opens and closes and causes a call to this
1260 if (count234(s
->channels
) == 0 &&
1261 !(s
->connshare
&& share_ndownstreams(s
->connshare
) > 0)) {
1263 * We used to send SSH_MSG_DISCONNECT here, because I'd
1264 * believed that _every_ conforming SSH-2 connection had to
1265 * end with a disconnect being sent by at least one side;
1266 * apparently I was wrong and it's perfectly OK to
1267 * unceremoniously slam the connection shut when you're done,
1268 * and indeed OpenSSH feels this is more polite than sending a
1269 * DISCONNECT. So now we don't.
1271 ssh_user_close(s
->ppl
.ssh
, "All channels closed");
1277 * Set up most of a new ssh2_channel. Nulls out sharectx, but leaves
1278 * chan untouched (since it will sometimes have been filled in before
1281 void ssh2_channel_init(struct ssh2_channel
*c
)
1283 struct ssh2_connection_state
*s
= c
->connlayer
;
1285 c
->pending_eof
= false;
1286 c
->throttling_conn
= false;
1287 c
->throttled_by_backlog
= false;
1289 c
->locwindow
= c
->locmaxwin
= c
->remlocwin
=
1290 s
->ssh_is_simple
? OUR_V2_BIGWIN
: OUR_V2_WINSIZE
;
1291 c
->chanreq_head
= NULL
;
1292 c
->throttle_state
= UNTHROTTLED
;
1293 bufchain_init(&c
->outbuffer
);
1294 bufchain_init(&c
->errbuffer
);
1295 c
->sc
.vt
= &ssh2channel_vtable
;
1297 c
->localid
= alloc_channel_id(s
->channels
, struct ssh2_channel
);
1298 add234(s
->channels
, c
);
1302 * Construct the common parts of a CHANNEL_OPEN.
1304 PktOut
*ssh2_chanopen_init(struct ssh2_channel
*c
, const char *type
)
1306 struct ssh2_connection_state
*s
= c
->connlayer
;
1309 pktout
= ssh_bpp_new_pktout(s
->ppl
.bpp
, SSH2_MSG_CHANNEL_OPEN
);
1310 put_stringz(pktout
, type
);
1311 put_uint32(pktout
, c
->localid
);
1312 put_uint32(pktout
, c
->locwindow
); /* our window size */
1313 put_uint32(pktout
, OUR_V2_MAXPKT
); /* our max pkt size */
1318 * Construct the common parts of a CHANNEL_REQUEST. If handler is not
1319 * NULL then a reply will be requested and the handler will be called
1320 * when it arrives. The returned packet is ready to have any
1321 * request-specific data added and be sent. Note that if a handler is
1322 * provided, it's essential that the request actually be sent.
1324 * The handler will usually be passed the response packet in pktin. If
1325 * pktin is NULL, this means that no reply will ever be forthcoming
1326 * (e.g. because the entire connection is being destroyed, or because
1327 * the server initiated channel closure before we saw the response)
1328 * and the handler should free any storage it's holding.
1330 PktOut
*ssh2_chanreq_init(struct ssh2_channel
*c
, const char *type
,
1331 cr_handler_fn_t handler
, void *ctx
)
1333 struct ssh2_connection_state
*s
= c
->connlayer
;
1336 assert(!(c
->closes
& (CLOSES_SENT_CLOSE
| CLOSES_RCVD_CLOSE
)));
1337 pktout
= ssh_bpp_new_pktout(s
->ppl
.bpp
, SSH2_MSG_CHANNEL_REQUEST
);
1338 put_uint32(pktout
, c
->remoteid
);
1339 put_stringz(pktout
, type
);
1340 put_bool(pktout
, handler
!= NULL
);
1341 if (handler
!= NULL
) {
1342 struct outstanding_channel_request
*ocr
=
1343 snew(struct outstanding_channel_request
);
1345 ocr
->handler
= handler
;
1348 if (!c
->chanreq_head
)
1349 c
->chanreq_head
= ocr
;
1351 c
->chanreq_tail
->next
= ocr
;
1352 c
->chanreq_tail
= ocr
;
1357 static Conf
*ssh2channel_get_conf(SshChannel
*sc
)
1359 struct ssh2_channel
*c
= container_of(sc
, struct ssh2_channel
, sc
);
1360 struct ssh2_connection_state
*s
= c
->connlayer
;
1364 static void ssh2channel_write_eof(SshChannel
*sc
)
1366 struct ssh2_channel
*c
= container_of(sc
, struct ssh2_channel
, sc
);
1368 if (c
->closes
& CLOSES_SENT_EOF
)
1371 c
->pending_eof
= true;
1372 ssh2_channel_try_eof(c
);
1375 static void ssh2channel_initiate_close(SshChannel
*sc
, const char *err
)
1377 struct ssh2_channel
*c
= container_of(sc
, struct ssh2_channel
, sc
);
1380 reason
= err
? dupprintf("due to local error: %s", err
) : NULL
;
1381 ssh2_channel_close_local(c
, reason
);
1383 c
->pending_eof
= false; /* this will confuse a zombie channel */
1385 ssh2_channel_check_close(c
);
1388 static void ssh2channel_unthrottle(SshChannel
*sc
, size_t bufsize
)
1390 struct ssh2_channel
*c
= container_of(sc
, struct ssh2_channel
, sc
);
1391 struct ssh2_connection_state
*s
= c
->connlayer
;
1394 buflimit
= s
->ssh_is_simple
? 0 : c
->locmaxwin
;
1395 if (bufsize
< buflimit
)
1396 ssh2_set_window(c
, buflimit
- bufsize
);
1398 if (c
->throttling_conn
&& bufsize
<= buflimit
) {
1399 c
->throttling_conn
= false;
1400 ssh_throttle_conn(s
->ppl
.ssh
, -1);
1404 static size_t ssh2channel_write(
1405 SshChannel
*sc
, bool is_stderr
, const void *buf
, size_t len
)
1407 struct ssh2_channel
*c
= container_of(sc
, struct ssh2_channel
, sc
);
1408 assert(!(c
->closes
& CLOSES_SENT_EOF
));
1409 bufchain_add(is_stderr
? &c
->errbuffer
: &c
->outbuffer
, buf
, len
);
1410 return ssh2_try_send(c
);
1413 static void ssh2channel_x11_sharing_handover(
1414 SshChannel
*sc
, ssh_sharing_connstate
*share_cs
, share_channel
*share_chan
,
1415 const char *peer_addr
, int peer_port
, int endian
,
1416 int protomajor
, int protominor
, const void *initial_data
, int initial_len
)
1418 struct ssh2_channel
*c
= container_of(sc
, struct ssh2_channel
, sc
);
1420 * This function is called when we've just discovered that an X
1421 * forwarding channel on which we'd been handling the initial auth
1422 * ourselves turns out to be destined for a connection-sharing
1423 * downstream. So we turn the channel into a sharing one, meaning
1424 * that we completely stop tracking windows and buffering data and
1425 * just pass more or less unmodified SSH messages back and forth.
1427 c
->sharectx
= share_cs
;
1428 share_setup_x11_channel(share_cs
, share_chan
,
1429 c
->localid
, c
->remoteid
, c
->remwindow
,
1430 c
->remmaxpkt
, c
->locwindow
,
1431 peer_addr
, peer_port
, endian
,
1432 protomajor
, protominor
,
1433 initial_data
, initial_len
);
1438 static void ssh2channel_window_override_removed(SshChannel
*sc
)
1440 struct ssh2_channel
*c
= container_of(sc
, struct ssh2_channel
, sc
);
1441 struct ssh2_connection_state
*s
= c
->connlayer
;
1444 * This function is called when a client-side Channel has just
1445 * stopped requiring an initial fixed-size window.
1447 assert(!c
->chan
->initial_fixed_window_size
);
1448 ssh2_set_window(c
, s
->ssh_is_simple
? OUR_V2_BIGWIN
: OUR_V2_WINSIZE
);
1451 static void ssh2channel_hint_channel_is_simple(SshChannel
*sc
)
1453 struct ssh2_channel
*c
= container_of(sc
, struct ssh2_channel
, sc
);
1454 struct ssh2_connection_state
*s
= c
->connlayer
;
1456 PktOut
*pktout
= ssh2_chanreq_init(
1457 c
, "simple@putty.projects.tartarus.org", NULL
, NULL
);
1458 pq_push(s
->ppl
.out_pq
, pktout
);
1461 static SshChannel
*ssh2_lportfwd_open(
1462 ConnectionLayer
*cl
, const char *hostname
, int port
,
1463 const char *description
, const SocketPeerInfo
*pi
, Channel
*chan
)
1465 struct ssh2_connection_state
*s
=
1466 container_of(cl
, struct ssh2_connection_state
, cl
);
1467 struct ssh2_channel
*c
= snew(struct ssh2_channel
);
1471 ssh2_channel_init(c
);
1475 pktout
= ssh2_portfwd_chanopen(s
, c
, hostname
, port
, description
, pi
);
1476 pq_push(s
->ppl
.out_pq
, pktout
);
1481 static void ssh2_sharing_globreq_response(
1482 struct ssh2_connection_state
*s
, PktIn
*pktin
, void *ctx
)
1484 ssh_sharing_connstate
*cs
= (ssh_sharing_connstate
*)ctx
;
1485 share_got_pkt_from_server(cs
, pktin
->type
,
1486 BinarySource_UPCAST(pktin
)->data
,
1487 BinarySource_UPCAST(pktin
)->len
);
1490 static void ssh2_sharing_queue_global_request(
1491 ConnectionLayer
*cl
, ssh_sharing_connstate
*cs
)
1493 struct ssh2_connection_state
*s
=
1494 container_of(cl
, struct ssh2_connection_state
, cl
);
1495 ssh2_queue_global_request_handler(s
, ssh2_sharing_globreq_response
, cs
);
1498 static void ssh2_sharing_no_more_downstreams(ConnectionLayer
*cl
)
1500 struct ssh2_connection_state
*s
=
1501 container_of(cl
, struct ssh2_connection_state
, cl
);
1502 queue_toplevel_callback(ssh2_check_termination_callback
, s
);
1505 static struct X11FakeAuth
*ssh2_add_x11_display(
1506 ConnectionLayer
*cl
, int authtype
, struct X11Display
*disp
)
1508 struct ssh2_connection_state
*s
=
1509 container_of(cl
, struct ssh2_connection_state
, cl
);
1510 struct X11FakeAuth
*auth
= x11_invent_fake_auth(s
->x11authtree
, authtype
);
1515 static struct X11FakeAuth
*ssh2_add_sharing_x11_display(
1516 ConnectionLayer
*cl
, int authtype
, ssh_sharing_connstate
*share_cs
,
1517 share_channel
*share_chan
)
1519 struct ssh2_connection_state
*s
=
1520 container_of(cl
, struct ssh2_connection_state
, cl
);
1521 struct X11FakeAuth
*auth
;
1524 * Make up a new set of fake X11 auth data, and add it to the tree
1525 * of currently valid ones with an indication of the sharing
1526 * context that it's relevant to.
1528 auth
= x11_invent_fake_auth(s
->x11authtree
, authtype
);
1529 auth
->share_cs
= share_cs
;
1530 auth
->share_chan
= share_chan
;
1535 static void ssh2_remove_sharing_x11_display(
1536 ConnectionLayer
*cl
, struct X11FakeAuth
*auth
)
1538 struct ssh2_connection_state
*s
=
1539 container_of(cl
, struct ssh2_connection_state
, cl
);
1540 del234(s
->x11authtree
, auth
);
1541 x11_free_fake_auth(auth
);
1544 static unsigned ssh2_alloc_sharing_channel(
1545 ConnectionLayer
*cl
, ssh_sharing_connstate
*connstate
)
1547 struct ssh2_connection_state
*s
=
1548 container_of(cl
, struct ssh2_connection_state
, cl
);
1549 struct ssh2_channel
*c
= snew(struct ssh2_channel
);
1552 ssh2_channel_init(c
);
1554 c
->sharectx
= connstate
;
1558 static void ssh2_delete_sharing_channel(ConnectionLayer
*cl
, unsigned localid
)
1560 struct ssh2_connection_state
*s
=
1561 container_of(cl
, struct ssh2_connection_state
, cl
);
1562 struct ssh2_channel
*c
= find234(s
->channels
, &localid
, ssh2_channelfind
);
1564 ssh2_channel_destroy(c
);
1567 static void ssh2_send_packet_from_downstream(
1568 ConnectionLayer
*cl
, unsigned id
, int type
,
1569 const void *data
, int datalen
, const char *additional_log_text
)
1571 struct ssh2_connection_state
*s
=
1572 container_of(cl
, struct ssh2_connection_state
, cl
);
1573 PktOut
*pkt
= ssh_bpp_new_pktout(s
->ppl
.bpp
, type
);
1574 pkt
->downstream_id
= id
;
1575 pkt
->additional_log_text
= additional_log_text
;
1576 put_data(pkt
, data
, datalen
);
1577 pq_push(s
->ppl
.out_pq
, pkt
);
1580 static bool ssh2_agent_forwarding_permitted(ConnectionLayer
*cl
)
1582 struct ssh2_connection_state
*s
=
1583 container_of(cl
, struct ssh2_connection_state
, cl
);
1584 return conf_get_bool(s
->conf
, CONF_agentfwd
) && agent_exists();
1587 static bool ssh2_connection_get_specials(
1588 PacketProtocolLayer
*ppl
, add_special_fn_t add_special
, void *ctx
)
1590 struct ssh2_connection_state
*s
=
1591 container_of(ppl
, struct ssh2_connection_state
, ppl
);
1595 mainchan_get_specials(s
->mainchan
, add_special
, ctx
);
1600 * Don't bother offering IGNORE if we've decided the remote
1601 * won't cope with it, since we wouldn't bother sending it if
1604 if (!(s
->ppl
.remote_bugs
& BUG_CHOKES_ON_SSH2_IGNORE
)) {
1606 add_special(ctx
, NULL
, SS_SEP
, 0);
1608 add_special(ctx
, "IGNORE message", SS_NOP
, 0);
1615 static void ssh2_connection_special_cmd(PacketProtocolLayer
*ppl
,
1616 SessionSpecialCode code
, int arg
)
1618 struct ssh2_connection_state
*s
=
1619 container_of(ppl
, struct ssh2_connection_state
, ppl
);
1622 if (code
== SS_PING
|| code
== SS_NOP
) {
1623 if (!(s
->ppl
.remote_bugs
& BUG_CHOKES_ON_SSH2_IGNORE
)) {
1624 pktout
= ssh_bpp_new_pktout(s
->ppl
.bpp
, SSH2_MSG_IGNORE
);
1625 put_stringz(pktout
, "");
1626 pq_push(s
->ppl
.out_pq
, pktout
);
1628 } else if (s
->mainchan
) {
1629 mainchan_special_cmd(s
->mainchan
, code
, arg
);
1633 static void ssh2_terminal_size(ConnectionLayer
*cl
, int width
, int height
)
1635 struct ssh2_connection_state
*s
=
1636 container_of(cl
, struct ssh2_connection_state
, cl
);
1638 s
->term_width
= width
;
1639 s
->term_height
= height
;
1641 mainchan_terminal_size(s
->mainchan
, width
, height
);
1644 static void ssh2_stdout_unthrottle(ConnectionLayer
*cl
, size_t bufsize
)
1646 struct ssh2_connection_state
*s
=
1647 container_of(cl
, struct ssh2_connection_state
, cl
);
1650 sshfwd_unthrottle(s
->mainchan_sc
, bufsize
);
1653 static size_t ssh2_stdin_backlog(ConnectionLayer
*cl
)
1655 struct ssh2_connection_state
*s
=
1656 container_of(cl
, struct ssh2_connection_state
, cl
);
1657 struct ssh2_channel
*c
;
1661 c
= container_of(s
->mainchan_sc
, struct ssh2_channel
, sc
);
1662 return s
->mainchan
?
1663 bufchain_size(&c
->outbuffer
) + bufchain_size(&c
->errbuffer
) : 0;
1666 static void ssh2_throttle_all_channels(ConnectionLayer
*cl
, bool throttled
)
1668 struct ssh2_connection_state
*s
=
1669 container_of(cl
, struct ssh2_connection_state
, cl
);
1670 struct ssh2_channel
*c
;
1673 s
->all_channels_throttled
= throttled
;
1675 for (i
= 0; NULL
!= (c
= index234(s
->channels
, i
)); i
++)
1677 ssh2_channel_check_throttle(c
);
1680 static bool ssh2_ldisc_option(ConnectionLayer
*cl
, int option
)
1682 struct ssh2_connection_state
*s
=
1683 container_of(cl
, struct ssh2_connection_state
, cl
);
1685 return s
->ldisc_opts
[option
];
1688 static void ssh2_set_ldisc_option(ConnectionLayer
*cl
, int option
, bool value
)
1690 struct ssh2_connection_state
*s
=
1691 container_of(cl
, struct ssh2_connection_state
, cl
);
1693 s
->ldisc_opts
[option
] = value
;
1696 static void ssh2_enable_x_fwd(ConnectionLayer
*cl
)
1698 struct ssh2_connection_state
*s
=
1699 container_of(cl
, struct ssh2_connection_state
, cl
);
1701 s
->X11_fwd_enabled
= true;
1704 static void ssh2_set_wants_user_input(ConnectionLayer
*cl
, bool wanted
)
1706 struct ssh2_connection_state
*s
=
1707 container_of(cl
, struct ssh2_connection_state
, cl
);
1709 s
->want_user_input
= wanted
;
1711 ssh_check_sendok(s
->ppl
.ssh
);
1714 static bool ssh2_get_wants_user_input(ConnectionLayer
*cl
)
1716 struct ssh2_connection_state
*s
=
1717 container_of(cl
, struct ssh2_connection_state
, cl
);
1718 return s
->want_user_input
;
1721 static void ssh2_got_user_input(ConnectionLayer
*cl
)
1723 struct ssh2_connection_state
*s
=
1724 container_of(cl
, struct ssh2_connection_state
, cl
);
1726 while (s
->mainchan
&& bufchain_size(s
->user_input
) > 0) {
1728 * Add user input to the main channel's buffer.
1730 ptrlen data
= bufchain_prefix(s
->user_input
);
1731 sshfwd_write(s
->mainchan_sc
, data
.ptr
, data
.len
);
1732 bufchain_consume(s
->user_input
, data
.len
);
1736 static void ssh2_connection_reconfigure(PacketProtocolLayer
*ppl
, Conf
*conf
)
1738 struct ssh2_connection_state
*s
=
1739 container_of(ppl
, struct ssh2_connection_state
, ppl
);
1742 s
->conf
= conf_copy(conf
);
1744 if (s
->portfwdmgr_configured
)
1745 portfwdmgr_config(s
->portfwdmgr
, s
->conf
);