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.

315 lines
9.2 KiB

  1. // File: regzone.h
  2. //
  3. // Contents: Registry management for a single zone.
  4. //
  5. // Classes: CRegZone
  6. //
  7. // Functions:
  8. //
  9. // History:
  10. //
  11. //----------------------------------------------------------------------------
  12. #ifndef _REGZONE_H_
  13. #define _REGZONE_H_
  14. // Constants corresponding to the registry.
  15. #define MAX_REG_ZONE_CACHE 20
  16. #define URLZONE_INVALID URLZONE_USER_MAX+1
  17. #define MAX_ZONE_NAME 240
  18. #define MAX_ZONE_PATH 256 // This is "Standard\\ZoneName"
  19. #define MAX_VALUE_NAME 256
  20. #define ZONEID_INVALID 0xFFFFFFFF
  21. #define URLZONE_FINDCACHEENTRY 0xFFFFFFFF
  22. // There are two registry keys under which the information is copied.
  23. // One of them is "Zones" which holds the actual information and the other
  24. // one is "TemplatePolicies" which holds the high medium and low policies.
  25. // This enumeration indicates which part of the registry to read.
  26. enum REGZONEUSE { REGZONEUSEZONES, REGZONEUSETEMPLATE };
  27. class CRegZone
  28. {
  29. public:
  30. CRegZone();
  31. // Seperate init function to allow for failure on return.
  32. BOOL Init(LPCTSTR lpStr, BOOL bCreate = TRUE, REGZONEUSE regZoneUse = REGZONEUSEZONES, BOOL bSystem = TRUE);
  33. ~CRegZone();
  34. // Attributes
  35. DWORD GetZoneId() const { return m_dwZoneId; }
  36. LPTSTR GetZoneName() const { return m_lpZoneName; }
  37. // Returns NULL terminated string, free using CoTaskFree
  38. STDMETHODIMP GetZoneAttributes (ZONEATTRIBUTES& zoneAttrib);
  39. STDMETHODIMP SetZoneAttributes (const ZONEATTRIBUTES& zoneAttrib);
  40. STDMETHODIMP GetActionPolicy (DWORD dwAction, URLZONEREG urlZone, DWORD& dwPolicy) const;
  41. STDMETHODIMP SetActionPolicy (DWORD dwAction, URLZONEREG urlZone, DWORD dwPolicy);
  42. STDMETHODIMP GetCustomPolicy (REFGUID guid, URLZONEREG urlZone, BYTE** ppByte, DWORD *pcb) const;
  43. STDMETHODIMP SetCustomPolicy (REFGUID guid, URLZONEREG urlZone, BYTE* pByte, DWORD cb);
  44. STDMETHODIMP CopyTemplatePolicies(DWORD dwTemplateIndex);
  45. BOOL UpdateZoneMapFlags( );
  46. // Should we display this zone in the UI.
  47. // Note that Zones not displayed in UI are not included in the zone enumeration.
  48. inline BOOL IsUIZone() const;
  49. protected:
  50. inline IsValid() const { return (m_dwZoneId != ZONEID_INVALID); }
  51. inline BOOL UseHKLM(URLZONEREG urlZoneReg) const;
  52. // Helper functions
  53. inline BOOL IsHardCodedZone() const { return FALSE; }
  54. inline BOOL GetHardCodedZonePolicy(DWORD dwAction, DWORD& dwPolicy) const;
  55. inline BOOL QueryTemplatePolicyIndex(CRegKey& regKey, LPCTSTR psz, LPDWORD pdw) const;
  56. inline BOOL SetTemplatePolicyIndex(CRegKey& regKey, LPCTSTR psz, DWORD dw);
  57. static BOOL IsAttributeName(LPCTSTR psz);
  58. static LPCTSTR GetTemplateNameFromIndex ( URLTEMPLATE urlTemplateIndex);
  59. inline static BOOL IsValidTemplateIndex( DWORD dwTemplateIndex );
  60. static BOOL GetAggregateAction(DWORD dwAction, LPDWORD dwAggregateAction);
  61. static void KludgeMapAggregatePolicy(DWORD dwAction, LPDWORD pdwAction);
  62. static VOID IncrementGlobalCounter( );
  63. // Methods/members to support caching
  64. protected:
  65. class CRegZoneCache {
  66. public:
  67. CRegZoneCache(void);
  68. ~CRegZoneCache(void);
  69. BOOL Lookup(DWORD dwZone, LPTSTR lpZonePath, DWORD dwAction, BOOL fUseHKLM, DWORD *pdwPolicy);
  70. void Add(DWORD dwZone, DWORD dwAction, BOOL fUseHKLM, DWORD dwPolicy, int iEntry = URLZONE_FINDCACHEENTRY);
  71. void Flush(void);
  72. static VOID IncrementGlobalCounter( );
  73. protected:
  74. // Counters to flag cross-process cache invalidation.
  75. DWORD m_dwPrevCounter ; // Global counter so we can correctly invalidate the cache if
  76. // user changes options.
  77. static HANDLE s_hMutexCounter; // mutex controlling access to shared memory counter.
  78. BOOL IsCounterEqual() const;
  79. VOID SetToCurrentCounter();
  80. // The body of the cache is this array of cache entries.
  81. // Cross-thread access control for the array is by critical section.
  82. CRITICAL_SECTION m_csectZoneCache; // assumes only one, static instance of the cache
  83. struct CRegZoneCacheEntry {
  84. CRegZoneCacheEntry(void) :
  85. m_dwZone(ZONEID_INVALID),
  86. m_dwAction(0),
  87. m_fUseHKLM(FALSE),
  88. m_dwPolicy(0) {};
  89. ~CRegZoneCacheEntry(void) { Flush(); };
  90. void Set(DWORD dwZone, DWORD dwAction, BOOL fUseHKLM, DWORD dwPolicy);
  91. void Flush(void);
  92. DWORD m_dwZone;
  93. DWORD m_dwAction;
  94. BOOL m_fUseHKLM;
  95. DWORD m_dwPolicy;
  96. }; // CRegZoneCacheEntry
  97. CRegZoneCacheEntry m_arzce[MAX_REG_ZONE_CACHE];
  98. int m_iAdd;
  99. BOOL FindCacheEntry(DWORD dwZone, DWORD dwAction, BOOL fUseHKLM, int& riEntry ); // must be called under critical section.
  100. }; // CRegZoneCache
  101. static CRegZoneCache s_rzcache;
  102. private:
  103. DWORD m_dwZoneId;
  104. DWORD m_dwZoneFlags;
  105. LPTSTR m_lpZoneName;
  106. LPTSTR m_lpZonePath;
  107. BOOL m_bHKLMOnly;
  108. BOOL m_bStandard;
  109. BOOL m_bZoneLockOut; // Is the whole zone locked out.
  110. REGZONEUSE m_regZoneUse;
  111. };
  112. typedef CRegZone *LPREGZONE;
  113. BOOL CRegZone::UseHKLM(URLZONEREG urlZoneReg) const
  114. {
  115. BOOL bReturn;
  116. switch(urlZoneReg)
  117. {
  118. case URLZONEREG_HKLM:
  119. bReturn = TRUE;
  120. break;
  121. case URLZONEREG_HKCU:
  122. bReturn = FALSE;
  123. break;
  124. case URLZONEREG_DEFAULT:
  125. bReturn = m_bHKLMOnly;
  126. break;
  127. default:
  128. TransAssert(FALSE);
  129. }
  130. return bReturn;
  131. }
  132. BOOL CRegZone::IsValidTemplateIndex(DWORD dwTemplateIndex)
  133. {
  134. BOOL bReturn = FALSE;
  135. switch (dwTemplateIndex)
  136. {
  137. case URLTEMPLATE_CUSTOM:
  138. case URLTEMPLATE_LOW:
  139. case URLTEMPLATE_MEDLOW:
  140. case URLTEMPLATE_MEDIUM:
  141. case URLTEMPLATE_HIGH:
  142. bReturn = TRUE;
  143. break;
  144. }
  145. return bReturn;
  146. }
  147. BOOL CRegZone::QueryTemplatePolicyIndex(CRegKey& regKey, LPCTSTR psz, LPDWORD pdw) const
  148. {
  149. LONG lRet;
  150. lRet = regKey.QueryValue(pdw, psz);
  151. if (NO_ERROR != lRet)
  152. {
  153. *pdw = URLTEMPLATE_CUSTOM;
  154. }
  155. else if (*pdw < URLTEMPLATE_PREDEFINED_MIN || *pdw > URLTEMPLATE_PREDEFINED_MAX)
  156. {
  157. // Invalid value, just return back default.
  158. *pdw = URLTEMPLATE_CUSTOM;
  159. }
  160. return TRUE;
  161. }
  162. BOOL CRegZone::SetTemplatePolicyIndex(CRegKey& regKey, LPCTSTR psz, DWORD dwIndex)
  163. {
  164. // Write this only if it is a valid template index.
  165. if (IsValidTemplateIndex(dwIndex))
  166. {
  167. if (regKey.SetValue(dwIndex, psz) == NO_ERROR)
  168. return TRUE;
  169. }
  170. else
  171. {
  172. TransAssert(FALSE);
  173. }
  174. return FALSE;
  175. }
  176. BOOL CRegZone::GetHardCodedZonePolicy(DWORD dwAction, DWORD& dwPolicy) const
  177. {
  178. TransAssert(IsHardCodedZone());
  179. if (!IsHardCodedZone())
  180. return FALSE;
  181. switch(dwAction)
  182. {
  183. case URLACTION_JAVA_PERMISSIONS:
  184. dwPolicy = URLPOLICY_JAVA_HIGH;
  185. break;
  186. case URLACTION_CREDENTIALS_USE:
  187. dwPolicy = URLPOLICY_CREDENTIALS_SILENT_LOGON_OK;
  188. break;
  189. case URLACTION_AUTHENTICATE_CLIENT:
  190. dwPolicy = URLPOLICY_AUTHENTICATE_CLEARTEXT_OK;
  191. break;
  192. case URLACTION_ACTIVEX_OVERRIDE_OBJECT_SAFETY:
  193. dwPolicy = URLPOLICY_QUERY;
  194. break;
  195. default:
  196. dwPolicy = 0;
  197. break;
  198. }
  199. return TRUE;
  200. }
  201. BOOL CRegZone::IsUIZone() const
  202. {
  203. return (m_dwZoneFlags & ZAFLAGS_NO_UI) ? FALSE : TRUE;
  204. }
  205. // This is the class that maintains the list of the RegZones currently in action
  206. class CRegZoneContainer
  207. {
  208. public:
  209. CRegZoneContainer();
  210. ~CRegZoneContainer();
  211. public:
  212. BOOL Attach(BOOL bUseHKLM, REGZONEUSE regZoneUse = REGZONEUSEZONES);
  213. BOOL Detach();
  214. BOOL SelfHeal(BOOL bUseHKLM);
  215. CRegZone * GetRegZoneByName(LPCTSTR lpszZoneName) const;
  216. CRegZone * GetRegZoneById(DWORD dwZoneId) const;
  217. DWORD GetZoneCount() const { return m_cZones; };
  218. STDMETHODIMP CreateZoneEnumerator(DWORD *pdwEnum, DWORD *pdwCount);
  219. STDMETHODIMP GetZoneAt(DWORD dwEnum, DWORD dwIndex, DWORD *pdwZone);
  220. STDMETHODIMP DestroyZoneEnumerator(DWORD dwEnum);
  221. protected:
  222. // Used internally only.
  223. struct CRegListElem {
  224. CRegListElem * next;
  225. CRegZone * pRegZone;
  226. DWORD dwZoneIndex;
  227. };
  228. struct CZoneEnumList {
  229. DWORD dwEnum; // Cookie indicating which enum this corresponds to.
  230. CZoneEnumList * next;
  231. };
  232. CZoneEnumList * m_pZoneEnumList;
  233. DWORD m_dwNextEnum;
  234. // Is this enumerator a valid enumerator.
  235. BOOL VerifyZoneEnum(DWORD dwEnum) const;
  236. private:
  237. CRegZone** m_ppRegZones; // Array of RegZones.
  238. DWORD m_cZones; // # of Zones.
  239. BOOL m_bHKLMOnly;
  240. CRITICAL_SECTION m_csect;
  241. };
  242. #endif // _REGZONE_H_