FreeRDP/channels/drive/client/drive_main.c

1015 lines
24 KiB
C
Raw Normal View History

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>
2015-06-03 16:33:59 +03:00
* Copyright 2015 Thincast Technologies GmbH
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
* Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <errno.h>
2011-08-05 20:12:16 +04:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winpr/crt.h>
2016-03-29 23:03:15 +03:00
#include <winpr/path.h>
#include <winpr/file.h>
2015-07-03 14:26:15 +03:00
#include <winpr/string.h>
#include <winpr/synch.h>
#include <winpr/thread.h>
#include <winpr/stream.h>
2016-03-29 23:03:15 +03:00
#include <winpr/environment.h>
#include <winpr/interlocked.h>
#include <winpr/collections.h>
#include <winpr/shell.h>
#include <freerdp/channels/rdpdr.h>
#include "drive_file.h"
2011-08-05 20:12:16 +04:00
typedef struct _DRIVE_DEVICE DRIVE_DEVICE;
struct _DRIVE_DEVICE
2011-08-05 20:12:16 +04:00
{
DEVICE device;
WCHAR* path;
UINT32 PathLength;
wListDictionary* files;
HANDLE thread;
wMessageQueue* IrpQueue;
2011-08-05 20:12:16 +04:00
DEVMAN* devman;
rdpContext* rdpcontext;
};
static DWORD drive_map_windows_err(DWORD fs_errno)
{
DWORD rc;
/* try to return NTSTATUS version of error code */
switch (fs_errno)
{
case STATUS_SUCCESS:
rc = STATUS_SUCCESS;
break;
case ERROR_ACCESS_DENIED:
case ERROR_SHARING_VIOLATION:
rc = STATUS_ACCESS_DENIED;
break;
case ERROR_FILE_NOT_FOUND:
rc = STATUS_NO_SUCH_FILE;
break;
case ERROR_BUSY_DRIVE:
rc = STATUS_DEVICE_BUSY;
break;
case ERROR_FILE_EXISTS:
case ERROR_ALREADY_EXISTS:
rc = STATUS_OBJECT_NAME_COLLISION;
break;
case ERROR_INVALID_NAME:
rc = STATUS_NO_SUCH_FILE;
break;
case ERROR_INVALID_HANDLE:
rc = STATUS_INVALID_HANDLE;
break;
case ERROR_NO_MORE_FILES:
rc = STATUS_NO_MORE_FILES;
break;
case ERROR_DIRECTORY:
rc = STATUS_NOT_A_DIRECTORY;
break;
case ERROR_PATH_NOT_FOUND:
rc = STATUS_OBJECT_PATH_NOT_FOUND;
break;
default:
rc = STATUS_UNSUCCESSFUL;
2017-03-18 00:05:21 +03:00
WLog_ERR(TAG, "Error code not found: %"PRId32"", fs_errno);
break;
}
return rc;
}
2013-11-04 04:10:33 +04:00
static DRIVE_FILE* drive_get_file_by_id(DRIVE_DEVICE* drive, UINT32 id)
{
DRIVE_FILE* file = NULL;
void* key = (void*)(size_t) id;
2013-11-04 04:10:33 +04:00
file = (DRIVE_FILE*) ListDictionary_GetItemValue(drive->files, key);
return file;
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT drive_process_irp_create(DRIVE_DEVICE* drive, IRP* irp)
{
void* key;
2012-10-09 11:26:39 +04:00
UINT32 FileId;
DRIVE_FILE* file;
BYTE Information;
UINT32 FileAttributes;
UINT32 SharedAccess;
2012-10-09 11:26:39 +04:00
UINT32 DesiredAccess;
UINT32 CreateDisposition;
UINT32 CreateOptions;
UINT32 PathLength;
const WCHAR* path;
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, DesiredAccess);
Stream_Seek(irp->input, 8); /* AllocationSize(8) */
Stream_Read_UINT32(irp->input, FileAttributes);
Stream_Read_UINT32(irp->input, SharedAccess);
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, CreateDisposition);
Stream_Read_UINT32(irp->input, CreateOptions);
Stream_Read_UINT32(irp->input, PathLength);
path = (WCHAR*) Stream_Pointer(irp->input);
FileId = irp->devman->id_sequence++;
file = drive_file_new(drive->path, path, PathLength, FileId, DesiredAccess, CreateDisposition,
CreateOptions, FileAttributes, SharedAccess);
if (!file)
{
irp->IoStatus = drive_map_windows_err(GetLastError());
FileId = 0;
Information = 0;
}
else
{
key = (void*)(size_t) file->id;
2015-06-03 16:33:59 +03:00
if (!ListDictionary_Add(drive->files, key, file))
{
WLog_ERR(TAG, "ListDictionary_Add failed!");
return ERROR_INTERNAL_ERROR;
}
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;
}
}
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, FileId);
Stream_Write_UINT8(irp->output, Information);
2015-06-03 16:33:59 +03:00
return irp->Complete(irp);
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT drive_process_irp_close(DRIVE_DEVICE* drive, IRP* irp)
{
void* key;
DRIVE_FILE* file;
2013-11-04 04:10:33 +04:00
file = drive_get_file_by_id(drive, irp->FileId);
key = (void*)(size_t) irp->FileId;
if (!file)
{
irp->IoStatus = STATUS_UNSUCCESSFUL;
}
else
{
2013-11-04 04:10:33 +04:00
ListDictionary_Remove(drive->files, key);
if (drive_file_free(file))
irp->IoStatus = STATUS_SUCCESS;
else
irp->IoStatus = drive_map_windows_err(GetLastError());
}
2013-05-09 00:27:21 +04:00
Stream_Zero(irp->output, 5); /* Padding(5) */
2015-06-03 16:33:59 +03:00
return irp->Complete(irp);
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT drive_process_irp_read(DRIVE_DEVICE* drive, IRP* irp)
{
DRIVE_FILE* file;
2012-10-09 11:26:39 +04:00
UINT32 Length;
UINT64 Offset;
BYTE* buffer = NULL;
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, Length);
Stream_Read_UINT64(irp->input, Offset);
2013-11-04 04:10:33 +04:00
file = drive_get_file_by_id(drive, irp->FileId);
if (!file)
{
irp->IoStatus = STATUS_UNSUCCESSFUL;
Length = 0;
}
else if (!drive_file_seek(file, Offset))
{
irp->IoStatus = drive_map_windows_err(GetLastError());
Length = 0;
}
else
{
buffer = (BYTE*) malloc(Length);
2015-06-03 16:33:59 +03:00
if (!buffer)
{
WLog_ERR(TAG, "malloc failed!");
return CHANNEL_RC_OK;
}
if (!drive_file_read(file, buffer, &Length))
{
irp->IoStatus = drive_map_windows_err(GetLastError());
free(buffer);
buffer = NULL;
Length = 0;
}
}
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, Length);
if (Length > 0)
{
2015-06-03 16:33:59 +03:00
if (!Stream_EnsureRemainingCapacity(irp->output, (int) Length))
{
WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
return ERROR_INTERNAL_ERROR;
}
2013-05-09 00:09:16 +04:00
Stream_Write(irp->output, buffer, Length);
}
free(buffer);
2015-06-03 16:33:59 +03:00
return irp->Complete(irp);
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT drive_process_irp_write(DRIVE_DEVICE* drive, IRP* irp)
{
DRIVE_FILE* file;
2012-10-09 11:26:39 +04:00
UINT32 Length;
UINT64 Offset;
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, Length);
Stream_Read_UINT64(irp->input, Offset);
Stream_Seek(irp->input, 20); /* Padding */
2013-11-04 04:10:33 +04:00
file = drive_get_file_by_id(drive, irp->FileId);
if (!file)
{
irp->IoStatus = STATUS_UNSUCCESSFUL;
Length = 0;
}
else if (!drive_file_seek(file, Offset))
{
irp->IoStatus = drive_map_windows_err(GetLastError());
Length = 0;
}
else if (!drive_file_write(file, Stream_Pointer(irp->input), Length))
{
irp->IoStatus = drive_map_windows_err(GetLastError());
Length = 0;
}
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, Length);
Stream_Write_UINT8(irp->output, 0); /* Padding */
2015-06-03 16:33:59 +03:00
return irp->Complete(irp);
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT drive_process_irp_query_information(DRIVE_DEVICE* drive, IRP* irp)
{
DRIVE_FILE* file;
2012-10-09 11:26:39 +04:00
UINT32 FsInformationClass;
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, FsInformationClass);
2013-11-04 04:10:33 +04:00
file = drive_get_file_by_id(drive, irp->FileId);
if (!file)
{
irp->IoStatus = STATUS_UNSUCCESSFUL;
}
else if (!drive_file_query_information(file, FsInformationClass, irp->output))
{
irp->IoStatus = drive_map_windows_err(GetLastError());
}
2015-06-03 16:33:59 +03:00
return irp->Complete(irp);
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT drive_process_irp_set_information(DRIVE_DEVICE* drive, IRP* irp)
{
DRIVE_FILE* file;
2012-10-09 11:26:39 +04:00
UINT32 FsInformationClass;
UINT32 Length;
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, FsInformationClass);
Stream_Read_UINT32(irp->input, Length);
Stream_Seek(irp->input, 24); /* Padding */
2013-11-04 04:10:33 +04:00
file = drive_get_file_by_id(drive, irp->FileId);
if (!file)
{
irp->IoStatus = STATUS_UNSUCCESSFUL;
}
else if (!drive_file_set_information(file, FsInformationClass, Length,
irp->input))
{
irp->IoStatus = drive_map_windows_err(GetLastError());
}
if (file && file->is_dir && !PathIsDirectoryEmptyW(file->fullpath))
irp->IoStatus = STATUS_DIRECTORY_NOT_EMPTY;
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, Length);
2015-06-03 16:33:59 +03:00
return irp->Complete(irp);
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT drive_process_irp_query_volume_information(DRIVE_DEVICE* drive,
IRP* irp)
{
2012-10-09 11:26:39 +04:00
UINT32 FsInformationClass;
wStream* output = irp->output;
char* volumeLabel = {"FREERDP"};
char* diskType = {"FAT32"};
WCHAR* outStr = NULL;
int length;
DWORD lpSectorsPerCluster;
DWORD lpBytesPerSector;
DWORD lpNumberOfFreeClusters;
DWORD lpTotalNumberOfClusters;
WIN32_FILE_ATTRIBUTE_DATA wfad;
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, FsInformationClass);
GetDiskFreeSpaceW(drive->path, &lpSectorsPerCluster, &lpBytesPerSector, &lpNumberOfFreeClusters,
&lpTotalNumberOfClusters);
switch (FsInformationClass)
{
case FileFsVolumeInformation:
2017-04-19 09:27:22 +03:00
/* http://msdn.microsoft.com/en-us/library/cc232108.aspx */
if ((length = ConvertToUnicode(sys_code_page, 0, volumeLabel, -1, &outStr, 0) * 2) <= 0)
{
WLog_ERR(TAG, "ConvertToUnicode failed!");
return CHANNEL_RC_NO_MEMORY;
}
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(output, 17 + length); /* Length */
2015-06-03 16:33:59 +03:00
if (!Stream_EnsureRemainingCapacity(output, 17 + length))
{
WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
free(outStr);
return CHANNEL_RC_NO_MEMORY;
}
GetFileAttributesExW(drive->path, GetFileExInfoStandard, &wfad);
Stream_Write_UINT32(output, wfad.ftCreationTime.dwLowDateTime); /* VolumeCreationTime */
Stream_Write_UINT32(output, wfad.ftCreationTime.dwHighDateTime); /* VolumeCreationTime */
Stream_Write_UINT32(output, lpNumberOfFreeClusters & 0xffff); /* VolumeSerialNumber */
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(output, length); /* VolumeLabelLength */
Stream_Write_UINT8(output, 0); /* SupportsObjects */
/* Reserved(1), MUST NOT be added! */
2013-05-09 00:09:16 +04:00
Stream_Write(output, outStr, length); /* VolumeLabel (Unicode) */
free(outStr);
break;
case FileFsSizeInformation:
/* http://msdn.microsoft.com/en-us/library/cc232107.aspx */
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(output, 24); /* Length */
2015-06-03 16:33:59 +03:00
if (!Stream_EnsureRemainingCapacity(output, 24))
{
WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
return CHANNEL_RC_NO_MEMORY;
}
Stream_Write_UINT64(output, lpTotalNumberOfClusters); /* TotalAllocationUnits */
Stream_Write_UINT64(output, lpNumberOfFreeClusters); /* AvailableAllocationUnits */
Stream_Write_UINT32(output, lpSectorsPerCluster); /* SectorsPerAllocationUnit */
Stream_Write_UINT32(output, lpBytesPerSector); /* BytesPerSector */
break;
case FileFsAttributeInformation:
2017-04-19 09:27:22 +03:00
/* http://msdn.microsoft.com/en-us/library/cc232101.aspx */
if ((length = ConvertToUnicode(sys_code_page, 0, diskType, -1, &outStr, 0) * 2) <= 0)
{
WLog_ERR(TAG, "ConvertToUnicode failed!");
return CHANNEL_RC_NO_MEMORY;
}
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(output, 12 + length); /* Length */
2015-06-03 16:33:59 +03:00
if (!Stream_EnsureRemainingCapacity(output, 12 + length))
{
WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
return CHANNEL_RC_NO_MEMORY;
}
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(output,
FILE_CASE_SENSITIVE_SEARCH |
FILE_CASE_PRESERVED_NAMES |
FILE_UNICODE_ON_DISK); /* FileSystemAttributes */
Stream_Write_UINT32(output, MAX_PATH); /* MaximumComponentNameLength */
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(output, length); /* FileSystemNameLength */
Stream_Write(output, outStr, length); /* FileSystemName (Unicode) */
free(outStr);
break;
case FileFsFullSizeInformation:
/* http://msdn.microsoft.com/en-us/library/cc232104.aspx */
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(output, 32); /* Length */
2015-06-03 16:33:59 +03:00
if (!Stream_EnsureRemainingCapacity(output, 32))
{
WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
return CHANNEL_RC_NO_MEMORY;
}
Stream_Write_UINT64(output, lpTotalNumberOfClusters); /* TotalAllocationUnits */
Stream_Write_UINT64(output, lpNumberOfFreeClusters); /* CallerAvailableAllocationUnits */
Stream_Write_UINT64(output, lpNumberOfFreeClusters); /* AvailableAllocationUnits */
Stream_Write_UINT32(output, lpSectorsPerCluster); /* SectorsPerAllocationUnit */
Stream_Write_UINT32(output, lpBytesPerSector); /* BytesPerSector */
break;
case FileFsDeviceInformation:
/* http://msdn.microsoft.com/en-us/library/cc232109.aspx */
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(output, 8); /* Length */
2015-06-03 16:33:59 +03:00
if (!Stream_EnsureRemainingCapacity(output, 8))
{
WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
return CHANNEL_RC_NO_MEMORY;
}
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(output, FILE_DEVICE_DISK); /* DeviceType */
Stream_Write_UINT32(output, 0); /* Characteristics */
break;
default:
irp->IoStatus = STATUS_UNSUCCESSFUL;
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(output, 0); /* Length */
break;
}
2015-06-03 16:33:59 +03:00
return irp->Complete(irp);
}
2013-02-05 16:21:25 +04:00
/* http://msdn.microsoft.com/en-us/library/cc241518.aspx */
2013-02-20 03:15:28 +04:00
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT drive_process_irp_silent_ignore(DRIVE_DEVICE* drive, IRP* irp)
2013-02-05 16:21:25 +04:00
{
UINT32 FsInformationClass;
wStream* output = irp->output;
2013-05-09 00:09:16 +04:00
Stream_Read_UINT32(irp->input, FsInformationClass);
Stream_Write_UINT32(output, 0); /* Length */
2015-06-03 16:33:59 +03:00
return irp->Complete(irp);
2013-02-05 16:21:25 +04:00
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT drive_process_irp_query_directory(DRIVE_DEVICE* drive, IRP* irp)
{
const WCHAR* path;
DRIVE_FILE* file;
BYTE InitialQuery;
2012-10-09 11:26:39 +04:00
UINT32 PathLength;
UINT32 FsInformationClass;
2013-05-09 00:09:16 +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 */
path = (WCHAR*) Stream_Pointer(irp->input);
2013-11-04 04:10:33 +04:00
file = drive_get_file_by_id(drive, irp->FileId);
if (file == NULL)
{
irp->IoStatus = STATUS_UNSUCCESSFUL;
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, 0); /* Length */
}
else if (!drive_file_query_directory(file, FsInformationClass, InitialQuery, path, PathLength,
irp->output))
{
irp->IoStatus = drive_map_windows_err(GetLastError());
}
2015-06-03 16:33:59 +03:00
return irp->Complete(irp);
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT drive_process_irp_directory_control(DRIVE_DEVICE* drive, IRP* irp)
{
switch (irp->MinorFunction)
{
case IRP_MN_QUERY_DIRECTORY:
2015-06-03 16:33:59 +03:00
return drive_process_irp_query_directory(drive, irp);
break;
case IRP_MN_NOTIFY_CHANGE_DIRECTORY: /* TODO */
2015-06-03 16:33:59 +03:00
return irp->Discard(irp);
break;
default:
irp->IoStatus = STATUS_NOT_SUPPORTED;
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, 0); /* Length */
2015-06-03 16:33:59 +03:00
return irp->Complete(irp);
break;
}
2015-06-03 16:33:59 +03:00
return CHANNEL_RC_OK;
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT drive_process_irp_device_control(DRIVE_DEVICE* drive, IRP* irp)
{
2013-05-09 00:09:16 +04:00
Stream_Write_UINT32(irp->output, 0); /* OutputBufferLength */
2015-06-03 16:33:59 +03:00
return irp->Complete(irp);
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT drive_process_irp(DRIVE_DEVICE* drive, IRP* irp)
{
UINT error;
2015-07-22 12:36:24 +03:00
irp->IoStatus = STATUS_SUCCESS;
switch (irp->MajorFunction)
{
case IRP_MJ_CREATE:
2015-06-03 16:33:59 +03:00
error = drive_process_irp_create(drive, irp);
break;
case IRP_MJ_CLOSE:
2015-06-03 16:33:59 +03:00
error = drive_process_irp_close(drive, irp);
break;
case IRP_MJ_READ:
2015-06-03 16:33:59 +03:00
error = drive_process_irp_read(drive, irp);
break;
case IRP_MJ_WRITE:
2015-06-03 16:33:59 +03:00
error = drive_process_irp_write(drive, irp);
break;
case IRP_MJ_QUERY_INFORMATION:
2015-06-03 16:33:59 +03:00
error = drive_process_irp_query_information(drive, irp);
break;
case IRP_MJ_SET_INFORMATION:
2015-06-03 16:33:59 +03:00
error = drive_process_irp_set_information(drive, irp);
break;
case IRP_MJ_QUERY_VOLUME_INFORMATION:
2015-06-03 16:33:59 +03:00
error = drive_process_irp_query_volume_information(drive, irp);
break;
case IRP_MJ_LOCK_CONTROL:
2015-06-03 16:33:59 +03:00
error = drive_process_irp_silent_ignore(drive, irp);
2013-02-05 16:21:25 +04:00
break;
case IRP_MJ_DIRECTORY_CONTROL:
2015-06-03 16:33:59 +03:00
error = drive_process_irp_directory_control(drive, irp);
break;
case IRP_MJ_DEVICE_CONTROL:
2015-06-03 16:33:59 +03:00
error = drive_process_irp_device_control(drive, irp);
break;
default:
irp->IoStatus = STATUS_NOT_SUPPORTED;
2015-06-03 16:33:59 +03:00
error = irp->Complete(irp);
break;
}
2015-06-03 16:33:59 +03:00
return error;
}
static void* drive_thread_func(void* arg)
{
2014-02-12 07:34:33 +04:00
IRP* irp;
wMessage message;
DRIVE_DEVICE* drive = (DRIVE_DEVICE*) arg;
UINT error = CHANNEL_RC_OK;
2014-02-12 07:34:33 +04:00
while (1)
{
if (!MessageQueue_Wait(drive->IrpQueue))
{
WLog_ERR(TAG, "MessageQueue_Wait failed!");
error = ERROR_INTERNAL_ERROR;
2014-02-12 07:34:33 +04:00
break;
}
2014-02-12 07:34:33 +04:00
if (!MessageQueue_Peek(drive->IrpQueue, &message, TRUE))
{
WLog_ERR(TAG, "MessageQueue_Peek failed!");
error = ERROR_INTERNAL_ERROR;
2014-02-12 07:34:33 +04:00
break;
}
2013-11-04 04:10:33 +04:00
2014-02-12 07:34:33 +04:00
if (message.id == WMQ_QUIT)
break;
2014-02-12 07:34:33 +04:00
irp = (IRP*) message.wParam;
2014-02-12 07:34:33 +04:00
if (irp)
2015-06-03 16:33:59 +03:00
if ((error = drive_process_irp(drive, irp)))
{
WLog_ERR(TAG, "drive_process_irp failed with error %"PRIu32"!", error);
break;
2015-06-03 16:33:59 +03:00
}
2014-02-12 07:34:33 +04:00
}
if (error && drive->rdpcontext)
setChannelError(drive->rdpcontext, error, "drive_thread_func reported an error");
ExitThread((DWORD)error);
2014-02-12 07:34:33 +04:00
return NULL;
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT drive_irp_request(DEVICE* device, IRP* irp)
2011-08-05 20:12:16 +04:00
{
2013-11-04 04:10:33 +04:00
DRIVE_DEVICE* drive = (DRIVE_DEVICE*) device;
if (!MessageQueue_Post(drive->IrpQueue, NULL, 0, (void*) irp, NULL))
{
WLog_ERR(TAG, "MessageQueue_Post failed!");
return ERROR_INTERNAL_ERROR;
}
2015-06-03 16:33:59 +03:00
return CHANNEL_RC_OK;
2011-08-05 20:12:16 +04:00
}
2017-07-13 14:28:00 +03:00
static void drive_free_resources(DRIVE_DEVICE* drive)
{
if (!drive)
return;
CloseHandle(drive->thread);
ListDictionary_Free(drive->files);
MessageQueue_Free(drive->IrpQueue);
Stream_Free(drive->device.data, TRUE);
free(drive->path);
free(drive);
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT drive_free(DEVICE* device)
2011-08-05 20:12:16 +04:00
{
2013-11-04 04:10:33 +04:00
DRIVE_DEVICE* drive = (DRIVE_DEVICE*) device;
UINT error = CHANNEL_RC_OK;
2013-11-04 04:10:33 +04:00
if (MessageQueue_PostQuit(drive->IrpQueue, 0)
&& (WaitForSingleObject(drive->thread, INFINITE) == WAIT_FAILED))
{
error = GetLastError();
WLog_ERR(TAG, "WaitForSingleObject failed with error %"PRIu32"", error);
return error;
}
2017-07-13 14:28:00 +03:00
drive_free_resources(drive);
return error;
2011-08-05 20:12:16 +04:00
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
UINT drive_register_drive_path(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints,
char* name, char* path)
2011-08-05 20:12:16 +04:00
{
int i, length;
2013-11-04 04:10:33 +04:00
DRIVE_DEVICE* drive;
UINT error;
#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
*/
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])
{
2017-05-18 10:43:02 +03:00
size_t pathLength = strnlen(path, MAX_PATH);
2015-06-03 16:33:59 +03:00
drive = (DRIVE_DEVICE*) calloc(1, sizeof(DRIVE_DEVICE));
2015-06-03 16:33:59 +03:00
if (!drive)
{
WLog_ERR(TAG, "calloc failed!");
return CHANNEL_RC_NO_MEMORY;
}
2011-08-05 20:12:16 +04:00
2013-11-04 04:10:33 +04:00
drive->device.type = RDPDR_DTYP_FILESYSTEM;
drive->device.name = name;
drive->device.IRPRequest = drive_irp_request;
drive->device.Free = drive_free;
drive->rdpcontext = pEntryPoints->rdpcontext;
2014-02-10 10:06:11 +04:00
length = (int) strlen(name);
2013-11-04 04:10:33 +04:00
drive->device.data = Stream_New(NULL, length + 1);
2015-06-03 16:33:59 +03:00
if (!drive->device.data)
{
WLog_ERR(TAG, "Stream_New failed!");
error = CHANNEL_RC_NO_MEMORY;
goto out_error;
}
for (i = 0; i <= length; i++)
2013-11-04 04:10:33 +04:00
Stream_Write_UINT8(drive->device.data, name[i] < 0 ? '_' : name[i]);
2011-08-05 20:12:16 +04:00
if ((pathLength > 1) && (path[pathLength-1] == '/'))
pathLength --;
if (ConvertToUnicode(sys_code_page, 0, path, pathLength, &drive->path, 0) <= 0)
{
WLog_ERR(TAG, "ConvertToUnicode failed!");
error = CHANNEL_RC_NO_MEMORY;
goto out_error;
}
2013-11-04 04:10:33 +04:00
drive->files = ListDictionary_New(TRUE);
2015-06-03 16:33:59 +03:00
if (!drive->files)
{
WLog_ERR(TAG, "ListDictionary_New failed!");
error = CHANNEL_RC_NO_MEMORY;
goto out_error;
}
2011-08-05 20:12:16 +04:00
ListDictionary_ValueObject(drive->files)->fnObjectFree = (OBJECT_FREE_FN) drive_file_free;
2013-11-12 18:18:18 +04:00
drive->IrpQueue = MessageQueue_New(NULL);
2015-06-03 16:33:59 +03:00
if (!drive->IrpQueue)
{
WLog_ERR(TAG, "ListDictionary_New failed!");
error = CHANNEL_RC_NO_MEMORY;
goto out_error;
}
if ((error = pEntryPoints->RegisterDevice(pEntryPoints->devman,
(DEVICE*) drive)))
2015-06-03 16:33:59 +03:00
{
WLog_ERR(TAG, "RegisterDevice failed with error %"PRIu32"!", error);
2015-06-03 16:33:59 +03:00
goto out_error;
}
if (!(drive->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) drive_thread_func, drive,
CREATE_SUSPENDED, NULL)))
2015-06-03 16:33:59 +03:00
{
WLog_ERR(TAG, "CreateThread failed!");
goto out_error;
}
2013-11-04 04:10:33 +04:00
ResumeThread(drive->thread);
2011-08-05 20:12:16 +04:00
}
2015-06-03 16:33:59 +03:00
return CHANNEL_RC_OK;
out_error:
2017-07-13 14:28:00 +03:00
drive_free_resources(drive);
2015-06-03 16:33:59 +03:00
return error;
2011-08-05 20:12:16 +04:00
}
#ifdef BUILTIN_CHANNELS
#define DeviceServiceEntry drive_DeviceServiceEntry
2016-02-29 17:18:19 +03:00
#else
#define DeviceServiceEntry FREERDP_API DeviceServiceEntry
#endif
UINT sys_code_page = 0;
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
UINT DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints)
{
RDPDR_DRIVE* drive;
UINT error;
#ifdef WIN32
char* dev;
int len;
char devlist[512], buf[512];
char* bufdup;
char* devdup;
#endif
drive = (RDPDR_DRIVE*) pEntryPoints->device;
#ifndef WIN32
sys_code_page = CP_UTF8;
2013-02-18 15:39:17 +04:00
if (strcmp(drive->Path, "*") == 0)
{
/* all drives */
2013-02-18 15:39:17 +04:00
free(drive->Path);
drive->Path = _strdup("/");
2015-06-03 16:33:59 +03:00
if (!drive->Path)
{
WLog_ERR(TAG, "_strdup failed!");
return CHANNEL_RC_NO_MEMORY;
}
}
2013-02-18 15:39:17 +04:00
else if (strcmp(drive->Path, "%") == 0)
{
char* home_env = NULL;
/* home directory */
home_env = getenv("HOME");
2013-02-18 15:39:17 +04:00
free(drive->Path);
if (home_env)
2015-06-03 16:33:59 +03:00
{
2013-02-18 15:39:17 +04:00
drive->Path = _strdup(home_env);
2015-06-03 16:33:59 +03:00
if (!drive->Path)
{
WLog_ERR(TAG, "_strdup failed!");
return CHANNEL_RC_NO_MEMORY;
}
}
else
2015-06-03 16:33:59 +03:00
{
2013-02-18 15:39:17 +04:00
drive->Path = _strdup("/");
2015-06-03 16:33:59 +03:00
if (!drive->Path)
{
WLog_ERR(TAG, "_strdup failed!");
return CHANNEL_RC_NO_MEMORY;
}
}
}
2015-06-03 16:33:59 +03:00
error = drive_register_drive_path(pEntryPoints, drive->Name, drive->Path);
#else
sys_code_page = GetACP();
2014-02-12 07:34:33 +04:00
/* Special case: path[0] == '*' -> export all drives */
2012-10-02 19:11:11 +04:00
/* Special case: path[0] == '%' -> user home dir */
2014-02-12 07:34:33 +04:00
if (strcmp(drive->Path, "%") == 0)
{
2016-03-29 23:03:15 +03:00
GetEnvironmentVariableA("USERPROFILE", buf, sizeof(buf));
PathCchAddBackslashA(buf, sizeof(buf));
2015-06-03 16:33:59 +03:00
free(drive->Path);
drive->Path = _strdup(buf);
2015-06-03 16:33:59 +03:00
if (!drive->Path)
{
WLog_ERR(TAG, "_strdup failed!");
return CHANNEL_RC_NO_MEMORY;
}
2015-06-03 16:33:59 +03:00
error = drive_register_drive_path(pEntryPoints, drive->Name, drive->Path);
}
2013-02-18 15:39:17 +04:00
else if (strcmp(drive->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 19:11:11 +04:00
if (*dev > 'B')
2014-02-12 07:34:33 +04:00
{
2012-10-02 19:11:11 +04:00
/* Suppress disk drives A and B to avoid pesty messages */
2015-07-03 14:26:15 +03:00
len = sprintf_s(buf, sizeof(buf) - 4, "%s", drive->Name);
2012-10-02 19:11:11 +04:00
buf[len] = '_';
buf[len + 1] = dev[0];
buf[len + 2] = 0;
buf[len + 3] = 0;
2015-06-03 16:33:59 +03:00
if (!(bufdup = _strdup(buf)))
{
WLog_ERR(TAG, "_strdup failed!");
return CHANNEL_RC_NO_MEMORY;
}
2015-06-03 16:33:59 +03:00
if (!(devdup = _strdup(dev)))
{
WLog_ERR(TAG, "_strdup failed!");
return CHANNEL_RC_NO_MEMORY;
}
if ((error = drive_register_drive_path(pEntryPoints, bufdup, devdup)))
{
break;
}
}
}
}
2014-02-12 07:34:33 +04:00
else
{
2015-06-03 16:33:59 +03:00
error = drive_register_drive_path(pEntryPoints, drive->Name, drive->Path);
}
2014-02-12 07:34:33 +04:00
#endif
2015-06-03 16:33:59 +03:00
return error;
2014-02-12 07:34:33 +04:00
}