Blame channels/rdpdr/server/rdpdr_main.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * Device Redirection Virtual Channel Extension
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2014 Dell Software <Mike.McDonald@software.dell.com>
Packit 1fb8d4
 * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
Packit 1fb8d4
 * Copyright 2015 Thincast Technologies GmbH
Packit 1fb8d4
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
#include <winpr/nt.h>
Packit 1fb8d4
#include <winpr/print.h>
Packit 1fb8d4
#include <winpr/stream.h>
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/channels/log.h>
Packit 1fb8d4
#include "rdpdr_main.h"
Packit 1fb8d4
Packit 1fb8d4
#define TAG "rdpdr.server"
Packit 1fb8d4
Packit 1fb8d4
static UINT32 g_ClientId = 0;
Packit 1fb8d4
Packit 1fb8d4
static RDPDR_IRP* rdpdr_server_irp_new()
Packit 1fb8d4
{
Packit 1fb8d4
	RDPDR_IRP* irp;
Packit Service 5a9772
	irp = (RDPDR_IRP*)calloc(1, sizeof(RDPDR_IRP));
Packit 1fb8d4
	return irp;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void rdpdr_server_irp_free(RDPDR_IRP* irp)
Packit 1fb8d4
{
Packit 1fb8d4
	free(irp);
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL rdpdr_server_enqueue_irp(RdpdrServerContext* context, RDPDR_IRP* irp)
Packit 1fb8d4
{
Packit Service 5a9772
	return ListDictionary_Add(context->priv->IrpList, (void*)(size_t)irp->CompletionId, irp);
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static RDPDR_IRP* rdpdr_server_dequeue_irp(RdpdrServerContext* context, UINT32 completionId)
Packit 1fb8d4
{
Packit 1fb8d4
	RDPDR_IRP* irp;
Packit Service 5a9772
	irp = (RDPDR_IRP*)ListDictionary_Remove(context->priv->IrpList, (void*)(size_t)completionId);
Packit 1fb8d4
	return irp;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT rdpdr_server_send_announce_request(RdpdrServerContext* context)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	BOOL status;
Packit 1fb8d4
	RDPDR_HEADER header;
Packit 1fb8d4
	ULONG written;
Packit 1fb8d4
	WLog_DBG(TAG, "RdpdrServerSendAnnounceRequest");
Packit 1fb8d4
	header.Component = RDPDR_CTYP_CORE;
Packit 1fb8d4
	header.PacketId = PAKID_CORE_SERVER_ANNOUNCE;
Packit 1fb8d4
	s = Stream_New(NULL, RDPDR_HEADER_LENGTH + 8);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_New failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Write_UINT16(s, header.Component);            /* Component (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT16(s, header.PacketId);             /* PacketId (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT16(s, context->priv->VersionMajor); /* VersionMajor (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT16(s, context->priv->VersionMinor); /* VersionMinor (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, context->priv->ClientId);     /* ClientId (4 bytes) */
Packit 1fb8d4
	Stream_SealLength(s);
Packit 1fb8d4
	winpr_HexDump(TAG, WLOG_DEBUG, Stream_Buffer(s), Stream_Length(s));
Packit Service 5a9772
	status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR)Stream_Buffer(s),
Packit Service 5a9772
	                                Stream_Length(s), &written);
Packit 1fb8d4
	Stream_Free(s, TRUE);
Packit 1fb8d4
	return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_receive_announce_response(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                   RDPDR_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 ClientId;
Packit 1fb8d4
	UINT16 VersionMajor;
Packit 1fb8d4
	UINT16 VersionMinor;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 8)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT16(s, VersionMajor); /* VersionMajor (2 bytes) */
Packit 1fb8d4
	Stream_Read_UINT16(s, VersionMinor); /* VersionMinor (2 bytes) */
Packit Service 5a9772
	Stream_Read_UINT32(s, ClientId);     /* ClientId (4 bytes) */
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "Client Announce Response: VersionMajor: 0x%08" PRIX16 " VersionMinor: 0x%04" PRIX16
Packit Service 5a9772
	         " ClientId: 0x%08" PRIX32 "",
Packit 1fb8d4
	         VersionMajor, VersionMinor, ClientId);
Packit 1fb8d4
	context->priv->ClientId = ClientId;
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_receive_client_name_request(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                     RDPDR_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 UnicodeFlag;
Packit 1fb8d4
	UINT32 ComputerNameLen;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 12)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Read_UINT32(s, UnicodeFlag);     /* UnicodeFlag (4 bytes) */
Packit Service 5a9772
	Stream_Seek_UINT32(s);                  /* CodePage (4 bytes), MUST be set to zero */
Packit 1fb8d4
	Stream_Read_UINT32(s, ComputerNameLen); /* ComputerNameLen (4 bytes) */
Packit 1fb8d4
	/* UnicodeFlag is either 0 or 1, the other 31 bits must be ignored.
Packit 1fb8d4
	 */
Packit 1fb8d4
	UnicodeFlag = UnicodeFlag & 0x00000001;
Packit 1fb8d4
Packit 1fb8d4
	/**
Packit 1fb8d4
	 * Caution: ComputerNameLen is given *bytes*,
Packit 1fb8d4
	 * not in characters, including the NULL terminator!
Packit 1fb8d4
	 */
Packit 1fb8d4
Packit 1fb8d4
	if (UnicodeFlag)
Packit 1fb8d4
	{
Packit 1fb8d4
		if ((ComputerNameLen % 2) || ComputerNameLen > 512 || ComputerNameLen < 2)
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_ERR(TAG, "invalid unicode computer name length: %" PRIu32 "", ComputerNameLen);
Packit 1fb8d4
			return ERROR_INVALID_DATA;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		if (ComputerNameLen > 256 || ComputerNameLen < 1)
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_ERR(TAG, "invalid ascii computer name length: %" PRIu32 "", ComputerNameLen);
Packit 1fb8d4
			return ERROR_INVALID_DATA;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < ComputerNameLen)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* ComputerName must be null terminated, check if it really is */
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_Pointer(s)[ComputerNameLen - 1] ||
Packit 1fb8d4
	    (UnicodeFlag && Stream_Pointer(s)[ComputerNameLen - 2]))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "computer name must be null terminated");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (context->priv->ClientComputerName)
Packit 1fb8d4
	{
Packit 1fb8d4
		free(context->priv->ClientComputerName);
Packit 1fb8d4
		context->priv->ClientComputerName = NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (UnicodeFlag)
Packit 1fb8d4
	{
Packit Service 5a9772
		if (ConvertFromUnicode(CP_UTF8, 0, (WCHAR*)Stream_Pointer(s), -1,
Packit 1fb8d4
		                       &(context->priv->ClientComputerName), 0, NULL, NULL) < 1)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "failed to convert client computer name");
Packit 1fb8d4
			return ERROR_INVALID_DATA;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit Service 5a9772
		context->priv->ClientComputerName = _strdup((char*)Stream_Pointer(s));
Packit 1fb8d4
Packit 1fb8d4
		if (!context->priv->ClientComputerName)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "failed to duplicate client computer name");
Packit 1fb8d4
			return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Seek(s, ComputerNameLen);
Packit 1fb8d4
	WLog_DBG(TAG, "ClientComputerName: %s", context->priv->ClientComputerName);
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_read_capability_set_header(wStream* s, RDPDR_CAPABILITY_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 8)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Read_UINT16(s, header->CapabilityType);   /* CapabilityType (2 bytes) */
Packit Service 5a9772
	Stream_Read_UINT16(s, header->CapabilityLength); /* CapabilityLength (2 bytes) */
