Leaked source code of windows server 2003
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
40 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_RETAIN_MID |
  182. SMBCE_EXCHANGE_TIMED_RECEIVE_OPERATION |
  183. SMBCE_EXCHANGE_MID_VALID);
  184. // Prevent the admin exchange from being finalized before returning back to this routine.
  185. SmbCeIncrementPendingLocalOperations((PSMB_EXCHANGE)pSmbAdminExchange);
  186. pStoredExchange = SmbSetServerEntryNegotiateExchange(
  187. pServerEntry,
  188. pSmbAdminExchange);
  189. if ((pServerEntry->Header.State == SMBCEDB_CONSTRUCTION_IN_PROGRESS) &&
  190. (pStoredExchange == NULL)) {
  191. // The Negotiate SMB exchange has been built successfully. Initiate it.
  192. Status = SmbCeInitiateExchange((PSMB_EXCHANGE)pSmbAdminExchange);
  193. if ((pSmbAdminExchange->SmbStatus != STATUS_SUCCESS) ||
  194. (Status != STATUS_PENDING && Status != STATUS_SUCCESS)) {
  195. pStoredExchange = (PSMB_EXCHANGE)InterlockedCompareExchangePointer(
  196. &pServerEntry->pNegotiateExchange,
  197. NULL,
  198. pSmbAdminExchange);
  199. if (pStoredExchange == (PSMB_EXCHANGE)pSmbAdminExchange) {
  200. SmbCeDecrementPendingLocalOperations((PSMB_EXCHANGE)pSmbAdminExchange);
  201. }
  202. if (pSmbAdminExchange->SmbStatus == STATUS_SUCCESS) {
  203. pSmbAdminExchange->SmbStatus = Status;
  204. }
  205. pSmbAdminExchange->Status = pSmbAdminExchange->SmbStatus;
  206. }
  207. // Admin exchange is ready to be finalized
  208. SmbCeDecrementPendingLocalOperationsAndFinalize((PSMB_EXCHANGE)pSmbAdminExchange);
  209. // Wait for the finalization.
  210. SmbCeSuspend(&ResumptionContext);
  211. Status = SmbCeCompleteAdminExchange(pSmbAdminExchange);
  212. } else {
  213. InterlockedCompareExchangePointer(
  214. &pServerEntry->pNegotiateExchange,
  215. NULL,
  216. pSmbAdminExchange);
  217. SmbCeDiscardAdminExchange(pSmbAdminExchange);
  218. Status = STATUS_CONNECTION_DISCONNECTED;
  219. }
  220. }
  221. } else {
  222. SmbMmFreeExchange((PSMB_EXCHANGE)pSmbAdminExchange);
  223. }
  224. } else {
  225. Status = STATUS_INSUFFICIENT_RESOURCES;
  226. }
  227. if (Status == STATUS_LOGIN_WKSTA_RESTRICTION) {
  228. DbgPrint("MRXSMB: Cannot talk to %wZ which doesn't support Security Signature.\n",&pServerEntry->Name);
  229. }
  230. return Status;
  231. }
  232. NTSTATUS
  233. SmbCeSendEchoProbe(
  234. PSMBCEDB_SERVER_ENTRY pServerEntry,
  235. PMRXSMB_ECHO_PROBE_SERVICE_CONTEXT pEchoProbeContext)
  236. /*++
  237. Routine Description:
  238. This routine sends an echo probe to the specified server
  239. Arguments:
  240. pServerEntry - the server entry
  241. pEchoProbeCOntext - the echo probe context
  242. Return Value:
  243. STATUS_SUCCESS - the disconnect SMB was sent successfully
  244. Other Status codes correspond to error situations.
  245. Notes:
  246. --*/
  247. {
  248. NTSTATUS Status;
  249. PSMB_ADMIN_EXCHANGE pSmbAdminExchange;
  250. pSmbAdminExchange = (PSMB_ADMIN_EXCHANGE)SmbMmAllocateExchange(ADMIN_EXCHANGE,NULL);
  251. if (pSmbAdminExchange != NULL) {
  252. Status = SmbCeInitializeAdminExchange(
  253. pSmbAdminExchange,
  254. pServerEntry,
  255. NULL,
  256. NULL,
  257. SMB_COM_ECHO);
  258. if (Status == STATUS_SUCCESS) {
  259. ULONG EchoMdlSize;
  260. ULONG requestSize;
  261. pSmbAdminExchange->Mid = SMBCE_ECHO_PROBE_MID;
  262. pSmbAdminExchange->SmbCeFlags = (SMBCE_EXCHANGE_RETAIN_MID |
  263. SMBCE_EXCHANGE_TIMED_RECEIVE_OPERATION |
  264. SMBCE_EXCHANGE_MID_VALID);
  265. requestSize = pEchoProbeContext->EchoSmbLength + TRANSPORT_HEADER_SIZE;
  266. EchoMdlSize = (ULONG)MmSizeOfMdl(
  267. pEchoProbeContext->pEchoSmb,
  268. requestSize);
  269. pSmbAdminExchange->EchoProbe.pEchoProbeMdl =
  270. RxAllocatePoolWithTag(
  271. NonPagedPool,
  272. (EchoMdlSize + requestSize ),
  273. MRXSMB_ADMIN_POOLTAG);
  274. if (pSmbAdminExchange->EchoProbe.pEchoProbeMdl != NULL) {
  275. PBYTE pEchoProbeBuffer;
  276. pEchoProbeBuffer = (PBYTE)pSmbAdminExchange->EchoProbe.pEchoProbeMdl +
  277. EchoMdlSize + TRANSPORT_HEADER_SIZE;
  278. pSmbAdminExchange->EchoProbe.EchoProbeLength = pEchoProbeContext->EchoSmbLength;
  279. RtlCopyMemory(
  280. pEchoProbeBuffer,
  281. pEchoProbeContext->pEchoSmb,
  282. pEchoProbeContext->EchoSmbLength);
  283. RxInitializeHeaderMdl(
  284. pSmbAdminExchange->EchoProbe.pEchoProbeMdl,
  285. pEchoProbeBuffer,
  286. pEchoProbeContext->EchoSmbLength);
  287. MmBuildMdlForNonPagedPool(
  288. pSmbAdminExchange->EchoProbe.pEchoProbeMdl);
  289. InterlockedIncrement(&pServerEntry->Server.NumberOfEchoProbesSent);
  290. // The ECHO probe SMB exchange has been built successfully. Initiate it.
  291. Status = SmbCeInitiateExchange((PSMB_EXCHANGE)pSmbAdminExchange);
  292. } else {
  293. Status = STATUS_INSUFFICIENT_RESOURCES;
  294. }
  295. if (Status != STATUS_PENDING) {
  296. Status = SmbCeCompleteAdminExchange(pSmbAdminExchange);
  297. }
  298. } else {
  299. SmbMmFreeExchange((PSMB_EXCHANGE)pSmbAdminExchange);
  300. }
  301. } else {
  302. Status = STATUS_INSUFFICIENT_RESOURCES;
  303. }
  304. return Status;
  305. }
  306. NTSTATUS
  307. SmbCeDisconnect(
  308. PSMBCE_V_NET_ROOT_CONTEXT pVNetRootContext)
  309. /*++
  310. Routine Description:
  311. This routine issues the disconnect SMB for an existing connection to the server
  312. Arguments:
  313. pServerEntry - the server entry
  314. pVNetRootContext - the VNetRootContext
  315. Return Value:
  316. STATUS_SUCCESS - the disconnect SMB was sent successfully
  317. Other Status codes correspond to error situations.
  318. Notes:
  319. --*/
  320. {
  321. NTSTATUS Status;
  322. PSMB_ADMIN_EXCHANGE pSmbAdminExchange;
  323. PSMB_HEADER pSmbHeader;
  324. PREQ_TREE_DISCONNECT pReqTreeDisconnect;
  325. PAGED_CODE();
  326. // On mailslot servers no disconnects are required.
  327. if (SmbCeGetServerType(pVNetRootContext->pServerEntry) == SMBCEDB_MAILSLOT_SERVER) {
  328. return STATUS_SUCCESS;
  329. }
  330. pSmbAdminExchange = (PSMB_ADMIN_EXCHANGE)SmbMmAllocateExchange(ADMIN_EXCHANGE,NULL);
  331. if (pSmbAdminExchange != NULL) {
  332. UCHAR LastCommandInHeader;
  333. PUCHAR pCommand;
  334. Status = SmbCeInitializeAdminExchange(
  335. pSmbAdminExchange,
  336. pVNetRootContext->pServerEntry,
  337. pVNetRootContext->pSessionEntry,
  338. pVNetRootContext->pNetRootEntry,
  339. SMB_COM_TREE_DISCONNECT);
  340. if (Status == STATUS_SUCCESS) {
  341. BOOLEAN fExchangeDiscarded = FALSE;
  342. pSmbAdminExchange->pSmbBuffer =
  343. (PCHAR) pSmbAdminExchange->Disconnect.DisconnectSmb + TRANSPORT_HEADER_SIZE;
  344. pSmbHeader = (PSMB_HEADER)pSmbAdminExchange->pSmbBuffer;
  345. pReqTreeDisconnect = (PREQ_TREE_DISCONNECT)(pSmbHeader + 1);
  346. // Build the header
  347. Status = SmbCeBuildSmbHeader(
  348. (PSMB_EXCHANGE)pSmbAdminExchange,
  349. pSmbAdminExchange->pSmbBuffer,
  350. sizeof(SMB_HEADER),
  351. &pSmbAdminExchange->SmbBufferLength,
  352. &LastCommandInHeader,
  353. &pCommand);
  354. if (Status == STATUS_SUCCESS) {
  355. ASSERT(LastCommandInHeader == SMB_COM_NO_ANDX_COMMAND);
  356. *pCommand = SMB_COM_TREE_DISCONNECT;
  357. pSmbHeader->Tid = pVNetRootContext->TreeId;
  358. pReqTreeDisconnect->WordCount = 0;
  359. SmbPutUshort(&pReqTreeDisconnect->ByteCount,0);
  360. pSmbAdminExchange->SmbBufferLength += FIELD_OFFSET(REQ_TREE_DISCONNECT,Buffer);
  361. Status = SmbCeInitiateExchange((PSMB_EXCHANGE)pSmbAdminExchange);
  362. if ((Status == STATUS_PENDING) ||
  363. (Status == STATUS_SUCCESS)) {
  364. // async completion will also discard the exchange
  365. fExchangeDiscarded = TRUE;
  366. }
  367. }
  368. if (!fExchangeDiscarded) {
  369. SmbCeDiscardAdminExchange(pSmbAdminExchange);
  370. }
  371. } else {
  372. SmbMmFreeExchange((PSMB_EXCHANGE)pSmbAdminExchange);
  373. }
  374. } else {
  375. Status = STATUS_INSUFFICIENT_RESOURCES;
  376. }
  377. return Status;
  378. }
  379. extern BOOLEAN MRxSmbEnableDownLevelLogOff;
  380. NTSTATUS
  381. SmbCeLogOff(
  382. PSMBCEDB_SERVER_ENTRY pServerEntry,
  383. PSMBCEDB_SESSION_ENTRY pSessionEntry)
  384. /*++
  385. Routine Description:
  386. This routine issues the logoff SMB for an existing session to the server
  387. Arguments:
  388. pServerEntry - the server entry
  389. pSessionEntry - the associated session entry
  390. Return Value:
  391. STATUS_SUCCESS - the logoff was successfully sent.
  392. Other Status codes correspond to error situations.
  393. Notes:
  394. --*/
  395. {
  396. NTSTATUS Status;
  397. PSMB_ADMIN_EXCHANGE pSmbAdminExchange;
  398. PSMB_HEADER pSmbHeader;
  399. PREQ_LOGOFF_ANDX pReqLogOffAndX;
  400. PAGED_CODE();
  401. if ((SmbCeGetServerType(pServerEntry) == SMBCEDB_MAILSLOT_SERVER) ||
  402. (pServerEntry->Server.SecurityMode == SECURITY_MODE_SHARE_LEVEL)) {
  403. if (pSessionEntry != NULL) {
  404. SmbCeDereferenceSessionEntry(pSessionEntry);
  405. }
  406. return STATUS_SUCCESS;
  407. }
  408. //
  409. // Some servers (like linux) don't really know how to handle session logoffs.
  410. // So, let's just be sure that we only do this to NT or better servers,
  411. // because we know that they handle it correctly. The version of Linux we have
  412. // seems to like to negotiate the NT dialect even though it really isn't NT. That's
  413. // why the extra check is put in here for NT status codes.
  414. //
  415. // Bypass this behavior based on the 'EnableDownLevelLogOff' registry key.
  416. //
  417. if( MRxSmbEnableDownLevelLogOff == FALSE ) {
  418. if( pServerEntry->Server.Dialect < NTLANMAN_DIALECT ||
  419. !FlagOn(pServerEntry->Server.DialectFlags,DF_NT_STATUS) ) {
  420. if (pSessionEntry != NULL) {
  421. SmbCeDereferenceSessionEntry(pSessionEntry);
  422. }
  423. return STATUS_SUCCESS;
  424. }
  425. }
  426. pSmbAdminExchange = (PSMB_ADMIN_EXCHANGE)SmbMmAllocateExchange(ADMIN_EXCHANGE,NULL);
  427. if (pSmbAdminExchange != NULL) {
  428. UCHAR LastCommandInHeader;
  429. PUCHAR pCommand;
  430. Status = SmbCeInitializeAdminExchange(
  431. pSmbAdminExchange,
  432. pServerEntry,
  433. pSessionEntry,
  434. NULL,
  435. SMB_COM_LOGOFF_ANDX);
  436. if (Status == STATUS_SUCCESS) {
  437. BOOLEAN fExchangeDiscarded = FALSE;
  438. pSmbAdminExchange->pSmbBuffer =
  439. (PCHAR) pSmbAdminExchange->LogOff.LogOffSmb + TRANSPORT_HEADER_SIZE;
  440. pSmbHeader = (PSMB_HEADER)pSmbAdminExchange->pSmbBuffer;
  441. pReqLogOffAndX = (PREQ_LOGOFF_ANDX)(pSmbHeader + 1);
  442. // Build the header
  443. Status = SmbCeBuildSmbHeader(
  444. (PSMB_EXCHANGE)pSmbAdminExchange,
  445. pSmbAdminExchange->pSmbBuffer,
  446. sizeof(SMB_HEADER),
  447. &pSmbAdminExchange->SmbBufferLength,
  448. &LastCommandInHeader,
  449. &pCommand);
  450. if (Status == STATUS_SUCCESS) {
  451. ASSERT(LastCommandInHeader == SMB_COM_NO_ANDX_COMMAND);
  452. *pCommand = SMB_COM_LOGOFF_ANDX;
  453. pReqLogOffAndX->WordCount = 2;
  454. pReqLogOffAndX->AndXCommand = SMB_COM_NO_ANDX_COMMAND;
  455. pReqLogOffAndX->AndXReserved = 0;
  456. SmbPutUshort(&pReqLogOffAndX->AndXOffset,0);
  457. SmbPutUshort(&pReqLogOffAndX->ByteCount,0);
  458. pSmbAdminExchange->SmbBufferLength += FIELD_OFFSET(REQ_LOGOFF_ANDX,Buffer);
  459. Status = SmbCeInitiateExchange((PSMB_EXCHANGE)pSmbAdminExchange);
  460. if ((Status == STATUS_PENDING) ||
  461. (Status == STATUS_SUCCESS)) {
  462. // async completion will discard the exchange
  463. fExchangeDiscarded = TRUE;
  464. }
  465. }
  466. if (!fExchangeDiscarded) {
  467. SmbCeDiscardAdminExchange(pSmbAdminExchange);
  468. }
  469. } else {
  470. SmbMmFreeExchange((PSMB_EXCHANGE)pSmbAdminExchange);
  471. }
  472. } else {
  473. Status = RX_MAP_STATUS(INSUFFICIENT_RESOURCES);
  474. }
  475. return Status;
  476. }
  477. NTSTATUS
  478. SmbCeInitializeAdminExchange(
  479. PSMB_ADMIN_EXCHANGE pSmbAdminExchange,
  480. PSMBCEDB_SERVER_ENTRY pServerEntry,
  481. PSMBCEDB_SESSION_ENTRY pSessionEntry,
  482. PSMBCEDB_NET_ROOT_ENTRY pNetRootEntry,
  483. UCHAR SmbCommand)
  484. /*++
  485. Routine Description:
  486. This routine initializes the ADMIN exchange
  487. Arguments:
  488. pSmbAdminExchange - the exchange
  489. pServerEntry - the associated server entry
  490. pSessionEntry - the associated session entry
  491. pNetRootEntry - the associated net root entry
  492. SmbCommand - the SMB command
  493. Return Value:
  494. STATUS_SUCCESS - the logoff was successfully sent.
  495. Other Status codes correspond to error situations.
  496. Notes:
  497. The ADMIN_EXCHANGE is a special type of exchange used for bootstrap/teardown
  498. situations in which the initialization of the exchange cannot follow the noraml
  499. course of events. In some cases not all the components required for proper
  500. initialization of the exchange are present, e.g., NEGOTIATE we do not have a
  501. valid session/tree connect. It is for this reason that the three important
  502. elements of initialization, i.e., Server/Session/NetRoot have to be explicitly
  503. specified. NULL is used to signify a dont care situation for a particular component.
  504. --*/
  505. {
  506. NTSTATUS Status;
  507. PAGED_CODE();
  508. Status = SmbCeIncrementActiveExchangeCount();
  509. if (Status == STATUS_SUCCESS) {
  510. PSMBCE_V_NET_ROOT_CONTEXT pVNetRootContext;
  511. pSmbAdminExchange->CancellationStatus = SMBCE_EXCHANGE_NOT_CANCELLED;
  512. if ((SmbCommand == SMB_COM_NEGOTIATE) ||
  513. (SmbCommand == SMB_COM_ECHO)) {
  514. pSmbAdminExchange->SmbCeContext.pServerEntry = pServerEntry;
  515. pSmbAdminExchange->SmbCeContext.pVNetRootContext = NULL;
  516. } else {
  517. pVNetRootContext = (PSMBCE_V_NET_ROOT_CONTEXT)
  518. RxAllocatePoolWithTag(
  519. NonPagedPool,
  520. sizeof(SMBCE_V_NET_ROOT_CONTEXT),
  521. MRXSMB_VNETROOT_POOLTAG);
  522. if (pVNetRootContext != NULL) {
  523. pVNetRootContext->pServerEntry = pServerEntry;
  524. pVNetRootContext->pSessionEntry = pSessionEntry;
  525. pVNetRootContext->pNetRootEntry = pNetRootEntry;
  526. pSmbAdminExchange->SmbCeContext.pVNetRootContext = pVNetRootContext;
  527. pSmbAdminExchange->SmbCeContext.pServerEntry = pServerEntry;
  528. } else {
  529. Status = STATUS_INSUFFICIENT_RESOURCES;
  530. }
  531. }
  532. if (Status == STATUS_SUCCESS) {
  533. SmbCeReferenceServerEntry(pServerEntry);
  534. pSmbAdminExchange->pSmbMdl = NULL;
  535. pSmbAdminExchange->pSmbBuffer = NULL;
  536. pSmbAdminExchange->SmbBufferLength = 0;
  537. // Set the SmbCe state to overrule the common method of having to hunt
  538. // up a valid TID/FID etc. and reconnects.
  539. pSmbAdminExchange->SmbCommand = SmbCommand;
  540. pSmbAdminExchange->SmbCeState = SMBCE_EXCHANGE_NETROOT_INITIALIZED;
  541. switch (pSmbAdminExchange->SmbCommand) {
  542. case SMB_COM_NEGOTIATE:
  543. {
  544. pSmbAdminExchange->Negotiate.DomainName.Length = 0;
  545. pSmbAdminExchange->Negotiate.DomainName.MaximumLength = 0;
  546. pSmbAdminExchange->Negotiate.DomainName.Buffer = NULL;
  547. pSmbAdminExchange->Negotiate.pSecurityBlobMdl = NULL;
  548. }
  549. break;
  550. case SMB_COM_TREE_DISCONNECT:
  551. case SMB_COM_LOGOFF_ANDX:
  552. break;
  553. case SMB_COM_ECHO:
  554. {
  555. pSmbAdminExchange->pDispatchVector = &EchoExchangeDispatch;
  556. pSmbAdminExchange->EchoProbe.pEchoProbeMdl = NULL;
  557. pSmbAdminExchange->EchoProbe.EchoProbeLength = 0;
  558. }
  559. break;
  560. default:
  561. ASSERT(!"Valid Command for Admin Exchange");
  562. break;
  563. }
  564. SmbCeAcquireResource();
  565. if ((pSessionEntry != NULL) &&
  566. pServerEntry->SecuritySignaturesEnabled &&
  567. !pServerEntry->SecuritySignaturesActive) {
  568. // if security signature is enabled and not yet turned on, exchange should wait for
  569. // outstanding extended session setup to finish before resume in order to avoid index mismatch.
  570. Status = SmbCeSyncExchangeForSecuritySignature((PSMB_EXCHANGE)pSmbAdminExchange);
  571. ASSERT(Status != STATUS_PENDING);
  572. }
  573. SmbCeReleaseResource();
  574. if (Status != STATUS_SUCCESS) {
  575. SmbCeDereferenceServerEntry(pServerEntry);
  576. RxFreePool(pSmbAdminExchange->SmbCeContext.pVNetRootContext);
  577. pSmbAdminExchange->SmbCeContext.pVNetRootContext = NULL;
  578. }
  579. }
  580. if(Status != STATUS_SUCCESS) {
  581. SmbCeDecrementActiveExchangeCount();
  582. }
  583. }
  584. return Status;
  585. }
  586. VOID
  587. SmbCeDiscardAdminExchange(
  588. PSMB_ADMIN_EXCHANGE pSmbAdminExchange)
  589. /*++
  590. Routine Description:
  591. This routine discards the ADMIN exchange
  592. Arguments:
  593. pSmbAdminExchange - the exchange
  594. --*/
  595. {
  596. PSMBCEDB_SERVER_ENTRY pServerEntry;
  597. PSMBCEDB_SESSION_ENTRY pSessionEntry;
  598. PSMBCEDB_NET_ROOT_ENTRY pNetRootEntry;
  599. PSMBCE_V_NET_ROOT_CONTEXT pVNetRootContext;
  600. PAGED_CODE();
  601. SmbCeAcquireResource();
  602. RemoveEntryList(&pSmbAdminExchange->ExchangeList);
  603. SmbCeReleaseResource();
  604. pServerEntry = SmbCeGetExchangeServerEntry(pSmbAdminExchange);
  605. pSessionEntry = SmbCeGetExchangeSessionEntry(pSmbAdminExchange);
  606. pNetRootEntry = SmbCeGetExchangeNetRootEntry(pSmbAdminExchange);
  607. pVNetRootContext = SmbCeGetExchangeVNetRootContext(pSmbAdminExchange);
  608. if (pSmbAdminExchange->pSmbMdl != NULL) {
  609. RxUnlockHeaderPages(pSmbAdminExchange->pSmbMdl);
  610. IoFreeMdl(pSmbAdminExchange->pSmbMdl);
  611. }
  612. switch (pSmbAdminExchange->SmbCommand) {
  613. case SMB_COM_NEGOTIATE:
  614. {
  615. pSmbAdminExchange->pSmbBuffer = NULL;
  616. if (pSmbAdminExchange->Negotiate.DomainName.Buffer != NULL) {
  617. RxFreePool(
  618. pSmbAdminExchange->Negotiate.DomainName.Buffer);
  619. }
  620. if (pSmbAdminExchange->Negotiate.pSecurityBlobMdl != NULL) {
  621. IoFreeMdl(
  622. pSmbAdminExchange->Negotiate.pSecurityBlobMdl);
  623. }
  624. }
  625. break;
  626. case SMB_COM_TREE_DISCONNECT:
  627. break;
  628. case SMB_COM_LOGOFF_ANDX:
  629. SmbCeDereferenceSessionEntry(pSessionEntry);
  630. break;
  631. case SMB_COM_ECHO:
  632. {
  633. if (pSmbAdminExchange->EchoProbe.pEchoProbeMdl != NULL) {
  634. MmPrepareMdlForReuse(pSmbAdminExchange->EchoProbe.pEchoProbeMdl);
  635. RxFreePool(pSmbAdminExchange->EchoProbe.pEchoProbeMdl);
  636. }
  637. }
  638. break;
  639. default:
  640. ASSERT(!"Valid Command For Admin Exchange");
  641. break;
  642. }
  643. // Tear down all the copy data requests associated with this exchange
  644. SmbCePurgeBuffersAssociatedWithExchange(pServerEntry,(PSMB_EXCHANGE)pSmbAdminExchange);
  645. SmbCeUninitializeExchangeTransport((PSMB_EXCHANGE)pSmbAdminExchange);
  646. SmbCeDereferenceServerEntry(pServerEntry);
  647. if (pVNetRootContext != NULL) {
  648. RxFreePool(pVNetRootContext);
  649. }
  650. SmbMmFreeExchange((PSMB_EXCHANGE)pSmbAdminExchange);
  651. SmbCeDecrementActiveExchangeCount();
  652. }
  653. NTSTATUS
  654. SmbCeCompleteAdminExchange(
  655. PSMB_ADMIN_EXCHANGE pSmbAdminExchange)
  656. /*++
  657. Routine Description:
  658. This is the routine used for completing the SMB ADMIN exchanges.
  659. Arguments:
  660. pExchange - the exchange instance
  661. Return Value:
  662. RXSTATUS - The return status for the operation
  663. Notes:
  664. This routine encapsulates the TAIL for all SMB admin exchanges. They carry
  665. out the local action required based upon the outcome of the exchange.
  666. --*/
  667. {
  668. NTSTATUS Status = STATUS_SUCCESS;
  669. PSMBCEDB_SERVER_ENTRY pServerEntry;
  670. SMBCEDB_OBJECT_STATE ServerState;
  671. PAGED_CODE();
  672. pServerEntry = SmbCeGetExchangeServerEntry(pSmbAdminExchange);
  673. switch (pSmbAdminExchange->SmbCommand) {
  674. case SMB_COM_NEGOTIATE:
  675. {
  676. if (pSmbAdminExchange->Status != STATUS_SUCCESS) {
  677. pServerEntry->ServerStatus = pSmbAdminExchange->Status;
  678. }
  679. if (pServerEntry->ServerStatus == STATUS_SUCCESS) {
  680. if (!FlagOn(pServerEntry->Server.DialectFlags,DF_EXTENDED_SECURITY)) {
  681. if (pServerEntry->DomainName.Buffer) {
  682. RxFreePool(pServerEntry->DomainName.Buffer);
  683. pServerEntry->DomainName.Buffer = NULL;
  684. }
  685. pServerEntry->DomainName.Length = pSmbAdminExchange->Negotiate.DomainName.Length;
  686. pServerEntry->DomainName.MaximumLength = pServerEntry->DomainName.Length;
  687. if (pServerEntry->DomainName.Length > 0) {
  688. pServerEntry->DomainName.Buffer = RxAllocatePoolWithTag(
  689. NonPagedPool,
  690. pServerEntry->DomainName.Length,
  691. MRXSMB_SERVER_POOLTAG);
  692. }
  693. if (pServerEntry->DomainName.Buffer != NULL) {
  694. // Copy the domain name into the server entry
  695. RtlCopyMemory(
  696. pServerEntry->DomainName.Buffer,
  697. pSmbAdminExchange->Negotiate.DomainName.Buffer,
  698. pServerEntry->DomainName.Length);
  699. } else {
  700. //The downlevel server doesn't have a domain name. It's not a problem if the
  701. //DomainName.Buffer equals to NULL.
  702. if (pServerEntry->DomainName.Length > 0) {
  703. pServerEntry->DomainName.Length = 0;
  704. pServerEntry->DomainName.MaximumLength = 0;
  705. pServerEntry->ServerStatus = STATUS_INSUFFICIENT_RESOURCES;
  706. }
  707. }
  708. }
  709. if (pServerEntry->ServerStatus == STATUS_SUCCESS) {
  710. pServerEntry->ServerStatus = SmbCeUpdateSrvCall(pServerEntry);
  711. }
  712. }
  713. Status = pServerEntry->ServerStatus;
  714. if (pServerEntry->ServerStatus == STATUS_SUCCESS) {
  715. pServerEntry->Server.EchoProbeState = ECHO_PROBE_IDLE;
  716. }
  717. }
  718. break;
  719. case SMB_COM_TREE_DISCONNECT:
  720. case SMB_COM_LOGOFF_ANDX:
  721. case SMB_COM_ECHO:
  722. default:
  723. break;
  724. }
  725. SmbCeDiscardAdminExchange(pSmbAdminExchange);
  726. return Status;
  727. }
  728. NTSTATUS
  729. SmbAdminExchangeStart(
  730. PSMB_EXCHANGE pExchange)
  731. /*++
  732. Routine Description:
  733. This is the start routine for administrative SMB exchanges.
  734. Arguments:
  735. pExchange - the exchange instance
  736. Return Value:
  737. RXSTATUS - The return status for the operation
  738. --*/
  739. {
  740. NTSTATUS Status;
  741. PSMB_ADMIN_EXCHANGE pSmbAdminExchange;
  742. PAGED_CODE();
  743. pSmbAdminExchange = (PSMB_ADMIN_EXCHANGE)pExchange;
  744. switch (pSmbAdminExchange->SmbCommand) {
  745. case SMB_COM_NEGOTIATE:
  746. case SMB_COM_LOGOFF_ANDX:
  747. case SMB_COM_TREE_DISCONNECT:
  748. {
  749. ASSERT(pSmbAdminExchange->pSmbMdl == NULL);
  750. RxAllocateHeaderMdl(
  751. pSmbAdminExchange->pSmbBuffer,
  752. pSmbAdminExchange->SmbBufferLength,
  753. pSmbAdminExchange->pSmbMdl
  754. );
  755. if (pSmbAdminExchange->pSmbMdl != NULL) {
  756. RxProbeAndLockHeaderPages(
  757. pSmbAdminExchange->pSmbMdl,
  758. KernelMode,
  759. IoModifyAccess,
  760. Status);
  761. if (Status == STATUS_SUCCESS) {
  762. Status = SmbCeTranceive(
  763. pExchange,
  764. RXCE_SEND_SYNCHRONOUS,
  765. pSmbAdminExchange->pSmbMdl,
  766. pSmbAdminExchange->SmbBufferLength);
  767. RxDbgTrace( 0, Dbg, ("Net Root SmbCeTranceive returned %lx\n",Status));
  768. } else {
  769. IoFreeMdl(pSmbAdminExchange->pSmbMdl);
  770. pSmbAdminExchange->pSmbMdl = NULL;
  771. }
  772. } else {
  773. Status = RX_MAP_STATUS(INSUFFICIENT_RESOURCES);
  774. }
  775. }
  776. break;
  777. case SMB_COM_ECHO:
  778. {
  779. Status = SmbCeSend(
  780. pExchange,
  781. 0,
  782. pSmbAdminExchange->EchoProbe.pEchoProbeMdl,
  783. pSmbAdminExchange->EchoProbe.EchoProbeLength);
  784. }
  785. break;
  786. default:
  787. Status = STATUS_UNSUCCESSFUL;
  788. break;
  789. }
  790. return Status;
  791. }
  792. NTSTATUS
  793. SmbAdminExchangeReceive(
  794. IN struct _SMB_EXCHANGE *pExchange, // The exchange instance
  795. IN ULONG BytesIndicated,
  796. IN ULONG BytesAvailable,
  797. OUT ULONG *pBytesTaken,
  798. IN PSMB_HEADER pSmbHeader,
  799. OUT PMDL *pDataBufferPointer,
  800. OUT PULONG pDataSize,
  801. IN ULONG ReceiveFlags)
  802. /*++
  803. Routine Description:
  804. This is the recieve indication handling routine for net root construction exchanges
  805. Arguments:
  806. pExchange - the exchange instance
  807. BytesIndicated - the number of bytes indicated
  808. Bytes Available - the number of bytes available
  809. pBytesTaken - the number of bytes consumed
  810. pSmbHeader - the byte buffer
  811. pDataBufferPointer - the buffer into which the remaining data is to be copied.
  812. pDataSize - the buffer size.
  813. Return Value:
  814. RXSTATUS - The return status for the operation
  815. Notes:
  816. This routine is called at DPC level.
  817. --*/
  818. {
  819. NTSTATUS Status;
  820. PSMB_ADMIN_EXCHANGE pSmbAdminExchange;
  821. RxDbgTrace( 0, (DEBUG_TRACE_ALWAYS), ("ParseSmbHeader BytesIndicated/Available %ld %ld\n",BytesIndicated,BytesAvailable));
  822. pSmbAdminExchange = (PSMB_ADMIN_EXCHANGE)pExchange;
  823. switch (pSmbAdminExchange->SmbCommand) {
  824. case SMB_COM_NEGOTIATE:
  825. {
  826. Status = ParseNegotiateResponse(
  827. pSmbAdminExchange,
  828. BytesIndicated,
  829. BytesAvailable,
  830. pBytesTaken,
  831. pSmbHeader,
  832. pDataBufferPointer,
  833. pDataSize);
  834. if (Status == STATUS_MORE_PROCESSING_REQUIRED) {
  835. if (*pDataBufferPointer != NULL &&
  836. ((*pBytesTaken + *pDataSize) <= BytesAvailable ||
  837. !FlagOn(ReceiveFlags,TDI_RECEIVE_ENTIRE_MESSAGE))) {
  838. pSmbAdminExchange->Negotiate.pSecurityBlobMdl = *pDataBufferPointer;
  839. } else {
  840. *pBytesTaken = BytesAvailable;
  841. Status = STATUS_SUCCESS;
  842. pExchange->Status = STATUS_INVALID_NETWORK_RESPONSE;
  843. }
  844. }
  845. }
  846. break;
  847. case SMB_COM_TREE_DISCONNECT:
  848. case SMB_COM_LOGOFF_ANDX:
  849. {
  850. *pBytesTaken = BytesAvailable;
  851. Status = STATUS_SUCCESS;
  852. }
  853. break;
  854. case SMB_COM_ECHO:
  855. // Since the echo probe responses are handled by the receive indication routine
  856. // at DPC level this routine should never be called for echo probes.
  857. default:
  858. {
  859. *pBytesTaken = 0;
  860. Status = STATUS_DATA_NOT_ACCEPTED;
  861. }
  862. break;
  863. }
  864. return Status;
  865. }
  866. NTSTATUS
  867. SmbAdminExchangeCopyDataHandler(
  868. IN PSMB_EXCHANGE pExchange, // The exchange instance
  869. IN PMDL pCopyDataBuffer,
  870. IN ULONG DataSize)
  871. /*++
  872. Routine Description:
  873. This is the copy data handling routine for administrative SMB exchanges
  874. Arguments:
  875. pExchange - the exchange instance
  876. Return Value:
  877. RXSTATUS - The return status for the operation
  878. --*/
  879. {
  880. UNREFERENCED_PARAMETER(pExchange);
  881. return STATUS_SUCCESS;
  882. }
  883. NTSTATUS
  884. SmbAdminExchangeSendCallbackHandler(
  885. IN PSMB_EXCHANGE pExchange,
  886. IN PMDL pXmitBuffer,
  887. IN NTSTATUS SendCompletionStatus)
  888. /*++
  889. Routine Description:
  890. This is the send call back indication handling routine for transact exchanges
  891. Arguments:
  892. pExchange - the exchange instance
  893. Return Value:
  894. NTSTATUS - The return status for the operation
  895. --*/
  896. {
  897. return STATUS_SUCCESS;
  898. UNREFERENCED_PARAMETER(pExchange);
  899. UNREFERENCED_PARAMETER(pXmitBuffer);
  900. UNREFERENCED_PARAMETER(SendCompletionStatus);
  901. }
  902. NTSTATUS
  903. SmbAdminExchangeFinalize(
  904. PSMB_EXCHANGE pExchange,
  905. BOOLEAN *pPostFinalize)
  906. /*++
  907. Routine Description:
  908. This routine finalkzes the construct net root exchange. It resumes the RDBSS by invoking
  909. the call back and discards the exchange
  910. Arguments:
  911. pExchange - the exchange instance
  912. CurrentIrql - the current interrupt request level
  913. pPostFinalize - a pointer to a BOOLEAN if the request should be posted
  914. Return Value:
  915. RXSTATUS - The return status for the operation
  916. --*/
  917. {
  918. PSMB_ADMIN_EXCHANGE pSmbAdminExchange = (PSMB_ADMIN_EXCHANGE)pExchange;
  919. if (pSmbAdminExchange->pResumptionContext != NULL) {
  920. // Signal the event
  921. *pPostFinalize = FALSE;
  922. SmbCeResume(pSmbAdminExchange->pResumptionContext);
  923. } else {
  924. if (RxShouldPostCompletion()) {
  925. *pPostFinalize = TRUE;
  926. return STATUS_SUCCESS;
  927. } else {
  928. *pPostFinalize = FALSE;
  929. SmbCeCompleteAdminExchange(pSmbAdminExchange);
  930. }
  931. }
  932. return STATUS_SUCCESS;
  933. }
  934. SMB_EXCHANGE_DISPATCH_VECTOR
  935. AdminExchangeDispatch = {
  936. SmbAdminExchangeStart,
  937. SmbAdminExchangeReceive,
  938. SmbAdminExchangeCopyDataHandler,
  939. NULL, // No Send Completion handler
  940. SmbAdminExchangeFinalize
  941. };
  942. SMB_EXCHANGE_DISPATCH_VECTOR
  943. EchoExchangeDispatch = {
  944. SmbAdminExchangeStart,
  945. SmbAdminExchangeReceive,
  946. SmbAdminExchangeCopyDataHandler,
  947. SmbAdminExchangeSendCallbackHandler,
  948. SmbAdminExchangeFinalize
  949. };
  950.