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.
24 * Library configuration.
29 #if defined(XMLTOOLING_LOG4SHIB)
30 # ifndef SHIBSP_LOG4SHIB
31 # error "Logging library mismatch (XMLTooling is using log4shib)."
33 #elif defined(XMLTOOLING_LOG4CPP)
34 # ifndef SHIBSP_LOG4CPP
35 # error "Logging library mismatch (XMLTooling is using log4cpp)."
38 # error "No supported logging library."
41 #include "exceptions.h"
43 #include "AccessControl.h"
44 #include "RequestMapper.h"
45 #include "ServiceProvider.h"
46 #include "SessionCache.h"
48 #include "attribute/Attribute.h"
49 #include "binding/ProtocolProvider.h"
50 #include "handler/LogoutInitiator.h"
51 #include "handler/SessionInitiator.h"
52 #include "remoting/ListenerService.h"
55 # include "attribute/AttributeDecoder.h"
56 # include "attribute/filtering/AttributeFilter.h"
57 # include "attribute/filtering/MatchFunctor.h"
58 # include "attribute/resolver/AttributeExtractor.h"
59 # include "attribute/resolver/AttributeResolver.h"
60 # include "binding/ArtifactResolver.h"
61 # include "metadata/MetadataExt.h"
62 # include "security/PKIXTrustEngine.h"
63 # include "security/SecurityPolicyProvider.h"
64 # include <saml/version.h>
65 # include <saml/SAMLConfig.h>
69 #include <boost/scoped_ptr.hpp>
70 #include <xercesc/util/XMLUniDefs.hpp>
71 #include <xmltooling/version.h>
72 #include <xmltooling/XMLToolingConfig.h>
73 #include <xmltooling/util/NDC.h>
74 #include <xmltooling/util/ParserPool.h>
75 #include <xmltooling/util/PathResolver.h>
76 #include <xmltooling/util/TemplateEngine.h>
77 #include <xmltooling/util/Threads.h>
78 #include <xmltooling/util/XMLHelper.h>
80 using namespace shibsp;
81 using namespace opensaml;
82 using namespace xmltooling;
83 using namespace boost;
86 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
87 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
88 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
89 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
90 DECL_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
91 DECL_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
94 DECL_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
95 DECL_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
96 DECL_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
97 DECL_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
98 DECL_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
99 DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
103 class SHIBSP_DLLLOCAL SPInternalConfig : public SPConfig
106 SPInternalConfig() : m_initCount(0), m_lock(Mutex::create()) {}
107 ~SPInternalConfig() {}
109 bool init(const char* catalog_path=nullptr, const char* inst_prefix=nullptr);
114 scoped_ptr<Mutex> m_lock;
117 SPInternalConfig g_config;
120 SPConfig& SPConfig::getConfig()
125 SPConfig::SPConfig() : attribute_value_delimeter(';'), m_serviceProvider(nullptr),
127 m_artifactResolver(nullptr),
129 m_features(0), m_configDoc(nullptr)
133 SPConfig::~SPConfig()
137 void SPConfig::setFeatures(unsigned long enabled)
139 m_features = enabled;
142 unsigned long SPConfig::getFeatures() const {
146 bool SPConfig::isEnabled(components_t feature) const
148 return (m_features & feature)>0;
151 ServiceProvider* SPConfig::getServiceProvider() const
153 return m_serviceProvider;
156 void SPConfig::setServiceProvider(ServiceProvider* serviceProvider)
158 delete m_serviceProvider;
159 m_serviceProvider = serviceProvider;
163 void SPConfig::setArtifactResolver(MessageDecoder::ArtifactResolver* artifactResolver)
165 delete m_artifactResolver;
166 m_artifactResolver = artifactResolver;
169 const MessageDecoder::ArtifactResolver* SPConfig::getArtifactResolver() const
171 return m_artifactResolver;
175 bool SPConfig::init(const char* catalog_path, const char* inst_prefix)
178 inst_prefix = getenv("SHIBSP_PREFIX");
180 inst_prefix = SHIBSP_PREFIX;
181 std::string inst_prefix2;
182 while (*inst_prefix) {
183 inst_prefix2.push_back((*inst_prefix=='\\') ? ('/') : (*inst_prefix));
187 const char* loglevel=getenv("SHIBSP_LOGGING");
189 loglevel = SHIBSP_LOGGING;
190 std::string ll(loglevel);
191 PathResolver localpr;
192 localpr.setDefaultPrefix(inst_prefix2.c_str());
193 inst_prefix = getenv("SHIBSP_CFGDIR");
195 inst_prefix = SHIBSP_CFGDIR;
196 localpr.setCfgDir(inst_prefix);
197 XMLToolingConfig::getConfig().log_config(localpr.resolve(ll, PathResolver::XMLTOOLING_CFG_FILE, PACKAGE_NAME).c_str());
199 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
200 log.debug("%s library initialization started", PACKAGE_STRING);
203 XMLToolingConfig::getConfig().user_agent = string(PACKAGE_NAME) + '/' + PACKAGE_VERSION +
204 " OpenSAML/" + gOpenSAMLDotVersionStr +
205 " XMLTooling/" + gXMLToolingDotVersionStr +
206 " XML-Security-C/" + XSEC_FULLVERSIONDOT +
207 " Xerces-C/" + XERCES_FULLVERSIONDOT +
208 #if defined(LOG4SHIB_VERSION)
209 " log4shib/" + LOG4SHIB_VERSION;
210 #elif defined(LOG4CPP_VERSION)
211 " log4cpp/" + LOG4CPP_VERSION;
213 if (!SAMLConfig::getConfig().init()) {
214 log.fatal("failed to initialize OpenSAML library");
218 XMLToolingConfig::getConfig().user_agent = string(PACKAGE_NAME) + '/' + PACKAGE_VERSION +
219 " XMLTooling/" + gXMLToolingDotVersionStr +
220 " Xerces-C/" + XERCES_FULLVERSIONDOT +
221 #if defined(LOG4SHIB_VERSION)
222 " log4shib/" + LOG4SHIB_VERSION;
223 #elif defined(LOG4CPP_VERSION)
224 " log4cpp/" + LOG4CPP_VERSION;
226 if (!XMLToolingConfig::getConfig().init()) {
227 log.fatal("failed to initialize XMLTooling library");
232 catalog_path = getenv("SHIBSP_SCHEMAS");
234 catalog_path = SHIBSP_SCHEMAS;
235 if (!XMLToolingConfig::getConfig().getValidatingParser().loadCatalogs(catalog_path)) {
236 log.warn("failed to load schema catalogs into validating parser");
239 PathResolver* pr = XMLToolingConfig::getConfig().getPathResolver();
240 pr->setDefaultPackageName(PACKAGE_NAME);
241 pr->setDefaultPrefix(inst_prefix2.c_str());
242 pr->setCfgDir(inst_prefix);
243 inst_prefix = getenv("SHIBSP_LIBDIR");
245 inst_prefix = SHIBSP_LIBDIR;
246 pr->setLibDir(inst_prefix);
247 inst_prefix = getenv("SHIBSP_LOGDIR");
249 inst_prefix = SHIBSP_LOGDIR;
250 pr->setLogDir(inst_prefix);
251 inst_prefix = getenv("SHIBSP_RUNDIR");
253 inst_prefix = SHIBSP_RUNDIR;
254 pr->setRunDir(inst_prefix);
255 inst_prefix = getenv("SHIBSP_XMLDIR");
257 inst_prefix = SHIBSP_XMLDIR;
258 pr->setXMLDir(inst_prefix);
260 XMLToolingConfig::getConfig().setTemplateEngine(new TemplateEngine());
261 XMLToolingConfig::getConfig().getTemplateEngine()->setTagPrefix("shibmlp");
263 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
264 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
265 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
266 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
267 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
268 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
271 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
272 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
273 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
274 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
275 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
276 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
280 if (isEnabled(Metadata))
281 registerMetadataExtClasses();
282 if (isEnabled(Trust))
283 registerPKIXTrustEngine();
286 registerAttributeFactories();
288 if (isEnabled(Handlers)) {
290 registerLogoutInitiators();
291 registerSessionInitiators();
292 registerProtocolProviders();
295 registerServiceProviders();
298 if (isEnabled(AttributeResolution)) {
299 registerAttributeExtractors();
300 registerAttributeDecoders();
301 registerAttributeResolvers();
302 registerAttributeFilters();
303 registerMatchFunctors();
305 if (isEnabled(Logging)) {
308 registerSecurityPolicyProviders();
311 if (isEnabled(Listener))
312 registerListenerServices();
314 if (isEnabled(RequestMapping)) {
315 registerAccessControls();
316 registerRequestMappers();
319 if (isEnabled(Caching))
320 registerSessionCaches();
323 if (isEnabled(OutOfProcess))
324 m_artifactResolver = new ArtifactResolver();
326 srand(static_cast<unsigned int>(std::time(nullptr)));
328 log.info("%s library initialization complete", PACKAGE_STRING);
332 void SPConfig::term()
334 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
335 log.info("%s library shutting down", PACKAGE_STRING);
337 setServiceProvider(nullptr);
339 m_configDoc->release();
340 m_configDoc = nullptr;
342 setArtifactResolver(nullptr);
345 if (isEnabled(Handlers)) {
346 ArtifactResolutionServiceManager.deregisterFactories();
347 AssertionConsumerServiceManager.deregisterFactories();
348 LogoutInitiatorManager.deregisterFactories();
349 ManageNameIDServiceManager.deregisterFactories();
350 SessionInitiatorManager.deregisterFactories();
351 SingleLogoutServiceManager.deregisterFactories();
352 HandlerManager.deregisterFactories();
353 ProtocolProviderManager.deregisterFactories();
356 ServiceProviderManager.deregisterFactories();
357 Attribute::deregisterFactories();
360 SecurityPolicyProviderManager.deregisterFactories();
361 if (isEnabled(Logging)) {
362 EventManager.deregisterFactories();
364 if (isEnabled(AttributeResolution)) {
365 MatchFunctorManager.deregisterFactories();
366 AttributeFilterManager.deregisterFactories();
367 AttributeDecoderManager.deregisterFactories();
368 AttributeExtractorManager.deregisterFactories();
369 AttributeResolverManager.deregisterFactories();
373 if (isEnabled(Listener))
374 ListenerServiceManager.deregisterFactories();
376 if (isEnabled(RequestMapping)) {
377 AccessControlManager.deregisterFactories();
378 RequestMapperManager.deregisterFactories();
381 if (isEnabled(Caching))
382 SessionCacheManager.deregisterFactories();
385 SAMLConfig::getConfig().term();
387 XMLToolingConfig::getConfig().term();
389 log.info("%s library shutdown complete", PACKAGE_STRING);
392 bool SPConfig::instantiate(const char* config, bool rethrow)
395 NDC ndc("instantiate");
398 config = getenv("SHIBSP_CONFIG");
400 config = SHIBSP_CONFIG;
402 xercesc::DOMDocument* dummydoc;
403 if (*config == '"' || *config == '\'') {
404 throw ConfigurationException("The value of SHIBSP_CONFIG started with a quote.");
406 else if (*config != '<') {
409 string resolved(config);
410 stringstream snippet;
413 << XMLToolingConfig::getConfig().getPathResolver()->resolve(resolved, PathResolver::XMLTOOLING_CFG_FILE)
414 << "' validate='1'/>";
415 dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
416 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
417 setServiceProvider(ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER, dummydoc->getDocumentElement()));
419 m_configDoc->release();
420 m_configDoc = docjanitor.release();
423 stringstream snippet(config);
424 dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
425 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
426 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
427 auto_ptr_char type(dummydoc->getDocumentElement()->getAttributeNS(nullptr,_type));
428 if (type.get() && *type.get())
429 setServiceProvider(ServiceProviderManager.newPlugin(type.get(), dummydoc->getDocumentElement()));
431 throw ConfigurationException("The supplied XML bootstrapping configuration did not include a type attribute.");
433 m_configDoc->release();
434 m_configDoc = docjanitor.release();
437 getServiceProvider()->init();
440 catch (exception& ex) {
443 Category::getInstance(SHIBSP_LOGCAT".Config").fatal("caught exception while loading configuration: %s", ex.what());
448 bool SPInternalConfig::init(const char* catalog_path, const char* inst_prefix)
451 xmltooling::NDC ndc("init");
454 Lock initLock(m_lock);
456 if (m_initCount == INT_MAX) {
457 Category::getInstance(SHIBSP_LOGCAT".Config").crit("library initialized too many times");
461 if (m_initCount >= 1) {
466 if (!SPConfig::init(catalog_path, inst_prefix)) {
474 void SPInternalConfig::term()
477 xmltooling::NDC ndc("term");
480 Lock initLock(m_lock);
481 if (m_initCount == 0) {
482 Category::getInstance(SHIBSP_LOGCAT".Config").crit("term without corresponding init");
485 else if (--m_initCount > 0) {