3 * Copyright 2001-2010 Internet2
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
21 * Library configuration.
26 #if defined(XMLTOOLING_LOG4SHIB)
27 # ifndef SHIBSP_LOG4SHIB
28 # error "Logging library mismatch (XMLTooling is using log4shib)."
30 #elif defined(XMLTOOLING_LOG4CPP)
31 # ifndef SHIBSP_LOG4CPP
32 # error "Logging library mismatch (XMLTooling is using log4cpp)."
35 # error "No supported logging library."
38 #include "exceptions.h"
40 #include "AccessControl.h"
41 #include "RequestMapper.h"
42 #include "ServiceProvider.h"
43 #include "SessionCache.h"
45 #include "TransactionLog.h"
46 #include "attribute/Attribute.h"
47 #include "binding/ProtocolProvider.h"
48 #include "handler/LogoutInitiator.h"
49 #include "handler/SessionInitiator.h"
50 #include "remoting/ListenerService.h"
53 # include "attribute/AttributeDecoder.h"
54 # include "attribute/filtering/AttributeFilter.h"
55 # include "attribute/filtering/MatchFunctor.h"
56 # include "attribute/resolver/AttributeExtractor.h"
57 # include "attribute/resolver/AttributeResolver.h"
58 # include "binding/ArtifactResolver.h"
59 # include "metadata/MetadataExt.h"
60 # include "security/PKIXTrustEngine.h"
61 # include "security/SecurityPolicyProvider.h"
62 # include <saml/version.h>
63 # include <saml/SAMLConfig.h>
67 #include <xercesc/util/XMLUniDefs.hpp>
68 #include <xmltooling/version.h>
69 #include <xmltooling/XMLToolingConfig.h>
70 #include <xmltooling/util/NDC.h>
71 #include <xmltooling/util/ParserPool.h>
72 #include <xmltooling/util/PathResolver.h>
73 #include <xmltooling/util/TemplateEngine.h>
74 #include <xmltooling/util/Threads.h>
75 #include <xmltooling/util/XMLHelper.h>
77 using namespace shibsp;
78 using namespace opensaml;
79 using namespace xmltooling;
82 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
83 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
84 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
85 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
86 DECL_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
87 DECL_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
90 DECL_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
91 DECL_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
92 DECL_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
93 DECL_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
94 DECL_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
95 DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
102 SPConfig& SPConfig::getConfig()
107 SPConfig::SPConfig() : attribute_value_delimeter(';'), m_serviceProvider(nullptr),
109 m_artifactResolver(nullptr),
111 m_features(0), m_configDoc(nullptr)
115 SPConfig::~SPConfig()
119 void SPConfig::setFeatures(unsigned long enabled)
121 m_features = enabled;
124 unsigned long SPConfig::getFeatures() const {
128 bool SPConfig::isEnabled(components_t feature) const
130 return (m_features & feature)>0;
133 ServiceProvider* SPConfig::getServiceProvider() const
135 return m_serviceProvider;
138 void SPConfig::setServiceProvider(ServiceProvider* serviceProvider)
140 delete m_serviceProvider;
141 m_serviceProvider = serviceProvider;
145 void SPConfig::setArtifactResolver(MessageDecoder::ArtifactResolver* artifactResolver)
147 delete m_artifactResolver;
148 m_artifactResolver = artifactResolver;
151 const MessageDecoder::ArtifactResolver* SPConfig::getArtifactResolver() const
153 return m_artifactResolver;
157 bool SPConfig::init(const char* catalog_path, const char* inst_prefix)
163 inst_prefix = getenv("SHIBSP_PREFIX");
165 inst_prefix = SHIBSP_PREFIX;
166 std::string inst_prefix2;
167 while (*inst_prefix) {
168 inst_prefix2.push_back((*inst_prefix=='\\') ? ('/') : (*inst_prefix));
172 const char* loglevel=getenv("SHIBSP_LOGGING");
174 loglevel = SHIBSP_LOGGING;
175 std::string ll(loglevel);
176 PathResolver localpr;
177 localpr.setDefaultPrefix(inst_prefix2.c_str());
178 inst_prefix = getenv("SHIBSP_CFGDIR");
180 inst_prefix = SHIBSP_CFGDIR;
181 localpr.setCfgDir(inst_prefix);
182 XMLToolingConfig::getConfig().log_config(localpr.resolve(ll, PathResolver::XMLTOOLING_CFG_FILE, PACKAGE_NAME).c_str());
184 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
185 log.debug("%s library initialization started", PACKAGE_STRING);
188 catalog_path = getenv("SHIBSP_SCHEMAS");
190 catalog_path = SHIBSP_SCHEMAS;
191 XMLToolingConfig::getConfig().catalog_path = catalog_path;
194 XMLToolingConfig::getConfig().user_agent = string(PACKAGE_NAME) + '/' + PACKAGE_VERSION +
195 " OpenSAML/" + OPENSAML_FULLVERSIONDOT +
196 " XMLTooling/" + XMLTOOLING_FULLVERSIONDOT +
197 " XML-Security-C/" + XSEC_FULLVERSIONDOT +
198 " Xerces-C/" + XERCES_FULLVERSIONDOT +
199 #if defined(LOG4SHIB_VERSION)
200 " log4shib/" + LOG4SHIB_VERSION;
201 #elif defined(LOG4CPP_VERSION)
202 " log4cpp/" + LOG4CPP_VERSION;
204 if (!SAMLConfig::getConfig().init()) {
205 log.fatal("failed to initialize OpenSAML library");
209 XMLToolingConfig::getConfig().user_agent = string(PACKAGE_NAME) + '/' + PACKAGE_VERSION +
210 " XMLTooling/" + XMLTOOLING_FULLVERSIONDOT +
211 " Xerces-C/" + XERCES_FULLVERSIONDOT +
212 #if defined(LOG4SHIB_VERSION)
213 " log4shib/" + LOG4SHIB_VERSION;
214 #elif defined(LOG4CPP_VERSION)
215 " log4cpp/" + LOG4CPP_VERSION;
217 if (!XMLToolingConfig::getConfig().init()) {
218 log.fatal("failed to initialize XMLTooling library");
222 PathResolver* pr = XMLToolingConfig::getConfig().getPathResolver();
223 pr->setDefaultPackageName(PACKAGE_NAME);
224 pr->setDefaultPrefix(inst_prefix2.c_str());
225 pr->setCfgDir(inst_prefix);
226 inst_prefix = getenv("SHIBSP_LIBDIR");
228 inst_prefix = SHIBSP_LIBDIR;
229 pr->setLibDir(inst_prefix);
230 inst_prefix = getenv("SHIBSP_LOGDIR");
232 inst_prefix = SHIBSP_LOGDIR;
233 pr->setLogDir(inst_prefix);
234 inst_prefix = getenv("SHIBSP_RUNDIR");
236 inst_prefix = SHIBSP_RUNDIR;
237 pr->setRunDir(inst_prefix);
238 inst_prefix = getenv("SHIBSP_XMLDIR");
240 inst_prefix = SHIBSP_XMLDIR;
241 pr->setXMLDir(inst_prefix);
243 XMLToolingConfig::getConfig().setTemplateEngine(new TemplateEngine());
244 XMLToolingConfig::getConfig().getTemplateEngine()->setTagPrefix("shibmlp");
246 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
247 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
248 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
249 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
250 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
251 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
254 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
255 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
256 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
257 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
258 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
259 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
263 if (isEnabled(Metadata))
264 registerMetadataExtClasses();
265 if (isEnabled(Trust))
266 registerPKIXTrustEngine();
269 registerAttributeFactories();
271 if (isEnabled(Handlers)) {
273 registerLogoutInitiators();
274 registerSessionInitiators();
275 registerProtocolProviders();
278 registerServiceProviders();
281 if (isEnabled(AttributeResolution)) {
282 registerAttributeExtractors();
283 registerAttributeDecoders();
284 registerAttributeResolvers();
285 registerAttributeFilters();
286 registerMatchFunctors();
288 registerSecurityPolicyProviders();
291 if (isEnabled(Listener))
292 registerListenerServices();
294 if (isEnabled(RequestMapping)) {
295 registerAccessControls();
296 registerRequestMappers();
299 if (isEnabled(Caching))
300 registerSessionCaches();
303 if (isEnabled(OutOfProcess))
304 m_artifactResolver = new ArtifactResolver();
306 srand(static_cast<unsigned int>(std::time(nullptr)));
308 log.info("%s library initialization complete", PACKAGE_STRING);
312 void SPConfig::term()
317 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
318 log.info("%s library shutting down", PACKAGE_STRING);
320 setServiceProvider(nullptr);
322 m_configDoc->release();
323 m_configDoc = nullptr;
325 setArtifactResolver(nullptr);
328 if (isEnabled(Handlers)) {
329 ArtifactResolutionServiceManager.deregisterFactories();
330 AssertionConsumerServiceManager.deregisterFactories();
331 LogoutInitiatorManager.deregisterFactories();
332 ManageNameIDServiceManager.deregisterFactories();
333 SessionInitiatorManager.deregisterFactories();
334 SingleLogoutServiceManager.deregisterFactories();
335 HandlerManager.deregisterFactories();
336 ProtocolProviderManager.deregisterFactories();
339 ServiceProviderManager.deregisterFactories();
340 Attribute::deregisterFactories();
343 SecurityPolicyProviderManager.deregisterFactories();
344 if (isEnabled(AttributeResolution)) {
345 MatchFunctorManager.deregisterFactories();
346 AttributeFilterManager.deregisterFactories();
347 AttributeDecoderManager.deregisterFactories();
348 AttributeExtractorManager.deregisterFactories();
349 AttributeResolverManager.deregisterFactories();
353 if (isEnabled(Listener))
354 ListenerServiceManager.deregisterFactories();
356 if (isEnabled(RequestMapping)) {
357 AccessControlManager.deregisterFactories();
358 RequestMapperManager.deregisterFactories();
361 if (isEnabled(Caching))
362 SessionCacheManager.deregisterFactories();
365 SAMLConfig::getConfig().term();
367 XMLToolingConfig::getConfig().term();
369 log.info("%s library shutdown complete", PACKAGE_STRING);
372 bool SPConfig::instantiate(const char* config, bool rethrow)
375 NDC ndc("instantiate");
378 config = getenv("SHIBSP_CONFIG");
380 config = SHIBSP_CONFIG;
382 xercesc::DOMDocument* dummydoc;
383 if (*config == '"' || *config == '\'') {
384 throw ConfigurationException("The value of SHIBSP_CONFIG started with a quote.");
386 else if (*config != '<') {
389 string resolved(config);
390 stringstream snippet;
393 << XMLToolingConfig::getConfig().getPathResolver()->resolve(resolved, PathResolver::XMLTOOLING_CFG_FILE)
394 << "' validate='1'/>";
395 dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
396 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
397 setServiceProvider(ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER, dummydoc->getDocumentElement()));
399 m_configDoc->release();
400 m_configDoc = docjanitor.release();
403 stringstream snippet(config);
404 dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
405 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
406 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
407 auto_ptr_char type(dummydoc->getDocumentElement()->getAttributeNS(nullptr,_type));
408 if (type.get() && *type.get())
409 setServiceProvider(ServiceProviderManager.newPlugin(type.get(), dummydoc->getDocumentElement()));
411 throw ConfigurationException("The supplied XML bootstrapping configuration did not include a type attribute.");
413 m_configDoc->release();
414 m_configDoc = docjanitor.release();
417 getServiceProvider()->init();
420 catch (exception& ex) {
423 Category::getInstance(SHIBSP_LOGCAT".Config").fatal("caught exception while loading configuration: %s", ex.what());
428 TransactionLog::TransactionLog() : log(logging::Category::getInstance(SHIBSP_TX_LOGCAT)), m_lock(Mutex::create())
432 TransactionLog::~TransactionLog()
437 Lockable* TransactionLog::lock()
443 void TransactionLog::unlock()