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.

376 lines
8.0 KiB

  1. #include "precomp.h"
  2. LPWSTR gpszIpsecMMFiltersKey =
  3. L"SOFTWARE\\Microsoft\\IPSec\\MM Filters";
  4. DWORD
  5. PersistMMFilter(
  6. GUID gFilterID,
  7. PMM_FILTER pMMFilter
  8. )
  9. {
  10. DWORD dwError = 0;
  11. HKEY hRegistryKey = NULL;
  12. DWORD dwDisposition = 0;
  13. dwError = RegCreateKeyExW(
  14. HKEY_LOCAL_MACHINE,
  15. gpszIpsecMMFiltersKey,
  16. 0,
  17. NULL,
  18. 0,
  19. KEY_ALL_ACCESS,
  20. NULL,
  21. &hRegistryKey,
  22. &dwDisposition
  23. );
  24. BAIL_ON_WIN32_ERROR(dwError);
  25. dwError = SPDWriteMMFilter(
  26. hRegistryKey,
  27. gFilterID,
  28. pMMFilter
  29. );
  30. BAIL_ON_WIN32_ERROR(dwError);
  31. cleanup:
  32. if (hRegistryKey) {
  33. RegCloseKey(hRegistryKey);
  34. }
  35. return (dwError);
  36. error:
  37. if (hRegistryKey) {
  38. (VOID) SPDPurgeMMFilter(
  39. gFilterID
  40. );
  41. }
  42. goto cleanup;
  43. }
  44. DWORD
  45. SPDWriteMMFilter(
  46. HKEY hParentRegKey,
  47. GUID gFilterID,
  48. PMM_FILTER pMMFilter
  49. )
  50. {
  51. DWORD dwError = 0;
  52. WCHAR szFilterID[MAX_PATH];
  53. WCHAR szPolicyID[MAX_PATH];
  54. WCHAR szAuthID[MAX_PATH];
  55. LPWSTR pszStringUuid = NULL;
  56. LPWSTR pszPolicyUuid = NULL;
  57. LPWSTR pszAuthUuid = NULL;
  58. HKEY hRegKey = NULL;
  59. DWORD dwDisposition = 0;
  60. LPBYTE pBuffer = NULL;
  61. DWORD dwBufferSize = 0;
  62. DWORD dwInterfaceType = 0;
  63. DWORD dwMirrored = 0;
  64. szFilterID[0] = L'\0';
  65. szPolicyID[0] = L'\0';
  66. szAuthID[0] = L'\0';
  67. dwError = UuidToString(
  68. &gFilterID,
  69. &pszStringUuid
  70. );
  71. BAIL_ON_WIN32_ERROR(dwError);
  72. wcscpy(szFilterID, L"{");
  73. wcscat(szFilterID, pszStringUuid);
  74. wcscat(szFilterID, L"}");
  75. dwError = UuidToString(
  76. &pMMFilter->gPolicyID,
  77. &pszPolicyUuid
  78. );
  79. BAIL_ON_WIN32_ERROR(dwError);
  80. wcscpy(szPolicyID, L"{");
  81. wcscat(szPolicyID, pszPolicyUuid);
  82. wcscat(szPolicyID, L"}");
  83. dwError = UuidToString(
  84. &pMMFilter->gMMAuthID,
  85. &pszAuthUuid
  86. );
  87. BAIL_ON_WIN32_ERROR(dwError);
  88. wcscpy(szAuthID, L"{");
  89. wcscat(szAuthID, pszAuthUuid);
  90. wcscat(szAuthID, L"}");
  91. dwError = RegCreateKeyExW(
  92. hParentRegKey,
  93. szFilterID,
  94. 0,
  95. NULL,
  96. 0,
  97. KEY_ALL_ACCESS,
  98. NULL,
  99. &hRegKey,
  100. &dwDisposition
  101. );
  102. BAIL_ON_WIN32_ERROR(dwError);
  103. dwError = RegSetValueExW(
  104. hRegKey,
  105. L"FilterID",
  106. 0,
  107. REG_SZ,
  108. (LPBYTE) szFilterID,
  109. (wcslen(szFilterID) + 1)*sizeof(WCHAR)
  110. );
  111. BAIL_ON_WIN32_ERROR(dwError);
  112. dwError = RegSetValueExW(
  113. hRegKey,
  114. L"FilterName",
  115. 0,
  116. REG_SZ,
  117. (LPBYTE) pMMFilter->pszFilterName,
  118. (wcslen(pMMFilter->pszFilterName) + 1)*sizeof(WCHAR)
  119. );
  120. BAIL_ON_WIN32_ERROR(dwError);
  121. dwInterfaceType = (DWORD) pMMFilter->InterfaceType;
  122. dwError = RegSetValueExW(
  123. hRegKey,
  124. L"InterfaceType",
  125. 0,
  126. REG_DWORD,
  127. (LPBYTE)&dwInterfaceType,
  128. sizeof(DWORD)
  129. );
  130. BAIL_ON_WIN32_ERROR(dwError);
  131. dwMirrored = (DWORD) pMMFilter->bCreateMirror;
  132. dwError = RegSetValueExW(
  133. hRegKey,
  134. L"Mirrored",
  135. 0,
  136. REG_DWORD,
  137. (LPBYTE)&dwMirrored,
  138. sizeof(DWORD)
  139. );
  140. BAIL_ON_WIN32_ERROR(dwError);
  141. dwError = RegSetValueExW(
  142. hRegKey,
  143. L"Flags",
  144. 0,
  145. REG_DWORD,
  146. (LPBYTE)&pMMFilter->dwFlags,
  147. sizeof(DWORD)
  148. );
  149. BAIL_ON_WIN32_ERROR(dwError);
  150. dwError = MarshallMMFilterBuffer(
  151. pMMFilter,
  152. &pBuffer,
  153. &dwBufferSize
  154. );
  155. BAIL_ON_WIN32_ERROR(dwError);
  156. dwError = RegSetValueExW(
  157. hRegKey,
  158. L"MM Filter Buffer",
  159. 0,
  160. REG_BINARY,
  161. (LPBYTE) pBuffer,
  162. dwBufferSize
  163. );
  164. BAIL_ON_WIN32_ERROR(dwError);
  165. dwError = RegSetValueExW(
  166. hRegKey,
  167. L"PolicyID",
  168. 0,
  169. REG_SZ,
  170. (LPBYTE) szPolicyID,
  171. (wcslen(szPolicyID) + 1)*sizeof(WCHAR)
  172. );
  173. BAIL_ON_WIN32_ERROR(dwError);
  174. dwError = RegSetValueExW(
  175. hRegKey,
  176. L"AuthID",
  177. 0,
  178. REG_SZ,
  179. (LPBYTE) szAuthID,
  180. (wcslen(szAuthID) + 1)*sizeof(WCHAR)
  181. );
  182. BAIL_ON_WIN32_ERROR(dwError);
  183. cleanup:
  184. if (pszStringUuid) {
  185. RpcStringFree(&pszStringUuid);
  186. }
  187. if (pszPolicyUuid) {
  188. RpcStringFree(&pszPolicyUuid);
  189. }
  190. if (pszAuthUuid) {
  191. RpcStringFree(&pszAuthUuid);
  192. }
  193. if (hRegKey) {
  194. RegCloseKey(hRegKey);
  195. }
  196. if (pBuffer) {
  197. FreeSPDMem(pBuffer);
  198. }
  199. return (dwError);
  200. error:
  201. goto cleanup;
  202. }
  203. DWORD
  204. MarshallMMFilterBuffer(
  205. PMM_FILTER pMMFilter,
  206. LPBYTE * ppBuffer,
  207. PDWORD pdwBufferSize
  208. )
  209. {
  210. DWORD dwError = 0;
  211. LPBYTE pBuffer = NULL;
  212. DWORD dwBufferSize = 0;
  213. LPBYTE pMem = NULL;
  214. static const GUID GUID_IPSEC_MM_FILTER_VER1 =
  215. { 0xabcd0004, 0x0001, 0x0001, { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 } };
  216. dwBufferSize = sizeof(GUID) +
  217. sizeof(DWORD) +
  218. sizeof(ADDR) +
  219. sizeof(ADDR);
  220. pBuffer = (LPBYTE) AllocSPDMem(
  221. dwBufferSize
  222. );
  223. if (!pBuffer) {
  224. dwError = ERROR_OUTOFMEMORY;
  225. BAIL_ON_WIN32_ERROR(dwError);
  226. }
  227. pMem = pBuffer;
  228. memcpy(
  229. pMem,
  230. (LPBYTE) &GUID_IPSEC_MM_FILTER_VER1,
  231. sizeof(GUID)
  232. );
  233. pMem += sizeof(GUID);
  234. memcpy(
  235. pMem,
  236. (LPBYTE) &dwBufferSize,
  237. sizeof(DWORD)
  238. );
  239. pMem += sizeof(DWORD);
  240. memcpy(
  241. pMem,
  242. (LPBYTE) &pMMFilter->SrcAddr,
  243. sizeof(ADDR)
  244. );
  245. pMem += sizeof(ADDR);
  246. memcpy(
  247. pMem,
  248. (LPBYTE) &pMMFilter->DesAddr,
  249. sizeof(ADDR)
  250. );
  251. pMem += sizeof(ADDR);
  252. *ppBuffer = pBuffer;
  253. *pdwBufferSize = dwBufferSize;
  254. return (dwError);
  255. error:
  256. *ppBuffer = NULL;
  257. *pdwBufferSize = 0;
  258. return (dwError);
  259. }
  260. DWORD
  261. SPDPurgeMMFilter(
  262. GUID gMMFilterID
  263. )
  264. {
  265. DWORD dwError = 0;
  266. HKEY hParentRegKey = NULL;
  267. DWORD dwDisposition = 0;
  268. WCHAR szFilterID[MAX_PATH];
  269. LPWSTR pszStringUuid = NULL;
  270. dwError = RegCreateKeyExW(
  271. HKEY_LOCAL_MACHINE,
  272. gpszIpsecMMFiltersKey,
  273. 0,
  274. NULL,
  275. 0,
  276. KEY_ALL_ACCESS,
  277. NULL,
  278. &hParentRegKey,
  279. &dwDisposition
  280. );
  281. BAIL_ON_WIN32_ERROR(dwError);
  282. szFilterID[0] = L'\0';
  283. dwError = UuidToString(
  284. &gMMFilterID,
  285. &pszStringUuid
  286. );
  287. BAIL_ON_WIN32_ERROR(dwError);
  288. wcscpy(szFilterID, L"{");
  289. wcscat(szFilterID, pszStringUuid);
  290. wcscat(szFilterID, L"}");
  291. dwError = RegDeleteKeyW(
  292. hParentRegKey,
  293. szFilterID
  294. );
  295. BAIL_ON_WIN32_ERROR(dwError);
  296. error:
  297. if (hParentRegKey) {
  298. RegCloseKey(hParentRegKey);
  299. }
  300. if (pszStringUuid) {
  301. RpcStringFree(&pszStringUuid);
  302. }
  303. return(dwError);
  304. }