Add ASCII loadCatalog method.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / ParserPool.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  * ParserPool.cpp
23  *
24  * A thread-safe pool of parsers that share characteristics.
25  */
26
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "logging.h"
30 #include "util/CurlURLInputStream.h"
31 #include "util/NDC.h"
32 #include "util/ParserPool.h"
33 #include "util/Threads.h"
34 #include "util/XMLHelper.h"
35
36 #include <algorithm>
37 #include <functional>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <xercesc/util/PlatformUtils.hpp>
41 #include <xercesc/util/XMLUniDefs.hpp>
42 #include <xercesc/sax/SAXException.hpp>
43 #include <xercesc/framework/MemBufInputSource.hpp>
44 #include <xercesc/framework/LocalFileInputSource.hpp>
45 #include <xercesc/framework/Wrapper4InputSource.hpp>
46
47 using namespace xmltooling::logging;
48 using namespace xmltooling;
49 using namespace xercesc;
50 using namespace std;
51
52
53 namespace {
54     class MyErrorHandler : public DOMErrorHandler {
55     public:
56         unsigned int errors;
57
58         MyErrorHandler() : errors(0) {}
59
60         bool handleError(const DOMError& e)
61         {
62 #ifdef _DEBUG
63             xmltooling::NDC ndc("handleError");
64 #endif
65             Category& log=Category::getInstance(XMLTOOLING_LOGCAT".ParserPool");
66
67             DOMLocator* locator=e.getLocation();
68             auto_ptr_char temp(e.getMessage());
69
70             switch (e.getSeverity()) {
71                 case DOMError::DOM_SEVERITY_WARNING:
72                     log.warnStream() << "warning on line " << locator->getLineNumber()
73                         << ", column " << locator->getColumnNumber()
74                         << ", message: " << temp.get() << logging::eol;
75                     return true;
76
77                 case DOMError::DOM_SEVERITY_ERROR:
78                     ++errors;
79                     log.errorStream() << "error on line " << locator->getLineNumber()
80                         << ", column " << locator->getColumnNumber()
81                         << ", message: " << temp.get() << logging::eol;
82                     return true;
83
84                 case DOMError::DOM_SEVERITY_FATAL_ERROR:
85                     ++errors;
86                     log.errorStream() << "fatal error on line " << locator->getLineNumber()
87                         << ", column " << locator->getColumnNumber()
88                         << ", message: " << temp.get() << logging::eol;
89                     return true;
90             }
91
92             ++errors;
93             log.errorStream() << "undefined error type on line " << locator->getLineNumber()
94                 << ", column " << locator->getColumnNumber()
95                 << ", message: " << temp.get() << logging::eol;
96             return false;
97         }
98     };
99 }
100
101
102 ParserPool::ParserPool(bool namespaceAware, bool schemaAware)
103     : m_namespaceAware(namespaceAware), m_schemaAware(schemaAware), m_lock(Mutex::create()), m_security(new SecurityManager()) {}
104
105 ParserPool::~ParserPool()
106 {
107     while(!m_pool.empty()) {
108         m_pool.top()->release();
109         m_pool.pop();
110     }
111     delete m_lock;
112     delete m_security;
113 }
114
115 DOMDocument* ParserPool::newDocument()
116 {
117     return DOMImplementationRegistry::getDOMImplementation(nullptr)->createDocument();
118 }
119
120 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
121
122 DOMDocument* ParserPool::parse(DOMLSInput& domsrc)
123 {
124     DOMLSParser* parser=checkoutBuilder();
125     XercesJanitor<DOMLSParser> janitor(parser);
126     try {
127         MyErrorHandler deh;
128         parser->getDomConfig()->setParameter(XMLUni::fgDOMErrorHandler, dynamic_cast<DOMErrorHandler*>(&deh));
129         DOMDocument* doc=parser->parse(&domsrc);
130         if (deh.errors) {
131             if (doc)
132                 doc->release();
133             throw XMLParserException("XML error(s) during parsing, check log for specifics");
134         }
135         parser->getDomConfig()->setParameter(XMLUni::fgDOMErrorHandler, (void*)nullptr);
136         parser->getDomConfig()->setParameter(XMLUni::fgXercesUserAdoptsDOMDocument, true);
137         checkinBuilder(janitor.release());
138         return doc;
139     }
140     catch (XMLException& ex) {
141         parser->getDomConfig()->setParameter(XMLUni::fgDOMErrorHandler, (void*)nullptr);
142         parser->getDomConfig()->setParameter(XMLUni::fgXercesUserAdoptsDOMDocument, true);
143         checkinBuilder(janitor.release());
144         auto_ptr_char temp(ex.getMessage());
145         throw XMLParserException(string("Xerces error during parsing: ") + (temp.get() ? temp.get() : "no message"));
146     }
147     catch (XMLToolingException&) {
148         parser->getDomConfig()->setParameter(XMLUni::fgDOMErrorHandler, (void*)nullptr);
149         parser->getDomConfig()->setParameter(XMLUni::fgXercesUserAdoptsDOMDocument, true);
150         checkinBuilder(janitor.release());
151         throw;
152     }
153 }
154
155 #else
156
157 DOMDocument* ParserPool::parse(DOMInputSource& domsrc)
158 {
159     DOMBuilder* parser=checkoutBuilder();
160     XercesJanitor<DOMBuilder> janitor(parser);
161     try {
162         MyErrorHandler deh;
163         parser->setErrorHandler(&deh);
164         DOMDocument* doc=parser->parse(domsrc);
165         if (deh.errors) {
166             if (doc)
167                 doc->release();
168             throw XMLParserException("XML error(s) during parsing, check log for specifics");
169         }
170         parser->setErrorHandler(nullptr);
171         parser->setFeature(XMLUni::fgXercesUserAdoptsDOMDocument, true);
172         checkinBuilder(janitor.release());
173         return doc;
174     }
175     catch (XMLException& ex) {
176         parser->setErrorHandler(nullptr);
177         parser->setFeature(XMLUni::fgXercesUserAdoptsDOMDocument, true);
178         checkinBuilder(janitor.release());
179         auto_ptr_char temp(ex.getMessage());
180         throw XMLParserException(string("Xerces error during parsing: ") + (temp.get() ? temp.get() : "no message"));
181     }
182     catch (XMLToolingException&) {
183         parser->setErrorHandler(nullptr);
184         parser->setFeature(XMLUni::fgXercesUserAdoptsDOMDocument, true);
185         checkinBuilder(janitor.release());
186         throw;
187     }
188 }
189
190 #endif
191
192 DOMDocument* ParserPool::parse(istream& is)
193 {
194     StreamInputSource src(is);
195     Wrapper4InputSource domsrc(&src,false);
196     return parse(domsrc);
197 }
198
199 // Functor to double its argument separated by a character and append to a buffer
200 template <class T> class doubleit
201 {
202 public:
203     doubleit(T& t, const typename T::value_type& s) : temp(t), sep(s) {}
204     void operator() (const pair<const T,T>& s) { temp += s.first + sep + s.first + sep; }
205     T& temp;
206     const typename T::value_type& sep;
207 };
208
209 bool ParserPool::loadSchema(const XMLCh* nsURI, const XMLCh* pathname)
210 {
211     // Just check the pathname and then directly register the pair into the map.
212
213     auto_ptr_char p(pathname);
214 #ifdef WIN32
215     struct _stat stat_buf;
216     if (_stat(p.get(), &stat_buf) != 0)
217 #else
218     struct stat stat_buf;
219     if (stat(p.get(), &stat_buf) != 0)
220 #endif
221     {
222 #if _DEBUG
223         xmltooling::NDC ndc("loadSchema");
224 #endif
225         Category& log=Category::getInstance(XMLTOOLING_LOGCAT".ParserPool");
226         auto_ptr_char n(nsURI);
227         log.error("failed to load schema for (%s), file not found (%s)",n.get(),p.get());
228         return false;
229     }
230
231     Lock lock(m_lock);
232     m_schemaLocMap[nsURI]=pathname;
233     m_schemaLocations.erase();
234     for_each(m_schemaLocMap.begin(),m_schemaLocMap.end(),doubleit<xstring>(m_schemaLocations,chSpace));
235
236     return true;
237 }
238
239 bool ParserPool::loadCatalog(const char* pathname)
240 {
241     auto_ptr_XMLCh temp(pathname);
242     return loadCatalog(temp.get());
243 }
244
245 bool ParserPool::loadCatalog(const XMLCh* pathname)
246 {
247 #if _DEBUG
248     xmltooling::NDC ndc("loadCatalog");
249 #endif
250     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".ParserPool");
251
252     // XML constants
253     static const XMLCh catalog[] =  UNICODE_LITERAL_7(c,a,t,a,l,o,g);
254     static const XMLCh system[] =   UNICODE_LITERAL_6(s,y,s,t,e,m);
255     static const XMLCh systemId[] = UNICODE_LITERAL_8(s,y,s,t,e,m,I,d);
256     static const XMLCh uri[] =      UNICODE_LITERAL_3(u,r,i);
257     static const XMLCh CATALOG_NS[] = {
258         chLatin_u, chLatin_r, chLatin_n, chColon,
259         chLatin_o, chLatin_a, chLatin_s, chLatin_i, chLatin_s, chColon,
260         chLatin_n, chLatin_a, chLatin_m, chLatin_e, chLatin_s, chColon,
261         chLatin_t, chLatin_c, chColon,
262         chLatin_e, chLatin_n, chLatin_t, chLatin_i, chLatin_t, chLatin_y, chColon,
263         chLatin_x, chLatin_m, chLatin_l, chLatin_n, chLatin_s, chColon,
264         chLatin_x, chLatin_m, chLatin_l, chColon,
265         chLatin_c, chLatin_a, chLatin_t, chLatin_a, chLatin_l, chLatin_o, chLatin_g, chNull
266     };
267
268     // Parse the catalog with the internal parser pool.
269
270     if (log.isDebugEnabled()) {
271         auto_ptr_char temp(pathname);
272         log.debug("loading XML catalog from %s", temp.get());
273     }
274
275     LocalFileInputSource fsrc(nullptr,pathname);
276     Wrapper4InputSource domsrc(&fsrc,false);
277     try {
278         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(domsrc);
279         XercesJanitor<DOMDocument> janitor(doc);
280
281         // Check root element.
282         const DOMElement* root=doc->getDocumentElement();
283         if (!XMLHelper::isNodeNamed(root,CATALOG_NS,catalog)) {
284             auto_ptr_char temp(pathname);
285             log.error("unknown root element, failed to load XML catalog from %s", temp.get());
286             return false;
287         }
288
289         // Fetch all the <system> elements.
290         DOMNodeList* mappings=root->getElementsByTagNameNS(CATALOG_NS,system);
291         Lock lock(m_lock);
292         for (XMLSize_t i=0; i<mappings->getLength(); i++) {
293             root=static_cast<DOMElement*>(mappings->item(i));
294             const XMLCh* from=root->getAttributeNS(nullptr,systemId);
295             const XMLCh* to=root->getAttributeNS(nullptr,uri);
296             m_schemaLocMap[from]=to;
297         }
298         m_schemaLocations.erase();
299         for_each(m_schemaLocMap.begin(),m_schemaLocMap.end(),doubleit<xstring>(m_schemaLocations,chSpace));
300     }
301     catch (exception& e) {
302         log.error("catalog loader caught exception: %s", e.what());
303         return false;
304     }
305
306     return true;
307 }
308
309 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
310 DOMLSInput* ParserPool::resolveResource(
311             const XMLCh *const resourceType,
312             const XMLCh *const namespaceUri,
313             const XMLCh *const publicId,
314             const XMLCh *const systemId,
315             const XMLCh *const baseURI
316             )
317 #else
318 DOMInputSource* ParserPool::resolveEntity(
319     const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI
320     )
321 #endif
322 {
323 #if _DEBUG
324     xmltooling::NDC ndc("resolveEntity");
325 #endif
326     if (!systemId)
327         return nullptr;
328
329     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".ParserPool");
330     if (log.isDebugEnabled()) {
331         auto_ptr_char sysId(systemId);
332         auto_ptr_char base(baseURI);
333         log.debug("asked to resolve %s with baseURI %s",sysId.get(),base.get() ? base.get() : "(null)");
334     }
335
336     // Find well-known schemas in the specified location.
337     map<xstring,xstring>::const_iterator i=m_schemaLocMap.find(systemId);
338     if (i!=m_schemaLocMap.end())
339         return new Wrapper4InputSource(new LocalFileInputSource(baseURI,i->second.c_str()));
340
341     // Check for entity as a value in the map.
342     for (i=m_schemaLocMap.begin(); i!=m_schemaLocMap.end(); ++i) {
343         if (XMLString::endsWith(i->second.c_str(), systemId))
344             return new Wrapper4InputSource(new LocalFileInputSource(baseURI,i->second.c_str()));
345     }
346
347     // We'll allow anything without embedded slashes.
348     if (XMLString::indexOf(systemId, chForwardSlash)==-1)
349         return new Wrapper4InputSource(new LocalFileInputSource(baseURI,systemId));
350
351     // Shortcircuit the request.
352     auto_ptr_char temp(systemId);
353     log.debug("unauthorized entity request (%s), blocking it", temp.get());
354     static const XMLByte nullbuf[] = {0};
355     return new Wrapper4InputSource(new MemBufInputSource(nullbuf,0,systemId));
356 }
357
358 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
359
360 DOMLSParser* ParserPool::createBuilder()
361 {
362     static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
363     DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
364     DOMLSParser* parser=static_cast<DOMImplementationLS*>(impl)->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS,nullptr);
365     parser->getDomConfig()->setParameter(XMLUni::fgDOMNamespaces, m_namespaceAware);
366     if (m_schemaAware) {
367         parser->getDomConfig()->setParameter(XMLUni::fgDOMNamespaces, true);
368         parser->getDomConfig()->setParameter(XMLUni::fgXercesSchema, true);
369         parser->getDomConfig()->setParameter(XMLUni::fgDOMValidate, true);
370         parser->getDomConfig()->setParameter(XMLUni::fgXercesCacheGrammarFromParse, true);
371
372         // We build a "fake" schema location hint that binds each namespace to itself.
373         // This ensures the entity resolver will be given the namespace as a systemId it can check.
374         parser->getDomConfig()->setParameter(XMLUni::fgXercesSchemaExternalSchemaLocation, const_cast<XMLCh*>(m_schemaLocations.c_str()));
375     }
376     parser->getDomConfig()->setParameter(XMLUni::fgXercesUserAdoptsDOMDocument, true);
377     parser->getDomConfig()->setParameter(XMLUni::fgXercesDisableDefaultEntityResolution, true);
378     parser->getDomConfig()->setParameter(XMLUni::fgDOMResourceResolver, dynamic_cast<DOMLSResourceResolver*>(this));
379     parser->getDomConfig()->setParameter(XMLUni::fgXercesSecurityManager, m_security);
380     return parser;
381 }
382
383 DOMLSParser* ParserPool::checkoutBuilder()
384 {
385     Lock lock(m_lock);
386     if (m_pool.empty()) {
387         DOMLSParser* builder=createBuilder();
388         return builder;
389     }
390     DOMLSParser* p=m_pool.top();
391     m_pool.pop();
392     if (m_schemaAware)
393         p->getDomConfig()->setParameter(XMLUni::fgXercesSchemaExternalSchemaLocation, const_cast<XMLCh*>(m_schemaLocations.c_str()));
394     return p;
395 }
396
397 void ParserPool::checkinBuilder(DOMLSParser* builder)
398 {
399     if (builder) {
400         Lock lock(m_lock);
401         m_pool.push(builder);
402     }
403 }
404
405 #else
406
407 DOMBuilder* ParserPool::createBuilder()
408 {
409     static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
410     DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
411     DOMBuilder* parser=static_cast<DOMImplementationLS*>(impl)->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS,0);
412     parser->setFeature(XMLUni::fgDOMNamespaces, m_namespaceAware);
413     if (m_schemaAware) {
414         parser->setFeature(XMLUni::fgDOMNamespaces, true);
415         parser->setFeature(XMLUni::fgXercesSchema, true);
416         parser->setFeature(XMLUni::fgDOMValidation, true);
417         parser->setFeature(XMLUni::fgXercesCacheGrammarFromParse, true);
418
419         // We build a "fake" schema location hint that binds each namespace to itself.
420         // This ensures the entity resolver will be given the namespace as a systemId it can check.
421         parser->setProperty(XMLUni::fgXercesSchemaExternalSchemaLocation,const_cast<XMLCh*>(m_schemaLocations.c_str()));
422     }
423     parser->setProperty(XMLUni::fgXercesSecurityManager, m_security);
424     parser->setFeature(XMLUni::fgXercesUserAdoptsDOMDocument, true);
425     parser->setFeature(XMLUni::fgXercesDisableDefaultEntityResolution, true);
426     parser->setEntityResolver(this);
427     return parser;
428 }
429
430 DOMBuilder* ParserPool::checkoutBuilder()
431 {
432     Lock lock(m_lock);
433     if (m_pool.empty()) {
434         DOMBuilder* builder=createBuilder();
435         return builder;
436     }
437     DOMBuilder* p=m_pool.top();
438     m_pool.pop();
439     if (m_schemaAware)
440         p->setProperty(XMLUni::fgXercesSchemaExternalSchemaLocation,const_cast<XMLCh*>(m_schemaLocations.c_str()));
441     return p;
442 }
443
444 void ParserPool::checkinBuilder(DOMBuilder* builder)
445 {
446     if (builder) {
447         Lock lock(m_lock);
448         m_pool.push(builder);
449     }
450 }
451
452 #endif
453
454 StreamInputSource::StreamInputSource(istream& is, const char* systemId) : InputSource(systemId), m_is(is)
455 {
456 }
457
458 BinInputStream* StreamInputSource::makeStream() const
459 {
460     return new StreamBinInputStream(m_is);
461 }
462
463 StreamInputSource::StreamBinInputStream::StreamBinInputStream(istream& is) : m_is(is), m_pos(0)
464 {
465 }
466
467 #ifdef XMLTOOLING_XERCESC_64BITSAFE
468 XMLFilePos
469 #else
470 unsigned int
471 #endif
472 StreamInputSource::StreamBinInputStream::curPos() const
473 {
474     return m_pos;
475 }
476
477 #ifdef XMLTOOLING_XERCESC_64BITSAFE
478 const XMLCh* StreamInputSource::StreamBinInputStream::getContentType() const
479 {
480     return nullptr;
481 }
482 #endif
483
484 xsecsize_t StreamInputSource::StreamBinInputStream::readBytes(XMLByte* const toFill, const xsecsize_t maxToRead)
485 {
486     XMLByte* target=toFill;
487     xsecsize_t bytes_read=0,request=maxToRead;
488
489     // Fulfill the rest by reading from the stream.
490     if (request && !m_is.eof() && !m_is.fail()) {
491         try {
492             m_is.read(reinterpret_cast<char* const>(target),request);
493             m_pos+=m_is.gcount();
494             bytes_read+=m_is.gcount();
495         }
496         catch(ios_base::failure& e) {
497             Category::getInstance(XMLTOOLING_LOGCAT".StreamInputSource").critStream()
498                 << "XML::StreamInputSource::StreamBinInputStream::readBytes caught an exception: " << e.what()
499                 << logging::eol;
500             *toFill=0;
501             return 0;
502         }
503     }
504     return bytes_read;
505 }
506
507 #ifdef XMLTOOLING_LITE
508
509 URLInputSource::URLInputSource(const XMLCh* url, const char* systemId, string* cacheTag) : InputSource(systemId), m_url(url)
510 {
511 }
512
513 URLInputSource::URLInputSource(const DOMElement* e, const char* systemId, string* cacheTag) : InputSource(systemId)
514 {
515     static const XMLCh uri[] = UNICODE_LITERAL_3(u,r,i);
516     static const XMLCh url[] = UNICODE_LITERAL_3(u,r,l);
517
518     const XMLCh* attr = e->getAttributeNS(nullptr, url);
519     if (!attr || !*attr) {
520         attr = e->getAttributeNS(nullptr, uri);
521         if (!attr || !*attr)
522             throw IOException("No URL supplied via DOM to URLInputSource constructor.");
523     }
524
525     m_url.setURL(attr);
526 }
527
528 BinInputStream* URLInputSource::makeStream() const
529 {
530     // Ask the URL to create us an appropriate input stream
531     return m_url.makeNewStream();
532 }
533
534 #else
535
536 URLInputSource::URLInputSource(const XMLCh* url, const char* systemId, string* cacheTag)
537     : InputSource(systemId), m_cacheTag(cacheTag), m_url(url), m_root(nullptr)
538 {
539 }
540
541 URLInputSource::URLInputSource(const DOMElement* e, const char* systemId, string* cacheTag)
542     : InputSource(systemId), m_cacheTag(cacheTag), m_root(e)
543 {
544 }
545
546 BinInputStream* URLInputSource::makeStream() const
547 {
548     return m_root ? new CurlURLInputStream(m_root, m_cacheTag) : new CurlURLInputStream(m_url.get(), m_cacheTag);
549 }
550
551 #endif
552
553 const char URLInputSource::asciiStatusCodeElementName[] = "URLInputSourceStatus";
554
555 const XMLCh URLInputSource::utf16StatusCodeElementName[] = UNICODE_LITERAL_20(U,R,L,I,n,p,u,t,S,o,u,r,c,e,S,t,a,t,u,s);