--- sourceforge.net/trunk/rdesktop/rdpdr.c 2004/02/03 14:02:59 595 +++ sourceforge.net/trunk/rdesktop/rdpdr.c 2004/11/03 13:56:52 795 @@ -1,14 +1,46 @@ +/* -*- c-basic-offset: 8 -*- + rdesktop: A Remote Desktop Protocol client. + Copyright (C) Matthew Chapman 1999-2004 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +/* + Here are some resources, for your IRP hacking pleasure: + + http://cvs.sourceforge.net/viewcvs.py/mingw/w32api/include/ddk/winddk.h?view=markup + + http://win32.mvps.org/ntfs/streams.cpp + + http://www.acc.umu.se/~bosse/ntifs.h + + http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/NT%20Objects/File/ + + http://us1.samba.org/samba/ftp/specs/smb-nt01.txt + + http://www.osronline.com/ +*/ + #include #include +#include +#include /* opendir, closedir, readdir */ #include +#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 @@ -18,24 +50,28 @@ #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 -extern char hostname[16]; +extern char g_hostname[16]; extern DEVICE_FNS serial_fns; extern DEVICE_FNS printer_fns; extern DEVICE_FNS parallel_fns; extern DEVICE_FNS disk_fns; +extern FILEINFO g_fileinfo[]; +extern BOOL g_notify_stamp; static VCHANNEL *rdpdr_channel; /* If select() times out, the request for the device with handle g_min_timeout_fd is aborted */ -HANDLE g_min_timeout_fd; +NTHANDLE g_min_timeout_fd; uint32 g_num_devices; /* 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, */ @@ -55,7 +91,7 @@ /* Return device_id for a given handle */ int -get_device_index(HANDLE handle) +get_device_index(NTHANDLE handle) { int i; for (i = 0; i < RDPDR_MAX_DEVICES; i++) @@ -78,16 +114,39 @@ } } +static 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 +static BOOL add_async_iorequest(uint32 device, uint32 file, uint32 id, uint32 major, uint32 length, - DEVICE_FNS * fns, long total_timeout, long interval_timeout, uint8 * buffer) + DEVICE_FNS * fns, uint32 total_timeout, uint32 interval_timeout, uint8 * buffer, + uint32 offset) { struct async_iorequest *iorq; if (g_iorequest == NULL) { g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest)); + if (!g_iorequest) + return False; g_iorequest->fd = 0; g_iorequest->next = NULL; } @@ -101,6 +160,8 @@ { iorq->next = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest)); + if (!iorq->next) + return False; iorq->next->fd = 0; iorq->next->next = NULL; } @@ -116,10 +177,11 @@ iorq->timeout = total_timeout; iorq->itv_timeout = interval_timeout; iorq->buffer = buffer; + iorq->offset = offset; return True; } -void +static void rdpdr_send_connect(void) { uint8 magic[4] = "rDCC"; @@ -135,12 +197,18 @@ } -void +static void 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 = g_hostname; + } + hostlen = (strlen(g_rdpdr_clientname) + 1) * 2; s = channel_init(rdpdr_channel, 16 + hostlen); out_uint8a(s, magic, 4); @@ -148,13 +216,13 @@ 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); } /* Returns the size of the payload of the announce packet */ -int +static int announcedata_size() { int size, i; @@ -181,7 +249,7 @@ return size; } -void +static void rdpdr_send_available(void) { @@ -199,6 +267,8 @@ { out_uint32_le(s, g_rdpdr_device[i].device_type); out_uint32_le(s, i); /* RDP Device ID */ + /* Is it possible to use share names longer than 8 chars? + /astrand */ out_uint8p(s, g_rdpdr_device[i].name, 8); switch (g_rdpdr_device[i].device_type) @@ -220,7 +290,8 @@ rdp_out_unistr(s, printerinfo->printer, printerlen - 2); out_uint8a(s, printerinfo->blob, bloblen); - xfree(printerinfo->blob); /* Blob is sent twice if reconnecting */ + if (printerinfo->blob) + xfree(printerinfo->blob); /* Blob is sent twice if reconnecting */ break; default: out_uint32(s, 0); @@ -238,7 +309,7 @@ channel_send(s, rdpdr_channel); } -void +static void rdpdr_send_completion(uint32 device, uint32 id, uint32 status, uint32 result, uint8 * buffer, uint32 length) { @@ -253,8 +324,11 @@ out_uint32_le(s, result); out_uint8p(s, buffer, length); s_mark_end(s); - /* JIF - hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8); */ + /* JIF */ +#ifdef WITH_DEBUG_RDP5 + printf("--> rdpdr_send_completion\n"); + //hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8); +#endif channel_send(s, rdpdr_channel); } @@ -383,9 +457,20 @@ #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); + if (!buffer) + { + status = STATUS_CANCELLED; + break; + } status = fns->read(file, buffer, length, offset, &result); buffer_len = result; break; @@ -393,10 +478,15 @@ // Add request to table pst_buf = (uint8 *) xmalloc(length); + if (!pst_buf) + { + status = STATUS_CANCELLED; + break; + } serial_get_timeout(file, length, &total_timeout, &interval_timeout); if (add_async_iorequest (device, file, id, major, length, fns, total_timeout, interval_timeout, - pst_buf)) + pst_buf, offset)) { status = STATUS_PENDING; break; @@ -420,6 +510,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); @@ -428,10 +524,16 @@ // Add to table pst_buf = (uint8 *) xmalloc(length); + if (!pst_buf) + { + status = STATUS_CANCELLED; + break; + } + in_uint8a(s, pst_buf, length); if (add_async_iorequest - (device, file, id, major, length, fns, 0, 0, pst_buf)) + (device, file, id, major, length, fns, 0, 0, pst_buf, offset)) { status = STATUS_PENDING; break; @@ -525,15 +627,25 @@ case IRP_MN_NOTIFY_CHANGE_DIRECTORY: /* JIF - unimpl("IRP major=0x%x minor=0x%x: IRP_MN_NOTIFY_CHANGE_DIRECTORY\n", major, minor); */ - status = STATUS_PENDING; // Don't send completion packet + unimpl("IRP major=0x%x minor=0x%x: IRP_MN_NOTIFY_CHANGE_DIRECTORY\n", major, minor); */ + + in_uint32_le(s, info_level); // notify mask + + g_notify_stamp = True; + + status = disk_create_notify(file, info_level); + result = 0; + + if (status == STATUS_PENDING) + add_async_iorequest(device, file, id, major, length, + fns, 0, 0, NULL, 0); break; default: status = STATUS_INVALID_PARAMETER; - /* JIF - unimpl("IRP major=0x%x minor=0x%x\n", major, minor); */ + /* JIF */ + unimpl("IRP major=0x%x minor=0x%x\n", major, minor); } break; @@ -551,10 +663,46 @@ in_uint8s(s, 0x14); buffer = (uint8 *) xrealloc((void *) buffer, bytes_out + 0x14); + if (!buffer) + { + status = STATUS_CANCELLED; + break; + } + out.data = out.p = buffer; out.size = sizeof(buffer); status = fns->device_control(file, request, s, &out); result = buffer_len = out.p - out.data; + + /* Serial SERIAL_WAIT_ON_MASK */ + if (status == STATUS_PENDING) + { + if (add_async_iorequest + (device, file, id, major, length, fns, 0, 0, NULL, 0)) + { + status = STATUS_PENDING; + break; + } + } + break; + + + case IRP_MJ_LOCK_CONTROL: + + if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK) + { + status = STATUS_INVALID_HANDLE; + break; + } + + in_uint32_le(s, info_level); + + out.data = out.p = buffer; + out.size = sizeof(buffer); + /* FIXME: Perhaps consider actually *do* + something here :-) */ + status = STATUS_SUCCESS; + result = buffer_len = out.p - out.data; break; default: @@ -566,11 +714,12 @@ { rdpdr_send_completion(device, id, status, result, buffer, buffer_len); } - xfree(buffer); - + if (buffer) + xfree(buffer); + buffer = NULL; } -void +static void rdpdr_send_clientcapabilty(void) { uint8 magic[4] = "rDPC"; @@ -686,8 +835,9 @@ void rdpdr_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv, BOOL * timeout) { - long select_timeout = 0; // Timeout value to be used for select() (in millisecons). + uint32 select_timeout = 0; // Timeout value to be used for select() (in millisecons). struct async_iorequest *iorq; + char c; iorq = g_iorequest; while (iorq != NULL) @@ -697,39 +847,90 @@ switch (iorq->major) { case IRP_MJ_READ: + /* Is this FD valid? FDs will + be invalid when + reconnecting. FIXME: Real + support for reconnects. */ FD_SET(iorq->fd, rfds); + *n = MAX(*n, iorq->fd); - // Check if io request timeout is smaller than current (but not 0). + /* Check if io request timeout is smaller than current (but not 0). */ if (iorq->timeout && (select_timeout == 0 || iorq->timeout < select_timeout)) { - // Set new timeout + /* Set new timeout */ select_timeout = iorq->timeout; g_min_timeout_fd = iorq->fd; /* Remember fd */ tv->tv_sec = select_timeout / 1000; tv->tv_usec = (select_timeout % 1000) * 1000; *timeout = True; + break; + } + if (iorq->itv_timeout && iorq->partial_len > 0 + && (select_timeout == 0 + || iorq->itv_timeout < select_timeout)) + { + /* Set new timeout */ + select_timeout = iorq->itv_timeout; + g_min_timeout_fd = iorq->fd; /* Remember fd */ + tv->tv_sec = select_timeout / 1000; + tv->tv_usec = (select_timeout % 1000) * 1000; + *timeout = True; + break; } break; case IRP_MJ_WRITE: + /* FD still valid? See above. */ + if ((write(iorq->fd, &c, 0) != 0) && (errno == EBADF)) + break; + FD_SET(iorq->fd, wfds); + *n = MAX(*n, iorq->fd); + break; + + case IRP_MJ_DEVICE_CONTROL: + if (select_timeout > 5) + select_timeout = 5; /* serial event queue */ break; } - *n = MAX(*n, iorq->fd); + } iorq = iorq->next; } } +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 -rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out) +_rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out) { NTSTATUS status; uint32 result = 0; @@ -737,9 +938,53 @@ struct async_iorequest *iorq; struct async_iorequest *prev; uint32 req_size = 0; + uint32 buffer_len; + struct stream out; + uint8 *buffer = NULL; + if (timed_out) { + /* check serial iv_timeout */ + + iorq = g_iorequest; + prev = NULL; + while (iorq != NULL) + { + if (iorq->fd == g_min_timeout_fd) + { + if ((iorq->partial_len > 0) && + (g_rdpdr_device[iorq->device].device_type == + DEVICE_TYPE_SERIAL)) + { + + /* iv_timeout between 2 chars, send partial_len */ + //printf("RDPDR: IVT total %u bytes read of %u\n", iorq->partial_len, iorq->length); + rdpdr_send_completion(iorq->device, + iorq->id, STATUS_SUCCESS, + iorq->partial_len, + iorq->buffer, iorq->partial_len); + iorq = rdpdr_remove_iorequest(prev, iorq); + return; + } + else + { + break; + } + + } + else + { + break; + } + + + prev = iorq; + if (iorq) + iorq = iorq->next; + + } + rdpdr_abort_io(g_min_timeout_fd, 0, STATUS_TIMEOUT); return; } @@ -765,9 +1010,13 @@ /* never read larger chunks than 8k - chances are that it will block */ status = fns->read(iorq->fd, iorq->buffer + iorq->partial_len, - req_size, 0, &result); - iorq->partial_len += result; + req_size, iorq->offset, &result); + if ((long) result > 0) + { + iorq->partial_len += result; + iorq->offset += result; + } #if WITH_DEBUG_RDP5 DEBUG(("RDPDR: %d bytes of data read\n", result)); #endif @@ -779,26 +1028,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); - } - else - { - // Even if NULL - g_iorequest = iorq->next; - xfree(iorq); - } + iorq = rdpdr_remove_iorequest(prev, iorq); } } break; @@ -816,9 +1051,15 @@ /* never write larger chunks than 8k - chances are that it will block */ status = fns->write(iorq->fd, iorq->buffer + - iorq->partial_len, req_size, 0, - &result); - iorq->partial_len += result; + iorq->partial_len, req_size, + iorq->offset, &result); + + if ((long) result > 0) + { + iorq->partial_len += result; + iorq->offset += result; + } + #if WITH_DEBUG_RDP5 DEBUG(("RDPDR: %d bytes of data written\n", result)); @@ -831,38 +1072,102 @@ #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, "", - 1); + iorq->partial_len, + (uint8 *) "", 1); - xfree(iorq->buffer); - iorq->fd = 0; - if (prev != NULL) - { - prev->next = iorq->next; - xfree(iorq); - } - else + iorq = rdpdr_remove_iorequest(prev, iorq); + } + } + break; + case IRP_MJ_DEVICE_CONTROL: + if (serial_get_event(iorq->fd, &result)) + { + buffer = (uint8 *) xrealloc((void *) buffer, 0x14); + out.data = out.p = buffer; + out.size = sizeof(buffer); + out_uint32_le(&out, result); + result = buffer_len = out.p - out.data; + status = STATUS_SUCCESS; + rdpdr_send_completion(iorq->device, iorq->id, + status, result, buffer, + buffer_len); + xfree(buffer); + iorq = rdpdr_remove_iorequest(prev, iorq); + } + + break; + } + + } + prev = iorq; + if (iorq) + iorq = iorq->next; + } + + /* Check notify */ + iorq = g_iorequest; + prev = NULL; + while (iorq != NULL) + { + if (iorq->fd != 0) + { + switch (iorq->major) + { + + case IRP_MJ_DIRECTORY_CONTROL: + if (g_rdpdr_device[iorq->device].device_type == + DEVICE_TYPE_DISK) + { + + if (g_notify_stamp) + { + g_notify_stamp = False; + status = disk_check_notify(iorq->fd); + if (status != STATUS_PENDING) { - // Even if NULL - g_iorequest = iorq->next; - xfree(iorq); + rdpdr_send_completion(iorq->device, + iorq->id, + status, 0, + NULL, 0); + iorq = rdpdr_remove_iorequest(prev, + iorq); } } } break; - } + + + } } + prev = iorq; - iorq = iorq->next; + if (iorq) + iorq = iorq->next; } } +void +rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out) +{ + fd_set dummy; + + + FD_ZERO(&dummy); + + + /* fist check event queue only, + any serial wait event must be done before read block will be sent + */ + + _rdpdr_check_fds(&dummy, &dummy, False); + _rdpdr_check_fds(rfds, wfds, timed_out); +} + + /* Abort a pending io request for a given handle and major */ BOOL rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status) @@ -871,7 +1176,7 @@ struct async_iorequest *iorq; struct async_iorequest *prev; - iorq = &g_iorequest; + iorq = g_iorequest; prev = NULL; while (iorq != NULL) { @@ -880,20 +1185,10 @@ if ((iorq->fd == fd) && (major == 0 || iorq->major == major)) { result = 0; - rdpdr_send_completion(iorq->device, iorq->id, status, result, "", 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); - } + rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8 *) "", + 1); + + iorq = rdpdr_remove_iorequest(prev, iorq); return True; }