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.

585 lines
11 KiB

  1. /*
  2. * LCRpcInt.cpp
  3. *
  4. * Author: BreenH
  5. *
  6. * Internal functions to support the RPC interface.
  7. */
  8. /*
  9. * Includes
  10. */
  11. #include "precomp.h"
  12. #include "lscore.h"
  13. #include "lscorep.h"
  14. #include "lcrpcint.h"
  15. #include "lcrpc.h"
  16. #include "rpcwire.h"
  17. /*
  18. * Globals
  19. */
  20. ULONG g_ulRpcContext = 0;
  21. /*
  22. * TERMSRV.EXE Function Prototypes
  23. */
  24. extern "C" BOOL
  25. IsCallerAdmin(
  26. VOID
  27. );
  28. //
  29. // Returns RPC_S_OK if the binding represents a local administrator on this box,
  30. // AND if the binding is local.
  31. //
  32. RPC_STATUS
  33. TermsrvSecurityCallback(void * Interface, void *Context)
  34. {
  35. BOOL fIsImpersonatingClient = FALSE;
  36. RPC_STATUS rpcStatus;
  37. unsigned int RpcClientLocalFlag;
  38. rpcStatus = I_RpcBindingIsClientLocal(NULL, &RpcClientLocalFlag);
  39. if (rpcStatus != RPC_S_OK)
  40. goto error;
  41. if (!RpcClientLocalFlag) {
  42. rpcStatus = RPC_S_ACCESS_DENIED;
  43. goto error;
  44. }
  45. rpcStatus = RpcImpersonateClient(NULL);
  46. if (rpcStatus != RPC_S_OK)
  47. goto error;
  48. fIsImpersonatingClient = TRUE;
  49. if (!IsCallerAdmin()) {
  50. rpcStatus = RPC_S_ACCESS_DENIED;
  51. goto error;
  52. }
  53. rpcStatus = RPC_S_OK;
  54. error:
  55. if (fIsImpersonatingClient) {
  56. RpcRevertToSelf();
  57. }
  58. return rpcStatus;
  59. }
  60. /*
  61. * Function Implementations
  62. */
  63. RPC_STATUS
  64. InitializeRpcInterface(
  65. )
  66. {
  67. RPC_STATUS RpcStatus;
  68. RpcStatus = RpcServerUseProtseqEp(
  69. L"ncalrpc",
  70. LC_RPC_MAX_THREADS,
  71. LC_RPC_LRPC_EP,
  72. NULL
  73. );
  74. if (RpcStatus == RPC_S_OK)
  75. {
  76. RpcStatus = RpcServerRegisterIfEx(
  77. LCRPC_v1_0_s_ifspec,
  78. NULL,
  79. NULL,
  80. 0,
  81. RPC_C_LISTEN_MAX_CALLS_DEFAULT,
  82. TermsrvSecurityCallback
  83. );
  84. }
  85. else
  86. {
  87. goto exit;
  88. }
  89. exit:
  90. return(RpcStatus);
  91. }
  92. /*
  93. * RPC Interface Functions
  94. */
  95. extern "C" void
  96. LCRPC_HANDLE_rundown(
  97. LCRPC_HANDLE hServer
  98. )
  99. {
  100. RpcLicensingCloseServer(&hServer);
  101. }
  102. BOOLEAN
  103. RpcLicensingOpenServer(
  104. handle_t hBinding,
  105. HANDLE *phServer,
  106. PLONG pStatus
  107. )
  108. {
  109. BOOLEAN fReturn;
  110. PULONG pulContext;
  111. RPC_STATUS RpcStatus;
  112. UNREFERENCED_PARAMETER(hBinding);
  113. //
  114. // These pointers are REF pointers; they cannot be NULL.
  115. //
  116. ASSERT(pStatus != NULL);
  117. ASSERT(phServer != NULL);
  118. //
  119. // Impersonate client.
  120. //
  121. RpcStatus = RpcImpersonateClient(NULL);
  122. if (RpcStatus != RPC_S_OK)
  123. {
  124. *pStatus = STATUS_CANNOT_IMPERSONATE;
  125. return(FALSE);
  126. }
  127. //
  128. // Check for administration privileges.
  129. //
  130. if (!IsCallerAdmin())
  131. {
  132. RpcStatus = RpcRevertToSelf();
  133. *pStatus = STATUS_ACCESS_DENIED;
  134. return(FALSE);
  135. }
  136. RpcStatus = RpcRevertToSelf();
  137. if(RpcStatus != RPC_S_OK)
  138. {
  139. *pStatus = STATUS_CANNOT_IMPERSONATE;
  140. return (FALSE);
  141. }
  142. pulContext = (PULONG)MIDL_user_allocate(sizeof(ULONG));
  143. //
  144. // Currently, RPC contexts are not necessary, but in case they are
  145. // needed in the future, the RPC calls today must pass them. For
  146. // now, just increment a ULONG for a context value.
  147. //
  148. if (pulContext != NULL)
  149. {
  150. *pulContext = g_ulRpcContext++;
  151. *phServer = pulContext;
  152. *pStatus = STATUS_SUCCESS;
  153. fReturn = TRUE;
  154. }
  155. else
  156. {
  157. *pStatus = STATUS_INSUFF_SERVER_RESOURCES;
  158. fReturn = FALSE;
  159. }
  160. return(fReturn);
  161. }
  162. VOID
  163. RpcLicensingCloseServer(
  164. HANDLE *phServer
  165. )
  166. {
  167. ASSERT(phServer != NULL);
  168. ASSERT(*phServer != NULL);
  169. RPC_STATUS RpcStatus;
  170. //
  171. // Impersonate client.
  172. //
  173. RpcStatus = RpcImpersonateClient(NULL);
  174. if (RpcStatus != RPC_S_OK)
  175. {
  176. return;
  177. }
  178. //
  179. // Check for administration privileges.
  180. //
  181. if (!IsCallerAdmin())
  182. {
  183. RpcStatus = RpcRevertToSelf();
  184. return;
  185. }
  186. RpcStatus = RpcRevertToSelf();
  187. MIDL_user_free(*phServer);
  188. *phServer = NULL;
  189. }
  190. BOOLEAN
  191. RpcLicensingLoadPolicy(
  192. HANDLE hServer,
  193. ULONG ulPolicyId,
  194. PLONG pStatus
  195. )
  196. {
  197. UNREFERENCED_PARAMETER(hServer);
  198. UNREFERENCED_PARAMETER(ulPolicyId);
  199. //
  200. // These pointers are REF pointers; they cannot be NULL.
  201. //
  202. ASSERT(pStatus != NULL);
  203. *pStatus = STATUS_NOT_IMPLEMENTED;
  204. return(FALSE);
  205. }
  206. BOOLEAN
  207. RpcLicensingUnloadPolicy(
  208. HANDLE hServer,
  209. ULONG ulPolicyId,
  210. PLONG pStatus
  211. )
  212. {
  213. UNREFERENCED_PARAMETER(hServer);
  214. UNREFERENCED_PARAMETER(ulPolicyId);
  215. //
  216. // These pointers are REF pointers; they cannot be NULL.
  217. //
  218. ASSERT(pStatus != NULL);
  219. *pStatus = STATUS_NOT_IMPLEMENTED;
  220. return(FALSE);
  221. }
  222. LONG
  223. RpcLicensingSetPolicy(
  224. HANDLE hServer,
  225. ULONG ulPolicyId,
  226. PLONG pNewPolicyStatus
  227. )
  228. {
  229. NTSTATUS Status;
  230. RPC_STATUS RpcStatus;
  231. UNREFERENCED_PARAMETER(hServer);
  232. //
  233. // These pointers are REF pointers; they cannot be NULL.
  234. //
  235. ASSERT(pNewPolicyStatus != NULL);
  236. //
  237. // Impersonate client.
  238. //
  239. RpcStatus = RpcImpersonateClient(NULL);
  240. if (RpcStatus != RPC_S_OK)
  241. {
  242. Status = STATUS_CANNOT_IMPERSONATE;
  243. *pNewPolicyStatus = STATUS_SUCCESS;
  244. return(Status);
  245. }
  246. //
  247. // Check for administration privileges.
  248. //
  249. if (!IsCallerAdmin())
  250. {
  251. RpcStatus = RpcRevertToSelf();
  252. Status = STATUS_ACCESS_DENIED;
  253. *pNewPolicyStatus = STATUS_SUCCESS;
  254. return(Status);
  255. }
  256. RpcStatus = RpcRevertToSelf();
  257. if(RpcStatus != RPC_S_OK)
  258. {
  259. Status = STATUS_CANNOT_IMPERSONATE;
  260. return (Status);
  261. }
  262. Status = LCSetPolicy(ulPolicyId, pNewPolicyStatus);
  263. return(Status);
  264. }
  265. BOOLEAN
  266. RpcLicensingGetAvailablePolicyIds(
  267. HANDLE hServer,
  268. PULONG *ppulPolicyIds,
  269. PULONG pcPolicies,
  270. PLONG pStatus
  271. )
  272. {
  273. UNREFERENCED_PARAMETER(hServer);
  274. RPC_STATUS RpcStatus;
  275. //
  276. // These pointers are REF pointers; they cannot be NULL.
  277. //
  278. ASSERT(ppulPolicyIds != NULL);
  279. ASSERT(pcPolicies != NULL);
  280. ASSERT(pStatus != NULL);
  281. //
  282. // Impersonate client.
  283. //
  284. RpcStatus = RpcImpersonateClient(NULL);
  285. if (RpcStatus != RPC_S_OK)
  286. {
  287. *pStatus = STATUS_CANNOT_IMPERSONATE;
  288. return(FALSE);
  289. }
  290. //
  291. // Check for administration privileges.
  292. //
  293. if (!IsCallerAdmin())
  294. {
  295. RpcStatus = RpcRevertToSelf();
  296. *pStatus = STATUS_ACCESS_DENIED;
  297. return(FALSE);
  298. }
  299. RpcStatus = RpcRevertToSelf();
  300. if(RpcStatus != RPC_S_OK)
  301. {
  302. *pStatus = STATUS_CANNOT_IMPERSONATE;
  303. return (FALSE);
  304. }
  305. *pStatus = LCGetAvailablePolicyIds(ppulPolicyIds, pcPolicies);
  306. return(*pStatus == STATUS_SUCCESS);
  307. }
  308. BOOLEAN
  309. RpcLicensingGetPolicy(
  310. HANDLE hServer,
  311. PULONG pulPolicyId,
  312. PLONG pStatus
  313. )
  314. {
  315. UNREFERENCED_PARAMETER(hServer);
  316. RPC_STATUS RpcStatus;
  317. //
  318. // These pointers are REF pointers; they cannot be NULL.
  319. //
  320. ASSERT(pulPolicyId != NULL);
  321. ASSERT(pStatus != NULL);
  322. //
  323. // Impersonate client.
  324. //
  325. RpcStatus = RpcImpersonateClient(NULL);
  326. if (RpcStatus != RPC_S_OK)
  327. {
  328. *pStatus = STATUS_CANNOT_IMPERSONATE;
  329. return(FALSE);
  330. }
  331. //
  332. // Check for administration privileges.
  333. //
  334. if (!IsCallerAdmin())
  335. {
  336. RpcStatus = RpcRevertToSelf();
  337. *pStatus = STATUS_ACCESS_DENIED;
  338. return(FALSE);
  339. }
  340. RpcStatus = RpcRevertToSelf();
  341. if(RpcStatus != RPC_S_OK)
  342. {
  343. *pStatus = STATUS_CANNOT_IMPERSONATE;
  344. return (FALSE);
  345. }
  346. *pulPolicyId = LCGetPolicy();
  347. *pStatus = STATUS_SUCCESS;
  348. return(TRUE);
  349. }
  350. BOOLEAN
  351. RpcLicensingGetPolicyInformation(
  352. HANDLE hServer,
  353. ULONG ulPolicyId,
  354. PULONG pulVersion,
  355. PCHAR *ppWire,
  356. PULONG pcbWire,
  357. PLONG pStatus
  358. )
  359. {
  360. BOOLEAN fRet;
  361. LPLCPOLICYINFOGENERIC lpPolicyInfo;
  362. NTSTATUS Status;
  363. RPC_STATUS RpcStatus;
  364. UNREFERENCED_PARAMETER(hServer);
  365. //
  366. // These pointers are REF pointers; they cannot be NULL.
  367. //
  368. ASSERT(pulVersion != NULL);
  369. ASSERT(ppWire != NULL);
  370. ASSERT(pcbWire != NULL);
  371. ASSERT(pStatus != NULL);
  372. fRet = FALSE;
  373. *pulVersion = min(*pulVersion, LCPOLICYINFOTYPE_CURRENT);
  374. *ppWire = NULL;
  375. //
  376. // Impersonate client.
  377. //
  378. RpcStatus = RpcImpersonateClient(NULL);
  379. if (RpcStatus != RPC_S_OK)
  380. {
  381. *pStatus = STATUS_CANNOT_IMPERSONATE;
  382. return(FALSE);
  383. }
  384. //
  385. // Check for administration privileges.
  386. //
  387. if (!IsCallerAdmin())
  388. {
  389. RpcStatus = RpcRevertToSelf();
  390. *pStatus = STATUS_ACCESS_DENIED;
  391. return(FALSE);
  392. }
  393. RpcStatus = RpcRevertToSelf();
  394. if(RpcStatus != RPC_S_OK)
  395. {
  396. *pStatus = STATUS_CANNOT_IMPERSONATE;
  397. return(FALSE);
  398. }
  399. Status = AllocatePolicyInformation(&lpPolicyInfo, *pulVersion);
  400. if (Status == STATUS_SUCCESS)
  401. {
  402. Status = LCGetPolicyInformation(ulPolicyId, lpPolicyInfo);
  403. if (Status == STATUS_SUCCESS)
  404. {
  405. *pcbWire = CopyPolicyInformationToWire(
  406. (LPLCPOLICYINFOGENERIC*)ppWire,
  407. lpPolicyInfo
  408. );
  409. if (*pcbWire != 0)
  410. {
  411. fRet = TRUE;
  412. Status = STATUS_SUCCESS;
  413. }
  414. else
  415. {
  416. Status = STATUS_INSUFF_SERVER_RESOURCES;
  417. }
  418. FreePolicyInformation(&lpPolicyInfo);
  419. }
  420. else if (Status == STATUS_NO_MEMORY)
  421. {
  422. Status = STATUS_INSUFF_SERVER_RESOURCES;
  423. }
  424. }
  425. *pStatus = Status;
  426. return(fRet);
  427. }
  428. BOOLEAN
  429. RpcLicensingDeactivateCurrentPolicy(
  430. HANDLE hServer,
  431. PLONG pStatus
  432. )
  433. {
  434. RPC_STATUS RpcStatus;
  435. UNREFERENCED_PARAMETER(hServer);
  436. //
  437. // These pointers are REF pointers; they cannot be NULL.
  438. //
  439. ASSERT(pStatus != NULL);
  440. //
  441. // Impersonate client.
  442. //
  443. RpcStatus = RpcImpersonateClient(NULL);
  444. if (RpcStatus != RPC_S_OK)
  445. {
  446. *pStatus = STATUS_CANNOT_IMPERSONATE;
  447. return(FALSE);
  448. }
  449. //
  450. // Check for administration privileges.
  451. //
  452. if (!IsCallerAdmin())
  453. {
  454. RpcStatus = RpcRevertToSelf();
  455. *pStatus = STATUS_ACCESS_DENIED;
  456. return(FALSE);
  457. }
  458. RpcStatus = RpcRevertToSelf();
  459. if (RpcStatus != RPC_S_OK)
  460. {
  461. *pStatus = STATUS_CANNOT_IMPERSONATE;
  462. return (FALSE);
  463. }
  464. *pStatus = LCDeactivateCurrentPolicy();
  465. return(*pStatus == STATUS_SUCCESS);
  466. }