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.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * @file ReloadableXMLFile.cpp
24 * Base class for file-based XML configuration.
28 #include "io/HTTPResponse.h"
29 #ifndef XMLTOOLING_LITE
30 # include "security/Credential.h"
31 # include "security/CredentialCriteria.h"
32 # include "security/CredentialResolver.h"
33 # include "security/SignatureTrustEngine.h"
34 # include "signature/Signature.h"
35 # include "signature/SignatureValidator.h"
38 #include "util/PathResolver.h"
39 #include "util/ReloadableXMLFile.h"
40 #include "util/Threads.h"
41 #include "util/XMLConstants.h"
42 #include "util/XMLHelper.h"
44 #if defined(XMLTOOLING_LOG4SHIB)
45 # include <log4shib/NDC.hh>
46 #elif defined(XMLTOOLING_LOG4CPP)
47 # include <log4cpp/NDC.hh>
52 #include <sys/types.h>
55 #include <xercesc/framework/LocalFileInputSource.hpp>
56 #include <xercesc/framework/Wrapper4InputSource.hpp>
57 #include <xercesc/util/XMLUniDefs.hpp>
59 #ifndef XMLTOOLING_LITE
60 # include <xsec/dsig/DSIGReference.hpp>
61 # include <xsec/dsig/DSIGTransformList.hpp>
62 using namespace xmlsignature;
65 using namespace xmltooling::logging;
66 using namespace xmltooling;
67 using namespace xercesc;
70 #ifndef XMLTOOLING_LITE
72 class XMLTOOL_DLLLOCAL DummyCredentialResolver : public CredentialResolver
75 DummyCredentialResolver() {}
76 ~DummyCredentialResolver() {}
78 Lockable* lock() {return this;}
81 const Credential* resolve(const CredentialCriteria* criteria=nullptr) const {return nullptr;}
82 vector<const Credential*>::size_type resolve(
83 vector<const Credential*>& results, const CredentialCriteria* criteria=nullptr
89 static const XMLCh id[] = UNICODE_LITERAL_2(i,d);
90 static const XMLCh uri[] = UNICODE_LITERAL_3(u,r,i);
91 static const XMLCh url[] = UNICODE_LITERAL_3(u,r,l);
92 static const XMLCh path[] = UNICODE_LITERAL_4(p,a,t,h);
93 static const XMLCh pathname[] = UNICODE_LITERAL_8(p,a,t,h,n,a,m,e);
94 static const XMLCh file[] = UNICODE_LITERAL_4(f,i,l,e);
95 static const XMLCh filename[] = UNICODE_LITERAL_8(f,i,l,e,n,a,m,e);
96 static const XMLCh validate[] = UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);
97 static const XMLCh reloadChanges[] = UNICODE_LITERAL_13(r,e,l,o,a,d,C,h,a,n,g,e,s);
98 static const XMLCh reloadInterval[] = UNICODE_LITERAL_14(r,e,l,o,a,d,I,n,t,e,r,v,a,l);
99 static const XMLCh maxRefreshDelay[] = UNICODE_LITERAL_15(m,a,x,R,e,f,r,e,s,h,D,e,l,a,y);
100 static const XMLCh backingFilePath[] = UNICODE_LITERAL_15(b,a,c,k,i,n,g,F,i,l,e,P,a,t,h);
101 static const XMLCh type[] = UNICODE_LITERAL_4(t,y,p,e);
102 static const XMLCh certificate[] = UNICODE_LITERAL_11(c,e,r,t,i,f,i,c,a,t,e);
103 static const XMLCh signerName[] = UNICODE_LITERAL_10(s,i,g,n,e,r,N,a,m,e);
104 static const XMLCh _TrustEngine[] = UNICODE_LITERAL_11(T,r,u,s,t,E,n,g,i,n,e);
105 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);
108 ReloadableXMLFile::ReloadableXMLFile(const DOMElement* e, Category& log, bool startReloadThread)
109 : m_root(e), m_local(true), m_validate(false), m_filestamp(0), m_reloadInterval(0),
110 m_lock(nullptr), m_log(log), m_loaded(false),
111 #ifndef XMLTOOLING_LITE
112 m_credResolver(nullptr), m_trust(nullptr),
114 m_shutdown(false), m_reload_wait(nullptr), m_reload_thread(nullptr)
117 NDC ndc("ReloadableXMLFile");
120 // Establish source of data...
121 const XMLCh* source=e->getAttributeNS(nullptr,uri);
122 if (!source || !*source) {
123 source=e->getAttributeNS(nullptr,url);
124 if (!source || !*source) {
125 source=e->getAttributeNS(nullptr,path);
126 if (!source || !*source) {
127 source=e->getAttributeNS(nullptr,pathname);
128 if (!source || !*source) {
129 source=e->getAttributeNS(nullptr,file);
130 if (!source || !*source) {
131 source=e->getAttributeNS(nullptr,filename);
144 if (source && *source) {
145 m_validate = XMLHelper::getAttrBool(e, false, validate);
147 auto_ptr_char temp(source);
148 m_source = temp.get();
150 if (!m_local && !strstr(m_source.c_str(),"://")) {
151 log.warn("deprecated usage of uri/url attribute for a local resource, use path instead");
155 #ifndef XMLTOOLING_LITE
156 // Check for signature bits.
157 if (e->hasAttributeNS(nullptr, certificate)) {
158 // Use a file-based credential resolver rooted here.
159 m_credResolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(FILESYSTEM_CREDENTIAL_RESOLVER, e);
162 const DOMElement* sub = XMLHelper::getFirstChildElement(e, _CredentialResolver);
163 string t(XMLHelper::getAttrString(sub, nullptr, type));
165 m_credResolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(t.c_str(), sub);
168 sub = XMLHelper::getFirstChildElement(e, _TrustEngine);
169 t = XMLHelper::getAttrString(sub, nullptr, type);
171 TrustEngine* trust = XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(t.c_str(), sub);
172 if (!(m_trust = dynamic_cast<SignatureTrustEngine*>(trust))) {
174 throw XMLToolingException("TrustEngine-based ReloadableXMLFile requires a SignatureTrustEngine plugin.");
177 m_signerName = XMLHelper::getAttrString(e, nullptr, signerName);
184 XMLToolingConfig::getConfig().getPathResolver()->resolve(m_source, PathResolver::XMLTOOLING_CFG_FILE);
186 bool flag = XMLHelper::getAttrBool(e, true, reloadChanges);
189 struct _stat stat_buf;
190 if (_stat(m_source.c_str(), &stat_buf) == 0)
192 struct stat stat_buf;
193 if (stat(m_source.c_str(), &stat_buf) == 0)
195 m_filestamp = stat_buf.st_mtime;
197 throw IOException("Unable to access local file ($1)", params(1,m_source.c_str()));
198 m_lock = RWLock::create();
200 FILE* cfile = fopen(m_source.c_str(), "r");
204 throw IOException("Unable to access local file ($1)", params(1,m_source.c_str()));
205 log.debug("using local resource (%s), will %smonitor for changes", m_source.c_str(), m_lock ? "" : "not ");
208 log.debug("using remote resource (%s)", m_source.c_str());
209 m_backing = XMLHelper::getAttrString(e, nullptr, backingFilePath);
210 if (!m_backing.empty()) {
211 XMLToolingConfig::getConfig().getPathResolver()->resolve(m_backing, PathResolver::XMLTOOLING_RUN_FILE);
212 log.debug("backup remote resource to (%s)", m_backing.c_str());
214 string tagname = m_backing + ".tag";
215 ifstream backer(tagname.c_str());
218 if (backer.getline(cachebuf, 255)) {
219 m_cacheTag = cachebuf;
220 log.debug("loaded initial cache tag (%s)", m_cacheTag.c_str());
227 m_reloadInterval = XMLHelper::getAttrInt(e, 0, reloadInterval);
228 if (m_reloadInterval == 0)
229 m_reloadInterval = XMLHelper::getAttrInt(e, 0, maxRefreshDelay);
230 if (m_reloadInterval > 0) {
231 m_log.debug("will reload remote resource at most every %d seconds", m_reloadInterval);
232 m_lock = RWLock::create();
234 m_filestamp = time(nullptr); // assume it gets loaded initially
237 if (startReloadThread)
241 log.debug("no resource uri/path/name supplied, will load inline configuration");
244 m_id = XMLHelper::getAttrString(e, nullptr, id);
247 ReloadableXMLFile::~ReloadableXMLFile()
253 void ReloadableXMLFile::startup()
255 if (m_lock && !m_reload_thread) {
256 m_reload_wait = CondWait::create();
257 m_reload_thread = Thread::create(&reload_fn, this);
261 void ReloadableXMLFile::shutdown()
263 if (m_reload_thread) {
264 // Shut down the reload thread and let it know.
266 m_reload_wait->signal();
267 m_reload_thread->join(nullptr);
268 delete m_reload_thread;
269 delete m_reload_wait;
270 m_reload_thread = nullptr;
271 m_reload_wait = nullptr;
275 void* ReloadableXMLFile::reload_fn(void* pv)
277 ReloadableXMLFile* r = reinterpret_cast<ReloadableXMLFile*>(pv);
280 // First, let's block all signals
281 Thread::mask_all_signals();
284 if (!r->m_id.empty()) {
285 string threadid("[");
286 threadid += r->m_id + ']';
287 logging::NDC::push(threadid);
294 auto_ptr<Mutex> mutex(Mutex::create());
298 r->m_log.info("reload thread started...running when signaled");
300 r->m_log.info("reload thread started...running every %d seconds", r->m_reloadInterval);
302 while (!r->m_shutdown) {
304 r->m_reload_wait->wait(mutex.get());
306 r->m_reload_wait->timedwait(mutex.get(), r->m_reloadInterval);
311 r->m_log.info("reloading %s resource...", r->m_local ? "local" : "remote");
312 pair<bool,DOMElement*> ret = r->background_load();
314 ret.second->getOwnerDocument()->release();
317 if (ex == HTTPResponse::XMLTOOLING_HTTP_STATUS_NOTMODIFIED) {
318 r->m_log.info("remote resource (%s) unchanged from cached version", r->m_source.c_str());
321 // Shouldn't happen, we should only get codes intended to be gracefully handled.
322 r->m_log.crit("maintaining existing configuration, remote resource fetch returned atypical status code (%d)", ex);
325 catch (exception& ex) {
326 r->m_log.crit("maintaining existing configuration, error reloading resource (%s): %s", r->m_source.c_str(), ex.what());
330 r->m_log.info("reload thread finished");
334 if (!r->m_id.empty()) {
341 Lockable* ReloadableXMLFile::lock()
349 // Check if we need to refresh.
351 struct _stat stat_buf;
352 if (_stat(m_source.c_str(), &stat_buf) != 0)
355 struct stat stat_buf;
356 if (stat(m_source.c_str(), &stat_buf) != 0)
359 if (m_filestamp >= stat_buf.st_mtime)
362 // Elevate lock and recheck.
363 m_log.debug("timestamp of local resource changed, elevating to a write lock");
366 if (m_filestamp >= stat_buf.st_mtime) {
367 // Somebody else handled it, just downgrade.
368 m_log.debug("update of local resource handled by another thread, downgrading lock");
374 // Update the timestamp regardless.
375 m_filestamp = stat_buf.st_mtime;
377 m_log.info("change detected, signaling reload thread...");
378 m_reload_wait->signal();
381 m_log.warn("change detected, but reload thread not started");
388 void ReloadableXMLFile::unlock()
394 pair<bool,DOMElement*> ReloadableXMLFile::load(bool backup)
401 if (m_source.empty()) {
402 // Data comes from the DOM we were handed.
403 m_log.debug("loading inline configuration...");
404 return make_pair(false, XMLHelper::getFirstChildElement(m_root));
407 // Data comes from a file we have to parse.
409 m_log.info("using local backup of remote resource");
411 m_log.debug("loading configuration from external resource...");
413 DOMDocument* doc=nullptr;
414 if (m_local || backup) {
415 auto_ptr_XMLCh widenit(backup ? m_backing.c_str() : m_source.c_str());
416 // Use library-wide lock for now, nothing else is using it anyway.
417 Locker locker(backup ? getBackupLock() : nullptr);
418 LocalFileInputSource src(widenit.get());
419 Wrapper4InputSource dsrc(&src, false);
421 doc=XMLToolingConfig::getConfig().getValidatingParser().parse(dsrc);
423 doc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
426 URLInputSource src(m_root, nullptr, &m_cacheTag);
427 Wrapper4InputSource dsrc(&src, false);
429 doc=XMLToolingConfig::getConfig().getValidatingParser().parse(dsrc);
431 doc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
433 // Check for a response code signal.
434 if (XMLHelper::isNodeNamed(doc->getDocumentElement(), xmlconstants::XMLTOOLING_NS, URLInputSource::utf16StatusCodeElementName)) {
435 int responseCode = XMLString::parseInt(doc->getDocumentElement()->getFirstChild()->getNodeValue());
437 if (responseCode == HTTPResponse::XMLTOOLING_HTTP_STATUS_NOTMODIFIED)
438 throw (long)responseCode; // toss out as a "known" case to handle gracefully
440 m_log.warn("remote resource fetch returned atypical status code (%d)", responseCode);
441 throw IOException("remote resource fetch failed, check log for status code of response");
446 m_log.infoStream() << "loaded XML resource (" << (backup ? m_backing : m_source) << ")" << logging::eol;
447 #ifndef XMLTOOLING_LITE
448 if (m_credResolver || m_trust) {
449 m_log.debug("checking signature on XML resource");
451 DOMElement* sigel = XMLHelper::getFirstChildElement(doc->getDocumentElement(), xmlconstants::XMLSIG_NS, Signature::LOCAL_NAME);
453 throw XMLSecurityException("Signature validation required, but no signature found.");
455 // Wrap and unmarshall the signature for the duration of the check.
456 auto_ptr<Signature> sigobj(dynamic_cast<Signature*>(SignatureBuilder::buildOneFromElement(sigel))); // don't bind to document
457 validateSignature(*sigobj.get());
466 return make_pair(true, doc->getDocumentElement());
469 catch (XMLException& e) {
470 auto_ptr_char msg(e.getMessage());
471 m_log.errorStream() << "Xerces error while loading resource (" << (backup ? m_backing : m_source) << "): "
472 << msg.get() << logging::eol;
473 throw XMLParserException(msg.get());
475 catch (exception& e) {
476 m_log.errorStream() << "error while loading resource ("
477 << (m_source.empty() ? "inline" : (backup ? m_backing : m_source)) << "): " << e.what() << logging::eol;
482 pair<bool,DOMElement*> ReloadableXMLFile::load()
484 // If this method is used, we're responsible for managing failover to a
485 // backup of a remote resource (if available), and for backing up remote
488 pair<bool,DOMElement*> ret = load(false);
489 if (!m_backing.empty()) {
490 m_log.debug("backing up remote resource to (%s)", m_backing.c_str());
492 Locker locker(getBackupLock());
493 ofstream backer(m_backing.c_str());
494 backer << *(ret.second->getOwnerDocument());
497 catch (exception& ex) {
498 m_log.crit("exception while backing up resource: %s", ex.what());
503 catch (long& responseCode) {
504 // If there's an HTTP error or the document hasn't changed,
505 // use the backup iff we have no "valid" resource in place.
506 // That prevents reload of the backup copy any time the document
508 if (responseCode == HTTPResponse::XMLTOOLING_HTTP_STATUS_NOTMODIFIED)
509 m_log.info("remote resource (%s) unchanged from cached version", m_source.c_str());
510 if (!m_loaded && !m_backing.empty())
515 // Same as above, but for general load/parse errors.
516 if (!m_loaded && !m_backing.empty())
522 pair<bool,DOMElement*> ReloadableXMLFile::background_load()
524 // If this method isn't overridden, we acquire a write lock
525 // and just call the old override.
528 SharedLock locker(m_lock, false);
532 Lockable* ReloadableXMLFile::getBackupLock()
534 return &XMLToolingConfig::getConfig();
537 void ReloadableXMLFile::preserveCacheTag()
539 if (!m_cacheTag.empty() && !m_backing.empty()) {
541 string tagname = m_backing + ".tag";
542 ofstream backer(tagname.c_str());
543 backer << m_cacheTag;
550 #ifndef XMLTOOLING_LITE
552 void ReloadableXMLFile::validateSignature(Signature& sigObj) const
554 DSIGSignature* sig=sigObj.getXMLSignature();
556 throw XMLSecurityException("Signature does not exist yet.");
558 // Make sure the whole document was signed.
560 DSIGReferenceList* refs=sig->getReferenceList();
561 if (refs && refs->getSize()==1) {
562 DSIGReference* ref=refs->item(0);
564 const XMLCh* URI=ref->getURI();
565 if (URI==nullptr || *URI==0) {
566 DSIGTransformList* tlist=ref->getTransforms();
567 if (tlist->getSize() <= 2) {
568 for (unsigned int i=0; tlist && i<tlist->getSize(); i++) {
569 if (tlist->item(i)->getTransformType()==TRANSFORM_ENVELOPED_SIGNATURE)
571 else if (tlist->item(i)->getTransformType()!=TRANSFORM_EXC_C14N &&
572 tlist->item(i)->getTransformType()!=TRANSFORM_C14N
573 #ifdef XMLTOOLING_XMLSEC_C14N11
574 && tlist->item(i)->getTransformType()!=TRANSFORM_C14N11
587 throw XMLSecurityException("Invalid signature profile for signed configuration resource.");
590 CredentialCriteria cc;
591 cc.setUsage(Credential::SIGNING_CREDENTIAL);
592 cc.setSignature(sigObj, CredentialCriteria::KEYINFO_EXTRACTION_KEY);
593 if (!m_signerName.empty())
594 cc.setPeerName(m_signerName.c_str());
596 if (m_credResolver) {
597 Locker locker(m_credResolver);
598 vector<const Credential*> creds;
599 if (m_credResolver->resolve(creds, &cc)) {
600 SignatureValidator sigValidator;
601 for (vector<const Credential*>::const_iterator i = creds.begin(); i != creds.end(); ++i) {
603 sigValidator.setCredential(*i);
604 sigValidator.validate(&sigObj);
610 throw XMLSecurityException("Unable to verify signature with supplied key(s).");
613 throw XMLSecurityException("CredentialResolver did not supply any candidate keys.");
617 DummyCredentialResolver dummy;
618 if (m_trust->validate(sigObj, dummy, &cc))
620 throw XMLSecurityException("TrustEngine unable to verify signature.");
623 throw XMLSecurityException("Unable to verify signature.");