forked from rdesktop/rdesktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdpdr.c
1240 lines (1044 loc) · 27.7 KB
/
rdpdr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- c-basic-offset: 8 -*-
rdesktop: A Remote Desktop Protocol client.
Copyright (C) Matthew Chapman <matthewc.unsw.edu.au> 1999-2008
Copyright 2004-2011 Peter Astrand <[email protected]> for Cendio AB
Copyright 2010-2014 Henrik Andersson <[email protected]> for Cendio AB
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 3 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, see <https://www.gnu.org/licenses/>.
*/
/*
Here are some resources, for your IRP hacking pleasure:
https://cvs.sourceforge.net/viewcvs.py/mingw/w32api/include/ddk/winddk.h?view=markup
https://win32.mvps.org/ntfs/streams.cpp
https://www.acc.umu.se/~bosse/ntifs.h
https://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/NT%20Objects/File/
https://us1.samba.org/samba/ftp/specs/smb-nt01.txt
https://www.osronline.com/
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <dirent.h> /* opendir, closedir, readdir */
#include <time.h>
#include <errno.h>
#include "rdesktop.h"
#define IRP_MJ_CREATE 0x00
#define IRP_MJ_CLOSE 0x02
#define IRP_MJ_READ 0x03
#define IRP_MJ_WRITE 0x04
#define IRP_MJ_QUERY_INFORMATION 0x05
#define IRP_MJ_SET_INFORMATION 0x06
#define IRP_MJ_QUERY_VOLUME_INFORMATION 0x0a
#define IRP_MJ_DIRECTORY_CONTROL 0x0c
#define IRP_MJ_DEVICE_CONTROL 0x0e
#define IRP_MJ_LOCK_CONTROL 0x11
#define IRP_MN_QUERY_DIRECTORY 0x01
#define IRP_MN_NOTIFY_CHANGE_DIRECTORY 0x02
extern char g_hostname[16];
extern DEVICE_FNS serial_fns;
extern DEVICE_FNS printer_fns;
extern DEVICE_FNS parallel_fns;
extern DEVICE_FNS disk_fns;
#ifdef WITH_SCARD
extern DEVICE_FNS scard_fns;
#endif
extern FILEINFO g_fileinfo[];
extern RD_BOOL g_notify_stamp;
static VCHANNEL *rdpdr_channel;
static uint32 g_epoch;
/* If select() times out, the request for the device with handle g_min_timeout_fd is aborted */
RD_NTHANDLE g_min_timeout_fd;
uint32 g_num_devices;
uint32 g_client_id;
/* Table with information about rdpdr devices */
RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES];
char *g_rdpdr_clientname = NULL;
/* Used to store incoming io request, until they are ready to be completed */
/* using a linked list ensures that they are processed in the right order, */
/* if multiple ios are being done on the same fd */
struct async_iorequest
{
uint32 fd, major, minor, offset, device, id, length, partial_len;
long timeout, /* Total timeout */
itv_timeout; /* Interval timeout (between serial characters) */
uint8 *buffer;
DEVICE_FNS *fns;
struct async_iorequest *next; /* next element in list */
};
struct async_iorequest *g_iorequest;
/* Return device_id for a given handle */
int
get_device_index(RD_NTHANDLE handle)
{
int i;
for (i = 0; i < RDPDR_MAX_DEVICES; i++)
{
if (g_rdpdr_device[i].handle == handle)
return i;
}
return -1;
}
/* Converts a windows path to a unix path */
void
convert_to_unix_filename(char *filename)
{
char *p;
while ((p = strchr(filename, '\\')))
{
*p = '/';
}
}
static RD_BOOL
rdpdr_handle_ok(int device, int handle)
{
switch (g_rdpdr_device[device].device_type)
{
case DEVICE_TYPE_PARALLEL:
case DEVICE_TYPE_SERIAL:
case DEVICE_TYPE_PRINTER:
case DEVICE_TYPE_SCARD:
if (g_rdpdr_device[device].handle != handle)
return False;
break;
case DEVICE_TYPE_DISK:
if (g_fileinfo[handle].device_id != device)
return False;
break;
}
return True;
}
/* Add a new io request to the table containing pending io requests so it won't block rdesktop */
static RD_BOOL
add_async_iorequest(uint32 device, uint32 file, uint32 id, uint32 major, uint32 length,
DEVICE_FNS * fns, uint32 total_timeout, uint32 interval_timeout, uint8 * buffer,
uint32 offset)
{
struct async_iorequest *iorq;
if (g_iorequest == NULL)
{
g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
if (!g_iorequest)
return False;
g_iorequest->fd = 0;
g_iorequest->next = NULL;
}
iorq = g_iorequest;
while (iorq->fd != 0)
{
/* create new element if needed */
if (iorq->next == NULL)
{
iorq->next =
(struct async_iorequest *) xmalloc(sizeof(struct async_iorequest));
if (!iorq->next)
return False;
iorq->next->fd = 0;
iorq->next->next = NULL;
}
iorq = iorq->next;
}
iorq->device = device;
iorq->fd = file;
iorq->id = id;
iorq->major = major;
iorq->length = length;
iorq->partial_len = 0;
iorq->fns = fns;
iorq->timeout = total_timeout;
iorq->itv_timeout = interval_timeout;
iorq->buffer = buffer;
iorq->offset = offset;
return True;
}
static void
rdpdr_send_client_announce_reply(void)
{
/* DR_CORE_CLIENT_ANNOUNCE_RSP */
STREAM s;
s = channel_init(rdpdr_channel, 12);
out_uint16_le(s, RDPDR_CTYP_CORE);
out_uint16_le(s, PAKID_CORE_CLIENTID_CONFIRM);
out_uint16_le(s, 1); /* VersionMajor, MUST be set to 0x1 */
out_uint16_le(s, 5); /* VersionMinor */
out_uint32_be(s, g_client_id); /* ClientID */
s_mark_end(s);
channel_send(s, rdpdr_channel);
}
static void
rdpdr_send_client_name_request(void)
{
/* DR_CORE_CLIENT_NAME_REQ */
STREAM s;
uint32 hostlen;
if (NULL == g_rdpdr_clientname)
{
g_rdpdr_clientname = g_hostname;
}
hostlen = (strlen(g_rdpdr_clientname) + 1) * 2;
s = channel_init(rdpdr_channel, 16 + hostlen);
out_uint16_le(s, RDPDR_CTYP_CORE);
out_uint16_le(s, PAKID_CORE_CLIENT_NAME);
out_uint32_le(s, 1); /* UnicodeFlag */
out_uint32_le(s, 0); /* CodePage */
out_uint32_le(s, hostlen); /* ComputerNameLen */
rdp_out_unistr(s, g_rdpdr_clientname, hostlen - 2);
s_mark_end(s);
channel_send(s, rdpdr_channel);
}
/* Returns the size of the payload of the announce packet */
static int
announcedata_size()
{
int size, i;
PRINTER *printerinfo;
size = 8; /* static announce size */
size += g_num_devices * 0x14;
for (i = 0; i < g_num_devices; i++)
{
if (g_rdpdr_device[i].device_type == DEVICE_TYPE_PRINTER)
{
printerinfo = (PRINTER *) g_rdpdr_device[i].pdevice_data;
printerinfo->bloblen =
printercache_load_blob(printerinfo->printer, &(printerinfo->blob));
size += 0x18;
size += 2 * strlen(printerinfo->driver) + 2;
size += 2 * strlen(printerinfo->printer) + 2;
size += printerinfo->bloblen;
}
}
return size;
}
static void
rdpdr_send_client_device_list_announce(void)
{
/* DR_CORE_CLIENT_ANNOUNCE_RSP */
uint32 driverlen, printerlen, bloblen;
int i;
STREAM s;
PRINTER *printerinfo;
s = channel_init(rdpdr_channel, announcedata_size());
out_uint16_le(s, RDPDR_CTYP_CORE);
out_uint16_le(s, PAKID_CORE_DEVICE_LIST_ANNOUNCE);
out_uint32_le(s, g_num_devices);
for (i = 0; i < g_num_devices; i++)
{
out_uint32_le(s, g_rdpdr_device[i].device_type);
out_uint32_le(s, i); /* RDP Device ID */
/* Is it possible to use share names longer than 8 chars?
/astrand */
out_uint8p(s, g_rdpdr_device[i].name, 8);
switch (g_rdpdr_device[i].device_type)
{
case DEVICE_TYPE_PRINTER:
printerinfo = (PRINTER *) g_rdpdr_device[i].pdevice_data;
driverlen = 2 * strlen(printerinfo->driver) + 2;
printerlen = 2 * strlen(printerinfo->printer) + 2;
bloblen = printerinfo->bloblen;
out_uint32_le(s, 24 + driverlen + printerlen + bloblen); /* length of extra info */
out_uint32_le(s, printerinfo->default_printer ? 2 : 0);
out_uint8s(s, 8); /* unknown */
out_uint32_le(s, driverlen);
out_uint32_le(s, printerlen);
out_uint32_le(s, bloblen);
rdp_out_unistr(s, printerinfo->driver, driverlen - 2);
rdp_out_unistr(s, printerinfo->printer, printerlen - 2);
out_uint8a(s, printerinfo->blob, bloblen);
if (printerinfo->blob)
xfree(printerinfo->blob); /* Blob is sent twice if reconnecting */
break;
default:
out_uint32(s, 0);
}
}
s_mark_end(s);
channel_send(s, rdpdr_channel);
}
void
rdpdr_send_completion(uint32 device, uint32 id, uint32 status, uint32 result, uint8 * buffer,
uint32 length)
{
STREAM s;
#ifdef WITH_SCARD
scard_lock(SCARD_LOCK_RDPDR);
#endif
s = channel_init(rdpdr_channel, 20 + length);
out_uint16_le(s, RDPDR_CTYP_CORE);
out_uint16_le(s, PAKID_CORE_DEVICE_IOCOMPLETION);
out_uint32_le(s, device);
out_uint32_le(s, id);
out_uint32_le(s, status);
out_uint32_le(s, result);
out_uint8p(s, buffer, length);
s_mark_end(s);
/* JIF */
#ifdef WITH_DEBUG_RDP5
printf("--> rdpdr_send_completion\n");
/* hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8); */
#endif
channel_send(s, rdpdr_channel);
#ifdef WITH_SCARD
scard_unlock(SCARD_LOCK_RDPDR);
#endif
}
static void
rdpdr_process_irp(STREAM s)
{
uint32 result = 0,
length = 0,
desired_access = 0,
request,
file,
info_level,
buffer_len,
id,
major,
minor,
device,
offset,
bytes_in,
bytes_out,
error_mode,
share_mode, disposition, total_timeout, interval_timeout, flags_and_attributes = 0;
char *filename;
uint32 filename_len;
uint8 *buffer, *pst_buf;
struct stream out;
DEVICE_FNS *fns;
RD_BOOL rw_blocking = True;
RD_NTSTATUS status = RD_STATUS_INVALID_DEVICE_REQUEST;
in_uint32_le(s, device);
in_uint32_le(s, file);
in_uint32_le(s, id);
in_uint32_le(s, major);
in_uint32_le(s, minor);
filename = NULL;
buffer_len = 0;
buffer = (uint8 *) xmalloc(1024);
buffer[0] = 0;
if (device >= RDPDR_MAX_DEVICES)
{
error("invalid irp device 0x%lx file 0x%lx id 0x%lx major 0x%lx minor 0x%lx\n",
device, file, id, major, minor);
xfree(buffer);
return;
}
switch (g_rdpdr_device[device].device_type)
{
case DEVICE_TYPE_SERIAL:
fns = &serial_fns;
rw_blocking = False;
break;
case DEVICE_TYPE_PARALLEL:
fns = ¶llel_fns;
rw_blocking = False;
break;
case DEVICE_TYPE_PRINTER:
fns = &printer_fns;
break;
case DEVICE_TYPE_DISK:
fns = &disk_fns;
rw_blocking = False;
break;
case DEVICE_TYPE_SCARD:
#ifdef WITH_SCARD
fns = &scard_fns;
rw_blocking = False;
break;
#endif
default:
error("IRP for bad device %ld\n", device);
xfree(buffer);
return;
}
switch (major)
{
case IRP_MJ_CREATE:
in_uint32_be(s, desired_access);
in_uint8s(s, 0x08); /* unknown */
in_uint32_le(s, error_mode);
in_uint32_le(s, share_mode);
in_uint32_le(s, disposition);
in_uint32_le(s, flags_and_attributes);
in_uint32_le(s, length);
if (length && (length / 2) < 256)
{
rdp_in_unistr(s, length, &filename, &filename_len);
if (filename)
convert_to_unix_filename(filename);
}
if (!fns->create)
{
status = RD_STATUS_NOT_SUPPORTED;
free(filename);
break;
}
status = fns->create(device, desired_access, share_mode, disposition,
flags_and_attributes, filename, &result);
free(filename);
buffer_len = 1;
break;
case IRP_MJ_CLOSE:
if (!fns->close)
{
status = RD_STATUS_NOT_SUPPORTED;
break;
}
status = fns->close(file);
break;
case IRP_MJ_READ:
if (!fns->read)
{
status = RD_STATUS_NOT_SUPPORTED;
break;
}
in_uint32_le(s, length);
in_uint32_le(s, offset);
#if WITH_DEBUG_RDP5
DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset));
#endif
if (!rdpdr_handle_ok(device, file))
{
status = RD_STATUS_INVALID_HANDLE;
break;
}
if (rw_blocking) /* Complete read immediately */
{
buffer = (uint8 *) xrealloc((void *) buffer, length);
if (!buffer)
{
status = RD_STATUS_CANCELLED;
break;
}
status = fns->read(file, buffer, length, offset, &result);
buffer_len = result;
break;
}
/* Add request to table */
pst_buf = (uint8 *) xmalloc(length);
if (!pst_buf)
{
status = RD_STATUS_CANCELLED;
break;
}
serial_get_timeout(file, length, &total_timeout, &interval_timeout);
if (add_async_iorequest
(device, file, id, major, length, fns, total_timeout, interval_timeout,
pst_buf, offset))
{
status = RD_STATUS_PENDING;
break;
}
status = RD_STATUS_CANCELLED;
break;
case IRP_MJ_WRITE:
buffer_len = 1;
if (!fns->write)
{
status = RD_STATUS_NOT_SUPPORTED;
break;
}
in_uint32_le(s, length);
in_uint32_le(s, offset);
in_uint8s(s, 0x18);
#if WITH_DEBUG_RDP5
DEBUG(("RDPDR IRP Write (length: %d)\n", result));
#endif
if (!rdpdr_handle_ok(device, file))
{
status = RD_STATUS_INVALID_HANDLE;
break;
}
if (rw_blocking) /* Complete immediately */
{
status = fns->write(file, s->p, length, offset, &result);
break;
}
/* Add to table */
pst_buf = (uint8 *) xmalloc(length);
if (!pst_buf)
{
status = RD_STATUS_CANCELLED;
break;
}
in_uint8a(s, pst_buf, length);
if (add_async_iorequest
(device, file, id, major, length, fns, 0, 0, pst_buf, offset))
{
status = RD_STATUS_PENDING;
break;
}
status = RD_STATUS_CANCELLED;
break;
case IRP_MJ_QUERY_INFORMATION:
if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
{
status = RD_STATUS_INVALID_HANDLE;
break;
}
in_uint32_le(s, info_level);
out.data = out.p = buffer;
out.size = sizeof(buffer);
status = disk_query_information(file, info_level, &out);
result = buffer_len = out.p - out.data;
break;
case IRP_MJ_SET_INFORMATION:
if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
{
status = RD_STATUS_INVALID_HANDLE;
break;
}
in_uint32_le(s, info_level);
out.data = out.p = buffer;
out.size = sizeof(buffer);
status = disk_set_information(file, info_level, s, &out);
result = buffer_len = out.p - out.data;
break;
case IRP_MJ_QUERY_VOLUME_INFORMATION:
if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
{
status = RD_STATUS_INVALID_HANDLE;
break;
}
in_uint32_le(s, info_level);
out.data = out.p = buffer;
out.size = sizeof(buffer);
status = disk_query_volume_information(file, info_level, &out);
result = buffer_len = out.p - out.data;
break;
case IRP_MJ_DIRECTORY_CONTROL:
if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
{
status = RD_STATUS_INVALID_HANDLE;
break;
}
switch (minor)
{
case IRP_MN_QUERY_DIRECTORY:
in_uint32_le(s, info_level);
in_uint8s(s, 1);
in_uint32_le(s, length);
in_uint8s(s, 0x17);
if (length && length < 2 * 255)
{
rdp_in_unistr(s, length, &filename, &filename_len);
if (filename)
convert_to_unix_filename(filename);
}
out.data = out.p = buffer;
out.size = sizeof(buffer);
status = disk_query_directory(file, info_level, filename,
&out);
result = buffer_len = out.p - out.data;
if (!buffer_len)
buffer_len++;
free(filename);
break;
case IRP_MN_NOTIFY_CHANGE_DIRECTORY:
/* JIF
unimpl("IRP major=0x%x minor=0x%x: IRP_MN_NOTIFY_CHANGE_DIRECTORY\n", major, minor); */
in_uint32_le(s, info_level); /* notify mask */
status = disk_create_notify(file, info_level);
result = 0;
if (status == RD_STATUS_PENDING)
add_async_iorequest(device, file, id, major, length,
fns, 0, 0, NULL, 0);
break;
default:
status = RD_STATUS_INVALID_PARAMETER;
/* JIF */
unimpl("IRP major=0x%x minor=0x%x\n", major, minor);
}
break;
case IRP_MJ_DEVICE_CONTROL:
if (!fns->device_control)
{
status = RD_STATUS_NOT_SUPPORTED;
break;
}
in_uint32_le(s, bytes_out);
in_uint32_le(s, bytes_in);
in_uint32_le(s, request);
in_uint8s(s, 0x14);
buffer = (uint8 *) xrealloc((void *) buffer, bytes_out + 0x14);
if (!buffer)
{
status = RD_STATUS_CANCELLED;
break;
}
out.data = out.p = buffer;
out.size = sizeof(buffer);
#ifdef WITH_SCARD
scardSetInfo(g_epoch, device, id, bytes_out + 0x14);
#endif
status = fns->device_control(file, request, s, &out);
result = buffer_len = out.p - out.data;
/* Serial SERIAL_WAIT_ON_MASK */
if (status == RD_STATUS_PENDING)
{
if (add_async_iorequest
(device, file, id, major, length, fns, 0, 0, NULL, 0))
{
status = RD_STATUS_PENDING;
break;
}
}
#ifdef WITH_SCARD
else if (status == (RD_STATUS_PENDING | 0xC0000000))
status = RD_STATUS_PENDING;
#endif
break;
case IRP_MJ_LOCK_CONTROL:
if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK)
{
status = RD_STATUS_INVALID_HANDLE;
break;
}
in_uint32_le(s, info_level);
out.data = out.p = buffer;
out.size = sizeof(buffer);
/* FIXME: Perhaps consider actually *do*
something here :-) */
status = RD_STATUS_SUCCESS;
result = buffer_len = out.p - out.data;
break;
default:
unimpl("IRP major=0x%x minor=0x%x\n", major, minor);
break;
}
if (status != RD_STATUS_PENDING)
{
rdpdr_send_completion(device, id, status, result, buffer, buffer_len);
}
if (buffer)
xfree(buffer);
buffer = NULL;
}
static void
rdpdr_send_client_capability_response(void)
{
/* DR_CORE_CAPABILITY_RSP */
STREAM s;
s = channel_init(rdpdr_channel, 0x50);
out_uint16_le(s, RDPDR_CTYP_CORE);
out_uint16_le(s, PAKID_CORE_CLIENT_CAPABILITY);
out_uint32_le(s, 5); /* count */
out_uint16_le(s, 1); /* first */
out_uint16_le(s, 0x28); /* length */
out_uint32_le(s, 1);
out_uint32_le(s, 2);
out_uint16_le(s, 2);
out_uint16_le(s, 5);
out_uint16_le(s, 1);
out_uint16_le(s, 5);
out_uint16_le(s, 0xFFFF);
out_uint16_le(s, 0);
out_uint32_le(s, 0);
out_uint32_le(s, 3);
out_uint32_le(s, 0);
out_uint32_le(s, 0);
out_uint16_le(s, 2); /* second */
out_uint16_le(s, 8); /* length */
out_uint32_le(s, 1);
out_uint16_le(s, 3); /* third */
out_uint16_le(s, 8); /* length */
out_uint32_le(s, 1);
out_uint16_le(s, 4); /* fourth */
out_uint16_le(s, 8); /* length */
out_uint32_le(s, 1);
out_uint16_le(s, 5); /* fifth */
out_uint16_le(s, 8); /* length */
out_uint32_le(s, 1);
s_mark_end(s);
channel_send(s, rdpdr_channel);
}
static void
rdpdr_process(STREAM s)
{
uint32 handle;
uint16 vmin;
uint16 component;
uint16 pakid;
#if WITH_DEBUG_RDP5
printf("--- rdpdr_process ---\n");
hexdump(s->p, s->end - s->p);
#endif
in_uint16(s, component);
in_uint16(s, pakid);
if (component == RDPDR_CTYP_CORE)
{
switch (pakid)
{
case PAKID_CORE_DEVICE_IOREQUEST:
rdpdr_process_irp(s);
break;
case PAKID_CORE_SERVER_ANNOUNCE:
/* DR_CORE_SERVER_ANNOUNCE_REQ */
in_uint8s(s, 2); /* skip versionMajor */
in_uint16_le(s, vmin); /* VersionMinor */
in_uint32_le(s, g_client_id); /* ClientID */
/* The RDP client is responsibility to provide a random client id
if server version is < 12 */
if (vmin < 0x000c)
g_client_id = 0x815ed39d; /* IP address (use 127.0.0.1) 0x815ed39d */
g_epoch++;
rdpdr_send_client_announce_reply();
rdpdr_send_client_name_request();
break;
case PAKID_CORE_CLIENTID_CONFIRM:
rdpdr_send_client_device_list_announce();
break;
case PAKID_CORE_DEVICE_REPLY:
in_uint32(s, handle);
#if WITH_DEBUG_RDP5
DEBUG(("RDPDR: Server connected to resource %d\n", handle));
#endif
break;
case PAKID_CORE_SERVER_CAPABILITY:
rdpdr_send_client_capability_response();
break;
default:
unimpl("RDPDR pakid 0x%x of component 0x%x\n", pakid, component);
break;
}
}
else if (component == RDPDR_CTYP_PRN)
{
if (pakid == PAKID_PRN_CACHE_DATA)
printercache_process(s);
}
else
unimpl("RDPDR component 0x%x\n", component);
}
RD_BOOL
rdpdr_init()
{
rdpdr_channel =
channel_register("rdpdr",
CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_COMPRESS_RDP,
rdpdr_process);
return (rdpdr_channel != NULL);
}
/* Add file descriptors of pending io request to select() */
void
rdpdr_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv, RD_BOOL * timeout)
{
uint32 select_timeout = 0; /* Timeout value to be used for select() (in millisecons). */
struct async_iorequest *iorq;
char c;
iorq = g_iorequest;
while (iorq != NULL)
{
if (iorq->fd != 0)
{
switch (iorq->major)
{
case IRP_MJ_READ:
/* Is this FD valid? FDs will
be invalid when
reconnecting. FIXME: Real
support for reconnects. */
FD_SET(iorq->fd, rfds);
*n = MAX(*n, iorq->fd);
/* Check if io request timeout is smaller than current (but not 0). */
if (iorq->timeout
&& (select_timeout == 0
|| iorq->timeout < select_timeout))
{
/* Set new timeout */
select_timeout = iorq->timeout;
g_min_timeout_fd = iorq->fd; /* Remember fd */
tv->tv_sec = select_timeout / 1000;
tv->tv_usec = (select_timeout % 1000) * 1000;
*timeout = True;
break;
}
if (iorq->itv_timeout && iorq->partial_len > 0
&& (select_timeout == 0
|| iorq->itv_timeout < select_timeout))
{
/* Set new timeout */
select_timeout = iorq->itv_timeout;
g_min_timeout_fd = iorq->fd; /* Remember fd */
tv->tv_sec = select_timeout / 1000;
tv->tv_usec = (select_timeout % 1000) * 1000;
*timeout = True;
break;
}
break;
case IRP_MJ_WRITE:
/* FD still valid? See above. */
if ((write(iorq->fd, &c, 0) != 0) && (errno == EBADF))
break;
FD_SET(iorq->fd, wfds);
*n = MAX(*n, iorq->fd);
break;
case IRP_MJ_DEVICE_CONTROL:
if (select_timeout > 5)
select_timeout = 5; /* serial event queue */
break;
}
}
iorq = iorq->next;
}
}
struct async_iorequest *
rdpdr_remove_iorequest(struct async_iorequest *prev, struct async_iorequest *iorq)
{
if (!iorq)
return NULL;
if (iorq->buffer)
xfree(iorq->buffer);
if (prev)
{
prev->next = iorq->next;
xfree(iorq);
iorq = prev->next;
}
else
{
/* Even if NULL */
g_iorequest = iorq->next;
xfree(iorq);
iorq = NULL;
}
return iorq;
}
/* Check if select() returned with one of the rdpdr file descriptors, and complete io if it did */
static void
_rdpdr_check_fds(fd_set * rfds, fd_set * wfds, RD_BOOL timed_out)
{
RD_NTSTATUS status;
uint32 result = 0;
DEVICE_FNS *fns;
struct async_iorequest *iorq;
struct async_iorequest *prev;
uint32 req_size = 0;
uint32 buffer_len;
struct stream out;
uint8 *buffer = NULL;
if (timed_out)
{
/* check serial iv_timeout */
iorq = g_iorequest;
prev = NULL;
while (iorq != NULL)
{
if (iorq->fd == g_min_timeout_fd)
{
if ((iorq->partial_len > 0) &&
(g_rdpdr_device[iorq->device].device_type ==
DEVICE_TYPE_SERIAL))
{