Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1783 lines
58 KiB

  1. /*++ BUILD Version: 0009 // Increment this if a change has global effects
  2. Copyright (c) 1987-1993 Microsoft Corporation
  3. Module Name:
  4. srvcall.c
  5. Abstract:
  6. This module implements the routines for handling the creation/manipulation of
  7. server entries in the connection engine database. It also contains the routines
  8. for parsing the negotiate response from the server.
  9. Author:
  10. Balan Sethu Raman (SethuR) 06-Mar-95 Created
  11. --*/
  12. #include "precomp.h"
  13. #pragma hdrstop
  14. #ifdef ALLOC_PRAGMA
  15. #pragma alloc_text(PAGE, SmbCeCreateSrvCall)
  16. #pragma alloc_text(PAGE, MRxSmbCreateSrvCall)
  17. #pragma alloc_text(PAGE, MRxSmbFinalizeSrvCall)
  18. #pragma alloc_text(PAGE, MRxSmbSrvCallWinnerNotify)
  19. #pragma alloc_text(PAGE, MRxSmbInitializeEchoProbeService)
  20. #pragma alloc_text(PAGE, MRxSmbTearDownEchoProbeService)
  21. #pragma alloc_text(PAGE, MRxSmbMaskNegotiateSmb)
  22. #pragma alloc_text(PAGE, BuildNegotiateSmb)
  23. #endif
  24. RXDT_DefineCategory(SRVCALL);
  25. #define Dbg (DEBUG_TRACE_SRVCALL)
  26. extern BOOLEAN MRxSmbSecuritySignaturesEnabled;
  27. extern BOOLEAN MRxSmbSecuritySignaturesRequired;
  28. VOID
  29. SmbCeCreateSrvCall(
  30. PMRX_SRVCALL_CALLBACK_CONTEXT pCallbackContext)
  31. /*++
  32. Routine Description:
  33. This routine patches the RDBSS created srv call instance with the information required
  34. by the mini redirector.
  35. Arguments:
  36. CallBackContext - the call back context in RDBSS for continuation.
  37. Return Value:
  38. RXSTATUS - The return status for the operation
  39. --*/
  40. {
  41. NTSTATUS Status;
  42. PMRX_SRVCALLDOWN_STRUCTURE SrvCalldownStructure;
  43. PMRX_SRV_CALL pSrvCall;
  44. PAGED_CODE();
  45. SrvCalldownStructure =
  46. (PMRX_SRVCALLDOWN_STRUCTURE)(pCallbackContext->SrvCalldownStructure);
  47. pSrvCall = SrvCalldownStructure->SrvCall;
  48. ASSERT( pSrvCall );
  49. ASSERT( NodeType(pSrvCall) == RDBSS_NTC_SRVCALL );
  50. SmbCeInitializeServerEntry(
  51. pSrvCall,
  52. pCallbackContext,
  53. SrvCalldownStructure->RxContext->Create.TreeConnectOpenDeferred);
  54. }
  55. NTSTATUS
  56. MRxSmbCreateSrvCall(
  57. PMRX_SRV_CALL pSrvCall,
  58. PMRX_SRVCALL_CALLBACK_CONTEXT pCallbackContext)
  59. /*++
  60. Routine Description:
  61. This routine patches the RDBSS created srv call instance with the information required
  62. by the mini redirector.
  63. Arguments:
  64. RxContext - Supplies the context of the original create/ioctl
  65. CallBackContext - the call back context in RDBSS for continuation.
  66. Return Value:
  67. RXSTATUS - The return status for the operation
  68. Notes:
  69. Certain transport related interfaces require handle to be passed in. This
  70. implies that the SRV_CALL instances need to be initialized in the context
  71. of a well known process, i.e., the RDBSS process.
  72. In the normal course of event is this request was issued within the context
  73. of the system process we should continue without having to post. However
  74. there are cases in MIPS when stack overflows. In order to avoid such situations
  75. the request is posted in all cases.
  76. --*/
  77. {
  78. NTSTATUS Status;
  79. UNICODE_STRING ServerName;
  80. PMRX_SRVCALL_CALLBACK_CONTEXT SCCBC = pCallbackContext;
  81. PMRX_SRVCALLDOWN_STRUCTURE SrvCalldownStructure = (PMRX_SRVCALLDOWN_STRUCTURE)(pCallbackContext->SrvCalldownStructure);
  82. PAGED_CODE();
  83. ASSERT( pSrvCall );
  84. ASSERT( NodeType(pSrvCall) == RDBSS_NTC_SRVCALL );
  85. // Dispatch the request to a system thread.
  86. Status = RxDispatchToWorkerThread(
  87. MRxSmbDeviceObject,
  88. DelayedWorkQueue,
  89. SmbCeCreateSrvCall,
  90. pCallbackContext);
  91. if (Status == STATUS_SUCCESS) {
  92. // Map the return value since the wrapper expects PENDING.
  93. Status = STATUS_PENDING;
  94. } else {
  95. // There was an error in dispatching the SmbCeCreateSrvCall method to
  96. // a worker thread. Complete the request and return STATUS_PENDING.
  97. SCCBC->Status = Status;
  98. SrvCalldownStructure->CallBack(SCCBC);
  99. Status = STATUS_PENDING;
  100. }
  101. return Status;
  102. }
  103. NTSTATUS
  104. MRxSmbFinalizeSrvCall(
  105. PMRX_SRV_CALL pSrvCall,
  106. BOOLEAN Force)
  107. /*++
  108. Routine Description:
  109. This routine destroys a given server call instance
  110. Arguments:
  111. pSrvCall - the server call instance to be disconnected.
  112. Force - TRUE if a disconnection is to be enforced immediately.
  113. Return Value:
  114. RXSTATUS - The return status for the operation
  115. --*/
  116. {
  117. NTSTATUS Status = STATUS_SUCCESS;
  118. PSMBCEDB_SERVER_ENTRY pServerEntry;
  119. PAGED_CODE();
  120. // if the server entry is not filled in, then there's nothing to do. this occurs
  121. // on a srvcall that we never successfuly hooked up to........
  122. if (pSrvCall->Context == NULL) {
  123. return(Status);
  124. }
  125. pServerEntry = SmbCeGetAssociatedServerEntry(pSrvCall);
  126. if (pServerEntry != NULL) {
  127. InterlockedCompareExchangePointer(
  128. &pServerEntry->pRdbssSrvCall,
  129. NULL,
  130. pSrvCall);
  131. SmbCeDereferenceServerEntry(pServerEntry);
  132. }
  133. pSrvCall->Context = NULL;
  134. return Status;
  135. }
  136. NTSTATUS
  137. MRxSmbSrvCallWinnerNotify(
  138. IN PMRX_SRV_CALL pSrvCall,
  139. IN BOOLEAN ThisMinirdrIsTheWinner,
  140. IN OUT PVOID pSrvCallContext)
  141. /*++
  142. Routine Description:
  143. This routine finalizes the mini rdr context associated with an RDBSS Server call instance
  144. Arguments:
  145. pSrvCall - the Server Call
  146. ThisMinirdrIsTheWinner - TRUE if this mini rdr is the choosen one.
  147. pSrvCallContext - the server call context created by the mini redirector.
  148. Return Value:
  149. RXSTATUS - The return status for the operation
  150. Notes:
  151. The two phase construction protocol for Server calls is required because of parallel
  152. initiation of a number of mini redirectors. The RDBSS finalizes the particular mini
  153. redirector to be used in communicating with a given server based on quality of
  154. service criterion.
  155. --*/
  156. {
  157. NTSTATUS Status = RX_MAP_STATUS(SUCCESS);
  158. PSMBCEDB_SERVER_ENTRY pServerEntry;
  159. PAGED_CODE();
  160. pServerEntry = (PSMBCEDB_SERVER_ENTRY)pSrvCallContext;
  161. if (!ThisMinirdrIsTheWinner) {
  162. //
  163. // Some other mini rdr has been choosen to connect to the server. Destroy
  164. // the data structures created for this mini redirector.
  165. //
  166. SmbCeUpdateServerEntryState(pServerEntry,SMBCEDB_MARKED_FOR_DELETION);
  167. SmbCeDereferenceServerEntry(pServerEntry);
  168. return STATUS_SUCCESS;
  169. }
  170. pSrvCall->Context = pServerEntry;
  171. pSrvCall->Flags |= SRVCALL_FLAG_CASE_INSENSITIVE_NETROOTS |
  172. SRVCALL_FLAG_CASE_INSENSITIVE_FILENAMES;
  173. pSrvCall->MaximumNumberOfCloseDelayedFiles = MRxSmbConfiguration.DormantFileLimit + 1;
  174. return STATUS_SUCCESS;
  175. }
  176. extern GUID CachedServerGuid;
  177. NTSTATUS
  178. MRxSmbCheckForLoopBack(
  179. IN PSMBCEDB_SERVER_ENTRY pServerEntry)
  180. /*++
  181. Routine Description:
  182. This routine checks for loop back case and set the flag accordingly
  183. Arguments:
  184. pServerEntry - the Server Entry
  185. Return Value:
  186. RXSTATUS - The return status for the operation
  187. Notes:
  188. --*/
  189. {
  190. pServerEntry->Server.IsLoopBack = FALSE;
  191. //
  192. // Check for loopback. NT5 servers support extended security, and return a
  193. // GUID in the negotiate response. We also know that the NT5 server stores
  194. // its GUID in the registry. So we compare the registry value on this machine
  195. // to the GUID value returned from the server to which we just connected. If
  196. // they match, then we are looping back to ourselves!
  197. //
  198. // The Server registry entry is now cached to prevent deadlock.
  199. //
  200. if( BooleanFlagOn( pServerEntry->Server.DialectFlags, DF_EXTENDED_SECURITY ) ) {
  201. if( RtlCompareMemory( &CachedServerGuid,
  202. &pServerEntry->Server.NtServer.ServerGuid,
  203. sizeof( GUID ) ) == sizeof( GUID ) ) {
  204. //
  205. // The GUID in the registry (cached) matches the GUID we got back from the
  206. // server! We must be looping back to ourselves!
  207. //
  208. pServerEntry->Server.IsLoopBack = TRUE;
  209. }
  210. }
  211. return STATUS_SUCCESS;
  212. }
  213. //
  214. // The following type defines and data structures are used for parsing negotiate SMB
  215. // responses.
  216. //
  217. #include "protocol.h"
  218. //superceded in smbxchng.h
  219. //#define MRXSMB_PROCESS_ID 0xCAFE
  220. typedef enum _SMB_NEGOTIATE_TYPE_ {
  221. SMB_CORE_NEGOTIATE,
  222. SMB_EXTENDED_NEGOTIATE,
  223. SMB_NT_NEGOTIATE
  224. } SMB_NEGOTIATE_TYPE, *PSMB_NEGOTIATE_TYPE;
  225. typedef struct _SMB_DIALECTS_ {
  226. SMB_NEGOTIATE_TYPE NegotiateType;
  227. USHORT DispatchVectorIndex;
  228. } SMB_DIALECTS, *PSMB_DIALECTS;
  229. SMBCE_SERVER_DISPATCH_VECTOR
  230. s_SmbServerDispatchVectors[] = {
  231. {BuildSessionSetupSmb,CoreBuildTreeConnectSmb},
  232. {BuildSessionSetupSmb,LmBuildTreeConnectSmb},
  233. {BuildSessionSetupSmb,NtBuildTreeConnectSmb},
  234. {BuildSessionSetupSmb,NtBuildTreeConnectSmb}
  235. };
  236. //CODE.IMPROVEMENT since, in fact, we may never implement XENIXCORE and MSNET13
  237. // we should hack them out. however, there is an enum in smbce.h that must be kept
  238. // sync with these arrays.
  239. SMB_DIALECTS
  240. s_SmbDialects[] = {
  241. { SMB_CORE_NEGOTIATE, 0},
  242. //{ SMB_CORE_NEGOTIATE, 0 },
  243. //{ SMB_EXTENDED_NEGOTIATE, 1 },
  244. { SMB_EXTENDED_NEGOTIATE, 1 },
  245. { SMB_EXTENDED_NEGOTIATE, 1 },
  246. { SMB_EXTENDED_NEGOTIATE, 1 },
  247. { SMB_EXTENDED_NEGOTIATE, 1 },
  248. { SMB_NT_NEGOTIATE, 2 },
  249. { SMB_NT_NEGOTIATE, 3}
  250. };
  251. CHAR s_DialectNames[] = {
  252. "\2" PCNET1 "\0"
  253. //\2notyet" XENIXCORE "\0"
  254. //\2notyet" MSNET103 "\0"
  255. "\2" LANMAN10 "\0"
  256. "\2" WFW10 "\0"
  257. "\2" LANMAN12 "\0"
  258. "\2" LANMAN21
  259. "\0\2" NTLANMAN
  260. };
  261. #define __second(a,b) (b)
  262. ULONG
  263. MRxSmbDialectFlags[] = {
  264. __second( PCNET1, DF_CORE ),
  265. //__second( XENIXCORE, DF_CORE | DF_MIXEDCASEPW | DF_MIXEDCASE ),
  266. //__second( MSNET103, DF_CORE | DF_OLDRAWIO | DF_LOCKREAD | DF_EXTENDNEGOT ),
  267. __second( LANMAN10, DF_CORE | DF_NEWRAWIO | DF_LOCKREAD | DF_EXTENDNEGOT |
  268. DF_LANMAN10 ),
  269. __second( WFW10, DF_CORE | DF_NEWRAWIO | DF_LOCKREAD | DF_EXTENDNEGOT |
  270. DF_LANMAN10 | DF_WFW),
  271. __second( LANMAN12, DF_CORE | DF_NEWRAWIO | DF_LOCKREAD | DF_EXTENDNEGOT |
  272. DF_LANMAN10 | DF_LANMAN20 |
  273. DF_MIXEDCASE | DF_LONGNAME | DF_SUPPORTEA ),
  274. __second( LANMAN21, DF_CORE | DF_NEWRAWIO | DF_LOCKREAD | DF_EXTENDNEGOT |
  275. DF_LANMAN10 | DF_LANMAN20 |
  276. DF_MIXEDCASE | DF_LONGNAME | DF_SUPPORTEA |
  277. DF_LANMAN21),
  278. __second( NTLANMAN, DF_CORE | DF_NEWRAWIO |
  279. DF_NTPROTOCOL | DF_NTNEGOTIATE |
  280. DF_MIXEDCASEPW | DF_LANMAN10 | DF_LANMAN20 |
  281. DF_LANMAN21 | DF_MIXEDCASE | DF_LONGNAME |
  282. DF_SUPPORTEA | DF_TIME_IS_UTC )
  283. };
  284. ULONG s_NumberOfDialects = sizeof(s_SmbDialects) / sizeof(s_SmbDialects[0]);
  285. PBYTE s_pNegotiateSmb = NULL;
  286. PBYTE s_pNegotiateSmbRemoteBoot = NULL;
  287. ULONG s_NegotiateSmbLength = 0;
  288. PBYTE s_pEchoSmb = NULL;
  289. BYTE s_EchoData[] = "JlJmIhClBsr";
  290. #define SMB_ECHO_COUNT (1)
  291. // Number of ticks 100ns ticks in a day.
  292. LARGE_INTEGER s_MaxTimeZoneBias;
  293. extern NTSTATUS
  294. GetNTSecurityParameters(
  295. PSMB_ADMIN_EXCHANGE pSmbAdminExchange,
  296. PSMBCE_SERVER pServer,
  297. PUNICODE_STRING pDomainName,
  298. PRESP_NT_NEGOTIATE pNtNegotiateResponse,
  299. ULONG BytesIndicated,
  300. ULONG BytesAvailable,
  301. PULONG pBytesTaken,
  302. PMDL *pDataBufferPointer,
  303. PULONG pDataSize);
  304. extern NTSTATUS
  305. GetLanmanSecurityParameters(
  306. PSMBCE_SERVER pServer,
  307. PRESP_NEGOTIATE pNegotiateResponse);
  308. extern VOID
  309. GetLanmanTimeBias(
  310. PSMBCE_SERVER pServer,
  311. PRESP_NEGOTIATE pNegotiateResponse);
  312. // Number of 100 ns ticks in one minute
  313. #define ONE_MINUTE_IN_TIME (60 * 1000 * 10000)
  314. NTSTATUS
  315. MRxSmbInitializeEchoProbeService(
  316. PMRXSMB_ECHO_PROBE_SERVICE_CONTEXT pEchoProbeContext)
  317. /*++
  318. Routine Description:
  319. This routine builds the echo SMB
  320. Return Value:
  321. STATUS_SUCCESS if construction of an ECHO smb was successful
  322. Other Status codes correspond to error situations.
  323. --*/
  324. {
  325. NTSTATUS Status = STATUS_SUCCESS;
  326. ULONG DialectIndex;
  327. PSMB_HEADER pSmbHeader = NULL;
  328. PREQ_ECHO pReqEcho = NULL;
  329. PAGED_CODE();
  330. pEchoProbeContext->EchoSmbLength = sizeof(SMB_HEADER) +
  331. FIELD_OFFSET(REQ_ECHO,Buffer) +
  332. sizeof(s_EchoData);
  333. pEchoProbeContext->pEchoSmb = (PBYTE)RxAllocatePoolWithTag(
  334. NonPagedPool,
  335. pEchoProbeContext->EchoSmbLength,
  336. MRXSMB_ECHO_POOLTAG);
  337. if (pEchoProbeContext->pEchoSmb != NULL) {
  338. pSmbHeader = (PSMB_HEADER)pEchoProbeContext->pEchoSmb;
  339. pReqEcho = (PREQ_ECHO)((PBYTE)pEchoProbeContext->pEchoSmb + sizeof(SMB_HEADER));
  340. // Fill in the header
  341. RtlZeroMemory( pSmbHeader, sizeof( SMB_HEADER ) );
  342. *(PULONG)(&pSmbHeader->Protocol) = (ULONG)SMB_HEADER_PROTOCOL;
  343. // By default, paths in SMBs are marked as case insensitive and
  344. // canonicalized.
  345. pSmbHeader->Flags =
  346. SMB_FLAGS_CASE_INSENSITIVE | SMB_FLAGS_CANONICALIZED_PATHS;
  347. // Get the flags2 field out of the SmbContext
  348. SmbPutAlignedUshort(
  349. &pSmbHeader->Flags2,
  350. (SMB_FLAGS2_KNOWS_LONG_NAMES |
  351. SMB_FLAGS2_KNOWS_EAS |
  352. SMB_FLAGS2_IS_LONG_NAME |
  353. SMB_FLAGS2_NT_STATUS |
  354. SMB_FLAGS2_UNICODE));
  355. // Fill in the process id.
  356. SmbPutUshort(&pSmbHeader->Pid, MRXSMB_PROCESS_ID );
  357. SmbPutUshort(&pSmbHeader->Tid,0xffff); // Invalid TID
  358. // Lastly, fill in the smb command code.
  359. pSmbHeader->Command = (UCHAR) SMB_COM_ECHO;
  360. pReqEcho->WordCount = 1;
  361. RtlMoveMemory( pReqEcho->Buffer, s_EchoData, sizeof( s_EchoData ) );
  362. SmbPutUshort(&pReqEcho->EchoCount, SMB_ECHO_COUNT);
  363. SmbPutUshort(&pReqEcho->ByteCount, (USHORT) sizeof( s_EchoData ) );
  364. } else {
  365. Status = STATUS_INSUFFICIENT_RESOURCES;
  366. }
  367. return Status;
  368. }
  369. VOID
  370. MRxSmbTearDownEchoProbeService(
  371. PMRXSMB_ECHO_PROBE_SERVICE_CONTEXT pEchoProbeContext)
  372. /*++
  373. Routine Description:
  374. This routine tears down the echo processing context
  375. --*/
  376. {
  377. PAGED_CODE();
  378. if (pEchoProbeContext->pEchoSmb != NULL) {
  379. RxFreePool(pEchoProbeContext->pEchoSmb);
  380. pEchoProbeContext->pEchoSmb = NULL;
  381. }
  382. }
  383. ULONG MRxSmbNegotiateMask = 6; //controls which protocols are not negotiated
  384. //#define MRXSMB_ALLOW_NEGOTIATE_MASKING
  385. #ifdef MRXSMB_ALLOW_NEGOTIATE_MASKING
  386. ULONG MRxSmbCapturedNegotiateMask = 0; //indicates which protocols are currently defeated
  387. CHAR MRxSmbDefeatString[] = "xyz";
  388. VOID
  389. MRxSmbMaskNegotiateSmb ()
  390. /*++
  391. Routine Description:
  392. This routine masks the negotiate buffer to reduce the number of dialects that are
  393. negotiated.
  394. Arguments:
  395. none
  396. Return Value:
  397. NA
  398. --*/
  399. {
  400. PREQ_NEGOTIATE pReqNegotiate;
  401. PUCHAR p,pshadow,q;
  402. ULONG i,numberofdialects,negotiatemask;
  403. PAGED_CODE();
  404. if ((MRxSmbNegotiateMask == MRxSmbCapturedNegotiateMask)
  405. || (s_pNegotiateSmb == NULL) ){
  406. return;
  407. }
  408. pReqNegotiate = (PREQ_NEGOTIATE)(s_pNegotiateSmb + sizeof(SMB_HEADER));
  409. p = pReqNegotiate->Buffer;
  410. pshadow = s_DialectNames;
  411. negotiatemask = MRxSmbNegotiateMask;
  412. numberofdialects = sizeof(MRxSmbDialectFlags)/sizeof(MRxSmbDialectFlags[0]);
  413. DbgPrint("Build Negotiate mask=%x\n",negotiatemask);
  414. for (i=0;;) {
  415. ASSERT(*p == '\2');
  416. ASSERT(*pshadow == '\2');
  417. p++; pshadow++;
  418. DbgPrint("Considering Protocol %s\n",pshadow);
  419. if (negotiatemask&1) {
  420. DbgPrint("Protocol to fffff %s\n",pshadow);
  421. }
  422. //for each protocol, either copy in characters from the defeat string
  423. //or from the original source depending on the mask. here, pshadow points
  424. //to the original, q points to the defeat string and p points to the actual
  425. //bits in the SMB_COM_NEGOTIATE
  426. for (q=MRxSmbDefeatString;;) {
  427. if (*p=='\2') break;
  428. if (*q==0) break;
  429. if (negotiatemask&1) {
  430. *p = *q;
  431. } else {
  432. *p = *pshadow;
  433. }
  434. p++; q++; pshadow++;
  435. }
  436. i++;
  437. if (i>=numberofdialects) break;
  438. negotiatemask>>=1;
  439. //if(negotiate_mask==0)break;
  440. DbgPrint("moving up to i=%d\n",i);
  441. for (;*p!='\2';) {
  442. p++; pshadow++;
  443. }
  444. }
  445. MRxSmbCapturedNegotiateMask = MRxSmbNegotiateMask;
  446. }
  447. #else
  448. #define MRxSmbMaskNegotiateSmb()
  449. #endif
  450. NTSTATUS
  451. BuildNegotiateSmb(
  452. PVOID *pSmbBufferPointer,
  453. PULONG pSmbBufferLength,
  454. BOOLEAN RemoteBootSession)
  455. /*++
  456. Routine Description:
  457. This routine builds the negotiate SMB
  458. Arguments:
  459. pSmbBufferPointer - a placeholder for the smb buffer
  460. pNegotiateSmbLength - the smb buffer size
  461. RemoteBootServer - is this for connection to a remote boot server
  462. Return Value:
  463. STATUS_SUCCESS - implies that pServer is a valid instnace .
  464. Other Status codes correspond to error situations.
  465. --*/
  466. {
  467. NTSTATUS Status = STATUS_SUCCESS;
  468. ULONG DialectIndex;
  469. PSMB_HEADER pSmbHeader = NULL;
  470. PREQ_NEGOTIATE pReqNegotiate = NULL;
  471. PAGED_CODE();
  472. if (s_pNegotiateSmb == NULL) {
  473. s_NegotiateSmbLength = sizeof(SMB_HEADER) +
  474. FIELD_OFFSET(REQ_NEGOTIATE,Buffer) +
  475. sizeof(s_DialectNames);
  476. s_pNegotiateSmb = (PBYTE)RxAllocatePoolWithTag(
  477. PagedPool,
  478. s_NegotiateSmbLength + TRANSPORT_HEADER_SIZE,
  479. MRXSMB_ADMIN_POOLTAG);
  480. if (s_pNegotiateSmb != NULL) {
  481. s_pNegotiateSmb += TRANSPORT_HEADER_SIZE;
  482. pSmbHeader = (PSMB_HEADER)s_pNegotiateSmb;
  483. pReqNegotiate = (PREQ_NEGOTIATE)(s_pNegotiateSmb + sizeof(SMB_HEADER));
  484. // Fill in the header
  485. RtlZeroMemory( pSmbHeader, sizeof( SMB_HEADER ) );
  486. *(PULONG)(&pSmbHeader->Protocol) = (ULONG)SMB_HEADER_PROTOCOL;
  487. // By default, paths in SMBs are marked as case insensitive and
  488. // canonicalized.
  489. pSmbHeader->Flags =
  490. SMB_FLAGS_CASE_INSENSITIVE | SMB_FLAGS_CANONICALIZED_PATHS;
  491. // Put our flags2 field. The Ox10 is a temporary flag for SLM
  492. // corruption detection
  493. SmbPutAlignedUshort(
  494. &pSmbHeader->Flags2,
  495. (SMB_FLAGS2_KNOWS_LONG_NAMES
  496. | SMB_FLAGS2_KNOWS_EAS
  497. | SMB_FLAGS2_IS_LONG_NAME
  498. | SMB_FLAGS2_NT_STATUS
  499. | SMB_FLAGS2_UNICODE
  500. | SMB_FLAGS2_EXTENDED_SECURITY
  501. | 0x10
  502. ));
  503. // Fill in the process id.
  504. SmbPutUshort( &pSmbHeader->Pid, MRXSMB_PROCESS_ID );
  505. // Lastly, fill in the smb command code.
  506. pSmbHeader->Command = (UCHAR) SMB_COM_NEGOTIATE;
  507. pReqNegotiate->WordCount = 0;
  508. RtlMoveMemory(
  509. pReqNegotiate->Buffer,
  510. s_DialectNames,
  511. sizeof( s_DialectNames ) );
  512. SmbPutUshort(
  513. &pReqNegotiate->ByteCount,
  514. (USHORT) sizeof( s_DialectNames ) );
  515. // Initialize the maximum time zone bias used in negotiate response parsing.
  516. s_MaxTimeZoneBias.QuadPart = Int32x32To64(24*60*60,1000*10000);
  517. } else {
  518. Status = STATUS_INSUFFICIENT_RESOURCES;
  519. }
  520. }
  521. if (MRxSmbBootedRemotely && s_pNegotiateSmbRemoteBoot == NULL) {
  522. s_pNegotiateSmbRemoteBoot = (PBYTE)RxAllocatePoolWithTag(
  523. PagedPool,
  524. s_NegotiateSmbLength + TRANSPORT_HEADER_SIZE,
  525. MRXSMB_ADMIN_POOLTAG);
  526. if (s_pNegotiateSmbRemoteBoot != NULL) {
  527. USHORT RemoteBootFlags2;
  528. //
  529. // Now that s_pNegotiateSmb has been filled in, copy it to
  530. // s_pNegotiateSmbRemoteBoot and modify as needed. We don't
  531. // worry about masking s_pNegotiateSmbRemoteBoot.
  532. //
  533. s_pNegotiateSmbRemoteBoot += TRANSPORT_HEADER_SIZE;
  534. RtlCopyMemory(s_pNegotiateSmbRemoteBoot,
  535. s_pNegotiateSmb,
  536. s_NegotiateSmbLength);
  537. pSmbHeader = (PSMB_HEADER)s_pNegotiateSmbRemoteBoot;
  538. //
  539. // Turn off the SMB_FLAGS2_EXTENDED_SECURITY bit.
  540. //
  541. RemoteBootFlags2 = SmbGetAlignedUshort(&pSmbHeader->Flags2);
  542. RemoteBootFlags2 &= ~SMB_FLAGS2_EXTENDED_SECURITY;
  543. SmbPutAlignedUshort(&pSmbHeader->Flags2,
  544. RemoteBootFlags2);
  545. } else {
  546. RxFreePool(s_pNegotiateSmb - TRANSPORT_HEADER_SIZE);
  547. Status = STATUS_INSUFFICIENT_RESOURCES;
  548. }
  549. }
  550. if (NT_SUCCESS(Status)) {
  551. *pSmbBufferLength = s_NegotiateSmbLength;
  552. if (RemoteBootSession) {
  553. *pSmbBufferPointer = s_pNegotiateSmbRemoteBoot;
  554. } else {
  555. *pSmbBufferPointer = s_pNegotiateSmb;
  556. }
  557. }
  558. MRxSmbMaskNegotiateSmb();
  559. return Status;
  560. }
  561. ULONG MRxSmbSrvWriteBufSize = 0xffff; //use the negotiated size
  562. NTSTATUS
  563. ParseNegotiateResponse(
  564. IN OUT PSMB_ADMIN_EXCHANGE pSmbAdminExchange,
  565. IN ULONG BytesIndicated,
  566. IN ULONG BytesAvailable,
  567. OUT PULONG pBytesTaken,
  568. IN PSMB_HEADER pSmbHeader,
  569. OUT PMDL *pDataBufferPointer,
  570. OUT PULONG pDataSize)
  571. /*++
  572. Routine Description:
  573. This routine parses the response from the server
  574. Arguments:
  575. pServer - the server instance
  576. pDomainName - the domain name string to be extracted from the response
  577. pSmbHeader - the response SMB
  578. BytesAvailable - length of the response
  579. pBytesTaken - response consumed
  580. Return Value:
  581. STATUS_SUCCESS - the server call construction has been finalized.
  582. Other Status codes correspond to error situations.
  583. Notes:
  584. The SMB servers can speak a variety of dialects of the SMB protocol. The initial
  585. negotiate response can come in one of three possible flavours. Either we get the
  586. NT negotiate response SMB from a NT server or the extended response from DOS and
  587. OS/2 servers or the CORE response from other servers.
  588. --*/
  589. {
  590. NTSTATUS Status = STATUS_SUCCESS;
  591. PSMBCEDB_SERVER_ENTRY pServerEntry;
  592. PSMBCE_SERVER pServer;
  593. PUNICODE_STRING pDomainName;
  594. USHORT DialectIndex;
  595. PRESP_NEGOTIATE pNegotiateResponse;
  596. ULONG NegotiateSmbLength;
  597. ASSERT( pSmbHeader != NULL );
  598. pServerEntry = SmbCeGetExchangeServerEntry(pSmbAdminExchange);
  599. pServer = &pServerEntry->Server;
  600. pDomainName = &pSmbAdminExchange->Negotiate.DomainName;
  601. pNegotiateResponse = (PRESP_NEGOTIATE) (pSmbHeader + 1);
  602. NegotiateSmbLength = sizeof(SMB_HEADER);
  603. *pBytesTaken = NegotiateSmbLength;
  604. // Assume that the indicated response is sufficient. The only cases in which this
  605. // will not be TRUE is for the EXTENDED NEGOITIATE in which the security blob
  606. // is passed back. In all other cases the TDI imposed minimum of 128 bytes
  607. // subsumes the negotiate response.
  608. *pDataBufferPointer = NULL;
  609. *pDataSize = 0;
  610. DialectIndex = SmbGetUshort( &pNegotiateResponse->DialectIndex );
  611. if (DialectIndex == (USHORT) -1) {
  612. // means server cannot accept any requests from
  613. *pBytesTaken = BytesAvailable;
  614. pServerEntry->ServerStatus = STATUS_REQUEST_NOT_ACCEPTED;
  615. return Status;
  616. }
  617. if (pNegotiateResponse->WordCount < 1 || DialectIndex > s_NumberOfDialects) {
  618. *pBytesTaken = BytesAvailable;
  619. pServerEntry->ServerStatus = STATUS_INVALID_NETWORK_RESPONSE;
  620. return Status;
  621. }
  622. // set the domain name length to zero ( default initialization )
  623. pDomainName->Length = 0;
  624. // Fix up the dialect type and the corresponding dispatch vector.
  625. pServer->Dialect = (SMB_DIALECT)DialectIndex;
  626. pServer->DialectFlags = MRxSmbDialectFlags[DialectIndex];
  627. pServer->pDispatch = &s_SmbServerDispatchVectors[s_SmbDialects[DialectIndex].DispatchVectorIndex];
  628. // Parse the response based upon the type of negotiate response expected.
  629. switch (s_SmbDialects[DialectIndex].NegotiateType) {
  630. case SMB_NT_NEGOTIATE:
  631. {
  632. ULONG NegotiateResponseLength;
  633. LARGE_INTEGER ZeroTime;
  634. LARGE_INTEGER LocalTimeBias;
  635. LARGE_INTEGER ServerTimeBias;
  636. PRESP_NT_NEGOTIATE pNtNegotiateResponse = (PRESP_NT_NEGOTIATE) pNegotiateResponse;
  637. ASSERT(BytesAvailable > sizeof(RESP_NT_NEGOTIATE));
  638. if (pNtNegotiateResponse->WordCount != 17) {
  639. *pBytesTaken = BytesAvailable;
  640. Status = STATUS_INVALID_NETWORK_RESPONSE;
  641. } else {
  642. // parse and map the capabilities.
  643. ULONG NtCapabilities;
  644. NegotiateResponseLength = FIELD_OFFSET(RESP_NT_NEGOTIATE,Buffer) +
  645. SmbGetUshort(&pNtNegotiateResponse->ByteCount);
  646. NegotiateSmbLength += NegotiateResponseLength;
  647. //Start with a clean slate
  648. pServer->Capabilities = 0;
  649. // Initialize server based constants
  650. pServer->MaximumRequests = SmbGetUshort( &pNtNegotiateResponse->MaxMpxCount );
  651. pServer->MaximumVCs = SmbGetUshort( &pNtNegotiateResponse->MaxNumberVcs );
  652. pServer->MaximumBufferSize = SmbGetUlong( &pNtNegotiateResponse->MaxBufferSize );
  653. NtCapabilities = pServer->NtServer.NtCapabilities = SmbGetUlong(&pNtNegotiateResponse->Capabilities);
  654. if (NtCapabilities & CAP_RAW_MODE) {
  655. pServer->Capabilities |= (RAW_READ_CAPABILITY | RAW_WRITE_CAPABILITY);
  656. }
  657. if ((NtCapabilities & CAP_COMPRESSED_DATA) &&
  658. MRxSmbEnableCompression) {
  659. pServer->Capabilities |= COMPRESSED_DATA_CAPABILITY;
  660. }
  661. if (NtCapabilities & CAP_DFS) {
  662. pServer->Capabilities |= CAP_DFS;
  663. }
  664. //copy other nt capabilities into the dialog flags
  665. if (NtCapabilities & CAP_UNICODE) {
  666. pServer->DialectFlags |= DF_UNICODE;
  667. }
  668. if (NtCapabilities & CAP_LARGE_FILES) {
  669. pServer->DialectFlags |= DF_LARGE_FILES;
  670. }
  671. if (NtCapabilities & CAP_NT_SMBS) {
  672. pServer->DialectFlags |= DF_NT_SMBS | DF_NT_FIND;
  673. }
  674. if (NtCapabilities & CAP_NT_FIND) {
  675. pServer->DialectFlags |= DF_NT_FIND;
  676. }
  677. if (NtCapabilities & CAP_RPC_REMOTE_APIS) {
  678. pServer->DialectFlags |= DF_RPC_REMOTE;
  679. }
  680. if (NtCapabilities & CAP_NT_STATUS) {
  681. pServer->DialectFlags |= DF_NT_STATUS;
  682. }
  683. if (NtCapabilities & CAP_LEVEL_II_OPLOCKS) {
  684. pServer->DialectFlags |= DF_OPLOCK_LVL2;
  685. }
  686. if (NtCapabilities & CAP_LOCK_AND_READ) {
  687. pServer->DialectFlags |= DF_LOCKREAD;
  688. }
  689. if (NtCapabilities & CAP_EXTENDED_SECURITY) {
  690. pServer->DialectFlags |= DF_EXTENDED_SECURITY;
  691. }
  692. if (NtCapabilities & CAP_INFOLEVEL_PASSTHRU) {
  693. pServer->DialectFlags |= DF_NT_INFO_PASSTHROUGH;
  694. }
  695. // For non disk files the LARGE_READX capability is not useful.
  696. pServer->MaximumNonDiskFileReadBufferSize =
  697. pServer->MaximumBufferSize -
  698. QuadAlign(
  699. sizeof(SMB_HEADER) +
  700. FIELD_OFFSET(
  701. REQ_NT_READ_ANDX,
  702. Buffer[0]));
  703. if (NtCapabilities & CAP_LARGE_READX) {
  704. if (NtCapabilities & CAP_LARGE_WRITEX) {
  705. pServer->MaximumDiskFileReadBufferSize = 60*1024;
  706. } else {
  707. // The maximum size for reads to servers which support
  708. // large read and x is constrained by the USHORT to record
  709. // lengths in the SMB. Thus the maximum length that can be used
  710. // is (65536 - 1) . This length should accomodate the header as
  711. // well as the rest of the SMB. Actually, tho, we cut back to 60K.
  712. pServer->MaximumDiskFileReadBufferSize = 60*1024;
  713. }
  714. } else {
  715. pServer->MaximumDiskFileReadBufferSize = pServer->MaximumNonDiskFileReadBufferSize;
  716. }
  717. // Specifying a zero local time will give you the time zone bias
  718. ZeroTime.HighPart = ZeroTime.LowPart = 0;
  719. ExLocalTimeToSystemTime( &ZeroTime, &LocalTimeBias );
  720. ServerTimeBias = RtlEnlargedIntegerMultiply(
  721. (LONG)SmbGetUshort(
  722. &pNtNegotiateResponse->ServerTimeZone),
  723. ONE_MINUTE_IN_TIME );
  724. pServer->TimeZoneBias.QuadPart = ServerTimeBias.QuadPart -
  725. LocalTimeBias.QuadPart;
  726. if (!FlagOn(pServer->DialectFlags,DF_NT_SMBS)) {
  727. //sigh...........
  728. pServer->DialectFlags &= ~(DF_MIXEDCASEPW);
  729. pServer->DialectFlags |= DF_W95;
  730. }
  731. Status = GetNTSecurityParameters(
  732. pSmbAdminExchange,
  733. pServer,
  734. pDomainName,
  735. pNtNegotiateResponse,
  736. BytesIndicated,
  737. BytesAvailable,
  738. pBytesTaken,
  739. pDataBufferPointer,
  740. pDataSize);
  741. pServer->MaximumNonDiskFileWriteBufferSize =
  742. min(
  743. MRxSmbSrvWriteBufSize,
  744. pServer->MaximumBufferSize -
  745. QuadAlign(
  746. sizeof(SMB_HEADER) +
  747. FIELD_OFFSET(
  748. REQ_NT_WRITE_ANDX,
  749. Buffer[0])));
  750. if (NtCapabilities & CAP_LARGE_WRITEX &&
  751. !(MRxSmbSecuritySignaturesEnabled && pServer->SecuritySignaturesEnabled)) {
  752. pServer->DialectFlags |= DF_LARGE_WRITEX;
  753. pServer->MaximumDiskFileWriteBufferSize = 0x10000;
  754. } else {
  755. pServer->MaximumDiskFileWriteBufferSize =
  756. pServer->MaximumNonDiskFileWriteBufferSize;
  757. }
  758. }
  759. }
  760. break;
  761. case SMB_EXTENDED_NEGOTIATE :
  762. {
  763. // An SMB_EXTENDED_NEGOTIATE response is never partially indicated. The response
  764. // length is ithin the TDI minimum for indication.
  765. USHORT RawMode;
  766. // DOS or OS2 server
  767. if (pNegotiateResponse->WordCount != 13 &&
  768. pNegotiateResponse->WordCount != 10 && // some downlevel server returns invalid WordCount
  769. pNegotiateResponse->WordCount != 8) {
  770. Status = STATUS_INVALID_NETWORK_RESPONSE;
  771. } else {
  772. NegotiateSmbLength += FIELD_OFFSET(RESP_NEGOTIATE,Buffer) +
  773. SmbGetUshort(&pNegotiateResponse->ByteCount);
  774. ASSERT(
  775. (BytesIndicated >= NegotiateSmbLength) &&
  776. (BytesIndicated == BytesAvailable));
  777. RawMode = SmbGetUshort( &pNegotiateResponse->RawMode );
  778. pServer->Capabilities |= ((RawMode & 0x1) != 0
  779. ? RAW_READ_CAPABILITY : 0);
  780. pServer->Capabilities |= ((RawMode & 0x2) != 0
  781. ? RAW_WRITE_CAPABILITY : 0);
  782. if (pSmbHeader->Flags & SMB_FLAGS_LOCK_AND_READ_OK) {
  783. pServer->DialectFlags |= DF_LOCKREAD;
  784. }
  785. pServer->EncryptPasswords = FALSE;
  786. pServer->MaximumVCs = 1;
  787. pServer->MaximumBufferSize = SmbGetUshort( &pNegotiateResponse->MaxBufferSize );
  788. pServer->MaximumDiskFileReadBufferSize =
  789. pServer->MaximumBufferSize -
  790. QuadAlign(
  791. sizeof(SMB_HEADER) +
  792. FIELD_OFFSET(
  793. RESP_READ_ANDX,
  794. Buffer[0]));
  795. pServer->MaximumNonDiskFileReadBufferSize = pServer->MaximumDiskFileReadBufferSize;
  796. pServer->MaximumDiskFileWriteBufferSize = pServer->MaximumDiskFileReadBufferSize;
  797. pServer->MaximumNonDiskFileWriteBufferSize = pServer->MaximumDiskFileReadBufferSize;
  798. pServer->MaximumRequests = SmbGetUshort(
  799. &pNegotiateResponse->MaxMpxCount );
  800. pServer->MaximumVCs = SmbGetUshort(
  801. &pNegotiateResponse->MaxNumberVcs );
  802. if (pNegotiateResponse->WordCount == 13) {
  803. //CODE.IMPROVEMENT use the DF_bit for this
  804. switch (pServer->Dialect) {
  805. case LANMAN10_DIALECT:
  806. case WFW10_DIALECT:
  807. case LANMAN12_DIALECT:
  808. case LANMAN21_DIALECT:
  809. GetLanmanTimeBias( pServer,pNegotiateResponse );
  810. break;
  811. }
  812. Status = GetLanmanSecurityParameters( pServer,pNegotiateResponse );
  813. }
  814. }
  815. *pBytesTaken = BytesAvailable;
  816. }
  817. break;
  818. case SMB_CORE_NEGOTIATE :
  819. default :
  820. {
  821. // An SMB_CORE_NEGOTIATE response is never partially indicated. The response
  822. // length is ithin the TDI minimum for indication.
  823. pServer->SecurityMode = SECURITY_MODE_SHARE_LEVEL;
  824. pServer->EncryptPasswords = FALSE;
  825. pServer->MaximumBufferSize = 0;
  826. pServer->MaximumRequests = 1;
  827. pServer->MaximumVCs = 1;
  828. pServer->SessionKey = 0;
  829. if (pSmbHeader->Flags & SMB_FLAGS_OPLOCK) {
  830. pServer->DialectFlags |= DF_OPLOCK;
  831. }
  832. *pBytesTaken = BytesAvailable;
  833. ASSERT(BytesIndicated == BytesAvailable);
  834. }
  835. }
  836. if (pServer->MaximumRequests == 0) {
  837. //
  838. // If this is a Lanman 1.0 or better server, this is a invalid negotiate
  839. // response. For others it would have been set to 1.
  840. //
  841. Status = STATUS_INVALID_NETWORK_RESPONSE;
  842. }
  843. if ((Status == STATUS_SUCCESS) ||
  844. (Status == STATUS_MORE_PROCESSING_REQUIRED)) {
  845. // Note that this code relies on the minimum incication size covering
  846. // the negotiate response header.
  847. // Check to make sure that the time zone bias isn't more than +-24
  848. // hours.
  849. //
  850. #ifndef WIN9X
  851. if ((pServer->TimeZoneBias.QuadPart > s_MaxTimeZoneBias.QuadPart) ||
  852. (-pServer->TimeZoneBias.QuadPart > s_MaxTimeZoneBias.QuadPart)) {
  853. #else
  854. if ((pServer->TimeZoneBias.HighPart > s_MaxTimeZoneBias.HighPart) ||
  855. (-pServer->TimeZoneBias.HighPart > s_MaxTimeZoneBias.HighPart)) {
  856. #endif
  857. // Set the bias to 0 - assume local time zone.
  858. pServer->TimeZoneBias.LowPart = pServer->TimeZoneBias.HighPart = 0;
  859. }
  860. // Do not allow negotiated buffersize to exceed the size of a USHORT.
  861. // Remove 4096 bytes to avoid overrun and make it easier to handle
  862. // than 0xffff
  863. pServer->MaximumBufferSize =
  864. (pServer->MaximumBufferSize < 0x00010000) ? pServer->MaximumBufferSize :
  865. 0x00010000 - 4096;
  866. } else {
  867. pServerEntry->ServerStatus = Status;
  868. *pBytesTaken = BytesAvailable;
  869. Status = STATUS_SUCCESS;
  870. }
  871. if ((pServer->DialectFlags & DF_NTNEGOTIATE)!=0) {
  872. InterlockedIncrement(&MRxSmbStatistics.LanmanNtConnects);
  873. } else if ((pServer->DialectFlags & DF_LANMAN21)!=0) {
  874. InterlockedIncrement(&MRxSmbStatistics.Lanman21Connects);
  875. } else if ((pServer->DialectFlags & DF_LANMAN20)!=0) {
  876. InterlockedIncrement(&MRxSmbStatistics.Lanman20Connects);
  877. } else {
  878. InterlockedIncrement(&MRxSmbStatistics.CoreConnects);
  879. }
  880. if (pServer->MaximumRequests > (USHORT)MRxSmbConfiguration.MaximumNumberOfCommands) {
  881. pServer->MaximumRequests = (USHORT)MRxSmbConfiguration.MaximumNumberOfCommands;
  882. }
  883. if (MRxSmbSecuritySignaturesRequired &&
  884. !pServer->SecuritySignaturesEnabled &&
  885. pServerEntry->ServerStatus == STATUS_SUCCESS ) {
  886. // the client refuses to connect to a server that doesn't support security
  887. // signature which is required by the client.
  888. pServerEntry->ServerStatus = STATUS_LOGIN_WKSTA_RESTRICTION;
  889. }
  890. return Status;
  891. }
  892. NTSTATUS
  893. GetNTSecurityParameters(
  894. PSMB_ADMIN_EXCHANGE pSmbAdminExchange,
  895. PSMBCE_SERVER pServer,
  896. PUNICODE_STRING pDomainName,
  897. PRESP_NT_NEGOTIATE pNtNegotiateResponse,
  898. ULONG BytesIndicated,
  899. ULONG BytesAvailable,
  900. PULONG pBytesTaken,
  901. PMDL *pDataBufferPointer,
  902. PULONG pDataSize)
  903. /*++
  904. Routine Description:
  905. This routine extracts the security parameters from an NT server
  906. Arguments:
  907. pServer - the server
  908. pDomainName - the domain name
  909. pNtNegotiateResponse - the response
  910. NegotiateResponseLength - size of the negotiate response
  911. Return Value:
  912. STATUS_SUCCESS - implies that pServer is a valid instnace .
  913. Other Status codes correspond to error situations.
  914. --*/
  915. {
  916. NTSTATUS Status = STATUS_SUCCESS;
  917. USHORT ByteCount;
  918. PUSHORT pByteCountInSmb =
  919. ((PUSHORT)((PUCHAR) pNtNegotiateResponse + 1)) +
  920. pNtNegotiateResponse->WordCount;
  921. PUCHAR pBuffer = (PUCHAR)(pByteCountInSmb + 1);
  922. *pBytesTaken += FIELD_OFFSET(RESP_NT_NEGOTIATE,Buffer);
  923. ByteCount = SmbGetUshort(pByteCountInSmb);
  924. pServer->SecurityMode = (((pNtNegotiateResponse->SecurityMode & NEGOTIATE_USER_SECURITY) != 0)
  925. ? SECURITY_MODE_USER_LEVEL
  926. : SECURITY_MODE_SHARE_LEVEL);
  927. pServer->EncryptPasswords = ((pNtNegotiateResponse->SecurityMode & NEGOTIATE_ENCRYPT_PASSWORDS) != 0);
  928. pServer->EncryptionKeyLength = 0;
  929. pServer->SecuritySignaturesEnabled = ((pNtNegotiateResponse->SecurityMode &
  930. NEGOTIATE_SECURITY_SIGNATURES_ENABLED) != 0);
  931. pServer->SecuritySignaturesRequired = ((pNtNegotiateResponse->SecurityMode &
  932. NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) != 0);
  933. if (BooleanFlagOn(pServer->NtServer.NtCapabilities,CAP_EXTENDED_SECURITY)) {
  934. ULONG SecurityBlobLength;
  935. // The server supports the new security validation scheme. In such cases
  936. // the BLOB to be passed to the local security package is shipped as
  937. // part of the negotiate response.
  938. ASSERT(
  939. (ByteCount >= sizeof(GUID)) &&
  940. ((*pBytesTaken + sizeof(GUID)) <= BytesIndicated));
  941. // Extract the Server GUID
  942. RtlCopyMemory(
  943. &pServer->NtServer.ServerGuid,
  944. pBuffer,
  945. sizeof(GUID));
  946. *pBytesTaken += sizeof(GUID);
  947. if (pServer->NtServer.pSecurityBlob != NULL) {
  948. RxFreePool(pServer->NtServer.pSecurityBlob);
  949. pServer->NtServer.pSecurityBlob = NULL;
  950. pServer->NtServer.SecurityBlobLength = 0;
  951. }
  952. // Allocate the Blob and copy the security Blob from the response
  953. if ((SecurityBlobLength = ByteCount - sizeof(GUID)) > 0) {
  954. pServer->NtServer.pSecurityBlob = RxAllocatePoolWithTag(
  955. NonPagedPool,
  956. SecurityBlobLength,
  957. MRXSMB_ADMIN_POOLTAG);
  958. if (pServer->NtServer.pSecurityBlob != NULL) {
  959. pServer->NtServer.SecurityBlobLength = SecurityBlobLength;
  960. // If the Blob has been completely indicated it can be copied directly.
  961. // On the other hand if it is not completely indicated an MDl needs to
  962. // be setup to allow the underlying TDI layer to complete the copy
  963. // into the allocated buffer. This entails allocating an MDL of the
  964. // appropriate size and setting it up.
  965. if ((*pBytesTaken + SecurityBlobLength) <= BytesIndicated) {
  966. RtlCopyMemory(
  967. pServer->NtServer.pSecurityBlob,
  968. (pBuffer + sizeof(GUID)),
  969. SecurityBlobLength);
  970. *pBytesTaken += SecurityBlobLength;
  971. ASSERT(*pBytesTaken == BytesAvailable);
  972. } else {
  973. if ((*pBytesTaken + SecurityBlobLength) <= BytesAvailable) {
  974. // In this case the blob was not indicated completely.
  975. *pDataBufferPointer = RxAllocateMdl(
  976. pServer->NtServer.pSecurityBlob,
  977. SecurityBlobLength);
  978. if (*pDataBufferPointer == NULL) {
  979. RxFreePool(pServer->NtServer.pSecurityBlob);
  980. pServer->NtServer.pSecurityBlob = NULL;
  981. pServer->NtServer.SecurityBlobLength = 0;
  982. Status = STATUS_INSUFFICIENT_RESOURCES;
  983. } else {
  984. MmBuildMdlForNonPagedPool(*pDataBufferPointer);
  985. *pDataSize = SecurityBlobLength;
  986. Status = STATUS_MORE_PROCESSING_REQUIRED;
  987. }
  988. } else {
  989. *pBytesTaken = BytesAvailable;
  990. Status = STATUS_SUCCESS;
  991. pSmbAdminExchange->Status = STATUS_INVALID_NETWORK_RESPONSE;
  992. }
  993. }
  994. } else {
  995. Status = STATUS_INSUFFICIENT_RESOURCES;
  996. }
  997. }
  998. } else {
  999. *pBytesTaken = BytesAvailable;
  1000. pServer->SessionKey = SmbGetUlong( &pNtNegotiateResponse->SessionKey );
  1001. if (pServer->EncryptPasswords) {
  1002. pServer->EncryptionKeyLength = pNtNegotiateResponse->EncryptionKeyLength;
  1003. if (pServer->EncryptionKeyLength != 0) {
  1004. ASSERT( CRYPT_TXT_LEN == MSV1_0_CHALLENGE_LENGTH );
  1005. if (pServer->EncryptionKeyLength != CRYPT_TXT_LEN) {
  1006. Status = STATUS_INVALID_NETWORK_RESPONSE;
  1007. } else {
  1008. RtlCopyMemory(
  1009. pServer->EncryptionKey,
  1010. pBuffer,
  1011. pServer->EncryptionKeyLength );
  1012. if (ByteCount - pServer->EncryptionKeyLength > 0) {
  1013. ASSERT((pDomainName->Buffer != NULL) &&
  1014. (pDomainName->MaximumLength >= (ByteCount - pServer->EncryptionKeyLength)));
  1015. pBuffer = pBuffer + pServer->EncryptionKeyLength;
  1016. pDomainName->Length = ByteCount - pServer->EncryptionKeyLength;
  1017. if (pDomainName->Length & 1) {
  1018. // The remainder of the length is odd. This implies that the server did
  1019. // some alignment.
  1020. pBuffer++;
  1021. pDomainName->Length -= 1;
  1022. }
  1023. RtlCopyMemory(
  1024. pDomainName->Buffer,
  1025. pBuffer,
  1026. pDomainName->Length);
  1027. }
  1028. }
  1029. }
  1030. }
  1031. }
  1032. return Status;
  1033. }
  1034. NTSTATUS
  1035. GetLanmanSecurityParameters(
  1036. PSMBCE_SERVER pServer,
  1037. PRESP_NEGOTIATE pNegotiateResponse)
  1038. /*++
  1039. Routine Description:
  1040. This routine extracts the security parameters from a LANMAN server
  1041. Arguments:
  1042. pServer - the server
  1043. pNtNegotiateResponse - the response
  1044. Return Value:
  1045. STATUS_SUCCESS - implies that pServer is a valid instnace .
  1046. Other Status codes correspond to error situations.
  1047. --*/
  1048. {
  1049. USHORT i;
  1050. USHORT SecurityMode;
  1051. pServer->SessionKey = SmbGetUlong( &pNegotiateResponse->SessionKey );
  1052. SecurityMode = SmbGetUshort( &pNegotiateResponse->SecurityMode );
  1053. pServer->SecurityMode = (((SecurityMode & 1) != 0)
  1054. ? SECURITY_MODE_USER_LEVEL
  1055. : SECURITY_MODE_SHARE_LEVEL);
  1056. pServer->EncryptPasswords = ((SecurityMode & 2) != 0);
  1057. if (pServer->EncryptPasswords) {
  1058. if (pServer->Dialect == LANMAN21_DIALECT) {
  1059. pServer->EncryptionKeyLength = SmbGetUshort(&pNegotiateResponse->EncryptionKeyLength);
  1060. } else {
  1061. pServer->EncryptionKeyLength = SmbGetUshort(&pNegotiateResponse->ByteCount);
  1062. }
  1063. if (pServer->EncryptionKeyLength != 0) {
  1064. if (pServer->EncryptionKeyLength > CRYPT_TXT_LEN) {
  1065. return( STATUS_INVALID_NETWORK_RESPONSE );
  1066. }
  1067. for (i = 0; i < pServer->EncryptionKeyLength; i++) {
  1068. pServer->EncryptionKey[i] = pNegotiateResponse->Buffer[i];
  1069. }
  1070. }
  1071. }
  1072. return( STATUS_SUCCESS );
  1073. }
  1074. LARGE_INTEGER
  1075. ConvertSmbTimeToTime (
  1076. IN SMB_TIME Time,
  1077. IN SMB_DATE Date
  1078. )
  1079. /*++
  1080. Routine Description:
  1081. This routine converts an SMB time to an NT time structure.
  1082. Arguments:
  1083. IN SMB_TIME Time - Supplies the time of day to convert
  1084. IN SMB_DATE Date - Supplies the day of the year to convert
  1085. IN PSERVERLISTENTRY Server - if supplied, supplies the server for tz bias.
  1086. Return Value:
  1087. LARGE_INTEGER - Time structure describing input time.
  1088. --*/
  1089. {
  1090. TIME_FIELDS TimeFields;
  1091. LARGE_INTEGER OutputTime;
  1092. //
  1093. // This routine cannot be paged because it is called from both the
  1094. // RdrFileDiscardableSection and the RdrVCDiscardableSection.
  1095. //
  1096. if (SmbIsTimeZero(&Date) && SmbIsTimeZero(&Time)) {
  1097. OutputTime.LowPart = OutputTime.HighPart = 0;
  1098. } else {
  1099. TimeFields.Year = Date.Struct.Year + (USHORT )1980;
  1100. TimeFields.Month = Date.Struct.Month;
  1101. TimeFields.Day = Date.Struct.Day;
  1102. TimeFields.Hour = Time.Struct.Hours;
  1103. TimeFields.Minute = Time.Struct.Minutes;
  1104. TimeFields.Second = Time.Struct.TwoSeconds*(USHORT )2;
  1105. TimeFields.Milliseconds = 0;
  1106. //
  1107. // Make sure that the times specified in the SMB are reasonable
  1108. // before converting them.
  1109. //
  1110. if (TimeFields.Year < 1601) {
  1111. TimeFields.Year = 1601;
  1112. }
  1113. if (TimeFields.Month > 12) {
  1114. TimeFields.Month = 12;
  1115. }
  1116. if (TimeFields.Hour >= 24) {
  1117. TimeFields.Hour = 23;
  1118. }
  1119. if (TimeFields.Minute >= 60) {
  1120. TimeFields.Minute = 59;
  1121. }
  1122. if (TimeFields.Second >= 60) {
  1123. TimeFields.Second = 59;
  1124. }
  1125. if (!RtlTimeFieldsToTime(&TimeFields, &OutputTime)) {
  1126. OutputTime.HighPart = 0;
  1127. OutputTime.LowPart = 0;
  1128. return OutputTime;
  1129. }
  1130. ExLocalTimeToSystemTime(&OutputTime, &OutputTime);
  1131. }
  1132. return OutputTime;
  1133. }
  1134. VOID
  1135. GetLanmanTimeBias(
  1136. PSMBCE_SERVER pServer,
  1137. PRESP_NEGOTIATE pNegotiateResponse)
  1138. /*++
  1139. Routine Description:
  1140. This routine extracts the time bias from a Lanman server
  1141. Arguments:
  1142. pServer - the server
  1143. pNtNegotiateResponse - the response
  1144. Return Value:
  1145. STATUS_SUCCESS - implies that pServer is a valid instnace .
  1146. Other Status codes correspond to error situations.
  1147. --*/
  1148. {
  1149. // If this is a LM 1.0 or 2.0 server (ie a non NT server), we
  1150. // remember the timezone and bias our time based on this value.
  1151. //
  1152. // The redirector assumes that all times from these servers are
  1153. // local time for the server, and converts them to local time
  1154. // using this bias. It then tells the user the local time for
  1155. // the file on the server.
  1156. LARGE_INTEGER Workspace, ServerTime, CurrentTime;
  1157. BOOLEAN Negated = FALSE;
  1158. SMB_TIME SmbServerTime;
  1159. SMB_DATE SmbServerDate;
  1160. SmbMoveTime(&SmbServerTime, &pNegotiateResponse->ServerTime);
  1161. SmbMoveDate(&SmbServerDate, &pNegotiateResponse->ServerDate);
  1162. ServerTime = ConvertSmbTimeToTime(SmbServerTime, SmbServerDate);
  1163. KeQuerySystemTime(&CurrentTime);
  1164. #ifndef WIN9X
  1165. Workspace.QuadPart = CurrentTime.QuadPart - ServerTime.QuadPart;
  1166. #else
  1167. RxLiSubLi(&Workspace.QuadPart, &CurrentTime.QuadPart, &ServerTime.QuadPart);
  1168. #endif
  1169. if ( Workspace.HighPart < 0) {
  1170. // avoid using -ve large integers to routines that accept only unsigned
  1171. #ifndef WIN9X
  1172. Workspace.QuadPart = -Workspace.QuadPart;
  1173. #else
  1174. Workspace.HighPart = -Workspace.HighPart;
  1175. Workspace.LowPart = -Workspace.LowPart;
  1176. #endif
  1177. Negated = TRUE;
  1178. }
  1179. //
  1180. // Workspace has the exact difference in 100ns intervals
  1181. // between the server and redirector times. To remove the minor
  1182. // difference between the time settings on the two machines we
  1183. // round the Bias to the nearest 30 minutes.
  1184. //
  1185. // Calculate ((exact bias+15minutes)/30minutes)* 30minutes
  1186. // then convert back to the bias time.
  1187. //
  1188. #ifndef WIN9X
  1189. Workspace.QuadPart += ((LONGLONG) ONE_MINUTE_IN_TIME) * 15;
  1190. // Workspace is now exact bias + 15 minutes in 100ns units
  1191. Workspace.QuadPart /= ((LONGLONG) ONE_MINUTE_IN_TIME) * 30;
  1192. pServer->TimeZoneBias.QuadPart = Workspace.QuadPart * ((LONGLONG) ONE_MINUTE_IN_TIME) * 30;
  1193. #else
  1194. pServer->TimeZoneBias.HighPart = Workspace.HighPart;
  1195. pServer->TimeZoneBias.LowPart = Workspace.LowPart;
  1196. #endif
  1197. if ( Negated == TRUE ) {
  1198. #ifndef WIN9X
  1199. pServer->TimeZoneBias.QuadPart = -pServer->TimeZoneBias.QuadPart;
  1200. #else
  1201. pServer->TimeZoneBias.HighPart = -pServer->TimeZoneBias.HighPart;
  1202. pServer->TimeZoneBias.LowPart = -pServer->TimeZoneBias.LowPart;
  1203. #endif
  1204. }
  1205. }
  1206. NTSTATUS
  1207. MRxSmbCheckTransportName(
  1208. IN PIRP Irp,
  1209. OUT PSMBCEDB_SERVER_ENTRY *ppServerEntry)
  1210. /*++
  1211. Routine Description:
  1212. This routine implements the transport name checking on existing connection the server.
  1213. If the tranport name is provided on the Irp, we have 3 cases:
  1214. 1. There is no connection to the the server requested;
  1215. 2. There is an existing connection to the server and the transport used by the connection has
  1216. the same name as provided;
  1217. 3. There is an existing connection to the server and the transport used by the connection has
  1218. the different name as provided.
  1219. This routine will force to tear down the connection on case 3 and let the server reconnected
  1220. on the new transport.
  1221. Arguments:
  1222. Irp - Supplies the Irp being processed
  1223. Return Value:
  1224. NTSTATUS - The return status for the operation
  1225. --*/
  1226. {
  1227. PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation( Irp );
  1228. PSMBCEDB_SERVER_ENTRY pServerEntry = NULL;
  1229. NTSTATUS Status = STATUS_SUCCESS;
  1230. PFILE_FULL_EA_INFORMATION pEaEntry;
  1231. pEaEntry = (PFILE_FULL_EA_INFORMATION)Irp->AssociatedIrp.SystemBuffer;
  1232. if ((pEaEntry != NULL) && (Irp->Flags & IRP_CREATE_OPERATION)) {
  1233. if (IrpSp->Parameters.Create.Options & FILE_CREATE_TREE_CONNECTION) {
  1234. for(;;) {
  1235. if (strcmp(pEaEntry->EaName, EA_NAME_TRANSPORT) == 0) {
  1236. if (pEaEntry->EaValueLength > 0) {
  1237. RXCE_TRANSPORT RxCeTransport;
  1238. USHORT NameLength;
  1239. PUNICODE_STRING ServerName = &IrpSp->FileObject->FileName;
  1240. PUNICODE_STRING TransportName = NULL;
  1241. if (pEaEntry->EaValueLength > 0) {
  1242. TransportName = RxAllocatePoolWithTag(
  1243. NonPagedPool,
  1244. (sizeof(UNICODE_STRING) + pEaEntry->EaValueLength),
  1245. MRXSMB_MISC_POOLTAG);
  1246. } else {
  1247. break;
  1248. }
  1249. if (TransportName != NULL) {
  1250. TransportName->Length = pEaEntry->EaValueLength;
  1251. TransportName->MaximumLength = pEaEntry->EaValueLength;
  1252. TransportName->Buffer = (PWCHAR)((PCHAR)TransportName + sizeof(UNICODE_STRING));
  1253. RtlCopyMemory(
  1254. TransportName->Buffer,
  1255. pEaEntry->EaName + pEaEntry->EaNameLength + 1,
  1256. TransportName->Length);
  1257. } else {
  1258. Status = STATUS_INSUFFICIENT_RESOURCES;
  1259. break;
  1260. }
  1261. NameLength = ServerName->Length;
  1262. SmbCeAcquireResource();
  1263. pServerEntry = SmbCeGetFirstServerEntry();
  1264. while (pServerEntry != NULL) {
  1265. if ((NameLength > pServerEntry->Name.Length) &&
  1266. (ServerName->Buffer[pServerEntry->Name.Length/2] == OBJ_NAME_PATH_SEPARATOR)) {
  1267. ServerName->Length = pServerEntry->Name.Length;
  1268. if (RtlEqualUnicodeString(
  1269. &pServerEntry->Name,
  1270. ServerName,
  1271. TRUE)) {
  1272. if (pServerEntry->Header.State == SMBCEDB_CONSTRUCTION_IN_PROGRESS) {
  1273. pServerEntry = NULL;
  1274. Status = STATUS_CONNECTION_ACTIVE;
  1275. break;
  1276. }
  1277. SmbCeReferenceServerEntry(pServerEntry);
  1278. if (pServerEntry->pTransport != NULL) {
  1279. RxCeTransport = pServerEntry->pTransport->pTransport->RxCeTransport;
  1280. if (!RtlEqualUnicodeString(
  1281. &RxCeTransport.Name,
  1282. TransportName,
  1283. TRUE)) {
  1284. PSMBCE_TRANSPORT PreferredTransport;
  1285. if (!MRxSmbBootedRemotely &&
  1286. IsListEmpty(&pServerEntry->ActiveExchanges) &&
  1287. (pServerEntry->Server.NumberOfSrvOpens == 0) && //needs to be fixed
  1288. (InterlockedCompareExchange(&(pServerEntry->TransportSpecifiedByUser),1,0) == 0) &&
  1289. ((PreferredTransport = SmbCeFindTransport(TransportName)) != NULL)) {
  1290. if (pServerEntry->Server.NumberOfSrvOpens > 0) {
  1291. PSMBCEDB_NET_ROOT_ENTRY pNetRootEntry = NULL;
  1292. pNetRootEntry = SmbCeGetFirstNetRootEntry(pServerEntry);
  1293. while (pNetRootEntry != NULL) {
  1294. RxFinalizeNetRoot((PNET_ROOT)pNetRootEntry->pRdbssNetRoot,TRUE,FALSE);
  1295. pNetRootEntry = SmbCeGetNextNetRootEntry(pServerEntry,pNetRootEntry);
  1296. }
  1297. }
  1298. RxDbgTrace(0, Dbg, ("Force tear down connection over Transport: %wZ\n", &RxCeTransport.Name));
  1299. SmbCeTransportDisconnectIndicated(pServerEntry);
  1300. RxDbgTrace(0, Dbg, ("Build connection over Transport: %wZ\n", TransportName));
  1301. if (pServerEntry->PreferredTransport != NULL) {
  1302. SmbCeDereferenceTransport(pServerEntry->PreferredTransport);
  1303. }
  1304. pServerEntry->PreferredTransport = PreferredTransport;
  1305. } else {
  1306. SmbCeDereferenceServerEntry(pServerEntry);
  1307. pServerEntry = NULL;
  1308. Status = STATUS_CONNECTION_ACTIVE;
  1309. }
  1310. }
  1311. }
  1312. break;
  1313. }
  1314. }
  1315. pServerEntry = SmbCeGetNextServerEntry(pServerEntry);
  1316. }
  1317. SmbCeReleaseResource();
  1318. RxFreePool(TransportName);
  1319. ServerName->Length = NameLength;
  1320. }
  1321. break;
  1322. } else {
  1323. if (pEaEntry->NextEntryOffset == 0) {
  1324. break;
  1325. } else
  1326. pEaEntry = (PFILE_FULL_EA_INFORMATION)
  1327. ((PCHAR) pEaEntry + pEaEntry->NextEntryOffset);
  1328. }
  1329. }
  1330. }
  1331. }
  1332. *ppServerEntry = pServerEntry;
  1333. return Status;
  1334. }