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.

3868 lines
108 KiB

  1. //*************************************************************
  2. // File name: GPMGR.C
  3. //
  4. // Description: Group Policy Manager - property sheet extension
  5. // for DS Admin
  6. //
  7. //
  8. // Microsoft Confidential
  9. // Copyright (c) Microsoft Corporation 1998
  10. // All rights reserved
  11. //
  12. //*************************************************************
  13. #include "main.h"
  14. unsigned int CGroupPolicyMgr::m_cfDSObjectName = RegisterClipboardFormat(CFSTR_DSOBJECTNAMES);
  15. unsigned int CGroupPolicyMgr::m_cfNodeTypeString = RegisterClipboardFormat(CCF_SZNODETYPE);
  16. //
  17. // Snapin manager's CLSID
  18. //
  19. const CLSID CLSID_NodeInit = {0x43136EB5,0xD36C,0x11CF,{0xAD,0xBC,0x00,0xAA,0x00,0xA8,0x00,0x33}};
  20. //
  21. // CheckMark string
  22. //
  23. TCHAR szCheckMark[] = TEXT("a"); // In the Marlett font, "a" becomes a check mark
  24. TCHAR szNoCheckMark[] = TEXT("");
  25. //
  26. // Help ids
  27. //
  28. DWORD aGroupPolicyMgrHelpIds[] =
  29. {
  30. IDC_GPM_DCNAME, IDH_GPMGR_DCNAME,
  31. IDC_GPM_LIST, IDH_GPMGR_LIST,
  32. IDC_GPM_UP, IDH_GPMGR_UP,
  33. IDC_GPM_DOWN, IDH_GPMGR_DOWN,
  34. IDC_GPM_ADD, IDH_GPMGR_ADD,
  35. IDC_GPM_EDIT, IDH_GPMGR_EDIT,
  36. IDC_GPM_DELETE, IDH_GPMGR_DELETE,
  37. IDC_GPM_PROPERTIES, IDH_GPMGR_PROPERTIES,
  38. IDC_GPM_BLOCK, IDH_GPMGR_BLOCK,
  39. IDC_GPM_NEW, IDH_GPMGR_NEW,
  40. IDC_GPM_OPTIONS, IDH_GPMGR_OPTIONS,
  41. IDC_GPM_TITLE, -1,
  42. IDC_GPM_ICON, -1,
  43. IDC_GPM_LINE1, -1,
  44. IDC_GPM_PRIORITY, -1,
  45. IDC_GPM_LINE2, -1,
  46. 0, 0
  47. };
  48. DWORD aLinkOptionsHelpIds[] =
  49. {
  50. IDC_GPM_NOOVERRIDE, IDH_GPMGR_NOOVERRIDE,
  51. IDC_GPM_DISABLED, IDH_GPMGR_DISABLED,
  52. 0, 0
  53. };
  54. DWORD aRemoveGPOHelpIds[] =
  55. {
  56. IDC_REMOVE_LIST, IDH_REMOVE_LIST,
  57. IDC_REMOVE_DS, IDH_REMOVE_DS,
  58. 0, 0
  59. };
  60. ///////////////////////////////////////////////////////////////////////////////
  61. // //
  62. // CGroupPolicyMgr object implementation //
  63. // //
  64. ///////////////////////////////////////////////////////////////////////////////
  65. CGroupPolicyMgr::CGroupPolicyMgr()
  66. {
  67. InterlockedIncrement(&g_cRefThisDll);
  68. m_cRef = 1;
  69. m_lpDSObject = NULL;
  70. m_lpGPODCName = NULL;
  71. m_lpDSADCName = NULL;
  72. m_lpDomainName = NULL;
  73. m_hDefaultFont = NULL;
  74. m_hMarlettFont = NULL;
  75. m_bReadOnly = FALSE;
  76. m_bDirty = FALSE;
  77. m_gpHint = GPHintUnknown;
  78. }
  79. CGroupPolicyMgr::~CGroupPolicyMgr()
  80. {
  81. if (m_hMarlettFont)
  82. {
  83. DeleteObject (m_hMarlettFont);
  84. }
  85. if (m_lpDSObject)
  86. {
  87. LocalFree (m_lpDSObject);
  88. }
  89. if (m_lpGPODCName)
  90. {
  91. LocalFree (m_lpGPODCName);
  92. }
  93. if (m_lpDSADCName)
  94. {
  95. LocalFree (m_lpDSADCName);
  96. }
  97. if (m_lpDomainName)
  98. {
  99. LocalFree (m_lpDomainName);
  100. }
  101. InterlockedDecrement(&g_cRefThisDll);
  102. }
  103. ///////////////////////////////////////////////////////////////////////////////
  104. // //
  105. // CGroupPolicyMgr object implementation (IUnknown) //
  106. // //
  107. ///////////////////////////////////////////////////////////////////////////////
  108. HRESULT CGroupPolicyMgr::QueryInterface (REFIID riid, void **ppv)
  109. {
  110. if (IsEqualIID(riid, IID_IUnknown) ||IsEqualIID(riid, IID_IExtendPropertySheet))
  111. {
  112. *ppv = (LPEXTENDPROPERTYSHEET)this;
  113. m_cRef++;
  114. return S_OK;
  115. }
  116. else if (IsEqualIID(riid, IID_ISnapinHelp))
  117. {
  118. *ppv = (LPSNAPINHELP)this;
  119. m_cRef++;
  120. return S_OK;
  121. }
  122. else
  123. {
  124. *ppv = NULL;
  125. return E_NOINTERFACE;
  126. }
  127. }
  128. ULONG CGroupPolicyMgr::AddRef (void)
  129. {
  130. return ++m_cRef;
  131. }
  132. ULONG CGroupPolicyMgr::Release (void)
  133. {
  134. if (--m_cRef == 0) {
  135. delete this;
  136. return 0;
  137. }
  138. return m_cRef;
  139. }
  140. ///////////////////////////////////////////////////////////////////////////////
  141. // //
  142. // CGroupPolicyMgr object implementation (ISnapinHelp) //
  143. // //
  144. ///////////////////////////////////////////////////////////////////////////////
  145. STDMETHODIMP CGroupPolicyMgr::GetHelpTopic(LPOLESTR *lpCompiledHelpFile)
  146. {
  147. LPOLESTR lpHelpFile;
  148. lpHelpFile = (LPOLESTR) CoTaskMemAlloc (MAX_PATH * sizeof(WCHAR));
  149. if (!lpHelpFile)
  150. {
  151. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::GetHelpTopic: Failed to allocate memory.")));
  152. return E_OUTOFMEMORY;
  153. }
  154. ExpandEnvironmentStringsW (L"%SystemRoot%\\Help\\gpedit.chm",
  155. lpHelpFile, MAX_PATH);
  156. *lpCompiledHelpFile = lpHelpFile;
  157. return S_OK;
  158. }
  159. ///////////////////////////////////////////////////////////////////////////////
  160. // //
  161. // CGroupPolicyMgr object implementation (IExtendPropertySheet) //
  162. // //
  163. ///////////////////////////////////////////////////////////////////////////////
  164. STDMETHODIMP CGroupPolicyMgr::CreatePropertyPages(LPPROPERTYSHEETCALLBACK lpProvider,
  165. LONG_PTR handle, LPDATAOBJECT lpDataObject)
  166. {
  167. HRESULT hr;
  168. PROPSHEETPAGE psp;
  169. HPROPSHEETPAGE hPage;
  170. FORMATETC fm;
  171. STGMEDIUM medium;
  172. LPDSOBJECTNAMES lpNames;
  173. LPTSTR lpTemp;
  174. //
  175. // Ask DS admin for the ldap path to the selected object
  176. //
  177. ZeroMemory (&fm, sizeof(fm));
  178. fm.cfFormat = (WORD)m_cfDSObjectName;
  179. fm.tymed = TYMED_HGLOBAL;
  180. ZeroMemory (&medium, sizeof(medium));
  181. medium.tymed = TYMED_HGLOBAL;
  182. medium.hGlobal = GlobalAlloc (GMEM_MOVEABLE | GMEM_NODISCARD, 512);
  183. if (medium.hGlobal)
  184. {
  185. hr = lpDataObject->GetData(&fm, &medium);
  186. if (SUCCEEDED(hr))
  187. {
  188. lpNames = (LPDSOBJECTNAMES) GlobalLock (medium.hGlobal);
  189. lpTemp = (LPWSTR) (((LPBYTE)lpNames) + lpNames->aObjects[0].offsetName);
  190. if (m_lpDSObject)
  191. {
  192. LocalFree (m_lpDSObject);
  193. }
  194. m_lpDSObject = (LPTSTR) LocalAlloc (LPTR, (lstrlen (lpTemp) + 1) * sizeof(TCHAR));
  195. if (m_lpDSObject)
  196. {
  197. lstrcpy (m_lpDSObject, lpTemp);
  198. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyMgr::CreatePropertyPages: LDAP path from DS Admin %s"), m_lpDSObject));
  199. //
  200. // Now look at the object type to get a hint type
  201. //
  202. m_gpHint = GPHintUnknown;
  203. if (lpNames->aObjects[0].offsetClass) {
  204. lpTemp = (LPWSTR) (((LPBYTE)lpNames) + lpNames->aObjects[0].offsetClass);
  205. if (lstrcmpi (lpTemp, TEXT("domainDNS")) == 0)
  206. {
  207. m_gpHint = GPHintDomain;
  208. }
  209. else if (lstrcmpi (lpTemp, TEXT("organizationalUnit")) == 0)
  210. {
  211. m_gpHint = GPHintOrganizationalUnit;
  212. }
  213. else if (lstrcmpi (lpTemp, TEXT("site")) == 0)
  214. {
  215. m_gpHint = GPHintSite;
  216. }
  217. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyMgr::CreatePropertyPages: m_gpHint = %d"), m_gpHint));
  218. } else {
  219. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyMgr::CreatePropertyPages: No objectclass defined.")));
  220. }
  221. }
  222. else
  223. {
  224. hr = HRESULT_FROM_WIN32(GetLastError());
  225. }
  226. GlobalUnlock (medium.hGlobal);
  227. }
  228. #if FGPO_SUPPORT
  229. else
  230. {
  231. // ToDo: remove this code after the dstree snapin supports
  232. // dsobjectnames on its root node
  233. //
  234. // if we're supposed to be on the forest then we can just fudge it
  235. //
  236. fm.cfFormat = (WORD)m_cfNodeTypeString;
  237. if (SUCCEEDED(lpDataObject->GetDataHere(&fm, &medium)))
  238. {
  239. lpTemp = (LPWSTR) GlobalLock (medium.hGlobal);
  240. if (lstrcmpi(lpTemp, TEXT("{4c06495e-a241-11d0-b09b-00c04fd8dca6}"))) // DSTree snapin's root node
  241. {
  242. // croft up our own forest path
  243. lpTemp = GetPathToForest(NULL);
  244. if (lpTemp)
  245. {
  246. m_lpDSObject = (LPTSTR) LocalAlloc(LPTR, (lstrlen(lpTemp) + 1) * sizeof(TCHAR));
  247. if (m_lpDSObject)
  248. {
  249. lstrcpy (m_lpDSObject, lpTemp);
  250. m_gpHint = GPHintForest;
  251. hr = S_OK;
  252. }
  253. delete [] lpTemp;
  254. }
  255. }
  256. GlobalUnlock (medium.hGlobal);
  257. }
  258. }
  259. #endif
  260. GlobalFree (medium.hGlobal);
  261. }
  262. else
  263. {
  264. hr = HRESULT_FROM_WIN32(GetLastError());
  265. }
  266. //
  267. // Add the GPM property sheet page
  268. //
  269. if (SUCCEEDED(hr))
  270. {
  271. ZeroMemory (&psp, sizeof(psp));
  272. psp.dwSize = sizeof(psp);
  273. psp.dwFlags = PSP_USECALLBACK;
  274. psp.hInstance = g_hInstance;
  275. psp.pszTemplate = MAKEINTRESOURCE (IDD_GPMANAGER);
  276. psp.pfnDlgProc = GPMDlgProc;
  277. psp.lParam = (LPARAM) this;
  278. psp.pfnCallback = PropSheetPageCallback;
  279. hPage = CreatePropertySheetPage(&psp);
  280. if (hPage)
  281. {
  282. hr = lpProvider->AddPage(hPage);
  283. }
  284. else
  285. {
  286. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::CreatePropertyPages: Failed to create property sheet page with %d."),
  287. GetLastError()));
  288. hr = E_FAIL;
  289. }
  290. }
  291. return (hr);
  292. }
  293. STDMETHODIMP CGroupPolicyMgr::QueryPagesFor(LPDATAOBJECT lpDataObject)
  294. {
  295. return S_OK;
  296. }
  297. UINT CALLBACK CGroupPolicyMgr::PropSheetPageCallback(HWND hwnd, UINT uMsg, LPPROPSHEETPAGE ppsp)
  298. {
  299. CGroupPolicyMgr * pGPM;
  300. pGPM = (CGroupPolicyMgr *) ppsp->lParam;
  301. if (uMsg == PSPCB_ADDREF)
  302. {
  303. pGPM->AddRef();
  304. }
  305. else if (uMsg == PSPCB_RELEASE)
  306. {
  307. pGPM->Release();
  308. }
  309. return 1;
  310. }
  311. INT_PTR CALLBACK CGroupPolicyMgr::GPMDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  312. {
  313. CGroupPolicyMgr * pGPM;
  314. static BOOL bDisableWarningIssued;
  315. switch (message)
  316. {
  317. case WM_INITDIALOG:
  318. pGPM = (CGroupPolicyMgr *) (((LPPROPSHEETPAGE)lParam)->lParam);
  319. SetWindowLongPtr (hDlg, DWLP_USER, (LONG_PTR) pGPM);
  320. SetWaitCursor();
  321. bDisableWarningIssued = FALSE;
  322. EnableWindow (GetDlgItem (hDlg, IDC_GPM_DELETE), FALSE);
  323. EnableWindow (GetDlgItem (hDlg, IDC_GPM_EDIT), FALSE);
  324. EnableWindow (GetDlgItem (hDlg, IDC_GPM_UP), FALSE);
  325. EnableWindow (GetDlgItem (hDlg, IDC_GPM_DOWN), FALSE);
  326. EnableWindow (GetDlgItem (hDlg, IDC_GPM_OPTIONS), FALSE);
  327. EnableWindow (GetDlgItem (hDlg, IDC_GPM_PROPERTIES), FALSE);
  328. if (!pGPM->OnInitDialog(hDlg))
  329. {
  330. EnableWindow (GetDlgItem (hDlg, IDC_GPM_LIST), FALSE);
  331. EnableWindow (GetDlgItem (hDlg, IDC_GPM_NEW), FALSE);
  332. EnableWindow (GetDlgItem (hDlg, IDC_GPM_ADD), FALSE);
  333. EnableWindow (GetDlgItem (hDlg, IDC_GPM_BLOCK), FALSE);
  334. }
  335. if (pGPM->m_bReadOnly)
  336. {
  337. EnableWindow (GetDlgItem (hDlg, IDC_GPM_NEW), FALSE);
  338. EnableWindow (GetDlgItem (hDlg, IDC_GPM_ADD), FALSE);
  339. EnableWindow (GetDlgItem (hDlg, IDC_GPM_DELETE), FALSE);
  340. EnableWindow (GetDlgItem (hDlg, IDC_GPM_UP), FALSE);
  341. EnableWindow (GetDlgItem (hDlg, IDC_GPM_DOWN), FALSE);
  342. EnableWindow (GetDlgItem (hDlg, IDC_GPM_OPTIONS), FALSE);
  343. EnableWindow (GetDlgItem (hDlg, IDC_GPM_BLOCK), FALSE);
  344. }
  345. #if FGPO_SUPPORT
  346. if (pGPM->m_gpHint == GPHintForest)
  347. #else
  348. if (pGPM->m_gpHint == GPHintSite)
  349. #endif
  350. {
  351. EnableWindow (GetDlgItem (hDlg, IDC_GPM_BLOCK), FALSE);
  352. }
  353. ClearWaitCursor();
  354. break;
  355. case WM_COMMAND:
  356. pGPM = (CGroupPolicyMgr *) GetWindowLongPtr (hDlg, DWLP_USER);
  357. if (!pGPM) {
  358. break;
  359. }
  360. if ((LOWORD(wParam) == IDC_GPM_ADD) || (LOWORD(wParam) == IDM_GPM_ADD))
  361. {
  362. TCHAR szPath[512];
  363. TCHAR szName[MAX_FRIENDLYNAME];
  364. TCHAR szTitle[100];
  365. LPTSTR lpNamelessPath;
  366. GPOBROWSEINFO stGBI;
  367. IADs * pADs;
  368. HRESULT hr;
  369. BOOL bReadOnly = FALSE;
  370. LoadString (g_hInstance, IDS_GPM_ADDTITLE, szTitle, ARRAYSIZE(szTitle));
  371. ZeroMemory(&stGBI, sizeof(GPOBROWSEINFO));
  372. stGBI.dwSize = sizeof(GPOBROWSEINFO);
  373. stGBI.dwFlags = GPO_BROWSE_NOCOMPUTERS | GPO_BROWSE_DISABLENEW;
  374. stGBI.hwndOwner = hDlg;
  375. stGBI.lpTitle = szTitle;
  376. stGBI.lpInitialOU = pGPM->m_lpDSObject;
  377. stGBI.lpDSPath = szPath;
  378. stGBI.dwDSPathSize = ARRAYSIZE(szPath);
  379. stGBI.lpName = szName;
  380. stGBI.dwNameSize = ARRAYSIZE(szName);
  381. if (SUCCEEDED(BrowseForGPO(&stGBI)))
  382. {
  383. //
  384. // Check if the user has write access to the select GPO
  385. //
  386. lpNamelessPath = MakeNamelessPath (szPath);
  387. if (lpNamelessPath)
  388. {
  389. hr = OpenDSObject(szPath, IID_IADs, (void **)&pADs);
  390. if (SUCCEEDED(hr)) {
  391. if (FAILED(CheckDSWriteAccess((LPUNKNOWN) pADs, GPO_VERSION_PROPERTY)))
  392. {
  393. bReadOnly = TRUE;
  394. }
  395. pADs->Release();
  396. }
  397. //
  398. // Read the policy value for the GPO link
  399. //
  400. HKEY hKey;
  401. DWORD dwSize, dwType;
  402. BOOL bDisabledLink = FALSE;
  403. //
  404. // Check if there is a user preference or policy that
  405. // any new GPOs should be created with a disabled link
  406. // by default
  407. //
  408. if (RegOpenKeyEx (HKEY_CURRENT_USER, GPE_KEY, 0,
  409. KEY_READ, &hKey) == ERROR_SUCCESS)
  410. {
  411. dwSize = sizeof(bDisabledLink);
  412. RegQueryValueEx (hKey, NEW_LINKS_DISABLED_VALUE, NULL, &dwType,
  413. (LPBYTE) &bDisabledLink, &dwSize);
  414. RegCloseKey (hKey);
  415. }
  416. if (RegOpenKeyEx (HKEY_CURRENT_USER, GPE_POLICIES_KEY, 0,
  417. KEY_READ, &hKey) == ERROR_SUCCESS)
  418. {
  419. dwSize = sizeof(bDisabledLink);
  420. RegQueryValueEx (hKey, NEW_LINKS_DISABLED_VALUE, NULL, &dwType,
  421. (LPBYTE) &bDisabledLink, &dwSize);
  422. RegCloseKey (hKey);
  423. }
  424. if (pGPM->AddGPOToList (GetDlgItem(hDlg, IDC_GPM_LIST),
  425. szName, lpNamelessPath, (bDisabledLink ? GPO_FLAG_DISABLE : 0), FALSE,
  426. pGPM->IsGPODisabled (lpNamelessPath), bReadOnly))
  427. {
  428. pGPM->m_bDirty = TRUE;
  429. SendMessage (GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0);
  430. SetFocus (GetDlgItem(hDlg, IDC_GPM_LIST));
  431. PostMessage (hDlg, WM_REFRESHDISPLAY, 0, 0);
  432. }
  433. LocalFree (lpNamelessPath);
  434. }
  435. }
  436. }
  437. if ((LOWORD(wParam) == IDC_GPM_DELETE) || (LOWORD(wParam) == IDM_GPM_DELETE))
  438. {
  439. INT iIndex, iNext;
  440. HWND hLV = GetDlgItem(hDlg, IDC_GPM_LIST);
  441. LVITEM item;
  442. //
  443. // Enumerate through the selected items
  444. //
  445. iIndex = ListView_GetNextItem (hLV, -1, LVNI_ALL | LVNI_SELECTED);
  446. if (iIndex != -1)
  447. {
  448. HRESULT hr;
  449. LPGPOITEM lpGPO, lpTemp;
  450. LPGROUPPOLICYOBJECT pGPO;
  451. TCHAR szMessageFmt[100];
  452. LPTSTR lpMessage;
  453. TCHAR szTitle[100];
  454. INT iResult;
  455. LPTSTR lpDSPath;
  456. LPTSTR lpFullPath;
  457. //
  458. // The GPO item pointer
  459. //
  460. item.mask = LVIF_PARAM;
  461. item.iItem = iIndex;
  462. item.iSubItem = 0;
  463. if (!ListView_GetItem (hLV, &item))
  464. {
  465. break;
  466. }
  467. lpGPO = (LPGPOITEM) item.lParam;
  468. if ( !lpGPO )
  469. {
  470. break;
  471. }
  472. lpGPO->bLocked = TRUE;
  473. //
  474. // Offer the user a choice of Remove actions
  475. //
  476. iResult = (INT)DialogBoxParam (g_hInstance, MAKEINTRESOURCE(IDD_REMOVE_GPO),
  477. hDlg, RemoveGPODlgProc, (LPARAM) lpGPO);
  478. if ((iResult == -1) || (iResult == 0))
  479. {
  480. SetFocus (hLV);
  481. lpGPO->bLocked = FALSE;
  482. break;
  483. }
  484. iNext = ListView_GetNextItem (hLV, iIndex, LVNI_ALL);
  485. if (iNext > 0)
  486. {
  487. iNext--;
  488. }
  489. else
  490. {
  491. iNext = 0;
  492. }
  493. if (iResult == 1)
  494. {
  495. IADs * pADs;
  496. //
  497. // Bind to the DS object to make sure it's still reachable
  498. //
  499. hr = OpenDSObject(pGPM->m_lpDSObject, IID_IADs, (void **)&pADs);
  500. if (SUCCEEDED(hr))
  501. {
  502. pADs->Release();
  503. ListView_DeleteItem (hLV, iIndex);
  504. }
  505. else
  506. {
  507. DebugMsg((DM_WARNING, TEXT("GPMDlgProc: Failed to get IADs interface with 0x%x"), hr));
  508. ReportError(hDlg, hr, IDS_FAILEDGPINFO);
  509. lpGPO->bLocked = FALSE;
  510. break;
  511. }
  512. }
  513. else if (iResult == 2)
  514. {
  515. //
  516. // Confirm the delete operation
  517. //
  518. LoadString (g_hInstance, IDS_DELETECONFIRM, szMessageFmt, ARRAYSIZE(szMessageFmt));
  519. lpMessage = (LPTSTR) LocalAlloc (LPTR, (lstrlen(szMessageFmt) +
  520. lstrlen(lpGPO->lpDisplayName) + 1) * sizeof(TCHAR));
  521. if (!lpMessage)
  522. {
  523. DebugMsg((DM_WARNING, TEXT("GPMDlgProc: Failed to allocate memory with %d."),
  524. GetLastError()));
  525. SetFocus (hLV);
  526. ClearWaitCursor ();
  527. lpGPO->bLocked = FALSE;
  528. break;
  529. }
  530. wsprintf (lpMessage, szMessageFmt, lpGPO->lpDisplayName);
  531. LoadString (g_hInstance, IDS_CONFIRMTITLE, szTitle, ARRAYSIZE(szTitle));
  532. if (MessageBox (hDlg, lpMessage, szTitle,
  533. MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
  534. {
  535. LocalFree (lpMessage);
  536. SetFocus (hLV);
  537. lpGPO->bLocked = FALSE;
  538. break;
  539. }
  540. LocalFree (lpMessage);
  541. SetWaitCursor ();
  542. lpDSPath = (LPTSTR) LocalAlloc (LPTR, (lstrlen(lpGPO->lpDSPath) + 1) * sizeof(TCHAR));
  543. if (!lpDSPath)
  544. {
  545. DebugMsg((DM_WARNING, TEXT("GPMDlgProc: Failed to allocate memory with %d."),
  546. GetLastError()));
  547. SetFocus (hLV);
  548. ClearWaitCursor ();
  549. lpGPO->bLocked = FALSE;
  550. break;
  551. }
  552. lstrcpy (lpDSPath, lpGPO->lpDSPath);
  553. //
  554. // Create a new GPO object to work with
  555. //
  556. hr = CoCreateInstance (CLSID_GroupPolicyObject, NULL,
  557. CLSCTX_SERVER, IID_IGroupPolicyObject,
  558. (void**)&pGPO);
  559. if (FAILED(hr))
  560. {
  561. DebugMsg((DM_WARNING, TEXT("GPMDlgProc: Failed to create GPO object with 0x%x."), hr));
  562. LocalFree (lpDSPath);
  563. SetFocus (hLV);
  564. ClearWaitCursor ();
  565. lpGPO->bLocked = FALSE;
  566. break;
  567. }
  568. lpFullPath = pGPM->GetFullGPOPath (lpGPO->lpDSPath, hDlg);
  569. if (!lpFullPath)
  570. {
  571. pGPO->Release();
  572. LocalFree (lpDSPath);
  573. SetFocus (hLV);
  574. ClearWaitCursor ();
  575. lpGPO->bLocked = FALSE;
  576. break;
  577. }
  578. //
  579. // Open the requested object without mounting the registry
  580. //
  581. hr = pGPO->OpenDSGPO(lpFullPath, 0);
  582. LocalFree (lpFullPath);
  583. if (FAILED(hr))
  584. {
  585. DebugMsg((DM_WARNING, TEXT("GPMDlgProc: Failed to open GPO object with 0x%x."), hr));
  586. ReportError(hDlg, hr, IDS_FAILEDGPODELETE, lpGPO->lpDisplayName);
  587. pGPO->Release();
  588. LocalFree (lpDSPath);
  589. SetFocus (hLV);
  590. ClearWaitCursor ();
  591. lpGPO->bLocked = FALSE;
  592. break;
  593. }
  594. //
  595. // Delete the object
  596. //
  597. hr = pGPO->Delete();
  598. if (FAILED(hr))
  599. {
  600. DebugMsg((DM_WARNING, TEXT("GPMDlgProc: Failed to delete GPO object with 0x%x."), hr));
  601. ReportError(hDlg, hr, IDS_FAILEDGPODELETE, lpGPO->lpDisplayName);
  602. pGPO->Release();
  603. LocalFree (lpDSPath);
  604. SetFocus (hLV);
  605. ClearWaitCursor ();
  606. lpGPO->bLocked = FALSE;
  607. break;
  608. }
  609. pGPO->Release();
  610. //
  611. // Delete all the entries of this item in the listview
  612. //
  613. iIndex = (ListView_GetItemCount (hLV) - 1);
  614. while (iIndex >= 0)
  615. {
  616. item.mask = LVIF_PARAM;
  617. item.iItem = iIndex;
  618. item.iSubItem = 0;
  619. if (!ListView_GetItem (hLV, &item))
  620. {
  621. lpGPO->bLocked = FALSE;
  622. break;
  623. }
  624. lpTemp = (LPGPOITEM) item.lParam;
  625. if (lpTemp)
  626. {
  627. if (!lstrcmpi(lpTemp->lpDSPath, lpDSPath))
  628. {
  629. if (iNext == iIndex)
  630. {
  631. iNext--;
  632. }
  633. ListView_DeleteItem (hLV, iIndex);
  634. }
  635. }
  636. iIndex--;
  637. }
  638. LocalFree (lpDSPath);
  639. ClearWaitCursor ();
  640. }
  641. if (iNext < 0)
  642. {
  643. iNext = 0;
  644. }
  645. //
  646. // Select the next item
  647. //
  648. item.mask = LVIF_STATE;
  649. item.iItem = iNext;
  650. item.iSubItem = 0;
  651. item.state = LVIS_SELECTED | LVIS_FOCUSED;
  652. item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
  653. SendMessage (hLV, LVM_SETITEMSTATE, iNext, (LPARAM) &item);
  654. pGPM->m_bDirty = TRUE;
  655. if (!pGPM->Save(hDlg))
  656. {
  657. pGPM->RefreshGPM (hDlg, FALSE);
  658. }
  659. SendMessage (GetParent(hDlg), PSM_CANCELTOCLOSE, 0, 0);
  660. SetFocus (hLV);
  661. PostMessage (hDlg, WM_REFRESHDISPLAY, 0, 0);
  662. lpGPO->bLocked = FALSE;
  663. }
  664. }
  665. if (LOWORD(wParam) == IDC_GPM_UP)
  666. {
  667. INT iSrc, iDest, iSrcImage, iDestImage;
  668. LPGPOITEM lpSrc, lpDest;
  669. HWND hLV = GetDlgItem(hDlg, IDC_GPM_LIST);
  670. LVITEM item;
  671. ListView_EditLabel (hLV, -1);
  672. iSrc = ListView_GetNextItem (hLV, -1,
  673. LVNI_ALL | LVNI_SELECTED);
  674. if (iSrc != -1)
  675. {
  676. iDest = iSrc - 1;
  677. //
  678. // Get the current lpGPOItem pointers
  679. //
  680. item.mask = LVIF_PARAM | LVIF_IMAGE;
  681. item.iItem = iSrc;
  682. item.iSubItem = 0;
  683. if (!ListView_GetItem (hLV, &item))
  684. {
  685. break;
  686. }
  687. lpSrc = (LPGPOITEM) item.lParam;
  688. iSrcImage = item.iImage;
  689. item.mask = LVIF_PARAM | LVIF_IMAGE;
  690. item.iItem = iDest;
  691. item.iSubItem = 0;
  692. if (!ListView_GetItem (hLV, &item))
  693. {
  694. break;
  695. }
  696. lpDest = (LPGPOITEM) item.lParam;
  697. iDestImage = item.iImage;
  698. //
  699. // Swap them
  700. //
  701. item.mask = LVIF_PARAM | LVIF_IMAGE;
  702. item.iItem = iSrc;
  703. item.iImage = iDestImage;
  704. item.iSubItem = 0;
  705. item.lParam = (LPARAM)lpDest;
  706. if (!ListView_SetItem (hLV, &item))
  707. {
  708. break;
  709. }
  710. item.mask = LVIF_PARAM | LVIF_IMAGE;
  711. item.iItem = iDest;
  712. item.iImage = iSrcImage;
  713. item.iSubItem = 0;
  714. item.lParam = (LPARAM)lpSrc;
  715. if (!ListView_SetItem (hLV, &item))
  716. {
  717. break;
  718. }
  719. //
  720. // Select the item
  721. //
  722. item.mask = LVIF_STATE;
  723. item.iItem = iSrc;
  724. item.iSubItem = 0;
  725. item.state = 0;
  726. item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
  727. SendMessage (hLV, LVM_SETITEMSTATE, iSrc, (LPARAM) &item);
  728. item.mask = LVIF_STATE;
  729. item.iItem = iDest;
  730. item.iSubItem = 0;
  731. item.state = LVIS_SELECTED | LVIS_FOCUSED;
  732. item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
  733. SendMessage (hLV, LVM_SETITEMSTATE, iDest, (LPARAM) &item);
  734. SendMessage (hLV, LVM_ENSUREVISIBLE, iDest, (LPARAM) FALSE);
  735. //
  736. // Update the listview
  737. //
  738. ListView_RedrawItems (hLV, iDest, iSrc);
  739. pGPM->m_bDirty = TRUE;
  740. SendMessage (GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0);
  741. SetFocus (hLV);
  742. }
  743. }
  744. if (LOWORD(wParam) == IDC_GPM_DOWN)
  745. {
  746. INT iSrc, iDest, iSrcImage, iDestImage;
  747. LPGPOITEM lpSrc, lpDest;
  748. HWND hLV = GetDlgItem(hDlg, IDC_GPM_LIST);
  749. LVITEM item;
  750. ListView_EditLabel (hLV, -1);
  751. iSrc = ListView_GetNextItem (hLV, -1,
  752. LVNI_ALL | LVNI_SELECTED);
  753. if (iSrc != -1)
  754. {
  755. iDest = iSrc + 1;
  756. //
  757. // Get the current lpGPOItem pointers
  758. //
  759. item.mask = LVIF_PARAM | LVIF_IMAGE;
  760. item.iItem = iSrc;
  761. item.iSubItem = 0;
  762. if (!ListView_GetItem (hLV, &item))
  763. {
  764. break;
  765. }
  766. lpSrc = (LPGPOITEM) item.lParam;
  767. iSrcImage = item.iImage;
  768. item.mask = LVIF_PARAM | LVIF_IMAGE;
  769. item.iItem = iDest;
  770. item.iSubItem = 0;
  771. if (!ListView_GetItem (hLV, &item))
  772. {
  773. break;
  774. }
  775. lpDest = (LPGPOITEM) item.lParam;
  776. iDestImage = item.iImage;
  777. //
  778. // Swap them
  779. //
  780. item.mask = LVIF_PARAM | LVIF_IMAGE;
  781. item.iItem = iSrc;
  782. item.iImage = iDestImage;
  783. item.iSubItem = 0;
  784. item.lParam = (LPARAM)lpDest;
  785. if (!ListView_SetItem (hLV, &item))
  786. {
  787. break;
  788. }
  789. item.mask = LVIF_PARAM | LVIF_IMAGE;
  790. item.iItem = iDest;
  791. item.iImage = iSrcImage;
  792. item.iSubItem = 0;
  793. item.lParam = (LPARAM)lpSrc;
  794. if (!ListView_SetItem (hLV, &item))
  795. {
  796. break;
  797. }
  798. //
  799. // Select the item
  800. //
  801. item.mask = LVIF_STATE;
  802. item.iItem = iSrc;
  803. item.iSubItem = 0;
  804. item.state = 0;
  805. item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
  806. SendMessage (hLV, LVM_SETITEMSTATE, iSrc, (LPARAM) &item);
  807. item.mask = LVIF_STATE;
  808. item.iItem = iDest;
  809. item.iSubItem = 0;
  810. item.state = LVIS_SELECTED | LVIS_FOCUSED;
  811. item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
  812. SendMessage (hLV, LVM_SETITEMSTATE, iDest, (LPARAM) &item);
  813. SendMessage (hLV, LVM_ENSUREVISIBLE, iDest, (LPARAM) FALSE);
  814. //
  815. // Update the listview
  816. //
  817. ListView_RedrawItems (hLV, iSrc, iDest);
  818. pGPM->m_bDirty = TRUE;
  819. SendMessage (GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0);
  820. SetFocus (hLV);
  821. }
  822. }
  823. if (LOWORD(wParam) == IDM_GPM_NOOVERRIDE)
  824. {
  825. INT iIndex;
  826. LPGPOITEM lpItem;
  827. LVITEM item;
  828. HWND hLV = GetDlgItem(hDlg, IDC_GPM_LIST);
  829. //
  830. // Enumerate through the selected items
  831. //
  832. iIndex = ListView_GetNextItem (hLV, -1,
  833. LVNI_ALL | LVNI_SELECTED);
  834. if (iIndex != -1)
  835. {
  836. item.mask = LVIF_PARAM;
  837. item.iItem = iIndex;
  838. item.iSubItem = 0;
  839. if (ListView_GetItem (hLV, &item))
  840. {
  841. lpItem = (LPGPOITEM) item.lParam;
  842. lpItem->bLocked = TRUE;
  843. if (lpItem->dwOptions & GPO_FLAG_FORCE)
  844. lpItem->dwOptions &= ~GPO_FLAG_FORCE;
  845. else
  846. lpItem->dwOptions |= GPO_FLAG_FORCE;
  847. ListView_RedrawItems (hLV, iIndex, iIndex);
  848. UpdateWindow (hLV);
  849. pGPM->m_bDirty = TRUE;
  850. SendMessage (GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0);
  851. SetFocus (hLV);
  852. lpItem->bLocked = FALSE;
  853. }
  854. }
  855. }
  856. if (LOWORD(wParam) == IDM_GPM_DISABLED)
  857. {
  858. INT iIndex;
  859. LPGPOITEM lpItem;
  860. LVITEM item;
  861. HWND hLV = GetDlgItem(hDlg, IDC_GPM_LIST);
  862. //
  863. // Enumerate through the selected items
  864. //
  865. iIndex = ListView_GetNextItem (hLV, -1,
  866. LVNI_ALL | LVNI_SELECTED);
  867. if (iIndex != -1)
  868. {
  869. item.mask = LVIF_PARAM;
  870. item.iItem = iIndex;
  871. item.iSubItem = 0;
  872. if (ListView_GetItem (hLV, &item))
  873. {
  874. lpItem = (LPGPOITEM) item.lParam;
  875. if (lpItem->dwOptions & GPO_FLAG_DISABLE)
  876. lpItem->dwOptions &= ~GPO_FLAG_DISABLE;
  877. else
  878. {
  879. if (bDisableWarningIssued)
  880. {
  881. lpItem->dwOptions |= GPO_FLAG_DISABLE;
  882. }
  883. else
  884. {
  885. TCHAR szMessage[200];
  886. TCHAR szTitle[100];
  887. bDisableWarningIssued = TRUE;
  888. LoadString (g_hInstance, IDS_CONFIRMDISABLE, szMessage, ARRAYSIZE(szMessage));
  889. LoadString (g_hInstance, IDS_CONFIRMTITLE2, szTitle, ARRAYSIZE(szTitle));
  890. if (MessageBox (hDlg, szMessage, szTitle, MB_YESNO |
  891. MB_ICONWARNING | MB_DEFBUTTON2) == IDYES) {
  892. lpItem->dwOptions |= GPO_FLAG_DISABLE;
  893. }
  894. }
  895. }
  896. ListView_RedrawItems (hLV, iIndex, iIndex);
  897. UpdateWindow (hLV);
  898. pGPM->m_bDirty = TRUE;
  899. SendMessage (GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0);
  900. SetFocus (hLV);
  901. }
  902. }
  903. }
  904. if ((LOWORD(wParam) == IDC_GPM_EDIT) || (LOWORD(wParam) == IDM_GPM_EDIT))
  905. {
  906. INT iIndex;
  907. LPGPOITEM lpItem;
  908. LVITEM item;
  909. HWND hLV = GetDlgItem(hDlg, IDC_GPM_LIST);
  910. //
  911. // Enumerate through the selected items
  912. //
  913. iIndex = ListView_GetNextItem (hLV, -1,
  914. LVNI_ALL | LVNI_SELECTED);
  915. if (iIndex != -1)
  916. {
  917. item.mask = LVIF_PARAM;
  918. item.iItem = iIndex;
  919. item.iSubItem = 0;
  920. if (ListView_GetItem (hLV, &item))
  921. {
  922. lpItem = (LPGPOITEM) item.lParam;
  923. lpItem->bLocked = TRUE;
  924. pGPM->StartGPE (lpItem->lpDSPath, hDlg);
  925. lpItem->bLocked = FALSE;
  926. SetFocus (hLV);
  927. }
  928. }
  929. }
  930. if ((LOWORD(wParam) == IDC_GPM_NEW) || (LOWORD(wParam) == IDM_GPM_NEW))
  931. {
  932. SetWaitCursor();
  933. pGPM->OnNew (hDlg);
  934. ClearWaitCursor();
  935. }
  936. if (LOWORD(wParam) == IDC_GPM_OPTIONS)
  937. {
  938. INT iIndex;
  939. LPGPOITEM lpItem;
  940. LVITEM item;
  941. HWND hLV = GetDlgItem(hDlg, IDC_GPM_LIST);
  942. //
  943. // Enumerate through the selected items
  944. //
  945. iIndex = ListView_GetNextItem (hLV, -1,
  946. LVNI_ALL | LVNI_SELECTED);
  947. if (iIndex != -1)
  948. {
  949. item.mask = LVIF_PARAM;
  950. item.iItem = iIndex;
  951. item.iSubItem = 0;
  952. if (ListView_GetItem (hLV, &item))
  953. {
  954. lpItem = (LPGPOITEM) item.lParam;
  955. lpItem->bLocked = TRUE;
  956. if (DialogBoxParam (g_hInstance, MAKEINTRESOURCE(IDD_GPM_LINK_OPTIONS),
  957. hDlg, LinkOptionsDlgProc, (LPARAM) lpItem))
  958. {
  959. ListView_RedrawItems (hLV, iIndex, iIndex);
  960. UpdateWindow (hLV);
  961. pGPM->m_bDirty = TRUE;
  962. SendMessage (GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0);
  963. }
  964. lpItem->bLocked = FALSE;
  965. SetFocus (hLV);
  966. }
  967. }
  968. }
  969. if (LOWORD(wParam) == IDM_GPM_RENAME)
  970. {
  971. INT iIndex;
  972. HWND hLV = GetDlgItem(hDlg, IDC_GPM_LIST);
  973. //
  974. // Enumerate through the selected items
  975. //
  976. iIndex = ListView_GetNextItem (hLV, -1,
  977. LVNI_ALL | LVNI_SELECTED);
  978. if (iIndex != -1)
  979. {
  980. ListView_EditLabel (hLV, iIndex);
  981. }
  982. }
  983. if ((LOWORD(wParam) == IDC_GPM_PROPERTIES) || (LOWORD(wParam) == IDM_GPM_PROPERTIES))
  984. {
  985. pGPM->OnProperties (hDlg);
  986. }
  987. if (LOWORD(wParam) == IDM_GPM_REFRESH)
  988. {
  989. pGPM->RefreshGPM (hDlg, FALSE);
  990. }
  991. if (LOWORD(wParam) == IDC_GPM_BLOCK)
  992. {
  993. pGPM->m_bDirty = TRUE;
  994. SendMessage (GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0);
  995. }
  996. break;
  997. case WM_NOTIFY:
  998. pGPM = (CGroupPolicyMgr *) GetWindowLongPtr (hDlg, DWLP_USER);
  999. if (!pGPM) {
  1000. break;
  1001. }
  1002. switch (((NMHDR FAR*)lParam)->code)
  1003. {
  1004. case NM_CUSTOMDRAW:
  1005. {
  1006. LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;
  1007. SelectObject(lplvcd->nmcd.hdc, pGPM->m_hDefaultFont);
  1008. if ((lplvcd->nmcd.dwDrawStage == CDDS_PREPAINT) ||
  1009. (lplvcd->nmcd.dwDrawStage == CDDS_ITEMPREPAINT))
  1010. {
  1011. SetWindowLongPtr (hDlg, DWLP_MSGRESULT, CDRF_NOTIFYSUBITEMDRAW);
  1012. return TRUE;
  1013. }
  1014. if ((lplvcd->nmcd.dwDrawStage == (CDDS_SUBITEM | CDDS_ITEMPREPAINT)) &&
  1015. (lplvcd->iSubItem > 0))
  1016. {
  1017. SelectObject(lplvcd->nmcd.hdc, pGPM->m_hMarlettFont);
  1018. SetWindowLongPtr (hDlg, DWLP_MSGRESULT, CDRF_NEWFONT);
  1019. return TRUE;
  1020. }
  1021. SetWindowLongPtr (hDlg, DWLP_MSGRESULT, CDRF_DODEFAULT);
  1022. return TRUE;
  1023. }
  1024. case LVN_GETDISPINFO:
  1025. {
  1026. NMLVDISPINFO * lpDispInfo = (NMLVDISPINFO *) lParam;
  1027. LPGPOITEM lpItem = (LPGPOITEM)lpDispInfo->item.lParam;
  1028. if (lpDispInfo->item.iSubItem == 0)
  1029. {
  1030. lpDispInfo->item.pszText = lpItem->lpDisplayName;
  1031. }
  1032. else
  1033. {
  1034. lpDispInfo->item.pszText = szNoCheckMark;
  1035. if ((lpDispInfo->item.iSubItem == 1) &&
  1036. (lpItem->dwOptions & GPO_FLAG_FORCE))
  1037. {
  1038. lpDispInfo->item.pszText = szCheckMark;
  1039. }
  1040. if ((lpDispInfo->item.iSubItem == 2) &&
  1041. (lpItem->dwOptions & GPO_FLAG_DISABLE))
  1042. {
  1043. lpDispInfo->item.pszText = szCheckMark;
  1044. }
  1045. }
  1046. }
  1047. break;
  1048. case LVN_DELETEITEM:
  1049. {
  1050. NMLISTVIEW * pLVInfo = (NMLISTVIEW *) lParam;
  1051. if (pLVInfo->lParam)
  1052. {
  1053. LocalFree ((LPTSTR)pLVInfo->lParam);
  1054. }
  1055. }
  1056. break;
  1057. case LVN_ITEMCHANGED:
  1058. PostMessage (hDlg, WM_REFRESHDISPLAY, 0, 0);
  1059. break;
  1060. case LVN_ITEMACTIVATE:
  1061. {
  1062. LPNMITEMACTIVATE pItem = (LPNMITEMACTIVATE) lParam;
  1063. LPGPOITEM lpItem;
  1064. LVITEM item;
  1065. HWND hLV = GetDlgItem(hDlg, IDC_GPM_LIST);
  1066. if (pItem->uKeyFlags != 0)
  1067. {
  1068. break;
  1069. }
  1070. item.mask = LVIF_PARAM;
  1071. item.iItem = pItem->iItem;
  1072. item.iSubItem = 0;
  1073. if (!ListView_GetItem (hLV, &item))
  1074. {
  1075. break;
  1076. }
  1077. lpItem = (LPGPOITEM) item.lParam;
  1078. if (!lpItem)
  1079. {
  1080. break;
  1081. }
  1082. if (pItem->iSubItem == 0)
  1083. {
  1084. if (!lpItem->bReadOnly)
  1085. {
  1086. pGPM->StartGPE (lpItem->lpDSPath, hDlg);
  1087. }
  1088. }
  1089. else if (pItem->iSubItem == 1)
  1090. {
  1091. if (!pGPM->m_bReadOnly)
  1092. {
  1093. if (lpItem->dwOptions & GPO_FLAG_FORCE)
  1094. lpItem->dwOptions &= ~GPO_FLAG_FORCE;
  1095. else
  1096. lpItem->dwOptions |= GPO_FLAG_FORCE;
  1097. ListView_RedrawItems (hLV, pItem->iItem, pItem->iItem);
  1098. UpdateWindow (hLV);
  1099. pGPM->m_bDirty = TRUE;
  1100. SendMessage (GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0);
  1101. }
  1102. }
  1103. else if (pItem->iSubItem == 2)
  1104. {
  1105. if (!pGPM->m_bReadOnly)
  1106. {
  1107. lpItem = (LPGPOITEM) item.lParam;
  1108. if (lpItem->dwOptions & GPO_FLAG_DISABLE)
  1109. {
  1110. lpItem->dwOptions &= ~GPO_FLAG_DISABLE;
  1111. }
  1112. else
  1113. {
  1114. if (bDisableWarningIssued)
  1115. {
  1116. lpItem->dwOptions |= GPO_FLAG_DISABLE;
  1117. }
  1118. else
  1119. {
  1120. TCHAR szMessage[200];
  1121. TCHAR szTitle[100];
  1122. bDisableWarningIssued = TRUE;
  1123. LoadString (g_hInstance, IDS_CONFIRMDISABLE, szMessage, ARRAYSIZE(szMessage));
  1124. LoadString (g_hInstance, IDS_CONFIRMTITLE2, szTitle, ARRAYSIZE(szTitle));
  1125. if (MessageBox (hDlg, szMessage, szTitle, MB_YESNO |
  1126. MB_ICONWARNING | MB_DEFBUTTON2) == IDYES) {
  1127. lpItem->dwOptions |= GPO_FLAG_DISABLE;
  1128. }
  1129. }
  1130. }
  1131. ListView_RedrawItems (hLV, pItem->iItem, pItem->iItem);
  1132. UpdateWindow (hLV);
  1133. pGPM->m_bDirty = TRUE;
  1134. SendMessage (GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0);
  1135. }
  1136. }
  1137. }
  1138. break;
  1139. case LVN_BEGINLABELEDIT:
  1140. {
  1141. NMLVDISPINFO * pInfo = (NMLVDISPINFO *) lParam;
  1142. LPGPOITEM lpItem;
  1143. if (pInfo)
  1144. {
  1145. lpItem = (LPGPOITEM) pInfo->item.lParam;
  1146. if (lpItem)
  1147. {
  1148. if (lpItem->bReadOnly)
  1149. {
  1150. SetWindowLongPtr (hDlg, DWLP_MSGRESULT, 1);
  1151. return TRUE;
  1152. }
  1153. else
  1154. {
  1155. HWND hEdit;
  1156. hEdit = ListView_GetEditControl(GetDlgItem(hDlg, IDC_GPM_LIST));
  1157. if (hEdit)
  1158. {
  1159. SetWindowText (hEdit, lpItem->lpGPOName);
  1160. }
  1161. }
  1162. }
  1163. }
  1164. }
  1165. break;
  1166. case LVN_ENDLABELEDIT:
  1167. {
  1168. NMLVDISPINFO * pInfo = (NMLVDISPINFO *) lParam;
  1169. LPGROUPPOLICYOBJECT pGPO;
  1170. LPGPOITEM lpItem, lpTemp;
  1171. LV_ITEM item;
  1172. HRESULT hr;
  1173. DWORD dwSize;
  1174. LPTSTR lpFullPath;
  1175. TCHAR szDisplayName[MAX_FRIENDLYNAME];
  1176. if (pInfo->item.pszText && (*pInfo->item.pszText))
  1177. {
  1178. //
  1179. // Get the LPGPOITEM pointer
  1180. //
  1181. lpItem = (LPGPOITEM) pInfo->item.lParam;
  1182. if (!lpItem)
  1183. {
  1184. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::GPMDlgProc: NULL lpGPOItem pointer")));
  1185. SetWindowLongPtr (hDlg, DWLP_MSGRESULT, 0);
  1186. return TRUE;
  1187. }
  1188. if ( lpItem->bLocked )
  1189. {
  1190. SetWindowLongPtr (hDlg, DWLP_MSGRESULT, 0);
  1191. return TRUE;
  1192. }
  1193. //
  1194. // Create a GPO object to work with
  1195. //
  1196. hr = CoCreateInstance(CLSID_GroupPolicyObject, NULL,
  1197. CLSCTX_SERVER, IID_IGroupPolicyObject,
  1198. (void **)&pGPO);
  1199. if (FAILED(hr))
  1200. {
  1201. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::GPMDlgProc: CoCreateInstance failed with 0x%x"), hr));
  1202. SetWindowLongPtr (hDlg, DWLP_MSGRESULT, 0);
  1203. return TRUE;
  1204. }
  1205. lpFullPath = pGPM->GetFullGPOPath (lpItem->lpDSPath, hDlg);
  1206. if (!lpFullPath)
  1207. {
  1208. pGPO->Release();
  1209. SetWindowLongPtr (hDlg, DWLP_MSGRESULT, 0);
  1210. return TRUE;
  1211. }
  1212. //
  1213. // Open GPO object without opening registry data
  1214. //
  1215. hr = pGPO->OpenDSGPO(lpFullPath, 0);
  1216. LocalFree (lpFullPath);
  1217. if (FAILED(hr))
  1218. {
  1219. ReportError(hDlg, hr, IDS_FAILEDDS);
  1220. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::GPMDlgProc: OpenDSGPO failed with 0x%x"), hr));
  1221. pGPO->Release();
  1222. SetWindowLongPtr (hDlg, DWLP_MSGRESULT, 0);
  1223. return TRUE;
  1224. }
  1225. //
  1226. // Rename it
  1227. //
  1228. hr = pGPO->SetDisplayName(pInfo->item.pszText);
  1229. if (FAILED(hr))
  1230. {
  1231. ReportError(hDlg, hr, IDS_FAILEDSETNAME);
  1232. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::GPMDlgProc: SetDisplayName failed with 0x%x"), hr));
  1233. pGPO->Release();
  1234. SetWindowLongPtr (hDlg, DWLP_MSGRESULT, 0);
  1235. return TRUE;
  1236. }
  1237. //
  1238. // Query for the display name again in case its been truncated
  1239. //
  1240. hr = pGPO->GetDisplayName(szDisplayName, ARRAYSIZE(szDisplayName));
  1241. if (FAILED(hr))
  1242. {
  1243. ReportError(hDlg, hr, IDS_FAILEDSETNAME);
  1244. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::GPMDlgProc: GetDisplayName failed with 0x%x"), hr));
  1245. pGPO->Release();
  1246. SetWindowLongPtr (hDlg, DWLP_MSGRESULT, 0);
  1247. return TRUE;
  1248. }
  1249. pGPO->Release();
  1250. //
  1251. // Update the name in the LPGPOITEM structure
  1252. //
  1253. lpTemp = pGPM->CreateEntry (szDisplayName, lpItem->lpDSPath,
  1254. lpItem->dwOptions, lpItem->dwDisabled,
  1255. lpItem->bReadOnly);
  1256. if (!lpTemp) {
  1257. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::GPMDlgProc: Failed to create replacement entry.")));
  1258. SetWindowLongPtr (hDlg, DWLP_MSGRESULT, 1);
  1259. return TRUE;
  1260. }
  1261. ZeroMemory (&item, sizeof(item));
  1262. item.mask = LVIF_PARAM;
  1263. item.iItem = pInfo->item.iItem;
  1264. item.lParam = (LPARAM)lpTemp;
  1265. ListView_SetItem (GetDlgItem (hDlg, IDC_GPM_LIST), &item);
  1266. LocalFree (lpItem);
  1267. SetWindowLongPtr (hDlg, DWLP_MSGRESULT, 1);
  1268. }
  1269. }
  1270. return TRUE;
  1271. case LVN_KEYDOWN:
  1272. {
  1273. LPNMLVKEYDOWN pKey = (LPNMLVKEYDOWN) lParam;
  1274. if (pKey->wVKey == VK_DELETE)
  1275. {
  1276. if (!pGPM->m_bReadOnly)
  1277. {
  1278. PostMessage (hDlg, WM_COMMAND, IDC_GPM_DELETE, 0);
  1279. }
  1280. }
  1281. if (pKey->wVKey == VK_F5)
  1282. {
  1283. PostMessage (hDlg, WM_COMMAND, IDM_GPM_REFRESH, 0);
  1284. }
  1285. if (pKey->wVKey == VK_RETURN)
  1286. {
  1287. PostMessage (hDlg, WM_COMMAND, IDM_GPM_PROPERTIES, 0);
  1288. }
  1289. if (pKey->wVKey == VK_F2)
  1290. {
  1291. HWND hLV;
  1292. int i;
  1293. LPGPOITEM lpItem;
  1294. LV_ITEM item;
  1295. //
  1296. // Allow the rename only if it is possible to rename
  1297. //
  1298. //
  1299. // Get the selected item (if any)
  1300. //
  1301. hLV = GetDlgItem (hDlg, IDC_GPM_LIST);
  1302. i = ListView_GetNextItem(GetDlgItem (hDlg, IDC_GPM_LIST), -1, LVNI_SELECTED);
  1303. if (i >= 0)
  1304. {
  1305. //
  1306. // Get the lpGPOItem structure pointer
  1307. //
  1308. ZeroMemory(&item, sizeof(item));
  1309. item.mask = LVIF_PARAM;
  1310. item.iItem = i;
  1311. ListView_GetItem(hLV, &item);
  1312. lpItem = (LPGPOITEM)item.lParam;
  1313. if ( (lpItem) && (!(lpItem->bReadOnly)) ) {
  1314. PostMessage (hDlg, WM_COMMAND, IDM_GPM_RENAME, 0);
  1315. }
  1316. }
  1317. }
  1318. }
  1319. break;
  1320. case PSN_APPLY:
  1321. {
  1322. PSHNOTIFY * pNotify = (PSHNOTIFY *) lParam;
  1323. if (!pGPM->Save(hDlg))
  1324. {
  1325. SetWindowLongPtr (hDlg, DWLP_MSGRESULT, PSNRET_INVALID);
  1326. return TRUE;
  1327. }
  1328. }
  1329. // fall through...
  1330. case PSN_RESET:
  1331. SetWindowLongPtr (hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
  1332. return TRUE;
  1333. }
  1334. break;
  1335. case WM_REFRESHDISPLAY:
  1336. {
  1337. INT iIndex, iCount;
  1338. LPGPOITEM lpItem = NULL;
  1339. HWND hLV = GetDlgItem(hDlg, IDC_GPM_LIST);
  1340. LVITEM item;
  1341. pGPM = (CGroupPolicyMgr *) GetWindowLongPtr (hDlg, DWLP_USER);
  1342. iIndex = ListView_GetNextItem (hLV, -1,
  1343. LVNI_ALL | LVNI_SELECTED);
  1344. if (iIndex != -1)
  1345. {
  1346. item.mask = LVIF_PARAM;
  1347. item.iItem = iIndex;
  1348. item.iSubItem = 0;
  1349. if (!ListView_GetItem (hLV, &item))
  1350. {
  1351. break;
  1352. }
  1353. lpItem = (LPGPOITEM) item.lParam;
  1354. }
  1355. if (pGPM && (!pGPM->m_bReadOnly))
  1356. {
  1357. if (iIndex != -1)
  1358. {
  1359. EnableWindow (GetDlgItem (hDlg, IDC_GPM_DELETE), TRUE);
  1360. if (lpItem && !lpItem->bReadOnly)
  1361. {
  1362. EnableWindow (GetDlgItem (hDlg, IDC_GPM_EDIT), TRUE);
  1363. }
  1364. else
  1365. {
  1366. EnableWindow (GetDlgItem (hDlg, IDC_GPM_EDIT), FALSE);
  1367. }
  1368. EnableWindow (GetDlgItem (hDlg, IDC_GPM_OPTIONS), TRUE);
  1369. EnableWindow (GetDlgItem (hDlg, IDC_GPM_PROPERTIES), TRUE);
  1370. iCount = ListView_GetItemCount(hLV);
  1371. if (iIndex > 0)
  1372. EnableWindow (GetDlgItem (hDlg, IDC_GPM_UP), TRUE);
  1373. else
  1374. EnableWindow (GetDlgItem (hDlg, IDC_GPM_UP), FALSE);
  1375. if (iIndex < (iCount - 1))
  1376. EnableWindow (GetDlgItem (hDlg, IDC_GPM_DOWN), TRUE);
  1377. else
  1378. EnableWindow (GetDlgItem (hDlg, IDC_GPM_DOWN), FALSE);
  1379. }
  1380. else
  1381. {
  1382. EnableWindow (GetDlgItem (hDlg, IDC_GPM_DELETE), FALSE);
  1383. EnableWindow (GetDlgItem (hDlg, IDC_GPM_EDIT), FALSE);
  1384. EnableWindow (GetDlgItem (hDlg, IDC_GPM_UP), FALSE);
  1385. EnableWindow (GetDlgItem (hDlg, IDC_GPM_DOWN), FALSE);
  1386. EnableWindow (GetDlgItem (hDlg, IDC_GPM_OPTIONS), FALSE);
  1387. EnableWindow (GetDlgItem (hDlg, IDC_GPM_PROPERTIES), FALSE);
  1388. }
  1389. }
  1390. else
  1391. {
  1392. if (lpItem)
  1393. {
  1394. EnableWindow (GetDlgItem (hDlg, IDC_GPM_PROPERTIES), TRUE);
  1395. if (!lpItem->bReadOnly)
  1396. {
  1397. EnableWindow (GetDlgItem (hDlg, IDC_GPM_EDIT), TRUE);
  1398. }
  1399. else
  1400. {
  1401. EnableWindow (GetDlgItem (hDlg, IDC_GPM_EDIT), FALSE);
  1402. }
  1403. }
  1404. else
  1405. {
  1406. EnableWindow (GetDlgItem (hDlg, IDC_GPM_PROPERTIES), FALSE);
  1407. }
  1408. }
  1409. }
  1410. break;
  1411. case WM_CONTEXTMENU:
  1412. if (GetDlgItem(hDlg, IDC_GPM_LIST) == (HWND)wParam)
  1413. {
  1414. pGPM = (CGroupPolicyMgr *) GetWindowLongPtr (hDlg, DWLP_USER);
  1415. if (pGPM)
  1416. {
  1417. pGPM->OnContextMenu(hDlg, lParam);
  1418. }
  1419. }
  1420. else
  1421. {
  1422. // right mouse click
  1423. WinHelp((HWND) wParam, HELP_FILE, HELP_CONTEXTMENU,
  1424. (ULONG_PTR) (LPSTR) aGroupPolicyMgrHelpIds);
  1425. }
  1426. return TRUE;
  1427. case WM_HELP: // F1
  1428. WinHelp((HWND)((LPHELPINFO) lParam)->hItemHandle, HELP_FILE, HELP_WM_HELP,
  1429. (ULONG_PTR) (LPSTR) aGroupPolicyMgrHelpIds);
  1430. break;
  1431. }
  1432. return FALSE;
  1433. }
  1434. void CGroupPolicyMgr::OnContextMenu(HWND hDlg, LPARAM lParam)
  1435. {
  1436. LPGPOITEM lpItem;
  1437. LV_ITEM item;
  1438. HMENU hPopup;
  1439. HWND hLV;
  1440. int i;
  1441. RECT rc;
  1442. POINT pt;
  1443. //
  1444. // Get the selected item (if any)
  1445. //
  1446. hLV = GetDlgItem (hDlg, IDC_GPM_LIST);
  1447. i = ListView_GetNextItem(hLV, -1, LVNI_SELECTED);
  1448. //
  1449. // Figure out where to place the context menu
  1450. //
  1451. pt.x = ((int)(short)LOWORD(lParam));
  1452. pt.y = ((int)(short)HIWORD(lParam));
  1453. GetWindowRect (hLV, &rc);
  1454. if (!PtInRect (&rc, pt))
  1455. {
  1456. if ((lParam == (LPARAM) -1) && (i >= 0))
  1457. {
  1458. rc.left = LVIR_SELECTBOUNDS;
  1459. SendMessage (hLV, LVM_GETITEMRECT, i, (LPARAM) &rc);
  1460. pt.x = rc.left + 8;
  1461. pt.y = rc.top + ((rc.bottom - rc.top) / 2);
  1462. ClientToScreen (hLV, &pt);
  1463. }
  1464. else
  1465. {
  1466. pt.x = rc.left + ((rc.right - rc.left) / 2);
  1467. pt.y = rc.top + ((rc.bottom - rc.top) / 2);
  1468. }
  1469. }
  1470. //
  1471. // Load the context menu
  1472. //
  1473. hPopup = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDM_GPM_CONTEXTMENU));
  1474. if (!hPopup) {
  1475. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::OnContextMenu: LoadMenu failed with error %d"), GetLastError()));
  1476. return;
  1477. }
  1478. HMENU hSubMenu = GetSubMenu(hPopup, 0);
  1479. //
  1480. // If there is an item selected, then set the checkmarks appropriately
  1481. //
  1482. if (i >= 0)
  1483. {
  1484. //
  1485. // Get the lpGPOItem structure pointer
  1486. //
  1487. ZeroMemory(&item, sizeof(item));
  1488. item.mask = LVIF_PARAM;
  1489. item.iItem = i;
  1490. ListView_GetItem(hLV, &item);
  1491. lpItem = (LPGPOITEM)item.lParam;
  1492. if (!lpItem)
  1493. {
  1494. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::OnContextMenu: Failed to get lpGPOItem pointer")));
  1495. return;
  1496. }
  1497. //
  1498. // Check the menu items
  1499. //
  1500. if (lpItem->dwOptions & GPO_FLAG_DISABLE)
  1501. {
  1502. CheckMenuRadioItem(hSubMenu, IDM_GPM_DISABLED, IDM_GPM_DISABLED,
  1503. IDM_GPM_DISABLED, MF_BYCOMMAND);
  1504. }
  1505. if (lpItem->dwOptions & GPO_FLAG_FORCE)
  1506. {
  1507. CheckMenuRadioItem(hSubMenu, IDM_GPM_NOOVERRIDE, IDM_GPM_NOOVERRIDE,
  1508. IDM_GPM_NOOVERRIDE, MF_BYCOMMAND);
  1509. }
  1510. RemoveMenu(hSubMenu, 9, MF_BYPOSITION);
  1511. RemoveMenu(hSubMenu, IDM_GPM_REFRESH, MF_BYCOMMAND);
  1512. //
  1513. // Gray out Edit / Rename if read only
  1514. //
  1515. if (lpItem->bReadOnly)
  1516. {
  1517. EnableMenuItem (hSubMenu, IDM_GPM_EDIT, MF_BYCOMMAND | MF_GRAYED);
  1518. EnableMenuItem (hSubMenu, IDM_GPM_RENAME, MF_BYCOMMAND | MF_GRAYED);
  1519. }
  1520. }
  1521. else
  1522. {
  1523. //
  1524. // No item selected, remove some of the items on the
  1525. // context menu
  1526. //
  1527. RemoveMenu(hSubMenu, IDM_GPM_NOOVERRIDE, MF_BYCOMMAND);
  1528. RemoveMenu(hSubMenu, IDM_GPM_DISABLED, MF_BYCOMMAND);
  1529. RemoveMenu(hSubMenu, 0, MF_BYPOSITION);
  1530. RemoveMenu(hSubMenu, IDM_GPM_EDIT, MF_BYCOMMAND);
  1531. RemoveMenu(hSubMenu, IDM_GPM_DELETE, MF_BYCOMMAND);
  1532. RemoveMenu(hSubMenu, IDM_GPM_RENAME, MF_BYCOMMAND);
  1533. RemoveMenu(hSubMenu, IDM_GPM_PROPERTIES, MF_BYCOMMAND);
  1534. RemoveMenu(hSubMenu, (GetMenuItemCount(hSubMenu) - 1), MF_BYPOSITION);
  1535. RemoveMenu(hSubMenu, (GetMenuItemCount(hSubMenu) - 2), MF_BYPOSITION);
  1536. }
  1537. //
  1538. // Gray out some menu items in read only mode
  1539. //
  1540. if (m_bReadOnly)
  1541. {
  1542. EnableMenuItem (hSubMenu, IDM_GPM_NEW, MF_BYCOMMAND | MF_GRAYED);
  1543. EnableMenuItem (hSubMenu, IDM_GPM_ADD, MF_BYCOMMAND | MF_GRAYED);
  1544. EnableMenuItem (hSubMenu, IDM_GPM_DELETE, MF_BYCOMMAND | MF_GRAYED);
  1545. EnableMenuItem (hSubMenu, IDM_GPM_NOOVERRIDE, MF_BYCOMMAND | MF_GRAYED);
  1546. EnableMenuItem (hSubMenu, IDM_GPM_DISABLED, MF_BYCOMMAND | MF_GRAYED);
  1547. }
  1548. //
  1549. // Display the menu
  1550. //
  1551. TrackPopupMenu(hSubMenu, TPM_LEFTALIGN, pt.x, pt.y, 0, hDlg, NULL);
  1552. DestroyMenu(hPopup);
  1553. }
  1554. void CGroupPolicyMgr::OnProperties(HWND hDlg)
  1555. {
  1556. INT iIndex;
  1557. LVITEM item;
  1558. HWND hLV;
  1559. HRESULT hr;
  1560. LPGPOITEM pItem;
  1561. LPGROUPPOLICYOBJECT pGPO;
  1562. HPROPSHEETPAGE *hPages;
  1563. UINT i, uPageCount;
  1564. PROPSHEETHEADER psh;
  1565. LPTSTR lpTemp;
  1566. //
  1567. // Get the selected item
  1568. //
  1569. hLV = GetDlgItem (hDlg, IDC_GPM_LIST);
  1570. iIndex = ListView_GetNextItem(hLV, -1, LVNI_ALL | LVNI_SELECTED);
  1571. if (iIndex >= 0)
  1572. {
  1573. SetWaitCursor();
  1574. //
  1575. // Get the lpGPOItem pointer
  1576. //
  1577. ZeroMemory(&item, sizeof(item));
  1578. item.mask = LVIF_PARAM;
  1579. item.iItem = iIndex;
  1580. ListView_GetItem(hLV, &item);
  1581. pItem = (LPGPOITEM)item.lParam;
  1582. if (!pItem)
  1583. {
  1584. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::OnProperties: Failed to get lpGPOItem pointer")));
  1585. ClearWaitCursor();
  1586. return;
  1587. }
  1588. pItem->bLocked = TRUE;
  1589. hr = CoCreateInstance (CLSID_GroupPolicyObject, NULL,
  1590. CLSCTX_SERVER, IID_IGroupPolicyObject,
  1591. (void**)&pGPO);
  1592. if (FAILED(hr))
  1593. {
  1594. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::OnProperties: CoCreateInstance failed with 0x%x"), hr));
  1595. pItem->bLocked = FALSE;
  1596. ClearWaitCursor();
  1597. return;
  1598. }
  1599. lpTemp = GetFullGPOPath (pItem->lpDSPath, hDlg);
  1600. if (!lpTemp)
  1601. {
  1602. ClearWaitCursor();
  1603. pItem->bLocked = FALSE;
  1604. ReportError(hDlg, hr, IDS_FAILEDDS);
  1605. return;
  1606. }
  1607. //
  1608. // Open the requested object without mounting the registry
  1609. //
  1610. hr = pGPO->OpenDSGPO(lpTemp, pItem->bReadOnly ? GPO_OPEN_READ_ONLY : 0);
  1611. LocalFree (lpTemp);
  1612. if (FAILED(hr))
  1613. {
  1614. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::OnProperties: Failed to open GPO object with 0x%x"), hr));
  1615. pItem->bLocked = FALSE;
  1616. ClearWaitCursor();
  1617. ReportError(hDlg, hr, IDS_FAILEDDS);
  1618. return;
  1619. }
  1620. //
  1621. // Ask the GPO for the property sheet pages
  1622. //
  1623. hr = pGPO->GetPropertySheetPages (&hPages, &uPageCount);
  1624. if (FAILED(hr))
  1625. {
  1626. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::OnProperties: Failed to query property sheet pages with 0x%x."), hr));
  1627. pGPO->Release();
  1628. pItem->bLocked = FALSE;
  1629. ClearWaitCursor();
  1630. return;
  1631. }
  1632. //
  1633. // Display the property sheet
  1634. //
  1635. ZeroMemory (&psh, sizeof(psh));
  1636. psh.dwSize = sizeof(psh);
  1637. psh.dwFlags = PSH_PROPTITLE;
  1638. psh.hwndParent = hDlg;
  1639. psh.hInstance = g_hInstance;
  1640. psh.pszCaption = pItem->lpDisplayName;
  1641. psh.nPages = uPageCount;
  1642. psh.phpage = hPages;
  1643. PropertySheet (&psh);
  1644. LocalFree (hPages);
  1645. pGPO->Release();
  1646. CheckIconStatus (hLV, pItem);
  1647. pItem->bLocked = FALSE;
  1648. ClearWaitCursor();
  1649. }
  1650. }
  1651. void CGroupPolicyMgr::OnNew(HWND hDlg)
  1652. {
  1653. HRESULT hr;
  1654. HWND hLV;
  1655. INT iIndex;
  1656. LPGROUPPOLICYOBJECT pGPO = NULL;
  1657. TCHAR szName[256];
  1658. TCHAR szGPOPath[MAX_PATH];
  1659. LPOLESTR lpDomain = NULL;
  1660. HKEY hKey;
  1661. DWORD dwSize, dwType;
  1662. BOOL bDisabledLink = FALSE;
  1663. //
  1664. // Check if there is a user preference or policy that
  1665. // any new GPOs should be created with a disabled link
  1666. // by default
  1667. //
  1668. if (RegOpenKeyEx (HKEY_CURRENT_USER, GPE_KEY, 0,
  1669. KEY_READ, &hKey) == ERROR_SUCCESS)
  1670. {
  1671. dwSize = sizeof(bDisabledLink);
  1672. RegQueryValueEx (hKey, NEW_LINKS_DISABLED_VALUE, NULL, &dwType,
  1673. (LPBYTE) &bDisabledLink, &dwSize);
  1674. RegCloseKey (hKey);
  1675. }
  1676. if (RegOpenKeyEx (HKEY_CURRENT_USER, GPE_POLICIES_KEY, 0,
  1677. KEY_READ, &hKey) == ERROR_SUCCESS)
  1678. {
  1679. dwSize = sizeof(bDisabledLink);
  1680. RegQueryValueEx (hKey, NEW_LINKS_DISABLED_VALUE, NULL, &dwType,
  1681. (LPBYTE) &bDisabledLink, &dwSize);
  1682. RegCloseKey (hKey);
  1683. }
  1684. //
  1685. // Get the domain name
  1686. //
  1687. lpDomain = GetDomainFromLDAPPath(m_lpDSObject);
  1688. if (!lpDomain)
  1689. {
  1690. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::OnNew: Failed to get the domain name")));
  1691. goto Exit;
  1692. }
  1693. //
  1694. // Create a new GPO object to work with
  1695. //
  1696. hr = CoCreateInstance (CLSID_GroupPolicyObject, NULL,
  1697. CLSCTX_SERVER, IID_IGroupPolicyObject,
  1698. (void**)&pGPO);
  1699. if (FAILED(hr))
  1700. {
  1701. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::OnNew: CoCreateInstance failed with 0x%x"), hr));
  1702. goto Exit;
  1703. }
  1704. //
  1705. // Create a new GPO without mounting the registry
  1706. //
  1707. GetNewGPODisplayName (szName, ARRAYSIZE(szName));
  1708. #if FGPO_SUPPORT
  1709. hr = pGPO->New(lpDomain, szName, (m_gpHint == GPHintForest) ? GPO_OPEN_FOREST : 0);
  1710. #else
  1711. hr = pGPO->New(lpDomain, szName, 0);
  1712. #endif
  1713. if (FAILED(hr))
  1714. {
  1715. ReportError(hDlg, hr, IDS_FAILEDNEW);
  1716. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::OnNew: Failed to create GPO object with 0x%x"), hr));
  1717. goto Exit;
  1718. }
  1719. hr = pGPO->GetPath(szGPOPath, ARRAYSIZE(szGPOPath));
  1720. if (FAILED(hr))
  1721. {
  1722. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::OnNew: Failed to get GPO object path with 0x%x"), hr));
  1723. pGPO->Delete();
  1724. goto Exit;
  1725. }
  1726. //
  1727. // Add the GPO to the list view
  1728. //
  1729. hLV = GetDlgItem (hDlg, IDC_GPM_LIST);
  1730. if (!AddGPOToList (hLV, szName, szGPOPath,
  1731. (bDisabledLink ? GPO_FLAG_DISABLE : 0), FALSE, FALSE, FALSE))
  1732. {
  1733. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::OnNew: Failed to add the GPO to the listview")));
  1734. pGPO->Delete();
  1735. goto Exit;
  1736. }
  1737. m_bDirty = TRUE;
  1738. Save(hDlg);
  1739. SendMessage (GetParent(hDlg), PSM_CANCELTOCLOSE, 0, 0);
  1740. iIndex = ListView_GetItemCount(hLV) - 1;
  1741. //
  1742. // Now trigger an edit of the entry
  1743. //
  1744. SetFocus(hLV);
  1745. ListView_EnsureVisible (hLV, iIndex, FALSE);
  1746. ListView_EditLabel(hLV, iIndex);
  1747. Exit:
  1748. if (lpDomain)
  1749. {
  1750. delete [] lpDomain;
  1751. }
  1752. if (pGPO)
  1753. {
  1754. pGPO->Release();
  1755. }
  1756. }
  1757. BOOL CGroupPolicyMgr::RefreshGPM (HWND hDlg, BOOL bInitial)
  1758. {
  1759. HRESULT hr;
  1760. BOOL bResult = FALSE;
  1761. TCHAR szHeaderName[50];
  1762. TCHAR szBuffer1[100];
  1763. TCHAR szBuffer2[MAX_FRIENDLYNAME];
  1764. HWND hLV = GetDlgItem (hDlg, IDC_GPM_LIST);
  1765. LV_COLUMN col;
  1766. RECT rc;
  1767. INT iTotal = 0, iCurrent, iMaxVisibleItems, iSize;
  1768. IADs * pADs;
  1769. VARIANT var;
  1770. BSTR bstrProperty;
  1771. //
  1772. // Prep work
  1773. //
  1774. SetWaitCursor();
  1775. SendMessage (hLV, WM_SETREDRAW, 0, 0);
  1776. ListView_DeleteAllItems(hLV);
  1777. CheckDlgButton (hDlg, IDC_GPM_BLOCK, BST_UNCHECKED);
  1778. //
  1779. // Insert Columns
  1780. //
  1781. if (bInitial)
  1782. {
  1783. GetClientRect (hLV, &rc);
  1784. LoadString(g_hInstance, IDS_GPM_NAME, szHeaderName, ARRAYSIZE(szHeaderName));
  1785. col.mask = LVCF_FMT | LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH;
  1786. col.fmt = LVCFMT_LEFT;
  1787. iCurrent = (int)(rc.right * .65);
  1788. iTotal += iCurrent;
  1789. col.cx = iCurrent;
  1790. col.pszText = szHeaderName;
  1791. col.iSubItem = 0;
  1792. ListView_InsertColumn (hLV, 0, &col);
  1793. LoadString(g_hInstance, IDS_GPM_NOOVERRIDE, szHeaderName, ARRAYSIZE(szHeaderName));
  1794. iCurrent = (int)(rc.right * .20);
  1795. iTotal += iCurrent;
  1796. col.cx = iCurrent;
  1797. col.fmt = LVCFMT_CENTER;
  1798. col.iSubItem = 1;
  1799. ListView_InsertColumn (hLV, 1, &col);
  1800. LoadString(g_hInstance, IDS_GPM_DISABLED, szHeaderName, ARRAYSIZE(szHeaderName));
  1801. col.iSubItem = 2;
  1802. col.cx = rc.right - iTotal;
  1803. col.fmt = LVCFMT_CENTER;
  1804. ListView_InsertColumn (hLV, 2, &col);
  1805. }
  1806. //
  1807. // Set the DC name
  1808. //
  1809. if (bInitial)
  1810. {
  1811. LoadString (g_hInstance, IDS_GPM_DCNAME, szBuffer1, ARRAYSIZE(szBuffer1));
  1812. wsprintf (szBuffer2, szBuffer1, m_lpGPODCName);
  1813. SetDlgItemText (hDlg, IDC_GPM_DCNAME, szBuffer2);
  1814. }
  1815. //
  1816. // Bind to the object and get the friendly name
  1817. //
  1818. hr = OpenDSObject(m_lpDSObject, IID_IADs, (void **)&pADs);
  1819. if (FAILED(hr)) {
  1820. if (hr == HRESULT_FROM_WIN32(ERROR_DS_NO_SUCH_OBJECT))
  1821. {
  1822. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::RefreshGPM: %s does not exist on the server."), m_lpDSObject));
  1823. DialogBoxParam (g_hInstance, MAKEINTRESOURCE(IDD_NODSOBJECT),
  1824. hDlg, NoDSObjectDlgProc, (LPARAM) this);
  1825. }
  1826. else
  1827. {
  1828. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::RefreshGPM: OpenDSObject failed with 0x%x"), hr));
  1829. ReportError(hDlg, hr, IDS_FAILEDGPQUERY, hr);
  1830. }
  1831. goto Exit;
  1832. }
  1833. if (bInitial)
  1834. {
  1835. #if FGPO_SUPPORT
  1836. if (m_gpHint != GPHintForest)
  1837. {
  1838. #endif
  1839. VariantInit(&var);
  1840. bstrProperty = SysAllocString (GPM_NAME_PROPERTY);
  1841. if (!bstrProperty)
  1842. {
  1843. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::RefreshGPM: Failed to allocate memory with %d"), GetLastError()));
  1844. ReportError(hDlg, hr, IDS_FAILEDGPQUERY, hr);
  1845. VariantClear (&var);
  1846. pADs->Release();
  1847. goto Exit;
  1848. }
  1849. hr = pADs->Get(bstrProperty, &var);
  1850. if (FAILED(hr))
  1851. {
  1852. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::RefreshGPM: Failed to query for display name with 0x%x"), hr));
  1853. ReportError(hDlg, hr, IDS_FAILEDGPQUERY, hr);
  1854. SysFreeString (bstrProperty);
  1855. VariantClear (&var);
  1856. pADs->Release();
  1857. goto Exit;
  1858. }
  1859. LoadString (g_hInstance, IDS_GPM_DESCRIPTION, szBuffer1, ARRAYSIZE(szBuffer1));
  1860. wsprintf (szBuffer2, szBuffer1, var.bstrVal);
  1861. SysFreeString (bstrProperty);
  1862. VariantClear (&var);
  1863. #if FGPO_SUPPORT
  1864. }
  1865. else
  1866. {
  1867. LoadString (g_hInstance, IDS_GPM_FORESTDESC, szBuffer2, ARRAYSIZE(szBuffer2));
  1868. }
  1869. #endif
  1870. SetDlgItemText (hDlg, IDC_GPM_TITLE, szBuffer2);
  1871. //
  1872. // Check if the user has write access to gPLink
  1873. //
  1874. hr = CheckDSWriteAccess ((LPUNKNOWN)pADs, TEXT("gPLink"));
  1875. if (FAILED(hr))
  1876. {
  1877. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::RefreshGPM: User has read only access to the gPLink property.")));
  1878. m_bReadOnly = TRUE;
  1879. }
  1880. }
  1881. //
  1882. // Get the options for this DS object
  1883. //
  1884. VariantInit(&var);
  1885. bstrProperty = SysAllocString (GPM_OPTIONS_PROPERTY);
  1886. if (bstrProperty)
  1887. {
  1888. hr = pADs->Get(bstrProperty, &var);
  1889. if (SUCCEEDED(hr))
  1890. {
  1891. if (var.lVal & GPC_BLOCK_POLICY)
  1892. {
  1893. CheckDlgButton (hDlg, IDC_GPM_BLOCK, BST_CHECKED);
  1894. }
  1895. }
  1896. SysFreeString (bstrProperty);
  1897. }
  1898. VariantClear (&var);
  1899. //
  1900. // Get the GPOs linked to this object
  1901. //
  1902. VariantInit(&var);
  1903. bstrProperty = SysAllocString (GPM_LINK_PROPERTY);
  1904. if (bstrProperty)
  1905. {
  1906. hr = pADs->Get(bstrProperty, &var);
  1907. if (SUCCEEDED(hr))
  1908. {
  1909. AddGPOs (hDlg, var.bstrVal);
  1910. }
  1911. SysFreeString (bstrProperty);
  1912. }
  1913. VariantClear (&var);
  1914. pADs->Release();
  1915. //
  1916. // Get the max number of visible items and the total number
  1917. // of items in the listview
  1918. //
  1919. if (bInitial)
  1920. {
  1921. iMaxVisibleItems = ListView_GetCountPerPage (hLV);
  1922. iTotal = ListView_GetItemCount(hLV);
  1923. //
  1924. // If the number of items in the listview is greater than
  1925. // the max visible items, then we need to make the first
  1926. // column smaller by the width of a vertical scroll bar so
  1927. // that the horizontal scroll bar doesn't appear
  1928. //
  1929. if (iTotal > iMaxVisibleItems) {
  1930. iSize = ListView_GetColumnWidth (hLV, 0);
  1931. iSize -= GetSystemMetrics(SM_CYHSCROLL);
  1932. ListView_SetColumnWidth (hLV, 0, iSize);
  1933. }
  1934. }
  1935. PropSheet_UnChanged (GetParent(hDlg), hDlg);
  1936. //
  1937. // Success
  1938. //
  1939. bResult = TRUE;
  1940. Exit:
  1941. SendMessage (hLV, WM_SETREDRAW, 1, 0);
  1942. ClearWaitCursor();
  1943. return bResult;
  1944. }
  1945. BOOL CGroupPolicyMgr::OnInitDialog (HWND hDlg)
  1946. {
  1947. BOOL bResult = FALSE;
  1948. HWND hLV = GetDlgItem (hDlg, IDC_GPM_LIST);
  1949. LOGFONT lf;
  1950. HICON hIcon;
  1951. HIMAGELIST hImageList;
  1952. HRESULT hr;
  1953. LPOLESTR pszDomain;
  1954. LPTSTR lpTemp;
  1955. //
  1956. // Retreive the name of the DC DSAdmin is using
  1957. //
  1958. m_lpDSADCName = ExtractServerName (m_lpDSObject);
  1959. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyObject::OnInitDialog: DS Admin is focused on DC %s"),
  1960. m_lpDSADCName));
  1961. //
  1962. // Get the friendly domain name
  1963. //
  1964. pszDomain = GetDomainFromLDAPPath(m_lpDSObject);
  1965. if (!pszDomain)
  1966. {
  1967. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OnInitDialog: Failed to get domain name")));
  1968. return FALSE;
  1969. }
  1970. //
  1971. // Convert LDAP to dot (DN) style
  1972. //
  1973. hr = ConvertToDotStyle (pszDomain, &m_lpDomainName);
  1974. delete [] pszDomain;
  1975. if (FAILED(hr))
  1976. {
  1977. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::CreatePropertyPages: Failed to convert domain name with 0x%x"), hr));
  1978. }
  1979. //
  1980. // Get the GPO DC for this domain
  1981. //
  1982. m_lpGPODCName = GetDCName (m_lpDomainName, m_lpDSADCName, hDlg, TRUE, VALIDATE_INHERIT_DC);
  1983. if (!m_lpGPODCName)
  1984. {
  1985. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OnInitDialog: Failed to get DC name for %s"),
  1986. m_lpDomainName));
  1987. return FALSE;
  1988. }
  1989. //
  1990. // Switch to using the GPO domain controller for this DS object
  1991. //
  1992. lpTemp = MakeFullPath (m_lpDSObject, m_lpGPODCName);
  1993. if (!lpTemp)
  1994. {
  1995. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::OnInitDialog: Failed to build new DS object path")));
  1996. return FALSE;
  1997. }
  1998. LocalFree (m_lpDSObject);
  1999. m_lpDSObject = lpTemp;
  2000. //
  2001. // Create the Marlett font based upon the currently selected font
  2002. //
  2003. m_hDefaultFont = (HFONT) SendMessage (hLV, WM_GETFONT, 0, 0);
  2004. GetObject (m_hDefaultFont, sizeof(lf), &lf);
  2005. lf.lfHeight += (LONG)(lf.lfHeight * .20);
  2006. lf.lfCharSet = SYMBOL_CHARSET;
  2007. lf.lfPitchAndFamily = FF_DECORATIVE | DEFAULT_PITCH;
  2008. lstrcpy (lf.lfFaceName, TEXT("Marlett"));
  2009. m_hMarlettFont = CreateFontIndirect (&lf);
  2010. //
  2011. // Set extended LV styles
  2012. //
  2013. SendMessage(hLV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0,
  2014. LVS_EX_FULLROWSELECT | LVS_EX_LABELTIP);
  2015. //
  2016. // Create the imagelist
  2017. //
  2018. hImageList = ImageList_Create (16, 16, ILC_MASK, 3, 3);
  2019. if (!hImageList)
  2020. {
  2021. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::OnInitDialog: Failed to create the image list")));
  2022. return FALSE;
  2023. }
  2024. hIcon = (HICON) LoadImage (g_hInstance, MAKEINTRESOURCE(IDI_POLICY),
  2025. IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
  2026. ImageList_AddIcon (hImageList, hIcon);
  2027. DestroyIcon (hIcon);
  2028. hIcon = (HICON) LoadImage (g_hInstance, MAKEINTRESOURCE(IDI_POLICY2),
  2029. IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
  2030. ImageList_AddIcon (hImageList, hIcon);
  2031. DestroyIcon (hIcon);
  2032. hIcon = (HICON) LoadImage (g_hInstance, MAKEINTRESOURCE(IDI_POLICY3),
  2033. IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
  2034. ImageList_AddIcon (hImageList, hIcon);
  2035. DestroyIcon (hIcon);
  2036. //
  2037. // Associate the imagelist with the listview.
  2038. // The listview will free this when the
  2039. // control is destroyed.
  2040. //
  2041. SendMessage (hLV, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM) hImageList);
  2042. //
  2043. // Refresh GPM
  2044. //
  2045. if (!RefreshGPM(hDlg, TRUE))
  2046. {
  2047. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::OnInitDialog: Failed to refresh GPM")));
  2048. return FALSE;
  2049. }
  2050. return TRUE;
  2051. }
  2052. BOOL CGroupPolicyMgr::Save (HWND hDlg)
  2053. {
  2054. HRESULT hr;
  2055. HWND hLV = GetDlgItem (hDlg, IDC_GPM_LIST);
  2056. IADs * pADs;
  2057. VARIANT var;
  2058. BSTR bstrName;
  2059. LVITEM item;
  2060. LPGPOITEM lpItem;
  2061. TCHAR szOptions[12];
  2062. LPTSTR lpTemp, lpResult = NULL;
  2063. DWORD dwStrLen, dwOptions;
  2064. TCHAR szEmpty [] = TEXT(" ");
  2065. INT iIndex = -1;
  2066. if (m_bReadOnly)
  2067. {
  2068. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyMgr::Save: User only has read access, so no changes will be saved.")));
  2069. return TRUE;
  2070. }
  2071. if (!m_bDirty)
  2072. {
  2073. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyMgr::Save: Nothing has changed (dirty flag is FALSE), so no changes will be saved.")));
  2074. return TRUE;
  2075. }
  2076. //
  2077. // Enumerate through the selected items
  2078. //
  2079. while ((iIndex = ListView_GetNextItem (hLV, iIndex, LVNI_ALL)) != -1)
  2080. {
  2081. item.mask = LVIF_PARAM;
  2082. item.iItem = iIndex;
  2083. item.iSubItem = 0;
  2084. if (!ListView_GetItem (hLV, &item))
  2085. {
  2086. continue;
  2087. }
  2088. lpItem = (LPGPOITEM) item.lParam;
  2089. _itot(lpItem->dwOptions, szOptions, 10);
  2090. // [ ldap path ; options ] 0
  2091. dwStrLen = 1 + lstrlen(lpItem->lpDSPath) + 1 + lstrlen(szOptions) + 1 + 1;
  2092. if (lpResult)
  2093. {
  2094. dwStrLen += lstrlen(lpResult);
  2095. }
  2096. dwStrLen *= sizeof(TCHAR);
  2097. lpTemp = (LPTSTR) LocalAlloc (LPTR, dwStrLen);
  2098. if (!lpTemp)
  2099. {
  2100. continue;
  2101. }
  2102. lstrcpy (lpTemp, TEXT("["));
  2103. lstrcat (lpTemp, lpItem->lpDSPath);
  2104. lstrcat (lpTemp, TEXT(";"));
  2105. lstrcat (lpTemp, szOptions);
  2106. lstrcat (lpTemp, TEXT("]"));
  2107. if (lpResult)
  2108. {
  2109. lstrcat (lpTemp, lpResult);
  2110. LocalFree (lpResult);
  2111. }
  2112. lpResult = lpTemp;
  2113. }
  2114. if (!lpResult)
  2115. {
  2116. lpResult = szEmpty;
  2117. }
  2118. //
  2119. // Bind to the DS object
  2120. //
  2121. hr = OpenDSObject(m_lpDSObject, IID_IADs, (void **)&pADs);
  2122. if (FAILED(hr)) {
  2123. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::Save: Failed to get gpo interface with 0x%x"), hr));
  2124. ReportError(hDlg, hr, IDS_FAILEDGPINFO);
  2125. return FALSE;
  2126. }
  2127. //
  2128. // Set the link property
  2129. //
  2130. VariantInit(&var);
  2131. var.vt = VT_BSTR;
  2132. var.bstrVal = SysAllocString (lpResult);
  2133. if (!var.bstrVal)
  2134. {
  2135. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::Save: Failed to allocate memory with %d"), GetLastError()));
  2136. ReportError(hDlg, hr, IDS_FAILEDGPINFO);
  2137. VariantClear (&var);
  2138. pADs->Release();
  2139. return FALSE;
  2140. }
  2141. bstrName = SysAllocString (GPM_LINK_PROPERTY);
  2142. if (!bstrName)
  2143. {
  2144. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::Save: Failed to allocate memory with %d"), GetLastError()));
  2145. ReportError(hDlg, hr, IDS_FAILEDGPINFO);
  2146. VariantClear (&var);
  2147. pADs->Release();
  2148. return FALSE;
  2149. }
  2150. hr = pADs->Put(bstrName, var);
  2151. SysFreeString (bstrName);
  2152. VariantClear (&var);
  2153. if (FAILED(hr))
  2154. {
  2155. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::Save: Failed to put link property with 0x%x"), hr));
  2156. ReportError(hDlg, hr, IDS_FAILEDGPINFO);
  2157. pADs->Release();
  2158. return FALSE;
  2159. }
  2160. dwOptions = 0;
  2161. if (IsDlgButtonChecked (hDlg, IDC_GPM_BLOCK) == BST_CHECKED)
  2162. {
  2163. dwOptions |= GPC_BLOCK_POLICY;
  2164. }
  2165. //
  2166. // Set the options
  2167. //
  2168. VariantInit(&var);
  2169. var.vt = VT_I4;
  2170. var.lVal = dwOptions;
  2171. bstrName = SysAllocString (GPM_OPTIONS_PROPERTY);
  2172. if (!bstrName)
  2173. {
  2174. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::Save: Failed to allocate memory with %d"), GetLastError()));
  2175. ReportError(hDlg, hr, IDS_FAILEDGPINFO);
  2176. VariantClear (&var);
  2177. pADs->Release();
  2178. return FALSE;
  2179. }
  2180. hr = pADs->Put(bstrName, var);
  2181. SysFreeString (bstrName);
  2182. VariantClear (&var);
  2183. if (FAILED(hr))
  2184. {
  2185. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::Save: Failed to set options with 0x%x"), hr));
  2186. ReportError(hDlg, hr, IDS_FAILEDGPINFO);
  2187. pADs->Release();
  2188. return FALSE;
  2189. }
  2190. //
  2191. // Commit the changes
  2192. //
  2193. hr = pADs->SetInfo();
  2194. if (FAILED(hr))
  2195. {
  2196. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::Save: Failed to commit changes with 0x%x"), hr));
  2197. ReportError(hDlg, hr, IDS_FAILEDGPINFO);
  2198. pADs->Release();
  2199. return FALSE;
  2200. }
  2201. if (lpResult != szEmpty)
  2202. {
  2203. LocalFree (lpResult);
  2204. }
  2205. pADs->Release();
  2206. m_bDirty = FALSE;
  2207. return TRUE;
  2208. }
  2209. BOOL CGroupPolicyMgr::AddGPOs (HWND hDlg, LPTSTR lpGPOList)
  2210. {
  2211. HRESULT hr;
  2212. TCHAR szGPO[512];
  2213. TCHAR szOptions[20];
  2214. TCHAR szDisplayName[MAX_PATH];
  2215. DWORD dwOptions;
  2216. LPTSTR lpTemp, lpGPO, lpNamedGPO;
  2217. HWND hLV = GetDlgItem (hDlg, IDC_GPM_LIST);
  2218. IADs * pADs;
  2219. VARIANT var;
  2220. BSTR bstrProperty;
  2221. DWORD dwDisabled;
  2222. BOOL bReadOnly;
  2223. if (!lpGPOList)
  2224. {
  2225. return TRUE;
  2226. }
  2227. lpTemp = lpGPOList;
  2228. while (TRUE)
  2229. {
  2230. szDisplayName[0] = TEXT('\0');
  2231. dwDisabled = 0;
  2232. bReadOnly = FALSE;
  2233. //
  2234. // Look for the [
  2235. //
  2236. while (*lpTemp && (*lpTemp != TEXT('[')))
  2237. lpTemp++;
  2238. if (!(*lpTemp))
  2239. goto Exit;
  2240. lpTemp++;
  2241. //
  2242. // Copy the GPO name
  2243. //
  2244. lpGPO = szGPO;
  2245. while (*lpTemp && (*lpTemp != TEXT(';')))
  2246. *lpGPO++ = *lpTemp++;
  2247. *lpGPO = TEXT('\0');
  2248. if (!(*lpTemp))
  2249. goto Exit;
  2250. lpTemp++;
  2251. //
  2252. // Get the options
  2253. //
  2254. lpGPO = szOptions;
  2255. while (*lpTemp && (*lpTemp != TEXT(']')))
  2256. *lpGPO++ = *lpTemp++;
  2257. *lpGPO = TEXT('\0');
  2258. dwOptions = _ttoi (szOptions);
  2259. if (!(*lpTemp))
  2260. goto Exit;
  2261. lpTemp++;
  2262. //
  2263. // Convert the nameless path into a named path
  2264. //
  2265. lpNamedGPO = GetFullGPOPath (szGPO, hDlg);
  2266. if (lpNamedGPO)
  2267. {
  2268. //
  2269. // Get the friendly display name and GPO options
  2270. //
  2271. hr = OpenDSObject(lpNamedGPO, IID_IADs, (void **)&pADs);
  2272. if (SUCCEEDED(hr)) {
  2273. VariantInit(&var);
  2274. bstrProperty = SysAllocString (GPO_NAME_PROPERTY);
  2275. if (bstrProperty)
  2276. {
  2277. hr = pADs->Get(bstrProperty, &var);
  2278. if (SUCCEEDED(hr))
  2279. {
  2280. lstrcpyn (szDisplayName, var.bstrVal, ARRAYSIZE(szDisplayName));
  2281. }
  2282. SysFreeString (bstrProperty);
  2283. }
  2284. VariantClear (&var);
  2285. //
  2286. // Query for the options
  2287. //
  2288. VariantInit(&var);
  2289. bstrProperty = SysAllocString (GPO_OPTIONS_PROPERTY);
  2290. if (bstrProperty)
  2291. {
  2292. hr = pADs->Get(bstrProperty, &var);
  2293. if (SUCCEEDED(hr))
  2294. {
  2295. dwDisabled = var.lVal;
  2296. }
  2297. SysFreeString (bstrProperty);
  2298. }
  2299. VariantClear (&var);
  2300. if (FAILED(CheckDSWriteAccess((LPUNKNOWN) pADs, GPO_VERSION_PROPERTY)))
  2301. {
  2302. bReadOnly = TRUE;
  2303. }
  2304. pADs->Release();
  2305. }
  2306. else
  2307. {
  2308. if (hr == HRESULT_FROM_WIN32(ERROR_DS_NO_SUCH_OBJECT))
  2309. {
  2310. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyMgr::AddGPOs: Skipping link to deleted object. %s"), lpNamedGPO));
  2311. LocalFree (lpNamedGPO);
  2312. continue;
  2313. }
  2314. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::AddGPOs: OpenDSObject failed with 0x%x"), hr));
  2315. }
  2316. LocalFree (lpNamedGPO);
  2317. }
  2318. else
  2319. {
  2320. DebugMsg((DM_WARNING, TEXT("CGroupPolicyMgr::AddGPOs: Failed to get the full domain name for %s"), szGPO));
  2321. }
  2322. if (szDisplayName[0] == TEXT('\0'))
  2323. {
  2324. LoadString (g_hInstance, IDS_GPM_NOGPONAME, szDisplayName, ARRAYSIZE(szDisplayName));
  2325. dwDisabled = (GPO_OPTION_DISABLE_USER | GPO_OPTION_DISABLE_MACHINE);
  2326. bReadOnly = TRUE;
  2327. }
  2328. AddGPOToList (hLV, szDisplayName, szGPO, dwOptions, TRUE, dwDisabled, bReadOnly);
  2329. }
  2330. Exit:
  2331. return TRUE;
  2332. }
  2333. LPGPOITEM CGroupPolicyMgr::CreateEntry (LPTSTR szName, LPTSTR szGPO, DWORD dwOptions,
  2334. DWORD dwDisabled, BOOL bReadOnly)
  2335. {
  2336. LPGPOITEM lpItem;
  2337. DWORD dwSize;
  2338. TCHAR szFormat[20];
  2339. LPTSTR lpResult, lpName = szName, lpFullName = NULL;
  2340. LPOLESTR pszDomain;
  2341. //
  2342. // Check if the GPO is in this domain
  2343. //
  2344. pszDomain = GetDomainFromLDAPPath(szGPO);
  2345. if (pszDomain)
  2346. {
  2347. if (SUCCEEDED(ConvertToDotStyle (pszDomain, &lpResult)))
  2348. {
  2349. if (lstrcmpi(lpResult, m_lpDomainName) != 0)
  2350. {
  2351. LoadString (g_hInstance, IDS_GPM_DOMAINNAME, szFormat,
  2352. ARRAYSIZE(szFormat));
  2353. lpFullName = (LPTSTR) LocalAlloc (LPTR, (lstrlen (szName) +
  2354. lstrlen (szFormat) +
  2355. lstrlen (lpResult)) * sizeof(TCHAR));
  2356. if (lpFullName)
  2357. {
  2358. wsprintf (lpFullName, szFormat, szName, lpResult);
  2359. lpName = lpFullName;
  2360. }
  2361. }
  2362. LocalFree (lpResult);
  2363. }
  2364. delete [] pszDomain;
  2365. }
  2366. //
  2367. // Calculate the size needed and fill in the structure
  2368. //
  2369. dwSize = sizeof(GPOITEM);
  2370. dwSize += ((lstrlen(lpName) + 1) * sizeof(TCHAR));
  2371. dwSize += ((lstrlen(szName) + 1) * sizeof(TCHAR));
  2372. dwSize += ((lstrlen(szGPO) + 1) * sizeof(TCHAR));
  2373. lpItem = (LPGPOITEM) LocalAlloc (LPTR, dwSize);
  2374. if (!lpItem)
  2375. {
  2376. if (lpFullName)
  2377. {
  2378. LocalFree (lpFullName);
  2379. }
  2380. return NULL;
  2381. }
  2382. lpItem->lpDisplayName = (LPTSTR) (((LPBYTE)lpItem) + sizeof(GPOITEM));
  2383. lstrcpy (lpItem->lpDisplayName, lpName);
  2384. lpItem->lpGPOName = lpItem->lpDisplayName + lstrlen (lpItem->lpDisplayName) + 1;
  2385. lstrcpy (lpItem->lpGPOName, szName);
  2386. lpItem->lpDSPath = lpItem->lpGPOName + lstrlen (lpItem->lpGPOName) + 1;
  2387. lstrcpy (lpItem->lpDSPath, szGPO);
  2388. lpItem->dwOptions = dwOptions;
  2389. lpItem->dwDisabled = dwDisabled;
  2390. lpItem->bReadOnly = bReadOnly;
  2391. lpItem->bLocked = FALSE;
  2392. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyMgr::CreateEntry: Adding %s"), lpName));
  2393. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyMgr::CreateEntry: GPO Name %s"), szName));
  2394. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyMgr::CreateEntry: DS Path %s"), szGPO));
  2395. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyMgr::CreateEntry: Options %d"), dwOptions));
  2396. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyMgr::CreateEntry: Disabled %d"), dwDisabled));
  2397. DebugMsg((DM_VERBOSE, TEXT("CGroupPolicyMgr::CreateEntry: ReadOnly %d"), bReadOnly));
  2398. if (lpFullName)
  2399. {
  2400. LocalFree (lpFullName);
  2401. }
  2402. return lpItem;
  2403. }
  2404. BOOL CGroupPolicyMgr::AddGPOToList (HWND hLV, LPTSTR szName, LPTSTR szGPO,
  2405. DWORD dwOptions, BOOL bHighest,
  2406. DWORD dwDisabled, BOOL bReadOnly)
  2407. {
  2408. LPGPOITEM lpItem;
  2409. LV_ITEM item;
  2410. INT iItem;
  2411. //
  2412. // Create the link list entry
  2413. //
  2414. lpItem = CreateEntry (szName, szGPO, dwOptions, dwDisabled, bReadOnly);
  2415. if (!lpItem)
  2416. {
  2417. return FALSE;
  2418. }
  2419. //
  2420. // Add the item
  2421. //
  2422. item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_IMAGE;
  2423. item.iItem = (bHighest ? 0 : ListView_GetItemCount(hLV));
  2424. item.iSubItem = 0;
  2425. item.pszText = LPSTR_TEXTCALLBACK;
  2426. item.lParam = (LPARAM) lpItem;
  2427. if ((dwDisabled & GPO_OPTION_DISABLE_USER) && (dwDisabled & GPO_OPTION_DISABLE_MACHINE))
  2428. {
  2429. item.iImage = 1;
  2430. }
  2431. else if (dwDisabled == 0)
  2432. {
  2433. item.iImage = 0;
  2434. }
  2435. else
  2436. {
  2437. item.iImage = 2;
  2438. }
  2439. iItem = ListView_InsertItem (hLV, &item);
  2440. //
  2441. // Select the item
  2442. //
  2443. item.mask = LVIF_STATE;
  2444. item.iItem = iItem;
  2445. item.iSubItem = 0;
  2446. item.state = LVIS_SELECTED | LVIS_FOCUSED;
  2447. item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
  2448. SendMessage (hLV, LVM_SETITEMSTATE, 0, (LPARAM) &item);
  2449. return TRUE;
  2450. }
  2451. INT_PTR CALLBACK CGroupPolicyMgr::RemoveGPODlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  2452. {
  2453. switch (message)
  2454. {
  2455. case WM_INITDIALOG:
  2456. {
  2457. LPGPOITEM lpGPO = (LPGPOITEM)lParam;
  2458. TCHAR szBuffer[200];
  2459. LPTSTR lpTitle;
  2460. HICON hIcon;
  2461. hIcon = LoadIcon (NULL, IDI_QUESTION);
  2462. if (hIcon)
  2463. {
  2464. SendDlgItemMessage (hDlg, IDC_QUESTION, STM_SETICON, (WPARAM) hIcon, 0);
  2465. }
  2466. GetDlgItemText (hDlg, IDC_REMOVE_TITLE, szBuffer, ARRAYSIZE(szBuffer));
  2467. lpTitle = (LPTSTR) LocalAlloc (LPTR, (lstrlen(szBuffer) +
  2468. lstrlen(lpGPO->lpDisplayName) + 1) * sizeof(TCHAR));
  2469. if (lpTitle)
  2470. {
  2471. wsprintf (lpTitle, szBuffer, lpGPO->lpDisplayName);
  2472. SetDlgItemText (hDlg, IDC_REMOVE_TITLE, lpTitle);
  2473. LocalFree (lpTitle);
  2474. }
  2475. CheckDlgButton (hDlg, IDC_REMOVE_LIST, BST_CHECKED);
  2476. if (lpGPO->bReadOnly)
  2477. {
  2478. EnableWindow (GetDlgItem(hDlg, IDC_REMOVE_DS), FALSE);
  2479. }
  2480. }
  2481. break;
  2482. case WM_COMMAND:
  2483. switch (LOWORD(wParam))
  2484. {
  2485. case IDOK:
  2486. if (IsDlgButtonChecked (hDlg, IDC_REMOVE_LIST) == BST_CHECKED)
  2487. {
  2488. EndDialog (hDlg, 1);
  2489. }
  2490. else
  2491. {
  2492. EndDialog (hDlg, 2);
  2493. }
  2494. return TRUE;
  2495. case IDCANCEL:
  2496. EndDialog (hDlg, 0);
  2497. return TRUE;
  2498. }
  2499. break;
  2500. case WM_HELP: // F1
  2501. WinHelp((HWND)((LPHELPINFO) lParam)->hItemHandle, HELP_FILE, HELP_WM_HELP,
  2502. (ULONG_PTR) (LPSTR) aRemoveGPOHelpIds);
  2503. break;
  2504. case WM_CONTEXTMENU: // right mouse click
  2505. WinHelp((HWND) wParam, HELP_FILE, HELP_CONTEXTMENU,
  2506. (ULONG_PTR) (LPSTR) aRemoveGPOHelpIds);
  2507. return (TRUE);
  2508. }
  2509. return FALSE;
  2510. }
  2511. INT_PTR CALLBACK CGroupPolicyMgr::LinkOptionsDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  2512. {
  2513. switch (message)
  2514. {
  2515. case WM_INITDIALOG:
  2516. {
  2517. LPGPOITEM lpItem = (LPGPOITEM) lParam;
  2518. TCHAR szBuffer[50];
  2519. LPTSTR lpTitle;
  2520. SetWindowLongPtr (hDlg, DWLP_USER, (LONG_PTR) lpItem);
  2521. GetWindowText (hDlg, szBuffer, ARRAYSIZE(szBuffer));
  2522. lpTitle = (LPTSTR) LocalAlloc (LPTR, (lstrlen(szBuffer) +
  2523. lstrlen(lpItem->lpDisplayName) + 1) * sizeof(TCHAR));
  2524. if (lpTitle)
  2525. {
  2526. wsprintf (lpTitle, szBuffer, lpItem->lpDisplayName);
  2527. SetWindowText (hDlg, lpTitle);
  2528. LocalFree (lpTitle);
  2529. }
  2530. if (lpItem->dwOptions & GPO_FLAG_DISABLE)
  2531. {
  2532. CheckDlgButton (hDlg, IDC_GPM_DISABLED, BST_CHECKED);
  2533. }
  2534. if (lpItem->dwOptions & GPO_FLAG_FORCE)
  2535. {
  2536. CheckDlgButton (hDlg, IDC_GPM_NOOVERRIDE, BST_CHECKED);
  2537. }
  2538. }
  2539. break;
  2540. case WM_COMMAND:
  2541. switch (LOWORD(wParam))
  2542. {
  2543. case IDC_GPM_DISABLED:
  2544. {
  2545. if ((HIWORD(wParam) == BN_CLICKED) &&
  2546. (IsDlgButtonChecked (hDlg, IDC_GPM_DISABLED) == BST_CHECKED))
  2547. {
  2548. TCHAR szMessage[200];
  2549. TCHAR szTitle[100];
  2550. LoadString (g_hInstance, IDS_CONFIRMDISABLE, szMessage, ARRAYSIZE(szMessage));
  2551. LoadString (g_hInstance, IDS_CONFIRMTITLE2, szTitle, ARRAYSIZE(szTitle));
  2552. if (MessageBox (hDlg, szMessage, szTitle, MB_YESNO |
  2553. MB_ICONWARNING | MB_DEFBUTTON2) == IDNO) {
  2554. CheckDlgButton (hDlg, IDC_GPM_DISABLED, BST_UNCHECKED);
  2555. }
  2556. }
  2557. }
  2558. break;
  2559. case IDOK:
  2560. {
  2561. LPGPOITEM lpItem;
  2562. DWORD dwTemp = 0;
  2563. lpItem = (LPGPOITEM) GetWindowLongPtr (hDlg, DWLP_USER);
  2564. if (IsDlgButtonChecked (hDlg, IDC_GPM_DISABLED) == BST_CHECKED)
  2565. {
  2566. dwTemp |= GPO_FLAG_DISABLE;
  2567. }
  2568. if (IsDlgButtonChecked (hDlg, IDC_GPM_NOOVERRIDE) == BST_CHECKED)
  2569. {
  2570. dwTemp |= GPO_FLAG_FORCE;
  2571. }
  2572. if (dwTemp != lpItem->dwOptions)
  2573. {
  2574. lpItem->dwOptions = dwTemp;
  2575. EndDialog (hDlg, 1);
  2576. }
  2577. else
  2578. {
  2579. EndDialog (hDlg, 0);
  2580. }
  2581. }
  2582. return TRUE;
  2583. case IDCANCEL:
  2584. EndDialog (hDlg, 0);
  2585. return TRUE;
  2586. }
  2587. break;
  2588. case WM_HELP: // F1
  2589. WinHelp((HWND)((LPHELPINFO) lParam)->hItemHandle, HELP_FILE, HELP_WM_HELP,
  2590. (ULONG_PTR) (LPSTR) aLinkOptionsHelpIds);
  2591. break;
  2592. case WM_CONTEXTMENU: // right mouse click
  2593. WinHelp((HWND) wParam, HELP_FILE, HELP_CONTEXTMENU,
  2594. (ULONG_PTR) (LPSTR) aLinkOptionsHelpIds);
  2595. return (TRUE);
  2596. }
  2597. return FALSE;
  2598. }
  2599. INT_PTR CALLBACK CGroupPolicyMgr::NoDSObjectDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  2600. {
  2601. switch (message)
  2602. {
  2603. case WM_INITDIALOG:
  2604. {
  2605. CGroupPolicyMgr *pGPM = (CGroupPolicyMgr *)lParam;
  2606. TCHAR szBuffer[100];
  2607. TCHAR szTitle[300];
  2608. LPTSTR lpMsg, lpMsgEx;
  2609. INT iSize;
  2610. HWND hMsg = GetDlgItem (hDlg, IDC_NODSOBJECT_TEXT);
  2611. HICON hIcon;
  2612. hIcon = LoadIcon (NULL, IDI_ERROR);
  2613. if (hIcon)
  2614. {
  2615. SendDlgItemMessage (hDlg, IDC_NODSOBJECT_ICON, STM_SETICON, (WPARAM) hIcon, 0);
  2616. }
  2617. GetWindowText (hDlg, szBuffer, ARRAYSIZE(szBuffer));
  2618. wsprintf (szTitle, szBuffer, pGPM->m_lpGPODCName);
  2619. SetWindowText (hDlg, szTitle);
  2620. iSize = 600;
  2621. iSize += lstrlen(pGPM->m_lpGPODCName);
  2622. iSize += lstrlen(pGPM->m_lpDSADCName);
  2623. iSize++;
  2624. lpMsg = (LPTSTR) LocalAlloc(LPTR, iSize * sizeof(TCHAR));
  2625. if (lpMsg)
  2626. {
  2627. lpMsgEx = (LPTSTR) LocalAlloc(LPTR, iSize * sizeof(TCHAR));
  2628. if (lpMsgEx)
  2629. {
  2630. LoadString (g_hInstance, IDS_NODSOBJECT_MSG, lpMsg, iSize);
  2631. wsprintf (lpMsgEx, lpMsg, pGPM->m_lpGPODCName, pGPM->m_lpDSADCName);
  2632. SetWindowText (hMsg, lpMsgEx);
  2633. LocalFree (lpMsgEx);
  2634. }
  2635. LocalFree (lpMsg);
  2636. }
  2637. }
  2638. break;
  2639. case WM_COMMAND:
  2640. switch (LOWORD(wParam))
  2641. {
  2642. case IDOK:
  2643. case IDCANCEL:
  2644. EndDialog (hDlg, 1);
  2645. return TRUE;
  2646. case IDHELP:
  2647. WinExec("hh.exe spconcepts.chm::/sag_spTShoot.htm", SW_SHOWNORMAL);
  2648. break;
  2649. }
  2650. break;
  2651. }
  2652. return FALSE;
  2653. }
  2654. DWORD CGroupPolicyMgr::IsGPODisabled(LPTSTR lpGPO)
  2655. {
  2656. HRESULT hr;
  2657. LPTSTR lpTemp;
  2658. DWORD dwOptions = 0;
  2659. LPGROUPPOLICYOBJECT pGPO = NULL;
  2660. //
  2661. // Create a GroupPolicyObject to work with
  2662. //
  2663. hr = CoCreateInstance (CLSID_GroupPolicyObject, NULL,
  2664. CLSCTX_SERVER, IID_IGroupPolicyObject,
  2665. (void**)&pGPO);
  2666. if (FAILED(hr))
  2667. {
  2668. DebugMsg((DM_WARNING, TEXT("IsGPODisabled: Failed to create GPO object with 0x%x."), hr));
  2669. return 0;
  2670. }
  2671. lpTemp = GetFullGPOPath (lpGPO, NULL);
  2672. if (!lpTemp)
  2673. {
  2674. pGPO->Release();
  2675. return 0;
  2676. }
  2677. //
  2678. // Open the requested object without mounting the registry
  2679. //
  2680. hr = pGPO->OpenDSGPO(lpTemp, GPO_OPEN_READ_ONLY);
  2681. LocalFree (lpTemp);
  2682. if (FAILED(hr))
  2683. {
  2684. DebugMsg((DM_WARNING, TEXT("IsGPODisabled: Failed to open GPO object with 0x%x."), hr));
  2685. pGPO->Release();
  2686. return 0;
  2687. }
  2688. //
  2689. // Get the options
  2690. //
  2691. hr = pGPO->GetOptions(&dwOptions);
  2692. if (FAILED(hr))
  2693. {
  2694. DebugMsg((DM_WARNING, TEXT("IsGPODisabled: Failed to get GPO options with 0x%x."), hr));
  2695. pGPO->Release();
  2696. return 0;
  2697. }
  2698. pGPO->Release();
  2699. return dwOptions;
  2700. }
  2701. void CGroupPolicyMgr::CheckIconStatus (HWND hLV, LPGPOITEM lpGPO)
  2702. {
  2703. LPGPOITEM lpItem;
  2704. LV_ITEM item;
  2705. INT iIndex = -1;
  2706. DWORD dwDisabled;
  2707. //
  2708. // Check if the GPO disabled
  2709. //
  2710. dwDisabled = IsGPODisabled(lpGPO->lpDSPath);
  2711. //
  2712. // If the status hasn't changed, exit now
  2713. //
  2714. if (dwDisabled == lpGPO->dwDisabled)
  2715. {
  2716. return;
  2717. }
  2718. //
  2719. // Enumerate through the items
  2720. //
  2721. while ((iIndex = ListView_GetNextItem (hLV, iIndex, LVNI_ALL)) != -1)
  2722. {
  2723. item.mask = LVIF_PARAM;
  2724. item.iItem = iIndex;
  2725. item.iSubItem = 0;
  2726. if (!ListView_GetItem (hLV, &item))
  2727. {
  2728. continue;
  2729. }
  2730. lpItem = (LPGPOITEM) item.lParam;
  2731. //
  2732. // If the item has a path to the GPO in question,
  2733. // update the icon
  2734. //
  2735. if (!lstrcmpi (lpItem->lpDSPath, lpGPO->lpDSPath))
  2736. {
  2737. //
  2738. // Update the icon if appropriate
  2739. //
  2740. item.mask = LVIF_IMAGE;
  2741. item.iItem = iIndex;
  2742. item.iSubItem = 0;
  2743. if ((dwDisabled & GPO_OPTION_DISABLE_USER) && (dwDisabled & GPO_OPTION_DISABLE_MACHINE))
  2744. {
  2745. item.iImage = 1;
  2746. }
  2747. else if (dwDisabled == 0)
  2748. {
  2749. item.iImage = 0;
  2750. }
  2751. else
  2752. {
  2753. item.iImage = 2;
  2754. }
  2755. SendMessage (hLV, LVM_SETITEM, 0, (LPARAM) &item);
  2756. ListView_RedrawItems (hLV, iIndex, iIndex);
  2757. }
  2758. }
  2759. lpGPO->dwDisabled = dwDisabled;
  2760. }
  2761. LPTSTR CGroupPolicyMgr::GetFullGPOPath (LPTSTR lpGPO, HWND hParent)
  2762. {
  2763. LPTSTR lpFullPath = NULL, lpDomainName = NULL;
  2764. LPTSTR lpGPODCName;
  2765. LPOLESTR pszDomain;
  2766. HRESULT hr;
  2767. //
  2768. // Get the friendly domain name
  2769. //
  2770. pszDomain = GetDomainFromLDAPPath(lpGPO);
  2771. if (!pszDomain)
  2772. {
  2773. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetFullGPOPath: Failed to get domain name")));
  2774. return NULL;
  2775. }
  2776. //
  2777. // Convert LDAP to dot (DN) style
  2778. //
  2779. hr = ConvertToDotStyle (pszDomain, &lpDomainName);
  2780. delete [] pszDomain;
  2781. if (FAILED(hr))
  2782. {
  2783. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::CreatePropertyPages: Failed to convert domain name with 0x%x"), hr));
  2784. return NULL;
  2785. }
  2786. if (!lstrcmpi(lpDomainName, m_lpDomainName))
  2787. {
  2788. //
  2789. // Make the full path
  2790. //
  2791. lpFullPath = MakeFullPath (lpGPO, m_lpGPODCName);
  2792. if (!lpFullPath)
  2793. {
  2794. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetFullGPOPath: Failed to build new DS object path")));
  2795. goto Exit;
  2796. }
  2797. }
  2798. else
  2799. {
  2800. //
  2801. // Get the GPO DC for this domain
  2802. //
  2803. lpGPODCName = GetDCName (lpDomainName, NULL, hParent, TRUE, 0);
  2804. if (!lpGPODCName)
  2805. {
  2806. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetFullGPOPath: Failed to get DC name for %s"),
  2807. lpDomainName));
  2808. goto Exit;
  2809. }
  2810. //
  2811. // Make the full path
  2812. //
  2813. lpFullPath = MakeFullPath (lpGPO, lpGPODCName);
  2814. LocalFree (lpGPODCName);
  2815. if (!lpFullPath)
  2816. {
  2817. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::GetFullGPOPath: Failed to build new DS object path")));
  2818. goto Exit;
  2819. }
  2820. }
  2821. Exit:
  2822. if (lpDomainName)
  2823. {
  2824. LocalFree (lpDomainName);
  2825. }
  2826. return lpFullPath;
  2827. }
  2828. BOOL CGroupPolicyMgr::StartGPE (LPTSTR lpGPO, HWND hParent)
  2829. {
  2830. LPTSTR lpDomainName;
  2831. LPOLESTR pszDomain;
  2832. HRESULT hr;
  2833. BOOL bResult;
  2834. //
  2835. // Get the friendly domain name
  2836. //
  2837. pszDomain = GetDomainFromLDAPPath(lpGPO);
  2838. if (!pszDomain)
  2839. {
  2840. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::StartGPE: Failed to get domain name")));
  2841. return FALSE;
  2842. }
  2843. //
  2844. // Convert LDAP to dot (DN) style
  2845. //
  2846. hr = ConvertToDotStyle (pszDomain, &lpDomainName);
  2847. delete [] pszDomain;
  2848. if (FAILED(hr))
  2849. {
  2850. DebugMsg((DM_WARNING, TEXT("CGroupPolicyObject::CreatePropertyPages: Failed to convert domain name with 0x%x"), hr));
  2851. return FALSE;
  2852. }
  2853. //
  2854. // Check if the GPO is in the same domain as GPM is focused on
  2855. //
  2856. if (!lstrcmpi(lpDomainName, m_lpDomainName))
  2857. {
  2858. bResult = SpawnGPE (lpGPO, m_gpHint, m_lpGPODCName, hParent);
  2859. }
  2860. else
  2861. {
  2862. bResult = SpawnGPE (lpGPO, m_gpHint, NULL, hParent);
  2863. }
  2864. LocalFree (lpDomainName);
  2865. return bResult;
  2866. }
  2867. ///////////////////////////////////////////////////////////////////////////////
  2868. // //
  2869. // Class factory object implementation //
  2870. // //
  2871. ///////////////////////////////////////////////////////////////////////////////
  2872. CGroupPolicyMgrCF::CGroupPolicyMgrCF()
  2873. {
  2874. m_cRef = 1;
  2875. InterlockedIncrement(&g_cRefThisDll);
  2876. }
  2877. CGroupPolicyMgrCF::~CGroupPolicyMgrCF()
  2878. {
  2879. InterlockedDecrement(&g_cRefThisDll);
  2880. }
  2881. ///////////////////////////////////////////////////////////////////////////////
  2882. // //
  2883. // Class factory object implementation (IUnknown) //
  2884. // //
  2885. ///////////////////////////////////////////////////////////////////////////////
  2886. STDMETHODIMP_(ULONG)
  2887. CGroupPolicyMgrCF::AddRef()
  2888. {
  2889. return ++m_cRef;
  2890. }
  2891. STDMETHODIMP_(ULONG)
  2892. CGroupPolicyMgrCF::Release()
  2893. {
  2894. if (--m_cRef == 0)
  2895. {
  2896. delete this;
  2897. return 0;
  2898. }
  2899. return m_cRef;
  2900. }
  2901. STDMETHODIMP
  2902. CGroupPolicyMgrCF::QueryInterface(REFIID riid, LPVOID FAR* ppv)
  2903. {
  2904. if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IClassFactory))
  2905. {
  2906. *ppv = (LPCLASSFACTORY)this;
  2907. m_cRef++;
  2908. return S_OK;
  2909. }
  2910. else
  2911. {
  2912. *ppv = NULL;
  2913. return E_NOINTERFACE;
  2914. }
  2915. }
  2916. ///////////////////////////////////////////////////////////////////////////////
  2917. // //
  2918. // Class factory object implementation (IClassFactory) //
  2919. // //
  2920. ///////////////////////////////////////////////////////////////////////////////
  2921. STDMETHODIMP
  2922. CGroupPolicyMgrCF::CreateInstance(LPUNKNOWN pUnkOuter,
  2923. REFIID riid,
  2924. LPVOID FAR* ppvObj)
  2925. {
  2926. *ppvObj = NULL;
  2927. if (pUnkOuter)
  2928. return CLASS_E_NOAGGREGATION;
  2929. CGroupPolicyMgr *pComponentData = new CGroupPolicyMgr(); // ref count == 1
  2930. if (!pComponentData)
  2931. return E_OUTOFMEMORY;
  2932. HRESULT hr = pComponentData->QueryInterface(riid, ppvObj);
  2933. pComponentData->Release(); // release initial ref
  2934. return hr;
  2935. }
  2936. STDMETHODIMP
  2937. CGroupPolicyMgrCF::LockServer(BOOL fLock)
  2938. {
  2939. return E_NOTIMPL;
  2940. }