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.

355 lines
10 KiB

  1. /*++
  2. Copyright (c) 2000-2001 Microsoft Corporation
  3. Module Name:
  4. params.cpp
  5. Abstract:
  6. Class the manages the dump parameters
  7. Author:
  8. Stefan R. Steiner [ssteiner] 02-18-2000
  9. Revision History:
  10. Avinash Pillai [apillai] 07-29-2002 Added options -o:t, -o:y, -o:f and -o:i
  11. --*/
  12. #include "stdafx.h"
  13. #define VERSION_INFO1 L"FsDumplib.lib Version 1.3g - 8/23/2000"
  14. #define VERSION_INFO2 L" Checksum version 2 - 2/22/2000"
  15. // Forward defines
  16. static BOOL
  17. AssertPrivilege(
  18. IN LPCWSTR privName
  19. );
  20. /*++
  21. Routine Description:
  22. Based on the class variables, sets up the utility to write to the
  23. correct files and gets backup privs.
  24. Arguments:
  25. None
  26. Return Value:
  27. <Enter return values here>
  28. --*/
  29. INT
  30. CDumpParameters::Initialize(
  31. IN INT argc,
  32. IN WCHAR *argv[]
  33. )
  34. {
  35. LPWSTR pwszFileName;
  36. //
  37. // Get the directory the fsdump.exe lives in. For use for finding .exclude files amoung
  38. // other things.
  39. //
  40. if ( ::GetFullPathName(
  41. m_cwsArgv0,
  42. FSD_MAX_PATH,
  43. m_cwsFullPathToEXE.GetBufferSetLength( FSD_MAX_PATH ),
  44. &pwszFileName ) == 0 )
  45. {
  46. ::fwprintf( stderr, L"ERROR getting full path for '%s', won't be able to find .include files\n", m_cwsArgv0.c_str() );
  47. m_cwsFullPathToEXE.ReleaseBuffer();
  48. }
  49. else
  50. {
  51. m_cwsFullPathToEXE.ReleaseBuffer();
  52. m_cwsArgv0 = m_cwsFullPathToEXE; // Keep the full path version
  53. CBsString m_cwsRight4 = m_cwsArgv0.Right( 4 );
  54. m_cwsRight4.MakeLower();
  55. if ( m_cwsRight4 != L".exe" )
  56. m_cwsArgv0 += L".exe";
  57. m_cwsFullPathToEXE = m_cwsFullPathToEXE.Left( m_cwsFullPathToEXE.GetLength() - ::wcslen( pwszFileName ) );
  58. }
  59. //
  60. // Set up checksum format
  61. //
  62. if ( m_bDumpCommaDelimited )
  63. ::wcscpy( m_pwszULongHexFmt, L"0x%08x" );
  64. else
  65. ::wcscpy( m_pwszULongHexFmt, L"%08x" );
  66. //
  67. // Set up the dump file
  68. //
  69. if ( m_cwsDumpFileName.IsEmpty() )
  70. {
  71. wprintf( L"fsdump: Printing dump information to 'stdout'\n" );
  72. }
  73. else
  74. {
  75. CBsString cwsFullPath;
  76. LPWSTR pwszFileName;
  77. //
  78. // Get the full path name for the dump file in case we change the working
  79. // directory later.
  80. //
  81. if ( ::GetFullPathName(
  82. m_cwsDumpFileName,
  83. FSD_MAX_PATH,
  84. cwsFullPath.GetBufferSetLength( FSD_MAX_PATH ),
  85. &pwszFileName ) == 0 )
  86. {
  87. ::fwprintf( stderr, L"ERROR - Unable to get full path name of dump file '%s' for write\n", m_cwsDumpFileName.c_str() );
  88. return 10;
  89. }
  90. cwsFullPath.ReleaseBuffer();
  91. m_cwsDumpFileName = cwsFullPath;
  92. m_fpDump = ::_wfopen( m_cwsDumpFileName, m_bUnicode ? L"wb" : L"w" );
  93. if ( m_fpDump == NULL )
  94. {
  95. ::fwprintf( stderr, L"ERROR - Unable to open dump file '%s' for write\n", m_cwsDumpFileName.c_str() );
  96. return 10;
  97. }
  98. if ( m_bNoHeaderFooter )
  99. {
  100. //
  101. // Try to create a named stream with the header and summary information
  102. //
  103. m_fpExtraInfoDump = ::_wfopen( m_cwsDumpFileName + L":Info", m_bUnicode ? L"wb" : L"w" );
  104. if ( m_fpExtraInfoDump != NULL )
  105. {
  106. wprintf( L"fsdump: Printing dump header and summary information to NTFS stream '%s'\n", (m_cwsDumpFileName + L":Info").c_str() );
  107. }
  108. else
  109. {
  110. m_fpExtraInfoDump = ::_wfopen( m_cwsDumpFileName + L".Info", m_bUnicode ? L"wb" : L"w" );
  111. if ( m_fpExtraInfoDump != NULL )
  112. {
  113. wprintf( L"fsdump: Printing dump header and summary information to file '%s'\n", (m_cwsDumpFileName + L".Info").c_str() );
  114. }
  115. else
  116. {
  117. wprintf( L"fsdump: Unable to create dump header and summary information file '%s'\n", (m_cwsDumpFileName + L".Info").c_str() );
  118. }
  119. }
  120. }
  121. else
  122. m_fpExtraInfoDump = m_fpDump;
  123. wprintf( L"fsdump: Printing dump information to '%s'\n", m_cwsDumpFileName.c_str() );
  124. }
  125. //
  126. // Set up the error log file
  127. //
  128. if ( m_cwsErrLogFileName.IsEmpty() )
  129. {
  130. wprintf( L"fsdump: Printing errors to 'stderr'\n" );
  131. }
  132. else
  133. {
  134. CBsString cwsFullPath;
  135. LPWSTR pwszFileName;
  136. //
  137. // Get the full path name for the dump file in case we change the working
  138. // directory later.
  139. //
  140. if ( ::GetFullPathName(
  141. m_cwsErrLogFileName,
  142. 1024,
  143. cwsFullPath.GetBufferSetLength( 1024 ),
  144. &pwszFileName ) == 0 )
  145. {
  146. fwprintf( stderr, L"ERROR - Unable to get full path name of error log file '%s' for write\n", m_cwsDumpFileName.c_str() );
  147. return 11;
  148. }
  149. cwsFullPath.ReleaseBuffer();
  150. m_cwsErrLogFileName = cwsFullPath;
  151. m_fpErrLog = ::_wfopen( m_cwsErrLogFileName, m_bUnicode ? L"wb" : L"w" );
  152. if ( m_fpErrLog == NULL )
  153. {
  154. ::fwprintf( stderr, L"ERROR - Unable to open error log file '%s' for write\n", m_cwsErrLogFileName.c_str() );
  155. return 11;
  156. }
  157. ::wprintf( L"fsdump: Printing errors to '%s'\n", m_cwsErrLogFileName.c_str() );
  158. }
  159. //
  160. // Print out a header in the dump file so that it is easy to determine
  161. // if dump formats are the same.
  162. //
  163. DumpPrint( VERSION_INFO1 );
  164. DumpPrint( VERSION_INFO2 );
  165. //
  166. // Dump out the command-line
  167. //
  168. CBsString cwsCommandLine;
  169. for ( INT idx = 0; idx < argc; ++idx )
  170. {
  171. cwsCommandLine += L" \"";
  172. cwsCommandLine += argv[ idx ];
  173. cwsCommandLine += L"\"";
  174. }
  175. DumpPrint( L" Command-line: %s", cwsCommandLine.c_str() );
  176. //
  177. // Enable backup and security privs
  178. //
  179. if ( !::AssertPrivilege( SE_BACKUP_NAME ) )
  180. DumpPrint( L" n.b. could not get SE_BACKUP_NAME Privilege (%d), will be unable to get certain information",
  181. ::GetLastError() );
  182. if ( !::AssertPrivilege( SE_SECURITY_NAME ) )
  183. {
  184. DumpPrint( L" n.b. could not get SE_SECURITY_NAME Privilege (%d), SACL entries information will be invalid",
  185. ::GetLastError() );
  186. m_bHaveSecurityPrivilege = FALSE;
  187. }
  188. DumpPrint( L" File attributes masked: %04x", m_dwFileAttributesMask );
  189. DumpPrint( L" Command line options enabled:" );
  190. if ( m_bHex )
  191. DumpPrint( L" Printing sizes in hexidecimal" );
  192. if ( m_bNoChecksums )
  193. DumpPrint( L" Checksums disabled" );
  194. else if( m_bNoEncryptedChecksum )
  195. DumpPrint( L" Encrypted Checksum disabled" );
  196. if ( m_bUnicode )
  197. DumpPrint( L" Unicode output" );
  198. if ( m_bDontTraverseMountpoints )
  199. DumpPrint( L" Mountpoint traversal disabled" );
  200. if ( !m_bDontChecksumHighLatencyData )
  201. DumpPrint( L" High latency data checksum enabled" );
  202. if(!m_bAddSecsToTimestamps) //added by apillai
  203. DumpPrint( L" Disabling secs from timestamps" ); //added by apillai
  204. if ( m_bAddMillisecsToTimestamps )
  205. DumpPrint( L" Adding millsecs to timestamps" );
  206. if ( m_bShowSymbolicSIDNames )
  207. DumpPrint( L" Converting SIDs to symbolic DOMAIN\\ACCOUNTNAME format" );
  208. if ( !m_bDontShowDirectoryTimestamps )
  209. DumpPrint( L" Dumping directory timestamps" );
  210. if(!m_bNoShortFileName)
  211. DumpPrint(L" Dumping Short File names" );
  212. if((m_bDumpCommaDelimited) && (!m_bNoFileSystemType))
  213. DumpPrint(L" Dumping File System type" );
  214. if ( m_bUseExcludeProcessor )
  215. {
  216. if ( m_bDontUseRegistryExcludes )
  217. DumpPrint( L" Excluding file based on exclude files" );
  218. else
  219. DumpPrint( L" Excluding file based on FilesNotToBackup reg keys and exclude files" );
  220. }
  221. if ( m_bDisableLongPaths )
  222. DumpPrint( L" Long path support disabled" );
  223. if ( m_bEnableObjectIdExtendedDataChecksums )
  224. DumpPrint( L" Object Id extended data checksums Enabled" );
  225. DumpPrint( L"" );
  226. fflush( GetDumpFile() );
  227. return 0;
  228. }
  229. /*++
  230. Routine Description:
  231. Destructor for the CDumpParameters class
  232. Arguments:
  233. None
  234. Return Value:
  235. <Enter return values here>
  236. --*/
  237. CDumpParameters::~CDumpParameters()
  238. {
  239. if ( m_fpDump != NULL && m_fpDump != stdout )
  240. ::fclose( m_fpDump );
  241. if ( m_fpExtraInfoDump != NULL && m_fpExtraInfoDump != m_fpDump )
  242. ::fclose( m_fpExtraInfoDump );
  243. if ( m_fpErrLog != NULL && m_fpErrLog != stderr )
  244. ::fclose( m_fpErrLog );
  245. }
  246. /*++
  247. Routine Description:
  248. Enables an NT privilege. Used to get backup privs in the utility.
  249. Arguments:
  250. privName - The privilege name.
  251. Return Value:
  252. <Enter return values here>
  253. --*/
  254. static BOOL
  255. AssertPrivilege(
  256. IN LPCWSTR privName
  257. )
  258. {
  259. HANDLE tokenHandle;
  260. BOOL stat = FALSE;
  261. if ( OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &tokenHandle ) )
  262. {
  263. LUID value;
  264. if ( LookupPrivilegeValue( NULL, privName, &value ) )
  265. {
  266. TOKEN_PRIVILEGES newState;
  267. DWORD error;
  268. newState.PrivilegeCount = 1;
  269. newState.Privileges[0].Luid = value;
  270. newState.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  271. /*
  272. * We will always call GetLastError below, so clear
  273. * any prior error values on this thread.
  274. */
  275. SetLastError( ERROR_SUCCESS );
  276. stat = AdjustTokenPrivileges(
  277. tokenHandle,
  278. FALSE,
  279. &newState,
  280. (DWORD)0,
  281. NULL,
  282. NULL );
  283. /*
  284. * Supposedly, AdjustTokenPriveleges always returns TRUE
  285. * (even when it fails). So, call GetLastError to be
  286. * extra sure everything's cool.
  287. */
  288. if ( (error = GetLastError()) != ERROR_SUCCESS )
  289. {
  290. stat = FALSE;
  291. }
  292. }
  293. CloseHandle( tokenHandle );
  294. }
  295. return stat;
  296. }