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.

2037 lines
53 KiB

  1. /*++
  2. Copyright (c) 1991-1992 Microsoft Corporation
  3. Module Name:
  4. wsdevice.c
  5. Abstract:
  6. This module contains the support routines for the APIs that call
  7. into the redirector or the datagram receiver.
  8. Author:
  9. Rita Wong (ritaw) 20-Feb-1991
  10. Revision History:
  11. --*/
  12. #include "wsutil.h"
  13. #include "wsconfig.h"
  14. #include "wsdevice.h"
  15. #include "wsbind.h"
  16. #include <lmerrlog.h> // Eventlog message IDs
  17. #include "winreg.h" // registry API's
  18. #include <prefix.h> // PREFIX_ equates.
  19. //
  20. // Buffer allocation size for enumeration output buffer.
  21. //
  22. #define INITIAL_ALLOCATION_SIZE 4096 // First attempt size
  23. #define FUDGE_FACTOR_SIZE 1024 // Second try TotalBytesNeeded
  24. // plus this amount
  25. #define CSC_WAIT_TIME 15 * 1000 // give agent 15 seconds to stop CSC
  26. //-------------------------------------------------------------------//
  27. // //
  28. // Local Function Prototypes //
  29. // //
  30. //-------------------------------------------------------------------//
  31. STATIC
  32. NET_API_STATUS
  33. WsOpenRedirector(
  34. VOID
  35. );
  36. STATIC
  37. NET_API_STATUS
  38. WsOpenDgReceiver (
  39. VOID
  40. );
  41. HANDLE
  42. CreateNamedEvent(
  43. LPWSTR lpwEventName
  44. );
  45. BOOL
  46. AgentIsAlive(
  47. VOID
  48. );
  49. //-------------------------------------------------------------------//
  50. // //
  51. // Global variables //
  52. // //
  53. //-------------------------------------------------------------------//
  54. //
  55. // Handle to the Redirector FSD
  56. //
  57. HANDLE WsRedirDeviceHandle = NULL;
  58. HANDLE WsRedirAsyncDeviceHandle = NULL;
  59. BOOLEAN LoadedMRxSmbInsteadOfRdr = FALSE;
  60. //
  61. // Handle to the Datagram Receiver DD
  62. //
  63. HANDLE WsDgReceiverDeviceHandle = NULL;
  64. HANDLE WsDgrecAsyncDeviceHandle = NULL;
  65. HANDLE heventWkssvcToAgentStart = NULL;
  66. HANDLE heventWkssvcToAgentStop = NULL;
  67. HANDLE heventAgentToWkssvc = NULL;
  68. BOOL vfRedirStarted = FALSE;
  69. static WCHAR wszWkssvcToAgentStartEvent[] = L"WkssvcToAgentStartEvent";
  70. static WCHAR wszWkssvcToAgentStopEvent[] = L"WkssvcToAgentStopEvent";
  71. static WCHAR wszAgentToWkssvcEvent[] = L"AgentToWkssvcEvent";
  72. static WCHAR wzAgentExistsEvent[] = L"AgentExistsEvent"; // used to detect if agent exists
  73. NET_API_STATUS
  74. WsDeviceControlGetInfo(
  75. IN DDTYPE DeviceDriverType,
  76. IN HANDLE FileHandle,
  77. IN ULONG DeviceControlCode,
  78. IN PVOID RequestPacket,
  79. IN ULONG RequestPacketLength,
  80. OUT LPBYTE *OutputBuffer,
  81. IN ULONG PreferedMaximumLength,
  82. IN ULONG BufferHintSize,
  83. OUT PULONG_PTR Information OPTIONAL
  84. )
  85. /*++
  86. Routine Description:
  87. This function allocates the buffer and fill it with the information
  88. that is retrieved from the redirector or datagram receiver.
  89. Arguments:
  90. DeviceDriverType - Supplies the value which indicates whether to call
  91. the redirector or the datagram receiver.
  92. FileHandle - Supplies a handle to the file or device of which to get
  93. information about.
  94. DeviceControlCode - Supplies the NtFsControlFile or NtIoDeviceControlFile
  95. function control code.
  96. RequestPacket - Supplies a pointer to the device request packet.
  97. RrequestPacketLength - Supplies the length of the device request packet.
  98. OutputBuffer - Returns a pointer to the buffer allocated by this routine
  99. which contains the use information requested. This pointer is set to
  100. NULL if return code is not NERR_Success.
  101. PreferedMaximumLength - Supplies the number of bytes of information to
  102. return in the buffer. If this value is MAXULONG, we will try to
  103. return all available information if there is enough memory resource.
  104. BufferHintSize - Supplies the hint size of the output buffer so that the
  105. memory allocated for the initial buffer will most likely be large
  106. enough to hold all requested data.
  107. Information - Returns the information code from the NtFsControlFile or
  108. NtIoDeviceControlFile call.
  109. Return Value:
  110. NET_API_STATUS - NERR_Success or reason for failure.
  111. --*/
  112. {
  113. NET_API_STATUS status;
  114. DWORD OutputBufferLength;
  115. DWORD TotalBytesNeeded = 1;
  116. ULONG OriginalResumeKey;
  117. //
  118. // If PreferedMaximumLength is MAXULONG, then we are supposed to get all
  119. // the information, regardless of size. Allocate the output buffer of a
  120. // reasonable size and try to use it. If this fails, the Redirector FSD
  121. // will say how much we need to allocate.
  122. //
  123. if (PreferedMaximumLength == MAXULONG) {
  124. OutputBufferLength = (BufferHintSize) ?
  125. BufferHintSize :
  126. INITIAL_ALLOCATION_SIZE;
  127. }
  128. else {
  129. OutputBufferLength = PreferedMaximumLength;
  130. }
  131. OutputBufferLength = ROUND_UP_COUNT(OutputBufferLength, ALIGN_WCHAR);
  132. if ((*OutputBuffer = MIDL_user_allocate(OutputBufferLength)) == NULL) {
  133. return ERROR_NOT_ENOUGH_MEMORY;
  134. }
  135. RtlZeroMemory((PVOID) *OutputBuffer, OutputBufferLength);
  136. if (DeviceDriverType == Redirector) {
  137. PLMR_REQUEST_PACKET Rrp = (PLMR_REQUEST_PACKET) RequestPacket;
  138. OriginalResumeKey = Rrp->Parameters.Get.ResumeHandle;
  139. //
  140. // Make the request of the Redirector
  141. //
  142. status = WsRedirFsControl(
  143. FileHandle,
  144. DeviceControlCode,
  145. Rrp,
  146. RequestPacketLength,
  147. *OutputBuffer,
  148. OutputBufferLength,
  149. Information
  150. );
  151. if (status == ERROR_MORE_DATA) {
  152. TotalBytesNeeded = Rrp->Parameters.Get.TotalBytesNeeded;
  153. }
  154. }
  155. else {
  156. PLMDR_REQUEST_PACKET Drrp = (PLMDR_REQUEST_PACKET) RequestPacket;
  157. OriginalResumeKey = Drrp->Parameters.EnumerateNames.ResumeHandle;
  158. //
  159. // Make the request of the Datagram Receiver
  160. //
  161. status = WsDgReceiverIoControl(
  162. FileHandle,
  163. DeviceControlCode,
  164. Drrp,
  165. RequestPacketLength,
  166. *OutputBuffer,
  167. OutputBufferLength,
  168. NULL
  169. );
  170. if (status == ERROR_MORE_DATA) {
  171. NetpAssert(
  172. FIELD_OFFSET(
  173. LMDR_REQUEST_PACKET,
  174. Parameters.EnumerateNames.TotalBytesNeeded
  175. ) ==
  176. FIELD_OFFSET(
  177. LMDR_REQUEST_PACKET,
  178. Parameters.EnumerateServers.TotalBytesNeeded
  179. )
  180. );
  181. TotalBytesNeeded = Drrp->Parameters.EnumerateNames.TotalBytesNeeded;
  182. }
  183. }
  184. if ((TotalBytesNeeded > OutputBufferLength) &&
  185. (PreferedMaximumLength == MAXULONG)) {
  186. //
  187. // Initial output buffer allocated was too small and we need to return
  188. // all data. First free the output buffer before allocating the
  189. // required size plus a fudge factor just in case the amount of data
  190. // grew.
  191. //
  192. MIDL_user_free(*OutputBuffer);
  193. OutputBufferLength =
  194. ROUND_UP_COUNT((TotalBytesNeeded + FUDGE_FACTOR_SIZE),
  195. ALIGN_WCHAR);
  196. if ((*OutputBuffer = MIDL_user_allocate(OutputBufferLength)) == NULL) {
  197. return ERROR_NOT_ENOUGH_MEMORY;
  198. }
  199. RtlZeroMemory((PVOID) *OutputBuffer, OutputBufferLength);
  200. //
  201. // Try again to get the information from the redirector or datagram
  202. // receiver
  203. //
  204. if (DeviceDriverType == Redirector) {
  205. PLMR_REQUEST_PACKET Rrp = (PLMR_REQUEST_PACKET) RequestPacket;
  206. Rrp->Parameters.Get.ResumeHandle = OriginalResumeKey;
  207. //
  208. // Make the request of the Redirector
  209. //
  210. status = WsRedirFsControl(
  211. FileHandle,
  212. DeviceControlCode,
  213. Rrp,
  214. RequestPacketLength,
  215. *OutputBuffer,
  216. OutputBufferLength,
  217. Information
  218. );
  219. }
  220. else {
  221. PLMDR_REQUEST_PACKET Drrp = (PLMDR_REQUEST_PACKET) RequestPacket;
  222. NetpAssert(
  223. FIELD_OFFSET(
  224. LMDR_REQUEST_PACKET,
  225. Parameters.EnumerateNames.ResumeHandle
  226. ) ==
  227. FIELD_OFFSET(
  228. LMDR_REQUEST_PACKET,
  229. Parameters.EnumerateServers.ResumeHandle
  230. )
  231. );
  232. Drrp->Parameters.EnumerateNames.ResumeHandle = OriginalResumeKey;
  233. //
  234. // Make the request of the Datagram Receiver
  235. //
  236. status = WsDgReceiverIoControl(
  237. FileHandle,
  238. DeviceControlCode,
  239. Drrp,
  240. RequestPacketLength,
  241. *OutputBuffer,
  242. OutputBufferLength,
  243. NULL
  244. );
  245. }
  246. }
  247. //
  248. // If not successful in getting any data, or if the caller asked for
  249. // all available data with PreferedMaximumLength == MAXULONG and
  250. // our buffer overflowed, free the output buffer and set its pointer
  251. // to NULL.
  252. //
  253. if ((status != NERR_Success && status != ERROR_MORE_DATA) ||
  254. (TotalBytesNeeded == 0) ||
  255. (PreferedMaximumLength == MAXULONG && status == ERROR_MORE_DATA)) {
  256. MIDL_user_free(*OutputBuffer);
  257. *OutputBuffer = NULL;
  258. //
  259. // PreferedMaximumLength == MAXULONG and buffer overflowed means
  260. // we do not have enough memory to satisfy the request.
  261. //
  262. if (status == ERROR_MORE_DATA) {
  263. status = ERROR_NOT_ENOUGH_MEMORY;
  264. }
  265. }
  266. return status;
  267. }
  268. NET_API_STATUS
  269. WsInitializeRedirector(
  270. VOID
  271. )
  272. /*++
  273. Routine Description:
  274. This routine opens the NT LAN Man redirector. It then reads in
  275. the configuration information and initializes to redirector.
  276. Arguments:
  277. None.
  278. Return Value:
  279. NET_API_STATUS - NERR_Success or reason for failure.
  280. --*/
  281. {
  282. NET_API_STATUS status;
  283. status = WsLoadRedirector();
  284. if (status != NERR_Success && status != ERROR_SERVICE_ALREADY_RUNNING) {
  285. DbgPrint("WKSSVC Load redirector returned %lx\n", status);
  286. return status;
  287. }
  288. //
  289. // Open redirector FSD to get handle to it
  290. //
  291. if ((status = WsOpenRedirector()) != NERR_Success) {
  292. DbgPrint("WKSSVC Open redirector returned %lx\n", status);
  293. return status;
  294. }
  295. // if ((status = WsLoadDriver(L"DGRcvr")) != NERR_Success) {
  296. // return status;
  297. // }
  298. //
  299. // Open datagram receiver FSD to get handle to it.
  300. //
  301. if ((status = WsOpenDgReceiver()) != NERR_Success) {
  302. DbgPrint("WKSSVC Open datagram receiver returned %lx\n", status);
  303. return status;
  304. }
  305. //
  306. // Load redirector and datagram receiver configuration
  307. //
  308. if ((status = WsGetWorkstationConfiguration()) != NERR_Success) {
  309. DbgPrint("WKSSVC Get workstation configuration returned %lx\n", status);
  310. DbgPrint("WKSSVC Shut down the redirector\n");
  311. (void) WsShutdownRedirector();
  312. return status;
  313. }
  314. IF_DEBUG(START) {
  315. DbgPrint("WKSSVC Get workstation configuration returned %lx\n", status);
  316. }
  317. return NERR_Success;
  318. }
  319. STATIC
  320. NET_API_STATUS
  321. WsOpenRedirector(
  322. VOID
  323. )
  324. /*++
  325. Routine Description:
  326. This routine opens the NT LAN Man redirector FSD.
  327. Arguments:
  328. None.
  329. Return Value:
  330. NET_API_STATUS - NERR_Success or reason for failure.
  331. --*/
  332. {
  333. NTSTATUS ntstatus;
  334. UNICODE_STRING DeviceName;
  335. IO_STATUS_BLOCK IoStatusBlock;
  336. OBJECT_ATTRIBUTES ObjectAttributes;
  337. //
  338. // Open the redirector device.
  339. //
  340. RtlInitUnicodeString(&DeviceName,DD_NFS_DEVICE_NAME_U);
  341. InitializeObjectAttributes(
  342. &ObjectAttributes,
  343. &DeviceName,
  344. OBJ_CASE_INSENSITIVE,
  345. NULL,
  346. NULL
  347. );
  348. ntstatus = NtOpenFile(
  349. &WsRedirDeviceHandle,
  350. SYNCHRONIZE,
  351. &ObjectAttributes,
  352. &IoStatusBlock,
  353. FILE_SHARE_VALID_FLAGS,
  354. FILE_SYNCHRONOUS_IO_NONALERT
  355. );
  356. if (NT_SUCCESS(ntstatus)) {
  357. ntstatus = IoStatusBlock.Status;
  358. }
  359. if (! NT_SUCCESS(ntstatus)) {
  360. NetpKdPrint(("[Wksta] NtOpenFile redirector failed: 0x%08lx\n",
  361. ntstatus));
  362. WsRedirDeviceHandle = NULL;
  363. return NetpNtStatusToApiStatus( ntstatus);
  364. }
  365. ntstatus = NtOpenFile(
  366. &WsRedirAsyncDeviceHandle,
  367. FILE_READ_DATA | FILE_WRITE_DATA,
  368. &ObjectAttributes,
  369. &IoStatusBlock,
  370. FILE_SHARE_VALID_FLAGS,
  371. 0L
  372. );
  373. if (NT_SUCCESS(ntstatus)) {
  374. ntstatus = IoStatusBlock.Status;
  375. }
  376. if (! NT_SUCCESS(ntstatus)) {
  377. NetpKdPrint(("[Wksta] NtOpenFile redirector ASYNC failed: 0x%08lx\n",
  378. ntstatus));
  379. WsRedirAsyncDeviceHandle = NULL;
  380. }
  381. return NetpNtStatusToApiStatus(ntstatus);
  382. }
  383. STATIC
  384. NET_API_STATUS
  385. WsOpenDgReceiver(
  386. VOID
  387. )
  388. /*++
  389. Routine Description:
  390. This routine opens the NT LAN Man Datagram Receiver driver.
  391. Arguments:
  392. None.
  393. Return Value:
  394. NET_API_STATUS - NERR_Success or reason for failure.
  395. --*/
  396. {
  397. NTSTATUS ntstatus;
  398. UNICODE_STRING DeviceName;
  399. IO_STATUS_BLOCK IoStatusBlock;
  400. OBJECT_ATTRIBUTES ObjectAttributes;
  401. RtlInitUnicodeString( &DeviceName, DD_BROWSER_DEVICE_NAME_U);
  402. InitializeObjectAttributes(
  403. &ObjectAttributes,
  404. &DeviceName,
  405. OBJ_CASE_INSENSITIVE,
  406. NULL,
  407. NULL
  408. );
  409. if (WsDgReceiverDeviceHandle == NULL) {
  410. //
  411. // Open the BOSWER device. The check is based on the fact that Services process
  412. // does not actually unload the driver when the service is stopped.
  413. //
  414. ntstatus = NtOpenFile(
  415. &WsDgReceiverDeviceHandle,
  416. SYNCHRONIZE,
  417. &ObjectAttributes,
  418. &IoStatusBlock,
  419. FILE_SHARE_VALID_FLAGS,
  420. FILE_SYNCHRONOUS_IO_NONALERT
  421. );
  422. if (NT_SUCCESS(ntstatus)) {
  423. ntstatus = IoStatusBlock.Status;
  424. }
  425. if (! NT_SUCCESS(ntstatus)) {
  426. NetpKdPrint(("[Wksta] NtOpenFile datagram receiver failed: 0x%08lx\n",
  427. ntstatus));
  428. WsDgReceiverDeviceHandle = NULL;
  429. return NetpNtStatusToApiStatus(ntstatus);
  430. }
  431. }
  432. ntstatus = NtOpenFile(
  433. &WsDgrecAsyncDeviceHandle,
  434. FILE_READ_DATA | FILE_WRITE_DATA,
  435. &ObjectAttributes,
  436. &IoStatusBlock,
  437. FILE_SHARE_VALID_FLAGS,
  438. 0L
  439. );
  440. if (NT_SUCCESS(ntstatus)) {
  441. ntstatus = IoStatusBlock.Status;
  442. }
  443. if (! NT_SUCCESS(ntstatus)) {
  444. NetpKdPrint(("[Wksta] NtOpenFile datagram receiver ASYNC failed: 0x%08lx\n",
  445. ntstatus));
  446. WsDgrecAsyncDeviceHandle = NULL;
  447. }
  448. return NetpNtStatusToApiStatus(ntstatus);
  449. }
  450. NET_API_STATUS
  451. WsUnloadDriver(
  452. IN LPWSTR DriverNameString
  453. )
  454. {
  455. ULONG Privileges[1];
  456. LPWSTR DriverRegistryName;
  457. UNICODE_STRING DriverRegistryString;
  458. NET_API_STATUS Status;
  459. NTSTATUS ntstatus;
  460. DriverRegistryName = (LPWSTR) LocalAlloc(
  461. LMEM_FIXED,
  462. (UINT) (sizeof(SERVICE_REGISTRY_KEY) +
  463. (wcslen(DriverNameString) *
  464. sizeof(WCHAR)))
  465. );
  466. if (DriverRegistryName == NULL) {
  467. return ERROR_NOT_ENOUGH_MEMORY;
  468. }
  469. Privileges[0] = SE_LOAD_DRIVER_PRIVILEGE;
  470. Status = NetpGetPrivilege(1, Privileges);
  471. if (Status != NERR_Success) {
  472. LocalFree(DriverRegistryName);
  473. return Status;
  474. }
  475. wcscpy(DriverRegistryName, SERVICE_REGISTRY_KEY);
  476. wcscat(DriverRegistryName, DriverNameString);
  477. RtlInitUnicodeString(&DriverRegistryString, DriverRegistryName);
  478. ntstatus = NtUnloadDriver(&DriverRegistryString);
  479. LocalFree(DriverRegistryName);
  480. NetpReleasePrivilege();
  481. return(WsMapStatus(ntstatus));
  482. }
  483. NET_API_STATUS
  484. WsLoadDriver(
  485. IN LPWSTR DriverNameString
  486. )
  487. {
  488. ULONG Privileges[1];
  489. LPWSTR DriverRegistryName;
  490. UNICODE_STRING DriverRegistryString;
  491. NET_API_STATUS Status;
  492. NTSTATUS ntstatus;
  493. DriverRegistryName = (LPWSTR) LocalAlloc(
  494. LMEM_FIXED,
  495. (UINT) (sizeof(SERVICE_REGISTRY_KEY) +
  496. (wcslen(DriverNameString) *
  497. sizeof(WCHAR)))
  498. );
  499. if (DriverRegistryName == NULL) {
  500. return ERROR_NOT_ENOUGH_MEMORY;
  501. }
  502. Privileges[0] = SE_LOAD_DRIVER_PRIVILEGE;
  503. Status = NetpGetPrivilege(1, Privileges);
  504. if (Status != NERR_Success) {
  505. LocalFree(DriverRegistryName);
  506. return Status;
  507. }
  508. wcscpy(DriverRegistryName, SERVICE_REGISTRY_KEY);
  509. wcscat(DriverRegistryName, DriverNameString);
  510. RtlInitUnicodeString(&DriverRegistryString, DriverRegistryName);
  511. ntstatus = NtLoadDriver(&DriverRegistryString);
  512. NetpReleasePrivilege();
  513. LocalFree(DriverRegistryName);
  514. if (ntstatus != STATUS_SUCCESS && ntstatus != STATUS_IMAGE_ALREADY_LOADED) {
  515. LPWSTR subString[1];
  516. subString[0] = DriverNameString;
  517. WsLogEvent(
  518. NELOG_DriverNotLoaded,
  519. EVENTLOG_ERROR_TYPE,
  520. 1,
  521. subString,
  522. ntstatus
  523. );
  524. }
  525. return(WsMapStatus(ntstatus));
  526. }
  527. NET_API_STATUS
  528. WsShutdownRedirector(
  529. VOID
  530. )
  531. /*++
  532. Routine Description:
  533. This routine close the LAN Man Redirector device.
  534. Arguments:
  535. None.
  536. Return Value:
  537. --*/
  538. {
  539. LMR_REQUEST_PACKET Rrp;
  540. LMDR_REQUEST_PACKET Drp;
  541. NET_API_STATUS Status;
  542. // tell csc to stop doing stuff
  543. if ((Status = WsCSCWantToStopRedir()) != ERROR_SUCCESS)
  544. {
  545. return Status;
  546. }
  547. Rrp.Version = REQUEST_PACKET_VERSION;
  548. Status = WsRedirFsControl(
  549. WsRedirDeviceHandle,
  550. FSCTL_LMR_STOP,
  551. &Rrp,
  552. sizeof(LMR_REQUEST_PACKET),
  553. NULL,
  554. 0,
  555. NULL
  556. );
  557. (void) NtClose(WsRedirDeviceHandle);
  558. WsRedirDeviceHandle = NULL;
  559. if (Status != ERROR_REDIRECTOR_HAS_OPEN_HANDLES) {
  560. //
  561. // After the workstation has been stopped, we want to unload our
  562. // dependant drivers (the RDR and BOWSER).
  563. //
  564. if (WsDgReceiverDeviceHandle != NULL) {
  565. Drp.Version = LMDR_REQUEST_PACKET_VERSION;
  566. (void) WsDgReceiverIoControl(
  567. WsDgReceiverDeviceHandle,
  568. IOCTL_LMDR_STOP,
  569. &Drp,
  570. sizeof(LMDR_REQUEST_PACKET),
  571. NULL,
  572. 0,
  573. NULL
  574. );
  575. (void) NtClose(WsDgReceiverDeviceHandle);
  576. WsDgReceiverDeviceHandle = NULL;
  577. //
  578. // WsUnloadDriver(L"DGRcvr");
  579. //
  580. }
  581. WsUnloadRedirector();
  582. } else {
  583. NET_API_STATUS TempStatus;
  584. HKEY hRedirectorKey;
  585. DWORD FinalStatus;
  586. TempStatus = RegOpenKeyEx(
  587. HKEY_LOCAL_MACHINE,
  588. MRXSMB_REGISTRY_KEY,
  589. 0,
  590. KEY_ALL_ACCESS,
  591. &hRedirectorKey);
  592. if (TempStatus == ERROR_SUCCESS) {
  593. // if this is a controlled shutdown and the driver could not be
  594. // unloaded mark the status in the registry for resumption
  595. FinalStatus = ERROR_SUCCESS;
  596. TempStatus = RegSetValueEx(
  597. hRedirectorKey,
  598. LAST_LOAD_STATUS,
  599. 0,
  600. REG_DWORD,
  601. (PCHAR)&FinalStatus,
  602. sizeof(DWORD));
  603. if (TempStatus == ERROR_SUCCESS) {
  604. NetpKdPrint((PREFIX_WKSTA "New RDR will be loaded on restart\n"));
  605. }
  606. RegCloseKey(hRedirectorKey);
  607. }
  608. }
  609. if (Status != NERR_Success)
  610. {
  611. // NetpAssert(vfRedirStarted == 0);
  612. WsCSCReportStartRedir();
  613. }
  614. return Status;
  615. }
  616. NET_API_STATUS
  617. WsRedirFsControl(
  618. IN HANDLE FileHandle,
  619. IN ULONG RedirControlCode,
  620. IN PLMR_REQUEST_PACKET Rrp,
  621. IN ULONG RrpLength,
  622. IN PVOID SecondBuffer OPTIONAL,
  623. IN ULONG SecondBufferLength,
  624. OUT PULONG_PTR Information OPTIONAL
  625. )
  626. /*++
  627. Routine Description:
  628. Arguments:
  629. FileHandle - Supplies a handle to the file or device on which the service
  630. is being performed.
  631. RedirControlCode - Supplies the NtFsControlFile function code given to
  632. the redirector.
  633. Rrp - Supplies the redirector request packet.
  634. RrpLength - Supplies the length of the redirector request packet.
  635. SecondBuffer - Supplies the second buffer in call to NtFsControlFile.
  636. SecondBufferLength - Supplies the length of the second buffer.
  637. Information - Returns the information field of the I/O status block.
  638. Return Value:
  639. NET_API_STATUS - NERR_Success or reason for failure.
  640. --*/
  641. {
  642. NTSTATUS ntstatus;
  643. IO_STATUS_BLOCK IoStatusBlock;
  644. //
  645. // Send the request to the Redirector FSD.
  646. //
  647. ntstatus = NtFsControlFile(
  648. FileHandle,
  649. NULL,
  650. NULL,
  651. NULL,
  652. &IoStatusBlock,
  653. RedirControlCode,
  654. Rrp,
  655. RrpLength,
  656. SecondBuffer,
  657. SecondBufferLength
  658. );
  659. if (NT_SUCCESS(ntstatus)) {
  660. ntstatus = IoStatusBlock.Status;
  661. }
  662. if (ARGUMENT_PRESENT(Information)) {
  663. *Information = IoStatusBlock.Information;
  664. }
  665. IF_DEBUG(UTIL) {
  666. if (! NT_SUCCESS(ntstatus)) {
  667. NetpKdPrint(("[Wksta] fsctl to redir returns %08lx\n", ntstatus));
  668. }
  669. }
  670. return WsMapStatus(ntstatus);
  671. }
  672. NET_API_STATUS
  673. WsDgReceiverIoControl(
  674. IN HANDLE FileHandle,
  675. IN ULONG DgReceiverControlCode,
  676. IN PLMDR_REQUEST_PACKET Drp,
  677. IN ULONG DrpSize,
  678. IN PVOID SecondBuffer OPTIONAL,
  679. IN ULONG SecondBufferLength,
  680. OUT PULONG_PTR Information OPTIONAL
  681. )
  682. /*++
  683. Routine Description:
  684. Arguments:
  685. FileHandle - Supplies a handle to the file or device on which the service
  686. is being performed.
  687. DgReceiverControlCode - Supplies the NtDeviceIoControlFile function code
  688. given to the datagram receiver.
  689. Drp - Supplies the datagram receiver request packet.
  690. DrpSize - Supplies the length of the datagram receiver request packet.
  691. SecondBuffer - Supplies the second buffer in call to NtDeviceIoControlFile.
  692. SecondBufferLength - Supplies the length of the second buffer.
  693. Information - Returns the information field of the I/O status block.
  694. Return Value:
  695. NET_API_STATUS - NERR_Success or reason for failure.
  696. --*/
  697. {
  698. NTSTATUS ntstatus;
  699. IO_STATUS_BLOCK IoStatusBlock;
  700. if (FileHandle == NULL) {
  701. IF_DEBUG(UTIL) {
  702. NetpKdPrint(("[Wksta] Datagram receiver not implemented\n"));
  703. }
  704. return ERROR_NOT_SUPPORTED;
  705. }
  706. Drp->TransportName.Length = 0;
  707. //
  708. // Send the request to the Datagram Receiver DD.
  709. //
  710. ntstatus = NtDeviceIoControlFile(
  711. FileHandle,
  712. NULL,
  713. NULL,
  714. NULL,
  715. &IoStatusBlock,
  716. DgReceiverControlCode,
  717. Drp,
  718. DrpSize,
  719. SecondBuffer,
  720. SecondBufferLength
  721. );
  722. // as our handles are always async, only if the driver returned pending
  723. // do we copy the status from the iostatusblock
  724. if (ntstatus==STATUS_PENDING) {
  725. ntstatus = IoStatusBlock.Status;
  726. }
  727. if (ARGUMENT_PRESENT(Information)) {
  728. if (NT_SUCCESS(ntstatus))
  729. {
  730. *Information = IoStatusBlock.Information;
  731. }
  732. }
  733. IF_DEBUG(UTIL) {
  734. if (! NT_SUCCESS(ntstatus)) {
  735. NetpKdPrint(("[Wksta] fsctl to dgreceiver returns %08lx\n", ntstatus));
  736. }
  737. }
  738. if (ntstatus == STATUS_TIMEOUT) {
  739. return ERROR_TIMEOUT;
  740. } else {
  741. return WsMapStatus(ntstatus);
  742. }
  743. }
  744. NET_API_STATUS
  745. WsAsyncBindTransport(
  746. IN LPWSTR transportName,
  747. IN DWORD qualityOfService,
  748. IN PLIST_ENTRY pHeader
  749. )
  750. /*++
  751. Routine Description:
  752. This function async binds the specified transport to the redirector
  753. and the datagram receiver.
  754. NOTE: The transport name length pass to the redirector and
  755. datagram receiver is the number of bytes.
  756. Arguments:
  757. transportName - Supplies the name of the transport to bind to.
  758. qualityOfService - Supplies a value which specifies the search
  759. order of the transport with respect to other transports. The
  760. highest value is searched first.
  761. Return Value:
  762. NO_ERROR
  763. --*/
  764. {
  765. NTSTATUS ntStatus;
  766. NET_API_STATUS status;
  767. DWORD size;
  768. DWORD redirSize;
  769. DWORD dgrecSize;
  770. DWORD nameLength;
  771. PWS_BIND pBind;
  772. PWS_BIND_REDIR pBindRedir;
  773. PWS_BIND_DGREC pBindDgrec;
  774. DWORD variablePart;
  775. // we don't need to add in an extra space for the NULL because
  776. // the structure definitions have the space built in
  777. nameLength = wcslen(transportName);
  778. //
  779. // Make sure *Size-s are PVOID aligned.
  780. //
  781. variablePart = nameLength * sizeof( WCHAR );
  782. variablePart = (variablePart + sizeof(PVOID) - 1) & ~(sizeof(PVOID) - 1);
  783. //
  784. // Then add the fixed part to *Size-s.
  785. //
  786. size = sizeof( WS_BIND) + variablePart;
  787. redirSize = sizeof( WS_BIND_REDIR) + variablePart;
  788. dgrecSize = sizeof( WS_BIND_DGREC) + variablePart;
  789. pBind = (PVOID) LocalAlloc(
  790. LMEM_ZEROINIT,
  791. (UINT) (size + redirSize + dgrecSize)
  792. );
  793. if ( pBind == NULL) {
  794. NetpKdPrint(( "[Wksta] Failed to allocate pBind memory\n"));
  795. return GetLastError();
  796. }
  797. pBind->TransportNameLength = nameLength * sizeof( WCHAR);
  798. StringCchCopyW(pBind->TransportName, nameLength, transportName);
  799. pBind->Redir = pBindRedir = (PWS_BIND_REDIR)( (PCHAR)pBind + size);
  800. pBind->Dgrec = pBindDgrec = (PWS_BIND_DGREC)( (PCHAR)pBindRedir + redirSize);
  801. pBindRedir->EventHandle = INVALID_HANDLE_VALUE;
  802. pBindRedir->Bound = FALSE;
  803. pBindRedir->Packet.Version = REQUEST_PACKET_VERSION;
  804. pBindRedir->Packet.Parameters.Bind.QualityOfService = qualityOfService;
  805. pBindRedir->Packet.Parameters.Bind.TransportNameLength =
  806. nameLength * sizeof( WCHAR);
  807. StringCchCopyW(pBindRedir->Packet.Parameters.Bind.TransportName,
  808. nameLength, transportName);
  809. pBindDgrec->EventHandle = INVALID_HANDLE_VALUE;
  810. pBindDgrec->Bound = FALSE;
  811. pBindDgrec->Packet.Version = LMDR_REQUEST_PACKET_VERSION;
  812. pBindDgrec->Packet.Level = 0; // Indicate computername doesn't follow transport name
  813. pBindDgrec->Packet.Parameters.Bind.TransportNameLength =
  814. nameLength * sizeof( WCHAR);
  815. StringCchCopyW(pBindDgrec->Packet.Parameters.Bind.TransportName,
  816. nameLength, transportName);
  817. pBindRedir->EventHandle = CreateEvent(
  818. NULL,
  819. TRUE,
  820. FALSE,
  821. NULL
  822. );
  823. if ( pBindRedir->EventHandle == NULL) {
  824. NetpKdPrint(( "[Wksta] Failed to allocate event handle\n"));
  825. status = GetLastError();
  826. goto tail_exit;
  827. }
  828. ntStatus = NtFsControlFile(
  829. WsRedirAsyncDeviceHandle,
  830. pBindRedir->EventHandle,
  831. NULL, // apc routine
  832. NULL, // apc context
  833. &pBindRedir->IoStatusBlock,
  834. FSCTL_LMR_BIND_TO_TRANSPORT, // control code
  835. &pBindRedir->Packet,
  836. sizeof( LMR_REQUEST_PACKET) +
  837. pBindRedir->Packet.Parameters.Bind.TransportNameLength,
  838. NULL,
  839. 0
  840. );
  841. if ( ntStatus != STATUS_PENDING) {
  842. CloseHandle( pBindRedir->EventHandle);
  843. pBindRedir->EventHandle = INVALID_HANDLE_VALUE;
  844. pBindRedir->Bound = NT_SUCCESS( ntStatus );
  845. }
  846. pBindDgrec->EventHandle = CreateEvent(
  847. NULL,
  848. TRUE,
  849. FALSE,
  850. NULL
  851. );
  852. if ( pBindDgrec->EventHandle == NULL) {
  853. status = GetLastError();
  854. goto tail_exit;
  855. }
  856. #ifdef RDR_PNP_POWER
  857. ntStatus = STATUS_SUCCESS;
  858. #else
  859. ntStatus = NtDeviceIoControlFile(
  860. WsDgrecAsyncDeviceHandle,
  861. pBindDgrec->EventHandle,
  862. NULL,
  863. NULL,
  864. &pBindDgrec->IoStatusBlock,
  865. IOCTL_LMDR_BIND_TO_TRANSPORT,
  866. &pBindDgrec->Packet,
  867. dgrecSize - FIELD_OFFSET( WS_BIND_DGREC, Packet ),
  868. NULL,
  869. 0
  870. );
  871. #endif
  872. if ( ntStatus != STATUS_PENDING) {
  873. CloseHandle( pBindDgrec->EventHandle);
  874. pBindDgrec->EventHandle = INVALID_HANDLE_VALUE;
  875. pBindDgrec->Bound = NT_SUCCESS( ntStatus );
  876. }
  877. tail_exit:
  878. InsertTailList( pHeader, &pBind->ListEntry);
  879. return NO_ERROR;
  880. }
  881. NET_API_STATUS
  882. WsBindTransport(
  883. IN LPTSTR TransportName,
  884. IN DWORD QualityOfService,
  885. OUT LPDWORD ErrorParameter OPTIONAL
  886. )
  887. /*++
  888. Routine Description:
  889. This function binds the specified transport to the redirector
  890. and the datagram receiver.
  891. NOTE: The transport name length pass to the redirector and
  892. datagram receiver is the number of bytes.
  893. Arguments:
  894. TransportName - Supplies the name of the transport to bind to.
  895. QualityOfService - Supplies a value which specifies the search
  896. order of the transport with respect to other transports. The
  897. highest value is searched first.
  898. ErrorParameter - Returns the identifier to the invalid parameter if
  899. this function returns ERROR_INVALID_PARAMETER.
  900. Return Value:
  901. NET_API_STATUS - NERR_Success or reason for failure.
  902. --*/
  903. {
  904. NET_API_STATUS status;
  905. DWORD RequestPacketSize;
  906. DWORD TransportNameSize = STRLEN(TransportName) * sizeof(TCHAR);
  907. PLMR_REQUEST_PACKET Rrp;
  908. PLMDR_REQUEST_PACKET Drrp;
  909. //
  910. // Size of request packet buffer
  911. //
  912. RequestPacketSize = STRLEN(TransportName) * sizeof(WCHAR) +
  913. max(sizeof(LMR_REQUEST_PACKET),
  914. sizeof(LMDR_REQUEST_PACKET));
  915. //
  916. // Allocate memory for redirector/datagram receiver request packet
  917. //
  918. if ((Rrp = (PVOID) LocalAlloc(LMEM_ZEROINIT, (UINT) RequestPacketSize)) == NULL) {
  919. return GetLastError();
  920. }
  921. //
  922. // Get redirector to bind to transport
  923. //
  924. Rrp->Version = REQUEST_PACKET_VERSION;
  925. Rrp->Parameters.Bind.QualityOfService = QualityOfService;
  926. Rrp->Parameters.Bind.TransportNameLength = TransportNameSize;
  927. STRCPY(Rrp->Parameters.Bind.TransportName, TransportName);
  928. if ((status = WsRedirFsControl(
  929. WsRedirDeviceHandle,
  930. FSCTL_LMR_BIND_TO_TRANSPORT,
  931. Rrp,
  932. sizeof(LMR_REQUEST_PACKET) +
  933. Rrp->Parameters.Bind.TransportNameLength,
  934. NULL,
  935. 0,
  936. NULL
  937. )) != NERR_Success) {
  938. if (status == ERROR_INVALID_PARAMETER &&
  939. ARGUMENT_PRESENT(ErrorParameter)) {
  940. IF_DEBUG(INFO) {
  941. NetpKdPrint((
  942. "[Wksta] NetrWkstaTransportAdd: invalid parameter is %lu\n",
  943. Rrp->Parameters.Bind.WkstaParameter));
  944. }
  945. *ErrorParameter = Rrp->Parameters.Bind.WkstaParameter;
  946. }
  947. (void) LocalFree(Rrp);
  948. return status;
  949. }
  950. //
  951. // Get dgrec to bind to transport
  952. //
  953. //
  954. // Make use of the same request packet buffer as FSCtl to
  955. // redirector.
  956. //
  957. Drrp = (PLMDR_REQUEST_PACKET) Rrp;
  958. Drrp->Version = LMDR_REQUEST_PACKET_VERSION;
  959. Drrp->Parameters.Bind.TransportNameLength = TransportNameSize;
  960. STRCPY(Drrp->Parameters.Bind.TransportName, TransportName);
  961. status = WsDgReceiverIoControl(
  962. WsDgReceiverDeviceHandle,
  963. IOCTL_LMDR_BIND_TO_TRANSPORT,
  964. Drrp,
  965. RequestPacketSize,
  966. NULL,
  967. 0,
  968. NULL
  969. );
  970. (void) LocalFree(Rrp);
  971. return status;
  972. }
  973. VOID
  974. WsUnbindTransport2(
  975. IN PWS_BIND pBind
  976. )
  977. /*++
  978. Routine Description:
  979. This function unbinds the specified transport from the redirector
  980. and the datagram receiver.
  981. Arguments:
  982. pBind - structure constructed via WsAsyncBindTransport()
  983. Return Value:
  984. None.
  985. --*/
  986. {
  987. // NET_API_STATUS status;
  988. PWS_BIND_REDIR pBindRedir = pBind->Redir;
  989. PWS_BIND_DGREC pBindDgrec = pBind->Dgrec;
  990. //
  991. // Get redirector to unbind from transport
  992. //
  993. if ( pBindRedir->Bound == TRUE) {
  994. pBindRedir->Packet.Parameters.Unbind.TransportNameLength
  995. = pBind->TransportNameLength;
  996. memcpy(
  997. pBindRedir->Packet.Parameters.Unbind.TransportName,
  998. pBind->TransportName,
  999. pBind->TransportNameLength
  1000. );
  1001. (VOID)NtFsControlFile(
  1002. WsRedirDeviceHandle,
  1003. NULL,
  1004. NULL, // apc routine
  1005. NULL, // apc context
  1006. &pBindRedir->IoStatusBlock,
  1007. FSCTL_LMR_UNBIND_FROM_TRANSPORT, // control code
  1008. &pBindRedir->Packet,
  1009. sizeof( LMR_REQUEST_PACKET) +
  1010. pBindRedir->Packet.Parameters.Unbind.TransportNameLength,
  1011. NULL,
  1012. 0
  1013. );
  1014. pBindRedir->Bound = FALSE;
  1015. }
  1016. //
  1017. // Get datagram receiver to unbind from transport
  1018. //
  1019. if ( pBindDgrec->Bound == TRUE) {
  1020. pBindDgrec->Packet.Parameters.Unbind.TransportNameLength
  1021. = pBind->TransportNameLength;
  1022. memcpy(
  1023. pBindDgrec->Packet.Parameters.Unbind.TransportName,
  1024. pBind->TransportName,
  1025. pBind->TransportNameLength
  1026. );
  1027. (VOID)NtDeviceIoControlFile(
  1028. WsDgReceiverDeviceHandle,
  1029. NULL,
  1030. NULL, // apc routine
  1031. NULL, // apc context
  1032. &pBindDgrec->IoStatusBlock,
  1033. FSCTL_LMR_UNBIND_FROM_TRANSPORT, // control code
  1034. &pBindDgrec->Packet,
  1035. sizeof( LMR_REQUEST_PACKET) +
  1036. pBindDgrec->Packet.Parameters.Unbind.TransportNameLength,
  1037. NULL,
  1038. 0
  1039. );
  1040. pBindDgrec->Bound = FALSE;
  1041. }
  1042. }
  1043. NET_API_STATUS
  1044. WsUnbindTransport(
  1045. IN LPTSTR TransportName,
  1046. IN DWORD ForceLevel
  1047. )
  1048. /*++
  1049. Routine Description:
  1050. This function unbinds the specified transport from the redirector
  1051. and the datagram receiver.
  1052. NOTE: The transport name length pass to the redirector and
  1053. datagram receiver is the number of bytes.
  1054. Arguments:
  1055. TransportName - Supplies the name of the transport to bind to.
  1056. ForceLevel - Supplies the force level to delete active connections
  1057. on the specified transport.
  1058. Return Value:
  1059. NET_API_STATUS - NERR_Success or reason for failure.
  1060. --*/
  1061. {
  1062. NET_API_STATUS status;
  1063. DWORD RequestPacketSize;
  1064. DWORD TransportNameSize = STRLEN(TransportName) * sizeof(TCHAR);
  1065. PLMR_REQUEST_PACKET Rrp;
  1066. PLMDR_REQUEST_PACKET Drrp;
  1067. //
  1068. // Size of request packet buffer
  1069. //
  1070. RequestPacketSize = STRLEN(TransportName) * sizeof(WCHAR) +
  1071. max(sizeof(LMR_REQUEST_PACKET),
  1072. sizeof(LMDR_REQUEST_PACKET));
  1073. //
  1074. // Allocate memory for redirector/datagram receiver request packet
  1075. //
  1076. if ((Rrp = (PVOID) LocalAlloc(LMEM_ZEROINIT, (UINT) RequestPacketSize)) == NULL) {
  1077. return GetLastError();
  1078. }
  1079. //
  1080. // Get redirector to unbind from transport
  1081. //
  1082. Rrp->Version = REQUEST_PACKET_VERSION;
  1083. Rrp->Level = ForceLevel;
  1084. Rrp->Parameters.Unbind.TransportNameLength = TransportNameSize;
  1085. STRCPY(Rrp->Parameters.Unbind.TransportName, TransportName);
  1086. if ((status = WsRedirFsControl(
  1087. WsRedirDeviceHandle,
  1088. FSCTL_LMR_UNBIND_FROM_TRANSPORT,
  1089. Rrp,
  1090. sizeof(LMR_REQUEST_PACKET) +
  1091. Rrp->Parameters.Unbind.TransportNameLength,
  1092. NULL,
  1093. 0,
  1094. NULL
  1095. )) != NERR_Success) {
  1096. (void) LocalFree(Rrp);
  1097. return status;
  1098. }
  1099. //
  1100. // Get datagram receiver to unbind from transport
  1101. //
  1102. //
  1103. // Make use of the same request packet buffer as FSCtl to
  1104. // redirector.
  1105. //
  1106. Drrp = (PLMDR_REQUEST_PACKET) Rrp;
  1107. Drrp->Version = LMDR_REQUEST_PACKET_VERSION;
  1108. Drrp->Level = ForceLevel;
  1109. Drrp->Parameters.Unbind.TransportNameLength = TransportNameSize;
  1110. STRCPY(Drrp->Parameters.Unbind.TransportName, TransportName);
  1111. if ((status = WsDgReceiverIoControl(
  1112. WsDgReceiverDeviceHandle,
  1113. IOCTL_LMDR_UNBIND_FROM_TRANSPORT,
  1114. Drrp,
  1115. RequestPacketSize,
  1116. NULL,
  1117. 0,
  1118. NULL
  1119. )) != NERR_Success) {
  1120. // NTRAID-70693-2/6/2000 davey This is a hack until the bowser supports XNS and LOOP.
  1121. if (status == NERR_UseNotFound) {
  1122. status = NERR_Success;
  1123. }
  1124. }
  1125. (void) LocalFree(Rrp);
  1126. return status;
  1127. }
  1128. NET_API_STATUS
  1129. WsDeleteDomainName(
  1130. IN PLMDR_REQUEST_PACKET Drp,
  1131. IN DWORD DrpSize,
  1132. IN LPTSTR DomainName,
  1133. IN DWORD DomainNameSize
  1134. )
  1135. /*++
  1136. Routine Description:
  1137. This function deletes a domain name from the datagram receiver for
  1138. the current user. It assumes that enough memory is allocate for the
  1139. request packet that is passed in.
  1140. Arguments:
  1141. Drp - Pointer to a datagram receiver request packet with the
  1142. request packet version, and name type initialized.
  1143. DrpSize - Length of datagram receiver request packet in bytes.
  1144. DomainName - Pointer to the domain name to delete.
  1145. DomainNameSize - Length of the domain name in bytes.
  1146. Return Value:
  1147. NET_API_STATUS - NERR_Success or reason for failure.
  1148. --*/
  1149. {
  1150. Drp->Parameters.AddDelName.DgReceiverNameLength = DomainNameSize;
  1151. memcpy(
  1152. (LPBYTE) Drp->Parameters.AddDelName.Name,
  1153. (LPBYTE) DomainName,
  1154. DomainNameSize
  1155. );
  1156. return WsDgReceiverIoControl(
  1157. WsDgReceiverDeviceHandle,
  1158. IOCTL_LMDR_DELETE_NAME,
  1159. Drp,
  1160. DrpSize,
  1161. NULL,
  1162. 0,
  1163. NULL
  1164. );
  1165. }
  1166. NET_API_STATUS
  1167. WsAddDomainName(
  1168. IN PLMDR_REQUEST_PACKET Drp,
  1169. IN DWORD DrpSize,
  1170. IN LPTSTR DomainName,
  1171. IN DWORD DomainNameSize
  1172. )
  1173. /*++
  1174. Routine Description:
  1175. This function adds a domain name to the datagram receiver for the
  1176. current user. It assumes that enough memory is allocate for the
  1177. request packet that is passed in.
  1178. Arguments:
  1179. Drp - Pointer to a datagram receiver request packet with the
  1180. request packet version, and name type initialized.
  1181. DrpSize - Length of datagram receiver request packet in bytes.
  1182. DomainName - Pointer to the domain name to delete.
  1183. DomainNameSize - Length of the domain name in bytes.
  1184. Return Value:
  1185. NET_API_STATUS - NERR_Success or reason for failure.
  1186. --*/
  1187. {
  1188. Drp->Parameters.AddDelName.DgReceiverNameLength = DomainNameSize;
  1189. memcpy(
  1190. (LPBYTE) Drp->Parameters.AddDelName.Name,
  1191. (LPBYTE) DomainName,
  1192. DomainNameSize
  1193. );
  1194. return WsDgReceiverIoControl(
  1195. WsDgReceiverDeviceHandle,
  1196. IOCTL_LMDR_ADD_NAME,
  1197. Drp,
  1198. DrpSize,
  1199. NULL,
  1200. 0,
  1201. NULL
  1202. );
  1203. }
  1204. NET_API_STATUS
  1205. WsTryToLoadSmbMiniRedirector(
  1206. VOID
  1207. );
  1208. NET_API_STATUS
  1209. WsLoadRedirector(
  1210. VOID
  1211. )
  1212. /*++
  1213. Routine Description:
  1214. This routine loads rdr.sys or mrxsmb.sys et al depending on whether
  1215. the conditions for loading mrxsmb.sys are met.
  1216. Arguments:
  1217. None.
  1218. Return Value:
  1219. NET_API_STATUS - NERR_Success or reason for failure.
  1220. Notes:
  1221. The new redirector consists of two parts -- the RDBSS (redirected drive buffering
  1222. subsystem ) and the corresponding smb mini redirectors. Only the minirdr is loaded here;
  1223. the minirdr loads the RDBSS itself.
  1224. As a stopgap measure the old redirector is loaded in the event of any problem
  1225. associated with loading the new redirector.
  1226. --*/
  1227. {
  1228. NET_API_STATUS status;
  1229. status = WsTryToLoadSmbMiniRedirector();
  1230. if ((status != ERROR_SUCCESS) &&
  1231. (status != ERROR_SERVICE_ALREADY_RUNNING)) {
  1232. // Either the new redirector load did not succeed or it does not exist.
  1233. // Load the old redirector.
  1234. LoadedMRxSmbInsteadOfRdr = FALSE;
  1235. status = WsLoadDriver(REDIRECTOR);
  1236. }
  1237. return(status);
  1238. }
  1239. VOID
  1240. WsUnloadRedirector(
  1241. VOID
  1242. )
  1243. /*++
  1244. Routine Description:
  1245. This routine unloads the drivers that we loaded above.
  1246. Arguments:
  1247. None.
  1248. Return Value:
  1249. None.
  1250. --*/
  1251. {
  1252. NET_API_STATUS status;
  1253. DWORD NameLength,NameOffset;
  1254. HKEY hRedirectorKey;
  1255. DWORD FinalStatus;
  1256. if (!LoadedMRxSmbInsteadOfRdr) {
  1257. WsUnloadDriver(REDIRECTOR);
  1258. return;
  1259. }
  1260. status = WsUnloadDriver(SMB_MINIRDR);
  1261. if (status == ERROR_SUCCESS) {
  1262. WsUnloadDriver(RDBSS);
  1263. }
  1264. status = RegOpenKeyEx(
  1265. HKEY_LOCAL_MACHINE,
  1266. MRXSMB_REGISTRY_KEY,
  1267. 0,
  1268. KEY_ALL_ACCESS,
  1269. &hRedirectorKey);
  1270. if (status == ERROR_SUCCESS) {
  1271. // if the unloading was successful, reset the LastLoadStatus so that
  1272. // the new redirector will be loaded on the next attempt as well.
  1273. FinalStatus = ERROR_SUCCESS;
  1274. status = RegSetValueEx(
  1275. hRedirectorKey,
  1276. LAST_LOAD_STATUS,
  1277. 0,
  1278. REG_DWORD,
  1279. (PCHAR)&FinalStatus,
  1280. sizeof(DWORD));
  1281. if (status == ERROR_SUCCESS) {
  1282. NetpKdPrint((PREFIX_WKSTA "New RDR will be loaded on restart\n"));
  1283. }
  1284. RegCloseKey(hRedirectorKey);
  1285. }
  1286. return;
  1287. }
  1288. ////////////////////// minirdr stuff
  1289. NET_API_STATUS
  1290. WsTryToLoadSmbMiniRedirector(
  1291. VOID
  1292. )
  1293. /*++
  1294. Routine Description:
  1295. This routine loads rdr.sys or mrxsmb.sys et al depending on whether
  1296. the conditions for loading mrxsmb.sys are met.
  1297. Arguments:
  1298. None.
  1299. Return Value:
  1300. NET_API_STATUS - NERR_Success or reason for failure.
  1301. Notes:
  1302. The new redirector consists of two parts -- the RDBSS (redirected drive buffering
  1303. subsystem ) and the corresponding smb mini redirectors. Only the minirdr is loaded here;
  1304. the minirdr loads the RDBSS itself.
  1305. As a stopgap measure the old redirector is loaded in the event of any problem
  1306. associated with loading the new redirector.
  1307. --*/
  1308. {
  1309. NET_API_STATUS status;
  1310. ULONG Attributes;
  1311. DWORD ValueType;
  1312. DWORD ValueSize;
  1313. DWORD NameLength,NameOffset;
  1314. HKEY hRedirectorKey;
  1315. DWORD FinalStatus; // Temporary till the new rdr is the default
  1316. DWORD LastLoadStatus;
  1317. //try to open the minirdr's registry key....if fails GET OUT IMMEDIATELY!!!!
  1318. status = RegOpenKeyEx(
  1319. HKEY_LOCAL_MACHINE,
  1320. MRXSMB_REGISTRY_KEY,
  1321. 0,
  1322. KEY_ALL_ACCESS,
  1323. &hRedirectorKey);
  1324. if (status != ERROR_SUCCESS) {
  1325. return(status);
  1326. } else {
  1327. status = WsLoadDriver(RDBSS);
  1328. if ((status == ERROR_SUCCESS) || (status == ERROR_SERVICE_ALREADY_RUNNING)) {
  1329. status = WsLoadDriver(SMB_MINIRDR);
  1330. if (status == ERROR_SUCCESS) {
  1331. LoadedMRxSmbInsteadOfRdr = TRUE;
  1332. } else if (status == ERROR_SERVICE_ALREADY_RUNNING) {
  1333. NetpKdPrint((PREFIX_WKSTA "Reactivating Previously Loaded Service\n"));
  1334. LoadedMRxSmbInsteadOfRdr = TRUE;
  1335. status = ERROR_SUCCESS;
  1336. } else {
  1337. // error loading the minirdr
  1338. //WsUnloadDriver(RDBSS);
  1339. NetpKdPrint((PREFIX_WKSTA "Error Loading MRxSmb\n"));
  1340. }
  1341. }
  1342. // NetpKdPrint((PREFIX_WKSTA "New redirector(RDR2) load status %lx\n",status));
  1343. }
  1344. // Close the handle to the registry key irrespective of the result.
  1345. RegCloseKey(hRedirectorKey);
  1346. return(status);
  1347. }
  1348. NET_API_STATUS
  1349. WsCSCReportStartRedir(
  1350. VOID
  1351. )
  1352. /*++
  1353. Routine Description:
  1354. Arguments:
  1355. None.
  1356. Return Value:
  1357. NET_API_STATUS - NERR_Success or reason for failure.
  1358. Notes:
  1359. --*/
  1360. {
  1361. NET_API_STATUS status = ERROR_SUCCESS;
  1362. DWORD dwError = ERROR_GEN_FAILURE;
  1363. // NetpKdPrint(("Wkssvc: Reporting redir start \n"));
  1364. // ensure that there are named autoreset events
  1365. if (!heventWkssvcToAgentStart)
  1366. {
  1367. NetpAssert(!heventAgentToWkssvc);
  1368. heventWkssvcToAgentStart = CreateNamedEvent(wszWkssvcToAgentStartEvent);
  1369. if (!heventWkssvcToAgentStart)
  1370. {
  1371. dwError = GetLastError();
  1372. NetpKdPrint(("Wkssvc: Failed to create heventWkssvcToAgentStart, error = %d\n", dwError));
  1373. goto bailout;
  1374. }
  1375. heventWkssvcToAgentStop = CreateNamedEvent(wszWkssvcToAgentStopEvent);
  1376. if (!heventWkssvcToAgentStop)
  1377. {
  1378. dwError = GetLastError();
  1379. NetpKdPrint(("Wkssvc: Failed to create heventWkssvcToAgentStop, error = %d\n", dwError));
  1380. goto bailout;
  1381. }
  1382. heventAgentToWkssvc = CreateNamedEvent(wszAgentToWkssvcEvent);
  1383. if (!heventAgentToWkssvc)
  1384. {
  1385. dwError = GetLastError();
  1386. NetpKdPrint(("Wkssvc: Failed to create heventAgentToWkssvc, error = %d\n", dwError));
  1387. goto bailout;
  1388. }
  1389. }
  1390. // NetpAssert(!vfRedirStarted);
  1391. if (!vfRedirStarted)
  1392. {
  1393. SetEvent(heventWkssvcToAgentStart);
  1394. vfRedirStarted = TRUE;
  1395. // NetpKdPrint(("Wkssvc: Reported redir start \n"));
  1396. }
  1397. dwError = ERROR_SUCCESS;
  1398. bailout:
  1399. if (dwError != ERROR_SUCCESS)
  1400. {
  1401. if (heventWkssvcToAgentStart)
  1402. {
  1403. CloseHandle(heventWkssvcToAgentStart);
  1404. heventWkssvcToAgentStart = NULL;
  1405. }
  1406. if (heventWkssvcToAgentStop)
  1407. {
  1408. CloseHandle(heventWkssvcToAgentStop);
  1409. heventWkssvcToAgentStop = NULL;
  1410. }
  1411. if (heventAgentToWkssvc)
  1412. {
  1413. CloseHandle(heventAgentToWkssvc);
  1414. heventAgentToWkssvc = NULL;
  1415. }
  1416. NetpKdPrint(("Wkssvc: Failed to report redir start error code=%d\n", dwError));
  1417. }
  1418. return (dwError);
  1419. }
  1420. NET_API_STATUS
  1421. WsCSCWantToStopRedir(
  1422. VOID
  1423. )
  1424. /*++
  1425. Routine Description:
  1426. Arguments:
  1427. None.
  1428. Return Value:
  1429. NET_API_STATUS - NERR_Success or reason for failure.
  1430. Notes:
  1431. --*/
  1432. {
  1433. NET_API_STATUS status = ERROR_SUCCESS;
  1434. DWORD dwError;
  1435. // NetpKdPrint(("Wkssvc: Asking agent to stop, so the redir can be stopped\n"));
  1436. if (!vfRedirStarted)
  1437. {
  1438. NetpKdPrint(("Wkssvc: getting a stop without a start\n"));
  1439. return ERROR_GEN_FAILURE;
  1440. }
  1441. if (!heventWkssvcToAgentStop)
  1442. {
  1443. NetpAssert(!heventWkssvcToAgentStart && !heventAgentToWkssvc);
  1444. NetpKdPrint(("Wkssvc: Need events for redir stop\n"));
  1445. return ERROR_GEN_FAILURE;
  1446. }
  1447. // Bleed the event
  1448. WaitForSingleObject(heventAgentToWkssvc, 0);
  1449. if (!AgentIsAlive())
  1450. {
  1451. // the agent isn't up
  1452. // no need to issue stop
  1453. NetpKdPrint(("Wkssvc: Agent not alive\n"));
  1454. }
  1455. else
  1456. {
  1457. // NetpKdPrint(("Wkssvc: Agent Alive\n"));
  1458. // agent is up
  1459. // tell him to stop
  1460. SetEvent(heventWkssvcToAgentStop);
  1461. // Wait some reasonable time to see if he stops
  1462. dwError = WaitForSingleObject(heventAgentToWkssvc, CSC_WAIT_TIME);
  1463. if (dwError!=WAIT_OBJECT_0)
  1464. {
  1465. HANDLE hT[2];
  1466. NetpKdPrint(("Wkssvc: Agent didn't disbale CSC in %d milli-seconds\n", CSC_WAIT_TIME));
  1467. // let us try to reset our event in a way that we don't just miss the agent
  1468. hT[0] = heventWkssvcToAgentStop;
  1469. hT[1] = heventAgentToWkssvc;
  1470. dwError = WaitForMultipleObjects(2, hT, FALSE, 0);
  1471. // if we fired because of 1, then the agent gave us an ack
  1472. // otherwise the stop event would be reset and the agent won't get confused
  1473. if (dwError == WAIT_OBJECT_0+1)
  1474. {
  1475. // NetpKdPrint(("Wkssvc: Agent disabled CSC\n"));
  1476. vfRedirStarted = FALSE;
  1477. }
  1478. ResetEvent(heventWkssvcToAgentStop);
  1479. }
  1480. else
  1481. {
  1482. // NetpKdPrint(("Wkssvc: Agent disabled CSC\n"));
  1483. vfRedirStarted = FALSE;
  1484. }
  1485. }
  1486. return status;
  1487. }
  1488. HANDLE
  1489. CreateNamedEvent(
  1490. LPWSTR lpwEventName
  1491. )
  1492. /*++
  1493. Routine Description:
  1494. Arguments:
  1495. lpwEventName Name of the event to create.
  1496. Return Value:
  1497. Notes:
  1498. --*/
  1499. {
  1500. HANDLE hevent = NULL;
  1501. hevent = CreateEvent(NULL, FALSE, FALSE, lpwEventName);
  1502. return hevent;
  1503. }
  1504. BOOL
  1505. AgentIsAlive(
  1506. VOID
  1507. )
  1508. /*++
  1509. Routine Description:
  1510. Arguments:
  1511. None.
  1512. Return Value:
  1513. TRUE if agent is alive, FALSE otherwise
  1514. Notes:
  1515. The named event is created by the agent thread when it comes up.
  1516. --*/
  1517. {
  1518. HANDLE hT;
  1519. BOOL fRet = FALSE;
  1520. // see whether the agent has already created the event
  1521. hT = OpenEvent(SYNCHRONIZE, FALSE, wzAgentExistsEvent);
  1522. if (hT != NULL)
  1523. {
  1524. CloseHandle(hT);
  1525. fRet = TRUE;
  1526. }
  1527. else
  1528. {
  1529. NetpKdPrint(("Wkssvc: Agent error = %d\n", GetLastError()));
  1530. }
  1531. return (fRet);
  1532. }