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.

306 lines
6.2 KiB

  1. #include "precomp.h"
  2. LPWSTR gpszIpsecMMPoliciesKey =
  3. L"SOFTWARE\\Microsoft\\IPSec\\MM Policies";
  4. DWORD
  5. PersistMMPolicy(
  6. PIPSEC_MM_POLICY pMMPolicy
  7. )
  8. {
  9. DWORD dwError = 0;
  10. HKEY hRegistryKey = NULL;
  11. DWORD dwDisposition = 0;
  12. dwError = RegCreateKeyExW(
  13. HKEY_LOCAL_MACHINE,
  14. gpszIpsecMMPoliciesKey,
  15. 0,
  16. NULL,
  17. 0,
  18. KEY_ALL_ACCESS,
  19. NULL,
  20. &hRegistryKey,
  21. &dwDisposition
  22. );
  23. BAIL_ON_WIN32_ERROR(dwError);
  24. dwError = SPDWriteMMPolicy(
  25. hRegistryKey,
  26. pMMPolicy
  27. );
  28. BAIL_ON_WIN32_ERROR(dwError);
  29. cleanup:
  30. if (hRegistryKey) {
  31. RegCloseKey(hRegistryKey);
  32. }
  33. return (dwError);
  34. error:
  35. if (hRegistryKey) {
  36. (VOID) SPDPurgeMMPolicy(
  37. pMMPolicy->gPolicyID
  38. );
  39. }
  40. goto cleanup;
  41. }
  42. DWORD
  43. SPDWriteMMPolicy(
  44. HKEY hParentRegKey,
  45. PIPSEC_MM_POLICY pMMPolicy
  46. )
  47. {
  48. DWORD dwError = 0;
  49. WCHAR szPolicyID[MAX_PATH];
  50. LPWSTR pszStringUuid = NULL;
  51. HKEY hRegKey = NULL;
  52. DWORD dwDisposition = 0;
  53. LPBYTE pBuffer = NULL;
  54. DWORD dwBufferSize = 0;
  55. szPolicyID[0] = L'\0';
  56. dwError = UuidToString(
  57. &pMMPolicy->gPolicyID,
  58. &pszStringUuid
  59. );
  60. BAIL_ON_WIN32_ERROR(dwError);
  61. wcscpy(szPolicyID, L"{");
  62. wcscat(szPolicyID, pszStringUuid);
  63. wcscat(szPolicyID, L"}");
  64. dwError = RegCreateKeyExW(
  65. hParentRegKey,
  66. szPolicyID,
  67. 0,
  68. NULL,
  69. 0,
  70. KEY_ALL_ACCESS,
  71. NULL,
  72. &hRegKey,
  73. &dwDisposition
  74. );
  75. BAIL_ON_WIN32_ERROR(dwError);
  76. dwError = RegSetValueExW(
  77. hRegKey,
  78. L"PolicyID",
  79. 0,
  80. REG_SZ,
  81. (LPBYTE) szPolicyID,
  82. (wcslen(szPolicyID) + 1)*sizeof(WCHAR)
  83. );
  84. BAIL_ON_WIN32_ERROR(dwError);
  85. dwError = RegSetValueExW(
  86. hRegKey,
  87. L"PolicyName",
  88. 0,
  89. REG_SZ,
  90. (LPBYTE) pMMPolicy->pszPolicyName,
  91. (wcslen(pMMPolicy->pszPolicyName) + 1)*sizeof(WCHAR)
  92. );
  93. BAIL_ON_WIN32_ERROR(dwError);
  94. dwError = RegSetValueExW(
  95. hRegKey,
  96. L"Flags",
  97. 0,
  98. REG_DWORD,
  99. (LPBYTE)&pMMPolicy->dwFlags,
  100. sizeof(DWORD)
  101. );
  102. BAIL_ON_WIN32_ERROR(dwError);
  103. dwError = RegSetValueExW(
  104. hRegKey,
  105. L"SoftSAExpirationTime",
  106. 0,
  107. REG_DWORD,
  108. (LPBYTE)&pMMPolicy->uSoftSAExpirationTime,
  109. sizeof(DWORD)
  110. );
  111. BAIL_ON_WIN32_ERROR(dwError);
  112. dwError = MarshallMMOffers(
  113. pMMPolicy->pOffers,
  114. pMMPolicy->dwOfferCount,
  115. &pBuffer,
  116. &dwBufferSize
  117. );
  118. BAIL_ON_WIN32_ERROR(dwError);
  119. dwError = RegSetValueExW(
  120. hRegKey,
  121. L"Offers",
  122. 0,
  123. REG_BINARY,
  124. (LPBYTE) pBuffer,
  125. dwBufferSize
  126. );
  127. BAIL_ON_WIN32_ERROR(dwError);
  128. cleanup:
  129. if (pszStringUuid) {
  130. RpcStringFree(&pszStringUuid);
  131. }
  132. if (hRegKey) {
  133. RegCloseKey(hRegKey);
  134. }
  135. if (pBuffer) {
  136. FreeSPDMem(pBuffer);
  137. }
  138. return (dwError);
  139. error:
  140. goto cleanup;
  141. }
  142. DWORD
  143. MarshallMMOffers(
  144. PIPSEC_MM_OFFER pOffers,
  145. DWORD dwOfferCount,
  146. LPBYTE * ppBuffer,
  147. PDWORD pdwBufferSize
  148. )
  149. {
  150. DWORD dwError = 0;
  151. LPBYTE pBuffer = NULL;
  152. DWORD dwBufferSize = 0;
  153. LPBYTE pMem = NULL;
  154. static const GUID GUID_IPSEC_MM_OFFER_VER1 =
  155. { 0xabcd0001, 0x0001, 0x0001, { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 } };
  156. dwBufferSize = sizeof(GUID) +
  157. sizeof(DWORD) +
  158. sizeof(DWORD) +
  159. sizeof(IPSEC_MM_OFFER)*dwOfferCount;
  160. pBuffer = (LPBYTE) AllocSPDMem(
  161. dwBufferSize
  162. );
  163. if (!pBuffer) {
  164. dwError = ERROR_OUTOFMEMORY;
  165. BAIL_ON_WIN32_ERROR(dwError);
  166. }
  167. pMem = pBuffer;
  168. memcpy(
  169. pMem,
  170. (LPBYTE) &GUID_IPSEC_MM_OFFER_VER1,
  171. sizeof(GUID)
  172. );
  173. pMem += sizeof(GUID);
  174. memcpy(
  175. pMem,
  176. (LPBYTE) &dwBufferSize,
  177. sizeof(DWORD)
  178. );
  179. pMem += sizeof(DWORD);
  180. memcpy(
  181. pMem,
  182. (LPBYTE) &dwOfferCount,
  183. sizeof(DWORD)
  184. );
  185. pMem += sizeof(DWORD);
  186. memcpy(
  187. pMem,
  188. (LPBYTE) pOffers,
  189. sizeof(IPSEC_MM_OFFER)*dwOfferCount
  190. );
  191. *ppBuffer = pBuffer;
  192. *pdwBufferSize = dwBufferSize;
  193. return (dwError);
  194. error:
  195. *ppBuffer = NULL;
  196. *pdwBufferSize = 0;
  197. return (dwError);
  198. }
  199. DWORD
  200. SPDPurgeMMPolicy(
  201. GUID gMMPolicyID
  202. )
  203. {
  204. DWORD dwError = 0;
  205. HKEY hParentRegKey = NULL;
  206. DWORD dwDisposition = 0;
  207. WCHAR szPolicyID[MAX_PATH];
  208. LPWSTR pszStringUuid = NULL;
  209. dwError = RegCreateKeyExW(
  210. HKEY_LOCAL_MACHINE,
  211. gpszIpsecMMPoliciesKey,
  212. 0,
  213. NULL,
  214. 0,
  215. KEY_ALL_ACCESS,
  216. NULL,
  217. &hParentRegKey,
  218. &dwDisposition
  219. );
  220. BAIL_ON_WIN32_ERROR(dwError);
  221. szPolicyID[0] = L'\0';
  222. dwError = UuidToString(
  223. &gMMPolicyID,
  224. &pszStringUuid
  225. );
  226. BAIL_ON_WIN32_ERROR(dwError);
  227. wcscpy(szPolicyID, L"{");
  228. wcscat(szPolicyID, pszStringUuid);
  229. wcscat(szPolicyID, L"}");
  230. dwError = RegDeleteKeyW(
  231. hParentRegKey,
  232. szPolicyID
  233. );
  234. BAIL_ON_WIN32_ERROR(dwError);
  235. error:
  236. if (hParentRegKey) {
  237. RegCloseKey(hParentRegKey);
  238. }
  239. if (pszStringUuid) {
  240. RpcStringFree(&pszStringUuid);
  241. }
  242. return(dwError);
  243. }