VirtualBox

source: vbox/trunk/src/VBox/RDP/client-1.8.3/rdpdr.c@ 55121

Last change on this file since 55121 was 55121, checked in by vboxsync, 9 years ago

rdesktop 1.8.3 unmodified

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette