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.

397 lines
9.6 KiB

  1. #include "precomp.h"
  2. DWORD
  3. ImportPoliciesFromFile(
  4. HANDLE hSrcPolicyStore,
  5. HANDLE hDesPolicyStore
  6. )
  7. {
  8. DWORD dwError = 0;
  9. PIPSEC_POLICY_STORE pSrcPolicyStore = NULL;
  10. PIPSEC_POLICY_DATA * ppIpsecPolicyData = NULL;
  11. DWORD dwNumPolicyObjects = 0;
  12. PIPSEC_POLICY_STORE pDesPolicyStore = NULL;
  13. pSrcPolicyStore = (PIPSEC_POLICY_STORE) hSrcPolicyStore;
  14. dwError = EnablePrivilege(
  15. SE_RESTORE_NAME
  16. );
  17. BAIL_ON_WIN32_ERROR(dwError);
  18. dwError = RegRestoreKeyW(
  19. pSrcPolicyStore->hRegistryKey,
  20. pSrcPolicyStore->pszFileName,
  21. 0
  22. );
  23. BAIL_ON_WIN32_ERROR(dwError);
  24. dwError = DeleteDuplicatePolicyDataBeforeImport(
  25. pSrcPolicyStore,
  26. hDesPolicyStore
  27. );
  28. BAIL_ON_WIN32_ERROR(dwError);
  29. dwError = ImportFilterDataFromFile(
  30. pSrcPolicyStore,
  31. hDesPolicyStore
  32. );
  33. dwError = ImportNegPolDataFromFile(
  34. pSrcPolicyStore,
  35. hDesPolicyStore
  36. );
  37. dwError = ImportISAKMPDataFromFile(
  38. pSrcPolicyStore,
  39. hDesPolicyStore
  40. );
  41. BAIL_ON_WIN32_ERROR(dwError);
  42. dwError = ImportPolicyDataFromFile(
  43. pSrcPolicyStore,
  44. hDesPolicyStore,
  45. &ppIpsecPolicyData,
  46. &dwNumPolicyObjects
  47. );
  48. BAIL_ON_WIN32_ERROR(dwError);
  49. dwError = ImportNFADataFromFile(
  50. pSrcPolicyStore,
  51. hDesPolicyStore,
  52. ppIpsecPolicyData,
  53. dwNumPolicyObjects
  54. );
  55. BAIL_ON_WIN32_ERROR(dwError);
  56. pDesPolicyStore = (PIPSEC_POLICY_STORE) hDesPolicyStore;
  57. if (pDesPolicyStore->dwProvider == IPSEC_REGISTRY_PROVIDER) {
  58. (VOID) RegPingPASvcForActivePolicy(
  59. pDesPolicyStore->hRegistryKey,
  60. pDesPolicyStore->pszIpsecRootContainer,
  61. pDesPolicyStore->pszLocationName
  62. );
  63. }
  64. error:
  65. if (ppIpsecPolicyData) {
  66. FreeMulIpsecPolicyData(
  67. ppIpsecPolicyData,
  68. dwNumPolicyObjects
  69. );
  70. }
  71. FlushRegSaveKey(
  72. pSrcPolicyStore->hRegistryKey
  73. );
  74. return (dwError);
  75. }
  76. DWORD
  77. DeleteDuplicatePolicyDataBeforeImport(
  78. PIPSEC_POLICY_STORE pSrcPolicyStore,
  79. HANDLE hDesPolicyStore
  80. )
  81. {
  82. DWORD dwError = 0;
  83. PIPSEC_POLICY_DATA * ppIpsecPolicyData = NULL;
  84. PIPSEC_POLICY_DATA pIpsecPolicyData = NULL;
  85. DWORD dwNumPolicyObjects = 0;
  86. DWORD i = 0;
  87. dwError = RegEnumPolicyData(
  88. pSrcPolicyStore->hRegistryKey,
  89. pSrcPolicyStore->pszIpsecRootContainer,
  90. &ppIpsecPolicyData,
  91. &dwNumPolicyObjects
  92. );
  93. for (i = 0; i < dwNumPolicyObjects; i++) {
  94. pIpsecPolicyData = * (ppIpsecPolicyData + i);
  95. dwError = VerifyPolicyDataExistence(
  96. hDesPolicyStore,
  97. pIpsecPolicyData->PolicyIdentifier
  98. );
  99. if (!dwError) {
  100. dwError = IPSecDeletePolicy(
  101. hDesPolicyStore,
  102. pIpsecPolicyData
  103. );
  104. }
  105. }
  106. if (ppIpsecPolicyData) {
  107. FreeMulIpsecPolicyData(
  108. ppIpsecPolicyData,
  109. dwNumPolicyObjects
  110. );
  111. }
  112. dwError = ERROR_SUCCESS;
  113. return (dwError);
  114. }
  115. DWORD
  116. IPSecDeletePolicy(
  117. HANDLE hPolicyStore,
  118. PIPSEC_POLICY_DATA pIpsecPolicyData
  119. )
  120. {
  121. DWORD dwError = 0;
  122. PIPSEC_POLICY_STORE pPolicyStore = NULL;
  123. pPolicyStore = (PIPSEC_POLICY_STORE) hPolicyStore;
  124. switch (pPolicyStore->dwProvider) {
  125. case IPSEC_REGISTRY_PROVIDER:
  126. dwError = RegDeletePolicy(
  127. pPolicyStore->hRegistryKey,
  128. pPolicyStore->pszIpsecRootContainer,
  129. pPolicyStore->pszLocationName,
  130. pIpsecPolicyData->PolicyIdentifier
  131. );
  132. BAIL_ON_WIN32_ERROR (dwError);
  133. break;
  134. case IPSEC_DIRECTORY_PROVIDER:
  135. dwError = DirDeletePolicyBeforeImport(
  136. pPolicyStore->hLdapBindHandle,
  137. pPolicyStore->pszIpsecRootContainer,
  138. pIpsecPolicyData->PolicyIdentifier
  139. );
  140. BAIL_ON_WIN32_ERROR (dwError);
  141. break;
  142. }
  143. error:
  144. return (dwError);
  145. }
  146. DWORD
  147. RegDeletePolicy(
  148. HKEY hRegistryKey,
  149. LPWSTR pszIpsecRootContainer,
  150. LPWSTR pszLocationName,
  151. GUID PolicyGUID
  152. )
  153. {
  154. DWORD dwError = 0;
  155. PIPSEC_POLICY_DATA pIpsecPolicyData = NULL;
  156. PIPSEC_NFA_DATA * ppIpsecNFAData = NULL;
  157. DWORD dwNumNFAObjects = 0;
  158. DWORD i = 0;
  159. PIPSEC_NFA_DATA pIpsecNFAData = NULL;
  160. dwError = RegGetPolicyData(
  161. hRegistryKey,
  162. pszIpsecRootContainer,
  163. PolicyGUID,
  164. &pIpsecPolicyData
  165. );
  166. BAIL_ON_WIN32_ERROR(dwError);
  167. dwError = RegEnumNFAData(
  168. hRegistryKey,
  169. pszIpsecRootContainer,
  170. PolicyGUID,
  171. &ppIpsecNFAData,
  172. &dwNumNFAObjects
  173. );
  174. for (i = 0; i < dwNumNFAObjects; i++) {
  175. pIpsecNFAData = *(ppIpsecNFAData + i);
  176. dwError = RegDeleteNFAData(
  177. hRegistryKey,
  178. pszIpsecRootContainer,
  179. PolicyGUID,
  180. pszLocationName,
  181. pIpsecNFAData
  182. );
  183. dwError = RegDeleteDynamicDefaultNegPolData(
  184. hRegistryKey,
  185. pszIpsecRootContainer,
  186. pszLocationName,
  187. pIpsecNFAData->NegPolIdentifier
  188. );
  189. }
  190. dwError = RegDeletePolicyData(
  191. hRegistryKey,
  192. pszIpsecRootContainer,
  193. pIpsecPolicyData
  194. );
  195. BAIL_ON_WIN32_ERROR(dwError);
  196. dwError = RegDeleteISAKMPData(
  197. hRegistryKey,
  198. pszIpsecRootContainer,
  199. pIpsecPolicyData->ISAKMPIdentifier
  200. );
  201. BAIL_ON_WIN32_ERROR(dwError);
  202. error:
  203. if (ppIpsecNFAData) {
  204. FreeMulIpsecNFAData(
  205. ppIpsecNFAData,
  206. dwNumNFAObjects
  207. );
  208. }
  209. if (pIpsecPolicyData) {
  210. FreeIpsecPolicyData(
  211. pIpsecPolicyData
  212. );
  213. }
  214. return (dwError);
  215. }
  216. DWORD
  217. DirDeletePolicyBeforeImport(
  218. HLDAP hLdapBindHandle,
  219. LPWSTR pszIpsecRootContainer,
  220. GUID PolicyIdentifier
  221. )
  222. {
  223. DWORD dwError = 0;
  224. PIPSEC_NFA_DATA * ppIpsecNFAData = NULL;
  225. PIPSEC_NFA_DATA pIpsecNFAData = NULL;
  226. PIPSEC_POLICY_DATA pIpsecPolicyData = NULL;
  227. DWORD dwNumNFAObjects = 0;
  228. DWORD i = 0;
  229. dwError = DirGetPolicyData(
  230. hLdapBindHandle,
  231. pszIpsecRootContainer,
  232. PolicyIdentifier,
  233. &pIpsecPolicyData
  234. );
  235. BAIL_ON_WIN32_ERROR (dwError);
  236. dwError = DirEnumNFAData(
  237. hLdapBindHandle,
  238. pszIpsecRootContainer,
  239. PolicyIdentifier,
  240. &ppIpsecNFAData,
  241. &dwNumNFAObjects
  242. );
  243. for (i = 0; i < dwNumNFAObjects; i++) {
  244. pIpsecNFAData = *(ppIpsecNFAData + i);
  245. dwError = DirDeleteNFAData(
  246. hLdapBindHandle,
  247. pszIpsecRootContainer,
  248. PolicyIdentifier,
  249. pIpsecNFAData
  250. );
  251. dwError = DirDeleteDynamicDefaultNegPolData(
  252. hLdapBindHandle,
  253. pszIpsecRootContainer,
  254. pIpsecNFAData->NegPolIdentifier
  255. );
  256. }
  257. dwError = DirDeleteISAKMPData(
  258. hLdapBindHandle,
  259. pszIpsecRootContainer,
  260. pIpsecPolicyData->ISAKMPIdentifier
  261. );
  262. BAIL_ON_WIN32_ERROR (dwError);
  263. error:
  264. if (ppIpsecNFAData) {
  265. FreeMulIpsecNFAData(
  266. ppIpsecNFAData,
  267. dwNumNFAObjects
  268. );
  269. }
  270. if (pIpsecPolicyData) {
  271. FreeIpsecPolicyData(
  272. pIpsecPolicyData
  273. );
  274. }
  275. return (dwError);
  276. }
  277. DWORD
  278. DirDeleteDynamicDefaultNegPolData(
  279. HLDAP hLdapBindHandle,
  280. LPWSTR pszIpsecRootContainer,
  281. GUID NegPolGUID
  282. )
  283. {
  284. DWORD dwError = 0;
  285. PIPSEC_NEGPOL_DATA pIpsecNegPolData = NULL;
  286. dwError = DirGetNegPolData(
  287. hLdapBindHandle,
  288. pszIpsecRootContainer,
  289. NegPolGUID,
  290. &pIpsecNegPolData
  291. );
  292. BAIL_ON_WIN32_ERROR(dwError);
  293. if (!memcmp(
  294. &(pIpsecNegPolData->NegPolType),
  295. &(GUID_NEGOTIATION_TYPE_DEFAULT),
  296. sizeof(GUID))) {
  297. dwError = DirDeleteNegPolData(
  298. hLdapBindHandle,
  299. pszIpsecRootContainer,
  300. NegPolGUID
  301. );
  302. BAIL_ON_WIN32_ERROR(dwError);
  303. }
  304. error:
  305. if (pIpsecNegPolData) {
  306. FreeIpsecNegPolData(
  307. pIpsecNegPolData
  308. );
  309. }
  310. return (dwError);
  311. }