Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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