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
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
|
/**
* xrdp: A Remote Desktop Protocol server.
*
* Copyright (C) Jay Sorg 2009-2012
* Copyright (C) Laxmikant Rashinkar 2009-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.
*/
#include "arch.h"
#include "os_calls.h"
#include "thread_calls.h"
#include "trans.h"
#include "chansrv.h"
#include "defines.h"
#include "sound.h"
#include "clipboard.h"
#include "devredir.h"
#include "list.h"
#include "file.h"
#include "file_loc.h"
#include "log.h"
#include "rail.h"
#include "xcommon.h"
#include "chansrv_fuse.h"
static struct trans *g_lis_trans = 0;
static struct trans *g_con_trans = 0;
static struct trans *g_api_lis_trans = 0;
static struct trans *g_api_con_trans = 0;
static struct chan_item g_chan_items[32];
static int g_num_chan_items = 0;
static int g_cliprdr_index = -1;
static int g_rdpsnd_index = -1;
static int g_rdpdr_index = -1;
static int g_rail_index = -1;
static int g_drdynvc_index = -1;
/* state info for dynamic virtual channels */
static struct xrdp_api_data *g_dvc_channels[MAX_DVC_CHANNELS];
static tbus g_term_event = 0;
static tbus g_thread_done_event = 0;
static int g_use_unix_socket = 0;
int g_display_num = 0;
int g_cliprdr_chan_id = -1; /* cliprdr */
int g_rdpsnd_chan_id = -1; /* rdpsnd */
int g_rdpdr_chan_id = -1; /* rdpdr */
int g_rail_chan_id = -1; /* rail */
int g_drdynvc_chan_id = -1; /* drdynvc */
char *g_exec_name;
tbus g_exec_event;
tbus g_exec_mutex;
tbus g_exec_sem;
int g_exec_pid = 0;
/* each time we create a DVC we need a unique DVC channel id */
/* this variable gets bumped up once per DVC we create */
tui32 g_dvc_chan_id = 100;
/*****************************************************************************/
/* add data to chan_item, on its way to the client */
/* returns error */
static int APP_CC
add_data_to_chan_item(struct chan_item *chan_item, char *data, int size)
{
struct stream *s;
struct chan_out_data *cod;
make_stream(s);
init_stream(s, size);
g_memcpy(s->data, data, size);
s->end = s->data + size;
cod = (struct chan_out_data *)g_malloc(sizeof(struct chan_out_data), 1);
cod->s = s;
if (chan_item->tail == 0)
{
chan_item->tail = cod;
chan_item->head = cod;
}
else
{
chan_item->tail->next = cod;
chan_item->tail = cod;
}
return 0;
}
/*****************************************************************************/
/* returns error */
static int APP_CC
send_data_from_chan_item(struct chan_item *chan_item)
{
struct stream *s;
struct chan_out_data *cod;
int bytes_left;
int size;
int chan_flags;
int error;
if (chan_item->head == 0)
{
return 0;
}
cod = chan_item->head;
bytes_left = (int)(cod->s->end - cod->s->p);
size = MIN(1600, bytes_left);
chan_flags = 0;
if (cod->s->p == cod->s->data)
{
chan_flags |= 1; /* first */
}
if (cod->s->p + size >= cod->s->end)
{
chan_flags |= 2; /* last */
}
s = trans_get_out_s(g_con_trans, 8192);
out_uint32_le(s, 0); /* version */
out_uint32_le(s, 8 + 8 + 2 + 2 + 2 + 4 + size); /* size */
out_uint32_le(s, 8); /* msg id */
out_uint32_le(s, 8 + 2 + 2 + 2 + 4 + size); /* size */
out_uint16_le(s, chan_item->id);
out_uint16_le(s, chan_flags);
out_uint16_le(s, size);
out_uint32_le(s, cod->s->size);
out_uint8a(s, cod->s->p, size);
s_mark_end(s);
LOGM((LOG_LEVEL_DEBUG, "chansrv::send_channel_data: -- "
"size %d chan_flags 0x%8.8x", size, chan_flags));
error = trans_force_write(g_con_trans);
if (error != 0)
{
return 1;
}
cod->s->p += size;
if (cod->s->p >= cod->s->end)
{
free_stream(cod->s);
chan_item->head = chan_item->head->next;
if (chan_item->head == 0)
{
chan_item->tail = 0;
}
g_free(cod);
}
return 0;
}
/*****************************************************************************/
/* returns error */
static int APP_CC
check_chan_items(void)
{
int index;
for (index = 0; index < g_num_chan_items; index++)
{
if (g_chan_items[index].head != 0)
{
send_data_from_chan_item(g_chan_items + index);
}
}
return 0;
}
/*****************************************************************************/
/* returns error */
int APP_CC
send_channel_data(int chan_id, char *data, int size)
{
int index;
//g_writeln("send_channel_data chan_id %d size %d", chan_id, size);
LOGM((LOG_LEVEL_DEBUG, "chansrv::send_channel_data: size %d", size));
if (chan_id == -1)
{
g_writeln("send_channel_data: error, chan_id is -1");
return 1;
}
for (index = 0; index < g_num_chan_items; index++)
{
if (g_chan_items[index].id == chan_id)
{
add_data_to_chan_item(g_chan_items + index, data, size);
check_chan_items();
return 0;
}
}
return 1;
}
/*****************************************************************************/
/* returns error */
static int APP_CC
send_init_response_message(void)
{
struct stream *s = (struct stream *)NULL;
LOGM((LOG_LEVEL_INFO, "send_init_response_message:"));
s = trans_get_out_s(g_con_trans, 8192);
if (s == 0)
{
return 1;
}
out_uint32_le(s, 0); /* version */
out_uint32_le(s, 8 + 8); /* size */
out_uint32_le(s, 2); /* msg id */
out_uint32_le(s, 8); /* size */
s_mark_end(s);
return trans_force_write(g_con_trans);
}
/*****************************************************************************/
/* returns error */
static int APP_CC
send_channel_setup_response_message(void)
{
struct stream *s = (struct stream *)NULL;
LOGM((LOG_LEVEL_DEBUG, "send_channel_setup_response_message:"));
s = trans_get_out_s(g_con_trans, 8192);
if (s == 0)
{
return 1;
}
out_uint32_le(s, 0); /* version */
out_uint32_le(s, 8 + 8); /* size */
out_uint32_le(s, 4); /* msg id */
out_uint32_le(s, 8); /* size */
s_mark_end(s);
return trans_force_write(g_con_trans);
}
/*****************************************************************************/
/* returns error */
static int APP_CC
send_channel_data_response_message(void)
{
struct stream *s = (struct stream *)NULL;
LOGM((LOG_LEVEL_DEBUG, "send_channel_data_response_message:"));
s = trans_get_out_s(g_con_trans, 8192);
if (s == 0)
{
return 1;
}
out_uint32_le(s, 0); /* version */
out_uint32_le(s, 8 + 8); /* size */
out_uint32_le(s, 6); /* msg id */
out_uint32_le(s, 8); /* size */
s_mark_end(s);
return trans_force_write(g_con_trans);
}
/*****************************************************************************/
/* returns error */
static int APP_CC
process_message_init(struct stream *s)
{
LOGM((LOG_LEVEL_DEBUG, "process_message_init:"));
return send_init_response_message();
}
/*****************************************************************************/
/* returns error */
static int APP_CC
process_message_channel_setup(struct stream *s)
{
int num_chans;
int index;
int rv;
struct chan_item *ci;
g_num_chan_items = 0;
g_cliprdr_index = -1;
g_rdpsnd_index = -1;
g_rdpdr_index = -1;
g_rail_index = -1;
g_cliprdr_chan_id = -1;
g_rdpsnd_chan_id = -1;
g_rdpdr_chan_id = -1;
g_rail_chan_id = -1;
g_drdynvc_chan_id = -1;
LOGM((LOG_LEVEL_DEBUG, "process_message_channel_setup:"));
in_uint16_le(s, num_chans);
LOGM((LOG_LEVEL_DEBUG, "process_message_channel_setup: num_chans %d",
num_chans));
for (index = 0; index < num_chans; index++)
{
ci = &(g_chan_items[g_num_chan_items]);
g_memset(ci->name, 0, sizeof(ci->name));
in_uint8a(s, ci->name, 8);
in_uint16_le(s, ci->id);
in_uint16_le(s, ci->flags);
LOGM((LOG_LEVEL_DEBUG, "process_message_channel_setup: chan name '%s' "
"id %d flags %8.8x", ci->name, ci->id, ci->flags));
g_writeln("process_message_channel_setup: chan name '%s' "
"id %d flags %8.8x", ci->name, ci->id, ci->flags);
if (g_strcasecmp(ci->name, "cliprdr") == 0)
{
g_cliprdr_index = g_num_chan_items;
g_cliprdr_chan_id = ci->id;
}
else if (g_strcasecmp(ci->name, "rdpsnd") == 0)
{
g_rdpsnd_index = g_num_chan_items;
g_rdpsnd_chan_id = ci->id;
}
else if (g_strcasecmp(ci->name, "rdpdr") == 0)
{
g_rdpdr_index = g_num_chan_items;
g_rdpdr_chan_id = ci->id;
}
else if (g_strcasecmp(ci->name, "rail") == 0)
{
g_rail_index = g_num_chan_items;
g_rail_chan_id = ci->id;
}
else if (g_strcasecmp(ci->name, "drdynvc") == 0)
{
g_drdynvc_index = g_num_chan_items; // LK_TODO use this
g_drdynvc_chan_id = ci->id; // LK_TODO use this
}
else
{
LOG(10, ("other %s", ci->name));
}
g_num_chan_items++;
}
rv = send_channel_setup_response_message();
if (g_cliprdr_index >= 0)
{
clipboard_init();
fuse_init();
}
if (g_rdpsnd_index >= 0)
{
sound_init();
}
if (g_rdpdr_index >= 0)
{
dev_redir_init();
}
if (g_rail_index >= 0)
{
rail_init();
}
if (g_drdynvc_index >= 0)
{
g_memset(&g_dvc_channels[0], 0, sizeof(g_dvc_channels));
drdynvc_init();
}
return rv;
}
/*****************************************************************************/
/* returns error */
static int APP_CC
process_message_channel_data(struct stream *s)
{
int chan_id = 0;
int chan_flags = 0;
int rv = 0;
int length = 0;
int total_length = 0;
struct stream *ls;
in_uint16_le(s, chan_id);
in_uint16_le(s, chan_flags);
in_uint16_le(s, length);
in_uint32_le(s, total_length);
LOGM((LOG_LEVEL_DEBUG, "process_message_channel_data: chan_id %d "
"chan_flags %d", chan_id, chan_flags));
LOG(10, ("process_message_channel_data"));
rv = send_channel_data_response_message();
if (rv == 0)
{
if (chan_id == g_cliprdr_chan_id)
{
rv = clipboard_data_in(s, chan_id, chan_flags, length, total_length);
}
else if (chan_id == g_rdpsnd_chan_id)
{
rv = sound_data_in(s, chan_id, chan_flags, length, total_length);
}
else if (chan_id == g_rdpdr_chan_id)
{
rv = dev_redir_data_in(s, chan_id, chan_flags, length, total_length);
}
else if (chan_id == g_rail_chan_id)
{
rv = rail_data_in(s, chan_id, chan_flags, length, total_length);
}
else if (chan_id == g_drdynvc_chan_id)
{
rv = drdynvc_data_in(s, chan_id, chan_flags, length, total_length);
}
else if (chan_id == ((struct xrdp_api_data *)
(g_api_con_trans->callback_data))->chan_id)
{
LOG(10, ("process_message_channel_data length %d total_length %d "
"chan_flags 0x%8.8x", length, total_length, chan_flags));
ls = g_api_con_trans->out_s;
if (chan_flags & 1) /* first */
{
init_stream(ls, total_length);
}
out_uint8a(ls, s->p, length);
if (chan_flags & 2) /* last */
{
s_mark_end(ls);
trans_force_write(g_api_con_trans);
}
}
}
return rv;
}
/*****************************************************************************/
/* returns error */
static int APP_CC
process_message_channel_data_response(struct stream *s)
{
LOG(10, ("process_message_channel_data_response:"));
check_chan_items();
return 0;
}
/*****************************************************************************/
/* returns error */
static int APP_CC
process_message(void)
{
struct stream *s = (struct stream *)NULL;
int size = 0;
int id = 0;
int rv = 0;
char *next_msg = (char *)NULL;
if (g_con_trans == 0)
{
return 1;
}
s = trans_get_in_s(g_con_trans);
if (s == 0)
{
return 1;
}
rv = 0;
while (s_check_rem(s, 8))
{
next_msg = s->p;
in_uint32_le(s, id);
in_uint32_le(s, size);
next_msg += size;
switch (id)
{
case 1: /* init */
rv = process_message_init(s);
break;
case 3: /* channel setup */
rv = process_message_channel_setup(s);
break;
case 5: /* channel data */
rv = process_message_channel_data(s);
break;
case 7: /* channel data response */
rv = process_message_channel_data_response(s);
break;
default:
LOGM((LOG_LEVEL_ERROR, "process_message: error in process_message ",
"unknown msg %d", id));
break;
}
if (rv != 0)
{
break;
}
s->p = next_msg;
}
return rv;
}
/*****************************************************************************/
/* returns error */
int DEFAULT_CC
my_trans_data_in(struct trans *trans)
{
struct stream *s = (struct stream *)NULL;
int id = 0;
int size = 0;
int error = 0;
if (trans == 0)
{
return 0;
}
if (trans != g_con_trans)
{
return 1;
}
LOGM((LOG_LEVEL_DEBUG, "my_trans_data_in:"));
s = trans_get_in_s(trans);
in_uint32_le(s, id);
in_uint32_le(s, size);
error = trans_force_read(trans, size - 8);
if (error == 0)
{
/* here, the entire message block is read in, process it */
error = process_message();
}
return error;
}
/*
* called when WTSVirtualChannelWrite() is invoked in xrdpapi.c
*
******************************************************************************/
int DEFAULT_CC
my_api_trans_data_in(struct trans *trans)
{
struct stream *s;
int bytes_read;
struct xrdp_api_data *ad;
//g_writeln("my_api_trans_data_in:");
LOG(10, ("my_api_trans_data_in:"));
if (trans == 0)
{
return 0;
}
if (trans != g_api_con_trans)
{
return 1;
}
LOGM((LOG_LEVEL_DEBUG, "my_api_trans_data_in:"));
s = trans_get_in_s(trans);
bytes_read = g_tcp_recv(trans->sck, s->data, 8192 * 4, 0);
//g_writeln("bytes_read %d", bytes_read);
if (bytes_read > 0)
{
LOG(10, ("my_api_trans_data_in: got data %d", bytes_read));
ad = (struct xrdp_api_data *) trans->callback_data;
if (ad->dvc_chan_id < 0)
{
/* writing data to a static virtual channel */
if (send_channel_data(ad->chan_id, s->data, bytes_read) != 0)
{
LOG(0, ("my_api_trans_data_in: send_channel_data failed"));
}
}
else
{
/* writing data to a dynamic virtual channel */
drdynvc_write_data(ad->dvc_chan_id, s->data, bytes_read);
}
}
else
{
ad = (struct xrdp_api_data *) (trans->callback_data);
if ((ad != NULL) && (ad->dvc_chan_id > 0))
{
/* WTSVirtualChannelClose() was invoked, or connection dropped */
LOG(10, ("my_api_trans_data_in: g_tcp_recv failed or disconnected for DVC"));
ad->transp = NULL;
ad->is_connected = 0;
remove_struct_with_chan_id(ad->dvc_chan_id);
}
else
{
LOG(10, ("my_api_trans_data_in: g_tcp_recv failed or disconnected for SVC"));
}
return 1;
}
return 0;
}
/*****************************************************************************/
int DEFAULT_CC
my_trans_conn_in(struct trans *trans, struct trans *new_trans)
{
if (trans == 0)
{
return 1;
}
if (trans != g_lis_trans)
{
return 1;
}
if (g_con_trans != 0) /* if already set, error */
{
return 1;
}
if (new_trans == 0)
{
return 1;
}
LOGM((LOG_LEVEL_DEBUG, "my_trans_conn_in:"));
g_con_trans = new_trans;
g_con_trans->trans_data_in = my_trans_data_in;
g_con_trans->header_size = 8;
/* stop listening */
trans_delete(g_lis_trans);
g_lis_trans = 0;
return 0;
}
/*
* called when WTSVirtualChannelOpenEx is invoked in xrdpapi.c
*
******************************************************************************/
int DEFAULT_CC
my_api_trans_conn_in(struct trans *trans, struct trans *new_trans)
{
struct xrdp_api_data *ad;
struct stream *s;
int error;
int index;
char chan_pri;
if ((trans == 0) || (trans != g_api_lis_trans) || (new_trans == 0))
{
return 1;
}
LOGM((LOG_LEVEL_DEBUG, "my_api_trans_conn_in:"));
LOG(10, ("my_api_trans_conn_in: got incoming"));
s = trans_get_in_s(new_trans);
s->end = s->data;
error = trans_force_read(new_trans, 64);
if (error != 0)
{
LOG(0, ("my_api_trans_conn_in: trans_force_read failed"));
trans_delete(new_trans);
}
s->end = s->data;
ad = (struct xrdp_api_data *) g_malloc(sizeof(struct xrdp_api_data), 1);
g_memcpy(ad->header, s->data, 64);
ad->flags = GGET_UINT32(ad->header, 16);
ad->chan_id = -1;
ad->dvc_chan_id = -1;
if (ad->flags > 0)
{
/* opening a dynamic virtual channel */
if ((index = find_empty_slot_in_dvc_channels()) < 0)
{
/* exceeded MAX_DVC_CHANNELS */
LOG(0, ("my_api_trans_conn_in: MAX_DVC_CHANNELS reached; giving up!"))
g_free(ad);
trans_delete(new_trans);
return 1;
}
g_dvc_channels[index] = ad;
chan_pri = 4 - ad->flags;
ad->dvc_chan_id = g_dvc_chan_id++;
ad->is_connected = 0;
ad->transp = new_trans;
drdynvc_send_open_channel_request(chan_pri, ad->dvc_chan_id, ad->header);
}
else
{
/* opening a static virtual channel */
for (index = 0; index < g_num_chan_items; index++)
{
LOG(10, ("my_api_trans_conn_in: %s %s", ad->header,
g_chan_items[index].name));
if (g_strcasecmp(ad->header, g_chan_items[index].name) == 0)
{
LOG(10, ("my_api_trans_conn_in: found it at %d", index));
ad->chan_id = g_chan_items[index].id;
break;
}
}
if (index == g_num_chan_items)
{
g_writeln("did not find SVC named %s", ad->header);
}
}
new_trans->callback_data = ad;
trans_delete(g_api_con_trans);
g_api_con_trans = new_trans;
g_api_con_trans->trans_data_in = my_api_trans_data_in;
g_api_con_trans->header_size = 0;
return 0;
}
/*****************************************************************************/
static int APP_CC
setup_listen(void)
{
char port[256];
int error = 0;
if (g_lis_trans != 0)
{
trans_delete(g_lis_trans);
}
if (g_use_unix_socket)
{
g_lis_trans = trans_create(2, 8192, 8192);
g_snprintf(port, 255, "/tmp/.xrdp/xrdp_chansrv_socket_%d",
7200 + g_display_num);
}
else
{
g_lis_trans = trans_create(1, 8192, 8192);
g_snprintf(port, 255, "%d", 7200 + g_display_num);
}
g_lis_trans->trans_conn_in = my_trans_conn_in;
error = trans_listen(g_lis_trans, port);
if (error != 0)
{
LOGM((LOG_LEVEL_ERROR, "setup_listen: trans_listen failed for port %s",
port));
return 1;
}
return 0;
}
/*****************************************************************************/
static int APP_CC
setup_api_listen(void)
{
char port[256];
int error = 0;
g_api_lis_trans = trans_create(TRANS_MODE_UNIX, 8192 * 4, 8192 * 4);
g_snprintf(port, 255, "/tmp/.xrdp/xrdpapi_%d", g_display_num);
g_api_lis_trans->trans_conn_in = my_api_trans_conn_in;
error = trans_listen(g_api_lis_trans, port);
if (error != 0)
{
LOGM((LOG_LEVEL_ERROR, "setup_api_listen: trans_listen failed for port %s",
port));
return 1;
}
return 0;
}
/*****************************************************************************/
THREAD_RV THREAD_CC
channel_thread_loop(void *in_val)
{
tbus objs[32];
int num_objs;
int timeout;
int error;
THREAD_RV rv;
LOGM((LOG_LEVEL_INFO, "channel_thread_loop: thread start"));
rv = 0;
setup_api_listen();
error = setup_listen();
if (error == 0)
{
timeout = -1;
num_objs = 0;
objs[num_objs] = g_term_event;
num_objs++;
trans_get_wait_objs(g_lis_trans, objs, &num_objs);
trans_get_wait_objs(g_api_lis_trans, objs, &num_objs);
while (g_obj_wait(objs, num_objs, 0, 0, timeout) == 0)
{
if (g_is_wait_obj_set(g_term_event))
{
LOGM((LOG_LEVEL_INFO, "channel_thread_loop: g_term_event set"));
clipboard_deinit();
sound_deinit();
dev_redir_deinit();
rail_deinit();
break;
}
if (g_lis_trans != 0)
{
if (trans_check_wait_objs(g_lis_trans) != 0)
{
LOGM((LOG_LEVEL_INFO, "channel_thread_loop: "
"trans_check_wait_objs error"));
}
}
if (g_con_trans != 0)
{
if (trans_check_wait_objs(g_con_trans) != 0)
{
LOGM((LOG_LEVEL_INFO, "channel_thread_loop: "
"trans_check_wait_objs error resetting"));
clipboard_deinit();
sound_deinit();
dev_redir_deinit();
rail_deinit();
/* delete g_con_trans */
trans_delete(g_con_trans);
g_con_trans = 0;
/* create new listener */
error = setup_listen();
if (error != 0)
{
break;
}
}
}
if (g_api_lis_trans != 0)
{
if (trans_check_wait_objs(g_api_lis_trans) != 0)
{
LOG(0, ("channel_thread_loop: trans_check_wait_objs failed"));
}
}
LOG(10, ("0 %p", g_api_con_trans));
if (g_api_con_trans != 0)
{
LOG(10, ("1 %p %d", g_api_con_trans, g_tcp_can_recv(g_api_con_trans->sck, 0)));
if (trans_check_wait_objs(g_api_con_trans) != 0)
{
LOG(10, ("channel_thread_loop: trans_check_wait_objs failed, "
"or disconnected"));
g_free(g_api_con_trans->callback_data);
trans_delete(g_api_con_trans);
g_api_con_trans = 0;
}
}
xcommon_check_wait_objs();
sound_check_wait_objs();
dev_redir_check_wait_objs();
fuse_check_wait_objs();
timeout = -1;
num_objs = 0;
objs[num_objs] = g_term_event;
num_objs++;
trans_get_wait_objs(g_lis_trans, objs, &num_objs);
trans_get_wait_objs(g_con_trans, objs, &num_objs);
trans_get_wait_objs(g_api_lis_trans, objs, &num_objs);
trans_get_wait_objs(g_api_con_trans, objs, &num_objs);
xcommon_get_wait_objs(objs, &num_objs, &timeout);
sound_get_wait_objs(objs, &num_objs, &timeout);
dev_redir_get_wait_objs(objs, &num_objs, &timeout);
fuse_get_wait_objs(objs, &num_objs, &timeout);
}
}
trans_delete(g_lis_trans);
g_lis_trans = 0;
trans_delete(g_con_trans);
g_con_trans = 0;
trans_delete(g_api_lis_trans);
g_api_lis_trans = 0;
trans_delete(g_api_con_trans);
g_api_con_trans = 0;
LOGM((LOG_LEVEL_INFO, "channel_thread_loop: thread stop"));
g_set_wait_obj(g_thread_done_event);
return rv;
}
/*****************************************************************************/
void DEFAULT_CC
term_signal_handler(int sig)
{
LOGM((LOG_LEVEL_INFO, "term_signal_handler: got signal %d", sig));
g_set_wait_obj(g_term_event);
}
/*****************************************************************************/
void DEFAULT_CC
nil_signal_handler(int sig)
{
LOGM((LOG_LEVEL_INFO, "nil_signal_handler: got signal %d", sig));
}
/*****************************************************************************/
void DEFAULT_CC
child_signal_handler(int sig)
{
int i1;
LOG(10, ("child_signal_handler:"));
do
{
i1 = g_waitchild();
if (i1 == g_exec_pid)
{
LOG(0, ("child_signal_handler: found pid %d", i1));
//shutdownx();
}
LOG(10, (" %d", i1));
}
while (i1 >= 0);
}
/*****************************************************************************/
static int APP_CC
get_display_num_from_display(char *display_text)
{
int index;
int mode;
int host_index;
int disp_index;
int scre_index;
char host[256];
char disp[256];
char scre[256];
g_memset(host, 0, 256);
g_memset(disp, 0, 256);
g_memset(scre, 0, 256);
index = 0;
host_index = 0;
disp_index = 0;
scre_index = 0;
mode = 0;
while (display_text[index] != 0)
{
if (display_text[index] == ':')
{
mode = 1;
}
else if (display_text[index] == '.')
{
mode = 2;
}
else if (mode == 0)
{
host[host_index] = display_text[index];
host_index++;
}
else if (mode == 1)
{
disp[disp_index] = display_text[index];
disp_index++;
}
else if (mode == 2)
{
scre[scre_index] = display_text[index];
scre_index++;
}
index++;
}
host[host_index] = 0;
disp[disp_index] = 0;
scre[scre_index] = 0;
g_display_num = g_atoi(disp);
return 0;
}
/*****************************************************************************/
int APP_CC
main_cleanup(void)
{
g_delete_wait_obj(g_term_event);
g_delete_wait_obj(g_thread_done_event);
g_delete_wait_obj(g_exec_event);
tc_mutex_delete(g_exec_mutex);
g_deinit(); /* os_calls */
return 0;
}
/*****************************************************************************/
static int APP_CC
read_ini(void)
{
char filename[256];
struct list *names;
struct list *values;
char *name;
char *value;
int index;
g_memset(filename, 0, (sizeof(char) * 256));
names = list_create();
names->auto_free = 1;
values = list_create();
values->auto_free = 1;
g_use_unix_socket = 0;
g_snprintf(filename, 255, "%s/sesman.ini", XRDP_CFG_PATH);
if (file_by_name_read_section(filename, "Globals", names, values) == 0)
{
for (index = 0; index < names->count; index++)
{
name = (char *)list_get_item(names, index);
value = (char *)list_get_item(values, index);
if (g_strcasecmp(name, "ListenAddress") == 0)
{
if (g_strcasecmp(value, "127.0.0.1") == 0)
{
g_use_unix_socket = 1;
}
}
}
}
list_delete(names);
list_delete(values);
return 0;
}
/*****************************************************************************/
static int APP_CC
run_exec(void)
{
int pid;
LOG(10, ("run_exec:"));
pid = g_fork();
if (pid == 0)
{
trans_delete(g_con_trans);
g_close_wait_obj(g_term_event);
g_close_wait_obj(g_thread_done_event);
g_close_wait_obj(g_exec_event);
tc_mutex_delete(g_exec_mutex);
tc_sem_delete(g_exec_sem);
g_execlp3(g_exec_name, g_exec_name, 0);
g_exit(0);
}
g_exec_pid = pid;
tc_sem_inc(g_exec_sem);
return 0;
}
/*****************************************************************************/
int DEFAULT_CC
main(int argc, char **argv)
{
tbus waiters[4];
int pid = 0;
char text[256];
char *home_text;
char *display_text;
char log_file[256];
enum logReturns error;
struct log_config logconfig;
g_init("xrdp-chansrv"); /* os_calls */
home_text = g_getenv("HOME");
if (home_text == 0)
{
g_writeln("error reading HOME environment variable");
g_deinit();
return 1;
}
read_ini();
pid = g_getpid();
/* starting logging subsystem */
g_memset(&logconfig, 0, sizeof(struct log_config));
logconfig.program_name = "XRDP-Chansrv";
g_snprintf(log_file, 255, "%s/xrdp-chansrv.log", home_text);
g_writeln("chansrv::main: using log file [%s]", log_file);
if (g_file_exist(log_file))
{
g_file_delete(log_file);
}
logconfig.log_file = log_file;
logconfig.fd = -1;
logconfig.log_level = LOG_LEVEL_ERROR;
logconfig.enable_syslog = 0;
logconfig.syslog_level = 0;
error = log_start_from_param(&logconfig);
if (error != LOG_STARTUP_OK)
{
switch (error)
{
case LOG_ERROR_MALLOC:
g_writeln("error on malloc. cannot start logging. quitting.");
break;
case LOG_ERROR_FILE_OPEN:
g_writeln("error opening log file [%s]. quitting.",
getLogFile(text, 255));
break;
default:
g_writeln("log_start error");
break;
}
g_deinit();
return 1;
}
LOGM((LOG_LEVEL_ALWAYS, "main: app started pid %d(0x%8.8x)", pid, pid));
/* set up signal handler */
g_signal_kill(term_signal_handler); /* SIGKILL */
g_signal_terminate(term_signal_handler); /* SIGTERM */
g_signal_user_interrupt(term_signal_handler); /* SIGINT */
g_signal_pipe(nil_signal_handler); /* SIGPIPE */
g_signal_child_stop(child_signal_handler); /* SIGCHLD */
display_text = g_getenv("DISPLAY");
LOGM((LOG_LEVEL_INFO, "main: DISPLAY env var set to %s", display_text));
get_display_num_from_display(display_text);
if (g_display_num == 0)
{
LOGM((LOG_LEVEL_ERROR, "main: error, display is zero"));
g_deinit();
return 1;
}
LOGM((LOG_LEVEL_INFO, "main: using DISPLAY %d", g_display_num));
g_snprintf(text, 255, "xrdp_chansrv_%8.8x_main_term", pid);
g_term_event = g_create_wait_obj(text);
g_snprintf(text, 255, "xrdp_chansrv_%8.8x_thread_done", pid);
g_thread_done_event = g_create_wait_obj(text);
g_snprintf(text, 255, "xrdp_chansrv_%8.8x_exec", pid);
g_exec_event = g_create_wait_obj(text);
g_exec_mutex = tc_mutex_create();
g_exec_sem = tc_sem_create(0);
tc_thread_create(channel_thread_loop, 0);
while (g_term_event > 0 && !g_is_wait_obj_set(g_term_event))
{
waiters[0] = g_term_event;
waiters[1] = g_exec_event;
if (g_obj_wait(waiters, 2, 0, 0, 0) != 0)
{
LOGM((LOG_LEVEL_ERROR, "main: error, g_obj_wait failed"));
break;
}
if (g_is_wait_obj_set(g_term_event))
{
break;
}
if (g_is_wait_obj_set(g_exec_event))
{
g_reset_wait_obj(g_exec_event);
run_exec();
}
}
while (g_thread_done_event > 0 && !g_is_wait_obj_set(g_thread_done_event))
{
/* wait for thread to exit */
if (g_obj_wait(&g_thread_done_event, 1, 0, 0, 0) != 0)
{
LOGM((LOG_LEVEL_ERROR, "main: error, g_obj_wait failed"));
break;
}
}
/* cleanup */
main_cleanup();
LOGM((LOG_LEVEL_INFO, "main: app exiting pid %d(0x%8.8x)", pid, pid));
g_deinit();
return 0;
}
/*
* return unused slot in dvc_channels[]
*
* @return unused slot index on success, -1 on failure
******************************************************************************/
int APP_CC
find_empty_slot_in_dvc_channels()
{
int i;
for (i = 0; i < MAX_DVC_CHANNELS; i++)
{
if (g_dvc_channels[i] == NULL)
{
return i;
}
}
return -1;
}
/*
* return struct xrdp_api_data that contains specified dvc_chan_id
*
* @param dvc_chan_id channel id to look for
*
* @return xrdp_api_data struct containing dvc_chan_id or NULL on failure
******************************************************************************/
struct xrdp_api_data *APP_CC
struct_from_dvc_chan_id(tui32 dvc_chan_id)
{
int i;
for (i = 0; i < MAX_DVC_CHANNELS; i++)
{
if (g_dvc_channels[i]->dvc_chan_id == dvc_chan_id)
{
return g_dvc_channels[i];
}
}
return NULL;
}
int
remove_struct_with_chan_id(tui32 dvc_chan_id)
{
int i;
if (dvc_chan_id < 0)
{
return -1;
}
for (i = 0; i < MAX_DVC_CHANNELS; i++)
{
if (g_dvc_channels[i]->dvc_chan_id == dvc_chan_id)
{
g_dvc_channels[i] = NULL;
return 0;
}
}
return -1;
}
|