Reducing header overuse, non-inlining selected methods (CPPOST-35).
[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 <ctime>
30 #include <xmltooling/logging.h>
31 #include <xmltooling/XMLToolingConfig.h>
32
33 using namespace opensaml::saml2;
34 using namespace opensaml;
35 using namespace xmltooling::logging;
36 using namespace xmltooling;
37 using namespace std;
38
39 namespace opensaml {
40     namespace saml2 {
41         class SAML_DLLLOCAL DelegationRestrictionRule : public SecurityPolicyRule
42         {
43         public:
44             DelegationRestrictionRule(const DOMElement* e);
45
46             virtual ~DelegationRestrictionRule() {
47                 for_each(m_delegates.begin(), m_delegates.end(), xmltooling::cleanup<Delegate>());
48             }
49             const char* getType() const {
50                 return DELEGATION_POLICY_RULE;
51             }
52             bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
53
54         private:
55             vector<Delegate*> m_delegates;
56             enum {
57                 MATCH_ANY,
58                 MATCH_NEWEST,
59                 MATCH_OLDEST
60             } m_match;
61             time_t m_maxTime;
62         };
63
64         SecurityPolicyRule* SAML_DLLLOCAL DelegationRestrictionRuleFactory(const DOMElement* const & e)
65         {
66             return new DelegationRestrictionRule(e);
67         }
68
69         class SAML_DLLLOCAL _isSameDelegate : public binary_function<const Delegate*,const Delegate*,bool>,
70             public unary_function<const Delegate*,bool>
71         {
72             const Delegate* m_operand;
73             bool isSameFormat(const XMLCh* f1, const XMLCh* f2) const {
74                 if (!f1 || !*f1)
75                     f1 = NameIDType::UNSPECIFIED;
76                 if (!f2 || !*f2)
77                     f2 = NameIDType::UNSPECIFIED;
78                 return XMLString::equals(f1, f2);
79             }
80             bool matches(const NameID* n1, const NameID* n2) const {
81                 return (isSameFormat(n1->getFormat(), n2->getFormat()) &&
82                         XMLString::equals(n1->getName(), n2->getName()) &&
83                         XMLString::equals(n1->getNameQualifier(), n2->getNameQualifier()) &&
84                         XMLString::equals(n1->getSPNameQualifier(), n2->getSPNameQualifier()));
85             }
86         public:
87             _isSameDelegate() : m_operand(NULL) {}
88             _isSameDelegate(const Delegate* d) : m_operand(d) {}
89
90             // d1 is the input from the message, d2 is from the policy
91             bool operator()(const Delegate* d1, const Delegate* d2) const {
92                 if (!d1->getNameID()) {
93                     Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.DelegationRestriction").error(
94                         "rule doesn't support evaluation of BaseID or EncryptedID in a Delegate"
95                         );
96                     return false;
97                 }
98                 if (!d2->getConfirmationMethod() || XMLString::equals(d1->getConfirmationMethod(), d2->getConfirmationMethod())) {
99                     return matches(d1->getNameID(), d2->getNameID());
100                 }
101                 return false;
102             }
103
104             // d is from the policy
105             bool operator()(const Delegate* d) const {
106                 return this->operator()(m_operand, d);
107             }
108         };
109
110         static XMLCh match[] =  UNICODE_LITERAL_5(m,a,t,c,h);
111         static XMLCh any[] =    UNICODE_LITERAL_8(a,n,y,O,r,d,e,r);
112         static XMLCh newest[] = UNICODE_LITERAL_6(n,e,w,e,s,t);
113         static XMLCh oldest[] = UNICODE_LITERAL_6(o,l,d,e,s,t);
114         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);
115     }
116 };
117
118 DelegationRestrictionRule::DelegationRestrictionRule(const DOMElement* e) : m_match(MATCH_ANY), m_maxTime(0)
119 {
120     if (e) {
121         const XMLCh* m = e->getAttributeNS(NULL, match);
122         if (XMLString::equals(m, newest))
123             m_match = MATCH_NEWEST;
124         else if (XMLString::equals(m, oldest))
125             m_match = MATCH_OLDEST;
126         else if (m && *m && !XMLString::equals(m, any))
127             throw SecurityPolicyException("Invalid value for \"match\" attribute in Delegation rule.");
128         m = e->getAttributeNS(NULL, maxTimeSinceDelegation);
129         if (m && *m)
130             m_maxTime = XMLString::parseInt(m);
131
132         try {
133             DOMElement* d = XMLHelper::getFirstChildElement(e, samlconstants::SAML20_DELEGATION_CONDITION_NS, Delegate::LOCAL_NAME);
134             while (d) {
135                 auto_ptr<XMLObject> wrapper(XMLObjectBuilder::buildOneFromElement(d));
136                 Delegate* down = dynamic_cast<Delegate*>(wrapper.get());
137                 if (down) {
138                     m_delegates.push_back(down);
139                     wrapper.release();
140                 }
141                 d = XMLHelper::getNextSiblingElement(d, samlconstants::SAML20_DELEGATION_CONDITION_NS, Delegate::LOCAL_NAME);
142             }
143         }
144         catch (exception&) {
145             for_each(m_delegates.begin(), m_delegates.end(), xmltooling::cleanup<Delegate>());
146             throw;
147         }
148     }
149 }
150
151 bool DelegationRestrictionRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
152 {
153     const DelegationRestrictionType* drt=dynamic_cast<const DelegationRestrictionType*>(&message);
154     if (!drt)
155         return false;
156     const vector<Delegate*>& dels = drt->getDelegates();
157
158     if (!m_delegates.empty()) {
159         if (m_match == MATCH_ANY) {
160             // Each Delegate in the condition MUST match an embedded Delegate.
161             for (vector<Delegate*>::const_iterator d1 = dels.begin(); d1 != dels.end(); ++d1) {
162                 if (find_if(m_delegates.begin(), m_delegates.end(), _isSameDelegate(*d1)) == m_delegates.end())
163                     return false;
164             }
165         }
166         else if (m_match == MATCH_OLDEST) {
167             if (search(dels.begin(), dels.end(), m_delegates.begin(), m_delegates.end(), _isSameDelegate()) != dels.begin())
168                 return false;
169         }
170         else if (m_match == MATCH_NEWEST) {
171             if (search(dels.rbegin(), dels.rend(), m_delegates.begin(), m_delegates.end(), _isSameDelegate()) != dels.rbegin())
172                 return false;
173         }
174     }
175
176     if (m_maxTime > 0) {
177         return (!dels.empty() && dels.front()->getDelegationInstant() &&
178             (time(NULL) - dels.front()->getDelegationInstantEpoch() - XMLToolingConfig::getConfig().clock_skew_secs <= m_maxTime));
179     }
180
181     return true;
182 }