Packit Service 5a9772
	Stream_Read_UINT32(s, header->Version);          /* Version (4 bytes) */
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_write_capability_set_header(wStream* s, RDPDR_CAPABILITY_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	if (!Stream_EnsureRemainingCapacity(s, 8))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Write_UINT16(s, header->CapabilityType);   /* CapabilityType (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT16(s, header->CapabilityLength); /* CapabilityLength (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, header->Version);          /* Version (4 bytes) */
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_read_general_capability_set(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                     RDPDR_CAPABILITY_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 ioCode1;
Packit 1fb8d4
	UINT32 extraFlags1;
Packit 1fb8d4
	UINT32 extendedPdu;
Packit 1fb8d4
	UINT16 VersionMajor;
Packit 1fb8d4
	UINT16 VersionMinor;
Packit 1fb8d4
	UINT32 SpecialTypeDeviceCap;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 32)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Seek_UINT32(s);               /* osType (4 bytes), ignored on receipt */
Packit Service 5a9772
	Stream_Seek_UINT32(s);               /* osVersion (4 bytes), unused and must be set to zero */
Packit 1fb8d4
	Stream_Read_UINT16(s, VersionMajor); /* protocolMajorVersion (2 bytes) */
Packit 1fb8d4
	Stream_Read_UINT16(s, VersionMinor); /* protocolMinorVersion (2 bytes) */
Packit Service 5a9772
	Stream_Read_UINT32(s, ioCode1);      /* ioCode1 (4 bytes) */
Packit Service 5a9772
	Stream_Seek_UINT32(s); /* ioCode2 (4 bytes), must be set to zero, reserved for future use */
Packit 1fb8d4
	Stream_Read_UINT32(s, extendedPdu); /* extendedPdu (4 bytes) */
Packit 1fb8d4
	Stream_Read_UINT32(s, extraFlags1); /* extraFlags1 (4 bytes) */
Packit Service 5a9772
	Stream_Seek_UINT32(s); /* extraFlags2 (4 bytes), must be set to zero, reserved for future use */
Packit 1fb8d4
Packit 1fb8d4
	if (header->Version == GENERAL_CAPABILITY_VERSION_02)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
			return ERROR_INVALID_DATA;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit Service 5a9772
		Stream_Read_UINT32(s, SpecialTypeDeviceCap); /* SpecialTypeDeviceCap (4 bytes) */
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	context->priv->UserLoggedOnPdu = (extendedPdu & RDPDR_USER_LOGGEDON_PDU) ? TRUE : FALSE;
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_write_general_capability_set(RdpdrServerContext* context, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 ioCode1;
Packit 1fb8d4
	UINT32 extendedPdu;
Packit 1fb8d4
	UINT32 extraFlags1;
Packit 1fb8d4
	UINT32 SpecialTypeDeviceCap;
Packit 1fb8d4
	RDPDR_CAPABILITY_HEADER header;
Packit 1fb8d4
	header.CapabilityType = CAP_GENERAL_TYPE;
Packit 1fb8d4
	header.CapabilityLength = RDPDR_CAPABILITY_HEADER_LENGTH + 36;
Packit 1fb8d4
	header.Version = GENERAL_CAPABILITY_VERSION_02;
Packit 1fb8d4
	ioCode1 = 0;
Packit Service 5a9772
	ioCode1 |= RDPDR_IRP_MJ_CREATE;                   /* always set */
Packit Service 5a9772
	ioCode1 |= RDPDR_IRP_MJ_CLEANUP;                  /* always set */
Packit Service 5a9772
	ioCode1 |= RDPDR_IRP_MJ_CLOSE;                    /* always set */
Packit Service 5a9772
	ioCode1 |= RDPDR_IRP_MJ_READ;                     /* always set */
Packit Service 5a9772
	ioCode1 |= RDPDR_IRP_MJ_WRITE;                    /* always set */
Packit Service 5a9772
	ioCode1 |= RDPDR_IRP_MJ_FLUSH_BUFFERS;            /* always set */
Packit Service 5a9772
	ioCode1 |= RDPDR_IRP_MJ_SHUTDOWN;                 /* always set */
Packit Service 5a9772
	ioCode1 |= RDPDR_IRP_MJ_DEVICE_CONTROL;           /* always set */
Packit 1fb8d4
	ioCode1 |= RDPDR_IRP_MJ_QUERY_VOLUME_INFORMATION; /* always set */
Packit Service 5a9772
	ioCode1 |= RDPDR_IRP_MJ_SET_VOLUME_INFORMATION;   /* always set */
Packit Service 5a9772
	ioCode1 |= RDPDR_IRP_MJ_QUERY_INFORMATION;        /* always set */
Packit Service 5a9772
	ioCode1 |= RDPDR_IRP_MJ_SET_INFORMATION;          /* always set */
Packit Service 5a9772
	ioCode1 |= RDPDR_IRP_MJ_DIRECTORY_CONTROL;        /* always set */
Packit Service 5a9772
	ioCode1 |= RDPDR_IRP_MJ_LOCK_CONTROL;             /* always set */
Packit Service 5a9772
	ioCode1 |= RDPDR_IRP_MJ_QUERY_SECURITY;           /* optional */
Packit Service 5a9772
	ioCode1 |= RDPDR_IRP_MJ_SET_SECURITY;             /* optional */
Packit 1fb8d4
	extendedPdu = 0;
Packit 1fb8d4
	extendedPdu |= RDPDR_CLIENT_DISPLAY_NAME_PDU; /* always set */
Packit Service 5a9772
	extendedPdu |= RDPDR_DEVICE_REMOVE_PDUS;      /* optional */
Packit 1fb8d4
Packit 1fb8d4
	if (context->priv->UserLoggedOnPdu)
Packit 1fb8d4
		extendedPdu |= RDPDR_USER_LOGGEDON_PDU; /* optional */
Packit 1fb8d4
Packit 1fb8d4
	extraFlags1 = 0;
Packit 1fb8d4
	extraFlags1 |= ENABLE_ASYNCIO; /* optional */
Packit 1fb8d4
	SpecialTypeDeviceCap = 0;
Packit 1fb8d4
Packit 1fb8d4
	if (!Stream_EnsureRemainingCapacity(s, header.CapabilityLength))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	rdpdr_server_write_capability_set_header(s, &header);
Packit 1fb8d4
	Stream_Write_UINT32(s, 0); /* osType (4 bytes), ignored on receipt */
Packit Service 5a9772
	Stream_Write_UINT32(s, 0); /* osVersion (4 bytes), unused and must be set to zero */
Packit Service 5a9772
	Stream_Write_UINT16(s, context->priv->VersionMajor); /* protocolMajorVersion (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT16(s, context->priv->VersionMinor); /* protocolMinorVersion (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, ioCode1);                     /* ioCode1 (4 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, 0); /* ioCode2 (4 bytes), must be set to zero, reserved for future use */
Packit 1fb8d4
	Stream_Write_UINT32(s, extendedPdu); /* extendedPdu (4 bytes) */
Packit 1fb8d4
	Stream_Write_UINT32(s, extraFlags1); /* extraFlags1 (4 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(
Packit Service 5a9772
	    s, 0); /* extraFlags2 (4 bytes), must be set to zero, reserved for future use */
Packit Service 5a9772
	Stream_Write_UINT32(s, SpecialTypeDeviceCap); /* SpecialTypeDeviceCap (4 bytes) */
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_read_printer_capability_set(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                     RDPDR_CAPABILITY_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_write_printer_capability_set(RdpdrServerContext* context, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	RDPDR_CAPABILITY_HEADER header;
Packit 1fb8d4
	header.CapabilityType = CAP_PRINTER_TYPE;
Packit 1fb8d4
	header.CapabilityLength = RDPDR_CAPABILITY_HEADER_LENGTH;
Packit 1fb8d4
	header.Version = PRINT_CAPABILITY_VERSION_01;
Packit 1fb8d4
Packit 1fb8d4
	if (!Stream_EnsureRemainingCapacity(s, header.CapabilityLength))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return rdpdr_server_write_capability_set_header(s, &header);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_read_port_capability_set(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                  RDPDR_CAPABILITY_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_write_port_capability_set(RdpdrServerContext* context, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	RDPDR_CAPABILITY_HEADER header;
Packit 1fb8d4
	header.CapabilityType = CAP_PORT_TYPE;
Packit 1fb8d4
	header.CapabilityLength = RDPDR_CAPABILITY_HEADER_LENGTH;
Packit 1fb8d4
	header.Version = PORT_CAPABILITY_VERSION_01;
Packit 1fb8d4
Packit 1fb8d4
	if (!Stream_EnsureRemainingCapacity(s, header.CapabilityLength))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return rdpdr_server_write_capability_set_header(s, &header);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_read_drive_capability_set(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                   RDPDR_CAPABILITY_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_write_drive_capability_set(RdpdrServerContext* context, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	RDPDR_CAPABILITY_HEADER header;
Packit 1fb8d4
	header.CapabilityType = CAP_DRIVE_TYPE;
Packit 1fb8d4
	header.CapabilityLength = RDPDR_CAPABILITY_HEADER_LENGTH;
Packit 1fb8d4
	header.Version = DRIVE_CAPABILITY_VERSION_02;
Packit 1fb8d4
Packit 1fb8d4
	if (!Stream_EnsureRemainingCapacity(s, header.CapabilityLength))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return rdpdr_server_write_capability_set_header(s, &header);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_read_smartcard_capability_set(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                       RDPDR_CAPABILITY_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_write_smartcard_capability_set(RdpdrServerContext* context, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	RDPDR_CAPABILITY_HEADER header;
Packit 1fb8d4
	header.CapabilityType = CAP_SMARTCARD_TYPE;
Packit 1fb8d4
	header.CapabilityLength = RDPDR_CAPABILITY_HEADER_LENGTH;
Packit 1fb8d4
	header.Version = SMARTCARD_CAPABILITY_VERSION_01;
Packit 1fb8d4
Packit 1fb8d4
	if (!Stream_EnsureRemainingCapacity(s, header.CapabilityLength))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
Packit 1fb8d4
		return CHANNEL_RC_OK;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return rdpdr_server_write_capability_set_header(s, &header);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_send_core_capability_request(RdpdrServerContext* context)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	BOOL status;
Packit 1fb8d4
	RDPDR_HEADER header;
Packit 1fb8d4
	UINT16 numCapabilities;
Packit 1fb8d4
	ULONG written;
Packit 1fb8d4
	UINT error;
Packit 1fb8d4
	WLog_DBG(TAG, "RdpdrServerSendCoreCapabilityRequest");
Packit 1fb8d4
	header.Component = RDPDR_CTYP_CORE;
Packit 1fb8d4
	header.PacketId = PAKID_CORE_SERVER_CAPABILITY;
Packit 1fb8d4
	numCapabilities = 1;
Packit 1fb8d4
Packit 1fb8d4
	if (context->supportsDrives)
Packit 1fb8d4
		numCapabilities++;
Packit 1fb8d4
Packit 1fb8d4
	if (context->supportsPorts)
Packit 1fb8d4
		numCapabilities++;
Packit 1fb8d4
Packit 1fb8d4
	if (context->supportsPrinters)
Packit 1fb8d4
		numCapabilities++;
Packit 1fb8d4
Packit 1fb8d4
	if (context->supportsSmartcards)
Packit 1fb8d4
		numCapabilities++;
Packit 1fb8d4
Packit 1fb8d4
	s = Stream_New(NULL, RDPDR_HEADER_LENGTH + 512);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_New failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write_UINT16(s, header.Component); /* Component (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT16(s, header.PacketId);  /* PacketId (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT16(s, numCapabilities);  /* numCapabilities (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT16(s, 0);                /* Padding (2 bytes) */
Packit 1fb8d4
Packit 1fb8d4
	if ((error = rdpdr_server_write_general_capability_set(context, s)))
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "rdpdr_server_write_general_capability_set failed with error %" PRIu32 "!",
Packit Service 5a9772
		         error);
Packit 1fb8d4
		goto out;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (context->supportsDrives)
Packit 1fb8d4
	{
Packit 1fb8d4
		if ((error = rdpdr_server_write_drive_capability_set(context, s)))
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_ERR(TAG, "rdpdr_server_write_drive_capability_set failed with error %" PRIu32 "!",
Packit 1fb8d4
			         error);
Packit 1fb8d4
			goto out;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (context->supportsPorts)
Packit 1fb8d4
	{
Packit 1fb8d4
		if ((error = rdpdr_server_write_port_capability_set(context, s)))
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_ERR(TAG, "rdpdr_server_write_port_capability_set failed with error %" PRIu32 "!",
Packit 1fb8d4
			         error);
Packit 1fb8d4
			goto out;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (context->supportsPrinters)
Packit 1fb8d4
	{
Packit 1fb8d4
		if ((error = rdpdr_server_write_printer_capability_set(context, s)))
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG,
Packit Service 5a9772
			         "rdpdr_server_write_printer_capability_set failed with error %" PRIu32 "!",
Packit Service 5a9772
			         error);
Packit 1fb8d4
			goto out;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (context->supportsSmartcards)
Packit 1fb8d4
	{
Packit 1fb8d4
		if ((error = rdpdr_server_write_smartcard_capability_set(context, s)))
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG,
Packit Service 5a9772
			         "rdpdr_server_write_printer_capability_set failed with error %" PRIu32 "!",
Packit Service 5a9772
			         error);
Packit 1fb8d4
			goto out;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_SealLength(s);
Packit 1fb8d4
	winpr_HexDump(TAG, WLOG_DEBUG, Stream_Buffer(s), Stream_Length(s));
Packit Service 5a9772
	status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR)Stream_Buffer(s),
Packit Service 5a9772
	                                Stream_Length(s), &written);
Packit 1fb8d4
	Stream_Free(s, TRUE);
Packit 1fb8d4
	return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
Packit 1fb8d4
out:
Packit 1fb8d4
	Stream_Free(s, TRUE);
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_receive_core_capability_response(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                          RDPDR_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	int i;
Packit 1fb8d4
	UINT status;
Packit 1fb8d4
	UINT16 numCapabilities;
Packit 1fb8d4
	RDPDR_CAPABILITY_HEADER capabilityHeader;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT16(s, numCapabilities); /* numCapabilities (2 bytes) */
Packit Service 5a9772
	Stream_Seek_UINT16(s);                  /* Padding (2 bytes) */
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < numCapabilities; i++)
Packit 1fb8d4
	{
Packit 1fb8d4
		if ((status = rdpdr_server_read_capability_set_header(s, &capabilityHeader)))
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_ERR(TAG, "rdpdr_server_read_capability_set_header failed with error %" PRIu32 "!",
Packit 1fb8d4
			         status);
Packit 1fb8d4
			return status;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		switch (capabilityHeader.CapabilityType)
Packit 1fb8d4
		{
Packit 1fb8d4
			case CAP_GENERAL_TYPE:
Packit Service 5a9772
				if ((status =
Packit Service 5a9772
				         rdpdr_server_read_general_capability_set(context, s, &capabilityHeader)))
Packit 1fb8d4
				{
Packit Service 5a9772
					WLog_ERR(TAG,
Packit Service 5a9772
					         "rdpdr_server_read_general_capability_set failed with error %" PRIu32
Packit Service 5a9772
					         "!",
Packit 1fb8d4
					         status);
Packit 1fb8d4
					return status;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case CAP_PRINTER_TYPE:
Packit Service 5a9772
				if ((status =
Packit Service 5a9772
				         rdpdr_server_read_printer_capability_set(context, s, &capabilityHeader)))
Packit 1fb8d4
				{
Packit Service 5a9772
					WLog_ERR(TAG,
Packit Service 5a9772
					         "rdpdr_server_read_printer_capability_set failed with error %" PRIu32
Packit Service 5a9772
					         "!",
Packit 1fb8d4
					         status);
Packit 1fb8d4
					return status;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case CAP_PORT_TYPE:
Packit Service 5a9772
				if ((status = rdpdr_server_read_port_capability_set(context, s, &capabilityHeader)))
Packit 1fb8d4
				{
Packit Service 5a9772
					WLog_ERR(TAG,
Packit Service 5a9772
					         "rdpdr_server_read_port_capability_set failed with error %" PRIu32 "!",
Packit 1fb8d4
					         status);
Packit 1fb8d4
					return status;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case CAP_DRIVE_TYPE:
Packit Service 5a9772
				if ((status =
Packit Service 5a9772
				         rdpdr_server_read_drive_capability_set(context, s, &capabilityHeader)))
Packit 1fb8d4
				{
Packit Service 5a9772
					WLog_ERR(TAG,
Packit Service 5a9772
					         "rdpdr_server_read_drive_capability_set failed with error %" PRIu32
Packit Service 5a9772
					         "!",
Packit 1fb8d4
					         status);
Packit 1fb8d4
					return status;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case CAP_SMARTCARD_TYPE:
Packit Service 5a9772
				if ((status =
Packit Service 5a9772
				         rdpdr_server_read_smartcard_capability_set(context, s, &capabilityHeader)))
Packit 1fb8d4
				{
Packit 1fb8d4
					WLog_ERR(TAG,
Packit Service 5a9772
					         "rdpdr_server_read_smartcard_capability_set failed with error %" PRIu32
Packit Service 5a9772
					         "!",
Packit Service 5a9772
					         status);
Packit 1fb8d4
					return status;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			default:
Packit Service 5a9772
				WLog_DBG(TAG, "Unknown capabilityType %" PRIu16 "",
Packit Service 5a9772
				         capabilityHeader.CapabilityType);
Packit Service 5a9772
				Stream_Seek(s, capabilityHeader.CapabilityLength - RDPDR_CAPABILITY_HEADER_LENGTH);
Packit 1fb8d4
				return ERROR_INVALID_DATA;
Packit 1fb8d4
				break;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT rdpdr_server_send_client_id_confirm(RdpdrServerContext* context)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	BOOL status;
Packit 1fb8d4
	RDPDR_HEADER header;
Packit 1fb8d4
	ULONG written;
Packit 1fb8d4
	WLog_DBG(TAG, "RdpdrServerSendClientIdConfirm");
Packit 1fb8d4
	header.Component = RDPDR_CTYP_CORE;
Packit 1fb8d4
	header.PacketId = PAKID_CORE_CLIENTID_CONFIRM;
Packit 1fb8d4
	s = Stream_New(NULL, RDPDR_HEADER_LENGTH + 8);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_New failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Write_UINT16(s, header.Component);            /* Component (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT16(s, header.PacketId);             /* PacketId (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT16(s, context->priv->VersionMajor); /* VersionMajor (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT16(s, context->priv->VersionMinor); /* VersionMinor (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, context->priv->ClientId);     /* ClientId (4 bytes) */
Packit 1fb8d4
	Stream_SealLength(s);
Packit 1fb8d4
	winpr_HexDump(TAG, WLOG_DEBUG, Stream_Buffer(s), Stream_Length(s));
Packit Service 5a9772
	status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR)Stream_Buffer(s),
Packit Service 5a9772
	                                Stream_Length(s), &written);
Packit 1fb8d4
	Stream_Free(s, TRUE);
Packit 1fb8d4
	return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_receive_device_list_announce_request(RdpdrServerContext* context,
Packit Service 5a9772
                                                              wStream* s, RDPDR_HEADER* header)
Packit 1fb8d4
{
Packit Service 5a9772
	UINT32 i;
Packit 1fb8d4
	UINT32 DeviceCount;
Packit 1fb8d4
	UINT32 DeviceType;
Packit 1fb8d4
	UINT32 DeviceId;
Packit 1fb8d4
	char PreferredDosName[9];
Packit 1fb8d4
	UINT32 DeviceDataLength;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, DeviceCount); /* DeviceCount (4 bytes) */
Packit Service 5a9772
	WLog_DBG(TAG, "DeviceCount: %" PRIu32 "", DeviceCount);
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < DeviceCount; i++)
Packit 1fb8d4
	{
Packit 1fb8d4
		ZeroMemory(PreferredDosName, sizeof(PreferredDosName));
Packit 1fb8d4
Packit 1fb8d4
		if (Stream_GetRemainingLength(s) < 20)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
			return ERROR_INVALID_DATA;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit Service 5a9772
		Stream_Read_UINT32(s, DeviceType);       /* DeviceType (4 bytes) */
Packit Service 5a9772
		Stream_Read_UINT32(s, DeviceId);         /* DeviceId (4 bytes) */
Packit Service 5a9772
		Stream_Read(s, PreferredDosName, 8);     /* PreferredDosName (8 bytes) */
Packit 1fb8d4
		Stream_Read_UINT32(s, DeviceDataLength); /* DeviceDataLength (4 bytes) */
Packit 1fb8d4
Packit 1fb8d4
		if (Stream_GetRemainingLength(s) < DeviceDataLength)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
			return ERROR_INVALID_DATA;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit Service 5a9772
		WLog_DBG(TAG, "Device %d Name: %s Id: 0x%08" PRIX32 " DataLength: %" PRIu32 "", i,
Packit Service 5a9772
		         PreferredDosName, DeviceId, DeviceDataLength);
Packit 1fb8d4
Packit 1fb8d4
		switch (DeviceType)
Packit 1fb8d4
		{
Packit 1fb8d4
			case RDPDR_DTYP_FILESYSTEM:
Packit 1fb8d4
				if (context->supportsDrives)
Packit 1fb8d4
				{
Packit 1fb8d4
					IFCALL(context->OnDriveCreate, context, DeviceId, PreferredDosName);
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case RDPDR_DTYP_PRINT:
Packit 1fb8d4
				if (context->supportsPrinters)
Packit 1fb8d4
				{
Packit 1fb8d4
					IFCALL(context->OnPrinterCreate, context, DeviceId, PreferredDosName);
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case RDPDR_DTYP_SERIAL:
Packit 1fb8d4
			case RDPDR_DTYP_PARALLEL:
Packit 1fb8d4
				if (context->supportsPorts)
Packit 1fb8d4
				{
Packit 1fb8d4
					IFCALL(context->OnPortCreate, context, DeviceId, PreferredDosName);
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case RDPDR_DTYP_SMARTCARD:
Packit 1fb8d4
				if (context->supportsSmartcards)
Packit 1fb8d4
				{
Packit 1fb8d4
					IFCALL(context->OnSmartcardCreate, context, DeviceId, PreferredDosName);
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			default:
Packit 1fb8d4
				break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		Stream_Seek(s, DeviceDataLength);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_receive_device_list_remove_request(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                            RDPDR_HEADER* header)
Packit 1fb8d4
{
Packit Service 5a9772
	UINT32 i;
Packit 1fb8d4
	UINT32 DeviceCount;
Packit 1fb8d4
	UINT32 DeviceType;
Packit 1fb8d4
	UINT32 DeviceId;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, DeviceCount); /* DeviceCount (4 bytes) */
Packit Service 5a9772
	WLog_DBG(TAG, "DeviceCount: %" PRIu32 "", DeviceCount);
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < DeviceCount; i++)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
			return ERROR_INVALID_DATA;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		Stream_Read_UINT32(s, DeviceId); /* DeviceId (4 bytes) */
Packit Service 5a9772
		WLog_DBG(TAG, "Device %d Id: 0x%08" PRIX32 "", i, DeviceId);
Packit 1fb8d4
		DeviceType = 0; /* TODO: Save the device type on the announce request. */
Packit 1fb8d4
Packit 1fb8d4
		switch (DeviceType)
Packit 1fb8d4
		{
Packit 1fb8d4
			case RDPDR_DTYP_FILESYSTEM:
Packit 1fb8d4
				if (context->supportsDrives)
Packit 1fb8d4
				{
Packit 1fb8d4
					IFCALL(context->OnDriveDelete, context, DeviceId);
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case RDPDR_DTYP_PRINT:
Packit 1fb8d4
				if (context->supportsPrinters)
Packit 1fb8d4
				{
Packit 1fb8d4
					IFCALL(context->OnPrinterDelete, context, DeviceId);
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case RDPDR_DTYP_SERIAL:
Packit 1fb8d4
			case RDPDR_DTYP_PARALLEL:
Packit 1fb8d4
				if (context->supportsPorts)
Packit 1fb8d4
				{
Packit 1fb8d4
					IFCALL(context->OnPortDelete, context, DeviceId);
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case RDPDR_DTYP_SMARTCARD:
Packit 1fb8d4
				if (context->supportsSmartcards)
Packit 1fb8d4
				{
Packit 1fb8d4
					IFCALL(context->OnSmartcardDelete, context, DeviceId);
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			default:
Packit 1fb8d4
				break;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_receive_device_io_completion(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                      RDPDR_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 deviceId;
Packit 1fb8d4
	UINT32 completionId;
Packit 1fb8d4
	UINT32 ioStatus;
Packit 1fb8d4
	RDPDR_IRP* irp;
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 12)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, deviceId);
Packit 1fb8d4
	Stream_Read_UINT32(s, completionId);
Packit 1fb8d4
	Stream_Read_UINT32(s, ioStatus);
Packit Service 5a9772
	WLog_DBG(TAG, "deviceId=%" PRIu32 ", completionId=0x%" PRIx32 ", ioStatus=0x%" PRIx32 "",
Packit Service 5a9772
	         deviceId, completionId, ioStatus);
Packit 1fb8d4
	irp = rdpdr_server_dequeue_irp(context, completionId);
Packit 1fb8d4
Packit 1fb8d4
	if (!irp)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "IRP not found for completionId=0x%" PRIx32 "", completionId);
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Invoke the callback. */
Packit 1fb8d4
	if (irp->Callback)
Packit 1fb8d4
	{
Packit 1fb8d4
		error = (*irp->Callback)(context, s, irp, deviceId, completionId, ioStatus);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT rdpdr_server_send_user_logged_on(RdpdrServerContext* context)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	BOOL status;
Packit 1fb8d4
	RDPDR_HEADER header;
Packit 1fb8d4
	ULONG written;
Packit 1fb8d4
	WLog_DBG(TAG, "RdpdrServerSendUserLoggedOn");
Packit 1fb8d4
	header.Component = RDPDR_CTYP_CORE;
Packit 1fb8d4
	header.PacketId = PAKID_CORE_USER_LOGGEDON;
Packit 1fb8d4
	s = Stream_New(NULL, RDPDR_HEADER_LENGTH);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_New failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write_UINT16(s, header.Component); /* Component (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT16(s, header.PacketId);  /* PacketId (2 bytes) */
Packit 1fb8d4
	Stream_SealLength(s);
Packit 1fb8d4
	winpr_HexDump(TAG, WLOG_DEBUG, Stream_Buffer(s), Stream_Length(s));
Packit Service 5a9772
	status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR)Stream_Buffer(s),
Packit Service 5a9772
	                                Stream_Length(s), &written);
Packit 1fb8d4
	Stream_Free(s, TRUE);
Packit 1fb8d4
	return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_receive_pdu(RdpdrServerContext* context, wStream* s, RDPDR_HEADER* header)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit Service 5a9772
	WLog_DBG(TAG, "RdpdrServerReceivePdu: Component: 0x%04" PRIX16 " PacketId: 0x%04" PRIX16 "",
Packit 1fb8d4
	         header->Component, header->PacketId);
Packit 1fb8d4
	winpr_HexDump(TAG, WLOG_DEBUG, Stream_Buffer(s), Stream_Length(s));
Packit 1fb8d4
Packit 1fb8d4
	if (header->Component == RDPDR_CTYP_CORE)
Packit 1fb8d4
	{
Packit 1fb8d4
		switch (header->PacketId)
Packit 1fb8d4
		{
Packit 1fb8d4
			case PAKID_CORE_CLIENTID_CONFIRM:
Packit 1fb8d4
				if ((error = rdpdr_server_receive_announce_response(context, s, header)))
Packit 1fb8d4
				{
Packit Service 5a9772
					WLog_ERR(TAG,
Packit Service 5a9772
					         "rdpdr_server_receive_announce_response failed with error %" PRIu32
Packit Service 5a9772
					         "!",
Packit 1fb8d4
					         error);
Packit 1fb8d4
					return error;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case PAKID_CORE_CLIENT_NAME:
Packit 1fb8d4
				if ((error = rdpdr_server_receive_client_name_request(context, s, header)))
Packit 1fb8d4
				{
Packit Service 5a9772
					WLog_ERR(TAG,
Packit Service 5a9772
					         "rdpdr_server_receive_client_name_request failed with error %" PRIu32
Packit Service 5a9772
					         "!",
Packit 1fb8d4
					         error);
Packit 1fb8d4
					return error;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				if ((error = rdpdr_server_send_core_capability_request(context)))
Packit 1fb8d4
				{
Packit 1fb8d4
					WLog_ERR(TAG,
Packit Service 5a9772
					         "rdpdr_server_send_core_capability_request failed with error %" PRIu32
Packit Service 5a9772
					         "!",
Packit Service 5a9772
					         error);
Packit 1fb8d4
					return error;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				if ((error = rdpdr_server_send_client_id_confirm(context)))
Packit 1fb8d4
				{
Packit Service 5a9772
					WLog_ERR(TAG,
Packit Service 5a9772
					         "rdpdr_server_send_client_id_confirm failed with error %" PRIu32 "!",
Packit 1fb8d4
					         error);
Packit 1fb8d4
					return error;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case PAKID_CORE_CLIENT_CAPABILITY:
Packit 1fb8d4
				if ((error = rdpdr_server_receive_core_capability_response(context, s, header)))
Packit 1fb8d4
				{
Packit Service 5a9772
					WLog_ERR(
Packit Service 5a9772
					    TAG,
Packit Service 5a9772
					    "rdpdr_server_receive_core_capability_response failed with error %" PRIu32
Packit Service 5a9772
					    "!",
Packit Service 5a9772
					    error);
Packit 1fb8d4
					return error;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				if (context->priv->UserLoggedOnPdu)
Packit 1fb8d4
					if ((error = rdpdr_server_send_user_logged_on(context)))
Packit 1fb8d4
					{
Packit Service 5a9772
						WLog_ERR(TAG,
Packit Service 5a9772
						         "rdpdr_server_send_user_logged_on failed with error %" PRIu32 "!",
Packit Service 5a9772
						         error);
Packit 1fb8d4
						return error;
Packit 1fb8d4
					}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case PAKID_CORE_DEVICELIST_ANNOUNCE:
Packit Service 5a9772
				if ((error = rdpdr_server_receive_device_list_announce_request(context, s, header)))
Packit 1fb8d4
				{
Packit 1fb8d4
					WLog_ERR(TAG,
Packit Service 5a9772
					         "rdpdr_server_receive_device_list_announce_request failed with error "
Packit Service 5a9772
					         "%" PRIu32 "!",
Packit 1fb8d4
					         error);
Packit 1fb8d4
					return error;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case PAKID_CORE_DEVICE_REPLY:
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case PAKID_CORE_DEVICE_IOREQUEST:
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case PAKID_CORE_DEVICE_IOCOMPLETION:
Packit 1fb8d4
				if ((error = rdpdr_server_receive_device_io_completion(context, s, header)))
Packit 1fb8d4
				{
Packit 1fb8d4
					WLog_ERR(TAG,
Packit Service 5a9772
					         "rdpdr_server_receive_device_io_completion failed with error %" PRIu32
Packit Service 5a9772
					         "!",
Packit Service 5a9772
					         error);
Packit 1fb8d4
					return error;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case PAKID_CORE_DEVICELIST_REMOVE:
Packit Service 5a9772
				if ((error = rdpdr_server_receive_device_list_remove_request(context, s, header)))
Packit 1fb8d4
				{
Packit 1fb8d4
					WLog_ERR(TAG,
Packit Service 5a9772
					         "rdpdr_server_receive_device_io_completion failed with error %" PRIu32
Packit Service 5a9772
					         "!",
Packit Service 5a9772
					         error);
Packit 1fb8d4
					return error;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			default:
Packit 1fb8d4
				break;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
	else if (header->Component == RDPDR_CTYP_PRN)
Packit 1fb8d4
	{
Packit 1fb8d4
		switch (header->PacketId)
Packit 1fb8d4
		{
Packit 1fb8d4
			case PAKID_PRN_CACHE_DATA:
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case PAKID_PRN_USING_XPS:
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			default:
Packit 1fb8d4
				break;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_WARN(TAG, "Unknown RDPDR_HEADER.Component: 0x%04" PRIX16 "", header->Component);
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static DWORD WINAPI rdpdr_server_thread(LPVOID arg)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	DWORD status;
Packit 1fb8d4
	DWORD nCount;
Packit 1fb8d4
	void* buffer;
Packit 1fb8d4
	HANDLE events[8];
Packit 1fb8d4
	RDPDR_HEADER header;
Packit 1fb8d4
	HANDLE ChannelEvent;
Packit 1fb8d4
	DWORD BytesReturned;
Packit 1fb8d4
	RdpdrServerContext* context;
Packit 1fb8d4
	UINT error;
Packit Service 5a9772
	context = (RdpdrServerContext*)arg;
Packit 1fb8d4
	buffer = NULL;
Packit 1fb8d4
	BytesReturned = 0;
Packit 1fb8d4
	ChannelEvent = NULL;
Packit 1fb8d4
	s = Stream_New(NULL, 4096);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_New failed!");
Packit 1fb8d4
		error = CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
		goto out;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	if (WTSVirtualChannelQuery(context->priv->ChannelHandle, WTSVirtualEventHandle, &buffer,
Packit Service 5a9772
	                           &BytesReturned) == TRUE)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (BytesReturned == sizeof(HANDLE))
Packit 1fb8d4
			CopyMemory(&ChannelEvent, buffer, sizeof(HANDLE));
Packit 1fb8d4
Packit 1fb8d4
		WTSFreeMemory(buffer);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	nCount = 0;
Packit 1fb8d4
	events[nCount++] = ChannelEvent;
Packit 1fb8d4
	events[nCount++] = context->priv->StopEvent;
Packit 1fb8d4
Packit 1fb8d4
	if ((error = rdpdr_server_send_announce_request(context)))
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "rdpdr_server_send_announce_request failed with error %" PRIu32 "!", error);
Packit 1fb8d4
		goto out_stream;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	while (1)
Packit 1fb8d4
	{
Packit 1fb8d4
		BytesReturned = 0;
Packit 1fb8d4
		status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
Packit 1fb8d4
Packit 1fb8d4
		if (status == WAIT_FAILED)
Packit 1fb8d4
		{
Packit 1fb8d4
			error = GetLastError();
Packit Service 5a9772
			WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "!", error);
Packit 1fb8d4
			goto out_stream;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		status = WaitForSingleObject(context->priv->StopEvent, 0);
Packit 1fb8d4
Packit 1fb8d4
		if (status == WAIT_FAILED)
Packit 1fb8d4
		{
Packit 1fb8d4
			error = GetLastError();
Packit Service 5a9772
			WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", error);
Packit 1fb8d4
			goto out_stream;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (status == WAIT_OBJECT_0)
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit Service 5a9772
		if (!WTSVirtualChannelRead(context->priv->ChannelHandle, 0, (PCHAR)Stream_Buffer(s),
Packit Service 5a9772
		                           Stream_Capacity(s), &BytesReturned))
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "WTSVirtualChannelRead failed!");
Packit 1fb8d4
			error = ERROR_INTERNAL_ERROR;
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (BytesReturned >= RDPDR_HEADER_LENGTH)
Packit 1fb8d4
		{
Packit 1fb8d4
			Stream_SetPosition(s, 0);
Packit 1fb8d4
			Stream_SetLength(s, BytesReturned);
Packit 1fb8d4
Packit 1fb8d4
			while (Stream_GetRemainingLength(s) >= RDPDR_HEADER_LENGTH)
Packit 1fb8d4
			{
Packit 1fb8d4
				Stream_Read_UINT16(s, header.Component); /* Component (2 bytes) */
Packit Service 5a9772
				Stream_Read_UINT16(s, header.PacketId);  /* PacketId (2 bytes) */
Packit 1fb8d4
Packit 1fb8d4
				if ((error = rdpdr_server_receive_pdu(context, s, &header)))
Packit 1fb8d4
				{
Packit Service 5a9772
					WLog_ERR(TAG, "rdpdr_server_receive_pdu failed with error %" PRIu32 "!", error);
Packit 1fb8d4
					goto out_stream;
Packit 1fb8d4
				}
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
out_stream:
Packit 1fb8d4
	Stream_Free(s, TRUE);
Packit 1fb8d4
out:
Packit 1fb8d4
Packit 1fb8d4
	if (error && context->rdpcontext)
Packit Service 5a9772
		setChannelError(context->rdpcontext, error, "rdpdr_server_thread reported an error");
Packit 1fb8d4
Packit 1fb8d4
	ExitThread(error);
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT rdpdr_server_start(RdpdrServerContext* context)
Packit 1fb8d4
{
Packit Service 5a9772
	context->priv->ChannelHandle =
Packit Service 5a9772
	    WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, "rdpdr");
Packit 1fb8d4
Packit 1fb8d4
	if (!context->priv->ChannelHandle)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "WTSVirtualChannelOpen failed!");
Packit 1fb8d4
		return CHANNEL_RC_BAD_CHANNEL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!(context->priv->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "CreateEvent failed!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	if (!(context->priv->Thread =
Packit Service 5a9772
	          CreateThread(NULL, 0, rdpdr_server_thread, (void*)context, 0, NULL)))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "CreateThread failed!");
Packit 1fb8d4
		CloseHandle(context->priv->StopEvent);
Packit 1fb8d4
		context->priv->StopEvent = NULL;
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT rdpdr_server_stop(RdpdrServerContext* context)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT error;
Packit 1fb8d4
Packit 1fb8d4
	if (context->priv->StopEvent)
Packit 1fb8d4
	{
Packit 1fb8d4
		SetEvent(context->priv->StopEvent);
Packit 1fb8d4
Packit 1fb8d4
		if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED)
Packit 1fb8d4
		{
Packit 1fb8d4
			error = GetLastError();
Packit Service 5a9772
			WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", error);
Packit 1fb8d4
			return error;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		CloseHandle(context->priv->Thread);
Packit 1fb8d4
		context->priv->Thread = NULL;
Packit 1fb8d4
		CloseHandle(context->priv->StopEvent);
Packit 1fb8d4
		context->priv->StopEvent = NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static void rdpdr_server_write_device_iorequest(wStream* s, UINT32 deviceId, UINT32 fileId,
Packit Service 5a9772
                                                UINT32 completionId, UINT32 majorFunction,
Packit Service 5a9772
                                                UINT32 minorFunction)
Packit 1fb8d4
{
Packit Service 5a9772
	Stream_Write_UINT16(s, RDPDR_CTYP_CORE);             /* Component (2 bytes) */
Packit 1fb8d4
	Stream_Write_UINT16(s, PAKID_CORE_DEVICE_IOREQUEST); /* PacketId (2 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, deviceId);                    /* DeviceId (4 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, fileId);                      /* FileId (4 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, completionId);                /* CompletionId (4 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, majorFunction);               /* MajorFunction (4 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, minorFunction);               /* MinorFunction (4 bytes) */
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT rdpdr_server_read_file_directory_information(wStream* s,
Packit Service 5a9772
                                                         FILE_DIRECTORY_INFORMATION* fdi)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 fileNameLength;
Packit 1fb8d4
	ZeroMemory(fdi, sizeof(FILE_DIRECTORY_INFORMATION));
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 64)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, fdi->NextEntryOffset); /* NextEntryOffset (4 bytes) */
Packit Service 5a9772
	Stream_Read_UINT32(s, fdi->FileIndex);       /* FileIndex (4 bytes) */
Packit Service 5a9772
	Stream_Read_UINT64(s, fdi->CreationTime);    /* CreationTime (8 bytes) */
Packit Service 5a9772
	Stream_Read_UINT64(s, fdi->LastAccessTime);  /* LastAccessTime (8 bytes) */
Packit Service 5a9772
	Stream_Read_UINT64(s, fdi->LastWriteTime);   /* LastWriteTime (8 bytes) */
Packit Service 5a9772
	Stream_Read_UINT64(s, fdi->ChangeTime);      /* ChangeTime (8 bytes) */
Packit Service 5a9772
	Stream_Read_UINT64(s, fdi->EndOfFile);       /* EndOfFile (8 bytes) */
Packit Service 5a9772
	Stream_Read_UINT64(s, fdi->AllocationSize);  /* AllocationSize (8 bytes) */
Packit Service 5a9772
	Stream_Read_UINT32(s, fdi->FileAttributes);  /* FileAttributes (4 bytes) */
Packit Service 5a9772
	Stream_Read_UINT32(s, fileNameLength);       /* FileNameLength (4 bytes) */
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < fileNameLength)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)Stream_Pointer(s), fileNameLength / 2, fdi->FileName,
Packit Service 5a9772
	                    sizeof(fdi->FileName), NULL, NULL);
Packit 1fb8d4
	Stream_Seek(s, fileNameLength);
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_send_device_create_request(RdpdrServerContext* context, UINT32 deviceId,
Packit Service 5a9772
                                                    UINT32 completionId, const char* path,
Packit Service 5a9772
                                                    UINT32 desiredAccess, UINT32 createOptions,
Packit Service 5a9772
                                                    UINT32 createDisposition)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 pathLength;
Packit 1fb8d4
	ULONG written;
Packit 1fb8d4
	BOOL status;
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "RdpdrServerSendDeviceCreateRequest: deviceId=%" PRIu32
Packit Service 5a9772
	         ", path=%s, desiredAccess=0x%" PRIx32 " createOptions=0x%" PRIx32
Packit Service 5a9772
	         " createDisposition=0x%" PRIx32 "",
Packit 1fb8d4
	         deviceId, path, desiredAccess, createOptions, createDisposition);
Packit 1fb8d4
	/* Compute the required Unicode size. */
Packit 1fb8d4
	pathLength = (strlen(path) + 1) * sizeof(WCHAR);
Packit 1fb8d4
	s = Stream_New(NULL, 256 + pathLength);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_New failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	rdpdr_server_write_device_iorequest(s, deviceId, 0, completionId, IRP_MJ_CREATE, 0);
Packit 1fb8d4
	Stream_Write_UINT32(s, desiredAccess); /* DesiredAccess (4 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, 0);             /* AllocationSize (8 bytes) */
Packit 1fb8d4
	Stream_Write_UINT32(s, 0);
Packit Service 5a9772
	Stream_Write_UINT32(s, 0);                 /* FileAttributes (4 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, 3);                 /* SharedAccess (4 bytes) */
Packit 1fb8d4
	Stream_Write_UINT32(s, createDisposition); /* CreateDisposition (4 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, createOptions);     /* CreateOptions (4 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, pathLength);        /* PathLength (4 bytes) */
Packit 1fb8d4
	/* Convert the path to Unicode. */
Packit Service 5a9772
	MultiByteToWideChar(CP_ACP, 0, path, -1, (LPWSTR)Stream_Pointer(s), pathLength);
Packit 1fb8d4
	Stream_Seek(s, pathLength);
Packit 1fb8d4
	Stream_SealLength(s);
Packit Service 5a9772
	status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR)Stream_Buffer(s),
Packit Service 5a9772
	                                Stream_Length(s), &written);
Packit 1fb8d4
	Stream_Free(s, TRUE);
Packit 1fb8d4
	return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_send_device_close_request(RdpdrServerContext* context, UINT32 deviceId,
Packit Service 5a9772
                                                   UINT32 fileId, UINT32 completionId)
Packit 1fb8d4
{
Packit 1fb8d4
	ULONG written;
Packit 1fb8d4
	BOOL status;
Packit 1fb8d4
	wStream* s;
Packit Service 5a9772
	WLog_DBG(TAG, "RdpdrServerSendDeviceCloseRequest: deviceId=%" PRIu32 ", fileId=%" PRIu32 "",
Packit 1fb8d4
	         deviceId, fileId);
Packit 1fb8d4
	s = Stream_New(NULL, 128);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_New failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	rdpdr_server_write_device_iorequest(s, deviceId, fileId, completionId, IRP_MJ_CLOSE, 0);
Packit 1fb8d4
	Stream_Zero(s, 32); /* Padding (32 bytes) */
Packit 1fb8d4
	Stream_SealLength(s);
Packit Service 5a9772
	status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR)Stream_Buffer(s),
Packit Service 5a9772
	                                Stream_Length(s), &written);
Packit 1fb8d4
	Stream_Free(s, TRUE);
Packit 1fb8d4
	return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_send_device_read_request(RdpdrServerContext* context, UINT32 deviceId,
Packit Service 5a9772
                                                  UINT32 fileId, UINT32 completionId, UINT32 length,
Packit Service 5a9772
                                                  UINT32 offset)
Packit 1fb8d4
{
Packit 1fb8d4
	ULONG written;
Packit 1fb8d4
	BOOL status;
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "RdpdrServerSendDeviceReadRequest: deviceId=%" PRIu32 ", fileId=%" PRIu32
Packit Service 5a9772
	         ", length=%" PRIu32 ", offset=%" PRIu32 "",
Packit 1fb8d4
	         deviceId, fileId, length, offset);
Packit 1fb8d4
	s = Stream_New(NULL, 128);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_New failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	rdpdr_server_write_device_iorequest(s, deviceId, fileId, completionId, IRP_MJ_READ, 0);
Packit 1fb8d4
	Stream_Write_UINT32(s, length); /* Length (4 bytes) */
Packit 1fb8d4
	Stream_Write_UINT32(s, offset); /* Offset (8 bytes) */
Packit 1fb8d4
	Stream_Write_UINT32(s, 0);
Packit 1fb8d4
	Stream_Zero(s, 20); /* Padding (20 bytes) */
Packit 1fb8d4
	Stream_SealLength(s);
Packit Service 5a9772
	status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR)Stream_Buffer(s),
Packit Service 5a9772
	                                Stream_Length(s), &written);
Packit 1fb8d4
	Stream_Free(s, TRUE);
Packit 1fb8d4
	return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_send_device_write_request(RdpdrServerContext* context, UINT32 deviceId,
Packit Service 5a9772
                                                   UINT32 fileId, UINT32 completionId,
Packit Service 5a9772
                                                   const char* data, UINT32 length, UINT32 offset)
Packit 1fb8d4
{
Packit 1fb8d4
	ULONG written;
Packit 1fb8d4
	BOOL status;
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "RdpdrServerSendDeviceWriteRequest: deviceId=%" PRIu32 ", fileId=%" PRIu32
Packit Service 5a9772
	         ", length=%" PRIu32 ", offset=%" PRIu32 "",
Packit 1fb8d4
	         deviceId, fileId, length, offset);
Packit 1fb8d4
	s = Stream_New(NULL, 64 + length);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_New failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	rdpdr_server_write_device_iorequest(s, deviceId, fileId, completionId, IRP_MJ_WRITE, 0);
Packit 1fb8d4
	Stream_Write_UINT32(s, length); /* Length (4 bytes) */
Packit 1fb8d4
	Stream_Write_UINT32(s, offset); /* Offset (8 bytes) */
Packit 1fb8d4
	Stream_Write_UINT32(s, 0);
Packit Service 5a9772
	Stream_Zero(s, 20);            /* Padding (20 bytes) */
Packit 1fb8d4
	Stream_Write(s, data, length); /* WriteData (variable) */
Packit 1fb8d4
	Stream_SealLength(s);
Packit Service 5a9772
	status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR)Stream_Buffer(s),
Packit Service 5a9772
	                                Stream_Length(s), &written);
Packit 1fb8d4
	Stream_Free(s, TRUE);
Packit 1fb8d4
	return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_send_device_query_directory_request(RdpdrServerContext* context,
Packit Service 5a9772
                                                             UINT32 deviceId, UINT32 fileId,
Packit Service 5a9772
                                                             UINT32 completionId, const char* path)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 pathLength;
Packit 1fb8d4
	ULONG written;
Packit 1fb8d4
	BOOL status;
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "RdpdrServerSendDeviceQueryDirectoryRequest: deviceId=%" PRIu32 ", fileId=%" PRIu32
Packit Service 5a9772
	         ", path=%s",
Packit 1fb8d4
	         deviceId, fileId, path);
Packit 1fb8d4
	/* Compute the required Unicode size. */
Packit 1fb8d4
	pathLength = path ? (strlen(path) + 1) * sizeof(WCHAR) : 0;
Packit 1fb8d4
	s = Stream_New(NULL, 64 + pathLength);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_New failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	rdpdr_server_write_device_iorequest(s, deviceId, fileId, completionId, IRP_MJ_DIRECTORY_CONTROL,
Packit Service 5a9772
	                                    IRP_MN_QUERY_DIRECTORY);
Packit Service 5a9772
	Stream_Write_UINT32(s, FileDirectoryInformation); /* FsInformationClass (4 bytes) */
Packit Service 5a9772
	Stream_Write_UINT8(s, path ? 1 : 0);              /* InitialQuery (1 byte) */
Packit Service 5a9772
	Stream_Write_UINT32(s, pathLength);               /* PathLength (4 bytes) */
Packit Service 5a9772
	Stream_Zero(s, 23);                               /* Padding (23 bytes) */
Packit 1fb8d4
Packit 1fb8d4
	/* Convert the path to Unicode. */
Packit 1fb8d4
	if (pathLength > 0)
Packit 1fb8d4
	{
Packit Service 5a9772
		MultiByteToWideChar(CP_ACP, 0, path, -1, (LPWSTR)Stream_Pointer(s), pathLength);
Packit 1fb8d4
		Stream_Seek(s, pathLength);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_SealLength(s);
Packit Service 5a9772
	status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR)Stream_Buffer(s),
Packit Service 5a9772
	                                Stream_Length(s), &written);
Packit 1fb8d4
	Stream_Free(s, TRUE);
Packit 1fb8d4
	return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_send_device_file_rename_request(RdpdrServerContext* context,
Packit Service 5a9772
                                                         UINT32 deviceId, UINT32 fileId,
Packit Service 5a9772
                                                         UINT32 completionId, const char* path)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 pathLength;
Packit 1fb8d4
	ULONG written;
Packit 1fb8d4
	BOOL status;
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "RdpdrServerSendDeviceFileNameRequest: deviceId=%" PRIu32 ", fileId=%" PRIu32
Packit Service 5a9772
	         ", path=%s",
Packit 1fb8d4
	         deviceId, fileId, path);
Packit 1fb8d4
	/* Compute the required Unicode size. */
Packit 1fb8d4
	pathLength = path ? (strlen(path) + 1) * sizeof(WCHAR) : 0;
Packit 1fb8d4
	s = Stream_New(NULL, 64 + pathLength);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_New failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	rdpdr_server_write_device_iorequest(s, deviceId, fileId, completionId, IRP_MJ_SET_INFORMATION,
Packit Service 5a9772
	                                    0);
Packit Service 5a9772
	Stream_Write_UINT32(s, FileRenameInformation); /* FsInformationClass (4 bytes) */
Packit Service 5a9772
	Stream_Write_UINT32(s, pathLength + 6);        /* Length (4 bytes) */
Packit Service 5a9772
	Stream_Zero(s, 24);                            /* Padding (24 bytes) */
Packit 1fb8d4
	/* RDP_FILE_RENAME_INFORMATION */
Packit Service 5a9772
	Stream_Write_UINT8(s, 0);           /* ReplaceIfExists (1 byte) */
Packit Service 5a9772
	Stream_Write_UINT8(s, 0);           /* RootDirectory (1 byte) */
Packit 1fb8d4
	Stream_Write_UINT32(s, pathLength); /* FileNameLength (4 bytes) */
Packit 1fb8d4
Packit 1fb8d4
	/* Convert the path to Unicode. */
Packit 1fb8d4
	if (pathLength > 0)
Packit 1fb8d4
	{
Packit Service 5a9772
		MultiByteToWideChar(CP_ACP, 0, path, -1, (LPWSTR)Stream_Pointer(s), pathLength);
Packit 1fb8d4
		Stream_Seek(s, pathLength);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_SealLength(s);
Packit Service 5a9772
	status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR)Stream_Buffer(s),
Packit Service 5a9772
	                                Stream_Length(s), &written);
Packit 1fb8d4
	Stream_Free(s, TRUE);
Packit 1fb8d4
	return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void rdpdr_server_convert_slashes(char* path, int size)
Packit 1fb8d4
{
Packit 1fb8d4
	int i;
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; (i < size) && (path[i] != '\0'); i++)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (path[i] == '/')
Packit 1fb8d4
			path[i] = '\\';
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/*************************************************
Packit 1fb8d4
 * Drive Create Directory
Packit 1fb8d4
 ************************************************/
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_create_directory_callback2(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                          RDPDR_IRP* irp, UINT32 deviceId,
Packit Service 5a9772
                                                          UINT32 completionId, UINT32 ioStatus)
Packit 1fb8d4
{
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "RdpdrServerDriveCreateDirectoryCallback2: deviceId=%" PRIu32 ", completionId=%" PRIu32
Packit Service 5a9772
	         ", ioStatus=0x%" PRIx32 "",
Packit 1fb8d4
	         deviceId, completionId, ioStatus);
Packit 1fb8d4
	/* Invoke the create directory completion routine. */
Packit 1fb8d4
	context->OnDriveCreateDirectoryComplete(context, irp->CallbackData, ioStatus);
Packit 1fb8d4
	/* Destroy the IRP. */
Packit 1fb8d4
	rdpdr_server_irp_free(irp);
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_create_directory_callback1(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                          RDPDR_IRP* irp, UINT32 deviceId,
Packit Service 5a9772
                                                          UINT32 completionId, UINT32 ioStatus)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 fileId;
Packit 1fb8d4
	UINT8 information;
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "RdpdrServerDriveCreateDirectoryCallback1: deviceId=%" PRIu32 ", completionId=%" PRIu32
Packit Service 5a9772
	         ", ioStatus=0x%" PRIx32 "",
Packit 1fb8d4
	         deviceId, completionId, ioStatus);
Packit 1fb8d4
Packit 1fb8d4
	if (ioStatus != STATUS_SUCCESS)
Packit 1fb8d4
	{
Packit 1fb8d4
		/* Invoke the create directory completion routine. */
Packit 1fb8d4
		context->OnDriveCreateDirectoryComplete(context, irp->CallbackData, ioStatus);
Packit 1fb8d4
		/* Destroy the IRP. */
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
		return CHANNEL_RC_OK;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 5)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Read_UINT32(s, fileId);     /* FileId (4 bytes) */
Packit 1fb8d4
	Stream_Read_UINT8(s, information); /* Information (1 byte) */
Packit 1fb8d4
	/* Setup the IRP. */
Packit 1fb8d4
	irp->CompletionId = context->priv->NextCompletionId++;
Packit 1fb8d4
	irp->Callback = rdpdr_server_drive_create_directory_callback2;
Packit 1fb8d4
	irp->DeviceId = deviceId;
Packit 1fb8d4
	irp->FileId = fileId;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdpdr_server_enqueue_irp(context, irp))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_enqueue_irp failed!");
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Send a request to close the file */
Packit Service 5a9772
	return rdpdr_server_send_device_close_request(context, deviceId, fileId, irp->CompletionId);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_create_directory(RdpdrServerContext* context, void* callbackData,
Packit Service 5a9772
                                                UINT32 deviceId, const char* path)
Packit 1fb8d4
{
Packit 1fb8d4
	RDPDR_IRP* irp;
Packit 1fb8d4
	irp = rdpdr_server_irp_new();
Packit 1fb8d4
Packit 1fb8d4
	if (!irp)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_irp_new failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	irp->CompletionId = context->priv->NextCompletionId++;
Packit 1fb8d4
	irp->Callback = rdpdr_server_drive_create_directory_callback1;
Packit 1fb8d4
	irp->CallbackData = callbackData;
Packit 1fb8d4
	irp->DeviceId = deviceId;
Packit 1fb8d4
	strncpy(irp->PathName, path, sizeof(irp->PathName) - 1);
Packit 1fb8d4
	rdpdr_server_convert_slashes(irp->PathName, sizeof(irp->PathName));
Packit 1fb8d4
Packit 1fb8d4
	if (!rdpdr_server_enqueue_irp(context, irp))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_enqueue_irp failed!");
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Send a request to open the file. */
Packit Service 5a9772
	return rdpdr_server_send_device_create_request(
Packit Service 5a9772
	    context, deviceId, irp->CompletionId, irp->PathName, FILE_READ_DATA | SYNCHRONIZE,
Packit Service 5a9772
	    FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, FILE_CREATE);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/*************************************************
Packit 1fb8d4
 * Drive Delete Directory
Packit 1fb8d4
 ************************************************/
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_delete_directory_callback2(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                          RDPDR_IRP* irp, UINT32 deviceId,
Packit Service 5a9772
                                                          UINT32 completionId, UINT32 ioStatus)
Packit 1fb8d4
{
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "RdpdrServerDriveDeleteDirectoryCallback2: deviceId=%" PRIu32 ", completionId=%" PRIu32
Packit Service 5a9772
	         ", ioStatus=0x%" PRIx32 "",
Packit 1fb8d4
	         deviceId, completionId, ioStatus);
Packit 1fb8d4
	/* Invoke the delete directory completion routine. */
Packit 1fb8d4
	context->OnDriveDeleteDirectoryComplete(context, irp->CallbackData, ioStatus);
Packit 1fb8d4
	/* Destroy the IRP. */
Packit 1fb8d4
	rdpdr_server_irp_free(irp);
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_delete_directory_callback1(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                          RDPDR_IRP* irp, UINT32 deviceId,
Packit Service 5a9772
                                                          UINT32 completionId, UINT32 ioStatus)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 fileId;
Packit 1fb8d4
	UINT8 information;
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "RdpdrServerDriveDeleteDirectoryCallback1: deviceId=%" PRIu32 ", completionId=%" PRIu32
Packit Service 5a9772
	         ", ioStatus=0x%" PRIx32 "",
Packit 1fb8d4
	         deviceId, completionId, ioStatus);
Packit 1fb8d4
Packit 1fb8d4
	if (ioStatus != STATUS_SUCCESS)
Packit 1fb8d4
	{
Packit 1fb8d4
		/* Invoke the delete directory completion routine. */
Packit 1fb8d4
		context->OnDriveDeleteFileComplete(context, irp->CallbackData, ioStatus);
Packit 1fb8d4
		/* Destroy the IRP. */
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
		return CHANNEL_RC_OK;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 5)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Read_UINT32(s, fileId);     /* FileId (4 bytes) */
Packit 1fb8d4
	Stream_Read_UINT8(s, information); /* Information (1 byte) */
Packit 1fb8d4
	/* Setup the IRP. */
Packit 1fb8d4
	irp->CompletionId = context->priv->NextCompletionId++;
Packit 1fb8d4
	irp->Callback = rdpdr_server_drive_delete_directory_callback2;
Packit 1fb8d4
	irp->DeviceId = deviceId;
Packit 1fb8d4
	irp->FileId = fileId;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdpdr_server_enqueue_irp(context, irp))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_enqueue_irp failed!");
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Send a request to close the file */
Packit Service 5a9772
	return rdpdr_server_send_device_close_request(context, deviceId, fileId, irp->CompletionId);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_delete_directory(RdpdrServerContext* context, void* callbackData,
Packit Service 5a9772
                                                UINT32 deviceId, const char* path)
Packit 1fb8d4
{
Packit 1fb8d4
	RDPDR_IRP* irp;
Packit 1fb8d4
	irp = rdpdr_server_irp_new();
Packit 1fb8d4
Packit 1fb8d4
	if (!irp)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_irp_new failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	irp->CompletionId = context->priv->NextCompletionId++;
Packit 1fb8d4
	irp->Callback = rdpdr_server_drive_delete_directory_callback1;
Packit 1fb8d4
	irp->CallbackData = callbackData;
Packit 1fb8d4
	irp->DeviceId = deviceId;
Packit 1fb8d4
	strncpy(irp->PathName, path, sizeof(irp->PathName) - 1);
Packit 1fb8d4
	rdpdr_server_convert_slashes(irp->PathName, sizeof(irp->PathName));
Packit 1fb8d4
Packit 1fb8d4
	if (!rdpdr_server_enqueue_irp(context, irp))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_enqueue_irp failed!");
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Send a request to open the file. */
Packit Service 5a9772
	return rdpdr_server_send_device_create_request(
Packit Service 5a9772
	    context, deviceId, irp->CompletionId, irp->PathName, DELETE | SYNCHRONIZE,
Packit Service 5a9772
	    FILE_DIRECTORY_FILE | FILE_DELETE_ON_CLOSE | FILE_SYNCHRONOUS_IO_NONALERT, FILE_OPEN);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/*************************************************
Packit 1fb8d4
 * Drive Query Directory
Packit 1fb8d4
 ************************************************/
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_query_directory_callback2(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                         RDPDR_IRP* irp, UINT32 deviceId,
Packit Service 5a9772
                                                         UINT32 completionId, UINT32 ioStatus)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT error;
Packit 1fb8d4
	UINT32 length;
Packit 1fb8d4
	FILE_DIRECTORY_INFORMATION fdi;
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "RdpdrServerDriveQueryDirectoryCallback2: deviceId=%" PRIu32 ", completionId=%" PRIu32
Packit Service 5a9772
	         ", ioStatus=0x%" PRIx32 "",
Packit 1fb8d4
	         deviceId, completionId, ioStatus);
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, length); /* Length (4 bytes) */
Packit 1fb8d4
Packit 1fb8d4
	if (length > 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		if ((error = rdpdr_server_read_file_directory_information(s, &fdi)))
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG,
Packit Service 5a9772
			         "rdpdr_server_read_file_directory_information failed with error %" PRIu32 "!",
Packit Service 5a9772
			         error);
Packit 1fb8d4
			return error;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		if (Stream_GetRemainingLength(s) < 1)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
			return ERROR_INVALID_DATA;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		Stream_Seek(s, 1); /* Padding (1 byte) */
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (ioStatus == STATUS_SUCCESS)
Packit 1fb8d4
	{
Packit 1fb8d4
		/* Invoke the query directory completion routine. */
Packit 1fb8d4
		context->OnDriveQueryDirectoryComplete(context, irp->CallbackData, ioStatus,
Packit 1fb8d4
		                                       length > 0 ? &fdi : NULL);
Packit 1fb8d4
		/* Setup the IRP. */
Packit 1fb8d4
		irp->CompletionId = context->priv->NextCompletionId++;
Packit 1fb8d4
		irp->Callback = rdpdr_server_drive_query_directory_callback2;
Packit 1fb8d4
Packit 1fb8d4
		if (!rdpdr_server_enqueue_irp(context, irp))
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "rdpdr_server_enqueue_irp failed!");
Packit 1fb8d4
			rdpdr_server_irp_free(irp);
Packit 1fb8d4
			return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		/* Send a request to query the directory. */
Packit Service 5a9772
		return rdpdr_server_send_device_query_directory_request(context, irp->DeviceId, irp->FileId,
Packit Service 5a9772
		                                                        irp->CompletionId, NULL);
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		/* Invoke the query directory completion routine. */
Packit Service 5a9772
		context->OnDriveQueryDirectoryComplete(context, irp->CallbackData, ioStatus, NULL);
Packit 1fb8d4
		/* Destroy the IRP. */
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_query_directory_callback1(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                         RDPDR_IRP* irp, UINT32 deviceId,
Packit Service 5a9772
                                                         UINT32 completionId, UINT32 ioStatus)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 fileId;
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "RdpdrServerDriveQueryDirectoryCallback1: deviceId=%" PRIu32 ", completionId=%" PRIu32
Packit Service 5a9772
	         ", ioStatus=0x%" PRIx32 "",
Packit 1fb8d4
	         deviceId, completionId, ioStatus);
Packit 1fb8d4
Packit 1fb8d4
	if (ioStatus != STATUS_SUCCESS)
Packit 1fb8d4
	{
Packit 1fb8d4
		/* Invoke the query directory completion routine. */
Packit Service 5a9772
		context->OnDriveQueryDirectoryComplete(context, irp->CallbackData, ioStatus, NULL);
Packit 1fb8d4
		/* Destroy the IRP. */
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
		return CHANNEL_RC_OK;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, fileId);
Packit 1fb8d4
	/* Setup the IRP. */
Packit 1fb8d4
	irp->CompletionId = context->priv->NextCompletionId++;
Packit 1fb8d4
	irp->Callback = rdpdr_server_drive_query_directory_callback2;
Packit 1fb8d4
	irp->DeviceId = deviceId;
Packit 1fb8d4
	irp->FileId = fileId;
Packit 1fb8d4
	strcat(irp->PathName, "\\*.*");
Packit 1fb8d4
Packit 1fb8d4
	if (!rdpdr_server_enqueue_irp(context, irp))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_enqueue_irp failed!");
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Send a request to query the directory. */
Packit Service 5a9772
	return rdpdr_server_send_device_query_directory_request(context, deviceId, fileId,
Packit Service 5a9772
	                                                        irp->CompletionId, irp->PathName);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_query_directory(RdpdrServerContext* context, void* callbackData,
Packit Service 5a9772
                                               UINT32 deviceId, const char* path)
Packit 1fb8d4
{
Packit 1fb8d4
	RDPDR_IRP* irp;
Packit 1fb8d4
	irp = rdpdr_server_irp_new();
Packit 1fb8d4
Packit 1fb8d4
	if (!irp)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_irp_new failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	irp->CompletionId = context->priv->NextCompletionId++;
Packit 1fb8d4
	irp->Callback = rdpdr_server_drive_query_directory_callback1;
Packit 1fb8d4
	irp->CallbackData = callbackData;
Packit 1fb8d4
	irp->DeviceId = deviceId;
Packit 1fb8d4
	strncpy(irp->PathName, path, sizeof(irp->PathName) - 1);
Packit 1fb8d4
	rdpdr_server_convert_slashes(irp->PathName, sizeof(irp->PathName));
Packit 1fb8d4
Packit 1fb8d4
	if (!rdpdr_server_enqueue_irp(context, irp))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_enqueue_irp failed!");
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Send a request to open the directory. */
Packit Service 5a9772
	return rdpdr_server_send_device_create_request(
Packit Service 5a9772
	    context, deviceId, irp->CompletionId, irp->PathName, FILE_READ_DATA | SYNCHRONIZE,
Packit Service 5a9772
	    FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, FILE_OPEN);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/*************************************************
Packit 1fb8d4
 * Drive Open File
Packit 1fb8d4
 ************************************************/
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_open_file_callback(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                  RDPDR_IRP* irp, UINT32 deviceId,
Packit Service 5a9772
                                                  UINT32 completionId, UINT32 ioStatus)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 fileId;
Packit 1fb8d4
	UINT8 information;
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "RdpdrServerDriveOpenFileCallback: deviceId=%" PRIu32 ", completionId=%" PRIu32
Packit Service 5a9772
	         ", ioStatus=0x%" PRIx32 "",
Packit 1fb8d4
	         deviceId, completionId, ioStatus);
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 5)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Read_UINT32(s, fileId);     /* FileId (4 bytes) */
Packit 1fb8d4
	Stream_Read_UINT8(s, information); /* Information (1 byte) */
Packit 1fb8d4
	/* Invoke the open file completion routine. */
Packit Service 5a9772
	context->OnDriveOpenFileComplete(context, irp->CallbackData, ioStatus, deviceId, fileId);
Packit 1fb8d4
	/* Destroy the IRP. */
Packit 1fb8d4
	rdpdr_server_irp_free(irp);
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_open_file(RdpdrServerContext* context, void* callbackData,
Packit Service 5a9772
                                         UINT32 deviceId, const char* path, UINT32 desiredAccess,
Packit Service 5a9772
                                         UINT32 createDisposition)
Packit 1fb8d4
{
Packit 1fb8d4
	RDPDR_IRP* irp;
Packit 1fb8d4
	irp = rdpdr_server_irp_new();
Packit 1fb8d4
Packit 1fb8d4
	if (!irp)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_irp_new failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	irp->CompletionId = context->priv->NextCompletionId++;
Packit 1fb8d4
	irp->Callback = rdpdr_server_drive_open_file_callback;
Packit 1fb8d4
	irp->CallbackData = callbackData;
Packit 1fb8d4
	irp->DeviceId = deviceId;
Packit 1fb8d4
	strncpy(irp->PathName, path, sizeof(irp->PathName) - 1);
Packit 1fb8d4
	rdpdr_server_convert_slashes(irp->PathName, sizeof(irp->PathName));
Packit 1fb8d4
Packit 1fb8d4
	if (!rdpdr_server_enqueue_irp(context, irp))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_enqueue_irp failed!");
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Send a request to open the file. */
Packit Service 5a9772
	return rdpdr_server_send_device_create_request(context, deviceId, irp->CompletionId,
Packit Service 5a9772
	                                               irp->PathName, desiredAccess | SYNCHRONIZE,
Packit Service 5a9772
	                                               FILE_SYNCHRONOUS_IO_NONALERT, createDisposition);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/*************************************************
Packit 1fb8d4
 * Drive Read File
Packit 1fb8d4
 ************************************************/
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_read_file_callback(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                  RDPDR_IRP* irp, UINT32 deviceId,
Packit Service 5a9772
                                                  UINT32 completionId, UINT32 ioStatus)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 length;
Packit 1fb8d4
	char* buffer = NULL;
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "RdpdrServerDriveReadFileCallback: deviceId=%" PRIu32 ", completionId=%" PRIu32
Packit Service 5a9772
	         ", ioStatus=0x%" PRIx32 "",
Packit 1fb8d4
	         deviceId, completionId, ioStatus);
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, length); /* Length (4 bytes) */
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < length)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (length > 0)
Packit 1fb8d4
	{
Packit Service 5a9772
		buffer = (char*)Stream_Pointer(s);
Packit 1fb8d4
		Stream_Seek(s, length);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Invoke the read file completion routine. */
Packit Service 5a9772
	context->OnDriveReadFileComplete(context, irp->CallbackData, ioStatus, buffer, length);
Packit 1fb8d4
	/* Destroy the IRP. */
Packit 1fb8d4
	rdpdr_server_irp_free(irp);
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_read_file(RdpdrServerContext* context, void* callbackData,
Packit Service 5a9772
                                         UINT32 deviceId, UINT32 fileId, UINT32 length,
Packit Service 5a9772
                                         UINT32 offset)
Packit 1fb8d4
{
Packit 1fb8d4
	RDPDR_IRP* irp;
Packit 1fb8d4
	irp = rdpdr_server_irp_new();
Packit 1fb8d4
Packit 1fb8d4
	if (!irp)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_irp_new failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	irp->CompletionId = context->priv->NextCompletionId++;
Packit 1fb8d4
	irp->Callback = rdpdr_server_drive_read_file_callback;
Packit 1fb8d4
	irp->CallbackData = callbackData;
Packit 1fb8d4
	irp->DeviceId = deviceId;
Packit 1fb8d4
	irp->FileId = fileId;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdpdr_server_enqueue_irp(context, irp))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_enqueue_irp failed!");
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Send a request to open the directory. */
Packit Service 5a9772
	return rdpdr_server_send_device_read_request(context, deviceId, fileId, irp->CompletionId,
Packit Service 5a9772
	                                             length, offset);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/*************************************************
Packit 1fb8d4
 * Drive Write File
Packit 1fb8d4
 ************************************************/
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_write_file_callback(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                   RDPDR_IRP* irp, UINT32 deviceId,
Packit Service 5a9772
                                                   UINT32 completionId, UINT32 ioStatus)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 length;
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "RdpdrServerDriveWriteFileCallback: deviceId=%" PRIu32 ", completionId=%" PRIu32
Packit Service 5a9772
	         ", ioStatus=0x%" PRIx32 "",
Packit 1fb8d4
	         deviceId, completionId, ioStatus);
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 5)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, length); /* Length (4 bytes) */
Packit Service 5a9772
	Stream_Seek(s, 1);             /* Padding (1 byte) */
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < length)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Invoke the write file completion routine. */
Packit 1fb8d4
	context->OnDriveWriteFileComplete(context, irp->CallbackData, ioStatus, length);
Packit 1fb8d4
	/* Destroy the IRP. */
Packit 1fb8d4
	rdpdr_server_irp_free(irp);
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_write_file(RdpdrServerContext* context, void* callbackData,
Packit Service 5a9772
                                          UINT32 deviceId, UINT32 fileId, const char* buffer,
Packit Service 5a9772
                                          UINT32 length, UINT32 offset)
Packit 1fb8d4
{
Packit 1fb8d4
	RDPDR_IRP* irp;
Packit 1fb8d4
	irp = rdpdr_server_irp_new();
Packit 1fb8d4
Packit 1fb8d4
	if (!irp)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_irp_new failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	irp->CompletionId = context->priv->NextCompletionId++;
Packit 1fb8d4
	irp->Callback = rdpdr_server_drive_write_file_callback;
Packit 1fb8d4
	irp->CallbackData = callbackData;
Packit 1fb8d4
	irp->DeviceId = deviceId;
Packit 1fb8d4
	irp->FileId = fileId;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdpdr_server_enqueue_irp(context, irp))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_enqueue_irp failed!");
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Send a request to open the directory. */
Packit Service 5a9772
	return rdpdr_server_send_device_write_request(context, deviceId, fileId, irp->CompletionId,
Packit Service 5a9772
	                                              buffer, length, offset);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/*************************************************
Packit 1fb8d4
 * Drive Close File
Packit 1fb8d4
 ************************************************/
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_close_file_callback(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                   RDPDR_IRP* irp, UINT32 deviceId,
Packit Service 5a9772
                                                   UINT32 completionId, UINT32 ioStatus)
Packit 1fb8d4
{
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "RdpdrServerDriveCloseFileCallback: deviceId=%" PRIu32 ", completionId=%" PRIu32
Packit Service 5a9772
	         ", ioStatus=0x%" PRIx32 "",
Packit 1fb8d4
	         deviceId, completionId, ioStatus);
Packit 1fb8d4
	/* Invoke the close file completion routine. */
Packit 1fb8d4
	context->OnDriveCloseFileComplete(context, irp->CallbackData, ioStatus);
Packit 1fb8d4
	/* Destroy the IRP. */
Packit 1fb8d4
	rdpdr_server_irp_free(irp);
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_close_file(RdpdrServerContext* context, void* callbackData,
Packit Service 5a9772
                                          UINT32 deviceId, UINT32 fileId)
Packit 1fb8d4
{
Packit 1fb8d4
	RDPDR_IRP* irp;
Packit 1fb8d4
	irp = rdpdr_server_irp_new();
Packit 1fb8d4
Packit 1fb8d4
	if (!irp)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_irp_new failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	irp->CompletionId = context->priv->NextCompletionId++;
Packit 1fb8d4
	irp->Callback = rdpdr_server_drive_close_file_callback;
Packit 1fb8d4
	irp->CallbackData = callbackData;
Packit 1fb8d4
	irp->DeviceId = deviceId;
Packit 1fb8d4
	irp->FileId = fileId;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdpdr_server_enqueue_irp(context, irp))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_enqueue_irp failed!");
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Send a request to open the directory. */
Packit Service 5a9772
	return rdpdr_server_send_device_close_request(context, deviceId, fileId, irp->CompletionId);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/*************************************************
Packit 1fb8d4
 * Drive Delete File
Packit 1fb8d4
 ************************************************/
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_delete_file_callback2(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                     RDPDR_IRP* irp, UINT32 deviceId,
Packit Service 5a9772
                                                     UINT32 completionId, UINT32 ioStatus)
Packit 1fb8d4
{
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "RdpdrServerDriveDeleteFileCallback2: deviceId=%" PRIu32 ", completionId=%" PRIu32
Packit Service 5a9772
	         ", ioStatus=0x%" PRIx32 "",
Packit 1fb8d4
	         deviceId, completionId, ioStatus);
Packit 1fb8d4
	/* Invoke the delete file completion routine. */
Packit 1fb8d4
	context->OnDriveDeleteFileComplete(context, irp->CallbackData, ioStatus);
Packit 1fb8d4
	/* Destroy the IRP. */
Packit 1fb8d4
	rdpdr_server_irp_free(irp);
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_delete_file_callback1(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                     RDPDR_IRP* irp, UINT32 deviceId,
Packit Service 5a9772
                                                     UINT32 completionId, UINT32 ioStatus)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 fileId;
Packit 1fb8d4
	UINT8 information;
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "RdpdrServerDriveDeleteFileCallback1: deviceId=%" PRIu32 ", completionId=%" PRIu32
Packit Service 5a9772
	         ", ioStatus=0x%" PRIx32 "",
Packit 1fb8d4
	         deviceId, completionId, ioStatus);
Packit 1fb8d4
Packit 1fb8d4
	if (ioStatus != STATUS_SUCCESS)
Packit 1fb8d4
	{
Packit 1fb8d4
		/* Invoke the close file completion routine. */
Packit 1fb8d4
		context->OnDriveDeleteFileComplete(context, irp->CallbackData, ioStatus);
Packit 1fb8d4
		/* Destroy the IRP. */
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
		return CHANNEL_RC_OK;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 5)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Read_UINT32(s, fileId);     /* FileId (4 bytes) */
Packit 1fb8d4
	Stream_Read_UINT8(s, information); /* Information (1 byte) */
Packit 1fb8d4
	/* Setup the IRP. */
Packit 1fb8d4
	irp->CompletionId = context->priv->NextCompletionId++;
Packit 1fb8d4
	irp->Callback = rdpdr_server_drive_delete_file_callback2;
Packit 1fb8d4
	irp->DeviceId = deviceId;
Packit 1fb8d4
	irp->FileId = fileId;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdpdr_server_enqueue_irp(context, irp))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_enqueue_irp failed!");
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Send a request to close the file */
Packit Service 5a9772
	return rdpdr_server_send_device_close_request(context, deviceId, fileId, irp->CompletionId);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_delete_file(RdpdrServerContext* context, void* callbackData,
Packit Service 5a9772
                                           UINT32 deviceId, const char* path)
Packit 1fb8d4
{
Packit 1fb8d4
	RDPDR_IRP* irp;
Packit 1fb8d4
	irp = rdpdr_server_irp_new();
Packit 1fb8d4
Packit 1fb8d4
	if (!irp)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_irp_new failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	irp->CompletionId = context->priv->NextCompletionId++;
Packit 1fb8d4
	irp->Callback = rdpdr_server_drive_delete_file_callback1;
Packit 1fb8d4
	irp->CallbackData = callbackData;
Packit 1fb8d4
	irp->DeviceId = deviceId;
Packit 1fb8d4
	strncpy(irp->PathName, path, sizeof(irp->PathName) - 1);
Packit 1fb8d4
	rdpdr_server_convert_slashes(irp->PathName, sizeof(irp->PathName));
Packit 1fb8d4
Packit 1fb8d4
	if (!rdpdr_server_enqueue_irp(context, irp))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_enqueue_irp failed!");
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Send a request to open the file. */
Packit Service 5a9772
	return rdpdr_server_send_device_create_request(
Packit Service 5a9772
	    context, deviceId, irp->CompletionId, irp->PathName, FILE_READ_DATA | SYNCHRONIZE,
Packit Service 5a9772
	    FILE_DELETE_ON_CLOSE | FILE_SYNCHRONOUS_IO_NONALERT, FILE_OPEN);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/*************************************************
Packit 1fb8d4
 * Drive Rename File
Packit 1fb8d4
 ************************************************/
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_rename_file_callback3(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                     RDPDR_IRP* irp, UINT32 deviceId,
Packit Service 5a9772
                                                     UINT32 completionId, UINT32 ioStatus)
Packit 1fb8d4
{
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "RdpdrServerDriveRenameFileCallback3: deviceId=%" PRIu32 ", completionId=%" PRIu32
Packit Service 5a9772
	         ", ioStatus=0x%" PRIx32 "",
Packit 1fb8d4
	         deviceId, completionId, ioStatus);
Packit 1fb8d4
	/* Destroy the IRP. */
Packit 1fb8d4
	rdpdr_server_irp_free(irp);
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_rename_file_callback2(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                     RDPDR_IRP* irp, UINT32 deviceId,
Packit Service 5a9772
                                                     UINT32 completionId, UINT32 ioStatus)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 length;
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "RdpdrServerDriveRenameFileCallback2: deviceId=%" PRIu32 ", completionId=%" PRIu32
Packit Service 5a9772
	         ", ioStatus=0x%" PRIx32 "",
Packit 1fb8d4
	         deviceId, completionId, ioStatus);
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 5)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, length); /* Length (4 bytes) */
Packit Service 5a9772
	Stream_Seek(s, 1);             /* Padding (1 byte) */
Packit 1fb8d4
	/* Invoke the rename file completion routine. */
Packit 1fb8d4
	context->OnDriveRenameFileComplete(context, irp->CallbackData, ioStatus);
Packit 1fb8d4
	/* Setup the IRP. */
Packit 1fb8d4
	irp->CompletionId = context->priv->NextCompletionId++;
Packit 1fb8d4
	irp->Callback = rdpdr_server_drive_rename_file_callback3;
Packit 1fb8d4
	irp->DeviceId = deviceId;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdpdr_server_enqueue_irp(context, irp))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_enqueue_irp failed!");
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Send a request to close the file */
Packit 1fb8d4
	return rdpdr_server_send_device_close_request(context, deviceId, irp->FileId,
Packit Service 5a9772
	                                              irp->CompletionId);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_rename_file_callback1(RdpdrServerContext* context, wStream* s,
Packit Service 5a9772
                                                     RDPDR_IRP* irp, UINT32 deviceId,
Packit Service 5a9772
                                                     UINT32 completionId, UINT32 ioStatus)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 fileId;
