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.

266 lines
5.6 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: util.h
  8. //
  9. //--------------------------------------------------------------------------
  10. // Util.h : Definition of ds routines and classes
  11. //+-------------------------------------------------------------------------
  12. #ifndef __UTIL_H__
  13. #define __UTIL_H__
  14. #include <htmlhelp.h>
  15. //
  16. // Forward declarations
  17. //
  18. class CDSCookie;
  19. //
  20. // Common macros
  21. //
  22. #define ARRAYLEN(x) (sizeof(x) / sizeof((x)[0]))
  23. #define LENGTH(x) (sizeof(x)/sizeof(x[0]))
  24. /////////////////////////////////////////////////////////////////////////////////////
  25. // Variant helpers
  26. //
  27. HRESULT BinaryToVariant(DWORD Length, BYTE* pByte, VARIANT* lpVarDestObject);
  28. HRESULT HrVariantToStringList(const CComVariant& refvar, CStringList& refstringlist);
  29. HRESULT HrStringListToVariant(CComVariant& refvar, const CStringList& refstringlist);
  30. ///////////////////////////////////////////////////////////////////////////////////////
  31. BOOL LoadStringToTchar(int ids, PTSTR * pptstr);
  32. ///////////////////////////////////////////////////////////////////////////////////////
  33. // streaming helper functions
  34. //
  35. HRESULT SaveStringHelper(LPCWSTR pwsz, IStream* pStm);
  36. HRESULT LoadStringHelper(CString& sz, IStream* pStm);
  37. HRESULT SaveDWordHelper(IStream* pStm, DWORD dw);
  38. HRESULT LoadDWordHelper(IStream* pStm, DWORD* pdw);
  39. /////////////////////////////////////////////////////////////////////
  40. // CCommandLineOptions
  41. class CCommandLineOptions
  42. {
  43. public:
  44. CCommandLineOptions()
  45. {
  46. m_bInit = FALSE;
  47. #ifdef DBG
  48. m_bNoName = FALSE;
  49. #endif
  50. }
  51. void Initialize();
  52. LPCWSTR GetServerOverride()
  53. { return m_szOverrideServerName.IsEmpty() ? NULL : (LPCWSTR)m_szOverrideServerName;}
  54. LPCWSTR GetDomainOverride()
  55. { return m_szOverrideDomainName.IsEmpty() ? NULL : (LPCWSTR)m_szOverrideDomainName;}
  56. LPCWSTR GetRDNOverride()
  57. { return m_szOverrideRDN.IsEmpty() ? NULL : (LPCWSTR)m_szOverrideRDN;}
  58. LPCWSTR GetSavedQueriesXMLFile()
  59. { return m_szSavedQueriesXMLFile.IsEmpty() ? NULL : (LPCWSTR)m_szSavedQueriesXMLFile;}
  60. #ifdef DBG
  61. BOOL IsNoNameCommandLine() { return m_bNoName;}
  62. #endif
  63. private:
  64. BOOL m_bInit;
  65. CString m_szOverrideDomainName;
  66. CString m_szOverrideServerName;
  67. CString m_szOverrideRDN;
  68. CString m_szSavedQueriesXMLFile; // experiment for loading queries
  69. #ifdef DBG
  70. BOOL m_bNoName;
  71. #endif
  72. };
  73. /////////////////////////////////////////////////////////////////////
  74. // CSidHolder
  75. class CSidHolder
  76. {
  77. public:
  78. CSidHolder()
  79. {
  80. _Init();
  81. }
  82. ~CSidHolder()
  83. {
  84. _Free();
  85. }
  86. PSID Get()
  87. {
  88. return m_pSID;
  89. }
  90. BOOL Copy(PSID p)
  91. {
  92. _Free();
  93. return _Copy(p);
  94. }
  95. void Attach(PSID p, BOOL bLocalAlloc)
  96. {
  97. _Free();
  98. m_pSID = p;
  99. m_bLocalAlloc = bLocalAlloc;
  100. }
  101. void Clear()
  102. {
  103. _Free();
  104. }
  105. private:
  106. void _Init()
  107. {
  108. m_pSID = NULL;
  109. m_bLocalAlloc = TRUE;
  110. }
  111. void _Free()
  112. {
  113. if (m_pSID != NULL)
  114. {
  115. if (m_bLocalAlloc)
  116. ::LocalFree(m_pSID);
  117. else
  118. ::FreeSid(m_pSID);
  119. _Init();
  120. }
  121. }
  122. BOOL _Copy(PSID p)
  123. {
  124. if ( (p == NULL) || !::IsValidSid(p) )
  125. return FALSE;
  126. DWORD dwLen = ::GetLengthSid(p);
  127. PSID pNew = ::LocalAlloc(LPTR, dwLen);
  128. if (pNew == NULL)
  129. {
  130. return FALSE;
  131. }
  132. if (!::CopySid(dwLen, pNew, p))
  133. {
  134. ::LocalFree(pNew);
  135. return FALSE;
  136. }
  137. m_bLocalAlloc = TRUE;
  138. m_pSID = pNew;
  139. ASSERT(dwLen == ::GetLengthSid(m_pSID));
  140. ASSERT(memcmp(p, m_pSID, dwLen) == 0);
  141. return TRUE;
  142. }
  143. PSID m_pSID;
  144. BOOL m_bLocalAlloc;
  145. };
  146. //////////////////////////////////////////////////////////////////////////////
  147. // Security helpers
  148. class CSimpleSecurityDescriptorHolder
  149. {
  150. public:
  151. CSimpleSecurityDescriptorHolder()
  152. {
  153. m_pSD = NULL;
  154. }
  155. ~CSimpleSecurityDescriptorHolder()
  156. {
  157. if (m_pSD != NULL)
  158. {
  159. ::LocalFree(m_pSD);
  160. m_pSD = NULL;
  161. }
  162. }
  163. HRESULT InitializeFromSDDL(PCWSTR server, PWSTR pszSDDL);
  164. PSECURITY_DESCRIPTOR m_pSD;
  165. private:
  166. CSimpleSecurityDescriptorHolder(const CSimpleSecurityDescriptorHolder&)
  167. {}
  168. CSimpleSecurityDescriptorHolder& operator=(const CSimpleSecurityDescriptorHolder&) {}
  169. };
  170. /////////////////////////////////////////////////////////////////////
  171. // CSimpleAclHolder
  172. class CSimpleAclHolder
  173. {
  174. public:
  175. CSimpleAclHolder()
  176. {
  177. m_pAcl = NULL;
  178. }
  179. ~CSimpleAclHolder()
  180. {
  181. Clear();
  182. }
  183. void Clear()
  184. {
  185. if (m_pAcl != NULL)
  186. {
  187. ::LocalFree(m_pAcl);
  188. m_pAcl = NULL;
  189. }
  190. }
  191. PACL m_pAcl;
  192. };
  193. //////////////////////////////////////////////////////////////////////////////////
  194. // String Utilities
  195. //
  196. #if 0
  197. struct DATA_BLOB
  198. {
  199. DWORD cbData;
  200. BYTE *pbData;
  201. };
  202. #endif
  203. typedef DATA_BLOB TBLOB; // Binary large object
  204. void wtoli(LPCWSTR lpsz, LARGE_INTEGER& liOut);
  205. void litow(LARGE_INTEGER& li, CString& szResult);
  206. void GetCurrentTimeStampMinusInterval(DWORD dwDays,
  207. LARGE_INTEGER* pLI);
  208. // This wrapper function required to make prefast shut up when we are
  209. // initializing a critical section in a constructor.
  210. void ExceptionPropagatingInitializeCriticalSection(LPCRITICAL_SECTION critsec);
  211. // Wrapper for GetModuleFileName that dynamically grows the buffer (up to USHRT_MAX)
  212. // until the file name fits without truncation
  213. HRESULT
  214. MyGetModuleFileName(
  215. HINSTANCE hInstance,
  216. CString& moduleName);
  217. #endif __UTIL_H__