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.

165 lines
4.5 KiB

  1. #include <wininetp.h>
  2. /* Forward declarations */
  3. class CP3PSettingsCache;
  4. struct P3PCookieState;
  5. struct CompactPolicy;
  6. class CPEvalRule;
  7. /* Abstract base class for representing evaluation rules */
  8. class CPEvalRule {
  9. public:
  10. /* virtual destructor is necessary... */
  11. virtual ~CPEvalRule() { }
  12. /* Derived class MUST provide an implementation */
  13. virtual int evaluate(const CompactPolicy &sitePolicy) = 0;
  14. protected:
  15. CPEvalRule *pNext; /* used for keeping linked list of rules */
  16. friend class CCookieSettings;
  17. };
  18. /* User-settings for handling cookies */
  19. class CCookieSettings {
  20. public:
  21. int EvaluatePolicy(P3PCookieState *pState);
  22. void AddRef() { iRefCount++; }
  23. void Release();
  24. /* Externally used function for clearing memory cache.
  25. Called when internet options are changed */
  26. static void RefreshP3PSettings();
  27. static bool GetSettings(CCookieSettings **pSettings, const char *pszURL, BOOL fis3rdParty, BOOL fRestricted=FALSE);
  28. static bool GetSettings(CCookieSettings **pSettings, DWORD dwSecZone, BOOL fis3rdParty);
  29. static bool extractCompactPolicy(const char *pszP3PHeader, char *pszPolicy, DWORD *pPolicyLen);
  30. protected:
  31. CCookieSettings(unsigned char *pBinaryRep, int cb);
  32. ~CCookieSettings();
  33. bool parseSpecialSymbol(char *pszToken, int iSetting);
  34. int evaluateToken(const char *pszToken);
  35. void addEvalRule(CPEvalRule *pRule);
  36. static void convertToASCII(char *pstrSettings, int cbBytes);
  37. private:
  38. int iRefCount;
  39. /* The flag determines if the settings always make the
  40. same decision regardless of policy.
  41. In that case the decision is stored in the next field */
  42. bool fConstant;
  43. unsigned long dwFixedDecision;
  44. /* Decision in the absence of compact-policy */
  45. unsigned long dwNoPolicyDecision;
  46. /* Does the evaluation result apply to session cookies? */
  47. bool fApplyToSC;
  48. unsigned char *MPactions;
  49. CPEvalRule *pRuleSet, **ppLast;
  50. static CP3PSettingsCache cookiePrefsCache;
  51. };
  52. /*
  53. * Utility class that controls ownership of a critical section
  54. * through its lifetime.
  55. * constructor invoked --> enter CS
  56. * destructor invoked --> leave CS
  57. */
  58. class CriticalSectOwner {
  59. public:
  60. CriticalSectOwner(CRITICAL_SECTION *pcs)
  61. { EnterCriticalSection(pSection=pcs); }
  62. ~CriticalSectOwner()
  63. { LeaveCriticalSection(pSection); }
  64. private:
  65. CRITICAL_SECTION *pSection;
  66. };
  67. class CP3PSettingsCache {
  68. public:
  69. CP3PSettingsCache();
  70. ~CP3PSettingsCache();
  71. CCookieSettings *lookupCookieSettings(DWORD dwZone, BOOL f3rdParty);
  72. void saveCookieSettings(DWORD dwZone, BOOL f3rdParty, CCookieSettings *pSettings);
  73. void evictAll();
  74. private:
  75. enum { MaxKnownZone = 5 };
  76. CCookieSettings *stdCookiePref[MaxKnownZone+1];
  77. CCookieSettings *std3rdPartyPref[MaxKnownZone+1];
  78. CRITICAL_SECTION csCache;
  79. };
  80. /*
  81. Data type for binary representation of compact-policies
  82. Since compact policy in V1 spec is simply a set of predefined
  83. tokens, we use a bit-set representation.
  84. */
  85. struct CompactPolicy {
  86. typedef unsigned __int64 quadword;
  87. CompactPolicy() { qwLow = qwHigh = 0; }
  88. CompactPolicy(quadword high, quadword low) : qwLow(low), qwHigh(high) { }
  89. CompactPolicy operator & (const CompactPolicy &ps) const;
  90. bool operator == (const CompactPolicy &ps) const;
  91. bool operator != (const CompactPolicy &ps) const;
  92. void addToken(int index);
  93. int contains(int index);
  94. /* Two quadwords are sufficient provided # of tokens <= 128 */
  95. quadword qwLow;
  96. quadword qwHigh;
  97. };
  98. /* structure used for communicating with CCookieSettings::Evaluate */
  99. struct P3PCookieState {
  100. const char *pszP3PHeader;
  101. unsigned long dwPolicyState;
  102. int fValidPolicy : 1; /* is there a syntactically valid policy? */
  103. int fEvaluated : 1; /* was the compact-policy evaluated? */
  104. int fIncSession : 1; /* does the outcome apply to session-cookies? */
  105. unsigned long dwEvalMode; /* {accept, evaluate, reject} */
  106. CompactPolicy cpSitePolicy;
  107. };
  108. /* Utility functions */
  109. const char *getNextToken(const char *pch, char *pszToken, int cbToken, bool fWhiteSpc=true, int *pLength=NULL);
  110. int mapCookieAction(char ch);
  111. int findSymbol(const char *pstr);
  112. CPEvalRule *parseEvalRule(char *pszRule, char **ppEndRule=NULL);