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.

603 lines
16 KiB

  1. #ifndef RC_INVOKED
  2. #include <nt.h>
  3. #include <ntrtl.h>
  4. #include <nturtl.h>
  5. #endif
  6. #include <windows.h>
  7. #include <tchar.h>
  8. #include <lm.h>
  9. #include <ole2.h>
  10. #include <olectl.h>
  11. #include <shellapi.h>
  12. #include <shlobj.h>
  13. #include <commctrl.h>
  14. #include <prsht.h>
  15. #include <dsclient.h>
  16. #include <dsgetdc.h>
  17. #include <mmc.h>
  18. #include <initguid.h>
  19. #include <gpedit.h>
  20. #define _USERENV_NO_LINK_APIS_ 1
  21. #include <userenv.h>
  22. #include <userenvp.h>
  23. #include "poltest.h"
  24. typedef struct _GPOINFO {
  25. TCHAR szGPOName[50];
  26. DWORD dwFlags;
  27. } GPOINFO, *LPGPOINFO;
  28. #define GPO_FLAG_VERSION 1
  29. #define GPO_FLAG_REGISTRY 2
  30. LPTSTR CheckSlash (LPTSTR lpDir);
  31. //*************************************************************
  32. //
  33. // GetDCList()
  34. //
  35. // Purpose: Reads the DC list from the registry
  36. //
  37. // Parameters:
  38. //
  39. //
  40. // Return: TRUE if successful
  41. // FALSE if an error occurs
  42. //
  43. //*************************************************************
  44. LPTSTR GetDCList (BOOL bZero)
  45. {
  46. HKEY hKey;
  47. LPTSTR lpList = NULL;
  48. LPTSTR lpTemp;
  49. DWORD dwType, dwSize;
  50. lpList = (LPTSTR) LocalAlloc (LPTR, 256 * sizeof(TCHAR));
  51. if (!lpList)
  52. return NULL;
  53. if (RegOpenKeyEx (HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Poltest"),
  54. 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
  55. dwSize = 256 * sizeof(TCHAR);
  56. RegQueryValueEx (hKey, TEXT("DCList"), NULL, &dwType,
  57. (LPBYTE) lpList, &dwSize);
  58. RegCloseKey (hKey);
  59. }
  60. if (bZero) {
  61. lpTemp = lpList;
  62. while (*lpTemp) {
  63. if (*lpTemp == TEXT(';')) {
  64. *lpTemp = TEXT('\0');
  65. }
  66. lpTemp++;
  67. }
  68. }
  69. if (*lpList == TEXT('\0')) {
  70. LocalFree (lpList);
  71. return NULL;
  72. }
  73. return lpList;
  74. }
  75. //*************************************************************
  76. //
  77. // GetDomainName()
  78. //
  79. // Purpose: Reads the domain name from the registry
  80. //
  81. // Parameters:
  82. //
  83. //
  84. // Return: TRUE if successful
  85. // FALSE if an error occurs
  86. //
  87. //*************************************************************
  88. LPTSTR GetDomainName (BOOL bZero)
  89. {
  90. HKEY hKey;
  91. LPTSTR lpList = NULL;
  92. LPTSTR lpTemp;
  93. DWORD dwType, dwSize;
  94. lpList = (LPTSTR) LocalAlloc (LPTR, 256 * sizeof(TCHAR));
  95. if (!lpList)
  96. return NULL;
  97. if (RegOpenKeyEx (HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Poltest"),
  98. 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
  99. dwSize = 256 * sizeof(TCHAR);
  100. RegQueryValueEx (hKey, TEXT("DomainName"), NULL, &dwType,
  101. (LPBYTE) lpList, &dwSize);
  102. RegCloseKey (hKey);
  103. }
  104. if (bZero) {
  105. lpTemp = lpList;
  106. while (*lpTemp) {
  107. if (*lpTemp == TEXT('.')) {
  108. *lpTemp = TEXT('\0');
  109. }
  110. lpTemp++;
  111. }
  112. }
  113. if (*lpList == TEXT('\0')) {
  114. LocalFree (lpList);
  115. return NULL;
  116. }
  117. return lpList;
  118. }
  119. //*************************************************************
  120. //
  121. // CheckSpecificGPO()
  122. //
  123. // Purpose: Checks a specific GPO's for the requested
  124. // attributes
  125. //
  126. // Parameters: lpGPOName - gpo name
  127. // dwFlags - GPO_FLAG_*
  128. //
  129. //
  130. // Return: TRUE if successful
  131. // FALSE if an error occurs
  132. //
  133. //*************************************************************
  134. BOOL CheckSpecificGPO (LPTSTR lpGPOName, DWORD dwFlags)
  135. {
  136. LPTSTR lpList;
  137. LPTSTR lpDomainName;
  138. LPTSTR lpDomainName2;
  139. LPTSTR lpDC, lpTemp;
  140. TCHAR szPath[MAX_PATH];
  141. TCHAR szLDAPPath[MAX_PATH];
  142. TCHAR szMsg[512];
  143. WIN32_FILE_ATTRIBUTE_DATA fad;
  144. DWORD dwFileSysVersion;
  145. LPTSTR lpEnd;
  146. FILETIME filetime;
  147. SYSTEMTIME systime;
  148. TCHAR szDate[50];
  149. TCHAR szTime[50];
  150. IADs *pADs;
  151. VARIANT var;
  152. BSTR bstrProperty;
  153. HRESULT hr;
  154. lpList = GetDCList (TRUE);
  155. if (!lpList) {
  156. AddString (TEXT("You need to enter the list of DC names"));
  157. return TRUE;
  158. }
  159. lpDomainName = GetDomainName (FALSE);
  160. if (!lpDomainName) {
  161. AddString (TEXT("You need to enter a domain name"));
  162. LocalFree (lpList);
  163. return TRUE;
  164. }
  165. CoInitialize(NULL);
  166. AddString (TEXT("============================================"));
  167. wsprintf (szMsg, TEXT("Listing GPO %s's information from all DCs"), lpGPOName);
  168. AddString (szMsg);
  169. lpDC = lpList;
  170. while (*lpDC) {
  171. AddString (TEXT("------------"));
  172. wsprintf (szMsg, TEXT("Checking DC: %s"), lpDC);
  173. AddString (szMsg);
  174. wsprintf (szPath, TEXT("\\\\%s\\SysVol\\%s\\policies\\%s"),
  175. lpDC, lpDomainName, lpGPOName);
  176. if (!GetFileAttributesEx (szPath, GetFileExInfoStandard, &fad)) {
  177. wsprintf (szMsg, TEXT("Failed to find %s with %d"), szPath, GetLastError());
  178. AddString (szMsg);
  179. goto LoopAgain;
  180. }
  181. lpEnd = CheckSlash (szPath);
  182. if (dwFlags & GPO_FLAG_VERSION) {
  183. lstrcpy (lpEnd, TEXT("gpt.ini"));
  184. dwFileSysVersion = GetPrivateProfileInt(TEXT("General"), TEXT("Version"), 0, szPath);
  185. lpDomainName2 = GetDomainName (TRUE);
  186. lstrcpy (szLDAPPath, TEXT("LDAP://"));
  187. lstrcat (szLDAPPath, lpDC);
  188. lstrcat (szLDAPPath, TEXT("/CN="));
  189. lstrcat (szLDAPPath, lpGPOName);
  190. lstrcat (szLDAPPath, TEXT(",CN=Policies,CN=System"));
  191. lpTemp = lpDomainName2;
  192. while (*lpTemp) {
  193. lstrcat (szLDAPPath, TEXT(",DC="));
  194. lstrcat (szLDAPPath, lpTemp);
  195. lpTemp += lstrlen(lpTemp) + 1;
  196. }
  197. hr = ADsGetObject(szLDAPPath, IID_IADs, (void **)&pADs);
  198. if (FAILED(hr)) {
  199. wsprintf (szMsg, TEXT("ADsGetObject failed with 0x%x"), hr);
  200. AddString (szMsg);
  201. goto LoopAgain;
  202. }
  203. VariantInit(&var);
  204. bstrProperty = SysAllocString (L"versionNumber");
  205. hr = pADs->Get(bstrProperty, &var);
  206. if (FAILED(hr)) {
  207. wsprintf (szMsg, TEXT("pADs->Get failed with 0x%x"), hr);
  208. AddString (szMsg);
  209. SysFreeString (bstrProperty);
  210. VariantClear (&var);
  211. goto LoopAgain;
  212. }
  213. wsprintf (szMsg, TEXT("DS Version: %d"), var.lVal);
  214. AddString (szMsg);
  215. SysFreeString (bstrProperty);
  216. VariantClear (&var);
  217. pADs->Release();
  218. wsprintf (szMsg, TEXT("SysVol Version: %d"), dwFileSysVersion);
  219. AddString (szMsg);
  220. }
  221. if (dwFlags & GPO_FLAG_REGISTRY) {
  222. lstrcpy (lpEnd, TEXT("machine\\registry.pol"));
  223. if (GetFileAttributesEx (szPath, GetFileExInfoStandard, &fad)) {
  224. FileTimeToLocalFileTime (&fad.ftLastWriteTime, &filetime);
  225. FileTimeToSystemTime (&filetime, &systime);
  226. GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &systime,
  227. NULL, szDate, 50);
  228. GetTimeFormat (LOCALE_USER_DEFAULT, 0, &systime,
  229. NULL, szTime, 50);
  230. wsprintf (szMsg, TEXT("Last Registry Update (machine): %s %s"), szDate, szTime);
  231. AddString (szMsg);
  232. }
  233. lstrcpy (lpEnd, TEXT("user\\registry.pol"));
  234. if (GetFileAttributesEx (szPath, GetFileExInfoStandard, &fad)) {
  235. FileTimeToLocalFileTime (&fad.ftLastWriteTime, &filetime);
  236. FileTimeToSystemTime (&filetime, &systime);
  237. GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &systime,
  238. NULL, szDate, 50);
  239. GetTimeFormat (LOCALE_USER_DEFAULT, 0, &systime,
  240. NULL, szTime, 50);
  241. wsprintf (szMsg, TEXT("Last Registry Update (user): %s %s"), szDate, szTime);
  242. AddString (szMsg);
  243. }
  244. }
  245. LoopAgain:
  246. lpDC += lstrlen(lpDC) + 1;
  247. }
  248. CoUninitialize();
  249. LocalFree (lpList);
  250. LocalFree (lpDomainName);
  251. return TRUE;
  252. }
  253. BOOL CALLBACK DomainInfo (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  254. {
  255. switch (message)
  256. {
  257. case WM_INITDIALOG:
  258. {
  259. LPTSTR lpTemp;
  260. lpTemp = GetDomainName (FALSE);
  261. if (lpTemp) {
  262. SetDlgItemText (hDlg, IDC_NAME, lpTemp);
  263. LocalFree (lpTemp);
  264. }
  265. lpTemp = GetDCList (FALSE);
  266. if (lpTemp) {
  267. SetDlgItemText (hDlg, IDC_DCLIST, lpTemp);
  268. LocalFree (lpTemp);
  269. }
  270. }
  271. return TRUE;
  272. case WM_COMMAND:
  273. if (LOWORD(wParam) == IDOK) {
  274. HKEY hKey;
  275. DWORD dwDisp;
  276. TCHAR szBuffer[512];
  277. if (RegCreateKeyEx (HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Poltest"),
  278. 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE,
  279. NULL, &hKey, &dwDisp) == ERROR_SUCCESS) {
  280. GetDlgItemText (hDlg, IDC_NAME, szBuffer, 512);
  281. RegSetValueEx (hKey, TEXT("DomainName"), NULL, REG_SZ,
  282. (LPBYTE) szBuffer, (lstrlen(szBuffer) + 1) *sizeof(TCHAR));
  283. GetDlgItemText (hDlg, IDC_DCLIST, szBuffer, 512);
  284. RegSetValueEx (hKey, TEXT("DCList"), NULL, REG_SZ,
  285. (LPBYTE) szBuffer, (lstrlen(szBuffer) + 1) *sizeof(TCHAR));
  286. RegCloseKey (hKey);
  287. }
  288. EndDialog(hDlg, TRUE);
  289. return (TRUE);
  290. }
  291. if (LOWORD(wParam) == IDCANCEL) {
  292. EndDialog(hDlg, FALSE);
  293. return (TRUE);
  294. }
  295. break;
  296. }
  297. return (FALSE);
  298. }
  299. BOOL ManageDomainInfo(HWND hWnd)
  300. {
  301. DialogBox (GetModuleHandle(NULL), TEXT("DOMAIN_INFO"), hWnd, DomainInfo);
  302. return TRUE;
  303. }
  304. DWORD WINAPI CheckGPOThread (LPGPOINFO lpInfo)
  305. {
  306. CheckSpecificGPO (lpInfo->szGPOName, lpInfo->dwFlags);
  307. LocalFree (lpInfo);
  308. return 0;
  309. }
  310. BOOL CALLBACK GPONameDlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  311. {
  312. switch (message)
  313. {
  314. case WM_INITDIALOG:
  315. {
  316. HKEY hKey;
  317. DWORD dwType, dwSize, dwTemp;
  318. TCHAR szGPOName[50];
  319. szGPOName[0] = TEXT('\0');
  320. if (RegOpenKeyEx (HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Poltest"),
  321. 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
  322. dwSize = 50 * sizeof(TCHAR);
  323. RegQueryValueEx (hKey, TEXT("GPOName"), NULL, &dwType,
  324. (LPBYTE) szGPOName, &dwSize);
  325. dwSize = sizeof(dwTemp);
  326. dwTemp = 0;
  327. RegQueryValueEx (hKey, TEXT("GPONameOptions"), NULL, &dwType,
  328. (LPBYTE) &dwTemp, &dwSize);
  329. RegCloseKey (hKey);
  330. }
  331. SetDlgItemText (hDlg, IDC_NAME, szGPOName);
  332. if (dwTemp & GPO_FLAG_VERSION) {
  333. CheckDlgButton (hDlg, IDC_VERSION, BST_CHECKED);
  334. }
  335. if (dwTemp & GPO_FLAG_REGISTRY) {
  336. CheckDlgButton (hDlg, IDC_REGISTRY, BST_CHECKED);
  337. }
  338. }
  339. return TRUE;
  340. case WM_COMMAND:
  341. if (LOWORD(wParam) == IDOK) {
  342. HKEY hKey;
  343. DWORD dwDisp, dwTemp;
  344. TCHAR szGPOName[50];
  345. LPGPOINFO lpInfo;
  346. HANDLE hThread;
  347. DWORD dwID;
  348. dwTemp = 0;
  349. if (IsDlgButtonChecked (hDlg, IDC_VERSION)) {
  350. dwTemp |= GPO_FLAG_VERSION;
  351. }
  352. if (IsDlgButtonChecked (hDlg, IDC_REGISTRY)) {
  353. dwTemp |= GPO_FLAG_REGISTRY;
  354. }
  355. if (RegCreateKeyEx (HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Poltest"),
  356. 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE,
  357. NULL, &hKey, &dwDisp) == ERROR_SUCCESS) {
  358. GetDlgItemText (hDlg, IDC_NAME, szGPOName, 50);
  359. RegSetValueEx (hKey, TEXT("GPOName"), NULL, REG_SZ,
  360. (LPBYTE) szGPOName, (lstrlen(szGPOName) + 1) *sizeof(TCHAR));
  361. RegSetValueEx (hKey, TEXT("GPONameOptions"), NULL, REG_SZ,
  362. (LPBYTE) &dwTemp, sizeof(dwTemp));
  363. RegCloseKey (hKey);
  364. }
  365. lpInfo = (LPGPOINFO) LocalAlloc (LPTR, sizeof(GPOINFO));
  366. if (lpInfo) {
  367. lstrcpy (lpInfo->szGPOName, szGPOName);
  368. lpInfo->dwFlags = dwTemp;
  369. hThread = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)CheckGPOThread,
  370. lpInfo, 0, &dwID);
  371. if (hThread) {
  372. CloseHandle (hThread);
  373. }
  374. }
  375. EndDialog(hDlg, TRUE);
  376. return (TRUE);
  377. }
  378. if (LOWORD(wParam) == IDCANCEL) {
  379. EndDialog(hDlg, FALSE);
  380. return (TRUE);
  381. }
  382. if (LOWORD(wParam) == IDC_BROWSE) {
  383. GPOBROWSEINFO info;
  384. TCHAR szName[500];
  385. TCHAR szPath[512];
  386. LPGROUPPOLICYOBJECT pGPO;
  387. szName[0] = TEXT('\0');
  388. ZeroMemory (&info, sizeof(info));
  389. info.dwSize = sizeof(info);
  390. info.dwFlags = GPO_BROWSE_NOCOMPUTERS;
  391. info.hwndOwner = hDlg;
  392. info.lpDSPath = szPath;
  393. info.dwDSPathSize = 512;
  394. info.lpName = szName;
  395. info.dwNameSize = 500;
  396. if (SUCCEEDED(BrowseForGPO (&info))) {
  397. if (SUCCEEDED(CoCreateInstance (CLSID_GroupPolicyObject, NULL,
  398. CLSCTX_SERVER, IID_IGroupPolicyObject,
  399. (void**)&pGPO))) {
  400. if (SUCCEEDED(pGPO->OpenDSGPO (szPath, GPO_OPEN_READ_ONLY))){
  401. pGPO->GetName (szName, 500);
  402. SetDlgItemText (hDlg, IDC_NAME, szName);
  403. }
  404. pGPO->Release();
  405. }
  406. }
  407. }
  408. break;
  409. }
  410. return (FALSE);
  411. }
  412. BOOL CheckGPO(HWND hWnd)
  413. {
  414. DialogBox (GetModuleHandle(NULL), TEXT("GPO_NAME"), hWnd, GPONameDlgProc);
  415. return TRUE;
  416. }
  417. //*************************************************************
  418. //
  419. // CheckSlash()
  420. //
  421. // Purpose: Checks for an ending slash and adds one if
  422. // it is missing.
  423. //
  424. // Parameters: lpDir - directory
  425. //
  426. // Return: Pointer to the end of the string
  427. //
  428. // Comments:
  429. //
  430. // History: Date Author Comment
  431. // 6/19/95 ericflo Created
  432. //
  433. //*************************************************************
  434. LPTSTR CheckSlash (LPTSTR lpDir)
  435. {
  436. DWORD dwStrLen;
  437. LPTSTR lpEnd;
  438. lpEnd = lpDir + lstrlen(lpDir);
  439. if (*(lpEnd - 1) != TEXT('\\')) {
  440. *lpEnd = TEXT('\\');
  441. lpEnd++;
  442. *lpEnd = TEXT('\0');
  443. }
  444. return lpEnd;
  445. }