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.

829 lines
17 KiB

  1. /*
  2. * LsCore.c
  3. *
  4. * Author: BreenH
  5. *
  6. * Client side functions to call the licensing core RPC interface.
  7. */
  8. /*
  9. * Includes
  10. */
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <windows.h>
  15. #include <winsta.h>
  16. #include <license.h>
  17. #include "lcrpc.h"
  18. #include "rpcwire.h"
  19. /*
  20. * External Globals and Function Prototypes
  21. */
  22. extern RTL_CRITICAL_SECTION WstHandleLock;
  23. extern LPWSTR pszOptions;
  24. extern LPWSTR pszProtocolSequence;
  25. extern LPWSTR pszRemoteProtocolSequence;
  26. RPC_STATUS
  27. RpcWinStationBind(
  28. LPWSTR pszUuid,
  29. LPWSTR pszProtocolSequence,
  30. LPWSTR pszNetworkAddress,
  31. LPWSTR pszEndPoint,
  32. LPWSTR pszOptions,
  33. RPC_BINDING_HANDLE *pHandle
  34. );
  35. /*
  36. * Internal Function Prototypes
  37. */
  38. BOOLEAN
  39. ConvertAnsiToUnicode(
  40. LPWSTR *ppUnicodeString,
  41. LPSTR pAnsiString
  42. );
  43. BOOLEAN
  44. ConvertUnicodeToAnsi(
  45. LPSTR *ppAnsiString,
  46. LPWSTR pUnicodeString
  47. );
  48. BOOLEAN
  49. ConvertPolicyInformationA2U(
  50. LPLCPOLICYINFOGENERIC *ppPolicyInfoW,
  51. LPLCPOLICYINFOGENERIC pPolicyInfoA
  52. );
  53. BOOLEAN
  54. ConvertPolicyInformationU2A(
  55. LPLCPOLICYINFOGENERIC *ppPolicyInfoA,
  56. LPLCPOLICYINFOGENERIC pPolicyInfoW
  57. );
  58. BOOLEAN
  59. LcRpcBindLocal(
  60. VOID
  61. );
  62. /*
  63. * Macros borrowed from winsta.c. RPC_HANDLE_NO_SERVER is not supported
  64. * for the license core RPC calls. This means that no license core RPC call
  65. * to the local machine will differentiate between "not a TS box" and "server
  66. * not available".
  67. */
  68. #define CheckLoaderLock() \
  69. ASSERT(NtCurrentTeb()->ClientId.UniqueThread != \
  70. ((PRTL_CRITICAL_SECTION)(NtCurrentPeb()->LoaderLock))->OwningThread);
  71. #define HANDLE_CURRENT_BINDING(hServer) \
  72. CheckLoaderLock(); \
  73. if (hServer == SERVERNAME_CURRENT) \
  74. { \
  75. if (LCRPC_IfHandle == NULL) \
  76. { \
  77. if (!LcRpcBindLocal()) \
  78. { \
  79. return(FALSE); \
  80. } \
  81. } \
  82. hServer = LCRPC_IfHandle; \
  83. }
  84. /*
  85. * Function Implementations
  86. */
  87. BOOLEAN
  88. ConvertAnsiToUnicode(
  89. LPWSTR *ppUnicodeString,
  90. LPSTR pAnsiString
  91. )
  92. {
  93. BOOLEAN fRet;
  94. NTSTATUS Status;
  95. ULONG cbAnsiString;
  96. ULONG cbBytesWritten;
  97. ULONG cbUnicodeString;
  98. ASSERT(ppUnicodeString != NULL);
  99. ASSERT(pAnsiString != NULL);
  100. cbAnsiString = lstrlenA(pAnsiString);
  101. Status = RtlMultiByteToUnicodeSize(
  102. &cbUnicodeString,
  103. pAnsiString,
  104. cbAnsiString
  105. );
  106. if (Status == STATUS_SUCCESS)
  107. {
  108. cbUnicodeString += sizeof(WCHAR);
  109. *ppUnicodeString = (LPWSTR)LocalAlloc(LPTR, cbUnicodeString);
  110. if (*ppUnicodeString != NULL)
  111. {
  112. Status = RtlMultiByteToUnicodeN(
  113. *ppUnicodeString,
  114. cbUnicodeString,
  115. &cbBytesWritten,
  116. pAnsiString,
  117. cbAnsiString
  118. );
  119. if (Status == STATUS_SUCCESS)
  120. {
  121. fRet = TRUE;
  122. }
  123. else
  124. {
  125. LocalFree(*ppUnicodeString);
  126. *ppUnicodeString = NULL;
  127. SetLastError(RtlNtStatusToDosError(Status));
  128. fRet = FALSE;
  129. }
  130. }
  131. else
  132. {
  133. SetLastError(ERROR_OUTOFMEMORY);
  134. fRet = FALSE;
  135. }
  136. }
  137. else
  138. {
  139. SetLastError(RtlNtStatusToDosError(Status));
  140. fRet = FALSE;
  141. }
  142. return(fRet);
  143. }
  144. BOOLEAN
  145. ConvertUnicodeToAnsi(
  146. LPSTR *ppAnsiString,
  147. LPWSTR pUnicodeString
  148. )
  149. {
  150. BOOLEAN fRet;
  151. NTSTATUS Status;
  152. ULONG cbAnsiString;
  153. ULONG cbBytesWritten;
  154. ULONG cbUnicodeString;
  155. ASSERT(ppAnsiString != NULL);
  156. ASSERT(pUnicodeString != NULL);
  157. cbUnicodeString = lstrlenW(pUnicodeString) * sizeof(WCHAR);
  158. Status = RtlUnicodeToMultiByteSize(
  159. &cbAnsiString,
  160. pUnicodeString,
  161. cbUnicodeString
  162. );
  163. if (Status == STATUS_SUCCESS)
  164. {
  165. cbAnsiString += sizeof(CHAR);
  166. *ppAnsiString = (LPSTR)LocalAlloc(LPTR, cbAnsiString);
  167. if (*ppAnsiString != NULL)
  168. {
  169. Status = RtlUnicodeToMultiByteN(
  170. *ppAnsiString,
  171. cbAnsiString,
  172. &cbBytesWritten,
  173. pUnicodeString,
  174. cbUnicodeString
  175. );
  176. if (Status == STATUS_SUCCESS)
  177. {
  178. fRet = TRUE;
  179. }
  180. else
  181. {
  182. LocalFree(*ppAnsiString);
  183. *ppAnsiString = NULL;
  184. SetLastError(RtlNtStatusToDosError(Status));
  185. fRet = FALSE;
  186. }
  187. }
  188. else
  189. {
  190. SetLastError(ERROR_OUTOFMEMORY);
  191. fRet = FALSE;
  192. }
  193. }
  194. else
  195. {
  196. SetLastError(RtlNtStatusToDosError(Status));
  197. fRet = FALSE;
  198. }
  199. return(fRet);
  200. }
  201. BOOLEAN
  202. ConvertPolicyInformationA2U(
  203. LPLCPOLICYINFOGENERIC *ppPolicyInfoW,
  204. LPLCPOLICYINFOGENERIC pPolicyInfoA
  205. )
  206. {
  207. BOOLEAN fRet;
  208. ASSERT(ppPolicyInfoW != NULL);
  209. ASSERT(pPolicyInfoA != NULL);
  210. if (pPolicyInfoA->ulVersion == LCPOLICYINFOTYPE_V1)
  211. {
  212. LPLCPOLICYINFO_V1W *ppPolicyInfoV1W;
  213. LPLCPOLICYINFO_V1A pPolicyInfoV1A;
  214. ppPolicyInfoV1W = (LPLCPOLICYINFO_V1W*)ppPolicyInfoW;
  215. pPolicyInfoV1A = (LPLCPOLICYINFO_V1A)pPolicyInfoA;
  216. *ppPolicyInfoV1W = LocalAlloc(LPTR, sizeof(LPLCPOLICYINFO_V1W));
  217. if (*ppPolicyInfoV1W != NULL)
  218. {
  219. (*ppPolicyInfoV1W)->ulVersion = LCPOLICYINFOTYPE_V1;
  220. fRet = ConvertAnsiToUnicode(
  221. &((*ppPolicyInfoV1W)->lpPolicyName),
  222. pPolicyInfoV1A->lpPolicyName
  223. );
  224. if (fRet)
  225. {
  226. fRet = ConvertAnsiToUnicode(
  227. &((*ppPolicyInfoV1W)->lpPolicyDescription),
  228. pPolicyInfoV1A->lpPolicyDescription
  229. );
  230. }
  231. if (fRet)
  232. {
  233. goto exit;
  234. }
  235. if ((*ppPolicyInfoV1W)->lpPolicyName != NULL)
  236. {
  237. LocalFree((*ppPolicyInfoV1W)->lpPolicyName);
  238. }
  239. if ((*ppPolicyInfoV1W)->lpPolicyDescription != NULL)
  240. {
  241. LocalFree((*ppPolicyInfoV1W)->lpPolicyDescription);
  242. }
  243. LocalFree(*ppPolicyInfoV1W);
  244. *ppPolicyInfoV1W = NULL;
  245. }
  246. else
  247. {
  248. SetLastError(ERROR_OUTOFMEMORY);
  249. fRet = FALSE;
  250. }
  251. }
  252. else
  253. {
  254. SetLastError(ERROR_UNKNOWN_REVISION);
  255. fRet = FALSE;
  256. }
  257. exit:
  258. return(fRet);
  259. }
  260. BOOLEAN
  261. ConvertPolicyInformationU2A(
  262. LPLCPOLICYINFOGENERIC *ppPolicyInfoA,
  263. LPLCPOLICYINFOGENERIC pPolicyInfoW
  264. )
  265. {
  266. BOOLEAN fRet;
  267. ASSERT(ppPolicyInfoA != NULL);
  268. ASSERT(pPolicyInfoW != NULL);
  269. if (pPolicyInfoW->ulVersion == LCPOLICYINFOTYPE_V1)
  270. {
  271. LPLCPOLICYINFO_V1A *ppPolicyInfoV1A;
  272. LPLCPOLICYINFO_V1W pPolicyInfoV1W;
  273. ppPolicyInfoV1A = (LPLCPOLICYINFO_V1A*)ppPolicyInfoA;
  274. pPolicyInfoV1W = (LPLCPOLICYINFO_V1W)pPolicyInfoW;
  275. *ppPolicyInfoV1A = LocalAlloc(LPTR, sizeof(LPLCPOLICYINFO_V1W));
  276. if (*ppPolicyInfoV1A != NULL)
  277. {
  278. (*ppPolicyInfoV1A)->ulVersion = LCPOLICYINFOTYPE_V1;
  279. fRet = ConvertUnicodeToAnsi(
  280. &((*ppPolicyInfoV1A)->lpPolicyName),
  281. pPolicyInfoV1W->lpPolicyName
  282. );
  283. if (fRet)
  284. {
  285. fRet = ConvertUnicodeToAnsi(
  286. &((*ppPolicyInfoV1A)->lpPolicyDescription),
  287. pPolicyInfoV1W->lpPolicyDescription
  288. );
  289. }
  290. if (fRet)
  291. {
  292. goto exit;
  293. }
  294. if ((*ppPolicyInfoV1A)->lpPolicyName != NULL)
  295. {
  296. LocalFree((*ppPolicyInfoV1A)->lpPolicyName);
  297. }
  298. if ((*ppPolicyInfoV1A)->lpPolicyDescription != NULL)
  299. {
  300. LocalFree((*ppPolicyInfoV1A)->lpPolicyDescription);
  301. }
  302. LocalFree(*ppPolicyInfoV1A);
  303. *ppPolicyInfoV1A = NULL;
  304. }
  305. else
  306. {
  307. SetLastError(ERROR_OUTOFMEMORY);
  308. fRet = FALSE;
  309. }
  310. }
  311. else
  312. {
  313. SetLastError(ERROR_UNKNOWN_REVISION);
  314. fRet = FALSE;
  315. }
  316. exit:
  317. return(fRet);
  318. }
  319. BOOLEAN
  320. LcRpcBindLocal(
  321. VOID
  322. )
  323. {
  324. //
  325. // Borrow the TSRPC handle critical section.
  326. //
  327. RtlEnterCriticalSection(&WstHandleLock);
  328. if (LCRPC_IfHandle == NULL)
  329. {
  330. LCRPC_IfHandle = ServerLicensingOpenW(NULL);
  331. if (LCRPC_IfHandle == NULL)
  332. {
  333. SetLastError(RPC_S_INVALID_BINDING);
  334. RtlLeaveCriticalSection(&WstHandleLock);
  335. return(FALSE);
  336. }
  337. }
  338. RtlLeaveCriticalSection(&WstHandleLock);
  339. return(TRUE);
  340. }
  341. HANDLE WINAPI
  342. ServerLicensingOpenW(
  343. LPWSTR pServerName
  344. )
  345. {
  346. BOOLEAN fRet;
  347. HANDLE hServer;
  348. NTSTATUS Status;
  349. RPC_STATUS RpcStatus;
  350. RPC_BINDING_HANDLE RpcHandle;
  351. if (pServerName == NULL)
  352. {
  353. if (!(USER_SHARED_DATA->SuiteMask & (1 << TerminalServer)))
  354. {
  355. return(NULL);
  356. }
  357. RpcStatus = RpcWinStationBind(
  358. LC_RPC_UUID,
  359. pszProtocolSequence,
  360. NULL,
  361. LC_RPC_LRPC_EP,
  362. pszOptions,
  363. &RpcHandle
  364. );
  365. }
  366. else
  367. {
  368. SetLastError(RPC_S_SERVER_UNAVAILABLE);
  369. return(NULL);
  370. }
  371. if (RpcStatus != RPC_S_OK)
  372. {
  373. SetLastError(RPC_S_SERVER_UNAVAILABLE);
  374. RpcBindingFree(&RpcHandle);
  375. return(NULL);
  376. }
  377. __try
  378. {
  379. hServer = NULL;
  380. fRet = RpcLicensingOpenServer(RpcHandle, &hServer, &Status);
  381. if (!fRet)
  382. {
  383. SetLastError(RtlNtStatusToDosError(Status));
  384. }
  385. }
  386. __except(EXCEPTION_EXECUTE_HANDLER)
  387. {
  388. fRet = FALSE;
  389. SetLastError(GetExceptionCode());
  390. }
  391. RpcBindingFree(&RpcHandle);
  392. return(fRet ? hServer : NULL);
  393. }
  394. HANDLE WINAPI
  395. ServerLicensingOpenA(
  396. LPSTR pServerName
  397. )
  398. {
  399. BOOLEAN fRet;
  400. HANDLE hServer;
  401. ULONG cchServerName;
  402. if (pServerName == NULL)
  403. {
  404. return(ServerLicensingOpenW(NULL));
  405. }
  406. else
  407. {
  408. hServer = NULL;
  409. SetLastError(RPC_S_SERVER_UNAVAILABLE);
  410. }
  411. return(hServer);
  412. }
  413. VOID WINAPI
  414. ServerLicensingClose(
  415. HANDLE hServer
  416. )
  417. {
  418. //
  419. // Don't try to close the define for the local server, and don't allow
  420. // the auto-binding handle to be closed.
  421. //
  422. if ((hServer == SERVERNAME_CURRENT) || (hServer == LCRPC_IfHandle))
  423. {
  424. return;
  425. }
  426. __try
  427. {
  428. RpcLicensingCloseServer(&hServer);
  429. }
  430. __except(EXCEPTION_EXECUTE_HANDLER)
  431. {
  432. }
  433. }
  434. BOOLEAN WINAPI
  435. ServerLicensingLoadPolicy(
  436. HANDLE hServer,
  437. ULONG ulPolicyId
  438. )
  439. {
  440. BOOLEAN fRet;
  441. NTSTATUS Status;
  442. HANDLE_CURRENT_BINDING(hServer);
  443. __try
  444. {
  445. fRet = RpcLicensingLoadPolicy(hServer, ulPolicyId, &Status);
  446. if (!fRet)
  447. {
  448. SetLastError(RtlNtStatusToDosError(Status));
  449. }
  450. }
  451. __except(EXCEPTION_EXECUTE_HANDLER)
  452. {
  453. fRet = FALSE;
  454. SetLastError(GetExceptionCode());
  455. }
  456. return(fRet);
  457. }
  458. BOOLEAN WINAPI
  459. ServerLicensingUnloadPolicy(
  460. HANDLE hServer,
  461. ULONG ulPolicyId
  462. )
  463. {
  464. BOOLEAN fRet;
  465. NTSTATUS Status;
  466. HANDLE_CURRENT_BINDING(hServer);
  467. __try
  468. {
  469. fRet = RpcLicensingUnloadPolicy(hServer, ulPolicyId, &Status);
  470. if (!fRet)
  471. {
  472. SetLastError(RtlNtStatusToDosError(Status));
  473. }
  474. }
  475. __except(EXCEPTION_EXECUTE_HANDLER)
  476. {
  477. fRet = FALSE;
  478. SetLastError(GetExceptionCode());
  479. }
  480. return(fRet);
  481. }
  482. DWORD WINAPI
  483. ServerLicensingSetPolicy(
  484. HANDLE hServer,
  485. ULONG ulPolicyId,
  486. LPDWORD lpNewPolicyStatus
  487. )
  488. {
  489. BOOLEAN fRet;
  490. DWORD dwRet;
  491. NTSTATUS Status;
  492. NTSTATUS NewPolicyStatus;
  493. HANDLE_CURRENT_BINDING(hServer);
  494. if (lpNewPolicyStatus == NULL)
  495. {
  496. return(ERROR_INVALID_PARAMETER);
  497. }
  498. __try
  499. {
  500. Status = RpcLicensingSetPolicy(hServer, ulPolicyId, &NewPolicyStatus);
  501. dwRet = RtlNtStatusToDosError(Status);
  502. *lpNewPolicyStatus = RtlNtStatusToDosError(NewPolicyStatus);
  503. }
  504. __except(EXCEPTION_EXECUTE_HANDLER)
  505. {
  506. dwRet = GetExceptionCode();
  507. *lpNewPolicyStatus = ERROR_SUCCESS;
  508. }
  509. return(dwRet);
  510. }
  511. BOOLEAN WINAPI
  512. ServerLicensingGetAvailablePolicyIds(
  513. HANDLE hServer,
  514. PULONG *ppulPolicyIds,
  515. PULONG pcPolicies
  516. )
  517. {
  518. BOOLEAN fRet;
  519. NTSTATUS Status;
  520. HANDLE_CURRENT_BINDING(hServer);
  521. if ((ppulPolicyIds == NULL) || (pcPolicies == NULL))
  522. {
  523. SetLastError(ERROR_INVALID_PARAMETER);
  524. return(FALSE);
  525. }
  526. *pcPolicies = 0;
  527. __try
  528. {
  529. fRet = RpcLicensingGetAvailablePolicyIds(hServer, ppulPolicyIds, pcPolicies, &Status);
  530. if (!fRet)
  531. {
  532. SetLastError(RtlNtStatusToDosError(Status));
  533. }
  534. }
  535. __except(EXCEPTION_EXECUTE_HANDLER)
  536. {
  537. fRet = FALSE;
  538. SetLastError(GetExceptionCode());
  539. }
  540. return(fRet);
  541. }
  542. BOOLEAN WINAPI
  543. ServerLicensingGetPolicy(
  544. HANDLE hServer,
  545. PULONG pulPolicyId
  546. )
  547. {
  548. BOOLEAN fRet;
  549. NTSTATUS Status;
  550. HANDLE_CURRENT_BINDING(hServer);
  551. if (pulPolicyId == NULL)
  552. {
  553. SetLastError(ERROR_INVALID_PARAMETER);
  554. return(FALSE);
  555. }
  556. __try
  557. {
  558. fRet = RpcLicensingGetPolicy(hServer, pulPolicyId, &Status);
  559. if (!fRet)
  560. {
  561. SetLastError(RtlNtStatusToDosError(Status));
  562. }
  563. }
  564. __except(EXCEPTION_EXECUTE_HANDLER)
  565. {
  566. fRet = FALSE;
  567. SetLastError(GetExceptionCode());
  568. }
  569. return(fRet);
  570. }
  571. BOOLEAN WINAPI
  572. ServerLicensingGetPolicyInformationW(
  573. HANDLE hServer,
  574. ULONG ulPolicyId,
  575. PULONG pulVersion,
  576. LPLCPOLICYINFOGENERIC *ppPolicyInfo
  577. )
  578. {
  579. BOOLEAN fRet;
  580. LPLCPOLICYINFOGENERIC pWire;
  581. NTSTATUS Status;
  582. ULONG cbPolicyInfo;
  583. HANDLE_CURRENT_BINDING(hServer);
  584. if ((ppPolicyInfo == NULL) || (pulVersion == NULL))
  585. {
  586. SetLastError(ERROR_INVALID_PARAMETER);
  587. return(FALSE);
  588. }
  589. *pulVersion = min(*pulVersion, LCPOLICYINFOTYPE_CURRENT);
  590. pWire = NULL;
  591. cbPolicyInfo = 0;
  592. __try
  593. {
  594. fRet = RpcLicensingGetPolicyInformation(
  595. hServer,
  596. ulPolicyId,
  597. pulVersion,
  598. (PCHAR*)&pWire,
  599. &cbPolicyInfo,
  600. &Status
  601. );
  602. if (!fRet)
  603. {
  604. SetLastError(RtlNtStatusToDosError(Status));
  605. }
  606. }
  607. __except(EXCEPTION_EXECUTE_HANDLER)
  608. {
  609. fRet = FALSE;
  610. SetLastError(GetExceptionCode());
  611. }
  612. if (fRet)
  613. {
  614. fRet = CopyPolicyInformationFromWire(ppPolicyInfo, pWire);
  615. MIDL_user_free(pWire);
  616. }
  617. return(fRet);
  618. }
  619. BOOLEAN WINAPI
  620. ServerLicensingGetPolicyInformationA(
  621. HANDLE hServer,
  622. ULONG ulPolicyId,
  623. PULONG pulVersion,
  624. LPLCPOLICYINFOGENERIC *ppPolicyInfo
  625. )
  626. {
  627. BOOLEAN fRet;
  628. LPLCPOLICYINFOGENERIC pPolicyInfoW;
  629. NTSTATUS Status;
  630. if (ppPolicyInfo == NULL)
  631. {
  632. SetLastError(ERROR_INVALID_PARAMETER);
  633. return(FALSE);
  634. }
  635. pPolicyInfoW = NULL;
  636. fRet = ServerLicensingGetPolicyInformationW(
  637. hServer,
  638. ulPolicyId,
  639. pulVersion,
  640. &pPolicyInfoW
  641. );
  642. if (fRet)
  643. {
  644. fRet = ConvertPolicyInformationU2A(ppPolicyInfo, pPolicyInfoW);
  645. ServerLicensingFreePolicyInformation(&pPolicyInfoW);
  646. }
  647. return(fRet);
  648. }
  649. VOID
  650. ServerLicensingFreePolicyInformation(
  651. LPLCPOLICYINFOGENERIC *ppPolicyInfo
  652. )
  653. {
  654. if ((ppPolicyInfo != NULL) && (*ppPolicyInfo != NULL))
  655. {
  656. if ((*ppPolicyInfo)->ulVersion == LCPOLICYINFOTYPE_V1)
  657. {
  658. LPLCPOLICYINFO_V1 pPolicyInfoV1 = (LPLCPOLICYINFO_V1)(*ppPolicyInfo);
  659. if (pPolicyInfoV1->lpPolicyName != NULL)
  660. {
  661. LocalFree(pPolicyInfoV1->lpPolicyName);
  662. }
  663. if (pPolicyInfoV1->lpPolicyDescription != NULL)
  664. {
  665. LocalFree(pPolicyInfoV1->lpPolicyDescription);
  666. }
  667. LocalFree(pPolicyInfoV1);
  668. pPolicyInfoV1 = NULL;
  669. }
  670. }
  671. }
  672. BOOLEAN WINAPI
  673. ServerLicensingDeactivateCurrentPolicy(
  674. HANDLE hServer
  675. )
  676. {
  677. BOOLEAN fRet;
  678. NTSTATUS Status;
  679. HANDLE_CURRENT_BINDING(hServer);
  680. __try
  681. {
  682. fRet = RpcLicensingDeactivateCurrentPolicy(hServer, &Status);
  683. if (!fRet)
  684. {
  685. SetLastError(RtlNtStatusToDosError(Status));
  686. }
  687. }
  688. __except(EXCEPTION_EXECUTE_HANDLER)
  689. {
  690. fRet = FALSE;
  691. SetLastError(GetExceptionCode());
  692. }
  693. return(fRet);
  694. }