Add maxTimeSinceDelegation option.
[shibboleth/cpp-opensaml.git] / saml / saml2 / profile / impl / DelegationRestrictionRule.cpp
1 /*
2  *  Copyright 2009 Internet2
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * DelegationRestrictionRule.cpp
19  *
20  * SAML DelegationRestriction SecurityPolicyRule
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "binding/SecurityPolicyRule.h"
26 #include "saml2/core/Assertions.h"
27 #include "util/SAMLConstants.h"
28
29 #include <xmltooling/logging.h>
30
31 using namespace opensaml::saml2;
32 using namespace opensaml;
33 using namespace xmltooling::logging;
34 using namespace xmltooling;
35 using namespace std;
36
37 namespace opensaml {
38     namespace saml2 {
39         class SAML_DLLLOCAL DelegationRestrictionRule : public SecurityPolicyRule
40         {
41         public:
42             DelegationRestrictionRule(const DOMElement* e);
43
44             virtual ~DelegationRestrictionRule() {
45                 for_each(m_delegates.begin(), m_delegates.end(), xmltooling::cleanup<Delegate>());
46             }
47             const char* getType() const {
48                 return DELEGATION_POLICY_RULE;
49             }
50             bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
51
52         private:
53             vector<Delegate*> m_delegates;
54             enum {
55                 MATCH_ANY,
56                 MATCH_NEWEST,
57                 MATCH_OLDEST
58             } m_match;
59             time_t m_maxTime;
60         };
61
62         SecurityPolicyRule* SAML_DLLLOCAL DelegationRestrictionRuleFactory(const DOMElement* const & e)
63         {
64             return new DelegationRestrictionRule(e);
65         }
66
67         class SAML_DLLLOCAL _isSameDelegate : public binary_function<const Delegate*,const Delegate*,bool>,
68             public unary_function<const Delegate*,bool>
69         {
70             const Delegate* m_operand;
71             bool isSameFormat(const XMLCh* f1, const XMLCh* f2) const {
72                 if (!f1 || !*f1)
73                     f1 = NameIDType::UNSPECIFIED;
74                 if (!f2 || !*f2)
75                     f2 = NameIDType::UNSPECIFIED;
76                 return XMLString::equals(f1, f2);
77             }
78             bool matches(const NameID* n1, const NameID* n2) const {
79                 return (isSameFormat(n1->getFormat(), n2->getFormat()) &&
80                         XMLString::equals(n1->getName(), n2->getName()) &&
81                         XMLString::equals(n1->getNameQualifier(), n2->getNameQualifier()) &&
82                         XMLString::equals(n1->getSPNameQualifier(), n2->getSPNameQualifier()));
83             }
84         public:
85             _isSameDelegate() : m_operand(NULL) {}
86             _isSameDelegate(const Delegate* d) : m_operand(d) {}
87
88             // d1 is the input from the message, d2 is from the policy
89             bool operator()(const Delegate* d1, const Delegate* d2) const {
90                 if (!d1->getNameID()) {
91                     Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.DelegationRestriction").error(
92                         "rule doesn't support evaluation of BaseID or EncryptedID in a Delegate"
93                         );
94                     return false;
95                 }
96                 if (!d2->getConfirmationMethod() || XMLString::equals(d1->getConfirmationMethod(), d2->getConfirmationMethod())) {
97                     return matches(d1->getNameID(), d2->getNameID());
98                 }
99                 return false;
100             }
101
102             // d is from the policy
103             bool operator()(const Delegate* d) const {
104                 return this->operator()(m_operand, d);
105             }
106         };
107
108         static XMLCh match[] =  UNICODE_LITERAL_5(m,a,t,c,h);
109         static XMLCh any[] =    UNICODE_LITERAL_8(a,n,y,O,r,d,e,r);
110         static XMLCh newest[] = UNICODE_LITERAL_6(n,e,w,e,s,t);
111         static XMLCh oldest[] = UNICODE_LITERAL_6(o,l,d,e,s,t);
112         static XMLCh maxTimeSinceDelegation[] = UNICODE_LITERAL_22(m,a,x,T,i,m,e,S,i,n,c,e,D,e,l,e,g,a,t,i,o,n);
113     }
114 };
115
116 DelegationRestrictionRule::DelegationRestrictionRule(const DOMElement* e) : m_match(MATCH_ANY), m_maxTime(0)
117 {
118     if (e) {
119         const XMLCh* m = e->getAttributeNS(NULL, match);
120         if (XMLString::equals(m, newest))
121             m_match = MATCH_NEWEST;
122         else if (XMLString::equals(m, oldest))
123             m_match = MATCH_OLDEST;
124         else if (m && *m && !XMLString::equals(m, any))
125             throw SecurityPolicyException("Invalid value for \"match\" attribute in Delegation rule.");
126         m = e->getAttributeNS(NULL, maxTimeSinceDelegation);
127         if (m && *m)
128             m_maxTime = XMLString::parseInt(m);
129
130         try {
131             DOMElement* d = XMLHelper::getFirstChildElement(e, samlconstants::SAML20_DELEGATION_CONDITION_NS, Delegate::LOCAL_NAME);
132             while (d) {
133                 auto_ptr<XMLObject> wrapper(XMLObjectBuilder::buildOneFromElement(d));
134                 Delegate* down = dynamic_cast<Delegate*>(wrapper.get());
135                 if (down) {
136                     m_delegates.push_back(down);
137                     wrapper.release();
138                 }
139                 d = XMLHelper::getNextSiblingElement(d, samlconstants::SAML20_DELEGATION_CONDITION_NS, Delegate::LOCAL_NAME);
140             }
141         }
142         catch (exception&) {
143             for_each(m_delegates.begin(), m_delegates.end(), xmltooling::cleanup<Delegate>());
144             throw;
145         }
146     }
147 }
148
149 bool DelegationRestrictionRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
150 {
151     const DelegationRestrictionType* drt=dynamic_cast<const DelegationRestrictionType*>(&message);
152     if (!drt)
153         return false;
154     const vector<Delegate*>& dels = drt->getDelegates();
155
156     if (!m_delegates.empty()) {
157         if (m_match == MATCH_ANY) {
158             // Each Delegate in the condition MUST match an embedded Delegate.
159             for (vector<Delegate*>::const_iterator d1 = dels.begin(); d1 != dels.end(); ++d1) {
160                 if (find_if(m_delegates.begin(), m_delegates.end(), _isSameDelegate(*d1)) == m_delegates.end())
161                     return false;
162             }
163         }
164         else if (m_match == MATCH_OLDEST) {
165             if (search(dels.begin(), dels.end(), m_delegates.begin(), m_delegates.end(), _isSameDelegate()) != dels.begin())
166                 return false;
167         }
168         else if (m_match == MATCH_NEWEST) {
169             if (search(dels.rbegin(), dels.rend(), m_delegates.begin(), m_delegates.end(), _isSameDelegate()) != dels.rbegin())
170                 return false;
171         }
172     }
173
174     if (m_maxTime > 0) {
175         return (!dels.empty() && dels.front()->getDelegationInstant() &&
176             (time(NULL) - dels.front()->getDelegationInstantEpoch() - XMLToolingConfig::getConfig().clock_skew_secs <= m_maxTime));
177     }
178
179     return true;
180 }