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.

7022 lines
179 KiB

  1. //*************************************************************
  2. // File name: GPOBJ.CPP
  3. //
  4. // Description: Group Policy Object class
  5. //
  6. //
  7. // Microsoft Confidential
  8. // Copyright (c) Microsoft Corporation 1998
  9. // All rights reserved
  10. //
  11. //*************************************************************
  12. #include "main.h"
  13. #include "browser.h"
  14. //
  15. // Help ids
  16. //
  17. DWORD aPropertiesHelpIds[] =
  18. {
  19. IDC_TITLE, IDH_PROP_TITLE,
  20. IDC_DISABLE_COMPUTER, IDH_PROP_DISABLE_COMPUTER,
  21. IDC_DISABLE_USER, IDH_PROP_DISABLE_USER,
  22. 0, 0
  23. };
  24. DWORD aLinkHelpIds[] =
  25. {
  26. IDC_CBDOMAIN, IDH_LINK_DOMAIN,
  27. IDC_ACTION, IDH_LINK_BUTTON,
  28. IDC_RESULTLIST, IDH_LINK_RESULT,
  29. 0, 0
  30. };
  31. DWORD aWQLFilterHelpIds[] =
  32. {
  33. IDC_NONE, IDH_WQL_FILTER_NONE,
  34. IDC_THIS_FILTER, IDH_WQL_FILTER_THIS_FILTER,
  35. IDC_FILTER_NAME, IDH_WQL_FILTER_NAME,
  36. IDC_FILTER_BROWSE, IDH_WQL_FILTER_BROWSE,
  37. 0, 0
  38. };
  39. ///////////////////////////////////////////////////////////////////////////////
  40. // //
  41. // CGroupPolicyObject implementation //
  42. // //
  43. ///////////////////////////////////////////////////////////////////////////////
  44. CGroupPolicyObject::CGroupPolicyObject()
  45. {
  46. InterlockedIncrement(&g_cRefThisDll);
  47. m_cRef = 1;
  48. m_bInitialized = FALSE;
  49. m_pADs = NULL;
  50. m_gpoType = GPOTypeLocal;
  51. m_dwFlags = 0;
  52. m_pName = NULL;
  53. m_pDisplayName = NULL;
  54. m_pMachineName = NULL;
  55. m_pUser = NULL;
  56. m_pMachine = NULL;
  57. m_hinstDSSec = NULL;
  58. m_pfnDSCreateSecurityPage = NULL;
  59. m_pTempFilterString = NULL;
  60. m_pDSPath = NULL;
  61. m_pFileSysPath = NULL;
  62. }
  63. CGroupPolicyObject::~CGroupPolicyObject()
  64. {
  65. CleanUp();
  66. if (m_hinstDSSec)
  67. {
  68. FreeLibrary (m_hinstDSSec);
  69. }
  70. InterlockedDecrement(&g_cRefThisDll);
  71. }
  72. ///////////////////////////////////////////////////////////////////////////////
  73. // //
  74. // CGroupPolicyObject object implementation (IUnknown) //
  75. // //
  76. ///////////////////////////////////////////////////////////////////////////////
  77. HRESULT CGroupPolicyObject::QueryInterface (REFIID riid, void **ppv)
  78. {
  79. if (IsEqualIID(riid, IID_IGroupPolicyObject) || IsEqualIID(riid, IID_IUnknown))
  80. {
  81. *ppv = (LPGROUPPOLICYOBJECT)this;
  82. m_cRef++;
  83. return S_OK;
  84. }
  85. else
  86. {
  87. *ppv = NULL;
  88. return E_NOINTERFACE;
  89. }
  90. }
  91. ULONG CGroupPolicyObject::AddRef (void)
  92. {
  93. return ++m_cRef;
  94. }
  95. ULONG CGroupPolicyObject::Release (void)
  96. {
  97. if (--m_cRef == 0) {
  98. delete this;
  99. return 0;
  100. }
  101. return m_cRef;
  102. }
  103. ///////////////////////////////////////////////////////////////////////////////
  104. // //
  105. // CGroupPolicyObject object implementation (IGroupPolicyObject) //
  106. // //
  107. ///////////////////////////////////////////////////////////////////////////////
  108. //*************************************************************
  109. //
  110. // CGroupPolicyObject::New()
  111. //
  112. // Purpose: Creates a new GPO in the DS
  113. //
  114. // Parameters: pszDomainName - Domain to create GPO in
  115. // pszDisplayName - GPO friendly name (optional)
  116. // dwFlags - Open / creation flags
  117. //
  118. // Note: The domain passed in should be in this format:
  119. // LDAP://DC=domain,DC=company,DC=COM
  120. //
  121. // Return: S_OK if successful
  122. //
  123. //*************************************************************
  124. STDMETHODIMP CGroupPolicyObject::New (LPOLESTR pszDomainName, LPOLESTR pszDisplayName,
  125. DWORD dwFlags)
  126. {
  127. HRESULT hr = E_FAIL;
  128. IADsPathname * pADsPathname = NULL;
  129. BSTR bstrContainer = NULL;
  130. BSTR bstrGPC = NULL;
  131. LPTSTR lpResult = NULL, lpDCName = NULL;
  132. LPTSTR lpEnd = NULL, lpTemp = NULL, lpGPTPath = NULL;
  133. LPTSTR lpForest = NULL;
  134. DWORD dwResult;
  135. GUID guid;
  136. TCHAR szGPOName[50];
  137. TCHAR szTemp[100];
  138. TCHAR szGPOPath[2*MAX_PATH];
  139. WIN32_FILE_ATTRIBUTE_DATA fad;
  140. IADs *pADs = NULL;
  141. PSECURITY_DESCRIPTOR pSD = NULL;
  142. SECURITY_INFORMATION si = (OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
  143. DACL_SECURITY_INFORMATION);
  144. //
  145. // Check if this object has already been initialized
  146. //
  147. if (m_bInitialized)
  148. {
  149. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Called on an initialized object.")));
  150. return STG_E_INUSE;
  151. }
  152. //
  153. // Check parameters
  154. //
  155. if (!pszDomainName)
  156. {
  157. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Null domain name")));
  158. hr = E_INVALIDARG;
  159. goto Exit;
  160. }
  161. if (CompareString (LOCALE_USER_DEFAULT, NORM_STOP_ON_NULL, TEXT("LDAP://"),
  162. 7, pszDomainName, 7) != CSTR_EQUAL)
  163. {
  164. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Domain name does not start with LDAP://")));
  165. hr = E_INVALIDARG;
  166. goto Exit;
  167. }
  168. //
  169. // Verbose output
  170. //
  171. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::New: Entering with:")));
  172. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::New: Domain Name: %s"), pszDomainName));
  173. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::New: Flags: 0x%x"), dwFlags));
  174. //
  175. // Convert the ADSI domain name into a DNS style name
  176. //
  177. hr = ConvertToDotStyle (pszDomainName, &lpResult);
  178. if (FAILED(hr))
  179. {
  180. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to convert domain name with 0x%x"), hr));
  181. goto Exit;
  182. }
  183. //
  184. // If we are working on the enterprise then we need to get the name of the
  185. // forest.
  186. //
  187. #if FGPO_SUPPORT
  188. if (GPO_OPEN_FOREST == (dwFlags & GPO_OPEN_FOREST))
  189. {
  190. DWORD dwResult = QueryForForestName(NULL,
  191. lpResult,
  192. DS_PDC_REQUIRED | DS_RETURN_DNS_NAME,
  193. &lpTemp);
  194. if (dwResult != ERROR_SUCCESS)
  195. {
  196. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: QueryForestName failed for domain name %s with %d"),
  197. lpResult, dwResult));
  198. hr = HRESULT_FROM_WIN32(dwResult);
  199. goto Exit;
  200. }
  201. int cch = 0;
  202. int n=0;
  203. // count the dots in lpTemp;
  204. while (lpTemp[n])
  205. {
  206. if (L'.' == lpTemp[n])
  207. {
  208. cch++;
  209. }
  210. n++;
  211. }
  212. cch *= 3; // multiply the number of dots by 3;
  213. cch += 11; // add 10 + 1 (for the null)
  214. cch += n; // add the string size;
  215. lpForest = (LPTSTR) LocalAlloc(LPTR, sizeof(WCHAR) * cch);
  216. if (!lpForest)
  217. {
  218. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to allocate memory for forest name with %d"),
  219. GetLastError()));
  220. hr = HRESULT_FROM_WIN32(GetLastError());
  221. goto Exit;
  222. }
  223. NameToPath(lpForest, lpTemp, cch);
  224. // substitute the forest's dot path for the domain's dot path
  225. LocalFree(lpResult);
  226. lpResult = lpTemp;
  227. lpTemp = NULL;
  228. //
  229. // Check to see if we have a domain path to a specific DC.
  230. // If we don't then the string will start "LDAP://DC=".
  231. // The equal sign in particular can only be there if we don't have a specific
  232. // DC so we'll just check for the equal sign.
  233. //
  234. if (*(pszDomainName + 9) != TEXT('='))
  235. {
  236. // we have a path to a specific DC
  237. // need to extract the server path and prepend it to the forest name
  238. lpDCName = ExtractServerName(pszDomainName);
  239. if (!lpDCName)
  240. {
  241. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to extract server name for Forest path")));
  242. hr = HRESULT_FROM_WIN32(GetLastError());
  243. goto Exit;
  244. }
  245. lpTemp = MakeFullPath(lpForest, lpDCName);
  246. if (!lpTemp)
  247. {
  248. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to combine server name with Forest path")));
  249. hr = HRESULT_FROM_WIN32(GetLastError());
  250. goto Exit;
  251. }
  252. // clean up the variables we just borrowed so they can be used later
  253. LocalFree(lpDCName);
  254. lpDCName = NULL;
  255. LocalFree(lpForest);
  256. lpForest = lpTemp;
  257. lpTemp = NULL;
  258. }
  259. // Substitute the path to the forest for the path to the domain
  260. pszDomainName = lpForest;
  261. }
  262. #endif
  263. //
  264. // Check to see if we have a domain path to a specific DC.
  265. // If we don't then the string will start "LDAP://DC=".
  266. // The equal sign in particular can only be there if we don't have a specific
  267. // DC so we'll just check for the equal sign.
  268. //
  269. if (*(pszDomainName + 9) == TEXT('='))
  270. {
  271. //
  272. // Convert LDAP to dot (DN) style
  273. //
  274. hr = ConvertToDotStyle (pszDomainName, &lpTemp);
  275. if (FAILED(hr))
  276. {
  277. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to convert domain name with 0x%x"), hr));
  278. goto Exit;
  279. }
  280. //
  281. // Get the GPO DC for this domain
  282. //
  283. lpDCName = GetDCName (lpTemp, NULL, NULL, FALSE, 0);
  284. if (!lpDCName)
  285. {
  286. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to get DC name with %d"),
  287. GetLastError()));
  288. hr = HRESULT_FROM_WIN32(GetLastError());
  289. goto Exit;
  290. }
  291. //
  292. // Build a fully qualified domain name to a specific DC
  293. //
  294. lpTemp = MakeFullPath (pszDomainName, lpDCName);
  295. if (!lpTemp)
  296. {
  297. hr = HRESULT_FROM_WIN32(GetLastError());
  298. goto Exit;
  299. }
  300. }
  301. else
  302. {
  303. lpDCName = ExtractServerName (pszDomainName);
  304. if (!lpDCName)
  305. {
  306. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to extract server name from ADSI path")));
  307. hr = HRESULT_FROM_WIN32(GetLastError());
  308. goto Exit;
  309. }
  310. lpTemp = (LPTSTR) LocalAlloc (LPTR, (lstrlen(pszDomainName) + 1) * sizeof(TCHAR));
  311. if (!lpTemp)
  312. {
  313. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to allocate memory for true domain name with %d"),
  314. GetLastError()));
  315. hr = HRESULT_FROM_WIN32(GetLastError());
  316. goto Exit;
  317. }
  318. lstrcpy (lpTemp, pszDomainName);
  319. }
  320. //
  321. // Create a pathname object we can work with
  322. //
  323. hr = CoCreateInstance(CLSID_Pathname, NULL, CLSCTX_INPROC_SERVER,
  324. IID_IADsPathname, (LPVOID*)&pADsPathname);
  325. if (FAILED(hr))
  326. {
  327. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to create adspathname instance with 0x%x"), hr));
  328. LocalFree (lpTemp);
  329. goto Exit;
  330. }
  331. //
  332. // Add the domain name
  333. //
  334. hr = pADsPathname->Set (lpTemp, ADS_SETTYPE_FULL);
  335. LocalFree (lpTemp);
  336. if (FAILED(hr))
  337. {
  338. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to set pathname with 0x%x"), hr));
  339. goto Exit;
  340. }
  341. #if FGPO_SUPPORT
  342. if (GPO_OPEN_FOREST != (dwFlags & GPO_OPEN_FOREST))
  343. {
  344. #endif
  345. //
  346. // Add the system folder to the path unless we're on the enterprise
  347. //
  348. hr = pADsPathname->AddLeafElement (TEXT("CN=System"));
  349. if (FAILED(hr))
  350. {
  351. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to add system folder with 0x%x"), hr));
  352. goto Exit;
  353. }
  354. #if FGPO_SUPPORT
  355. }
  356. else
  357. {
  358. //
  359. // We're on the enterprise so point at the Configuration folder instead
  360. //
  361. hr = pADsPathname->AddLeafElement (TEXT("CN=Configuration"));
  362. if (FAILED(hr))
  363. {
  364. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to add system folder with 0x%x"), hr));
  365. goto Exit;
  366. }
  367. }
  368. #endif
  369. //
  370. // Retreive the container path - this is the path to the parent of the policies folder
  371. //
  372. hr = pADsPathname->Retrieve (ADS_FORMAT_X500, &bstrContainer);
  373. if (FAILED(hr))
  374. {
  375. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to retreive container path with 0x%x"), hr));
  376. goto Exit;
  377. }
  378. //
  379. // Create the Policies container
  380. //
  381. hr = CreateContainer (bstrContainer, TEXT("Policies"), FALSE);
  382. if (FAILED(hr))
  383. {
  384. if (hr != HRESULT_FROM_WIN32(ERROR_OBJECT_ALREADY_EXISTS))
  385. {
  386. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to create the gpo container with 0x%x"), hr));
  387. goto Exit;
  388. }
  389. }
  390. SysFreeString (bstrContainer);
  391. bstrContainer = NULL;
  392. //
  393. // Add the policies container to the path
  394. //
  395. hr = pADsPathname->AddLeafElement (TEXT("CN=Policies"));
  396. if (FAILED(hr))
  397. {
  398. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to add policies folder with 0x%x"), hr));
  399. goto Exit;
  400. }
  401. //
  402. // Retreive the container path - this is the path to the policies folder
  403. //
  404. hr = pADsPathname->Retrieve (ADS_FORMAT_X500, &bstrContainer);
  405. if (FAILED(hr))
  406. {
  407. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to retreive container path with 0x%x"), hr));
  408. goto Exit;
  409. }
  410. //
  411. // Create a new GPO name (guid)
  412. //
  413. if (FAILED(CoCreateGuid(&guid)))
  414. {
  415. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to create GUID.")));
  416. goto Exit;
  417. }
  418. if (!StringFromGUID2 (guid, szGPOName, ARRAYSIZE(szGPOName)))
  419. {
  420. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to convert GUID.")));
  421. goto Exit;
  422. }
  423. //
  424. // Create a container for this GPO
  425. //
  426. hr = CreateContainer (bstrContainer, szGPOName, TRUE);
  427. if (FAILED(hr)) {
  428. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to create the gpo container with 0x%x"), hr));
  429. goto Exit;
  430. }
  431. SysFreeString (bstrContainer);
  432. bstrContainer = NULL;
  433. //
  434. // Add the GPO name to the path
  435. //
  436. lstrcpy (szTemp, TEXT("CN="));
  437. lstrcat (szTemp, szGPOName);
  438. hr = pADsPathname->AddLeafElement (szTemp);
  439. if (FAILED(hr))
  440. {
  441. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to add machine folder with 0x%x"), hr));
  442. goto Exit;
  443. }
  444. //
  445. // Retreive the GPC path
  446. //
  447. hr = pADsPathname->Retrieve (ADS_FORMAT_X500, &bstrGPC);
  448. if (FAILED(hr))
  449. {
  450. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to retreive container path with 0x%x"), hr));
  451. goto Exit;
  452. }
  453. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::New: GPO container path is: %s"), bstrGPC));
  454. //
  455. // Now create the machine and user containers
  456. //
  457. hr = CreateContainer (bstrGPC, MACHINE_SECTION, FALSE);
  458. if (FAILED(hr)) {
  459. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to create the machine container with 0x%x"), hr));
  460. goto Exit;
  461. }
  462. hr = CreateContainer (bstrGPC, USER_SECTION, FALSE);
  463. if (FAILED(hr)) {
  464. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to create the user container with 0x%x"), hr));
  465. goto Exit;
  466. }
  467. //
  468. // Prepare the file system storage on the sysvol
  469. //
  470. // Build the name
  471. //
  472. wsprintf (szGPOPath, TEXT("\\\\%s\\SysVol\\%s\\Policies\\%s"), lpDCName, lpResult, szGPOName);
  473. lpGPTPath = (LPTSTR) LocalAlloc(LPTR, (lstrlen(szGPOPath) + 1) * sizeof(TCHAR));
  474. if (!lpGPTPath)
  475. {
  476. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to allocate memory for GPT path with %d"),
  477. GetLastError()));
  478. hr = HRESULT_FROM_WIN32(GetLastError());
  479. goto Exit;
  480. }
  481. lstrcpy (lpGPTPath, szGPOPath);
  482. if (!CreateNestedDirectory (szGPOPath, NULL))
  483. {
  484. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to create file system directory %s with %d"),
  485. szGPOPath, GetLastError()));
  486. hr = HRESULT_FROM_WIN32(GetLastError());
  487. goto Exit;
  488. }
  489. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::New: File system folder is: %s"), szGPOPath));
  490. //
  491. // Set the security of the sysvol to match the security of the DS
  492. //
  493. // First, enable some security privilages so we can set the owner / sacl information
  494. //
  495. if (!EnableSecurityPrivs())
  496. {
  497. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to enable the security privilages with %d"),
  498. GetLastError()));
  499. hr = HRESULT_FROM_WIN32(GetLastError());
  500. goto Exit;
  501. }
  502. //
  503. // Bind to the GPO
  504. //
  505. hr = OpenDSObject(bstrGPC, IID_IADs, (void **)&pADs);
  506. if (FAILED(hr)) {
  507. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to get gpo IADs interface with 0x%x"), hr));
  508. SetThreadToken(NULL, NULL);
  509. goto Exit;
  510. }
  511. //
  512. // Get the security descriptor from the DS
  513. //
  514. hr = GetSecurityDescriptor (pADs, si, &pSD);
  515. if (FAILED(hr)) {
  516. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to query the security descriptor with 0x%x"), hr));
  517. SetThreadToken(NULL, NULL);
  518. goto Exit;
  519. }
  520. //
  521. // Set the security information on the sysvol
  522. //
  523. dwResult = SetSysvolSecurity (szGPOPath, si, pSD);
  524. if (dwResult != ERROR_SUCCESS)
  525. {
  526. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to set sysvol security for %s with %d"),
  527. szGPOPath, dwResult));
  528. hr = HRESULT_FROM_WIN32(dwResult);
  529. SetThreadToken(NULL, NULL);
  530. goto Exit;
  531. }
  532. //
  533. // Reset the security privilages
  534. //
  535. SetThreadToken(NULL, NULL);
  536. lpEnd = CheckSlash(szGPOPath);
  537. //
  538. // Set the initial version number
  539. //
  540. lstrcpy (lpEnd, TEXT("GPT.INI"));
  541. if (!WritePrivateProfileString (TEXT("General"), TEXT("Version"), TEXT("0"),
  542. szGPOPath))
  543. {
  544. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to set initial version number for %s with %d"),
  545. szGPOPath, GetLastError()));
  546. hr = HRESULT_FROM_WIN32(GetLastError());
  547. goto Exit;
  548. }
  549. //
  550. // Create the user and machine directories
  551. //
  552. lstrcpy (lpEnd, MACHINE_SECTION);
  553. if (!CreateNestedDirectory (szGPOPath, NULL))
  554. {
  555. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to create machine file system directory %s with %d"),
  556. szGPOPath, GetLastError()));
  557. hr = HRESULT_FROM_WIN32(GetLastError());
  558. goto Exit;
  559. }
  560. lstrcpy (lpEnd, USER_SECTION);
  561. if (!CreateNestedDirectory (szGPOPath, NULL))
  562. {
  563. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to create user file system directory %s with %d"),
  564. szGPOPath, GetLastError()));
  565. hr = HRESULT_FROM_WIN32(GetLastError());
  566. goto Exit;
  567. }
  568. //
  569. // Set the GPO specific information
  570. //
  571. // Note that we use the nameless form of the sysvol path
  572. //
  573. wsprintf (szGPOPath, TEXT("\\\\%s\\SysVol\\%s\\Policies\\%s"), lpResult, lpResult, szGPOName);
  574. hr = SetGPOInfo (bstrGPC, pszDisplayName, szGPOPath);
  575. if (FAILED(hr)) {
  576. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to set GPO information with 0x%x"), hr));
  577. goto Exit;
  578. }
  579. //
  580. // Call OpenDSGPO to do the loading work
  581. //
  582. hr = OpenDSGPO(bstrGPC, dwFlags);
  583. Exit:
  584. if (lpForest)
  585. {
  586. LocalFree (lpForest);
  587. }
  588. if (lpDCName)
  589. {
  590. LocalFree (lpDCName);
  591. }
  592. if (lpResult)
  593. {
  594. LocalFree (lpResult);
  595. }
  596. if (bstrContainer)
  597. {
  598. SysFreeString (bstrContainer);
  599. }
  600. if (bstrGPC)
  601. {
  602. if (FAILED(hr))
  603. {
  604. if (FAILED(DSDelnode(bstrGPC)))
  605. {
  606. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to delete GPC with 0x%x"), hr));
  607. }
  608. }
  609. SysFreeString (bstrGPC);
  610. }
  611. if (lpGPTPath)
  612. {
  613. if (FAILED(hr))
  614. {
  615. if (!Delnode(lpGPTPath))
  616. {
  617. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::New: Failed to delete GPT with %d"),
  618. GetLastError()));
  619. }
  620. }
  621. }
  622. if (pADsPathname)
  623. {
  624. pADsPathname->Release();
  625. }
  626. if (pADs)
  627. {
  628. pADs->Release();
  629. }
  630. if (pSD)
  631. {
  632. LocalFree (pSD);
  633. }
  634. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::New: Leaving with a status of 0x%x"), hr));
  635. return hr;
  636. }
  637. //*************************************************************
  638. //
  639. // OpenDSGPO()
  640. //
  641. // Purpose: Opens a DS Group Policy Object
  642. //
  643. // Parameters: pszPath - Path to the GPO to open
  644. // dwFlags - Open / creation flags
  645. //
  646. // Return: S_OK if successful
  647. //
  648. //*************************************************************
  649. STDMETHODIMP CGroupPolicyObject::OpenDSGPO (LPOLESTR pszPath, DWORD dwFlags)
  650. {
  651. HRESULT hr = E_FAIL;
  652. VARIANT var;
  653. IADsPathname * pADsPathname = NULL;
  654. IADsObjectOptions *pOptions = NULL;
  655. BSTR bstrProperty;
  656. BSTR bstrGPOName = NULL;
  657. BSTR bstrContainer;
  658. BSTR bstrDCName;
  659. TCHAR szPath[2*MAX_PATH];
  660. TCHAR szKeyName[100];
  661. LPTSTR lpTemp;
  662. LPTSTR lpEnd;
  663. LPTSTR pszFullPath = NULL;
  664. DWORD dwResult;
  665. WIN32_FILE_ATTRIBUTE_DATA fad;
  666. DFS_INFO_101 Info101;
  667. LPTSTR lpDCName = NULL;
  668. LPOLESTR pszDomain;
  669. UINT uiSize;
  670. TCHAR szFormat[10];
  671. LPTSTR lpNames[2];
  672. //
  673. // Check if this object has already been initialized
  674. //
  675. if (m_bInitialized)
  676. {
  677. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Called on an uninitialized object.")));
  678. return STG_E_INUSE;
  679. }
  680. //
  681. // Check parameters
  682. //
  683. if (!pszPath)
  684. {
  685. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: NULL GPO name")));
  686. return E_INVALIDARG;
  687. }
  688. if (CompareString (LOCALE_USER_DEFAULT, NORM_STOP_ON_NULL, TEXT("LDAP://"),
  689. 7, pszPath, 7) != CSTR_EQUAL)
  690. {
  691. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: %s does not start with LDAP://"), pszPath));
  692. hr = E_INVALIDARG;
  693. goto Exit;
  694. }
  695. //
  696. // Verbose output
  697. //
  698. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::OpenDSGPO: Entering with:")));
  699. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::OpenDSGPO: GPO Path: %s"), pszPath));
  700. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::OpenDSGPO: Flags: %d"), dwFlags));
  701. //
  702. // Save the flags
  703. //
  704. m_dwFlags = dwFlags;
  705. //
  706. // Retreive the server name if defined
  707. //
  708. lpDCName = ExtractServerName (pszPath);
  709. if (lpDCName)
  710. {
  711. pszFullPath = pszPath;
  712. }
  713. else
  714. {
  715. //
  716. // Get the domain name
  717. //
  718. pszDomain = GetDomainFromLDAPPath(pszPath);
  719. if (!pszDomain)
  720. {
  721. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to get domain name")));
  722. hr = E_FAIL;
  723. goto Exit;
  724. }
  725. //
  726. // Convert LDAP to dot (DN) style
  727. //
  728. hr = ConvertToDotStyle (pszDomain, &lpTemp);
  729. delete [] pszDomain;
  730. if (FAILED(hr))
  731. {
  732. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to convert domain name with 0x%x"), hr));
  733. goto Exit;
  734. }
  735. //
  736. // Get the GPO DC for this domain
  737. //
  738. lpDCName = GetDCName (lpTemp, NULL, NULL, FALSE, 0);
  739. LocalFree (lpTemp);
  740. if (!lpDCName)
  741. {
  742. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to get DC name with %d"),
  743. GetLastError()));
  744. hr = HRESULT_FROM_WIN32(GetLastError());
  745. goto Exit;
  746. }
  747. //
  748. // Make the fully qualified path
  749. //
  750. pszFullPath = MakeFullPath (pszPath, lpDCName);
  751. if (!pszFullPath)
  752. {
  753. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to make full GPO path")));
  754. hr = HRESULT_FROM_WIN32(GetLastError());
  755. goto Exit;
  756. }
  757. }
  758. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::OpenDSGPO: Using server %s"), lpDCName));
  759. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::OpenDSGPO: Using fully qualifed pathname of %s"), pszFullPath));
  760. //
  761. // Save the DC name
  762. //
  763. m_pMachineName = (LPTSTR) LocalAlloc (LPTR, (lstrlen(lpDCName) + 1) * sizeof(TCHAR));
  764. if (!m_pMachineName)
  765. {
  766. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to allocate memory for machine name")));
  767. hr = HRESULT_FROM_WIN32(GetLastError());
  768. goto Exit;
  769. }
  770. lstrcpy (m_pMachineName, lpDCName);
  771. //
  772. // Save the DS path
  773. //
  774. m_pDSPath = (LPTSTR) LocalAlloc (LPTR, (lstrlen(pszFullPath) + 2) * sizeof(TCHAR));
  775. if (!m_pDSPath)
  776. {
  777. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to allocate memory for ds path")));
  778. hr = HRESULT_FROM_WIN32(GetLastError());
  779. goto Exit;
  780. }
  781. lstrcpy (m_pDSPath, pszFullPath);
  782. //
  783. // Bind to the DS object. Note we hold on to this bind until
  784. // the object goes away. This way other ADSI calls will go to
  785. // the same DC.
  786. //
  787. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::OpenDSGPO: Binding to the object")));
  788. hr = OpenDSObject(m_pDSPath, IID_IADs, (void **)&m_pADs);
  789. if (FAILED(hr))
  790. {
  791. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: OpenDSObject failed with 0x%x"), hr));
  792. goto Exit;
  793. }
  794. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::OpenDSGPO: Bound successfully.")));
  795. //
  796. // Check if the user has write permission to the GPO
  797. //
  798. if (!(m_dwFlags & GPO_OPEN_READ_ONLY))
  799. {
  800. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::OpenDSGPO: Checking for write access")));
  801. hr = CheckDSWriteAccess ((LPUNKNOWN)m_pADs, TEXT("versionNumber"));
  802. if (FAILED(hr))
  803. {
  804. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: CheckDSWriteAccess failed with 0x%x"), hr));
  805. goto Exit;
  806. }
  807. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::OpenDSGPO: Write access granted")));
  808. }
  809. //
  810. // Query for the file system path
  811. //
  812. bstrProperty = SysAllocString (GPT_PATH_PROPERTY);
  813. if (!bstrProperty)
  814. {
  815. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to allocate memory")));
  816. hr = ERROR_OUTOFMEMORY;
  817. goto Exit;
  818. }
  819. VariantInit(&var);
  820. hr = m_pADs->Get(bstrProperty, &var);
  821. if (FAILED(hr))
  822. {
  823. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to query GPT path with 0x%x"), hr));
  824. SysFreeString (bstrProperty);
  825. VariantClear (&var);
  826. goto Exit;
  827. }
  828. m_pFileSysPath = (LPTSTR) LocalAlloc (LPTR, (lstrlen(var.bstrVal) + 2) * sizeof(TCHAR));
  829. if (!m_pFileSysPath)
  830. {
  831. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to allocate memory for gpt path")));
  832. SysFreeString (bstrProperty);
  833. VariantClear (&var);
  834. hr = HRESULT_FROM_WIN32(GetLastError());
  835. goto Exit;
  836. }
  837. lstrcpy (m_pFileSysPath, var.bstrVal);
  838. SysFreeString (bstrProperty);
  839. VariantClear (&var);
  840. //
  841. // Query for the display name
  842. //
  843. bstrProperty = SysAllocString (GPO_NAME_PROPERTY);
  844. if (!bstrProperty)
  845. {
  846. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to allocate memory")));
  847. hr = ERROR_OUTOFMEMORY;
  848. goto Exit;
  849. }
  850. VariantInit(&var);
  851. hr = m_pADs->Get(bstrProperty, &var);
  852. if (FAILED(hr))
  853. {
  854. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to query for display name with 0x%x"), hr));
  855. SysFreeString (bstrProperty);
  856. VariantClear (&var);
  857. goto Exit;
  858. }
  859. m_pDisplayName = (LPTSTR) LocalAlloc (LPTR, (lstrlen(var.bstrVal) + 1) * sizeof(TCHAR));
  860. if (!m_pDisplayName)
  861. {
  862. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to allocate memory for display name")));
  863. SysFreeString (bstrProperty);
  864. VariantClear (&var);
  865. hr = HRESULT_FROM_WIN32(GetLastError());
  866. goto Exit;
  867. }
  868. lstrcpy (m_pDisplayName, var.bstrVal);
  869. SysFreeString (bstrProperty);
  870. VariantClear (&var);
  871. //
  872. // Create a pathname object we can work with
  873. //
  874. hr = CoCreateInstance(CLSID_Pathname, NULL, CLSCTX_INPROC_SERVER,
  875. IID_IADsPathname, (LPVOID*)&pADsPathname);
  876. if (FAILED(hr))
  877. {
  878. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to create adspathname instance with 0x%x"), hr));
  879. goto Exit;
  880. }
  881. //
  882. // Add the domain name
  883. //
  884. hr = pADsPathname->Set (m_pDSPath, ADS_SETTYPE_FULL);
  885. if (FAILED(hr))
  886. {
  887. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to set pathname with 0x%x"), hr));
  888. goto Exit;
  889. }
  890. //
  891. // Retrieve the GPO name
  892. //
  893. hr = pADsPathname->GetElement (0, &bstrGPOName);
  894. if (FAILED(hr))
  895. {
  896. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to retreive GPO name with 0x%x"), hr));
  897. goto Exit;
  898. }
  899. //
  900. // Make a copy of the GPO name
  901. //
  902. m_pName = (LPTSTR) LocalAlloc (LPTR, (lstrlen(bstrGPOName) + 1 - 3) * sizeof(TCHAR));
  903. if (!m_pName)
  904. {
  905. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to allocate memory for gpo name")));
  906. hr = HRESULT_FROM_WIN32(GetLastError());
  907. goto Exit;
  908. }
  909. lstrcpy (m_pName, (bstrGPOName + 3));
  910. //
  911. // Set the ADSI preferred DC.
  912. //
  913. hr = m_pADs->QueryInterface(IID_IADsObjectOptions, (void**)&pOptions);
  914. if (SUCCEEDED(hr))
  915. {
  916. //
  917. // Get the domain name
  918. //
  919. pszDomain = GetDomainFromLDAPPath(pszPath);
  920. if (!pszDomain)
  921. {
  922. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to get domain name")));
  923. hr = E_FAIL;
  924. goto Exit;
  925. }
  926. //
  927. // Convert LDAP to dot (DN) style
  928. //
  929. hr = ConvertToDotStyle (pszDomain, &lpTemp);
  930. delete [] pszDomain;
  931. if (FAILED(hr))
  932. {
  933. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to convert domain name with 0x%x"), hr));
  934. goto Exit;
  935. }
  936. //
  937. // Build a variant containing the domain and dc names
  938. //
  939. VariantInit(&var);
  940. lpNames[0] = lpTemp;
  941. lpNames[1] = lpDCName;
  942. hr = ADsBuildVarArrayStr (lpNames, 2, &var);
  943. LocalFree (lpTemp);
  944. if (FAILED(hr))
  945. {
  946. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to convert domain name with 0x%x"), hr));
  947. goto Exit;
  948. }
  949. //
  950. // Set the DC name
  951. //
  952. hr = pOptions->SetOption(ADS_PRIVATE_OPTION_SPECIFIC_SERVER, var);
  953. VariantClear (&var);
  954. if (FAILED(hr))
  955. {
  956. //
  957. // TODO: Remove this block after lab03 RI's -- or -- remove post whistler beta2
  958. //
  959. if (hr == E_ADS_BAD_PARAMETER)
  960. {
  961. //
  962. // Set the DC name the old way
  963. //
  964. VariantInit(&var);
  965. var.vt = VT_BSTR;
  966. var.bstrVal = SysAllocString (lpDCName);
  967. if (var.bstrVal)
  968. {
  969. hr = pOptions->SetOption(ADS_PRIVATE_OPTION_SPECIFIC_SERVER, var);
  970. }
  971. else
  972. {
  973. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to allocate bstr DCName string")));
  974. }
  975. VariantClear (&var);
  976. }
  977. else
  978. {
  979. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to set private DC name with 0x%x"), hr));
  980. }
  981. }
  982. pOptions->Release();
  983. }
  984. else
  985. {
  986. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to get DS object options interface with 0x%x"), hr));
  987. }
  988. //
  989. // Ask the MUP to read/write to this DC's sysvol.
  990. // We first have to get attributes for the nameless path. This causes the MUP's
  991. // cache to be initialize if it isn't already. Then we can tell
  992. // the MUP which server to use.
  993. //
  994. if (!GetFileAttributesEx (m_pFileSysPath, GetFileExInfoStandard, &fad))
  995. {
  996. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: GetFileAttributes for %s FAILED with %d."), m_pFileSysPath, GetLastError()));
  997. }
  998. //
  999. // Now we need to take the full path and trim it down to just
  1000. // domain name \ share
  1001. //
  1002. lstrcpy (szPath, m_pFileSysPath);
  1003. if ((szPath[0] != TEXT('\\')) || (szPath[1] != TEXT('\\')))
  1004. {
  1005. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Sysvol path doesn't start with \\\\")));
  1006. goto Exit;
  1007. }
  1008. lpTemp = szPath + 2;
  1009. while (*lpTemp && (*lpTemp != TEXT('\\')))
  1010. lpTemp++;
  1011. if (!(*lpTemp))
  1012. {
  1013. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to find slash between domain name and share")));
  1014. goto Exit;
  1015. }
  1016. lpTemp++;
  1017. while (*lpTemp && (*lpTemp != TEXT('\\')))
  1018. lpTemp++;
  1019. if (!(*lpTemp))
  1020. {
  1021. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to find slash between share and directory")));
  1022. goto Exit;
  1023. }
  1024. *lpTemp = TEXT('\0');
  1025. Info101.State = DFS_STORAGE_STATE_ACTIVE;
  1026. dwResult = NetDfsSetClientInfo (szPath, lpDCName,
  1027. L"SysVol", 101, (LPBYTE)&Info101);
  1028. if (dwResult != NERR_Success)
  1029. {
  1030. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to set %s as the active sysvol with %d"),
  1031. lpDCName, dwResult));
  1032. }
  1033. //
  1034. // Now load the registry information
  1035. //
  1036. if (m_dwFlags & GPO_OPEN_LOAD_REGISTRY)
  1037. {
  1038. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::OpenDSGPO: Loading registry files")));
  1039. lstrcpy (szPath, m_pFileSysPath);
  1040. lpEnd = CheckSlash (szPath);
  1041. //
  1042. // Initialize the user registry (HKCU)
  1043. //
  1044. m_pUser = new CRegistryHive();
  1045. if (!m_pUser)
  1046. {
  1047. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to create User registry")));
  1048. hr = HRESULT_FROM_WIN32(GetLastError());
  1049. goto Exit;
  1050. }
  1051. lstrcpy (lpEnd, USER_SECTION);
  1052. lstrcat (lpEnd, TEXT("\\Registry.pol"));
  1053. lstrcpy (szKeyName, m_pName);
  1054. lstrcat (szKeyName, USER_SECTION);
  1055. hr = m_pUser->Initialize (szPath, szKeyName);
  1056. if (FAILED(hr))
  1057. {
  1058. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: User registry failed to initialize")));
  1059. goto Exit;
  1060. }
  1061. //
  1062. // Initialize the machine registry (HKLM)
  1063. //
  1064. m_pMachine = new CRegistryHive();
  1065. if (!m_pMachine)
  1066. {
  1067. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenDSGPO: Failed to create machine registry")));
  1068. hr = HRESULT_FROM_WIN32(GetLastError());
  1069. goto Exit;
  1070. }
  1071. lstrcpy (lpEnd, MACHINE_SECTION);
  1072. lstrcat (lpEnd, TEXT("\\Registry.pol"));
  1073. lstrcpy (szKeyName, m_pName);
  1074. lstrcat (szKeyName, MACHINE_SECTION);
  1075. hr = m_pMachine->Initialize (szPath, szKeyName);
  1076. if (FAILED(hr))
  1077. {
  1078. DebugMsg((DM_WARNING, TEXT("CComponentData::InitializeGPT: machine registry failed to initialize")));
  1079. goto Exit;
  1080. }
  1081. }
  1082. //
  1083. // Success
  1084. //
  1085. hr = S_OK;
  1086. Exit:
  1087. if (pADsPathname)
  1088. {
  1089. pADsPathname->Release();
  1090. }
  1091. if (bstrGPOName)
  1092. {
  1093. SysFreeString (bstrGPOName);
  1094. }
  1095. if (lpDCName)
  1096. {
  1097. LocalFree (lpDCName);
  1098. }
  1099. if (pszFullPath != pszPath)
  1100. {
  1101. LocalFree (pszFullPath);
  1102. }
  1103. if (SUCCEEDED(hr))
  1104. {
  1105. m_gpoType = GPOTypeDS;
  1106. m_bInitialized = TRUE;
  1107. } else {
  1108. CleanUp();
  1109. }
  1110. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::OpenDSGPO: Leaving with a status of 0x%x"), hr));
  1111. return hr;
  1112. }
  1113. //*************************************************************
  1114. //
  1115. // OpenLocalMachineGPO()
  1116. //
  1117. // Purpose: Opens this machines GPO
  1118. //
  1119. // Parameters: dwFlags - load flags
  1120. //
  1121. // Return: S_OK if successful
  1122. //
  1123. //*************************************************************
  1124. STDMETHODIMP CGroupPolicyObject::OpenLocalMachineGPO (DWORD dwFlags)
  1125. {
  1126. HRESULT hr = E_FAIL;
  1127. TCHAR szBuffer[MAX_PATH];
  1128. TCHAR szKeyName[100];
  1129. LPTSTR lpEnd;
  1130. TCHAR szPath[MAX_PATH];
  1131. TCHAR szFuncVersion[10];
  1132. UINT uRet = 0;
  1133. //
  1134. // Check if this object has already been initialized
  1135. //
  1136. if (m_bInitialized)
  1137. {
  1138. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenLocalMachineGPO: Called on an uninitialized object.")));
  1139. return STG_E_INUSE;
  1140. }
  1141. //
  1142. // Save the flags
  1143. //
  1144. m_dwFlags = dwFlags;
  1145. //
  1146. // Get the path to the local GPO
  1147. //
  1148. ExpandEnvironmentStrings (LOCAL_GPO_DIRECTORY, szBuffer, ARRAYSIZE(szBuffer));
  1149. //
  1150. // Save the file system path
  1151. //
  1152. m_pFileSysPath = (LPTSTR) LocalAlloc (LPTR, (lstrlen(szBuffer) + 1) * sizeof(TCHAR));
  1153. if (!m_pFileSysPath)
  1154. {
  1155. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenLocalMachineGPO: Failed to allocate memory for gpt path")));
  1156. hr = HRESULT_FROM_WIN32(GetLastError());
  1157. goto Exit;
  1158. }
  1159. lstrcpy (m_pFileSysPath, szBuffer);
  1160. //
  1161. // Create the directory
  1162. //
  1163. uRet = CreateSecureDirectory (szBuffer);
  1164. if (!uRet)
  1165. {
  1166. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenLocalMachineGPO: Failed to create file system directory %s with %d"),
  1167. szBuffer, GetLastError()));
  1168. hr = HRESULT_FROM_WIN32(GetLastError());
  1169. goto Exit;
  1170. }
  1171. SetFileAttributes (szBuffer, FILE_ATTRIBUTE_HIDDEN);
  1172. //
  1173. // Check if the user has write access to the directory
  1174. //
  1175. if (!(m_dwFlags & GPO_OPEN_READ_ONLY))
  1176. {
  1177. hr = CheckFSWriteAccess (szBuffer);
  1178. if (FAILED(hr))
  1179. {
  1180. if (hr == HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED))
  1181. {
  1182. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenLocalMachineGPO: User does not have write access to this GPO (access denied).")));
  1183. goto Exit;
  1184. }
  1185. else
  1186. {
  1187. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenLocalMachineGPO: CheckFSWriteAccess failed with 0x%x"), hr));
  1188. }
  1189. }
  1190. }
  1191. if ( uRet != ERROR_ALREADY_EXISTS )
  1192. {
  1193. lstrcpy( szPath, m_pFileSysPath );
  1194. lpEnd = CheckSlash(szPath);
  1195. lstrcpy( lpEnd, TEXT("gpt.ini") );
  1196. wsprintf( szFuncVersion, TEXT("%d"), GPO_FUNCTIONALITY_VERSION );
  1197. if (!WritePrivateProfileString (TEXT("General"), TEXT("gPCFunctionalityVersion"),
  1198. szFuncVersion, szPath))
  1199. {
  1200. hr = HRESULT_FROM_WIN32(GetLastError());
  1201. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenLocalMachineGPO: Failed to write functionality version with 0x%x"), hr));
  1202. goto Exit;
  1203. }
  1204. }
  1205. lpEnd = CheckSlash(szBuffer);
  1206. //
  1207. // Create the user and machine directories
  1208. //
  1209. lstrcpy (lpEnd, MACHINE_SECTION);
  1210. if (!CreateNestedDirectory (szBuffer, NULL))
  1211. {
  1212. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenLocalMachineGPO: Failed to create machine subdirectory with %d"),
  1213. GetLastError()));
  1214. hr = HRESULT_FROM_WIN32(GetLastError());
  1215. goto Exit;
  1216. }
  1217. lstrcpy (lpEnd, USER_SECTION);
  1218. if (!CreateNestedDirectory (szBuffer, NULL))
  1219. {
  1220. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenLocalMachineGPO: Failed to create user subdirectory with %d"),
  1221. GetLastError()));
  1222. hr = HRESULT_FROM_WIN32(GetLastError());
  1223. goto Exit;
  1224. }
  1225. //
  1226. // Load the GPO name
  1227. //
  1228. LoadString (g_hInstance, IDS_LOCAL_NAME, szBuffer, ARRAYSIZE(szBuffer));
  1229. m_pName = (LPTSTR) LocalAlloc (LPTR, (lstrlen(szBuffer) + 2) * sizeof(TCHAR));
  1230. if (!m_pName)
  1231. {
  1232. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenLocalMachineGPO: Failed to allocate memory for name")));
  1233. hr = HRESULT_FROM_WIN32(GetLastError());
  1234. goto Exit;
  1235. }
  1236. lstrcpy (m_pName, szBuffer);
  1237. //
  1238. // Load the display name
  1239. //
  1240. LoadString (g_hInstance, IDS_LOCAL_DISPLAY_NAME, szBuffer, ARRAYSIZE(szBuffer));
  1241. m_pDisplayName = (LPTSTR) LocalAlloc (LPTR, (lstrlen(szBuffer) + 2) * sizeof(TCHAR));
  1242. if (!m_pDisplayName)
  1243. {
  1244. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenLocalMachineGPO: Failed to allocate memory for display name")));
  1245. hr = HRESULT_FROM_WIN32(GetLastError());
  1246. goto Exit;
  1247. }
  1248. lstrcpy (m_pDisplayName, szBuffer);
  1249. //
  1250. // Now load the registry information
  1251. //
  1252. if (m_dwFlags & GPO_OPEN_LOAD_REGISTRY)
  1253. {
  1254. lstrcpy (szBuffer, m_pFileSysPath);
  1255. lpEnd = CheckSlash (szBuffer);
  1256. //
  1257. // Initialize the user registry (HKCU)
  1258. //
  1259. m_pUser = new CRegistryHive();
  1260. if (!m_pUser)
  1261. {
  1262. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenLocalMachineGPO: Failed to create User registry")));
  1263. hr = HRESULT_FROM_WIN32(GetLastError());
  1264. goto Exit;
  1265. }
  1266. lstrcpy (lpEnd, USER_SECTION);
  1267. lstrcat (lpEnd, TEXT("\\Registry.pol"));
  1268. lstrcpy (szKeyName, m_pName);
  1269. lstrcat (szKeyName, USER_SECTION);
  1270. hr = m_pUser->Initialize (szBuffer, szKeyName);
  1271. if (FAILED(hr))
  1272. {
  1273. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenLocalMachineGPO: User registry failed to initialize")));
  1274. goto Exit;
  1275. }
  1276. //
  1277. // Initialize the machine registry (HKLM)
  1278. //
  1279. m_pMachine = new CRegistryHive();
  1280. if (!m_pMachine)
  1281. {
  1282. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenLocalMachineGPO: Failed to create machine registry")));
  1283. hr = HRESULT_FROM_WIN32(GetLastError());
  1284. goto Exit;
  1285. }
  1286. lstrcpy (lpEnd, MACHINE_SECTION);
  1287. lstrcat (lpEnd, TEXT("\\Registry.pol"));
  1288. lstrcpy (szKeyName, m_pName);
  1289. lstrcat (szKeyName, MACHINE_SECTION);
  1290. hr = m_pMachine->Initialize (szBuffer, szKeyName);
  1291. if (FAILED(hr))
  1292. {
  1293. DebugMsg((DM_WARNING, TEXT("CComponentData::InitializeGPT: machine registry failed to initialize")));
  1294. goto Exit;
  1295. }
  1296. }
  1297. //
  1298. // Success
  1299. //
  1300. hr = S_OK;
  1301. Exit:
  1302. if (SUCCEEDED(hr))
  1303. {
  1304. m_gpoType = GPOTypeLocal;
  1305. m_bInitialized = TRUE;
  1306. } else {
  1307. CleanUp();
  1308. }
  1309. return hr;
  1310. }
  1311. //*************************************************************
  1312. //
  1313. // OpenRemoteMachineGPO()
  1314. //
  1315. // Purpose: Opens a remote machines GPO
  1316. // dwFlags - load flags
  1317. //
  1318. // Parameters: pszComputerName - name of computer
  1319. //
  1320. // Return: S_OK if successful
  1321. //
  1322. //*************************************************************
  1323. STDMETHODIMP CGroupPolicyObject::OpenRemoteMachineGPO (LPOLESTR pszComputerName,
  1324. DWORD dwFlags)
  1325. {
  1326. HRESULT hr = E_FAIL;
  1327. TCHAR szComputerName[MAX_PATH];
  1328. TCHAR szBuffer[MAX_PATH];
  1329. TCHAR szKeyName[100];
  1330. LPTSTR lpEnd;
  1331. TCHAR szPath[MAX_PATH];
  1332. TCHAR szFuncVersion[10];
  1333. UINT uRet = 0;
  1334. //
  1335. // Check if this object has already been initialized
  1336. //
  1337. if (m_bInitialized)
  1338. {
  1339. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenRemoteMachineGPO: Called on an uninitialized object.")));
  1340. return STG_E_INUSE;
  1341. }
  1342. //
  1343. // Check parameters
  1344. //
  1345. if (!pszComputerName)
  1346. return E_INVALIDARG;
  1347. //
  1348. // Save the flags
  1349. //
  1350. m_dwFlags = dwFlags;
  1351. //
  1352. // Parse the computer name
  1353. //
  1354. if ((pszComputerName[0] == TEXT('\\')) && (pszComputerName[1] == TEXT('\\')))
  1355. {
  1356. lstrcpy(szComputerName, pszComputerName+2);
  1357. }
  1358. else
  1359. {
  1360. lstrcpy(szComputerName, pszComputerName);
  1361. }
  1362. //
  1363. // Save the machine name
  1364. //
  1365. m_pMachineName = (LPTSTR) LocalAlloc (LPTR, (lstrlen(szComputerName) + 1) * sizeof(TCHAR));
  1366. if (!m_pMachineName)
  1367. {
  1368. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenRemoteMachineGPO: Failed to allocate memory for machine name")));
  1369. hr = HRESULT_FROM_WIN32(GetLastError());
  1370. goto Exit;
  1371. }
  1372. lstrcpy (m_pMachineName, szComputerName);
  1373. //
  1374. // Get the path to the local GPO
  1375. //
  1376. wsprintf (szBuffer, REMOTE_GPO_DIRECTORY, szComputerName);
  1377. //
  1378. // Save the file system path
  1379. //
  1380. m_pFileSysPath = (LPTSTR) LocalAlloc (LPTR, (lstrlen(szBuffer) + 1) * sizeof(TCHAR));
  1381. if (!m_pFileSysPath)
  1382. {
  1383. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenRemoteMachineGPO: Failed to allocate memory for gpt path")));
  1384. hr = HRESULT_FROM_WIN32(GetLastError());
  1385. goto Exit;
  1386. }
  1387. lstrcpy (m_pFileSysPath, szBuffer);
  1388. //
  1389. // Create the directory
  1390. //
  1391. uRet = CreateSecureDirectory (szBuffer);
  1392. if (!uRet)
  1393. {
  1394. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenRemoteMachineGPO: Failed to create file system directory %s with %d"),
  1395. szBuffer, GetLastError()));
  1396. hr = HRESULT_FROM_WIN32(GetLastError());
  1397. goto Exit;
  1398. }
  1399. SetFileAttributes (szBuffer, FILE_ATTRIBUTE_HIDDEN);
  1400. //
  1401. // Check if the user has write access to the directory
  1402. //
  1403. if (!(m_dwFlags & GPO_OPEN_READ_ONLY))
  1404. {
  1405. hr = CheckFSWriteAccess (szBuffer);
  1406. if (FAILED(hr))
  1407. {
  1408. if (hr == HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED))
  1409. {
  1410. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenRemoteMachineGPO: User does not have write access to this GPO (access denied).")));
  1411. goto Exit;
  1412. }
  1413. else
  1414. {
  1415. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenRemoteMachineGPO: CheckFSWriteAccess failed with 0x%x"), hr));
  1416. }
  1417. }
  1418. }
  1419. if ( uRet != ERROR_ALREADY_EXISTS )
  1420. {
  1421. lstrcpy( szPath, m_pFileSysPath );
  1422. lpEnd = CheckSlash(szPath);
  1423. lstrcpy( lpEnd, TEXT("gpt.ini") );
  1424. wsprintf( szFuncVersion, TEXT("%d"), GPO_FUNCTIONALITY_VERSION );
  1425. if (!WritePrivateProfileString (TEXT("General"), TEXT("gPCFunctionalityVersion"),
  1426. szFuncVersion, szPath))
  1427. {
  1428. hr = HRESULT_FROM_WIN32(GetLastError());
  1429. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenRemoteMachineGPO: Failed to write functionality version with 0x%x"), hr));
  1430. goto Exit;
  1431. }
  1432. }
  1433. lpEnd = CheckSlash(szBuffer);
  1434. //
  1435. // Create the user and machine directories
  1436. //
  1437. lstrcpy (lpEnd, MACHINE_SECTION);
  1438. if (!CreateNestedDirectory (szBuffer, NULL))
  1439. {
  1440. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenRemoteMachineGPO: Failed to create machine subdirectory with %d"),
  1441. GetLastError()));
  1442. hr = HRESULT_FROM_WIN32(GetLastError());
  1443. goto Exit;
  1444. }
  1445. lstrcpy (lpEnd, USER_SECTION);
  1446. if (!CreateNestedDirectory (szBuffer, NULL))
  1447. {
  1448. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenRemoteMachineGPO: Failed to create user subdirectory with %d"),
  1449. GetLastError()));
  1450. hr = HRESULT_FROM_WIN32(GetLastError());
  1451. goto Exit;
  1452. }
  1453. //
  1454. // Load the GPO name
  1455. //
  1456. m_pName = (LPTSTR) LocalAlloc (LPTR, (lstrlen(szComputerName) + 2) * sizeof(TCHAR));
  1457. if (!m_pName)
  1458. {
  1459. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenRemoteMachineGPO: Failed to allocate memory for name")));
  1460. hr = HRESULT_FROM_WIN32(GetLastError());
  1461. goto Exit;
  1462. }
  1463. lstrcpy (m_pName, szComputerName);
  1464. //
  1465. // Load the display name
  1466. //
  1467. m_pDisplayName = (LPTSTR) LocalAlloc (LPTR, (lstrlen(szComputerName) + 2) * sizeof(TCHAR));
  1468. if (!m_pDisplayName)
  1469. {
  1470. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenRemoteMachineGPO: Failed to allocate memory for display name")));
  1471. hr = HRESULT_FROM_WIN32(GetLastError());
  1472. goto Exit;
  1473. }
  1474. lstrcpy (m_pDisplayName, szComputerName);
  1475. //
  1476. // Now load the registry information
  1477. //
  1478. if (m_dwFlags & GPO_OPEN_LOAD_REGISTRY)
  1479. {
  1480. lstrcpy (szBuffer, m_pFileSysPath);
  1481. lpEnd = CheckSlash (szBuffer);
  1482. //
  1483. // Initialize the user registry (HKCU)
  1484. //
  1485. m_pUser = new CRegistryHive();
  1486. if (!m_pUser)
  1487. {
  1488. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenRemoteMachineGPO: Failed to create User registry")));
  1489. hr = HRESULT_FROM_WIN32(GetLastError());
  1490. goto Exit;
  1491. }
  1492. lstrcpy (lpEnd, USER_SECTION);
  1493. lstrcat (lpEnd, TEXT("\\Registry.pol"));
  1494. lstrcpy (szKeyName, m_pName);
  1495. lstrcat (szKeyName, USER_SECTION);
  1496. hr = m_pUser->Initialize (szBuffer, szKeyName);
  1497. if (FAILED(hr))
  1498. {
  1499. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenRemoteMachineGPO: User registry failed to initialize")));
  1500. goto Exit;
  1501. }
  1502. //
  1503. // Initialize the machine registry (HKLM)
  1504. //
  1505. m_pMachine = new CRegistryHive();
  1506. if (!m_pMachine)
  1507. {
  1508. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OpenRemoteMachineGPO: Failed to create machine registry")));
  1509. hr = HRESULT_FROM_WIN32(GetLastError());
  1510. goto Exit;
  1511. }
  1512. lstrcpy (lpEnd, MACHINE_SECTION);
  1513. lstrcat (lpEnd, TEXT("\\Registry.pol"));
  1514. lstrcpy (szKeyName, m_pName);
  1515. lstrcat (szKeyName, MACHINE_SECTION);
  1516. hr = m_pMachine->Initialize (szBuffer, szKeyName);
  1517. if (FAILED(hr))
  1518. {
  1519. DebugMsg((DM_WARNING, TEXT("CComponentData::InitializeGPT: machine registry failed to initialize")));
  1520. goto Exit;
  1521. }
  1522. }
  1523. //
  1524. // Success
  1525. //
  1526. hr = S_OK;
  1527. Exit:
  1528. if (SUCCEEDED(hr))
  1529. {
  1530. m_gpoType = GPOTypeRemote;
  1531. m_bInitialized = TRUE;
  1532. } else {
  1533. CleanUp();
  1534. }
  1535. return hr;
  1536. }
  1537. //*************************************************************
  1538. //
  1539. // Save()
  1540. //
  1541. // Purpose: Saves the registry information and bumps the
  1542. // version number
  1543. //
  1544. // Parameters: none
  1545. //
  1546. // Return: S_OK if successful
  1547. //
  1548. //*************************************************************
  1549. STDMETHODIMP CGroupPolicyObject::Save (BOOL bMachine, BOOL bAdd, GUID *pGuidExtension, GUID *pGuidSnapin)
  1550. {
  1551. HRESULT hr;
  1552. TCHAR szPath[2*MAX_PATH];
  1553. TCHAR szVersion[25];
  1554. ULONG ulVersion, ulOriginal;
  1555. USHORT uMachine, uUser;
  1556. BSTR bstrName;
  1557. VARIANT var;
  1558. GUID RegistryGuid = REGISTRY_EXTENSION_GUID;
  1559. BOOL bEmpty;
  1560. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::Save: Entering with bMachine = %d and bAdd = %d"),
  1561. bMachine, bAdd));
  1562. if (!m_bInitialized)
  1563. {
  1564. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Save: Called on an uninitialized object.")));
  1565. return OLE_E_BLANK;
  1566. }
  1567. if ( pGuidExtension == 0 || pGuidSnapin == 0 )
  1568. {
  1569. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Save: One of the guids is null")));
  1570. return ERROR_INVALID_PARAMETER;
  1571. }
  1572. if (m_dwFlags & GPO_OPEN_READ_ONLY)
  1573. {
  1574. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Save: Called on a READ ONLY GPO")));
  1575. return HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED);
  1576. }
  1577. //
  1578. // Save registry settings
  1579. //
  1580. if (!CompareGuid (pGuidExtension, &RegistryGuid))
  1581. {
  1582. if (bMachine)
  1583. {
  1584. if (m_pMachine)
  1585. {
  1586. hr = m_pMachine->Save();
  1587. if (FAILED(hr))
  1588. {
  1589. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Save: Failed to save the machine registry with 0x%x"), hr));
  1590. return hr;
  1591. }
  1592. hr = m_pMachine->IsRegistryEmpty(&bEmpty);
  1593. if (SUCCEEDED(hr) && bEmpty)
  1594. {
  1595. bAdd = FALSE;
  1596. }
  1597. else
  1598. {
  1599. bAdd = TRUE;
  1600. }
  1601. }
  1602. }
  1603. else
  1604. {
  1605. if (m_pUser)
  1606. {
  1607. hr = m_pUser->Save();
  1608. if (FAILED(hr))
  1609. {
  1610. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Save: Failed to save the user registry with 0x%x"), hr));
  1611. return hr;
  1612. }
  1613. hr = m_pUser->IsRegistryEmpty(&bEmpty);
  1614. if (SUCCEEDED(hr) && bEmpty)
  1615. {
  1616. bAdd = FALSE;
  1617. }
  1618. else
  1619. {
  1620. bAdd = TRUE;
  1621. }
  1622. }
  1623. }
  1624. }
  1625. XPtrST<TCHAR> xValueIn;
  1626. hr = GetProperty( bMachine ? GPO_MACHEXTENSION_NAMES
  1627. : GPO_USEREXTENSION_NAMES,
  1628. xValueIn );
  1629. if ( FAILED(hr) )
  1630. {
  1631. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Save: Failed to GetProperty with 0x%x"), hr));
  1632. return hr;
  1633. }
  1634. CGuidList guidList;
  1635. hr = guidList.UnMarshallGuids( xValueIn.GetPointer() );
  1636. if ( FAILED(hr) )
  1637. {
  1638. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Save: Failed to unmarshall guids with 0x%x"), hr));
  1639. return hr;
  1640. }
  1641. hr = guidList.Update( bAdd, pGuidExtension, pGuidSnapin );
  1642. if ( FAILED(hr) )
  1643. {
  1644. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Save: Failed to update with 0x%x"), hr));
  1645. return hr;
  1646. }
  1647. if ( guidList.GuidsChanged() )
  1648. {
  1649. XPtrST<TCHAR> xValueOut;
  1650. hr = guidList.MarshallGuids( xValueOut );
  1651. if ( FAILED(hr ) )
  1652. {
  1653. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Save: Failed to marshall guids with 0x%x"), hr));
  1654. return hr;
  1655. }
  1656. hr = SetProperty( bMachine ? GPO_MACHEXTENSION_NAMES
  1657. : GPO_USEREXTENSION_NAMES,
  1658. xValueOut.GetPointer() );
  1659. if ( FAILED(hr ) )
  1660. {
  1661. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Save: Failed to set property guids with 0x%x"), hr));
  1662. return hr;
  1663. }
  1664. }
  1665. //
  1666. // Get the current version number
  1667. //
  1668. lstrcpy (szPath, m_pFileSysPath);
  1669. lstrcat (szPath, TEXT("\\GPT.INI"));
  1670. if (m_gpoType == GPOTypeDS)
  1671. {
  1672. bstrName = SysAllocString (GPO_VERSION_PROPERTY);
  1673. if (!bstrName)
  1674. {
  1675. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Save: Failed to allocate memory")));
  1676. return ERROR_OUTOFMEMORY;
  1677. }
  1678. VariantInit(&var);
  1679. hr = m_pADs->Get(bstrName, &var);
  1680. if (SUCCEEDED(hr))
  1681. {
  1682. ulOriginal = var.lVal;
  1683. }
  1684. SysFreeString (bstrName);
  1685. VariantClear (&var);
  1686. if (FAILED(hr))
  1687. {
  1688. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Save: Failed to get ds version number with 0x%x"), hr));
  1689. return hr;
  1690. }
  1691. }
  1692. else
  1693. {
  1694. ulOriginal = GetPrivateProfileInt(TEXT("General"), TEXT("Version"), 0, szPath);
  1695. }
  1696. //
  1697. // Separate the user and machine version numbers
  1698. //
  1699. uUser = (USHORT) HIWORD(ulOriginal);
  1700. uMachine = (USHORT) LOWORD(ulOriginal);
  1701. //
  1702. // Increment the version number
  1703. //
  1704. if (bMachine)
  1705. {
  1706. uMachine = uMachine + 1;
  1707. if (uMachine == 0)
  1708. uMachine++;
  1709. }
  1710. else
  1711. {
  1712. uUser = uUser + 1;
  1713. if (uUser == 0)
  1714. uUser++;
  1715. }
  1716. //
  1717. // Put the version number back together
  1718. //
  1719. ulVersion = (ULONG) MAKELONG (uMachine, uUser);
  1720. //
  1721. // Update version number in the GPT
  1722. //
  1723. wsprintf (szVersion, TEXT("%d"), ulVersion);
  1724. if (!WritePrivateProfileString (TEXT("General"), TEXT("Version"),
  1725. szVersion, szPath))
  1726. {
  1727. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Save: Failed to write sysvol version number with %d"),
  1728. GetLastError()));
  1729. return HRESULT_FROM_WIN32(GetLastError());
  1730. }
  1731. //
  1732. // Put the original version number in szVersion in case
  1733. // we need to roll backwards below
  1734. //
  1735. wsprintf (szVersion, TEXT("%d"), ulOriginal);
  1736. //
  1737. // Set the version number in the GPC
  1738. //
  1739. if (m_gpoType == GPOTypeDS)
  1740. {
  1741. bstrName = SysAllocString (GPO_VERSION_PROPERTY);
  1742. if (bstrName)
  1743. {
  1744. VariantInit(&var);
  1745. var.vt = VT_I4;
  1746. var.lVal = ulVersion;
  1747. hr = m_pADs->Put(bstrName, var);
  1748. VariantClear (&var);
  1749. SysFreeString (bstrName);
  1750. }
  1751. else
  1752. {
  1753. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Save: Failed to allocate memory")));
  1754. hr = ERROR_OUTOFMEMORY;
  1755. }
  1756. if (SUCCEEDED(hr))
  1757. {
  1758. //
  1759. // Commit the changes
  1760. //
  1761. hr = m_pADs->SetInfo();
  1762. if (FAILED(hr))
  1763. {
  1764. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Save: Failed to commit version number update with 0x%x"), hr));
  1765. if (!WritePrivateProfileString (TEXT("General"), TEXT("Version"),
  1766. szVersion, szPath))
  1767. {
  1768. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Save: Failed to reset the sysvol version number with %d"),
  1769. GetLastError()));
  1770. }
  1771. }
  1772. } else {
  1773. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Save: Failed to update version number with 0x%x"), hr));
  1774. if (!WritePrivateProfileString (TEXT("General"), TEXT("Version"), szVersion, szPath))
  1775. {
  1776. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Save: Failed to reset the sysvol version number with %d"),
  1777. GetLastError()));
  1778. }
  1779. }
  1780. }
  1781. //
  1782. // If we are editing the local group policy object, then call
  1783. // RefreshGroupPolicy() so that the end user can see the results
  1784. // immediately.
  1785. //
  1786. if (m_gpoType == GPOTypeLocal)
  1787. {
  1788. RefreshGroupPolicy (bMachine);
  1789. }
  1790. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::Save: Leaving with 0x%x"), hr));
  1791. return hr;
  1792. }
  1793. //*************************************************************
  1794. //
  1795. // Delete()
  1796. //
  1797. // Purpose: Deletes this Group Policy Object
  1798. //
  1799. // Parameters: none
  1800. //
  1801. // Return: S_OK if successful
  1802. //
  1803. //*************************************************************
  1804. STDMETHODIMP CGroupPolicyObject::Delete (void)
  1805. {
  1806. HRESULT hr;
  1807. if (!m_bInitialized)
  1808. {
  1809. return OLE_E_BLANK;
  1810. }
  1811. if (m_dwFlags & GPO_OPEN_READ_ONLY)
  1812. {
  1813. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Delete: Delete called on a READ ONLY GPO")));
  1814. return HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED);
  1815. }
  1816. //
  1817. // Unmount the registry information
  1818. //
  1819. if (m_pUser)
  1820. {
  1821. m_pUser->Release();
  1822. m_pUser = NULL;
  1823. }
  1824. if (m_pMachine)
  1825. {
  1826. m_pMachine->Release();
  1827. m_pMachine = NULL;
  1828. }
  1829. //
  1830. // Clean out the DS stuff
  1831. //
  1832. if (m_gpoType == GPOTypeDS)
  1833. {
  1834. hr = DSDelnode (m_pDSPath);
  1835. if (FAILED(hr))
  1836. {
  1837. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Delete: Failed to delete DS storage with 0x%x"), hr));
  1838. goto Exit;
  1839. }
  1840. }
  1841. //
  1842. // Delete the file system stuff
  1843. //
  1844. if (Delnode (m_pFileSysPath))
  1845. {
  1846. hr = S_OK;
  1847. }
  1848. else
  1849. {
  1850. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::Delete: Failed to delete file system storage with %d"),
  1851. GetLastError()));
  1852. hr = HRESULT_FROM_WIN32(GetLastError());
  1853. }
  1854. if (SUCCEEDED(hr))
  1855. {
  1856. CleanUp();
  1857. }
  1858. Exit:
  1859. return hr;
  1860. }
  1861. //*************************************************************
  1862. //
  1863. // GetName()
  1864. //
  1865. // Purpose: Gets the unique GPO name
  1866. //
  1867. // Parameters: pszName is a pointer to a buffer which receives the name
  1868. // cchMaxLength is the max size of the buffer
  1869. //
  1870. // Return: S_OK if successful
  1871. //
  1872. //*************************************************************
  1873. STDMETHODIMP CGroupPolicyObject::GetName (LPOLESTR pszName, int cchMaxLength)
  1874. {
  1875. //
  1876. // Check parameters
  1877. //
  1878. if (!pszName || (cchMaxLength <= 0))
  1879. return E_INVALIDARG;
  1880. if (!m_bInitialized)
  1881. {
  1882. return OLE_E_BLANK;
  1883. }
  1884. //
  1885. // Save the name
  1886. //
  1887. if ((lstrlen (m_pName) + 1) <= cchMaxLength)
  1888. {
  1889. lstrcpy (pszName, m_pName);
  1890. return S_OK;
  1891. }
  1892. return E_OUTOFMEMORY;
  1893. }
  1894. //*************************************************************
  1895. //
  1896. // GetDisplayName()
  1897. //
  1898. // Purpose: Gets the friendly name for this GPO
  1899. //
  1900. // Parameters: pszName is a pointer to a buffer which receives the name
  1901. // cchMaxLength is the max size of the buffer
  1902. //
  1903. // Return: S_OK if successful
  1904. //
  1905. //*************************************************************
  1906. STDMETHODIMP CGroupPolicyObject::GetDisplayName (LPOLESTR pszName, int cchMaxLength)
  1907. {
  1908. //
  1909. // Check parameters
  1910. //
  1911. if (!pszName || (cchMaxLength <= 0))
  1912. return E_INVALIDARG;
  1913. if (!m_bInitialized)
  1914. {
  1915. return OLE_E_BLANK;
  1916. }
  1917. if ((lstrlen (m_pDisplayName) + 1) <= cchMaxLength)
  1918. {
  1919. lstrcpy (pszName, m_pDisplayName);
  1920. return S_OK;
  1921. }
  1922. return E_OUTOFMEMORY;
  1923. }
  1924. //+--------------------------------------------------------------------------
  1925. //
  1926. // Member: CGroupPolicyObject::SetDisplayName
  1927. //
  1928. // Synopsis: changes the friendly display name for a group policy object
  1929. //
  1930. // Arguments: [pszName] - new name (can be NULL to clear name)
  1931. //
  1932. // Returns: S_OK - success
  1933. //
  1934. // Modifies:
  1935. //
  1936. // Derivation:
  1937. //
  1938. // History: 05-002-1998 stevebl Created
  1939. //
  1940. // Notes:
  1941. //
  1942. //---------------------------------------------------------------------------
  1943. STDMETHODIMP CGroupPolicyObject::SetDisplayName (LPOLESTR lpDisplayName)
  1944. {
  1945. HRESULT hr = E_FAIL;
  1946. BSTR bstrName;
  1947. VARIANT var;
  1948. LPOLESTR lpNewName;
  1949. LPTSTR lpPath, lpEnd;
  1950. DWORD dwSize;
  1951. //
  1952. // Check parameters
  1953. //
  1954. if (!m_bInitialized)
  1955. {
  1956. return OLE_E_BLANK;
  1957. }
  1958. if (m_gpoType != GPOTypeDS)
  1959. {
  1960. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetDisplayName: Called for a non DS GPO")));
  1961. hr = E_INVALIDARG;
  1962. goto Exit;
  1963. }
  1964. if (!lpDisplayName)
  1965. {
  1966. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetDisplayName: NULL display name")));
  1967. hr = E_INVALIDARG;
  1968. goto Exit;
  1969. }
  1970. if (m_dwFlags & GPO_OPEN_READ_ONLY)
  1971. {
  1972. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetDisplayName: Called for a READ ONLY GPO")));
  1973. hr = HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED);
  1974. goto Exit;
  1975. }
  1976. //
  1977. // Call the internal method to set the display name
  1978. //
  1979. hr = SetDisplayNameI (m_pADs, lpDisplayName, m_pFileSysPath, TRUE);
  1980. Exit:
  1981. return hr;
  1982. }
  1983. //*************************************************************
  1984. //
  1985. // GetPath()
  1986. //
  1987. // Purpose: Returns the path to the GPO
  1988. //
  1989. // If the GPO is in the DS, this is an DN path
  1990. // If the GPO is machine based, it is a file system path
  1991. //
  1992. // Parameters: pszPath is a pointer to a buffer which receives the path
  1993. // cchMaxLength is the max size of the buffer
  1994. //
  1995. // Return: S_OK if successful
  1996. //
  1997. //*************************************************************
  1998. STDMETHODIMP CGroupPolicyObject::GetPath (LPOLESTR pszPath, int cchMaxLength)
  1999. {
  2000. LPTSTR lpTemp;
  2001. //
  2002. // Check parameters
  2003. //
  2004. if (!pszPath || (cchMaxLength <= 0))
  2005. return E_INVALIDARG;
  2006. if (!m_bInitialized)
  2007. {
  2008. return OLE_E_BLANK;
  2009. }
  2010. if (m_gpoType == GPOTypeDS)
  2011. {
  2012. lpTemp = MakeNamelessPath (m_pDSPath);
  2013. if (lpTemp)
  2014. {
  2015. if ((lstrlen (lpTemp) + 1) <= cchMaxLength)
  2016. {
  2017. lstrcpy (pszPath, lpTemp);
  2018. LocalFree (lpTemp);
  2019. return S_OK;
  2020. }
  2021. LocalFree (lpTemp);
  2022. }
  2023. }
  2024. else
  2025. {
  2026. if ((lstrlen (m_pFileSysPath) + 1) <= cchMaxLength)
  2027. {
  2028. lstrcpy (pszPath, m_pFileSysPath);
  2029. return S_OK;
  2030. }
  2031. }
  2032. return E_OUTOFMEMORY;
  2033. }
  2034. //*************************************************************
  2035. //
  2036. // GetDSPath()
  2037. //
  2038. // Purpose: Returns a DS path to the requested section
  2039. //
  2040. // Parameters: dwSection identifies root vs user vs machine
  2041. // pszPath is a pointer to a buffer which receives the path
  2042. // cchMaxLength is the max size of the buffer
  2043. //
  2044. // Return: S_OK if successful
  2045. //
  2046. //*************************************************************
  2047. STDMETHODIMP CGroupPolicyObject::GetDSPath (DWORD dwSection, LPOLESTR pszPath, int cchMaxPath)
  2048. {
  2049. HRESULT hr = E_FAIL;
  2050. BSTR bstrPath = NULL;
  2051. TCHAR szTemp[100];
  2052. IADsPathname * pADsPathname = NULL;
  2053. //
  2054. // Check for initialization
  2055. //
  2056. if (!m_bInitialized)
  2057. {
  2058. return OLE_E_BLANK;
  2059. }
  2060. //
  2061. // Check parameters
  2062. //
  2063. if (!pszPath || (cchMaxPath <= 0))
  2064. return E_INVALIDARG;
  2065. if ((dwSection != GPO_SECTION_ROOT) &&
  2066. (dwSection != GPO_SECTION_USER) &&
  2067. (dwSection != GPO_SECTION_MACHINE))
  2068. return E_INVALIDARG;
  2069. //
  2070. // If this is a local or remote machine GPO, then the
  2071. // caller gets an empty string back.
  2072. //
  2073. if (m_gpoType != GPOTypeDS)
  2074. {
  2075. *pszPath = TEXT('\0');
  2076. hr = S_OK;
  2077. goto Exit;
  2078. }
  2079. //
  2080. // Create a pathname object we can work with
  2081. //
  2082. hr = CoCreateInstance(CLSID_Pathname, NULL, CLSCTX_INPROC_SERVER,
  2083. IID_IADsPathname, (LPVOID*)&pADsPathname);
  2084. if (FAILED(hr))
  2085. {
  2086. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetDSPath: Failed to create adspathname instance with 0x%x"), hr));
  2087. goto Exit;
  2088. }
  2089. //
  2090. // Add the GPO name
  2091. //
  2092. hr = pADsPathname->Set (m_pDSPath, ADS_SETTYPE_FULL);
  2093. if (FAILED(hr))
  2094. {
  2095. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetDSPath: Failed to set pathname with 0x%x"), hr));
  2096. goto Exit;
  2097. }
  2098. //
  2099. // Add the appropriate subcontainer
  2100. //
  2101. if (dwSection != GPO_SECTION_ROOT)
  2102. {
  2103. lstrcpy (szTemp, TEXT("CN="));
  2104. if (dwSection == GPO_SECTION_USER)
  2105. lstrcat (szTemp, USER_SECTION);
  2106. else
  2107. lstrcat (szTemp, MACHINE_SECTION);
  2108. hr = pADsPathname->AddLeafElement (szTemp);
  2109. if (FAILED(hr))
  2110. {
  2111. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetDSPath: Failed to add subcontainer with 0x%x"), hr));
  2112. goto Exit;
  2113. }
  2114. }
  2115. hr = pADsPathname->Retrieve (ADS_FORMAT_X500_NO_SERVER, &bstrPath);
  2116. if (FAILED(hr))
  2117. {
  2118. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetDSPath: Failed to retreive container path with 0x%x"), hr));
  2119. goto Exit;
  2120. }
  2121. if ((lstrlen(bstrPath) + 1) <= cchMaxPath)
  2122. {
  2123. lstrcpy (pszPath, bstrPath);
  2124. hr = S_OK;
  2125. }
  2126. else
  2127. {
  2128. hr = E_OUTOFMEMORY;
  2129. }
  2130. SysFreeString (bstrPath);
  2131. Exit:
  2132. if (pADsPathname)
  2133. {
  2134. pADsPathname->Release();
  2135. }
  2136. return hr;
  2137. }
  2138. //*************************************************************
  2139. //
  2140. // GetFileSysPath()
  2141. //
  2142. // Purpose: Returns the file system path to the requested section
  2143. //
  2144. // Parameters: dwSection identifies user vs machine
  2145. // pszPath is a pointer to a buffer which receives the path
  2146. // cchMaxLength is the max size of the buffer
  2147. //
  2148. // Return: S_OK if successful
  2149. //
  2150. //*************************************************************
  2151. STDMETHODIMP CGroupPolicyObject::GetFileSysPath (DWORD dwSection, LPOLESTR pszPath, int cchMaxPath)
  2152. {
  2153. TCHAR szPath[2*MAX_PATH];
  2154. LPTSTR lpEnd;
  2155. //
  2156. // Check parameters
  2157. //
  2158. if (!pszPath || (cchMaxPath <= 0))
  2159. return E_INVALIDARG;
  2160. if (!m_bInitialized)
  2161. {
  2162. return OLE_E_BLANK;
  2163. }
  2164. lstrcpy (szPath, m_pFileSysPath);
  2165. if (dwSection != GPO_SECTION_ROOT)
  2166. {
  2167. if (dwSection == GPO_SECTION_USER)
  2168. {
  2169. lpEnd = CheckSlash (szPath);
  2170. lstrcpy (lpEnd, USER_SECTION);
  2171. }
  2172. else if (dwSection == GPO_SECTION_MACHINE)
  2173. {
  2174. lpEnd = CheckSlash (szPath);
  2175. lstrcpy (lpEnd, MACHINE_SECTION);
  2176. }
  2177. else
  2178. {
  2179. return E_INVALIDARG;
  2180. }
  2181. }
  2182. if ((lstrlen(szPath) + 1) <= cchMaxPath)
  2183. {
  2184. lstrcpy (pszPath, szPath);
  2185. return S_OK;
  2186. }
  2187. return E_OUTOFMEMORY;
  2188. }
  2189. //*************************************************************
  2190. //
  2191. // GetRegistryKey()
  2192. //
  2193. // Purpose: Returns the requested registry key
  2194. //
  2195. // Parameters: dwSection identifies user vs machine
  2196. // hKey receives the opened registry key
  2197. //
  2198. // Return: S_OK if successful
  2199. //
  2200. //*************************************************************
  2201. STDMETHODIMP CGroupPolicyObject::GetRegistryKey (DWORD dwSection, HKEY *hKey)
  2202. {
  2203. HRESULT hr = E_FAIL;
  2204. if (!m_bInitialized)
  2205. {
  2206. return OLE_E_BLANK;
  2207. }
  2208. switch (dwSection)
  2209. {
  2210. case GPO_SECTION_USER:
  2211. if (m_pUser)
  2212. {
  2213. hr = m_pUser->GetHKey(hKey);
  2214. }
  2215. break;
  2216. case GPO_SECTION_MACHINE:
  2217. if (m_pMachine)
  2218. {
  2219. hr = m_pMachine->GetHKey(hKey);
  2220. }
  2221. break;
  2222. }
  2223. return (hr);
  2224. }
  2225. //*************************************************************
  2226. //
  2227. // GetOptions()
  2228. //
  2229. // Purpose: Gets the GPO options
  2230. //
  2231. // Parameters: dwOptions receives the options
  2232. //
  2233. // Return: S_OK if successful
  2234. //
  2235. //*************************************************************
  2236. STDMETHODIMP CGroupPolicyObject::GetOptions (DWORD * dwOptions)
  2237. {
  2238. HRESULT hr;
  2239. //
  2240. // Check for initialization
  2241. //
  2242. if (!m_bInitialized)
  2243. {
  2244. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetOptions: Called on an uninitialized object.")));
  2245. return OLE_E_BLANK;
  2246. }
  2247. //
  2248. // Check argument
  2249. //
  2250. if (!dwOptions)
  2251. {
  2252. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetOptions: Received a NULL ptr.")));
  2253. return E_INVALIDARG;
  2254. }
  2255. //
  2256. // If this is a DS GPO, the options are stored as a property on the
  2257. // GPC. If this a machine GPO, they are in the gpt.ini file.
  2258. //
  2259. if (m_gpoType == GPOTypeDS)
  2260. {
  2261. VARIANT var;
  2262. BSTR bstrProperty;
  2263. //
  2264. // Query for the options
  2265. //
  2266. bstrProperty = SysAllocString (GPO_OPTIONS_PROPERTY);
  2267. if (bstrProperty)
  2268. {
  2269. VariantInit(&var);
  2270. hr = m_pADs->Get(bstrProperty, &var);
  2271. if (SUCCEEDED(hr))
  2272. {
  2273. *dwOptions = var.lVal;
  2274. }
  2275. else
  2276. {
  2277. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetOptions: Failed to query for options with 0x%x"), hr));
  2278. }
  2279. VariantClear (&var);
  2280. SysFreeString (bstrProperty);
  2281. }
  2282. else
  2283. {
  2284. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetOptions: Failed to allocate memory")));
  2285. hr = ERROR_OUTOFMEMORY;
  2286. }
  2287. }
  2288. else
  2289. {
  2290. TCHAR szPath[2*MAX_PATH];
  2291. LPTSTR lpEnd;
  2292. //
  2293. // Get the file system path
  2294. //
  2295. hr = GetPath (szPath, ARRAYSIZE(szPath));
  2296. if (FAILED(hr))
  2297. {
  2298. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetOptions: Failed to get path with 0x%x"), hr));
  2299. return hr;
  2300. }
  2301. //
  2302. // Tack on gpt.ini
  2303. //
  2304. lpEnd = CheckSlash (szPath);
  2305. lstrcpy (lpEnd, TEXT("GPT.INI"));
  2306. //
  2307. // Get the options
  2308. //
  2309. *dwOptions = GetPrivateProfileInt (TEXT("General"), TEXT("Options"),
  2310. 0, szPath);
  2311. hr = S_OK;
  2312. }
  2313. return hr;
  2314. }
  2315. //*************************************************************
  2316. //
  2317. // SetOptions()
  2318. //
  2319. // Purpose: Sets the GPO options
  2320. //
  2321. // Parameters: dwOptions is the new options
  2322. // dwMask states which options should be set
  2323. //
  2324. // Return: S_OK if successful
  2325. //
  2326. //*************************************************************
  2327. STDMETHODIMP CGroupPolicyObject::SetOptions (DWORD dwOptions, DWORD dwMask)
  2328. {
  2329. HRESULT hr;
  2330. DWORD dwResult = 0, dwOriginal;
  2331. //
  2332. // Check for initialization
  2333. //
  2334. if (!m_bInitialized)
  2335. {
  2336. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetOptions: Called on an uninitialized object.")));
  2337. return OLE_E_BLANK;
  2338. }
  2339. if (m_dwFlags & GPO_OPEN_READ_ONLY)
  2340. {
  2341. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetOptions: Called for a READ ONLY GPO")));
  2342. return HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED);
  2343. }
  2344. //
  2345. // Query for the current options
  2346. //
  2347. hr = GetOptions (&dwResult);
  2348. if (FAILED(hr))
  2349. {
  2350. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetOptions: Failed to get previous options with 0x%x"), hr));
  2351. return hr;
  2352. }
  2353. //
  2354. // Save the original options so we can compare later
  2355. //
  2356. dwOriginal = dwResult;
  2357. //
  2358. // Check for the machine disabled option
  2359. //
  2360. if (dwMask & GPO_OPTION_DISABLE_MACHINE)
  2361. {
  2362. if (dwOptions & GPO_OPTION_DISABLE_MACHINE)
  2363. {
  2364. dwResult |= GPO_OPTION_DISABLE_MACHINE;
  2365. }
  2366. else
  2367. {
  2368. dwResult &= ~GPO_OPTION_DISABLE_MACHINE;
  2369. }
  2370. }
  2371. //
  2372. // Check for the user disabled option
  2373. //
  2374. if (dwMask & GPO_OPTION_DISABLE_USER)
  2375. {
  2376. if (dwOptions & GPO_OPTION_DISABLE_USER)
  2377. {
  2378. dwResult |= GPO_OPTION_DISABLE_USER;
  2379. }
  2380. else
  2381. {
  2382. dwResult &= ~GPO_OPTION_DISABLE_USER;
  2383. }
  2384. }
  2385. //
  2386. // If something changed, set the options back in the GPO
  2387. //
  2388. if (dwResult != dwOriginal)
  2389. {
  2390. //
  2391. // Set the options in the DS or gpt.ini as appropriate
  2392. //
  2393. if (m_gpoType == GPOTypeDS)
  2394. {
  2395. VARIANT var;
  2396. BSTR bstrName;
  2397. bstrName = SysAllocString (GPO_OPTIONS_PROPERTY);
  2398. if (bstrName)
  2399. {
  2400. VariantInit(&var);
  2401. var.vt = VT_I4;
  2402. var.lVal = dwResult;
  2403. hr = m_pADs->Put(bstrName, var);
  2404. VariantClear (&var);
  2405. SysFreeString (bstrName);
  2406. }
  2407. else
  2408. {
  2409. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetOptions: Failed to allocate memory")));
  2410. hr = ERROR_OUTOFMEMORY;
  2411. }
  2412. if (SUCCEEDED(hr))
  2413. {
  2414. hr = m_pADs->SetInfo();
  2415. }
  2416. if (FAILED(hr))
  2417. {
  2418. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetOptions: Failed to set options with 0x%x"), hr));
  2419. }
  2420. }
  2421. else
  2422. {
  2423. TCHAR szPath[2*MAX_PATH];
  2424. TCHAR szOptions[20];
  2425. LPTSTR lpEnd;
  2426. //
  2427. // Get the file system path
  2428. //
  2429. hr = GetPath (szPath, ARRAYSIZE(szPath));
  2430. if (FAILED(hr))
  2431. {
  2432. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetOptions: Failed to get path with 0x%x"), hr));
  2433. return hr;
  2434. }
  2435. //
  2436. // Tack on gpt.ini
  2437. //
  2438. lpEnd = CheckSlash (szPath);
  2439. lstrcpy (lpEnd, TEXT("GPT.INI"));
  2440. //
  2441. // Convert the options to string format
  2442. //
  2443. _itot (dwResult, szOptions, 10);
  2444. //
  2445. // Set the options
  2446. //
  2447. if (!WritePrivateProfileString (TEXT("General"), TEXT("Options"),
  2448. szOptions, szPath))
  2449. {
  2450. hr = HRESULT_FROM_WIN32(GetLastError());
  2451. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetOptions: Failed to set options with 0x%x"), hr));
  2452. return hr;
  2453. }
  2454. //
  2455. // If this is the local GPO, trigger a policy refresh if appropriate
  2456. //
  2457. if (m_gpoType == GPOTypeLocal)
  2458. {
  2459. RefreshGroupPolicy (TRUE);
  2460. RefreshGroupPolicy (FALSE);
  2461. }
  2462. hr = S_OK;
  2463. }
  2464. }
  2465. else
  2466. {
  2467. hr = S_OK;
  2468. }
  2469. return hr;
  2470. }
  2471. //*************************************************************
  2472. //
  2473. // GetType()
  2474. //
  2475. // Purpose: Gets the GPO type
  2476. //
  2477. // Parameters: gpoType receives the type
  2478. //
  2479. //
  2480. // Return: S_OK if successful
  2481. //
  2482. //*************************************************************
  2483. STDMETHODIMP CGroupPolicyObject::GetType (GROUP_POLICY_OBJECT_TYPE *gpoType)
  2484. {
  2485. //
  2486. // Check for initialization
  2487. //
  2488. if (!m_bInitialized)
  2489. {
  2490. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetType: Called on an uninitialized object.")));
  2491. return OLE_E_BLANK;
  2492. }
  2493. //
  2494. // Check argument
  2495. //
  2496. if (!gpoType)
  2497. {
  2498. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetType: Received a NULL ptr.")));
  2499. return E_INVALIDARG;
  2500. }
  2501. //
  2502. // Store type
  2503. //
  2504. *gpoType = m_gpoType;
  2505. return S_OK;
  2506. }
  2507. //*************************************************************
  2508. //
  2509. // GetMachineName()
  2510. //
  2511. // Purpose: Gets the machine name of the remote GPO
  2512. //
  2513. // Parameters: pszName is a pointer to a buffer which receives the name
  2514. // cchMaxLength is the max size of the buffer
  2515. //
  2516. // Note: This method returns the name passed to OpenRemoteMachineGPO
  2517. //
  2518. // Return: S_OK if successful
  2519. //
  2520. //*************************************************************
  2521. STDMETHODIMP CGroupPolicyObject::GetMachineName (LPOLESTR pszName, int cchMaxLength)
  2522. {
  2523. HRESULT hr = S_OK;
  2524. //
  2525. // Check parameters
  2526. //
  2527. if (!pszName || (cchMaxLength <= 0))
  2528. return E_INVALIDARG;
  2529. if (!m_bInitialized)
  2530. {
  2531. return OLE_E_BLANK;
  2532. }
  2533. if (m_pMachineName)
  2534. {
  2535. //
  2536. // Save the name
  2537. //
  2538. if ((lstrlen (m_pMachineName) + 1) <= cchMaxLength)
  2539. {
  2540. lstrcpy (pszName, m_pMachineName);
  2541. }
  2542. else
  2543. {
  2544. hr = E_OUTOFMEMORY;
  2545. }
  2546. }
  2547. else
  2548. {
  2549. *pszName = TEXT('\0');
  2550. }
  2551. return hr;
  2552. }
  2553. BOOL
  2554. EnableWMIFilters( LPWSTR );
  2555. //*************************************************************
  2556. //
  2557. // GetPropertySheetPages()
  2558. //
  2559. // Purpose: Returns an array of property sheet pages for
  2560. // the callee to use. The callee needs to free
  2561. // the buffer with LocalFree when finished.
  2562. //
  2563. // Parameters: hPages receives a pointer to an array of page handles
  2564. // uPageCount receives the number of pages in hPages
  2565. //
  2566. // Return: S_OK if successful
  2567. //
  2568. //*************************************************************
  2569. STDMETHODIMP CGroupPolicyObject::GetPropertySheetPages (HPROPSHEETPAGE **hPages,
  2570. UINT *uPageCount)
  2571. {
  2572. HPROPSHEETPAGE hTempPages[4];
  2573. HPROPSHEETPAGE *lpPages;
  2574. PROPSHEETPAGE psp;
  2575. UINT i, uTempPageCount = 0;
  2576. HRESULT hr;
  2577. //
  2578. // Create the General property sheet
  2579. //
  2580. psp.dwSize = sizeof(PROPSHEETPAGE);
  2581. psp.dwFlags = 0;
  2582. psp.hInstance = g_hInstance;
  2583. psp.pszTemplate = MAKEINTRESOURCE(IDD_PROPERTIES);
  2584. psp.pfnDlgProc = PropertiesDlgProc;
  2585. psp.lParam = (LPARAM) this;
  2586. hTempPages[uTempPageCount] = CreatePropertySheetPage(&psp);
  2587. if (!hTempPages[uTempPageCount])
  2588. {
  2589. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetPropertySheetPages: Failed to create property sheet page with %d."),
  2590. GetLastError()));
  2591. return E_FAIL;
  2592. }
  2593. uTempPageCount++;
  2594. //
  2595. // If this is a DS GPO, then create the links, DS security, and WMI filter pages
  2596. //
  2597. if (m_gpoType == GPOTypeDS)
  2598. {
  2599. // Create the search for links page
  2600. psp.dwSize = sizeof(PROPSHEETPAGE);
  2601. psp.dwFlags = 0;
  2602. psp.hInstance = g_hInstance;
  2603. psp.pszTemplate = MAKEINTRESOURCE(IDD_GPE_LINKS);
  2604. psp.pfnDlgProc = GPELinksDlgProc;
  2605. psp.lParam = (LPARAM) this;
  2606. hTempPages[uTempPageCount] = CreatePropertySheetPage(&psp);
  2607. if (!hTempPages[uTempPageCount])
  2608. {
  2609. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetPropertySheetPages: Failed to create property sheet page with %d."),
  2610. GetLastError()));
  2611. // destroy the previous prop page
  2612. DestroyPropertySheetPage(hTempPages[uTempPageCount - 1]);
  2613. return E_FAIL;
  2614. }
  2615. uTempPageCount++;
  2616. //
  2617. // Load DSSec.dll
  2618. //
  2619. if (!m_hinstDSSec)
  2620. {
  2621. m_hinstDSSec = LoadLibrary (TEXT("dssec.dll"));
  2622. }
  2623. if (m_hinstDSSec)
  2624. {
  2625. if (!m_pfnDSCreateSecurityPage)
  2626. {
  2627. m_pfnDSCreateSecurityPage = (PFNDSCREATESECPAGE) GetProcAddress (
  2628. m_hinstDSSec, "DSCreateSecurityPage");
  2629. }
  2630. if (m_pfnDSCreateSecurityPage)
  2631. {
  2632. //
  2633. // Call DSCreateSecurityPage
  2634. //
  2635. hr = m_pfnDSCreateSecurityPage (m_pDSPath, L"groupPolicyContainer",
  2636. DSSI_IS_ROOT | ((m_dwFlags & GPO_OPEN_READ_ONLY) ? DSSI_READ_ONLY : 0),
  2637. &hTempPages[uTempPageCount],
  2638. ReadSecurityDescriptor,
  2639. WriteSecurityDescriptor, (LPARAM)this);
  2640. if (SUCCEEDED(hr))
  2641. {
  2642. uTempPageCount++;
  2643. }
  2644. else
  2645. {
  2646. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetPropertySheetPages: Failed to create DS security page with 0x%x."), hr));
  2647. }
  2648. }
  2649. else
  2650. {
  2651. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetPropertySheetPages: Failed to get function entry point with %d."), GetLastError()));
  2652. }
  2653. }
  2654. else
  2655. {
  2656. DebugMsg((DM_WARNING, TEXT("CComponentData::CreatePropertyPages: Failed to load dssec.dll with %d."), GetLastError()));
  2657. }
  2658. if ( EnableWMIFilters( m_pDSPath ) )
  2659. {
  2660. // Create the WQL filter page
  2661. psp.dwSize = sizeof(PROPSHEETPAGE);
  2662. psp.dwFlags = 0;
  2663. psp.hInstance = g_hInstance;
  2664. psp.pszTemplate = MAKEINTRESOURCE(IDD_WQLFILTER);
  2665. psp.pfnDlgProc = WQLFilterDlgProc;
  2666. psp.lParam = (LPARAM) this;
  2667. hTempPages[uTempPageCount] = CreatePropertySheetPage(&psp);
  2668. if (!hTempPages[uTempPageCount])
  2669. {
  2670. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetPropertySheetPages: Failed to create property sheet page with %d."),
  2671. GetLastError()));
  2672. for (i=0; i < uTempPageCount; i++)
  2673. {
  2674. DestroyPropertySheetPage(hTempPages[i]);
  2675. }
  2676. return E_FAIL;
  2677. }
  2678. uTempPageCount++;
  2679. }
  2680. }
  2681. //
  2682. // Save the results
  2683. //
  2684. lpPages = (HPROPSHEETPAGE *)LocalAlloc (LPTR, sizeof(HPROPSHEETPAGE) * uTempPageCount);
  2685. if (!lpPages)
  2686. {
  2687. for (i=0; i < uTempPageCount; i++)
  2688. {
  2689. DestroyPropertySheetPage(hTempPages[i]);
  2690. }
  2691. return E_OUTOFMEMORY;
  2692. }
  2693. for (i=0; i < uTempPageCount; i++)
  2694. {
  2695. lpPages[i] = hTempPages[i];
  2696. }
  2697. *hPages = lpPages;
  2698. *uPageCount = uTempPageCount;
  2699. return S_OK;
  2700. }
  2701. ///////////////////////////////////////////////////////////////////////////////
  2702. // //
  2703. // Internal methods //
  2704. // //
  2705. ///////////////////////////////////////////////////////////////////////////////
  2706. STDMETHODIMP CGroupPolicyObject::CreateContainer (LPOLESTR lpParent,
  2707. LPOLESTR lpCommonName,
  2708. BOOL bGPC)
  2709. {
  2710. HRESULT hr = E_FAIL;
  2711. VARIANT var;
  2712. IADs * pADs = NULL;
  2713. IADsContainer * pADsContainer = NULL;
  2714. IDispatch * pDispatch = NULL;
  2715. BSTR bstrProvider = NULL;
  2716. BSTR bstrName = NULL;
  2717. TCHAR szTemp[MAX_PATH];
  2718. // test to see if the container already exists
  2719. {
  2720. szTemp[0] = 0;
  2721. // scan lpParent to find the first instance of "CN="
  2722. LPTSTR lpSub = StrStr(lpParent, TEXT("CN="));
  2723. // insert CN=lpCommonName at that point
  2724. if (lpSub)
  2725. {
  2726. lstrcpyn(szTemp, lpParent, ((int)(lpSub - lpParent)) + 1);
  2727. lstrcat(szTemp, TEXT("CN="));
  2728. lstrcat(szTemp, lpCommonName);
  2729. lstrcat(szTemp, TEXT(","));
  2730. lstrcat(szTemp, lpSub);
  2731. }
  2732. hr = OpenDSObject(szTemp, IID_IADsContainer, (void **)&pADsContainer);
  2733. if (SUCCEEDED(hr))
  2734. {
  2735. hr = ERROR_OBJECT_ALREADY_EXISTS;
  2736. goto Exit;
  2737. }
  2738. }
  2739. //
  2740. // Bind to the parent object so we can create the container
  2741. //
  2742. hr = OpenDSObject(lpParent, IID_IADsContainer, (void **)&pADsContainer);
  2743. if (FAILED(hr)) {
  2744. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::CreateContainer: Failed to get gpo container interface with 0x%x"), hr));
  2745. goto Exit;
  2746. }
  2747. //
  2748. // Create the container (either GPC or normal container)
  2749. //
  2750. lstrcpy (szTemp, TEXT("CN="));
  2751. lstrcat (szTemp, lpCommonName);
  2752. hr = pADsContainer->Create ((bGPC ? TEXT("groupPolicyContainer") : TEXT("container")),
  2753. szTemp, &pDispatch);
  2754. if (FAILED(hr)) {
  2755. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::CreateContainer: Failed to create container with 0x%x"), hr));
  2756. goto Exit;
  2757. }
  2758. //
  2759. // Query for the IADs interface so we can set the CN name and
  2760. // commit the changes
  2761. //
  2762. hr = pDispatch->QueryInterface(IID_IADs, (LPVOID *)&pADs);
  2763. if (FAILED(hr)) {
  2764. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::CreateContainer: QI for IADs failed with 0x%x"), hr));
  2765. goto Exit;
  2766. }
  2767. //
  2768. // Set the common name (aka "cn")
  2769. //
  2770. bstrName = SysAllocString (L"cn");
  2771. if (bstrName)
  2772. {
  2773. VariantInit(&var);
  2774. var.vt = VT_BSTR;
  2775. var.bstrVal = SysAllocString (lpCommonName);
  2776. hr = pADs->Put(bstrName, var);
  2777. VariantClear (&var);
  2778. SysFreeString (bstrName);
  2779. }
  2780. else
  2781. {
  2782. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::CreateContainer: Failed to allocate memory")));
  2783. hr = ERROR_OUTOFMEMORY;
  2784. }
  2785. if (FAILED(hr))
  2786. {
  2787. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::CreateContainer: Failed to put common name with 0x%x"), hr));
  2788. goto Exit;
  2789. }
  2790. //
  2791. // Call SetInfo to commit the changes
  2792. //
  2793. hr = pADs->SetInfo();
  2794. Exit:
  2795. if (pDispatch)
  2796. {
  2797. pDispatch->Release();
  2798. }
  2799. if (pADs)
  2800. {
  2801. pADs->Release();
  2802. }
  2803. if (pADsContainer)
  2804. {
  2805. pADsContainer->Release();
  2806. }
  2807. return hr;
  2808. }
  2809. STDMETHODIMP CGroupPolicyObject::SetDisplayNameI (IADs * pADs, LPOLESTR lpDisplayName,
  2810. LPOLESTR lpGPTPath, BOOL bUpdateDisplayVar)
  2811. {
  2812. HRESULT hr = E_FAIL;
  2813. BSTR bstrName;
  2814. VARIANT var;
  2815. LPOLESTR lpNewName;
  2816. LPTSTR lpPath, lpEnd;
  2817. DWORD dwSize;
  2818. //
  2819. // Make a copy of the display name and limit it to MAX_FRIENDLYNAME characters
  2820. //
  2821. dwSize = lstrlen(lpDisplayName);
  2822. if (dwSize > (MAX_FRIENDLYNAME - 1))
  2823. {
  2824. dwSize = (MAX_FRIENDLYNAME - 1);
  2825. }
  2826. lpNewName = (LPOLESTR) LocalAlloc (LPTR, (dwSize + 2) * sizeof(OLECHAR));
  2827. if (!lpNewName)
  2828. {
  2829. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetDisplayNameI: Failed to allocate memory for display name")));
  2830. hr = HRESULT_FROM_WIN32(GetLastError());
  2831. goto Exit;
  2832. }
  2833. lstrcpyn (lpNewName, lpDisplayName, (dwSize + 1));
  2834. //
  2835. // Set the display name
  2836. //
  2837. bstrName = SysAllocString (GPO_NAME_PROPERTY);
  2838. if (!bstrName)
  2839. {
  2840. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetDisplayNameI: Failed to allocate memory")));
  2841. LocalFree (lpNewName);
  2842. return ERROR_OUTOFMEMORY;
  2843. }
  2844. VariantInit(&var);
  2845. var.vt = VT_BSTR;
  2846. var.bstrVal = SysAllocString (lpNewName);
  2847. if (var.bstrVal)
  2848. {
  2849. hr = pADs->Put(bstrName, var);
  2850. }
  2851. else
  2852. {
  2853. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetDisplayNameI: Failed to allocate memory")));
  2854. hr = ERROR_OUTOFMEMORY;
  2855. }
  2856. SysFreeString (bstrName);
  2857. VariantClear (&var);
  2858. if (FAILED(hr))
  2859. {
  2860. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetDisplayNameI: Failed to put display name with 0x%x"), hr));
  2861. LocalFree (lpNewName);
  2862. goto Exit;
  2863. }
  2864. //
  2865. // Commit the changes
  2866. //
  2867. hr = pADs->SetInfo();
  2868. if (FAILED(hr))
  2869. {
  2870. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetDisplayNameI: Failed to commit changes with 0x%x"), hr));
  2871. LocalFree (lpNewName);
  2872. goto Exit;
  2873. }
  2874. //
  2875. // Put the display name in the gpt.ini file also
  2876. //
  2877. lpPath = (LPTSTR) LocalAlloc (LPTR, (lstrlen(lpGPTPath) + 10) * sizeof(TCHAR));
  2878. if (lpPath)
  2879. {
  2880. lstrcpy(lpPath, lpGPTPath);
  2881. lpEnd = CheckSlash(lpPath);
  2882. lstrcpy(lpEnd, TEXT("gpt.ini"));
  2883. if (!WritePrivateProfileString (TEXT("General"), GPO_NAME_PROPERTY,
  2884. lpNewName, lpPath))
  2885. {
  2886. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetDisplayNameI: Failed to write display name to gpt.ini with 0x%x"), hr));
  2887. }
  2888. LocalFree (lpPath);
  2889. }
  2890. //
  2891. // Update the member variable if appropriate
  2892. //
  2893. if (bUpdateDisplayVar)
  2894. {
  2895. //
  2896. // Update the display name variable
  2897. //
  2898. if (m_pDisplayName)
  2899. {
  2900. LocalFree (m_pDisplayName);
  2901. m_pDisplayName = NULL;
  2902. }
  2903. m_pDisplayName = lpNewName;
  2904. }
  2905. else
  2906. {
  2907. LocalFree (lpNewName);
  2908. }
  2909. Exit:
  2910. return hr;
  2911. }
  2912. STDMETHODIMP CGroupPolicyObject::SetGPOInfo (LPOLESTR lpGPO,
  2913. LPOLESTR lpDisplayName,
  2914. LPOLESTR lpGPTPath)
  2915. {
  2916. HRESULT hr = E_FAIL;
  2917. IADs * pADs = NULL;
  2918. BSTR bstrName;
  2919. VARIANT var;
  2920. TCHAR szDefaultName[MAX_FRIENDLYNAME];
  2921. //
  2922. // Bind to the GPO container
  2923. //
  2924. hr = OpenDSObject(lpGPO, IID_IADs, (void **)&pADs);
  2925. if (FAILED(hr)) {
  2926. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetGPOInfo: Failed to get gpo interface with 0x%x"), hr));
  2927. goto Exit;
  2928. }
  2929. //
  2930. // Set the display name
  2931. //
  2932. GetNewGPODisplayName (szDefaultName, ARRAYSIZE(szDefaultName));
  2933. hr = SetDisplayNameI (pADs, (lpDisplayName ? lpDisplayName : szDefaultName),
  2934. lpGPTPath, FALSE);
  2935. if (FAILED(hr))
  2936. {
  2937. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetGPOInfo: Failed to set display name")));
  2938. goto Exit;
  2939. }
  2940. //
  2941. // Set the GPT location
  2942. //
  2943. bstrName = SysAllocString (GPT_PATH_PROPERTY);
  2944. if (bstrName)
  2945. {
  2946. VariantInit(&var);
  2947. var.vt = VT_BSTR;
  2948. var.bstrVal = SysAllocString (lpGPTPath);
  2949. if (var.bstrVal)
  2950. {
  2951. hr = pADs->Put(bstrName, var);
  2952. }
  2953. else
  2954. {
  2955. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetGPOInfo: Failed to allocate memory")));
  2956. hr = ERROR_OUTOFMEMORY;
  2957. }
  2958. VariantClear (&var);
  2959. SysFreeString (bstrName);
  2960. }
  2961. else
  2962. {
  2963. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetGPOInfo: Failed to allocate memory")));
  2964. hr = ERROR_OUTOFMEMORY;
  2965. }
  2966. if (FAILED(hr))
  2967. {
  2968. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetGPOInfo: Failed to save GPT path with 0x%x"), hr));
  2969. goto Exit;
  2970. }
  2971. //
  2972. // Set the version number
  2973. //
  2974. bstrName = SysAllocString (GPO_VERSION_PROPERTY);
  2975. if (bstrName)
  2976. {
  2977. VariantInit(&var);
  2978. var.vt = VT_I4;
  2979. var.lVal = 0;
  2980. hr = pADs->Put(bstrName, var);
  2981. VariantClear (&var);
  2982. SysFreeString (bstrName);
  2983. }
  2984. else
  2985. {
  2986. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetGPOInfo: Failed to allocate memory")));
  2987. hr = ERROR_OUTOFMEMORY;
  2988. }
  2989. if (FAILED(hr))
  2990. {
  2991. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetGPOInfo: Failed to set version number with 0x%x"), hr));
  2992. goto Exit;
  2993. }
  2994. //
  2995. // Set the functionality version number
  2996. //
  2997. bstrName = SysAllocString (GPO_FUNCTION_PROPERTY);
  2998. if (bstrName)
  2999. {
  3000. VariantInit(&var);
  3001. var.vt = VT_I4;
  3002. var.lVal = GPO_FUNCTIONALITY_VERSION;
  3003. hr = pADs->Put(bstrName, var);
  3004. VariantClear (&var);
  3005. SysFreeString (bstrName);
  3006. }
  3007. else
  3008. {
  3009. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetGPOInfo: Failed to allocate memory")));
  3010. hr = ERROR_OUTOFMEMORY;
  3011. }
  3012. if (FAILED(hr))
  3013. {
  3014. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetGPOInfo: Failed to set functionality version number with 0x%x"), hr));
  3015. goto Exit;
  3016. }
  3017. //
  3018. // Set the options
  3019. //
  3020. bstrName = SysAllocString (GPO_OPTIONS_PROPERTY);
  3021. if (bstrName)
  3022. {
  3023. VariantInit(&var);
  3024. var.vt = VT_I4;
  3025. var.lVal = 0;
  3026. hr = pADs->Put(bstrName, var);
  3027. VariantClear (&var);
  3028. SysFreeString (bstrName);
  3029. }
  3030. else
  3031. {
  3032. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetGPOInfo: Failed to allocate memory")));
  3033. hr = ERROR_OUTOFMEMORY;
  3034. }
  3035. if (FAILED(hr))
  3036. {
  3037. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetGPOInfo: Failed to set options with 0x%x"), hr));
  3038. goto Exit;
  3039. }
  3040. //
  3041. // Commit the changes
  3042. //
  3043. hr = pADs->SetInfo();
  3044. Exit:
  3045. if (pADs)
  3046. {
  3047. pADs->Release();
  3048. }
  3049. return hr;
  3050. }
  3051. STDMETHODIMP CGroupPolicyObject::CheckFSWriteAccess (LPOLESTR lpLocalGPO)
  3052. {
  3053. TCHAR szBuffer[MAX_PATH];
  3054. LPTSTR lpEnd;
  3055. HRESULT hr;
  3056. lstrcpy (szBuffer, lpLocalGPO);
  3057. lpEnd = CheckSlash (szBuffer);
  3058. lstrcpy (lpEnd, TEXT("gpt.ini"));
  3059. if (!WritePrivateProfileString (TEXT("General"), TEXT("AccessCheck"),
  3060. TEXT("test"), szBuffer))
  3061. {
  3062. hr = HRESULT_FROM_WIN32(GetLastError());
  3063. }
  3064. else
  3065. {
  3066. WritePrivateProfileString (TEXT("General"), TEXT("AccessCheck"),
  3067. NULL, szBuffer);
  3068. hr = S_OK;
  3069. }
  3070. return hr;
  3071. }
  3072. STDMETHODIMP CGroupPolicyObject::GetSecurityDescriptor (IADs *pADs,
  3073. SECURITY_INFORMATION si,
  3074. PSECURITY_DESCRIPTOR *pSD)
  3075. {
  3076. HRESULT hr;
  3077. VARIANT var;
  3078. LPWSTR pszSDProperty = L"nTSecurityDescriptor";
  3079. IDirectoryObject *pDsObject = NULL;
  3080. IADsObjectOptions *pOptions = NULL;
  3081. PADS_ATTR_INFO pSDAttributeInfo = NULL;
  3082. DWORD dwAttributesReturned;
  3083. //
  3084. // Retreive the DS Object interface
  3085. //
  3086. hr = pADs->QueryInterface(IID_IDirectoryObject, (void**)&pDsObject);
  3087. if (FAILED(hr)) {
  3088. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetSecurityDescriptor: Failed to get gpo DS object interface with 0x%x"), hr));
  3089. goto Exit;
  3090. }
  3091. //
  3092. // Retreive the DS Object Options interface
  3093. //
  3094. hr = pADs->QueryInterface(IID_IADsObjectOptions, (void**)&pOptions);
  3095. if (FAILED(hr)) {
  3096. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetSecurityDescriptor: Failed to get DS object options interface with 0x%x"), hr));
  3097. goto Exit;
  3098. }
  3099. //
  3100. // Set the SECURITY_INFORMATION mask
  3101. //
  3102. VariantInit(&var);
  3103. var.vt = VT_I4;
  3104. var.lVal = si;
  3105. hr = pOptions->SetOption(ADS_OPTION_SECURITY_MASK, var);
  3106. VariantClear (&var);
  3107. if (FAILED(hr)) {
  3108. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetSecurityDescriptor: Failed to set ADSI security options with 0x%x"), hr));
  3109. goto Exit;
  3110. }
  3111. //
  3112. // Read the security descriptor
  3113. //
  3114. hr = pDsObject->GetObjectAttributes(&pszSDProperty, 1, &pSDAttributeInfo,
  3115. &dwAttributesReturned);
  3116. if (SUCCEEDED(hr) && !pSDAttributeInfo)
  3117. {
  3118. hr = E_ACCESSDENIED; // This happens for SACL if no SecurityPrivilege
  3119. }
  3120. if (FAILED(hr)) {
  3121. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetSecurityDescriptor: Failed to get DS object attributes with 0x%x"), hr));
  3122. goto Exit;
  3123. }
  3124. //
  3125. // Duplicate the security descriptor
  3126. //
  3127. *pSD = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR, pSDAttributeInfo->pADsValues->SecurityDescriptor.dwLength);
  3128. if (!*pSD)
  3129. {
  3130. hr = HRESULT_FROM_WIN32(GetLastError());
  3131. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetSecurityDescriptor: Failed to allocate memory with 0x%x"), hr));
  3132. goto Exit;
  3133. }
  3134. CopyMemory(*pSD, pSDAttributeInfo->pADsValues->SecurityDescriptor.lpValue,
  3135. pSDAttributeInfo->pADsValues->SecurityDescriptor.dwLength);
  3136. Exit:
  3137. if (pSDAttributeInfo)
  3138. {
  3139. FreeADsMem(pSDAttributeInfo);
  3140. }
  3141. if (pOptions)
  3142. {
  3143. pOptions->Release();
  3144. }
  3145. if (pDsObject)
  3146. {
  3147. pDsObject->Release();
  3148. }
  3149. return hr;
  3150. }
  3151. BOOL CGroupPolicyObject::EnableSecurityPrivs(void)
  3152. {
  3153. BOOL bResult;
  3154. HANDLE hToken;
  3155. HANDLE hNewToken;
  3156. BYTE buffer[sizeof(PRIVILEGE_SET) + sizeof(LUID_AND_ATTRIBUTES)];
  3157. PTOKEN_PRIVILEGES pPrivileges = (PTOKEN_PRIVILEGES)buffer;
  3158. //
  3159. // Get a token and enable the Security and Take Ownership
  3160. // privileges, if possible.
  3161. //
  3162. bResult = OpenThreadToken(GetCurrentThread(), TOKEN_DUPLICATE, TRUE, &hToken);
  3163. if (!bResult)
  3164. {
  3165. bResult = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &hToken);
  3166. }
  3167. if (!bResult)
  3168. {
  3169. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::EnableSecurityPrivs: Failed to get both thread and process token with %d"),
  3170. GetLastError()));
  3171. return FALSE;
  3172. }
  3173. bResult = DuplicateTokenEx(hToken,
  3174. TOKEN_IMPERSONATE | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  3175. NULL, // PSECURITY_ATTRIBUTES
  3176. SecurityImpersonation, // SECURITY_IMPERSONATION_LEVEL
  3177. TokenImpersonation, // TokenType
  3178. &hNewToken); // Duplicate token
  3179. if (!bResult)
  3180. {
  3181. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::EnableSecurityPrivs: Failed to duplicate the token with %d"),
  3182. GetLastError()));
  3183. CloseHandle(hToken);
  3184. return FALSE;
  3185. }
  3186. //
  3187. // PRIVILEGE_SET contains 1 LUID_AND_ATTRIBUTES already, so
  3188. // this is enough for 2 LUID_AND_ATTRIBUTES (2 privileges).
  3189. //
  3190. CloseHandle(hToken);
  3191. hToken = hNewToken;
  3192. pPrivileges->PrivilegeCount = 2;
  3193. pPrivileges->Privileges[0].Luid = RtlConvertUlongToLuid(SE_SECURITY_PRIVILEGE);
  3194. pPrivileges->Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  3195. pPrivileges->Privileges[1].Luid = RtlConvertUlongToLuid(SE_TAKE_OWNERSHIP_PRIVILEGE);
  3196. pPrivileges->Privileges[1].Attributes = SE_PRIVILEGE_ENABLED;
  3197. bResult = AdjustTokenPrivileges(hToken, // TokenHandle
  3198. FALSE, // DisableAllPrivileges
  3199. pPrivileges,// NewState
  3200. 0, // BufferLength
  3201. NULL, // PreviousState
  3202. NULL); // ReturnLength
  3203. if (!bResult)
  3204. {
  3205. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::EnableSecurityPrivs: Failed to AdjustTokenPrivileges with %d"),
  3206. GetLastError()));
  3207. CloseHandle(hToken);
  3208. return FALSE;
  3209. }
  3210. //
  3211. // Set the new thread token
  3212. //
  3213. SetThreadToken(NULL, hToken);
  3214. CloseHandle(hToken);
  3215. return TRUE;
  3216. }
  3217. DWORD CGroupPolicyObject::EnableInheritance (PACL pAcl)
  3218. {
  3219. WORD wIndex;
  3220. DWORD dwResult = ERROR_SUCCESS;
  3221. ACE_HEADER *pAceHeader;
  3222. if (pAcl)
  3223. {
  3224. //
  3225. // Loop through the ACL looking at each ACE entry
  3226. //
  3227. for (wIndex = 0; wIndex < pAcl->AceCount; wIndex++)
  3228. {
  3229. if (!GetAce (pAcl, (DWORD)wIndex, (LPVOID *)&pAceHeader))
  3230. {
  3231. dwResult = GetLastError();
  3232. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::EnableInheritance: GetAce failed with %d"),
  3233. dwResult));
  3234. goto Exit;
  3235. }
  3236. //
  3237. // Turn on the inheritance flags
  3238. //
  3239. pAceHeader->AceFlags |= (CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE);
  3240. }
  3241. }
  3242. Exit:
  3243. return dwResult;
  3244. }
  3245. //
  3246. // This method will convert a DS ACL into a file system ACL
  3247. //
  3248. DWORD CGroupPolicyObject::MapSecurityRights (PACL pAcl)
  3249. {
  3250. WORD wIndex;
  3251. DWORD dwResult = ERROR_SUCCESS;
  3252. ACE_HEADER *pAceHeader;
  3253. PACCESS_ALLOWED_ACE pAce;
  3254. PACCESS_ALLOWED_OBJECT_ACE pObjectAce;
  3255. ACCESS_MASK AccessMask;
  3256. #if DBG
  3257. PSID pSid;
  3258. TCHAR szName[150], szDomain[100];
  3259. DWORD dwName, dwDomain;
  3260. SID_NAME_USE SidUse;
  3261. #endif
  3262. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::MapSecurityRights: ACL contains %d ACEs"), pAcl->AceCount));
  3263. //
  3264. // Loop through the ACL looking at each ACE entry
  3265. //
  3266. for (wIndex = 0; wIndex < pAcl->AceCount; wIndex++)
  3267. {
  3268. if (!GetAce (pAcl, (DWORD)wIndex, (LPVOID *)&pAceHeader))
  3269. {
  3270. dwResult = GetLastError();
  3271. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::MapSecurityRights: GetAce failed with %d"),
  3272. dwResult));
  3273. goto Exit;
  3274. }
  3275. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::MapSecurityRights: ==================")));
  3276. switch (pAceHeader->AceType)
  3277. {
  3278. case ACCESS_ALLOWED_ACE_TYPE:
  3279. case ACCESS_DENIED_ACE_TYPE:
  3280. case SYSTEM_AUDIT_ACE_TYPE:
  3281. {
  3282. pAce = (PACCESS_ALLOWED_ACE) pAceHeader;
  3283. #if DBG
  3284. pSid = (PSID) &pAce->SidStart;
  3285. dwName = ARRAYSIZE(szName);
  3286. dwDomain = ARRAYSIZE(szDomain);
  3287. if (LookupAccountSid (NULL, pSid, szName, &dwName, szDomain,
  3288. &dwDomain, &SidUse))
  3289. {
  3290. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::MapSecurityRights: Normal ACE entry for: Name = %s Domain = %s"),
  3291. szName, szDomain));
  3292. }
  3293. #endif
  3294. AccessMask = pAce->Mask;
  3295. pAce->Mask &= STANDARD_RIGHTS_ALL;
  3296. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::MapSecurityRights: DS access mask is 0x%x"),
  3297. AccessMask));
  3298. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::MapSecurityRights: Corresponding sysvol permissions follow:")));
  3299. //
  3300. // Read
  3301. //
  3302. if ((AccessMask & ACTRL_DS_READ_PROP) &&
  3303. (AccessMask & ACTRL_DS_LIST))
  3304. {
  3305. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::MapSecurityRights: Granting Read permission")));
  3306. pAce->Mask |= (SYNCHRONIZE | FILE_LIST_DIRECTORY |
  3307. FILE_READ_ATTRIBUTES | FILE_READ_EA |
  3308. FILE_READ_DATA | FILE_EXECUTE);
  3309. }
  3310. //
  3311. // Write
  3312. //
  3313. if (AccessMask & ACTRL_DS_WRITE_PROP)
  3314. {
  3315. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::MapSecurityRights: Granting Write permission")));
  3316. pAce->Mask |= (SYNCHRONIZE | FILE_WRITE_DATA |
  3317. FILE_APPEND_DATA | FILE_WRITE_EA |
  3318. FILE_WRITE_ATTRIBUTES | FILE_ADD_FILE |
  3319. FILE_ADD_SUBDIRECTORY);
  3320. }
  3321. //
  3322. // Misc
  3323. //
  3324. if (AccessMask & ACTRL_DS_CREATE_CHILD)
  3325. {
  3326. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::MapSecurityRights: Granting directory creation permission")));
  3327. pAce->Mask |= (FILE_ADD_SUBDIRECTORY | FILE_ADD_FILE);
  3328. }
  3329. if (AccessMask & ACTRL_DS_DELETE_CHILD)
  3330. {
  3331. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::MapSecurityRights: Granting directory delete permission")));
  3332. pAce->Mask |= FILE_DELETE_CHILD;
  3333. }
  3334. //
  3335. // Inheritance
  3336. //
  3337. pAceHeader->AceFlags |= (OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE);
  3338. }
  3339. break;
  3340. case ACCESS_ALLOWED_OBJECT_ACE_TYPE:
  3341. case ACCESS_DENIED_OBJECT_ACE_TYPE:
  3342. case SYSTEM_AUDIT_OBJECT_ACE_TYPE:
  3343. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::MapSecurityRights: Clearing access mask in DS object ACE entry")));
  3344. pObjectAce = (PACCESS_ALLOWED_OBJECT_ACE) pAceHeader;
  3345. pObjectAce->Mask = 0;
  3346. break;
  3347. default:
  3348. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::MapSecurityRights: Unknown ACE type 0x%x"), pAceHeader->AceType));
  3349. break;
  3350. }
  3351. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::MapSecurityRights: ==================")));
  3352. }
  3353. Exit:
  3354. return dwResult;
  3355. }
  3356. //
  3357. // This method will convert a DS security access list into a
  3358. // file system security access list and actually set the security
  3359. //
  3360. DWORD CGroupPolicyObject::SetSysvolSecurity (LPTSTR lpFileSysPath, SECURITY_INFORMATION si,
  3361. PSECURITY_DESCRIPTOR pSD)
  3362. {
  3363. PACL pSacl = NULL, pDacl = NULL;
  3364. PSID psidOwner = NULL, psidGroup = NULL;
  3365. BOOL bAclPresent, bDefaulted;
  3366. DWORD dwResult;
  3367. //
  3368. // Get the DACL
  3369. //
  3370. if (si & DACL_SECURITY_INFORMATION)
  3371. {
  3372. if (!GetSecurityDescriptorDacl (pSD, &bAclPresent, &pDacl, &bDefaulted))
  3373. {
  3374. dwResult = GetLastError();
  3375. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetSysvolSecurity: GetSecurityDescriptorDacl failed with %d"),
  3376. dwResult));
  3377. goto Exit;
  3378. }
  3379. }
  3380. //
  3381. // Get the SACL
  3382. //
  3383. if (si & SACL_SECURITY_INFORMATION)
  3384. {
  3385. if (!GetSecurityDescriptorSacl (pSD, &bAclPresent, &pSacl, &bDefaulted))
  3386. {
  3387. dwResult = GetLastError();
  3388. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetSysvolSecurity: GetSecurityDescriptorSacl failed with %d"),
  3389. dwResult));
  3390. goto Exit;
  3391. }
  3392. }
  3393. //
  3394. // Get the owner
  3395. //
  3396. if (si & OWNER_SECURITY_INFORMATION)
  3397. {
  3398. if (!GetSecurityDescriptorOwner (pSD, &psidOwner, &bDefaulted))
  3399. {
  3400. dwResult = GetLastError();
  3401. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetSysvolSecurity: GetSecurityDescriptorOwner failed with %d"),
  3402. dwResult));
  3403. goto Exit;
  3404. }
  3405. }
  3406. //
  3407. // Get the group
  3408. //
  3409. if (si & GROUP_SECURITY_INFORMATION)
  3410. {
  3411. if (!GetSecurityDescriptorGroup (pSD, &psidGroup, &bDefaulted))
  3412. {
  3413. dwResult = GetLastError();
  3414. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetSysvolSecurity: GetSecurityDescriptorGroup failed with %d"),
  3415. dwResult));
  3416. goto Exit;
  3417. }
  3418. }
  3419. //
  3420. // Convert the DS access control lists into file system
  3421. // access control lists
  3422. //
  3423. if (pDacl)
  3424. {
  3425. dwResult = MapSecurityRights (pDacl);
  3426. if (dwResult != ERROR_SUCCESS)
  3427. {
  3428. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetSysvolSecurity: MapSecurityRights for the DACL failed with %d"),
  3429. dwResult));
  3430. goto Exit;
  3431. }
  3432. }
  3433. if (pSacl)
  3434. {
  3435. dwResult = MapSecurityRights (pSacl);
  3436. if (dwResult != ERROR_SUCCESS)
  3437. {
  3438. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetSysvolSecurity: MapSecurityRights for the SACL failed with %d"),
  3439. dwResult));
  3440. goto Exit;
  3441. }
  3442. }
  3443. //
  3444. // Switch to using the PROTECTED_DACL_SECURITY_INFORMATION and
  3445. // PROTECTED_SACL_SECURITY_INFORMATION flags so that this subdirectory
  3446. // does not inherit settings from it's parent (aka: "protect" it)
  3447. //
  3448. if (si & DACL_SECURITY_INFORMATION)
  3449. {
  3450. si |= PROTECTED_DACL_SECURITY_INFORMATION;
  3451. }
  3452. if (si & SACL_SECURITY_INFORMATION)
  3453. {
  3454. si |= PROTECTED_SACL_SECURITY_INFORMATION;
  3455. }
  3456. //
  3457. // Set the access control information for the file system portion
  3458. //
  3459. dwResult = SetNamedSecurityInfo(lpFileSysPath, SE_FILE_OBJECT, si, psidOwner,
  3460. psidGroup, pDacl, pSacl);
  3461. Exit:
  3462. return dwResult;
  3463. }
  3464. HRESULT WINAPI CGroupPolicyObject::ReadSecurityDescriptor (LPCWSTR lpGPOPath,
  3465. SECURITY_INFORMATION si,
  3466. PSECURITY_DESCRIPTOR *pSD,
  3467. LPARAM lpContext)
  3468. {
  3469. CGroupPolicyObject * pGPO;
  3470. HRESULT hr;
  3471. //
  3472. // Convert lpContext into a pGPO
  3473. //
  3474. pGPO = (CGroupPolicyObject*)lpContext;
  3475. if (!pGPO)
  3476. {
  3477. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::ReadSecurityDescriptor: GPO interface pointer is NULL")));
  3478. return E_FAIL;
  3479. }
  3480. hr = pGPO->GetSecurityDescriptor (pGPO->m_pADs, si, pSD);
  3481. if (FAILED(hr))
  3482. {
  3483. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::ReadSecurityDescriptor: GetSecurityDescriptor returned 0x%x"), hr));
  3484. }
  3485. return (hr);
  3486. }
  3487. HRESULT WINAPI CGroupPolicyObject::WriteSecurityDescriptor (LPCWSTR lpGPOPath,
  3488. SECURITY_INFORMATION si,
  3489. PSECURITY_DESCRIPTOR pSD,
  3490. LPARAM lpContext)
  3491. {
  3492. CGroupPolicyObject * pGPO;
  3493. IDirectoryObject *pDsObject = NULL;
  3494. IADsObjectOptions *pOptions = NULL;
  3495. DWORD dwResult = ERROR_SUCCESS;
  3496. HRESULT hr;
  3497. VARIANT var;
  3498. ADSVALUE attributeValue;
  3499. ADS_ATTR_INFO attributeInfo;
  3500. DWORD dwAttributesModified;
  3501. DWORD dwSDLength;
  3502. PSECURITY_DESCRIPTOR psd = NULL, pSDOrg = NULL;
  3503. SECURITY_DESCRIPTOR_CONTROL sdControl = 0;
  3504. DWORD dwRevision;
  3505. PACL pAcl;
  3506. BOOL bPresent, bDefault;
  3507. //
  3508. // Convert lpContext into a pGPO
  3509. //
  3510. pGPO = (CGroupPolicyObject*)lpContext;
  3511. if (!pGPO)
  3512. {
  3513. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WriteSecurityDescriptor: GPO interface pointer is NULL")));
  3514. return E_FAIL;
  3515. }
  3516. //
  3517. // Get the original security descriptor from the DS
  3518. //
  3519. hr = pGPO->GetSecurityDescriptor (pGPO->m_pADs, si, &pSDOrg);
  3520. if (FAILED(hr)) {
  3521. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WriteSecurityDescriptor: Failed to query the security descriptor with 0x%x"), hr));
  3522. goto Exit;
  3523. }
  3524. //
  3525. // Retreive the DS Object interface
  3526. //
  3527. hr = pGPO->m_pADs->QueryInterface(IID_IDirectoryObject, (void**)&pDsObject);
  3528. if (FAILED(hr)) {
  3529. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WriteSecurityDescriptor: Failed to get DS object interface with 0x%x"), hr));
  3530. goto Exit;
  3531. }
  3532. //
  3533. // Retreive the DS Object Options interface
  3534. //
  3535. hr = pGPO->m_pADs->QueryInterface(IID_IADsObjectOptions, (void**)&pOptions);
  3536. if (FAILED(hr)) {
  3537. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WriteSecurityDescriptor: Failed to get DS object options interface with 0x%x"), hr));
  3538. goto Exit;
  3539. }
  3540. //
  3541. // Set the SECURITY_INFORMATION mask
  3542. //
  3543. VariantInit(&var);
  3544. var.vt = VT_I4;
  3545. var.lVal = si;
  3546. hr = pOptions->SetOption(ADS_OPTION_SECURITY_MASK, var);
  3547. VariantClear (&var);
  3548. if (FAILED(hr)) {
  3549. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WriteSecurityDescriptor: Failed to get DS object options interface with 0x%x"), hr));
  3550. goto Exit;
  3551. }
  3552. //
  3553. // Need the total size of the security descriptor
  3554. //
  3555. dwSDLength = GetSecurityDescriptorLength(pSD);
  3556. //
  3557. // If necessary, make a self-relative copy of the security descriptor
  3558. //
  3559. if (!GetSecurityDescriptorControl(pSD, &sdControl, &dwRevision))
  3560. {
  3561. dwResult = GetLastError();
  3562. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WriteSecurityDescriptor: Failed to get security descriptor control with %d"),
  3563. dwResult));
  3564. hr = HRESULT_FROM_WIN32(dwResult);
  3565. goto Exit;
  3566. }
  3567. if (!(sdControl & SE_SELF_RELATIVE))
  3568. {
  3569. psd = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR, dwSDLength);
  3570. if (!psd)
  3571. {
  3572. dwResult = GetLastError();
  3573. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WriteSecurityDescriptor: Failed to allocate memory for new SD with %d"),
  3574. dwResult));
  3575. hr = HRESULT_FROM_WIN32(dwResult);
  3576. goto Exit;
  3577. }
  3578. if (!MakeSelfRelativeSD(pSD, psd, &dwSDLength))
  3579. {
  3580. dwResult = GetLastError();
  3581. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WriteSecurityDescriptor: MakeSelfRelativeSD failed with %d"),
  3582. dwResult));
  3583. hr = HRESULT_FROM_WIN32(dwResult);
  3584. goto Exit;
  3585. }
  3586. //
  3587. // Point to the self-relative copy
  3588. //
  3589. pSD = psd;
  3590. }
  3591. //
  3592. // By default, the general page will set things up so the inheritance
  3593. // is for the root container only. We really want the inheritance to
  3594. // be for the root and all sub-containers, so run through the
  3595. // DACL and SACL and set the new inheritance flags
  3596. //
  3597. if (si & DACL_SECURITY_INFORMATION)
  3598. {
  3599. if (!GetSecurityDescriptorDacl(pSD, &bPresent, &pAcl, &bDefault))
  3600. {
  3601. dwResult = GetLastError();
  3602. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WriteSecurityDescriptor: GetSecurityDescriptorDacl failed with %d"),
  3603. dwResult));
  3604. hr = HRESULT_FROM_WIN32(dwResult);
  3605. goto Exit;
  3606. }
  3607. dwResult = pGPO->EnableInheritance (pAcl);
  3608. if (dwResult != ERROR_SUCCESS)
  3609. {
  3610. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WriteSecurityDescriptor: EnableInheritance failed with %d"),
  3611. dwResult));
  3612. hr = HRESULT_FROM_WIN32(dwResult);
  3613. goto Exit;
  3614. }
  3615. }
  3616. if (si & SACL_SECURITY_INFORMATION)
  3617. {
  3618. if (!GetSecurityDescriptorSacl(pSD, &bPresent, &pAcl, &bDefault))
  3619. {
  3620. dwResult = GetLastError();
  3621. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WriteSecurityDescriptor: GetSecurityDescriptorSacl failed with %d"),
  3622. dwResult));
  3623. hr = HRESULT_FROM_WIN32(dwResult);
  3624. goto Exit;
  3625. }
  3626. dwResult = pGPO->EnableInheritance (pAcl);
  3627. if (dwResult != ERROR_SUCCESS)
  3628. {
  3629. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WriteSecurityDescriptor: EnableInheritance failed with %d"),
  3630. dwResult));
  3631. hr = HRESULT_FROM_WIN32(dwResult);
  3632. goto Exit;
  3633. }
  3634. }
  3635. //
  3636. // Set the DS security
  3637. //
  3638. attributeValue.dwType = ADSTYPE_NT_SECURITY_DESCRIPTOR;
  3639. attributeValue.SecurityDescriptor.dwLength = dwSDLength;
  3640. attributeValue.SecurityDescriptor.lpValue = (LPBYTE)pSD;
  3641. attributeInfo.pszAttrName = L"nTSecurityDescriptor";
  3642. attributeInfo.dwControlCode = ADS_ATTR_UPDATE;
  3643. attributeInfo.dwADsType = ADSTYPE_NT_SECURITY_DESCRIPTOR;
  3644. attributeInfo.pADsValues = &attributeValue;
  3645. attributeInfo.dwNumValues = 1;
  3646. hr = pDsObject->SetObjectAttributes(&attributeInfo, 1, &dwAttributesModified);
  3647. if (FAILED(hr)) {
  3648. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WriteSecurityDescriptor: Failed to set DS security with 0x%x"), hr));
  3649. goto Exit;
  3650. }
  3651. //
  3652. // Set the sysvol security
  3653. //
  3654. dwResult = pGPO->SetSysvolSecurity (pGPO->m_pFileSysPath, si, pSD);
  3655. if (dwResult != ERROR_SUCCESS)
  3656. {
  3657. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WriteSecurityDescriptor: Failed to set the security for the file system portion <%s> with %d"),
  3658. pGPO->m_pFileSysPath, dwResult));
  3659. hr = HRESULT_FROM_WIN32(dwResult);
  3660. //
  3661. // Restore the orignal DS security
  3662. //
  3663. attributeValue.dwType = ADSTYPE_NT_SECURITY_DESCRIPTOR;
  3664. attributeValue.SecurityDescriptor.dwLength = GetSecurityDescriptorLength(pSDOrg);
  3665. attributeValue.SecurityDescriptor.lpValue = (LPBYTE)pSDOrg;
  3666. attributeInfo.pszAttrName = L"nTSecurityDescriptor";
  3667. attributeInfo.dwControlCode = ADS_ATTR_UPDATE;
  3668. attributeInfo.dwADsType = ADSTYPE_NT_SECURITY_DESCRIPTOR;
  3669. attributeInfo.pADsValues = &attributeValue;
  3670. attributeInfo.dwNumValues = 1;
  3671. if (FAILED(pDsObject->SetObjectAttributes(&attributeInfo, 1, &dwAttributesModified)))
  3672. {
  3673. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WriteSecurityDescriptor: Failed to restore DS security")));
  3674. }
  3675. goto Exit;
  3676. }
  3677. Exit:
  3678. if (pDsObject)
  3679. {
  3680. pDsObject->Release();
  3681. }
  3682. if (pOptions)
  3683. {
  3684. pOptions->Release();
  3685. }
  3686. if (psd)
  3687. {
  3688. LocalFree(psd);
  3689. }
  3690. if (pSDOrg)
  3691. {
  3692. LocalFree(pSDOrg);
  3693. }
  3694. return (hr);
  3695. }
  3696. STDMETHODIMP CGroupPolicyObject::CleanUp (void)
  3697. {
  3698. if (m_pUser)
  3699. {
  3700. m_pUser->Release();
  3701. m_pUser = NULL;
  3702. }
  3703. if (m_pMachine)
  3704. {
  3705. m_pMachine->Release();
  3706. m_pMachine = NULL;
  3707. }
  3708. if (m_pName)
  3709. {
  3710. LocalFree (m_pName);
  3711. m_pName = NULL;
  3712. }
  3713. if (m_pDisplayName)
  3714. {
  3715. LocalFree (m_pDisplayName);
  3716. m_pDisplayName = NULL;
  3717. }
  3718. if (m_pDSPath)
  3719. {
  3720. LocalFree (m_pDSPath);
  3721. m_pDSPath = NULL;
  3722. }
  3723. if (m_pFileSysPath)
  3724. {
  3725. LocalFree (m_pFileSysPath);
  3726. m_pFileSysPath = NULL;
  3727. }
  3728. if (m_pMachineName)
  3729. {
  3730. LocalFree (m_pMachineName);
  3731. m_pMachineName = NULL;
  3732. }
  3733. if (m_pADs)
  3734. {
  3735. m_pADs->Release();
  3736. m_pADs = NULL;
  3737. }
  3738. m_gpoType = GPOTypeLocal;
  3739. m_bInitialized = FALSE;
  3740. return S_OK;
  3741. }
  3742. STDMETHODIMP CGroupPolicyObject::RefreshGroupPolicy (BOOL bMachine)
  3743. {
  3744. HINSTANCE hInstUserEnv;
  3745. PFNREFRESHPOLICY pfnRefreshPolicy;
  3746. //
  3747. // Load the function we need
  3748. //
  3749. hInstUserEnv = LoadLibrary (TEXT("userenv.dll"));
  3750. if (!hInstUserEnv) {
  3751. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::RefreshGroupPolicy: Failed to load userenv with %d."),
  3752. GetLastError()));
  3753. return (HRESULT_FROM_WIN32(GetLastError()));
  3754. }
  3755. pfnRefreshPolicy = (PFNREFRESHPOLICY)GetProcAddress (hInstUserEnv,
  3756. "RefreshPolicy");
  3757. if (!pfnRefreshPolicy) {
  3758. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::RefreshGroupPolicy: Failed to find RefreshPolicy with %d."),
  3759. GetLastError()));
  3760. FreeLibrary (hInstUserEnv);
  3761. return (HRESULT_FROM_WIN32(GetLastError()));
  3762. }
  3763. //
  3764. // Refresh policy
  3765. //
  3766. pfnRefreshPolicy (bMachine);
  3767. //
  3768. // Clean up
  3769. //
  3770. FreeLibrary (hInstUserEnv);
  3771. return S_OK;
  3772. }
  3773. BSTR
  3774. ParseDomainName( LPWSTR szDomain )
  3775. {
  3776. BSTR bstrDomain = 0;
  3777. if ( szDomain )
  3778. {
  3779. WCHAR szXDomain[MAX_PATH*2];
  3780. DWORD dwSize = MAX_PATH*2;
  3781. if ( TranslateName( szDomain,
  3782. NameUnknown,
  3783. NameCanonical,
  3784. szXDomain,
  3785. &dwSize ) )
  3786. {
  3787. LPWSTR szTemp = wcschr( szXDomain, L'/' );
  3788. if ( szTemp )
  3789. {
  3790. *szTemp = 0;
  3791. }
  3792. bstrDomain = SysAllocString( szXDomain );
  3793. }
  3794. }
  3795. DebugMsg((DM_VERBOSE, TEXT("ParseDomainName: *** %s ***"), bstrDomain ? bstrDomain : L"" ));
  3796. return bstrDomain;
  3797. }
  3798. BSTR
  3799. ParseDomainName2( LPWSTR szDSObject )
  3800. {
  3801. BSTR bstrDomain = 0;
  3802. if ( !szDSObject )
  3803. {
  3804. return bstrDomain;
  3805. }
  3806. if ( CompareString( LOCALE_USER_DEFAULT,
  3807. NORM_IGNORECASE,
  3808. szDSObject,
  3809. 7,
  3810. L"LDAP://",
  3811. 7 ) == CSTR_EQUAL )
  3812. {
  3813. szDSObject += 7;
  3814. }
  3815. if ( *szDSObject )
  3816. {
  3817. WCHAR szXDomain[MAX_PATH*2];
  3818. DWORD dwSize = MAX_PATH*2;
  3819. if ( TranslateName( szDSObject,
  3820. NameUnknown,
  3821. NameCanonical,
  3822. szXDomain,
  3823. &dwSize ) )
  3824. {
  3825. LPWSTR szTemp = wcschr( szXDomain, L'/' );
  3826. if ( szTemp )
  3827. {
  3828. *szTemp = 0;
  3829. }
  3830. bstrDomain = SysAllocString( szXDomain );
  3831. }
  3832. }
  3833. DebugMsg((DM_VERBOSE, TEXT("ParseDomainName2: *** %s ***"), bstrDomain ? bstrDomain : L"" ));
  3834. return bstrDomain;
  3835. }
  3836. INT_PTR CALLBACK CGroupPolicyObject::WQLFilterDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  3837. {
  3838. CGroupPolicyObject* pGPO;
  3839. switch (message)
  3840. {
  3841. case WM_INITDIALOG:
  3842. {
  3843. HRESULT hr;
  3844. BSTR bstrName;
  3845. VARIANT var;
  3846. LPTSTR lpDisplayName;
  3847. pGPO = (CGroupPolicyObject*) (((LPPROPSHEETPAGE)lParam)->lParam);
  3848. SetWindowLongPtr (hDlg, DWLP_USER, (LONG_PTR) pGPO);
  3849. //
  3850. // Set the defaults
  3851. //
  3852. pGPO->m_pTempFilterString = NULL;
  3853. CheckDlgButton (hDlg, IDC_NONE, BST_CHECKED);
  3854. EnableWindow (GetDlgItem(hDlg, IDC_FILTER_NAME), FALSE);
  3855. EnableWindow (GetDlgItem(hDlg, IDC_FILTER_BROWSE), FALSE);
  3856. if (pGPO->m_dwFlags & GPO_OPEN_READ_ONLY)
  3857. {
  3858. EnableWindow (GetDlgItem(hDlg, IDC_NONE), FALSE);
  3859. EnableWindow (GetDlgItem(hDlg, IDC_THIS_FILTER), FALSE);
  3860. }
  3861. //
  3862. // Query for the filter
  3863. //
  3864. bstrName = SysAllocString (GPO_WQLFILTER_PROPERTY);
  3865. if (!bstrName)
  3866. {
  3867. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WQLFilterDlgProc: Failed to allocate memory")));
  3868. break;
  3869. }
  3870. VariantInit(&var);
  3871. hr = pGPO->m_pADs->Get(bstrName, &var);
  3872. //
  3873. // If we find a filter, initialize the UI and save the filter string in the
  3874. // temporary buffer
  3875. //
  3876. if (SUCCEEDED(hr))
  3877. {
  3878. //
  3879. // Check if we found a null filter (defined as one space character)
  3880. //
  3881. if (*var.bstrVal != TEXT(' '))
  3882. {
  3883. pGPO->m_pTempFilterString = new TCHAR [lstrlen(var.bstrVal) + 1];
  3884. if (!pGPO->m_pTempFilterString)
  3885. {
  3886. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WQLFilterDlgProc: Failed to allocate memory for filter")));
  3887. SysFreeString (bstrName);
  3888. VariantClear (&var);
  3889. break;
  3890. }
  3891. lstrcpy (pGPO->m_pTempFilterString, var.bstrVal);
  3892. lpDisplayName = GetWMIFilterDisplayName (hDlg, pGPO->m_pTempFilterString, TRUE, FALSE);
  3893. if (lpDisplayName)
  3894. {
  3895. SetDlgItemText (hDlg, IDC_FILTER_NAME, lpDisplayName);
  3896. delete [] lpDisplayName;
  3897. CheckDlgButton (hDlg, IDC_NONE, BST_UNCHECKED);
  3898. CheckDlgButton (hDlg, IDC_THIS_FILTER, BST_CHECKED);
  3899. EnableWindow (GetDlgItem(hDlg, IDC_FILTER_NAME), TRUE);
  3900. if (!(pGPO->m_dwFlags & GPO_OPEN_READ_ONLY))
  3901. {
  3902. EnableWindow (GetDlgItem(hDlg, IDC_FILTER_BROWSE), TRUE);
  3903. }
  3904. }
  3905. }
  3906. }
  3907. else
  3908. {
  3909. if (hr != E_ADS_PROPERTY_NOT_FOUND)
  3910. {
  3911. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WQLFilterDlgProc: Failed to query filter with 0x%x"), hr));
  3912. }
  3913. }
  3914. SysFreeString (bstrName);
  3915. VariantClear (&var);
  3916. break;
  3917. }
  3918. case WM_COMMAND:
  3919. {
  3920. pGPO = (CGroupPolicyObject *) GetWindowLongPtr (hDlg, DWLP_USER);
  3921. if (!pGPO) {
  3922. break;
  3923. }
  3924. if (LOWORD(wParam) == IDC_FILTER_BROWSE)
  3925. {
  3926. LPTSTR lpDisplayName = NULL, lpFilter = NULL;
  3927. WCHAR szDomain[2*MAX_PATH];
  3928. HRESULT hr = pGPO->GetPath( szDomain, ARRAYSIZE( szDomain ) );
  3929. if ( FAILED( hr ) )
  3930. {
  3931. break;
  3932. }
  3933. BSTR bstrDomain = ParseDomainName2( szDomain );
  3934. if (!GetWMIFilter(FALSE, hDlg, TRUE, &lpDisplayName, &(pGPO->m_pTempFilterString), bstrDomain ))
  3935. {
  3936. SysFreeString( bstrDomain );
  3937. break;
  3938. }
  3939. SysFreeString( bstrDomain );
  3940. if (!(pGPO->m_pTempFilterString)) {
  3941. SetDlgItemText (hDlg, IDC_FILTER_NAME, TEXT(""));
  3942. EnableWindow (GetDlgItem(hDlg, IDC_FILTER_NAME), FALSE);
  3943. EnableWindow (GetDlgItem(hDlg, IDC_FILTER_BROWSE), FALSE);
  3944. CheckDlgButton (hDlg, IDC_NONE, BST_CHECKED);
  3945. CheckDlgButton (hDlg, IDC_THIS_FILTER, BST_UNCHECKED);
  3946. SendMessage (GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0);
  3947. }
  3948. else {
  3949. SetDlgItemText (hDlg, IDC_FILTER_NAME, lpDisplayName);
  3950. delete [] lpDisplayName;
  3951. SendMessage (GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0);
  3952. }
  3953. }
  3954. if (LOWORD(wParam) == IDC_NONE)
  3955. {
  3956. EnableWindow (GetDlgItem(hDlg, IDC_FILTER_NAME), FALSE);
  3957. EnableWindow (GetDlgItem(hDlg, IDC_FILTER_BROWSE), FALSE);
  3958. if (pGPO->m_pTempFilterString)
  3959. {
  3960. delete [] pGPO->m_pTempFilterString;
  3961. pGPO->m_pTempFilterString = NULL;
  3962. }
  3963. }
  3964. else if (LOWORD(wParam) == IDC_THIS_FILTER)
  3965. {
  3966. EnableWindow (GetDlgItem(hDlg, IDC_FILTER_NAME), TRUE);
  3967. EnableWindow (GetDlgItem(hDlg, IDC_FILTER_BROWSE), TRUE);
  3968. }
  3969. break;
  3970. }
  3971. case WM_NOTIFY:
  3972. {
  3973. pGPO = (CGroupPolicyObject *) GetWindowLongPtr (hDlg, DWLP_USER);
  3974. if (!pGPO) {
  3975. break;
  3976. }
  3977. switch (((NMHDR FAR*)lParam)->code)
  3978. {
  3979. case PSN_APPLY:
  3980. {
  3981. HRESULT hr;
  3982. BSTR bstrName;
  3983. VARIANT var;
  3984. //
  3985. // Save the current WQL filter
  3986. //
  3987. bstrName = SysAllocString (GPO_WQLFILTER_PROPERTY);
  3988. if (!bstrName)
  3989. {
  3990. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WQLFilterDlgProc: Failed to allocate memory")));
  3991. break;
  3992. }
  3993. VariantInit(&var);
  3994. var.vt = VT_BSTR;
  3995. var.bstrVal = SysAllocString (pGPO->m_pTempFilterString ? pGPO->m_pTempFilterString : TEXT(" "));
  3996. if (var.bstrVal)
  3997. {
  3998. hr = pGPO->m_pADs->Put(bstrName, var);
  3999. }
  4000. else
  4001. {
  4002. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WQLFilterDlgProc: Failed to allocate memory")));
  4003. SysFreeString (bstrName);
  4004. break;
  4005. }
  4006. SysFreeString (bstrName);
  4007. VariantClear (&var);
  4008. if (FAILED(hr))
  4009. {
  4010. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WQLFilterDlgProc: Failed to put display name with 0x%x"), hr));
  4011. break;
  4012. }
  4013. //
  4014. // Commit the changes
  4015. //
  4016. hr = pGPO->m_pADs->SetInfo();
  4017. if (FAILED(hr))
  4018. {
  4019. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::WQLFilterDlgProc: Failed to commit changes with 0x%x"), hr));
  4020. break;
  4021. }
  4022. //
  4023. // Free the filter string if appropriate
  4024. //
  4025. if (((PSHNOTIFY *)lParam)->lParam)
  4026. {
  4027. if (pGPO->m_pTempFilterString)
  4028. {
  4029. delete [] pGPO->m_pTempFilterString;
  4030. pGPO->m_pTempFilterString = NULL;
  4031. }
  4032. }
  4033. break;
  4034. }
  4035. case PSN_RESET:
  4036. {
  4037. if (pGPO->m_pTempFilterString)
  4038. {
  4039. delete [] pGPO->m_pTempFilterString;
  4040. pGPO->m_pTempFilterString = NULL;
  4041. }
  4042. break;
  4043. }
  4044. }
  4045. break;
  4046. }
  4047. case WM_HELP: // F1
  4048. WinHelp((HWND)((LPHELPINFO) lParam)->hItemHandle, HELP_FILE, HELP_WM_HELP,
  4049. (ULONG_PTR) (LPSTR) aWQLFilterHelpIds);
  4050. break;
  4051. case WM_CONTEXTMENU: // right mouse click
  4052. WinHelp((HWND) wParam, HELP_FILE, HELP_CONTEXTMENU,
  4053. (ULONG_PTR) (LPSTR) aWQLFilterHelpIds);
  4054. return (TRUE);
  4055. default:
  4056. break;
  4057. }
  4058. return FALSE;
  4059. }
  4060. #define MAX_BUTTON_LEN 64
  4061. //////////////////////////////////////////////////////////////////////////////
  4062. //
  4063. //////////////////////////////////////////////////////////////////////////////
  4064. INT_PTR CALLBACK CGroupPolicyObject::GPELinksDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  4065. {
  4066. GLPARAM * pglp = NULL;
  4067. switch (message)
  4068. {
  4069. case WM_INITDIALOG:
  4070. {
  4071. LV_COLUMN lvc = {LVCF_WIDTH};
  4072. RECT rc;
  4073. HWND hList = GetDlgItem(hDlg, IDC_RESULTLIST);
  4074. // Allocate the per dialog structure
  4075. pglp = (GLPARAM*)LocalAlloc (LPTR, sizeof(GLPARAM));
  4076. if (pglp)
  4077. {
  4078. pglp->pGPO = (CGroupPolicyObject*) (((LPPROPSHEETPAGE)lParam)->lParam);
  4079. SetWindowLongPtr (hDlg, DWLP_USER, (LONG_PTR) pglp);
  4080. pglp->pGPO->FillDomainList (GetDlgItem(hDlg, IDC_CBDOMAIN));
  4081. }
  4082. else
  4083. {
  4084. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GPELinksDlgProc: Failed to LocalAlloc in WM_INITDIALOG")));
  4085. }
  4086. // Set the Columns, in the list view
  4087. if (IsWindow(hList))
  4088. {
  4089. SendMessage(hList, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_LABELTIP,
  4090. LVS_EX_LABELTIP);
  4091. GetClientRect(hList, &rc);
  4092. lvc.cx = (rc.right - rc.left);
  4093. ListView_InsertColumn(hList, 0, &lvc);
  4094. }
  4095. // Show icon in the corner
  4096. Animate_Open(GetDlgItem(hDlg, IDAC_FIND), MAKEINTRESOURCE(IDA_FIND));
  4097. break;
  4098. }
  4099. case WM_COMMAND:
  4100. {
  4101. pglp = (GLPARAM *) GetWindowLongPtr (hDlg, DWLP_USER);
  4102. if (!pglp)
  4103. {
  4104. break;
  4105. }
  4106. if ((IDC_CBDOMAIN == LOWORD(wParam)) && ((CBN_SELCHANGE == HIWORD(wParam)) || (CBN_SELENDOK == HIWORD(wParam))))
  4107. {
  4108. // Clear the list view
  4109. pglp->fAbort = TRUE;
  4110. SendDlgItemMessage(hDlg, IDC_RESULTLIST, LVM_DELETEALLITEMS, 0, 0L);
  4111. break;
  4112. }
  4113. // If the IDC_ACTION was clicked then do search
  4114. if ((IDC_ACTION == LOWORD(wParam)) && (BN_CLICKED == HIWORD(wParam)))
  4115. {
  4116. // If we are have been asked to start a search, create the thread to do so
  4117. if (!pglp->fFinding)
  4118. {
  4119. HANDLE hThread = NULL;
  4120. DWORD dwThreadId = 0;
  4121. GLTHREADPARAM * pgltp = NULL;
  4122. int nCurSel = 0;
  4123. // Make sure something has been selected in the combo box
  4124. nCurSel = (int)SendDlgItemMessage (hDlg, IDC_CBDOMAIN, CB_GETCURSEL, 0, 0L);
  4125. if (CB_ERR == nCurSel)
  4126. {
  4127. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GPELinksDlgProc: There was no Domain selected in the combo box. Exiting.")));
  4128. break;
  4129. }
  4130. // Allocate the Thread Param structure
  4131. pgltp = (GLTHREADPARAM*)LocalAlloc (LPTR, sizeof(GLTHREADPARAM));
  4132. if (!pgltp)
  4133. {
  4134. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GPELinksDlgProc: Failed to LocalAlloc Thread Param structure")));
  4135. break;
  4136. }
  4137. pgltp->hDlg = hDlg;
  4138. pgltp->pGPO = pglp->pGPO;
  4139. pgltp->pfAbort = &pglp->fAbort;
  4140. pgltp->pszLDAPName = (LPOLESTR)SendDlgItemMessage (hDlg, IDC_CBDOMAIN, CB_GETITEMDATA, nCurSel, 0L);
  4141. if (!pgltp->pszLDAPName)
  4142. {
  4143. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GPELinksDlgProc: The LDAP name buffer was NULL.")));
  4144. LocalFree(pgltp);
  4145. break;
  4146. }
  4147. pgltp->pszLDAPName = MakeFullPath (pgltp->pszLDAPName, pglp->pGPO->m_pMachineName);
  4148. // Unset the abort flag
  4149. pglp->fAbort = FALSE;
  4150. // Clear the list view
  4151. SendDlgItemMessage(hDlg, IDC_RESULTLIST, LVM_DELETEALLITEMS, 0, 0L);
  4152. // Fire off the thread to fill the list view
  4153. hThread = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)GLThreadFunc, pgltp, 0, &dwThreadId);
  4154. if (!hThread)
  4155. {
  4156. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GPELinksDlgProc: Could not create the search thread.")));
  4157. LocalFree(pgltp);
  4158. break;
  4159. }
  4160. CloseHandle (hThread);
  4161. // Change the text on the button to "Stop"
  4162. SendMessage (hDlg, PDM_CHANGEBUTTONTEXT, 0, 0L);
  4163. }
  4164. else
  4165. {
  4166. // The user wants to stop the search
  4167. pglp->fAbort = TRUE;
  4168. }
  4169. }
  4170. break;
  4171. }
  4172. case PDM_CHANGEBUTTONTEXT:
  4173. {
  4174. TCHAR szButtonText[MAX_BUTTON_LEN] = {0};
  4175. pglp = (GLPARAM *) GetWindowLongPtr (hDlg, DWLP_USER);
  4176. if (!pglp)
  4177. {
  4178. break;
  4179. }
  4180. if (!pglp->fFinding)
  4181. Animate_Play(GetDlgItem(hDlg, IDAC_FIND), 0, -1, -1);
  4182. else
  4183. Animate_Stop(GetDlgItem(hDlg, IDAC_FIND));
  4184. // Set the button to show appropriate text
  4185. LoadString (g_hInstance, pglp->fFinding ? IDS_FINDNOW: IDS_STOP, szButtonText, ARRAYSIZE(szButtonText));
  4186. SetDlgItemText (hDlg, IDC_ACTION, szButtonText);
  4187. // Flip the toggle
  4188. pglp->fFinding = !pglp->fFinding;
  4189. break;
  4190. }
  4191. case WM_NOTIFY:
  4192. {
  4193. pglp = (GLPARAM *) GetWindowLongPtr (hDlg, DWLP_USER);
  4194. if (!pglp)
  4195. {
  4196. break;
  4197. }
  4198. switch (((NMHDR FAR*)lParam)->code)
  4199. {
  4200. // In case thr user wants to cancel, bail from the thread
  4201. case PSN_QUERYCANCEL:
  4202. pglp->fAbort = TRUE;
  4203. break;
  4204. // In case thr user wants to close the prop sheet, bail from the thread
  4205. case PSN_APPLY:
  4206. case PSN_RESET:
  4207. {
  4208. int nCount = 0;
  4209. PSHNOTIFY * pNotify = (PSHNOTIFY *) lParam;
  4210. // User just hit the Apply button don't destroy everything.
  4211. if (!pNotify->lParam)
  4212. {
  4213. SetWindowLongPtr (hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
  4214. return TRUE;
  4215. }
  4216. pglp->fAbort = TRUE;
  4217. // When the dialog is going away, delete all the data that was stored with each CB item in
  4218. // FillDomainList() are freed
  4219. if (IsWindow(GetDlgItem(hDlg, IDC_CBDOMAIN)))
  4220. {
  4221. nCount = (int) SendDlgItemMessage(hDlg, IDC_CBDOMAIN, CB_GETCOUNT, 0, 0L);
  4222. for (int nIndex = 0; nIndex < nCount; nIndex++)
  4223. {
  4224. LPOLESTR pszStr;
  4225. pszStr = (LPOLESTR)SendDlgItemMessage(hDlg, IDC_CBDOMAIN, CB_GETITEMDATA, nIndex, 0L);
  4226. if (pszStr)
  4227. delete [] pszStr;
  4228. }
  4229. }
  4230. // Free the per dialog structure
  4231. LocalFree (pglp);
  4232. SetWindowLongPtr (hDlg, DWLP_USER, (LONG_PTR) NULL);
  4233. Animate_Close(GetDlgItem(hDlg, IDAC_FIND));
  4234. break;
  4235. }
  4236. }
  4237. break;
  4238. }
  4239. case WM_HELP: // F1
  4240. WinHelp((HWND)((LPHELPINFO) lParam)->hItemHandle, HELP_FILE, HELP_WM_HELP,
  4241. (ULONG_PTR) (LPSTR) aLinkHelpIds);
  4242. break;
  4243. case WM_CONTEXTMENU: // right mouse click
  4244. WinHelp((HWND) wParam, HELP_FILE, HELP_CONTEXTMENU,
  4245. (ULONG_PTR) (LPSTR) aLinkHelpIds);
  4246. return (TRUE);
  4247. default:
  4248. break;
  4249. }
  4250. return FALSE;
  4251. }
  4252. //////////////////////////////////////////////////////////////////////////////
  4253. // The thread that will look call the recursive find function. This function will clean up
  4254. // the param structure that has been passed in
  4255. //////////////////////////////////////////////////////////////////////////////
  4256. DWORD WINAPI CGroupPolicyObject::GLThreadFunc(GLTHREADPARAM * pgltp)
  4257. {
  4258. LPTSTR lpGPO;
  4259. DWORD dwRet;
  4260. HINSTANCE hInstance = LoadLibrary(TEXT("GPEdit.dll"));
  4261. HRESULT hr;
  4262. //
  4263. // Initialize COM
  4264. //
  4265. hr = CoInitialize(NULL);
  4266. if (FAILED(hr))
  4267. {
  4268. return 0L;
  4269. }
  4270. //
  4271. // Make sure we have a thread param structure
  4272. //
  4273. if (pgltp)
  4274. {
  4275. pgltp->pGPO->AddRef();
  4276. lpGPO = MakeNamelessPath (pgltp->pGPO->m_pDSPath);
  4277. if (lpGPO)
  4278. {
  4279. //
  4280. // Check if the user wants to abort. Otherwise make recursive call
  4281. //
  4282. if (!*(pgltp->pfAbort))
  4283. {
  4284. dwRet = pgltp->pGPO->FindLinkInDomain(pgltp, lpGPO);
  4285. }
  4286. if ((!*(pgltp->pfAbort)) && dwRet)
  4287. {
  4288. pgltp->pGPO->FindLinkInSite(pgltp, lpGPO);
  4289. }
  4290. if (IsWindow(GetDlgItem(pgltp->hDlg, IDC_RESULTLIST)))
  4291. {
  4292. ListView_SetItemState(GetDlgItem(pgltp->hDlg, IDC_RESULTLIST), 0, LVIS_SELECTED |LVIS_FOCUSED, LVIS_SELECTED |LVIS_FOCUSED);
  4293. }
  4294. //
  4295. // Switch the button text, change the cursor, and free the param that the
  4296. // dialog proc allocated and sent to us
  4297. //
  4298. SendMessage(pgltp->hDlg, PDM_CHANGEBUTTONTEXT, 0, 0L);
  4299. LocalFree (lpGPO);
  4300. }
  4301. pgltp->pGPO->Release();
  4302. LocalFree(pgltp->pszLDAPName);
  4303. LocalFree(pgltp);
  4304. }
  4305. //
  4306. // Uninitialize COM
  4307. //
  4308. CoUninitialize();
  4309. FreeLibraryAndExitThread(hInstance, 0);
  4310. return 0L;
  4311. }
  4312. DWORD WINAPI CGroupPolicyObject::FindLinkInSite(GLTHREADPARAM * pgltp, LPTSTR lpGPO)
  4313. {
  4314. IADsContainer * pADsContainer = NULL;
  4315. HRESULT hr;
  4316. IEnumVARIANT *pVar = NULL;
  4317. IADs * pADs = NULL;
  4318. VARIANT var;
  4319. ULONG ulResult;
  4320. IDispatch * pDispatch = NULL;
  4321. BSTR bstrClassName;
  4322. BSTR bstrSite = NULL;
  4323. IADsPathname * pADsPathname = NULL;
  4324. //
  4325. // Create a pathname object we can work with
  4326. //
  4327. hr = CoCreateInstance(CLSID_Pathname, NULL, CLSCTX_INPROC_SERVER,
  4328. IID_IADsPathname, (LPVOID*)&pADsPathname);
  4329. if (FAILED(hr))
  4330. {
  4331. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::FindLinkInSite: Failed to create adspathname instance with 0x%x"), hr));
  4332. goto Exit;
  4333. }
  4334. //
  4335. // Add the gpo name
  4336. //
  4337. hr = pADsPathname->Set (pgltp->pszLDAPName, ADS_SETTYPE_FULL);
  4338. if (FAILED(hr))
  4339. {
  4340. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::FindLinkInSite: Failed to set pathname with 0x%x"), hr));
  4341. goto Exit;
  4342. }
  4343. //
  4344. // Add the configuration folder to the path
  4345. //
  4346. hr = pADsPathname->AddLeafElement (TEXT("CN=Configuration"));
  4347. if (FAILED(hr))
  4348. {
  4349. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::FindLinkInSite: Failed to add configuration folder with 0x%x"), hr));
  4350. goto Exit;
  4351. }
  4352. //
  4353. // Add the sites container to the path
  4354. //
  4355. hr = pADsPathname->AddLeafElement (TEXT("CN=Sites"));
  4356. if (FAILED(hr))
  4357. {
  4358. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::FindLinkInSite: Failed to add sites folder with 0x%x"), hr));
  4359. goto Exit;
  4360. }
  4361. //
  4362. // Retreive the container path
  4363. //
  4364. hr = pADsPathname->Retrieve (ADS_FORMAT_X500, &bstrSite);
  4365. if (FAILED(hr))
  4366. {
  4367. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::FindLinkInSite: Failed to retreive site path with 0x%x"), hr));
  4368. goto Exit;
  4369. }
  4370. // Create Enumerator
  4371. hr = OpenDSObject(bstrSite, IID_IADsContainer, (void **)&pADsContainer);
  4372. if (FAILED(hr))
  4373. {
  4374. DebugMsg((DM_VERBOSE, TEXT("FindLinkInSite: Failed to get gpo container interface with 0x%x for object %s"),
  4375. hr, bstrSite));
  4376. goto Exit;
  4377. }
  4378. // Build the enumerator
  4379. hr = ADsBuildEnumerator (pADsContainer, &pVar);
  4380. if (FAILED(hr))
  4381. {
  4382. DebugMsg((DM_WARNING, TEXT("FindLinkInSite: Failed to get enumerator with 0x%x"), hr));
  4383. goto Exit;
  4384. }
  4385. //
  4386. // Enumerate
  4387. //
  4388. while (TRUE)
  4389. {
  4390. TCHAR lpSite[] = TEXT("site");
  4391. DWORD dwStrLen = lstrlen (lpSite);
  4392. // Check if the user wants to abort. Before proceeding
  4393. if (*(pgltp->pfAbort))
  4394. {
  4395. break;
  4396. }
  4397. VariantInit(&var);
  4398. hr = ADsEnumerateNext(pVar, 1, &var, &ulResult);
  4399. if (S_FALSE == hr)
  4400. {
  4401. VariantClear (&var);
  4402. break;
  4403. }
  4404. if ((FAILED(hr)) || (var.vt != VT_DISPATCH))
  4405. {
  4406. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::FindLinkInSite: Failed to enumerator with 0x%x or we didn't get the IDispatch"), hr));
  4407. VariantClear (&var);
  4408. break;
  4409. }
  4410. if (*(pgltp->pfAbort))
  4411. {
  4412. VariantClear (&var);
  4413. break;
  4414. }
  4415. //
  4416. // We found something, get the IDispatch interface
  4417. //
  4418. pDispatch = var.pdispVal;
  4419. if (!pDispatch)
  4420. {
  4421. VariantClear (&var);
  4422. goto Exit;
  4423. }
  4424. //
  4425. // Now query for the IADs interface so we can get some
  4426. // properties from this object
  4427. //
  4428. hr = pDispatch->QueryInterface(IID_IADs, (LPVOID *)&pADs);
  4429. if (FAILED(hr))
  4430. {
  4431. DebugMsg((DM_WARNING, TEXT("AddGPOsForDomain: QI for IADs failed with 0x%x"), hr));
  4432. VariantClear (&var);
  4433. goto Exit;
  4434. }
  4435. //
  4436. // Get the relative and class names
  4437. //
  4438. hr = pADs->get_Class (&bstrClassName);
  4439. if (FAILED(hr))
  4440. {
  4441. DebugMsg((DM_WARNING, TEXT("DSDelnodeRecurse: Failed get class name with 0x%x"), hr));
  4442. pADs->Release();
  4443. VariantClear (&var);
  4444. goto Exit;
  4445. }
  4446. if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | NORM_STOP_ON_NULL,
  4447. lpSite, dwStrLen, bstrClassName, dwStrLen) == CSTR_EQUAL)
  4448. {
  4449. VARIANT varLink;
  4450. BSTR bstrLinkProp;
  4451. VariantInit(&varLink);
  4452. bstrLinkProp = SysAllocString(GPM_LINK_PROPERTY);
  4453. if (bstrLinkProp)
  4454. {
  4455. // Now get the Name property
  4456. hr = pADs->Get(bstrLinkProp, &varLink);
  4457. // Create the new LDAP:// string and call FindLinkInDomain() recursively
  4458. if (SUCCEEDED(hr) && wcsstr(varLink.bstrVal, lpGPO))
  4459. {
  4460. VARIANT varName;
  4461. BSTR bstrNameProp;
  4462. VariantInit(&varName);
  4463. bstrNameProp = SysAllocString(GPM_NAME_PROPERTY);
  4464. if (bstrNameProp)
  4465. {
  4466. // Now get the Name property
  4467. hr = pADs->Get(bstrNameProp, &varName);
  4468. if (SUCCEEDED(hr))
  4469. {
  4470. LV_ITEM lvi = {LVIF_TEXT};
  4471. LPTSTR pszTemp = MakeNamelessPath(bstrSite);
  4472. if (pszTemp)
  4473. {
  4474. ULONG ulLen = wcslen(pszTemp) + 2 + wcslen(varName.bstrVal);
  4475. LPOLESTR pszTranslated = new OLECHAR[ulLen];
  4476. if (pszTranslated)
  4477. {
  4478. // Move pointer over the lDAP:// string and insert the rest into the listview
  4479. pszTemp += wcslen(TEXT("LDAP://"));
  4480. lvi.iItem = 0x7FFFFFFF;
  4481. if (TranslateName(pszTemp, NameFullyQualifiedDN, NameCanonical, pszTranslated, &ulLen))
  4482. {
  4483. wcscat(pszTranslated, TEXT("/"));
  4484. wcscat(pszTranslated, varName.bstrVal);
  4485. lvi.pszText = pszTranslated;
  4486. ListView_InsertItem(GetDlgItem(pgltp->hDlg, IDC_RESULTLIST), &lvi);
  4487. }
  4488. delete [] pszTranslated;
  4489. }
  4490. LocalFree (pszTemp);
  4491. }
  4492. }
  4493. SysFreeString (bstrNameProp);
  4494. }
  4495. VariantClear (&varName);
  4496. }
  4497. SysFreeString (bstrLinkProp);
  4498. }
  4499. VariantClear (&varLink);
  4500. }
  4501. pADs->Release();
  4502. SysFreeString (bstrClassName);
  4503. }
  4504. Exit:
  4505. if (pADsContainer)
  4506. pADsContainer->Release();
  4507. if (pADsPathname)
  4508. pADsPathname->Release();
  4509. if (bstrSite)
  4510. SysFreeString (bstrSite);
  4511. return 1L;
  4512. }
  4513. //////////////////////////////////////////////////////////////////////////////
  4514. // Recursive call that will look through all domains and OUs for our GUID
  4515. //////////////////////////////////////////////////////////////////////////////
  4516. DWORD WINAPI CGroupPolicyObject::FindLinkInDomain(GLTHREADPARAM * pgltp, LPTSTR lpGPO)
  4517. {
  4518. IADs * pADs = NULL;
  4519. IADsContainer * pADsContainer = NULL;
  4520. HRESULT hr;
  4521. IEnumVARIANT *pVar = NULL;
  4522. VARIANT var;
  4523. ULONG ulResult;
  4524. BSTR bstrClassName;
  4525. IDispatch * pDispatch = NULL;
  4526. DWORD dwResult = 1;
  4527. // Check if the user wants to abort. Before proceeding
  4528. if (*(pgltp->pfAbort))
  4529. {
  4530. return 0;
  4531. }
  4532. // Bind to Object
  4533. hr = OpenDSObject(pgltp->pszLDAPName, IID_IADs, (void **)&pADs);
  4534. if (SUCCEEDED(hr))
  4535. {
  4536. BSTR bstrLinkProp;
  4537. VariantInit(&var);
  4538. bstrLinkProp = SysAllocString(GPM_LINK_PROPERTY);
  4539. if (bstrLinkProp)
  4540. {
  4541. // Now get the link property
  4542. hr = pADs->Get(bstrLinkProp, &var);
  4543. // Check if out GUID is in there.
  4544. if (SUCCEEDED(hr) && StrStrI(var.bstrVal, lpGPO))
  4545. {
  4546. LV_ITEM lvi = {LVIF_TEXT};
  4547. //
  4548. // Check if this is a forest path
  4549. //
  4550. if (IsForest(pgltp->pszLDAPName))
  4551. {
  4552. TCHAR szForest[50] = {0};
  4553. LoadString (g_hInstance, IDS_FOREST, szForest, ARRAYSIZE(szForest));
  4554. lvi.iItem = 0x7FFFFFFF;
  4555. lvi.pszText = szForest;
  4556. ListView_InsertItem(GetDlgItem(pgltp->hDlg, IDC_RESULTLIST), &lvi);
  4557. }
  4558. else
  4559. {
  4560. LPTSTR pszTemp = MakeNamelessPath(pgltp->pszLDAPName);
  4561. if (pszTemp)
  4562. {
  4563. ULONG ulLen = wcslen(pszTemp) + 2;
  4564. LPOLESTR pszTranslated = new OLECHAR[ulLen];
  4565. if (pszTranslated)
  4566. {
  4567. // Move pointer over the lDAP:// string and insert the rest into the listview
  4568. pszTemp += wcslen(TEXT("LDAP://"));
  4569. lvi.iItem = 0x7FFFFFFF;
  4570. if (TranslateName(pszTemp, NameFullyQualifiedDN, NameCanonical, pszTranslated, &ulLen))
  4571. {
  4572. lvi.pszText = pszTranslated;
  4573. ListView_InsertItem(GetDlgItem(pgltp->hDlg, IDC_RESULTLIST), &lvi);
  4574. }
  4575. delete [] pszTranslated;
  4576. }
  4577. LocalFree (pszTemp);
  4578. }
  4579. }
  4580. }
  4581. // Cleanup
  4582. SysFreeString(bstrLinkProp);
  4583. }
  4584. VariantClear(&var);
  4585. pADs->Release();
  4586. }
  4587. else
  4588. {
  4589. DebugMsg((DM_VERBOSE, TEXT("FindLinkInDomain: Failed to get IID_IADs. hr: 0x%x, for %s"),hr, pgltp->pszLDAPName));
  4590. ReportError(pgltp->hDlg, hr, IDS_DSBINDFAILED);
  4591. dwResult = 0;
  4592. goto Exit;
  4593. }
  4594. // Check if the user wants to abort. Before proceeding
  4595. if (*(pgltp->pfAbort))
  4596. {
  4597. dwResult = 0;
  4598. goto Exit;
  4599. }
  4600. // Create Enumerator
  4601. hr = OpenDSObject(pgltp->pszLDAPName, IID_IADsContainer, (void **)&pADsContainer);
  4602. if (FAILED(hr))
  4603. {
  4604. DebugMsg((DM_VERBOSE, TEXT("FindLinkInDomain: Failed to get gpo container interface with 0x%x for object %s"),
  4605. hr, pgltp->pszLDAPName));
  4606. dwResult = 0;
  4607. goto Exit;
  4608. }
  4609. hr = ADsBuildEnumerator (pADsContainer, &pVar);
  4610. if (FAILED(hr))
  4611. {
  4612. DebugMsg((DM_WARNING, TEXT("FindLinkInDomain: Failed to get enumerator with 0x%x"), hr));
  4613. dwResult = 0;
  4614. goto Exit;
  4615. }
  4616. //
  4617. // Enumerate
  4618. //
  4619. while (TRUE)
  4620. {
  4621. TCHAR lpOU[] = TEXT("organizationalUnit");
  4622. DWORD dwStrLen = lstrlen (lpOU);
  4623. // Check if the user wants to abort. Before proceeding
  4624. if (*(pgltp->pfAbort))
  4625. {
  4626. break;
  4627. }
  4628. VariantInit(&var);
  4629. hr = ADsEnumerateNext(pVar, 1, &var, &ulResult);
  4630. if (S_FALSE == hr)
  4631. {
  4632. VariantClear (&var);
  4633. break;
  4634. }
  4635. if ((FAILED(hr)) || (var.vt != VT_DISPATCH))
  4636. {
  4637. VariantClear (&var);
  4638. break;
  4639. }
  4640. if (*(pgltp->pfAbort))
  4641. {
  4642. VariantClear (&var);
  4643. break;
  4644. }
  4645. //
  4646. // We found something, get the IDispatch interface
  4647. //
  4648. pDispatch = var.pdispVal;
  4649. if (!pDispatch)
  4650. {
  4651. VariantClear (&var);
  4652. dwResult = 0;
  4653. goto Exit;
  4654. }
  4655. //
  4656. // Now query for the IADs interface so we can get some
  4657. // properties from this object
  4658. //
  4659. hr = pDispatch->QueryInterface(IID_IADs, (LPVOID *)&pADs);
  4660. if (FAILED(hr))
  4661. {
  4662. DebugMsg((DM_WARNING, TEXT("AddGPOsForDomain: QI for IADs failed with 0x%x"), hr));
  4663. VariantClear (&var);
  4664. dwResult = 0;
  4665. goto Exit;
  4666. }
  4667. //
  4668. // Get the relative and class names
  4669. //
  4670. hr = pADs->get_Class (&bstrClassName);
  4671. if (FAILED(hr))
  4672. {
  4673. DebugMsg((DM_WARNING, TEXT("DSDelnodeRecurse: Failed get class name with 0x%x"), hr));
  4674. pADs->Release();
  4675. VariantClear (&var);
  4676. dwResult = 0;
  4677. goto Exit;
  4678. }
  4679. if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | NORM_STOP_ON_NULL,
  4680. lpOU, dwStrLen, bstrClassName, dwStrLen) == CSTR_EQUAL)
  4681. {
  4682. VARIANT varName;
  4683. BSTR bstrNameProp;
  4684. VariantInit(&varName);
  4685. bstrNameProp = SysAllocString(GPM_NAME_PROPERTY);
  4686. if (bstrNameProp)
  4687. {
  4688. // Now get the Name property
  4689. hr = pADs->Get(bstrNameProp, &varName);
  4690. // Create the new LDAP:// string and call FindLinkInDomain() recursively
  4691. if (SUCCEEDED(hr))
  4692. {
  4693. GLTHREADPARAM gltp = *pgltp;
  4694. IADsPathname * pADsPathname;
  4695. LPOLESTR pszNewName = new OLECHAR[wcslen(varName.bstrVal) + 10];
  4696. BSTR bstr;
  4697. //
  4698. // Build the new element name
  4699. //
  4700. if (!pszNewName)
  4701. {
  4702. dwResult = 0;
  4703. goto Exit;
  4704. }
  4705. wcscpy(pszNewName, TEXT("OU="));
  4706. wcscat(pszNewName, varName.bstrVal);
  4707. //
  4708. // Create a pathname object we can work with
  4709. //
  4710. hr = CoCreateInstance(CLSID_Pathname, NULL, CLSCTX_INPROC_SERVER,
  4711. IID_IADsPathname, (LPVOID*)&pADsPathname);
  4712. if (FAILED(hr))
  4713. {
  4714. delete [] pszNewName;
  4715. dwResult = 0;
  4716. goto Exit;
  4717. }
  4718. //
  4719. // Set the current name
  4720. //
  4721. hr = pADsPathname->Set (pgltp->pszLDAPName, ADS_SETTYPE_FULL);
  4722. if (FAILED(hr))
  4723. {
  4724. delete [] pszNewName;
  4725. pADsPathname->Release();
  4726. dwResult = 0;
  4727. goto Exit;
  4728. }
  4729. //
  4730. // Check for escape characters
  4731. //
  4732. hr = pADsPathname->GetEscapedElement (0, pszNewName, &bstr);
  4733. delete [] pszNewName;
  4734. if (FAILED(hr))
  4735. {
  4736. pADsPathname->Release();
  4737. dwResult = 0;
  4738. goto Exit;
  4739. }
  4740. //
  4741. // Add the new element
  4742. //
  4743. hr = pADsPathname->AddLeafElement (bstr);
  4744. SysFreeString (bstr);
  4745. if (FAILED(hr))
  4746. {
  4747. pADsPathname->Release();
  4748. dwResult = 0;
  4749. goto Exit;
  4750. }
  4751. //
  4752. // Get the new path
  4753. //
  4754. hr = pADsPathname->Retrieve(ADS_FORMAT_X500, &bstr);
  4755. pADsPathname->Release();
  4756. if (FAILED(hr))
  4757. {
  4758. dwResult = 0;
  4759. goto Exit;
  4760. }
  4761. //
  4762. // Recurse
  4763. //
  4764. gltp.pszLDAPName = bstr;
  4765. if (FindLinkInDomain(&gltp, lpGPO) == 0)
  4766. {
  4767. dwResult = 0;
  4768. goto Exit;
  4769. }
  4770. SysFreeString (bstr);
  4771. }
  4772. SysFreeString (bstrNameProp);
  4773. }
  4774. VariantClear (&varName);
  4775. }
  4776. pADs->Release();
  4777. SysFreeString (bstrClassName);
  4778. }
  4779. Exit:
  4780. if (pADsContainer)
  4781. pADsContainer->Release();
  4782. return dwResult;
  4783. }
  4784. //////////////////////////////////////////////////////////////////////////////
  4785. // Fill the combobox with available domains
  4786. //////////////////////////////////////////////////////////////////////////////
  4787. BOOL CGroupPolicyObject::FillDomainList (HWND hWndCombo)
  4788. {
  4789. HRESULT hr;
  4790. DWORD dwIndex = 0;
  4791. LPOLESTR pszDomain;
  4792. LPTSTR lpTemp;
  4793. HCURSOR hcur = SetCursor(LoadCursor(NULL, IDC_WAIT));
  4794. // get the ordered tree of domains
  4795. LOOKDATA * pDomainList = BuildDomainList(NULL);
  4796. LOOKDATA *pRemember = pDomainList;
  4797. // now walk the tree, adding elements to the dialog box
  4798. int nCBIndex;
  4799. // start at the head
  4800. while (pDomainList)
  4801. {
  4802. // add the LDAP path for the doman in this node
  4803. //SendMessage(hWndCombo, CB_INSERTSTRING, (WPARAM) -1, (LPARAM)(LPCTSTR) pDomainList->szData);
  4804. nCBIndex = (int)SendMessage(hWndCombo, CB_INSERTSTRING, (WPARAM) -1, (LPARAM)(LPCTSTR) pDomainList->szName);
  4805. SendMessage(hWndCombo, CB_SETITEMDATA, (WPARAM) nCBIndex, (LPARAM)(LPCTSTR) pDomainList->szData);
  4806. if (pDomainList->pChild)
  4807. {
  4808. // go to its child
  4809. pDomainList = pDomainList->pChild;
  4810. }
  4811. else
  4812. {
  4813. if (pDomainList->pSibling)
  4814. {
  4815. // go to its sibling if there are no children
  4816. pDomainList = pDomainList->pSibling;
  4817. }
  4818. else
  4819. {
  4820. // there are no children and no siblings
  4821. // back up until we find a parent with a sibling
  4822. // or there are no more parents (we're done)
  4823. do
  4824. {
  4825. pDomainList = pDomainList->pParent;
  4826. if (pDomainList)
  4827. {
  4828. if (pDomainList->pSibling)
  4829. {
  4830. pDomainList = pDomainList->pSibling;
  4831. break;
  4832. }
  4833. }
  4834. else
  4835. {
  4836. break;
  4837. }
  4838. } while (TRUE);
  4839. }
  4840. }
  4841. }
  4842. FreeDomainInfo (pRemember);
  4843. //
  4844. // Select the current domain in the combobox
  4845. //
  4846. pszDomain = GetDomainFromLDAPPath(m_pDSPath);
  4847. if (pszDomain)
  4848. {
  4849. //
  4850. // Convert LDAP to dot (DN) style
  4851. //
  4852. hr = ConvertToDotStyle (pszDomain, &lpTemp);
  4853. if (SUCCEEDED(hr))
  4854. {
  4855. dwIndex = (DWORD) SendMessage (hWndCombo, CB_FINDSTRINGEXACT, (WPARAM) -1,
  4856. (LONG_PTR)lpTemp);
  4857. if (dwIndex == CB_ERR)
  4858. {
  4859. dwIndex = 0;
  4860. }
  4861. LocalFree (lpTemp);
  4862. }
  4863. delete [] pszDomain;
  4864. }
  4865. SendMessage (hWndCombo, CB_SETCURSEL, (WPARAM)dwIndex, 0);
  4866. SetCursor(hcur);
  4867. return TRUE;
  4868. }
  4869. INT_PTR CALLBACK CGroupPolicyObject::PropertiesDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  4870. {
  4871. CGroupPolicyObject * pGPO;
  4872. static BOOL bDirty;
  4873. static BOOL bDisableWarningIssued;
  4874. switch (message)
  4875. {
  4876. case WM_INITDIALOG:
  4877. {
  4878. DWORD dwTemp;
  4879. LPTSTR lpEnd;
  4880. TCHAR szBuffer[2*MAX_PATH];
  4881. TCHAR szDate[100];
  4882. TCHAR szTime[100];
  4883. TCHAR szFormat[80];
  4884. TCHAR szVersion[100];
  4885. WIN32_FILE_ATTRIBUTE_DATA fad;
  4886. FILETIME filetime, CreateTime, ChangeTime;
  4887. SYSTEMTIME systime;
  4888. LPTSTR lpResult;
  4889. LPOLESTR pszDomain;
  4890. ULONG ulVersion = 0;
  4891. USHORT uMachine, uUser;
  4892. VARIANT var;
  4893. BSTR bstrName;
  4894. LPTSTR lpDisplayName;
  4895. WORD wDosDate, wDosTime;
  4896. pGPO = (CGroupPolicyObject *) (((LPPROPSHEETPAGE)lParam)->lParam);
  4897. SetWindowLongPtr (hDlg, DWLP_USER, (LONG_PTR) pGPO);
  4898. //
  4899. // Initialize
  4900. //
  4901. if ((pGPO->m_pMachineName) && (pGPO->m_gpoType == GPOTypeDS))
  4902. {
  4903. lpDisplayName = (LPTSTR) LocalAlloc (LPTR, (lstrlen(pGPO->m_pDisplayName) +
  4904. lstrlen(pGPO->m_pMachineName) +
  4905. 5) * sizeof(TCHAR));
  4906. if (lpDisplayName)
  4907. {
  4908. LoadString (g_hInstance, IDS_NAMEFORMAT, szFormat, ARRAYSIZE(szFormat));
  4909. wsprintf (lpDisplayName, szFormat, pGPO->m_pDisplayName, pGPO->m_pMachineName);
  4910. SetDlgItemText (hDlg, IDC_TITLE, lpDisplayName);
  4911. LocalFree (lpDisplayName);
  4912. }
  4913. }
  4914. else
  4915. {
  4916. SetDlgItemText (hDlg, IDC_TITLE, pGPO->m_pDisplayName);
  4917. }
  4918. if (pGPO->m_gpoType == GPOTypeDS)
  4919. {
  4920. if (IsForest(pGPO->m_pDSPath))
  4921. {
  4922. LoadString (g_hInstance, IDS_FORESTHEADING, szBuffer, ARRAYSIZE(szBuffer));
  4923. SetDlgItemText (hDlg, IDC_DOMAIN_HEADING, szBuffer);
  4924. }
  4925. pszDomain = GetDomainFromLDAPPath(pGPO->m_pDSPath);
  4926. if (pszDomain)
  4927. {
  4928. if (SUCCEEDED(ConvertToDotStyle (pszDomain, &lpResult)))
  4929. {
  4930. SetDlgItemText (hDlg, IDC_DOMAIN, lpResult);
  4931. LocalFree (lpResult);
  4932. }
  4933. delete [] pszDomain;
  4934. }
  4935. SetDlgItemText (hDlg, IDC_UNIQUE_NAME, pGPO->m_pName);
  4936. }
  4937. else
  4938. {
  4939. LoadString (g_hInstance, IDS_NOTAPPLICABLE, szBuffer, ARRAYSIZE(szBuffer));
  4940. SetDlgItemText (hDlg, IDC_DOMAIN, szBuffer);
  4941. SetDlgItemText (hDlg, IDC_UNIQUE_NAME, szBuffer);
  4942. }
  4943. if (SUCCEEDED(pGPO->GetOptions(&dwTemp)))
  4944. {
  4945. if (dwTemp & GPO_OPTION_DISABLE_MACHINE)
  4946. {
  4947. CheckDlgButton (hDlg, IDC_DISABLE_COMPUTER, BST_CHECKED);
  4948. }
  4949. if (dwTemp & GPO_OPTION_DISABLE_USER)
  4950. {
  4951. CheckDlgButton (hDlg, IDC_DISABLE_USER, BST_CHECKED);
  4952. }
  4953. }
  4954. lstrcpy (szBuffer, pGPO->m_pFileSysPath);
  4955. lpEnd = CheckSlash (szBuffer);
  4956. lstrcpy (lpEnd, TEXT("gpt.ini"));
  4957. if (pGPO->m_gpoType == GPOTypeDS)
  4958. {
  4959. VariantInit(&var);
  4960. bstrName = SysAllocString (GPO_VERSION_PROPERTY);
  4961. if (bstrName)
  4962. {
  4963. if (SUCCEEDED(pGPO->m_pADs->Get(bstrName, &var)))
  4964. {
  4965. ulVersion = var.lVal;
  4966. }
  4967. SysFreeString (bstrName);
  4968. }
  4969. VariantClear (&var);
  4970. }
  4971. else
  4972. {
  4973. ulVersion = GetPrivateProfileInt(TEXT("General"), TEXT("Version"), 0, szBuffer);
  4974. }
  4975. uMachine = (USHORT) LOWORD(ulVersion);
  4976. uUser = (USHORT) HIWORD(ulVersion);
  4977. LoadString (g_hInstance, IDS_REVISIONFORMAT, szFormat, ARRAYSIZE(szFormat));
  4978. wsprintf (szVersion, szFormat, uMachine, uUser);
  4979. SetDlgItemText (hDlg, IDC_REVISION, szVersion);
  4980. //
  4981. // Get the date / time info
  4982. //
  4983. CreateTime.dwLowDateTime = 0;
  4984. CreateTime.dwHighDateTime = 0;
  4985. ChangeTime.dwLowDateTime = 0;
  4986. ChangeTime.dwHighDateTime = 0;
  4987. if (pGPO->m_gpoType == GPOTypeDS)
  4988. {
  4989. //
  4990. // Get the creation time
  4991. //
  4992. VariantInit(&var);
  4993. bstrName = SysAllocString (TEXT("whenCreated"));
  4994. if (bstrName)
  4995. {
  4996. if (SUCCEEDED(pGPO->m_pADs->Get(bstrName, &var)))
  4997. {
  4998. if (VariantTimeToDosDateTime (var.date, &wDosDate, &wDosTime))
  4999. {
  5000. DosDateTimeToFileTime (wDosDate, wDosTime, &CreateTime);
  5001. }
  5002. }
  5003. SysFreeString (bstrName);
  5004. }
  5005. VariantClear (&var);
  5006. //
  5007. // Get the last write time
  5008. //
  5009. VariantInit(&var);
  5010. bstrName = SysAllocString (TEXT("whenChanged"));
  5011. if (bstrName)
  5012. {
  5013. if (SUCCEEDED(pGPO->m_pADs->Get(bstrName, &var)))
  5014. {
  5015. if (VariantTimeToDosDateTime (var.date, &wDosDate, &wDosTime))
  5016. {
  5017. DosDateTimeToFileTime (wDosDate, wDosTime, &ChangeTime);
  5018. }
  5019. }
  5020. SysFreeString (bstrName);
  5021. }
  5022. VariantClear (&var);
  5023. }
  5024. else
  5025. {
  5026. //
  5027. // Get the time info from the gpt.ini file
  5028. //
  5029. if (GetFileAttributesEx (szBuffer, GetFileExInfoStandard, &fad))
  5030. {
  5031. CreateTime.dwLowDateTime = fad.ftCreationTime.dwLowDateTime;
  5032. CreateTime.dwHighDateTime = fad.ftCreationTime.dwHighDateTime;
  5033. ChangeTime.dwLowDateTime = fad.ftLastWriteTime.dwLowDateTime;
  5034. ChangeTime.dwHighDateTime = fad.ftLastWriteTime.dwHighDateTime;
  5035. }
  5036. }
  5037. //
  5038. // Format & display the date / time information
  5039. //
  5040. FileTimeToLocalFileTime (&CreateTime, &filetime);
  5041. FileTimeToSystemTime (&filetime, &systime);
  5042. GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &systime,
  5043. NULL, szDate, ARRAYSIZE (szDate));
  5044. GetTimeFormat (LOCALE_USER_DEFAULT, 0, &systime,
  5045. NULL, szTime, ARRAYSIZE (szTime));
  5046. LoadString (g_hInstance, IDS_DATETIMEFORMAT, szFormat, ARRAYSIZE(szFormat));
  5047. wsprintf (szBuffer, szFormat, szDate, szTime);
  5048. SetDlgItemText (hDlg, IDC_CREATE_DATE, szBuffer);
  5049. FileTimeToLocalFileTime (&ChangeTime, &filetime);
  5050. FileTimeToSystemTime (&filetime, &systime);
  5051. GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &systime,
  5052. NULL, szDate, ARRAYSIZE (szDate));
  5053. GetTimeFormat (LOCALE_USER_DEFAULT, 0, &systime,
  5054. NULL, szTime, ARRAYSIZE (szTime));
  5055. wsprintf (szBuffer, szFormat, szDate, szTime);
  5056. SetDlgItemText (hDlg, IDC_MODIFIED_DATE, szBuffer);
  5057. if (pGPO->m_dwFlags & GPO_OPEN_READ_ONLY)
  5058. {
  5059. EnableWindow (GetDlgItem(hDlg, IDC_DISABLE_COMPUTER), FALSE);
  5060. EnableWindow (GetDlgItem(hDlg, IDC_DISABLE_USER), FALSE);
  5061. }
  5062. bDirty = FALSE;
  5063. bDisableWarningIssued = FALSE;
  5064. break;
  5065. }
  5066. case WM_COMMAND:
  5067. if (HIWORD(wParam) == BN_CLICKED)
  5068. {
  5069. if ((LOWORD(wParam) == IDC_DISABLE_COMPUTER) ||
  5070. (LOWORD(wParam) == IDC_DISABLE_USER))
  5071. {
  5072. if (!bDisableWarningIssued)
  5073. {
  5074. if (IsDlgButtonChecked (hDlg, LOWORD(wParam)) == BST_CHECKED)
  5075. {
  5076. TCHAR szMessage[200];
  5077. TCHAR szTitle[100];
  5078. bDisableWarningIssued = TRUE;
  5079. LoadString (g_hInstance, IDS_CONFIRMDISABLE, szMessage, ARRAYSIZE(szMessage));
  5080. LoadString (g_hInstance, IDS_CONFIRMTITLE2, szTitle, ARRAYSIZE(szTitle));
  5081. if (MessageBox (hDlg, szMessage, szTitle, MB_YESNO |
  5082. MB_ICONWARNING | MB_DEFBUTTON2) == IDNO) {
  5083. CheckDlgButton (hDlg, LOWORD(wParam), BST_UNCHECKED);
  5084. break;
  5085. }
  5086. }
  5087. }
  5088. }
  5089. if (!bDirty)
  5090. {
  5091. SendMessage (GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0);
  5092. bDirty = TRUE;
  5093. }
  5094. }
  5095. break;
  5096. case WM_NOTIFY:
  5097. pGPO = (CGroupPolicyObject *) GetWindowLongPtr (hDlg, DWLP_USER);
  5098. if (!pGPO) {
  5099. break;
  5100. }
  5101. switch (((NMHDR FAR*)lParam)->code)
  5102. {
  5103. case PSN_APPLY:
  5104. {
  5105. if (bDirty)
  5106. {
  5107. DWORD dwTemp = 0;
  5108. HRESULT hr;
  5109. //
  5110. // Set the disable flags in the GPO
  5111. //
  5112. if (IsDlgButtonChecked (hDlg, IDC_DISABLE_COMPUTER) == BST_CHECKED)
  5113. {
  5114. dwTemp |= GPO_OPTION_DISABLE_MACHINE;
  5115. }
  5116. if (IsDlgButtonChecked (hDlg, IDC_DISABLE_USER) == BST_CHECKED)
  5117. {
  5118. dwTemp |= GPO_OPTION_DISABLE_USER;
  5119. }
  5120. hr = pGPO->SetOptions (dwTemp, (GPO_OPTION_DISABLE_MACHINE | GPO_OPTION_DISABLE_USER));
  5121. if (FAILED(hr))
  5122. {
  5123. ReportError(hDlg, hr, IDS_FAILEDPROPERTIES);
  5124. SetWindowLongPtr (hDlg, DWLP_MSGRESULT, PSNRET_INVALID);
  5125. return TRUE;
  5126. }
  5127. bDirty = FALSE;
  5128. }
  5129. }
  5130. // fall through...
  5131. case PSN_RESET:
  5132. SetWindowLongPtr (hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
  5133. return TRUE;
  5134. }
  5135. break;
  5136. case WM_HELP: // F1
  5137. WinHelp((HWND)((LPHELPINFO) lParam)->hItemHandle, HELP_FILE, HELP_WM_HELP,
  5138. (ULONG_PTR) (LPSTR) aPropertiesHelpIds);
  5139. break;
  5140. case WM_CONTEXTMENU: // right mouse click
  5141. WinHelp((HWND) wParam, HELP_FILE, HELP_CONTEXTMENU,
  5142. (ULONG_PTR) (LPSTR) aPropertiesHelpIds);
  5143. return (TRUE);
  5144. }
  5145. return FALSE;
  5146. }
  5147. ///////////////////////////////////////////////////////////////////////////////
  5148. // //
  5149. // Class factory object implementation //
  5150. // //
  5151. ///////////////////////////////////////////////////////////////////////////////
  5152. CGroupPolicyObjectCF::CGroupPolicyObjectCF()
  5153. {
  5154. m_cRef = 1;
  5155. InterlockedIncrement(&g_cRefThisDll);
  5156. }
  5157. CGroupPolicyObjectCF::~CGroupPolicyObjectCF()
  5158. {
  5159. InterlockedDecrement(&g_cRefThisDll);
  5160. }
  5161. ///////////////////////////////////////////////////////////////////////////////
  5162. // //
  5163. // Class factory object implementation (IUnknown) //
  5164. // //
  5165. ///////////////////////////////////////////////////////////////////////////////
  5166. STDMETHODIMP_(ULONG)
  5167. CGroupPolicyObjectCF::AddRef()
  5168. {
  5169. return ++m_cRef;
  5170. }
  5171. STDMETHODIMP_(ULONG)
  5172. CGroupPolicyObjectCF::Release()
  5173. {
  5174. if (--m_cRef == 0)
  5175. {
  5176. delete this;
  5177. return 0;
  5178. }
  5179. return m_cRef;
  5180. }
  5181. STDMETHODIMP
  5182. CGroupPolicyObjectCF::QueryInterface(REFIID riid, LPVOID FAR* ppv)
  5183. {
  5184. if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IClassFactory))
  5185. {
  5186. *ppv = (LPCLASSFACTORY)this;
  5187. m_cRef++;
  5188. return S_OK;
  5189. }
  5190. else
  5191. {
  5192. *ppv = NULL;
  5193. return E_NOINTERFACE;
  5194. }
  5195. }
  5196. ///////////////////////////////////////////////////////////////////////////////
  5197. // //
  5198. // Class factory object implementation (IClassFactory) //
  5199. // //
  5200. ///////////////////////////////////////////////////////////////////////////////
  5201. STDMETHODIMP
  5202. CGroupPolicyObjectCF::CreateInstance(LPUNKNOWN pUnkOuter,
  5203. REFIID riid,
  5204. LPVOID FAR* ppvObj)
  5205. {
  5206. *ppvObj = NULL;
  5207. if (pUnkOuter)
  5208. return CLASS_E_NOAGGREGATION;
  5209. CGroupPolicyObject *pGroupPolicyObject = new CGroupPolicyObject(); // ref count == 1
  5210. if (!pGroupPolicyObject)
  5211. return E_OUTOFMEMORY;
  5212. HRESULT hr = pGroupPolicyObject->QueryInterface(riid, ppvObj);
  5213. pGroupPolicyObject->Release(); // release initial ref
  5214. return hr;
  5215. }
  5216. STDMETHODIMP
  5217. CGroupPolicyObjectCF::LockServer(BOOL fLock)
  5218. {
  5219. return E_NOTIMPL;
  5220. }
  5221. //*************************************************************
  5222. //
  5223. // CGroupPolicyObject::GetProperty
  5224. //
  5225. // Purpose: Retrieves a property from DS or from gpt.ini
  5226. //
  5227. // Parameters: pszProp - Property to get
  5228. // xValueIn - Value returned here
  5229. //
  5230. // Returns: S_OK if successful
  5231. //
  5232. //*************************************************************
  5233. HRESULT CGroupPolicyObject::GetProperty( TCHAR *pszProp, XPtrST<TCHAR>& xValueIn )
  5234. {
  5235. HRESULT hr = E_FAIL;
  5236. if ( m_gpoType == GPOTypeDS )
  5237. {
  5238. VARIANT var;
  5239. BSTR bstrProperty;
  5240. VariantInit( &var );
  5241. bstrProperty = SysAllocString( pszProp );
  5242. if ( bstrProperty == NULL )
  5243. return E_OUTOFMEMORY;
  5244. hr = m_pADs->Get( bstrProperty, &var );
  5245. if ( SUCCEEDED(hr) )
  5246. {
  5247. TCHAR *pszValue = new TCHAR[lstrlen(var.bstrVal) + 1];
  5248. if ( pszValue == 0 )
  5249. hr = E_OUTOFMEMORY;
  5250. else
  5251. {
  5252. lstrcpy( pszValue, var.bstrVal );
  5253. xValueIn.Set( pszValue );
  5254. hr = S_OK;
  5255. }
  5256. } else if ( hr == E_ADS_PROPERTY_NOT_FOUND )
  5257. {
  5258. //
  5259. // Property has not be written out before
  5260. //
  5261. hr = S_OK;
  5262. }
  5263. if ( FAILED(hr) ) {
  5264. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetProperty: Failed with errorcode 0x%x"), hr));
  5265. }
  5266. SysFreeString( bstrProperty );
  5267. VariantClear( &var );
  5268. return hr;
  5269. }
  5270. else
  5271. {
  5272. TCHAR szPath[2*MAX_PATH];
  5273. //
  5274. // Get the file system path
  5275. //
  5276. hr = GetPath (szPath, ARRAYSIZE(szPath));
  5277. if ( FAILED(hr) ) {
  5278. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetProperty: Failed with errorcode 0x%x"), hr));
  5279. }
  5280. LPTSTR lpEnd = CheckSlash (szPath);
  5281. lstrcpy (lpEnd, TEXT("GPT.INI"));
  5282. XPtrST<TCHAR> xszValue( new TCHAR[2*MAX_PATH] );
  5283. if ( xszValue.GetPointer() == NULL )
  5284. return E_OUTOFMEMORY;
  5285. DWORD dwSize = (2*MAX_PATH);
  5286. DWORD dwCount = GetPrivateProfileString( TEXT("General"),
  5287. pszProp,
  5288. TEXT(""),
  5289. xszValue.GetPointer(),
  5290. dwSize,
  5291. szPath );
  5292. while ( dwCount == dwSize - 1 )
  5293. {
  5294. //
  5295. // Value has been truncated, so retry with larger buffer
  5296. //
  5297. dwSize *= 2;
  5298. delete xszValue.Acquire();
  5299. xszValue.Set( new TCHAR[dwSize] );
  5300. if ( xszValue.GetPointer() == NULL )
  5301. return E_OUTOFMEMORY;
  5302. dwCount = GetPrivateProfileString( TEXT("General"),
  5303. pszProp,
  5304. TEXT(""),
  5305. xszValue.GetPointer(),
  5306. dwSize,
  5307. szPath );
  5308. }
  5309. xValueIn.Set( xszValue.Acquire() );
  5310. return S_OK;
  5311. }
  5312. }
  5313. //*************************************************************
  5314. //
  5315. // CGroupPolicyObject::SetProperty
  5316. //
  5317. // Purpose: Writes a property to DS or to gpt.ini
  5318. //
  5319. // Parameters: pszProp - Property to set
  5320. // pszPropValue - Property value
  5321. //
  5322. // Returns: S_OK if successful
  5323. //
  5324. //*************************************************************
  5325. HRESULT CGroupPolicyObject::SetProperty( TCHAR *pszProp, TCHAR *pszPropValue )
  5326. {
  5327. HRESULT hr = E_FAIL;
  5328. if ( m_gpoType == GPOTypeDS )
  5329. {
  5330. VARIANT var;
  5331. VariantInit( &var );
  5332. var.vt = VT_BSTR;
  5333. var.bstrVal = SysAllocString( pszPropValue );
  5334. if ( var.bstrVal == 0 )
  5335. return E_OUTOFMEMORY;
  5336. BSTR bstrProperty = SysAllocString( pszProp );
  5337. if ( bstrProperty == 0 )
  5338. {
  5339. VariantClear( &var );
  5340. return E_OUTOFMEMORY;
  5341. }
  5342. hr = m_pADs->Put( bstrProperty, var );
  5343. if ( FAILED(hr) )
  5344. {
  5345. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetProperty: Failed with errorcode 0x%x"), hr));
  5346. return hr;
  5347. }
  5348. SysFreeString( bstrProperty );
  5349. VariantClear( &var );
  5350. return S_OK;
  5351. }
  5352. else
  5353. {
  5354. TCHAR szPath[2*MAX_PATH];
  5355. //
  5356. // Get the file system path
  5357. //
  5358. hr = GetPath (szPath, ARRAYSIZE(szPath));
  5359. if ( FAILED(hr) ) {
  5360. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::SetProperty: Failed with errorcode 0x%x"), hr));
  5361. }
  5362. LPTSTR lpEnd = CheckSlash (szPath);
  5363. lstrcpy (lpEnd, TEXT("GPT.INI"));
  5364. BOOL bOk = WritePrivateProfileString( TEXT("General"),
  5365. pszProp,
  5366. pszPropValue,
  5367. szPath );
  5368. if ( bOk )
  5369. hr = S_OK;
  5370. else
  5371. hr = GetLastError();
  5372. return hr;
  5373. }
  5374. }