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.

417 lines
11 KiB

  1. /*******************************************************************
  2. *
  3. * Copyright (c) 1999 Microsoft Corporation
  4. *
  5. * DESCRIPTION: an extension to dump the contents of registry keys and values
  6. *
  7. * AUTHOR:
  8. * Based on Code by : danielwe (Dan Weisman)
  9. * ntsd addition by : kksharma (Kshitiz K. Sharma)
  10. *
  11. * DATE:4/20/1999
  12. *
  13. *******************************************************************/
  14. #include "ntsdextp.h"
  15. #include <strsafe.h>
  16. #ifndef KERNEL
  17. #ifndef Print
  18. #define Print dprintf
  19. #endif
  20. #define OFLAG(l) (1L << ((DWORD)#@l - (DWORD)'a'))
  21. #define LINE_NUMBER 0
  22. #define NUM_ASCII_CHARS 16
  23. #define NUM_HEX_CHARS (NUM_ASCII_CHARS * 3)
  24. #define SPACE 7
  25. #define PB_BUFFER_SIZE (NUM_ASCII_CHARS * 50)
  26. VOID dregHelp() {
  27. dprintf("!dreg -[d|w] <keyPath>[![<valueName> | *]] - Dumps registry information\n");
  28. dprintf("!dreg -d ... - Prints binary values as DWORDs\n");
  29. dprintf("!dreg -w ... - Prints binary values as WORDs\n");
  30. dprintf("!dreg <keyPath>!* - Prints all values under <keyPath>\n");
  31. dprintf("!dreg <keyPath> - Prints all subkeys of <keyPath>\n");
  32. dprintf("\n");
  33. dprintf("<keypath> can begin with any of the following:\n");
  34. dprintf("\thklm - HKEY_LOCAL_MACHINE\n");
  35. dprintf("\thkcu - HKEY_CURRENT_USER\n");
  36. dprintf("\thkcr - HKEY_CLASSES_ROOT\n");
  37. dprintf("\thku - HKEY_USERS\n");
  38. dprintf("\tif absent, hklm is assumed\n");
  39. dprintf("\n");
  40. dprintf("Ex:\n");
  41. dprintf("!dreg hkcu\\Software\\Microsoft\n");
  42. dprintf("!dreg System\\CurrentControlSet\\Services\\Tcpip!*\n");
  43. dprintf("!dreg System\\CurrentControlSet\\Services\\Tcpip!Start\n");
  44. }
  45. VOID PrintBinary(PBYTE pbData, DWORD cbData, USHORT uWidth)
  46. {
  47. CHAR line[80];
  48. INT i;
  49. INT ascii = 0;
  50. PBYTE temp = pbData;
  51. BOOL fDone = FALSE;
  52. DWORD cbCount = 0;
  53. CHAR hex_digits[] = "0123456789ABCDEF";
  54. while (!fDone)
  55. {
  56. DWORD cb;
  57. memset(line, 0x20, sizeof(line));
  58. Print("%04X: ", cbCount);
  59. for (ascii = 0,i = LINE_NUMBER, cb = 0;
  60. ascii < NUM_ASCII_CHARS;
  61. ascii++, temp++)
  62. {
  63. if ((DWORD)(temp - pbData) >= cbData)
  64. {
  65. if (cbData < PB_BUFFER_SIZE)
  66. {
  67. fDone = TRUE;
  68. break;
  69. }
  70. else
  71. return;
  72. }
  73. line[i] = hex_digits[(*temp & 0xF0) >> 4];
  74. line[i + 1] = hex_digits[(*temp & 0x0F)];
  75. cb++;
  76. if ((ascii + 1) % uWidth == 0)
  77. {
  78. line[i + 2] = 0x20;
  79. i++;
  80. if (uWidth > 1)
  81. {
  82. line[i + 3] = 0x20;
  83. i++;
  84. }
  85. else if (uWidth == 1 && (!(cb % 4)))
  86. {
  87. line[i + 3] = 0x20;
  88. line[i + 4] = 0x20;
  89. i += 2;
  90. }
  91. }
  92. i += 2;
  93. line[ascii + NUM_HEX_CHARS + SPACE + LINE_NUMBER] =
  94. (isprint(*temp) ? *temp : '.');
  95. cbCount++;
  96. }
  97. line[79] = 0;
  98. Print("%s\n", line);
  99. }
  100. }
  101. VOID PrintMultiSz(PBYTE pbData)
  102. {
  103. LPSTR sz = (LPSTR)pbData;
  104. DWORD csz = 0;
  105. while (*sz)
  106. {
  107. Print("%d: \"%s\"\n", csz, *sz ? sz : "<empty>");
  108. csz++;
  109. sz += lstrlenA(sz) + 1;
  110. }
  111. }
  112. VOID PrintRegistryValue(DWORD dwType, PBYTE pbData, DWORD cbData, USHORT uWidth)
  113. {
  114. switch (dwType)
  115. {
  116. case REG_SZ:
  117. Print("REG_SZ: \"%s\"\n", *pbData ? pbData : "<empty>");
  118. break;
  119. case REG_EXPAND_SZ:
  120. {
  121. CHAR szExpanded[MAX_PATH + 1];
  122. Print("REG_EXPAND_SZ: \"%s\"\n", pbData);
  123. ExpandEnvironmentStringsA((LPCSTR)pbData, (LPSTR)szExpanded,
  124. MAX_PATH);
  125. Print("expanded = \"%s\"\n", szExpanded);
  126. break;
  127. }
  128. case REG_DWORD:
  129. {
  130. DWORD dwData = * ((DWORD *)pbData);
  131. Print("REG_DWORD: %lu = 0x%08X\n", dwData, dwData);
  132. break;
  133. }
  134. case REG_BINARY:
  135. {
  136. Print("REG_BINARY:\n");
  137. PrintBinary(pbData, cbData, uWidth);
  138. break;
  139. }
  140. case REG_MULTI_SZ:
  141. {
  142. Print("REG_MULTI_SZ:\n");
  143. PrintMultiSz(pbData);
  144. break;
  145. }
  146. }
  147. }
  148. VOID EnumSubKeys(HKEY hkeyRoot, LPSTR szKey)
  149. {
  150. HKEY hkey;
  151. LONG l;
  152. BOOL fFound = FALSE;
  153. l = RegOpenKeyExA(hkeyRoot, szKey, 0, KEY_READ, &hkey);
  154. if (ERROR_SUCCESS == l)
  155. {
  156. FILETIME ft;
  157. DWORD cbName;
  158. CHAR szName[MAX_PATH + 1];
  159. DWORD dwIndex;
  160. for (dwIndex = 0; l == ERROR_SUCCESS; dwIndex++)
  161. {
  162. cbName = MAX_PATH;
  163. l = RegEnumKeyExA(hkey, dwIndex, szName, &cbName, NULL,
  164. NULL, NULL,&ft);
  165. if (ERROR_SUCCESS == l)
  166. {
  167. Print("Subkey: %s\n", szName);
  168. fFound = TRUE;
  169. }
  170. }
  171. RegCloseKey(hkey);
  172. }
  173. else
  174. {
  175. Print("Could not open subkey %s. Error (%d).\n", szKey, l);
  176. }
  177. if (!fFound)
  178. {
  179. Print("No subkeys\n");
  180. }
  181. }
  182. VOID EnumValues(HKEY hkeyRoot, LPSTR szKey, USHORT uWidth)
  183. {
  184. HKEY hkey;
  185. LONG l;
  186. BOOL fFound = FALSE;
  187. l = RegOpenKeyExA(hkeyRoot, szKey, 0, KEY_READ, &hkey);
  188. if (ERROR_SUCCESS == l)
  189. {
  190. DWORD cbMax;
  191. l = RegQueryInfoKeyA(hkey, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  192. NULL, &cbMax, NULL, NULL);
  193. if (ERROR_SUCCESS == l)
  194. {
  195. DWORD cbName;
  196. CHAR szName[MAX_PATH + 1];
  197. DWORD dwIndex;
  198. PBYTE pbData;
  199. DWORD dwType;
  200. DWORD cbData;
  201. pbData = (PBYTE)LocalAlloc(LPTR, cbMax);
  202. if (pbData)
  203. {
  204. for (dwIndex = 0; l == ERROR_SUCCESS; dwIndex++)
  205. {
  206. cbName = MAX_PATH;
  207. cbData = cbMax;
  208. l = RegEnumValueA(hkey, dwIndex, szName, &cbName, NULL,
  209. &dwType, pbData, &cbData);
  210. if (ERROR_SUCCESS == l)
  211. {
  212. Print("Value: \"%s\" - ", szName);
  213. PrintRegistryValue(dwType, pbData, cbData, uWidth);
  214. Print("------------------------------------------------------------------------\n");
  215. fFound = TRUE;
  216. }
  217. }
  218. LocalFree(pbData);
  219. }
  220. }
  221. RegCloseKey(hkey);
  222. }
  223. else
  224. {
  225. Print("Could not open subkey %s. Error (%d).\n", szKey, l);
  226. }
  227. if (!fFound)
  228. {
  229. Print("No values\n");
  230. }
  231. }
  232. /************************************************************************\
  233. * Procedure: Idreg
  234. *
  235. * Description: Dumps registry value
  236. *
  237. * Returns: fSuccess
  238. *
  239. * 4/14/1999 Created DanielWe
  240. *
  241. \************************************************************************/
  242. BOOL Idreg(
  243. DWORD opts,
  244. LPCSTR InString)
  245. {
  246. LONG l;
  247. HKEY hkey;
  248. DWORD cbData = 0;
  249. DWORD dwType;
  250. LPBYTE pbData = NULL;
  251. LPSTR szKey = NULL;
  252. LPSTR szValue = NULL;
  253. CHAR String[512] = {0};
  254. LPTSTR lpas = String;
  255. LPTSTR lpasOrig = String;
  256. HKEY hkeyRoot;
  257. if (strlen(InString) < sizeof(String))
  258. {
  259. StringCchCopy(String, sizeof(String), InString);
  260. }
  261. // Eat leading spaces first
  262. while (*lpas && *lpas == ' ')
  263. {
  264. lpas++;
  265. }
  266. while (*lpas && *lpas != '\\')
  267. {
  268. lpas++;
  269. }
  270. if (!*lpas)
  271. {
  272. // Corner case.. no backslash at all. Assume HKLM and start over
  273. hkeyRoot = HKEY_LOCAL_MACHINE;
  274. lpas = lpasOrig;
  275. }
  276. else
  277. {
  278. // Figure out which hive they want to open
  279. *lpas = 0;
  280. if (!_stricmp(lpasOrig, "hkcu"))
  281. {
  282. hkeyRoot = HKEY_CURRENT_USER;
  283. lpas++;
  284. }
  285. else if (!_stricmp(lpasOrig, "hklm"))
  286. {
  287. hkeyRoot = HKEY_LOCAL_MACHINE;
  288. lpas++;
  289. }
  290. else if (!_stricmp(lpasOrig, "hku"))
  291. {
  292. hkeyRoot = HKEY_USERS;
  293. lpas++;
  294. }
  295. else if (!_stricmp(lpasOrig, "hkcr"))
  296. {
  297. hkeyRoot = HKEY_CLASSES_ROOT;
  298. lpas++;
  299. }
  300. else if (!_stricmp(lpasOrig, "help"))
  301. {
  302. dregHelp();
  303. return FALSE;
  304. }
  305. else
  306. {
  307. hkeyRoot = HKEY_LOCAL_MACHINE;
  308. // Restore the backslash because we assume if they don't use these
  309. // keywords, then they want HKLM
  310. *lpas = '\\';
  311. lpas = lpasOrig;
  312. }
  313. }
  314. szKey = (LPSTR)lpas;
  315. while (*lpas && *lpas != '!')
  316. {
  317. lpas++;
  318. }
  319. if (*lpas)
  320. {
  321. // Null terminate the !
  322. *lpas++ = 0;
  323. // mark beginning of new string
  324. szValue = (LPSTR)lpas;
  325. }
  326. if (szKey == NULL || *szKey == 0)
  327. {
  328. Print("Expected subkey name\n");
  329. dregHelp();
  330. return FALSE;
  331. }
  332. if (szValue == NULL || *szValue == 0)
  333. {
  334. EnumSubKeys(hkeyRoot, szKey);
  335. }
  336. else if (!lstrcmpA(szValue, "*"))
  337. {
  338. EnumValues(hkeyRoot, szKey, (USHORT)opts);
  339. }
  340. else
  341. {
  342. l = RegOpenKeyExA(hkeyRoot, (LPCSTR)szKey, 0, KEY_READ, &hkey);
  343. if (ERROR_SUCCESS == l)
  344. {
  345. l = RegQueryValueExA(hkey, (LPCSTR)szValue, NULL, &dwType, NULL,
  346. &cbData);
  347. if (ERROR_SUCCESS == l)
  348. {
  349. pbData = (LPBYTE)LocalAlloc(LPTR, cbData);
  350. l = RegQueryValueExA(hkey, (LPCSTR)szValue, NULL, &dwType, pbData,
  351. &cbData);
  352. if (ERROR_SUCCESS == l)
  353. {
  354. PrintRegistryValue(dwType, pbData, cbData, (USHORT)opts);
  355. }
  356. LocalFree(pbData);
  357. }
  358. else
  359. {
  360. Print("Could not query value %s!%s. Error (%d).\n", szKey, szValue, l);
  361. }
  362. RegCloseKey(hkey);
  363. }
  364. else
  365. {
  366. Print("Could not open subkey %s. Error (%d).\n", szKey, l);
  367. }
  368. }
  369. return TRUE;
  370. }
  371. #endif // !KERNEL