Switch to scoped_ptr/swap pattern
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / XMLMetadataProvider.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * XMLMetadataProvider.cpp
23  *
24  * Supplies metadata from an XML file
25  */
26
27 #include "internal.h"
28 #include "binding/SAMLArtifact.h"
29 #include "saml2/metadata/Metadata.h"
30 #include "saml2/metadata/MetadataFilter.h"
31 #include "saml2/metadata/AbstractMetadataProvider.h"
32 #include "saml2/metadata/DiscoverableMetadataProvider.h"
33
34 #include <fstream>
35 #include <boost/scoped_ptr.hpp>
36 #include <xmltooling/XMLToolingConfig.h>
37 #include <xmltooling/io/HTTPResponse.h>
38 #include <xmltooling/util/DateTime.h>
39 #include <xmltooling/util/NDC.h>
40 #include <xmltooling/util/PathResolver.h>
41 #include <xmltooling/util/ReloadableXMLFile.h>
42 #include <xmltooling/util/Threads.h>
43 #include <xmltooling/validation/ValidatorSuite.h>
44
45 #if defined(OPENSAML_LOG4SHIB)
46 # include <log4shib/NDC.hh>
47 #elif defined(OPENSAML_LOG4CPP)
48 # include <log4cpp/NDC.hh>
49 #endif
50
51 using namespace opensaml::saml2md;
52 using namespace xmltooling::logging;
53 using namespace xmltooling;
54 using namespace boost;
55 using namespace std;
56
57 #if defined (_MSC_VER)
58     #pragma warning( push )
59     #pragma warning( disable : 4250 )
60 #endif
61
62 namespace opensaml {
63     namespace saml2md {
64
65         class SAML_DLLLOCAL XMLMetadataProvider
66             : public AbstractMetadataProvider, public DiscoverableMetadataProvider, public ReloadableXMLFile
67         {
68         public:
69             XMLMetadataProvider(const DOMElement* e);
70
71             virtual ~XMLMetadataProvider() {
72                 shutdown();
73             }
74
75             void init() {
76                 try {
77                     if (!m_id.empty()) {
78                         string threadid("[");
79                         threadid += m_id + ']';
80                         logging::NDC::push(threadid);
81                     }
82                     background_load();
83                     startup();
84                 }
85                 catch (...) {
86                     startup();
87                     if (!m_id.empty()) {
88                         logging::NDC::pop();
89                     }
90                     throw;
91                 }
92
93                 if (!m_id.empty()) {
94                     logging::NDC::pop();
95                 }
96             }
97
98             const char* getId() const {
99                 return m_id.c_str();
100             }
101
102             void outputStatus(ostream& os) const {
103                 os << "<MetadataProvider";
104
105                 if (getId() && *getId()) {
106                     os << " id='" << getId() << "'";
107                 }
108
109                 if (!m_source.empty()) {
110                     os << " source='" << m_source << "'";
111                 }
112
113                 if (m_lastUpdate > 0) {
114                     DateTime ts(m_lastUpdate);
115                     ts.parseDateTime();
116                     auto_ptr_char timestamp(ts.getFormattedString());
117                     os << " lastUpdate='" << timestamp.get() << "'";
118                 }
119
120                 if (!m_local && m_reloadInterval > 0) {
121                     os << " reloadInterval='" << m_reloadInterval << "'";
122                 }
123
124                 os << "/>";
125             }
126
127             const XMLObject* getMetadata() const {
128                 return m_object.get();
129             }
130
131         protected:
132             pair<bool,DOMElement*> load(bool backup);
133             pair<bool,DOMElement*> background_load();
134
135         private:
136             using AbstractMetadataProvider::index;
137             void index(time_t& validUntil);
138             time_t computeNextRefresh();
139
140             scoped_ptr<XMLObject> m_object;
141             bool m_discoveryFeed;
142             double m_refreshDelayFactor;
143             unsigned int m_backoffFactor;
144             time_t m_minRefreshDelay,m_maxRefreshDelay,m_lastValidUntil;
145         };
146
147         MetadataProvider* SAML_DLLLOCAL XMLMetadataProviderFactory(const DOMElement* const & e)
148         {
149             return new XMLMetadataProvider(e);
150         }
151
152         static const XMLCh discoveryFeed[] =        UNICODE_LITERAL_13(d,i,s,c,o,v,e,r,y,F,e,e,d);
153         static const XMLCh minRefreshDelay[] =      UNICODE_LITERAL_15(m,i,n,R,e,f,r,e,s,h,D,e,l,a,y);
154         static const XMLCh refreshDelayFactor[] =   UNICODE_LITERAL_18(r,e,f,r,e,s,h,D,e,l,a,y,F,a,c,t,o,r);
155     };
156 };
157
158 #if defined (_MSC_VER)
159     #pragma warning( pop )
160 #endif
161
162 XMLMetadataProvider::XMLMetadataProvider(const DOMElement* e)
163     : MetadataProvider(e), AbstractMetadataProvider(e), DiscoverableMetadataProvider(e),
164         ReloadableXMLFile(e, Category::getInstance(SAML_LOGCAT".MetadataProvider.XML"), false),
165         m_discoveryFeed(XMLHelper::getAttrBool(e, true, discoveryFeed)),
166         m_refreshDelayFactor(0.75), m_backoffFactor(1),
167         m_minRefreshDelay(XMLHelper::getAttrInt(e, 600, minRefreshDelay)),
168         m_maxRefreshDelay(m_reloadInterval), m_lastValidUntil(SAMLTIME_MAX)
169 {
170     if (!m_local && m_maxRefreshDelay) {
171         const XMLCh* setting = e->getAttributeNS(nullptr, refreshDelayFactor);
172         if (setting && *setting) {
173             auto_ptr_char delay(setting);
174             m_refreshDelayFactor = atof(delay.get());
175             if (m_refreshDelayFactor <= 0.0 || m_refreshDelayFactor >= 1.0) {
176                 m_log.error("invalid refreshDelayFactor setting, using default");
177                 m_refreshDelayFactor = 0.75;
178             }
179         }
180
181         if (m_minRefreshDelay > m_maxRefreshDelay) {
182             m_log.warn("minRefreshDelay setting exceeds maxRefreshDelay/reloadInterval setting, lowering to match it");
183             m_minRefreshDelay = m_maxRefreshDelay;
184         }
185     }
186 }
187
188 pair<bool,DOMElement*> XMLMetadataProvider::load(bool backup)
189 {
190     if (!backup) {
191         // Lower the refresh rate in case of an error.
192         m_reloadInterval = m_minRefreshDelay;
193     }
194
195     // Call the base class to load/parse the appropriate XML resource.
196     pair<bool,DOMElement*> raw = ReloadableXMLFile::load(backup);
197
198     // If we own it, wrap it for now.
199     XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : nullptr);
200
201     // Unmarshall objects, binding the document.
202     scoped_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(raw.second, true));
203     docjanitor.release();
204
205     if (!dynamic_cast<const EntitiesDescriptor*>(xmlObject.get()) && !dynamic_cast<const EntityDescriptor*>(xmlObject.get()))
206         throw MetadataException(
207             "Root of metadata instance not recognized: $1", params(1,xmlObject->getElementQName().toString().c_str())
208             );
209
210     // Preprocess the metadata (even if we schema-validated).
211     try {
212         SchemaValidators.validate(xmlObject.get());
213     }
214     catch (exception& ex) {
215         m_log.error("metadata intance failed manual validation checking: %s", ex.what());
216         throw MetadataException("Metadata instance failed manual validation checking.");
217     }
218
219     // This is the best place to take a backup, since it's superficially "correct" metadata.
220     string backupKey;
221     if (!backup && !m_backing.empty()) {
222         // We compute a random filename extension to the "real" location.
223         SAMLConfig::getConfig().generateRandomBytes(backupKey, 2);
224         backupKey = m_backing + '.' + SAMLArtifact::toHex(backupKey);
225         m_log.debug("backing up remote metadata resource to (%s)", backupKey.c_str());
226         try {
227             ofstream backer(backupKey.c_str());
228             backer << *(raw.second->getOwnerDocument());
229         }
230         catch (exception& ex) {
231             m_log.crit("exception while backing up metadata: %s", ex.what());
232             backupKey.erase();
233         }
234     }
235
236     try {
237         doFilters(*xmlObject);
238     }
239     catch (exception&) {
240         if (!backupKey.empty())
241             remove(backupKey.c_str());
242         throw;
243     }
244
245     if (!backupKey.empty()) {
246         m_log.debug("committing backup file to permanent location (%s)", m_backing.c_str());
247         Locker locker(getBackupLock());
248         remove(m_backing.c_str());
249         if (rename(backupKey.c_str(), m_backing.c_str()) != 0)
250             m_log.crit("unable to rename metadata backup file");
251         preserveCacheTag();
252     }
253
254     xmlObject->releaseThisAndChildrenDOM();
255     xmlObject->setDocument(nullptr);
256
257     // Swap it in after acquiring write lock if necessary.
258     if (m_lock)
259         m_lock->wrlock();
260     SharedLock locker(m_lock, false);
261     bool changed = m_object!=nullptr;
262     m_object.swap(xmlObject);
263     m_lastValidUntil = SAMLTIME_MAX;
264     index(m_lastValidUntil);
265     if (m_discoveryFeed)
266         generateFeed();
267     if (changed)
268         emitChangeEvent();
269     m_lastUpdate = time(nullptr);
270
271     // Tracking cacheUntil through the tree is TBD, but
272     // validUntil is the tightest interval amongst the children.
273
274     // If a remote resource that's monitored, adjust the reload interval.
275     if (!backup && !m_local && m_lock) {
276         m_backoffFactor = 1;
277         m_reloadInterval = computeNextRefresh();
278         m_log.info("adjusted reload interval to %d seconds", m_reloadInterval);
279     }
280
281     m_loaded = true;
282     return make_pair(false,(DOMElement*)nullptr);
283 }
284
285 pair<bool,DOMElement*> XMLMetadataProvider::background_load()
286 {
287     try {
288         return load(false);
289     }
290     catch (long& ex) {
291         if (ex == HTTPResponse::XMLTOOLING_HTTP_STATUS_NOTMODIFIED) {
292             // Unchanged document, so re-establish previous refresh interval.
293             m_reloadInterval = computeNextRefresh();
294             m_log.info("remote resource (%s) unchanged, adjusted reload interval to %u seconds", m_source.c_str(), m_reloadInterval);
295         }
296         else {
297             // Any other status code, just treat as an error.
298             m_reloadInterval = m_minRefreshDelay * m_backoffFactor++;
299             if (m_reloadInterval > m_maxRefreshDelay)
300                 m_reloadInterval = m_maxRefreshDelay;
301             m_log.warn("adjusted reload interval to %u seconds", m_reloadInterval);
302         }
303         if (!m_loaded && !m_backing.empty())
304             return load(true);
305         throw;
306     }
307     catch (exception&) {
308         if (!m_local) {
309             m_reloadInterval = m_minRefreshDelay * m_backoffFactor++;
310             if (m_reloadInterval > m_maxRefreshDelay)
311                 m_reloadInterval = m_maxRefreshDelay;
312             m_log.warn("adjusted reload interval to %u seconds", m_reloadInterval);
313             if (!m_loaded && !m_backing.empty())
314                 return load(true);
315         }
316         throw;
317     }
318 }
319
320 time_t XMLMetadataProvider::computeNextRefresh()
321 {
322     time_t now = time(nullptr);
323
324     // If some or all of the metadata is already expired, reload after the minimum.
325     if (m_lastValidUntil < now) {
326         return m_minRefreshDelay;
327     }
328     else {
329         // Compute the smaller of the validUntil / cacheDuration constraints.
330         time_t ret = m_lastValidUntil - now;
331         const CacheableSAMLObject* cacheable = dynamic_cast<const CacheableSAMLObject*>(m_object.get());
332         if (cacheable && cacheable->getCacheDuration())
333             ret = min(ret, cacheable->getCacheDurationEpoch());
334             
335         // Adjust for the delay factor.
336         ret *= m_refreshDelayFactor;
337
338         // Bound by max and min.
339         if (ret > m_maxRefreshDelay)
340             return m_maxRefreshDelay;
341         else if (ret < m_minRefreshDelay)
342             return m_minRefreshDelay;
343
344         return ret;
345     }
346 }
347
348 void XMLMetadataProvider::index(time_t& validUntil)
349 {
350     clearDescriptorIndex();
351     EntitiesDescriptor* group = dynamic_cast<EntitiesDescriptor*>(m_object.get());
352     if (group) {
353         indexGroup(group, validUntil);
354         return;
355     }
356     indexEntity(dynamic_cast<EntityDescriptor*>(m_object.get()), validUntil);
357 }