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.

614 lines
20 KiB

  1. #include <windows.h>
  2. #include <ole2.h>
  3. #include <userenv.h>
  4. #define SECURITY_WIN32
  5. #include <security.h>
  6. #include <dsgetdc.h>
  7. #include <lm.h>
  8. #include <shellapi.h>
  9. #include <commdlg.h>
  10. #include "poltest.h"
  11. HINSTANCE hInst;
  12. HWND hwndMain;
  13. HWND hwndListBox;
  14. HMENU hMenu;
  15. HANDLE hMachineSection;
  16. HANDLE hUserSection;
  17. HANDLE hMachineEvent;
  18. HANDLE hUserEvent;
  19. HANDLE hExit;
  20. HANDLE hThread;
  21. BOOL bVerbose = FALSE;
  22. BOOL bSync = FALSE;
  23. TCHAR szAppName[] = TEXT("PolTest");
  24. TCHAR szTitle[] = TEXT("Group Policy Tester");
  25. /****************************************************************************
  26. FUNCTION: WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  27. PURPOSE: calls initialization function, processes message loop
  28. ****************************************************************************/
  29. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  30. LPSTR lpCmdLine, INT nCmdShow)
  31. {
  32. MSG msg;
  33. HANDLE hAccelTable;
  34. if (!hPrevInstance)
  35. {
  36. if (!InitApplication(hInstance))
  37. {
  38. return (FALSE);
  39. }
  40. }
  41. // Perform initializations that apply to a specific instance
  42. if (!InitInstance(hInstance, nCmdShow))
  43. {
  44. return (FALSE);
  45. }
  46. hAccelTable = LoadAccelerators (hInstance, szAppName);
  47. while (GetMessage(&msg, NULL, 0, 0))
  48. {
  49. if (!TranslateAccelerator (msg.hwnd, hAccelTable, &msg))
  50. {
  51. TranslateMessage(&msg);
  52. DispatchMessage(&msg);
  53. }
  54. }
  55. SetEvent (hExit);
  56. WaitForSingleObject (hThread, INFINITE);
  57. CloseHandle (hExit);
  58. CloseHandle (hMachineEvent);
  59. CloseHandle (hUserEvent);
  60. CloseHandle (hThread);
  61. return (msg.wParam);
  62. lpCmdLine;
  63. }
  64. /****************************************************************************
  65. FUNCTION: InitApplication(HINSTANCE)
  66. PURPOSE: Initializes window data and registers window class
  67. ****************************************************************************/
  68. BOOL InitApplication(HINSTANCE hInstance)
  69. {
  70. WNDCLASS wc;
  71. hExit = CreateEvent (NULL, FALSE, FALSE, TEXT("poltest: exit event"));
  72. if (!hExit) {
  73. AddString (TEXT("failed to create exit event"));
  74. }
  75. hMachineEvent = CreateEvent (NULL, FALSE, FALSE, TEXT("poltest: machine policy event"));
  76. if (!hMachineEvent) {
  77. AddString (TEXT("failed to create machine notify event"));
  78. }
  79. hUserEvent = CreateEvent (NULL, FALSE, FALSE, TEXT("poltest: user policy event"));
  80. if (!hUserEvent) {
  81. AddString (TEXT("failed to create user notify event"));
  82. }
  83. wc.style = CS_HREDRAW | CS_VREDRAW;
  84. wc.lpfnWndProc = WndProc;
  85. wc.cbClsExtra = 0;
  86. wc.cbWndExtra = 0;
  87. wc.hInstance = hInstance;
  88. wc.hIcon = LoadIcon (hInstance, szAppName);
  89. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  90. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  91. wc.lpszMenuName = szAppName;
  92. wc.lpszClassName = szAppName;
  93. return (RegisterClass(&wc));
  94. }
  95. /****************************************************************************
  96. FUNCTION: InitInstance(HINSTANCE, int)
  97. PURPOSE: Saves instance handle and creates main window
  98. ****************************************************************************/
  99. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  100. {
  101. HWND hWnd;
  102. HKEY hKey;
  103. DWORD dwSize,dwType;
  104. hInst = hInstance;
  105. hWnd = CreateWindow(szAppName,
  106. szTitle,
  107. WS_OVERLAPPEDWINDOW,
  108. CW_USEDEFAULT, 0, 500, 500,
  109. NULL,
  110. NULL,
  111. hInstance,
  112. NULL);
  113. if (!hWnd)
  114. {
  115. return (FALSE);
  116. }
  117. else
  118. {
  119. hwndMain = hWnd;
  120. }
  121. ShowWindow(hWnd, SW_SHOWDEFAULT);
  122. UpdateWindow(hWnd);
  123. if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\Diagnostics"),
  124. 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
  125. dwSize = sizeof(bVerbose);
  126. RegQueryValueEx (hKey, TEXT("RunDiagnosticLoggingGroupPolicy"),
  127. NULL, &dwType, (LPBYTE) &bVerbose, &dwSize);
  128. RegCloseKey (hKey);
  129. }
  130. CheckMenuItem (GetMenu(hWnd), IDM_VERBOSE, MF_BYCOMMAND |
  131. (bVerbose ? MF_CHECKED : MF_UNCHECKED));
  132. if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"),
  133. 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
  134. dwSize = sizeof(bSync);
  135. RegQueryValueEx (hKey, TEXT("SynchronousGroupPolicy"),
  136. NULL, &dwType, (LPBYTE) &bSync, &dwSize);
  137. RegCloseKey (hKey);
  138. }
  139. CheckMenuItem (GetMenu(hWnd), IDM_SYNCPOLICY, MF_BYCOMMAND |
  140. (bSync ? MF_CHECKED : MF_UNCHECKED));
  141. return (TRUE);
  142. }
  143. /****************************************************************************
  144. FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  145. PURPOSE: Processes messages
  146. ****************************************************************************/
  147. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  148. {
  149. DWORD dwID;
  150. switch (message)
  151. {
  152. case WM_CREATE:
  153. {
  154. RECT rc;
  155. GetClientRect (hWnd, &rc);
  156. hMenu = GetMenu(hWnd);
  157. EnableMenuItem (hMenu, IDM_RESUME_MACHINE, MF_BYCOMMAND | MF_GRAYED);
  158. EnableMenuItem (hMenu, IDM_RESUME_USER, MF_BYCOMMAND | MF_GRAYED);
  159. hwndListBox = CreateWindow(TEXT("LISTBOX"),
  160. NULL,
  161. WS_CHILDWINDOW | WS_VSCROLL | WS_VISIBLE | LBS_NOINTEGRALHEIGHT,
  162. 0, 0, rc.right, rc.bottom,
  163. hWnd,
  164. NULL,
  165. hInst,
  166. NULL);
  167. SendMessage (hwndListBox, WM_SETFONT, (WPARAM) GetStockObject(ANSI_VAR_FONT), 0);
  168. hThread = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) NotifyThread,
  169. 0, 0, &dwID);
  170. }
  171. break;
  172. case WM_COMMAND:
  173. {
  174. switch (LOWORD(wParam))
  175. {
  176. case IDM_UPDATE_MACHINE:
  177. RefreshPolicy (TRUE);
  178. break;
  179. case IDM_UPDATE_USER:
  180. RefreshPolicy (FALSE);
  181. break;
  182. case IDM_PAUSE_MACHINE:
  183. hMachineSection = EnterCriticalPolicySection (TRUE);
  184. if (hMachineSection) {
  185. EnableMenuItem (hMenu, IDM_RESUME_MACHINE, MF_BYCOMMAND | MF_ENABLED);
  186. EnableMenuItem (hMenu, IDM_PAUSE_MACHINE, MF_BYCOMMAND | MF_GRAYED);
  187. } else {
  188. AddString (TEXT("Failed to claim critical section."));
  189. }
  190. break;
  191. case IDM_RESUME_MACHINE:
  192. LeaveCriticalPolicySection (hMachineSection);
  193. hMachineSection = NULL;
  194. EnableMenuItem (hMenu, IDM_RESUME_MACHINE, MF_BYCOMMAND | MF_GRAYED);
  195. EnableMenuItem (hMenu, IDM_PAUSE_MACHINE, MF_BYCOMMAND | MF_ENABLED);
  196. break;
  197. case IDM_PAUSE_USER:
  198. hUserSection = EnterCriticalPolicySection (FALSE);
  199. if (hUserSection) {
  200. EnableMenuItem (hMenu, IDM_RESUME_USER, MF_BYCOMMAND | MF_ENABLED);
  201. EnableMenuItem (hMenu, IDM_PAUSE_USER, MF_BYCOMMAND | MF_GRAYED);
  202. } else {
  203. AddString (TEXT("Failed to claim critical section."));
  204. }
  205. break;
  206. case IDM_RESUME_USER:
  207. LeaveCriticalPolicySection (hUserSection);
  208. hUserSection = NULL;
  209. EnableMenuItem (hMenu, IDM_RESUME_USER, MF_BYCOMMAND | MF_GRAYED);
  210. EnableMenuItem (hMenu, IDM_PAUSE_USER, MF_BYCOMMAND | MF_ENABLED);
  211. break;
  212. case IDM_EXIT:
  213. DestroyWindow (hwndMain);
  214. break;
  215. case IDM_VERBOSE:
  216. {
  217. HKEY hKey;
  218. DWORD dwDisp, dwTemp;
  219. TCHAR szMsg[MAX_PATH];
  220. LONG lResult;
  221. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\Diagnostics"),
  222. 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE,
  223. NULL, &hKey, &dwDisp);
  224. if (lResult == ERROR_SUCCESS) {
  225. bVerbose = !bVerbose;
  226. RegSetValueEx (hKey, TEXT("RunDiagnosticLoggingGroupPolicy"),
  227. 0, REG_DWORD, (LPBYTE) &bVerbose, sizeof(bVerbose));
  228. RegCloseKey (hKey);
  229. CheckMenuItem (GetMenu(hWnd), IDM_VERBOSE, MF_BYCOMMAND |
  230. (bVerbose ? MF_CHECKED : MF_UNCHECKED));
  231. } else {
  232. szMsg[0] = TEXT('\0');
  233. FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
  234. NULL, lResult,
  235. MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
  236. szMsg, MAX_PATH, NULL);
  237. MessageBox (hWnd, szMsg, TEXT("Error setting registry value"),
  238. MB_OK | MB_ICONERROR);
  239. }
  240. }
  241. break;
  242. case IDM_EVENTVWR:
  243. ShellExecute (NULL, TEXT("open"), TEXT("eventvwr.msc"), NULL,
  244. NULL, SW_SHOWNORMAL);
  245. break;
  246. case IDM_USERNAME:
  247. {
  248. TCHAR szName[512];
  249. DWORD dwSize = 512;
  250. if (GetUserNameEx(NameFullyQualifiedDN, szName, &dwSize)) {
  251. AddString (szName);
  252. } else {
  253. TCHAR szError[100];
  254. wsprintf (szError, TEXT("Failed to get user name with %d"), GetLastError());
  255. AddString (szError);
  256. }
  257. }
  258. break;
  259. case IDM_COMPUTERNAME:
  260. {
  261. TCHAR szName[512];
  262. DWORD dwSize = 512;
  263. if (GetComputerObjectName(NameFullyQualifiedDN, szName, &dwSize)) {
  264. AddString (szName);
  265. } else {
  266. TCHAR szError[100];
  267. wsprintf (szError, TEXT("Failed to get computer name with %d"), GetLastError());
  268. AddString (szError);
  269. }
  270. }
  271. break;
  272. case IDM_SITENAME:
  273. {
  274. LPTSTR lpName;
  275. ULONG ulResult;
  276. ulResult = DsGetSiteName (NULL, &lpName);
  277. if (ulResult == ERROR_SUCCESS) {
  278. AddString (lpName);
  279. NetApiBufferFree (lpName);
  280. } else {
  281. TCHAR szError[100];
  282. wsprintf (szError, TEXT("Failed to get site name with %d"), ulResult);
  283. AddString (szError);
  284. }
  285. }
  286. break;
  287. case IDM_PDCNAME:
  288. {
  289. DWORD dwResult;
  290. PDOMAIN_CONTROLLER_INFO pDCI;
  291. dwResult = DsGetDcName (NULL, NULL, NULL, NULL,
  292. DS_DIRECTORY_SERVICE_REQUIRED | DS_PDC_REQUIRED,
  293. &pDCI);
  294. if (dwResult == ERROR_SUCCESS) {
  295. AddString (pDCI->DomainControllerName);
  296. NetApiBufferFree(pDCI);
  297. } else {
  298. TCHAR szBuffer[100];
  299. wsprintf (szBuffer, TEXT("Failed to get the PDC name with %d"), dwResult);
  300. AddString (szBuffer);
  301. }
  302. }
  303. break;
  304. case IDM_SYNCPOLICY:
  305. {
  306. HKEY hKey;
  307. DWORD dwDisp, dwTemp;
  308. TCHAR szMsg[MAX_PATH];
  309. LONG lResult;
  310. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"),
  311. 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE,
  312. NULL, &hKey, &dwDisp);
  313. if (lResult == ERROR_SUCCESS) {
  314. bSync = !bSync;
  315. RegSetValueEx (hKey, TEXT("SynchronousGroupPolicy"),
  316. 0, REG_DWORD, (LPBYTE) &bSync, sizeof(bSync));
  317. RegCloseKey (hKey);
  318. CheckMenuItem (GetMenu(hWnd), IDM_SYNCPOLICY, MF_BYCOMMAND |
  319. (bSync ? MF_CHECKED : MF_UNCHECKED));
  320. AddString (TEXT("You need to reboot for this change to take effect."));
  321. } else {
  322. szMsg[0] = TEXT('\0');
  323. FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
  324. NULL, lResult,
  325. MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
  326. szMsg, MAX_PATH, NULL);
  327. MessageBox (hWnd, szMsg, TEXT("Error setting registry value"),
  328. MB_OK | MB_ICONERROR);
  329. }
  330. }
  331. break;
  332. case IDM_DCLIST:
  333. ManageDomainInfo(hWnd);
  334. break;
  335. case IDM_CHECKGPO:
  336. CheckGPO(hWnd);
  337. break;
  338. case IDM_CLEARWINDOW:
  339. SendMessage (hwndListBox, LB_RESETCONTENT, 0, 0);
  340. break;
  341. case IDM_SAVEAS:
  342. {
  343. HANDLE hFile;
  344. INT iCount, i;
  345. DWORD dwWrite;
  346. TCHAR szFilter[100];
  347. TCHAR szFileName[MAX_PATH];
  348. CHAR szString[MAX_PATH];
  349. TCHAR szError[100];
  350. CHAR szLF[] = "\r\n";
  351. OPENFILENAME ofn;
  352. iCount = SendMessage (hwndListBox, LB_GETCOUNT, 0, 0);
  353. if (iCount == LB_ERR) {
  354. break;
  355. }
  356. lstrcpy (szFileName, TEXT("Poltest Results.txt"));
  357. ZeroMemory (szFilter, sizeof(szFilter));
  358. lstrcpy (szFilter, TEXT("Text Files#*.txt"));
  359. szFilter[10] = TEXT('\0');
  360. ZeroMemory (&ofn, sizeof(ofn));
  361. ofn.lStructSize = sizeof(ofn);
  362. ofn.hwndOwner = hWnd;
  363. ofn.hInstance = hInst;
  364. ofn.lpstrFile = szFileName;
  365. ofn.nMaxFile = MAX_PATH;
  366. ofn.lpstrFilter = szFilter;
  367. ofn.lpstrDefExt = TEXT("txt");
  368. ofn.Flags = OFN_CREATEPROMPT | OFN_ENABLESIZING | OFN_HIDEREADONLY |
  369. OFN_OVERWRITEPROMPT;
  370. if (!GetSaveFileName (&ofn)) {
  371. break;
  372. }
  373. hFile = CreateFile (szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
  374. FILE_ATTRIBUTE_NORMAL, NULL);
  375. if (hFile == INVALID_HANDLE_VALUE) {
  376. wsprintf (szError, TEXT("Failed to open file with %d"), GetLastError());
  377. AddString (szError);
  378. break;
  379. }
  380. for (i = 0; i < iCount; i++) {
  381. szString[0] = '\0';
  382. SendMessageA (hwndListBox, LB_GETTEXT, i, (LPARAM)szString);
  383. WriteFile (hFile, szString, lstrlenA(szString),
  384. &dwWrite, NULL);
  385. WriteFile (hFile, szLF, lstrlenA(szLF),
  386. &dwWrite, NULL);
  387. }
  388. CloseHandle (hFile);
  389. }
  390. break;
  391. default:
  392. return (DefWindowProc(hWnd, message, wParam, lParam));
  393. }
  394. }
  395. break;
  396. case WM_WININICHANGE:
  397. if (!lstrcmpi ((LPTSTR)lParam, TEXT("Policy"))) {
  398. if (wParam) {
  399. AddString (TEXT("Received WM_WININICHANGE: machine policy applied."));
  400. } else {
  401. AddString (TEXT("Received WM_WININICHANGE: user policy applied."));
  402. }
  403. }
  404. break;
  405. case WM_SIZE:
  406. SetWindowPos (hwndListBox, HWND_TOP, 0, 0,
  407. LOWORD(lParam), HIWORD(lParam),
  408. SWP_NOMOVE | SWP_NOZORDER);
  409. break;
  410. case WM_DESTROY:
  411. PostQuitMessage(0);
  412. break;
  413. default:
  414. return (DefWindowProc(hWnd, message, wParam, lParam));
  415. }
  416. return FALSE;
  417. }
  418. VOID AddString (LPTSTR lpString)
  419. {
  420. SYSTEMTIME systime;
  421. TCHAR szString[300];
  422. GetLocalTime (&systime);
  423. wsprintf (szString, TEXT("%02d:%02d:%02d:%03d %s"),
  424. systime.wHour, systime.wMinute, systime.wSecond,
  425. systime.wMilliseconds, lpString);
  426. SendMessage (hwndListBox, LB_INSERTSTRING, -1,
  427. (LPARAM) szString);
  428. }
  429. DWORD NotifyThread (DWORD dwDummy)
  430. {
  431. HANDLE hHandles[3];
  432. DWORD dwResult;
  433. RegisterGPNotification(hMachineEvent, TRUE);
  434. RegisterGPNotification(hUserEvent, FALSE);
  435. hHandles[0] = hExit;
  436. hHandles[1] = hMachineEvent;
  437. hHandles[2] = hUserEvent;
  438. while (TRUE) {
  439. dwResult = WaitForMultipleObjects (3, hHandles, FALSE, INFINITE);
  440. if ((dwResult == WAIT_FAILED) || ((dwResult - WAIT_OBJECT_0) == 0)) {
  441. if (dwResult == WAIT_FAILED) {
  442. AddString (TEXT("WaitForMultipleObjects failed."));
  443. }
  444. break;
  445. }
  446. if ((dwResult - WAIT_OBJECT_0) == 1) {
  447. AddString (TEXT("Machine notify event signaled."));
  448. } else {
  449. AddString (TEXT("User notify event signaled."));
  450. }
  451. }
  452. UnregisterGPNotification(hMachineEvent);
  453. UnregisterGPNotification(hUserEvent);
  454. return 0;
  455. }