SSPCPP-616 - clean up concatenated string literals
[shibboleth/cpp-opensaml.git] / saml / saml2 / profile / impl / DelegationRestrictionRule.cpp
index 5bc3bea..2faf209 100644 (file)
@@ -1,23 +1,27 @@
-/*
- *  Copyright 2009 Internet2
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
  *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * UCAID licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the
+ * License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
  */
 
 /**
  * DelegationRestrictionRule.cpp
  *
- * SAML DelegationRestriction SecurityPolicyRule
+ * SAML DelegationRestriction SecurityPolicyRule.
  */
 
 #include "internal.h"
 #include "saml2/core/Assertions.h"
 #include "util/SAMLConstants.h"
 
+#include <ctime>
+#include <boost/ptr_container/ptr_vector.hpp>
+#include <xercesc/util/XMLUniDefs.hpp>
 #include <xmltooling/logging.h>
+#include <xmltooling/XMLToolingConfig.h>
 
 using namespace opensaml::saml2;
 using namespace opensaml;
 using namespace xmltooling::logging;
 using namespace xmltooling;
+using namespace boost;
 using namespace std;
 
 namespace opensaml {
@@ -42,7 +51,6 @@ namespace opensaml {
             DelegationRestrictionRule(const DOMElement* e);
 
             virtual ~DelegationRestrictionRule() {
-                for_each(m_delegates.begin(), m_delegates.end(), xmltooling::cleanup<Delegate>());
             }
             const char* getType() const {
                 return DELEGATION_POLICY_RULE;
@@ -50,12 +58,13 @@ namespace opensaml {
             bool evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
 
         private:
-            vector<Delegate*> m_delegates;
+            ptr_vector<Delegate> m_delegates;
             enum {
                 MATCH_ANY,
                 MATCH_NEWEST,
                 MATCH_OLDEST
             } m_match;
+            time_t m_maxTime;
         };
 
         SecurityPolicyRule* SAML_DLLLOCAL DelegationRestrictionRuleFactory(const DOMElement* const & e)
@@ -81,25 +90,25 @@ namespace opensaml {
                         XMLString::equals(n1->getSPNameQualifier(), n2->getSPNameQualifier()));
             }
         public:
-            _isSameDelegate() : m_operand(NULL) {}
+            _isSameDelegate() : m_operand(nullptr) {}
             _isSameDelegate(const Delegate* d) : m_operand(d) {}
 
             // d1 is the input from the message, d2 is from the policy
-            bool operator()(const Delegate* d1, const Delegate* d2) const {
+            bool operator()(const Delegate* d1, const Delegate& d2) const {
                 if (!d1->getNameID()) {
-                    Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.DelegationRestriction").error(
+                    Category::getInstance(SAML_LOGCAT ".SecurityPolicyRule.DelegationRestriction").error(
                         "rule doesn't support evaluation of BaseID or EncryptedID in a Delegate"
                         );
                     return false;
                 }
-                if (!d2->getConfirmationMethod() || XMLString::equals(d1->getConfirmationMethod(), d2->getConfirmationMethod())) {
-                    return matches(d1->getNameID(), d2->getNameID());
+                if (!d2.getConfirmationMethod() || XMLString::equals(d1->getConfirmationMethod(), d2.getConfirmationMethod())) {
+                    return matches(d1->getNameID(), d2.getNameID());
                 }
                 return false;
             }
 
             // d is from the policy
-            bool operator()(const Delegate* d) const {
+            bool operator()(const Delegate& d) const {
                 return this->operator()(m_operand, d);
             }
         };
@@ -108,14 +117,15 @@ namespace opensaml {
         static XMLCh any[] =    UNICODE_LITERAL_8(a,n,y,O,r,d,e,r);
         static XMLCh newest[] = UNICODE_LITERAL_6(n,e,w,e,s,t);
         static XMLCh oldest[] = UNICODE_LITERAL_6(o,l,d,e,s,t);
-
+        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);
     }
 };
 
-DelegationRestrictionRule::DelegationRestrictionRule(const DOMElement* e) : m_match(MATCH_ANY)
+DelegationRestrictionRule::DelegationRestrictionRule(const DOMElement* e)
+    : m_match(MATCH_ANY), m_maxTime(XMLHelper::getAttrInt(e, 0, maxTimeSinceDelegation))
 {
     if (e) {
-        const XMLCh* m = e->getAttributeNS(NULL, match);
+        const XMLCh* m = e ? e->getAttributeNS(nullptr, match) : nullptr;
         if (XMLString::equals(m, newest))
             m_match = MATCH_NEWEST;
         else if (XMLString::equals(m, oldest))
@@ -123,21 +133,15 @@ DelegationRestrictionRule::DelegationRestrictionRule(const DOMElement* e) : m_ma
         else if (m && *m && !XMLString::equals(m, any))
             throw SecurityPolicyException("Invalid value for \"match\" attribute in Delegation rule.");
 
-        try {
-            DOMElement* d = XMLHelper::getFirstChildElement(e, samlconstants::SAML20_DELEGATION_CONDITION_NS, Delegate::LOCAL_NAME);
-            while (d) {
-                auto_ptr<XMLObject> wrapper(XMLObjectBuilder::buildOneFromElement(d));
-                Delegate* down = dynamic_cast<Delegate*>(wrapper.get());
-                if (down) {
-                    m_delegates.push_back(down);
-                    wrapper.release();
-                }
-                d = XMLHelper::getNextSiblingElement(d, samlconstants::SAML20_DELEGATION_CONDITION_NS, Delegate::LOCAL_NAME);
+        DOMElement* d = XMLHelper::getFirstChildElement(e, samlconstants::SAML20_DELEGATION_CONDITION_NS, Delegate::LOCAL_NAME);
+        while (d) {
+            auto_ptr<XMLObject> wrapper(XMLObjectBuilder::buildOneFromElement(d));
+            Delegate* down = dynamic_cast<Delegate*>(wrapper.get());
+            if (down) {
+                m_delegates.push_back(down);
+                wrapper.release();
             }
-        }
-        catch (exception&) {
-            for_each(m_delegates.begin(), m_delegates.end(), xmltooling::cleanup<Delegate>());
-            throw;
+            d = XMLHelper::getNextSiblingElement(d, samlconstants::SAML20_DELEGATION_CONDITION_NS, Delegate::LOCAL_NAME);
         }
     }
 }
@@ -147,24 +151,29 @@ bool DelegationRestrictionRule::evaluate(const XMLObject& message, const Generic
     const DelegationRestrictionType* drt=dynamic_cast<const DelegationRestrictionType*>(&message);
     if (!drt)
         return false;
-
-    // If we have no embedded Delegates, the condition evaluates to true.
-    if (m_delegates.empty())
-        return true;
-
     const vector<Delegate*>& dels = drt->getDelegates();
-    if (m_match == MATCH_ANY) {
-        // Each Delegate in the condition MUST match an embedded Delegate.
-        for (vector<Delegate*>::const_iterator d1 = dels.begin(); d1 != dels.end(); ++d1) {
-            if (find_if(m_delegates.begin(), m_delegates.end(), _isSameDelegate(*d1)) == m_delegates.end())
+
+    if (!m_delegates.empty()) {
+        if (m_match == MATCH_ANY) {
+            // Each Delegate in the condition MUST match an embedded Delegate.
+            for (vector<Delegate*>::const_iterator d1 = dels.begin(); d1 != dels.end(); ++d1) {
+                if (find_if(m_delegates.begin(), m_delegates.end(), _isSameDelegate(*d1)) == m_delegates.end())
+                    return false;
+            }
+        }
+        else if (m_match == MATCH_OLDEST) {
+            if (search(dels.begin(), dels.end(), m_delegates.begin(), m_delegates.end(), _isSameDelegate()) != dels.begin())
+                return false;
+        }
+        else if (m_match == MATCH_NEWEST) {
+            if (search(dels.rbegin(), dels.rend(), m_delegates.begin(), m_delegates.end(), _isSameDelegate()) != dels.rbegin())
                 return false;
         }
     }
-    else if (m_match == MATCH_OLDEST) {
-        return (search(dels.begin(), dels.end(), m_delegates.begin(), m_delegates.end(), _isSameDelegate()) == dels.begin());
-    }
-    else if (m_match == MATCH_NEWEST) {
-        return (search(dels.rbegin(), dels.rend(), m_delegates.begin(), m_delegates.end(), _isSameDelegate()) == dels.rbegin());
+
+    if (m_maxTime > 0) {
+        return (!dels.empty() && dels.front()->getDelegationInstant() &&
+            (time(nullptr) - dels.front()->getDelegationInstantEpoch() - XMLToolingConfig::getConfig().clock_skew_secs <= m_maxTime));
     }
 
     return true;