Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

506 lines
12 KiB

  1. //*************************************************************
  2. // File name: REGVIEW.C
  3. //
  4. // Description: Routines to print out a registry.pol file
  5. //
  6. //
  7. // Microsoft Confidential
  8. // Copyright (c) Microsoft Corporation 1999
  9. // All rights reserved
  10. //
  11. //*************************************************************
  12. #include "gpresult.h"
  13. #define MAX_KEYNAME_SIZE 2048
  14. #define MAX_VALUENAME_SIZE 512
  15. //
  16. // Verison number for the registry file format
  17. //
  18. #define REGISTRY_FILE_VERSION 1
  19. //
  20. // File signature
  21. //
  22. #define REGFILE_SIGNATURE 0x67655250
  23. //
  24. // True policy keys
  25. //
  26. #define SOFTWARE_POLICIES TEXT("Software\\Policies")
  27. #define SOFTWARE_POLICIES_LEN 17
  28. #define WINDOWS_POLICIES TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Policies")
  29. #define WINDOWS_POLICIES_LEN 46
  30. //*************************************************************
  31. //
  32. // DisplayRegistryData()
  33. //
  34. // Purpose: Displays the registry data
  35. //
  36. // Parameters: lpRegistry - Path to registry.pol
  37. //
  38. //
  39. // Return: TRUE if successful
  40. // FALSE if an error occurs
  41. //
  42. //*************************************************************
  43. BOOL DisplayRegistryData (LPTSTR lpRegistry)
  44. {
  45. HANDLE hFile;
  46. BOOL bResult = FALSE;
  47. DWORD dwTemp, dwBytesRead, dwType, dwDataLength, dwIndex, dwCount;
  48. LPWSTR lpKeyName, lpValueName, lpTemp;
  49. LPBYTE lpData = NULL, lpIndex;
  50. WCHAR chTemp;
  51. INT i;
  52. CHAR szString[20];
  53. BOOL bTruePolicy;
  54. //
  55. // Open the registry file
  56. //
  57. hFile = CreateFile (lpRegistry, GENERIC_READ, FILE_SHARE_READ, NULL,
  58. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
  59. NULL);
  60. if (hFile == INVALID_HANDLE_VALUE) {
  61. if (GetLastError() == ERROR_FILE_NOT_FOUND)
  62. {
  63. return TRUE;
  64. }
  65. else
  66. {
  67. PrintString(IDS_CREATEFILE, GetLastError());
  68. return FALSE;
  69. }
  70. }
  71. //
  72. // Allocate buffers to hold the keyname, valuename, and data
  73. //
  74. lpKeyName = (LPWSTR) LocalAlloc (LPTR, MAX_KEYNAME_SIZE * sizeof(WCHAR));
  75. if (!lpKeyName)
  76. {
  77. PrintString(IDS_MEMALLOCFAILED, GetLastError());
  78. CloseHandle (hFile);
  79. return FALSE;
  80. }
  81. lpValueName = (LPWSTR) LocalAlloc (LPTR, MAX_VALUENAME_SIZE * sizeof(WCHAR));
  82. if (!lpValueName)
  83. {
  84. PrintString(IDS_MEMALLOCFAILED, GetLastError());
  85. LocalFree (lpKeyName);
  86. CloseHandle (hFile);
  87. return FALSE;
  88. }
  89. //
  90. // Read the header block
  91. //
  92. // 2 DWORDS, signature (PReg) and version number and 2 newlines
  93. //
  94. if (!ReadFile (hFile, &dwTemp, sizeof(dwTemp), &dwBytesRead, NULL) ||
  95. dwBytesRead != sizeof(dwTemp))
  96. {
  97. PrintString(IDS_INVALIDSIGNATURE1, GetLastError());
  98. goto Exit;
  99. }
  100. if (dwTemp != REGFILE_SIGNATURE)
  101. {
  102. PrintString(IDS_INVALIDSIGNATURE2);
  103. goto Exit;
  104. }
  105. if (!ReadFile (hFile, &dwTemp, sizeof(dwTemp), &dwBytesRead, NULL) ||
  106. dwBytesRead != sizeof(dwTemp))
  107. {
  108. PrintString(IDS_VERSIONNUMBER1, GetLastError());
  109. goto Exit;
  110. }
  111. if (dwTemp != REGISTRY_FILE_VERSION)
  112. {
  113. PrintString(IDS_VERSIONNUMBER2);
  114. goto Exit;
  115. }
  116. PrintString (IDS_NEWLINE);
  117. //
  118. // Read the data
  119. //
  120. while (TRUE)
  121. {
  122. //
  123. // Read the first character. It will either be a [ or the end
  124. // of the file.
  125. //
  126. if (!ReadFile (hFile, &chTemp, sizeof(WCHAR), &dwBytesRead, NULL))
  127. {
  128. if (GetLastError() != ERROR_HANDLE_EOF)
  129. {
  130. PrintString(IDS_FAILEDFIRSTCHAR, GetLastError());
  131. goto Exit;
  132. }
  133. break;
  134. }
  135. if ((dwBytesRead == 0) || (chTemp != L'['))
  136. {
  137. break;
  138. }
  139. //
  140. // Read the keyname
  141. //
  142. lpTemp = lpKeyName;
  143. while (TRUE)
  144. {
  145. if (!ReadFile (hFile, &chTemp, sizeof(WCHAR), &dwBytesRead, NULL))
  146. {
  147. PrintString(IDS_FAILEDKEYNAMECHAR, GetLastError());
  148. goto Exit;
  149. }
  150. *lpTemp++ = chTemp;
  151. if (chTemp == TEXT('\0'))
  152. break;
  153. }
  154. //
  155. // Read the semi-colon
  156. //
  157. if (!ReadFile (hFile, &chTemp, sizeof(WCHAR), &dwBytesRead, NULL))
  158. {
  159. if (GetLastError() != ERROR_HANDLE_EOF)
  160. {
  161. PrintString(IDS_FAILEDSEMICOLON, GetLastError());
  162. goto Exit;
  163. }
  164. break;
  165. }
  166. if ((dwBytesRead == 0) || (chTemp != L';'))
  167. {
  168. break;
  169. }
  170. //
  171. // Read the valuename
  172. //
  173. lpTemp = lpValueName;
  174. while (TRUE)
  175. {
  176. if (!ReadFile (hFile, &chTemp, sizeof(WCHAR), &dwBytesRead, NULL))
  177. {
  178. PrintString(IDS_FAILEDVALUENAME, GetLastError());
  179. goto Exit;
  180. }
  181. *lpTemp++ = chTemp;
  182. if (chTemp == TEXT('\0'))
  183. break;
  184. }
  185. //
  186. // Read the semi-colon
  187. //
  188. if (!ReadFile (hFile, &chTemp, sizeof(WCHAR), &dwBytesRead, NULL))
  189. {
  190. if (GetLastError() != ERROR_HANDLE_EOF)
  191. {
  192. PrintString(IDS_FAILEDSEMICOLON, GetLastError());
  193. goto Exit;
  194. }
  195. break;
  196. }
  197. if ((dwBytesRead == 0) || (chTemp != L';'))
  198. {
  199. break;
  200. }
  201. //
  202. // Read the type
  203. //
  204. if (!ReadFile (hFile, &dwType, sizeof(DWORD), &dwBytesRead, NULL))
  205. {
  206. PrintString(IDS_FAILEDTYPE, GetLastError());
  207. goto Exit;
  208. }
  209. //
  210. // Skip semicolon
  211. //
  212. if (!ReadFile (hFile, &dwTemp, sizeof(WCHAR), &dwBytesRead, NULL))
  213. {
  214. PrintString(IDS_FAILEDSEMICOLON, GetLastError());
  215. goto Exit;
  216. }
  217. //
  218. // Read the data length
  219. //
  220. if (!ReadFile (hFile, &dwDataLength, sizeof(DWORD), &dwBytesRead, NULL))
  221. {
  222. PrintString(IDS_FAILEDDATALENGTH, GetLastError());
  223. goto Exit;
  224. }
  225. //
  226. // Skip semicolon
  227. //
  228. if (!ReadFile (hFile, &dwTemp, sizeof(WCHAR), &dwBytesRead, NULL))
  229. {
  230. PrintString(IDS_FAILEDSEMICOLON, GetLastError());
  231. goto Exit;
  232. }
  233. //
  234. // Allocate memory for data
  235. //
  236. lpData = (LPBYTE) LocalAlloc (LPTR, dwDataLength);
  237. if (!lpData)
  238. {
  239. PrintString(IDS_MEMALLOCFAILED, GetLastError());
  240. goto Exit;
  241. }
  242. //
  243. // Read data
  244. //
  245. if (!ReadFile (hFile, lpData, dwDataLength, &dwBytesRead, NULL))
  246. {
  247. PrintString(IDS_FAILEDDATA, GetLastError());
  248. goto Exit;
  249. }
  250. //
  251. // Skip closing bracket
  252. //
  253. if (!ReadFile (hFile, &chTemp, sizeof(WCHAR), &dwBytesRead, NULL))
  254. {
  255. PrintString(IDS_CLOSINGBRACKET1, GetLastError());
  256. goto Exit;
  257. }
  258. if (chTemp != L']')
  259. {
  260. PrintString(IDS_CLOSINGBRACKET2, chTemp);
  261. goto Exit;
  262. }
  263. //
  264. // Print out the entry
  265. //
  266. bTruePolicy = FALSE;
  267. if (CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
  268. lpKeyName, SOFTWARE_POLICIES_LEN,
  269. SOFTWARE_POLICIES, SOFTWARE_POLICIES_LEN) == CSTR_EQUAL)
  270. {
  271. bTruePolicy = TRUE;
  272. }
  273. else if (CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
  274. lpKeyName, WINDOWS_POLICIES_LEN,
  275. WINDOWS_POLICIES, WINDOWS_POLICIES_LEN) == CSTR_EQUAL)
  276. {
  277. bTruePolicy = TRUE;
  278. }
  279. if (!bTruePolicy)
  280. {
  281. PrintString (IDS_2NEWLINE);
  282. PrintString (IDS_REGVIEW_PREF1);
  283. PrintString (IDS_REGVIEW_PREF2);
  284. PrintString (IDS_REGVIEW_PREF3);
  285. }
  286. //
  287. // Check if this is comment holding the GPO name
  288. //
  289. if (CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
  290. lpValueName, 20, TEXT("**Comment:GPO Name: "), 20) == CSTR_EQUAL)
  291. {
  292. PrintString (IDS_REGVIEW_GPONAME, (lpValueName+20));
  293. }
  294. else
  295. {
  296. PrintString (IDS_REGVIEW_KEYNAME, lpKeyName);
  297. PrintString (IDS_REGVIEW_VALUENAME, lpValueName);
  298. switch (dwType) {
  299. case REG_DWORD:
  300. PrintString (IDS_REGVIEW_DWORD);
  301. PrintString (IDS_REGVIEW_DWORDDATA, *((LPDWORD)lpData));
  302. break;
  303. case REG_SZ:
  304. PrintString (IDS_REGVIEW_SZ);
  305. PrintString (IDS_REGVIEW_SZDATA, (LPTSTR)lpData);
  306. break;
  307. case REG_EXPAND_SZ:
  308. PrintString (IDS_REGVIEW_EXPANDSZ);
  309. PrintString (IDS_REGVIEW_SZDATA, (LPTSTR)lpData);
  310. break;
  311. case REG_MULTI_SZ:
  312. PrintString (IDS_REGVIEW_MULTISZ);
  313. PrintString (IDS_REGVIEW_MULTIDATA1);
  314. lpTemp = (LPWSTR) lpData;
  315. while (*lpTemp) {
  316. PrintString (IDS_REGVIEW_MULTIDATA2, lpTemp);
  317. lpTemp += lstrlen(lpTemp) + 1;
  318. }
  319. break;
  320. case REG_BINARY:
  321. PrintString (IDS_REGVIEW_BINARY);
  322. if (g_bSuperVerbose)
  323. {
  324. PrintString (IDS_REGVIEW_BINARYDATA1);
  325. dwIndex = 0;
  326. dwCount = 0;
  327. lpIndex = lpData;
  328. ZeroMemory(szString, sizeof(szString));
  329. while (dwIndex <= dwDataLength) {
  330. PrintString (IDS_REGVIEW_BINARYFRMT, *lpIndex);
  331. if ((*lpIndex > 32) && (*lpIndex < 127)) {
  332. szString[dwCount] = *lpIndex;
  333. } else {
  334. szString[dwCount] = '.';
  335. }
  336. if (dwCount < 15) {
  337. dwCount++;
  338. } else {
  339. PrintString (IDS_REGVIEW_STRING1, szString);
  340. PrintString (IDS_REGVIEW_NEXTLINE);
  341. ZeroMemory(szString, sizeof(szString));
  342. dwCount = 0;
  343. }
  344. dwIndex++;
  345. lpIndex++;
  346. }
  347. if (dwCount > 0) {
  348. while (dwCount < 16) {
  349. PrintString (IDS_REGVIEW_SPACE);
  350. dwCount++;
  351. }
  352. PrintString (IDS_REGVIEW_STRING2, szString);
  353. }
  354. PrintString (IDS_NEWLINE);
  355. } else {
  356. PrintString (IDS_REGVIEW_VERBOSE);
  357. }
  358. break;
  359. case REG_NONE:
  360. PrintString (IDS_REGVIEW_NONE);
  361. PrintString (IDS_REGVIEW_NOVALUES, *lpData);
  362. break;
  363. default:
  364. PrintString (IDS_REGVIEW_UNKNOWN);
  365. PrintString (IDS_REGVIEW_UNKNOWNSIZE, dwDataLength);
  366. break;
  367. }
  368. }
  369. LocalFree (lpData);
  370. lpData = NULL;
  371. }
  372. bResult = TRUE;
  373. Exit:
  374. //
  375. // Finished
  376. //
  377. if (lpData) {
  378. LocalFree (lpData);
  379. }
  380. CloseHandle (hFile);
  381. LocalFree (lpKeyName);
  382. LocalFree (lpValueName);
  383. return bResult;
  384. }