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.

318 lines
9.1 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1998-1999 Microsoft Corporation
  4. //
  5. // security.h
  6. //
  7. // alanbos 25-Sep-98 Created.
  8. //
  9. // Defines the CSWbemSecurity and subsiduary objects
  10. //
  11. //***************************************************************************
  12. #ifndef _SECURITY_H_
  13. #define _SECURITY_H_
  14. //***************************************************************************
  15. //
  16. // CLASS NAME:
  17. //
  18. // CWbemLocatorSecurity
  19. //
  20. // DESCRIPTION:
  21. //
  22. // Implements the ISWbemSecurity interface for SWbemLocator objects.
  23. //
  24. //***************************************************************************
  25. class CWbemLocatorSecurity : public ISWbemSecurity,
  26. public ISupportErrorInfo,
  27. public IProvideClassInfo
  28. {
  29. private:
  30. CSWbemPrivilegeSet *m_pPrivilegeSet;
  31. CDispatchHelp m_Dispatch;
  32. bool m_impLevelSet;
  33. WbemImpersonationLevelEnum m_impLevel;
  34. bool m_authnLevelSet;
  35. WbemAuthenticationLevelEnum m_authnLevel;
  36. protected:
  37. long m_cRef; //Object reference count
  38. public:
  39. CWbemLocatorSecurity (CSWbemPrivilegeSet *pPrivilegeSet);
  40. CWbemLocatorSecurity (CWbemLocatorSecurity *pCWbemLocatorSecurity);
  41. virtual ~CWbemLocatorSecurity (void);
  42. //Non-delegating object IUnknown
  43. STDMETHODIMP QueryInterface(REFIID, LPVOID*);
  44. STDMETHODIMP_(ULONG) AddRef(void);
  45. STDMETHODIMP_(ULONG) Release(void);
  46. // IDispatch
  47. STDMETHODIMP GetTypeInfoCount(UINT* pctinfo)
  48. {return m_Dispatch.GetTypeInfoCount(pctinfo);}
  49. STDMETHODIMP GetTypeInfo(UINT itinfo, LCID lcid, ITypeInfo** pptinfo)
  50. {return m_Dispatch.GetTypeInfo(itinfo, lcid, pptinfo);}
  51. STDMETHODIMP GetIDsOfNames(REFIID riid, OLECHAR** rgszNames,
  52. UINT cNames, LCID lcid, DISPID* rgdispid)
  53. {return m_Dispatch.GetIDsOfNames(riid, rgszNames, cNames,
  54. lcid,
  55. rgdispid);}
  56. STDMETHODIMP Invoke(DISPID dispidMember, REFIID riid, LCID lcid,
  57. WORD wFlags, DISPPARAMS* pdispparams, VARIANT* pvarResult,
  58. EXCEPINFO* pexcepinfo, UINT* puArgErr)
  59. {return m_Dispatch.Invoke(dispidMember, riid, lcid, wFlags,
  60. pdispparams, pvarResult, pexcepinfo, puArgErr);}
  61. // ISWbemSecurity methods
  62. HRESULT STDMETHODCALLTYPE get_ImpersonationLevel
  63. (
  64. /* [out] */ WbemImpersonationLevelEnum *iImpersonationLevel
  65. );
  66. HRESULT STDMETHODCALLTYPE put_ImpersonationLevel
  67. (
  68. /* [in] */ WbemImpersonationLevelEnum iImpersonationLevel
  69. );
  70. HRESULT STDMETHODCALLTYPE get_AuthenticationLevel
  71. (
  72. /* [out] */ WbemAuthenticationLevelEnum *iAuthenticationLevel
  73. );
  74. HRESULT STDMETHODCALLTYPE put_AuthenticationLevel
  75. (
  76. /* [in] */ WbemAuthenticationLevelEnum iAuthenticationLevel
  77. );
  78. HRESULT STDMETHODCALLTYPE get_Privileges
  79. (
  80. /* [out] */ ISWbemPrivilegeSet **objWbemPrivileges
  81. );
  82. // ISupportErrorInfo methods
  83. HRESULT STDMETHODCALLTYPE InterfaceSupportsErrorInfo
  84. (
  85. /* [in] */ REFIID riid
  86. );
  87. // IProvideClassInfo methods
  88. HRESULT STDMETHODCALLTYPE GetClassInfo
  89. (
  90. /* [in] */ ITypeInfo **ppTI
  91. )
  92. {
  93. return m_Dispatch.GetClassInfo (ppTI);
  94. }
  95. // CWbemLocatorSecurity methods
  96. bool IsImpersonationSet () { return m_impLevelSet; }
  97. bool IsAuthenticationSet () { return m_authnLevelSet; }
  98. BOOL SetSecurity (BSTR bsUser, bool &needToResetSecurity, HANDLE &hThreadToken);
  99. void ResetSecurity (HANDLE hThreadToken);
  100. CSWbemPrivilegeSet *GetPrivilegeSet ()
  101. {
  102. CSWbemPrivilegeSet *pPrivSet = m_pPrivilegeSet;
  103. if (pPrivSet)
  104. pPrivSet->AddRef ();
  105. return pPrivSet;
  106. }
  107. };
  108. //***************************************************************************
  109. //
  110. // CLASS NAME:
  111. //
  112. // CSWbemSecurity
  113. //
  114. // DESCRIPTION:
  115. //
  116. // Implements the ISWbemSecurity interface for non-SWbemLocator objects.
  117. //
  118. //***************************************************************************
  119. class CSWbemSecurity : public ISWbemSecurity,
  120. public ISupportErrorInfo,
  121. public ISWbemInternalSecurity,
  122. public IProvideClassInfo
  123. {
  124. private:
  125. CSWbemPrivilegeSet *m_pPrivilegeSet;
  126. CDispatchHelp m_Dispatch;
  127. CSWbemProxyCache *m_pProxyCache;
  128. IUnknown *m_pCurProxy;
  129. CSWbemProxyCache *GetProxyCache ()
  130. {
  131. CSWbemProxyCache *pCache = m_pProxyCache;
  132. if (pCache)
  133. pCache->AddRef ();
  134. return pCache;
  135. }
  136. // Sundry statics
  137. static bool s_bInitialized;
  138. static bool s_bIsNT;
  139. static DWORD s_dwNTMajorVersion;
  140. static HINSTANCE s_hAdvapi;
  141. static bool s_bCanRevert;
  142. static WbemImpersonationLevelEnum s_dwDefaultImpersonationLevel;
  143. static SECURITY_IMPERSONATION_LEVEL MapImpersonationLevel(WbemImpersonationLevelEnum ImpersonationLevel);
  144. protected:
  145. long m_cRef; //Object reference count
  146. public:
  147. CSWbemSecurity (IUnknown *pUnk,
  148. BSTR bsAuthority = NULL,
  149. BSTR bsUser = NULL, BSTR bsPassword = NULL,
  150. CWbemLocatorSecurity *pSecurity = NULL);
  151. CSWbemSecurity (IUnknown *pUnk,
  152. COAUTHIDENTITY *pCoAuthIdentity,
  153. BSTR bsPrincipal,
  154. BSTR bsAuthority);
  155. CSWbemSecurity (CSWbemSecurity *pSecurity);
  156. CSWbemSecurity (IUnknown *pUnk,
  157. CSWbemSecurity *pSecurity);
  158. CSWbemSecurity (IUnknown *pUnk,
  159. ISWbemInternalSecurity *pSecurity);
  160. virtual ~CSWbemSecurity (void);
  161. //Non-delegating object IUnknown
  162. STDMETHODIMP QueryInterface(REFIID, LPVOID*);
  163. STDMETHODIMP_(ULONG) AddRef(void);
  164. STDMETHODIMP_(ULONG) Release(void);
  165. // IDispatch
  166. STDMETHODIMP GetTypeInfoCount(UINT* pctinfo)
  167. {return m_Dispatch.GetTypeInfoCount(pctinfo);}
  168. STDMETHODIMP GetTypeInfo(UINT itinfo, LCID lcid, ITypeInfo** pptinfo)
  169. {return m_Dispatch.GetTypeInfo(itinfo, lcid, pptinfo);}
  170. STDMETHODIMP GetIDsOfNames(REFIID riid, OLECHAR** rgszNames,
  171. UINT cNames, LCID lcid, DISPID* rgdispid)
  172. {return m_Dispatch.GetIDsOfNames(riid, rgszNames, cNames,
  173. lcid,
  174. rgdispid);}
  175. STDMETHODIMP Invoke(DISPID dispidMember, REFIID riid, LCID lcid,
  176. WORD wFlags, DISPPARAMS* pdispparams, VARIANT* pvarResult,
  177. EXCEPINFO* pexcepinfo, UINT* puArgErr)
  178. {return m_Dispatch.Invoke(dispidMember, riid, lcid, wFlags,
  179. pdispparams, pvarResult, pexcepinfo, puArgErr);}
  180. // ISWbemSecurity methods
  181. HRESULT STDMETHODCALLTYPE get_ImpersonationLevel
  182. (
  183. /* [out] */ WbemImpersonationLevelEnum *iImpersonationLevel
  184. );
  185. HRESULT STDMETHODCALLTYPE put_ImpersonationLevel
  186. (
  187. /* [in] */ WbemImpersonationLevelEnum iImpersonationLevel
  188. );
  189. HRESULT STDMETHODCALLTYPE get_AuthenticationLevel
  190. (
  191. /* [out] */ WbemAuthenticationLevelEnum *iAuthenticationLevel
  192. );
  193. HRESULT STDMETHODCALLTYPE put_AuthenticationLevel
  194. (
  195. /* [in] */ WbemAuthenticationLevelEnum iAuthenticationLevel
  196. );
  197. HRESULT STDMETHODCALLTYPE get_Privileges
  198. (
  199. /* [out] */ ISWbemPrivilegeSet **objWbemPrivileges
  200. );
  201. // ISupportErrorInfo methods
  202. HRESULT STDMETHODCALLTYPE InterfaceSupportsErrorInfo
  203. (
  204. /* [in] */ REFIID riid
  205. );
  206. // IProvideClassInfo methods
  207. HRESULT STDMETHODCALLTYPE GetClassInfo
  208. (
  209. /* [in] */ ITypeInfo **ppTI
  210. )
  211. {
  212. return m_Dispatch.GetClassInfo (ppTI);
  213. }
  214. // ISWbemInternalSecurity methods
  215. HRESULT STDMETHODCALLTYPE GetAuthority (BSTR *bsAuthority);
  216. HRESULT STDMETHODCALLTYPE GetUPD (BSTR *bsUser, BSTR *bsPassword, BSTR *bsDomain);
  217. HRESULT STDMETHODCALLTYPE GetPrincipal (BSTR *bsPrincipal);
  218. // CSWbemSecurity methods
  219. COAUTHIDENTITY *GetCoAuthIdentity () { return (m_pProxyCache ? m_pProxyCache->GetCoAuthIdentity () : NULL); }
  220. BSTR GetPrincipal () { return (m_pProxyCache ? m_pProxyCache->GetPrincipal (): NULL); }
  221. BSTR GetAuthority () { return (m_pProxyCache ? m_pProxyCache->GetAuthority (): NULL); }
  222. bool IsUsingExplicitUserName () { return (m_pProxyCache ? m_pProxyCache->IsUsingExplicitUserName (): false); }
  223. IUnknown *GetProxy ()
  224. {
  225. IUnknown *pProxy = m_pCurProxy;
  226. if (pProxy)
  227. pProxy->AddRef ();
  228. return pProxy;
  229. }
  230. CSWbemPrivilegeSet *GetPrivilegeSet ()
  231. {
  232. CSWbemPrivilegeSet *pPrivSet = m_pPrivilegeSet;
  233. if (pPrivSet)
  234. pPrivSet->AddRef ();
  235. return pPrivSet;
  236. }
  237. void SecureInterface (IUnknown *pUnk);
  238. void SecureInterfaceRev (IUnknown *pUnk);
  239. // Sundry Privilege-related functions
  240. static BOOL AdjustTokenPrivileges (HANDLE hHandle, CSWbemPrivilegeSet *pPrivilegeSet);
  241. static BOOL LookupPrivilegeValue (LPCTSTR lpName, PLUID pLuid);
  242. static void LookupPrivilegeDisplayName (LPCTSTR lpName, BSTR *pDisplayName);
  243. BOOL SetSecurity (bool &needToResetSecurity, HANDLE &hThreadToken);
  244. void ResetSecurity (HANDLE hThreadToken);
  245. // Used to define resources
  246. static void Initialize ();
  247. static void Uninitialize ();
  248. // Sundry OS versioning helper routines
  249. static bool IsNT () { return s_bIsNT; }
  250. static DWORD GetNTMajorVersion () { return s_dwNTMajorVersion; }
  251. static bool CanRevertToSelf () { return s_bCanRevert; }
  252. static WbemImpersonationLevelEnum GetDefaultImpersonationLevel ()
  253. { return s_dwDefaultImpersonationLevel; }
  254. static bool IsImpersonating (bool useDefaultUser, bool useDefaultAuthority);
  255. #ifdef WSCRPDEBUG
  256. static void PrintPrivileges (HANDLE hToken);
  257. #endif
  258. };
  259. #endif