-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsshshare.c
2122 lines (1899 loc) · 76.1 KB
/
sshshare.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Support for SSH connection sharing, i.e. permitting one PuTTY to
* open its own channels over the SSH session being run by another.
*/
/*
* Discussion and technical documentation
* ======================================
*
* The basic strategy for PuTTY's implementation of SSH connection
* sharing is to have a single 'upstream' PuTTY process, which manages
* the real SSH connection and all the cryptography, and then zero or
* more 'downstream' PuTTYs, which never talk to the real host but
* only talk to the upstream through local IPC (Unix-domain sockets or
* Windows named pipes).
*
* The downstreams communicate with the upstream using a protocol
* derived from SSH itself, which I'll document in detail below. In
* brief, though: the downstream->upstream protocol uses a trivial
* binary packet protocol (just length/type/data) to encapsulate
* unencrypted SSH messages, and downstreams talk to the upstream more
* or less as if it was an SSH server itself. (So downstreams can
* themselves open multiple SSH channels, for example, by sending
* multiple SSH2_MSG_CHANNEL_OPENs; they can send CHANNEL_REQUESTs of
* their choice within each channel, and they handle their own
* WINDOW_ADJUST messages.)
*
* The upstream would ideally handle these downstreams by just putting
* their messages into the queue for proper SSH-2 encapsulation and
* encryption and sending them straight on to the server. However,
* that's not quite feasible as written, because client-side channel
* IDs could easily conflict (between multiple downstreams, or between
* a downstream and the upstream). To protect against that, the
* upstream rewrites the client-side channel IDs in messages it passes
* on to the server, so that it's performing what you might describe
* as 'channel-number NAT'. Then the upstream remembers which of its
* own channel IDs are channels it's managing itself, and which are
* placeholders associated with a particular downstream, so that when
* replies come in from the server they can be sent on to the relevant
* downstream (after un-NATting the channel number, of course).
*
* Global requests from downstreams are only accepted if the upstream
* knows what to do about them; currently the only such requests are
* the ones having to do with remote-to-local port forwarding (in
* which, again, the upstream remembers that some of the forwardings
* it's asked the server to set up were on behalf of particular
* downstreams, and sends the incoming CHANNEL_OPENs to those
* downstreams when connections come in).
*
* Other fiddly pieces of this mechanism are X forwarding and
* (OpenSSH-style) agent forwarding. Both of these have a fundamental
* problem arising from the protocol design: that the CHANNEL_OPEN
* from the server introducing a forwarded connection does not carry
* any indication of which session channel gave rise to it; so if
* session channels from multiple downstreams enable those forwarding
* methods, it's hard for the upstream to know which downstream to
* send the resulting connections back to.
*
* For X forwarding, we can work around this in a really painful way
* by using the fake X11 authorisation data sent to the server as part
* of the forwarding setup: upstream ensures that every X forwarding
* request carries distinguishable fake auth data, and then when X
* connections come in it waits to see the auth data in the X11 setup
* message before it decides which downstream to pass the connection
* on to.
*
* For agent forwarding, that workaround is unavailable. As a result,
* this system (and, as far as I can think of, any other system too)
* has the fundamental constraint that it can only forward one SSH
* agent - it can't forward two agents to different session channels.
* So downstreams can request agent forwarding if they like, but if
* they do, they'll get whatever SSH agent is known to the upstream
* (if any) forwarded to their sessions.
*
* Downstream-to-upstream protocol
* -------------------------------
*
* Here I document in detail the protocol spoken between PuTTY
* downstreams and upstreams over local IPC. The IPC mechanism can
* vary between host platforms, but the protocol is the same.
*
* The protocol commences with a version exchange which is exactly
* like the SSH-2 one, in that each side sends a single line of text
* of the form
*
* <protocol>-<version>-<softwareversion> [comments] \r\n
*
* The only difference is that in real SSH-2, <protocol> is the string
* "SSH", whereas in this protocol the string is
* "[email protected]".
*
* (The SSH RFCs allow many protocol-level identifier namespaces to be
* extended by implementors without central standardisation as long as
* they suffix "@" and a domain name they control to their new ids.
* RFC 4253 does not define this particular name to be changeable at
* all, but I like to think this is obviously how it would have done
* so if the working group had foreseen the need :-)
*
* Thereafter, all data exchanged consists of a sequence of binary
* packets concatenated end-to-end, each of which is of the form
*
* uint32 length of packet, N
* byte[N] N bytes of packet data
*
* and, since these are SSH-2 messages, the first data byte is taken
* to be the packet type code.
*
* These messages are interpreted as those of an SSH connection, after
* userauth completes, and without any repeat key exchange.
* Specifically, any message from the SSH Connection Protocol is
* permitted, and also SSH_MSG_IGNORE, SSH_MSG_DEBUG,
* SSH_MSG_DISCONNECT and SSH_MSG_UNIMPLEMENTED from the SSH Transport
* Protocol.
*
* This protocol imposes a few additional requirements, over and above
* those of the standard SSH Connection Protocol:
*
* Message sizes are not permitted to exceed 0x4010 (16400) bytes,
* including their length header.
*
* When the server (i.e. really the PuTTY upstream) sends
* SSH_MSG_CHANNEL_OPEN with channel type "x11", and the client
* (downstream) responds with SSH_MSG_CHANNEL_OPEN_CONFIRMATION, that
* confirmation message MUST include an initial window size of at
* least 256. (Rationale: this is a bit of a fudge which makes it
* easier, by eliminating the possibility of nasty edge cases, for an
* upstream to arrange not to pass the CHANNEL_OPEN on to downstream
* until after it's seen the X11 auth data to decide which downstream
* it needs to go to.)
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include "putty.h"
#include "tree234.h"
#include "ssh.h"
struct ssh_sharing_state {
const struct plug_function_table *fn;
/* the above variable absolutely *must* be the first in this structure */
char *sockname; /* the socket name, kept for cleanup */
Socket listensock; /* the master listening Socket */
tree234 *connections; /* holds ssh_sharing_connstates */
unsigned nextid; /* preferred id for next connstate */
Ssh ssh; /* instance of the ssh backend */
char *server_verstring; /* server version string after "SSH-" */
};
struct share_globreq;
struct ssh_sharing_connstate {
const struct plug_function_table *fn;
/* the above variable absolutely *must* be the first in this structure */
unsigned id; /* used to identify this downstream in log messages */
Socket sock; /* the Socket for this connection */
struct ssh_sharing_state *parent;
int crLine; /* coroutine state for share_receive */
int sent_verstring, got_verstring, curr_packetlen;
unsigned char recvbuf[0x4010];
int recvlen;
/*
* Assorted state we have to remember about this downstream, so
* that we can clean it up appropriately when the downstream goes
* away.
*/
/* Channels which don't have a downstream id, i.e. we've passed a
* CHANNEL_OPEN down from the server but not had an
* OPEN_CONFIRMATION or OPEN_FAILURE back. If downstream goes
* away, we respond to all of these with OPEN_FAILURE. */
tree234 *halfchannels; /* stores 'struct share_halfchannel' */
/* Channels which do have a downstream id. We need to index these
* by both server id and upstream id, so we can find a channel
* when handling either an upward or a downward message referring
* to it. */
tree234 *channels_by_us; /* stores 'struct share_channel' */
tree234 *channels_by_server; /* stores 'struct share_channel' */
/* Another class of channel which doesn't have a downstream id.
* The difference between these and halfchannels is that xchannels
* do have an *upstream* id, because upstream has already accepted
* the channel request from the server. This arises in the case of
* X forwarding, where we have to accept the request and read the
* X authorisation data before we know whether the channel needs
* to be forwarded to a downstream. */
tree234 *xchannels_by_us; /* stores 'struct share_xchannel' */
tree234 *xchannels_by_server; /* stores 'struct share_xchannel' */
/* Remote port forwarding requests in force. */
tree234 *forwardings; /* stores 'struct share_forwarding' */
/* Global requests we've sent on to the server, pending replies. */
struct share_globreq *globreq_head, *globreq_tail;
};
struct share_halfchannel {
unsigned server_id;
};
/* States of a share_channel. */
enum {
OPEN,
SENT_CLOSE,
RCVD_CLOSE,
/* Downstream has sent CHANNEL_OPEN but server hasn't replied yet.
* If downstream goes away when a channel is in this state, we
* must wait for the server's response before starting to send
* CLOSE. Channels in this state are also not held in
* channels_by_server, because their server_id field is
* meaningless. */
UNACKNOWLEDGED
};
struct share_channel {
unsigned downstream_id, upstream_id, server_id;
int downstream_maxpkt;
int state;
/*
* Some channels (specifically, channels on which downstream has
* sent "x11-req") have the additional function of storing a set
* of downstream X authorisation data and a handle to an upstream
* fake set.
*/
struct X11FakeAuth *x11_auth_upstream;
int x11_auth_proto;
char *x11_auth_data;
int x11_auth_datalen;
int x11_one_shot;
};
struct share_forwarding {
char *host;
int port;
int active; /* has the server sent REQUEST_SUCCESS? */
};
struct share_xchannel_message {
struct share_xchannel_message *next;
int type;
unsigned char *data;
int datalen;
};
struct share_xchannel {
unsigned upstream_id, server_id;
/*
* xchannels come in two flavours: live and dead. Live ones are
* waiting for an OPEN_CONFIRMATION or OPEN_FAILURE from
* downstream; dead ones have had an OPEN_FAILURE, so they only
* exist as a means of letting us conveniently respond to further
* channel messages from the server until such time as the server
* sends us CHANNEL_CLOSE.
*/
int live;
/*
* When we receive OPEN_CONFIRMATION, we will need to send a
* WINDOW_ADJUST to the server to synchronise the windows. For
* this purpose we need to know what window we have so far offered
* the server. We record this as exactly the value in the
* OPEN_CONFIRMATION that upstream sent us, adjusted by the amount
* by which the two X greetings differed in length.
*/
int window;
/*
* Linked list of SSH messages from the server relating to this
* channel, which we queue up until downstream sends us an
* OPEN_CONFIRMATION and we can belatedly send them all on.
*/
struct share_xchannel_message *msghead, *msgtail;
};
enum {
GLOBREQ_TCPIP_FORWARD,
GLOBREQ_CANCEL_TCPIP_FORWARD
};
struct share_globreq {
struct share_globreq *next;
int type;
int want_reply;
struct share_forwarding *fwd;
};
static int share_connstate_cmp(void *av, void *bv)
{
const struct ssh_sharing_connstate *a =
(const struct ssh_sharing_connstate *)av;
const struct ssh_sharing_connstate *b =
(const struct ssh_sharing_connstate *)bv;
if (a->id < b->id)
return -1;
else if (a->id > b->id)
return +1;
else
return 0;
}
static unsigned share_find_unused_id
(struct ssh_sharing_state *sharestate, unsigned first)
{
int low_orig, low, mid, high, high_orig;
struct ssh_sharing_connstate *cs;
unsigned ret;
/*
* Find the lowest unused downstream ID greater or equal to
* 'first'.
*
* Begin by seeing if 'first' itself is available. If it is, we'll
* just return it; if it's already in the tree, we'll find the
* tree index where it appears and use that for the next stage.
*/
{
struct ssh_sharing_connstate dummy;
dummy.id = first;
cs = findrelpos234(sharestate->connections, &dummy, NULL,
REL234_GE, &low_orig);
if (!cs)
return first;
}
/*
* Now binary-search using the counted B-tree, to find the largest
* ID which is in a contiguous sequence from the beginning of that
* range.
*/
low = low_orig;
high = high_orig = count234(sharestate->connections);
while (high - low > 1) {
mid = (high + low) / 2;
cs = index234(sharestate->connections, mid);
if (cs->id == first + (mid - low_orig))
low = mid; /* this one is still in the sequence */
else
high = mid; /* this one is past the end */
}
/*
* Now low is the tree index of the largest ID in the initial
* sequence. So the return value is one more than low's id, and we
* know low's id is given by the formula in the binary search loop
* above.
*
* (If an SSH connection went on for _enormously_ long, we might
* reach a point where all ids from 'first' to UINT_MAX were in
* use. In that situation the formula below would wrap round by
* one and return zero, which is conveniently the right way to
* signal 'no id available' from this function.)
*/
ret = first + (low - low_orig) + 1;
{
struct ssh_sharing_connstate dummy;
dummy.id = ret;
assert(NULL == find234(sharestate->connections, &dummy, NULL));
}
return ret;
}
static int share_halfchannel_cmp(void *av, void *bv)
{
const struct share_halfchannel *a = (const struct share_halfchannel *)av;
const struct share_halfchannel *b = (const struct share_halfchannel *)bv;
if (a->server_id < b->server_id)
return -1;
else if (a->server_id > b->server_id)
return +1;
else
return 0;
}
static int share_channel_us_cmp(void *av, void *bv)
{
const struct share_channel *a = (const struct share_channel *)av;
const struct share_channel *b = (const struct share_channel *)bv;
if (a->upstream_id < b->upstream_id)
return -1;
else if (a->upstream_id > b->upstream_id)
return +1;
else
return 0;
}
static int share_channel_server_cmp(void *av, void *bv)
{
const struct share_channel *a = (const struct share_channel *)av;
const struct share_channel *b = (const struct share_channel *)bv;
if (a->server_id < b->server_id)
return -1;
else if (a->server_id > b->server_id)
return +1;
else
return 0;
}
static int share_xchannel_us_cmp(void *av, void *bv)
{
const struct share_xchannel *a = (const struct share_xchannel *)av;
const struct share_xchannel *b = (const struct share_xchannel *)bv;
if (a->upstream_id < b->upstream_id)
return -1;
else if (a->upstream_id > b->upstream_id)
return +1;
else
return 0;
}
static int share_xchannel_server_cmp(void *av, void *bv)
{
const struct share_xchannel *a = (const struct share_xchannel *)av;
const struct share_xchannel *b = (const struct share_xchannel *)bv;
if (a->server_id < b->server_id)
return -1;
else if (a->server_id > b->server_id)
return +1;
else
return 0;
}
static int share_forwarding_cmp(void *av, void *bv)
{
const struct share_forwarding *a = (const struct share_forwarding *)av;
const struct share_forwarding *b = (const struct share_forwarding *)bv;
int i;
if ((i = strcmp(a->host, b->host)) != 0)
return i;
else if (a->port < b->port)
return -1;
else if (a->port > b->port)
return +1;
else
return 0;
}
static void share_xchannel_free(struct share_xchannel *xc)
{
while (xc->msghead) {
struct share_xchannel_message *tmp = xc->msghead;
xc->msghead = tmp->next;
sfree(tmp);
}
sfree(xc);
}
static void share_connstate_free(struct ssh_sharing_connstate *cs)
{
struct share_halfchannel *hc;
struct share_xchannel *xc;
struct share_channel *chan;
struct share_forwarding *fwd;
while ((hc = (struct share_halfchannel *)
delpos234(cs->halfchannels, 0)) != NULL)
sfree(hc);
freetree234(cs->halfchannels);
/* All channels live in 'channels_by_us' but only some in
* 'channels_by_server', so we use the former to find the list of
* ones to free */
freetree234(cs->channels_by_server);
while ((chan = (struct share_channel *)
delpos234(cs->channels_by_us, 0)) != NULL)
sfree(chan);
freetree234(cs->channels_by_us);
/* But every xchannel is in both trees, so it doesn't matter which
* we use to free them. */
while ((xc = (struct share_xchannel *)
delpos234(cs->xchannels_by_us, 0)) != NULL)
share_xchannel_free(xc);
freetree234(cs->xchannels_by_us);
freetree234(cs->xchannels_by_server);
while ((fwd = (struct share_forwarding *)
delpos234(cs->forwardings, 0)) != NULL)
sfree(fwd);
freetree234(cs->forwardings);
while (cs->globreq_head) {
struct share_globreq *globreq = cs->globreq_head;
cs->globreq_head = cs->globreq_head->next;
sfree(globreq);
}
sfree(cs);
}
void sharestate_free(void *v)
{
struct ssh_sharing_state *sharestate = (struct ssh_sharing_state *)v;
struct ssh_sharing_connstate *cs;
platform_ssh_share_cleanup(sharestate->sockname);
while ((cs = (struct ssh_sharing_connstate *)
delpos234(sharestate->connections, 0)) != NULL) {
share_connstate_free(cs);
}
freetree234(sharestate->connections);
if (sharestate->listensock) {
sk_close(sharestate->listensock);
sharestate->listensock = NULL;
}
sfree(sharestate->server_verstring);
sfree(sharestate->sockname);
sfree(sharestate);
}
static struct share_halfchannel *share_add_halfchannel
(struct ssh_sharing_connstate *cs, unsigned server_id)
{
struct share_halfchannel *hc = snew(struct share_halfchannel);
hc->server_id = server_id;
if (add234(cs->halfchannels, hc) != hc) {
/* Duplicate?! */
sfree(hc);
return NULL;
} else {
return hc;
}
}
static struct share_halfchannel *share_find_halfchannel
(struct ssh_sharing_connstate *cs, unsigned server_id)
{
struct share_halfchannel dummyhc;
dummyhc.server_id = server_id;
return find234(cs->halfchannels, &dummyhc, NULL);
}
static void share_remove_halfchannel(struct ssh_sharing_connstate *cs,
struct share_halfchannel *hc)
{
del234(cs->halfchannels, hc);
sfree(hc);
}
static struct share_channel *share_add_channel
(struct ssh_sharing_connstate *cs, unsigned downstream_id,
unsigned upstream_id, unsigned server_id, int state, int maxpkt)
{
struct share_channel *chan = snew(struct share_channel);
chan->downstream_id = downstream_id;
chan->upstream_id = upstream_id;
chan->server_id = server_id;
chan->state = state;
chan->downstream_maxpkt = maxpkt;
chan->x11_auth_upstream = NULL;
chan->x11_auth_data = NULL;
chan->x11_auth_proto = -1;
chan->x11_auth_datalen = 0;
chan->x11_one_shot = 0;
if (add234(cs->channels_by_us, chan) != chan) {
sfree(chan);
return NULL;
}
if (chan->state != UNACKNOWLEDGED) {
if (add234(cs->channels_by_server, chan) != chan) {
del234(cs->channels_by_us, chan);
sfree(chan);
return NULL;
}
}
return chan;
}
static void share_channel_set_server_id(struct ssh_sharing_connstate *cs,
struct share_channel *chan,
unsigned server_id, int newstate)
{
chan->server_id = server_id;
chan->state = newstate;
assert(newstate != UNACKNOWLEDGED);
add234(cs->channels_by_server, chan);
}
static struct share_channel *share_find_channel_by_upstream
(struct ssh_sharing_connstate *cs, unsigned upstream_id)
{
struct share_channel dummychan;
dummychan.upstream_id = upstream_id;
return find234(cs->channels_by_us, &dummychan, NULL);
}
static struct share_channel *share_find_channel_by_server
(struct ssh_sharing_connstate *cs, unsigned server_id)
{
struct share_channel dummychan;
dummychan.server_id = server_id;
return find234(cs->channels_by_server, &dummychan, NULL);
}
static void share_remove_channel(struct ssh_sharing_connstate *cs,
struct share_channel *chan)
{
del234(cs->channels_by_us, chan);
del234(cs->channels_by_server, chan);
if (chan->x11_auth_upstream)
ssh_sharing_remove_x11_display(cs->parent->ssh,
chan->x11_auth_upstream);
sfree(chan->x11_auth_data);
sfree(chan);
}
static struct share_xchannel *share_add_xchannel
(struct ssh_sharing_connstate *cs,
unsigned upstream_id, unsigned server_id)
{
struct share_xchannel *xc = snew(struct share_xchannel);
xc->upstream_id = upstream_id;
xc->server_id = server_id;
xc->live = TRUE;
xc->msghead = xc->msgtail = NULL;
if (add234(cs->xchannels_by_us, xc) != xc) {
sfree(xc);
return NULL;
}
if (add234(cs->xchannels_by_server, xc) != xc) {
del234(cs->xchannels_by_us, xc);
sfree(xc);
return NULL;
}
return xc;
}
static struct share_xchannel *share_find_xchannel_by_upstream
(struct ssh_sharing_connstate *cs, unsigned upstream_id)
{
struct share_xchannel dummyxc;
dummyxc.upstream_id = upstream_id;
return find234(cs->xchannels_by_us, &dummyxc, NULL);
}
static struct share_xchannel *share_find_xchannel_by_server
(struct ssh_sharing_connstate *cs, unsigned server_id)
{
struct share_xchannel dummyxc;
dummyxc.server_id = server_id;
return find234(cs->xchannels_by_server, &dummyxc, NULL);
}
static void share_remove_xchannel(struct ssh_sharing_connstate *cs,
struct share_xchannel *xc)
{
del234(cs->xchannels_by_us, xc);
del234(cs->xchannels_by_server, xc);
share_xchannel_free(xc);
}
static struct share_forwarding *share_add_forwarding
(struct ssh_sharing_connstate *cs,
const char *host, int port)
{
struct share_forwarding *fwd = snew(struct share_forwarding);
fwd->host = dupstr(host);
fwd->port = port;
fwd->active = FALSE;
if (add234(cs->forwardings, fwd) != fwd) {
/* Duplicate?! */
sfree(fwd);
return NULL;
}
return fwd;
}
static struct share_forwarding *share_find_forwarding
(struct ssh_sharing_connstate *cs, const char *host, int port)
{
struct share_forwarding dummyfwd, *ret;
dummyfwd.host = dupstr(host);
dummyfwd.port = port;
ret = find234(cs->forwardings, &dummyfwd, NULL);
sfree(dummyfwd.host);
return ret;
}
static void share_remove_forwarding(struct ssh_sharing_connstate *cs,
struct share_forwarding *fwd)
{
del234(cs->forwardings, fwd);
sfree(fwd);
}
static void send_packet_to_downstream(struct ssh_sharing_connstate *cs,
int type, const void *pkt, int pktlen,
struct share_channel *chan)
{
if (!cs->sock) /* throw away all packets destined for a dead downstream */
return;
if (type == SSH2_MSG_CHANNEL_DATA) {
/*
* Special case which we take care of at a low level, so as to
* be sure to apply it in all cases. On rare occasions we
* might find that we have a channel for which the
* downstream's maximum packet size exceeds the max packet
* size we presented to the server on its behalf. (This can
* occur in X11 forwarding, where we have to send _our_
* CHANNEL_OPEN_CONFIRMATION before we discover which if any
* downstream the channel is destined for, so if that
* downstream turns out to present a smaller max packet size
* then we're in this situation.)
*
* If that happens, we just chop up the packet into pieces and
* send them as separate CHANNEL_DATA packets.
*/
const char *upkt = (const char *)pkt;
char header[13]; /* 4 length + 1 type + 4 channel id + 4 string len */
int len = toint(GET_32BIT(upkt + 4));
upkt += 8; /* skip channel id + length field */
if (len < 0 || len > pktlen - 8)
len = pktlen - 8;
do {
int this_len = (len > chan->downstream_maxpkt ?
chan->downstream_maxpkt : len);
PUT_32BIT(header, this_len + 9);
header[4] = type;
PUT_32BIT(header + 5, chan->downstream_id);
PUT_32BIT(header + 9, this_len);
sk_write(cs->sock, header, 13);
sk_write(cs->sock, upkt, this_len);
len -= this_len;
upkt += this_len;
} while (len > 0);
} else {
/*
* Just do the obvious thing.
*/
char header[9];
PUT_32BIT(header, pktlen + 1);
header[4] = type;
sk_write(cs->sock, header, 5);
sk_write(cs->sock, pkt, pktlen);
}
}
static void share_try_cleanup(struct ssh_sharing_connstate *cs)
{
int i;
struct share_halfchannel *hc;
struct share_channel *chan;
struct share_forwarding *fwd;
/*
* Any half-open channels, i.e. those for which we'd received
* CHANNEL_OPEN from the server but not passed back a response
* from downstream, should be responded to with OPEN_FAILURE.
*/
while ((hc = (struct share_halfchannel *)
index234(cs->halfchannels, 0)) != NULL) {
static const char reason[] = "PuTTY downstream no longer available";
static const char lang[] = "en";
unsigned char packet[256];
int pos = 0;
PUT_32BIT(packet + pos, hc->server_id); pos += 4;
PUT_32BIT(packet + pos, SSH2_OPEN_CONNECT_FAILED); pos += 4;
PUT_32BIT(packet + pos, strlen(reason)); pos += 4;
memcpy(packet + pos, reason, strlen(reason)); pos += strlen(reason);
PUT_32BIT(packet + pos, strlen(lang)); pos += 4;
memcpy(packet + pos, lang, strlen(lang)); pos += strlen(lang);
ssh_send_packet_from_downstream(cs->parent->ssh, cs->id,
SSH2_MSG_CHANNEL_OPEN_FAILURE,
packet, pos, "cleanup after"
" downstream went away");
share_remove_halfchannel(cs, hc);
}
/*
* Any actually open channels should have a CHANNEL_CLOSE sent for
* them, unless we've already done so. We won't be able to
* actually clean them up until CHANNEL_CLOSE comes back from the
* server, though (unless the server happens to have sent a CLOSE
* already).
*
* Another annoying exception is UNACKNOWLEDGED channels, i.e.
* we've _sent_ a CHANNEL_OPEN to the server but not received an
* OPEN_CONFIRMATION or OPEN_FAILURE. We must wait for a reply
* before closing the channel, because until we see that reply we
* won't have the server's channel id to put in the close message.
*/
for (i = 0; (chan = (struct share_channel *)
index234(cs->channels_by_us, i)) != NULL; i++) {
unsigned char packet[256];
int pos = 0;
if (chan->state != SENT_CLOSE && chan->state != UNACKNOWLEDGED) {
PUT_32BIT(packet + pos, chan->server_id); pos += 4;
ssh_send_packet_from_downstream(cs->parent->ssh, cs->id,
SSH2_MSG_CHANNEL_CLOSE,
packet, pos, "cleanup after"
" downstream went away");
if (chan->state != RCVD_CLOSE) {
chan->state = SENT_CLOSE;
} else {
/* In this case, we _can_ clear up the channel now. */
ssh_delete_sharing_channel(cs->parent->ssh, chan->upstream_id);
share_remove_channel(cs, chan);
i--; /* don't accidentally skip one as a result */
}
}
}
/*
* Any remote port forwardings we're managing on behalf of this
* downstream should be cancelled. Again, we must defer those for
* which we haven't yet seen REQUEST_SUCCESS/FAILURE.
*
* We take a fire-and-forget approach during cleanup, not
* bothering to set want_reply.
*/
for (i = 0; (fwd = (struct share_forwarding *)
index234(cs->forwardings, i)) != NULL; i++) {
if (fwd->active) {
static const char request[] = "cancel-tcpip-forward";
char *packet = snewn(256 + strlen(fwd->host), char);
int pos = 0;
PUT_32BIT(packet + pos, strlen(request)); pos += 4;
memcpy(packet + pos, request, strlen(request));
pos += strlen(request);
packet[pos++] = 0; /* !want_reply */
PUT_32BIT(packet + pos, strlen(fwd->host)); pos += 4;
memcpy(packet + pos, fwd->host, strlen(fwd->host));
pos += strlen(fwd->host);
PUT_32BIT(packet + pos, fwd->port); pos += 4;
ssh_send_packet_from_downstream(cs->parent->ssh, cs->id,
SSH2_MSG_GLOBAL_REQUEST,
packet, pos, "cleanup after"
" downstream went away");
sfree(packet);
share_remove_forwarding(cs, fwd);
i--; /* don't accidentally skip one as a result */
}
}
if (count234(cs->halfchannels) == 0 &&
count234(cs->channels_by_us) == 0 &&
count234(cs->forwardings) == 0) {
/*
* Now we're _really_ done, so we can get rid of cs completely.
*/
del234(cs->parent->connections, cs);
ssh_sharing_downstream_disconnected(cs->parent->ssh, cs->id);
share_connstate_free(cs);
}
}
static void share_begin_cleanup(struct ssh_sharing_connstate *cs)
{
sk_close(cs->sock);
cs->sock = NULL;
share_try_cleanup(cs);
}
static void share_disconnect(struct ssh_sharing_connstate *cs,
const char *message)
{
static const char lang[] = "en";
int msglen = strlen(message);
char *packet = snewn(msglen + 256, char);
int pos = 0;
PUT_32BIT(packet + pos, SSH2_DISCONNECT_PROTOCOL_ERROR); pos += 4;
PUT_32BIT(packet + pos, msglen); pos += 4;
memcpy(packet + pos, message, msglen);
pos += msglen;
PUT_32BIT(packet + pos, strlen(lang)); pos += 4;
memcpy(packet + pos, lang, strlen(lang)); pos += strlen(lang);
send_packet_to_downstream(cs, SSH2_MSG_DISCONNECT, packet, pos, NULL);
share_begin_cleanup(cs);
}
static int share_closing(Plug plug, const char *error_msg, int error_code,
int calling_back)
{
struct ssh_sharing_connstate *cs = (struct ssh_sharing_connstate *)plug;
if (error_msg)
ssh_sharing_logf(cs->parent->ssh, cs->id, "%s", error_msg);
share_begin_cleanup(cs);
return 1;
}
static int getstring_inner(const void *vdata, int datalen,
char **out, int *outlen)
{
const unsigned char *data = (const unsigned char *)vdata;
int len;
if (datalen < 4)
return FALSE;
len = toint(GET_32BIT(data));
if (len < 0 || len > datalen - 4)
return FALSE;
if (outlen)
*outlen = len + 4; /* total size including length field */
if (out)
*out = dupprintf("%.*s", len, (char *)data + 4);
return TRUE;
}
static char *getstring(const void *data, int datalen)
{
char *ret;
if (getstring_inner(data, datalen, &ret, NULL))
return ret;
else
return NULL;
}
static int getstring_size(const void *data, int datalen)
{
int ret;
if (getstring_inner(data, datalen, NULL, &ret))
return ret;
else
return -1;
}
/*
* Append a message to the end of an xchannel's queue, with the length
* and type code filled in and the data block allocated but
* uninitialised.
*/
struct share_xchannel_message *share_xchannel_add_message
(struct share_xchannel *xc, int type, int len)
{
unsigned char *block;
struct share_xchannel_message *msg;
/*
* Be a little tricksy here by allocating a single memory block
* containing both the 'struct share_xchannel_message' and the
* actual data. Simplifies freeing it later.
*/
block = smalloc(sizeof(struct share_xchannel_message) + len);
msg = (struct share_xchannel_message *)block;
msg->data = block + sizeof(struct share_xchannel_message);
msg->datalen = len;
msg->type = type;
/*
* Queue it in the xchannel.
*/
if (xc->msgtail)
xc->msgtail->next = msg;
else
xc->msghead = msg;
msg->next = NULL;
xc->msgtail = msg;
return msg;
}
void share_dead_xchannel_respond(struct ssh_sharing_connstate *cs,
struct share_xchannel *xc)
{
/*
* Handle queued incoming messages from the server destined for an
* xchannel which is dead (i.e. downstream sent OPEN_FAILURE).
*/
int delete = FALSE;