Fix race condition during shutdown.
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / XMLMetadataProvider.cpp
index d47a1d4..5462564 100644 (file)
@@ -1,6 +1,6 @@
 /*
- *  Copyright 2001-2006 Internet2
- * 
+ *  Copyright 2001-2010 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 "binding/SAMLArtifact.h"
 #include "saml2/metadata/Metadata.h"
+#include "saml2/metadata/MetadataFilter.h"
 #include "saml2/metadata/AbstractMetadataProvider.h"
 
-#include <log4cpp/Category.hh>
+#include <fstream>
 #include <xmltooling/util/NDC.h>
 #include <xmltooling/util/ReloadableXMLFile.h>
-#include <xmltooling/util/XMLConstants.h>
+#include <xmltooling/util/Threads.h>
+#include <xmltooling/validation/ValidatorSuite.h>
 
 using namespace opensaml::saml2md;
+using namespace xmltooling::logging;
 using namespace xmltooling;
-using namespace log4cpp;
 using namespace std;
 
 #if defined (_MSC_VER)
@@ -45,26 +48,33 @@ namespace opensaml {
         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_maxCacheDuration(m_reloadInterval) {
+            }
             virtual ~XMLMetadataProvider() {
+                shutdown();
                 delete m_object;
             }
 
             void init() {
-                load(); // guarantees an exception or the metadata is loaded
+                background_load(); // guarantees an exception or the metadata is loaded
             }
-            
-            pair<bool,DOMElement*> load();
 
             const XMLObject* getMetadata() const {
                 return m_object;
             }
 
+        protected:
+            pair<bool,DOMElement*> background_load();
+
         private:
+            using AbstractMetadataProvider::index;
             void index();
-        
+
             XMLObject* m_object;
-        }; 
+            time_t m_maxCacheDuration;
+        };
 
         MetadataProvider* SAML_DLLLOCAL XMLMetadataProviderFactory(const DOMElement* const & e)
         {
@@ -78,42 +88,94 @@ namespace opensaml {
     #pragma warning( pop )
 #endif
 
-pair<bool,DOMElement*> XMLMetadataProvider::load()
+pair<bool,DOMElement*> XMLMetadataProvider::background_load()
 {
-#ifdef _DEBUG
-    NDC ndc("load");
-#endif
-    
+    // Turn off auto-backup so we can filter first.
+    m_backupIndicator = false;
+
+    // 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())
+            );
+
+    // Preprocess the metadata (even if we schema-validated).
+    try {
+        SchemaValidators.validate(xmlObject.get());
+    }
+    catch (exception& ex) {
+        m_log.error("metadata intance failed manual validation checking: %s", ex.what());
+        throw MetadataException("Metadata instance failed manual validation checking.");
+    }
+
+    // If the backup indicator is flipped, then this was a remote load and we need a backup.
+    // This is the best place to take a backup, since it's superficially "correct" metadata.
+    string backupKey;
+    if (m_backupIndicator) {
+        // We compute a random filename extension to the "real" location.
+        SAMLConfig::getConfig().generateRandomBytes(backupKey, 2);
+        backupKey = m_backing + '.' + SAMLArtifact::toHex(backupKey);
+        m_log.debug("backing up remote metadata resource to (%s)", backupKey.c_str());
+        try {
+            ofstream backer(backupKey.c_str());
+            backer << *raw.second->getOwnerDocument();
+        }
+        catch (exception& ex) {
+            m_log.crit("exception while backing up metadata: %s", ex.what());
+            backupKey.erase();
+        }
+    }
+
     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);
+        doFilters(*xmlObject.get());
     }
-    catch (XMLToolingException& e) {
-        Category::getInstance(SAML_LOGCAT".Metadata").error("error while loading metadata: %s", e.what());
+    catch (exception&) {
+        if (!backupKey.empty())
+            remove(backupKey.c_str());
         throw;
     }
+
+    if (!backupKey.empty()) {
+        m_log.debug("committing backup file to permanent location (%s)", m_backing.c_str());
+        Locker locker(getBackupLock());
+        remove(m_backing.c_str());
+        if (rename(backupKey.c_str(), m_backing.c_str()) != 0)
+            m_log.crit("unable to rename metadata backup file");
+    }
+
+    xmlObject->releaseThisAndChildrenDOM();
+    xmlObject->setDocument(NULL);
+
+    // Swap it in after acquiring write lock if necessary.
+    if (m_lock)
+        m_lock->wrlock();
+    SharedLock locker(m_lock, false);
+    bool changed = m_object!=NULL;
+    delete m_object;
+    m_object = xmlObject.release();
+    index();
+    if (changed)
+        emitChangeEvent();
+
+    // If a remote resource, adjust the reload interval if cacheDuration is set.
+    if (!m_local) {
+        const CacheableSAMLObject* cacheable = dynamic_cast<const CacheableSAMLObject*>(m_object);
+        if (cacheable && cacheable->getCacheDuration() && cacheable->getCacheDurationEpoch() < m_maxCacheDuration)
+            m_reloadInterval = cacheable->getCacheDurationEpoch();
+        else
+            m_reloadInterval = m_maxCacheDuration;
+    }
+
+    return make_pair(false,(DOMElement*)NULL);
 }
 
 void XMLMetadataProvider::index()
@@ -124,6 +186,5 @@ void XMLMetadataProvider::index()
         AbstractMetadataProvider::index(group, SAMLTIME_MAX);
         return;
     }
-    EntityDescriptor* site=dynamic_cast<EntityDescriptor*>(m_object);
-    AbstractMetadataProvider::index(site, SAMLTIME_MAX);
+    AbstractMetadataProvider::index(dynamic_cast<EntityDescriptor*>(m_object), SAMLTIME_MAX);
 }