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.

512 lines
9.5 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. rpcstub.c
  5. Abstract:
  6. License Logging Service client stubs.
  7. Author:
  8. Arthur Hanson (arth) 06-Dec-1994
  9. Environment: User mode only.
  10. Revision History:
  11. --*/
  12. #include <nt.h>
  13. #include <ntrtl.h>
  14. #include <nturtl.h>
  15. #include <windows.h>
  16. #include <string.h>
  17. #include <zwapi.h>
  18. #include <llsconst.h>
  19. #include <debug.h>
  20. #include "lsapi_c.h"
  21. // #define API_TRACE 1
  22. BOOLEAN LLSUp = FALSE;
  23. #define MAX_EXPECTED_SID_LENGTH 72
  24. LPTSTR pszStringBinding = NULL;
  25. RTL_CRITICAL_SECTION LPCInitLock;
  26. static HANDLE LpcPortHandle = NULL;
  27. /////////////////////////////////////////////////////////////////////////
  28. NTSTATUS
  29. LLSReInitLPC( )
  30. /*++
  31. Routine Description:
  32. This service connects to the LLS server and initializes the LPC port.
  33. Arguments:
  34. Return Value:
  35. STATUS_SUCCESS - The call completed successfully.
  36. --*/
  37. {
  38. RPC_STATUS Status = STATUS_SUCCESS;
  39. LPTSTR pszUuid = NULL;
  40. LPTSTR pszProtocolSequence = NULL;
  41. LPTSTR pszNetworkAddress = NULL;
  42. LPTSTR pszEndpoint = NULL;
  43. LPTSTR pszOptions = NULL;
  44. pszProtocolSequence = TEXT("ncalrpc");
  45. pszEndpoint = TEXT(LLS_LPC_ENDPOINT);
  46. pszNetworkAddress = NULL;
  47. if (LLSUp) {
  48. LLSUp = FALSE;
  49. if (pszStringBinding != NULL) {
  50. Status = RpcStringFree(&pszStringBinding);
  51. pszStringBinding = NULL;
  52. }
  53. if (Status == STATUS_SUCCESS) {
  54. if (lsapirpc_handle != NULL) {
  55. Status = RpcBindingFree(&lsapirpc_handle);
  56. }
  57. lsapirpc_handle = NULL;
  58. }
  59. }
  60. try {
  61. // Compose a string binding
  62. Status = RpcStringBindingComposeW(pszUuid,
  63. pszProtocolSequence,
  64. pszNetworkAddress,
  65. pszEndpoint,
  66. pszOptions,
  67. &pszStringBinding);
  68. }
  69. except (TRUE) {
  70. Status = RpcExceptionCode();
  71. }
  72. if(Status) {
  73. #if DBG
  74. dprintf(TEXT("NTLSAPI RpcStringBindingComposeW Failed: 0x%lX\n"), Status);
  75. #endif
  76. if (pszStringBinding != NULL) {
  77. RpcStringFree(&pszStringBinding);
  78. pszStringBinding = NULL;
  79. }
  80. return I_RpcMapWin32Status(Status);
  81. }
  82. // Bind using the created string binding...
  83. try {
  84. Status = RpcBindingFromStringBindingW(pszStringBinding, &lsapirpc_handle);
  85. }
  86. except (TRUE) {
  87. Status = RpcExceptionCode();
  88. }
  89. if(Status) {
  90. #if DBG
  91. dprintf(TEXT("NTLSAPI RpcBindingFromStringBindingW Failed: 0x%lX\n"), Status);
  92. #endif
  93. // lsapirpc_handle = NULL;
  94. if (pszStringBinding != NULL) {
  95. RpcStringFree(&pszStringBinding);
  96. pszStringBinding = NULL;
  97. }
  98. return I_RpcMapWin32Status(Status);
  99. }
  100. LLSUp = TRUE;
  101. return I_RpcMapWin32Status(Status);
  102. } // LLSReInitLPC
  103. /////////////////////////////////////////////////////////////////////////
  104. NTSTATUS
  105. LLSInitLPC( )
  106. /*++
  107. Routine Description:
  108. This service connects to the LLS server and initializes the LPC port.
  109. Arguments:
  110. Return Value:
  111. STATUS_SUCCESS - The call completed successfully.
  112. --*/
  113. {
  114. NTSTATUS status;
  115. status = RtlInitializeCriticalSection(&LPCInitLock);
  116. lsapirpc_handle = NULL;
  117. return status;
  118. } // LLSInitLPC
  119. /////////////////////////////////////////////////////////////////////////
  120. NTSTATUS
  121. LLSCloseLPC( )
  122. /*++
  123. Routine Description:
  124. This closes the LPC port connection to the service.
  125. Arguments:
  126. Return Value:
  127. STATUS_SUCCESS - The call completed successfully.
  128. --*/
  129. {
  130. RPC_STATUS Status = STATUS_SUCCESS, Status2 = STATUS_SUCCESS;
  131. RtlEnterCriticalSection(&LPCInitLock);
  132. LLSUp = FALSE;
  133. if (pszStringBinding != NULL) {
  134. Status = RpcStringFree(&pszStringBinding);
  135. pszStringBinding = NULL;
  136. }
  137. if (lsapirpc_handle != NULL) {
  138. Status2 = RpcBindingFree(&lsapirpc_handle);
  139. }
  140. if (Status == STATUS_SUCCESS)
  141. Status = Status2;
  142. lsapirpc_handle = NULL;
  143. RtlLeaveCriticalSection(&LPCInitLock);
  144. return Status;
  145. } // LLSCloseLPC
  146. /////////////////////////////////////////////////////////////////////////
  147. NTSTATUS
  148. LLSLicenseRequest2 (
  149. IN LPWSTR ProductName,
  150. IN LPWSTR Version,
  151. IN ULONG DataType,
  152. IN BOOLEAN IsAdmin,
  153. IN PVOID Data,
  154. OUT PHANDLE LicenseHandle
  155. )
  156. /*++
  157. Arguments:
  158. ProductName -
  159. Version -
  160. DataType -
  161. IsAdmin -
  162. Data -
  163. LicenseHandle -
  164. Return Status:
  165. STATUS_SUCCESS - Indicates the service completed successfully.
  166. Routine Description:
  167. --*/
  168. {
  169. WCHAR ProductID[MAX_PRODUCT_NAME_LENGTH + MAX_VERSION_LENGTH + 2];
  170. NTSTATUS Status = STATUS_SUCCESS;
  171. BOOL Close = FALSE;
  172. ULONG VersionIndex;
  173. ULONG Size = 0;
  174. ULONG i;
  175. LICENSE_HANDLE RpcLicenseHandle;
  176. BOOL bActuallyConnect = TRUE;
  177. //
  178. // Get this out of the way in-case anything goes wrong
  179. //
  180. *LicenseHandle = NULL;
  181. //
  182. // If LicenseService isn't running (no LPC port) then just return
  183. // dummy info - and let the user on.
  184. //
  185. RtlEnterCriticalSection(&LPCInitLock);
  186. if (!LLSUp)
  187. Status = LLSReInitLPC();
  188. RtlLeaveCriticalSection(&LPCInitLock);
  189. if (!NT_SUCCESS(Status))
  190. return STATUS_SUCCESS;
  191. if (((i = lstrlen(ProductName)) > MAX_PRODUCT_NAME_LENGTH) || (lstrlen(Version) > MAX_VERSION_LENGTH))
  192. return STATUS_SUCCESS;
  193. //
  194. // Create productID - product name + version string.
  195. //
  196. lstrcpy(ProductID, ProductName);
  197. lstrcat(ProductID, TEXT(" "));
  198. lstrcat(ProductID, Version);
  199. VersionIndex = i;
  200. //
  201. // Based on DataType figure out if we are doing a name or a SID
  202. // and copy the data appropriatly
  203. //
  204. if (DataType == NT_LS_USER_NAME) {
  205. Size = lstrlen((LPWSTR) Data);
  206. if (Size > MAX_USER_NAME_LENGTH)
  207. return STATUS_SUCCESS;
  208. Size = (Size + 1) * sizeof(TCHAR);
  209. }
  210. if (DataType == NT_LS_USER_SID) {
  211. //
  212. // Friggin SID, so need to copy it manually.
  213. // WARNING: This makes it dependent on the structure of the
  214. // SID!!!
  215. //
  216. Size = RtlLengthSid( (PSID) Data);
  217. if (Size > MAX_EXPECTED_SID_LENGTH)
  218. return STATUS_SUCCESS;
  219. }
  220. //
  221. // Call the Server.
  222. //
  223. try {
  224. Status = LlsrLicenseRequestW(
  225. &RpcLicenseHandle,
  226. ProductID,
  227. VersionIndex,
  228. IsAdmin,
  229. DataType,
  230. Size,
  231. (PBYTE) Data );
  232. }
  233. except (TRUE) {
  234. #if DBG
  235. Status = I_RpcMapWin32Status(RpcExceptionCode());
  236. if (Status != RPC_NT_SERVER_UNAVAILABLE) {
  237. dprintf(TEXT("ERROR NTLSAPI.DLL: RPC Exception: 0x%lX\n"), Status);
  238. // ASSERT(FALSE);
  239. }
  240. #endif
  241. *LicenseHandle = NULL;
  242. Status = STATUS_SUCCESS;
  243. bActuallyConnect = FALSE;
  244. }
  245. if (Close)
  246. LLSCloseLPC();
  247. // This is really a ULONG, we just treated it as a PVOID so
  248. // RPC would treat is as a context handle
  249. if(bActuallyConnect == TRUE)
  250. *LicenseHandle = RpcLicenseHandle;
  251. return Status;
  252. } // LLSLicenseRequest
  253. /////////////////////////////////////////////////////////////////////////
  254. NTSTATUS
  255. LLSLicenseRequest (
  256. IN LPWSTR ProductName,
  257. IN LPWSTR Version,
  258. IN ULONG DataType,
  259. IN BOOLEAN IsAdmin,
  260. IN PVOID Data,
  261. OUT PULONG LicenseHandle
  262. )
  263. /*++
  264. Arguments:
  265. ProductName -
  266. Version -
  267. DataType -
  268. IsAdmin -
  269. Data -
  270. LicenseHandle -
  271. Return Status:
  272. STATUS_SUCCESS - Indicates the service completed successfully.
  273. Routine Description:
  274. --*/
  275. {
  276. HANDLE RealLicenseHandle;
  277. NTSTATUS status;
  278. if (sizeof(ULONG) == sizeof(HANDLE))
  279. {
  280. // Should still work on Win32
  281. status = LLSLicenseRequest2(ProductName,Version,DataType,IsAdmin,Data,&RealLicenseHandle);
  282. if (NULL != LicenseHandle)
  283. *LicenseHandle = PtrToUlong(RealLicenseHandle);
  284. }
  285. else
  286. {
  287. status = STATUS_NOT_IMPLEMENTED;
  288. if (NULL != LicenseHandle)
  289. *LicenseHandle = (ULONG) 0xFFFFFFFF;
  290. }
  291. return status;
  292. }
  293. /////////////////////////////////////////////////////////////////////////
  294. NTSTATUS
  295. LLSLicenseFree2 (
  296. IN HANDLE LicenseHandle
  297. )
  298. /*++
  299. Arguments:
  300. LicenseHandle -
  301. Return Status:
  302. STATUS_SUCCESS - Indicates the service completed successfully.
  303. --*/
  304. {
  305. BOOL Close = FALSE;
  306. NTSTATUS Status;
  307. LICENSE_HANDLE RpcLicenseHandle = (LICENSE_HANDLE) LicenseHandle;
  308. //
  309. // If LicenseService isn't running (no LPC port) then just return
  310. // dummy info - and let the user on.
  311. //
  312. if (!LLSUp)
  313. {
  314. return STATUS_SUCCESS;
  315. }
  316. //
  317. // Call the Server.
  318. //
  319. try {
  320. Status = LlsrLicenseFree( &RpcLicenseHandle );
  321. }
  322. except (TRUE) {
  323. #if DBG
  324. Status = I_RpcMapWin32Status(RpcExceptionCode());
  325. if (Status != RPC_NT_SERVER_UNAVAILABLE) {
  326. dprintf(TEXT("ERROR NTLSAPI.DLL: RPC Exception: 0x%lX\n"), Status);
  327. // ASSERT(FALSE);
  328. }
  329. #endif
  330. Status = STATUS_SUCCESS;
  331. }
  332. if (Close)
  333. LLSCloseLPC();
  334. return Status;
  335. } // LLSLicenseFree
  336. /////////////////////////////////////////////////////////////////////////
  337. NTSTATUS
  338. LLSLicenseFree (
  339. IN ULONG LicenseHandle
  340. )
  341. /*++
  342. Arguments:
  343. LicenseHandle -
  344. Return Status:
  345. STATUS_SUCCESS - Indicates the service completed successfully.
  346. --*/
  347. {
  348. if (sizeof(ULONG) == sizeof(HANDLE))
  349. {
  350. // Should still work on Win32
  351. return LLSLicenseFree2(ULongToPtr(LicenseHandle));
  352. }
  353. else
  354. {
  355. return STATUS_NOT_IMPLEMENTED;
  356. }
  357. }