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.

448 lines
9.0 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. ipsecspd.c
  5. Abstract:
  6. This module contains all of the code to drive
  7. the IPSecSPD Service.
  8. Author:
  9. abhisheV 30-September-1999
  10. Environment
  11. User Level: Win32
  12. Revision History:
  13. --*/
  14. #include "precomp.h"
  15. SERVICE_STATUS IPSecSPDStatus;
  16. SERVICE_STATUS_HANDLE IPSecSPDStatusHandle = NULL;
  17. #define IPSECSPD_SERVICE L"PolicyAgent"
  18. void WINAPI
  19. SPDServiceMain(
  20. IN DWORD dwArgc,
  21. IN LPTSTR * lpszArgv
  22. )
  23. {
  24. DWORD dwError = 0;
  25. // Sleep(30000);
  26. dwError = InitSPDThruRegistry();
  27. BAIL_ON_WIN32_ERROR(dwError);
  28. // Initialize all the status fields so that the subsequent calls
  29. // to SetServiceStatus need to only update fields that changed.
  30. IPSecSPDStatus.dwServiceType = SERVICE_WIN32_SHARE_PROCESS;
  31. IPSecSPDStatus.dwCurrentState = SERVICE_START_PENDING;
  32. IPSecSPDStatus.dwControlsAccepted = 0;
  33. IPSecSPDStatus.dwCheckPoint = 1;
  34. IPSecSPDStatus.dwWaitHint = 5000;
  35. IPSecSPDStatus.dwWin32ExitCode = NO_ERROR;
  36. IPSecSPDStatus.dwServiceSpecificExitCode = 0;
  37. // Initialize the workstation to receive service requests
  38. // by registering the service control handler.
  39. IPSecSPDStatusHandle = RegisterServiceCtrlHandlerW(
  40. IPSECSPD_SERVICE,
  41. IPSecSPDControlHandler
  42. );
  43. if (IPSecSPDStatusHandle == (SERVICE_STATUS_HANDLE) NULL) {
  44. dwError = ERROR_INVALID_HANDLE;
  45. BAIL_ON_WIN32_ERROR(dwError);
  46. }
  47. (void) IPSecSPDUpdateStatus();
  48. dwError = InitSPDGlobals();
  49. BAIL_ON_WIN32_ERROR(dwError);
  50. IPSecSPDStatus.dwCurrentState = SERVICE_RUNNING;
  51. IPSecSPDStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP |
  52. SERVICE_ACCEPT_SHUTDOWN;
  53. IPSecSPDStatus.dwCheckPoint = 0;
  54. IPSecSPDStatus.dwWaitHint = 0;
  55. IPSecSPDStatus.dwWin32ExitCode = NO_ERROR;
  56. IPSecSPDStatus.dwServiceSpecificExitCode = 0;
  57. (void) IPSecSPDUpdateStatus();
  58. //
  59. // Get the current list of active interfaces on the machine.
  60. //
  61. (VOID) CreateInterfaceList(
  62. &gpInterfaceList
  63. );
  64. //
  65. // Open the IPSec Driver.
  66. //
  67. dwError = SPDOpenIPSecDriver(
  68. &ghIPSecDriver
  69. );
  70. if (dwError) {
  71. AuditOneArgErrorEvent(
  72. SE_CATEGID_POLICY_CHANGE,
  73. SE_AUDITID_IPSEC_POLICY_CHANGED,
  74. IPSECSVC_DRIVER_INIT_FAILURE,
  75. dwError,
  76. FALSE,
  77. TRUE
  78. );
  79. }
  80. BAIL_ON_WIN32_ERROR(dwError);
  81. (VOID) LoadPersistedIPSecInformation();
  82. dwError = SPDSetIPSecDriverOpMode(
  83. (DWORD) IPSEC_SECURE_MODE
  84. );
  85. if (dwError) {
  86. AuditOneArgErrorEvent(
  87. SE_CATEGID_POLICY_CHANGE,
  88. SE_AUDITID_IPSEC_POLICY_CHANGED,
  89. IPSECSVC_DRIVER_INIT_FAILURE,
  90. dwError,
  91. FALSE,
  92. TRUE
  93. );
  94. }
  95. BAIL_ON_WIN32_ERROR(dwError);
  96. dwError = SPDRegisterIPSecDriverProtocols(
  97. (DWORD) IPSEC_REGISTER_PROTOCOLS
  98. );
  99. if (dwError) {
  100. AuditOneArgErrorEvent(
  101. SE_CATEGID_POLICY_CHANGE,
  102. SE_AUDITID_IPSEC_POLICY_CHANGED,
  103. IPSECSVC_DRIVER_INIT_FAILURE,
  104. dwError,
  105. FALSE,
  106. TRUE
  107. );
  108. }
  109. BAIL_ON_WIN32_ERROR(dwError);
  110. //
  111. // Start IKE Service.
  112. //
  113. dwError = IKEInit();
  114. if (dwError) {
  115. AuditOneArgErrorEvent(
  116. SE_CATEGID_POLICY_CHANGE,
  117. SE_AUDITID_IPSEC_POLICY_CHANGED,
  118. IPSECSVC_IKE_INIT_FAILURE,
  119. dwError,
  120. FALSE,
  121. TRUE
  122. );
  123. }
  124. BAIL_ON_WIN32_ERROR(dwError);
  125. gbIsIKEUp = TRUE;
  126. gbIKENotify = TRUE;
  127. //
  128. // Start the RPC Server.
  129. //
  130. dwError = SPDStartRPCServer(
  131. );
  132. if (dwError) {
  133. AuditOneArgErrorEvent(
  134. SE_CATEGID_POLICY_CHANGE,
  135. SE_AUDITID_IPSEC_POLICY_CHANGED,
  136. IPSECSVC_RPC_INIT_FAILURE,
  137. dwError,
  138. FALSE,
  139. TRUE
  140. );
  141. }
  142. BAIL_ON_WIN32_ERROR(dwError);
  143. dwError = ServiceWait();
  144. error:
  145. IPSecSPDShutdown(dwError);
  146. return;
  147. }
  148. DWORD
  149. IPSecSPDUpdateStatus(
  150. )
  151. {
  152. DWORD dwError = 0;
  153. if (!SetServiceStatus(IPSecSPDStatusHandle, &IPSecSPDStatus)) {
  154. dwError = GetLastError();
  155. }
  156. return (dwError);
  157. }
  158. VOID
  159. IPSecSPDControlHandler(
  160. IN DWORD dwOpCode
  161. )
  162. {
  163. switch (dwOpCode)
  164. {
  165. case SERVICE_CONTROL_STOP:
  166. case SERVICE_CONTROL_SHUTDOWN:
  167. if (IPSecSPDStatus.dwCurrentState != SERVICE_STOP_PENDING) {
  168. IPSecSPDStatus.dwCurrentState = SERVICE_STOP_PENDING;
  169. IPSecSPDStatus.dwCheckPoint = 1;
  170. IPSecSPDStatus.dwWaitHint = 60000;
  171. (void) IPSecSPDUpdateStatus();
  172. SetEvent(ghServiceStopEvent);
  173. return;
  174. }
  175. break;
  176. case SERVICE_CONTROL_NEW_LOCAL_POLICY:
  177. SetEvent(ghNewLocalPolicyEvent);
  178. break;
  179. case SERVICE_CONTROL_FORCED_POLICY_RELOAD:
  180. SetEvent(ghForcedPolicyReloadEvent);
  181. break;
  182. case SERVICE_CONTROL_INTERROGATE:
  183. break;
  184. }
  185. (void) IPSecSPDUpdateStatus();
  186. return;
  187. }
  188. VOID
  189. IPSecSPDShutdown(
  190. IN DWORD dwErrorCode
  191. )
  192. {
  193. gbIKENotify = FALSE;
  194. (VOID) DeleteAllPolicyInformation();
  195. ClearPolicyStateBlock(
  196. gpIpsecPolicyState
  197. );
  198. if (gbLoadedISAKMPDefaults) {
  199. UnLoadDefaultISAKMPInformation(gpszDefaultISAKMPPolicyDN);
  200. }
  201. ClearPAStoreGlobals();
  202. //
  203. // Service stop still pending.
  204. // Increment checkpoint counter and update
  205. // the status with the Service Control Manager.
  206. //
  207. (IPSecSPDStatus.dwCheckPoint)++;
  208. (void) IPSecSPDUpdateStatus();
  209. if (gbSPDRPCServerUp) {
  210. gbSPDRPCServerUp = FALSE;
  211. SPDStopRPCServer();
  212. }
  213. if (gbIsIKEUp) {
  214. gbIsIKEUp = FALSE;
  215. IKEShutdown();
  216. }
  217. if (gpIniMMPolicy) {
  218. FreeIniMMPolicyList(gpIniMMPolicy);
  219. }
  220. if (gpIniMMAuthMethods) {
  221. FreeIniMMAuthMethodsList(gpIniMMAuthMethods);
  222. }
  223. if (gpIniQMPolicy) {
  224. FreeIniQMPolicyList(gpIniQMPolicy);
  225. }
  226. if (gpIniMMSFilter) {
  227. FreeIniMMSFilterList(gpIniMMSFilter);
  228. }
  229. if (gpMMFilterHandle) {
  230. FreeMMFilterHandleList(gpMMFilterHandle);
  231. }
  232. if (gpIniMMFilter) {
  233. FreeIniMMFilterList(gpIniMMFilter);
  234. }
  235. if (gpIniTxSFilter) {
  236. (VOID) DeleteTransportFiltersFromIPSec(gpIniTxSFilter);
  237. FreeIniTxSFilterList(gpIniTxSFilter);
  238. }
  239. if (gpTxFilterHandle) {
  240. FreeTxFilterHandleList(gpTxFilterHandle);
  241. }
  242. if (gpIniTxFilter) {
  243. FreeIniTxFilterList(gpIniTxFilter);
  244. }
  245. if (gpIniTnSFilter) {
  246. (VOID) DeleteTunnelFiltersFromIPSec(gpIniTnSFilter);
  247. FreeIniTnSFilterList(gpIniTnSFilter);
  248. }
  249. if (gpTnFilterHandle) {
  250. FreeTnFilterHandleList(gpTnFilterHandle);
  251. }
  252. if (gpIniTnFilter) {
  253. FreeIniTnFilterList(gpIniTnFilter);
  254. }
  255. (VOID) SPDRegisterIPSecDriverProtocols(
  256. (DWORD) IPSEC_DEREGISTER_PROTOCOLS
  257. );
  258. if (ghIPSecDriver != INVALID_HANDLE_VALUE) {
  259. SPDCloseIPSecDriver(ghIPSecDriver);
  260. }
  261. if (gpInterfaceList) {
  262. DestroyInterfaceList(
  263. gpInterfaceList
  264. );
  265. }
  266. ClearSPDGlobals();
  267. IPSecSPDStatus.dwCurrentState = SERVICE_STOPPED;
  268. IPSecSPDStatus.dwControlsAccepted = 0;
  269. IPSecSPDStatus.dwCheckPoint = 0;
  270. IPSecSPDStatus.dwWaitHint = 0;
  271. IPSecSPDStatus.dwWin32ExitCode = dwErrorCode;
  272. IPSecSPDStatus.dwServiceSpecificExitCode = 0;
  273. (void) IPSecSPDUpdateStatus();
  274. return;
  275. }
  276. VOID
  277. ClearSPDGlobals(
  278. )
  279. {
  280. DestroyInterfaceChangeEvent();
  281. if (gbSPDSection) {
  282. DeleteCriticalSection(&gcSPDSection);
  283. }
  284. if (gbServerListenSection == TRUE) {
  285. DeleteCriticalSection(&gcServerListenSection);
  286. }
  287. if (ghServiceStopEvent) {
  288. CloseHandle(ghServiceStopEvent);
  289. }
  290. if (ghNewDSPolicyEvent) {
  291. CloseHandle(ghNewDSPolicyEvent);
  292. }
  293. if (ghNewLocalPolicyEvent) {
  294. CloseHandle(ghNewLocalPolicyEvent);
  295. }
  296. if (ghForcedPolicyReloadEvent) {
  297. CloseHandle(ghForcedPolicyReloadEvent);
  298. }
  299. if (ghPolicyChangeNotifyEvent) {
  300. CloseHandle(ghPolicyChangeNotifyEvent);
  301. }
  302. if (gbSPDAuditSection) {
  303. DeleteCriticalSection(&gcSPDAuditSection);
  304. }
  305. if (gpSPDSD) {
  306. LocalFree(gpSPDSD);
  307. }
  308. }
  309. VOID
  310. ClearPAStoreGlobals(
  311. )
  312. {
  313. if (gpMMFilterState) {
  314. PAFreeMMFilterStateList(gpMMFilterState);
  315. }
  316. if (gpMMPolicyState) {
  317. PAFreeMMPolicyStateList(gpMMPolicyState);
  318. }
  319. if (gpMMAuthState) {
  320. PAFreeMMAuthStateList(gpMMAuthState);
  321. }
  322. if (gpTxFilterState) {
  323. PAFreeTxFilterStateList(gpTxFilterState);
  324. }
  325. if (gpTnFilterState) {
  326. PAFreeTnFilterStateList(gpTnFilterState);
  327. }
  328. if (gpQMPolicyState) {
  329. PAFreeQMPolicyStateList(gpQMPolicyState);
  330. }
  331. }