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.

764 lines
18 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. ikerpc.c
  5. Abstract:
  6. This module contains all of the code to service the
  7. RPC calls made to the SPD that are serviced in IKE.
  8. Author:
  9. abhisheV 30-September-1999
  10. Environment
  11. User Level: Win32
  12. Revision History:
  13. --*/
  14. #include "precomp.h"
  15. VOID
  16. IKENEGOTIATION_HANDLE_rundown(
  17. IKENEGOTIATION_HANDLE hIKENegotiation
  18. )
  19. {
  20. if (!gbIsIKEUp) {
  21. return;
  22. }
  23. if (hIKENegotiation) {
  24. (VOID) IKECloseIKENegotiationHandle(
  25. hIKENegotiation
  26. );
  27. }
  28. }
  29. VOID
  30. IKENOTIFY_HANDLE_rundown(
  31. IKENOTIFY_HANDLE hIKENotifyHandle
  32. )
  33. {
  34. if (!gbIsIKEUp) {
  35. return;
  36. }
  37. if (hIKENotifyHandle) {
  38. (VOID) IKECloseIKENotifyHandle(
  39. hIKENotifyHandle
  40. );
  41. }
  42. }
  43. DWORD
  44. RpcInitiateIKENegotiation(
  45. STRING_HANDLE pServerName,
  46. DWORD dwVersion,
  47. PQM_FILTER_CONTAINER pQMFilterContainer,
  48. DWORD dwClientProcessId,
  49. ULONG uhClientEvent,
  50. DWORD dwFlags,
  51. IPSEC_UDP_ENCAP_CONTEXT UdpEncapContext,
  52. IKENEGOTIATION_HANDLE * phIKENegotiation
  53. )
  54. {
  55. DWORD dwError = 0;
  56. HANDLE hClientEvent = NULL;
  57. PIPSEC_QM_FILTER pQMFilter = NULL;
  58. BOOL bImpersonating = FALSE;
  59. dwError = SPDImpersonateClient(
  60. &bImpersonating
  61. );
  62. BAIL_ON_WIN32_ERROR(dwError);
  63. if (dwVersion) {
  64. dwError = ERROR_NOT_SUPPORTED;
  65. BAIL_ON_WIN32_ERROR(dwError);
  66. }
  67. ENTER_SPD_SECTION();
  68. dwError = ValidateSecurity(
  69. SPD_OBJECT_SERVER,
  70. SERVER_ACCESS_ADMINISTER,
  71. NULL,
  72. NULL
  73. );
  74. LEAVE_SPD_SECTION();
  75. BAIL_ON_WIN32_ERROR(dwError);
  76. dwError = ValidateInitiateIKENegotiation(pServerName,
  77. pQMFilterContainer,
  78. dwClientProcessId,
  79. uhClientEvent,
  80. dwFlags,
  81. UdpEncapContext,
  82. phIKENegotiation);
  83. BAIL_ON_WIN32_ERROR(dwError);
  84. hClientEvent = LongToHandle(uhClientEvent);
  85. pQMFilter = pQMFilterContainer->pQMFilters;
  86. if (pQMFilter && (pQMFilter->IpVersion != IPSEC_PROTOCOL_V4)) {
  87. dwError = ERROR_INVALID_LEVEL;
  88. BAIL_ON_WIN32_ERROR(dwError);
  89. }
  90. dwError = IKEInitiateIKENegotiation(
  91. pQMFilter,
  92. dwClientProcessId,
  93. hClientEvent,
  94. dwFlags,
  95. UdpEncapContext,
  96. phIKENegotiation
  97. );
  98. BAIL_ON_WIN32_ERROR(dwError);
  99. error:
  100. SPDRevertToSelf(bImpersonating);
  101. return (dwError);
  102. }
  103. DWORD
  104. RpcQueryIKENegotiationStatus(
  105. IKENEGOTIATION_HANDLE hIKENegotiation,
  106. DWORD dwVersion,
  107. SA_NEGOTIATION_STATUS_INFO * pNegotiationStatus
  108. )
  109. {
  110. DWORD dwError = 0;
  111. DWORD dwFlags = 0;
  112. BOOL bImpersonating = FALSE;
  113. dwError = SPDImpersonateClient(
  114. &bImpersonating
  115. );
  116. BAIL_ON_WIN32_ERROR(dwError);
  117. if (dwVersion) {
  118. dwError = ERROR_NOT_SUPPORTED;
  119. BAIL_ON_WIN32_ERROR(dwError);
  120. }
  121. ENTER_SPD_SECTION();
  122. dwError = ValidateSecurity(
  123. SPD_OBJECT_SERVER,
  124. SERVER_ACCESS_ADMINISTER,
  125. NULL,
  126. NULL
  127. );
  128. LEAVE_SPD_SECTION();
  129. BAIL_ON_WIN32_ERROR(dwError);
  130. dwError = ValidateQueryIKENegotiationStatus(
  131. hIKENegotiation,
  132. pNegotiationStatus
  133. );
  134. BAIL_ON_WIN32_ERROR(dwError);
  135. dwError = IKEQueryIKENegotiationStatus(
  136. hIKENegotiation,
  137. pNegotiationStatus,
  138. dwFlags
  139. );
  140. BAIL_ON_WIN32_ERROR(dwError);
  141. error:
  142. SPDRevertToSelf(bImpersonating);
  143. return (dwError);
  144. }
  145. DWORD
  146. RpcCloseIKENegotiationHandle(
  147. IKENEGOTIATION_HANDLE * phIKENegotiation
  148. )
  149. {
  150. DWORD dwError = 0;
  151. BOOL bImpersonating = FALSE;
  152. dwError = SPDImpersonateClient(
  153. &bImpersonating
  154. );
  155. BAIL_ON_WIN32_ERROR(dwError);
  156. ENTER_SPD_SECTION();
  157. dwError = ValidateSecurity(
  158. SPD_OBJECT_SERVER,
  159. SERVER_ACCESS_ADMINISTER,
  160. NULL,
  161. NULL
  162. );
  163. LEAVE_SPD_SECTION();
  164. BAIL_ON_WIN32_ERROR(dwError);
  165. dwError = ValidateCloseIKENegotiationHandle(phIKENegotiation);
  166. BAIL_ON_WIN32_ERROR(dwError);
  167. dwError = IKECloseIKENegotiationHandle(
  168. *phIKENegotiation
  169. );
  170. BAIL_ON_WIN32_ERROR(dwError);
  171. *phIKENegotiation = NULL;
  172. error:
  173. SPDRevertToSelf(bImpersonating);
  174. return (dwError);
  175. }
  176. DWORD
  177. RpcEnumMMSAs(
  178. STRING_HANDLE pServerName,
  179. DWORD dwVersion,
  180. PMM_SA_CONTAINER pMMTemplate,
  181. DWORD dwFlags,
  182. DWORD dwPreferredNumEntries,
  183. PMM_SA_CONTAINER * ppMMSAContainer,
  184. LPDWORD pdwNumEntries,
  185. LPDWORD pdwTotalMMsAvailable,
  186. LPDWORD pdwEnumHandle
  187. )
  188. {
  189. DWORD dwError = 0;
  190. PIPSEC_MM_SA pMMSAs = NULL;
  191. BOOL bImpersonating = FALSE;
  192. dwError = SPDImpersonateClient(
  193. &bImpersonating
  194. );
  195. BAIL_ON_WIN32_ERROR(dwError);
  196. if (dwVersion) {
  197. dwError = ERROR_NOT_SUPPORTED;
  198. BAIL_ON_WIN32_ERROR(dwError);
  199. }
  200. ENTER_SPD_SECTION();
  201. dwError = ValidateSecurity(
  202. SPD_OBJECT_SERVER,
  203. SERVER_ACCESS_ADMINISTER,
  204. NULL,
  205. NULL
  206. );
  207. LEAVE_SPD_SECTION();
  208. BAIL_ON_WIN32_ERROR(dwError);
  209. dwError = ValidateEnumMMSAs(
  210. pServerName,
  211. pMMTemplate,
  212. ppMMSAContainer,
  213. pdwNumEntries,
  214. pdwTotalMMsAvailable,
  215. pdwEnumHandle,
  216. dwFlags
  217. );
  218. BAIL_ON_WIN32_ERROR(dwError);
  219. if (pMMTemplate->pMMSAs && (pMMTemplate->pMMSAs->IpVersion != IPSEC_PROTOCOL_V4)) {
  220. dwError = ERROR_INVALID_LEVEL;
  221. BAIL_ON_WIN32_ERROR(dwError);
  222. }
  223. dwError= IKEEnumMMs(
  224. pMMTemplate->pMMSAs,
  225. &pMMSAs,
  226. pdwNumEntries,
  227. pdwTotalMMsAvailable,
  228. pdwEnumHandle,
  229. dwFlags
  230. );
  231. BAIL_ON_WIN32_ERROR(dwError);
  232. (*ppMMSAContainer)->pMMSAs = pMMSAs;
  233. (*ppMMSAContainer)->dwNumMMSAs = *pdwNumEntries;
  234. error:
  235. if (dwError != ERROR_SUCCESS) {
  236. if (ppMMSAContainer && *ppMMSAContainer) {
  237. (*ppMMSAContainer)->pMMSAs = NULL;
  238. (*ppMMSAContainer)->dwNumMMSAs = 0;
  239. }
  240. }
  241. SPDRevertToSelf(bImpersonating);
  242. return (dwError);
  243. }
  244. DWORD
  245. RpcDeleteMMSAs(
  246. STRING_HANDLE pServerName,
  247. DWORD dwVersion,
  248. PMM_SA_CONTAINER pMMTemplate,
  249. DWORD dwFlags
  250. )
  251. {
  252. DWORD dwError = 0;
  253. BOOL bImpersonating = FALSE;
  254. dwError = SPDImpersonateClient(
  255. &bImpersonating
  256. );
  257. BAIL_ON_WIN32_ERROR(dwError);
  258. if (dwVersion) {
  259. dwError = ERROR_NOT_SUPPORTED;
  260. BAIL_ON_WIN32_ERROR(dwError);
  261. }
  262. ENTER_SPD_SECTION();
  263. dwError = ValidateSecurity(
  264. SPD_OBJECT_SERVER,
  265. SERVER_ACCESS_ADMINISTER,
  266. NULL,
  267. NULL
  268. );
  269. LEAVE_SPD_SECTION();
  270. BAIL_ON_WIN32_ERROR(dwError);
  271. dwError = ValidateDeleteMMSAs(
  272. pServerName,
  273. pMMTemplate,
  274. dwFlags
  275. );
  276. BAIL_ON_WIN32_ERROR(dwError);
  277. if (pMMTemplate->pMMSAs && (pMMTemplate->pMMSAs->IpVersion != IPSEC_PROTOCOL_V4)) {
  278. dwError = ERROR_INVALID_LEVEL;
  279. BAIL_ON_WIN32_ERROR(dwError);
  280. }
  281. dwError= IKEDeleteAssociation(
  282. pMMTemplate->pMMSAs,
  283. dwFlags
  284. );
  285. BAIL_ON_WIN32_ERROR(dwError);
  286. error:
  287. SPDRevertToSelf(bImpersonating);
  288. return (dwError);
  289. }
  290. DWORD
  291. RpcQueryIKEStatistics(
  292. STRING_HANDLE pServerName,
  293. DWORD dwVersion,
  294. IKE_STATISTICS * pIKEStatistics
  295. )
  296. {
  297. DWORD dwError = 0;
  298. BOOL bImpersonating = FALSE;
  299. dwError = SPDImpersonateClient(
  300. &bImpersonating
  301. );
  302. BAIL_ON_WIN32_ERROR(dwError);
  303. if (dwVersion) {
  304. dwError = ERROR_NOT_SUPPORTED;
  305. BAIL_ON_WIN32_ERROR(dwError);
  306. }
  307. ENTER_SPD_SECTION();
  308. dwError = ValidateSecurity(
  309. SPD_OBJECT_SERVER,
  310. SERVER_ACCESS_ADMINISTER,
  311. NULL,
  312. NULL
  313. );
  314. LEAVE_SPD_SECTION();
  315. BAIL_ON_WIN32_ERROR(dwError);
  316. dwError = ValidateQueryIKEStatistics(
  317. pServerName,
  318. pIKEStatistics
  319. );
  320. BAIL_ON_WIN32_ERROR(dwError);
  321. dwError = IKEQueryStatistics(pIKEStatistics);
  322. BAIL_ON_WIN32_ERROR(dwError);
  323. error:
  324. SPDRevertToSelf(bImpersonating);
  325. return (dwError);
  326. }
  327. DWORD
  328. RpcRegisterIKENotifyClient(
  329. STRING_HANDLE pServerName,
  330. DWORD dwVersion,
  331. DWORD dwClientProcessId,
  332. ULONG uhClientEvent,
  333. PQM_SA_CONTAINER pQMSATemplateContainer,
  334. DWORD dwFlags,
  335. IKENOTIFY_HANDLE * phNotifyHandle
  336. )
  337. {
  338. DWORD dwError = 0;
  339. HANDLE hClientEvent = LongToHandle(uhClientEvent);
  340. BOOL bImpersonating = FALSE;
  341. dwError = SPDImpersonateClient(
  342. &bImpersonating
  343. );
  344. BAIL_ON_WIN32_ERROR(dwError);
  345. if (dwVersion) {
  346. dwError = ERROR_NOT_SUPPORTED;
  347. BAIL_ON_WIN32_ERROR(dwError);
  348. }
  349. ENTER_SPD_SECTION();
  350. dwError = ValidateSecurity(
  351. SPD_OBJECT_SERVER,
  352. SERVER_ACCESS_ADMINISTER,
  353. NULL,
  354. NULL
  355. );
  356. LEAVE_SPD_SECTION();
  357. BAIL_ON_WIN32_ERROR(dwError);
  358. dwError = ValidateRegisterIKENotifyClient(
  359. pServerName,
  360. dwClientProcessId,
  361. uhClientEvent,
  362. pQMSATemplateContainer,
  363. phNotifyHandle,
  364. dwFlags
  365. );
  366. BAIL_ON_WIN32_ERROR(dwError);
  367. if (pQMSATemplateContainer->pQMSAs &&
  368. (pQMSATemplateContainer->pQMSAs->IpsecQMFilter.IpVersion != IPSEC_PROTOCOL_V4)) {
  369. dwError = ERROR_INVALID_LEVEL;
  370. BAIL_ON_WIN32_ERROR(dwError);
  371. }
  372. dwError = IKERegisterNotifyClient(
  373. dwClientProcessId,
  374. hClientEvent,
  375. *pQMSATemplateContainer->pQMSAs,
  376. phNotifyHandle
  377. );
  378. BAIL_ON_WIN32_ERROR(dwError);
  379. error:
  380. SPDRevertToSelf(bImpersonating);
  381. return (dwError);
  382. }
  383. DWORD
  384. RpcQueryIKENotifyData(
  385. IKENOTIFY_HANDLE uhNotifyHandle,
  386. DWORD dwVersion,
  387. DWORD dwFlags,
  388. PQM_SA_CONTAINER * ppQMSAContainer,
  389. PDWORD pdwNumEntries
  390. )
  391. {
  392. DWORD dwError = 0;
  393. PIPSEC_QM_SA pQMSAs = NULL;
  394. BOOL bImpersonating = FALSE;
  395. dwError = SPDImpersonateClient(
  396. &bImpersonating
  397. );
  398. BAIL_ON_WIN32_ERROR(dwError);
  399. if (dwVersion) {
  400. dwError = ERROR_NOT_SUPPORTED;
  401. BAIL_ON_WIN32_ERROR(dwError);
  402. }
  403. ENTER_SPD_SECTION();
  404. dwError = ValidateSecurity(
  405. SPD_OBJECT_SERVER,
  406. SERVER_ACCESS_ADMINISTER,
  407. NULL,
  408. NULL
  409. );
  410. LEAVE_SPD_SECTION();
  411. BAIL_ON_WIN32_ERROR(dwError);
  412. dwError = ValidateQueryNotifyData(
  413. uhNotifyHandle,
  414. pdwNumEntries,
  415. ppQMSAContainer,
  416. dwFlags
  417. );
  418. BAIL_ON_WIN32_ERROR(dwError);
  419. dwError = IKEQuerySpiChange(
  420. uhNotifyHandle,
  421. pdwNumEntries,
  422. &pQMSAs
  423. );
  424. if ((dwError == ERROR_SUCCESS) ||
  425. (dwError == ERROR_MORE_DATA)) {
  426. (*ppQMSAContainer)->pQMSAs = pQMSAs;
  427. (*ppQMSAContainer)->dwNumQMSAs = *pdwNumEntries;
  428. SPDRevertToSelf(bImpersonating);
  429. return (dwError);
  430. }
  431. error:
  432. if (ppQMSAContainer && *ppQMSAContainer) {
  433. (*ppQMSAContainer)->pQMSAs = NULL;
  434. (*ppQMSAContainer)->dwNumQMSAs = 0;
  435. }
  436. if (pdwNumEntries) {
  437. *pdwNumEntries = 0;
  438. }
  439. SPDRevertToSelf(bImpersonating);
  440. return (dwError);
  441. }
  442. DWORD
  443. RpcCloseIKENotifyHandle(
  444. IKENOTIFY_HANDLE * phHandle
  445. )
  446. {
  447. DWORD dwError = 0;
  448. BOOL bImpersonating = FALSE;
  449. dwError = SPDImpersonateClient(
  450. &bImpersonating
  451. );
  452. BAIL_ON_WIN32_ERROR(dwError);
  453. ENTER_SPD_SECTION();
  454. dwError = ValidateSecurity(
  455. SPD_OBJECT_SERVER,
  456. SERVER_ACCESS_ADMINISTER,
  457. NULL,
  458. NULL
  459. );
  460. LEAVE_SPD_SECTION();
  461. BAIL_ON_WIN32_ERROR(dwError);
  462. dwError = ValidateCloseNotifyHandle(phHandle);
  463. BAIL_ON_WIN32_ERROR(dwError);
  464. dwError = IKECloseIKENotifyHandle(*phHandle);
  465. BAIL_ON_WIN32_ERROR(dwError);
  466. *phHandle = NULL;
  467. error:
  468. SPDRevertToSelf(bImpersonating);
  469. return (dwError);
  470. }
  471. DWORD
  472. RpcAddSAs(
  473. STRING_HANDLE pServerName,
  474. DWORD dwVersion,
  475. IPSEC_SA_DIRECTION SADirection,
  476. PIPSEC_QM_POLICY_CONTAINER pQMPolicyContainer,
  477. PQM_FILTER_CONTAINER pQMFilterContainer,
  478. ULONG * puhLarvalContext,
  479. DWORD dwInboundKeyMatLen,
  480. BYTE * pInboundKeyMat,
  481. DWORD dwOutboundKeyMatLen,
  482. BYTE *pOutboundKeyMat,
  483. BYTE *pContextInfo,
  484. UDP_ENCAP_INFO EncapInfo,
  485. DWORD dwFlags)
  486. {
  487. DWORD dwError = 0;
  488. HANDLE hLarvalContext = NULL;
  489. PIPSEC_QM_FILTER pQMFilter = NULL;
  490. PIPSEC_QM_OFFER pQMOffer = NULL;
  491. BOOL bImpersonating = FALSE;
  492. dwError = SPDImpersonateClient(
  493. &bImpersonating
  494. );
  495. BAIL_ON_WIN32_ERROR(dwError);
  496. if (dwVersion) {
  497. dwError = ERROR_NOT_SUPPORTED;
  498. BAIL_ON_WIN32_ERROR(dwError);
  499. }
  500. ENTER_SPD_SECTION();
  501. dwError = ValidateSecurity(
  502. SPD_OBJECT_SERVER,
  503. SERVER_ACCESS_ADMINISTER,
  504. NULL,
  505. NULL
  506. );
  507. LEAVE_SPD_SECTION();
  508. BAIL_ON_WIN32_ERROR(dwError);
  509. dwError=ValidateIPSecAddSA(pServerName,
  510. SADirection,
  511. pQMPolicyContainer,
  512. pQMFilterContainer,
  513. puhLarvalContext,
  514. dwInboundKeyMatLen,
  515. pInboundKeyMat,
  516. dwOutboundKeyMatLen,
  517. pOutboundKeyMat,
  518. pContextInfo,
  519. EncapInfo,
  520. dwFlags);
  521. BAIL_ON_WIN32_ERROR(dwError);
  522. hLarvalContext = LongToHandle(*puhLarvalContext);
  523. pQMFilter = pQMFilterContainer->pQMFilters;
  524. pQMOffer = pQMPolicyContainer->pPolicies->pOffers;
  525. if (pQMFilter && (pQMFilter->IpVersion != IPSEC_PROTOCOL_V4)) {
  526. dwError = ERROR_INVALID_LEVEL;
  527. BAIL_ON_WIN32_ERROR(dwError);
  528. }
  529. dwError=IKEAddSAs(
  530. SADirection,
  531. pQMOffer,
  532. pQMFilter,
  533. &hLarvalContext,
  534. dwInboundKeyMatLen,
  535. pInboundKeyMat,
  536. dwOutboundKeyMatLen,
  537. pOutboundKeyMat,
  538. pContextInfo,
  539. EncapInfo,
  540. dwFlags);
  541. BAIL_ON_WIN32_ERROR(dwError);
  542. *puhLarvalContext = HandleToLong(hLarvalContext);
  543. error:
  544. SPDRevertToSelf(bImpersonating);
  545. return (dwError);
  546. }
  547. DWORD
  548. RpcGetConfigurationVariables(
  549. STRING_HANDLE pServerName,
  550. IKE_CONFIG *pIKEConfig
  551. )
  552. {
  553. DWORD dwError = 0;
  554. BOOL bImpersonating = FALSE;
  555. dwError = SPDImpersonateClient(
  556. &bImpersonating
  557. );
  558. BAIL_ON_WIN32_ERROR(dwError);
  559. ENTER_SPD_SECTION();
  560. dwError = ValidateSecurity(
  561. SPD_OBJECT_SERVER,
  562. SERVER_ACCESS_ADMINISTER,
  563. NULL,
  564. NULL
  565. );
  566. LEAVE_SPD_SECTION();
  567. BAIL_ON_WIN32_ERROR(dwError);
  568. dwError=ValidateGetConfigurationVariables(pServerName,
  569. pIKEConfig);
  570. BAIL_ON_WIN32_ERROR(dwError);
  571. dwError = IKEGetConfigurationVariables(pIKEConfig);
  572. BAIL_ON_WIN32_ERROR(dwError);
  573. error:
  574. SPDRevertToSelf(bImpersonating);
  575. return dwError;
  576. }
  577. DWORD
  578. RpcSetConfigurationVariables(
  579. STRING_HANDLE pServerName,
  580. IKE_CONFIG IKEConfig
  581. )
  582. {
  583. DWORD dwError = 0;
  584. BOOL bImpersonating = FALSE;
  585. dwError = SPDImpersonateClient(
  586. &bImpersonating
  587. );
  588. BAIL_ON_WIN32_ERROR(dwError);
  589. ENTER_SPD_SECTION();
  590. dwError = ValidateSecurity(
  591. SPD_OBJECT_SERVER,
  592. SERVER_ACCESS_ADMINISTER,
  593. NULL,
  594. NULL
  595. );
  596. LEAVE_SPD_SECTION();
  597. BAIL_ON_WIN32_ERROR(dwError);
  598. dwError=ValidateSetConfigurationVariables(pServerName,
  599. IKEConfig);
  600. BAIL_ON_WIN32_ERROR(dwError);
  601. dwError = IKESetConfigurationVariables(IKEConfig);
  602. BAIL_ON_WIN32_ERROR(dwError);
  603. error:
  604. SPDRevertToSelf(bImpersonating);
  605. return dwError;
  606. }