/[rdesktop]/sourceforge.net/trunk/rdesktop/rdpdr.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /sourceforge.net/trunk/rdesktop/rdpdr.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1319 - (hide annotations)
Thu Nov 2 20:48:28 2006 UTC (17 years, 7 months ago) by stargo
File MIME type: text/plain
File size: 27344 byte(s)
clean up smartcard debugging

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

  ViewVC Help
Powered by ViewVC 1.1.26