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.

364 lines
12 KiB

  1. /*
  2. ****************************************************************************
  3. | Copyright (C) 2002 Microsoft Corporation
  4. |
  5. | Component / Subcomponent
  6. | IIS 6.0 / IIS Migration Wizard
  7. |
  8. | Based on:
  9. | http://iis6/Specs/IIS%20Migration6.0_Final.doc
  10. |
  11. | Abstract:
  12. | Utility helpers for migration
  13. |
  14. | Author:
  15. | ivelinj
  16. |
  17. | Revision History:
  18. | V1.00 March 2002
  19. |
  20. ****************************************************************************
  21. */
  22. #pragma once
  23. #include "Wrappers.h"
  24. #include "resource.h"
  25. // Directory utilities
  26. class CDirTools
  27. {
  28. private:
  29. CDirTools(); // Do not instantiate
  30. public:
  31. static void PathAppendLocal ( LPWSTR wszBuffer, LPCWSTR wszPath, LPCWSTR wszPathToAppend );
  32. static void CleanupDir ( LPCWSTR wszDir,
  33. bool bRemoveRoot = true,
  34. bool bReportErrors = false );
  35. static DWORD FileCount ( LPCWSTR wszDir, WORD wOptions );
  36. static DWORDLONG FilesSize ( LPCWSTR wszDir, WORD wOptions );
  37. static int DoPathsNest ( LPCWSTR wszPath1, LPCWSTR wszPath2 );
  38. };
  39. // CTempDir - used to create, store and autoclean tempdirs
  40. /////////////////////////////////////////////////////////////////////////////////////////
  41. class CTempDir
  42. {
  43. public:
  44. CTempDir ( LPCWSTR wszTemplate = L"Migr" );
  45. ~CTempDir ( void );
  46. public:
  47. void CleanUp ( bool bReportErrors = false );
  48. operator LPCWSTR ( void )const
  49. {
  50. return m_strDir.c_str();
  51. }
  52. private:
  53. std::wstring m_strDir;
  54. };
  55. // Class for any additional tool procs
  56. /////////////////////////////////////////////////////////////////////////////////////////
  57. class CTools
  58. {
  59. private:
  60. CTools();
  61. public:
  62. static bool IsAdmin ( void );
  63. static bool IsIISRunning ( void );
  64. static HRESULT CopyBSTR ( const _bstr_t& bstrSource, BSTR* pVal );
  65. static WORD GetOSVer ( void );
  66. static bool IsNTFS ( void );
  67. static void SetErrorInfo ( LPCWSTR wszError );
  68. static void SetErrorInfoFromRes ( UINT nResID );
  69. static bool IsSelfSignedCert ( PCCERT_CONTEXT pContext );
  70. static bool IsValidCert ( PCCERT_CONTEXT hCert, DWORD& rdwError );
  71. static std::wstring GetMachineName ( void );
  72. static void WriteFile ( HANDLE hFile, LPCVOID pvData, DWORD dwSize );
  73. static ULONGLONG GetFilePtrPos ( HANDLE hFile );
  74. static void SetFilePtrPos ( HANDLE hFile, DWORDLONG nOffset );
  75. static const TCertContextHandle AddCertToSysStore( PCCERT_CONTEXT pContext,
  76. LPCWSTR wszStore,
  77. bool bReuseCert );
  78. static const TCryptKeyHandle GetCryptKeyFromPwd( HCRYPTPROV hCryptProv, LPCWSTR wszPassword );
  79. static int __cdecl BadAllocHandler ( size_t )
  80. {
  81. // Installed at startup time for handling mem alloc failures
  82. throw CBaseException( IDS_E_OUTOFMEM, ERROR_SUCCESS );
  83. }
  84. };
  85. // CTools inline implementation
  86. /////////////////////////////////////////////////////////////////////////////////////////
  87. inline HRESULT CTools::CopyBSTR( const _bstr_t& bstrSource , BSTR* pVal )
  88. {
  89. _ASSERT( pVal != NULL );
  90. *pVal = ::SysAllocString( bstrSource );
  91. return ( (*pVal) != NULL ? S_OK : E_OUTOFMEMORY );
  92. }
  93. inline void CTools::WriteFile( HANDLE hFile, LPCVOID pvData, DWORD dwSize )
  94. {
  95. _ASSERT( ( hFile != NULL ) && ( hFile != INVALID_HANDLE_VALUE ) );
  96. _ASSERT( pvData != NULL );
  97. DWORD dwUnused = 0;
  98. // dwSize can be 0
  99. IF_FAILED_BOOL_THROW( ::WriteFile( hFile, pvData, dwSize, &dwUnused, NULL ),
  100. CBaseException( IDS_E_WRITE_OUTPUT ) );
  101. }
  102. // CFindFile - class that behaves like FindFirstFile with the only difference
  103. // that it works all the way down the tree ( all files from subdirs are retruned as well
  104. // No file mask currently implemented - if needed - store the mask and pass it on every FindFirstFile API
  105. /////////////////////////////////////////////////////////////////////////////////////////
  106. class CFindFile
  107. {
  108. // Data types
  109. public:
  110. // Use this options to refine the search criteria
  111. enum SearchOptions
  112. {
  113. ffRecursive = 0x0001, // If set - subdirs are searched for matches. Else - only the root dir is searched
  114. ffGetFiles = 0x0002, // If set - file objects are considered a match
  115. ffGetDirs = 0x0004, // If set - directory objects are considered a match
  116. ffAbsolutePaths = 0x0008, // If set - the dir names, returned from FindFirst and Next will be absolute.
  117. ffAddFilename = 0x0010, // If set - the dir names, returned from FindFirst and Next will include the name of the object.
  118. };
  119. // Construction / Destruction
  120. public:
  121. CFindFile ( void );
  122. ~CFindFile ( void );
  123. // Interface
  124. public:
  125. bool FindFirst ( LPCWSTR wszDirToScan,
  126. WORD wOptions,
  127. LPWSTR wszDir,
  128. WIN32_FIND_DATAW* pData );
  129. bool Next ( bool* pbDirChanged,
  130. LPWSTR wszDir,
  131. WIN32_FIND_DATAW* pData );
  132. void Close ( void );
  133. // Implementation
  134. private:
  135. bool CheckFile ( LPCWSTR wszRelativeDir, const WIN32_FIND_DATAW& fd );
  136. bool ScanDir ( LPCWSTR wszDirToScan,
  137. LPCWSTR wszRelative,
  138. WIN32_FIND_DATAW& FileData );
  139. bool ContinueCurrent ( WIN32_FIND_DATAW& FilepData );
  140. // Data members
  141. private:
  142. WORD m_wOptions; // Search options ( bitmask with SearchOptions enum values )
  143. TStringList m_DirsToScan; // Dirs that will be scaned after the current one
  144. TSearchHandle m_shSearch; // Search Handle
  145. std::wstring m_strRootDir; // Dir that is searched ( search root )
  146. std::wstring m_strCurrentDir; // Dir where m_hSearch is opened
  147. };
  148. // CXMLTools - class for XML input/output support
  149. // You may need the Convert class for easier handling different types of in/out XML data
  150. /////////////////////////////////////////////////////////////////////////////////////////
  151. class CXMLTools
  152. {
  153. private:
  154. CXMLTools();
  155. public:
  156. static IXMLDOMElementPtr AddTextNode( const IXMLDOMDocumentPtr& spDoc,
  157. const IXMLDOMElementPtr& spEl,
  158. LPCWSTR wszName,
  159. LPCWSTR wszValue );
  160. static const std::wstring GetAttrib ( const IXMLDOMNodePtr& spEl, LPCWSTR wszName );
  161. static void SetAttrib ( const IXMLDOMElementPtr& spEl, LPCWSTR wszName, LPCWSTR wszData );
  162. static void LoadXMLFile ( LPCWSTR wszFile, IXMLDOMDocumentPtr& rspDoc );
  163. static IXMLDOMElementPtr CreateSubNode( const IXMLDOMDocumentPtr& spDoc,
  164. const IXMLDOMElementPtr& spParent,
  165. LPCWSTR wszName );
  166. static void RemoveNodes ( const IXMLDOMElementPtr& spContext, LPCWSTR wszXPath );
  167. static const std::wstring GetDataValue( const IXMLDOMNodePtr& spRoot,
  168. LPCWSTR wszQuery,
  169. LPCWSTR wszAttrib,
  170. LPCWSTR wszDefaut );
  171. static const std::wstring GetDataValueAbs( const IXMLDOMDocumentPtr& spRoot,
  172. LPCWSTR wszQuery,
  173. LPCWSTR wszAttrib,
  174. LPCWSTR wszDefaut );
  175. static const IXMLDOMNodePtr SetDataValue( const IXMLDOMNodePtr& spRoot,
  176. LPCWSTR wszQuery,
  177. LPCWSTR wszAttrib,
  178. LPCWSTR wszNewValue,
  179. LPCWSTR wszNewElName = NULL );
  180. };
  181. // CXMLTols inline implementation
  182. /////////////////////////////////////////////////////////////////////////////////////////
  183. inline void CXMLTools::SetAttrib( const IXMLDOMElementPtr& spEl,
  184. LPCWSTR wszName,
  185. LPCWSTR wszData )
  186. {
  187. _ASSERT( spEl != NULL );
  188. _ASSERT( wszName != NULL );
  189. _ASSERT( wszData != NULL );
  190. IF_FAILED_HR_THROW( spEl->setAttribute( _bstr_t( wszName ), _variant_t( wszData ) ),
  191. CBaseException( IDS_E_XML_GENERATE ) );
  192. }
  193. inline IXMLDOMElementPtr CXMLTools::CreateSubNode( const IXMLDOMDocumentPtr& spDoc,
  194. const IXMLDOMElementPtr& spParent,
  195. LPCWSTR wszName )
  196. {
  197. _ASSERT( spDoc != NULL );
  198. _ASSERT( spParent != NULL );
  199. IXMLDOMElementPtr spResult;
  200. IF_FAILED_HR_THROW( spDoc->createElement( _bstr_t( wszName ), &spResult ),
  201. CBaseException( IDS_E_XML_GENERATE ) );
  202. IF_FAILED_HR_THROW( spParent->appendChild( spResult, NULL ),
  203. CBaseException( IDS_E_XML_GENERATE ) );
  204. return spResult;
  205. }
  206. // Convert class - simple class for providing basic type conversions.
  207. /////////////////////////////////////////////////////////////////////////////////////////
  208. class Convert
  209. {
  210. private:
  211. Convert();
  212. public:
  213. static const std::wstring ToString ( BYTE btVal );
  214. static const std::wstring ToString ( DWORD dwVal );
  215. static const std::wstring ToString ( DWORDLONG dwVal );
  216. static const std::wstring ToString ( bool bVal );
  217. static const std::wstring ToString ( const BYTE* pvData, DWORD dwSize );
  218. static void ToBLOB ( LPCWSTR wszData, TByteAutoPtr& rspData, DWORD&dwSize );
  219. static DWORD ToDWORD ( LPCWSTR wszData );
  220. static DWORDLONG ToDWORDLONG ( LPCWSTR wszData );
  221. static bool ToBool ( LPCWSTR wszData );
  222. private:
  223. static WCHAR ByteToWChar( BYTE b );
  224. static BYTE WCharsToByte( WCHAR chLow, WCHAR chHigh );
  225. };
  226. // Convert class inline implementation
  227. inline const std::wstring Convert::ToString( DWORD dwVal )
  228. {
  229. WCHAR wszBuff[ 16 ];
  230. ::swprintf( wszBuff, L"%u", dwVal );
  231. return std::wstring( wszBuff );
  232. }
  233. inline const std::wstring Convert::ToString( DWORDLONG dwVal )
  234. {
  235. WCHAR wszBuff[ 32 ];
  236. ::_ui64tow( dwVal, wszBuff, 10 );
  237. return std::wstring( wszBuff );
  238. }
  239. inline const std::wstring Convert::ToString( bool bVal )
  240. {
  241. return std::wstring( bVal ? L"True" : L"False" );
  242. }
  243. inline WCHAR Convert::ByteToWChar( BYTE b )
  244. {
  245. _ASSERT( b < 17 );
  246. return static_cast<WCHAR>( b >= 10 ? L'a' + b - 10 : L'0' + b );
  247. }
  248. inline BYTE Convert::WCharsToByte( WCHAR chLow, WCHAR chHigh )
  249. {
  250. _ASSERT( ( ( chLow >= L'0' ) && ( chLow <= L'9' ) ) ||
  251. ( ( chLow >= L'a' ) && ( chLow <= 'f' ) ) );
  252. _ASSERT( ( ( chHigh >= L'0' ) && ( chHigh <= L'9' ) ) ||
  253. ( ( chHigh >= L'a' ) && ( chHigh <= 'f' ) ) );
  254. BYTE bt = static_cast<BYTE>( ( chHigh >= L'a' ? chHigh - L'a' + 10 : chHigh - L'0' ) << 4 );
  255. bt = static_cast<BYTE>( bt + ( chLow >= L'a' ? chLow - L'a' + 10 : chLow - L'0' ) );
  256. return bt;
  257. }
  258. inline DWORDLONG Convert::ToDWORDLONG( LPCWSTR wszData )
  259. {
  260. return static_cast<DWORDLONG>( ::_wtoi64( wszData ) );
  261. }
  262. inline const std::wstring Convert::ToString( BYTE btVal )
  263. {
  264. return ToString( static_cast<DWORD>( btVal ) );
  265. }
  266. inline bool Convert::ToBool( LPCWSTR wszData )
  267. {
  268. return ::_wcsicmp( wszData, L"true" ) == 0;
  269. }