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.

337 lines
10 KiB

  1. /******************************************************************************
  2. *
  3. * Copyright (c) 2000 Microsoft Corporation
  4. *
  5. * Module Name:
  6. * utils.h
  7. *
  8. * Abstract:
  9. * Declarations for commonly used util functions.
  10. *
  11. * Revision History:
  12. * Brijesh Krishnaswami (brijeshk) 03/17/2000
  13. * created
  14. *
  15. *****************************************************************************/
  16. #ifndef _UTILS_H_
  17. #define _UTILS_H_
  18. // trace macros
  19. #define TENTER TraceFunctEnter
  20. #define TLEAVE TraceFunctLeave
  21. #define TRACE DebugTrace
  22. #define tenter TraceFunctEnter
  23. #define tleave TraceFunctLeave
  24. #define trace DebugTrace
  25. // lock macros
  26. #define LOCKORLEAVE(a) if (! (a = m_DSLock.Lock(CLock::TIMEOUT))) { dwRc = ERROR_TIMEOUT; goto done; }
  27. #define LOCKORLEAVE_EX(a, t) if (! (a = m_DSLock.Lock(t))) { dwRc = ERROR_TIMEOUT; goto done; }
  28. #define UNLOCK(a) if (a) { m_DSLock.Unlock(); a = FALSE; }
  29. #define CHECKERR(f, trace) dwErr = (f); if (dwErr != ERROR_SUCCESS) \
  30. { \
  31. TRACE(0, "! %s : %ld", trace, dwErr); \
  32. goto Err; \
  33. }
  34. // mem macros
  35. #define SRMemAlloc(a) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, a)
  36. #define SRMemFree(a) if (a) HeapFree(GetProcessHeap(), 0, a)
  37. // unicode-ansi conversion routines
  38. WCHAR * ConvertToUnicode(CHAR * pszString);
  39. CHAR * ConvertToANSI(WCHAR * pszwString);
  40. #define UnicodeStringToWchar(US, pwsz) CopyMemory(pwsz, US.Buffer, US.Length); \
  41. pwsz[US.Length/sizeof(WCHAR)]=L'\0'
  42. // directory traversal routines
  43. DWORD GetFileSize_Recurse (const WCHAR *pwszDir,
  44. INT64 *pllTotalBytes,
  45. BOOL *pfStop);
  46. DWORD CompressFile (const WCHAR *pwszPath, BOOL fCompress, BOOL fDirectory);
  47. DWORD TakeOwnership( LPCWSTR pwcszPath, BOOL fAllowDelete );
  48. DWORD Delnode_Recurse (const WCHAR *pwszDir, BOOL fDeleteRoot, BOOL *pfStop);
  49. DWORD CopyFile_Recurse (const WCHAR *pwszSource, const WCHAR *pwszDest);
  50. // returns system drive as "C:\" (if system drive is C) or as volume name
  51. #define MAX_SYS_DRIVE 10
  52. BOOL GetSystemDrive(LPWSTR pszDrive);
  53. // returns TRUE if pszDrive contains the string L"C:" (if system drive is C)
  54. BOOL IsSystemDrive(LPWSTR pszDrive);
  55. // restore point routines
  56. LPWSTR GetMachineGuid();
  57. LPWSTR MakeRestorePath(LPWSTR pszDest, LPCWSTR pszDrive, LPCWSTR pszSuffix);
  58. ULONG GetID(LPCWSTR pszStr);
  59. // registry routines
  60. DWORD RegReadDWORD(HKEY hKey, LPCWSTR pszName, PDWORD pdwValue);
  61. DWORD RegWriteDWORD(HKEY hKey, LPCWSTR pszName, PDWORD pdwValue);
  62. // set/get start type of specified service
  63. DWORD SetServiceStartup(LPCWSTR pszName, DWORD dwStartType);
  64. DWORD GetServiceStartup(LPCWSTR pszName, PDWORD pdwStartType);
  65. DWORD GetServiceStartupRegistry(LPCWSTR pszName, PDWORD pdwStartType);
  66. BOOL StopSRService(BOOL fWait);
  67. // get the current domain or workgroup name
  68. DWORD GetDomainMembershipInfo (WCHAR *pwszPath, WCHAR *pwszzBuffer);
  69. // get the LSA secrets for restore
  70. DWORD GetLsaRestoreState (HKEY hKeySoftware);
  71. DWORD SetLsaSecret (PVOID hPolicy, const WCHAR *wszSecret,
  72. WCHAR * wszSecretValue);
  73. BOOL DoesDirExist(const TCHAR * pszFileName );
  74. BOOL DoesFileExist(const TCHAR * pszFileName);
  75. // this function creates all sub directories under the specified file
  76. // name.
  77. BOOL CreateBaseDirectory(const WCHAR * pszFileName);
  78. DWORD SRLoadString(LPCWSTR pszModule, DWORD dwStringId, LPWSTR pszString, DWORD cbBytes);
  79. // sets acl allowing specific access to LocalSystem/Admin
  80. // and to everyone
  81. DWORD
  82. SetAclInObject(HANDLE hObject,
  83. DWORD dwObjectType,
  84. DWORD dwSystemMask,
  85. DWORD dwEveryoneMask,
  86. BOOL fInherit);
  87. void
  88. PostTestMessage(UINT msg, WPARAM wp, LPARAM lp);
  89. // inline mem alloc class
  90. class CSRAlloc
  91. {
  92. public:
  93. inline void *operator new(size_t size)
  94. {
  95. return SRMemAlloc (size);
  96. }
  97. inline void operator delete (void * pv)
  98. {
  99. SRMemFree (pv);
  100. }
  101. };
  102. //////////////////////////////////////////////////////////////////////
  103. // CLock - class that allows exclusive access to a resource
  104. // uses a mutex - does not differentiate between readers/writers
  105. class CLock
  106. {
  107. HANDLE hResource;
  108. public:
  109. BOOL fHaveLock;
  110. CLock();
  111. ~CLock();
  112. DWORD Init();
  113. BOOL Lock(int iTimeOut);
  114. void Unlock();
  115. static const enum {TIMEOUT = 10*60000};
  116. };
  117. //
  118. // util function that checks the SR Stop event
  119. // to see if it has been signalled or not
  120. // will return TRUE if the event does not exist
  121. //
  122. BOOL IsStopSignalled(HANDLE hEvent);
  123. // The following function logs the name of a file in the DS. The
  124. // problem right now is that the path of the DS is so long that the
  125. // relevant information is thrown away from the trace buffer.
  126. void LogDSFileTrace(DWORD dwTraceID,
  127. const WCHAR * pszPrefix, // Initial message to be traced
  128. const WCHAR * pszDSFile);
  129. typedef DWORD (* PPROCESSFILEMETHOD) (WCHAR * pszBaseDir,// Base Directory
  130. const WCHAR * pszFile);
  131. // File to process
  132. DWORD DeleteGivenFile(WCHAR * pszBaseDir, // Base Directory
  133. const WCHAR * pszFile); // file to delete
  134. DWORD ProcessGivenFiles(WCHAR * pszBaseDir,
  135. PPROCESSFILEMETHOD pfnMethod,
  136. WCHAR * pszFindFileData);
  137. //++-----------------------------------------------------------------------
  138. //
  139. // Function: WriteRegKey
  140. //
  141. // Synopsis: This function writes into a registry key. It also creates it
  142. // if it does not exist.
  143. //
  144. // Arguments:
  145. //
  146. // Returns: TRUE no error
  147. // FALSE a fatal error happened
  148. //
  149. // History: AshishS Created 5/22/96
  150. //------------------------------------------------------------------------
  151. BOOL WriteRegKey(BYTE * pbRegValue,
  152. DWORD dwNumBytes,
  153. const TCHAR * pszRegKey,
  154. const TCHAR * pszRegValueName,
  155. DWORD dwRegType);
  156. //++------------------------------------------------------------------------
  157. //
  158. // Function: ReadRegKey
  159. //
  160. // Synopsis: This function reads a registry key and creates it
  161. // if it does not exist with the default value.
  162. //
  163. // Arguments:
  164. //
  165. // Returns: TRUE no error
  166. // FALSE a fatal error happened
  167. //
  168. // History: AshishS Created 5/22/96
  169. //------------------------------------------------------------------------
  170. BOOL ReadRegKeyOrCreate(BYTE * pbRegValue, // The value of the reg key will be
  171. // stored here
  172. DWORD * pdwNumBytes, // Pointer to DWORD conataining
  173. // the number of bytes in the above buffer - will be
  174. // set to actual bytes stored.
  175. const TCHAR * pszRegKey, // Reg Key to be opened
  176. const TCHAR * pszRegValueName, // Reg Value to query
  177. DWORD dwRegTypeExpected,
  178. BYTE * pbDefaultValue, // default value
  179. DWORD dwDefaultValueSize); // size of default value
  180. //++------------------------------------------------------------------------
  181. //
  182. // Function: ReadRegKey
  183. //
  184. // Synopsis: This function reads a registry key.
  185. //
  186. // Arguments:
  187. //
  188. // Returns: TRUE no error
  189. // FALSE a fatal error happened
  190. //
  191. // History: AshishS Created 5/22/96
  192. //------------------------------------------------------------------------
  193. BOOL ReadRegKey(BYTE * pbRegValue, // The value of the reg key will be
  194. // stored here
  195. DWORD * pdwNumBytes, // Pointer to DWORD conataining
  196. // the number of bytes in the above buffer - will be
  197. // set to actual bytes stored.
  198. const TCHAR * pszRegKey, // Reg Key to be opened
  199. const TCHAR * pszRegValueName, // Reg Value to query
  200. DWORD dwRegTypeExpected); // Expected type of Value
  201. // this function checks to see of the restore failed because of disk space
  202. BOOL CheckForDiskSpaceError();
  203. // this function sets the error hit by restore in the registry
  204. BOOL SetRestoreError(DWORD dwRestoreError);
  205. // this function sets the status whether restore was done in safe mode
  206. BOOL SetRestoreSafeModeStatus(DWORD dwSafeModeStatus);
  207. // this function checks to see if the last restore was done in safe mode
  208. BOOL WasLastRestoreInSafeMode();
  209. LPCWSTR GetSysErrStr();
  210. LPCWSTR GetSysErrStr( DWORD dwErr );
  211. DWORD SRCopyFile( LPCWSTR cszSrc, LPCWSTR cszDst );
  212. DWORD SRCreateSubdirectory ( LPCWSTR cszDst, LPSECURITY_ATTRIBUTES pSecAttr);
  213. // this function returns whether the SR service is running
  214. BOOL IsSRServiceRunning();
  215. LPWSTR SRGetRegMultiSz( HKEY hkRoot, LPCWSTR cszSubKey, LPCWSTR cszValue, LPDWORD pdwData );
  216. BOOL SRSetRegMultiSz( HKEY hkRoot, LPCWSTR cszSubKey, LPCWSTR cszValue, LPCWSTR cszData, DWORD cbData );
  217. // this returns the name after the volume name
  218. // For example input: c:\file output: file
  219. // input \\?\Volume{GUID}\file1 output: file1
  220. WCHAR * ReturnPastVolumeName(const WCHAR * pszFileName);
  221. //This API sets the ShortFileName for a given file
  222. DWORD SetShortFileName(const WCHAR * pszFile, // complete file path
  223. const WCHAR * pszShortName); // desired short file name
  224. void SRLogEvent (HANDLE hEventSource,
  225. WORD wType,
  226. DWORD dwID,
  227. void * pRawData,
  228. DWORD dwDataSize,
  229. const WCHAR * pszS1,
  230. const WCHAR * pszS2,
  231. const WCHAR * pszS3);
  232. BOOL IsAdminOrSystem();
  233. BOOL IsPowerUsers();
  234. void ChangeCCS(HKEY hkMount, LPWSTR pszString);
  235. void RemoveTrailingFilename(WCHAR * pszString, WCHAR wchSlash);
  236. class CSRClientLoader
  237. {
  238. public:
  239. CSRClientLoader();
  240. ~CSRClientLoader();
  241. BOOL LoadSrClient();
  242. HMODULE m_hSRClient;
  243. private:
  244. HMODULE m_hFrameDyn;
  245. BOOL LoadFrameDyn();
  246. };
  247. #endif