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.

1271 lines
39 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. smbadmin.c
  5. Abstract:
  6. This module implements the SMB's that need to be exchanged to facilitate
  7. bookkeeping at the server
  8. Author:
  9. Balan Sethu Raman [SethuR] 7-March-1995
  10. Revision History:
  11. --*/
  12. #include "precomp.h"
  13. #pragma hdrstop
  14. #ifdef ALLOC_PRAGMA
  15. #pragma alloc_text(PAGE, SmbCeNegotiate)
  16. #pragma alloc_text(PAGE, SmbCeDisconnect)
  17. #pragma alloc_text(PAGE, SmbCeLogOff)
  18. #pragma alloc_text(PAGE, SmbCeInitializeAdminExchange)
  19. #pragma alloc_text(PAGE, SmbCeDiscardAdminExchange)
  20. #pragma alloc_text(PAGE, SmbCeCompleteAdminExchange)
  21. #pragma alloc_text(PAGE, SmbAdminExchangeStart)
  22. #endif
  23. //
  24. // The Bug check file id for this module
  25. //
  26. #define BugCheckFileId (RDBSS_BUG_CHECK_SMB_NETROOT)
  27. //
  28. // The local debug trace level
  29. //
  30. #define Dbg (DEBUG_TRACE_DISPATCH)
  31. extern
  32. SMB_EXCHANGE_DISPATCH_VECTOR EchoExchangeDispatch;
  33. extern NTSTATUS
  34. SmbCeInitializeAdminExchange(
  35. PSMB_ADMIN_EXCHANGE pSmbAdminExchange,
  36. PSMBCEDB_SERVER_ENTRY pServerEntry,
  37. PSMBCEDB_SESSION_ENTRY pSessionEntry,
  38. PSMBCEDB_NET_ROOT_ENTRY pNetRootEntry,
  39. UCHAR SmbCommand);
  40. extern VOID
  41. SmbCeDiscardAdminExchange(
  42. PSMB_ADMIN_EXCHANGE pSmbAdminExchange);
  43. extern NTSTATUS
  44. SmbCeCompleteAdminExchange(
  45. PSMB_ADMIN_EXCHANGE pSmbAdminExchange);
  46. VOID
  47. SmbReferenceRecord(
  48. PREFERENCE_RECORD pReferenceRecord,
  49. PVOID FileName,
  50. ULONG FileLine)
  51. {
  52. int i;
  53. for (i=REFERENCE_RECORD_SIZE-1;i>0;i--) {
  54. pReferenceRecord[i].FileName = pReferenceRecord[i-1].FileName;
  55. pReferenceRecord[i].FileLine = pReferenceRecord[i-1].FileLine;
  56. }
  57. pReferenceRecord[0].FileName = FileName;
  58. pReferenceRecord[0].FileLine = FileLine;
  59. }
  60. PSMB_EXCHANGE
  61. SmbSetServerEntryNegotiateExchange(
  62. PSMBCEDB_SERVER_ENTRY pServerEntry,
  63. PSMB_ADMIN_EXCHANGE pSmbAdminExchange)
  64. {
  65. PSMB_EXCHANGE pStoredExchange;
  66. SmbCeIncrementPendingLocalOperations((PSMB_EXCHANGE)pSmbAdminExchange);
  67. pStoredExchange = InterlockedCompareExchangePointer(
  68. &pServerEntry->pNegotiateExchange,
  69. pSmbAdminExchange,
  70. NULL);
  71. if (pStoredExchange != NULL) {
  72. SmbCeDecrementPendingLocalOperationsAndFinalize((PSMB_EXCHANGE)pSmbAdminExchange);
  73. }
  74. return pStoredExchange;
  75. }
  76. PSMB_EXCHANGE
  77. SmbResetServerEntryNegotiateExchange(
  78. PSMBCEDB_SERVER_ENTRY pServerEntry)
  79. {
  80. PSMB_EXCHANGE pStoredExchange;
  81. pStoredExchange = (PSMB_EXCHANGE)InterlockedCompareExchangePointer(
  82. &pServerEntry->pNegotiateExchange,
  83. NULL,
  84. pServerEntry->pNegotiateExchange);
  85. return pStoredExchange;
  86. }
  87. NTSTATUS
  88. SmbCeNegotiate(
  89. PSMBCEDB_SERVER_ENTRY pServerEntry,
  90. PMRX_SRV_CALL pSrvCall,
  91. BOOLEAN RemoteBootSession)
  92. /*++
  93. Routine Description:
  94. This routine issues the negotiate SMB to the server
  95. Arguments:
  96. pServerEntry - the server entry
  97. pSrvCall - the associated srv call instance in the wrapper
  98. RemoteBootSession - is this a negotiate for a remote boot session
  99. Return Value:
  100. STATUS_SUCCESS - the server call construction has been finalized.
  101. Other Status codes correspond to error situations.
  102. Notes:
  103. Since the negotiate SMB can be directed at either a unknown server or a server
  104. whose capabilitiese are known it is upto the caller to decide to wait for the
  105. response.
  106. On some transports a reconnect is possible without having to tear down an existing
  107. connection, i.e. attempting to send a packet reestablishes the connection at the
  108. lower level. Since this is not supported by all the transports ( with the exception
  109. of TCP/IP) the reference server entry initiates this process by tearing down the
  110. existing transport and reinitializsing it.
  111. As part of the negotiate response the domain name to which the server belongs is
  112. sent back. Since the negotiate response is processed at DPC level, a preparatory
  113. allocation needs to be made ( This will ensure minimal work at DPC level).
  114. In this routine this is accomplished by allocating a buffer from nonpaged
  115. pool of MAX_PATH and associating it with the DomainName fild in the server entry
  116. prior to the TRanceive. On resumption from Tranceive this buffer is deallocated and
  117. a buffer from paged pool corresponding to the exact length is allocated to hold the
  118. domain name.
  119. --*/
  120. {
  121. NTSTATUS Status;
  122. PSMB_ADMIN_EXCHANGE pSmbAdminExchange;
  123. ULONG NegotiateSmbLength;
  124. PVOID pNegotiateSmb;
  125. PAGED_CODE();
  126. IF_NOT_MRXSMB_BUILD_FOR_DISCONNECTED_CSC {
  127. NOTHING;
  128. } else {
  129. #if 0
  130. if (SmbCeIsServerInDisconnectedMode(pServerEntry)) {
  131. return MRxSmbCscNegotiateDisconnected(pServerEntry);
  132. }
  133. #endif
  134. }
  135. pSmbAdminExchange = (PSMB_ADMIN_EXCHANGE)SmbMmAllocateExchange(ADMIN_EXCHANGE,NULL);
  136. if (pSmbAdminExchange != NULL) {
  137. Status = SmbCeInitializeAdminExchange(
  138. pSmbAdminExchange,
  139. pServerEntry,
  140. NULL,
  141. NULL,
  142. SMB_COM_NEGOTIATE);
  143. if (Status == STATUS_SUCCESS) {
  144. // Build the negotiate SMB and allocate the temporary buffer for
  145. // the DOMAIN name.
  146. Status = BuildNegotiateSmb(
  147. &pNegotiateSmb,
  148. &NegotiateSmbLength,
  149. RemoteBootSession);
  150. if (Status == STATUS_SUCCESS) {
  151. pSmbAdminExchange->pSmbBuffer = pNegotiateSmb;
  152. pSmbAdminExchange->SmbBufferLength = NegotiateSmbLength;
  153. // Preparatory allocation for the domain name buffer
  154. pSmbAdminExchange->Negotiate.pSrvCall = pSrvCall;
  155. pSmbAdminExchange->Negotiate.DomainName.Length = 0;
  156. pSmbAdminExchange->Negotiate.DomainName.MaximumLength = MAX_PATH;
  157. pSmbAdminExchange->Negotiate.DomainName.Buffer
  158. = (PWCHAR)RxAllocatePoolWithTag(
  159. NonPagedPool,
  160. MAX_PATH,
  161. MRXSMB_ADMIN_POOLTAG);
  162. if (pSmbAdminExchange->Negotiate.DomainName.Buffer == NULL) {
  163. Status = STATUS_INSUFFICIENT_RESOURCES;
  164. }
  165. }
  166. if (Status == STATUS_SUCCESS) {
  167. BOOLEAN fExchangeDiscarded = FALSE;
  168. SMBCE_RESUMPTION_CONTEXT ResumptionContext;
  169. PSMB_EXCHANGE pStoredExchange;
  170. SmbCeInitializeResumptionContext(&ResumptionContext);
  171. pSmbAdminExchange->pResumptionContext = &ResumptionContext;
  172. // Since the Negotiate SMB is the first SMB that is sent on a
  173. // connection the MID mapping data structures have not been setup.
  174. // Therefore a certain amount of additional initialization is
  175. // required to ensure that the Negotiate SMB can be handled correctly.
  176. // This involves presetting the MID field in the header and the
  177. // SMBCE_EXCHANGE_MID_VALID field in the exchange.
  178. //
  179. // A beneficial side effect of implementing it this way is the reduced
  180. // path length for the regular Send/Receives on a connection.
  181. pSmbAdminExchange->SmbCeFlags = (SMBCE_EXCHANGE_REUSE_MID |
  182. SMBCE_EXCHANGE_RETAIN_MID |
  183. SMBCE_EXCHANGE_TIMED_RECEIVE_OPERATION |
  184. SMBCE_EXCHANGE_MID_VALID);
  185. // Prevent the admin exchange from being finalized before returning back to this routine.
  186. SmbCeIncrementPendingLocalOperations((PSMB_EXCHANGE)pSmbAdminExchange);
  187. pStoredExchange = SmbSetServerEntryNegotiateExchange(
  188. pServerEntry,
  189. pSmbAdminExchange);
  190. if ((pServerEntry->Header.State == SMBCEDB_CONSTRUCTION_IN_PROGRESS) &&
  191. (pStoredExchange == NULL)) {
  192. // The Negotiate SMB exchange has been built successfully. Initiate it.
  193. Status = SmbCeInitiateExchange((PSMB_EXCHANGE)pSmbAdminExchange);
  194. if ((pSmbAdminExchange->SmbStatus != STATUS_SUCCESS) ||
  195. (Status != STATUS_PENDING && Status != STATUS_SUCCESS)) {
  196. pStoredExchange = (PSMB_EXCHANGE)InterlockedCompareExchangePointer(
  197. &pServerEntry->pNegotiateExchange,
  198. NULL,
  199. pSmbAdminExchange);
  200. if (pStoredExchange == (PSMB_EXCHANGE)pSmbAdminExchange) {
  201. SmbCeDecrementPendingLocalOperations((PSMB_EXCHANGE)pSmbAdminExchange);
  202. }
  203. if (pSmbAdminExchange->SmbStatus == STATUS_SUCCESS) {
  204. pSmbAdminExchange->SmbStatus = Status;
  205. }
  206. pSmbAdminExchange->Status = pSmbAdminExchange->SmbStatus;
  207. }
  208. // Admin exchange is ready to be finalized
  209. SmbCeDecrementPendingLocalOperationsAndFinalize((PSMB_EXCHANGE)pSmbAdminExchange);
  210. // Wait for the finalization.
  211. SmbCeSuspend(&ResumptionContext);
  212. Status = SmbCeCompleteAdminExchange(pSmbAdminExchange);
  213. } else {
  214. InterlockedCompareExchangePointer(
  215. &pServerEntry->pNegotiateExchange,
  216. NULL,
  217. pSmbAdminExchange);
  218. SmbCeDiscardAdminExchange(pSmbAdminExchange);
  219. Status = STATUS_CONNECTION_DISCONNECTED;
  220. }
  221. }
  222. } else {
  223. SmbMmFreeExchange((PSMB_EXCHANGE)pSmbAdminExchange);
  224. }
  225. } else {
  226. Status = STATUS_INSUFFICIENT_RESOURCES;
  227. }
  228. if (Status == STATUS_LOGIN_WKSTA_RESTRICTION) {
  229. DbgPrint("MRXSMB: Cannot talk to %wZ which doesn't support Security Signature.\n",&pServerEntry->Name);
  230. }
  231. return Status;
  232. }
  233. NTSTATUS
  234. SmbCeSendEchoProbe(
  235. PSMBCEDB_SERVER_ENTRY pServerEntry,
  236. PMRXSMB_ECHO_PROBE_SERVICE_CONTEXT pEchoProbeContext)
  237. /*++
  238. Routine Description:
  239. This routine sends an echo probe to the specified server
  240. Arguments:
  241. pServerEntry - the server entry
  242. pEchoProbeCOntext - the echo probe context
  243. Return Value:
  244. STATUS_SUCCESS - the disconnect SMB was sent successfully
  245. Other Status codes correspond to error situations.
  246. Notes:
  247. --*/
  248. {
  249. NTSTATUS Status;
  250. PSMB_ADMIN_EXCHANGE pSmbAdminExchange;
  251. pSmbAdminExchange = (PSMB_ADMIN_EXCHANGE)SmbMmAllocateExchange(ADMIN_EXCHANGE,NULL);
  252. if (pSmbAdminExchange != NULL) {
  253. Status = SmbCeInitializeAdminExchange(
  254. pSmbAdminExchange,
  255. pServerEntry,
  256. NULL,
  257. NULL,
  258. SMB_COM_ECHO);
  259. if (Status == STATUS_SUCCESS) {
  260. ULONG EchoMdlSize;
  261. ULONG requestSize;
  262. pSmbAdminExchange->Mid = SMBCE_ECHO_PROBE_MID;
  263. pSmbAdminExchange->SmbCeFlags = (SMBCE_EXCHANGE_REUSE_MID |
  264. SMBCE_EXCHANGE_RETAIN_MID |
  265. SMBCE_EXCHANGE_TIMED_RECEIVE_OPERATION |
  266. SMBCE_EXCHANGE_MID_VALID);
  267. requestSize = pEchoProbeContext->EchoSmbLength + TRANSPORT_HEADER_SIZE;
  268. EchoMdlSize = (ULONG)MmSizeOfMdl(
  269. pEchoProbeContext->pEchoSmb,
  270. requestSize);
  271. pSmbAdminExchange->EchoProbe.pEchoProbeMdl =
  272. RxAllocatePoolWithTag(
  273. NonPagedPool,
  274. (EchoMdlSize + requestSize ),
  275. MRXSMB_ADMIN_POOLTAG);
  276. if (pSmbAdminExchange->EchoProbe.pEchoProbeMdl != NULL) {
  277. PBYTE pEchoProbeBuffer;
  278. pEchoProbeBuffer = (PBYTE)pSmbAdminExchange->EchoProbe.pEchoProbeMdl +
  279. EchoMdlSize + TRANSPORT_HEADER_SIZE;
  280. pSmbAdminExchange->EchoProbe.EchoProbeLength = pEchoProbeContext->EchoSmbLength;
  281. RtlCopyMemory(
  282. pEchoProbeBuffer,
  283. pEchoProbeContext->pEchoSmb,
  284. pEchoProbeContext->EchoSmbLength);
  285. RxInitializeHeaderMdl(
  286. pSmbAdminExchange->EchoProbe.pEchoProbeMdl,
  287. pEchoProbeBuffer,
  288. pEchoProbeContext->EchoSmbLength);
  289. MmBuildMdlForNonPagedPool(
  290. pSmbAdminExchange->EchoProbe.pEchoProbeMdl);
  291. InterlockedIncrement(&pServerEntry->Server.NumberOfEchoProbesSent);
  292. // The ECHO probe SMB exchange has been built successfully. Initiate it.
  293. Status = SmbCeInitiateExchange((PSMB_EXCHANGE)pSmbAdminExchange);
  294. } else {
  295. Status = STATUS_INSUFFICIENT_RESOURCES;
  296. }
  297. if (Status != STATUS_PENDING) {
  298. Status = SmbCeCompleteAdminExchange(pSmbAdminExchange);
  299. }
  300. } else {
  301. SmbMmFreeExchange((PSMB_EXCHANGE)pSmbAdminExchange);
  302. }
  303. } else {
  304. Status = STATUS_INSUFFICIENT_RESOURCES;
  305. }
  306. return Status;
  307. }
  308. NTSTATUS
  309. SmbCeDisconnect(
  310. PSMBCE_V_NET_ROOT_CONTEXT pVNetRootContext)
  311. /*++
  312. Routine Description:
  313. This routine issues the disconnect SMB for an existing connection to the server
  314. Arguments:
  315. pServerEntry - the server entry
  316. pVNetRootContext - the VNetRootContext
  317. Return Value:
  318. STATUS_SUCCESS - the disconnect SMB was sent successfully
  319. Other Status codes correspond to error situations.
  320. Notes:
  321. --*/
  322. {
  323. NTSTATUS Status;
  324. PSMB_ADMIN_EXCHANGE pSmbAdminExchange;
  325. PSMB_HEADER pSmbHeader;
  326. PREQ_TREE_DISCONNECT pReqTreeDisconnect;
  327. PAGED_CODE();
  328. // On mailslot servers no disconnects are required.
  329. if (SmbCeGetServerType(pVNetRootContext->pServerEntry) == SMBCEDB_MAILSLOT_SERVER) {
  330. return STATUS_SUCCESS;
  331. }
  332. pSmbAdminExchange = (PSMB_ADMIN_EXCHANGE)SmbMmAllocateExchange(ADMIN_EXCHANGE,NULL);
  333. if (pSmbAdminExchange != NULL) {
  334. UCHAR LastCommandInHeader;
  335. PUCHAR pCommand;
  336. Status = SmbCeInitializeAdminExchange(
  337. pSmbAdminExchange,
  338. pVNetRootContext->pServerEntry,
  339. pVNetRootContext->pSessionEntry,
  340. pVNetRootContext->pNetRootEntry,
  341. SMB_COM_TREE_DISCONNECT);
  342. if (Status == STATUS_SUCCESS) {
  343. BOOLEAN fExchangeDiscarded = FALSE;
  344. pSmbAdminExchange->pSmbBuffer =
  345. (PCHAR) pSmbAdminExchange->Disconnect.DisconnectSmb + TRANSPORT_HEADER_SIZE;
  346. pSmbHeader = (PSMB_HEADER)pSmbAdminExchange->pSmbBuffer;
  347. pReqTreeDisconnect = (PREQ_TREE_DISCONNECT)(pSmbHeader + 1);
  348. // Build the header
  349. Status = SmbCeBuildSmbHeader(
  350. (PSMB_EXCHANGE)pSmbAdminExchange,
  351. pSmbAdminExchange->pSmbBuffer,
  352. sizeof(SMB_HEADER),
  353. &pSmbAdminExchange->SmbBufferLength,
  354. &LastCommandInHeader,
  355. &pCommand);
  356. if (Status == STATUS_SUCCESS) {
  357. ASSERT(LastCommandInHeader == SMB_COM_NO_ANDX_COMMAND);
  358. *pCommand = SMB_COM_TREE_DISCONNECT;
  359. pSmbHeader->Tid = pVNetRootContext->TreeId;
  360. pReqTreeDisconnect->WordCount = 0;
  361. SmbPutUshort(&pReqTreeDisconnect->ByteCount,0);
  362. pSmbAdminExchange->SmbBufferLength += FIELD_OFFSET(REQ_TREE_DISCONNECT,Buffer);
  363. Status = SmbCeInitiateExchange((PSMB_EXCHANGE)pSmbAdminExchange);
  364. if ((Status == STATUS_PENDING) ||
  365. (Status == STATUS_SUCCESS)) {
  366. // async completion will also discard the exchange
  367. fExchangeDiscarded = TRUE;
  368. }
  369. }
  370. if (!fExchangeDiscarded) {
  371. SmbCeDiscardAdminExchange(pSmbAdminExchange);
  372. }
  373. } else {
  374. SmbMmFreeExchange((PSMB_EXCHANGE)pSmbAdminExchange);
  375. }
  376. } else {
  377. Status = STATUS_INSUFFICIENT_RESOURCES;
  378. }
  379. return Status;
  380. }
  381. extern BOOLEAN MRxSmbEnableDownLevelLogOff;
  382. NTSTATUS
  383. SmbCeLogOff(
  384. PSMBCEDB_SERVER_ENTRY pServerEntry,
  385. PSMBCEDB_SESSION_ENTRY pSessionEntry)
  386. /*++
  387. Routine Description:
  388. This routine issues the logoff SMB for an existing session to the server
  389. Arguments:
  390. pServerEntry - the server entry
  391. pSessionEntry - the associated session entry
  392. Return Value:
  393. STATUS_SUCCESS - the logoff was successfully sent.
  394. Other Status codes correspond to error situations.
  395. Notes:
  396. --*/
  397. {
  398. NTSTATUS Status;
  399. PSMB_ADMIN_EXCHANGE pSmbAdminExchange;
  400. PSMB_HEADER pSmbHeader;
  401. PREQ_LOGOFF_ANDX pReqLogOffAndX;
  402. PAGED_CODE();
  403. if ((SmbCeGetServerType(pServerEntry) == SMBCEDB_MAILSLOT_SERVER) ||
  404. (pServerEntry->Server.SecurityMode == SECURITY_MODE_SHARE_LEVEL)) {
  405. if (pSessionEntry != NULL) {
  406. SmbCeDereferenceSessionEntry(pSessionEntry);
  407. }
  408. return STATUS_SUCCESS;
  409. }
  410. //
  411. // Some servers (like linux) don't really know how to handle session logoffs.
  412. // So, let's just be sure that we only do this to NT or better servers,
  413. // because we know that they handle it correctly. The version of Linux we have
  414. // seems to like to negotiate the NT dialect even though it really isn't NT. That's
  415. // why the extra check is put in here for NT status codes.
  416. //
  417. // Bypass this behavior based on the 'EnableDownLevelLogOff' registry key.
  418. //
  419. if( MRxSmbEnableDownLevelLogOff == FALSE ) {
  420. if( pServerEntry->Server.Dialect < NTLANMAN_DIALECT ||
  421. !FlagOn(pServerEntry->Server.DialectFlags,DF_NT_STATUS) ) {
  422. if (pSessionEntry != NULL) {
  423. SmbCeDereferenceSessionEntry(pSessionEntry);
  424. }
  425. return STATUS_SUCCESS;
  426. }
  427. }
  428. pSmbAdminExchange = (PSMB_ADMIN_EXCHANGE)SmbMmAllocateExchange(ADMIN_EXCHANGE,NULL);
  429. if (pSmbAdminExchange != NULL) {
  430. UCHAR LastCommandInHeader;
  431. PUCHAR pCommand;
  432. Status = SmbCeInitializeAdminExchange(
  433. pSmbAdminExchange,
  434. pServerEntry,
  435. pSessionEntry,
  436. NULL,
  437. SMB_COM_LOGOFF_ANDX);
  438. if (Status == STATUS_SUCCESS) {
  439. BOOLEAN fExchangeDiscarded = FALSE;
  440. pSmbAdminExchange->pSmbBuffer =
  441. (PCHAR) pSmbAdminExchange->LogOff.LogOffSmb + TRANSPORT_HEADER_SIZE;
  442. pSmbHeader = (PSMB_HEADER)pSmbAdminExchange->pSmbBuffer;
  443. pReqLogOffAndX = (PREQ_LOGOFF_ANDX)(pSmbHeader + 1);
  444. // Build the header
  445. Status = SmbCeBuildSmbHeader(
  446. (PSMB_EXCHANGE)pSmbAdminExchange,
  447. pSmbAdminExchange->pSmbBuffer,
  448. sizeof(SMB_HEADER),
  449. &pSmbAdminExchange->SmbBufferLength,
  450. &LastCommandInHeader,
  451. &pCommand);
  452. if (Status == STATUS_SUCCESS) {
  453. ASSERT(LastCommandInHeader == SMB_COM_NO_ANDX_COMMAND);
  454. *pCommand = SMB_COM_LOGOFF_ANDX;
  455. pReqLogOffAndX->WordCount = 2;
  456. pReqLogOffAndX->AndXCommand = SMB_COM_NO_ANDX_COMMAND;
  457. pReqLogOffAndX->AndXReserved = 0;
  458. SmbPutUshort(&pReqLogOffAndX->AndXOffset,0);
  459. SmbPutUshort(&pReqLogOffAndX->ByteCount,0);
  460. pSmbAdminExchange->SmbBufferLength += FIELD_OFFSET(REQ_LOGOFF_ANDX,Buffer);
  461. Status = SmbCeInitiateExchange((PSMB_EXCHANGE)pSmbAdminExchange);
  462. if ((Status == STATUS_PENDING) ||
  463. (Status == STATUS_SUCCESS)) {
  464. // async completion will discard the exchange
  465. fExchangeDiscarded = TRUE;
  466. }
  467. }
  468. if (!fExchangeDiscarded) {
  469. SmbCeDiscardAdminExchange(pSmbAdminExchange);
  470. }
  471. } else {
  472. SmbMmFreeExchange((PSMB_EXCHANGE)pSmbAdminExchange);
  473. }
  474. } else {
  475. Status = RX_MAP_STATUS(INSUFFICIENT_RESOURCES);
  476. }
  477. return Status;
  478. }
  479. NTSTATUS
  480. SmbCeInitializeAdminExchange(
  481. PSMB_ADMIN_EXCHANGE pSmbAdminExchange,
  482. PSMBCEDB_SERVER_ENTRY pServerEntry,
  483. PSMBCEDB_SESSION_ENTRY pSessionEntry,
  484. PSMBCEDB_NET_ROOT_ENTRY pNetRootEntry,
  485. UCHAR SmbCommand)
  486. /*++
  487. Routine Description:
  488. This routine initializes the ADMIN exchange
  489. Arguments:
  490. pSmbAdminExchange - the exchange
  491. pServerEntry - the associated server entry
  492. pSessionEntry - the associated session entry
  493. pNetRootEntry - the associated net root entry
  494. SmbCommand - the SMB command
  495. Return Value:
  496. STATUS_SUCCESS - the logoff was successfully sent.
  497. Other Status codes correspond to error situations.
  498. Notes:
  499. The ADMIN_EXCHANGE is a special type of exchange used for bootstrap/teardown
  500. situations in which the initialization of the exchange cannot follow the noraml
  501. course of events. In some cases not all the components required for proper
  502. initialization of the exchange are present, e.g., NEGOTIATE we do not have a
  503. valid session/tree connect. It is for this reason that the three important
  504. elements of initialization, i.e., Server/Session/NetRoot have to be explicitly
  505. specified. NULL is used to signify a dont care situation for a particular component.
  506. --*/
  507. {
  508. NTSTATUS Status;
  509. PAGED_CODE();
  510. Status = SmbCeIncrementActiveExchangeCount();
  511. if (Status == STATUS_SUCCESS) {
  512. PSMBCE_V_NET_ROOT_CONTEXT pVNetRootContext;
  513. pSmbAdminExchange->CancellationStatus = SMBCE_EXCHANGE_NOT_CANCELLED;
  514. if ((SmbCommand == SMB_COM_NEGOTIATE) ||
  515. (SmbCommand == SMB_COM_ECHO)) {
  516. pSmbAdminExchange->SmbCeContext.pServerEntry = pServerEntry;
  517. pSmbAdminExchange->SmbCeContext.pVNetRootContext = NULL;
  518. } else {
  519. pVNetRootContext = (PSMBCE_V_NET_ROOT_CONTEXT)
  520. RxAllocatePoolWithTag(
  521. NonPagedPool,
  522. sizeof(SMBCE_V_NET_ROOT_CONTEXT),
  523. MRXSMB_VNETROOT_POOLTAG);
  524. if (pVNetRootContext != NULL) {
  525. pVNetRootContext->pServerEntry = pServerEntry;
  526. pVNetRootContext->pSessionEntry = pSessionEntry;
  527. pVNetRootContext->pNetRootEntry = pNetRootEntry;
  528. pSmbAdminExchange->SmbCeContext.pVNetRootContext = pVNetRootContext;
  529. pSmbAdminExchange->SmbCeContext.pServerEntry = pServerEntry;
  530. } else {
  531. Status = STATUS_INSUFFICIENT_RESOURCES;
  532. }
  533. }
  534. if (Status == STATUS_SUCCESS) {
  535. SmbCeReferenceServerEntry(pServerEntry);
  536. pSmbAdminExchange->pSmbMdl = NULL;
  537. pSmbAdminExchange->pSmbBuffer = NULL;
  538. pSmbAdminExchange->SmbBufferLength = 0;
  539. // Set the SmbCe state to overrule the common method of having to hunt
  540. // up a valid TID/FID etc. and reconnects.
  541. pSmbAdminExchange->SmbCommand = SmbCommand;
  542. pSmbAdminExchange->SmbCeState = SMBCE_EXCHANGE_NETROOT_INITIALIZED;
  543. switch (pSmbAdminExchange->SmbCommand) {
  544. case SMB_COM_NEGOTIATE:
  545. {
  546. pSmbAdminExchange->Negotiate.DomainName.Length = 0;
  547. pSmbAdminExchange->Negotiate.DomainName.MaximumLength = 0;
  548. pSmbAdminExchange->Negotiate.DomainName.Buffer = NULL;
  549. pSmbAdminExchange->Negotiate.pSecurityBlobMdl = NULL;
  550. }
  551. break;
  552. case SMB_COM_TREE_DISCONNECT:
  553. case SMB_COM_LOGOFF_ANDX:
  554. break;
  555. case SMB_COM_ECHO:
  556. {
  557. pSmbAdminExchange->pDispatchVector = &EchoExchangeDispatch;
  558. pSmbAdminExchange->EchoProbe.pEchoProbeMdl = NULL;
  559. pSmbAdminExchange->EchoProbe.EchoProbeLength = 0;
  560. }
  561. break;
  562. default:
  563. ASSERT(!"Valid Command for Admin Exchange");
  564. break;
  565. }
  566. SmbCeAcquireResource();
  567. if ((pSessionEntry != NULL) &&
  568. pServerEntry->SecuritySignaturesEnabled &&
  569. !pServerEntry->SecuritySignaturesActive) {
  570. // if security signature is enabled and not yet turned on, exchange should wait for
  571. // outstanding extended session setup to finish before resume in order to avoid index mismatch.
  572. Status = SmbCeSyncExchangeForSecuritySignature((PSMB_EXCHANGE)pSmbAdminExchange);
  573. ASSERT(Status != STATUS_PENDING);
  574. }
  575. SmbCeReleaseResource();
  576. if (Status != STATUS_SUCCESS) {
  577. RxFreePool(pSmbAdminExchange->SmbCeContext.pVNetRootContext);
  578. pSmbAdminExchange->SmbCeContext.pVNetRootContext = NULL;
  579. }
  580. }
  581. }
  582. return Status;
  583. }
  584. VOID
  585. SmbCeDiscardAdminExchange(
  586. PSMB_ADMIN_EXCHANGE pSmbAdminExchange)
  587. /*++
  588. Routine Description:
  589. This routine discards the ADMIN exchange
  590. Arguments:
  591. pSmbAdminExchange - the exchange
  592. --*/
  593. {
  594. PSMBCEDB_SERVER_ENTRY pServerEntry;
  595. PSMBCEDB_SESSION_ENTRY pSessionEntry;
  596. PSMBCEDB_NET_ROOT_ENTRY pNetRootEntry;
  597. PSMBCE_V_NET_ROOT_CONTEXT pVNetRootContext;
  598. PAGED_CODE();
  599. SmbCeAcquireResource();
  600. RemoveEntryList(&pSmbAdminExchange->ExchangeList);
  601. SmbCeReleaseResource();
  602. pServerEntry = SmbCeGetExchangeServerEntry(pSmbAdminExchange);
  603. pSessionEntry = SmbCeGetExchangeSessionEntry(pSmbAdminExchange);
  604. pNetRootEntry = SmbCeGetExchangeNetRootEntry(pSmbAdminExchange);
  605. pVNetRootContext = SmbCeGetExchangeVNetRootContext(pSmbAdminExchange);
  606. if (pSmbAdminExchange->pSmbMdl != NULL) {
  607. RxUnlockHeaderPages(pSmbAdminExchange->pSmbMdl);
  608. IoFreeMdl(pSmbAdminExchange->pSmbMdl);
  609. }
  610. switch (pSmbAdminExchange->SmbCommand) {
  611. case SMB_COM_NEGOTIATE:
  612. {
  613. pSmbAdminExchange->pSmbBuffer = NULL;
  614. if (pSmbAdminExchange->Negotiate.DomainName.Buffer != NULL) {
  615. RxFreePool(
  616. pSmbAdminExchange->Negotiate.DomainName.Buffer);
  617. }
  618. if (pSmbAdminExchange->Negotiate.pSecurityBlobMdl != NULL) {
  619. IoFreeMdl(
  620. pSmbAdminExchange->Negotiate.pSecurityBlobMdl);
  621. }
  622. }
  623. break;
  624. case SMB_COM_TREE_DISCONNECT:
  625. break;
  626. case SMB_COM_LOGOFF_ANDX:
  627. SmbCeDereferenceSessionEntry(pSessionEntry);
  628. break;
  629. case SMB_COM_ECHO:
  630. {
  631. if (pSmbAdminExchange->EchoProbe.pEchoProbeMdl != NULL) {
  632. MmPrepareMdlForReuse(pSmbAdminExchange->EchoProbe.pEchoProbeMdl);
  633. RxFreePool(pSmbAdminExchange->EchoProbe.pEchoProbeMdl);
  634. }
  635. }
  636. break;
  637. default:
  638. ASSERT(!"Valid Command For Admin Exchange");
  639. break;
  640. }
  641. // Tear down all the copy data requests associated with this exchange
  642. SmbCePurgeBuffersAssociatedWithExchange(pServerEntry,(PSMB_EXCHANGE)pSmbAdminExchange);
  643. SmbCeUninitializeExchangeTransport((PSMB_EXCHANGE)pSmbAdminExchange);
  644. SmbCeDereferenceServerEntry(pServerEntry);
  645. if (pVNetRootContext != NULL) {
  646. RxFreePool(pVNetRootContext);
  647. }
  648. SmbCeFreeBufferForServerResponse((PSMB_EXCHANGE)pSmbAdminExchange);
  649. SmbMmFreeExchange((PSMB_EXCHANGE)pSmbAdminExchange);
  650. SmbCeDecrementActiveExchangeCount();
  651. }
  652. NTSTATUS
  653. SmbCeCompleteAdminExchange(
  654. PSMB_ADMIN_EXCHANGE pSmbAdminExchange)
  655. /*++
  656. Routine Description:
  657. This is the routine used for completing the SMB ADMIN exchanges.
  658. Arguments:
  659. pExchange - the exchange instance
  660. Return Value:
  661. RXSTATUS - The return status for the operation
  662. Notes:
  663. This routine encapsulates the TAIL for all SMB admin exchanges. They carry
  664. out the local action required based upon the outcome of the exchange.
  665. --*/
  666. {
  667. NTSTATUS Status = STATUS_SUCCESS;
  668. PSMBCEDB_SERVER_ENTRY pServerEntry;
  669. SMBCEDB_OBJECT_STATE ServerState;
  670. PAGED_CODE();
  671. pServerEntry = SmbCeGetExchangeServerEntry(pSmbAdminExchange);
  672. switch (pSmbAdminExchange->SmbCommand) {
  673. case SMB_COM_NEGOTIATE:
  674. {
  675. if (pSmbAdminExchange->Status != STATUS_SUCCESS) {
  676. pServerEntry->ServerStatus = pSmbAdminExchange->Status;
  677. }
  678. if (pServerEntry->ServerStatus == STATUS_SUCCESS) {
  679. if (!FlagOn(pServerEntry->Server.DialectFlags,DF_EXTENDED_SECURITY)) {
  680. if (pServerEntry->DomainName.Buffer) {
  681. RxFreePool(pServerEntry->DomainName.Buffer);
  682. pServerEntry->DomainName.Buffer = NULL;
  683. }
  684. pServerEntry->DomainName.Length = pSmbAdminExchange->Negotiate.DomainName.Length;
  685. pServerEntry->DomainName.MaximumLength = pServerEntry->DomainName.Length;
  686. if (pServerEntry->DomainName.Length > 0) {
  687. pServerEntry->DomainName.Buffer = RxAllocatePoolWithTag(
  688. NonPagedPool,
  689. pServerEntry->DomainName.Length,
  690. MRXSMB_SERVER_POOLTAG);
  691. }
  692. if (pServerEntry->DomainName.Buffer != NULL) {
  693. // Copy the domain name into the server entry
  694. RtlCopyMemory(
  695. pServerEntry->DomainName.Buffer,
  696. pSmbAdminExchange->Negotiate.DomainName.Buffer,
  697. pServerEntry->DomainName.Length);
  698. } else {
  699. //The downlevel server doesn't have a domain name. It's not a problem if the
  700. //DomainName.Buffer equals to NULL.
  701. if (pServerEntry->DomainName.Length > 0) {
  702. pServerEntry->DomainName.Length = 0;
  703. pServerEntry->DomainName.MaximumLength = 0;
  704. pServerEntry->ServerStatus = STATUS_INSUFFICIENT_RESOURCES;
  705. }
  706. }
  707. }
  708. if (pServerEntry->ServerStatus == STATUS_SUCCESS) {
  709. pServerEntry->ServerStatus = SmbCeUpdateSrvCall(pServerEntry);
  710. }
  711. }
  712. Status = pServerEntry->ServerStatus;
  713. if (pServerEntry->ServerStatus == STATUS_SUCCESS) {
  714. pServerEntry->Server.EchoProbeState = ECHO_PROBE_IDLE;
  715. }
  716. }
  717. break;
  718. case SMB_COM_TREE_DISCONNECT:
  719. case SMB_COM_LOGOFF_ANDX:
  720. case SMB_COM_ECHO:
  721. default:
  722. break;
  723. }
  724. SmbCeDiscardAdminExchange(pSmbAdminExchange);
  725. return Status;
  726. }
  727. NTSTATUS
  728. SmbAdminExchangeStart(
  729. PSMB_EXCHANGE pExchange)
  730. /*++
  731. Routine Description:
  732. This is the start routine for administrative SMB exchanges.
  733. Arguments:
  734. pExchange - the exchange instance
  735. Return Value:
  736. RXSTATUS - The return status for the operation
  737. --*/
  738. {
  739. NTSTATUS Status;
  740. PSMB_ADMIN_EXCHANGE pSmbAdminExchange;
  741. PAGED_CODE();
  742. pSmbAdminExchange = (PSMB_ADMIN_EXCHANGE)pExchange;
  743. switch (pSmbAdminExchange->SmbCommand) {
  744. case SMB_COM_NEGOTIATE:
  745. case SMB_COM_LOGOFF_ANDX:
  746. case SMB_COM_TREE_DISCONNECT:
  747. {
  748. ASSERT(pSmbAdminExchange->pSmbMdl == NULL);
  749. RxAllocateHeaderMdl(
  750. pSmbAdminExchange->pSmbBuffer,
  751. pSmbAdminExchange->SmbBufferLength,
  752. pSmbAdminExchange->pSmbMdl
  753. );
  754. if (pSmbAdminExchange->pSmbMdl != NULL) {
  755. RxProbeAndLockHeaderPages(
  756. pSmbAdminExchange->pSmbMdl,
  757. KernelMode,
  758. IoModifyAccess,
  759. Status);
  760. if (Status == STATUS_SUCCESS) {
  761. Status = SmbCeTranceive(
  762. pExchange,
  763. RXCE_SEND_SYNCHRONOUS,
  764. pSmbAdminExchange->pSmbMdl,
  765. pSmbAdminExchange->SmbBufferLength);
  766. RxDbgTrace( 0, Dbg, ("Net Root SmbCeTranceive returned %lx\n",Status));
  767. } else {
  768. IoFreeMdl(pSmbAdminExchange->pSmbMdl);
  769. pSmbAdminExchange->pSmbMdl = NULL;
  770. }
  771. } else {
  772. Status = RX_MAP_STATUS(INSUFFICIENT_RESOURCES);
  773. }
  774. }
  775. break;
  776. case SMB_COM_ECHO:
  777. {
  778. Status = SmbCeSend(
  779. pExchange,
  780. 0,
  781. pSmbAdminExchange->EchoProbe.pEchoProbeMdl,
  782. pSmbAdminExchange->EchoProbe.EchoProbeLength);
  783. }
  784. break;
  785. default:
  786. Status = STATUS_UNSUCCESSFUL;
  787. break;
  788. }
  789. return Status;
  790. }
  791. NTSTATUS
  792. SmbAdminExchangeReceive(
  793. IN struct _SMB_EXCHANGE *pExchange, // The exchange instance
  794. IN ULONG BytesIndicated,
  795. IN ULONG BytesAvailable,
  796. OUT ULONG *pBytesTaken,
  797. IN PSMB_HEADER pSmbHeader,
  798. OUT PMDL *pDataBufferPointer,
  799. OUT PULONG pDataSize,
  800. IN ULONG ReceiveFlags)
  801. /*++
  802. Routine Description:
  803. This is the recieve indication handling routine for net root construction exchanges
  804. Arguments:
  805. pExchange - the exchange instance
  806. BytesIndicated - the number of bytes indicated
  807. Bytes Available - the number of bytes available
  808. pBytesTaken - the number of bytes consumed
  809. pSmbHeader - the byte buffer
  810. pDataBufferPointer - the buffer into which the remaining data is to be copied.
  811. pDataSize - the buffer size.
  812. Return Value:
  813. RXSTATUS - The return status for the operation
  814. Notes:
  815. This routine is called at DPC level.
  816. --*/
  817. {
  818. NTSTATUS Status;
  819. PSMB_ADMIN_EXCHANGE pSmbAdminExchange;
  820. RxDbgTrace( 0, (DEBUG_TRACE_ALWAYS), ("ParseSmbHeader BytesIndicated/Available %ld %ld\n",BytesIndicated,BytesAvailable));
  821. pSmbAdminExchange = (PSMB_ADMIN_EXCHANGE)pExchange;
  822. switch (pSmbAdminExchange->SmbCommand) {
  823. case SMB_COM_NEGOTIATE:
  824. {
  825. Status = ParseNegotiateResponse(
  826. pSmbAdminExchange,
  827. BytesIndicated,
  828. BytesAvailable,
  829. pBytesTaken,
  830. pSmbHeader,
  831. pDataBufferPointer,
  832. pDataSize);
  833. if (Status == STATUS_MORE_PROCESSING_REQUIRED) {
  834. if (*pDataBufferPointer != NULL &&
  835. ((*pBytesTaken + *pDataSize) <= BytesAvailable ||
  836. !FlagOn(ReceiveFlags,TDI_RECEIVE_ENTIRE_MESSAGE))) {
  837. pSmbAdminExchange->Negotiate.pSecurityBlobMdl = *pDataBufferPointer;
  838. } else {
  839. *pBytesTaken = BytesAvailable;
  840. Status = STATUS_SUCCESS;
  841. pExchange->Status = STATUS_INVALID_NETWORK_RESPONSE;
  842. }
  843. }
  844. }
  845. break;
  846. case SMB_COM_TREE_DISCONNECT:
  847. case SMB_COM_LOGOFF_ANDX:
  848. {
  849. *pBytesTaken = BytesAvailable;
  850. Status = STATUS_SUCCESS;
  851. }
  852. break;
  853. case SMB_COM_ECHO:
  854. // Since the echo probe responses are handled by the receive indication routine
  855. // at DPC level this routine should never be called for echo probes.
  856. default:
  857. {
  858. *pBytesTaken = 0;
  859. Status = STATUS_DATA_NOT_ACCEPTED;
  860. }
  861. break;
  862. }
  863. return Status;
  864. }
  865. NTSTATUS
  866. SmbAdminExchangeCopyDataHandler(
  867. IN PSMB_EXCHANGE pExchange, // The exchange instance
  868. IN PMDL pCopyDataBuffer,
  869. IN ULONG DataSize)
  870. /*++
  871. Routine Description:
  872. This is the copy data handling routine for administrative SMB exchanges
  873. Arguments:
  874. pExchange - the exchange instance
  875. Return Value:
  876. RXSTATUS - The return status for the operation
  877. --*/
  878. {
  879. UNREFERENCED_PARAMETER(pExchange);
  880. return STATUS_SUCCESS;
  881. }
  882. NTSTATUS
  883. SmbAdminExchangeSendCallbackHandler(
  884. IN PSMB_EXCHANGE pExchange,
  885. IN PMDL pXmitBuffer,
  886. IN NTSTATUS SendCompletionStatus)
  887. /*++
  888. Routine Description:
  889. This is the send call back indication handling routine for transact exchanges
  890. Arguments:
  891. pExchange - the exchange instance
  892. Return Value:
  893. NTSTATUS - The return status for the operation
  894. --*/
  895. {
  896. return STATUS_SUCCESS;
  897. UNREFERENCED_PARAMETER(pExchange);
  898. UNREFERENCED_PARAMETER(pXmitBuffer);
  899. UNREFERENCED_PARAMETER(SendCompletionStatus);
  900. }
  901. NTSTATUS
  902. SmbAdminExchangeFinalize(
  903. PSMB_EXCHANGE pExchange,
  904. BOOLEAN *pPostFinalize)
  905. /*++
  906. Routine Description:
  907. This routine finalkzes the construct net root exchange. It resumes the RDBSS by invoking
  908. the call back and discards the exchange
  909. Arguments:
  910. pExchange - the exchange instance
  911. CurrentIrql - the current interrupt request level
  912. pPostFinalize - a pointer to a BOOLEAN if the request should be posted
  913. Return Value:
  914. RXSTATUS - The return status for the operation
  915. --*/
  916. {
  917. PSMB_ADMIN_EXCHANGE pSmbAdminExchange = (PSMB_ADMIN_EXCHANGE)pExchange;
  918. if (pSmbAdminExchange->pResumptionContext != NULL) {
  919. // Signal the event
  920. *pPostFinalize = FALSE;
  921. SmbCeResume(pSmbAdminExchange->pResumptionContext);
  922. } else {
  923. if (RxShouldPostCompletion()) {
  924. *pPostFinalize = TRUE;
  925. return STATUS_SUCCESS;
  926. } else {
  927. *pPostFinalize = FALSE;
  928. SmbCeCompleteAdminExchange(pSmbAdminExchange);
  929. }
  930. }
  931. return STATUS_SUCCESS;
  932. }
  933. SMB_EXCHANGE_DISPATCH_VECTOR
  934. AdminExchangeDispatch = {
  935. SmbAdminExchangeStart,
  936. SmbAdminExchangeReceive,
  937. SmbAdminExchangeCopyDataHandler,
  938. NULL, // No Send Completion handler
  939. SmbAdminExchangeFinalize
  940. };
  941. SMB_EXCHANGE_DISPATCH_VECTOR
  942. EchoExchangeDispatch = {
  943. SmbAdminExchangeStart,
  944. SmbAdminExchangeReceive,
  945. SmbAdminExchangeCopyDataHandler,
  946. SmbAdminExchangeSendCallbackHandler,
  947. SmbAdminExchangeFinalize
  948. };
  949.