rpc.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. #ifndef _DEFAULT_SOURCE
  2. #define _DEFAULT_SOURCE
  3. #endif // _DEFAULT_SOURCE
  4. #ifndef CONFIG
  5. #define CONFIG "config.h"
  6. #endif // CONFIG
  7. #include CONFIG
  8. #ifndef USE_MSRPC
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdint.h>
  13. #include <ctype.h>
  14. #include <time.h>
  15. #if !defined(_WIN32)
  16. #include <sys/socket.h>
  17. #include <netdb.h>
  18. #endif
  19. #include "rpc.h"
  20. #include "output.h"
  21. #include "crypto.h"
  22. #include "endian.h"
  23. #include "helpers.h"
  24. #include "network.h"
  25. #include "shared_globals.h"
  26. /* Forwards */
  27. static int checkRpcHeader(const RPC_HEADER *const Header, const BYTE desiredPacketType, const PRINTFUNC p);
  28. /* Data definitions */
  29. // All GUIDs are defined as BYTE[16] here. No big-endian/little-endian byteswapping required.
  30. static const BYTE TransferSyntaxNDR32[] = {
  31. 0x04, 0x5D, 0x88, 0x8A, 0xEB, 0x1C, 0xC9, 0x11, 0x9F, 0xE8, 0x08, 0x00, 0x2B, 0x10, 0x48, 0x60
  32. };
  33. static const BYTE InterfaceUuid[] = {
  34. 0x75, 0x21, 0xc8, 0x51, 0x4e, 0x84, 0x50, 0x47, 0xB0, 0xD8, 0xEC, 0x25, 0x55, 0x55, 0xBC, 0x06
  35. };
  36. static const BYTE TransferSyntaxNDR64[] = {
  37. 0x33, 0x05, 0x71, 0x71, 0xba, 0xbe, 0x37, 0x49, 0x83, 0x19, 0xb5, 0xdb, 0xef, 0x9c, 0xcc, 0x36
  38. };
  39. static const BYTE BindTimeFeatureNegotiation[] = {
  40. 0x2c, 0x1c, 0xb7, 0x6c, 0x12, 0x98, 0x40, 0x45, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  41. };
  42. //
  43. // Dispatch RPC payload to kms.c
  44. //
  45. typedef int (*CreateResponse_t)(const void *const, void *const, const char* const);
  46. static const struct {
  47. unsigned int RequestSize;
  48. CreateResponse_t CreateResponse;
  49. } _Versions[] = {
  50. { sizeof(REQUEST_V4), (CreateResponse_t) CreateResponseV4 },
  51. { sizeof(REQUEST_V6), (CreateResponse_t) CreateResponseV6 },
  52. { sizeof(REQUEST_V6), (CreateResponse_t) CreateResponseV6 }
  53. };
  54. RPC_FLAGS RpcFlags;
  55. static int_fast8_t firstPacketSent;
  56. //
  57. // RPC request (server)
  58. //
  59. #if defined(_PEDANTIC) && !defined(NO_LOG)
  60. static void CheckRpcRequest(const RPC_REQUEST64 *const Request, const unsigned int len, WORD* NdrCtx, WORD* Ndr64Ctx, WORD Ctx)
  61. {
  62. uint_fast8_t kmsMajorVersion;
  63. uint32_t requestSize = Ctx != *Ndr64Ctx ? sizeof(RPC_REQUEST) : sizeof(RPC_REQUEST64);
  64. if (len < requestSize)
  65. {
  66. logger("Fatal: RPC request (including header) must be at least %i bytes but is only %i bytes.\n",
  67. (int)(sizeof(RPC_HEADER) + requestSize),
  68. (int)(len + sizeof(RPC_HEADER))
  69. );
  70. return;
  71. }
  72. if (len < requestSize + sizeof(DWORD))
  73. {
  74. logger("Fatal: KMS Request too small to contain version info (less than 4 bytes).\n");
  75. return;
  76. }
  77. if (Ctx != *Ndr64Ctx)
  78. kmsMajorVersion = LE16(((WORD*)Request->Ndr.Data)[1]);
  79. else
  80. kmsMajorVersion = LE16(((WORD*)Request->Ndr64.Data)[1]);
  81. if (kmsMajorVersion > 6)
  82. {
  83. logger("Fatal: KMSv%u is not supported.\n", (unsigned int)kmsMajorVersion);
  84. }
  85. else
  86. {
  87. if (len >_Versions[kmsMajorVersion].RequestSize + requestSize)
  88. logger("Warning: %u excess bytes in RPC request.\n",
  89. len - _Versions[kmsMajorVersion].RequestSize
  90. );
  91. }
  92. if (Ctx != *Ndr64Ctx && Ctx != *NdrCtx)
  93. logger("Warning: Context id should be %u (NDR32) or %u (NDR64) but is %u.\n",
  94. (unsigned int)*NdrCtx,
  95. (unsigned int)*Ndr64Ctx,
  96. Ctx
  97. );
  98. if (Request->Opnum)
  99. logger("Warning: OpNum should be 0 but is %u.\n",
  100. (unsigned int)LE16(Request->Opnum)
  101. );
  102. if (LE32(Request->AllocHint) != len - sizeof(RPC_REQUEST) + sizeof(Request->Ndr))
  103. logger("Warning: Allocation hint should be %u but is %u.\n",
  104. len + sizeof(Request->Ndr),
  105. LE32(Request->AllocHint)
  106. );
  107. if (Ctx != *Ndr64Ctx)
  108. {
  109. if (LE32(Request->Ndr.DataLength) != len - sizeof(RPC_REQUEST))
  110. logger("Warning: NDR32 data length field should be %u but is %u.\n",
  111. len - sizeof(RPC_REQUEST),
  112. LE32(Request->Ndr.DataLength)
  113. );
  114. if (LE32(Request->Ndr.DataSizeIs) != len - sizeof(RPC_REQUEST))
  115. logger("Warning: NDR32 data size field should be %u but is %u.\n",
  116. len - sizeof(RPC_REQUEST),
  117. LE32(Request->Ndr.DataSizeIs)
  118. );
  119. }
  120. else
  121. {
  122. if (LE64(Request->Ndr64.DataLength) != len - sizeof(RPC_REQUEST64))
  123. logger("Warning: NDR32 data length field should be %u but is %u.\n",
  124. len - sizeof(RPC_REQUEST) + sizeof(Request->Ndr),
  125. LE64(Request->Ndr64.DataLength)
  126. );
  127. if (LE64(Request->Ndr64.DataSizeIs) != len - sizeof(RPC_REQUEST64))
  128. logger("Warning: NDR32 data size field should be %u but is %u.\n",
  129. len - sizeof(RPC_REQUEST64),
  130. LE64(Request->Ndr64.DataSizeIs)
  131. );
  132. }
  133. }
  134. #endif // defined(_PEDANTIC) && !defined(NO_LOG)
  135. /*
  136. * check RPC request for (somewhat) correct size
  137. * allow any size that does not cause CreateResponse to fail badly
  138. */
  139. static unsigned int checkRpcRequestSize(const RPC_REQUEST64 *const Request, const unsigned int requestSize, WORD* NdrCtx, WORD* Ndr64Ctx)
  140. {
  141. WORD Ctx = LE16(Request->ContextId);
  142. # if defined(_PEDANTIC) && !defined(NO_LOG)
  143. CheckRpcRequest(Request, requestSize, NdrCtx, Ndr64Ctx, Ctx);
  144. # endif // defined(_PEDANTIC) && !defined(NO_LOG)
  145. // Anything that is smaller than a v4 request is illegal
  146. if (requestSize < sizeof(REQUEST_V4) + (Ctx != *Ndr64Ctx ? sizeof(RPC_REQUEST) : sizeof(RPC_REQUEST64))) return 0;
  147. // Get KMS major version
  148. uint16_t majorIndex, minor;
  149. DWORD version;
  150. if (Ctx != *Ndr64Ctx)
  151. {
  152. version = LE32(*(DWORD*)Request->Ndr.Data);
  153. }
  154. else
  155. {
  156. version = LE32(*(DWORD*)Request->Ndr64.Data);
  157. }
  158. majorIndex = (uint16_t)(version >> 16) - 4;
  159. minor = (uint16_t)(version & 0xffff);
  160. // Only KMS v4, v5 and v6 are supported
  161. if (majorIndex >= vlmcsd_countof(_Versions) || minor)
  162. {
  163. # ifndef NO_LOG
  164. logger("Fatal: KMSv%hu.%hu unsupported\n", (unsigned short)majorIndex + 4, (unsigned short)minor);
  165. # endif // NO_LOG
  166. return 0;
  167. }
  168. // Could check for equality but allow bigger requests to support buggy RPC clients (e.g. wine)
  169. // Buffer overrun is check by caller.
  170. return (requestSize >= _Versions[majorIndex].RequestSize);
  171. }
  172. /*
  173. * Handles the actual KMS request from the client.
  174. * Calls KMS functions (CreateResponseV4 or CreateResponseV6) in kms.c
  175. * Returns size of the KMS response packet or 0 on failure.
  176. *
  177. * The RPC packet size (excluding header) is actually in Response->AllocHint
  178. */
  179. 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)
  180. {
  181. int ResponseSize; // <0 = Errorcode (HRESULT)
  182. WORD Ctx = LE16(Request->ContextId);
  183. BYTE* requestData;
  184. BYTE* responseData;
  185. BYTE* pRpcReturnCode;
  186. int len;
  187. if (Ctx != *Ndr64Ctx)
  188. {
  189. requestData = (BYTE*)&Request->Ndr.Data;
  190. responseData = (BYTE*)&Response->Ndr.Data;
  191. }
  192. else
  193. {
  194. requestData = (BYTE*)&Request->Ndr64.Data;
  195. responseData = (BYTE*)&Response->Ndr64.Data;
  196. }
  197. ResponseSize = 0x8007000D; // Invalid Data
  198. if (isValid)
  199. {
  200. uint16_t majorIndex = LE16(((WORD*)requestData)[1]) - 4;
  201. if (!(ResponseSize = _Versions[majorIndex].CreateResponse(requestData, responseData, ipstr))) ResponseSize = 0x8007000D;
  202. }
  203. if (Ctx != *Ndr64Ctx)
  204. {
  205. if (ResponseSize < 0)
  206. {
  207. Response->Ndr.DataSizeMax = Response->Ndr.DataLength = 0;
  208. len = sizeof(Response->Ndr) - sizeof(Response->Ndr.DataSizeIs);
  209. }
  210. else
  211. {
  212. Response->Ndr.DataSizeMax = LE32(0x00020000);
  213. Response->Ndr.DataLength = Response->Ndr.DataSizeIs = LE32(ResponseSize);
  214. len = ResponseSize + sizeof(Response->Ndr);
  215. }
  216. }
  217. else
  218. {
  219. if (ResponseSize < 0)
  220. {
  221. Response->Ndr64.DataSizeMax = Response->Ndr64.DataLength = 0;
  222. len = sizeof(Response->Ndr64) - sizeof(Response->Ndr64.DataSizeIs);
  223. }
  224. else
  225. {
  226. Response->Ndr64.DataSizeMax = LE64(0x00020000ULL);
  227. Response->Ndr64.DataLength = Response->Ndr64.DataSizeIs = LE64((uint64_t)ResponseSize);
  228. len = ResponseSize + sizeof(Response->Ndr64);
  229. }
  230. }
  231. pRpcReturnCode = ((BYTE*)&Response->Ndr) + len;
  232. UA32(pRpcReturnCode) = ResponseSize < 0 ? LE32(ResponseSize) : 0;
  233. len += sizeof(DWORD);
  234. // Pad zeros to 32-bit align (seems not neccassary but Windows RPC does it this way)
  235. int pad = ((~len & 3) + 1) & 3;
  236. memset(pRpcReturnCode + sizeof(DWORD), 0, pad);
  237. len += pad;
  238. Response->AllocHint = LE32(len);
  239. Response->ContextId = Request->ContextId;
  240. *((WORD*)&Response->CancelCount) = 0; // CancelCount + Pad1
  241. return len + 8;
  242. }
  243. #if defined(_PEDANTIC) && !defined(NO_LOG)
  244. static void CheckRpcBindRequest(const RPC_BIND_REQUEST *const Request, const unsigned int len)
  245. {
  246. uint_fast8_t i, HasTransferSyntaxNDR32 = FALSE;
  247. char guidBuffer1[GUID_STRING_LENGTH + 1], guidBuffer2[GUID_STRING_LENGTH + 1];
  248. uint32_t CapCtxItems = (len - sizeof(*Request) + sizeof(Request->CtxItems)) / sizeof(Request->CtxItems);
  249. DWORD NumCtxItems = LE32(Request->NumCtxItems);
  250. if (NumCtxItems < CapCtxItems) // Can't be too small because already handled by RpcBindSize
  251. logger("Warning: Excess bytes in RPC bind request.\n");
  252. for (i = 0; i < NumCtxItems; i++)
  253. {
  254. if (!IsEqualGUID(&Request->CtxItems[i].InterfaceUUID, InterfaceUuid))
  255. {
  256. uuid2StringLE((GUID*)&Request->CtxItems[i].InterfaceUUID, guidBuffer1);
  257. uuid2StringLE((GUID*)InterfaceUuid, guidBuffer2);
  258. logger("Warning: Interface UUID is %s but should be %s in Ctx item %u.\n", guidBuffer1, guidBuffer2, (unsigned int)i);
  259. }
  260. if (Request->CtxItems[i].NumTransItems != LE16(1))
  261. logger("Fatal: %u NDR32 transfer items detected in Ctx item %u, but only one is supported.\n",
  262. (unsigned int)LE16(Request->CtxItems[i].NumTransItems), (unsigned int)i
  263. );
  264. if (Request->CtxItems[i].InterfaceVerMajor != LE16(1) || Request->CtxItems[i].InterfaceVerMinor != 0)
  265. logger("Warning: NDR32 Interface version is %u.%u but should be 1.0.\n",
  266. (unsigned int)LE16(Request->CtxItems[i].InterfaceVerMajor),
  267. (unsigned int)LE16(Request->CtxItems[i].InterfaceVerMinor)
  268. );
  269. if (Request->CtxItems[i].ContextId != LE16((WORD)i))
  270. logger("Warning: context id of Ctx item %u is %u.\n", (unsigned int)i, (unsigned int)Request->CtxItems[i].ContextId);
  271. if ( IsEqualGUID((GUID*)TransferSyntaxNDR32, &Request->CtxItems[i].TransferSyntax) )
  272. {
  273. HasTransferSyntaxNDR32 = TRUE;
  274. if (Request->CtxItems[i].SyntaxVersion != LE32(2))
  275. logger("NDR32 transfer syntax version is %u but should be 2.\n", LE32(Request->CtxItems[i].SyntaxVersion));
  276. }
  277. else if ( IsEqualGUID((GUID*)TransferSyntaxNDR64, &Request->CtxItems[i].TransferSyntax) )
  278. {
  279. if (Request->CtxItems[i].SyntaxVersion != LE32(1))
  280. logger("NDR64 transfer syntax version is %u but should be 1.\n", LE32(Request->CtxItems[i].SyntaxVersion));
  281. }
  282. else if (!memcmp(BindTimeFeatureNegotiation, (BYTE*)(&Request->CtxItems[i].TransferSyntax), 8))
  283. {
  284. if (Request->CtxItems[i].SyntaxVersion != LE32(1))
  285. logger("BTFN syntax version is %u but should be 1.\n", LE32(Request->CtxItems[i].SyntaxVersion));
  286. }
  287. }
  288. if (!HasTransferSyntaxNDR32)
  289. logger("Warning: RPC bind request has no NDR32 CtxItem.\n");
  290. }
  291. #endif // defined(_PEDANTIC) && !defined(NO_LOG)
  292. /*
  293. * Check, if we receive enough bytes to return a valid RPC bind response
  294. */
  295. static unsigned int checkRpcBindSize(const RPC_BIND_REQUEST *const Request, const unsigned int RequestSize, WORD* NdrCtx, WORD* Ndr64Ctx)
  296. {
  297. if ( RequestSize < sizeof(RPC_BIND_REQUEST) ) return FALSE;
  298. unsigned int _NumCtxItems = LE32(Request->NumCtxItems);
  299. if ( RequestSize < sizeof(RPC_BIND_REQUEST) - sizeof(Request->CtxItems[0]) + _NumCtxItems * sizeof(Request->CtxItems[0]) ) return FALSE;
  300. #if defined(_PEDANTIC) && !defined(NO_LOG)
  301. CheckRpcBindRequest(Request, RequestSize);
  302. #endif // defined(_PEDANTIC) && !defined(NO_LOG)
  303. return TRUE;
  304. }
  305. /*
  306. * Accepts a bind or alter context request from the client and composes the bind response.
  307. * Needs the socket because the tcp port number is part of the response.
  308. * len is not used here.
  309. *
  310. * Returns TRUE on success.
  311. */
  312. 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)
  313. {
  314. unsigned int i, _st = FALSE;
  315. DWORD numCtxItems = LE32(Request->NumCtxItems);
  316. int_fast8_t IsNDR64possible = FALSE;
  317. uint_fast8_t portNumberSize;
  318. socklen_t socklen;
  319. struct sockaddr_storage addr;
  320. // M$ RPC does not do this. Pad bytes contain apparently random data
  321. // memset(Response->SecondaryAddress, 0, sizeof(Response->SecondaryAddress));
  322. socklen = sizeof addr;
  323. if (
  324. packetType == RPC_PT_ALTERCONTEXT_REQ ||
  325. getsockname(sock, (struct sockaddr*)&addr, &socklen) ||
  326. getnameinfo((struct sockaddr*)&addr, socklen, NULL, 0, (char*)Response->SecondaryAddress, sizeof(Response->SecondaryAddress), NI_NUMERICSERV))
  327. {
  328. portNumberSize = Response->SecondaryAddressLength = 0;
  329. }
  330. else
  331. {
  332. portNumberSize = strlen((char*)Response->SecondaryAddress) + 1;
  333. Response->SecondaryAddressLength = LE16(portNumberSize);
  334. }
  335. Response->MaxXmitFrag = Request->MaxXmitFrag;
  336. Response->MaxRecvFrag = Request->MaxRecvFrag;
  337. Response->AssocGroup = LE32(RpcAssocGroup);
  338. // This is really ugly (but efficient) code to support padding after the secondary address field
  339. if (portNumberSize < 3)
  340. {
  341. Response = (RPC_BIND_RESPONSE*)((BYTE*)Response - 4);
  342. }
  343. Response->NumResults = Request->NumCtxItems;
  344. if (UseRpcNDR64)
  345. {
  346. for (i = 0; i < numCtxItems; i++)
  347. {
  348. if ( IsEqualGUID((GUID*)TransferSyntaxNDR32, &Request->CtxItems[i].TransferSyntax) )
  349. {
  350. /*if (packetType == RPC_PT_BIND_REQ)*/
  351. *NdrCtx = LE16(Request->CtxItems[i].ContextId);
  352. }
  353. if ( IsEqualGUID((GUID*)TransferSyntaxNDR64, &Request->CtxItems[i].TransferSyntax) )
  354. {
  355. IsNDR64possible = TRUE;
  356. /*if (packetType == RPC_PT_BIND_REQ)*/
  357. *Ndr64Ctx = LE16(Request->CtxItems[i].ContextId);
  358. }
  359. }
  360. }
  361. for (i = 0; i < numCtxItems; i++)
  362. {
  363. memset(&Response->Results[i].TransferSyntax, 0, sizeof(GUID));
  364. if ( !IsNDR64possible && IsEqualGUID((GUID*)TransferSyntaxNDR32, &Request->CtxItems[i].TransferSyntax) )
  365. {
  366. Response->Results[i].SyntaxVersion = LE32(2);
  367. Response->Results[i].AckResult =
  368. Response->Results[i].AckReason = RPC_BIND_ACCEPT;
  369. memcpy(&Response->Results[i].TransferSyntax, TransferSyntaxNDR32, sizeof(GUID));
  370. _st = TRUE;
  371. }
  372. else if ( IsNDR64possible && IsEqualGUID((GUID*)TransferSyntaxNDR64, &Request->CtxItems[i].TransferSyntax) )
  373. {
  374. Response->Results[i].SyntaxVersion = LE32(1);
  375. Response->Results[i].AckResult =
  376. Response->Results[i].AckReason = RPC_BIND_ACCEPT;
  377. memcpy(&Response->Results[i].TransferSyntax, TransferSyntaxNDR64, sizeof(GUID));
  378. _st = TRUE;
  379. }
  380. else if ( UseRpcBTFN && !memcmp(BindTimeFeatureNegotiation, (BYTE*)(&Request->CtxItems[i].TransferSyntax), 8) )
  381. {
  382. Response->Results[i].SyntaxVersion = 0;
  383. Response->Results[i].AckResult = RPC_BIND_ACK;
  384. // Features requested are actually encoded in the GUID
  385. Response->Results[i].AckReason =
  386. ((WORD*)(&Request->CtxItems[i].TransferSyntax))[4] &
  387. (RPC_BTFN_SEC_CONTEXT_MULTIPLEX | RPC_BTFN_KEEP_ORPHAN);
  388. }
  389. else
  390. {
  391. Response->Results[i].SyntaxVersion = 0;
  392. Response->Results[i].AckResult =
  393. Response->Results[i].AckReason = RPC_BIND_NACK; // Unsupported
  394. }
  395. }
  396. if ( !_st ) return 0;
  397. return sizeof(RPC_BIND_RESPONSE) + numCtxItems * sizeof(((RPC_BIND_RESPONSE *)0)->Results[0]) - (portNumberSize < 3 ? 4 : 0);
  398. }
  399. //
  400. // Main RPC handling routine
  401. //
  402. typedef unsigned int (*GetResponseSize_t)(const void *const request, const unsigned int requestSize, WORD* NdrCtx, WORD* Ndr64Ctx);
  403. 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);
  404. static const struct {
  405. BYTE ResponsePacketType;
  406. GetResponseSize_t CheckRequestSize;
  407. GetResponse_t GetResponse;
  408. }
  409. _Actions[] = {
  410. { RPC_PT_BIND_ACK, (GetResponseSize_t)checkRpcBindSize, (GetResponse_t) rpcBind },
  411. { RPC_PT_RESPONSE, (GetResponseSize_t)checkRpcRequestSize, (GetResponse_t) rpcRequest },
  412. { RPC_PT_ALTERCONTEXT_ACK, (GetResponseSize_t)checkRpcBindSize, (GetResponse_t) rpcBind },
  413. };
  414. /*
  415. * This is the main RPC server loop. Returns after KMS request has been serviced
  416. * or a timeout has occured.
  417. */
  418. void rpcServer(const SOCKET sock, const DWORD RpcAssocGroup, const char* const ipstr)
  419. {
  420. RPC_HEADER rpcRequestHeader;
  421. WORD NdrCtx = INVALID_NDR_CTX, Ndr64Ctx = INVALID_NDR_CTX;
  422. randomNumberInit();
  423. while (_recv(sock, &rpcRequestHeader, sizeof(rpcRequestHeader)))
  424. {
  425. //int_fast8_t _st;
  426. unsigned int request_len, response_len;
  427. uint_fast8_t _a;
  428. #if defined(_PEDANTIC) && !defined(NO_LOG)
  429. checkRpcHeader(&rpcRequestHeader, rpcRequestHeader.PacketType, &logger);
  430. #endif // defined(_PEDANTIC) && !defined(NO_LOG)
  431. switch (rpcRequestHeader.PacketType)
  432. {
  433. case RPC_PT_BIND_REQ: _a = 0; break;
  434. case RPC_PT_REQUEST: _a = 1; break;
  435. case RPC_PT_ALTERCONTEXT_REQ: _a = 2; break;
  436. default: return;
  437. }
  438. request_len = LE16(rpcRequestHeader.FragLength) - sizeof(rpcRequestHeader);
  439. BYTE requestBuffer[MAX_REQUEST_SIZE + sizeof(RPC_RESPONSE64)];
  440. BYTE responseBuffer[MAX_RESPONSE_SIZE + sizeof(RPC_HEADER) + sizeof(RPC_RESPONSE64)];
  441. RPC_HEADER *rpcResponseHeader = (RPC_HEADER *)responseBuffer;
  442. RPC_RESPONSE* rpcResponse = (RPC_RESPONSE*)(responseBuffer + sizeof(rpcRequestHeader));
  443. // The request is larger than the buffer size
  444. if (request_len > MAX_REQUEST_SIZE + sizeof(RPC_REQUEST64)) return;
  445. // Unable to receive the complete request
  446. if (!_recv(sock, requestBuffer, request_len)) return;
  447. // Request is invalid
  448. BYTE isValid = _Actions[_a].CheckRequestSize(requestBuffer, request_len, &NdrCtx, &Ndr64Ctx);
  449. if (rpcRequestHeader.PacketType != RPC_PT_REQUEST && !isValid) return;
  450. // Unable to create a valid response from request
  451. if (!(response_len = _Actions[_a].GetResponse(requestBuffer, rpcResponse, RpcAssocGroup, sock, &NdrCtx, &Ndr64Ctx, rpcRequestHeader.PacketType != RPC_PT_REQUEST ? rpcRequestHeader.PacketType : isValid, ipstr))) return;
  452. response_len += sizeof(RPC_HEADER);
  453. memcpy(rpcResponseHeader, &rpcRequestHeader, sizeof(RPC_HEADER));
  454. rpcResponseHeader->FragLength = LE16(response_len);
  455. rpcResponseHeader->PacketType = _Actions[_a].ResponsePacketType;
  456. if (rpcResponseHeader->PacketType == RPC_PT_ALTERCONTEXT_ACK)
  457. rpcResponseHeader->PacketFlags = RPC_PF_FIRST | RPC_PF_LAST;
  458. if (!_send(sock, responseBuffer, response_len)) return;
  459. if (DisconnectImmediately && rpcResponseHeader->PacketType == RPC_PT_RESPONSE)
  460. shutdown(sock, VLMCSD_SHUT_RDWR);
  461. }
  462. }
  463. /* RPC client functions */
  464. static DWORD CallId = 2; // M$ starts with CallId 2. So we do the same.
  465. /*
  466. * Checks RPC header. Returns 0 on success.
  467. * This is mainly for debugging a non Microsoft KMS server that uses its own RPC code.
  468. */
  469. static int checkRpcHeader(const RPC_HEADER *const Header, const BYTE desiredPacketType, const PRINTFUNC p)
  470. {
  471. int status = 0;
  472. if (Header->PacketType != desiredPacketType)
  473. {
  474. p("Fatal: Received wrong RPC packet type. Expected %u but got %u\n",
  475. (uint32_t)desiredPacketType,
  476. Header->PacketType
  477. );
  478. status = RPC_S_PROTOCOL_ERROR;
  479. }
  480. if (Header->DataRepresentation != BE32(0x10000000))
  481. {
  482. p("Fatal: RPC response does not conform to Microsoft's limited support of DCE RPC\n");
  483. status = RPC_S_PROTOCOL_ERROR;
  484. }
  485. if (Header->AuthLength != 0)
  486. {
  487. p("Fatal: RPC response requests authentication\n");
  488. status = RPC_S_UNKNOWN_AUTHN_TYPE;
  489. }
  490. // vlmcsd does not support fragmented packets (not yet neccassary)
  491. if ( (Header->PacketFlags & (RPC_PF_FIRST | RPC_PF_LAST)) != (RPC_PF_FIRST | RPC_PF_LAST) )
  492. {
  493. p("Fatal: RPC packet flags RPC_PF_FIRST and RPC_PF_LAST are not both set.\n");
  494. status = RPC_S_CANNOT_SUPPORT;
  495. }
  496. if (Header->PacketFlags & RPC_PF_CANCEL_PENDING) p("Warning: %s should not be set\n", "RPC_PF_CANCEL_PENDING");
  497. if (Header->PacketFlags & RPC_PF_RESERVED) p("Warning: %s should not be set\n", "RPC_PF_RESERVED");
  498. if (Header->PacketFlags & RPC_PF_NOT_EXEC) p("Warning: %s should not be set\n", "RPC_PF_NOT_EXEC");
  499. if (Header->PacketFlags & RPC_PF_MAYBE) p("Warning: %s should not be set\n", "RPC_PF_MAYBE");
  500. if (Header->PacketFlags & RPC_PF_OBJECT) p("Warning: %s should not be set\n", "RPC_PF_OBJECT");
  501. if (Header->VersionMajor != 5 || Header->VersionMinor != 0)
  502. {
  503. p("Fatal: Expected RPC version 5.0 and got %u.%u\n", Header->VersionMajor, Header->VersionMinor);
  504. status = RPC_S_INVALID_VERS_OPTION;
  505. }
  506. return status;
  507. }
  508. /*
  509. * Checks an RPC response header. Does basic header checks by calling checkRpcHeader()
  510. * and then does additional checks if response header complies with the respective request header.
  511. * PRINTFUNC p can be anything that has the same prototype as printf.
  512. * Returns 0 on success.
  513. */
  514. static int checkRpcResponseHeader(const RPC_HEADER *const ResponseHeader, const RPC_HEADER *const RequestHeader, const BYTE desiredPacketType, const PRINTFUNC p)
  515. {
  516. static int_fast8_t WineBugDetected = FALSE;
  517. int status = checkRpcHeader(ResponseHeader, desiredPacketType, p);
  518. if (desiredPacketType == RPC_PT_BIND_ACK)
  519. {
  520. if ((ResponseHeader->PacketFlags & RPC_PF_MULTIPLEX) != (RequestHeader->PacketFlags & RPC_PF_MULTIPLEX))
  521. {
  522. p("Warning: RPC_PF_MULTIPLEX of RPC request and response should match\n");
  523. }
  524. }
  525. else
  526. {
  527. if (ResponseHeader->PacketFlags & RPC_PF_MULTIPLEX)
  528. {
  529. p("Warning: %s should not be set\n", "RPC_PF_MULTIPLEX");
  530. }
  531. }
  532. if (!status && ResponseHeader->CallId == LE32(1))
  533. {
  534. if (!WineBugDetected)
  535. {
  536. p("Warning: Buggy RPC of Wine detected. Call Id of Response is always 1\n");
  537. WineBugDetected = TRUE;
  538. }
  539. }
  540. else if (ResponseHeader->CallId != RequestHeader->CallId)
  541. {
  542. p("Fatal: Sent Call Id %u but received answer for Call Id %u\n",
  543. (uint32_t)LE32(RequestHeader->CallId),
  544. (uint32_t)LE32(ResponseHeader->CallId)
  545. );
  546. status = RPC_S_PROTOCOL_ERROR;
  547. }
  548. return status;
  549. }
  550. /*
  551. * Initializes an RPC request header as needed for KMS, i.e. packet always fits in one fragment.
  552. * size cannot be greater than fragment length negotiated during RPC bind.
  553. */
  554. static void createRpcRequestHeader(RPC_HEADER* RequestHeader, BYTE packetType, WORD size)
  555. {
  556. RequestHeader->PacketType = packetType;
  557. RequestHeader->PacketFlags = RPC_PF_FIRST | RPC_PF_LAST;
  558. RequestHeader->VersionMajor = 5;
  559. RequestHeader->VersionMinor = 0;
  560. RequestHeader->AuthLength = 0;
  561. RequestHeader->DataRepresentation = BE32(0x10000000); // Little endian, ASCII charset, IEEE floating point
  562. RequestHeader->CallId = LE32(CallId);
  563. RequestHeader->FragLength = LE16(size);
  564. }
  565. /*
  566. * Sends a KMS request via RPC and receives a response.
  567. * Parameters are raw (encrypted) reqeuests / responses.
  568. * Returns 0 on success.
  569. */
  570. RpcStatus rpcSendRequest(const RpcCtx sock, const BYTE *const KmsRequest, const size_t requestSize, BYTE **KmsResponse, size_t *const responseSize)
  571. {
  572. #define MAX_EXCESS_BYTES 16
  573. RPC_HEADER *RequestHeader, ResponseHeader;
  574. RPC_REQUEST64 *RpcRequest;
  575. RPC_RESPONSE64 _Response;
  576. int status = 0;
  577. int_fast8_t useNdr64 = UseRpcNDR64 && firstPacketSent;
  578. size_t size = sizeof(RPC_HEADER) + (useNdr64 ? sizeof(RPC_REQUEST64) : sizeof(RPC_REQUEST)) + requestSize;
  579. size_t responseSize2;
  580. *KmsResponse = NULL;
  581. BYTE *_Request = (BYTE*)vlmcsd_malloc(size);
  582. RequestHeader = (RPC_HEADER*)_Request;
  583. RpcRequest = (RPC_REQUEST64*)(_Request + sizeof(RPC_HEADER));
  584. createRpcRequestHeader(RequestHeader, RPC_PT_REQUEST, size);
  585. // Increment CallId for next Request
  586. CallId++;
  587. RpcRequest->Opnum = 0;
  588. if (useNdr64)
  589. {
  590. RpcRequest->ContextId = LE16(1); // We negotiate NDR64 always as context 1
  591. RpcRequest->AllocHint = LE32(requestSize + sizeof(RpcRequest->Ndr64));
  592. RpcRequest->Ndr64.DataLength = LE64((uint64_t)requestSize);
  593. RpcRequest->Ndr64.DataSizeIs = LE64((uint64_t)requestSize);
  594. memcpy(RpcRequest->Ndr64.Data, KmsRequest, requestSize);
  595. }
  596. else
  597. {
  598. RpcRequest->ContextId = 0; // We negotiate NDR32 always as context 0
  599. RpcRequest->AllocHint = LE32(requestSize + sizeof(RpcRequest->Ndr));
  600. RpcRequest->Ndr.DataLength = LE32(requestSize);
  601. RpcRequest->Ndr.DataSizeIs = LE32(requestSize);
  602. memcpy(RpcRequest->Ndr.Data, KmsRequest, requestSize);
  603. }
  604. for(;;)
  605. {
  606. int bytesread;
  607. if (!_send(sock, _Request, size))
  608. {
  609. printerrorf("\nFatal: Could not send RPC request\n");
  610. status = RPC_S_COMM_FAILURE;
  611. break;
  612. }
  613. if (!_recv(sock, &ResponseHeader, sizeof(RPC_HEADER)))
  614. {
  615. printerrorf("\nFatal: No RPC response received from server\n");
  616. status = RPC_S_COMM_FAILURE;
  617. break;
  618. }
  619. if ((status = checkRpcResponseHeader(&ResponseHeader, RequestHeader, RPC_PT_RESPONSE, &printerrorf))) break;
  620. size = useNdr64 ? sizeof(RPC_RESPONSE64) : sizeof(RPC_RESPONSE);
  621. if (size > LE16(ResponseHeader.FragLength) - sizeof(ResponseHeader))
  622. size = LE16(ResponseHeader.FragLength) - sizeof(ResponseHeader);
  623. if (!_recv(sock, &_Response, size))
  624. {
  625. printerrorf("\nFatal: RPC response is incomplete\n");
  626. status = RPC_S_COMM_FAILURE;
  627. break;
  628. }
  629. if (_Response.CancelCount != 0)
  630. {
  631. printerrorf("\nFatal: RPC response cancel count is not 0\n");
  632. status = RPC_S_CALL_CANCELLED;
  633. }
  634. if (_Response.ContextId != (useNdr64 ? LE16(1) : 0))
  635. {
  636. printerrorf("\nFatal: RPC response context id %u is not bound\n", (unsigned int)LE16(_Response.ContextId));
  637. status = RPC_X_SS_CONTEXT_DAMAGED;
  638. }
  639. int_fast8_t sizesMatch;
  640. if (useNdr64)
  641. {
  642. *responseSize = (size_t)LE64(_Response.Ndr64.DataLength);
  643. responseSize2 = (size_t)LE64(_Response.Ndr64.DataSizeIs);
  644. if (/*!*responseSize ||*/ !_Response.Ndr64.DataSizeMax)
  645. {
  646. status = (int)LE32(_Response.Ndr64.status);
  647. break;
  648. }
  649. sizesMatch = (size_t)LE64(_Response.Ndr64.DataLength) == responseSize2;
  650. }
  651. else
  652. {
  653. *responseSize = (size_t)LE32(_Response.Ndr.DataLength);
  654. responseSize2 = (size_t)LE32(_Response.Ndr.DataSizeIs);
  655. if (/*!*responseSize ||*/ !_Response.Ndr.DataSizeMax)
  656. {
  657. status = (int)LE32(_Response.Ndr.status);
  658. break;
  659. }
  660. sizesMatch = (size_t)LE32(_Response.Ndr.DataLength) == responseSize2;
  661. }
  662. if (!sizesMatch)
  663. {
  664. printerrorf("\nFatal: NDR data length (%u) does not match NDR data size (%u)\n",
  665. (uint32_t)*responseSize,
  666. (uint32_t)LE32(_Response.Ndr.DataSizeIs)
  667. );
  668. status = RPC_S_PROTOCOL_ERROR;
  669. }
  670. *KmsResponse = (BYTE*)vlmcsd_malloc(*responseSize + MAX_EXCESS_BYTES);
  671. // If RPC stub is too short, assume missing bytes are zero (same ill behavior as MS RPC)
  672. memset(*KmsResponse, 0, *responseSize + MAX_EXCESS_BYTES);
  673. // Read up to 16 bytes more than bytes expected to detect faulty KMS emulators
  674. if ((bytesread = recv(sock, (char*)*KmsResponse, *responseSize + MAX_EXCESS_BYTES, 0)) < (int)*responseSize)
  675. {
  676. printerrorf("\nFatal: No or incomplete KMS response received. Required %u bytes but only got %i\n",
  677. (uint32_t)*responseSize,
  678. (int32_t)(bytesread < 0 ? 0 : bytesread)
  679. );
  680. status = RPC_S_PROTOCOL_ERROR;
  681. break;
  682. }
  683. DWORD *pReturnCode;
  684. size_t len = *responseSize + (useNdr64 ? sizeof(_Response.Ndr64) : sizeof(_Response.Ndr)) + sizeof(*pReturnCode);
  685. size_t pad = ((~len & 3) + 1) & 3;
  686. if (len + pad != LE32(_Response.AllocHint))
  687. {
  688. printerrorf("\nWarning: RPC stub size is %u, should be %u (probably incorrect padding)\n", (uint32_t)LE32(_Response.AllocHint), (uint32_t)(len + pad));
  689. }
  690. else
  691. {
  692. size_t i;
  693. for (i = 0; i < pad; i++)
  694. {
  695. if (*(*KmsResponse + *responseSize + sizeof(*pReturnCode) + i))
  696. {
  697. printerrorf("\nWarning: RPC stub data not padded to zeros according to Microsoft standard\n");
  698. break;
  699. }
  700. }
  701. }
  702. pReturnCode = (DWORD*)(*KmsResponse + *responseSize + pad);
  703. status = LE32(UA32(pReturnCode));
  704. break;
  705. }
  706. free(_Request);
  707. firstPacketSent = TRUE;
  708. return status;
  709. #undef MAX_EXCESS_BYTES
  710. }
  711. static int_fast8_t IsNullGuid(BYTE* guidPtr)
  712. {
  713. int_fast8_t i;
  714. for (i = 0; i < 16; i++)
  715. {
  716. if (guidPtr[i]) return FALSE;
  717. }
  718. return TRUE;
  719. }
  720. /*
  721. * Perform RPC client bind. Accepts a connected client socket.
  722. * Returns 0 on success. RPC binding is required before any payload can be
  723. * exchanged. It negotiates about protocol details.
  724. */
  725. RpcStatus rpcBindOrAlterClientContext(const RpcCtx sock, BYTE packetType, const int_fast8_t verbose)
  726. {
  727. RPC_HEADER *RequestHeader, ResponseHeader;
  728. RPC_BIND_REQUEST *bindRequest;
  729. RPC_BIND_RESPONSE *bindResponse;
  730. int status;
  731. WORD ctxItems = 1 + (packetType == RPC_PT_BIND_REQ ? UseRpcNDR64 + UseRpcBTFN : 0);
  732. size_t rpcBindSize = (sizeof(RPC_HEADER) + sizeof(RPC_BIND_REQUEST) + (ctxItems - 1) * sizeof(bindRequest->CtxItems[0]));
  733. WORD ctxIndex = 0;
  734. WORD i;
  735. WORD CtxBTFN = (WORD)~0, CtxNDR64 = (WORD)~0;
  736. BYTE _Request[rpcBindSize];
  737. RequestHeader = (RPC_HEADER*)_Request;
  738. bindRequest = (RPC_BIND_REQUEST* )(_Request + sizeof(RPC_HEADER));
  739. createRpcRequestHeader(RequestHeader, packetType, rpcBindSize);
  740. RequestHeader->PacketFlags |= UseMultiplexedRpc ? RPC_PF_MULTIPLEX : 0;
  741. bindRequest->AssocGroup = 0;
  742. bindRequest->MaxRecvFrag = bindRequest->MaxXmitFrag = LE16(5840);
  743. bindRequest->NumCtxItems = LE32(ctxItems);
  744. // data that is identical in all Ctx items
  745. for (i = 0; i < ctxItems; i++)
  746. {
  747. bindRequest->CtxItems[i].ContextId = LE16(i);
  748. bindRequest->CtxItems[i].InterfaceVerMajor = LE16(1);
  749. bindRequest->CtxItems[i].InterfaceVerMinor = 0;
  750. bindRequest->CtxItems[i].NumTransItems = LE16(1);
  751. bindRequest->CtxItems[i].SyntaxVersion = i ? LE32(1) : LE32(2);
  752. memcpy(&bindRequest->CtxItems[i].InterfaceUUID, InterfaceUuid, sizeof(GUID));
  753. }
  754. memcpy(&bindRequest->CtxItems[0].TransferSyntax, TransferSyntaxNDR32, sizeof(GUID));
  755. if (UseRpcNDR64 && packetType == RPC_PT_BIND_REQ)
  756. {
  757. memcpy(&bindRequest->CtxItems[++ctxIndex].TransferSyntax, TransferSyntaxNDR64, sizeof(GUID));
  758. CtxNDR64 = ctxIndex;
  759. }
  760. if (UseRpcBTFN && packetType == RPC_PT_BIND_REQ)
  761. {
  762. memcpy(&bindRequest->CtxItems[++ctxIndex].TransferSyntax, BindTimeFeatureNegotiation, sizeof(GUID));
  763. CtxBTFN = ctxIndex;
  764. }
  765. if (!_send(sock, _Request, rpcBindSize))
  766. {
  767. printerrorf("\nFatal: Sending RPC bind request failed\n");
  768. return RPC_S_COMM_FAILURE;
  769. }
  770. if (!_recv(sock, &ResponseHeader, sizeof(RPC_HEADER)))
  771. {
  772. printerrorf("\nFatal: Did not receive a response from server\n");
  773. return RPC_S_COMM_FAILURE;
  774. }
  775. if ((status = checkRpcResponseHeader
  776. (
  777. &ResponseHeader,
  778. RequestHeader,
  779. packetType == RPC_PT_BIND_REQ ? RPC_PT_BIND_ACK : RPC_PT_ALTERCONTEXT_ACK,
  780. &printerrorf
  781. )))
  782. {
  783. return status;
  784. }
  785. bindResponse = (RPC_BIND_RESPONSE*)vlmcsd_malloc(LE16(ResponseHeader.FragLength) - sizeof(RPC_HEADER));
  786. BYTE* bindResponseBytePtr = (BYTE*)bindResponse;
  787. if (!_recv(sock, bindResponse, LE16(ResponseHeader.FragLength) - sizeof(RPC_HEADER)))
  788. {
  789. printerrorf("\nFatal: Incomplete RPC bind acknowledgement received\n");
  790. free(bindResponseBytePtr);
  791. return RPC_S_COMM_FAILURE;
  792. }
  793. else
  794. {
  795. /*
  796. * checking, whether a bind or alter context response is as expected.
  797. * This check is very strict and checks whether a KMS emulator behaves exactly the same way
  798. * as Microsoft's RPC does.
  799. */
  800. status = 0;
  801. if (bindResponse->SecondaryAddressLength < LE16(3))
  802. bindResponse = (RPC_BIND_RESPONSE*)(bindResponseBytePtr - 4);
  803. if (bindResponse->NumResults != bindRequest->NumCtxItems)
  804. {
  805. printerrorf("\nFatal: Expected %u CTX items but got %u\n",
  806. (uint32_t)LE32(bindRequest->NumCtxItems),
  807. (uint32_t)LE32(bindResponse->NumResults)
  808. );
  809. status = RPC_S_PROTOCOL_ERROR;
  810. }
  811. for (i = 0; i < ctxItems; i++)
  812. {
  813. const char* transferSyntaxName =
  814. i == CtxBTFN ? "BTFN" : i == CtxNDR64 ? "NDR64" : "NDR32";
  815. if (bindResponse->Results[i].AckResult == RPC_BIND_NACK) // transfer syntax was declined
  816. {
  817. if (!IsNullGuid((BYTE*)&bindResponse->Results[i].TransferSyntax))
  818. {
  819. printerrorf(
  820. "\nWarning: Rejected transfer syntax %s did not return NULL Guid\n",
  821. transferSyntaxName
  822. );
  823. }
  824. if (bindResponse->Results[i].SyntaxVersion)
  825. {
  826. printerrorf(
  827. "\nWarning: Rejected transfer syntax %s did not return syntax version 0 but %u\n",
  828. transferSyntaxName,
  829. LE32(bindResponse->Results[i].SyntaxVersion)
  830. );
  831. }
  832. if (bindResponse->Results[i].AckReason == RPC_ABSTRACTSYNTAX_UNSUPPORTED)
  833. {
  834. printerrorf(
  835. "\nWarning: Transfer syntax %s does not support KMS activation\n",
  836. transferSyntaxName
  837. );
  838. }
  839. else if (bindResponse->Results[i].AckReason != RPC_SYNTAX_UNSUPPORTED)
  840. {
  841. printerrorf(
  842. "\nWarning: Rejected transfer syntax %s did not return ack reason RPC_SYNTAX_UNSUPPORTED\n",
  843. transferSyntaxName
  844. );
  845. }
  846. continue;
  847. }
  848. if (i == CtxBTFN) // BTFN
  849. {
  850. if (bindResponse->Results[i].AckResult != RPC_BIND_ACK)
  851. {
  852. printerrorf("\nWarning: BTFN did not respond with RPC_BIND_ACK or RPC_BIND_NACK\n");
  853. }
  854. if (bindResponse->Results[i].AckReason != LE16(3))
  855. {
  856. printerrorf("\nWarning: BTFN did not return expected feature mask 0x3 but 0x%X\n", (unsigned int)LE16(bindResponse->Results[i].AckReason));
  857. }
  858. if (verbose) printf("... BTFN ");
  859. RpcFlags.HasBTFN = TRUE;
  860. continue;
  861. }
  862. // NDR32 or NDR64 Ctx
  863. if (bindResponse->Results[i].AckResult != RPC_BIND_ACCEPT)
  864. {
  865. printerrorf(
  866. "\nFatal: transfer syntax %s returned an invalid status, neither RPC_BIND_ACCEPT nor RPC_BIND_NACK\n",
  867. transferSyntaxName
  868. );
  869. status = RPC_S_PROTOCOL_ERROR;
  870. }
  871. if (!IsEqualGUID(&bindResponse->Results[i].TransferSyntax, &bindRequest->CtxItems[i].TransferSyntax))
  872. {
  873. printerrorf(
  874. "\nFatal: Transfer syntax of RPC bind request and response does not match\n"
  875. );
  876. status = RPC_S_UNSUPPORTED_TRANS_SYN;
  877. }
  878. if (bindResponse->Results[i].SyntaxVersion != bindRequest->CtxItems[i].SyntaxVersion)
  879. {
  880. printerrorf("\nFatal: Expected transfer syntax version %u for %s but got %u\n",
  881. (uint32_t)LE32(bindRequest->CtxItems[0].SyntaxVersion),
  882. transferSyntaxName,
  883. (uint32_t)LE32(bindResponse->Results[0].SyntaxVersion)
  884. );
  885. status = RPC_S_UNSUPPORTED_TRANS_SYN;
  886. }
  887. // The ack reason field is actually undefined here but Microsoft sets this to 0
  888. if (bindResponse->Results[i].AckReason != 0)
  889. {
  890. printerrorf(
  891. "\nWarning: Ack reason should be 0 but is %u\n",
  892. LE16(bindResponse->Results[i].AckReason)
  893. );
  894. }
  895. if (!status)
  896. {
  897. if (i == CtxNDR64)
  898. {
  899. RpcFlags.HasNDR64 = TRUE;
  900. if (verbose) printf("... NDR64 ");
  901. }
  902. if (!i)
  903. {
  904. RpcFlags.HasNDR32 = TRUE;
  905. if (verbose) printf("... NDR32 ");
  906. }
  907. }
  908. }
  909. }
  910. free(bindResponseBytePtr);
  911. if (!RpcFlags.HasNDR64 && !RpcFlags.HasNDR32)
  912. {
  913. printerrorf("\nFatal: Could neither negotiate NDR32 nor NDR64 with the RPC server\n");
  914. status = RPC_S_NO_PROTSEQS;
  915. }
  916. return status;
  917. }
  918. RpcStatus rpcBindClient(const RpcCtx sock, const int_fast8_t verbose)
  919. {
  920. firstPacketSent = FALSE;
  921. RpcFlags.mask = 0;
  922. RpcStatus status =
  923. rpcBindOrAlterClientContext(sock, RPC_PT_BIND_REQ, verbose);
  924. if (status) return status;
  925. if (!RpcFlags.HasNDR32)
  926. status = rpcBindOrAlterClientContext(sock, RPC_PT_ALTERCONTEXT_REQ, verbose);
  927. return status;
  928. }
  929. #endif // USE_MSRPC