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
|
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
xrdp: A Remote Desktop Protocol server.
Copyright (C) Jay Sorg 2004-2008
module manager
*/
#include "xrdp.h"
/*****************************************************************************/
struct xrdp_mm* APP_CC
xrdp_mm_create(struct xrdp_wm* owner)
{
struct xrdp_mm* self;
self = (struct xrdp_mm*)g_malloc(sizeof(struct xrdp_mm), 1);
self->wm = owner;
self->login_names = list_create();
self->login_names->auto_free = 1;
self->login_values = list_create();
self->login_values->auto_free = 1;
return self;
}
/*****************************************************************************/
/* called from main thread */
static long DEFAULT_CC
sync_unload(long param1, long param2)
{
return g_free_library(param1);
}
/*****************************************************************************/
/* called from main thread */
static long DEFAULT_CC
sync_load(long param1, long param2)
{
long rv;
char* libname;
libname = (char*)param1;
rv = g_load_library(libname);
return rv;
}
/*****************************************************************************/
static void APP_CC
xrdp_mm_module_cleanup(struct xrdp_mm* self)
{
if (self->mod != 0)
{
if (self->mod_exit != 0)
{
self->mod_exit(self->mod);
}
}
if (self->mod_handle != 0)
{
g_xrdp_sync(sync_unload, self->mod_handle, 0);
}
trans_delete(self->chan_trans);
self->mod_init = 0;
self->mod_exit = 0;
self->mod = 0;
self->mod_handle = 0;
}
/*****************************************************************************/
void APP_CC
xrdp_mm_delete(struct xrdp_mm* self)
{
if (self == 0)
{
return;
}
/* free any module stuff */
xrdp_mm_module_cleanup(self);
if (self->sck_obj != 0)
{
g_delete_wait_obj_from_socket(self->sck_obj);
self->sck_obj = 0;
}
if (self->sck != 0)
{
g_tcp_close(self->sck);
self->sck = 0;
}
list_delete(self->login_names);
list_delete(self->login_values);
g_free(self);
}
/******************************************************************************/
/* returns error */
static int APP_CC
xrdp_mm_recv(struct xrdp_mm* self, char* data, int len)
{
int rcvd;
if (self->sck_closed)
{
return 1;
}
while (len > 0)
{
rcvd = g_tcp_recv(self->sck, data, len, 0);
if (rcvd == -1)
{
if (g_tcp_last_error_would_block(self->sck))
{
if (g_is_term())
{
return 1;
}
g_tcp_can_recv(self->sck, 10);
}
else
{
return 1;
}
}
else if (rcvd == 0)
{
self->sck_closed = 1;
return 1;
}
else
{
data += rcvd;
len -= rcvd;
}
}
return 0;
}
/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mm_send(struct xrdp_mm* self, char* data, int len)
{
int sent;
if (self->sck_closed)
{
return 1;
}
while (len > 0)
{
sent = g_tcp_send(self->sck, data, len, 0);
if (sent == -1)
{
if (g_tcp_last_error_would_block(self->sck))
{
if (g_is_term())
{
return 1;
}
g_tcp_can_send(self->sck, 10);
}
else
{
return 1;
}
}
else if (sent == 0)
{
self->sck_closed = 1;
return 1;
}
else
{
data += sent;
len -= sent;
}
}
return 0;
}
/*****************************************************************************/
static int APP_CC
xrdp_mm_send_login(struct xrdp_mm* self)
{
struct stream* s;
int rv;
int index;
int count;
char* username;
char* password;
char* name;
char* value;
xrdp_wm_log_msg(self->wm, "sending login info to sesman");
username = 0;
password = 0;
self->code = 0;
count = self->login_names->count;
for (index = 0; index < count; index++)
{
name = (char*)list_get_item(self->login_names, index);
value = (char*)list_get_item(self->login_values, index);
if (g_strcasecmp(name, "username") == 0)
{
username = value;
}
else if (g_strcasecmp(name, "password") == 0)
{
password = value;
}
else if (g_strcasecmp(name, "lib") == 0)
{
if ((g_strcasecmp(value, "libxup.so") == 0) ||
(g_strcasecmp(value, "xup.dll") == 0))
{
self->code = 10;
}
}
}
if ((username == 0) || (password == 0))
{
xrdp_wm_log_msg(self->wm, "error finding username and password");
return 1;
}
make_stream(s);
init_stream(s, 8192);
s_push_layer(s, channel_hdr, 8);
/* this code is either 0 for Xvnc or 10 for X11rdp */
out_uint16_be(s, self->code);
index = g_strlen(username);
out_uint16_be(s, index);
out_uint8a(s, username, index);
index = g_strlen(password);
out_uint16_be(s, index);
out_uint8a(s, password, index);
out_uint16_be(s, self->wm->screen->width);
out_uint16_be(s, self->wm->screen->height);
out_uint16_be(s, self->wm->screen->bpp);
s_mark_end(s);
s_pop_layer(s, channel_hdr);
out_uint32_be(s, 0); /* version */
index = s->end - s->data;
out_uint32_be(s, index); /* size */
rv = xrdp_mm_send(self, s->data, index);
free_stream(s);
return rv;
}
/*****************************************************************************/
/* returns error */
/* this goes through the login_names looking for one called 'lib'
then it copies the corisponding login_values item into 'dest'
'dest' must be at least 'dest_len' + 1 bytes in size */
static int APP_CC
xrdp_mm_get_lib(struct xrdp_mm* self, char* dest, int dest_len)
{
char* name;
char* value;
int index;
int count;
int rv;
rv = 1;
/* find the library name */
dest[0] = 0;
count = self->login_names->count;
for (index = 0; index < count; index++)
{
name = (char*)list_get_item(self->login_names, index);
value = (char*)list_get_item(self->login_values, index);
if ((name == 0) || (value == 0))
{
break;
}
if (g_strcasecmp(name, "lib") == 0)
{
g_strncpy(dest, value, dest_len);
rv = 0;
}
}
return rv;
}
/*****************************************************************************/
static int APP_CC
xrdp_mm_setup_mod1(struct xrdp_mm* self)
{
void* func;
char lib[256];
char text[256];
if (self == 0)
{
return 1;
}
lib[0] = 0;
if (xrdp_mm_get_lib(self, lib, 255) != 0)
{
g_snprintf(text, 255, "error finding lib");
xrdp_wm_log_msg(self->wm, text);
return 1;
}
if (lib[0] == 0)
{
g_snprintf(text, 255, "error finding lib");
xrdp_wm_log_msg(self->wm, text);
return 1;
}
if (self->mod_handle == 0)
{
self->mod_handle = g_xrdp_sync(sync_load, (long)lib, 0);
if (self->mod_handle != 0)
{
func = g_get_proc_address(self->mod_handle, "mod_init");
if (func == 0)
{
func = g_get_proc_address(self->mod_handle, "_mod_init");
}
if (func == 0)
{
g_snprintf(text, 255, "error finding proc mod_init in %s", lib);
xrdp_wm_log_msg(self->wm, text);
}
self->mod_init = (struct xrdp_mod* (*)(void))func;
func = g_get_proc_address(self->mod_handle, "mod_exit");
if (func == 0)
{
func = g_get_proc_address(self->mod_handle, "_mod_exit");
}
if (func == 0)
{
g_snprintf(text, 255, "error finding proc mod_exit in %s", lib);
xrdp_wm_log_msg(self->wm, text);
}
self->mod_exit = (int (*)(struct xrdp_mod*))func;
if ((self->mod_init != 0) && (self->mod_exit != 0))
{
self->mod = self->mod_init();
}
}
else
{
g_snprintf(text, 255, "error loading %s", lib);
xrdp_wm_log_msg(self->wm, text);
}
if (self->mod != 0)
{
self->mod->wm = (long)(self->wm);
self->mod->server_begin_update = server_begin_update;
self->mod->server_end_update = server_end_update;
self->mod->server_fill_rect = server_fill_rect;
self->mod->server_screen_blt = server_screen_blt;
self->mod->server_paint_rect = server_paint_rect;
self->mod->server_set_pointer = server_set_pointer;
self->mod->server_palette = server_palette;
self->mod->server_msg = server_msg;
self->mod->server_is_term = server_is_term;
self->mod->server_set_clip = server_set_clip;
self->mod->server_reset_clip = server_reset_clip;
self->mod->server_set_fgcolor = server_set_fgcolor;
self->mod->server_set_bgcolor = server_set_bgcolor;
self->mod->server_set_opcode = server_set_opcode;
self->mod->server_set_mixmode = server_set_mixmode;
self->mod->server_set_brush = server_set_brush;
self->mod->server_set_pen = server_set_pen;
self->mod->server_draw_line = server_draw_line;
self->mod->server_add_char = server_add_char;
self->mod->server_draw_text = server_draw_text;
self->mod->server_reset = server_reset;
self->mod->server_query_channel = server_query_channel;
self->mod->server_get_channel_id = server_get_channel_id;
self->mod->server_send_to_channel = server_send_to_channel;
}
}
/* id self->mod is null, there must be a problem */
if (self->mod == 0)
{
DEBUG(("problem loading lib in xrdp_mm_setup_mod1"));
return 1;
}
return 0;
}
/*****************************************************************************/
static int APP_CC
xrdp_mm_setup_mod2(struct xrdp_mm* self)
{
char text[256];
char* name;
char* value;
int i;
int rv;
int key_flags;
int device_flags;
rv = 1;
text[0] = 0;
if (!g_is_wait_obj_set(self->wm->pro_layer->self_term_event))
{
if (self->mod->mod_start(self->mod, self->wm->screen->width,
self->wm->screen->height,
self->wm->screen->bpp) != 0)
{
g_set_wait_obj(self->wm->pro_layer->self_term_event); /* kill session */
}
}
if (!g_is_wait_obj_set(self->wm->pro_layer->self_term_event))
{
if (self->display > 0)
{
if (self->code == 0) /* Xvnc */
{
g_snprintf(text, 255, "%d", 5900 + self->display);
}
else if (self->code == 10) /* X11rdp */
{
g_snprintf(text, 255, "%d", 6200 + self->display);
}
else
{
g_set_wait_obj(self->wm->pro_layer->self_term_event); /* kill session */
}
}
}
if (!g_is_wait_obj_set(self->wm->pro_layer->self_term_event))
{
/* this adds the port to the end of the list, it will already be in
the list as -1
the module should use the last one */
if (g_strlen(text) > 0)
{
list_add_item(self->login_names, (long)g_strdup("port"));
list_add_item(self->login_values, (long)g_strdup(text));
}
/* always set these */
name = self->wm->session->client_info->hostname;
self->mod->mod_set_param(self->mod, "hostname", name);
g_snprintf(text, 255, "%d", self->wm->session->client_info->keylayout);
self->mod->mod_set_param(self->mod, "keylayout", text);
for (i = 0; i < self->login_names->count; i++)
{
name = (char*)list_get_item(self->login_names, i);
value = (char*)list_get_item(self->login_values, i);
self->mod->mod_set_param(self->mod, name, value);
}
/* connect */
if (self->mod->mod_connect(self->mod) == 0)
{
rv = 0;
}
}
if (rv == 0)
{
/* sync modifiers */
key_flags = 0;
device_flags = 0;
if (self->wm->scroll_lock)
{
key_flags |= 1;
}
if (self->wm->num_lock)
{
key_flags |= 2;
}
if (self->wm->caps_lock)
{
key_flags |= 4;
}
if (self->mod != 0)
{
if (self->mod->mod_event != 0)
{
self->mod->mod_event(self->mod, 17, key_flags, device_flags,
key_flags, device_flags);
}
}
}
return rv;
}
/*****************************************************************************/
/* returns error
send a list of channels to the channel handler */
static int APP_CC
xrdp_mm_trans_send_channel_setup(struct xrdp_mm* self, struct trans* trans)
{
int index;
int chan_id;
int chan_flags;
int size;
struct stream* s;
char chan_name[256];
s = trans_get_out_s(trans, 8192);
if (s == 0)
{
return 1;
}
s_push_layer(s, iso_hdr, 8);
s_push_layer(s, mcs_hdr, 8);
s_push_layer(s, sec_hdr, 2);
index = 0;
while (server_query_channel(self->mod, index, chan_name,
&chan_flags) == 0)
{
chan_id = server_get_channel_id(self->mod, chan_name);
out_uint8a(s, chan_name, 8);
out_uint16_le(s, chan_id);
out_uint16_le(s, chan_flags);
index++;
}
s_mark_end(s);
s_pop_layer(s, sec_hdr);
out_uint16_le(s, index);
s_pop_layer(s, mcs_hdr);
size = (int)(s->end - s->p);
out_uint32_le(s, 3); /* msg id */
out_uint32_le(s, size); /* msg size */
s_pop_layer(s, iso_hdr);
size = (int)(s->end - s->p);
out_uint32_le(s, 0); /* version */
out_uint32_le(s, size); /* block size */
return trans_force_write(trans);
}
/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_mm_trans_send_channel_data_response(struct xrdp_mm* self,
struct trans* trans)
{
struct stream* s;
s = trans_get_out_s(trans, 8192);
if (s == 0)
{
return 1;
}
out_uint32_le(s, 0); /* version */
out_uint32_le(s, 8 + 8); /* size */
out_uint32_le(s, 7); /* msg id */
out_uint32_le(s, 8); /* size */
s_mark_end(s);
return trans_force_write(trans);
}
/*****************************************************************************/
/* returns error
init is done, sent channel setup */
static int APP_CC
xrdp_mm_trans_process_init_response(struct xrdp_mm* self, struct trans* trans)
{
return xrdp_mm_trans_send_channel_setup(self, trans);
}
/*****************************************************************************/
/* returns error
data coming in from the channel handler, send it to the client */
static int APP_CC
xrdp_mm_trans_process_channel_data(struct xrdp_mm* self, struct trans* trans)
{
struct stream* s;
int size;
int total_size;
int chan_id;
int chan_flags;
int rv;
s = trans_get_in_s(trans);
if (s == 0)
{
return 1;
}
in_uint16_le(s, chan_id);
in_uint16_le(s, chan_flags);
in_uint16_le(s, size);
in_uint32_le(s, total_size);
rv = xrdp_mm_trans_send_channel_data_response(self, trans);
if (rv == 0)
{
rv = server_send_to_channel(self->mod, chan_id, s->p, size, total_size,
chan_flags);
}
return rv;
}
/*****************************************************************************/
/* returns error
process a message for the channel handler */
static int APP_CC
xrdp_mm_chan_process_msg(struct xrdp_mm* self, struct trans* trans,
struct stream* s)
{
int rv;
int id;
int size;
char* next_msg;
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 2: /* channel init response */
g_writeln("channel init response");
rv = xrdp_mm_trans_process_init_response(self, trans);
break;
case 4: /* channel setup response */
g_writeln("channel setup response");
break;
case 6: /* channel data response */
g_writeln("channel data response");
break;
case 8: /* channel data */
g_writeln("channel data");
rv = xrdp_mm_trans_process_channel_data(self, trans);
break;
default:
g_writeln("unknown");
break;
}
if (rv != 0)
{
break;
}
s->p = next_msg;
}
return rv;
}
/*****************************************************************************/
/* this is callback from trans obj
returns error */
static int APP_CC
xrdp_mm_chan_data_in(struct trans* trans)
{
struct xrdp_mm* self;
struct stream* s;
int id;
int size;
int error;
if (trans == 0)
{
return 1;
}
g_writeln("xrdp_mm_chan_data_in");
self = (struct xrdp_mm*)(trans->callback_data);
s = trans_get_in_s(trans);
if (s == 0)
{
return 1;
}
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 = xrdp_mm_chan_process_msg(self, trans, s);
}
return error;
}
/*****************************************************************************/
static int APP_CC
xrdp_mm_chan_send_init(struct xrdp_mm* self)
{
struct stream* s;
s = trans_get_out_s(self->chan_trans, 8192);
if (s == 0)
{
return 1;
}
out_uint32_le(s, 0); /* version */
out_uint32_le(s, 8 + 8); /* size */
out_uint32_le(s, 1); /* msg id */
out_uint32_le(s, 8); /* size */
s_mark_end(s);
return trans_force_write(self->chan_trans);
}
/*****************************************************************************/
static int APP_CC
xrdp_mm_process_login_response(struct xrdp_mm* self, struct stream* s)
{
int ok;
int display;
int rv;
char text[256];
rv = 0;
in_uint16_be(s, ok);
in_uint16_be(s, display);
if (ok)
{
self->display = display;
g_snprintf(text, 255, "login successful for display %d", display);
xrdp_wm_log_msg(self->wm, text);
if (xrdp_mm_setup_mod1(self) == 0)
{
if (xrdp_mm_setup_mod2(self) == 0)
{
xrdp_wm_set_login_mode(self->wm, 10);
self->wm->dragging = 0;
/* connect channel redir */
self->chan_trans = trans_create(1, 8192, 8192);
self->chan_trans->trans_data_in = xrdp_mm_chan_data_in;
self->chan_trans->header_size = 8;
self->chan_trans->callback_data = self;
g_snprintf(text, 255, "%d", 7200 + display);
if (trans_connect(self->chan_trans, "127.0.0.1", text, 3) != 0)
{
g_writeln("error in trans_connect");
}
if (xrdp_mm_chan_send_init(self) != 0)
{
g_writeln("error in xrdp_mm_chan_send_init");
}
}
}
}
else
{
xrdp_wm_log_msg(self->wm, "login failed");
}
/* close socket */
g_delete_wait_obj_from_socket(self->sck_obj);
self->sck_obj = 0;
g_tcp_close(self->sck);
self->sck = 0;
self->connected_state = 0;
if (self->wm->login_mode != 10)
{
xrdp_wm_set_login_mode(self->wm, 11);
xrdp_mm_module_cleanup(self);
}
return rv;
}
/*****************************************************************************/
int APP_CC
xrdp_mm_connect(struct xrdp_mm* self)
{
struct list* names;
struct list* values;
int index;
int count;
int use_sesman;
int error;
int ok;
int rv;
char* name;
char* value;
char ip[256];
char errstr[256];
char text[256];
rv = 0;
use_sesman = 0;
names = self->login_names;
values = self->login_values;
count = names->count;
for (index = 0; index < count; index++)
{
name = (char*)list_get_item(names, index);
value = (char*)list_get_item(values, index);
if (g_strcasecmp(name, "ip") == 0)
{
g_strncpy(ip, value, 255);
}
else if (g_strcasecmp(name, "port") == 0)
{
if (g_strcasecmp(value, "-1") == 0)
{
use_sesman = 1;
}
}
}
if (use_sesman)
{
ok = 0;
errstr[0] = 0;
self->sck = g_tcp_socket();
self->sck_obj = g_create_wait_obj_from_socket(self->sck, 0);
g_tcp_set_non_blocking(self->sck);
g_snprintf(text, 255, "connecting to sesman ip %s port 3350", ip);
xrdp_wm_log_msg(self->wm, text);
error = g_tcp_connect(self->sck, ip, "3350");
if (error == 0)
{
ok = 1;
}
else if (error == -1)
{
if (g_tcp_last_error_would_block(self->sck))
{
ok = 1;
/* wait up to 3 seconds for connect to complete */
index = 0;
while (!g_tcp_can_send(self->sck, 100))
{
index++;
if ((index >= 30) || g_is_term())
{
g_snprintf(errstr, 255, "error connect sesman - connect timeout");
ok = 0;
break;
}
}
}
else
{
name = g_get_strerror();
g_snprintf(errstr, 255, "error connect sesman - %s", name);
}
}
if (ok)
{
/* fully connect */
xrdp_wm_log_msg(self->wm, "sesman connect ok");
self->connected_state = 1;
rv = xrdp_mm_send_login(self);
}
else
{
xrdp_wm_log_msg(self->wm, errstr);
g_delete_wait_obj_from_socket(self->sck_obj);
self->sck_obj = 0;
g_tcp_close(self->sck);
self->sck = 0;
rv = 1;
}
}
else /* no sesman */
{
if (xrdp_mm_setup_mod1(self) == 0)
{
if (xrdp_mm_setup_mod2(self) == 0)
{
xrdp_wm_set_login_mode(self->wm, 10);
}
}
if (self->wm->login_mode != 10)
{
xrdp_wm_set_login_mode(self->wm, 11);
xrdp_mm_module_cleanup(self);
}
}
self->sesman_controlled = use_sesman;
return rv;
}
/*****************************************************************************/
int APP_CC
xrdp_mm_signal(struct xrdp_mm* self)
{
struct stream* s;
int error;
int version;
int size;
int code;
int rv;
rv = 0;
if (self->connected_state == 1)
{
make_stream(s);
init_stream(s, 8192);
error = xrdp_mm_recv(self, s->data, 8);
if (error == 0)
{
in_uint32_be(s, version);
in_uint32_be(s, size);
init_stream(s, 8192);
error = (size <= 8) || (size > 8000);
}
if (error == 0)
{
error = xrdp_mm_recv(self, s->data, size - 8);
}
if (error == 0)
{
in_uint16_be(s, code);
switch (code)
{
case 3:
rv = xrdp_mm_process_login_response(self, s);
break;
default:
g_writeln("unknown code %d in xrdp_mm_signal", code);
break;
}
}
free_stream(s);
}
return rv;
}
/*****************************************************************************/
/* returns error
data coming from client that need to go to channel handler */
int APP_CC
xrdp_mm_process_channel_data(struct xrdp_mm* self, tbus param1, tbus param2,
tbus param3, tbus param4)
{
struct stream* s;
int rv;
int length;
int total_length;
int flags;
int id;
char* data;
g_writeln("in xrdp_mm_process_channel_data %d %d %d %d", param1, param2, param3, param4);
rv = 0;
if (self->chan_trans != 0)
{
s = trans_get_out_s(self->chan_trans, 8192);
if (s != 0)
{
id = LOWORD(param1);
flags = HIWORD(param1);
length = param2;
data = (char*)param3;
total_length = param4;
if (total_length < length)
{
g_writeln("warning in xrdp_mm_process_channel_data total_len < length");
total_length = length;
}
out_uint32_le(s, 0); /* version */
out_uint32_le(s, 8 + 8 + 2 + 2 + 2 + 4 + length);
out_uint32_le(s, 5); /* msg id */
out_uint32_le(s, 8 + 2 + 2 + 2 + 4 + length);
out_uint16_le(s, id);
out_uint16_le(s, flags);
out_uint16_le(s, length);
out_uint32_le(s, total_length);
out_uint8a(s, data, length);
s_mark_end(s);
rv = trans_force_write(self->chan_trans);
}
}
return rv;
}
/*****************************************************************************/
int DEFAULT_CC
server_begin_update(struct xrdp_mod* mod)
{
struct xrdp_wm* wm;
struct xrdp_painter* p;
wm = (struct xrdp_wm*)mod->wm;
p = xrdp_painter_create(wm, wm->session);
xrdp_painter_begin_update(p);
mod->painter = (long)p;
return 0;
}
/*****************************************************************************/
int DEFAULT_CC
server_end_update(struct xrdp_mod* mod)
{
struct xrdp_painter* p;
p = (struct xrdp_painter*)mod->painter;
xrdp_painter_end_update(p);
xrdp_painter_delete(p);
mod->painter = 0;
return 0;
}
/*****************************************************************************/
int DEFAULT_CC
server_fill_rect(struct xrdp_mod* mod, int x, int y, int cx, int cy)
{
struct xrdp_wm* wm;
struct xrdp_painter* p;
wm = (struct xrdp_wm*)mod->wm;
p = (struct xrdp_painter*)mod->painter;
xrdp_painter_fill_rect(p, wm->screen, x, y, cx, cy);
return 0;
}
/*****************************************************************************/
int DEFAULT_CC
server_screen_blt(struct xrdp_mod* mod, int x, int y, int cx, int cy,
int srcx, int srcy)
{
struct xrdp_wm* wm;
struct xrdp_painter* p;
wm = (struct xrdp_wm*)mod->wm;
p = (struct xrdp_painter*)mod->painter;
p->rop = 0xcc;
xrdp_painter_copy(p, wm->screen, wm->screen, x, y, cx, cy, srcx, srcy);
return 0;
}
/*****************************************************************************/
int DEFAULT_CC
server_paint_rect(struct xrdp_mod* mod, int x, int y, int cx, int cy,
char* data, int width, int height, int srcx, int srcy)
{
struct xrdp_wm* wm;
struct xrdp_bitmap* b;
struct xrdp_painter* p;
wm = (struct xrdp_wm*)mod->wm;
p = (struct xrdp_painter*)mod->painter;
b = xrdp_bitmap_create_with_data(width, height, wm->screen->bpp, data, wm);
xrdp_painter_copy(p, b, wm->screen, x, y, cx, cy, srcx, srcy);
xrdp_bitmap_delete(b);
return 0;
}
/*****************************************************************************/
int DEFAULT_CC
server_set_pointer(struct xrdp_mod* mod, int x, int y,
char* data, char* mask)
{
struct xrdp_wm* wm;
wm = (struct xrdp_wm*)mod->wm;
xrdp_wm_pointer(wm, data, mask, x, y);
return 0;
}
/*****************************************************************************/
int DEFAULT_CC
server_palette(struct xrdp_mod* mod, int* palette)
{
struct xrdp_wm* wm;
wm = (struct xrdp_wm*)mod->wm;
if (g_memcmp(wm->palette, palette, 255 * sizeof(int)) != 0)
{
g_memcpy(wm->palette, palette, 256 * sizeof(int));
xrdp_wm_send_palette(wm);
}
return 0;
}
/*****************************************************************************/
int DEFAULT_CC
server_msg(struct xrdp_mod* mod, char* msg, int code)
{
struct xrdp_wm* wm;
if (code == 1)
{
g_writeln(msg);
return 0;
}
wm = (struct xrdp_wm*)mod->wm;
return xrdp_wm_log_msg(wm, msg);
}
/*****************************************************************************/
int DEFAULT_CC
server_is_term(struct xrdp_mod* mod)
{
return g_is_term();
}
/*****************************************************************************/
int DEFAULT_CC
server_set_clip(struct xrdp_mod* mod, int x, int y, int cx, int cy)
{
struct xrdp_painter* p;
p = (struct xrdp_painter*)mod->painter;
return xrdp_painter_set_clip(p, x, y, cx, cy);
}
/*****************************************************************************/
int DEFAULT_CC
server_reset_clip(struct xrdp_mod* mod)
{
struct xrdp_painter* p;
p = (struct xrdp_painter*)mod->painter;
return xrdp_painter_clr_clip(p);
}
/*****************************************************************************/
int DEFAULT_CC
server_set_fgcolor(struct xrdp_mod* mod, int fgcolor)
{
struct xrdp_painter* p;
p = (struct xrdp_painter*)mod->painter;
p->fg_color = fgcolor;
p->pen.color = p->fg_color;
return 0;
}
/*****************************************************************************/
int DEFAULT_CC
server_set_bgcolor(struct xrdp_mod* mod, int bgcolor)
{
struct xrdp_painter* p;
p = (struct xrdp_painter*)mod->painter;
p->bg_color = bgcolor;
return 0;
}
/*****************************************************************************/
int DEFAULT_CC
server_set_opcode(struct xrdp_mod* mod, int opcode)
{
struct xrdp_painter* p;
p = (struct xrdp_painter*)mod->painter;
p->rop = opcode;
return 0;
}
/*****************************************************************************/
int DEFAULT_CC
server_set_mixmode(struct xrdp_mod* mod, int mixmode)
{
struct xrdp_painter* p;
p = (struct xrdp_painter*)mod->painter;
p->mix_mode = mixmode;
return 0;
}
/*****************************************************************************/
int DEFAULT_CC
server_set_brush(struct xrdp_mod* mod, int x_orgin, int y_orgin,
int style, char* pattern)
{
struct xrdp_painter* p;
p = (struct xrdp_painter*)mod->painter;
p->brush.x_orgin = x_orgin;
p->brush.y_orgin = y_orgin;
p->brush.style = style;
g_memcpy(p->brush.pattern, pattern, 8);
return 0;
}
/*****************************************************************************/
int DEFAULT_CC
server_set_pen(struct xrdp_mod* mod, int style, int width)
{
struct xrdp_painter* p;
p = (struct xrdp_painter*)mod->painter;
p->pen.style = style;
p->pen.width = width;
return 0;
}
/*****************************************************************************/
int DEFAULT_CC
server_draw_line(struct xrdp_mod* mod, int x1, int y1, int x2, int y2)
{
struct xrdp_wm* wm;
struct xrdp_painter* p;
wm = (struct xrdp_wm*)mod->wm;
p = (struct xrdp_painter*)mod->painter;
return xrdp_painter_line(p, wm->screen, x1, y1, x2, y2);
}
/*****************************************************************************/
int DEFAULT_CC
server_add_char(struct xrdp_mod* mod, int font, int charactor,
int offset, int baseline,
int width, int height, char* data)
{
struct xrdp_font_char fi;
fi.offset = offset;
fi.baseline = baseline;
fi.width = width;
fi.height = height;
fi.incby = 0;
fi.data = data;
return libxrdp_orders_send_font(((struct xrdp_wm*)mod->wm)->session,
&fi, font, charactor);
}
/*****************************************************************************/
int DEFAULT_CC
server_draw_text(struct xrdp_mod* mod, int font,
int flags, int mixmode, int clip_left, int clip_top,
int clip_right, int clip_bottom,
int box_left, int box_top,
int box_right, int box_bottom,
int x, int y, char* data, int data_len)
{
struct xrdp_wm* wm;
struct xrdp_painter* p;
wm = (struct xrdp_wm*)mod->wm;
p = (struct xrdp_painter*)mod->painter;
return xrdp_painter_draw_text2(p, wm->screen, font, flags,
mixmode, clip_left, clip_top,
clip_right, clip_bottom,
box_left, box_top,
box_right, box_bottom,
x, y, data, data_len);
}
/*****************************************************************************/
int DEFAULT_CC
server_reset(struct xrdp_mod* mod, int width, int height, int bpp)
{
struct xrdp_wm* wm;
wm = (struct xrdp_wm*)mod->wm;
if (wm->client_info == 0)
{
return 1;
}
/* older client can't resize */
if (wm->client_info->build <= 419)
{
return 0;
}
/* if same, don't need to do anything */
if (wm->client_info->width == width &&
wm->client_info->height == height &&
wm->client_info->bpp == bpp)
{
return 0;
}
/* reset lib, client_info gets updated in libxrdp_reset */
if (libxrdp_reset(wm->session, width, height, bpp) != 0)
{
return 1;
}
/* reset cache */
xrdp_cache_reset(wm->cache, wm->client_info);
/* resize the main window */
xrdp_bitmap_resize(wm->screen, wm->client_info->width,
wm->client_info->height);
/* load some stuff */
xrdp_wm_load_static_colors(wm);
xrdp_wm_load_static_pointers(wm);
return 0;
}
/*****************************************************************************/
int DEFAULT_CC
server_query_channel(struct xrdp_mod* mod, int index, char* channel_name,
int* channel_flags)
{
struct xrdp_wm* wm;
wm = (struct xrdp_wm*)mod->wm;
if (wm->mm->sesman_controlled)
{
return 1;
}
return libxrdp_query_channel(wm->session, index, channel_name,
channel_flags);
}
/*****************************************************************************/
int DEFAULT_CC
server_get_channel_id(struct xrdp_mod* mod, char* name)
{
struct xrdp_wm* wm;
wm = (struct xrdp_wm*)mod->wm;
if (wm->mm->sesman_controlled)
{
return 1;
}
return libxrdp_get_channel_id(wm->session, name);
}
/*****************************************************************************/
int DEFAULT_CC
server_send_to_channel(struct xrdp_mod* mod, int channel_id,
char* data, int data_len,
int total_data_len, int flags)
{
struct xrdp_wm* wm;
wm = (struct xrdp_wm*)mod->wm;
if (wm->mm->sesman_controlled)
{
return 1;
}
return libxrdp_send_to_channel(wm->session, channel_id, data, data_len,
total_data_len, flags);
}
|