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