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.

397 lines
8.6 KiB

  1. #include "precomp.h"
  2. LPWSTR gpszIpsecTxFiltersKey =
  3. L"SOFTWARE\\Microsoft\\IPSec\\Transport Filters";
  4. DWORD
  5. PersistTxFilter(
  6. GUID gFilterID,
  7. PTRANSPORT_FILTER pTxFilter
  8. )
  9. {
  10. DWORD dwError = 0;
  11. HKEY hRegistryKey = NULL;
  12. DWORD dwDisposition = 0;
  13. dwError = RegCreateKeyExW(
  14. HKEY_LOCAL_MACHINE,
  15. gpszIpsecTxFiltersKey,
  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 = SPDWriteTxFilter(
  26. hRegistryKey,
  27. gFilterID,
  28. pTxFilter
  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) SPDPurgeTxFilter(
  39. gFilterID
  40. );
  41. }
  42. goto cleanup;
  43. }
  44. DWORD
  45. SPDWriteTxFilter(
  46. HKEY hParentRegKey,
  47. GUID gFilterID,
  48. PTRANSPORT_FILTER pTxFilter
  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. &pTxFilter->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) pTxFilter->pszFilterName,
  109. (wcslen(pTxFilter->pszFilterName) + 1)*sizeof(WCHAR)
  110. );
  111. BAIL_ON_WIN32_ERROR(dwError);
  112. dwInterfaceType = (DWORD) pTxFilter->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) pTxFilter->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)&pTxFilter->dwFlags,
  138. sizeof(DWORD)
  139. );
  140. BAIL_ON_WIN32_ERROR(dwError);
  141. dwError = MarshallTxFilterBuffer(
  142. pTxFilter,
  143. &pBuffer,
  144. &dwBufferSize
  145. );
  146. BAIL_ON_WIN32_ERROR(dwError);
  147. dwError = RegSetValueExW(
  148. hRegKey,
  149. L"Transport Filter Buffer",
  150. 0,
  151. REG_BINARY,
  152. (LPBYTE) pBuffer,
  153. dwBufferSize
  154. );
  155. BAIL_ON_WIN32_ERROR(dwError);
  156. dwInboundFilterFlag = (DWORD) pTxFilter->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) pTxFilter->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. MarshallTxFilterBuffer(
  204. PTRANSPORT_FILTER pTxFilter,
  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_TX_FILTER_VER1 =
  214. { 0xabcd0005, 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. pBuffer = (LPBYTE) AllocSPDMem(
  223. dwBufferSize
  224. );
  225. if (!pBuffer) {
  226. dwError = ERROR_OUTOFMEMORY;
  227. BAIL_ON_WIN32_ERROR(dwError);
  228. }
  229. pMem = pBuffer;
  230. memcpy(
  231. pMem,
  232. (LPBYTE) &GUID_IPSEC_TX_FILTER_VER1,
  233. sizeof(GUID)
  234. );
  235. pMem += sizeof(GUID);
  236. memcpy(
  237. pMem,
  238. (LPBYTE) &dwBufferSize,
  239. sizeof(DWORD)
  240. );
  241. pMem += sizeof(DWORD);
  242. memcpy(
  243. pMem,
  244. (LPBYTE) &pTxFilter->SrcAddr,
  245. sizeof(ADDR)
  246. );
  247. pMem += sizeof(ADDR);
  248. memcpy(
  249. pMem,
  250. (LPBYTE) &pTxFilter->DesAddr,
  251. sizeof(ADDR)
  252. );
  253. pMem += sizeof(ADDR);
  254. memcpy(
  255. pMem,
  256. (LPBYTE) &pTxFilter->Protocol,
  257. sizeof(PROTOCOL)
  258. );
  259. pMem += sizeof(PROTOCOL);
  260. memcpy(
  261. pMem,
  262. (LPBYTE) &pTxFilter->SrcPort,
  263. sizeof(PORT)
  264. );
  265. pMem += sizeof(PORT);
  266. memcpy(
  267. pMem,
  268. (LPBYTE) &pTxFilter->DesPort,
  269. sizeof(PORT)
  270. );
  271. pMem += sizeof(PORT);
  272. *ppBuffer = pBuffer;
  273. *pdwBufferSize = dwBufferSize;
  274. return (dwError);
  275. error:
  276. *ppBuffer = NULL;
  277. *pdwBufferSize = 0;
  278. return (dwError);
  279. }
  280. DWORD
  281. SPDPurgeTxFilter(
  282. GUID gTxFilterID
  283. )
  284. {
  285. DWORD dwError = 0;
  286. HKEY hParentRegKey = NULL;
  287. DWORD dwDisposition = 0;
  288. WCHAR szFilterID[MAX_PATH];
  289. LPWSTR pszStringUuid = NULL;
  290. dwError = RegCreateKeyExW(
  291. HKEY_LOCAL_MACHINE,
  292. gpszIpsecTxFiltersKey,
  293. 0,
  294. NULL,
  295. 0,
  296. KEY_ALL_ACCESS,
  297. NULL,
  298. &hParentRegKey,
  299. &dwDisposition
  300. );
  301. BAIL_ON_WIN32_ERROR(dwError);
  302. szFilterID[0] = L'\0';
  303. dwError = UuidToString(
  304. &gTxFilterID,
  305. &pszStringUuid
  306. );
  307. BAIL_ON_WIN32_ERROR(dwError);
  308. wcscpy(szFilterID, L"{");
  309. wcscat(szFilterID, pszStringUuid);
  310. wcscat(szFilterID, L"}");
  311. dwError = RegDeleteKeyW(
  312. hParentRegKey,
  313. szFilterID
  314. );
  315. BAIL_ON_WIN32_ERROR(dwError);
  316. error:
  317. if (hParentRegKey) {
  318. RegCloseKey(hParentRegKey);
  319. }
  320. if (pszStringUuid) {
  321. RpcStringFree(&pszStringUuid);
  322. }
  323. return(dwError);
  324. }