VirtualBox

source: vbox/trunk/src/VBox/RDP/client-1.8.4/rdpdr.c@ 77807

Last change on this file since 77807 was 76779, checked in by vboxsync, 6 years ago

RDP: add client-1.8.4.
bugref:9356: Update rdesktop-vrdp to 1.8.4
client-1.8.4 is a Subversion copy of 1.8.3 with the upstream 1.8.3 to 1.8.4
patch applied and a couple of fixes and changes after review, namely:

  • Stopped disabling the new pointer data format for our build, as this is no

longer needed.

  • Adjusted some snprintf buffers to make GCC happy.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.5 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 * 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 struct stream packet = *s;
810
811#if WITH_DEBUG_RDP5
812 printf("--- rdpdr_process ---\n");
813 hexdump(s->p, s->end - s->p);
814#endif
815
816 in_uint16(s, component);
817 in_uint16(s, pakid);
818
819 if (component == RDPDR_CTYP_CORE)
820 {
821 switch (pakid)
822 {
823 case PAKID_CORE_DEVICE_IOREQUEST:
824 rdpdr_process_irp(s);
825 break;
826
827 case PAKID_CORE_SERVER_ANNOUNCE:
828 /* DR_CORE_SERVER_ANNOUNCE_REQ */
829 in_uint8s(s, 2); /* skip versionMajor */
830 in_uint16_le(s, vmin); /* VersionMinor */
831
832 in_uint32_le(s, g_client_id); /* ClientID */
833
834 /* g_client_id is sent back to server,
835 so lets check that we actually got
836 valid data from stream to prevent
837 that we leak back data to server */
838 if (!s_check(s))
839 {
840 rdp_protocol_error("rdpdr_process(), consume of g_client_id from stream did overrun", &packet);
841 }
842
843 /* The RDP client is responsibility to provide a random client id
844 if server version is < 12 */
845 if (vmin < 0x000c)
846 g_client_id = 0x815ed39d; /* IP address (use 127.0.0.1) 0x815ed39d */
847 g_epoch++;
848
849 rdpdr_send_client_announce_reply();
850 rdpdr_send_client_name_request();
851 break;
852
853 case PAKID_CORE_CLIENTID_CONFIRM:
854 rdpdr_send_client_device_list_announce();
855 break;
856
857 case PAKID_CORE_DEVICE_REPLY:
858 in_uint32(s, handle);
859#if WITH_DEBUG_RDP5
860 DEBUG(("RDPDR: Server connected to resource %d\n", handle));
861#endif
862 break;
863
864 case PAKID_CORE_SERVER_CAPABILITY:
865 rdpdr_send_client_capability_response();
866 break;
867
868 default:
869 unimpl("RDPDR pakid 0x%x of component 0x%x\n", pakid, component);
870 break;
871
872 }
873 }
874 else if (component == RDPDR_CTYP_PRN)
875 {
876 if (pakid == PAKID_PRN_CACHE_DATA)
877 printercache_process(s);
878 }
879 else
880 unimpl("RDPDR component 0x%x\n", component);
881}
882
883RD_BOOL
884rdpdr_init()
885{
886 rdpdr_channel =
887 channel_register("rdpdr",
888 CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_COMPRESS_RDP,
889 rdpdr_process);
890
891 return (rdpdr_channel != NULL);
892}
893
894/* Add file descriptors of pending io request to select() */
895void
896rdpdr_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv, RD_BOOL * timeout)
897{
898 uint32 select_timeout = 0; /* Timeout value to be used for select() (in millisecons). */
899 struct async_iorequest *iorq;
900 char c;
901
902 iorq = g_iorequest;
903 while (iorq != NULL)
904 {
905 if (iorq->fd != 0)
906 {
907 switch (iorq->major)
908 {
909 case IRP_MJ_READ:
910 /* Is this FD valid? FDs will
911 be invalid when
912 reconnecting. FIXME: Real
913 support for reconnects. */
914
915 FD_SET(iorq->fd, rfds);
916 *n = MAX(*n, iorq->fd);
917
918 /* Check if io request timeout is smaller than current (but not 0). */
919 if (iorq->timeout
920 && (select_timeout == 0
921 || iorq->timeout < select_timeout))
922 {
923 /* Set new timeout */
924 select_timeout = iorq->timeout;
925 g_min_timeout_fd = iorq->fd; /* Remember fd */
926 tv->tv_sec = select_timeout / 1000;
927 tv->tv_usec = (select_timeout % 1000) * 1000;
928 *timeout = True;
929 break;
930 }
931 if (iorq->itv_timeout && iorq->partial_len > 0
932 && (select_timeout == 0
933 || iorq->itv_timeout < select_timeout))
934 {
935 /* Set new timeout */
936 select_timeout = iorq->itv_timeout;
937 g_min_timeout_fd = iorq->fd; /* Remember fd */
938 tv->tv_sec = select_timeout / 1000;
939 tv->tv_usec = (select_timeout % 1000) * 1000;
940 *timeout = True;
941 break;
942 }
943 break;
944
945 case IRP_MJ_WRITE:
946 /* FD still valid? See above. */
947 if ((write(iorq->fd, &c, 0) != 0) && (errno == EBADF))
948 break;
949
950 FD_SET(iorq->fd, wfds);
951 *n = MAX(*n, iorq->fd);
952 break;
953
954 case IRP_MJ_DEVICE_CONTROL:
955 if (select_timeout > 5)
956 select_timeout = 5; /* serial event queue */
957 break;
958
959 }
960
961 }
962
963 iorq = iorq->next;
964 }
965}
966
967struct async_iorequest *
968rdpdr_remove_iorequest(struct async_iorequest *prev, struct async_iorequest *iorq)
969{
970 if (!iorq)
971 return NULL;
972
973 if (iorq->buffer)
974 xfree(iorq->buffer);
975 if (prev)
976 {
977 prev->next = iorq->next;
978 xfree(iorq);
979 iorq = prev->next;
980 }
981 else
982 {
983 /* Even if NULL */
984 g_iorequest = iorq->next;
985 xfree(iorq);
986 iorq = NULL;
987 }
988 return iorq;
989}
990
991/* Check if select() returned with one of the rdpdr file descriptors, and complete io if it did */
992static void
993_rdpdr_check_fds(fd_set * rfds, fd_set * wfds, RD_BOOL timed_out)
994{
995 RD_NTSTATUS status;
996 uint32 result = 0;
997 DEVICE_FNS *fns;
998 struct async_iorequest *iorq;
999 struct async_iorequest *prev;
1000 uint32 req_size = 0;
1001 uint32 buffer_len;
1002 struct stream out;
1003 uint8 *buffer = NULL;
1004
1005
1006 if (timed_out)
1007 {
1008 /* check serial iv_timeout */
1009
1010 iorq = g_iorequest;
1011 prev = NULL;
1012 while (iorq != NULL)
1013 {
1014 if (iorq->fd == g_min_timeout_fd)
1015 {
1016 if ((iorq->partial_len > 0) &&
1017 (g_rdpdr_device[iorq->device].device_type ==
1018 DEVICE_TYPE_SERIAL))
1019 {
1020
1021 /* iv_timeout between 2 chars, send partial_len */
1022 /*printf("RDPDR: IVT total %u bytes read of %u\n", iorq->partial_len, iorq->length); */
1023 rdpdr_send_completion(iorq->device,
1024 iorq->id, RD_STATUS_SUCCESS,
1025 iorq->partial_len,
1026 iorq->buffer, iorq->partial_len);
1027 iorq = rdpdr_remove_iorequest(prev, iorq);
1028 return;
1029 }
1030 else
1031 {
1032 break;
1033 }
1034
1035 }
1036 else
1037 {
1038 break;
1039 }
1040
1041
1042 prev = iorq;
1043 if (iorq)
1044 iorq = iorq->next;
1045
1046 }
1047
1048 rdpdr_abort_io(g_min_timeout_fd, 0, RD_STATUS_TIMEOUT);
1049 return;
1050 }
1051
1052 iorq = g_iorequest;
1053 prev = NULL;
1054 while (iorq != NULL)
1055 {
1056 if (iorq->fd != 0)
1057 {
1058 switch (iorq->major)
1059 {
1060 case IRP_MJ_READ:
1061 if (FD_ISSET(iorq->fd, rfds))
1062 {
1063 /* Read the data */
1064 fns = iorq->fns;
1065
1066 req_size =
1067 (iorq->length - iorq->partial_len) >
1068 8192 ? 8192 : (iorq->length -
1069 iorq->partial_len);
1070 /* never read larger chunks than 8k - chances are that it will block */
1071 status = fns->read(iorq->fd,
1072 iorq->buffer + iorq->partial_len,
1073 req_size, iorq->offset, &result);
1074
1075 if ((long) result > 0)
1076 {
1077 iorq->partial_len += result;
1078 iorq->offset += result;
1079 }
1080#if WITH_DEBUG_RDP5
1081 DEBUG(("RDPDR: %d bytes of data read\n", result));
1082#endif
1083 /* only delete link if all data has been transfered */
1084 /* or if result was 0 and status success - EOF */
1085 if ((iorq->partial_len == iorq->length) ||
1086 (result == 0))
1087 {
1088#if WITH_DEBUG_RDP5
1089 DEBUG(("RDPDR: AIO total %u bytes read of %u\n", iorq->partial_len, iorq->length));
1090#endif
1091 rdpdr_send_completion(iorq->device,
1092 iorq->id, status,
1093 iorq->partial_len,
1094 iorq->buffer,
1095 iorq->partial_len);
1096 iorq = rdpdr_remove_iorequest(prev, iorq);
1097 }
1098 }
1099 break;
1100 case IRP_MJ_WRITE:
1101 if (FD_ISSET(iorq->fd, wfds))
1102 {
1103 /* Write data. */
1104 fns = iorq->fns;
1105
1106 req_size =
1107 (iorq->length - iorq->partial_len) >
1108 8192 ? 8192 : (iorq->length -
1109 iorq->partial_len);
1110
1111 /* never write larger chunks than 8k - chances are that it will block */
1112 status = fns->write(iorq->fd,
1113 iorq->buffer +
1114 iorq->partial_len, req_size,
1115 iorq->offset, &result);
1116
1117 if ((long) result > 0)
1118 {
1119 iorq->partial_len += result;
1120 iorq->offset += result;
1121 }
1122
1123#if WITH_DEBUG_RDP5
1124 DEBUG(("RDPDR: %d bytes of data written\n",
1125 result));
1126#endif
1127 /* only delete link if all data has been transfered */
1128 /* or we couldn't write */
1129 if ((iorq->partial_len == iorq->length)
1130 || (result == 0))
1131 {
1132#if WITH_DEBUG_RDP5
1133 DEBUG(("RDPDR: AIO total %u bytes written of %u\n", iorq->partial_len, iorq->length));
1134#endif
1135 rdpdr_send_completion(iorq->device,
1136 iorq->id, status,
1137 iorq->partial_len,
1138 (uint8 *) "", 1);
1139
1140 iorq = rdpdr_remove_iorequest(prev, iorq);
1141 }
1142 }
1143 break;
1144 case IRP_MJ_DEVICE_CONTROL:
1145 if (serial_get_event(iorq->fd, &result))
1146 {
1147 buffer = (uint8 *) xrealloc((void *) buffer, 0x14);
1148 out.data = out.p = buffer;
1149 out.size = sizeof(buffer);
1150 out_uint32_le(&out, result);
1151 result = buffer_len = out.p - out.data;
1152 status = RD_STATUS_SUCCESS;
1153 rdpdr_send_completion(iorq->device, iorq->id,
1154 status, result, buffer,
1155 buffer_len);
1156 xfree(buffer);
1157 iorq = rdpdr_remove_iorequest(prev, iorq);
1158 }
1159
1160 break;
1161 }
1162
1163 }
1164 prev = iorq;
1165 if (iorq)
1166 iorq = iorq->next;
1167 }
1168
1169 /* Check notify */
1170 iorq = g_iorequest;
1171 prev = NULL;
1172 while (iorq != NULL)
1173 {
1174 if (iorq->fd != 0)
1175 {
1176 switch (iorq->major)
1177 {
1178
1179 case IRP_MJ_DIRECTORY_CONTROL:
1180 if (g_rdpdr_device[iorq->device].device_type ==
1181 DEVICE_TYPE_DISK)
1182 {
1183
1184 if (g_notify_stamp)
1185 {
1186 g_notify_stamp = False;
1187 status = disk_check_notify(iorq->fd);
1188 if (status != RD_STATUS_PENDING)
1189 {
1190 rdpdr_send_completion(iorq->device,
1191 iorq->id,
1192 status, 0,
1193 NULL, 0);
1194 iorq = rdpdr_remove_iorequest(prev,
1195 iorq);
1196 }
1197 }
1198 }
1199 break;
1200
1201
1202
1203 }
1204 }
1205
1206 prev = iorq;
1207 if (iorq)
1208 iorq = iorq->next;
1209 }
1210
1211}
1212
1213void
1214rdpdr_check_fds(fd_set * rfds, fd_set * wfds, RD_BOOL timed_out)
1215{
1216 fd_set dummy;
1217
1218
1219 FD_ZERO(&dummy);
1220
1221
1222 /* fist check event queue only,
1223 any serial wait event must be done before read block will be sent
1224 */
1225
1226 _rdpdr_check_fds(&dummy, &dummy, False);
1227 _rdpdr_check_fds(rfds, wfds, timed_out);
1228}
1229
1230
1231/* Abort a pending io request for a given handle and major */
1232RD_BOOL
1233rdpdr_abort_io(uint32 fd, uint32 major, RD_NTSTATUS status)
1234{
1235 uint32 result;
1236 struct async_iorequest *iorq;
1237 struct async_iorequest *prev;
1238
1239 iorq = g_iorequest;
1240 prev = NULL;
1241 while (iorq != NULL)
1242 {
1243 /* Only remove from table when major is not set, or when correct major is supplied.
1244 Abort read should not abort a write io request. */
1245 if ((iorq->fd == fd) && (major == 0 || iorq->major == major))
1246 {
1247 result = 0;
1248 rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8 *) "",
1249 1);
1250
1251 iorq = rdpdr_remove_iorequest(prev, iorq);
1252 return True;
1253 }
1254
1255 prev = iorq;
1256 iorq = iorq->next;
1257 }
1258
1259 return False;
1260}
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