Fixes to constants, allow sequences of condition rules, new policy rule for Delegatio...
[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         };
60
61         SecurityPolicyRule* SAML_DLLLOCAL DelegationRestrictionRuleFactory(const DOMElement* const & e)
62         {
63             return new DelegationRestrictionRule(e);
64         }
65
66         class SAML_DLLLOCAL _isSameDelegate : public binary_function<const Delegate*,const Delegate*,bool>,
67             public unary_function<const Delegate*,bool>
68         {
69             const Delegate* m_operand;
70             bool isSameFormat(const XMLCh* f1, const XMLCh* f2) const {
71                 if (!f1 || !*f1)
72                     f1 = NameIDType::UNSPECIFIED;
73                 if (!f2 || !*f2)
74                     f2 = NameIDType::UNSPECIFIED;
75                 return XMLString::equals(f1, f2);
76             }
77             bool matches(const NameID* n1, const NameID* n2) const {
78                 return (isSameFormat(n1->getFormat(), n2->getFormat()) &&
79                         XMLString::equals(n1->getName(), n2->getName()) &&
80                         XMLString::equals(n1->getNameQualifier(), n2->getNameQualifier()) &&
81                         XMLString::equals(n1->getSPNameQualifier(), n2->getSPNameQualifier()));
82             }
83         public:
84             _isSameDelegate() : m_operand(NULL) {}
85             _isSameDelegate(const Delegate* d) : m_operand(d) {}
86
87             // d1 is the input from the message, d2 is from the policy
88             bool operator()(const Delegate* d1, const Delegate* d2) const {
89                 if (!d1->getNameID()) {
90                     Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.DelegationRestriction").error(
91                         "rule doesn't support evaluation of BaseID or EncryptedID in a Delegate"
92                         );
93                     return false;
94                 }
95                 if (!d2->getConfirmationMethod() || XMLString::equals(d1->getConfirmationMethod(), d2->getConfirmationMethod())) {
96                     return matches(d1->getNameID(), d2->getNameID());
97                 }
98                 return false;
99             }
100
101             // d is from the policy
102             bool operator()(const Delegate* d) const {
103                 return this->operator()(m_operand, d);
104             }
105         };
106
107         static XMLCh match[] =  UNICODE_LITERAL_5(m,a,t,c,h);
108         static XMLCh any[] =    UNICODE_LITERAL_8(a,n,y,O,r,d,e,r);
109         static XMLCh newest[] = UNICODE_LITERAL_6(n,e,w,e,s,t);
110         static XMLCh oldest[] = UNICODE_LITERAL_6(o,l,d,e,s,t);
111
112     }
113 };
114
115 DelegationRestrictionRule::DelegationRestrictionRule(const DOMElement* e) : m_match(MATCH_ANY)
116 {
117     if (e) {
118         const XMLCh* m = e->getAttributeNS(NULL, match);
119         if (XMLString::equals(m, newest))
120             m_match = MATCH_NEWEST;
121         else if (XMLString::equals(m, oldest))
122             m_match = MATCH_OLDEST;
123         else if (m && *m && !XMLString::equals(m, any))
124             throw SecurityPolicyException("Invalid value for \"match\" attribute in Delegation rule.");
125
126         try {
127             DOMElement* d = XMLHelper::getFirstChildElement(e, samlconstants::SAML20_DELEGATION_CONDITION_NS, Delegate::LOCAL_NAME);
128             while (d) {
129                 auto_ptr<XMLObject> wrapper(XMLObjectBuilder::buildOneFromElement(d));
130                 Delegate* down = dynamic_cast<Delegate*>(wrapper.get());
131                 if (down) {
132                     m_delegates.push_back(down);
133                     wrapper.release();
134                 }
135                 d = XMLHelper::getNextSiblingElement(d, samlconstants::SAML20_DELEGATION_CONDITION_NS, Delegate::LOCAL_NAME);
136             }
137         }
138         catch (exception&) {
139             for_each(m_delegates.begin(), m_delegates.end(), xmltooling::cleanup<Delegate>());
140             throw;
141         }
142     }
143 }
144
145 bool DelegationRestrictionRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
146 {
147     const DelegationRestrictionType* drt=dynamic_cast<const DelegationRestrictionType*>(&message);
148     if (!drt)
149         return false;
150
151     // If we have no embedded Delegates, the condition evaluates to true.
152     if (m_delegates.empty())
153         return true;
154
155     const vector<Delegate*>& dels = drt->getDelegates();
156     if (m_match == MATCH_ANY) {
157         // Each Delegate in the condition MUST match an embedded Delegate.
158         for (vector<Delegate*>::const_iterator d1 = dels.begin(); d1 != dels.end(); ++d1) {
159             if (find_if(m_delegates.begin(), m_delegates.end(), _isSameDelegate(*d1)) == m_delegates.end())
160                 return false;
161         }
162     }
163     else if (m_match == MATCH_OLDEST) {
164         return (search(dels.begin(), dels.end(), m_delegates.begin(), m_delegates.end(), _isSameDelegate()) == dels.begin());
165     }
166     else if (m_match == MATCH_NEWEST) {
167         return (search(dels.rbegin(), dels.rend(), m_delegates.begin(), m_delegates.end(), _isSameDelegate()) == dels.rbegin());
168     }
169
170     return true;
171 }