VirtualBox

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

Last change on this file since 60669 was 55123, checked in by vboxsync, 10 years ago

rdesktop 1.8.3 modified for VBox

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