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.

381 lines
13 KiB

  1. // File: secmgr.h
  2. //
  3. // Contents: The object that implements the base IInternetSecurityManager interface
  4. //
  5. // Classes: CSecurityManager
  6. //
  7. // Functions:
  8. //
  9. // History:
  10. //
  11. //----------------------------------------------------------------------------
  12. #ifndef _SECMGR_H_
  13. #define _SECMGR_H_
  14. #pragma warning(disable:4200)
  15. #define MAX_SEC_MGR_CACHE 4
  16. #define URLZONE_INVALID URLZONE_USER_MAX+1
  17. #define MUTZ_NOCACHE 0x80000000 // start private flags from high end
  18. #define TSZMICROSOFTPATH TEXT("Software\\Microsoft")
  19. #define TSZIEPATH TSZMICROSOFTPATH TEXT("\\Internet Explorer")
  20. #define REGSTR_PATH_IE_MAIN TSZIEPATH TEXT("\\Main")
  21. #define REGVAL_TRUSTDLG_ENABLED TEXT("DisplayTrustAlertDlg")
  22. // private function only exported by ordinal:
  23. #define SHOWURLINNEWBROWSERINSTANCE_ORDINAL 230
  24. typedef HRESULT (STDAPICALLTYPE * SHOWURLINNEWBROWSERINSTANCE)(LPCWSTR);
  25. struct ZONEMAP_COMPONENTS;
  26. struct RANGE_ITEM
  27. {
  28. BYTE bLow[4]; // high byte values for range
  29. BYTE bHigh[4]; // low byte values for range
  30. TCHAR szName[1]; // actually variable length
  31. };
  32. // This structure is used to exchange data between the security manager
  33. // and the dialog proc's.
  34. struct DlgData
  35. {
  36. DWORD dwAction;
  37. DWORD dwZone;
  38. LPCWSTR pstr;
  39. DWORD dwFlags;
  40. // TrustAlertDialogProc use:
  41. IInternetSecurityManager *pSecurityManager;
  42. BOOL bFromShdocvw;
  43. };
  44. typedef DlgData * LPDLGDATA;
  45. class CSecurityManager : public IInternetSecurityManager
  46. {
  47. public:
  48. // IUnknown methods
  49. STDMETHODIMP QueryInterface(REFIID iid, void **ppvObj);
  50. STDMETHODIMP_(ULONG) AddRef(void);
  51. STDMETHODIMP_(ULONG) Release(void);
  52. STDMETHODIMP MapUrlToZone(
  53. /* [in] */ LPCWSTR pwszUrl,
  54. /* [out] */ DWORD *pdwZone,
  55. /* [in] */ DWORD dwReserved);
  56. STDMETHODIMP GetSecurityId(
  57. /* [in] */ LPCWSTR pwszUrl,
  58. /* [size_is][out] */ BYTE* pbSecurityId,
  59. /* [out][in] */ DWORD *pcbSecurityId,
  60. /* [in] */ DWORD_PTR dwReserved);
  61. STDMETHODIMP ProcessUrlAction(
  62. /* [in] */ LPCWSTR pwszUrl,
  63. /* [in] */ DWORD dwAction,
  64. /* [size_is][out] */ BYTE *pPolicy,
  65. /* [in] */ DWORD cbPolicy,
  66. /* [in] */ BYTE *pContext,
  67. /* [in] */ DWORD cbContext,
  68. /* [in] */ DWORD dwFlags,
  69. /* [in] */ DWORD dwReserved);
  70. STDMETHODIMP QueryCustomPolicy(
  71. /* [in] */ LPCWSTR pwszUrl,
  72. /* [in] */ REFGUID guidKey,
  73. /* [size_is][size_is][out] */ BYTE **ppPolicy,
  74. /* [out] */ DWORD *pcbPolicy,
  75. /* [in] */ BYTE *pContext,
  76. /* [in] */ DWORD cbContext,
  77. /* [in] */ DWORD dwReserved
  78. );
  79. STDMETHODIMP SetSecuritySite(
  80. /* [in] */ IInternetSecurityMgrSite *pSite
  81. );
  82. STDMETHODIMP GetSecuritySite(
  83. /* [out] */ IInternetSecurityMgrSite **ppSite
  84. );
  85. STDMETHODIMP SetZoneMapping(
  86. /* [in] */ DWORD dwZone,
  87. /* [in] */ LPCWSTR lpszPattern,
  88. /* [in] */ DWORD dwFlags);
  89. STDMETHODIMP GetZoneMappings(
  90. /* [in] */ DWORD dwZone,
  91. /* [out] */ IEnumString **ppEnumString,
  92. /* [in] */ DWORD dwFlags);
  93. // Constructors/destructors
  94. public:
  95. CSecurityManager(IUnknown *pUnkOuter, IUnknown **ppUnkInner);
  96. virtual ~CSecurityManager();
  97. static BOOL GlobalInit( ) ;
  98. static BOOL GlobalCleanup( );
  99. // Aggregation and RefCount support.
  100. protected:
  101. CRefCount m_ref;
  102. class CPrivUnknown : public IUnknown
  103. {
  104. public:
  105. STDMETHOD(QueryInterface) ( REFIID riid, LPVOID FAR* ppvObj);
  106. STDMETHOD_(ULONG,AddRef) (void);
  107. STDMETHOD_(ULONG,Release) (void);
  108. ~CPrivUnknown() {}
  109. CPrivUnknown() : m_ref () {}
  110. private:
  111. CRefCount m_ref; // the total refcount of this object
  112. };
  113. friend class CPrivUnknown;
  114. CPrivUnknown m_Unknown;
  115. IUnknown* m_pUnkOuter;
  116. STDMETHODIMP_(ULONG) PrivAddRef()
  117. {
  118. return m_Unknown.AddRef();
  119. }
  120. STDMETHODIMP_(ULONG) PrivRelease()
  121. {
  122. return m_Unknown.Release();
  123. }
  124. protected:
  125. BOOL EnsureZoneManager();
  126. VOID PickZCString(ZONEMAP_COMPONENTS *pzc, LPCWSTR *ppwsz, DWORD *pcch, LPCWSTR pwszDocDomain);
  127. // Helper methods to deal with IP Rules
  128. HRESULT ReadAllIPRules( );
  129. HRESULT AddDeleteIPRule(ZONEMAP_COMPONENTS *pzc, DWORD dwZone, DWORD dwFlags);
  130. // helper methods to do GetZoneMappings.
  131. HRESULT AddUrlsToEnum(CRegKey *pRegKey, DWORD dwZone, LPCTSTR lpsz, int cch, BOOL bAddWildCard, CEnumString *);
  132. HRESULT AddIPRulesToEnum(DWORD dwZone, CEnumString *);
  133. static HRESULT ComposeUrlSansProtocol(LPCTSTR pszDomain, int cchDomain, LPCTSTR pszSite, int cchSite,
  134. LPTSTR * ppszRet, int * cchRet);
  135. static HRESULT ComposeUrl(LPCTSTR pszUrlSansProt, int cchUrlSansProt, LPCTSTR pszProt, int cchProt, BOOL bAddWildCard,
  136. LPTSTR * ppszRet, int * cchRet);
  137. protected: // UI related definitions.
  138. enum { MAX_ALERT_SIZE = 256 };
  139. // Return values from DialogProc's
  140. enum { ZALERT_NO = 0 /* should be 0*/ , ZALERT_YES, ZALERT_YESPERSIST };
  141. // helper methods to display generic UI.
  142. static DWORD GetAlertIdForAction(DWORD dwAction);
  143. static DWORD GetWarnIdForAction(DWORD dwAction);
  144. // Dialog proc's etc.
  145. static BOOL IsScriptOrActiveXHardenedInternet(DWORD dwAction, DWORD dwPermissions, DWORD dwZone);
  146. static BOOL IsSiteInZone(LPCWSTR pszUrl, DWORD dwZone, IInternetSecurityManager *pSecMgr);
  147. static BOOL ShowAddToSitesList(HWND hwnd, LPCWSTR pszUrl, DWORD dwZone);
  148. static HWND SetControlText(HWND hDlg, UINT ctrlID, DWORD dwStrId, LPCWSTR pszDomain = NULL);
  149. static BOOL IsHostedInIE(IUnknown* punk);
  150. static BOOL IsBrowserHosted();
  151. static void ShowUrlInNewBrowserInstance(LPCWSTR pwszUrl);
  152. public:
  153. static INT_PTR TrustAlertDialogProc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam);
  154. protected:
  155. static INT_PTR ZonesAlertDialogProc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam);
  156. static INT_PTR ZonesWarnDialogProc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam);
  157. static inline BOOL IsFormsSubmitAction(DWORD dwAction);
  158. static INT_PTR FormsAlertDialogProc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam);
  159. INT CSecurityManager::ShowFormsAlertDialog(HWND hwndParent, LPDLGDATA lpDlgData);
  160. protected:
  161. // Methods to help Map a URL to a zone.
  162. HRESULT MapUrlToZone
  163. (ZONEMAP_COMPONENTS* pzc, DWORD* pdwZone, DWORD dwFlags, BOOL *pfMarked = NULL, LPWSTR *ppszURLMark = NULL);
  164. HRESULT WrapMapUrlToZone(LPCWSTR pwszSecUrl, ZONEMAP_COMPONENTS* pzc, DWORD* pdwZone, DWORD dwFlags,
  165. BOOL *pfMarked, LPWSTR *ppszMarkURL);
  166. HRESULT CheckAddressAgainstRanges
  167. (ZONEMAP_COMPONENTS* pzc, DWORD* pdwZone, LPCTSTR pszProt);
  168. HRESULT CheckSiteAndDomainMappings
  169. (ZONEMAP_COMPONENTS* pzc, DWORD* pdwZone, LPCTSTR pszProt, BOOL fCheckHardenKey = FALSE);
  170. HRESULT CheckUNCAsIntranet
  171. (ZONEMAP_COMPONENTS* pzc, DWORD* pdwZone, LPCTSTR pszProt);
  172. HRESULT CheckIntranetName
  173. (ZONEMAP_COMPONENTS* pzc, DWORD* pdwZone, LPCTSTR pszProt);
  174. HRESULT CheckProxyBypassRule
  175. (ZONEMAP_COMPONENTS* pzc, DWORD* pdwZone, LPCTSTR pszProt);
  176. HRESULT CheckMKURL
  177. (ZONEMAP_COMPONENTS* pzc, DWORD* pdwZone, LPCTSTR pszProt);
  178. protected:
  179. // Class to remember persistent actions.
  180. class CPersistAnswers
  181. {
  182. public:
  183. CPersistAnswers( ) : m_pAnswerEntry(NULL) { };
  184. ~CPersistAnswers( );
  185. public:
  186. BOOL GetPrevAnswer(LPCWSTR pszUrl, DWORD dwAction, INT* piAnswer);
  187. VOID RememberAnswer(LPCWSTR pszUrl, DWORD dwAction, INT iAnswer);
  188. static inline BOOL IsPersistentAnswerAction(DWORD dwAction);
  189. private:
  190. struct CAnswerEntry
  191. {
  192. // Construction
  193. CAnswerEntry(LPCWSTR pszUrl, DWORD dwAction, INT iAnswer);
  194. ~CAnswerEntry( );
  195. // Methods.
  196. BOOL MatchEntry (LPCWSTR pszUrl, DWORD dwAction);
  197. INT GetAnswer( ) const { return m_iAnswer; }
  198. LPCWSTR GetUrl( ) const { return m_pszUrl; }
  199. CAnswerEntry * GetNext( ) const { return m_pNext; }
  200. VOID SetNext(CAnswerEntry * pNext) { m_pNext = pNext; }
  201. private:
  202. CAnswerEntry * m_pNext;
  203. LPWSTR m_pszUrl;
  204. DWORD m_dwAction;
  205. INT m_iAnswer;
  206. };
  207. CAnswerEntry* m_pAnswerEntry;
  208. };
  209. CPersistAnswers m_persistAnswers;
  210. // Methods/members to support caching so we optimize MapUrlToZone, etc.
  211. protected:
  212. class CSecMgrCache {
  213. public:
  214. CSecMgrCache(void);
  215. ~CSecMgrCache(void);
  216. BOOL Lookup(LPCWSTR pwszURL,
  217. DWORD *pdwZone = NULL,
  218. BOOL *pfMarked = NULL,
  219. BYTE *pbSecurityID = NULL,
  220. DWORD *pcbSecurityID = NULL,
  221. LPCWSTR pwszDocDomain = NULL);
  222. void Add(LPCWSTR pwszURL,
  223. DWORD dwZone,
  224. BOOL fMarked,
  225. const BYTE *pbSecurityID = NULL,
  226. DWORD cbSecurityID = NULL,
  227. LPCWSTR pwszDocDomain = NULL );
  228. void Flush(void);
  229. static VOID IncrementGlobalCounter( );
  230. protected:
  231. // Counters to flag cross-process cache invalidation.
  232. DWORD m_dwPrevCounter ; // Global counter so we can correctly invalidate the cache if
  233. // user changes options.
  234. static HANDLE s_hMutexCounter; // mutex controlling access to shared memory counter
  235. BOOL IsCounterEqual() const;
  236. VOID SetToCurrentCounter();
  237. // The body of the cache is this array of cache entries.
  238. // Cross-thread access control for the array is by critical section.
  239. CRITICAL_SECTION m_csectZoneCache; // assumes only one, static instance of the cache
  240. struct CSecMgrCacheEntry {
  241. CSecMgrCacheEntry(void) :
  242. m_pwszURL(NULL),
  243. m_pbSecurityID(NULL),
  244. m_cbSecurityID(NULL),
  245. m_dwZone(URLZONE_INVALID),
  246. m_fMarked(FALSE),
  247. m_pwszDocDomain(NULL) {};
  248. ~CSecMgrCacheEntry(void) { Flush(); };
  249. void Set(LPCWSTR pwszURL, DWORD dwZone, BOOL fMarked,
  250. const BYTE *pbSecurityID, DWORD cbSecurityID, LPCWSTR pwszDocDomain);
  251. void Flush(void);
  252. LPWSTR m_pwszURL;
  253. BYTE* m_pbSecurityID;
  254. DWORD m_cbSecurityID;
  255. DWORD m_dwZone;
  256. BOOL m_fMarked;
  257. LPWSTR m_pwszDocDomain;
  258. }; // CSecMgrCacheEntry
  259. CSecMgrCacheEntry m_asmce[MAX_SEC_MGR_CACHE];
  260. int m_iAdd; // index in m_asmce to add the next element
  261. BOOL FindCacheEntry( LPCWSTR pwszURL, int& riEntry ); // must be called under critical section.
  262. }; // CSecMgrCache
  263. static CSecMgrCache s_smcache;
  264. protected:
  265. // Methods to manage List of Allowed ActiveX controls
  266. static BOOL EnsureListReady(BOOL bForce = FALSE);
  267. static void IntializeAllowedControls();
  268. static HRESULT GetControlPermissions(BYTE * raw_CLSID, DWORD & dwPerm);
  269. // Get the final decision on the whether to run a CLSID (for this zone, etc)
  270. static HRESULT GetActiveXRunPermissions(BYTE * raw_CLSID, DWORD & dwPerm);
  271. public:
  272. static VOID IncrementGlobalCounter( );
  273. private:
  274. IInternetSecurityMgrSite* m_pSite;
  275. IInternetZoneManager* m_pZoneManager;
  276. IInternetSecurityManager* m_pDelegateSecMgr;
  277. CRegKey m_regZoneMap;
  278. BOOL m_fHardened;
  279. // Static members to do remember the correct IP Ranges.
  280. static BOOL s_bIPInit; // have we read the IP ranges.
  281. static BYTE* s_pRanges; // array of range items
  282. static DWORD s_cNumRanges; // number of range items
  283. static DWORD s_cbRangeItem; // size of each range item
  284. static DWORD s_dwNextRangeIndex; // Next index to use to add numbers in the range entry.
  285. static CRITICAL_SECTION s_csectIP; // crit section to protect it all.
  286. static BOOL s_bcsectInit;
  287. static CLSID * s_clsidAllowedList;
  288. static CRITICAL_SECTION s_csectAList;
  289. static DWORD s_dwNumAllowedControls;
  290. };
  291. #pragma warning(default:4200)
  292. #endif // _SECMGR_H_