/[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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.435  
changed lines
  Added in v.1415

  ViewVC Help
Powered by ViewVC 1.1.26