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 * @file ReloadableXMLFile.cpp
20 * Base class for file-based XML configuration.
24 #include "io/HTTPResponse.h"
25 #ifndef XMLTOOLING_LITE
26 # include "security/Credential.h"
27 # include "security/CredentialCriteria.h"
28 # include "security/CredentialResolver.h"
29 # include "security/SignatureTrustEngine.h"
30 # include "signature/Signature.h"
31 # include "signature/SignatureValidator.h"
34 #include "util/PathResolver.h"
35 #include "util/ReloadableXMLFile.h"
36 #include "util/Threads.h"
37 #include "util/XMLConstants.h"
38 #include "util/XMLHelper.h"
40 #if defined(XMLTOOLING_LOG4SHIB)
41 # include <log4shib/NDC.hh>
42 #elif defined(XMLTOOLING_LOG4CPP)
43 # include <log4cpp/NDC.hh>
48 #include <sys/types.h>
51 #include <xercesc/framework/LocalFileInputSource.hpp>
52 #include <xercesc/framework/Wrapper4InputSource.hpp>
53 #include <xercesc/util/XMLUniDefs.hpp>
55 #ifndef XMLTOOLING_LITE
56 # include <xsec/dsig/DSIGReference.hpp>
57 # include <xsec/dsig/DSIGTransformList.hpp>
58 using namespace xmlsignature;
61 using namespace xmltooling::logging;
62 using namespace xmltooling;
63 using namespace xercesc;
66 #ifndef XMLTOOLING_LITE
68 class XMLTOOL_DLLLOCAL DummyCredentialResolver : public CredentialResolver
71 DummyCredentialResolver() {}
72 ~DummyCredentialResolver() {}
74 Lockable* lock() {return this;}
77 const Credential* resolve(const CredentialCriteria* criteria=nullptr) const {return nullptr;}
78 vector<const Credential*>::size_type resolve(
79 vector<const Credential*>& results, const CredentialCriteria* criteria=nullptr
85 static const XMLCh id[] = UNICODE_LITERAL_2(i,d);
86 static const XMLCh uri[] = UNICODE_LITERAL_3(u,r,i);
87 static const XMLCh url[] = UNICODE_LITERAL_3(u,r,l);
88 static const XMLCh path[] = UNICODE_LITERAL_4(p,a,t,h);
89 static const XMLCh pathname[] = UNICODE_LITERAL_8(p,a,t,h,n,a,m,e);
90 static const XMLCh file[] = UNICODE_LITERAL_4(f,i,l,e);
91 static const XMLCh filename[] = UNICODE_LITERAL_8(f,i,l,e,n,a,m,e);
92 static const XMLCh validate[] = UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);
93 static const XMLCh reloadChanges[] = UNICODE_LITERAL_13(r,e,l,o,a,d,C,h,a,n,g,e,s);
94 static const XMLCh reloadInterval[] = UNICODE_LITERAL_14(r,e,l,o,a,d,I,n,t,e,r,v,a,l);
95 static const XMLCh maxRefreshDelay[] = UNICODE_LITERAL_15(m,a,x,R,e,f,r,e,s,h,D,e,l,a,y);
96 static const XMLCh backingFilePath[] = UNICODE_LITERAL_15(b,a,c,k,i,n,g,F,i,l,e,P,a,t,h);
97 static const XMLCh type[] = UNICODE_LITERAL_4(t,y,p,e);
98 static const XMLCh certificate[] = UNICODE_LITERAL_11(c,e,r,t,i,f,i,c,a,t,e);
99 static const XMLCh signerName[] = UNICODE_LITERAL_10(s,i,g,n,e,r,N,a,m,e);
100 static const XMLCh _TrustEngine[] = UNICODE_LITERAL_11(T,r,u,s,t,E,n,g,i,n,e);
101 static const XMLCh _CredentialResolver[] = UNICODE_LITERAL_18(C,r,e,d,e,n,t,i,a,l,R,e,s,o,l,v,e,r);
104 ReloadableXMLFile::ReloadableXMLFile(const DOMElement* e, Category& log, bool startReloadThread)
105 : m_root(e), m_local(true), m_validate(false), m_filestamp(0), m_reloadInterval(0),
106 m_lock(nullptr), m_log(log), m_loaded(false),
107 #ifndef XMLTOOLING_LITE
108 m_credResolver(nullptr), m_trust(nullptr),
110 m_shutdown(false), m_reload_wait(nullptr), m_reload_thread(nullptr)
113 NDC ndc("ReloadableXMLFile");
116 // Establish source of data...
117 const XMLCh* source=e->getAttributeNS(nullptr,uri);
118 if (!source || !*source) {
119 source=e->getAttributeNS(nullptr,url);
120 if (!source || !*source) {
121 source=e->getAttributeNS(nullptr,path);
122 if (!source || !*source) {
123 source=e->getAttributeNS(nullptr,pathname);
124 if (!source || !*source) {
125 source=e->getAttributeNS(nullptr,file);
126 if (!source || !*source) {
127 source=e->getAttributeNS(nullptr,filename);
140 if (source && *source) {
141 m_validate = XMLHelper::getAttrBool(e, false, validate);
143 auto_ptr_char temp(source);
144 m_source = temp.get();
146 if (!m_local && !strstr(m_source.c_str(),"://")) {
147 log.warn("deprecated usage of uri/url attribute for a local resource, use path instead");
151 #ifndef XMLTOOLING_LITE
152 // Check for signature bits.
153 if (e->hasAttributeNS(nullptr, certificate)) {
154 // Use a file-based credential resolver rooted here.
155 m_credResolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(FILESYSTEM_CREDENTIAL_RESOLVER, e);
158 const DOMElement* sub = XMLHelper::getFirstChildElement(e, _CredentialResolver);
159 string t(XMLHelper::getAttrString(sub, nullptr, type));
161 m_credResolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(t.c_str(), sub);
164 sub = XMLHelper::getFirstChildElement(e, _TrustEngine);
165 t = XMLHelper::getAttrString(sub, nullptr, type);
167 TrustEngine* trust = XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(t.c_str(), sub);
168 if (!(m_trust = dynamic_cast<SignatureTrustEngine*>(trust))) {
170 throw XMLToolingException("TrustEngine-based ReloadableXMLFile requires a SignatureTrustEngine plugin.");
173 m_signerName = XMLHelper::getAttrString(e, nullptr, signerName);
180 XMLToolingConfig::getConfig().getPathResolver()->resolve(m_source, PathResolver::XMLTOOLING_CFG_FILE);
182 bool flag = XMLHelper::getAttrBool(e, true, reloadChanges);
185 struct _stat stat_buf;
186 if (_stat(m_source.c_str(), &stat_buf) == 0)
188 struct stat stat_buf;
189 if (stat(m_source.c_str(), &stat_buf) == 0)
191 m_filestamp = stat_buf.st_mtime;
193 throw IOException("Unable to access local file ($1)", params(1,m_source.c_str()));
194 m_lock = RWLock::create();
196 FILE* cfile = fopen(m_source.c_str(), "r");
200 throw IOException("Unable to access local file ($1)", params(1,m_source.c_str()));
201 log.debug("using local resource (%s), will %smonitor for changes", m_source.c_str(), m_lock ? "" : "not ");
204 log.debug("using remote resource (%s)", m_source.c_str());
205 m_backing = XMLHelper::getAttrString(e, nullptr, backingFilePath);
206 if (!m_backing.empty()) {
207 XMLToolingConfig::getConfig().getPathResolver()->resolve(m_backing, PathResolver::XMLTOOLING_RUN_FILE);
208 log.debug("backup remote resource to (%s)", m_backing.c_str());
210 string tagname = m_backing + ".tag";
211 ifstream backer(tagname.c_str());
214 if (backer.getline(cachebuf, 255)) {
215 m_cacheTag = cachebuf;
216 log.debug("loaded initial cache tag (%s)", m_cacheTag.c_str());
223 m_reloadInterval = XMLHelper::getAttrInt(e, 0, reloadInterval);
224 if (m_reloadInterval == 0)
225 m_reloadInterval = XMLHelper::getAttrInt(e, 0, maxRefreshDelay);
226 if (m_reloadInterval > 0) {
227 m_log.debug("will reload remote resource at most every %d seconds", m_reloadInterval);
228 m_lock = RWLock::create();
230 m_filestamp = time(nullptr); // assume it gets loaded initially
233 if (startReloadThread)
237 log.debug("no resource uri/path/name supplied, will load inline configuration");
240 m_id = XMLHelper::getAttrString(e, nullptr, id);
243 ReloadableXMLFile::~ReloadableXMLFile()
249 void ReloadableXMLFile::startup()
251 if (m_lock && !m_reload_thread) {
252 m_reload_wait = CondWait::create();
253 m_reload_thread = Thread::create(&reload_fn, this);
257 void ReloadableXMLFile::shutdown()
259 if (m_reload_thread) {
260 // Shut down the reload thread and let it know.
262 m_reload_wait->signal();
263 m_reload_thread->join(nullptr);
264 delete m_reload_thread;
265 delete m_reload_wait;
266 m_reload_thread = nullptr;
267 m_reload_wait = nullptr;
271 void* ReloadableXMLFile::reload_fn(void* pv)
273 ReloadableXMLFile* r = reinterpret_cast<ReloadableXMLFile*>(pv);
276 // First, let's block all signals
277 Thread::mask_all_signals();
280 if (!r->m_id.empty()) {
281 string threadid("[");
282 threadid += r->m_id + ']';
283 logging::NDC::push(threadid);
290 auto_ptr<Mutex> mutex(Mutex::create());
294 r->m_log.info("reload thread started...running when signaled");
296 r->m_log.info("reload thread started...running every %d seconds", r->m_reloadInterval);
298 while (!r->m_shutdown) {
300 r->m_reload_wait->wait(mutex.get());
302 r->m_reload_wait->timedwait(mutex.get(), r->m_reloadInterval);
307 r->m_log.info("reloading %s resource...", r->m_local ? "local" : "remote");
308 pair<bool,DOMElement*> ret = r->background_load();
310 ret.second->getOwnerDocument()->release();
313 if (ex == HTTPResponse::XMLTOOLING_HTTP_STATUS_NOTMODIFIED) {
314 r->m_log.info("remote resource (%s) unchanged from cached version", r->m_source.c_str());
317 // Shouldn't happen, we should only get codes intended to be gracefully handled.
318 r->m_log.crit("maintaining existing configuration, remote resource fetch returned atypical status code (%d)", ex);
321 catch (exception& ex) {
322 r->m_log.crit("maintaining existing configuration, error reloading resource (%s): %s", r->m_source.c_str(), ex.what());
326 r->m_log.info("reload thread finished");
330 if (!r->m_id.empty()) {
337 Lockable* ReloadableXMLFile::lock()
345 // Check if we need to refresh.
347 struct _stat stat_buf;
348 if (_stat(m_source.c_str(), &stat_buf) != 0)
351 struct stat stat_buf;
352 if (stat(m_source.c_str(), &stat_buf) != 0)
355 if (m_filestamp >= stat_buf.st_mtime)
358 // Elevate lock and recheck.
359 m_log.debug("timestamp of local resource changed, elevating to a write lock");
362 if (m_filestamp >= stat_buf.st_mtime) {
363 // Somebody else handled it, just downgrade.
364 m_log.debug("update of local resource handled by another thread, downgrading lock");
370 // Update the timestamp regardless.
371 m_filestamp = stat_buf.st_mtime;
373 m_log.info("change detected, signaling reload thread...");
374 m_reload_wait->signal();
377 m_log.warn("change detected, but reload thread not started");
384 void ReloadableXMLFile::unlock()
390 pair<bool,DOMElement*> ReloadableXMLFile::load(bool backup)
397 if (m_source.empty()) {
398 // Data comes from the DOM we were handed.
399 m_log.debug("loading inline configuration...");
400 return make_pair(false, XMLHelper::getFirstChildElement(m_root));
403 // Data comes from a file we have to parse.
405 m_log.info("using local backup of remote resource");
407 m_log.debug("loading configuration from external resource...");
409 DOMDocument* doc=nullptr;
410 if (m_local || backup) {
411 auto_ptr_XMLCh widenit(backup ? m_backing.c_str() : m_source.c_str());
412 // Use library-wide lock for now, nothing else is using it anyway.
413 Locker locker(backup ? getBackupLock() : nullptr);
414 LocalFileInputSource src(widenit.get());
415 Wrapper4InputSource dsrc(&src, false);
417 doc=XMLToolingConfig::getConfig().getValidatingParser().parse(dsrc);
419 doc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
422 URLInputSource src(m_root, nullptr, &m_cacheTag);
423 Wrapper4InputSource dsrc(&src, false);
425 doc=XMLToolingConfig::getConfig().getValidatingParser().parse(dsrc);
427 doc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
429 // Check for a response code signal.
430 if (XMLHelper::isNodeNamed(doc->getDocumentElement(), xmlconstants::XMLTOOLING_NS, URLInputSource::utf16StatusCodeElementName)) {
431 int responseCode = XMLString::parseInt(doc->getDocumentElement()->getFirstChild()->getNodeValue());
433 if (responseCode == HTTPResponse::XMLTOOLING_HTTP_STATUS_NOTMODIFIED)
434 throw (long)responseCode; // toss out as a "known" case to handle gracefully
436 m_log.warn("remote resource fetch returned atypical status code (%d)", responseCode);
437 throw IOException("remote resource fetch failed, check log for status code of response");
442 m_log.infoStream() << "loaded XML resource (" << (backup ? m_backing : m_source) << ")" << logging::eol;
443 #ifndef XMLTOOLING_LITE
444 if (m_credResolver || m_trust) {
445 m_log.debug("checking signature on XML resource");
447 DOMElement* sigel = XMLHelper::getFirstChildElement(doc->getDocumentElement(), xmlconstants::XMLSIG_NS, Signature::LOCAL_NAME);
449 throw XMLSecurityException("Signature validation required, but no signature found.");
451 // Wrap and unmarshall the signature for the duration of the check.
452 auto_ptr<Signature> sigobj(dynamic_cast<Signature*>(SignatureBuilder::buildOneFromElement(sigel))); // don't bind to document
453 validateSignature(*sigobj.get());
462 return make_pair(true, doc->getDocumentElement());
465 catch (XMLException& e) {
466 auto_ptr_char msg(e.getMessage());
467 m_log.errorStream() << "Xerces error while loading resource (" << (backup ? m_backing : m_source) << "): "
468 << msg.get() << logging::eol;
469 throw XMLParserException(msg.get());
471 catch (exception& e) {
472 m_log.errorStream() << "error while loading resource ("
473 << (m_source.empty() ? "inline" : (backup ? m_backing : m_source)) << "): " << e.what() << logging::eol;
478 pair<bool,DOMElement*> ReloadableXMLFile::load()
480 // If this method is used, we're responsible for managing failover to a
481 // backup of a remote resource (if available), and for backing up remote
484 pair<bool,DOMElement*> ret = load(false);
485 if (!m_backing.empty()) {
486 m_log.debug("backing up remote resource to (%s)", m_backing.c_str());
488 Locker locker(getBackupLock());
489 ofstream backer(m_backing.c_str());
490 backer << *(ret.second->getOwnerDocument());
493 catch (exception& ex) {
494 m_log.crit("exception while backing up resource: %s", ex.what());
499 catch (long& responseCode) {
500 // If there's an HTTP error or the document hasn't changed,
501 // use the backup iff we have no "valid" resource in place.
502 // That prevents reload of the backup copy any time the document
504 if (responseCode == HTTPResponse::XMLTOOLING_HTTP_STATUS_NOTMODIFIED)
505 m_log.info("remote resource (%s) unchanged from cached version", m_source.c_str());
506 if (!m_loaded && !m_backing.empty())
511 // Same as above, but for general load/parse errors.
512 if (!m_loaded && !m_backing.empty())
518 pair<bool,DOMElement*> ReloadableXMLFile::background_load()
520 // If this method isn't overridden, we acquire a write lock
521 // and just call the old override.
524 SharedLock locker(m_lock, false);
528 Lockable* ReloadableXMLFile::getBackupLock()
530 return &XMLToolingConfig::getConfig();
533 void ReloadableXMLFile::preserveCacheTag()
535 if (!m_cacheTag.empty() && !m_backing.empty()) {
537 string tagname = m_backing + ".tag";
538 ofstream backer(tagname.c_str());
539 backer << m_cacheTag;
546 #ifndef XMLTOOLING_LITE
548 void ReloadableXMLFile::validateSignature(Signature& sigObj) const
550 DSIGSignature* sig=sigObj.getXMLSignature();
552 throw XMLSecurityException("Signature does not exist yet.");
554 // Make sure the whole document was signed.
556 DSIGReferenceList* refs=sig->getReferenceList();
557 if (refs && refs->getSize()==1) {
558 DSIGReference* ref=refs->item(0);
560 const XMLCh* URI=ref->getURI();
561 if (URI==nullptr || *URI==0) {
562 DSIGTransformList* tlist=ref->getTransforms();
563 if (tlist->getSize() <= 2) {
564 for (unsigned int i=0; tlist && i<tlist->getSize(); i++) {
565 if (tlist->item(i)->getTransformType()==TRANSFORM_ENVELOPED_SIGNATURE)
567 else if (tlist->item(i)->getTransformType()!=TRANSFORM_EXC_C14N &&
568 tlist->item(i)->getTransformType()!=TRANSFORM_C14N
569 #ifdef XMLTOOLING_XMLSEC_C14N11
570 && tlist->item(i)->getTransformType()!=TRANSFORM_C14N11
583 throw XMLSecurityException("Invalid signature profile for signed configuration resource.");
586 CredentialCriteria cc;
587 cc.setUsage(Credential::SIGNING_CREDENTIAL);
588 cc.setSignature(sigObj, CredentialCriteria::KEYINFO_EXTRACTION_KEY);
589 if (!m_signerName.empty())
590 cc.setPeerName(m_signerName.c_str());
592 if (m_credResolver) {
593 Locker locker(m_credResolver);
594 vector<const Credential*> creds;
595 if (m_credResolver->resolve(creds, &cc)) {
596 SignatureValidator sigValidator;
597 for (vector<const Credential*>::const_iterator i = creds.begin(); i != creds.end(); ++i) {
599 sigValidator.setCredential(*i);
600 sigValidator.validate(&sigObj);
606 throw XMLSecurityException("Unable to verify signature with supplied key(s).");
609 throw XMLSecurityException("CredentialResolver did not supply any candidate keys.");
613 DummyCredentialResolver dummy;
614 if (m_trust->validate(sigObj, dummy, &cc))
616 throw XMLSecurityException("TrustEngine unable to verify signature.");
619 throw XMLSecurityException("Unable to verify signature.");