Packit 1fb8d4
	UINT8 information;
Packit 1fb8d4
	WLog_DBG(TAG,
Packit Service 5a9772
	         "RdpdrServerDriveRenameFileCallback1: deviceId=%" PRIu32 ", completionId=%" PRIu32
Packit Service 5a9772
	         ", ioStatus=0x%" PRIx32 "",
Packit 1fb8d4
	         deviceId, completionId, ioStatus);
Packit 1fb8d4
Packit 1fb8d4
	if (ioStatus != STATUS_SUCCESS)
Packit 1fb8d4
	{
Packit 1fb8d4
		/* Invoke the rename file completion routine. */
Packit 1fb8d4
		context->OnDriveRenameFileComplete(context, irp->CallbackData, ioStatus);
Packit 1fb8d4
		/* Destroy the IRP. */
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
		return CHANNEL_RC_OK;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 5)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough data in stream!");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	Stream_Read_UINT32(s, fileId);     /* FileId (4 bytes) */
Packit 1fb8d4
	Stream_Read_UINT8(s, information); /* Information (1 byte) */
Packit 1fb8d4
	/* Setup the IRP. */
Packit 1fb8d4
	irp->CompletionId = context->priv->NextCompletionId++;
Packit 1fb8d4
	irp->Callback = rdpdr_server_drive_rename_file_callback2;
