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.

437 lines
14 KiB

  1. #include "setupp.h"
  2. #pragma hdrstop
  3. #define SYSTEM_HIVE (LPWSTR)L"system"
  4. #define SOFTWARE_HIVE (LPWSTR)L"software"
  5. #define SECURITY_HIVE (LPWSTR)L"security"
  6. #define SAM_HIVE (LPWSTR)L"sam"
  7. #define DEFAULT_USER_HIVE (LPWSTR)L".default"
  8. #define DEFAULT_USER_HIVE_FILE (LPWSTR)L"default"
  9. #define NTUSER_HIVE_FILE (LPWSTR)L"ntuser.dat"
  10. #define REPAIR_DIRECTORY (LPWSTR)L"\\repair"
  11. #define SETUP_LOG_FILE (LPWSTR)L"setup.log"
  12. #define NTUSER_COMPRESSED_FILE_NAME ( LPWSTR )L"ntuser.da_"
  13. #define AUTOEXEC_NT_FILE_NAME ( LPWSTR )L"autoexec.nt"
  14. #define CONFIG_NT_FILE_NAME ( LPWSTR )L"config.nt"
  15. //
  16. // Relative costs to perform various actions,
  17. // to help make the gas gauge mean something.
  18. //
  19. #define COST_SAVE_HIVE 3
  20. #define COST_COMPRESS_HIVE 20
  21. #define COST_SAVE_VDM_FILE 1
  22. //
  23. // Structure used in the array of hives to be saved.
  24. // This structure contains the predefined key that contains the hive
  25. // to be saved, and the name of the hive root, and the name of the file
  26. // where the hive should be saved.
  27. //
  28. typedef struct _HIVE_INFO {
  29. HKEY PredefinedKey;
  30. PWSTR HiveName;
  31. PWSTR FileName;
  32. } HIVE_INFO, *PHIVE_INFO;
  33. //
  34. // The array below contains the location and name of all hives that
  35. // we need to save. When the utility is operating on the silent mode,
  36. // (invoked from setup), then all hives will be saved. Otherwise, only
  37. // system and software will be saved.
  38. // For this reason, do not change the order of the hives in the array
  39. // below. System and software must be the first elements of
  40. // the array.
  41. //
  42. static
  43. HIVE_INFO HiveList[] = {
  44. { HKEY_LOCAL_MACHINE, SYSTEM_HIVE, SYSTEM_HIVE },
  45. { HKEY_LOCAL_MACHINE, SOFTWARE_HIVE, SOFTWARE_HIVE },
  46. { HKEY_USERS, DEFAULT_USER_HIVE, DEFAULT_USER_HIVE_FILE },
  47. { HKEY_LOCAL_MACHINE, SECURITY_HIVE, SECURITY_HIVE },
  48. { HKEY_LOCAL_MACHINE, SAM_HIVE, SAM_HIVE }
  49. };
  50. static
  51. PWSTR VdmFiles[] = {
  52. AUTOEXEC_NT_FILE_NAME,
  53. CONFIG_NT_FILE_NAME
  54. };
  55. DWORD
  56. SaveOneHive(
  57. IN LPWSTR DirectoryName,
  58. IN LPWSTR HiveName,
  59. IN HKEY hkey,
  60. IN HWND hWnd,
  61. IN OUT PDWORD GaugePosition,
  62. IN DWORD GaugeDeltaUnit
  63. )
  64. /*++
  65. Routine Description:
  66. Save one registry hive. The way we will do this is to do a RegSaveKey
  67. of the hive into a temporary localtion, and then call the LZ apis to
  68. compress the file from that temporary location to the floppy.
  69. LZ must have already been initialized via InitGloablBuffersEx()
  70. BEFORE calling this routine.
  71. Arguments:
  72. DirectoryName - Full path of the directory where the hive will be saved.
  73. HiveName - base name of the hive file to save. The file will end up
  74. compressed on disk with the name <HiveName>._.
  75. hkey - supplies handle to open key of root of hive to save.
  76. GaugePosition - in input, supplies current position of the gas gauge.
  77. On output, supplies new position of gas gauge.
  78. GaugeDeltaUnit - supplies cost of one unit of activity.
  79. Return Value:
  80. DWORD - Return ERROR_SUCCESS if the hive was saved. Otherwise, it returns
  81. an error code.
  82. --*/
  83. {
  84. DWORD Status;
  85. WCHAR SaveFilename[ MAX_PATH + 1 ];
  86. WCHAR CompressPath[ MAX_PATH + 1 ];
  87. CHAR SaveFilenameAnsi[ MAX_PATH + 1 ];
  88. CHAR CompressPathAnsi[ MAX_PATH + 1 ];
  89. LPWSTR TempName = ( LPWSTR )L"\\$$hive$$.tmp";
  90. //
  91. // Create the name of the file into which we will save the
  92. // uncompressed hive.
  93. //
  94. wsprintf(SaveFilename,L"%ls\\%ls.",DirectoryName,HiveName);
  95. wsprintfA(SaveFilenameAnsi,"%ls\\%ls.",DirectoryName,HiveName);
  96. //
  97. // Delete the file just in case, because RegSaveKey will fail if the file
  98. // already exists.
  99. //
  100. SetFileAttributes(SaveFilename,FILE_ATTRIBUTE_NORMAL);
  101. DeleteFile(SaveFilename);
  102. //
  103. // Save the registry hive into the temporary file.
  104. //
  105. Status = RegSaveKey(hkey,SaveFilename,NULL);
  106. //
  107. // Update the gas gauge.
  108. //
  109. *GaugePosition += GaugeDeltaUnit * COST_SAVE_HIVE;
  110. SendMessage(
  111. hWnd,
  112. PBM_SETPOS,
  113. *GaugePosition,
  114. 0L
  115. );
  116. //
  117. // If the hive was saved successfully, then delete the old compressed
  118. // one if it happens to be there.
  119. //
  120. if(Status == ERROR_SUCCESS) {
  121. //
  122. // Form the name of the file into which the saved hive file is
  123. // to be compressed.
  124. //
  125. wsprintf(CompressPath,L"%ls\\%ls._",DirectoryName,HiveName);
  126. wsprintfA(CompressPathAnsi,"%ls\\%ls._",DirectoryName,HiveName );
  127. //
  128. // Delete the destination file just in case.
  129. //
  130. SetFileAttributes(CompressPath,FILE_ATTRIBUTE_NORMAL);
  131. DeleteFile(CompressPath);
  132. }
  133. return(Status);
  134. }
  135. VOID
  136. SaveRepairInfo(
  137. IN HWND hWnd,
  138. IN ULONG StartAtPercent,
  139. IN ULONG StopAtPercent
  140. )
  141. /*++
  142. Routine Description:
  143. This routine implements the thread that saves all system configuration
  144. files into the repair directory. It first saves and compresses the
  145. registry hives, and then it save the VDM configuration files (autoexec.nt
  146. and config.nt).
  147. If the application is running in the SilentMode (invoked by setup),
  148. then system, software, default, security and sam hives will be saved
  149. and compressed.
  150. If the application was invoked by the user, then only system, software
  151. and default will be saved.
  152. This thread will send messages to the gas gauge dialog prcedure, so that
  153. the gas gauge gets updated after each configuration file is saved.
  154. This thread will also inform the user about errors that might have
  155. occurred during the process of saving the configuration files.
  156. Arguments:
  157. hWnd - handle to progress gauge
  158. StartAtPercent - Position where the progress window should start (0% to 100%).
  159. StopAtPercent - Maximum position where the progress window can be moved to (0% to 100%).
  160. Return Value:
  161. None.
  162. However, the this routine will send a message to the dialog procedure
  163. that created the thread, informing the outcome the operation.
  164. --*/
  165. {
  166. DWORD i;
  167. HKEY hkey;
  168. BOOL ErrorOccurred;
  169. CHAR SourceUserHivePathAnsi[ MAX_PATH + 1 ];
  170. CHAR UncompressedUserHivePathAnsi[ MAX_PATH + 1 ];
  171. CHAR CompressedUserHivePathAnsi[ MAX_PATH + 1 ];
  172. WCHAR ProfilesDirectory[ MAX_PATH + 1 ];
  173. WCHAR RepairDirectory[ MAX_PATH + 1 ];
  174. WCHAR SystemDirectory[ MAX_PATH + 1 ];
  175. WCHAR Source[ MAX_PATH + 1 ];
  176. WCHAR Target[ MAX_PATH + 1 ];
  177. DWORD GaugeDeltaUnit;
  178. DWORD GaugeTotalCost;
  179. DWORD GaugeRange;
  180. DWORD GaugePosition;
  181. DWORD NumberOfHivesToSave;
  182. DWORD NumberOfUserHivesToSave;
  183. DWORD NumberOfVdmFiles;
  184. DWORD dwSize;
  185. DWORD Error;
  186. DWORD Status;
  187. HANDLE Token;
  188. BOOL b;
  189. TOKEN_PRIVILEGES NewPrivileges;
  190. LUID Luid;
  191. Error = ERROR_SUCCESS;
  192. ErrorOccurred = FALSE;
  193. //
  194. // Compute the cost of saving the hives and vdm files.
  195. // For every hive we save, we have to save a key into a file and then
  196. // compress the file. After each of these tasks is completed, we upgrade
  197. // the gas gauge by the amount dicated by the COST_xxx values.
  198. // The cost of saving the hives depends on the mode that the utility is
  199. // running.
  200. //
  201. NumberOfHivesToSave = sizeof( HiveList ) / sizeof( HIVE_INFO );
  202. NumberOfUserHivesToSave = 1;
  203. NumberOfVdmFiles = sizeof( VdmFiles ) / sizeof( PWSTR );
  204. GaugeTotalCost = (COST_SAVE_HIVE * NumberOfHivesToSave)
  205. + (COST_SAVE_VDM_FILE * NumberOfVdmFiles);
  206. GaugeRange = (GaugeTotalCost*100/(StopAtPercent-StartAtPercent));
  207. GaugeDeltaUnit = 1;
  208. GaugePosition = GaugeRange*StartAtPercent/100;
  209. SendMessage(hWnd, WMX_PROGRESSTICKS, GaugeTotalCost, 0);
  210. SendMessage(hWnd,PBM_SETRANGE,0,MAKELPARAM(0,GaugeRange));
  211. SendMessage(hWnd,PBM_SETPOS,GaugePosition,0);
  212. //
  213. // Enable BACKUP privilege
  214. //
  215. if(OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&Token)) {
  216. if(LookupPrivilegeValue(NULL,SE_BACKUP_NAME,&Luid)) {
  217. NewPrivileges.PrivilegeCount = 1;
  218. NewPrivileges.Privileges[0].Luid = Luid;
  219. NewPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  220. AdjustTokenPrivileges(Token,FALSE,&NewPrivileges,0,NULL,NULL);
  221. }
  222. }
  223. Status = GetWindowsDirectory( RepairDirectory, sizeof( RepairDirectory ) / sizeof( WCHAR ) );
  224. if( Status == 0) {
  225. MYASSERT(FALSE);
  226. return;
  227. }
  228. lstrcat( RepairDirectory, REPAIR_DIRECTORY );
  229. dwSize = MAX_PATH + 1;
  230. GetDefaultUserProfileDirectoryW (ProfilesDirectory, &dwSize);
  231. GetSystemDirectory( SystemDirectory, sizeof( SystemDirectory ) / sizeof( WCHAR ) );
  232. //
  233. // Make sure that the repair directory already exists.
  234. // If it doesn't exist, then create one.
  235. //
  236. if( CreateDirectory( RepairDirectory, NULL ) ||
  237. ( ( Error = GetLastError() ) == ERROR_ALREADY_EXISTS ) ||
  238. ( Error == ERROR_ACCESS_DENIED )
  239. ) {
  240. //
  241. // If the repair directory didn't exist and we were able to create it,
  242. // or if the repair directory already exists, then save and compress
  243. // the hives.
  244. //
  245. Error = ERROR_SUCCESS;
  246. for( i=0; i < NumberOfHivesToSave; i++ ) {
  247. //
  248. // First open the root of the hive to be saved
  249. //
  250. Status = RegOpenKeyEx( HiveList[i].PredefinedKey,
  251. HiveList[i].HiveName,
  252. REG_OPTION_RESERVED,
  253. READ_CONTROL,
  254. &hkey );
  255. //
  256. // If unable to open the key, update the gas gauge to reflect
  257. // that the operation on this hive was completed.
  258. // Otherwise, save the hive. Note that Save hive will update
  259. // the gas gauge, as it saves and compresses the hive.
  260. //
  261. if(Status != ERROR_SUCCESS) {
  262. //
  263. // If this is the first error while saving the hives,
  264. // then save the error code, so that we can display the
  265. // correct error message to the user.
  266. //
  267. if( Error == ERROR_SUCCESS ) {
  268. Error = Status;
  269. }
  270. //
  271. // Update the gas gauge
  272. //
  273. GaugePosition += GaugeDeltaUnit * (COST_SAVE_HIVE + COST_COMPRESS_HIVE);
  274. SendMessage( hWnd,
  275. PBM_SETPOS,
  276. GaugePosition,
  277. 0L );
  278. } else {
  279. //
  280. // Save and compress the hive.
  281. // Note that the gas gauge will up be updated by SaveOneHive
  282. // Note also that when we save the default user hive, we skip
  283. // the first character of the
  284. //
  285. Status = SaveOneHive(RepairDirectory,
  286. HiveList[i].FileName,
  287. hkey,
  288. hWnd,
  289. &GaugePosition,
  290. GaugeDeltaUnit );
  291. //
  292. // If this is the first error while saving the hives,
  293. // then save the error code, so that we can display the
  294. // correct error message to the user.
  295. //
  296. if( Error == ERROR_SUCCESS ) {
  297. Error = Status;
  298. }
  299. RegCloseKey(hkey);
  300. }
  301. }
  302. //
  303. // Save the hive for the Default User
  304. //
  305. wsprintfA(SourceUserHivePathAnsi,"%ls\\%ls",ProfilesDirectory,NTUSER_HIVE_FILE);
  306. wsprintfA(UncompressedUserHivePathAnsi,"%ls\\%ls",RepairDirectory,NTUSER_HIVE_FILE);
  307. wsprintfA(CompressedUserHivePathAnsi, "%ls\\%ls",RepairDirectory,NTUSER_COMPRESSED_FILE_NAME);
  308. Status = CopyFileA (
  309. SourceUserHivePathAnsi,
  310. UncompressedUserHivePathAnsi,
  311. FALSE);
  312. if(Status) {
  313. //
  314. // Delete the destination file just in case.
  315. //
  316. SetFileAttributesA(CompressedUserHivePathAnsi,FILE_ATTRIBUTE_NORMAL);
  317. DeleteFileA(CompressedUserHivePathAnsi);
  318. } else if(Error == ERROR_SUCCESS) {
  319. //
  320. // If this is the first error, remember it.
  321. //
  322. Error = GetLastError();
  323. }
  324. //
  325. // Now that the hives are saved, save the vdm files
  326. //
  327. for( i = 0; i < NumberOfVdmFiles; i++ ) {
  328. wsprintf(Source,L"%ls\\%ls",SystemDirectory,VdmFiles[i]);
  329. wsprintf(Target,L"%ls\\%ls",RepairDirectory,VdmFiles[i]);
  330. if( !CopyFile( Source, Target, FALSE ) ) {
  331. Status = GetLastError();
  332. if( Error != ERROR_SUCCESS ) {
  333. Error = Status;
  334. }
  335. }
  336. GaugePosition += GaugeDeltaUnit * COST_SAVE_VDM_FILE;
  337. SendMessage( ( HWND )hWnd,
  338. PBM_SETPOS,
  339. GaugePosition,
  340. 0L );
  341. }
  342. }
  343. if( Error != ERROR_SUCCESS ) {
  344. SetupDebugPrint1( L"SETUP: SaveRepairInfo() failed. Error = %d", Error );
  345. }
  346. //
  347. // Set security on all the files.
  348. //
  349. ApplySecurityToRepairInfo();
  350. //
  351. // At this point, the operation was completed (successfully, or not).
  352. // So update the gas gauge to 100%
  353. //
  354. GaugePosition = GaugeRange*StopAtPercent/100;
  355. SendMessage(hWnd,PBM_SETPOS,GaugePosition,0L);
  356. }