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.

381 lines
11 KiB

  1. /****************************************************************************\
  2. *
  3. * reghive.cpp
  4. *
  5. * Created: William Taylor (wtaylor) 02/13/01
  6. *
  7. * MS Ratings Registry Hive Handling
  8. *
  9. \****************************************************************************/
  10. #include "msrating.h"
  11. #include "mslubase.h"
  12. #include "reghive.h" // CRegistryHive
  13. #include "debug.h"
  14. const int c_iHiveFile1 = 0x1;
  15. const int c_iHiveFile2 = 0x2;
  16. CRegistryHive::CRegistryHive()
  17. {
  18. m_fHiveLoaded = false;
  19. ClearHivePath();
  20. }
  21. CRegistryHive::~CRegistryHive()
  22. {
  23. UnloadHive();
  24. }
  25. void CRegistryHive::UnloadHive( void )
  26. {
  27. if ( m_keyHive.m_hKey != NULL )
  28. {
  29. ::RegFlushKey( m_keyHive.m_hKey );
  30. m_keyHive.Close();
  31. ::RegFlushKey( HKEY_LOCAL_MACHINE );
  32. }
  33. if ( m_fHiveLoaded )
  34. {
  35. LONG err;
  36. err = ::RegUnLoadKey( HKEY_LOCAL_MACHINE, szTMPDATA );
  37. if ( err == ERROR_SUCCESS )
  38. {
  39. TraceMsg( TF_ALWAYS, "CRegistryHive::UnloadHive() - Succesfully Unloaded Hive '%s' from szTMPDATA='%s'!", m_szPath, szTMPDATA );
  40. m_fHiveLoaded = false;
  41. ClearHivePath();
  42. }
  43. else
  44. {
  45. TraceMsg( TF_WARNING, "CRegistryHive::UnloadHive() - Failed RegUnLoadKey 0x%x with szTMPDATA='%s'!", err, szTMPDATA );
  46. }
  47. }
  48. ASSERT( ! m_fHiveLoaded );
  49. }
  50. bool CRegistryHive::OpenHiveFile( bool p_fCreate )
  51. {
  52. UnloadHive();
  53. ASSERT( ! m_fHiveLoaded );
  54. LoadHiveFile( c_iHiveFile1 );
  55. if ( ! m_fHiveLoaded )
  56. {
  57. LoadHiveFile( c_iHiveFile2 );
  58. }
  59. if ( m_fHiveLoaded )
  60. {
  61. #ifdef DEBUG
  62. EnumerateRegistryKeys( HKEY_LOCAL_MACHINE, (LPSTR) szTMPDATA, 0 );
  63. #endif
  64. if ( OpenHiveKey() )
  65. {
  66. TraceMsg( TF_ALWAYS, "CRegistryHive::OpenHiveFile() - OpenHiveKey() succeeeded." );
  67. return true;
  68. }
  69. else
  70. {
  71. TraceMsg( TF_WARNING, "CRegistryHive::OpenHiveFile() - OpenHiveKey() failed!" );
  72. }
  73. }
  74. UnloadHive();
  75. if ( ! p_fCreate )
  76. {
  77. TraceMsg( TF_WARNING, "CRegistryHive::OpenHiveFile() - Failed to Open Existing Hive File!" );
  78. return false;
  79. }
  80. DeleteRegistryHive();
  81. int iHiveFile;
  82. // Returns the iHiveFile set to the hive file created (c_iHiveFile1 or c_iHiveFile2).
  83. if ( ! CreateNewHive( iHiveFile ) )
  84. {
  85. TraceMsg( TF_WARNING, "CRegistryHive::OpenHiveFile() - Failed to Create Hive File!" );
  86. return false;
  87. }
  88. DeleteRegistryHive();
  89. LoadHiveFile( iHiveFile );
  90. if ( m_fHiveLoaded )
  91. {
  92. if ( OpenHiveKey() )
  93. {
  94. TraceMsg( TF_ALWAYS, "CRegistryHive::OpenHiveFile() - OpenHiveKey() succeeeded." );
  95. return true;
  96. }
  97. else
  98. {
  99. TraceMsg( TF_WARNING, "CRegistryHive::OpenHiveFile() - OpenHiveKey() failed!" );
  100. }
  101. }
  102. UnloadHive();
  103. return false;
  104. }
  105. bool CRegistryHive::OpenHiveKey( void )
  106. {
  107. LONG err;
  108. err = m_keyHive.Open( HKEY_LOCAL_MACHINE, szPOLUSER );
  109. if (err == ERROR_SUCCESS)
  110. {
  111. TraceMsg( TF_ALWAYS, "CRegistryHive::OpenHiveKey() - Successful m_keyHive Open with szPOLUSER='%s'", szPOLUSER );
  112. return true;
  113. }
  114. else
  115. {
  116. TraceMsg( TF_WARNING, "CRegistryHive::OpenHiveKey() - Failed m_keyHive Open with szPOLUSER='%s'!", szPOLUSER );
  117. }
  118. return false;
  119. }
  120. void CRegistryHive::DeleteRegistryHive( void )
  121. {
  122. MyRegDeleteKey( HKEY_LOCAL_MACHINE, szTMPDATA );
  123. RegFlushKey( HKEY_LOCAL_MACHINE );
  124. }
  125. bool CRegistryHive::CreateNewHive( int & p_riHiveFile )
  126. {
  127. CRegKey keyHive;
  128. if ( keyHive.Create( HKEY_LOCAL_MACHINE, szTMPDATA ) != ERROR_SUCCESS )
  129. {
  130. TraceMsg( TF_WARNING, "CRegistryHive::CreateNewHive() - Failed to Create Hive Key szTMPDATA='%s'!", szTMPDATA );
  131. return false;
  132. }
  133. CRegKey keyUser;
  134. if ( keyUser.Create( keyHive.m_hKey, szUSERS ) != ERROR_SUCCESS )
  135. {
  136. TraceMsg( TF_WARNING, "CRegistryHive::CreateNewHive() - Failed to Create User Key szUSERS='%s'!", szUSERS );
  137. return false;
  138. }
  139. if ( SaveHiveKey( keyHive, c_iHiveFile1 ) )
  140. {
  141. TraceMsg( TF_ALWAYS, "CRegistryHive::CreateNewHive() - Saved Hive Key to Hive File 1!" );
  142. p_riHiveFile = c_iHiveFile1;
  143. return true;
  144. }
  145. if ( SaveHiveKey( keyHive, c_iHiveFile2 ) )
  146. {
  147. TraceMsg( TF_ALWAYS, "CRegistryHive::CreateNewHive() - Saved Hive Key to Hive File 2!" );
  148. p_riHiveFile = c_iHiveFile2;
  149. return true;
  150. }
  151. TraceMsg( TF_WARNING, "CRegistryHive::CreateNewHive() - Failed to Save Hive Key to Registry!" );
  152. return false;
  153. }
  154. bool CRegistryHive::SaveHiveKey( CRegKey & p_keyHive, int p_iFile )
  155. {
  156. bool fReturn = false;
  157. ASSERT( p_keyHive.m_hKey != NULL );
  158. SetHiveName( p_iFile );
  159. LONG err;
  160. err = ::RegSaveKey( p_keyHive.m_hKey, m_szPath, 0 );
  161. if ( err == ERROR_SUCCESS )
  162. {
  163. TraceMsg( TF_ALWAYS, "CRegistryHive::SaveHiveKey() - Saved Hive Key to m_szPath='%s'!", m_szPath );
  164. fReturn = true;
  165. }
  166. else
  167. {
  168. TraceMsg( TF_WARNING, "CRegistryHive::SaveHiveKey() - Failed to Save Hive Key 0x%x to m_szPath='%s'!", err, m_szPath );
  169. }
  170. return fReturn;
  171. }
  172. BOOL CRegistryHive::BuildPolName(LPSTR pBuffer, UINT cbBuffer, UINT (WINAPI *PathProvider)(LPTSTR, UINT))
  173. {
  174. if ((*PathProvider)(pBuffer, cbBuffer) + strlenf(szPOLFILE) + 2 > cbBuffer)
  175. return FALSE;
  176. LPSTR pchBackslash = strrchrf(pBuffer, '\\');
  177. if (pchBackslash == NULL || *(pchBackslash+1) != '\0')
  178. strcatf(pBuffer, "\\");
  179. strcatf(pBuffer, szPOLFILE);
  180. return TRUE;
  181. }
  182. void CRegistryHive::SetHiveName( int p_iFile )
  183. {
  184. ASSERT( p_iFile == c_iHiveFile1 || p_iFile == c_iHiveFile2 );
  185. ClearHivePath();
  186. if ( p_iFile == c_iHiveFile1 )
  187. {
  188. BuildPolName( m_szPath, sizeof(m_szPath), GetSystemDirectory );
  189. }
  190. else
  191. {
  192. BuildPolName( m_szPath, sizeof(m_szPath), GetWindowsDirectory );
  193. }
  194. }
  195. void CRegistryHive::LoadHiveFile( int p_iFile )
  196. {
  197. LONG err;
  198. err = ERROR_FILE_NOT_FOUND;
  199. ASSERT( ! m_fHiveLoaded );
  200. if ( m_fHiveLoaded )
  201. {
  202. TraceMsg( TF_WARNING, "CRegistryHive::LoadHiveFile() - Hive File Already Loaded!" );
  203. return;
  204. }
  205. SetHiveName( p_iFile );
  206. if ( ::GetFileAttributes( m_szPath ) != 0xFFFFFFFF )
  207. {
  208. err = ::RegLoadKey( HKEY_LOCAL_MACHINE, szTMPDATA, m_szPath );
  209. if ( err == ERROR_SUCCESS )
  210. {
  211. TraceMsg( TF_ALWAYS, "CRegistryHive::LoadHiveFile() - Loaded Hive File szTMPDATA='%s' m_szPath='%s'!", szTMPDATA, m_szPath );
  212. m_fHiveLoaded = true;
  213. }
  214. else
  215. {
  216. TraceMsg( TF_WARNING, "CRegistryHive::LoadHiveFile() - Failed RegLoadKey szTMPDATA='%s' m_szPath='%s'!", szTMPDATA, m_szPath );
  217. }
  218. }
  219. if ( ! m_fHiveLoaded )
  220. {
  221. ClearHivePath();
  222. }
  223. return;
  224. }
  225. #ifdef DEBUG
  226. void CRegistryHive::EnumerateRegistryKeys( HKEY hkeyTop, LPSTR pszKeyName, int iLevel )
  227. {
  228. if ( ! hkeyTop )
  229. {
  230. TraceMsg( TF_WARNING, "CRegistryHive::EnumerateRegistryKeys() - hkeyTop is NULL!" );
  231. return;
  232. }
  233. if ( ! pszKeyName )
  234. {
  235. TraceMsg( TF_WARNING, "CRegistryHive::EnumerateRegistryKeys() - pszKeyName is NULL!" );
  236. return;
  237. }
  238. CRegKey keyHive;
  239. if ( keyHive.Open( hkeyTop, pszKeyName ) == ERROR_SUCCESS )
  240. {
  241. // Enumerate Open Key's Values.
  242. {
  243. char szKeyValue[MAXPATHLEN];
  244. int j = 0;
  245. DWORD cchValueSize = sizeof(szKeyValue);
  246. DWORD dwType;
  247. // enumerate the subkeys, which are rating systems
  248. while ( RegEnumValue( keyHive.m_hKey, j, szKeyValue, &cchValueSize,
  249. NULL, &dwType, NULL, NULL ) == ERROR_SUCCESS )
  250. {
  251. switch ( dwType )
  252. {
  253. case REG_DWORD:
  254. {
  255. ETN etn;
  256. EtNumRegRead( etn, keyHive.m_hKey, szKeyValue );
  257. TraceMsg( TF_ALWAYS, "CRegistryHive::EnumerateRegistryKeys() - [%d]: etn=0x%x for %d pszKeyName='%s' szKeyValue='%s'", iLevel, etn.Get(), j, pszKeyName, szKeyValue );
  258. }
  259. break;
  260. case REG_BINARY:
  261. {
  262. ETB etb;
  263. EtBoolRegRead( etb, keyHive.m_hKey, szKeyValue );
  264. TraceMsg( TF_ALWAYS, "CRegistryHive::EnumerateRegistryKeys() - [%d]: etb=0x%x for %d pszKeyName='%s' szKeyValue='%s'", iLevel, etb.Get(), j, pszKeyName, szKeyValue );
  265. }
  266. break;
  267. case REG_SZ:
  268. {
  269. ETS ets;
  270. EtStringRegRead( ets, keyHive.m_hKey, szKeyValue );
  271. TraceMsg( TF_ALWAYS, "CRegistryHive::EnumerateRegistryKeys() - [%d]: ets='%s' for %d pszKeyName='%s' szKeyValue='%s'", iLevel, ets.Get(), j, pszKeyName, szKeyValue );
  272. }
  273. break;
  274. default:
  275. TraceMsg( TF_WARNING, "CRegistryHive::EnumerateRegistryKeys() - [%d]: Unhandled Enumeration Type %d for szKeyValue='%s'!", iLevel, dwType, szKeyValue );
  276. break;
  277. }
  278. cchValueSize = sizeof(szKeyValue);
  279. j++;
  280. }
  281. TraceMsg( TF_ALWAYS, "CRegistryHive::EnumerateRegistryKeys() - [%d]: Completed Enumeration of %d values in pszKeyName='%s'", iLevel, j, pszKeyName );
  282. }
  283. // Enumerate Open Key's Subkeys.
  284. {
  285. char szKeyName[MAXPATHLEN];
  286. int j = 0;
  287. // enumerate the subkeys, which are rating systems
  288. while ( RegEnumKey( keyHive.m_hKey, j, szKeyName, sizeof(szKeyName) ) == ERROR_SUCCESS )
  289. {
  290. EnumerateRegistryKeys( keyHive.m_hKey, szKeyName, iLevel+1 );
  291. j++;
  292. }
  293. TraceMsg( TF_ALWAYS, "CRegistryHive::EnumerateRegistryKeys() - [%d]: Completed Enumeration of %d keys in pszKeyName='%s'", iLevel, j, pszKeyName );
  294. }
  295. }
  296. else
  297. {
  298. TraceMsg( TF_WARNING, "CRegistryHive::EnumerateRegistryKeys() - [%d]: Failed to Open key pszKeyName='%s' for Enumeration!", iLevel, pszKeyName );
  299. }
  300. }
  301. #endif