Packit 1fb8d4
	irp->DeviceId = deviceId;
Packit 1fb8d4
	irp->FileId = fileId;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdpdr_server_enqueue_irp(context, irp))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_enqueue_irp failed!");
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Send a request to rename the file */
Packit 1fb8d4
	return rdpdr_server_send_device_file_rename_request(context, deviceId, fileId,
Packit Service 5a9772
	                                                    irp->CompletionId, irp->ExtraBuffer);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT rdpdr_server_drive_rename_file(RdpdrServerContext* context, void* callbackData,
Packit Service 5a9772
                                           UINT32 deviceId, const char* oldPath,
Packit Service 5a9772
                                           const char* newPath)
Packit 1fb8d4
{
Packit 1fb8d4
	RDPDR_IRP* irp;
Packit 1fb8d4
	irp = rdpdr_server_irp_new();
Packit 1fb8d4
Packit 1fb8d4
	if (!irp)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_irp_new failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	irp->CompletionId = context->priv->NextCompletionId++;
Packit 1fb8d4
	irp->Callback = rdpdr_server_drive_rename_file_callback1;
Packit 1fb8d4
	irp->CallbackData = callbackData;
Packit 1fb8d4
	irp->DeviceId = deviceId;
Packit 1fb8d4
	strncpy(irp->PathName, oldPath, sizeof(irp->PathName) - 1);
Packit 1fb8d4
	strncpy(irp->ExtraBuffer, newPath, sizeof(irp->ExtraBuffer) - 1);
Packit 1fb8d4
	rdpdr_server_convert_slashes(irp->PathName, sizeof(irp->PathName));
Packit 1fb8d4
	rdpdr_server_convert_slashes(irp->ExtraBuffer, sizeof(irp->ExtraBuffer));
Packit 1fb8d4
Packit 1fb8d4
	if (!rdpdr_server_enqueue_irp(context, irp))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "rdpdr_server_enqueue_irp failed!");
