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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
|
/**
* xrdp: A Remote Desktop Protocol server.
*
* Copyright (C) Jay Sorg 2004-2012
*
* 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.
*
* librdp rdp layer
*/
#include "rdp.h"
#ifndef NULL
#define NULL 0
#endif
/*****************************************************************************/
struct rdp_rdp *APP_CC
rdp_rdp_create(struct mod *owner)
{
struct rdp_rdp *self;
self = (struct rdp_rdp *)g_malloc(sizeof(struct rdp_rdp), 1);
self->mod = owner;
self->sec_layer = rdp_sec_create(self);
self->bitmap_compression = 1;
self->bitmap_cache = 1;
self->desktop_save = 0;
self->orders = rdp_orders_create(self);
self->rec_mode = 0;
return self;
}
/*****************************************************************************/
void APP_CC
rdp_rdp_delete(struct rdp_rdp *self)
{
if (self == 0)
{
return;
}
rdp_orders_delete(self->orders);
rdp_sec_delete(self->sec_layer);
if (self->rec_fd != 0)
{
g_file_close(self->rec_fd);
self->rec_fd = 0;
}
g_free(self);
}
/******************************************************************************/
/* Initialise an RDP packet */
int APP_CC
rdp_rdp_init(struct rdp_rdp *self, struct stream *s)
{
if (rdp_sec_init(self->sec_layer, s, SEC_ENCRYPT) != 0)
{
return 1;
}
s_push_layer(s, rdp_hdr, 6);
return 0;
}
/******************************************************************************/
/* Send an RDP packet */
int APP_CC
rdp_rdp_send(struct rdp_rdp *self, struct stream *s, int pdu_type)
{
int len;
int sec_flags;
s_pop_layer(s, rdp_hdr);
len = s->end - s->p;
out_uint16_le(s, len);
out_uint16_le(s, pdu_type | 0x10);
out_uint16_le(s, self->sec_layer->mcs_layer->userid);
sec_flags = SEC_ENCRYPT;
if (rdp_sec_send(self->sec_layer, s, sec_flags) != 0)
{
return 1;
}
return 0;
}
/******************************************************************************/
/* Initialise an RDP data packet */
int APP_CC
rdp_rdp_init_data(struct rdp_rdp *self, struct stream *s)
{
if (rdp_sec_init(self->sec_layer, s, SEC_ENCRYPT) != 0)
{
return 1;
}
s_push_layer(s, rdp_hdr, 18);
return 0;
}
/******************************************************************************/
/* Send an RDP data packet */
int APP_CC
rdp_rdp_send_data(struct rdp_rdp *self, struct stream *s, int pdu_data_type)
{
int len;
int sec_flags;
s_pop_layer(s, rdp_hdr);
len = s->end - s->p;
out_uint16_le(s, len);
out_uint16_le(s, RDP_PDU_DATA | 0x10);
out_uint16_le(s, self->sec_layer->mcs_layer->userid);
out_uint32_le(s, self->share_id);
out_uint8(s, 0);
out_uint8(s, 1);
out_uint16_le(s, len - 14);
out_uint8(s, pdu_data_type);
out_uint8(s, 0); /* compress type */
out_uint16_le(s, 0); /* compress len */
sec_flags = SEC_ENCRYPT;
if (rdp_sec_send(self->sec_layer, s, sec_flags) != 0)
{
return 1;
}
return 0;
}
/******************************************************************************/
/* Output general capability set */
static int APP_CC
rdp_rdp_out_general_caps(struct rdp_rdp *self, struct stream *s)
{
out_uint16_le(s, RDP_CAPSET_GENERAL);
out_uint16_le(s, RDP_CAPLEN_GENERAL);
out_uint16_le(s, 1); /* OS major type */
out_uint16_le(s, 3); /* OS minor type */
out_uint16_le(s, 0x200); /* Protocol version */
out_uint16_le(s, 0); /* Pad */
out_uint16_le(s, 0); /* Compression types */
out_uint16_le(s, self->use_rdp5 ? 0x40d : 0);
out_uint16_le(s, 0); /* Update capability */
out_uint16_le(s, 0); /* Remote unshare capability */
out_uint16_le(s, 0); /* Compression level */
out_uint16_le(s, 0); /* Pad */
return 0;
}
/******************************************************************************/
/* Output bitmap capability set */
static int APP_CC
rdp_rdp_out_bitmap_caps(struct rdp_rdp *self, struct stream *s)
{
out_uint16_le(s, RDP_CAPSET_BITMAP);
out_uint16_le(s, RDP_CAPLEN_BITMAP);
out_uint16_le(s, self->mod->rdp_bpp); /* Preferred BPP */
out_uint16_le(s, 1); /* Receive 1 BPP */
out_uint16_le(s, 1); /* Receive 4 BPP */
out_uint16_le(s, 1); /* Receive 8 BPP */
out_uint16_le(s, 800); /* Desktop width */
out_uint16_le(s, 600); /* Desktop height */
out_uint16_le(s, 0); /* Pad */
out_uint16_le(s, 1); /* Allow resize */
out_uint16_le(s, self->bitmap_compression); /* Support compression */
out_uint16_le(s, 0); /* Unknown */
out_uint16_le(s, 1); /* Unknown */
out_uint16_le(s, 0); /* Pad */
return 0;
}
/******************************************************************************/
/* Output order capability set */
static int APP_CC
rdp_rdp_out_order_caps(struct rdp_rdp *self, struct stream *s)
{
char order_caps[32];
g_memset(order_caps, 0, 32);
order_caps[0] = 1; /* dest blt */
order_caps[1] = 1; /* pat blt */
order_caps[2] = 1; /* screen blt */
order_caps[3] = self->bitmap_cache; /* memblt */
order_caps[4] = 0; /* triblt */
order_caps[8] = 1; /* line */
order_caps[9] = 1; /* line */
order_caps[10] = 1; /* rect */
order_caps[11] = self->desktop_save; /* desksave */
order_caps[13] = 1; /* memblt another above */
order_caps[14] = 1; /* triblt another above */
order_caps[20] = self->polygon_ellipse_orders; /* polygon */
order_caps[21] = self->polygon_ellipse_orders; /* polygon2 */
order_caps[22] = 0; /* todo polyline */
order_caps[25] = self->polygon_ellipse_orders; /* ellipse */
order_caps[26] = self->polygon_ellipse_orders; /* ellipse2 */
order_caps[27] = 1; /* text2 */
out_uint16_le(s, RDP_CAPSET_ORDER);
out_uint16_le(s, RDP_CAPLEN_ORDER);
out_uint8s(s, 20); /* Terminal desc, pad */
out_uint16_le(s, 1); /* Cache X granularity */
out_uint16_le(s, 20); /* Cache Y granularity */
out_uint16_le(s, 0); /* Pad */
out_uint16_le(s, 1); /* Max order level */
out_uint16_le(s, 0x147); /* Number of fonts */
out_uint16_le(s, 0x2a); /* Capability flags */
out_uint8p(s, order_caps, 32); /* Orders supported */
out_uint16_le(s, 0x6a1); /* Text capability flags */
out_uint8s(s, 6); /* Pad */
out_uint32_le(s, self->desktop_save * 0x38400); /* Desktop cache size */
out_uint32_le(s, 0); /* Unknown */
out_uint32_le(s, 0x4e4); /* Unknown */
return 0;
}
/******************************************************************************/
/* Output bitmap cache capability set */
static int APP_CC
rdp_rdp_out_bmpcache_caps(struct rdp_rdp *self, struct stream *s)
{
int Bpp = 0;
out_uint16_le(s, RDP_CAPSET_BMPCACHE);
out_uint16_le(s, RDP_CAPLEN_BMPCACHE);
Bpp = (self->mod->rdp_bpp + 7) / 8;
out_uint8s(s, 24); /* unused */
out_uint16_le(s, 0x258); /* entries */
out_uint16_le(s, 0x100 * Bpp); /* max cell size */
out_uint16_le(s, 0x12c); /* entries */
out_uint16_le(s, 0x400 * Bpp); /* max cell size */
out_uint16_le(s, 0x106); /* entries */
out_uint16_le(s, 0x1000 * Bpp); /* max cell size */
return 0;
}
/******************************************************************************/
/* Output control capability set */
static int APP_CC
rdp_rdp_out_control_caps(struct rdp_rdp *self, struct stream *s)
{
out_uint16_le(s, RDP_CAPSET_CONTROL);
out_uint16_le(s, RDP_CAPLEN_CONTROL);
out_uint16_le(s, 0); /* Control capabilities */
out_uint16_le(s, 0); /* Remote detach */
out_uint16_le(s, 2); /* Control interest */
out_uint16_le(s, 2); /* Detach interest */
return 0;
}
/******************************************************************************/
/* Output activation capability set */
static int APP_CC
rdp_rdp_out_activate_caps(struct rdp_rdp *self, struct stream *s)
{
out_uint16_le(s, RDP_CAPSET_ACTIVATE);
out_uint16_le(s, RDP_CAPLEN_ACTIVATE);
out_uint16_le(s, 0); /* Help key */
out_uint16_le(s, 0); /* Help index key */
out_uint16_le(s, 0); /* Extended help key */
out_uint16_le(s, 0); /* Window activate */
return 0;
}
/******************************************************************************/
/* Output pointer capability set */
static int APP_CC
rdp_rdp_out_pointer_caps(struct rdp_rdp *self, struct stream *s)
{
out_uint16_le(s, RDP_CAPSET_POINTER);
out_uint16_le(s, RDP_CAPLEN_POINTER_MONO);
out_uint16_le(s, 0); /* Color pointer */
out_uint16_le(s, 20); /* Cache size */
return 0;
}
/******************************************************************************/
/* Output share capability set */
static int APP_CC
rdp_rdp_out_share_caps(struct rdp_rdp *self, struct stream *s)
{
out_uint16_le(s, RDP_CAPSET_SHARE);
out_uint16_le(s, RDP_CAPLEN_SHARE);
out_uint16_le(s, 0); /* userid */
out_uint16_le(s, 0); /* pad */
return 0;
}
/******************************************************************************/
/* Output color cache capability set */
static int APP_CC
rdp_rdp_out_colcache_caps(struct rdp_rdp *self, struct stream *s)
{
out_uint16_le(s, RDP_CAPSET_COLCACHE);
out_uint16_le(s, RDP_CAPLEN_COLCACHE);
out_uint16_le(s, 6); /* cache size */
out_uint16_le(s, 0); /* pad */
return 0;
}
static char caps_0x0d[] =
{
0x01, 0x00, 0x00, 0x00, 0x09, 0x04, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
static char caps_0x0c[] = { 0x01, 0x00, 0x00, 0x00 };
static char caps_0x0e[] = { 0x01, 0x00, 0x00, 0x00 };
static char caps_0x10[] =
{
0xFE, 0x00, 0x04, 0x00, 0xFE, 0x00, 0x04, 0x00,
0xFE, 0x00, 0x08, 0x00, 0xFE, 0x00, 0x08, 0x00,
0xFE, 0x00, 0x10, 0x00, 0xFE, 0x00, 0x20, 0x00,
0xFE, 0x00, 0x40, 0x00, 0xFE, 0x00, 0x80, 0x00,
0xFE, 0x00, 0x00, 0x01, 0x40, 0x00, 0x00, 0x08,
0x00, 0x01, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00
};
/******************************************************************************/
/* Output unknown capability sets */
static int APP_CC
rdp_rdp_out_unknown_caps(struct rdp_rdp *self, struct stream *s, int id,
int length, char *caps)
{
out_uint16_le(s, id);
out_uint16_le(s, length);
out_uint8p(s, caps, length - 4);
return 0;
}
#define RDP5_FLAG 0x0030
/******************************************************************************/
/* Send a confirm active PDU */
static int APP_CC
rdp_rdp_send_confirm_active(struct rdp_rdp *self, struct stream *s)
{
int sec_flags;
int caplen;
sec_flags = SEC_ENCRYPT;
//sec_flags = RDP5_FLAG | SEC_ENCRYPT;
caplen = RDP_CAPLEN_GENERAL + RDP_CAPLEN_BITMAP + RDP_CAPLEN_ORDER +
RDP_CAPLEN_BMPCACHE + RDP_CAPLEN_COLCACHE +
RDP_CAPLEN_ACTIVATE + RDP_CAPLEN_CONTROL +
RDP_CAPLEN_POINTER_MONO + RDP_CAPLEN_SHARE +
0x58 + 0x08 + 0x08 + 0x34 /* unknown caps */ +
4 /* w2k fix, why? */ ;
if (rdp_sec_init(self->sec_layer, s, sec_flags) != 0)
{
return 1;
}
out_uint16_le(s, 2 + 14 + caplen + sizeof(RDP_SOURCE));
out_uint16_le(s, (RDP_PDU_CONFIRM_ACTIVE | 0x10)); /* Version 1 */
out_uint16_le(s, (self->sec_layer->mcs_layer->userid + 1001));
out_uint32_le(s, self->share_id);
out_uint16_le(s, 0x3ea); /* userid */
out_uint16_le(s, sizeof(RDP_SOURCE));
out_uint16_le(s, caplen);
out_uint8p(s, RDP_SOURCE, sizeof(RDP_SOURCE));
out_uint16_le(s, 0xd); /* num_caps */
out_uint8s(s, 2); /* pad */
rdp_rdp_out_general_caps(self, s);
rdp_rdp_out_bitmap_caps(self, s);
rdp_rdp_out_order_caps(self, s);
rdp_rdp_out_bmpcache_caps(self, s);
rdp_rdp_out_colcache_caps(self, s);
rdp_rdp_out_activate_caps(self, s);
rdp_rdp_out_control_caps(self, s);
rdp_rdp_out_pointer_caps(self, s);
rdp_rdp_out_share_caps(self, s);
rdp_rdp_out_unknown_caps(self, s, 0x0d, 0x58, caps_0x0d); /* international? */
rdp_rdp_out_unknown_caps(self, s, 0x0c, 0x08, caps_0x0c);
rdp_rdp_out_unknown_caps(self, s, 0x0e, 0x08, caps_0x0e);
rdp_rdp_out_unknown_caps(self, s, 0x10, 0x34, caps_0x10); /* glyph cache? */
s_mark_end(s);
if (rdp_sec_send(self->sec_layer, s, sec_flags) != 0)
{
return 1;
}
return 0;
}
/******************************************************************************/
/* Process a color pointer PDU */
static int APP_CC
rdp_rdp_process_color_pointer_pdu(struct rdp_rdp *self, struct stream *s)
{
int cache_idx;
int dlen;
int mlen;
struct rdp_cursor *cursor;
in_uint16_le(s, cache_idx);
if (cache_idx >= sizeof(self->cursors) / sizeof(cursor))
{
return 1;
}
cursor = self->cursors + cache_idx;
in_uint16_le(s, cursor->x);
in_uint16_le(s, cursor->y);
in_uint16_le(s, cursor->width);
in_uint16_le(s, cursor->height);
in_uint16_le(s, mlen); /* mask length */
in_uint16_le(s, dlen); /* data length */
if ((mlen > sizeof(cursor->mask)) || (dlen > sizeof(cursor->data)))
{
return 1;
}
in_uint8a(s, cursor->data, dlen);
in_uint8a(s, cursor->mask, mlen);
self->mod->server_set_cursor(self->mod, cursor->x, cursor->y,
cursor->data, cursor->mask);
return 0;
}
/******************************************************************************/
/* Process a cached pointer PDU */
static int APP_CC
rdp_rdp_process_cached_pointer_pdu(struct rdp_rdp *self, struct stream *s)
{
int cache_idx;
struct rdp_cursor *cursor;
in_uint16_le(s, cache_idx);
if (cache_idx >= sizeof(self->cursors) / sizeof(cursor))
{
return 1;
}
cursor = self->cursors + cache_idx;
self->mod->server_set_cursor(self->mod, cursor->x, cursor->y,
cursor->data, cursor->mask);
return 0;
}
/******************************************************************************/
/* Process a system pointer PDU */
static int APP_CC
rdp_rdp_process_system_pointer_pdu(struct rdp_rdp *self, struct stream *s)
{
int system_pointer_type;
struct rdp_cursor *cursor;
in_uint16_le(s, system_pointer_type);
switch (system_pointer_type)
{
case RDP_NULL_POINTER:
cursor = (struct rdp_cursor *)g_malloc(sizeof(struct rdp_cursor), 1);
g_memset(cursor->mask, 0xff, sizeof(cursor->mask));
self->mod->server_set_cursor(self->mod, cursor->x, cursor->y,
cursor->data, cursor->mask);
g_free(cursor);
break;
default:
break;
}
return 0;
}
/******************************************************************************/
/* Process a pointer PDU */
static int APP_CC
rdp_rdp_process_pointer_pdu(struct rdp_rdp *self, struct stream *s)
{
int message_type;
int x;
int y;
int rv;
rv = 0;
in_uint16_le(s, message_type);
in_uint8s(s, 2); /* pad */
switch (message_type)
{
case RDP_POINTER_MOVE:
in_uint16_le(s, x);
in_uint16_le(s, y);
break;
case RDP_POINTER_COLOR:
rv = rdp_rdp_process_color_pointer_pdu(self, s);
break;
case RDP_POINTER_CACHED:
rv = rdp_rdp_process_cached_pointer_pdu(self, s);
break;
case RDP_POINTER_SYSTEM:
rv = rdp_rdp_process_system_pointer_pdu(self, s);
break;
default:
break;
}
return rv;
}
/******************************************************************************/
/* Process bitmap updates */
static void APP_CC
rdp_rdp_process_bitmap_updates(struct rdp_rdp *self, struct stream *s)
{
int num_updates = 0;
int left = 0;
int top = 0;
int right = 0;
int bottom = 0;
int width = 0;
int height = 0;
int cx = 0;
int cy = 0;
int bpp = 0;
int Bpp = 0;
int compress = 0;
int bufsize = 0;
int size = 0;
int i = 0;
int x = 0;
int y = 0;
char *data = NULL;
char *bmpdata0 = NULL;
char *bmpdata1 = NULL;
in_uint16_le(s, num_updates);
for (i = 0; i < num_updates; i++)
{
in_uint16_le(s, left);
in_uint16_le(s, top);
in_uint16_le(s, right);
in_uint16_le(s, bottom);
in_uint16_le(s, width);
in_uint16_le(s, height);
in_uint16_le(s, bpp);
Bpp = (bpp + 7) / 8;
in_uint16_le(s, compress);
in_uint16_le(s, bufsize);
cx = (right - left) + 1;
cy = (bottom - top) + 1;
bmpdata0 = (char *)g_malloc(width * height * Bpp, 0);
if (compress)
{
if (compress & 0x400)
{
size = bufsize;
}
else
{
in_uint8s(s, 2); /* pad */
in_uint16_le(s, size);
in_uint8s(s, 4); /* line_size, final_size */
}
in_uint8p(s, data, size);
rdp_bitmap_decompress(bmpdata0, width, height, data, size, Bpp);
bmpdata1 = rdp_orders_convert_bitmap(bpp, self->mod->xrdp_bpp,
bmpdata0, width, height,
self->colormap.colors);
self->mod->server_paint_rect(self->mod, left, top, cx, cy, bmpdata1,
width, height, 0, 0);
}
else /* not compressed */
{
for (y = 0; y < height; y++)
{
data = bmpdata0 + ((height - y) - 1) * (width * Bpp);
if (Bpp == 1)
{
for (x = 0; x < width; x++)
{
in_uint8(s, data[x]);
}
}
else if (Bpp == 2)
{
for (x = 0; x < width; x++)
{
in_uint16_le(s, ((tui16 *)data)[x]);
}
}
else if (Bpp == 3)
{
for (x = 0; x < width; x++)
{
in_uint8(s, data[x * 3 + 0]);
in_uint8(s, data[x * 3 + 1]);
in_uint8(s, data[x * 3 + 2]);
}
}
}
bmpdata1 = rdp_orders_convert_bitmap(bpp, self->mod->xrdp_bpp,
bmpdata0, width, height,
self->colormap.colors);
self->mod->server_paint_rect(self->mod, left, top, cx, cy, bmpdata1,
width, height, 0, 0);
}
if (bmpdata0 != bmpdata1)
{
g_free(bmpdata1);
}
g_free(bmpdata0);
}
}
/******************************************************************************/
/* Process a palette update */
static void APP_CC
rdp_rdp_process_palette(struct rdp_rdp *self, struct stream *s)
{
int i;
int r;
int g;
int b;
in_uint8s(s, 2); /* pad */
in_uint16_le(s, self->colormap.ncolors);
in_uint8s(s, 2); /* pad */
for (i = 0; i < self->colormap.ncolors; i++)
{
in_uint8(s, r);
in_uint8(s, g);
in_uint8(s, b);
self->colormap.colors[i] = (r << 16) | (g << 8) | b;
}
//ui_set_colormap(hmap);
}
/******************************************************************************/
/* Process an update PDU */
static int APP_CC
rdp_rdp_process_update_pdu(struct rdp_rdp *self, struct stream *s)
{
int update_type;
int count;
in_uint16_le(s, update_type);
self->mod->server_begin_update(self->mod);
switch (update_type)
{
case RDP_UPDATE_ORDERS:
in_uint8s(s, 2); /* pad */
in_uint16_le(s, count);
in_uint8s(s, 2); /* pad */
rdp_orders_process_orders(self->orders, s, count);
break;
case RDP_UPDATE_BITMAP:
rdp_rdp_process_bitmap_updates(self, s);
break;
case RDP_UPDATE_PALETTE:
rdp_rdp_process_palette(self, s);
break;
case RDP_UPDATE_SYNCHRONIZE:
break;
default:
break;
}
self->mod->server_end_update(self->mod);
return 0;
}
/******************************************************************************/
void APP_CC
rdp_rdp_out_unistr(struct stream *s, char *text)
{
int i;
i = 0;
while (text[i] != 0)
{
out_uint8(s, text[i]);
out_uint8(s, 0);
i++;
}
out_uint8(s, 0);
out_uint8(s, 0);
}
/******************************************************************************/
int APP_CC
rdp_rdp_send_login_info(struct rdp_rdp *self, int flags)
{
int len_domain;
int len_username;
int len_password;
int len_program;
int len_directory;
int sec_flags;
struct stream *s;
DEBUG(("in rdp_rdp_send_login_info"));
make_stream(s);
init_stream(s, 8192);
len_domain = 2 * g_strlen(self->mod->domain);
len_username = 2 * g_strlen(self->mod->username);
len_password = 2 * g_strlen(self->mod->password);
len_program = 2 * g_strlen(self->mod->program);
len_directory = 2 * g_strlen(self->mod->directory);
sec_flags = SEC_LOGON_INFO | SEC_ENCRYPT;
if (rdp_sec_init(self->sec_layer, s, sec_flags) != 0)
{
free_stream(s);
DEBUG(("out rdp_rdp_send_login_info error 1"));
return 1;
}
out_uint32_le(s, 0);
out_uint32_le(s, flags);
out_uint16_le(s, len_domain);
out_uint16_le(s, len_username);
out_uint16_le(s, len_password);
out_uint16_le(s, len_program);
out_uint16_le(s, len_directory);
rdp_rdp_out_unistr(s, self->mod->domain);
rdp_rdp_out_unistr(s, self->mod->username);
rdp_rdp_out_unistr(s, self->mod->password);
rdp_rdp_out_unistr(s, self->mod->program);
rdp_rdp_out_unistr(s, self->mod->directory);
s_mark_end(s);
if (rdp_sec_send(self->sec_layer, s, sec_flags) != 0)
{
free_stream(s);
DEBUG(("out rdp_rdp_send_login_info error 2"));
return 1;
}
free_stream(s);
DEBUG(("out rdp_rdp_send_login_info"));
return 0;
}
/******************************************************************************/
int APP_CC
rdp_rdp_connect(struct rdp_rdp *self, char *ip, char *port)
{
int flags;
DEBUG(("in rdp_rdp_connect"));
flags = RDP_LOGON_NORMAL;
if (g_strlen(self->mod->password) > 0)
{
flags |= RDP_LOGON_AUTO;
}
if (rdp_sec_connect(self->sec_layer, ip, port) != 0)
{
DEBUG(("out rdp_rdp_connect error rdp_sec_connect failed"));
return 1;
}
if (rdp_rdp_send_login_info(self, flags) != 0)
{
DEBUG(("out rdp_rdp_connect error rdp_rdp_send_login_info failed"));
return 1;
}
DEBUG(("out rdp_rdp_connect"));
return 0;
}
/******************************************************************************/
int APP_CC
rdp_rdp_send_input(struct rdp_rdp *self, struct stream *s,
int time, int message_type,
int device_flags, int param1, int param2)
{
if (rdp_rdp_init_data(self, s) != 0)
{
return 1;
}
out_uint16_le(s, 1); /* number of events */
out_uint16_le(s, 0);
out_uint32_le(s, time);
out_uint16_le(s, message_type);
out_uint16_le(s, device_flags);
out_uint16_le(s, param1);
out_uint16_le(s, param2);
s_mark_end(s);
if (rdp_rdp_send_data(self, s, RDP_DATA_PDU_INPUT) != 0)
{
return 1;
}
return 0;
}
/******************************************************************************/
int APP_CC
rdp_rdp_send_invalidate(struct rdp_rdp *self, struct stream *s,
int left, int top, int width, int height)
{
if (rdp_rdp_init_data(self, s) != 0)
{
return 1;
}
out_uint32_le(s, 1);
out_uint16_le(s, left);
out_uint16_le(s, top);
out_uint16_le(s, (left + width) - 1);
out_uint16_le(s, (top + height) - 1);
s_mark_end(s);
if (rdp_rdp_send_data(self, s, 33) != 0)
{
return 1;
}
return 0;
}
/******************************************************************************/
int APP_CC
rdp_rdp_recv(struct rdp_rdp *self, struct stream *s, int *type)
{
int len;
int pdu_type;
int chan;
chan = 0;
DEBUG(("in rdp_rdp_recv"));
if (s->next_packet >= s->end || s->next_packet == 0)
{
if (rdp_sec_recv(self->sec_layer, s, &chan) != 0)
{
DEBUG(("error in rdp_rdp_recv, rdp_sec_recv failed"));
return 1;
}
s->next_packet = s->p;
}
else
{
chan = MCS_GLOBAL_CHANNEL;
s->p = s->next_packet;
}
if (chan == MCS_GLOBAL_CHANNEL)
{
in_uint16_le(s, len);
DEBUG(("rdp_rdp_recv got %d len", len));
if (len == 0x8000)
{
s->next_packet += 8;
DEBUG(("out rdp_rdp_recv"));
return 0;
}
in_uint16_le(s, pdu_type);
in_uint8s(s, 2);
*type = pdu_type & 0xf;
s->next_packet += len;
}
else
{
/* todo, process channel data */
DEBUG(("got channel data channel %d", chan));
s->next_packet = s->end;
}
DEBUG(("out rdp_rdp_recv"));
return 0;
}
/******************************************************************************/
static int APP_CC
rdp_rdp_process_disconnect_pdu(struct rdp_rdp *self, struct stream *s)
{
return 0;
}
/******************************************************************************/
int APP_CC
rdp_rdp_process_data_pdu(struct rdp_rdp *self, struct stream *s)
{
int data_pdu_type;
int ctype;
int len;
int rv;
rv = 0;
in_uint8s(s, 6); /* shareid, pad, streamid */
in_uint16_le(s, len);
in_uint8(s, data_pdu_type);
in_uint8(s, ctype);
in_uint8s(s, 2); /* clen */
switch (data_pdu_type)
{
case RDP_DATA_PDU_UPDATE:
rv = rdp_rdp_process_update_pdu(self, s);
break;
case RDP_DATA_PDU_CONTROL:
break;
case RDP_DATA_PDU_SYNCHRONISE:
break;
case RDP_DATA_PDU_POINTER:
rv = rdp_rdp_process_pointer_pdu(self, s);
break;
case RDP_DATA_PDU_PLAY_SOUND:
break;
case RDP_DATA_PDU_LOGON:
break;
case RDP_DATA_PDU_DISCONNECT:
rv = rdp_rdp_process_disconnect_pdu(self, s);
break;
default:
break;
}
return rv;
}
/******************************************************************************/
/* Process a bitmap capability set */
static void APP_CC
rdp_rdp_process_general_caps(struct rdp_rdp *self, struct stream *s)
{
}
/******************************************************************************/
/* Process a bitmap capability set */
static void APP_CC
rdp_rdp_process_bitmap_caps(struct rdp_rdp *self, struct stream *s)
{
int width = 0;
int height = 0;
int bpp = 0;
in_uint16_le(s, bpp);
in_uint8s(s, 6);
in_uint16_le(s, width);
in_uint16_le(s, height);
self->mod->rdp_bpp = bpp;
/* todo, call reset if needed and use width and height */
}
/******************************************************************************/
/* Process server capabilities */
/* returns error */
static int APP_CC
rdp_rdp_process_server_caps(struct rdp_rdp *self, struct stream *s, int len)
{
int n = 0;
int ncapsets = 0;
int capset_type = 0;
int capset_length = 0;
char *next = NULL;
char *start = NULL;
start = s->p;
in_uint16_le(s, ncapsets);
in_uint8s(s, 2); /* pad */
for (n = 0; n < ncapsets; n++)
{
if (s->p > start + len)
{
return 0;
}
in_uint16_le(s, capset_type);
in_uint16_le(s, capset_length);
next = (s->p + capset_length) - 4;
switch (capset_type)
{
case RDP_CAPSET_GENERAL:
rdp_rdp_process_general_caps(self, s);
break;
case RDP_CAPSET_BITMAP:
rdp_rdp_process_bitmap_caps(self, s);
break;
default:
break;
}
s->p = next;
}
return 0;
}
/******************************************************************************/
/* Send a control PDU */
/* returns error */
static int APP_CC
rdp_rdp_send_control(struct rdp_rdp *self, struct stream *s, int action)
{
if (rdp_rdp_init_data(self, s) != 0)
{
return 1;
}
out_uint16_le(s, action);
out_uint16_le(s, 0); /* userid */
out_uint32_le(s, 0); /* control id */
s_mark_end(s);
if (rdp_rdp_send_data(self, s, RDP_DATA_PDU_CONTROL) != 0)
{
return 1;
}
return 0;
}
/******************************************************************************/
/* Send a synchronisation PDU */
/* returns error */
static int APP_CC
rdp_rdp_send_synchronise(struct rdp_rdp *self, struct stream *s)
{
if (rdp_rdp_init_data(self, s) != 0)
{
return 1;
}
out_uint16_le(s, 1); /* type */
out_uint16_le(s, 1002);
s_mark_end(s);
if (rdp_rdp_send_data(self, s, RDP_DATA_PDU_SYNCHRONISE) != 0)
{
return 1;
}
return 0;
}
/******************************************************************************/
/* Send an (empty) font information PDU */
static int APP_CC
rdp_rdp_send_fonts(struct rdp_rdp *self, struct stream *s, int seq)
{
if (rdp_rdp_init_data(self, s) != 0)
{
return 1;
}
out_uint16_le(s, 0); /* number of fonts */
out_uint16_le(s, 0); /* pad? */
out_uint16_le(s, seq); /* unknown */
out_uint16_le(s, 0x32); /* entry size */
s_mark_end(s);
if (rdp_rdp_send_data(self, s, RDP_DATA_PDU_FONT2) != 0)
{
return 1;
}
return 0;
}
/******************************************************************************/
/* Respond to a demand active PDU */
int APP_CC
rdp_rdp_process_demand_active(struct rdp_rdp *self, struct stream *s)
{
int type = 0;
int len_src_descriptor = 0;
int len_combined_caps = 0;
in_uint32_le(s, self->share_id);
in_uint16_le(s, len_src_descriptor);
in_uint16_le(s, len_combined_caps);
in_uint8s(s, len_src_descriptor);
rdp_rdp_process_server_caps(self, s, len_combined_caps);
rdp_rdp_send_confirm_active(self, s);
rdp_rdp_send_synchronise(self, s);
rdp_rdp_send_control(self, s, RDP_CTL_COOPERATE);
rdp_rdp_send_control(self, s, RDP_CTL_REQUEST_CONTROL);
rdp_rdp_recv(self, s, &type); /* RDP_PDU_SYNCHRONIZE */
rdp_rdp_recv(self, s, &type); /* RDP_CTL_COOPERATE */
rdp_rdp_recv(self, s, &type); /* RDP_CTL_GRANT_CONTROL */
rdp_rdp_send_input(self, s, 0, RDP_INPUT_SYNCHRONIZE, 0, 0, 0);
rdp_rdp_send_fonts(self, s, 1);
rdp_rdp_send_fonts(self, s, 2);
rdp_rdp_recv(self, s, &type); /* RDP_PDU_UNKNOWN 0x28 (Fonts?) */
rdp_orders_reset_state(self->orders);
return 0;
}
/******************************************************************************/
int APP_CC
rdp_rec_check_file(struct rdp_rdp *self)
{
char file_name[256];
int index = 0;
int len = 0;
struct stream *s = (struct stream *)NULL;
g_memset(file_name, 0, sizeof(char) * 256);
if (self->rec_fd == 0)
{
index = 1;
g_sprintf(file_name, "rec%8.8d.rec", index);
while (g_file_exist(file_name))
{
index++;
if (index >= 9999)
{
return 1;
}
g_sprintf(file_name, "rec%8.8d.rec", index);
}
self->rec_fd = g_file_open(file_name);
make_stream(s);
init_stream(s, 8192);
out_uint8a(s, "XRDPREC1", 8);
out_uint8s(s, 8);
s_mark_end(s);
len = s->end - s->data;
g_file_write(self->rec_fd, s->data, len);
free_stream(s);
}
return 0;
}
/******************************************************************************/
int APP_CC
rdp_rec_write_item(struct rdp_rdp *self, struct stream *s)
{
int len = 0;
int time = 0;
if (self->rec_fd == 0)
{
return 1;
}
time = g_time1();
out_uint32_le(s, time);
s_mark_end(s);
len = s->end - s->data;
s_pop_layer(s, iso_hdr);
out_uint32_le(s, len);
g_file_write(self->rec_fd, s->data, len);
return 0;
}
|