vlmcsd/src/rpc.c

1256 lines
36 KiB
C
Raw Normal View History

2015-11-29 10:30:52 +01:00
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif // _DEFAULT_SOURCE
#ifndef CONFIG
#define CONFIG "config.h"
#endif // CONFIG
#include CONFIG
#ifndef USE_MSRPC
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
2016-10-14 07:28:23 +02:00
//#include <ctype.h>
//#include <time.h>
2015-11-29 10:30:52 +01:00
#if !defined(_WIN32)
#include <sys/socket.h>
#include <netdb.h>
#endif
#include "rpc.h"
#include "output.h"
2016-10-14 07:28:23 +02:00
//#include "crypto.h"
2015-11-29 10:30:52 +01:00
#include "endian.h"
#include "helpers.h"
#include "network.h"
#include "shared_globals.h"
/* Forwards */
2018-10-24 05:40:18 +02:00
static int checkRpcHeader(const RPC_HEADER *const header, const BYTE desiredPacketType, const PRINTFUNC p);
2015-11-29 10:30:52 +01:00
/* Data definitions */
// All GUIDs are defined as BYTE[16] here. No big-endian/little-endian byteswapping required.
static const BYTE TransferSyntaxNDR32[] = {
0x04, 0x5D, 0x88, 0x8A, 0xEB, 0x1C, 0xC9, 0x11, 0x9F, 0xE8, 0x08, 0x00, 0x2B, 0x10, 0x48, 0x60
};
static const BYTE InterfaceUuid[] = {
0x75, 0x21, 0xc8, 0x51, 0x4e, 0x84, 0x50, 0x47, 0xB0, 0xD8, 0xEC, 0x25, 0x55, 0x55, 0xBC, 0x06
};
2016-10-24 15:32:24 +02:00
//#ifndef SIMPLE_RPC
2015-11-29 10:30:52 +01:00
static const BYTE TransferSyntaxNDR64[] = {
0x33, 0x05, 0x71, 0x71, 0xba, 0xbe, 0x37, 0x49, 0x83, 0x19, 0xb5, 0xdb, 0xef, 0x9c, 0xcc, 0x36
};
static const BYTE BindTimeFeatureNegotiation[] = {
0x2c, 0x1c, 0xb7, 0x6c, 0x12, 0x98, 0x40, 0x45, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
2016-10-24 15:32:24 +02:00
//#endif // SIMPLE_RPC
2015-11-29 10:30:52 +01:00
//
// Dispatch RPC payload to kms.c
//
2016-10-14 07:28:23 +02:00
typedef int(*CreateResponse_t)(const void *const, void *const, const char* const);
2015-11-29 10:30:52 +01:00
2016-10-14 07:28:23 +02:00
// ReSharper disable CppIncompatiblePointerConversion
2015-11-29 10:30:52 +01:00
static const struct {
unsigned int RequestSize;
CreateResponse_t CreateResponse;
} _Versions[] = {
2016-10-14 07:28:23 +02:00
{ sizeof(REQUEST_V4), (CreateResponse_t)CreateResponseV4 },
{ sizeof(REQUEST_V6), (CreateResponse_t)CreateResponseV6 },
{ sizeof(REQUEST_V6), (CreateResponse_t)CreateResponseV6 }
2015-11-29 10:30:52 +01:00
};
2016-10-14 07:28:23 +02:00
// ReSharper restore CppIncompatiblePointerConversion
2015-11-29 10:30:52 +01:00
RPC_FLAGS RpcFlags;
static int_fast8_t firstPacketSent;
2016-12-02 08:56:18 +01:00
static DWORD CallId = 2; // M$ starts with CallId 2. So we do the same.
2015-11-29 10:30:52 +01:00
//
// RPC request (server)
//
#if defined(_PEDANTIC) && !defined(NO_LOG)
static void CheckRpcRequest(const RPC_REQUEST64 *const Request, const unsigned int len, WORD* NdrCtx, WORD* Ndr64Ctx, WORD Ctx)
{
uint_fast8_t kmsMajorVersion;
uint32_t requestSize = Ctx != *Ndr64Ctx ? sizeof(RPC_REQUEST) : sizeof(RPC_REQUEST64);
if (len < requestSize)
{
logger("Fatal: RPC request (including header) must be at least %i bytes but is only %i bytes.\n",
2016-10-14 07:28:23 +02:00
(int)(sizeof(RPC_HEADER) + requestSize),
(int)(len + sizeof(RPC_HEADER))
2015-11-29 10:30:52 +01:00
);
return;
}
if (len < requestSize + sizeof(DWORD))
{
logger("Fatal: KMS Request too small to contain version info (less than 4 bytes).\n");
return;
}
if (Ctx != *Ndr64Ctx)
2016-10-08 07:35:48 +02:00
kmsMajorVersion = (uint_fast8_t)LE16(((WORD*)Request->Ndr.Data)[1]);
2015-11-29 10:30:52 +01:00
else
2016-10-08 07:35:48 +02:00
kmsMajorVersion = (uint_fast8_t)LE16(((WORD*)Request->Ndr64.Data)[1]);
2015-11-29 10:30:52 +01:00
if (kmsMajorVersion > 6)
{
logger("Fatal: KMSv%u is not supported.\n", (unsigned int)kmsMajorVersion);
}
else
{
2016-10-31 13:59:15 +01:00
if (len > _Versions[kmsMajorVersion - 4].RequestSize + requestSize)
2015-11-29 10:30:52 +01:00
logger("Warning: %u excess bytes in RPC request.\n",
2016-10-31 13:59:15 +01:00
len - (_Versions[kmsMajorVersion - 4].RequestSize + requestSize)
2015-11-29 10:30:52 +01:00
);
}
if (Ctx != *Ndr64Ctx && Ctx != *NdrCtx)
2016-12-02 08:56:18 +01:00
{
if (*Ndr64Ctx == RPC_INVALID_CTX)
{
logger("Warning: Context id should be %u but is %u.\n", (unsigned int)*NdrCtx, Ctx);
}
else
{
logger("Warning: Context id should be %u (NDR32) or %u (NDR64) but is %u.\n",
(unsigned int)*NdrCtx,
(unsigned int)*Ndr64Ctx,
Ctx
);
}
}
2015-11-29 10:30:52 +01:00
if (Request->Opnum)
logger("Warning: OpNum should be 0 but is %u.\n",
2016-10-14 07:28:23 +02:00
(unsigned int)LE16(Request->Opnum)
2015-11-29 10:30:52 +01:00
);
if (LE32(Request->AllocHint) != len - sizeof(RPC_REQUEST) + sizeof(Request->Ndr))
logger("Warning: Allocation hint should be %u but is %u.\n",
2016-10-14 07:28:23 +02:00
len + sizeof(Request->Ndr),
LE32(Request->AllocHint)
2015-11-29 10:30:52 +01:00
);
if (Ctx != *Ndr64Ctx)
{
if (LE32(Request->Ndr.DataLength) != len - sizeof(RPC_REQUEST))
logger("Warning: NDR32 data length field should be %u but is %u.\n",
2016-10-14 07:28:23 +02:00
len - sizeof(RPC_REQUEST),
LE32(Request->Ndr.DataLength)
2015-11-29 10:30:52 +01:00
);
if (LE32(Request->Ndr.DataSizeIs) != len - sizeof(RPC_REQUEST))
logger("Warning: NDR32 data size field should be %u but is %u.\n",
2016-10-14 07:28:23 +02:00
len - sizeof(RPC_REQUEST),
LE32(Request->Ndr.DataSizeIs)
2015-11-29 10:30:52 +01:00
);
}
else
{
if (LE64(Request->Ndr64.DataLength) != len - sizeof(RPC_REQUEST64))
logger("Warning: NDR32 data length field should be %u but is %u.\n",
2016-10-14 07:28:23 +02:00
len - sizeof(RPC_REQUEST) + sizeof(Request->Ndr),
LE64(Request->Ndr64.DataLength)
2015-11-29 10:30:52 +01:00
);
if (LE64(Request->Ndr64.DataSizeIs) != len - sizeof(RPC_REQUEST64))
logger("Warning: NDR32 data size field should be %u but is %u.\n",
2016-10-14 07:28:23 +02:00
len - sizeof(RPC_REQUEST64),
LE64(Request->Ndr64.DataSizeIs)
2015-11-29 10:30:52 +01:00
);
}
}
#endif // defined(_PEDANTIC) && !defined(NO_LOG)
/*
* check RPC request for (somewhat) correct size
* allow any size that does not cause CreateResponse to fail badly
*/
static unsigned int checkRpcRequestSize(const RPC_REQUEST64 *const Request, const unsigned int requestSize, WORD* NdrCtx, WORD* Ndr64Ctx)
{
WORD Ctx = LE16(Request->ContextId);
# if defined(_PEDANTIC) && !defined(NO_LOG)
CheckRpcRequest(Request, requestSize, NdrCtx, Ndr64Ctx, Ctx);
# endif // defined(_PEDANTIC) && !defined(NO_LOG)
// Anything that is smaller than a v4 request is illegal
if (requestSize < sizeof(REQUEST_V4) + (Ctx != *Ndr64Ctx ? sizeof(RPC_REQUEST) : sizeof(RPC_REQUEST64))) return 0;
// Get KMS major version
2016-08-15 12:35:59 +02:00
uint16_t majorIndex, minor;
DWORD version;
2015-11-29 10:30:52 +01:00
2016-10-24 15:32:24 +02:00
# ifndef SIMPLE_RPC
2015-11-29 10:30:52 +01:00
if (Ctx != *Ndr64Ctx)
2016-08-15 12:35:59 +02:00
{
version = LE32(*(DWORD*)Request->Ndr.Data);
}
2015-11-29 10:30:52 +01:00
else
2016-08-15 12:35:59 +02:00
{
version = LE32(*(DWORD*)Request->Ndr64.Data);
}
2016-10-24 15:32:24 +02:00
# else // SIMPLE_RPC
version = LE32(*(DWORD*)Request->Ndr.Data);
# endif // SIMPLE_RPC
2016-08-15 12:35:59 +02:00
majorIndex = (uint16_t)(version >> 16) - 4;
minor = (uint16_t)(version & 0xffff);
2015-11-29 10:30:52 +01:00
// Only KMS v4, v5 and v6 are supported
2016-08-15 12:35:59 +02:00
if (majorIndex >= vlmcsd_countof(_Versions) || minor)
2015-11-29 10:30:52 +01:00
{
# ifndef NO_LOG
2016-08-15 12:35:59 +02:00
logger("Fatal: KMSv%hu.%hu unsupported\n", (unsigned short)majorIndex + 4, (unsigned short)minor);
2015-11-29 10:30:52 +01:00
# endif // NO_LOG
return 0;
}
// Could check for equality but allow bigger requests to support buggy RPC clients (e.g. wine)
// Buffer overrun is check by caller.
2016-08-15 12:35:59 +02:00
return (requestSize >= _Versions[majorIndex].RequestSize);
2015-11-29 10:30:52 +01:00
}
2016-12-02 08:56:18 +01:00
#ifndef SIMPLE_RPC
static int SendError(RPC_RESPONSE64 *const Response, DWORD nca_error)
{
Response->Error.Code = nca_error;
Response->Error.Padding = 0;
Response->AllocHint = LE32(32);
Response->ContextId = 0;
return 32;
}
#endif // SIMPLE_RPC
2015-11-29 10:30:52 +01:00
/*
* Handles the actual KMS request from the client.
* Calls KMS functions (CreateResponseV4 or CreateResponseV6) in kms.c
* Returns size of the KMS response packet or 0 on failure.
*
* The RPC packet size (excluding header) is actually in Response->AllocHint
*/
2016-08-15 12:35:59 +02:00
static int rpcRequest(const RPC_REQUEST64 *const Request, RPC_RESPONSE64 *const Response, const DWORD RpcAssocGroup_unused, const SOCKET sock_unused, WORD* NdrCtx, WORD* Ndr64Ctx, BYTE isValid, const char* const ipstr)
2015-11-29 10:30:52 +01:00
{
2016-08-15 12:35:59 +02:00
int ResponseSize; // <0 = Errorcode (HRESULT)
2015-11-29 10:30:52 +01:00
BYTE* requestData;
BYTE* responseData;
BYTE* pRpcReturnCode;
int len;
2016-10-24 15:32:24 +02:00
# ifndef SIMPLE_RPC
2018-10-24 05:40:18 +02:00
const WORD Ctx = LE16(Request->ContextId);
2016-10-24 15:32:24 +02:00
2016-12-02 08:56:18 +01:00
if (Ctx == *NdrCtx)
2015-11-29 10:30:52 +01:00
{
requestData = (BYTE*)&Request->Ndr.Data;
responseData = (BYTE*)&Response->Ndr.Data;
}
2016-12-02 08:56:18 +01:00
else if (Ctx == *Ndr64Ctx)
2015-11-29 10:30:52 +01:00
{
requestData = (BYTE*)&Request->Ndr64.Data;
responseData = (BYTE*)&Response->Ndr64.Data;
}
2016-12-02 08:56:18 +01:00
else
{
return SendError(Response, RPC_NCA_UNK_IF);
}
2015-11-29 10:30:52 +01:00
2016-10-24 15:32:24 +02:00
# else // SIMPLE_RPC
requestData = (BYTE*)&Request->Ndr.Data;
responseData = (BYTE*)&Response->Ndr.Data;
# endif // SIMPLE_RPC
2016-08-15 12:35:59 +02:00
ResponseSize = 0x8007000D; // Invalid Data
2015-11-29 10:30:52 +01:00
2016-08-15 12:35:59 +02:00
if (isValid)
2015-11-29 10:30:52 +01:00
{
2018-10-24 05:40:18 +02:00
const uint16_t majorIndex = LE16(((WORD*)requestData)[1]) - 4;
2016-10-14 07:28:23 +02:00
if (!((ResponseSize = _Versions[majorIndex].CreateResponse(requestData, responseData, ipstr)))) ResponseSize = 0x8007000D;
2015-11-29 10:30:52 +01:00
}
2016-10-24 15:32:24 +02:00
# ifndef SIMPLE_RPC
2015-11-29 10:30:52 +01:00
if (Ctx != *Ndr64Ctx)
{
2016-10-24 15:32:24 +02:00
# endif // !SIMPLE_RPC
2016-08-15 12:35:59 +02:00
if (ResponseSize < 0)
{
Response->Ndr.DataSizeMax = Response->Ndr.DataLength = 0;
len = sizeof(Response->Ndr) - sizeof(Response->Ndr.DataSizeIs);
}
else
{
Response->Ndr.DataSizeMax = LE32(0x00020000);
2016-10-14 07:28:23 +02:00
Response->Ndr.DataLength = Response->Ndr.DataSizeIs = LE32(ResponseSize);
2016-08-15 12:35:59 +02:00
len = ResponseSize + sizeof(Response->Ndr);
}
2016-10-24 15:32:24 +02:00
# ifndef SIMPLE_RPC
2015-11-29 10:30:52 +01:00
}
else
{
2016-08-15 12:35:59 +02:00
if (ResponseSize < 0)
{
Response->Ndr64.DataSizeMax = Response->Ndr64.DataLength = 0;
len = sizeof(Response->Ndr64) - sizeof(Response->Ndr64.DataSizeIs);
}
else
{
Response->Ndr64.DataSizeMax = LE64(0x00020000ULL);
Response->Ndr64.DataLength = Response->Ndr64.DataSizeIs = LE64((uint64_t)ResponseSize);
len = ResponseSize + sizeof(Response->Ndr64);
}
2015-11-29 10:30:52 +01:00
}
2016-10-24 15:32:24 +02:00
# endif // !SIMPLE_RPC
2015-11-29 10:30:52 +01:00
pRpcReturnCode = ((BYTE*)&Response->Ndr) + len;
2020-03-30 07:21:09 +02:00
PUT_UA32LE(pRpcReturnCode, ResponseSize < 0 ? ResponseSize : 0);
2015-11-29 10:30:52 +01:00
len += sizeof(DWORD);
// Pad zeros to 32-bit align (seems not neccassary but Windows RPC does it this way)
2018-10-24 05:40:18 +02:00
const int pad = ((~len & 3) + 1) & 3;
2015-11-29 10:30:52 +01:00
memset(pRpcReturnCode + sizeof(DWORD), 0, pad);
len += pad;
Response->AllocHint = LE32(len);
Response->ContextId = Request->ContextId;
*((WORD*)&Response->CancelCount) = 0; // CancelCount + Pad1
return len + 8;
}
#if defined(_PEDANTIC) && !defined(NO_LOG)
static void CheckRpcBindRequest(const RPC_BIND_REQUEST *const Request, const unsigned int len)
{
uint_fast8_t i, HasTransferSyntaxNDR32 = FALSE;
char guidBuffer1[GUID_STRING_LENGTH + 1], guidBuffer2[GUID_STRING_LENGTH + 1];
2018-10-24 05:40:18 +02:00
const uint32_t CapCtxItems = (len - sizeof(*Request) + sizeof(Request->CtxItems)) / sizeof(Request->CtxItems);
const DWORD NumCtxItems = LE32(Request->NumCtxItems);
2015-11-29 10:30:52 +01:00
if (NumCtxItems < CapCtxItems) // Can't be too small because already handled by RpcBindSize
logger("Warning: Excess bytes in RPC bind request.\n");
for (i = 0; i < NumCtxItems; i++)
{
2016-12-02 08:56:18 +01:00
struct CtxItem const* ctxItem = Request->CtxItems + i;
if (!IsEqualGUID(&ctxItem->InterfaceUUID, InterfaceUuid))
2015-11-29 10:30:52 +01:00
{
2016-12-02 08:56:18 +01:00
uuid2StringLE(&ctxItem->InterfaceUUID, guidBuffer1);
2015-11-29 10:30:52 +01:00
uuid2StringLE((GUID*)InterfaceUuid, guidBuffer2);
2016-12-02 08:56:18 +01:00
logger("Fatal: Interface UUID is %s but should be %s in Ctx item %u.\n", guidBuffer1, guidBuffer2, (unsigned int)i);
2015-11-29 10:30:52 +01:00
}
2016-12-02 08:56:18 +01:00
if (ctxItem->NumTransItems != LE16(1))
2015-11-29 10:30:52 +01:00
logger("Fatal: %u NDR32 transfer items detected in Ctx item %u, but only one is supported.\n",
2016-12-02 08:56:18 +01:00
(unsigned int)LE16(ctxItem->NumTransItems), (unsigned int)i
2015-11-29 10:30:52 +01:00
);
2016-12-02 08:56:18 +01:00
if (ctxItem->InterfaceVerMajor != LE16(1) || ctxItem->InterfaceVerMinor != 0)
logger("Warning: Interface version is %u.%u but should be 1.0.\n",
2020-03-30 07:21:09 +02:00
(unsigned int)LE16(ctxItem->InterfaceVerMajor),
2016-12-02 08:56:18 +01:00
(unsigned int)LE16(ctxItem->InterfaceVerMinor)
2015-11-29 10:30:52 +01:00
);
2016-12-02 08:56:18 +01:00
if (ctxItem->ContextId != LE16((WORD)i))
logger("Warning: context id of Ctx item %u is %u.\n", (unsigned int)i, (unsigned int)ctxItem->ContextId);
2015-11-29 10:30:52 +01:00
2016-12-02 08:56:18 +01:00
if (IsEqualGUID((GUID*)TransferSyntaxNDR32, &ctxItem->TransferSyntax))
2015-11-29 10:30:52 +01:00
{
HasTransferSyntaxNDR32 = TRUE;
2016-12-02 08:56:18 +01:00
if (ctxItem->SyntaxVersion != LE32(2))
logger("NDR32 transfer syntax version is %u but should be 2.\n", LE32(ctxItem->SyntaxVersion));
2015-11-29 10:30:52 +01:00
}
2016-12-02 08:56:18 +01:00
else if (IsEqualGUID((GUID*)TransferSyntaxNDR64, &ctxItem->TransferSyntax))
2015-11-29 10:30:52 +01:00
{
2016-12-02 08:56:18 +01:00
if (ctxItem->SyntaxVersion != LE32(1))
logger("NDR64 transfer syntax version is %u but should be 1.\n", LE32(ctxItem->SyntaxVersion));
2015-11-29 10:30:52 +01:00
}
2016-12-02 08:56:18 +01:00
else if (!memcmp(BindTimeFeatureNegotiation, (BYTE*)(&ctxItem->TransferSyntax), 8))
2015-11-29 10:30:52 +01:00
{
2016-12-02 08:56:18 +01:00
if (ctxItem->SyntaxVersion != LE32(1))
logger("BTFN syntax version is %u but should be 1.\n", LE32(ctxItem->SyntaxVersion));
2015-11-29 10:30:52 +01:00
}
}
if (!HasTransferSyntaxNDR32)
logger("Warning: RPC bind request has no NDR32 CtxItem.\n");
}
#endif // defined(_PEDANTIC) && !defined(NO_LOG)
/*
* Check, if we receive enough bytes to return a valid RPC bind response
*/
2017-06-22 09:21:58 +02:00
static unsigned int checkRpcBindSize(const RPC_BIND_REQUEST *const Request, const unsigned int RequestSize, WORD* NdrCtx_unused, WORD* Ndr64Ctx_unused)
2015-11-29 10:30:52 +01:00
{
2016-10-14 07:28:23 +02:00
if (RequestSize < sizeof(RPC_BIND_REQUEST)) return FALSE;
2015-11-29 10:30:52 +01:00
2018-10-24 05:40:18 +02:00
const unsigned int numCtxItems = LE32(Request->NumCtxItems);
2015-11-29 10:30:52 +01:00
2016-12-02 08:56:18 +01:00
if (RequestSize < sizeof(RPC_BIND_REQUEST) - sizeof(Request->CtxItems[0]) + numCtxItems * sizeof(Request->CtxItems[0])) return FALSE;
2015-11-29 10:30:52 +01:00
2016-10-14 07:28:23 +02:00
#if defined(_PEDANTIC) && !defined(NO_LOG)
2015-11-29 10:30:52 +01:00
CheckRpcBindRequest(Request, RequestSize);
2016-10-14 07:28:23 +02:00
#endif // defined(_PEDANTIC) && !defined(NO_LOG)
2015-11-29 10:30:52 +01:00
return TRUE;
}
/*
* Accepts a bind or alter context request from the client and composes the bind response.
* Needs the socket because the tcp port number is part of the response.
* len is not used here.
*
* Returns TRUE on success.
*/
static int rpcBind(const RPC_BIND_REQUEST *const Request, RPC_BIND_RESPONSE* Response, const DWORD RpcAssocGroup, const SOCKET sock, WORD* NdrCtx, WORD* Ndr64Ctx, BYTE packetType, const char* const ipstr_unused)
{
2016-12-02 08:56:18 +01:00
unsigned int i;
2018-10-24 05:40:18 +02:00
const DWORD numCtxItems = LE32(Request->NumCtxItems);
2015-11-29 10:30:52 +01:00
int_fast8_t IsNDR64possible = FALSE;
uint_fast8_t portNumberSize;
socklen_t socklen;
struct sockaddr_storage addr;
// M$ RPC does not do this. Pad bytes contain apparently random data
// memset(Response->SecondaryAddress, 0, sizeof(Response->SecondaryAddress));
socklen = sizeof addr;
if (
packetType == RPC_PT_ALTERCONTEXT_REQ ||
getsockname(sock, (struct sockaddr*)&addr, &socklen) ||
getnameinfo((struct sockaddr*)&addr, socklen, NULL, 0, (char*)Response->SecondaryAddress, sizeof(Response->SecondaryAddress), NI_NUMERICSERV))
{
2016-10-08 07:35:48 +02:00
portNumberSize = 0;
Response->SecondaryAddressLength = 0;
2015-11-29 10:30:52 +01:00
}
else
{
2016-10-08 07:35:48 +02:00
portNumberSize = (uint_fast8_t)strlen((char*)Response->SecondaryAddress) + 1;
2015-11-29 10:30:52 +01:00
Response->SecondaryAddressLength = LE16(portNumberSize);
}
Response->MaxXmitFrag = Request->MaxXmitFrag;
Response->MaxRecvFrag = Request->MaxRecvFrag;
2016-10-14 07:28:23 +02:00
Response->AssocGroup = LE32(RpcAssocGroup);
2015-11-29 10:30:52 +01:00
// This is really ugly (but efficient) code to support padding after the secondary address field
if (portNumberSize < 3)
{
Response = (RPC_BIND_RESPONSE*)((BYTE*)Response - 4);
}
Response->NumResults = Request->NumCtxItems;
2016-10-24 15:32:24 +02:00
# ifndef SIMPLE_RPC
2016-12-02 08:56:18 +01:00
for (i = 0; i < numCtxItems; i++)
2015-11-29 10:30:52 +01:00
{
2016-12-02 08:56:18 +01:00
const struct CtxItem* ctxItem = &Request->CtxItems[i];
if (IsEqualGUID((GUID*)TransferSyntaxNDR32, &ctxItem->TransferSyntax))
2015-11-29 10:30:52 +01:00
{
2016-12-02 08:56:18 +01:00
/*if (packetType == RPC_PT_BIND_REQ)*/
*NdrCtx = LE16(ctxItem->ContextId);
}
2015-11-29 10:30:52 +01:00
2016-12-02 08:56:18 +01:00
if (UseServerRpcNDR64 && IsEqualGUID((GUID*)TransferSyntaxNDR64, &ctxItem->TransferSyntax))
{
IsNDR64possible = TRUE;
2015-11-29 10:30:52 +01:00
2016-12-02 08:56:18 +01:00
/*if (packetType == RPC_PT_BIND_REQ)*/
*Ndr64Ctx = LE16(ctxItem->ContextId);
2015-11-29 10:30:52 +01:00
}
}
2016-10-24 15:32:24 +02:00
# endif // !SIMPLE_RPC
2015-11-29 10:30:52 +01:00
for (i = 0; i < numCtxItems; i++)
{
2020-03-30 07:21:09 +02:00
struct CtxResults* result = Response->Results + i;
2016-12-02 08:56:18 +01:00
const GUID* ctxTransferSyntax = &Request->CtxItems[i].TransferSyntax;
2015-11-29 10:30:52 +01:00
2016-12-02 08:56:18 +01:00
# ifndef SIMPLE_RPC
WORD nackReason = RPC_ABSTRACTSYNTAX_UNSUPPORTED;
# endif // !SIMPLE_RPC
memset(&result->TransferSyntax, 0, sizeof(GUID));
2015-11-29 10:30:52 +01:00
2016-12-02 08:56:18 +01:00
# ifndef SIMPLE_RPC
2018-10-24 05:40:18 +02:00
const int isInterfaceUUID = IsEqualGUID(&Request->CtxItems[i].InterfaceUUID, (GUID*)InterfaceUuid);
2016-12-02 08:56:18 +01:00
if (isInterfaceUUID) nackReason = RPC_SYNTAX_UNSUPPORTED;
# else // SIMPLE_RPC
# define isInterfaceUUID TRUE
# endif // SIMPLE_RPC
if (isInterfaceUUID && !IsNDR64possible && IsEqualGUID((GUID*)TransferSyntaxNDR32, ctxTransferSyntax))
{
result->SyntaxVersion = LE32(2);
result->AckResult = result->AckReason = RPC_BIND_ACCEPT;
memcpy(&result->TransferSyntax, TransferSyntaxNDR32, sizeof(GUID));
continue;
2015-11-29 10:30:52 +01:00
}
2016-10-24 15:32:24 +02:00
# ifndef SIMPLE_RPC
2016-12-02 08:56:18 +01:00
if (IsEqualGUID((GUID*)TransferSyntaxNDR64, ctxTransferSyntax))
2015-11-29 10:30:52 +01:00
{
2016-12-02 08:56:18 +01:00
if (!UseServerRpcNDR64) nackReason = RPC_SYNTAX_UNSUPPORTED;
2015-11-29 10:30:52 +01:00
2016-12-02 08:56:18 +01:00
if (isInterfaceUUID && IsNDR64possible)
{
result->SyntaxVersion = LE32(1);
result->AckResult = result->AckReason = RPC_BIND_ACCEPT;
memcpy(&result->TransferSyntax, TransferSyntaxNDR64, sizeof(GUID));
continue;
}
2015-11-29 10:30:52 +01:00
}
2016-12-02 08:56:18 +01:00
if (!memcmp(BindTimeFeatureNegotiation, ctxTransferSyntax, 8))
2015-11-29 10:30:52 +01:00
{
2016-12-02 08:56:18 +01:00
nackReason = RPC_SYNTAX_UNSUPPORTED;
if (UseServerRpcBTFN)
{
result->SyntaxVersion = 0;
result->AckResult = RPC_BIND_ACK;
2015-11-29 10:30:52 +01:00
2016-12-02 08:56:18 +01:00
// Features requested are actually encoded in the GUID
result->AckReason =
((WORD*)(ctxTransferSyntax))[4] &
(RPC_BTFN_SEC_CONTEXT_MULTIPLEX | RPC_BTFN_KEEP_ORPHAN);
continue;
}
2015-11-29 10:30:52 +01:00
}
2016-10-24 15:32:24 +02:00
# endif // !SIMPLE_RPC
2016-12-02 08:56:18 +01:00
result->SyntaxVersion = 0;
result->AckResult = RPC_BIND_NACK;
# ifndef SIMPLE_RPC
result->AckReason = nackReason;
# else // SIMPLE_RPC
# undef isInterfaceUUID
result->AckReason = RPC_SYNTAX_UNSUPPORTED;
# endif // SIMPLE_RPC
2015-11-29 10:30:52 +01:00
}
2016-12-02 08:56:18 +01:00
//if (!_st) return 0;
2015-11-29 10:30:52 +01:00
2016-12-02 08:56:18 +01:00
return sizeof(RPC_BIND_RESPONSE) + numCtxItems * sizeof(struct CtxResults) - (portNumberSize < 3 ? 4 : 0);
2015-11-29 10:30:52 +01:00
}
//
// Main RPC handling routine
//
2016-10-14 07:28:23 +02:00
typedef unsigned int(*GetResponseSize_t)(const void *const request, const unsigned int requestSize, WORD* NdrCtx, WORD* Ndr64Ctx);
typedef int(*GetResponse_t)(const void* const request, void* response, const DWORD rpcAssocGroup, const SOCKET socket, WORD* NdrCtx, WORD* Ndr64Ctx, BYTE packetType, const char* const ipstr);
2015-11-29 10:30:52 +01:00
2016-10-14 07:28:23 +02:00
// ReSharper disable CppIncompatiblePointerConversion
2015-11-29 10:30:52 +01:00
static const struct {
BYTE ResponsePacketType;
2016-12-02 08:56:18 +01:00
GetResponseSize_t CheckRequest;
2015-11-29 10:30:52 +01:00
GetResponse_t GetResponse;
}
_Actions[] = {
2016-10-14 07:28:23 +02:00
{ RPC_PT_BIND_ACK, (GetResponseSize_t)checkRpcBindSize, (GetResponse_t)rpcBind },
{ RPC_PT_RESPONSE, (GetResponseSize_t)checkRpcRequestSize, (GetResponse_t)rpcRequest },
{ RPC_PT_ALTERCONTEXT_ACK, (GetResponseSize_t)checkRpcBindSize, (GetResponse_t)rpcBind },
2015-11-29 10:30:52 +01:00
};
2016-10-14 07:28:23 +02:00
// ReSharper restore CppIncompatiblePointerConversion
2015-11-29 10:30:52 +01:00
2016-12-02 08:56:18 +01:00
/*
* Initializes an RPC request header as needed for KMS, i.e. packet always fits in one fragment.
* size cannot be greater than fragment length negotiated during RPC bind.
*/
static void createRpcHeader(RPC_HEADER* header, BYTE packetType, WORD size)
{
header->PacketType = packetType;
header->PacketFlags = RPC_PF_FIRST | RPC_PF_LAST;
header->VersionMajor = 5;
header->VersionMinor = 0;
header->AuthLength = 0;
header->DataRepresentation = BE32(0x10000000); // Little endian, ASCII charset, IEEE floating point
header->CallId = LE32(CallId);
header->FragLength = LE16(size);
}
2015-11-29 10:30:52 +01:00
/*
* This is the main RPC server loop. Returns after KMS request has been serviced
* or a timeout has occured.
*/
2016-12-02 08:56:18 +01:00
void rpcServer(const SOCKET sock, const DWORD rpcAssocGroup, const char* const ipstr)
2015-11-29 10:30:52 +01:00
{
RPC_HEADER rpcRequestHeader;
2016-12-02 08:56:18 +01:00
WORD NdrCtx = RPC_INVALID_CTX, Ndr64Ctx = RPC_INVALID_CTX;
2015-11-29 10:30:52 +01:00
randomNumberInit();
while (_recv(sock, &rpcRequestHeader, sizeof(rpcRequestHeader)))
{
//int_fast8_t _st;
unsigned int request_len, response_len;
uint_fast8_t _a;
2016-10-14 07:28:23 +02:00
#if defined(_PEDANTIC) && !defined(NO_LOG)
2015-11-29 10:30:52 +01:00
checkRpcHeader(&rpcRequestHeader, rpcRequestHeader.PacketType, &logger);
2016-10-14 07:28:23 +02:00
#endif // defined(_PEDANTIC) && !defined(NO_LOG)
2015-11-29 10:30:52 +01:00
switch (rpcRequestHeader.PacketType)
{
2016-10-14 07:28:23 +02:00
case RPC_PT_BIND_REQ: _a = 0; break;
case RPC_PT_REQUEST: _a = 1; break;
case RPC_PT_ALTERCONTEXT_REQ: _a = 2; break;
default: return;
2015-11-29 10:30:52 +01:00
}
request_len = LE16(rpcRequestHeader.FragLength) - sizeof(rpcRequestHeader);
BYTE requestBuffer[MAX_REQUEST_SIZE + sizeof(RPC_RESPONSE64)];
BYTE responseBuffer[MAX_RESPONSE_SIZE + sizeof(RPC_HEADER) + sizeof(RPC_RESPONSE64)];
RPC_HEADER *rpcResponseHeader = (RPC_HEADER *)responseBuffer;
2016-10-14 07:28:23 +02:00
RPC_RESPONSE* rpcResponse = (RPC_RESPONSE*)(responseBuffer + sizeof(rpcRequestHeader));
2015-11-29 10:30:52 +01:00
// The request is larger than the buffer size
if (request_len > MAX_REQUEST_SIZE + sizeof(RPC_REQUEST64)) return;
// Unable to receive the complete request
if (!_recv(sock, requestBuffer, request_len)) return;
2016-12-02 08:56:18 +01:00
# if !defined(SIMPLE_RPC) && defined(_PEDANTIC)
if (rpcRequestHeader.PacketType == RPC_PT_REQUEST && (rpcRequestHeader.VersionMajor != 5 || rpcRequestHeader.VersionMinor != 0))
{
response_len = SendError((RPC_RESPONSE64*)rpcResponse, RPC_NCA_PROTO_ERROR);
}
else
# endif // !defined(SIMPLE_RPC) && defined(_PEDANTIC)
{
BYTE isValid = (BYTE)_Actions[_a].CheckRequest(requestBuffer, request_len, &NdrCtx, &Ndr64Ctx);
if (rpcRequestHeader.PacketType != RPC_PT_REQUEST && !isValid) return;
2015-11-29 10:30:52 +01:00
2016-12-02 08:56:18 +01:00
// Unable to create a valid response from request
if (!((response_len = _Actions[_a].GetResponse(requestBuffer, rpcResponse, rpcAssocGroup, sock, &NdrCtx, &Ndr64Ctx, rpcRequestHeader.PacketType != RPC_PT_REQUEST ? rpcRequestHeader.PacketType : isValid, ipstr)))) return;
}
2015-11-29 10:30:52 +01:00
memcpy(rpcResponseHeader, &rpcRequestHeader, sizeof(RPC_HEADER));
2016-12-02 08:56:18 +01:00
# ifndef SIMPLE_RPC
if (response_len == 32)
{
createRpcHeader(rpcResponseHeader, RPC_PT_FAULT, 0);
rpcResponseHeader->PacketFlags = RPC_PF_FIRST | RPC_PF_LAST | RPC_PF_NOT_EXEC;
}
else
# endif // SIMPLE_RPC
{
response_len += sizeof(RPC_HEADER);
rpcResponseHeader->PacketType = _Actions[_a].ResponsePacketType;
2015-11-29 10:30:52 +01:00
2016-12-02 08:56:18 +01:00
if (rpcResponseHeader->PacketType == RPC_PT_ALTERCONTEXT_ACK)
{
rpcResponseHeader->PacketFlags = RPC_PF_FIRST | RPC_PF_LAST;
}
}
rpcResponseHeader->FragLength = LE16((WORD)response_len);
2015-11-29 10:30:52 +01:00
if (!_send(sock, responseBuffer, response_len)) return;
2016-12-02 08:56:18 +01:00
if (DisconnectImmediately && (rpcResponseHeader->PacketType == RPC_PT_RESPONSE || rpcResponseHeader->PacketType == RPC_PT_FAULT))
return;
2015-11-29 10:30:52 +01:00
}
}
/* RPC client functions */
/*
* Checks RPC header. Returns 0 on success.
* This is mainly for debugging a non Microsoft KMS server that uses its own RPC code.
*/
2016-12-02 08:56:18 +01:00
static int checkRpcHeader(const RPC_HEADER *const header, const BYTE desiredPacketType, const PRINTFUNC p)
2015-11-29 10:30:52 +01:00
{
int status = 0;
2016-12-02 08:56:18 +01:00
if (header->PacketType != desiredPacketType)
2015-11-29 10:30:52 +01:00
{
p("Fatal: Received wrong RPC packet type. Expected %u but got %u\n",
2016-10-14 07:28:23 +02:00
(uint32_t)desiredPacketType,
2016-12-02 08:56:18 +01:00
header->PacketType
2015-11-29 10:30:52 +01:00
);
2016-08-15 12:35:59 +02:00
status = RPC_S_PROTOCOL_ERROR;
2015-11-29 10:30:52 +01:00
}
2016-12-02 08:56:18 +01:00
if (header->DataRepresentation != BE32(0x10000000))
2015-11-29 10:30:52 +01:00
{
p("Fatal: RPC response does not conform to Microsoft's limited support of DCE RPC\n");
2016-08-15 12:35:59 +02:00
status = RPC_S_PROTOCOL_ERROR;
2015-11-29 10:30:52 +01:00
}
2016-12-02 08:56:18 +01:00
if (header->AuthLength != 0)
2015-11-29 10:30:52 +01:00
{
p("Fatal: RPC response requests authentication\n");
2016-08-15 12:35:59 +02:00
status = RPC_S_UNKNOWN_AUTHN_TYPE;
2015-11-29 10:30:52 +01:00
}
// vlmcsd does not support fragmented packets (not yet neccassary)
2016-12-02 08:56:18 +01:00
if ((header->PacketFlags & (RPC_PF_FIRST | RPC_PF_LAST)) != (RPC_PF_FIRST | RPC_PF_LAST))
2015-11-29 10:30:52 +01:00
{
p("Fatal: RPC packet flags RPC_PF_FIRST and RPC_PF_LAST are not both set.\n");
2016-08-15 12:35:59 +02:00
status = RPC_S_CANNOT_SUPPORT;
2015-11-29 10:30:52 +01:00
}
2016-12-02 08:56:18 +01:00
if (header->PacketFlags & RPC_PF_CANCEL_PENDING) p("Warning: %s should not be set\n", "RPC_PF_CANCEL_PENDING");
if (header->PacketFlags & RPC_PF_RESERVED) p("Warning: %s should not be set\n", "RPC_PF_RESERVED");
if (header->PacketFlags & RPC_PF_NOT_EXEC) p("Warning: %s should not be set\n", "RPC_PF_NOT_EXEC");
if (header->PacketFlags & RPC_PF_MAYBE) p("Warning: %s should not be set\n", "RPC_PF_MAYBE");
if (header->PacketFlags & RPC_PF_OBJECT) p("Warning: %s should not be set\n", "RPC_PF_OBJECT");
2015-11-29 10:30:52 +01:00
2016-12-02 08:56:18 +01:00
if (header->VersionMajor != 5 || header->VersionMinor != 0)
2015-11-29 10:30:52 +01:00
{
2016-12-02 08:56:18 +01:00
p("Fatal: Expected RPC version 5.0 and got %u.%u\n", header->VersionMajor, header->VersionMinor);
2016-08-15 12:35:59 +02:00
status = RPC_S_INVALID_VERS_OPTION;
2015-11-29 10:30:52 +01:00
}
return status;
}
/*
* Checks an RPC response header. Does basic header checks by calling checkRpcHeader()
* and then does additional checks if response header complies with the respective request header.
* PRINTFUNC p can be anything that has the same prototype as printf.
* Returns 0 on success.
*/
2016-10-14 07:28:23 +02:00
// ReSharper disable once CppIncompatiblePointerConversion
2015-11-29 10:30:52 +01:00
static int checkRpcResponseHeader(const RPC_HEADER *const ResponseHeader, const RPC_HEADER *const RequestHeader, const BYTE desiredPacketType, const PRINTFUNC p)
{
static int_fast8_t WineBugDetected = FALSE;
int status = checkRpcHeader(ResponseHeader, desiredPacketType, p);
if (desiredPacketType == RPC_PT_BIND_ACK)
{
if ((ResponseHeader->PacketFlags & RPC_PF_MULTIPLEX) != (RequestHeader->PacketFlags & RPC_PF_MULTIPLEX))
{
p("Warning: RPC_PF_MULTIPLEX of RPC request and response should match\n");
}
}
else
{
if (ResponseHeader->PacketFlags & RPC_PF_MULTIPLEX)
{
p("Warning: %s should not be set\n", "RPC_PF_MULTIPLEX");
}
}
if (!status && ResponseHeader->CallId == LE32(1))
{
if (!WineBugDetected)
{
p("Warning: Buggy RPC of Wine detected. Call Id of Response is always 1\n");
WineBugDetected = TRUE;
}
}
else if (ResponseHeader->CallId != RequestHeader->CallId)
{
p("Fatal: Sent Call Id %u but received answer for Call Id %u\n",
2016-10-14 07:28:23 +02:00
(uint32_t)LE32(RequestHeader->CallId),
(uint32_t)LE32(ResponseHeader->CallId)
2015-11-29 10:30:52 +01:00
);
2016-08-15 12:35:59 +02:00
status = RPC_S_PROTOCOL_ERROR;
2015-11-29 10:30:52 +01:00
}
return status;
}
/*
* Sends a KMS request via RPC and receives a response.
* Parameters are raw (encrypted) reqeuests / responses.
* Returns 0 on success.
*/
2016-12-02 08:56:18 +01:00
RpcStatus rpcSendRequest(const RpcCtx sock, const BYTE *const kmsRequest, const size_t requestSize, BYTE **kmsResponse, size_t *const responseSize)
2015-11-29 10:30:52 +01:00
{
2016-10-14 07:28:23 +02:00
#define MAX_EXCESS_BYTES 16
2015-11-29 10:30:52 +01:00
RPC_HEADER *RequestHeader, ResponseHeader;
RPC_REQUEST64 *RpcRequest;
RPC_RESPONSE64 _Response;
2016-10-14 07:28:23 +02:00
int status;
2018-10-24 05:40:18 +02:00
const int_fast8_t useNdr64 = RpcFlags.HasNDR64 && UseClientRpcNDR64 && firstPacketSent;
2015-11-29 10:30:52 +01:00
size_t size = sizeof(RPC_HEADER) + (useNdr64 ? sizeof(RPC_REQUEST64) : sizeof(RPC_REQUEST)) + requestSize;
size_t responseSize2;
2016-12-02 08:56:18 +01:00
*kmsResponse = NULL;
2015-11-29 10:30:52 +01:00
BYTE *_Request = (BYTE*)vlmcsd_malloc(size);
RequestHeader = (RPC_HEADER*)_Request;
RpcRequest = (RPC_REQUEST64*)(_Request + sizeof(RPC_HEADER));
2016-12-02 08:56:18 +01:00
createRpcHeader(RequestHeader, RPC_PT_REQUEST, (WORD)size);
2015-11-29 10:30:52 +01:00
// Increment CallId for next Request
CallId++;
RpcRequest->Opnum = 0;
if (useNdr64)
{
RpcRequest->ContextId = LE16(1); // We negotiate NDR64 always as context 1
2016-10-08 07:35:48 +02:00
RpcRequest->AllocHint = LE32((DWORD)(requestSize + sizeof(RpcRequest->Ndr64)));
2015-11-29 10:30:52 +01:00
RpcRequest->Ndr64.DataLength = LE64((uint64_t)requestSize);
RpcRequest->Ndr64.DataSizeIs = LE64((uint64_t)requestSize);
2016-12-02 08:56:18 +01:00
memcpy(RpcRequest->Ndr64.Data, kmsRequest, requestSize);
2015-11-29 10:30:52 +01:00
}
else
{
RpcRequest->ContextId = 0; // We negotiate NDR32 always as context 0
2016-10-08 07:35:48 +02:00
RpcRequest->AllocHint = LE32((DWORD)(requestSize + sizeof(RpcRequest->Ndr)));
RpcRequest->Ndr.DataLength = LE32((DWORD)requestSize);
RpcRequest->Ndr.DataSizeIs = LE32((DWORD)requestSize);
2016-12-02 08:56:18 +01:00
memcpy(RpcRequest->Ndr.Data, kmsRequest, requestSize);
2015-11-29 10:30:52 +01:00
}
2016-10-14 07:28:23 +02:00
for (;;)
2015-11-29 10:30:52 +01:00
{
int bytesread;
2016-10-08 07:35:48 +02:00
if (!_send(sock, _Request, (int)size))
2015-11-29 10:30:52 +01:00
{
2016-08-15 12:35:59 +02:00
printerrorf("\nFatal: Could not send RPC request\n");
status = RPC_S_COMM_FAILURE;
2015-11-29 10:30:52 +01:00
break;
}
if (!_recv(sock, &ResponseHeader, sizeof(RPC_HEADER)))
{
2016-08-15 12:35:59 +02:00
printerrorf("\nFatal: No RPC response received from server\n");
status = RPC_S_COMM_FAILURE;
2015-11-29 10:30:52 +01:00
break;
}
2016-08-15 12:35:59 +02:00
if ((status = checkRpcResponseHeader(&ResponseHeader, RequestHeader, RPC_PT_RESPONSE, &printerrorf))) break;
2015-11-29 10:30:52 +01:00
size = useNdr64 ? sizeof(RPC_RESPONSE64) : sizeof(RPC_RESPONSE);
if (size > LE16(ResponseHeader.FragLength) - sizeof(ResponseHeader))
size = LE16(ResponseHeader.FragLength) - sizeof(ResponseHeader);
2016-10-08 07:35:48 +02:00
if (!_recv(sock, &_Response, (int)size))
2015-11-29 10:30:52 +01:00
{
2016-08-15 12:35:59 +02:00
printerrorf("\nFatal: RPC response is incomplete\n");
status = RPC_S_COMM_FAILURE;
2015-11-29 10:30:52 +01:00
break;
}
if (_Response.CancelCount != 0)
{
2016-08-15 12:35:59 +02:00
printerrorf("\nFatal: RPC response cancel count is not 0\n");
status = RPC_S_CALL_CANCELLED;
2016-10-14 07:28:23 +02:00
break;
2015-11-29 10:30:52 +01:00
}
if (_Response.ContextId != (useNdr64 ? LE16(1) : 0))
{
2016-08-15 12:35:59 +02:00
printerrorf("\nFatal: RPC response context id %u is not bound\n", (unsigned int)LE16(_Response.ContextId));
status = RPC_X_SS_CONTEXT_DAMAGED;
2016-10-14 07:28:23 +02:00
break;
2015-11-29 10:30:52 +01:00
}
int_fast8_t sizesMatch;
if (useNdr64)
{
*responseSize = (size_t)LE64(_Response.Ndr64.DataLength);
responseSize2 = (size_t)LE64(_Response.Ndr64.DataSizeIs);
2016-08-15 12:35:59 +02:00
if (/*!*responseSize ||*/ !_Response.Ndr64.DataSizeMax)
2015-11-29 10:30:52 +01:00
{
status = (int)LE32(_Response.Ndr64.status);
break;
}
sizesMatch = (size_t)LE64(_Response.Ndr64.DataLength) == responseSize2;
}
else
{
*responseSize = (size_t)LE32(_Response.Ndr.DataLength);
responseSize2 = (size_t)LE32(_Response.Ndr.DataSizeIs);
2016-08-15 12:35:59 +02:00
if (/*!*responseSize ||*/ !_Response.Ndr.DataSizeMax)
2015-11-29 10:30:52 +01:00
{
status = (int)LE32(_Response.Ndr.status);
break;
}
sizesMatch = (size_t)LE32(_Response.Ndr.DataLength) == responseSize2;
}
if (!sizesMatch)
{
2016-08-15 12:35:59 +02:00
printerrorf("\nFatal: NDR data length (%u) does not match NDR data size (%u)\n",
2016-10-14 07:28:23 +02:00
(uint32_t)*responseSize,
(uint32_t)LE32(_Response.Ndr.DataSizeIs)
2015-11-29 10:30:52 +01:00
);
2016-08-15 12:35:59 +02:00
status = RPC_S_PROTOCOL_ERROR;
2016-10-14 07:28:23 +02:00
break;
2015-11-29 10:30:52 +01:00
}
2016-12-02 08:56:18 +01:00
*kmsResponse = (BYTE*)vlmcsd_malloc(*responseSize + MAX_EXCESS_BYTES);
2015-11-29 10:30:52 +01:00
// If RPC stub is too short, assume missing bytes are zero (same ill behavior as MS RPC)
2016-12-02 08:56:18 +01:00
memset(*kmsResponse, 0, *responseSize + MAX_EXCESS_BYTES);
2015-11-29 10:30:52 +01:00
// Read up to 16 bytes more than bytes expected to detect faulty KMS emulators
2016-12-02 08:56:18 +01:00
if ((bytesread = recv(sock, (char*)*kmsResponse, (int)(*responseSize) + MAX_EXCESS_BYTES, 0)) < (int)*responseSize)
2015-11-29 10:30:52 +01:00
{
2016-08-15 12:35:59 +02:00
printerrorf("\nFatal: No or incomplete KMS response received. Required %u bytes but only got %i\n",
2016-10-14 07:28:23 +02:00
(uint32_t)*responseSize,
(int32_t)(bytesread < 0 ? 0 : bytesread)
2015-11-29 10:30:52 +01:00
);
2016-08-15 12:35:59 +02:00
status = RPC_S_PROTOCOL_ERROR;
2015-11-29 10:30:52 +01:00
break;
}
DWORD *pReturnCode;
2018-10-24 05:40:18 +02:00
const size_t len = *responseSize + (useNdr64 ? sizeof(_Response.Ndr64) : sizeof(_Response.Ndr)) + sizeof(*pReturnCode);
const size_t pad = ((~len & 3) + 1) & 3;
2015-11-29 10:30:52 +01:00
if (len + pad != LE32(_Response.AllocHint))
{
2016-08-15 12:35:59 +02:00
printerrorf("\nWarning: RPC stub size is %u, should be %u (probably incorrect padding)\n", (uint32_t)LE32(_Response.AllocHint), (uint32_t)(len + pad));
2015-11-29 10:30:52 +01:00
}
else
{
size_t i;
for (i = 0; i < pad; i++)
{
2016-12-02 08:56:18 +01:00
if (*(*kmsResponse + *responseSize + sizeof(*pReturnCode) + i))
2015-11-29 10:30:52 +01:00
{
2016-08-15 12:35:59 +02:00
printerrorf("\nWarning: RPC stub data not padded to zeros according to Microsoft standard\n");
2015-11-29 10:30:52 +01:00
break;
}
}
}
2016-12-02 08:56:18 +01:00
pReturnCode = (DWORD*)(*kmsResponse + *responseSize + pad);
2020-03-30 07:21:09 +02:00
status = GET_UA32LE(pReturnCode);
//status = LE32(UA32(pReturnCode));
2015-11-29 10:30:52 +01:00
break;
}
free(_Request);
firstPacketSent = TRUE;
return status;
2016-10-14 07:28:23 +02:00
#undef MAX_EXCESS_BYTES
2015-11-29 10:30:52 +01:00
}
2018-10-24 05:40:18 +02:00
static int_fast8_t IsNullGuid(const BYTE* guidPtr)
2015-11-29 10:30:52 +01:00
{
int_fast8_t i;
for (i = 0; i < 16; i++)
{
if (guidPtr[i]) return FALSE;
}
return TRUE;
}
/*
* Perform RPC client bind. Accepts a connected client socket.
* Returns 0 on success. RPC binding is required before any payload can be
* exchanged. It negotiates about protocol details.
*/
2016-12-02 08:56:18 +01:00
static RpcStatus rpcBindOrAlterClientContext(const RpcCtx sock, const BYTE packetType, const int_fast8_t verbose)
2015-11-29 10:30:52 +01:00
{
RPC_HEADER *RequestHeader, ResponseHeader;
RPC_BIND_REQUEST *bindRequest;
RPC_BIND_RESPONSE *bindResponse;
int status;
2018-10-24 05:40:18 +02:00
const WORD ctxItems = 1 + (packetType == RPC_PT_BIND_REQ ? UseClientRpcNDR64 + UseClientRpcBTFN : 0);
const size_t rpcBindSize = (sizeof(RPC_HEADER) + sizeof(RPC_BIND_REQUEST) + (ctxItems - 1) * sizeof(bindRequest->CtxItems[0]));
2015-11-29 10:30:52 +01:00
WORD ctxIndex = 0;
WORD i;
2016-12-02 08:56:18 +01:00
WORD CtxBTFN = RPC_INVALID_CTX, CtxNDR64 = RPC_INVALID_CTX;
BYTE* request = (BYTE*)alloca(rpcBindSize);
2015-11-29 10:30:52 +01:00
2016-12-02 08:56:18 +01:00
RequestHeader = (RPC_HEADER*)request;
bindRequest = (RPC_BIND_REQUEST*)(request + sizeof(RPC_HEADER));
2015-11-29 10:30:52 +01:00
2016-12-02 08:56:18 +01:00
createRpcHeader(RequestHeader, packetType, (WORD)rpcBindSize);
2016-10-14 07:28:23 +02:00
RequestHeader->PacketFlags |= UseMultiplexedRpc ? RPC_PF_MULTIPLEX : 0;
2015-11-29 10:30:52 +01:00
2016-10-14 07:28:23 +02:00
bindRequest->AssocGroup = 0;
bindRequest->MaxRecvFrag = bindRequest->MaxXmitFrag = LE16(5840);
bindRequest->NumCtxItems = LE32(ctxItems);
2015-11-29 10:30:52 +01:00
// data that is identical in all Ctx items
for (i = 0; i < ctxItems; i++)
{
2016-12-02 08:56:18 +01:00
struct CtxItem* ctxItem = bindRequest->CtxItems + i;
ctxItem->ContextId = LE16(i);
ctxItem->InterfaceVerMajor = LE16(1);
ctxItem->InterfaceVerMinor = 0;
ctxItem->NumTransItems = LE16(1);
ctxItem->SyntaxVersion = i ? LE32(1) : LE32(2);
memcpy(&ctxItem->InterfaceUUID, InterfaceUuid, sizeof(GUID));
2015-11-29 10:30:52 +01:00
}
memcpy(&bindRequest->CtxItems[0].TransferSyntax, TransferSyntaxNDR32, sizeof(GUID));
2016-10-24 15:32:24 +02:00
if (UseClientRpcNDR64 && packetType == RPC_PT_BIND_REQ)
2015-11-29 10:30:52 +01:00
{
memcpy(&bindRequest->CtxItems[++ctxIndex].TransferSyntax, TransferSyntaxNDR64, sizeof(GUID));
CtxNDR64 = ctxIndex;
}
2016-10-24 15:32:24 +02:00
if (UseClientRpcBTFN && packetType == RPC_PT_BIND_REQ)
2015-11-29 10:30:52 +01:00
{
memcpy(&bindRequest->CtxItems[++ctxIndex].TransferSyntax, BindTimeFeatureNegotiation, sizeof(GUID));
CtxBTFN = ctxIndex;
}
2016-12-02 08:56:18 +01:00
if (!_send(sock, request, (int)rpcBindSize))
2015-11-29 10:30:52 +01:00
{
2016-08-15 12:35:59 +02:00
printerrorf("\nFatal: Sending RPC bind request failed\n");
return RPC_S_COMM_FAILURE;
2015-11-29 10:30:52 +01:00
}
if (!_recv(sock, &ResponseHeader, sizeof(RPC_HEADER)))
{
2016-08-15 12:35:59 +02:00
printerrorf("\nFatal: Did not receive a response from server\n");
return RPC_S_COMM_FAILURE;
2015-11-29 10:30:52 +01:00
}
if ((status = checkRpcResponseHeader
(
2016-10-14 07:28:23 +02:00
&ResponseHeader,
RequestHeader,
packetType == RPC_PT_BIND_REQ ? RPC_PT_BIND_ACK : RPC_PT_ALTERCONTEXT_ACK,
&printerrorf
2015-11-29 10:30:52 +01:00
)))
{
return status;
}
bindResponse = (RPC_BIND_RESPONSE*)vlmcsd_malloc(LE16(ResponseHeader.FragLength) - sizeof(RPC_HEADER));
BYTE* bindResponseBytePtr = (BYTE*)bindResponse;
if (!_recv(sock, bindResponse, LE16(ResponseHeader.FragLength) - sizeof(RPC_HEADER)))
{
2016-08-15 12:35:59 +02:00
printerrorf("\nFatal: Incomplete RPC bind acknowledgement received\n");
2015-11-29 10:30:52 +01:00
free(bindResponseBytePtr);
2016-08-15 12:35:59 +02:00
return RPC_S_COMM_FAILURE;
2015-11-29 10:30:52 +01:00
}
2016-10-14 07:28:23 +02:00
/*
* checking, whether a bind or alter context response is as expected.
* This check is very strict and checks whether a KMS emulator behaves exactly the same way
* as Microsoft's RPC does.
*/
status = 0;
2015-11-29 10:30:52 +01:00
2016-10-14 07:28:23 +02:00
if (bindResponse->SecondaryAddressLength < LE16(3))
bindResponse = (RPC_BIND_RESPONSE*)(bindResponseBytePtr - 4);
2015-11-29 10:30:52 +01:00
2016-10-14 07:28:23 +02:00
if (bindResponse->NumResults != bindRequest->NumCtxItems)
{
printerrorf("\nFatal: Expected %u CTX items but got %u\n",
(uint32_t)LE32(bindRequest->NumCtxItems),
(uint32_t)LE32(bindResponse->NumResults)
);
2015-11-29 10:30:52 +01:00
2016-10-14 07:28:23 +02:00
status = RPC_S_PROTOCOL_ERROR;
}
2015-11-29 10:30:52 +01:00
2016-10-14 07:28:23 +02:00
for (i = 0; i < ctxItems; i++)
{
const char* transferSyntaxName =
i == CtxBTFN ? "BTFN" : i == CtxNDR64 ? "NDR64" : "NDR32";
2015-11-29 10:30:52 +01:00
2016-12-02 08:56:18 +01:00
struct CtxResults* ctxResult = bindResponse->Results + i;
struct CtxItem* ctxItem = bindRequest->CtxItems + i;
if (ctxResult->AckResult == RPC_BIND_NACK) // transfer syntax was declined
2016-10-14 07:28:23 +02:00
{
2016-12-02 08:56:18 +01:00
if (!IsNullGuid((BYTE*)&ctxResult->TransferSyntax))
2016-10-14 07:28:23 +02:00
{
printerrorf(
"\nWarning: Rejected transfer syntax %s did not return NULL Guid\n",
transferSyntaxName
);
2015-11-29 10:30:52 +01:00
}
2016-12-02 08:56:18 +01:00
if (ctxResult->SyntaxVersion)
2015-11-29 10:30:52 +01:00
{
2016-10-14 07:28:23 +02:00
printerrorf(
"\nWarning: Rejected transfer syntax %s did not return syntax version 0 but %u\n",
transferSyntaxName,
2016-12-02 08:56:18 +01:00
LE32(ctxResult->SyntaxVersion)
2016-10-14 07:28:23 +02:00
);
2015-11-29 10:30:52 +01:00
}
2016-12-02 08:56:18 +01:00
if (ctxResult->AckReason == RPC_ABSTRACTSYNTAX_UNSUPPORTED)
2015-11-29 10:30:52 +01:00
{
2016-08-15 12:35:59 +02:00
printerrorf(
2016-10-14 07:28:23 +02:00
"\nWarning: Transfer syntax %s does not support KMS activation\n",
2015-11-29 10:30:52 +01:00
transferSyntaxName
);
}
2016-12-02 08:56:18 +01:00
else if (ctxResult->AckReason != RPC_SYNTAX_UNSUPPORTED)
2015-11-29 10:30:52 +01:00
{
2016-08-15 12:35:59 +02:00
printerrorf(
2016-10-14 07:28:23 +02:00
"\nWarning: Rejected transfer syntax %s did not return ack reason RPC_SYNTAX_UNSUPPORTED\n",
transferSyntaxName
2015-11-29 10:30:52 +01:00
);
}
2016-10-14 07:28:23 +02:00
continue;
}
2015-11-29 10:30:52 +01:00
2016-10-14 07:28:23 +02:00
if (i == CtxBTFN) // BTFN
{
2016-12-02 08:56:18 +01:00
if (ctxResult->AckResult != RPC_BIND_ACK)
2016-10-14 07:28:23 +02:00
{
printerrorf("\nWarning: BTFN did not respond with RPC_BIND_ACK or RPC_BIND_NACK\n");
2015-11-29 10:30:52 +01:00
}
2016-12-02 08:56:18 +01:00
if (ctxResult->AckReason != LE16(3))
2015-11-29 10:30:52 +01:00
{
2016-12-02 08:56:18 +01:00
printerrorf("\nWarning: BTFN did not return expected feature mask 0x3 but 0x%X\n", (unsigned int)LE16(ctxResult->AckReason));
2015-11-29 10:30:52 +01:00
}
2016-10-14 07:28:23 +02:00
if (verbose) printf("... BTFN ");
RpcFlags.HasBTFN = TRUE;
continue;
}
// NDR32 or NDR64 Ctx
2016-12-02 08:56:18 +01:00
if (ctxResult->AckResult != RPC_BIND_ACCEPT)
2016-10-14 07:28:23 +02:00
{
printerrorf(
"\nFatal: transfer syntax %s returned an invalid status, neither RPC_BIND_ACCEPT nor RPC_BIND_NACK\n",
transferSyntaxName
);
2015-11-29 10:30:52 +01:00
2016-10-14 07:28:23 +02:00
status = RPC_S_PROTOCOL_ERROR;
}
2016-12-02 08:56:18 +01:00
if (!IsEqualGUID(&ctxResult->TransferSyntax, &ctxItem->TransferSyntax))
2016-10-14 07:28:23 +02:00
{
printerrorf(
"\nFatal: Transfer syntax of RPC bind request and response does not match\n"
);
status = RPC_S_UNSUPPORTED_TRANS_SYN;
}
2016-12-02 08:56:18 +01:00
if (ctxResult->SyntaxVersion != ctxItem->SyntaxVersion)
2016-10-14 07:28:23 +02:00
{
printerrorf("\nFatal: Expected transfer syntax version %u for %s but got %u\n",
2016-12-02 08:56:18 +01:00
(uint32_t)LE32(ctxItem->SyntaxVersion),
2016-10-14 07:28:23 +02:00
transferSyntaxName,
2016-12-02 08:56:18 +01:00
(uint32_t)LE32(ctxResult->SyntaxVersion)
2016-10-14 07:28:23 +02:00
);
status = RPC_S_UNSUPPORTED_TRANS_SYN;
}
// The ack reason field is actually undefined here but Microsoft sets this to 0
2016-12-02 08:56:18 +01:00
if (ctxResult->AckReason != 0)
2016-10-14 07:28:23 +02:00
{
printerrorf(
"\nWarning: Ack reason should be 0 but is %u\n",
2016-12-02 08:56:18 +01:00
LE16(ctxResult->AckReason)
2016-10-14 07:28:23 +02:00
);
}
if (!status)
{
if (i == CtxNDR64)
{
RpcFlags.HasNDR64 = TRUE;
if (verbose) printf("... NDR64 ");
2015-11-29 10:30:52 +01:00
}
2016-10-14 07:28:23 +02:00
if (!i)
{
RpcFlags.HasNDR32 = TRUE;
if (verbose) printf("... NDR32 ");
}
2015-11-29 10:30:52 +01:00
}
}
free(bindResponseBytePtr);
if (!RpcFlags.HasNDR64 && !RpcFlags.HasNDR32)
{
2016-08-15 12:35:59 +02:00
printerrorf("\nFatal: Could neither negotiate NDR32 nor NDR64 with the RPC server\n");
status = RPC_S_NO_PROTSEQS;
2015-11-29 10:30:52 +01:00
}
return status;
}
2016-10-24 15:32:24 +02:00
RpcStatus rpcBindClient(const RpcCtx sock, const int_fast8_t verbose, PRpcDiag_t rpcDiag)
2015-11-29 10:30:52 +01:00
{
firstPacketSent = FALSE;
RpcFlags.mask = 0;
RpcStatus status =
rpcBindOrAlterClientContext(sock, RPC_PT_BIND_REQ, verbose);
2016-10-24 15:32:24 +02:00
if (status) goto end;
2015-11-29 10:30:52 +01:00
if (!RpcFlags.HasNDR32)
status = rpcBindOrAlterClientContext(sock, RPC_PT_ALTERCONTEXT_REQ, verbose);
2016-10-24 15:32:24 +02:00
end:
rpcDiag->HasRpcDiag = TRUE;
rpcDiag->HasNDR64 = !!RpcFlags.HasNDR64;
rpcDiag->HasBTFN = !!RpcFlags.HasBTFN;
2015-11-29 10:30:52 +01:00
return status;
}
#endif // USE_MSRPC