Leaked source code of windows server 2003
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.

913 lines
24 KiB

  1. #include "gptext.h"
  2. #include <initguid.h>
  3. #include <iadsp.h>
  4. #include "ipsecext.h"
  5. #include "SmartPtr.h"
  6. #include "wbemtime.h"
  7. #include <strsafe.h>
  8. #define GPEXT_PATH TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPExtensions\\{e437bc1c-aa7d-11d2-a382-00c04f991e27}")
  9. #define POLICY_PATH TEXT("Software\\Policies\\Microsoft\\Windows\\IPSec\\GPTIPSECPolicy")
  10. LPWSTR GetAttributes[] = {L"ipsecOwnersReference", L"ipsecName", L"description"};
  11. HRESULT
  12. RegisterIPSEC(void)
  13. {
  14. HKEY hKey;
  15. LONG lResult;
  16. DWORD dwDisp, dwValue;
  17. TCHAR szBuffer[512];
  18. lResult = RegCreateKeyEx (
  19. HKEY_LOCAL_MACHINE,
  20. GPEXT_PATH,
  21. 0,
  22. NULL,
  23. REG_OPTION_NON_VOLATILE,
  24. KEY_WRITE,
  25. NULL,
  26. &hKey,
  27. &dwDisp
  28. );
  29. if (lResult != ERROR_SUCCESS)
  30. {
  31. return lResult;
  32. }
  33. LoadString (g_hInstance, IDS_IPSEC_NAME, szBuffer, ARRAYSIZE(szBuffer));
  34. RegSetValueEx (
  35. hKey,
  36. NULL,
  37. 0,
  38. REG_SZ,
  39. (LPBYTE)szBuffer,
  40. (lstrlen(szBuffer) + 1) * sizeof(TCHAR)
  41. );
  42. RegSetValueEx (
  43. hKey,
  44. TEXT("ProcessGroupPolicyEx"),
  45. 0,
  46. REG_SZ,
  47. (LPBYTE)TEXT("ProcessIPSECPolicyEx"),
  48. (lstrlen(TEXT("ProcessIPSECPolicyEx")) + 1) * sizeof(TCHAR)
  49. );
  50. RegSetValueEx (
  51. hKey,
  52. TEXT("GenerateGroupPolicy"),
  53. 0,
  54. REG_SZ,
  55. (LPBYTE)TEXT("GenerateIPSECPolicy"),
  56. (lstrlen(TEXT("GenerateIPSECPolicy")) + 1) * sizeof(TCHAR)
  57. );
  58. szBuffer[0] = L'\0';
  59. (void) StringCchCopy(szBuffer, ARRAYSIZE(szBuffer), L"gptext.dll");
  60. RegSetValueEx (
  61. hKey,
  62. TEXT("DllName"),
  63. 0,
  64. REG_EXPAND_SZ,
  65. (LPBYTE)szBuffer,
  66. (lstrlen(szBuffer) + 1) * sizeof(TCHAR)
  67. );
  68. dwValue = 1;
  69. RegSetValueEx (
  70. hKey,
  71. TEXT("NoUserPolicy"),
  72. 0,
  73. REG_DWORD,
  74. (LPBYTE)&dwValue,
  75. sizeof(dwValue));
  76. dwValue = 0;
  77. RegSetValueEx (
  78. hKey,
  79. TEXT("NoGPOListChanges"),
  80. 0,
  81. REG_DWORD,
  82. (LPBYTE)&dwValue,
  83. sizeof(dwValue));
  84. RegCloseKey (hKey);
  85. return S_OK;
  86. }
  87. HRESULT
  88. UnregisterIPSEC(void)
  89. {
  90. RegDeleteKey (HKEY_LOCAL_MACHINE, GPEXT_PATH);
  91. return S_OK;
  92. }
  93. DWORD
  94. ProcessIPSECPolicyEx(
  95. DWORD dwFlags, // GPO_INFO_FLAGS
  96. HANDLE hToken, // User or machine token
  97. HKEY hKeyRoot, // Root of registry
  98. PGROUP_POLICY_OBJECT pDeletedGPOList, // Linked list of deleted GPOs
  99. PGROUP_POLICY_OBJECT pChangedGPOList, // Linked list of changed GPOs
  100. ASYNCCOMPLETIONHANDLE pHandle, // For asynchronous completion
  101. BOOL *pbAbort, // If true, then abort GPO processing
  102. PFNSTATUSMESSAGECALLBACK pStatusCallback,// Callback function for displaying status messages
  103. IWbemServices *pWbemServices, // Pointer to namespace to log diagnostic mode data
  104. // Note, this will be NULL when Rsop logging is disabled
  105. HRESULT *pRsopStatus // RSOP Logging succeeded or not.
  106. )
  107. {
  108. // Call ProcessIPSECPolicy & get path -> polstore funcs
  109. WCHAR szIPSECPolicy[MAX_PATH]; //policy path
  110. WCHAR szIPSECPolicyName[MAX_PATH]; //policy name
  111. WCHAR szIPSECPolicyDescription[512]; //policy descr
  112. HRESULT hr = S_OK;
  113. DWORD dwError = ERROR_SUCCESS;
  114. PGROUP_POLICY_OBJECT pGPO = NULL;
  115. GPO_INFO GPOInfo;
  116. BOOL ForcePolicyReload = FALSE;
  117. //
  118. // ASSERT: CoInitializeEx was called before this function
  119. // was invoked.
  120. //
  121. ForcePolicyReload = (dwFlags & GPO_INFO_FLAG_FORCED_REFRESH) ||
  122. !(dwFlags & GPO_INFO_FLAG_NOCHANGES);
  123. if (!ForcePolicyReload) {
  124. NotifyPolicyAgent();
  125. } else {
  126. memset(szIPSECPolicy, 0, sizeof(WCHAR)*MAX_PATH);
  127. memset(szIPSECPolicyName, 0, sizeof(WCHAR)*MAX_PATH);
  128. memset(szIPSECPolicyDescription, 0, sizeof(WCHAR)*512);
  129. // First process the Deleted GPO List. If there is a single
  130. // entry on the GPO list, just delete the entire list.
  131. // Example Rex->Cassius->Brutus. If the delete List has
  132. // Cassius to be deleted, then really, we shouldn't be deleting
  133. // our registry entry because we're interested in Brutus which
  134. // has not be deleted. But in our case, the pChangedGPOList will
  135. // have all the information, so Brutus gets written back in the
  136. // next stage.
  137. //
  138. if (pDeletedGPOList) {
  139. dwError = IPSecChooseDriverBootMode(
  140. HKEY_LOCAL_MACHINE,
  141. IPSEC_DIRECTORY_PROVIDER,
  142. POL_ACTION_UNASSIGN
  143. );
  144. hr = HRESULT_FROM_WIN32(dwError);
  145. if (dwError) {
  146. goto error;
  147. }
  148. DeleteIPSECPolicyFromRegistry();
  149. //
  150. // Also Clear WMI store if no GPO's applied and logging enabled
  151. //
  152. if (!pChangedGPOList && pWbemServices) {
  153. hr = IPSecClearWMIStore(
  154. pWbemServices
  155. );
  156. if (FAILED(hr)) {
  157. goto error;
  158. }
  159. DebugMsg( (DM_WARNING, L"ipsecext::ProcessIPSECPolicyEx: IPSec WMI store cleared") );
  160. }
  161. }
  162. if(pChangedGPOList) {
  163. DWORD dwNumGPO = 0;
  164. for(pGPO = pChangedGPOList; pGPO; pGPO = pGPO->pNext) {
  165. dwNumGPO++;
  166. //
  167. // Write only the last, highest precedence policy to registry
  168. //
  169. if(pGPO->pNext == NULL) {
  170. hr = RetrieveIPSECPolicyFromDS(
  171. pGPO,
  172. szIPSECPolicy,
  173. ARRAYSIZE(szIPSECPolicy),
  174. szIPSECPolicyName,
  175. ARRAYSIZE(szIPSECPolicyName),
  176. szIPSECPolicyDescription,
  177. ARRAYSIZE(szIPSECPolicyDescription)
  178. );
  179. if (FAILED(hr)) {
  180. goto success; // WMI store still consistent
  181. }
  182. dwError = WriteIPSECPolicyToRegistry(
  183. szIPSECPolicy,
  184. szIPSECPolicyName,
  185. szIPSECPolicyDescription
  186. );
  187. if (dwError) {
  188. goto success; // WMI store still consistent
  189. }
  190. dwError = IPSecChooseDriverBootMode(
  191. HKEY_LOCAL_MACHINE,
  192. IPSEC_DIRECTORY_PROVIDER,
  193. POL_ACTION_ASSIGN
  194. );
  195. hr = HRESULT_FROM_WIN32(dwError);
  196. if (dwError) {
  197. goto error;
  198. }
  199. }
  200. }
  201. DebugMsg( (DM_WARNING, L"ipsecext::ProcessIPSECPolicyEx: dwNumGPO: %d", dwNumGPO) );
  202. // Write WMI log if logging enabled
  203. if (pWbemServices) {
  204. DWORD dwPrecedence = dwNumGPO;
  205. for(pGPO = pChangedGPOList; pGPO; pGPO = pGPO->pNext) {
  206. hr = RetrieveIPSECPolicyFromDS(
  207. pGPO,
  208. szIPSECPolicy,
  209. ARRAYSIZE(szIPSECPolicy),
  210. szIPSECPolicyName,
  211. ARRAYSIZE(szIPSECPolicyName),
  212. szIPSECPolicyDescription,
  213. ARRAYSIZE(szIPSECPolicyDescription)
  214. );
  215. if (FAILED(hr)) {
  216. goto error;
  217. }
  218. LPWSTR pszIPSECPolicy = szIPSECPolicy + wcslen(L"LDAP://");
  219. DebugMsg( (DM_WARNING, L"ipsecext::ProcessIPSECPolicyEx: pszIPSECPolicy: %s", pszIPSECPolicy) );
  220. (VOID) CreatePolstoreGPOInfo(
  221. pGPO,
  222. dwPrecedence--,
  223. dwNumGPO,
  224. &GPOInfo
  225. );
  226. hr = WriteDirectoryPolicyToWMI(
  227. 0, //pszMachineName
  228. pszIPSECPolicy,
  229. &GPOInfo,
  230. pWbemServices
  231. );
  232. (VOID) FreePolstoreGPOInfo(&GPOInfo);
  233. if (FAILED(hr)) {
  234. DebugMsg( (DM_WARNING, L"ipsecext::ProcessIPSECPolicyEx: WriteDirectoryPolicyToWMI failed: 0x%x", hr) );
  235. goto error;
  236. }
  237. }
  238. }
  239. }
  240. DebugMsg( (DM_WARNING, L"ipsecext::ProcessIPSECPolicyEx completed") );
  241. PingPolicyAgent();
  242. }
  243. success:
  244. *pRsopStatus = S_OK;
  245. return(ERROR_SUCCESS);
  246. error:
  247. *pRsopStatus = hr;
  248. return(ERROR_POLICY_OBJECT_NOT_FOUND);
  249. }
  250. DWORD
  251. GenerateIPSECPolicy(
  252. DWORD dwFlags,
  253. BOOL *pbAbort,
  254. WCHAR *pwszSite,
  255. PRSOP_TARGET pMachTarget,
  256. PRSOP_TARGET pUserTarget
  257. )
  258. {
  259. // Call ProcessIPSECPolicy & get path -> polstore funcs
  260. WCHAR szIPSECPolicy[MAX_PATH]; //policy path
  261. WCHAR szIPSECPolicyName[MAX_PATH]; //policy name
  262. WCHAR szIPSECPolicyDescription[512]; //policy descr
  263. HRESULT hr = S_OK;
  264. PGROUP_POLICY_OBJECT pGPO = NULL;
  265. GPO_INFO GPOInfo;
  266. //
  267. // ASSERT: CoInitializeEx was called before this function
  268. // was invoked.
  269. //
  270. memset(szIPSECPolicy, 0, sizeof(WCHAR)*MAX_PATH);
  271. memset(szIPSECPolicyName, 0, sizeof(WCHAR)*MAX_PATH);
  272. memset(szIPSECPolicyDescription, 0, sizeof(WCHAR)*512);
  273. ////start
  274. PGROUP_POLICY_OBJECT pChangedGPOList = NULL;
  275. IWbemServices *pWbemServices = NULL;
  276. if(pMachTarget) {
  277. pChangedGPOList = pMachTarget->pGPOList;
  278. pWbemServices = pMachTarget->pWbemServices;
  279. }
  280. if(pUserTarget) {
  281. pChangedGPOList = pUserTarget->pGPOList;
  282. pWbemServices = pUserTarget->pWbemServices;
  283. }
  284. if(pChangedGPOList && pWbemServices) {
  285. DWORD dwNumGPO = 0;
  286. for(pGPO = pChangedGPOList; pGPO; pGPO = pGPO->pNext) {
  287. dwNumGPO++;
  288. }
  289. DebugMsg( (DM_WARNING, L"ipsecext::GenerateIPSECPolicy: dwNumGPO: %d", dwNumGPO) );
  290. DWORD dwPrecedence = dwNumGPO;
  291. for(pGPO = pChangedGPOList; pGPO; pGPO = pGPO->pNext) {
  292. hr = RetrieveIPSECPolicyFromDS(
  293. pGPO,
  294. szIPSECPolicy,
  295. ARRAYSIZE(szIPSECPolicy),
  296. szIPSECPolicyName,
  297. ARRAYSIZE(szIPSECPolicyName),
  298. szIPSECPolicyDescription,
  299. ARRAYSIZE(szIPSECPolicyDescription)
  300. );
  301. if (FAILED(hr)) {
  302. goto error;
  303. }
  304. LPWSTR pszIPSECPolicy = szIPSECPolicy + wcslen(L"LDAP://");
  305. DebugMsg( (DM_WARNING, L"ipsecext::GenerateIPSECPolicy: pszIPSECPolicy: %s", pszIPSECPolicy) );
  306. (VOID) CreatePolstoreGPOInfo(
  307. pGPO,
  308. dwPrecedence--,
  309. dwNumGPO,
  310. &GPOInfo
  311. );
  312. hr = WriteDirectoryPolicyToWMI(
  313. 0, //pszMachineName
  314. pszIPSECPolicy,
  315. &GPOInfo,
  316. pWbemServices
  317. );
  318. (VOID) FreePolstoreGPOInfo(&GPOInfo);
  319. if (FAILED(hr)) {
  320. DebugMsg( (DM_WARNING, L"ipsecext::GenerateIPSECPolicy: WriteDirectoryPolicyToWMI failed: 0x%x", hr) );
  321. goto error;
  322. }
  323. }
  324. }
  325. DebugMsg( (DM_WARNING, L"ipsecext::GenerateIPSECPolicy completed") );
  326. return(ERROR_SUCCESS);
  327. error:
  328. return(ERROR_POLICY_OBJECT_NOT_FOUND);
  329. }
  330. HRESULT
  331. CreatePolstoreGPOInfo(
  332. PGROUP_POLICY_OBJECT pGPO,
  333. UINT32 uiPrecedence,
  334. UINT32 uiTotalGPOs,
  335. PGPO_INFO pGPOInfo
  336. )
  337. {
  338. XBStr xbstrCurrentTime;
  339. HRESULT hr;
  340. memset(pGPOInfo, 0, sizeof(GPO_INFO));
  341. pGPOInfo->uiPrecedence = uiPrecedence;
  342. pGPOInfo->uiTotalGPOs = uiTotalGPOs;
  343. pGPOInfo->bsGPOID = SysAllocString(
  344. StripPrefixIpsec(pGPO->lpDSPath)
  345. );
  346. pGPOInfo->bsSOMID = SysAllocString(
  347. StripLinkPrefixIpsec(pGPO->lpLink)
  348. );
  349. // (Failing safe above by ignoring mem alloc errors)
  350. hr = GetCurrentWbemTime(xbstrCurrentTime);
  351. if ( FAILED (hr) ) {
  352. pGPOInfo->bsCreationtime = 0;
  353. }
  354. else {
  355. pGPOInfo->bsCreationtime = xbstrCurrentTime.Acquire();
  356. }
  357. return S_OK;
  358. }
  359. HRESULT
  360. FreePolstoreGPOInfo(
  361. PGPO_INFO pGPOInfo
  362. )
  363. {
  364. if (pGPOInfo && pGPOInfo->bsCreationtime) {
  365. SysFreeString(pGPOInfo->bsCreationtime);
  366. }
  367. if (pGPOInfo && pGPOInfo->bsGPOID) {
  368. SysFreeString(pGPOInfo->bsGPOID);
  369. }
  370. if (pGPOInfo && pGPOInfo->bsSOMID) {
  371. SysFreeString(pGPOInfo->bsSOMID);
  372. }
  373. return S_OK;
  374. }
  375. HRESULT
  376. CreateChildPath(
  377. LPWSTR pszParentPath,
  378. LPWSTR pszChildComponent,
  379. BSTR * ppszChildPath
  380. )
  381. {
  382. HRESULT hr = S_OK;
  383. IADsPathname *pPathname = NULL;
  384. hr = CoCreateInstance(
  385. CLSID_Pathname,
  386. NULL,
  387. CLSCTX_ALL,
  388. IID_IADsPathname,
  389. (void**)&pPathname
  390. );
  391. BAIL_ON_FAILURE(hr);
  392. hr = pPathname->Set(pszParentPath, ADS_SETTYPE_FULL);
  393. BAIL_ON_FAILURE(hr);
  394. hr = pPathname->AddLeafElement(pszChildComponent);
  395. BAIL_ON_FAILURE(hr);
  396. hr = pPathname->Retrieve(ADS_FORMAT_X500, ppszChildPath);
  397. BAIL_ON_FAILURE(hr);
  398. error:
  399. if (pPathname) {
  400. pPathname->Release();
  401. }
  402. return(hr);
  403. }
  404. HRESULT
  405. RetrieveIPSECPolicyFromDS(
  406. PGROUP_POLICY_OBJECT pGPOInfo,
  407. LPWSTR pszIPSecPolicy,
  408. DWORD dwPolLen,
  409. LPWSTR pszIPSecPolicyName,
  410. DWORD dwPolNameLen,
  411. LPWSTR pszIPSecPolicyDescription,
  412. DWORD dwPolDescLen
  413. )
  414. {
  415. LPWSTR pszMachinePath = NULL;
  416. BSTR pszMicrosoftPath = NULL;
  417. BSTR pszWindowsPath = NULL;
  418. BSTR pszIpsecPath = NULL;
  419. IDirectoryObject * pDirectoryObject = NULL;
  420. IDirectoryObject * pIpsecObject = NULL;
  421. BOOL bFound = FALSE;
  422. HRESULT hr = S_OK;
  423. LPWSTR pszOwnersReference = L"ipsecOwnersReference";
  424. PADS_ATTR_INFO pAttributeEntries = NULL;
  425. DWORD dwNumAttributesReturned = 0;
  426. DWORD i = 0;
  427. PADS_ATTR_INFO pAttributeEntry = NULL;
  428. pszMachinePath = pGPOInfo->lpDSPath;
  429. // Build the fully qualified ADsPath for my object
  430. hr = CreateChildPath(
  431. pszMachinePath,
  432. L"cn=Microsoft",
  433. &pszMicrosoftPath
  434. );
  435. BAIL_ON_FAILURE(hr);
  436. hr = CreateChildPath(
  437. pszMicrosoftPath,
  438. L"cn=Windows",
  439. &pszWindowsPath
  440. );
  441. BAIL_ON_FAILURE(hr);
  442. hr = CreateChildPath(
  443. pszWindowsPath,
  444. L"cn=ipsec",
  445. &pszIpsecPath
  446. );
  447. BAIL_ON_FAILURE(hr);
  448. hr = ADsOpenObject(
  449. pszIpsecPath,
  450. NULL,
  451. NULL,
  452. ADS_SECURE_AUTHENTICATION | ADS_USE_SEALING | ADS_USE_SIGNING,
  453. IID_IDirectoryObject,
  454. (void **)&pIpsecObject
  455. );
  456. BAIL_ON_FAILURE(hr);
  457. hr = pIpsecObject->GetObjectAttributes(
  458. GetAttributes,
  459. 3,
  460. &pAttributeEntries,
  461. &dwNumAttributesReturned
  462. );
  463. BAIL_ON_FAILURE(hr);
  464. if (dwNumAttributesReturned == 0) {
  465. hr = E_FAIL;
  466. BAIL_ON_FAILURE(hr);
  467. }
  468. //
  469. // Process the PathName
  470. //
  471. for (i = 0; i < dwNumAttributesReturned; i++) {
  472. pAttributeEntry = pAttributeEntries + i;
  473. if (!_wcsicmp(pAttributeEntry->pszAttrName, L"ipsecOwnersReference")) {
  474. hr = StringCchCopy(pszIPSecPolicy, dwPolLen, L"LDAP://");
  475. BAIL_ON_FAILURE(hr);
  476. hr = StringCchCat(pszIPSecPolicy, dwPolLen, pAttributeEntry->pADsValues->DNString);
  477. BAIL_ON_FAILURE(hr);
  478. bFound = TRUE;
  479. break;
  480. }
  481. }
  482. if (!bFound) {
  483. hr = E_FAIL;
  484. BAIL_ON_FAILURE(hr);
  485. }
  486. //
  487. // Process the name
  488. //
  489. for (i = 0; i < dwNumAttributesReturned; i++) {
  490. pAttributeEntry = pAttributeEntries + i;
  491. if (!_wcsicmp(pAttributeEntry->pszAttrName, L"ipsecName")) {
  492. hr = StringCchCopy(pszIPSecPolicyName, dwPolNameLen, pAttributeEntry->pADsValues->DNString);
  493. BAIL_ON_FAILURE(hr);
  494. break;
  495. }
  496. }
  497. //
  498. // Process the description
  499. //
  500. for (i = 0; i < dwNumAttributesReturned; i++) {
  501. pAttributeEntry = pAttributeEntries + i;
  502. if (!_wcsicmp(pAttributeEntry->pszAttrName, L"description")) {
  503. hr = StringCchCopy(pszIPSecPolicyDescription, dwPolDescLen, pAttributeEntry->pADsValues->DNString);
  504. BAIL_ON_FAILURE(hr);
  505. break;
  506. }
  507. }
  508. error:
  509. if (pAttributeEntries) {
  510. FreeADsMem(pAttributeEntries);
  511. }
  512. if (pIpsecObject) {
  513. pIpsecObject->Release();
  514. }
  515. if (pszMicrosoftPath) {
  516. SysFreeString(pszMicrosoftPath);
  517. }
  518. if (pszWindowsPath) {
  519. SysFreeString(pszWindowsPath);
  520. }
  521. if (pszIpsecPath) {
  522. SysFreeString(pszIpsecPath);
  523. }
  524. return(hr);
  525. }
  526. DWORD
  527. DeleteIPSECPolicyFromRegistry(
  528. )
  529. {
  530. DWORD dwError = 0;
  531. HKEY hKey = NULL;
  532. DWORD dwDisp = 0;
  533. dwError = RegCreateKeyEx (
  534. HKEY_LOCAL_MACHINE,
  535. TEXT("Software\\Policies\\Microsoft\\Windows\\IPSec"),
  536. 0,
  537. NULL,
  538. REG_OPTION_NON_VOLATILE,
  539. KEY_ALL_ACCESS,
  540. NULL,
  541. &hKey,
  542. &dwDisp
  543. );
  544. if (dwError) {
  545. goto error;
  546. }
  547. dwError = RegDeleteKey(
  548. hKey,
  549. L"GPTIPSECPolicy"
  550. );
  551. /*
  552. dwError = RegDeleteValue(
  553. hKey,
  554. TEXT("DSIPSECPolicyPath")
  555. );
  556. dwError = RegDeleteValue(
  557. hKey,
  558. TEXT("DSIPSECPolicyName")
  559. );*/
  560. error:
  561. if (hKey) {
  562. RegCloseKey (hKey);
  563. }
  564. return(dwError);
  565. }
  566. DWORD
  567. WriteIPSECPolicyToRegistry(
  568. LPWSTR pszIPSecPolicyPath,
  569. LPWSTR pszIPSecPolicyName,
  570. LPWSTR pszIPSecPolicyDescription
  571. )
  572. {
  573. DWORD dwError = 0;
  574. DWORD dwDisp = 0;
  575. HKEY hKey = NULL;
  576. DWORD dwFlags = 1;
  577. dwError = RegCreateKeyEx (
  578. HKEY_LOCAL_MACHINE,
  579. POLICY_PATH,
  580. 0,
  581. NULL,
  582. REG_OPTION_NON_VOLATILE,
  583. KEY_ALL_ACCESS,
  584. NULL,
  585. &hKey,
  586. &dwDisp
  587. );
  588. if (dwError) {
  589. goto error;
  590. }
  591. if (pszIPSecPolicyPath && *pszIPSecPolicyPath) {
  592. dwError = RegSetValueEx (
  593. hKey,
  594. TEXT("DSIPSECPolicyPath"),
  595. 0,
  596. REG_SZ,
  597. (LPBYTE)pszIPSecPolicyPath,
  598. (lstrlen(pszIPSecPolicyPath) + 1) * sizeof(TCHAR)
  599. );
  600. dwFlags = 1;
  601. dwError = RegSetValueEx (
  602. hKey,
  603. TEXT("DSIPSECPolicyFlags"),
  604. 0,
  605. REG_DWORD,
  606. (LPBYTE)&dwFlags,
  607. sizeof(dwFlags)
  608. );
  609. }
  610. if (pszIPSecPolicyName && *pszIPSecPolicyName) {
  611. dwError = RegSetValueEx (
  612. hKey,
  613. TEXT("DSIPSECPolicyName"),
  614. 0,
  615. REG_SZ,
  616. (LPBYTE)pszIPSecPolicyName,
  617. (lstrlen(pszIPSecPolicyName) + 1) * sizeof(TCHAR)
  618. );
  619. }
  620. if (pszIPSecPolicyDescription && *pszIPSecPolicyDescription) {
  621. dwError = RegSetValueEx (
  622. hKey,
  623. TEXT("DSIPSECPolicyDescription"),
  624. 0,
  625. REG_SZ,
  626. (LPBYTE)pszIPSecPolicyDescription,
  627. (lstrlen(pszIPSecPolicyDescription) + 1) * sizeof(TCHAR)
  628. );
  629. }
  630. error:
  631. if (hKey) {
  632. RegCloseKey (hKey);
  633. }
  634. return(dwError);
  635. }
  636. VOID
  637. PingPolicyAgent(
  638. )
  639. {
  640. HANDLE hPolicyChangeEvent = NULL;
  641. hPolicyChangeEvent = OpenEvent(
  642. EVENT_ALL_ACCESS,
  643. FALSE,
  644. L"IPSEC_POLICY_CHANGE_EVENT"
  645. );
  646. if (hPolicyChangeEvent) {
  647. SetEvent(hPolicyChangeEvent);
  648. CloseHandle(hPolicyChangeEvent);
  649. }
  650. }
  651. VOID
  652. NotifyPolicyAgent(
  653. )
  654. {
  655. HANDLE hGpRefreshEvent = NULL;
  656. hGpRefreshEvent = OpenEvent(
  657. EVENT_ALL_ACCESS,
  658. FALSE,
  659. IPSEC_GP_REFRESH_EVENT
  660. );
  661. if (hGpRefreshEvent) {
  662. SetEvent(hGpRefreshEvent);
  663. CloseHandle(hGpRefreshEvent);
  664. }
  665. }
  666. //
  667. // Prefix stripping functions copied from
  668. // gina\userenv\rsop\logger.cpp written by SitaramR
  669. //
  670. //*************************************************************
  671. //
  672. // StripPrefix()
  673. //
  674. // Purpose: Strips out prefix to get canonical path to Gpo
  675. //
  676. // Parameters: lpGPOInfo - Gpo Info
  677. // pWbemServices - Wbem services
  678. //
  679. // Returns: Pointer to suffix
  680. //
  681. //*************************************************************
  682. WCHAR *StripPrefixIpsec( WCHAR *pwszPath )
  683. {
  684. WCHAR wszMachPrefix[] = TEXT("LDAP://CN=Machine,");
  685. INT iMachPrefixLen = lstrlen( wszMachPrefix );
  686. WCHAR wszUserPrefix[] = TEXT("LDAP://CN=User,");
  687. INT iUserPrefixLen = lstrlen( wszUserPrefix );
  688. WCHAR *pwszPathSuffix;
  689. //
  690. // Strip out prefix to get the canonical path to Gpo
  691. //
  692. if ( CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE,
  693. pwszPath, iUserPrefixLen, wszUserPrefix, iUserPrefixLen ) == CSTR_EQUAL ) {
  694. pwszPathSuffix = pwszPath + iUserPrefixLen;
  695. } else if ( CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE,
  696. pwszPath, iMachPrefixLen, wszMachPrefix, iMachPrefixLen ) == CSTR_EQUAL ) {
  697. pwszPathSuffix = pwszPath + iMachPrefixLen;
  698. } else
  699. pwszPathSuffix = pwszPath;
  700. return pwszPathSuffix;
  701. }
  702. //*************************************************************
  703. //
  704. // StripLinkPrefix()
  705. //
  706. // Purpose: Strips out prefix to get canonical path to DS
  707. // object
  708. //
  709. // Parameters: pwszPath - path to strip
  710. //
  711. // Returns: Pointer to suffix
  712. //
  713. //*************************************************************
  714. WCHAR *StripLinkPrefixIpsec( WCHAR *pwszPath )
  715. {
  716. WCHAR wszPrefix[] = TEXT("LDAP://");
  717. INT iPrefixLen = lstrlen( wszPrefix );
  718. WCHAR *pwszPathSuffix;
  719. //
  720. // Strip out prefix to get the canonical path to Som
  721. //
  722. if ( wcslen(pwszPath) <= (DWORD) iPrefixLen ) {
  723. return pwszPath;
  724. }
  725. if ( CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE,
  726. pwszPath, iPrefixLen, wszPrefix, iPrefixLen ) == CSTR_EQUAL ) {
  727. pwszPathSuffix = pwszPath + iPrefixLen;
  728. } else
  729. pwszPathSuffix = pwszPath;
  730. return pwszPathSuffix;
  731. }