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.

413 lines
8.9 KiB

  1. #include "precomp.h"
  2. LPWSTR gpszIpsecTnFiltersKey =
  3. L"SOFTWARE\\Microsoft\\IPSec\\Tunnel Filters";
  4. DWORD
  5. PersistTnFilter(
  6. GUID gFilterID,
  7. PTUNNEL_FILTER pTnFilter
  8. )
  9. {
  10. DWORD dwError = 0;
  11. HKEY hRegistryKey = NULL;
  12. DWORD dwDisposition = 0;
  13. dwError = RegCreateKeyExW(
  14. HKEY_LOCAL_MACHINE,
  15. gpszIpsecTnFiltersKey,
  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 = SPDWriteTnFilter(
  26. hRegistryKey,
  27. gFilterID,
  28. pTnFilter
  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) SPDPurgeTnFilter(
  39. gFilterID
  40. );
  41. }
  42. goto cleanup;
  43. }
  44. DWORD
  45. SPDWriteTnFilter(
  46. HKEY hParentRegKey,
  47. GUID gFilterID,
  48. PTUNNEL_FILTER pTnFilter
  49. )
  50. {
  51. DWORD dwError = 0;
  52. WCHAR szFilterID[MAX_PATH];
  53. WCHAR szPolicyID[MAX_PATH];
  54. LPWSTR pszStringUuid = NULL;
  55. LPWSTR pszPolicyUuid = NULL;
  56. HKEY hRegKey = NULL;
  57. DWORD dwDisposition = 0;
  58. LPBYTE pBuffer = NULL;
  59. DWORD dwBufferSize = 0;
  60. DWORD dwInterfaceType = 0;
  61. DWORD dwMirrored = 0;
  62. DWORD dwInboundFilterFlag = 0;
  63. DWORD dwOutboundFilterFlag = 0;
  64. szFilterID[0] = L'\0';
  65. szPolicyID[0] = L'\0';
  66. dwError = UuidToString(
  67. &gFilterID,
  68. &pszStringUuid
  69. );
  70. BAIL_ON_WIN32_ERROR(dwError);
  71. wcscpy(szFilterID, L"{");
  72. wcscat(szFilterID, pszStringUuid);
  73. wcscat(szFilterID, L"}");
  74. dwError = UuidToString(
  75. &pTnFilter->gPolicyID,
  76. &pszPolicyUuid
  77. );
  78. BAIL_ON_WIN32_ERROR(dwError);
  79. wcscpy(szPolicyID, L"{");
  80. wcscat(szPolicyID, pszPolicyUuid);
  81. wcscat(szPolicyID, L"}");
  82. dwError = RegCreateKeyExW(
  83. hParentRegKey,
  84. szFilterID,
  85. 0,
  86. NULL,
  87. 0,
  88. KEY_ALL_ACCESS,
  89. NULL,
  90. &hRegKey,
  91. &dwDisposition
  92. );
  93. BAIL_ON_WIN32_ERROR(dwError);
  94. dwError = RegSetValueExW(
  95. hRegKey,
  96. L"FilterID",
  97. 0,
  98. REG_SZ,
  99. (LPBYTE) szFilterID,
  100. (wcslen(szFilterID) + 1)*sizeof(WCHAR)
  101. );
  102. BAIL_ON_WIN32_ERROR(dwError);
  103. dwError = RegSetValueExW(
  104. hRegKey,
  105. L"FilterName",
  106. 0,
  107. REG_SZ,
  108. (LPBYTE) pTnFilter->pszFilterName,
  109. (wcslen(pTnFilter->pszFilterName) + 1)*sizeof(WCHAR)
  110. );
  111. BAIL_ON_WIN32_ERROR(dwError);
  112. dwInterfaceType = (DWORD) pTnFilter->InterfaceType;
  113. dwError = RegSetValueExW(
  114. hRegKey,
  115. L"InterfaceType",
  116. 0,
  117. REG_DWORD,
  118. (LPBYTE)&dwInterfaceType,
  119. sizeof(DWORD)
  120. );
  121. BAIL_ON_WIN32_ERROR(dwError);
  122. dwMirrored = (DWORD) pTnFilter->bCreateMirror;
  123. dwError = RegSetValueExW(
  124. hRegKey,
  125. L"Mirrored",
  126. 0,
  127. REG_DWORD,
  128. (LPBYTE)&dwMirrored,
  129. sizeof(DWORD)
  130. );
  131. BAIL_ON_WIN32_ERROR(dwError);
  132. dwError = RegSetValueExW(
  133. hRegKey,
  134. L"Flags",
  135. 0,
  136. REG_DWORD,
  137. (LPBYTE)&pTnFilter->dwFlags,
  138. sizeof(DWORD)
  139. );
  140. BAIL_ON_WIN32_ERROR(dwError);
  141. dwError = MarshallTnFilterBuffer(
  142. pTnFilter,
  143. &pBuffer,
  144. &dwBufferSize
  145. );
  146. BAIL_ON_WIN32_ERROR(dwError);
  147. dwError = RegSetValueExW(
  148. hRegKey,
  149. L"Tunnel Filter Buffer",
  150. 0,
  151. REG_BINARY,
  152. (LPBYTE) pBuffer,
  153. dwBufferSize
  154. );
  155. BAIL_ON_WIN32_ERROR(dwError);
  156. dwInboundFilterFlag = (DWORD) pTnFilter->InboundFilterFlag;
  157. dwError = RegSetValueExW(
  158. hRegKey,
  159. L"InboundFilterFlag",
  160. 0,
  161. REG_DWORD,
  162. (LPBYTE)&dwInboundFilterFlag,
  163. sizeof(DWORD)
  164. );
  165. BAIL_ON_WIN32_ERROR(dwError);
  166. dwOutboundFilterFlag = (DWORD) pTnFilter->OutboundFilterFlag;
  167. dwError = RegSetValueExW(
  168. hRegKey,
  169. L"OutboundFilterFlag",
  170. 0,
  171. REG_DWORD,
  172. (LPBYTE)&dwOutboundFilterFlag,
  173. sizeof(DWORD)
  174. );
  175. BAIL_ON_WIN32_ERROR(dwError);
  176. dwError = RegSetValueExW(
  177. hRegKey,
  178. L"PolicyID",
  179. 0,
  180. REG_SZ,
  181. (LPBYTE) szPolicyID,
  182. (wcslen(szPolicyID) + 1)*sizeof(WCHAR)
  183. );
  184. BAIL_ON_WIN32_ERROR(dwError);
  185. cleanup:
  186. if (pszStringUuid) {
  187. RpcStringFree(&pszStringUuid);
  188. }
  189. if (pszPolicyUuid) {
  190. RpcStringFree(&pszPolicyUuid);
  191. }
  192. if (hRegKey) {
  193. RegCloseKey(hRegKey);
  194. }
  195. if (pBuffer) {
  196. FreeSPDMem(pBuffer);
  197. }
  198. return (dwError);
  199. error:
  200. goto cleanup;
  201. }
  202. DWORD
  203. MarshallTnFilterBuffer(
  204. PTUNNEL_FILTER pTnFilter,
  205. LPBYTE * ppBuffer,
  206. PDWORD pdwBufferSize
  207. )
  208. {
  209. DWORD dwError = 0;
  210. LPBYTE pBuffer = NULL;
  211. DWORD dwBufferSize = 0;
  212. LPBYTE pMem = NULL;
  213. static const GUID GUID_IPSEC_TN_FILTER_VER1 =
  214. { 0xabcd0006, 0x0001, 0x0001, { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 } };
  215. dwBufferSize = sizeof(GUID) +
  216. sizeof(DWORD) +
  217. sizeof(ADDR) +
  218. sizeof(ADDR) +
  219. sizeof(PROTOCOL) +
  220. sizeof(PORT) +
  221. sizeof(PORT) +
  222. sizeof(ADDR) +
  223. sizeof(ADDR);
  224. pBuffer = (LPBYTE) AllocSPDMem(
  225. dwBufferSize
  226. );
  227. if (!pBuffer) {
  228. dwError = ERROR_OUTOFMEMORY;
  229. BAIL_ON_WIN32_ERROR(dwError);
  230. }
  231. pMem = pBuffer;
  232. memcpy(
  233. pMem,
  234. (LPBYTE) &GUID_IPSEC_TN_FILTER_VER1,
  235. sizeof(GUID)
  236. );
  237. pMem += sizeof(GUID);
  238. memcpy(
  239. pMem,
  240. (LPBYTE) &dwBufferSize,
  241. sizeof(DWORD)
  242. );
  243. pMem += sizeof(DWORD);
  244. memcpy(
  245. pMem,
  246. (LPBYTE) &pTnFilter->SrcAddr,
  247. sizeof(ADDR)
  248. );
  249. pMem += sizeof(ADDR);
  250. memcpy(
  251. pMem,
  252. (LPBYTE) &pTnFilter->DesAddr,
  253. sizeof(ADDR)
  254. );
  255. pMem += sizeof(ADDR);
  256. memcpy(
  257. pMem,
  258. (LPBYTE) &pTnFilter->Protocol,
  259. sizeof(PROTOCOL)
  260. );
  261. pMem += sizeof(PROTOCOL);
  262. memcpy(
  263. pMem,
  264. (LPBYTE) &pTnFilter->SrcPort,
  265. sizeof(PORT)
  266. );
  267. pMem += sizeof(PORT);
  268. memcpy(
  269. pMem,
  270. (LPBYTE) &pTnFilter->DesPort,
  271. sizeof(PORT)
  272. );
  273. pMem += sizeof(PORT);
  274. memcpy(
  275. pMem,
  276. (LPBYTE) &pTnFilter->SrcTunnelAddr,
  277. sizeof(ADDR)
  278. );
  279. pMem += sizeof(ADDR);
  280. memcpy(
  281. pMem,
  282. (LPBYTE) &pTnFilter->DesTunnelAddr,
  283. sizeof(ADDR)
  284. );
  285. pMem += sizeof(ADDR);
  286. *ppBuffer = pBuffer;
  287. *pdwBufferSize = dwBufferSize;
  288. return (dwError);
  289. error:
  290. *ppBuffer = NULL;
  291. *pdwBufferSize = 0;
  292. return (dwError);
  293. }
  294. DWORD
  295. SPDPurgeTnFilter(
  296. GUID gTnFilterID
  297. )
  298. {
  299. DWORD dwError = 0;
  300. HKEY hParentRegKey = NULL;
  301. DWORD dwDisposition = 0;
  302. WCHAR szFilterID[MAX_PATH];
  303. LPWSTR pszStringUuid = NULL;
  304. dwError = RegCreateKeyExW(
  305. HKEY_LOCAL_MACHINE,
  306. gpszIpsecTnFiltersKey,
  307. 0,
  308. NULL,
  309. 0,
  310. KEY_ALL_ACCESS,
  311. NULL,
  312. &hParentRegKey,
  313. &dwDisposition
  314. );
  315. BAIL_ON_WIN32_ERROR(dwError);
  316. szFilterID[0] = L'\0';
  317. dwError = UuidToString(
  318. &gTnFilterID,
  319. &pszStringUuid
  320. );
  321. BAIL_ON_WIN32_ERROR(dwError);
  322. wcscpy(szFilterID, L"{");
  323. wcscat(szFilterID, pszStringUuid);
  324. wcscat(szFilterID, L"}");
  325. dwError = RegDeleteKeyW(
  326. hParentRegKey,
  327. szFilterID
  328. );
  329. BAIL_ON_WIN32_ERROR(dwError);
  330. error:
  331. if (hParentRegKey) {
  332. RegCloseKey(hParentRegKey);
  333. }
  334. if (pszStringUuid) {
  335. RpcStringFree(&pszStringUuid);
  336. }
  337. return(dwError);
  338. }