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 <xercesc/util/XMLUniDefs.hpp>
70 #include <xmltooling/version.h>
71 #include <xmltooling/XMLToolingConfig.h>
72 #include <xmltooling/util/NDC.h>
73 #include <xmltooling/util/ParserPool.h>
74 #include <xmltooling/util/PathResolver.h>
75 #include <xmltooling/util/TemplateEngine.h>
76 #include <xmltooling/util/Threads.h>
77 #include <xmltooling/util/XMLHelper.h>
79 using namespace shibsp;
80 using namespace opensaml;
81 using namespace xmltooling;
82 using namespace boost;
85 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
86 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
87 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
88 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
89 DECL_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
90 DECL_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
93 DECL_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
94 DECL_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
95 DECL_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
96 DECL_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
97 DECL_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
98 DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
102 class SHIBSP_DLLLOCAL SPInternalConfig : public SPConfig
105 SPInternalConfig() : m_initCount(0), m_lock(Mutex::create()) {}
106 ~SPInternalConfig() {}
108 bool init(const char* catalog_path=nullptr, const char* inst_prefix=nullptr);
113 scoped_ptr<Mutex> m_lock;
116 SPInternalConfig g_config;
119 SPConfig& SPConfig::getConfig()
124 SPConfig::SPConfig() : attribute_value_delimeter(';'), m_serviceProvider(nullptr),
126 m_artifactResolver(nullptr),
128 m_features(0), m_configDoc(nullptr)
132 SPConfig::~SPConfig()
136 void SPConfig::setFeatures(unsigned long enabled)
138 m_features = enabled;
141 unsigned long SPConfig::getFeatures() const {
145 bool SPConfig::isEnabled(components_t feature) const
147 return (m_features & feature)>0;
150 ServiceProvider* SPConfig::getServiceProvider() const
152 return m_serviceProvider;
155 void SPConfig::setServiceProvider(ServiceProvider* serviceProvider)
157 delete m_serviceProvider;
158 m_serviceProvider = serviceProvider;
162 void SPConfig::setArtifactResolver(MessageDecoder::ArtifactResolver* artifactResolver)
164 delete m_artifactResolver;
165 m_artifactResolver = artifactResolver;
168 const MessageDecoder::ArtifactResolver* SPConfig::getArtifactResolver() const
170 return m_artifactResolver;
174 bool SPConfig::init(const char* catalog_path, const char* inst_prefix)
177 inst_prefix = getenv("SHIBSP_PREFIX");
179 inst_prefix = SHIBSP_PREFIX;
180 std::string inst_prefix2;
181 while (*inst_prefix) {
182 inst_prefix2.push_back((*inst_prefix=='\\') ? ('/') : (*inst_prefix));
186 const char* loglevel=getenv("SHIBSP_LOGGING");
188 loglevel = SHIBSP_LOGGING;
189 std::string ll(loglevel);
190 PathResolver localpr;
191 localpr.setDefaultPrefix(inst_prefix2.c_str());
192 inst_prefix = getenv("SHIBSP_CFGDIR");
193 if (!inst_prefix || !*inst_prefix)
194 inst_prefix = SHIBSP_CFGDIR;
195 localpr.setCfgDir(inst_prefix);
196 XMLToolingConfig::getConfig().log_config(localpr.resolve(ll, PathResolver::XMLTOOLING_CFG_FILE, PACKAGE_NAME).c_str());
198 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
199 log.debug("%s library initialization started", PACKAGE_STRING);
202 XMLToolingConfig::getConfig().user_agent = string(PACKAGE_NAME) + '/' + PACKAGE_VERSION +
203 " OpenSAML/" + gOpenSAMLDotVersionStr +
204 " XMLTooling/" + gXMLToolingDotVersionStr +
205 " XML-Security-C/" + XSEC_FULLVERSIONDOT +
206 " Xerces-C/" + XERCES_FULLVERSIONDOT +
207 #if defined(LOG4SHIB_VERSION)
208 " log4shib/" + LOG4SHIB_VERSION;
209 #elif defined(LOG4CPP_VERSION)
210 " log4cpp/" + LOG4CPP_VERSION;
212 if (!SAMLConfig::getConfig().init()) {
213 log.fatal("failed to initialize OpenSAML library");
217 XMLToolingConfig::getConfig().user_agent = string(PACKAGE_NAME) + '/' + PACKAGE_VERSION +
218 " XMLTooling/" + gXMLToolingDotVersionStr +
219 " Xerces-C/" + XERCES_FULLVERSIONDOT +
220 #if defined(LOG4SHIB_VERSION)
221 " log4shib/" + LOG4SHIB_VERSION;
222 #elif defined(LOG4CPP_VERSION)
223 " log4cpp/" + LOG4CPP_VERSION;
225 if (!XMLToolingConfig::getConfig().init()) {
226 log.fatal("failed to initialize XMLTooling library");
231 catalog_path = getenv("SHIBSP_SCHEMAS");
233 catalog_path = SHIBSP_SCHEMAS;
234 if (!XMLToolingConfig::getConfig().getValidatingParser().loadCatalogs(catalog_path)) {
235 log.warn("failed to load schema catalogs into validating parser");
238 PathResolver* pr = XMLToolingConfig::getConfig().getPathResolver();
239 pr->setDefaultPackageName(PACKAGE_NAME);
240 pr->setDefaultPrefix(inst_prefix2.c_str());
241 pr->setCfgDir(inst_prefix);
242 inst_prefix = getenv("SHIBSP_LIBDIR");
243 if (!inst_prefix || !*inst_prefix)
244 inst_prefix = SHIBSP_LIBDIR;
245 pr->setLibDir(inst_prefix);
246 inst_prefix = getenv("SHIBSP_LOGDIR");
247 if (!inst_prefix || !*inst_prefix)
248 inst_prefix = SHIBSP_LOGDIR;
249 pr->setLogDir(inst_prefix);
250 inst_prefix = getenv("SHIBSP_RUNDIR");
251 if (!inst_prefix || !*inst_prefix)
252 inst_prefix = SHIBSP_RUNDIR;
253 pr->setRunDir(inst_prefix);
254 inst_prefix = getenv("SHIBSP_CACHEDIR");
255 if (!inst_prefix || !*inst_prefix)
256 inst_prefix = SHIBSP_CACHEDIR;
257 pr->setCacheDir(inst_prefix);
258 inst_prefix = getenv("SHIBSP_XMLDIR");
259 if (!inst_prefix || !*inst_prefix)
260 inst_prefix = SHIBSP_XMLDIR;
261 pr->setXMLDir(inst_prefix);
263 XMLToolingConfig::getConfig().setTemplateEngine(new TemplateEngine());
264 XMLToolingConfig::getConfig().getTemplateEngine()->setTagPrefix("shibmlp");
266 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
267 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
268 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
269 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
270 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
271 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
274 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
275 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
276 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
277 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
278 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
279 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
283 if (isEnabled(Metadata))
284 registerMetadataExtClasses();
285 if (isEnabled(Trust))
286 registerPKIXTrustEngine();
289 registerAttributeFactories();
291 if (isEnabled(Handlers)) {
293 registerLogoutInitiators();
294 registerSessionInitiators();
295 registerProtocolProviders();
298 registerServiceProviders();
301 if (isEnabled(AttributeResolution)) {
302 registerAttributeExtractors();
303 registerAttributeDecoders();
304 registerAttributeResolvers();
305 registerAttributeFilters();
306 registerMatchFunctors();
308 if (isEnabled(Logging)) {
311 registerSecurityPolicyProviders();
314 if (isEnabled(Listener))
315 registerListenerServices();
317 if (isEnabled(RequestMapping)) {
318 registerAccessControls();
319 registerRequestMappers();
322 if (isEnabled(Caching))
323 registerSessionCaches();
326 if (isEnabled(OutOfProcess))
327 m_artifactResolver = new ArtifactResolver();
329 srand(static_cast<unsigned int>(std::time(nullptr)));
331 log.info("%s library initialization complete", PACKAGE_STRING);
335 void SPConfig::term()
337 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
338 log.info("%s library shutting down", PACKAGE_STRING);
340 setServiceProvider(nullptr);
342 m_configDoc->release();
343 m_configDoc = nullptr;
345 setArtifactResolver(nullptr);
348 if (isEnabled(Handlers)) {
349 ArtifactResolutionServiceManager.deregisterFactories();
350 AssertionConsumerServiceManager.deregisterFactories();
351 LogoutInitiatorManager.deregisterFactories();
352 ManageNameIDServiceManager.deregisterFactories();
353 SessionInitiatorManager.deregisterFactories();
354 SingleLogoutServiceManager.deregisterFactories();
355 HandlerManager.deregisterFactories();
356 ProtocolProviderManager.deregisterFactories();
359 ServiceProviderManager.deregisterFactories();
360 Attribute::deregisterFactories();
363 SecurityPolicyProviderManager.deregisterFactories();
364 if (isEnabled(Logging)) {
365 EventManager.deregisterFactories();
367 if (isEnabled(AttributeResolution)) {
368 MatchFunctorManager.deregisterFactories();
369 AttributeFilterManager.deregisterFactories();
370 AttributeDecoderManager.deregisterFactories();
371 AttributeExtractorManager.deregisterFactories();
372 AttributeResolverManager.deregisterFactories();
376 if (isEnabled(Listener))
377 ListenerServiceManager.deregisterFactories();
379 if (isEnabled(RequestMapping)) {
380 AccessControlManager.deregisterFactories();
381 RequestMapperManager.deregisterFactories();
384 if (isEnabled(Caching))
385 SessionCacheManager.deregisterFactories();
388 SAMLConfig::getConfig().term();
390 XMLToolingConfig::getConfig().term();
392 log.info("%s library shutdown complete", PACKAGE_STRING);
395 bool SPConfig::instantiate(const char* config, bool rethrow)
398 NDC ndc("instantiate");
401 config = getenv("SHIBSP_CONFIG");
403 config = SHIBSP_CONFIG;
405 xercesc::DOMDocument* dummydoc;
406 if (*config == '"' || *config == '\'') {
407 throw ConfigurationException("The value of SHIBSP_CONFIG started with a quote.");
409 else if (*config != '<') {
412 string resolved(config);
413 stringstream snippet;
416 << XMLToolingConfig::getConfig().getPathResolver()->resolve(resolved, PathResolver::XMLTOOLING_CFG_FILE)
417 << "' validate='1'/>";
418 dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
419 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
420 setServiceProvider(ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER, dummydoc->getDocumentElement()));
422 m_configDoc->release();
423 m_configDoc = docjanitor.release();
426 stringstream snippet(config);
427 dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
428 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
429 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
430 auto_ptr_char type(dummydoc->getDocumentElement()->getAttributeNS(nullptr,_type));
431 if (type.get() && *type.get())
432 setServiceProvider(ServiceProviderManager.newPlugin(type.get(), dummydoc->getDocumentElement()));
434 throw ConfigurationException("The supplied XML bootstrapping configuration did not include a type attribute.");
436 m_configDoc->release();
437 m_configDoc = docjanitor.release();
440 getServiceProvider()->init();
443 catch (exception& ex) {
446 Category::getInstance(SHIBSP_LOGCAT".Config").fatal("caught exception while loading configuration: %s", ex.what());
451 bool SPInternalConfig::init(const char* catalog_path, const char* inst_prefix)
454 xmltooling::NDC ndc("init");
457 Lock initLock(m_lock);
459 if (m_initCount == INT_MAX) {
460 Category::getInstance(SHIBSP_LOGCAT".Config").crit("library initialized too many times");
464 if (m_initCount >= 1) {
469 if (!SPConfig::init(catalog_path, inst_prefix)) {
477 void SPInternalConfig::term()
480 xmltooling::NDC ndc("term");
483 Lock initLock(m_lock);
484 if (m_initCount == 0) {
485 Category::getInstance(SHIBSP_LOGCAT".Config").crit("term without corresponding init");
488 else if (--m_initCount > 0) {