2 * Copyright 2001-2010 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * XMLMetadataProvider.cpp
20 * Supplies metadata from an XML file
24 #include "binding/SAMLArtifact.h"
25 #include "saml2/metadata/Metadata.h"
26 #include "saml2/metadata/MetadataFilter.h"
27 #include "saml2/metadata/AbstractMetadataProvider.h"
30 #include <xmltooling/util/NDC.h>
31 #include <xmltooling/util/ReloadableXMLFile.h>
32 #include <xmltooling/util/Threads.h>
33 #include <xmltooling/validation/ValidatorSuite.h>
35 using namespace opensaml::saml2md;
36 using namespace xmltooling::logging;
37 using namespace xmltooling;
40 #if defined (_MSC_VER)
41 #pragma warning( push )
42 #pragma warning( disable : 4250 )
48 class SAML_DLLLOCAL XMLMetadataProvider : public AbstractMetadataProvider, public ReloadableXMLFile
51 XMLMetadataProvider(const DOMElement* e)
52 : AbstractMetadataProvider(e), ReloadableXMLFile(e, Category::getInstance(SAML_LOGCAT".MetadataProvider.XML")),
53 m_object(NULL), m_maxCacheDuration(m_reloadInterval) {
55 virtual ~XMLMetadataProvider() {
65 background_load(); // guarantees an exception or the metadata is loaded
68 const XMLObject* getMetadata() const {
73 pair<bool,DOMElement*> background_load();
76 using AbstractMetadataProvider::index;
80 time_t m_maxCacheDuration;
83 MetadataProvider* SAML_DLLLOCAL XMLMetadataProviderFactory(const DOMElement* const & e)
85 return new XMLMetadataProvider(e);
91 #if defined (_MSC_VER)
92 #pragma warning( pop )
95 pair<bool,DOMElement*> XMLMetadataProvider::background_load()
97 // Turn off auto-backup so we can filter first.
98 m_backupIndicator = false;
100 // Load from source using base class.
101 pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
103 // If we own it, wrap it for now.
104 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
106 // Unmarshall objects, binding the document.
107 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(raw.second, true));
108 docjanitor.release();
110 if (!dynamic_cast<const EntitiesDescriptor*>(xmlObject.get()) && !dynamic_cast<const EntityDescriptor*>(xmlObject.get()))
111 throw MetadataException(
112 "Root of metadata instance not recognized: $1", params(1,xmlObject->getElementQName().toString().c_str())
115 // Preprocess the metadata (even if we schema-validated).
117 SchemaValidators.validate(xmlObject.get());
119 catch (exception& ex) {
120 m_log.error("metadata intance failed manual validation checking: %s", ex.what());
121 throw MetadataException("Metadata instance failed manual validation checking.");
124 // If the backup indicator is flipped, then this was a remote load and we need a backup.
125 // This is the best place to take a backup, since it's superficially "correct" metadata.
127 if (m_backupIndicator) {
128 // We compute a random filename extension to the "real" location.
129 SAMLConfig::getConfig().generateRandomBytes(backupKey, 2);
130 backupKey = m_backing + '.' + SAMLArtifact::toHex(backupKey);
131 m_log.debug("backing up remote metadata resource to (%s)", backupKey.c_str());
133 ofstream backer(backupKey.c_str());
134 backer << *raw.second->getOwnerDocument();
136 catch (exception& ex) {
137 m_log.crit("exception while backing up metadata: %s", ex.what());
143 doFilters(*xmlObject.get());
146 if (!backupKey.empty())
147 remove(backupKey.c_str());
151 if (!backupKey.empty()) {
152 m_log.debug("committing backup file to permanent location (%s)", m_backing.c_str());
153 Locker locker(getBackupLock());
154 remove(m_backing.c_str());
155 if (rename(backupKey.c_str(), m_backing.c_str()) != 0)
156 m_log.crit("unable to rename metadata backup file");
159 xmlObject->releaseThisAndChildrenDOM();
160 xmlObject->setDocument(NULL);
162 // Swap it in after acquiring write lock if necessary.
165 SharedLock locker(m_lock, false);
166 bool changed = m_object!=NULL;
168 m_object = xmlObject.release();
173 // If a remote resource, adjust the reload interval if cacheDuration is set.
175 const CacheableSAMLObject* cacheable = dynamic_cast<const CacheableSAMLObject*>(m_object);
176 if (cacheable && cacheable->getCacheDuration() && cacheable->getCacheDurationEpoch() < m_maxCacheDuration)
177 m_reloadInterval = cacheable->getCacheDurationEpoch();
179 m_reloadInterval = m_maxCacheDuration;
182 return make_pair(false,(DOMElement*)NULL);
185 void XMLMetadataProvider::index()
187 clearDescriptorIndex();
188 EntitiesDescriptor* group=dynamic_cast<EntitiesDescriptor*>(m_object);
190 AbstractMetadataProvider::index(group, SAMLTIME_MAX);
193 AbstractMetadataProvider::index(dynamic_cast<EntityDescriptor*>(m_object), SAMLTIME_MAX);