Packit 1fb8d4
		rdpdr_server_irp_free(irp);
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Send a request to open the file. */
Packit Service 5a9772
	return rdpdr_server_send_device_create_request(context, deviceId, irp->CompletionId,
Packit Service 5a9772
	                                               irp->PathName, FILE_READ_DATA | SYNCHRONIZE,
Packit Service 5a9772
	                                               FILE_SYNCHRONOUS_IO_NONALERT, FILE_OPEN);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
RdpdrServerContext* rdpdr_server_context_new(HANDLE vcm)
Packit 1fb8d4
{
Packit 1fb8d4
	RdpdrServerContext* context;
Packit Service 5a9772
	context = (RdpdrServerContext*)calloc(1, sizeof(RdpdrServerContext));
Packit 1fb8d4
Packit 1fb8d4
	if (context)
Packit 1fb8d4
	{
Packit 1fb8d4
		context->vcm = vcm;
Packit 1fb8d4
		context->Start = rdpdr_server_start;
Packit 1fb8d4
		context->Stop = rdpdr_server_stop;
Packit 1fb8d4
		context->DriveCreateDirectory = rdpdr_server_drive_create_directory;
Packit 1fb8d4
		context->DriveDeleteDirectory = rdpdr_server_drive_delete_directory;
Packit 1fb8d4
		context->DriveQueryDirectory = rdpdr_server_drive_query_directory;
Packit 1fb8d4
		context->DriveOpenFile = rdpdr_server_drive_open_file;
Packit 1fb8d4
		context->DriveReadFile = rdpdr_server_drive_read_file;
Packit 1fb8d4
		context->DriveWriteFile = rdpdr_server_drive_write_file;
Packit 1fb8d4
		context->DriveCloseFile = rdpdr_server_drive_close_file;
Packit 1fb8d4
		context->DriveDeleteFile = rdpdr_server_drive_delete_file;
Packit 1fb8d4
		context->DriveRenameFile = rdpdr_server_drive_rename_file;
Packit Service 5a9772
		context->priv = (RdpdrServerPrivate*)calloc(1, sizeof(RdpdrServerPrivate));
Packit 1fb8d4
Packit 1fb8d4
		if (!context->priv)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "calloc failed!");
Packit 1fb8d4
			free(context);
Packit 1fb8d4
			return NULL;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		context->priv->VersionMajor = RDPDR_VERSION_MAJOR;
Packit 1fb8d4
		context->priv->VersionMinor = RDPDR_VERSION_MINOR_RDP6X;
Packit 1fb8d4
		context->priv->ClientId = g_ClientId++;
Packit 1fb8d4
		context->priv->UserLoggedOnPdu = TRUE;
Packit 1fb8d4
		context->priv->NextCompletionId = 1;
Packit 1fb8d4
		context->priv->IrpList = ListDictionary_New(TRUE);
Packit 1fb8d4
Packit 1fb8d4
		if (!context->priv->IrpList)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "ListDictionary_New failed!");
Packit 1fb8d4
			free(context->priv);
Packit 1fb8d4
			free(context);
Packit 1fb8d4
			return NULL;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "calloc failed!");
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return context;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void rdpdr_server_context_free(RdpdrServerContext* context)
Packit 1fb8d4
{
Packit 1fb8d4
	if (context)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (context->priv)
Packit 1fb8d4
		{
Packit 1fb8d4
			ListDictionary_Free(context->priv->IrpList);
Packit 1fb8d4
			free(context->priv);
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		free(context);
Packit 1fb8d4
	}
Packit 1fb8d4
}