-
Notifications
You must be signed in to change notification settings - Fork 0
/
xrdp_rdp.c
1693 lines (1543 loc) · 54.6 KB
/
xrdp_rdp.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
/**
* xrdp: A Remote Desktop Protocol server.
*
* Copyright (C) Jay Sorg 2004-2014
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* rdp layer
*/
#if defined(HAVE_CONFIG_H)
#include <config_ac.h>
#endif
#include "libxrdp.h"
#include "ms-rdpbcgr.h"
#include "log.h"
#include "ssl_calls.h"
#include "string_calls.h"
#if defined(XRDP_NEUTRINORDP)
#include <freerdp/codec/rfx.h>
#include <freerdp/constants.h>
#endif
#define FASTPATH_FRAG_SIZE (16 * 1024 - 128)
/*****************************************************************************/
static int
xrdp_rdp_read_config(const char *xrdp_ini, struct xrdp_client_info *client_info)
{
int index = 0;
struct list *items = (struct list *)NULL;
struct list *values = (struct list *)NULL;
char *item = NULL;
char *value = NULL;
int pos;
char *tmp = NULL;
int tmp_length = 0;
client_info->xrdp_keyboard_overrides.type = -1;
client_info->xrdp_keyboard_overrides.subtype = -1;
client_info->xrdp_keyboard_overrides.layout = -1;
/* initialize (zero out) local variables: */
items = list_create();
items->auto_free = 1;
values = list_create();
values->auto_free = 1;
LOG_DEVEL(LOG_LEVEL_TRACE, "Reading config file %s", xrdp_ini);
file_by_name_read_section(xrdp_ini, "globals", items, values);
for (index = 0; index < items->count; index++)
{
item = (char *)list_get_item(items, index);
value = (char *)list_get_item(values, index);
LOG(LOG_LEVEL_DEBUG, "item %s, value %s", item, value);
if (g_strcasecmp(item, "bitmap_cache") == 0)
{
client_info->use_bitmap_cache = g_text2bool(value);
}
else if (g_strcasecmp(item, "bitmap_compression") == 0)
{
client_info->use_bitmap_comp = g_text2bool(value);
}
else if (g_strcasecmp(item, "bulk_compression") == 0)
{
client_info->use_bulk_comp = g_text2bool(value);
}
else if (g_strcasecmp(item, "crypt_level") == 0)
{
if (g_strcasecmp(value, "none") == 0)
{
client_info->crypt_level = 0;
}
else if (g_strcasecmp(value, "low") == 0)
{
client_info->crypt_level = 1;
}
else if (g_strcasecmp(value, "medium") == 0)
{
client_info->crypt_level = 2;
}
else if (g_strcasecmp(value, "high") == 0)
{
client_info->crypt_level = 3;
}
else if (g_strcasecmp(value, "fips") == 0)
{
client_info->crypt_level = 4;
}
else
{
LOG(LOG_LEVEL_WARNING, "Your configured crypt level is "
"undefined, 'high' will be used");
client_info->crypt_level = 3;
}
}
else if (g_strcasecmp(item, "allow_channels") == 0)
{
client_info->channels_allowed = g_text2bool(value);
if (client_info->channels_allowed == 0)
{
LOG(LOG_LEVEL_INFO, "All channels are disabled");
}
}
else if (g_strcasecmp(item, "allow_multimon") == 0)
{
client_info->multimon = g_text2bool(value);
if (client_info->multimon == 0)
{
LOG(LOG_LEVEL_INFO, "Multi monitor server support disabled");
}
}
else if (g_strcasecmp(item, "max_bpp") == 0)
{
client_info->max_bpp = g_atoi(value);
}
else if (g_strcasecmp(item, "rfx_min_pixel") == 0)
{
client_info->rfx_min_pixel = g_atoi(value);
}
else if (g_strcasecmp(item, "new_cursors") == 0)
{
client_info->pointer_flags = g_text2bool(value) == 0 ? 2 : 0;
}
else if (g_strcasecmp(item, "require_credentials") == 0)
{
client_info->require_credentials = g_text2bool(value);
}
else if (g_strcasecmp(item, "enable_token_login") == 0)
{
client_info->enable_token_login = g_text2bool(value);
}
else if (g_strcasecmp(item, "use_fastpath") == 0)
{
if (g_strcasecmp(value, "output") == 0)
{
client_info->use_fast_path = 1;
}
else if (g_strcasecmp(value, "input") == 0)
{
client_info->use_fast_path = 2;
}
else if (g_strcasecmp(value, "both") == 0)
{
client_info->use_fast_path = 3;
}
else if (g_strcasecmp(value, "none") == 0)
{
client_info->use_fast_path = 0;
}
else
{
LOG(LOG_LEVEL_WARNING, "Your configured fastpath level is "
"undefined, fastpath will not be used");
client_info->use_fast_path = 0;
}
}
else if (g_strcasecmp(item, "ssl_protocols") == 0)
{
/* put leading/trailing comma to properly detect "TLSv1" without regex */
tmp_length = g_strlen(value) + 3;
tmp = g_new(char, tmp_length);
g_snprintf(tmp, tmp_length, "%s%s%s", ",", value, ",");
/* replace all spaces with comma */
/* to accept space after comma */
while ((pos = g_pos(tmp, " ")) != -1)
{
tmp[pos] = ',';
}
ssl_get_protocols_from_string(tmp, &(client_info->ssl_protocols));
g_free(tmp);
}
else if (g_strcasecmp(item, "tls_ciphers") == 0)
{
client_info->tls_ciphers = g_strdup(value);
}
else if (g_strcasecmp(item, "security_layer") == 0)
{
if (g_strcasecmp(value, "rdp") == 0)
{
client_info->security_layer = PROTOCOL_RDP;
}
else if (g_strcasecmp(value, "tls") == 0)
{
client_info->security_layer = PROTOCOL_SSL;
}
else if (g_strcasecmp(value, "hybrid") == 0)
{
client_info->security_layer = PROTOCOL_SSL | PROTOCOL_HYBRID;
}
else if (g_strcasecmp(value, "negotiate") == 0)
{
client_info->security_layer = PROTOCOL_SSL | PROTOCOL_HYBRID | PROTOCOL_HYBRID_EX;
}
else
{
LOG(LOG_LEVEL_WARNING, "security_layer=%s is not "
"recognized, will use security_layer=negotiate",
value);
client_info->security_layer = PROTOCOL_SSL | PROTOCOL_HYBRID | PROTOCOL_HYBRID_EX;
}
}
else if (g_strcasecmp(item, "certificate") == 0)
{
g_memset(client_info->certificate, 0, sizeof(char) * 1024);
if (g_strlen(value) == 0)
{
/* default certificate path */
g_snprintf(client_info->certificate, 1023, "%s/cert.pem", XRDP_CFG_PATH);
LOG(LOG_LEVEL_INFO,
"Using default X.509 certificate: %s",
client_info->certificate);
}
else if (value[0] != '/')
{
/* default certificate path */
g_snprintf(client_info->certificate, 1023, "%s/cert.pem", XRDP_CFG_PATH);
LOG(LOG_LEVEL_WARNING,
"X.509 certificate should use absolute path, using "
"default instead: %s", client_info->certificate);
}
else
{
/* use user defined certificate */
g_strncpy(client_info->certificate, value, 1023);
}
if (!g_file_readable(client_info->certificate))
{
LOG(LOG_LEVEL_ERROR, "Cannot read certificate file %s: %s",
client_info->certificate, g_get_strerror());
}
}
else if (g_strcasecmp(item, "key_file") == 0)
{
g_memset(client_info->key_file, 0, sizeof(char) * 1024);
if (g_strlen(value) == 0)
{
/* default key_file path */
g_snprintf(client_info->key_file, 1023, "%s/key.pem", XRDP_CFG_PATH);
LOG(LOG_LEVEL_INFO, "Using default X.509 key file: %s",
client_info->key_file);
}
else if (value[0] != '/')
{
/* default key_file path */
g_snprintf(client_info->key_file, 1023, "%s/key.pem", XRDP_CFG_PATH);
LOG(LOG_LEVEL_WARNING,
"X.509 key file should use absolute path, using "
"default instead: %s", client_info->key_file);
}
else
{
/* use user defined key_file */
g_strncpy(client_info->key_file, value, 1023);
}
if (!g_file_readable(client_info->key_file))
{
LOG(LOG_LEVEL_ERROR, "Cannot read private key file %s: %s",
client_info->key_file, g_get_strerror());
}
}
else if (g_strcasecmp(item, "domain_user_separator") == 0
&& g_strlen(value) > 0)
{
g_strncpy(client_info->domain_user_separator, value, sizeof(client_info->domain_user_separator) - 1);
}
else if (g_strcasecmp(item, "xrdp.override_keyboard_type") == 0)
{
client_info->xrdp_keyboard_overrides.type = g_atoix(value);
}
else if (g_strcasecmp(item, "xrdp.override_keyboard_subtype") == 0)
{
client_info->xrdp_keyboard_overrides.subtype = g_atoix(value);
}
else if (g_strcasecmp(item, "xrdp.override_keylayout") == 0)
{
client_info->xrdp_keyboard_overrides.layout = g_atoix(value);
}
}
list_delete(items);
list_delete(values);
return 0;
}
#if defined(XRDP_NEUTRINORDP)
/*****************************************************************************/
static void
cpuid(tui32 info, tui32 *eax, tui32 *ebx, tui32 *ecx, tui32 *edx)
{
#ifdef __GNUC__
#if defined(__i386__) || defined(__x86_64__)
__asm volatile
(
/* The EBX (or RBX register on x86_64) is used for the PIC base address
and must not be corrupted by our inline assembly. */
#if defined(__i386__)
"mov %%ebx, %%esi;"
"cpuid;"
"xchg %%ebx, %%esi;"
#else
"mov %%rbx, %%rsi;"
"cpuid;"
"xchg %%rbx, %%rsi;"
#endif
: "=a" (*eax), "=S" (*ebx), "=c" (*ecx), "=d" (*edx)
: "0" (info)
);
#endif
#endif
}
/*****************************************************************************/
static tui32
xrdp_rdp_detect_cpu(void)
{
tui32 eax;
tui32 ebx;
tui32 ecx;
tui32 edx;
tui32 cpu_opt;
eax = 0;
ebx = 0;
ecx = 0;
edx = 0;
cpu_opt = 0;
cpuid(1, &eax, &ebx, &ecx, &edx);
if (edx & (1 << 26))
{
LOG_DEVEL(LOG_LEVEL_TRACE, "SSE2 detected");
cpu_opt |= CPU_SSE2;
}
return cpu_opt;
}
#endif
/*****************************************************************************/
struct xrdp_rdp *
xrdp_rdp_create(struct xrdp_session *session, struct trans *trans)
{
struct xrdp_rdp *self = (struct xrdp_rdp *)NULL;
LOG_DEVEL(LOG_LEVEL_TRACE, "in xrdp_rdp_create");
self = (struct xrdp_rdp *)g_malloc(sizeof(struct xrdp_rdp), 1);
self->session = session;
self->share_id = 66538;
/* read ini settings */
xrdp_rdp_read_config(session->xrdp_ini, &self->client_info);
/* create sec layer */
self->sec_layer = xrdp_sec_create(self, trans);
/* default 8 bit v1 color bitmap cache entries and size */
self->client_info.cache1_entries = 600;
self->client_info.cache1_size = 256;
self->client_info.cache2_entries = 300;
self->client_info.cache2_size = 1024;
self->client_info.cache3_entries = 262;
self->client_info.cache3_size = 4096;
/* load client ip info */
g_sck_get_peer_ip_address(trans->sck,
self->client_info.client_ip,
sizeof(self->client_info.client_ip),
NULL);
g_sck_get_peer_description(trans->sck,
self->client_info.client_description,
sizeof(self->client_info.client_description));
self->mppc_enc = mppc_enc_new(PROTO_RDP_50);
#if defined(XRDP_NEUTRINORDP)
self->rfx_enc = rfx_context_new();
rfx_context_set_cpu_opt(self->rfx_enc, xrdp_rdp_detect_cpu());
#endif
self->client_info.size = sizeof(self->client_info);
self->client_info.version = CLIENT_INFO_CURRENT_VERSION;
LOG_DEVEL(LOG_LEVEL_TRACE, "out xrdp_rdp_create");
return self;
}
/*****************************************************************************/
void
xrdp_rdp_delete(struct xrdp_rdp *self)
{
if (self == 0)
{
return;
}
xrdp_sec_delete(self->sec_layer);
mppc_enc_free(self->mppc_enc);
#if defined(XRDP_NEUTRINORDP)
rfx_context_free((RFX_CONTEXT *)(self->rfx_enc));
#endif
g_free(self->client_info.tls_ciphers);
g_free(self);
}
/*****************************************************************************/
/* Initialize the stream for sending a [MS-RDPBCGR] Control PDU */
int
xrdp_rdp_init(struct xrdp_rdp *self, struct stream *s)
{
if (xrdp_sec_init(self->sec_layer, s) != 0)
{
LOG(LOG_LEVEL_ERROR, "xrdp_rdp_init: xrdp_sec_init failed");
return 1;
}
s_push_layer(s, rdp_hdr, 6); /* 6 = sizeof(TS_SHARECONTROLHEADER) */
return 0;
}
/*****************************************************************************/
/* Initialize the stream for sending a [MS-RDPBCGR] Data PDU */
int
xrdp_rdp_init_data(struct xrdp_rdp *self, struct stream *s)
{
if (xrdp_sec_init(self->sec_layer, s) != 0)
{
LOG(LOG_LEVEL_ERROR, "xrdp_rdp_init_data: xrdp_sec_init failed");
return 1;
}
s_push_layer(s, rdp_hdr, 18); /* 18 = sizeof(TS_SHAREDATAHEADER) */
return 0;
}
/*****************************************************************************/
/*
Receives and parses pdu code from next data unit.
@param self
@param (in/out) s: the stream to read from. Upon return the stream is ?
@param (out) code: the pdu code from the packet
returns error
*/
int
xrdp_rdp_recv(struct xrdp_rdp *self, struct stream *s, int *code)
{
int error = 0;
int len = 0;
int pdu_code = 0;
int chan = 0;
const tui8 *header;
if (s->next_packet == 0 || s->next_packet >= s->end)
{
/* check for fastpath first */
header = (const tui8 *) (s->p);
if (header[0] != 0x3)
{
if (xrdp_sec_recv_fastpath(self->sec_layer, s) != 0)
{
LOG(LOG_LEVEL_ERROR, "xrdp_rdp_recv: xrdp_sec_recv_fastpath failed");
return 1;
}
/* next_packet gets set in xrdp_sec_recv_fastpath */
*code = 2; // special code for fastpath input
LOG_DEVEL(LOG_LEVEL_DEBUG, "xrdp_rdp_recv: out code 2 (fastpath)");
return 0;
}
/* not fastpath, do tpkt */
chan = 0;
error = xrdp_sec_recv(self->sec_layer, s, &chan);
if (error == -1) /* special code for send demand active */
{
s->next_packet = 0;
*code = -1;
LOG_DEVEL(LOG_LEVEL_DEBUG, "xrdp_rdp_recv: out code -1 (send demand active)");
return 0;
}
if (error != 0)
{
LOG(LOG_LEVEL_ERROR, "xrdp_rdp_recv: xrdp_sec_recv failed");
return 1;
}
if ((chan != MCS_GLOBAL_CHANNEL) && (chan > 0))
{
if (chan > MCS_GLOBAL_CHANNEL)
{
if (xrdp_channel_process(self->sec_layer->chan_layer, s, chan) != 0)
{
LOG(LOG_LEVEL_ERROR, "xrdp_rdp_recv: xrdp_channel_process failed");
}
}
else
{
if (chan != 1)
{
LOG_DEVEL(LOG_LEVEL_WARNING,
"xrdp_rdp_recv: Wrong channel Id to be handled "
"by xrdp_channel_process, channel id %d", chan);
}
}
s->next_packet = 0;
*code = 0;
LOG_DEVEL(LOG_LEVEL_DEBUG, "xrdp_rdp_recv: out code 0 (skip data) "
"data processed by channel id %d", chan);
return 0;
}
s->next_packet = s->p;
}
else
{
LOG_DEVEL(LOG_LEVEL_DEBUG, "xrdp_rdp_recv: stream not touched");
s->p = s->next_packet;
}
if (!s_check_rem_and_log(s, 6, "Parsing [MS-RDPBCGR] TS_SHARECONTROLHEADER"))
{
s->next_packet = 0;
*code = 0;
LOG(LOG_LEVEL_ERROR, "xrdp_rdp_recv: out code 0 (skip data) "
"bad RDP packet");
return 0;
}
else
{
in_uint16_le(s, len); /* totalLength */
in_uint16_le(s, pdu_code); /* pduType */
*code = pdu_code & 0xf;
in_uint8s(s, 2); /* pduSource */
s->next_packet += len;
LOG_DEVEL(LOG_LEVEL_TRACE, "Received header [MS-RDPBCGR] TS_SHARECONTROLHEADER "
"totalLength %d, pduType.type %s (%d), pduType.PDUVersion %d, "
"pduSource (ignored)", len, PDUTYPE_TO_STR(*code), *code,
((pdu_code & 0xfff0) >> 4));
return 0;
}
}
/*****************************************************************************/
/* Send a [MS-RDPBCGR] Control PDU with for the given pduType with the headers
added */
int
xrdp_rdp_send(struct xrdp_rdp *self, struct stream *s, int pdu_type)
{
int len = 0;
s_pop_layer(s, rdp_hdr);
len = s->end - s->p;
/* TS_SHARECONTROLHEADER */
out_uint16_le(s, len); /* totalLength */
out_uint16_le(s, 0x10 | pdu_type); /* pduType */
out_uint16_le(s, self->mcs_channel); /* pduSource */
LOG_DEVEL(LOG_LEVEL_TRACE, "Adding header [MS-RDPBCGR] TS_SHARECONTROLHEADER "
"totalLength %d, pduType.type %s (%d), pduType.PDUVersion %d, "
"pduSource %d", len, PDUTYPE_TO_STR(pdu_type & 0xf),
pdu_type & 0xf, (((0x10 | pdu_type) & 0xfff0) >> 4),
self->mcs_channel);
if (xrdp_sec_send(self->sec_layer, s, MCS_GLOBAL_CHANNEL) != 0)
{
LOG(LOG_LEVEL_ERROR, "xrdp_rdp_send: xrdp_sec_send failed");
return 1;
}
return 0;
}
/*****************************************************************************/
/* Send a [MS-RDPBCGR] Data PDU for the given pduType2 from
* the specified source with the headers
added and data compressed */
int
xrdp_rdp_send_data_from_channel(struct xrdp_rdp *self, struct stream *s,
int data_pdu_type, int channel_id,
int compress)
{
int len;
int ctype;
int clen;
int dlen;
int pdulen;
int pdutype;
int tocomplen;
int iso_offset;
int mcs_offset;
int sec_offset;
int rdp_offset;
struct stream ls;
struct xrdp_mppc_enc *mppc_enc;
s_pop_layer(s, rdp_hdr);
len = (int)(s->end - s->p);
pdutype = 0x10 | PDUTYPE_DATAPDU;
pdulen = len;
dlen = len;
ctype = 0;
clen = len;
tocomplen = pdulen - 18;
if (compress && self->client_info.rdp_compression &&
self->session->up_and_running)
{
mppc_enc = self->mppc_enc;
if (compress_rdp(mppc_enc, (tui8 *)(s->p + 18), tocomplen))
{
clen = mppc_enc->bytes_in_opb + 18;
pdulen = clen;
ctype = mppc_enc->flags;
iso_offset = (int)(s->iso_hdr - s->data);
mcs_offset = (int)(s->mcs_hdr - s->data);
sec_offset = (int)(s->sec_hdr - s->data);
rdp_offset = (int)(s->rdp_hdr - s->data);
/* outputBuffer has 64 bytes preceding it */
ls.data = mppc_enc->outputBuffer - (rdp_offset + 18);
ls.p = ls.data + rdp_offset;
ls.end = ls.p + clen;
ls.size = s->end - s->data;
ls.iso_hdr = ls.data + iso_offset;
ls.mcs_hdr = ls.data + mcs_offset;
ls.sec_hdr = ls.data + sec_offset;
ls.rdp_hdr = ls.data + rdp_offset;
ls.channel_hdr = 0;
ls.next_packet = 0;
s = &ls;
}
else
{
LOG_DEVEL(LOG_LEVEL_TRACE,
"xrdp_rdp_send_data_from_channel: "
"compress_rdp failed, sending "
"uncompressed data. type %d, flags %d",
mppc_enc->protocol_type, mppc_enc->flags);
}
}
/* TS_SHARECONTROLHEADER */
out_uint16_le(s, pdulen); /* totalLength */
out_uint16_le(s, pdutype); /* pduType */
out_uint16_le(s, channel_id); /* pduSource */
LOG_DEVEL(LOG_LEVEL_TRACE, "Adding header [MS-RDPBCGR] TS_SHARECONTROLHEADER "
"totalLength %d, pduType.type %s (%d), pduType.PDUVersion %d, "
"pduSource %d", pdulen, PDUTYPE_TO_STR(pdutype & 0xf),
pdutype & 0xf, ((pdutype & 0xfff0) >> 4), channel_id);
/* TS_SHAREDATAHEADER */
out_uint32_le(s, self->share_id);
out_uint8(s, 0); /* pad */
out_uint8(s, 1); /* streamID */
out_uint16_le(s, dlen); /* uncompressedLength */
out_uint8(s, data_pdu_type); /* pduType2 */
out_uint8(s, ctype); /* compressedType */
out_uint16_le(s, clen); /* compressedLength */
LOG_DEVEL(LOG_LEVEL_TRACE, "Adding header [MS-RDPBCGR] TS_SHAREDATAHEADER "
"shareID %d, streamID 1, uncompressedLength %d, "
"pduType2 0x%2.2x, compressedType 0x%2.2x, compressedLength %d",
self->share_id, dlen, data_pdu_type, ctype, clen);
if (xrdp_sec_send(self->sec_layer, s, MCS_GLOBAL_CHANNEL) != 0)
{
LOG(LOG_LEVEL_ERROR, "xrdp_rdp_send_data_from_channel: "
"xrdp_sec_send failed");
return 1;
}
return 0;
}
/*****************************************************************************/
/* Send a [MS-RDPBCGR] Data PDU on the MCS channel for the given pduType2
* with the headers added and data compressed */
int
xrdp_rdp_send_data(struct xrdp_rdp *self, struct stream *s,
int data_pdu_type)
{
return xrdp_rdp_send_data_from_channel(self, s, data_pdu_type,
self->mcs_channel, 1);
}
/*****************************************************************************/
/* returns the fastpath rdp byte count */
int
xrdp_rdp_get_fastpath_bytes(struct xrdp_rdp *self)
{
if (self->client_info.rdp_compression)
{
return 4;
}
return 3;
}
/*****************************************************************************/
int
xrdp_rdp_init_fastpath(struct xrdp_rdp *self, struct stream *s)
{
if (xrdp_sec_init_fastpath(self->sec_layer, s) != 0)
{
LOG(LOG_LEVEL_ERROR, "xrdp_rdp_init_fastpath: xrdp_sec_init_fastpath failed");
return 1;
}
if (self->client_info.rdp_compression)
{
s_push_layer(s, rdp_hdr, 4);
}
else
{
s_push_layer(s, rdp_hdr, 3);
}
return 0;
}
/*****************************************************************************/
/* returns error */
/* 2.2.9.1.2.1 Fast-Path Update (TS_FP_UPDATE)
* http://msdn.microsoft.com/en-us/library/cc240622.aspx */
int
xrdp_rdp_send_fastpath(struct xrdp_rdp *self, struct stream *s,
int data_pdu_type)
{
int updateHeader;
int updateCode;
int fragmentation;
int compression;
int comp_type;
int comp_len;
int no_comp_len;
int send_len;
int cont;
int header_bytes;
int sec_bytes;
int to_comp_len;
int sec_offset;
int rdp_offset;
struct stream frag_s;
struct stream comp_s;
struct stream send_s;
struct xrdp_mppc_enc *mppc_enc;
char comp_type_str[7];
comp_type_str[0] = '\0';
s_pop_layer(s, rdp_hdr);
updateCode = data_pdu_type;
if (self->client_info.rdp_compression)
{
compression = 2;
header_bytes = 4;
}
else
{
compression = 0;
header_bytes = 3;
}
sec_bytes = xrdp_sec_get_fastpath_bytes(self->sec_layer);
fragmentation = 0;
frag_s = *s;
sec_offset = (int)(frag_s.sec_hdr - frag_s.data);
rdp_offset = (int)(frag_s.rdp_hdr - frag_s.data);
cont = 1;
while (cont)
{
comp_type = 0;
send_s = frag_s;
no_comp_len = (int)(frag_s.end - frag_s.p);
if (no_comp_len > FASTPATH_FRAG_SIZE)
{
no_comp_len = FASTPATH_FRAG_SIZE;
if (fragmentation == 0)
{
fragmentation = 2; /* FASTPATH_FRAGMENT_FIRST */
}
else if (fragmentation == 2)
{
fragmentation = 3; /* FASTPATH_FRAGMENT_NEXT */
}
}
else
{
if (fragmentation != 0)
{
fragmentation = 1; /* FASTPATH_FRAGMENT_LAST */
}
}
send_len = no_comp_len;
LOG_DEVEL(LOG_LEVEL_DEBUG, "xrdp_rdp_send_fastpath: no_comp_len %d, fragmentation %d",
no_comp_len, fragmentation);
if ((compression != 0) && (no_comp_len > header_bytes + 16))
{
to_comp_len = no_comp_len - header_bytes;
mppc_enc = self->mppc_enc;
if (compress_rdp(mppc_enc, (tui8 *)(frag_s.p + header_bytes),
to_comp_len))
{
comp_len = mppc_enc->bytes_in_opb + header_bytes;
send_len = comp_len;
comp_type = mppc_enc->flags;
/* outputBuffer has 64 bytes preceding it */
g_memset(&comp_s, 0, sizeof(comp_s));
comp_s.data = mppc_enc->outputBuffer -
(rdp_offset + header_bytes);
comp_s.p = comp_s.data + rdp_offset;
comp_s.end = comp_s.p + send_len;
comp_s.size = send_len;
comp_s.sec_hdr = comp_s.data + sec_offset;
comp_s.rdp_hdr = comp_s.data + rdp_offset;
send_s = comp_s;
}
else
{
LOG(LOG_LEVEL_DEBUG,
"compress_rdp failed, sending uncompressed data. "
"type %d, flags %d", mppc_enc->protocol_type,
mppc_enc->flags);
}
}
updateHeader = (updateCode & 15) |
((fragmentation & 3) << 4) |
((compression & 3) << 6);
send_s.end = send_s.p + send_len;
send_s.size = send_s.end - send_s.data;
out_uint8(&send_s, updateHeader);
if (compression != 0)
{
out_uint8(&send_s, comp_type);
g_snprintf(comp_type_str, 7, "0x%4.4x", comp_type);
}
send_len -= header_bytes;
out_uint16_le(&send_s, send_len);
LOG_DEVEL(LOG_LEVEL_TRACE, "Adding header [MS-RDPBCGR] TS_FP_UPDATE "
"updateCode %d, fragmentation %d, compression %d, compressionFlags %s, size %d",
updateCode, fragmentation, compression,
(compression ? comp_type_str : "(not present)"), send_len);
if (xrdp_sec_send_fastpath(self->sec_layer, &send_s) != 0)
{
LOG(LOG_LEVEL_ERROR, "xrdp_rdp_send_fastpath: xrdp_sec_send_fastpath failed");
return 1;
}
frag_s.p += no_comp_len;
cont = frag_s.p < frag_s.end;
frag_s.p -= header_bytes;
frag_s.sec_hdr = frag_s.p - sec_bytes;
frag_s.data = frag_s.sec_hdr;
}
return 0;
}
/*****************************************************************************/
/* Send a [MS-RDPBCGR] TS_UPDATE_SYNC or TS_FP_UPDATE_SYNCHRONIZE message
depending on if the client supports the fast path capability or not */
int
xrdp_rdp_send_data_update_sync(struct xrdp_rdp *self)
{
struct stream *s = (struct stream *)NULL;
make_stream(s);
init_stream(s, 8192);
if (self->client_info.use_fast_path & 1) /* fastpath output supported */
{
if (xrdp_rdp_init_fastpath(self, s) != 0)
{
LOG(LOG_LEVEL_ERROR, "xrdp_rdp_send_data_update_sync: xrdp_rdp_init_fastpath failed");
free_stream(s);
return 1;
}
}
else /* slowpath */
{
if (xrdp_rdp_init_data(self, s) != 0)
{
LOG(LOG_LEVEL_ERROR, "xrdp_rdp_send_data_update_sync: xrdp_rdp_init_data failed");
free_stream(s);
return 1;
}
out_uint16_le(s, RDP_UPDATE_SYNCHRONIZE); /* updateType */
out_uint16_le(s, 0); /* pad */
}
s_mark_end(s);
if (self->client_info.use_fast_path & 1) /* fastpath output supported */
{
LOG_DEVEL(LOG_LEVEL_TRACE, "Sending [MS-RDPBCGR] TS_FP_UPDATE_SYNCHRONIZE");
if (xrdp_rdp_send_fastpath(self, s,
FASTPATH_UPDATETYPE_SYNCHRONIZE) != 0)
{
LOG(LOG_LEVEL_ERROR, "Sending [MS-RDPBCGR] TS_FP_UPDATE_SYNCHRONIZE failed");
free_stream(s);
return 1;
}
}
else /* slowpath */
{
LOG_DEVEL(LOG_LEVEL_TRACE, "Sending [MS-RDPBCGR] TS_UPDATE_SYNC "
"updateType %s (%d)",
GRAPHICS_UPDATE_TYPE_TO_STR(RDP_UPDATE_SYNCHRONIZE),
RDP_UPDATE_SYNCHRONIZE);
if (xrdp_rdp_send_data(self, s, RDP_DATA_PDU_UPDATE) != 0)
{
LOG(LOG_LEVEL_ERROR, "Sending [MS-RDPBCGR] TS_UPDATE_SYNC failed");
free_stream(s);
return 1;
}
}
free_stream(s);
return 0;
}
/*****************************************************************************/
int
xrdp_rdp_incoming(struct xrdp_rdp *self)
{
struct xrdp_iso *iso;
iso = self->sec_layer->mcs_layer->iso_layer;
if (xrdp_sec_incoming(self->sec_layer) != 0)
{
LOG(LOG_LEVEL_ERROR, "xrdp_rdp_incoming: xrdp_sec_incoming failed");
return 1;
}
self->mcs_channel = self->sec_layer->mcs_layer->userid +
MCS_USERCHANNEL_BASE;
LOG_DEVEL(LOG_LEVEL_DEBUG, "xrdp_rdp->mcs_channel %d", self->mcs_channel);
/* log TLS version and cipher of TLS connections */
if (iso->selectedProtocol > PROTOCOL_RDP)
{
LOG(LOG_LEVEL_INFO,
"TLS connection established from %s %s with cipher %s",
self->client_info.client_description,
iso->trans->ssl_protocol,
iso->trans->cipher_name);
}
/* log non-TLS connections */
else
{
int crypt_level = self->sec_layer->crypt_level;
const char *security_level =
(crypt_level == CRYPT_LEVEL_NONE) ? "none" :
(crypt_level == CRYPT_LEVEL_LOW) ? "low" :
(crypt_level == CRYPT_LEVEL_CLIENT_COMPATIBLE) ? "medium" :
(crypt_level == CRYPT_LEVEL_HIGH) ? "high" :
(crypt_level == CRYPT_LEVEL_FIPS) ? "fips" :
/* default */ "unknown";
LOG(LOG_LEVEL_INFO,
"Non-TLS connection established from %s with security level : %s",
self->client_info.client_description, security_level);
}
return 0;
}
/*****************************************************************************/
/* Process a [MS-RDPBCGR] TS_POINTER_PDU message */
static int
xrdp_rdp_process_data_pointer(struct xrdp_rdp *self, struct stream *s)
{
LOG_DEVEL(LOG_LEVEL_WARNING, "Protocol error ignored: a [MS-RDPBCGR] "
"TS_SHAREDATAHEADER PDUTYPE2_POINTER was received by the server "
"but this type of PDU is only suppose to be sent by the server "
"to the client.");
return 0;
}
/*****************************************************************************/
/* Process a [MS-RDPBCGR] TS_INPUT_PDU_DATA message */
static int
xrdp_rdp_process_data_input(struct xrdp_rdp *self, struct stream *s)
{
int num_events;
int index;
int msg_type;
int device_flags;
int param1;
int param2;
int time;