--- sourceforge.net/trunk/rdesktop/rdpdr.c 2004/02/23 12:07:30 614 +++ sourceforge.net/trunk/rdesktop/rdpdr.c 2004/04/15 20:11:19 650 @@ -1,15 +1,10 @@ #include #include #include +#include /* opendir, closedir, readdir */ #include #include "rdesktop.h" -#define IRP_MJ_CREATE 0x00 -#define IRP_MJ_CLOSE 0x02 -#define IRP_MJ_READ 0x03 -#define IRP_MJ_WRITE 0x04 -#define IRP_MJ_DEVICE_CONTROL 0x0e - #define IRP_MJ_CREATE 0x00 #define IRP_MJ_CLOSE 0x02 #define IRP_MJ_READ 0x03 @@ -19,6 +14,7 @@ #define IRP_MJ_QUERY_VOLUME_INFORMATION 0x0a #define IRP_MJ_DIRECTORY_CONTROL 0x0c #define IRP_MJ_DEVICE_CONTROL 0x0e +#define IRP_MJ_LOCK_CONTROL 0x11 #define IRP_MN_QUERY_DIRECTORY 0x01 #define IRP_MN_NOTIFY_CHANGE_DIRECTORY 0x02 @@ -28,6 +24,7 @@ extern DEVICE_FNS printer_fns; extern DEVICE_FNS parallel_fns; extern DEVICE_FNS disk_fns; +extern FILEINFO g_fileinfo[]; static VCHANNEL *rdpdr_channel; @@ -37,6 +34,7 @@ /* Table with information about rdpdr devices */ RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES]; +char * g_rdpdr_clientname = NULL; /* Used to store incoming io request, until they are ready to be completed */ /* using a linked list ensures that they are processed in the right order, */ @@ -79,6 +77,26 @@ } } +BOOL +rdpdr_handle_ok(int device, int handle) +{ + switch (g_rdpdr_device[device].device_type) + { + case DEVICE_TYPE_PARALLEL: + case DEVICE_TYPE_SERIAL: + case DEVICE_TYPE_PRINTER: + case DEVICE_TYPE_SCARD: + if (g_rdpdr_device[device].handle != handle) + return False; + break; + case DEVICE_TYPE_DISK: + if (g_fileinfo[handle].device_id != device) + return False; + break; + } + return True; +} + /* Add a new io request to the table containing pending io requests so it won't block rdesktop */ BOOL add_async_iorequest(uint32 device, uint32 file, uint32 id, uint32 major, uint32 length, @@ -146,8 +164,13 @@ rdpdr_send_name(void) { uint8 magic[4] = "rDNC"; - uint32 hostlen = (strlen(hostname) + 1) * 2; STREAM s; + uint32 hostlen; + + if (NULL == g_rdpdr_clientname) { + g_rdpdr_clientname = hostname; + } + hostlen = (strlen(g_rdpdr_clientname) + 1) * 2; s = channel_init(rdpdr_channel, 16 + hostlen); out_uint8a(s, magic, 4); @@ -155,7 +178,7 @@ out_uint16_le(s, 0x72); out_uint32(s, 0); out_uint32_le(s, hostlen); - rdp_out_unistr(s, hostname, hostlen - 2); + rdp_out_unistr(s, g_rdpdr_clientname, hostlen - 2); s_mark_end(s); channel_send(s, rdpdr_channel); } @@ -391,6 +414,12 @@ #if WITH_DEBUG_RDP5 DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset)); #endif + if (!rdpdr_handle_ok(device, file)) + { + status = STATUS_INVALID_HANDLE; + break; + } + if (rw_blocking) // Complete read immediately { buffer = (uint8 *) xrealloc((void *) buffer, length); @@ -438,6 +467,12 @@ #if WITH_DEBUG_RDP5 DEBUG(("RDPDR IRP Write (length: %d)\n", result)); #endif + if (!rdpdr_handle_ok(device, file)) + { + status = STATUS_INVALID_HANDLE; + break; + } + if (rw_blocking) // Complete immediately { status = fns->write(file, s->p, length, offset, &result); @@ -757,6 +792,29 @@ } } +struct async_iorequest * +rdpdr_remove_iorequest(struct async_iorequest *prev, struct async_iorequest *iorq) +{ + if (!iorq) + return NULL; + + if (iorq->buffer) + xfree(iorq->buffer); + if (prev) + { + prev->next = iorq->next; + xfree(iorq); + iorq = prev->next; + } + else + { + // Even if NULL + g_iorequest = iorq->next; + xfree(iorq); + iorq = NULL; + } + return iorq; +} /* Check if select() returned with one of the rdpdr file descriptors, and complete io if it did */ void @@ -797,9 +855,12 @@ status = fns->read(iorq->fd, iorq->buffer + iorq->partial_len, req_size, iorq->offset, &result); - iorq->partial_len += result; - iorq->offset += result; + if (result > 0) + { + iorq->partial_len += result; + iorq->offset += result; + } #if WITH_DEBUG_RDP5 DEBUG(("RDPDR: %d bytes of data read\n", result)); #endif @@ -811,28 +872,12 @@ #if WITH_DEBUG_RDP5 DEBUG(("RDPDR: AIO total %u bytes read of %u\n", iorq->partial_len, iorq->length)); #endif - /* send the data */ - status = STATUS_SUCCESS; rdpdr_send_completion(iorq->device, iorq->id, status, iorq->partial_len, iorq->buffer, iorq->partial_len); - xfree(iorq->buffer); - iorq->fd = 0; - if (prev != NULL) - { - prev->next = iorq->next; - xfree(iorq); - iorq = prev->next; - } - else - { - // Even if NULL - g_iorequest = iorq->next; - xfree(iorq); - iorq = NULL; - } + iorq = rdpdr_remove_iorequest(prev, iorq); } } break; @@ -852,8 +897,13 @@ iorq->buffer + iorq->partial_len, req_size, iorq->offset, &result); - iorq->partial_len += result; - iorq->offset += result; + + if (result > 0) + { + iorq->partial_len += result; + iorq->offset += result; + } + #if WITH_DEBUG_RDP5 DEBUG(("RDPDR: %d bytes of data written\n", result)); @@ -866,28 +916,12 @@ #if WITH_DEBUG_RDP5 DEBUG(("RDPDR: AIO total %u bytes written of %u\n", iorq->partial_len, iorq->length)); #endif - /* send a status success */ - status = STATUS_SUCCESS; rdpdr_send_completion(iorq->device, iorq->id, status, iorq->partial_len, (uint8 *) "", 1); - xfree(iorq->buffer); - iorq->fd = 0; - if (prev != NULL) - { - prev->next = iorq->next; - xfree(iorq); - iorq = prev->next; - } - else - { - // Even if NULL - g_iorequest = iorq->next; - xfree(iorq); - iorq = NULL; - } + iorq = rdpdr_remove_iorequest(prev, iorq); } } break; @@ -920,19 +954,8 @@ result = 0; rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8 *) "", 1); - xfree(iorq->buffer); - iorq->fd = 0; - if (prev != NULL) - { - prev->next = iorq->next; - xfree(iorq); - } - else - { - // Even if NULL - g_iorequest = iorq->next; - xfree(iorq); - } + + iorq = rdpdr_remove_iorequest(prev, iorq); return True; }