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.

1501 lines
39 KiB

  1. /*++
  2. Copyright (c) 1991-1992 Microsoft Corporation
  3. Module Name:
  4. utils.c
  5. Abstract:
  6. Utility routines for the browser service.
  7. Author:
  8. Larry Osterman (LarryO) 23-Mar-1992
  9. Environment:
  10. User Mode - Win32
  11. Revision History:
  12. --*/
  13. #undef IF_DEBUG // avoid wsclient.h vs. debuglib.h conflicts.
  14. #include <nt.h> // DbgPrint prototype
  15. #include <ntrtl.h> // DbgPrint
  16. #include <nturtl.h> // Needed by winbase.h
  17. #include <windef.h> // DWORD
  18. #include <winbase.h> // LocalFree
  19. #include <winreg.h>
  20. #include <rpc.h> // DataTypes and runtime APIs
  21. #include <rpcutil.h> // GENERIC_ENUM_STRUCT
  22. #include <lmcons.h> // NET_API_STATUS
  23. #include <lmerr.h> // NetError codes
  24. #include <lmremutl.h> // SUPPORTS_RPC
  25. #include <netlib.h> // NetpNtStatusToApiStatus
  26. #include <netlibnt.h> // NetpNtStatusToApiStatus
  27. #include <netdebug.h>
  28. #include <bowser.h> // generated by the MIDL complier
  29. #include <brnames.h> // Service and interface names
  30. #include <winsvc.h>
  31. #include <debuglib.h> // IF_DEBUG() (needed by netrpc.h).
  32. #include <lmserver.h>
  33. #include <align.h>
  34. #include <tstr.h>
  35. #include <ntddbrow.h>
  36. #include <brcommon.h> // Routines common between client & server
  37. #include <nb30.h>
  38. #include <hostannc.h>
  39. #include <winnls.h>
  40. #ifdef ENABLE_PSEUDO_BROWSER
  41. #include <config.h> // for LPNET_CONFIG_HANDLE & friends
  42. #include <confname.h> // for SECT_NT_BROWSER
  43. #endif
  44. // begin_setup
  45. //
  46. // Buffer allocation size for enumeration output buffer.
  47. //
  48. #define INITIAL_ALLOCATION_SIZE 48*1024 // First attempt size (48K)
  49. #define FUDGE_FACTOR_SIZE 1024 // Second try TotalBytesNeeded
  50. // plus this amount
  51. //
  52. // prototypes
  53. //
  54. #ifdef ENABLE_PSEUDO_BROWSER
  55. DWORD
  56. IsBrowserEnabled(
  57. IN OPTIONAL LPTSTR Section,
  58. IN LPTSTR Key,
  59. IN BOOL fDefault
  60. );
  61. DWORD
  62. GetBrowserValue(
  63. IN OPTIONAL LPTSTR Section,
  64. IN LPTSTR Key,
  65. OUT PDWORD pdwValue
  66. );
  67. #endif
  68. //
  69. // Implementation
  70. //
  71. NET_API_STATUS
  72. BrDgReceiverIoControl(
  73. IN HANDLE FileHandle,
  74. IN ULONG DgReceiverControlCode,
  75. IN PLMDR_REQUEST_PACKET Drp,
  76. IN ULONG DrpSize,
  77. IN PVOID SecondBuffer OPTIONAL,
  78. IN ULONG SecondBufferLength,
  79. OUT PULONG Information OPTIONAL
  80. )
  81. /*++
  82. Routine Description:
  83. Arguments:
  84. FileHandle - Supplies a handle to the file or device on which the service
  85. is being performed.
  86. DgReceiverControlCode - Supplies the NtDeviceIoControlFile function code
  87. given to the datagram receiver.
  88. Drp - Supplies the datagram receiver request packet.
  89. DrpSize - Supplies the length of the datagram receiver request packet.
  90. SecondBuffer - Supplies the second buffer in call to NtDeviceIoControlFile.
  91. SecondBufferLength - Supplies the length of the second buffer.
  92. Information - Returns the information field of the I/O status block.
  93. Return Value:
  94. NET_API_STATUS - NERR_Success or reason for failure.
  95. --*/
  96. {
  97. NTSTATUS ntstatus;
  98. IO_STATUS_BLOCK IoStatusBlock;
  99. PLMDR_REQUEST_PACKET RealDrp;
  100. HANDLE CompletionEvent;
  101. LPBYTE Where;
  102. if (FileHandle == NULL) {
  103. return ERROR_NOT_SUPPORTED;
  104. }
  105. //
  106. // Allocate a copy of the request packet where we can put the transport and
  107. // emulated domain name in the packet itself.
  108. //
  109. RealDrp = (PLMDR_REQUEST_PACKET) MIDL_user_allocate(DrpSize+
  110. Drp->TransportName.Length+sizeof(WCHAR)+
  111. Drp->EmulatedDomainName.Length+sizeof(WCHAR) );
  112. if (RealDrp == NULL) {
  113. return ERROR_NOT_ENOUGH_MEMORY;
  114. }
  115. //
  116. // Copy the request packet into the local copy.
  117. //
  118. RtlCopyMemory(RealDrp, Drp, DrpSize);
  119. Where = (LPBYTE)RealDrp+DrpSize;
  120. if (Drp->TransportName.Length != 0) {
  121. RealDrp->TransportName.Buffer = (LPWSTR)Where;
  122. RealDrp->TransportName.MaximumLength = Drp->TransportName.Length+sizeof(WCHAR);
  123. RtlCopyUnicodeString(&RealDrp->TransportName, &Drp->TransportName);
  124. Where += RealDrp->TransportName.MaximumLength;
  125. }
  126. if (Drp->EmulatedDomainName.Length != 0) {
  127. RealDrp->EmulatedDomainName.Buffer = (LPWSTR)Where;
  128. RealDrp->EmulatedDomainName.MaximumLength = Drp->EmulatedDomainName.Length+sizeof(WCHAR);
  129. RtlCopyUnicodeString(&RealDrp->EmulatedDomainName, &Drp->EmulatedDomainName);
  130. Where += RealDrp->EmulatedDomainName.MaximumLength;
  131. }
  132. //
  133. // Create a completion event
  134. //
  135. CompletionEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  136. if (CompletionEvent == NULL) {
  137. MIDL_user_free(RealDrp);
  138. return(GetLastError());
  139. }
  140. //
  141. // Send the request to the Datagram Receiver DD.
  142. //
  143. ntstatus = NtDeviceIoControlFile(
  144. FileHandle,
  145. CompletionEvent,
  146. NULL,
  147. NULL,
  148. &IoStatusBlock,
  149. DgReceiverControlCode,
  150. RealDrp,
  151. (ULONG)(Where-(LPBYTE)RealDrp),
  152. SecondBuffer,
  153. SecondBufferLength
  154. );
  155. if (NT_SUCCESS(ntstatus)) {
  156. //
  157. // If pending was returned, then wait until the request completes.
  158. //
  159. if (ntstatus == STATUS_PENDING) {
  160. do {
  161. ntstatus = WaitForSingleObjectEx(CompletionEvent, 0xffffffff, TRUE);
  162. } while ( ntstatus == WAIT_IO_COMPLETION );
  163. }
  164. if (NT_SUCCESS(ntstatus)) {
  165. ntstatus = IoStatusBlock.Status;
  166. }
  167. }
  168. if (ARGUMENT_PRESENT(Information)) {
  169. *Information = (ULONG)IoStatusBlock.Information;
  170. }
  171. MIDL_user_free(RealDrp);
  172. CloseHandle(CompletionEvent);
  173. return NetpNtStatusToApiStatus(ntstatus);
  174. }
  175. NET_API_STATUS
  176. DeviceControlGetInfo(
  177. IN HANDLE FileHandle,
  178. IN ULONG DeviceControlCode,
  179. IN PVOID RequestPacket,
  180. IN ULONG RequestPacketLength,
  181. OUT LPVOID *OutputBuffer,
  182. IN ULONG PreferedMaximumLength,
  183. IN ULONG BufferHintSize,
  184. OUT PULONG Information OPTIONAL
  185. )
  186. /*++
  187. Routine Description:
  188. This function allocates the buffer and fill it with the information
  189. that is retrieved from the datagram receiver.
  190. Arguments:
  191. DeviceDriverType - Supplies the value which indicates whether to call
  192. the datagram receiver.
  193. FileHandle - Supplies a handle to the file or device of which to get
  194. information about.
  195. DeviceControlCode - Supplies the NtFsControlFile or NtIoDeviceControlFile
  196. function control code.
  197. RequestPacket - Supplies a pointer to the device request packet.
  198. RrequestPacketLength - Supplies the length of the device request packet.
  199. OutputBuffer - Returns a pointer to the buffer allocated by this routine
  200. which contains the use information requested. This pointer is set to
  201. NULL if return code is not NERR_Success.
  202. PreferedMaximumLength - Supplies the number of bytes of information to
  203. return in the buffer. If this value is MAXULONG, we will try to
  204. return all available information if there is enough memory resource.
  205. BufferHintSize - Supplies the hint size of the output buffer so that the
  206. memory allocated for the initial buffer will most likely be large
  207. enough to hold all requested data.
  208. Information - Returns the information code from the NtFsControlFile or
  209. NtIoDeviceControlFile call.
  210. Return Value:
  211. NET_API_STATUS - NERR_Success or reason for failure.
  212. --*/
  213. {
  214. NET_API_STATUS status;
  215. NTSTATUS ntstatus;
  216. DWORD OutputBufferLength;
  217. DWORD TotalBytesNeeded = 1;
  218. ULONG OriginalResumeKey;
  219. IO_STATUS_BLOCK IoStatusBlock;
  220. PLMDR_REQUEST_PACKET Drrp = (PLMDR_REQUEST_PACKET) RequestPacket;
  221. HANDLE CompletionEvent;
  222. OriginalResumeKey = Drrp->Parameters.EnumerateNames.ResumeHandle;
  223. //
  224. // If PreferedMaximumLength is MAXULONG, then we are supposed to get all
  225. // the information, regardless of size. Allocate the output buffer of a
  226. // reasonable size and try to use it. If this fails, the Redirector FSD
  227. // will say how much we need to allocate.
  228. //
  229. if (PreferedMaximumLength == MAXULONG) {
  230. OutputBufferLength = (BufferHintSize) ?
  231. BufferHintSize :
  232. INITIAL_ALLOCATION_SIZE;
  233. }
  234. else {
  235. OutputBufferLength = PreferedMaximumLength;
  236. }
  237. OutputBufferLength = ROUND_UP_COUNT(OutputBufferLength, ALIGN_WCHAR);
  238. if ((*OutputBuffer = MIDL_user_allocate(OutputBufferLength)) == NULL) {
  239. return ERROR_NOT_ENOUGH_MEMORY;
  240. }
  241. RtlZeroMemory((PVOID) *OutputBuffer, OutputBufferLength);
  242. CompletionEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  243. if (CompletionEvent == NULL) {
  244. MIDL_user_free(*OutputBuffer);
  245. *OutputBuffer = NULL;
  246. return(GetLastError());
  247. }
  248. Drrp->Parameters.EnumerateServers.EntriesRead = 0;
  249. //
  250. // Make the request of the Datagram Receiver
  251. //
  252. ntstatus = NtDeviceIoControlFile(
  253. FileHandle,
  254. CompletionEvent,
  255. NULL, // APC routine
  256. NULL, // APC context
  257. &IoStatusBlock,
  258. DeviceControlCode,
  259. Drrp,
  260. RequestPacketLength,
  261. *OutputBuffer,
  262. OutputBufferLength
  263. );
  264. if (NT_SUCCESS(ntstatus)) {
  265. //
  266. // If pending was returned, then wait until the request completes.
  267. //
  268. if (ntstatus == STATUS_PENDING) {
  269. do {
  270. ntstatus = WaitForSingleObjectEx(CompletionEvent, 0xffffffff, TRUE);
  271. } while ( ntstatus == WAIT_IO_COMPLETION );
  272. }
  273. if (NT_SUCCESS(ntstatus)) {
  274. ntstatus = IoStatusBlock.Status;
  275. }
  276. }
  277. //
  278. // Map NT status to Win error
  279. //
  280. status = NetpNtStatusToApiStatus(ntstatus);
  281. if (status == ERROR_MORE_DATA) {
  282. NetpAssert(
  283. FIELD_OFFSET(
  284. LMDR_REQUEST_PACKET,
  285. Parameters.EnumerateNames.TotalBytesNeeded
  286. ) ==
  287. FIELD_OFFSET(
  288. LMDR_REQUEST_PACKET,
  289. Parameters.EnumerateServers.TotalBytesNeeded
  290. )
  291. );
  292. NetpAssert(
  293. FIELD_OFFSET(
  294. LMDR_REQUEST_PACKET,
  295. Parameters.GetBrowserServerList.TotalBytesNeeded
  296. ) ==
  297. FIELD_OFFSET(
  298. LMDR_REQUEST_PACKET,
  299. Parameters.EnumerateServers.TotalBytesNeeded
  300. )
  301. );
  302. TotalBytesNeeded = Drrp->Parameters.EnumerateNames.TotalBytesNeeded;
  303. }
  304. if ((TotalBytesNeeded > OutputBufferLength) &&
  305. (PreferedMaximumLength == MAXULONG)) {
  306. PLMDR_REQUEST_PACKET Drrp = (PLMDR_REQUEST_PACKET) RequestPacket;
  307. //
  308. // Initial output buffer allocated was too small and we need to return
  309. // all data. First free the output buffer before allocating the
  310. // required size plus a fudge factor just in case the amount of data
  311. // grew.
  312. //
  313. MIDL_user_free(*OutputBuffer);
  314. OutputBufferLength =
  315. ROUND_UP_COUNT((TotalBytesNeeded + FUDGE_FACTOR_SIZE),
  316. ALIGN_WCHAR);
  317. if ((*OutputBuffer = MIDL_user_allocate(OutputBufferLength)) == NULL) {
  318. return ERROR_NOT_ENOUGH_MEMORY;
  319. }
  320. RtlZeroMemory((PVOID) *OutputBuffer, OutputBufferLength);
  321. NetpAssert(
  322. FIELD_OFFSET(
  323. LMDR_REQUEST_PACKET,
  324. Parameters.EnumerateNames.ResumeHandle
  325. ) ==
  326. FIELD_OFFSET(
  327. LMDR_REQUEST_PACKET,
  328. Parameters.EnumerateServers.ResumeHandle
  329. )
  330. );
  331. NetpAssert(
  332. FIELD_OFFSET(
  333. LMDR_REQUEST_PACKET,
  334. Parameters.EnumerateNames.ResumeHandle
  335. ) ==
  336. FIELD_OFFSET(
  337. LMDR_REQUEST_PACKET,
  338. Parameters.GetBrowserServerList.ResumeHandle
  339. )
  340. );
  341. Drrp->Parameters.EnumerateNames.ResumeHandle = OriginalResumeKey;
  342. Drrp->Parameters.EnumerateServers.EntriesRead = 0;
  343. //
  344. // Make the request of the Datagram Receiver
  345. //
  346. ntstatus = NtDeviceIoControlFile(
  347. FileHandle,
  348. CompletionEvent,
  349. NULL, // APC routine
  350. NULL, // APC context
  351. &IoStatusBlock,
  352. DeviceControlCode,
  353. Drrp,
  354. RequestPacketLength,
  355. *OutputBuffer,
  356. OutputBufferLength
  357. );
  358. if (NT_SUCCESS(ntstatus)) {
  359. //
  360. // If pending was returned, then wait until the request completes.
  361. //
  362. if (ntstatus == STATUS_PENDING) {
  363. do {
  364. ntstatus = WaitForSingleObjectEx(CompletionEvent, 0xffffffff, TRUE);
  365. } while ( ntstatus == WAIT_IO_COMPLETION );
  366. }
  367. if (NT_SUCCESS(ntstatus)) {
  368. ntstatus = IoStatusBlock.Status;
  369. }
  370. }
  371. status = NetpNtStatusToApiStatus(ntstatus);
  372. }
  373. //
  374. // If not successful in getting any data, or if the caller asked for
  375. // all available data with PreferedMaximumLength == MAXULONG and
  376. // our buffer overflowed, free the output buffer and set its pointer
  377. // to NULL.
  378. //
  379. if ((status != NERR_Success && status != ERROR_MORE_DATA) ||
  380. (TotalBytesNeeded == 0) ||
  381. (PreferedMaximumLength == MAXULONG && status == ERROR_MORE_DATA) ||
  382. (Drrp->Parameters.EnumerateServers.EntriesRead == 0)) {
  383. MIDL_user_free(*OutputBuffer);
  384. *OutputBuffer = NULL;
  385. //
  386. // PreferedMaximumLength == MAXULONG and buffer overflowed means
  387. // we do not have enough memory to satisfy the request.
  388. //
  389. if (status == ERROR_MORE_DATA) {
  390. status = ERROR_NOT_ENOUGH_MEMORY;
  391. }
  392. }
  393. CloseHandle(CompletionEvent);
  394. return status;
  395. UNREFERENCED_PARAMETER(Information);
  396. }
  397. // end_setup
  398. NET_API_STATUS
  399. GetBrowserServerList(
  400. IN PUNICODE_STRING TransportName,
  401. IN LPCWSTR Domain,
  402. OUT LPWSTR *BrowserList[],
  403. OUT PULONG BrowserListLength,
  404. IN BOOLEAN ForceRescan
  405. )
  406. /*++
  407. Routine Description:
  408. This function will return a list of browser servers.
  409. Arguments:
  410. IN PUNICODE_STRING TransportName - Transport to return list.
  411. Return Value:
  412. NET_API_STATUS - NERR_Success or reason for failure.
  413. --*/
  414. {
  415. NET_API_STATUS Status;
  416. HANDLE BrowserHandle;
  417. PLMDR_REQUEST_PACKET RequestPacket = NULL;
  418. // DbgPrint("Getting browser server list for transport %wZ\n", TransportName);
  419. Status = OpenBrowser(&BrowserHandle);
  420. if (Status != NERR_Success) {
  421. return Status;
  422. }
  423. RequestPacket = MIDL_user_allocate(sizeof(LMDR_REQUEST_PACKET)+(DNLEN*sizeof(WCHAR))+TransportName->MaximumLength);
  424. if (RequestPacket == NULL) {
  425. NtClose(BrowserHandle);
  426. return(GetLastError());
  427. }
  428. RequestPacket->Version = LMDR_REQUEST_PACKET_VERSION_DOM;
  429. RequestPacket->Level = 0;
  430. RequestPacket->Parameters.GetBrowserServerList.ForceRescan = ForceRescan;
  431. if (Domain != NULL) {
  432. STRCPY(RequestPacket->Parameters.GetBrowserServerList.DomainName, Domain);
  433. RequestPacket->Parameters.GetBrowserServerList.DomainNameLength = (USHORT)STRLEN(Domain) * sizeof(TCHAR);
  434. } else {
  435. RequestPacket->Parameters.GetBrowserServerList.DomainNameLength = 0;
  436. RequestPacket->Parameters.GetBrowserServerList.DomainName[0] = L'\0';
  437. }
  438. RequestPacket->TransportName.Buffer = (PWSTR)((PCHAR)RequestPacket+sizeof(LMDR_REQUEST_PACKET)+DNLEN*sizeof(WCHAR));
  439. RequestPacket->TransportName.MaximumLength = TransportName->MaximumLength;
  440. RtlCopyUnicodeString(&RequestPacket->TransportName, TransportName);
  441. RtlInitUnicodeString( &RequestPacket->EmulatedDomainName, NULL );
  442. RequestPacket->Parameters.GetBrowserServerList.ResumeHandle = 0;
  443. Status = DeviceControlGetInfo(
  444. BrowserHandle,
  445. IOCTL_LMDR_GET_BROWSER_SERVER_LIST,
  446. RequestPacket,
  447. sizeof(LMDR_REQUEST_PACKET)+
  448. (DNLEN*sizeof(WCHAR))+TransportName->MaximumLength,
  449. (PVOID *)BrowserList,
  450. 0xffffffff,
  451. 4096,
  452. NULL);
  453. if (Status == NERR_Success) {
  454. if ( NULL == *BrowserList ) {
  455. Status = ERROR_NO_BROWSER_SERVERS_FOUND;
  456. }
  457. else {
  458. *BrowserListLength = RequestPacket->Parameters.GetBrowserServerList.EntriesRead;
  459. }
  460. }
  461. NtClose(BrowserHandle);
  462. MIDL_user_free(RequestPacket);
  463. return Status;
  464. }
  465. NET_API_STATUS
  466. OpenBrowser(
  467. OUT PHANDLE BrowserHandle
  468. )
  469. /*++
  470. Routine Description:
  471. This function opens a handle to the bowser device driver.
  472. Arguments:
  473. OUT PHANDLE BrowserHandle - Returns the handle to the browser.
  474. Return Value:
  475. NET_API_STATUS - NERR_Success or reason for failure.
  476. --*/
  477. {
  478. NTSTATUS ntstatus;
  479. UNICODE_STRING DeviceName;
  480. IO_STATUS_BLOCK IoStatusBlock;
  481. OBJECT_ATTRIBUTES ObjectAttributes;
  482. //
  483. // Open the redirector device.
  484. //
  485. RtlInitUnicodeString(&DeviceName, DD_BROWSER_DEVICE_NAME_U);
  486. InitializeObjectAttributes(
  487. &ObjectAttributes,
  488. &DeviceName,
  489. OBJ_CASE_INSENSITIVE,
  490. NULL,
  491. NULL
  492. );
  493. ntstatus = NtOpenFile(
  494. BrowserHandle,
  495. SYNCHRONIZE | GENERIC_READ | GENERIC_WRITE,
  496. &ObjectAttributes,
  497. &IoStatusBlock,
  498. FILE_SHARE_READ | FILE_SHARE_WRITE,
  499. FILE_SYNCHRONOUS_IO_NONALERT
  500. );
  501. if (NT_SUCCESS(ntstatus)) {
  502. ntstatus = IoStatusBlock.Status;
  503. }
  504. return NetpNtStatusToApiStatus(ntstatus);
  505. }
  506. NET_API_STATUS
  507. CheckForService(
  508. IN LPTSTR ServiceName,
  509. OUT LPSERVICE_STATUS ServiceStatus OPTIONAL
  510. )
  511. {
  512. NET_API_STATUS NetStatus;
  513. SC_HANDLE ServiceControllerHandle;
  514. SC_HANDLE ServiceHandle;
  515. SERVICE_STATUS Status;
  516. if (!ARGUMENT_PRESENT(ServiceStatus)) {
  517. ServiceStatus = &Status;
  518. }
  519. ServiceControllerHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
  520. if (ServiceControllerHandle == NULL) {
  521. return GetLastError();
  522. }
  523. ServiceHandle = OpenService(ServiceControllerHandle, ServiceName, SERVICE_QUERY_STATUS);
  524. if (ServiceHandle == NULL) {
  525. NetStatus = GetLastError();
  526. CloseServiceHandle(ServiceControllerHandle);
  527. return NetStatus;
  528. }
  529. if (!QueryServiceStatus(ServiceHandle, ServiceStatus)) {
  530. NetStatus = GetLastError();
  531. CloseServiceHandle(ServiceHandle);
  532. CloseServiceHandle(ServiceControllerHandle);
  533. return NetStatus;
  534. }
  535. if ((ServiceStatus->dwCurrentState != SERVICE_RUNNING) &&
  536. (ServiceStatus->dwCurrentState != SERVICE_START_PENDING)) {
  537. CloseServiceHandle(ServiceHandle);
  538. CloseServiceHandle(ServiceControllerHandle);
  539. return(NERR_ServiceNotInstalled);
  540. }
  541. CloseServiceHandle(ServiceHandle);
  542. CloseServiceHandle(ServiceControllerHandle);
  543. return NERR_Success;
  544. }
  545. NET_API_STATUS
  546. BrGetLanaNumFromNetworkName(
  547. IN PWCHAR TransportName,
  548. OUT CCHAR *LanaNum
  549. )
  550. /*++
  551. NOTE: THIS CODE WILL NOT WORK IN THE FUTURE!!!!!!!!!!!!!!
  552. --*/
  553. {
  554. HKEY Key;
  555. LPWSTR BindInformation = NULL;
  556. LPWSTR DevicePointer;
  557. ULONG BindInfoSize = 0;
  558. struct {
  559. CHAR Enumerated;
  560. CHAR LanaNum;
  561. } *LanaMap = NULL;
  562. ULONG LanaMapSize = 0;
  563. NET_API_STATUS Status = ERROR_SUCCESS;
  564. DWORD Type;
  565. DWORD LanaIndex;
  566. LanaIndex = 0;
  567. if (Status = RegOpenKeyEx(
  568. HKEY_LOCAL_MACHINE,
  569. L"System\\CurrentControlSet\\Services\\Netbios\\Linkage",
  570. 0,
  571. KEY_QUERY_VALUE,
  572. &Key))
  573. {
  574. return Status;
  575. }
  576. //
  577. // Get buffers length
  578. //
  579. Status = RegQueryValueEx(
  580. Key,
  581. L"Bind",
  582. 0,
  583. NULL,
  584. NULL,
  585. &BindInfoSize
  586. );
  587. if ( Status != ERROR_SUCCESS ||
  588. BindInfoSize == 0 )
  589. {
  590. goto Cleanup;
  591. }
  592. Status = RegQueryValueEx(
  593. Key,
  594. L"LanaMap",
  595. 0,
  596. NULL,
  597. NULL,
  598. &LanaMapSize
  599. );
  600. if ( Status != ERROR_SUCCESS ||
  601. LanaMapSize == 0 )
  602. {
  603. goto Cleanup;
  604. }
  605. //
  606. // Allocate buffers for data.
  607. //
  608. BindInformation = MIDL_user_allocate(BindInfoSize);
  609. if ( !BindInformation )
  610. {
  611. Status = ERROR_OUTOFMEMORY;
  612. goto Cleanup;
  613. }
  614. LanaMap = MIDL_user_allocate(LanaMapSize);
  615. if ( !LanaMap )
  616. {
  617. Status = ERROR_OUTOFMEMORY;
  618. goto Cleanup;
  619. }
  620. //
  621. // Load values from registry
  622. //
  623. if (Status = RegQueryValueEx(
  624. Key,
  625. L"Bind",
  626. 0,
  627. &Type,
  628. (LPBYTE)BindInformation,
  629. &BindInfoSize))
  630. {
  631. goto Cleanup;
  632. }
  633. if (Status = RegQueryValueEx(
  634. Key,
  635. L"LanaMap",
  636. 0,
  637. &Type,
  638. (LPBYTE)LanaMap,
  639. &LanaMapSize))
  640. {
  641. goto Cleanup;
  642. }
  643. //
  644. // Calculate buffer size
  645. //
  646. DevicePointer = BindInformation;
  647. while (*DevicePointer != UNICODE_NULL) {
  648. if (!_wcsicmp(TransportName, DevicePointer)) {
  649. // found transport
  650. if (LanaMap[LanaIndex].Enumerated != 0) {
  651. *LanaNum = LanaMap[LanaIndex].LanaNum;
  652. Status = NERR_Success;
  653. } else {
  654. Status = ERROR_FILE_NOT_FOUND;
  655. }
  656. goto Cleanup;
  657. }
  658. LanaIndex += 1;
  659. DevicePointer += wcslen(DevicePointer)+1;
  660. }
  661. Cleanup:
  662. if ( BindInformation )
  663. {
  664. MIDL_user_free( BindInformation );
  665. }
  666. if ( LanaMap )
  667. {
  668. MIDL_user_free( LanaMap );
  669. }
  670. RegCloseKey(Key);
  671. return( Status );
  672. }
  673. // 1234567890123456
  674. #define SPACES " "
  675. #define ClearNcb( PNCB ) { \
  676. RtlZeroMemory( PNCB , sizeof (NCB) ); \
  677. RtlCopyMemory( (PNCB)->ncb_name, SPACES, sizeof(SPACES)-1 );\
  678. RtlCopyMemory( (PNCB)->ncb_callname, SPACES, sizeof(SPACES)-1 );\
  679. }
  680. NET_API_STATUS
  681. GetNetBiosMasterName(
  682. IN LPWSTR NetworkName,
  683. IN LPWSTR PrimaryDomain,
  684. OUT LPWSTR MasterName,
  685. IN PSVCS_NET_BIOS_RESET SvcsNetBiosReset OPTIONAL
  686. )
  687. {
  688. CCHAR LanaNum;
  689. NCB AStatNcb;
  690. #define MAX_NETBIOS_NAMES 256
  691. struct {
  692. ADAPTER_STATUS AdapterInfo;
  693. NAME_BUFFER Names[MAX_NETBIOS_NAMES];
  694. } AdapterStatus;
  695. WORD i;
  696. CHAR remoteName[CNLEN+1];
  697. NET_API_STATUS Status;
  698. OEM_STRING OemString;
  699. UNICODE_STRING UnicodeString;
  700. Status = BrGetLanaNumFromNetworkName(NetworkName, &LanaNum);
  701. if (Status != NERR_Success) {
  702. return Status;
  703. }
  704. //
  705. // If the SvcsNetBiosReset argument is present, then this routine is
  706. // being called from the service. In this case it needs to synchronize
  707. // its NetBios Reset with the workstation and the messenger.
  708. //
  709. if (ARGUMENT_PRESENT(SvcsNetBiosReset)) {
  710. SvcsNetBiosReset(LanaNum);
  711. }
  712. else {
  713. ClearNcb(&AStatNcb)
  714. AStatNcb.ncb_command = NCBRESET;
  715. AStatNcb.ncb_lsn = 0; // Request resources
  716. AStatNcb.ncb_lana_num = LanaNum;
  717. AStatNcb.ncb_callname[0] = 0; // 16 sessions
  718. AStatNcb.ncb_callname[1] = 0; // 16 commands
  719. AStatNcb.ncb_callname[2] = 0; // 8 names
  720. AStatNcb.ncb_callname[3] = 0; // Don't want the reserved address
  721. Netbios( &AStatNcb );
  722. }
  723. ClearNcb( &AStatNcb );
  724. //
  725. // Uppercase the remote name.
  726. //
  727. RtlInitUnicodeString(&UnicodeString, PrimaryDomain);
  728. OemString.Buffer=remoteName;
  729. OemString.MaximumLength=sizeof(remoteName);
  730. Status = RtlUpcaseUnicodeStringToOemString(&OemString,
  731. &UnicodeString,
  732. FALSE);
  733. if (!NT_SUCCESS(Status)) {
  734. return RtlNtStatusToDosError(Status);
  735. }
  736. AStatNcb.ncb_command = NCBASTAT;
  737. RtlCopyMemory( AStatNcb.ncb_callname, remoteName, strlen(remoteName));
  738. AStatNcb.ncb_callname[15] = MASTER_BROWSER_SIGNATURE;
  739. AStatNcb.ncb_lana_num = LanaNum;
  740. AStatNcb.ncb_length = sizeof( AdapterStatus );
  741. AStatNcb.ncb_buffer = (CHAR *)&AdapterStatus;
  742. Netbios( &AStatNcb );
  743. if ( AStatNcb.ncb_retcode == NRC_GOODRET ||
  744. AStatNcb.ncb_retcode == NRC_INCOMP ) {
  745. for ( i=0 ; i < min(AdapterStatus.AdapterInfo.name_count, MAX_NETBIOS_NAMES) ; i++ ) {
  746. if (AdapterStatus.Names[i].name[NCBNAMSZ-1] == SERVER_SIGNATURE) {
  747. DWORD j;
  748. //
  749. // Ignore malformed netbios names.
  750. //
  751. // Some transports have strange netbios names. For instance,
  752. // netbt registers a netbios name where the first 12 bytes
  753. // are 0 and the last 4 bytes are the IP address.
  754. //
  755. for ( j = 0 ; j < CNLEN ; j++ ) {
  756. if (AdapterStatus.Names[i].name[j] == '\0') {
  757. break;
  758. }
  759. }
  760. if ( j != CNLEN ) {
  761. continue;
  762. }
  763. //
  764. // Convert to unicode
  765. //
  766. if (MultiByteToWideChar(CP_OEMCP,
  767. 0,
  768. AdapterStatus.Names[i].name,
  769. CNLEN,
  770. MasterName,
  771. CNLEN) == 0) {
  772. return(GetLastError());
  773. }
  774. for (j = CNLEN - 1; j ; j -= 1) {
  775. if (MasterName[j] != L' ') {
  776. MasterName[j+1] = UNICODE_NULL;
  777. break;
  778. }
  779. }
  780. return NERR_Success;
  781. }
  782. }
  783. return AStatNcb.ncb_retcode;
  784. } else {
  785. return AStatNcb.ncb_retcode;
  786. }
  787. }
  788. NET_API_STATUS
  789. SendDatagram(
  790. IN HANDLE DgReceiverHandle,
  791. IN PUNICODE_STRING Network,
  792. IN PUNICODE_STRING EmulatedDomainName,
  793. IN PWSTR ResponseName,
  794. IN DGRECEIVER_NAME_TYPE NameType,
  795. IN PVOID Buffer,
  796. IN ULONG BufferLength
  797. )
  798. {
  799. NET_API_STATUS Status;
  800. UCHAR PacketBuffer[sizeof(LMDR_REQUEST_PACKET)+(LM20_CNLEN+1)*sizeof(WCHAR)];
  801. PLMDR_REQUEST_PACKET RequestPacket = (PLMDR_REQUEST_PACKET)PacketBuffer;
  802. RequestPacket->Version = LMDR_REQUEST_PACKET_VERSION_DOM;
  803. RequestPacket->TransportName = *Network;
  804. RequestPacket->EmulatedDomainName = *EmulatedDomainName;
  805. RequestPacket->Type = Datagram;
  806. RequestPacket->Parameters.SendDatagram.DestinationNameType = NameType;
  807. RequestPacket->Parameters.SendDatagram.MailslotNameLength = 0;
  808. //
  809. // The domain announcement name is special, so we don't have to specify
  810. // a destination name for it.
  811. //
  812. RequestPacket->Parameters.SendDatagram.NameLength = wcslen(ResponseName)*sizeof(WCHAR);
  813. wcscpy(RequestPacket->Parameters.SendDatagram.Name, ResponseName);
  814. //
  815. // This is a simple IoControl - It just sends the datagram.
  816. //
  817. Status = BrDgReceiverIoControl(DgReceiverHandle,
  818. IOCTL_LMDR_WRITE_MAILSLOT,
  819. RequestPacket,
  820. FIELD_OFFSET(LMDR_REQUEST_PACKET, Parameters.SendDatagram.Name)+
  821. RequestPacket->Parameters.SendDatagram.NameLength,
  822. Buffer,
  823. BufferLength,
  824. NULL);
  825. return Status;
  826. }
  827. #ifdef ENABLE_PSEUDO_BROWSER
  828. DWORD
  829. GetBrowserValue(
  830. IN OPTIONAL LPTSTR Section,
  831. IN LPTSTR Key,
  832. OUT PDWORD pdwValue
  833. )
  834. /*++
  835. Routine Description:
  836. Query Registry for browser DWORD value.
  837. Arguments:
  838. Section: registry section to query
  839. Key: Key to query value.
  840. Return Value:
  841. ERROR_SUCCESS: Got value
  842. Win32 error: failed to get value
  843. --*/
  844. {
  845. DWORD status = NERR_Success;
  846. LPNET_CONFIG_HANDLE BrowserSection;
  847. const DWORD dwDefault = 0;
  848. NetpAssert(pdwValue);
  849. if ( Section ) {
  850. //
  851. // Open specified section
  852. // (usually used for policy propagation & such)
  853. //
  854. if (NetpOpenConfigDataWithPath(
  855. &BrowserSection,
  856. NULL,
  857. Section,
  858. TRUE) != NERR_Success) {
  859. return ERROR_CANTREAD;
  860. }
  861. }
  862. else {
  863. //
  864. // Open default browser section
  865. //
  866. if (NetpOpenConfigData(
  867. &BrowserSection,
  868. NULL,
  869. SECT_NT_BROWSER,
  870. TRUE) != NERR_Success) {
  871. return ERROR_CANTREAD;
  872. }
  873. }
  874. //
  875. // Get config value
  876. //
  877. if ( NetpGetConfigDword(
  878. BrowserSection,
  879. Key,
  880. dwDefault,
  881. pdwValue ) ) {
  882. // free handle & return failure (default's assigned already)
  883. NetpCloseConfigData(BrowserSection);
  884. return (ERROR_CANTREAD);
  885. }
  886. // free handle
  887. NetpCloseConfigData(BrowserSection);
  888. //
  889. // Hack: Net api missing key fixup
  890. //
  891. if ( Section ){
  892. //
  893. // If explicit path was specified we distinguish missing key
  894. // from when the key is set. However Net api return
  895. // success (assuming default) if the key is missing.
  896. // Thus, upon success we query below for the key's existance.
  897. // Justification: We need to know when a policy key was
  898. // specified or not. However for default net section we can
  899. // accept default value. That is, we assume proper call order:
  900. // 1. query policy --> if specified accept, if missing -->
  901. // 2. query net default location --> if missing or error accept
  902. // default.
  903. // So why don't we just use our own? Cause net api has it standards
  904. // such as [yes|no] equiv to [DWORD(1)|DWORD(0)], default locations
  905. // and more.
  906. //
  907. HKEY SectionKey = NULL;
  908. DWORD cbData;
  909. LPWSTR pwszSection;
  910. const LPWSTR wszParameters = L"\\Parameters";
  911. // alloc & copy fixed up section name
  912. cbData = (wcslen(Section) + wcslen(wszParameters) + 1) * sizeof(WCHAR);
  913. pwszSection = MIDL_user_allocate(cbData);
  914. if (!pwszSection) {
  915. return (ERROR_CANTREAD);
  916. }
  917. wcscpy(pwszSection, Section);
  918. wcscat(pwszSection, wszParameters);
  919. status = RegOpenKeyEx (
  920. HKEY_LOCAL_MACHINE,
  921. pwszSection,
  922. REG_OPTION_NON_VOLATILE,
  923. KEY_READ,
  924. &SectionKey );
  925. if (status) {
  926. //
  927. // Can't even open the key.
  928. //
  929. MIDL_user_free( pwszSection );
  930. return (ERROR_CANTREAD);
  931. }
  932. // free mem & query value.
  933. MIDL_user_free( pwszSection );
  934. cbData = 0;
  935. status = RegQueryValueEx(
  936. SectionKey,
  937. Key,
  938. 0,
  939. NULL,
  940. NULL,
  941. &cbData );
  942. if ( status != ERROR_SUCCESS ||
  943. cbData == 0) {
  944. //
  945. // Key's not there or failed to get it
  946. //
  947. RegCloseKey(SectionKey);
  948. return (ERROR_CANTREAD);
  949. }
  950. RegCloseKey(SectionKey);
  951. }
  952. // Got value
  953. return (ERROR_SUCCESS);
  954. }
  955. DWORD
  956. IsBrowserEnabled(
  957. IN OPTIONAL LPTSTR Section,
  958. IN LPTSTR Key,
  959. IN BOOL fDefault
  960. )
  961. /*++
  962. Routine Description:
  963. Query Registry for browser boolean state.
  964. Arguments:
  965. Section: registry section to query
  966. Key: Key to query value.
  967. Return Value:
  968. ERROR_SUCCESS: Browser marked enabled.
  969. ERROR_SERVICE_DISABLED: Browser marked disabled.
  970. ERROR_CANTREAD: No regkey found or can't open.
  971. --*/
  972. {
  973. DWORD status = NERR_Success;
  974. LPNET_CONFIG_HANDLE BrowserSection;
  975. BOOL fEnabled = fDefault;
  976. if ( Section ) {
  977. //
  978. // Open specified section
  979. // (usually used for policy propagation & such)
  980. //
  981. if (NetpOpenConfigDataWithPath(
  982. &BrowserSection,
  983. NULL,
  984. Section,
  985. TRUE) != NERR_Success) {
  986. return ERROR_CANTREAD;
  987. }
  988. }
  989. else {
  990. //
  991. // Open default browser section
  992. //
  993. if (NetpOpenConfigData(
  994. &BrowserSection,
  995. NULL,
  996. SECT_NT_BROWSER,
  997. TRUE) != NERR_Success) {
  998. return ERROR_CANTREAD;
  999. }
  1000. }
  1001. //
  1002. // Get config value
  1003. //
  1004. if ( NetpGetConfigBool(
  1005. BrowserSection,
  1006. Key,
  1007. fDefault,
  1008. &fEnabled
  1009. ) ) {
  1010. // free handle & return failure (default's assigned already)
  1011. NetpCloseConfigData(BrowserSection);
  1012. return (ERROR_CANTREAD);
  1013. }
  1014. // free handle
  1015. NetpCloseConfigData(BrowserSection);
  1016. //
  1017. // Hack: Net api missing key fixup
  1018. //
  1019. if ( Section ){
  1020. //
  1021. // If explicit path was specified we distinguish missing key
  1022. // from when the key is set. However Net api return
  1023. // success (assuming default) if the key is missing.
  1024. // Thus, upon success we query below for the key's existance.
  1025. // Justification: We need to know when a policy key was
  1026. // specified or not. However for default net section we can
  1027. // accept default value. That is, we assume proper call order:
  1028. // 1. query policy --> if specified accept, if missing -->
  1029. // 2. query net default location --> if missing or error accept
  1030. // default.
  1031. // So why don't we just use our own? Cause net api has it standards
  1032. // such as [yes|no] equiv to [DWORD(1)|DWORD(0)], default locations
  1033. // and more.
  1034. //
  1035. HKEY SectionKey = NULL;
  1036. DWORD cbData;
  1037. LPWSTR pwszSection;
  1038. const LPWSTR wszParameters = L"\\Parameters";
  1039. // alloc & copy fixed up section name
  1040. cbData = (wcslen(Section) + wcslen(wszParameters) + 1) * sizeof(WCHAR);
  1041. pwszSection = MIDL_user_allocate(cbData);
  1042. if (!pwszSection) {
  1043. return (ERROR_CANTREAD);
  1044. }
  1045. wcscpy(pwszSection, Section);
  1046. wcscat(pwszSection, wszParameters);
  1047. status = RegOpenKeyEx (
  1048. HKEY_LOCAL_MACHINE,
  1049. pwszSection,
  1050. REG_OPTION_NON_VOLATILE,
  1051. KEY_READ,
  1052. &SectionKey );
  1053. if (status) {
  1054. //
  1055. // Can't even open the key.
  1056. //
  1057. MIDL_user_free( pwszSection );
  1058. return (ERROR_CANTREAD);
  1059. }
  1060. // free mem & query value.
  1061. MIDL_user_free( pwszSection );
  1062. cbData = 0;
  1063. status = RegQueryValueEx(
  1064. SectionKey,
  1065. Key,
  1066. 0,
  1067. NULL,
  1068. NULL,
  1069. &cbData );
  1070. if ( status != ERROR_SUCCESS ||
  1071. cbData == 0) {
  1072. //
  1073. // Key's not there or failed to get it
  1074. //
  1075. RegCloseKey(SectionKey);
  1076. return (ERROR_CANTREAD);
  1077. }
  1078. RegCloseKey(SectionKey);
  1079. }
  1080. // got value, return state.
  1081. return ( fEnabled ? ERROR_SUCCESS :
  1082. ERROR_SERVICE_DISABLED);
  1083. }
  1084. BOOL
  1085. IsEnumServerEnabled(
  1086. VOID
  1087. )
  1088. /*++
  1089. Routine Description:
  1090. Query the Registry to find if NetServerEnum functionality has
  1091. been disabled by an admin.
  1092. Arguments:
  1093. None.
  1094. Return Value:
  1095. FALSE: service is marked disabled.
  1096. TRUE: default or service is marked enabled
  1097. Remarks:
  1098. None.
  1099. --*/
  1100. {
  1101. DWORD status;
  1102. const BOOL fDefault = TRUE;
  1103. //
  1104. // Query default section
  1105. //
  1106. status = IsBrowserEnabled(
  1107. BROWSER_POLICY_REGPATH_W,
  1108. BROWSER_SEND_SERVER_ENUM_REGKEY_W,
  1109. fDefault);
  1110. if ( status == ERROR_SUCCESS ) {
  1111. // policy marked enabled.
  1112. return TRUE;
  1113. }
  1114. else if ( status == ERROR_SERVICE_DISABLED ) {
  1115. // policy marked disabled
  1116. return FALSE;
  1117. }
  1118. // else failed to read, thus try default (local) service location.
  1119. //
  1120. // Query default section
  1121. //
  1122. status = IsBrowserEnabled(
  1123. NULL,
  1124. BROWSER_SEND_SERVER_ENUM_REGKEY_W,
  1125. fDefault);
  1126. if ( status == ERROR_SERVICE_DISABLED ) {
  1127. // marked disabled
  1128. return FALSE;
  1129. }
  1130. // else either ERROR_SUCCESS (i.e. enabled)
  1131. // or error (default to enabled)
  1132. NetpAssert(fDefault);
  1133. // default is enabled.
  1134. return fDefault;
  1135. }
  1136. DWORD
  1137. GetBrowserPseudoServerLevel(
  1138. VOID
  1139. )
  1140. /*++
  1141. Routine Description:
  1142. Query the Registry to find if this machine should act
  1143. as a Pseudo Server (in case it's a master browser)
  1144. Arguments:
  1145. None.
  1146. Return Value:
  1147. FALSE: default or server is not a Pseudo Server.
  1148. TRUE: service is marked as Pseudo Server.
  1149. Remarks:
  1150. None.
  1151. --*/
  1152. {
  1153. DWORD status;
  1154. const DWORD dwDefault = BROWSER_NON_PSEUDO;
  1155. DWORD dwLevel = dwDefault;
  1156. //
  1157. // Query policy & then browser sections
  1158. //
  1159. if ( ERROR_SUCCESS == GetBrowserValue(
  1160. BROWSER_POLICY_REGPATH_W,
  1161. BROWSER_PSEUDO_SERVER_REGKEY_W,
  1162. &dwLevel) ||
  1163. ERROR_SUCCESS == GetBrowserValue(
  1164. NULL,
  1165. BROWSER_PSEUDO_SERVER_REGKEY_W,
  1166. &dwLevel) ) {
  1167. // policy level exits
  1168. if ( dwLevel != BROWSER_NON_PSEUDO &&
  1169. dwLevel != BROWSER_SEMI_PSEUDO_NO_DMB &&
  1170. dwLevel != BROWSER_PSEUDO ) {
  1171. NetpAssert(!"Regkey browser pseudo level set to invalid value");
  1172. return dwDefault;
  1173. }
  1174. return dwLevel;
  1175. }
  1176. // else failed to read all sections, use default.
  1177. return dwDefault;
  1178. }
  1179. #endif