https://issues.shibboleth.net/jira/browse/CPPOST-14
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / XMLMetadataProvider.cpp
index d47a1d4..adcdee8 100644 (file)
@@ -1,6 +1,6 @@
 /*
- *  Copyright 2001-2006 Internet2
- * 
+ *  Copyright 2001-2007 Internet2
+ *
  * 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
 
 /**
  * XMLMetadataProvider.cpp
- * 
+ *
  * Supplies metadata from an XML file
  */
 
 #include "internal.h"
 #include "saml2/metadata/Metadata.h"
+#include "saml2/metadata/MetadataFilter.h"
 #include "saml2/metadata/AbstractMetadataProvider.h"
 
-#include <log4cpp/Category.hh>
 #include <xmltooling/util/NDC.h>
 #include <xmltooling/util/ReloadableXMLFile.h>
-#include <xmltooling/util/XMLConstants.h>
 
 using namespace opensaml::saml2md;
+using namespace xmltooling::logging;
 using namespace xmltooling;
-using namespace log4cpp;
 using namespace std;
 
 #if defined (_MSC_VER)
@@ -42,10 +41,17 @@ using namespace std;
 namespace opensaml {
     namespace saml2md {
 
+        static const XMLCh requireValidUntil[] = UNICODE_LITERAL_17(r,e,q,u,i,r,e,V,a,l,i,d,U,n,t,i,l);
+
         class SAML_DLLLOCAL XMLMetadataProvider : public AbstractMetadataProvider, public ReloadableXMLFile
         {
         public:
-            XMLMetadataProvider(const DOMElement* e) : AbstractMetadataProvider(e), ReloadableXMLFile(e), m_object(NULL) {}
+            XMLMetadataProvider(const DOMElement* e)
+                : AbstractMetadataProvider(e), ReloadableXMLFile(e, Category::getInstance(SAML_LOGCAT".MetadataProvider.XML")),
+                    m_object(NULL), m_requireValidUntil(false) {
+                const XMLCh* flag = e ? e->getAttributeNS(NULL,requireValidUntil) : NULL;
+                m_requireValidUntil = (flag && (*flag == chLatin_t || *flag == chDigit_1));
+            }
             virtual ~XMLMetadataProvider() {
                 delete m_object;
             }
@@ -53,18 +59,21 @@ namespace opensaml {
             void init() {
                 load(); // guarantees an exception or the metadata is loaded
             }
-            
-            pair<bool,DOMElement*> load();
 
             const XMLObject* getMetadata() const {
                 return m_object;
             }
 
+        protected:
+            pair<bool,DOMElement*> load();
+
         private:
+            using AbstractMetadataProvider::index;
             void index();
-        
+
             XMLObject* m_object;
-        }; 
+            bool m_requireValidUntil;
+        };
 
         MetadataProvider* SAML_DLLLOCAL XMLMetadataProviderFactory(const DOMElement* const & e)
         {
@@ -80,40 +89,40 @@ namespace opensaml {
 
 pair<bool,DOMElement*> XMLMetadataProvider::load()
 {
-#ifdef _DEBUG
-    NDC ndc("load");
-#endif
-    
-    try {
-        // Load from source using base class.
-        pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
-        
-        // If we own it, wrap it for now.
-        XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
-                
-        // Unmarshall objects, binding the document.
-        XMLObject* xmlObject=XMLObjectBuilder::buildOneFromElement(raw.second, true);
-        docjanitor.release();
-        
-        // Preprocess the metadata.
-        auto_ptr<XMLObject> xmlObjectPtr(xmlObject);
-        doFilters(*xmlObject);
-        xmlObjectPtr->releaseThisAndChildrenDOM();
-        xmlObjectPtr->setDocument(NULL);
-        
-        // Swap it in.
-        bool changed = m_object!=NULL;
-        delete m_object;
-        m_object = xmlObjectPtr.release();
-        index();
-        if (changed)
-            emitChangeEvent();
-        return make_pair(false,(DOMElement*)NULL);
-    }
-    catch (XMLToolingException& e) {
-        Category::getInstance(SAML_LOGCAT".Metadata").error("error while loading metadata: %s", e.what());
-        throw;
+    // Load from source using base class.
+    pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
+
+    // If we own it, wrap it for now.
+    XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
+
+    // Unmarshall objects, binding the document.
+    auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(raw.second, true));
+    docjanitor.release();
+
+    if (!dynamic_cast<const EntitiesDescriptor*>(xmlObject.get()) && !dynamic_cast<const EntityDescriptor*>(xmlObject.get()))
+        throw MetadataException(
+            "Root of metadata instance not recognized: $1", params(1,xmlObject->getElementQName().toString().c_str())
+            );
+
+    if (m_requireValidUntil) {
+        const TimeBoundSAMLObject* tbo = dynamic_cast<const TimeBoundSAMLObject*>(xmlObject.get());
+        if (!tbo || tbo->getValidUntil() == NULL)
+            throw MetadataException("Root of metadata instance does not have validUntil atttribute.");
     }
+
+    // Preprocess the metadata.
+    doFilters(*xmlObject.get());
+    xmlObject->releaseThisAndChildrenDOM();
+    xmlObject->setDocument(NULL);
+
+    // Swap it in.
+    bool changed = m_object!=NULL;
+    delete m_object;
+    m_object = xmlObject.release();
+    index();
+    if (changed)
+        emitChangeEvent();
+    return make_pair(false,(DOMElement*)NULL);
 }
 
 void XMLMetadataProvider::index()