2011-08-05 20:12:16 +04:00
|
|
|
/**
|
2012-10-09 05:00:07 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-08-05 20:12:16 +04:00
|
|
|
* File System Virtual Channel
|
|
|
|
*
|
|
|
|
* Copyright 2010-2011 Vic Lee
|
2012-10-09 05:00:07 +04:00
|
|
|
* Copyright 2010-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2011-08-05 20:12:16 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-04-18 13:52:49 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
#define __USE_LARGEFILE64
|
|
|
|
#define _LARGEFILE_SOURCE
|
|
|
|
#define _LARGEFILE64_SOURCE
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
2011-10-19 17:43:04 +04:00
|
|
|
#include <errno.h>
|
2011-08-05 20:12:16 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2011-08-05 20:12:16 +04:00
|
|
|
#include <freerdp/utils/memory.h>
|
|
|
|
#include <freerdp/utils/stream.h>
|
|
|
|
#include <freerdp/utils/unicode.h>
|
|
|
|
#include <freerdp/utils/list.h>
|
2012-10-09 05:00:07 +04:00
|
|
|
#include <freerdp/channels/rdpdr.h>
|
2011-08-05 20:12:16 +04:00
|
|
|
#include <freerdp/utils/svc_plugin.h>
|
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
#include <winpr/crt.h>
|
2012-09-23 21:54:14 +04:00
|
|
|
#include <winpr/synch.h>
|
|
|
|
#include <winpr/thread.h>
|
2012-10-03 05:52:27 +04:00
|
|
|
#include <winpr/interlocked.h>
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2011-08-07 07:21:42 +04:00
|
|
|
#include "disk_file.h"
|
2011-08-05 20:12:16 +04:00
|
|
|
|
|
|
|
typedef struct _DISK_DEVICE DISK_DEVICE;
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2011-08-05 20:12:16 +04:00
|
|
|
struct _DISK_DEVICE
|
|
|
|
{
|
|
|
|
DEVICE device;
|
|
|
|
|
|
|
|
char* path;
|
|
|
|
LIST* files;
|
2011-08-07 07:21:42 +04:00
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
HANDLE thread;
|
|
|
|
HANDLE irpEvent;
|
|
|
|
HANDLE stopEvent;
|
2012-10-03 05:52:27 +04:00
|
|
|
PSLIST_HEADER pIrpList;
|
2011-08-05 20:12:16 +04:00
|
|
|
|
2012-04-18 13:52:49 +04:00
|
|
|
DEVMAN* devman;
|
|
|
|
};
|
2011-10-19 17:43:04 +04:00
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
static uint32 disk_map_posix_err(int fs_errno)
|
2011-10-19 17:43:04 +04:00
|
|
|
{
|
|
|
|
uint32 rc;
|
|
|
|
|
|
|
|
/* try to return NTSTATUS version of error code */
|
2012-10-03 05:52:27 +04:00
|
|
|
|
2011-10-19 17:43:04 +04:00
|
|
|
switch (fs_errno)
|
|
|
|
{
|
|
|
|
case EPERM:
|
|
|
|
case EACCES:
|
|
|
|
rc = STATUS_ACCESS_DENIED;
|
|
|
|
break;
|
|
|
|
case ENOENT:
|
|
|
|
rc = STATUS_NO_SUCH_FILE;
|
|
|
|
break;
|
|
|
|
case EBUSY:
|
|
|
|
rc = STATUS_DEVICE_BUSY;
|
|
|
|
break;
|
|
|
|
case EEXIST:
|
|
|
|
rc = STATUS_OBJECT_NAME_COLLISION;
|
|
|
|
break;
|
|
|
|
case EISDIR:
|
|
|
|
rc = STATUS_FILE_IS_A_DIRECTORY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
rc = STATUS_UNSUCCESSFUL;
|
|
|
|
break;
|
|
|
|
}
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2012-02-22 02:22:01 +04:00
|
|
|
DEBUG_SVC("errno 0x%x mapped to 0x%x", fs_errno, rc);
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2011-10-19 17:43:04 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2011-08-07 07:21:42 +04:00
|
|
|
static DISK_FILE* disk_get_file_by_id(DISK_DEVICE* disk, uint32 id)
|
|
|
|
{
|
|
|
|
LIST_ITEM* item;
|
|
|
|
DISK_FILE* file;
|
|
|
|
|
|
|
|
for (item = disk->files->head; item; item = item->next)
|
|
|
|
{
|
2012-09-23 21:54:14 +04:00
|
|
|
file = (DISK_FILE*) item->data;
|
|
|
|
|
2011-08-07 07:21:42 +04:00
|
|
|
if (file->id == id)
|
|
|
|
return file;
|
|
|
|
}
|
2012-10-03 05:52:27 +04:00
|
|
|
|
2011-08-07 07:21:42 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void disk_process_irp_create(DISK_DEVICE* disk, IRP* irp)
|
|
|
|
{
|
2012-10-06 02:30:14 +04:00
|
|
|
char* path;
|
|
|
|
uint32 FileId;
|
2011-08-07 07:21:42 +04:00
|
|
|
DISK_FILE* file;
|
2012-10-06 02:30:14 +04:00
|
|
|
uint8 Information;
|
2011-08-07 07:21:42 +04:00
|
|
|
uint32 DesiredAccess;
|
|
|
|
uint32 CreateDisposition;
|
|
|
|
uint32 CreateOptions;
|
|
|
|
uint32 PathLength;
|
|
|
|
|
|
|
|
stream_read_uint32(irp->input, DesiredAccess);
|
|
|
|
stream_seek(irp->input, 16); /* AllocationSize(8), FileAttributes(4), SharedAccess(4) */
|
|
|
|
stream_read_uint32(irp->input, CreateDisposition);
|
|
|
|
stream_read_uint32(irp->input, CreateOptions);
|
|
|
|
stream_read_uint32(irp->input, PathLength);
|
|
|
|
|
2012-09-24 04:11:50 +04:00
|
|
|
freerdp_UnicodeToAsciiAlloc((WCHAR*) stream_get_tail(irp->input), &path, PathLength / 2);
|
2011-08-07 07:21:42 +04:00
|
|
|
|
|
|
|
FileId = irp->devman->id_sequence++;
|
2012-04-18 13:52:49 +04:00
|
|
|
|
2011-08-07 07:21:42 +04:00
|
|
|
file = disk_file_new(disk->path, path, FileId,
|
|
|
|
DesiredAccess, CreateDisposition, CreateOptions);
|
|
|
|
|
|
|
|
if (file == NULL)
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
FileId = 0;
|
|
|
|
Information = 0;
|
|
|
|
|
|
|
|
DEBUG_WARN("failed to create %s.", path);
|
|
|
|
}
|
2011-10-19 17:43:04 +04:00
|
|
|
else if (file->err)
|
|
|
|
{
|
|
|
|
FileId = 0;
|
|
|
|
Information = 0;
|
|
|
|
|
2012-10-06 02:30:14 +04:00
|
|
|
/* map errno to windows result */
|
2011-10-19 17:43:04 +04:00
|
|
|
irp->IoStatus = disk_map_posix_err(file->err);
|
|
|
|
disk_file_free(file);
|
|
|
|
}
|
2011-08-07 07:21:42 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
list_enqueue(disk->files, file);
|
|
|
|
|
|
|
|
switch (CreateDisposition)
|
|
|
|
{
|
|
|
|
case FILE_SUPERSEDE:
|
|
|
|
case FILE_OPEN:
|
|
|
|
case FILE_CREATE:
|
|
|
|
case FILE_OVERWRITE:
|
|
|
|
Information = FILE_SUPERSEDED;
|
|
|
|
break;
|
|
|
|
case FILE_OPEN_IF:
|
|
|
|
Information = FILE_OPENED;
|
|
|
|
break;
|
|
|
|
case FILE_OVERWRITE_IF:
|
|
|
|
Information = FILE_OVERWRITTEN;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Information = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
DEBUG_SVC("%s(%d) created.", file->fullpath, file->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
stream_write_uint32(irp->output, FileId);
|
|
|
|
stream_write_uint8(irp->output, Information);
|
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(path);
|
2011-08-07 07:21:42 +04:00
|
|
|
|
|
|
|
irp->Complete(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void disk_process_irp_close(DISK_DEVICE* disk, IRP* irp)
|
|
|
|
{
|
|
|
|
DISK_FILE* file;
|
|
|
|
|
|
|
|
file = disk_get_file_by_id(disk, irp->FileId);
|
|
|
|
|
|
|
|
if (file == NULL)
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
|
|
|
|
DEBUG_WARN("FileId %d not valid.", irp->FileId);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUG_SVC("%s(%d) closed.", file->fullpath, file->id);
|
|
|
|
|
|
|
|
list_remove(disk->files, file);
|
|
|
|
disk_file_free(file);
|
|
|
|
}
|
|
|
|
|
2011-08-07 20:23:36 +04:00
|
|
|
stream_write_zero(irp->output, 5); /* Padding(5) */
|
2011-08-07 11:17:06 +04:00
|
|
|
|
|
|
|
irp->Complete(irp);
|
2011-08-07 07:21:42 +04:00
|
|
|
}
|
|
|
|
|
2011-08-07 09:11:52 +04:00
|
|
|
static void disk_process_irp_read(DISK_DEVICE* disk, IRP* irp)
|
|
|
|
{
|
|
|
|
DISK_FILE* file;
|
|
|
|
uint32 Length;
|
|
|
|
uint64 Offset;
|
|
|
|
uint8* buffer = NULL;
|
|
|
|
|
|
|
|
stream_read_uint32(irp->input, Length);
|
|
|
|
stream_read_uint64(irp->input, Offset);
|
|
|
|
|
|
|
|
file = disk_get_file_by_id(disk, irp->FileId);
|
|
|
|
|
|
|
|
if (file == NULL)
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
Length = 0;
|
|
|
|
|
|
|
|
DEBUG_WARN("FileId %d not valid.", irp->FileId);
|
|
|
|
}
|
|
|
|
else if (!disk_file_seek(file, Offset))
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
Length = 0;
|
|
|
|
|
|
|
|
DEBUG_WARN("seek %s(%d) failed.", file->fullpath, file->id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-09 07:21:26 +04:00
|
|
|
buffer = (uint8*) malloc(Length);
|
2011-08-07 09:11:52 +04:00
|
|
|
if (!disk_file_read(file, buffer, &Length))
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
2012-10-09 07:21:26 +04:00
|
|
|
free(buffer);
|
2011-08-07 09:11:52 +04:00
|
|
|
buffer = NULL;
|
|
|
|
Length = 0;
|
|
|
|
|
|
|
|
DEBUG_WARN("read %s(%d) failed.", file->fullpath, file->id);
|
|
|
|
}
|
2011-08-07 11:17:06 +04:00
|
|
|
else
|
|
|
|
{
|
2011-08-07 20:23:36 +04:00
|
|
|
DEBUG_SVC("read %llu-%llu from %s(%d).", Offset, Offset + Length, file->fullpath, file->id);
|
2011-08-07 11:17:06 +04:00
|
|
|
}
|
2011-08-07 09:11:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
stream_write_uint32(irp->output, Length);
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2011-08-07 09:11:52 +04:00
|
|
|
if (Length > 0)
|
|
|
|
{
|
2012-10-03 05:52:27 +04:00
|
|
|
stream_check_size(irp->output, (int) Length);
|
2011-08-07 09:11:52 +04:00
|
|
|
stream_write(irp->output, buffer, Length);
|
|
|
|
}
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(buffer);
|
2011-08-07 11:17:06 +04:00
|
|
|
|
|
|
|
irp->Complete(irp);
|
2011-08-07 09:11:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void disk_process_irp_write(DISK_DEVICE* disk, IRP* irp)
|
|
|
|
{
|
|
|
|
DISK_FILE* file;
|
|
|
|
uint32 Length;
|
|
|
|
uint64 Offset;
|
|
|
|
|
|
|
|
stream_read_uint32(irp->input, Length);
|
|
|
|
stream_read_uint64(irp->input, Offset);
|
|
|
|
stream_seek(irp->input, 20); /* Padding */
|
|
|
|
|
|
|
|
file = disk_get_file_by_id(disk, irp->FileId);
|
|
|
|
|
|
|
|
if (file == NULL)
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
Length = 0;
|
|
|
|
|
|
|
|
DEBUG_WARN("FileId %d not valid.", irp->FileId);
|
|
|
|
}
|
|
|
|
else if (!disk_file_seek(file, Offset))
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
Length = 0;
|
|
|
|
|
|
|
|
DEBUG_WARN("seek %s(%d) failed.", file->fullpath, file->id);
|
|
|
|
}
|
|
|
|
else if (!disk_file_write(file, stream_get_tail(irp->input), Length))
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
Length = 0;
|
|
|
|
|
|
|
|
DEBUG_WARN("write %s(%d) failed.", file->fullpath, file->id);
|
|
|
|
}
|
2011-08-07 11:17:06 +04:00
|
|
|
else
|
|
|
|
{
|
2011-08-07 20:23:36 +04:00
|
|
|
DEBUG_SVC("write %llu-%llu to %s(%d).", Offset, Offset + Length, file->fullpath, file->id);
|
2011-08-07 11:17:06 +04:00
|
|
|
}
|
2011-08-07 09:11:52 +04:00
|
|
|
|
|
|
|
stream_write_uint32(irp->output, Length);
|
|
|
|
stream_write_uint8(irp->output, 0); /* Padding */
|
2011-08-07 11:17:06 +04:00
|
|
|
|
|
|
|
irp->Complete(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void disk_process_irp_query_information(DISK_DEVICE* disk, IRP* irp)
|
|
|
|
{
|
|
|
|
DISK_FILE* file;
|
|
|
|
uint32 FsInformationClass;
|
|
|
|
|
|
|
|
stream_read_uint32(irp->input, FsInformationClass);
|
|
|
|
|
|
|
|
file = disk_get_file_by_id(disk, irp->FileId);
|
|
|
|
|
|
|
|
if (file == NULL)
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
|
|
|
|
DEBUG_WARN("FileId %d not valid.", irp->FileId);
|
|
|
|
}
|
|
|
|
else if (!disk_file_query_information(file, FsInformationClass, irp->output))
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
|
2011-08-07 20:23:36 +04:00
|
|
|
DEBUG_WARN("FsInformationClass %d on %s(%d) failed.", FsInformationClass, file->fullpath, file->id);
|
2011-08-07 11:17:06 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-08-07 20:23:36 +04:00
|
|
|
DEBUG_SVC("FsInformationClass %d on %s(%d).", FsInformationClass, file->fullpath, file->id);
|
2011-08-07 11:17:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
irp->Complete(irp);
|
|
|
|
}
|
|
|
|
|
2011-08-07 20:23:36 +04:00
|
|
|
static void disk_process_irp_set_information(DISK_DEVICE* disk, IRP* irp)
|
|
|
|
{
|
|
|
|
DISK_FILE* file;
|
|
|
|
uint32 FsInformationClass;
|
|
|
|
uint32 Length;
|
|
|
|
|
|
|
|
stream_read_uint32(irp->input, FsInformationClass);
|
|
|
|
stream_read_uint32(irp->input, Length);
|
|
|
|
stream_seek(irp->input, 24); /* Padding */
|
|
|
|
|
|
|
|
file = disk_get_file_by_id(disk, irp->FileId);
|
|
|
|
|
|
|
|
if (file == NULL)
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
|
|
|
|
DEBUG_WARN("FileId %d not valid.", irp->FileId);
|
|
|
|
}
|
|
|
|
else if (!disk_file_set_information(file, FsInformationClass, Length, irp->input))
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
|
|
|
|
DEBUG_WARN("FsInformationClass %d on %s(%d) failed.", FsInformationClass, file->fullpath, file->id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUG_SVC("FsInformationClass %d on %s(%d) ok.", FsInformationClass, file->fullpath, file->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
stream_write_uint32(irp->output, Length);
|
|
|
|
|
|
|
|
irp->Complete(irp);
|
|
|
|
}
|
|
|
|
|
2011-08-07 11:17:06 +04:00
|
|
|
static void disk_process_irp_query_volume_information(DISK_DEVICE* disk, IRP* irp)
|
|
|
|
{
|
|
|
|
uint32 FsInformationClass;
|
|
|
|
STREAM* output = irp->output;
|
2012-04-18 13:52:49 +04:00
|
|
|
struct STATVFS svfst;
|
|
|
|
struct STAT st;
|
2012-09-24 03:49:13 +04:00
|
|
|
char* volumeLabel = {"FREERDP"};
|
2012-09-23 21:54:14 +04:00
|
|
|
char* diskType = {"FAT32"};
|
2012-09-24 03:49:13 +04:00
|
|
|
WCHAR* outStr;
|
|
|
|
int length;
|
2011-08-07 11:17:06 +04:00
|
|
|
|
|
|
|
stream_read_uint32(irp->input, FsInformationClass);
|
|
|
|
|
2012-04-18 13:52:49 +04:00
|
|
|
STATVFS(disk->path, &svfst);
|
|
|
|
STAT(disk->path, &st);
|
|
|
|
|
2011-08-07 11:17:06 +04:00
|
|
|
switch (FsInformationClass)
|
|
|
|
{
|
|
|
|
case FileFsVolumeInformation:
|
|
|
|
/* http://msdn.microsoft.com/en-us/library/cc232108.aspx */
|
2012-09-24 03:49:13 +04:00
|
|
|
length = freerdp_AsciiToUnicodeAlloc(volumeLabel, &outStr, 0) * 2;
|
|
|
|
stream_write_uint32(output, 17 + length); /* Length */
|
|
|
|
stream_check_size(output, 17 + length);
|
2012-04-18 13:52:49 +04:00
|
|
|
stream_write_uint64(output, FILE_TIME_SYSTEM_TO_RDP(st.st_ctime)); /* VolumeCreationTime */
|
|
|
|
stream_write_uint32(output, svfst.f_fsid); /* VolumeSerialNumber */
|
2012-09-24 03:49:13 +04:00
|
|
|
stream_write_uint32(output, length); /* VolumeLabelLength */
|
2011-08-07 11:17:06 +04:00
|
|
|
stream_write_uint8(output, 0); /* SupportsObjects */
|
2012-04-18 13:52:49 +04:00
|
|
|
/* Reserved(1), MUST NOT be added! */
|
2012-09-24 03:49:13 +04:00
|
|
|
stream_write(output, outStr, length); /* VolumeLabel (Unicode) */
|
2012-10-09 07:21:26 +04:00
|
|
|
free(outStr);
|
2011-08-07 11:17:06 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FileFsSizeInformation:
|
|
|
|
/* http://msdn.microsoft.com/en-us/library/cc232107.aspx */
|
|
|
|
stream_write_uint32(output, 24); /* Length */
|
|
|
|
stream_check_size(output, 24);
|
2012-04-18 13:52:49 +04:00
|
|
|
stream_write_uint64(output, svfst.f_blocks); /* TotalAllocationUnits */
|
|
|
|
stream_write_uint64(output, svfst.f_bavail); /* AvailableAllocationUnits */
|
2011-08-07 11:17:06 +04:00
|
|
|
stream_write_uint32(output, 1); /* SectorsPerAllocationUnit */
|
2012-04-18 13:52:49 +04:00
|
|
|
stream_write_uint32(output, svfst.f_bsize); /* BytesPerSector */
|
2011-08-07 11:17:06 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FileFsAttributeInformation:
|
|
|
|
/* http://msdn.microsoft.com/en-us/library/cc232101.aspx */
|
2012-09-24 03:49:13 +04:00
|
|
|
length = freerdp_AsciiToUnicodeAlloc(diskType, &outStr, 0) * 2;
|
|
|
|
stream_write_uint32(output, 12 + length); /* Length */
|
|
|
|
stream_check_size(output, 12 + length);
|
2011-08-07 11:17:06 +04:00
|
|
|
stream_write_uint32(output,
|
2012-04-18 13:52:49 +04:00
|
|
|
FILE_CASE_SENSITIVE_SEARCH |
|
|
|
|
FILE_CASE_PRESERVED_NAMES |
|
2011-08-07 11:17:06 +04:00
|
|
|
FILE_UNICODE_ON_DISK); /* FileSystemAttributes */
|
2012-04-18 13:52:49 +04:00
|
|
|
stream_write_uint32(output, svfst.f_namemax/*510*/); /* MaximumComponentNameLength */
|
2012-09-24 03:49:13 +04:00
|
|
|
stream_write_uint32(output, length); /* FileSystemNameLength */
|
|
|
|
stream_write(output, outStr, length); /* FileSystemName (Unicode) */
|
2012-10-09 07:21:26 +04:00
|
|
|
free(outStr);
|
2011-08-07 11:17:06 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FileFsFullSizeInformation:
|
|
|
|
/* http://msdn.microsoft.com/en-us/library/cc232104.aspx */
|
|
|
|
stream_write_uint32(output, 32); /* Length */
|
|
|
|
stream_check_size(output, 32);
|
2012-04-18 13:52:49 +04:00
|
|
|
stream_write_uint64(output, svfst.f_blocks); /* TotalAllocationUnits */
|
|
|
|
stream_write_uint64(output, svfst.f_bavail); /* CallerAvailableAllocationUnits */
|
|
|
|
stream_write_uint64(output, svfst.f_bfree); /* AvailableAllocationUnits */
|
2011-08-07 11:17:06 +04:00
|
|
|
stream_write_uint32(output, 1); /* SectorsPerAllocationUnit */
|
2012-04-18 13:52:49 +04:00
|
|
|
stream_write_uint32(output, svfst.f_bsize); /* BytesPerSector */
|
2011-08-07 11:17:06 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FileFsDeviceInformation:
|
|
|
|
/* http://msdn.microsoft.com/en-us/library/cc232109.aspx */
|
|
|
|
stream_write_uint32(output, 8); /* Length */
|
|
|
|
stream_check_size(output, 8);
|
|
|
|
stream_write_uint32(output, FILE_DEVICE_DISK); /* DeviceType */
|
|
|
|
stream_write_uint32(output, 0); /* Characteristics */
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
stream_write_uint32(output, 0); /* Length */
|
|
|
|
DEBUG_WARN("invalid FsInformationClass %d", FsInformationClass);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
irp->Complete(irp);
|
2011-08-07 09:11:52 +04:00
|
|
|
}
|
|
|
|
|
2011-08-07 15:24:29 +04:00
|
|
|
static void disk_process_irp_query_directory(DISK_DEVICE* disk, IRP* irp)
|
|
|
|
{
|
2012-10-03 05:52:27 +04:00
|
|
|
char* path;
|
2011-08-07 15:24:29 +04:00
|
|
|
DISK_FILE* file;
|
|
|
|
uint8 InitialQuery;
|
|
|
|
uint32 PathLength;
|
2012-10-03 05:52:27 +04:00
|
|
|
uint32 FsInformationClass;
|
2011-08-07 15:24:29 +04:00
|
|
|
|
|
|
|
stream_read_uint32(irp->input, FsInformationClass);
|
|
|
|
stream_read_uint8(irp->input, InitialQuery);
|
|
|
|
stream_read_uint32(irp->input, PathLength);
|
|
|
|
stream_seek(irp->input, 23); /* Padding */
|
|
|
|
|
2012-09-24 04:11:50 +04:00
|
|
|
freerdp_UnicodeToAsciiAlloc((WCHAR*) stream_get_tail(irp->input), &path, PathLength / 2);
|
2011-08-07 15:24:29 +04:00
|
|
|
|
|
|
|
file = disk_get_file_by_id(disk, irp->FileId);
|
|
|
|
|
|
|
|
if (file == NULL)
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
stream_write_uint32(irp->output, 0); /* Length */
|
|
|
|
DEBUG_WARN("FileId %d not valid.", irp->FileId);
|
|
|
|
}
|
|
|
|
else if (!disk_file_query_directory(file, FsInformationClass, InitialQuery, path, irp->output))
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_NO_MORE_FILES;
|
|
|
|
}
|
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(path);
|
2011-08-07 15:24:29 +04:00
|
|
|
|
|
|
|
irp->Complete(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void disk_process_irp_directory_control(DISK_DEVICE* disk, IRP* irp)
|
|
|
|
{
|
|
|
|
switch (irp->MinorFunction)
|
|
|
|
{
|
|
|
|
case IRP_MN_QUERY_DIRECTORY:
|
|
|
|
disk_process_irp_query_directory(disk, irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MN_NOTIFY_CHANGE_DIRECTORY: /* TODO */
|
|
|
|
irp->Discard(irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DEBUG_WARN("MinorFunction 0x%X not supported", irp->MinorFunction);
|
|
|
|
irp->IoStatus = STATUS_NOT_SUPPORTED;
|
|
|
|
stream_write_uint32(irp->output, 0); /* Length */
|
|
|
|
irp->Complete(irp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-07 20:23:36 +04:00
|
|
|
static void disk_process_irp_device_control(DISK_DEVICE* disk, IRP* irp)
|
|
|
|
{
|
|
|
|
stream_write_uint32(irp->output, 0); /* OutputBufferLength */
|
|
|
|
irp->Complete(irp);
|
|
|
|
}
|
|
|
|
|
2011-08-07 07:21:42 +04:00
|
|
|
static void disk_process_irp(DISK_DEVICE* disk, IRP* irp)
|
|
|
|
{
|
2012-10-07 01:49:56 +04:00
|
|
|
irp->IoStatus = STATUS_SUCCESS;
|
|
|
|
|
2011-08-07 07:21:42 +04:00
|
|
|
switch (irp->MajorFunction)
|
|
|
|
{
|
|
|
|
case IRP_MJ_CREATE:
|
|
|
|
disk_process_irp_create(disk, irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_CLOSE:
|
|
|
|
disk_process_irp_close(disk, irp);
|
|
|
|
break;
|
|
|
|
|
2011-08-07 09:11:52 +04:00
|
|
|
case IRP_MJ_READ:
|
|
|
|
disk_process_irp_read(disk, irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_WRITE:
|
|
|
|
disk_process_irp_write(disk, irp);
|
|
|
|
break;
|
|
|
|
|
2011-08-07 11:17:06 +04:00
|
|
|
case IRP_MJ_QUERY_INFORMATION:
|
|
|
|
disk_process_irp_query_information(disk, irp);
|
|
|
|
break;
|
|
|
|
|
2011-08-07 20:23:36 +04:00
|
|
|
case IRP_MJ_SET_INFORMATION:
|
|
|
|
disk_process_irp_set_information(disk, irp);
|
|
|
|
break;
|
|
|
|
|
2011-08-07 11:17:06 +04:00
|
|
|
case IRP_MJ_QUERY_VOLUME_INFORMATION:
|
|
|
|
disk_process_irp_query_volume_information(disk, irp);
|
|
|
|
break;
|
|
|
|
|
2011-08-07 15:24:29 +04:00
|
|
|
case IRP_MJ_DIRECTORY_CONTROL:
|
|
|
|
disk_process_irp_directory_control(disk, irp);
|
|
|
|
break;
|
|
|
|
|
2011-08-07 20:23:36 +04:00
|
|
|
case IRP_MJ_DEVICE_CONTROL:
|
|
|
|
disk_process_irp_device_control(disk, irp);
|
|
|
|
break;
|
|
|
|
|
2011-08-07 07:21:42 +04:00
|
|
|
default:
|
|
|
|
DEBUG_WARN("MajorFunction 0x%X not supported", irp->MajorFunction);
|
|
|
|
irp->IoStatus = STATUS_NOT_SUPPORTED;
|
|
|
|
irp->Complete(irp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void disk_process_irp_list(DISK_DEVICE* disk)
|
|
|
|
{
|
|
|
|
IRP* irp;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2012-09-23 21:54:14 +04:00
|
|
|
if (WaitForSingleObject(disk->stopEvent, 0) == WAIT_OBJECT_0)
|
2011-08-07 07:21:42 +04:00
|
|
|
break;
|
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
irp = (IRP*) InterlockedPopEntrySList(disk->pIrpList);
|
2011-08-07 07:21:42 +04:00
|
|
|
|
|
|
|
if (irp == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
disk_process_irp(disk, irp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* disk_thread_func(void* arg)
|
|
|
|
{
|
2012-09-23 21:54:14 +04:00
|
|
|
DISK_DEVICE* disk = (DISK_DEVICE*) arg;
|
2011-08-07 07:21:42 +04:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2012-09-23 21:54:14 +04:00
|
|
|
WaitForSingleObject(disk->irpEvent, INFINITE);
|
2011-08-07 07:21:42 +04:00
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
if (WaitForSingleObject(disk->stopEvent, 0) == WAIT_OBJECT_0)
|
2011-08-07 07:21:42 +04:00
|
|
|
break;
|
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
ResetEvent(disk->irpEvent);
|
2011-08-07 07:21:42 +04:00
|
|
|
disk_process_irp_list(disk);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void disk_irp_request(DEVICE* device, IRP* irp)
|
2011-08-05 20:12:16 +04:00
|
|
|
{
|
2012-09-23 21:54:14 +04:00
|
|
|
DISK_DEVICE* disk = (DISK_DEVICE*) device;
|
2011-08-05 20:12:16 +04:00
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
InterlockedPushEntrySList(disk->pIrpList, &(irp->ItemEntry));
|
2011-08-07 07:21:42 +04:00
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
SetEvent(disk->irpEvent);
|
2011-08-05 20:12:16 +04:00
|
|
|
}
|
|
|
|
|
2011-08-07 07:21:42 +04:00
|
|
|
static void disk_free(DEVICE* device)
|
2011-08-05 20:12:16 +04:00
|
|
|
{
|
2011-08-07 07:21:42 +04:00
|
|
|
IRP* irp;
|
|
|
|
DISK_FILE* file;
|
2012-09-23 21:54:14 +04:00
|
|
|
DISK_DEVICE* disk = (DISK_DEVICE*) device;
|
2011-08-07 07:21:42 +04:00
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
SetEvent(disk->stopEvent);
|
|
|
|
CloseHandle(disk->thread);
|
|
|
|
CloseHandle(disk->irpEvent);
|
2012-04-18 13:52:49 +04:00
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
while ((irp = (IRP*) InterlockedPopEntrySList(disk->pIrpList)) != NULL)
|
2011-08-07 07:21:42 +04:00
|
|
|
irp->Discard(irp);
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
_aligned_free(disk->pIrpList);
|
2011-08-05 20:12:16 +04:00
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
while ((file = (DISK_FILE*) list_dequeue(disk->files)) != NULL)
|
2011-08-07 07:21:42 +04:00
|
|
|
disk_file_free(file);
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2011-08-05 20:12:16 +04:00
|
|
|
list_free(disk->files);
|
2012-10-09 07:21:26 +04:00
|
|
|
free(disk);
|
2011-08-05 20:12:16 +04:00
|
|
|
}
|
|
|
|
|
2012-10-03 04:17:57 +04:00
|
|
|
void disk_register_disk_path(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints, char* name, char* path)
|
2011-08-05 20:12:16 +04:00
|
|
|
{
|
2012-10-03 04:17:57 +04:00
|
|
|
int i, length;
|
2012-09-23 21:54:14 +04:00
|
|
|
DISK_DEVICE* disk;
|
2011-08-05 20:12:16 +04:00
|
|
|
|
2012-10-02 11:02:17 +04:00
|
|
|
#ifdef WIN32
|
2012-10-02 19:16:40 +04:00
|
|
|
/*
|
|
|
|
* We cannot enter paths like c:\ because : is an arg separator
|
|
|
|
* thus, paths are entered as c+\ and the + is substituted here
|
|
|
|
*/
|
2012-10-02 11:02:17 +04:00
|
|
|
if ( path[1] == '+' )
|
|
|
|
{
|
|
|
|
if ( (path[0]>='a' && path[0]<='z') || (path[0]>='A' && path[0]<='Z') )
|
|
|
|
{
|
|
|
|
path[1] = ':';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2011-08-05 20:12:16 +04:00
|
|
|
|
|
|
|
if (name[0] && path[0])
|
|
|
|
{
|
|
|
|
disk = xnew(DISK_DEVICE);
|
|
|
|
|
|
|
|
disk->device.type = RDPDR_DTYP_FILESYSTEM;
|
|
|
|
disk->device.name = name;
|
|
|
|
disk->device.IRPRequest = disk_irp_request;
|
|
|
|
disk->device.Free = disk_free;
|
|
|
|
|
2012-09-24 03:49:13 +04:00
|
|
|
length = strlen(name);
|
|
|
|
disk->device.data = stream_new(length + 1);
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2012-09-24 03:49:13 +04:00
|
|
|
for (i = 0; i <= length; i++)
|
2011-08-05 20:12:16 +04:00
|
|
|
stream_write_uint8(disk->device.data, name[i] < 0 ? '_' : name[i]);
|
|
|
|
|
|
|
|
disk->path = path;
|
|
|
|
disk->files = list_new();
|
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
disk->pIrpList = (PSLIST_HEADER) _aligned_malloc(sizeof(SLIST_HEADER), MEMORY_ALLOCATION_ALIGNMENT);
|
|
|
|
InitializeSListHead(disk->pIrpList);
|
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
disk->irpEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
disk->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
disk->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) disk_thread_func, disk, CREATE_SUSPENDED, NULL);
|
2011-08-07 07:21:42 +04:00
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
pEntryPoints->RegisterDevice(pEntryPoints->devman, (DEVICE*) disk);
|
2011-08-07 07:21:42 +04:00
|
|
|
|
2012-10-02 11:02:17 +04:00
|
|
|
ResumeThread(disk->thread);
|
2011-08-05 20:12:16 +04:00
|
|
|
}
|
|
|
|
}
|
2012-10-02 11:02:17 +04:00
|
|
|
|
2012-10-09 06:53:05 +04:00
|
|
|
#ifdef STATIC_CHANNELS
|
2012-10-08 06:53:24 +04:00
|
|
|
#define DeviceServiceEntry disk_DeviceServiceEntry
|
2012-10-02 11:02:17 +04:00
|
|
|
#endif
|
2012-10-08 06:53:24 +04:00
|
|
|
|
|
|
|
const int DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints)
|
2012-10-02 11:02:17 +04:00
|
|
|
{
|
|
|
|
char* name;
|
|
|
|
char* path;
|
|
|
|
#ifdef WIN32
|
2012-10-03 04:17:57 +04:00
|
|
|
char* dev;
|
|
|
|
int len;
|
2012-10-02 11:02:17 +04:00
|
|
|
char devlist[512], buf[512];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
name = (char*) pEntryPoints->plugin_data->data[1];
|
|
|
|
path = (char*) pEntryPoints->plugin_data->data[2];
|
|
|
|
|
|
|
|
#ifndef WIN32
|
|
|
|
disk_register_disk_path(pEntryPoints, name, path);
|
|
|
|
#else
|
2012-10-02 19:11:11 +04:00
|
|
|
/* Special case: path[0] == '*' -> export all drives */
|
|
|
|
/* Special case: path[0] == '%' -> user home dir */
|
2012-10-02 11:02:17 +04:00
|
|
|
if( path[0] == '%' )
|
|
|
|
{
|
|
|
|
_snprintf(buf, sizeof(buf), "%s\\", getenv("USERPROFILE"));
|
2012-10-09 07:42:01 +04:00
|
|
|
disk_register_disk_path(pEntryPoints, name, _strdup(buf));
|
2012-10-02 11:02:17 +04:00
|
|
|
}
|
|
|
|
else if( path[0] == '*' )
|
|
|
|
{
|
2012-10-02 19:11:11 +04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Enumerate all devices: */
|
|
|
|
GetLogicalDriveStringsA(sizeof(devlist) - 1, devlist);
|
|
|
|
|
|
|
|
for (dev = devlist, i = 0; *dev; dev += 4, i++)
|
2012-10-02 11:02:17 +04:00
|
|
|
{
|
2012-10-02 19:11:11 +04:00
|
|
|
if (*dev > 'B')
|
|
|
|
{
|
|
|
|
/* Suppress disk drives A and B to avoid pesty messages */
|
|
|
|
_snprintf(buf, sizeof(buf) - 4, "%s", name);
|
|
|
|
len = strlen(buf);
|
|
|
|
buf[len] = '_';
|
|
|
|
buf[len + 1] = dev[0];
|
|
|
|
buf[len + 2] = 0;
|
|
|
|
buf[len + 3] = 0;
|
2012-10-09 07:42:01 +04:00
|
|
|
disk_register_disk_path(pEntryPoints, _strdup(buf), _strdup(dev));
|
2012-10-02 11:02:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
disk_register_disk_path(pEntryPoints, name, path);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|