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 "AccessControl.h"
39 #include "exceptions.h"
40 #include "RequestMapper.h"
41 #include "ServiceProvider.h"
42 #include "SessionCache.h"
44 #include "TransactionLog.h"
45 #include "attribute/Attribute.h"
46 #include "handler/LogoutInitiator.h"
47 #include "handler/SessionInitiator.h"
48 #include "remoting/ListenerService.h"
51 # include "attribute/AttributeDecoder.h"
52 # include "attribute/filtering/AttributeFilter.h"
53 # include "attribute/filtering/MatchFunctor.h"
54 # include "attribute/resolver/AttributeExtractor.h"
55 # include "attribute/resolver/AttributeResolver.h"
56 # include "binding/ArtifactResolver.h"
57 # include "metadata/MetadataExt.h"
58 # include "security/PKIXTrustEngine.h"
59 # include "security/SecurityPolicyProvider.h"
60 # include <saml/SAMLConfig.h>
64 #include <xercesc/util/XMLUniDefs.hpp>
65 #include <xmltooling/XMLToolingConfig.h>
66 #include <xmltooling/util/NDC.h>
67 #include <xmltooling/util/ParserPool.h>
68 #include <xmltooling/util/PathResolver.h>
69 #include <xmltooling/util/TemplateEngine.h>
70 #include <xmltooling/util/Threads.h>
71 #include <xmltooling/util/XMLHelper.h>
73 using namespace shibsp;
74 using namespace opensaml;
75 using namespace xmltooling;
78 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
79 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
80 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
81 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
82 DECL_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
83 DECL_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
86 DECL_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
87 DECL_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
88 DECL_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
89 DECL_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
90 DECL_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
91 DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
98 SPConfig& SPConfig::getConfig()
103 SPConfig::SPConfig() : attribute_value_delimeter(';'), m_serviceProvider(nullptr),
105 m_artifactResolver(nullptr),
107 m_features(0), m_configDoc(nullptr)
111 SPConfig::~SPConfig()
115 void SPConfig::setFeatures(unsigned long enabled)
117 m_features = enabled;
120 bool SPConfig::isEnabled(components_t feature)
122 return (m_features & feature)>0;
125 ServiceProvider* SPConfig::getServiceProvider() const
127 return m_serviceProvider;
130 void SPConfig::setServiceProvider(ServiceProvider* serviceProvider)
132 delete m_serviceProvider;
133 m_serviceProvider = serviceProvider;
137 void SPConfig::setArtifactResolver(MessageDecoder::ArtifactResolver* artifactResolver)
139 delete m_artifactResolver;
140 m_artifactResolver = artifactResolver;
143 const MessageDecoder::ArtifactResolver* SPConfig::getArtifactResolver() const
145 return m_artifactResolver;
149 bool SPConfig::init(const char* catalog_path, const char* inst_prefix)
155 inst_prefix = getenv("SHIBSP_PREFIX");
157 inst_prefix = SHIBSP_PREFIX;
158 std::string inst_prefix2;
159 while (*inst_prefix) {
160 inst_prefix2.push_back((*inst_prefix=='\\') ? ('/') : (*inst_prefix));
164 const char* loglevel=getenv("SHIBSP_LOGGING");
166 loglevel = SHIBSP_LOGGING;
167 std::string ll(loglevel);
168 PathResolver localpr;
169 localpr.setDefaultPrefix(inst_prefix2.c_str());
170 inst_prefix = getenv("SHIBSP_CFGDIR");
172 inst_prefix = SHIBSP_CFGDIR;
173 localpr.setCfgDir(inst_prefix);
174 XMLToolingConfig::getConfig().log_config(localpr.resolve(ll, PathResolver::XMLTOOLING_CFG_FILE, PACKAGE_NAME).c_str());
176 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
177 log.debug("%s library initialization started", PACKAGE_STRING);
180 catalog_path = getenv("SHIBSP_SCHEMAS");
182 catalog_path = SHIBSP_SCHEMAS;
183 XMLToolingConfig::getConfig().catalog_path = catalog_path;
186 if (!SAMLConfig::getConfig().init()) {
187 log.fatal("failed to initialize OpenSAML library");
191 if (!XMLToolingConfig::getConfig().init()) {
192 log.fatal("failed to initialize XMLTooling library");
196 PathResolver* pr = XMLToolingConfig::getConfig().getPathResolver();
197 pr->setDefaultPackageName(PACKAGE_NAME);
198 pr->setDefaultPrefix(inst_prefix2.c_str());
199 pr->setCfgDir(inst_prefix);
200 inst_prefix = getenv("SHIBSP_LIBDIR");
202 inst_prefix = SHIBSP_LIBDIR;
203 pr->setLibDir(inst_prefix);
204 inst_prefix = getenv("SHIBSP_LOGDIR");
206 inst_prefix = SHIBSP_LOGDIR;
207 pr->setLogDir(inst_prefix);
208 inst_prefix = getenv("SHIBSP_RUNDIR");
210 inst_prefix = SHIBSP_RUNDIR;
211 pr->setRunDir(inst_prefix);
212 inst_prefix = getenv("SHIBSP_XMLDIR");
214 inst_prefix = SHIBSP_XMLDIR;
215 pr->setXMLDir(inst_prefix);
217 XMLToolingConfig::getConfig().setTemplateEngine(new TemplateEngine());
218 XMLToolingConfig::getConfig().getTemplateEngine()->setTagPrefix("shibmlp");
220 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
221 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
222 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
223 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
224 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
225 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
228 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
229 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
230 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
231 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
232 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
233 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
237 if (isEnabled(Metadata))
238 registerMetadataExtClasses();
239 if (isEnabled(Trust))
240 registerPKIXTrustEngine();
243 registerAttributeFactories();
245 registerLogoutInitiators();
246 registerSessionInitiators();
247 registerServiceProviders();
250 if (isEnabled(AttributeResolution)) {
251 registerAttributeExtractors();
252 registerAttributeDecoders();
253 registerAttributeResolvers();
254 registerAttributeFilters();
255 registerMatchFunctors();
257 registerSecurityPolicyProviders();
260 if (isEnabled(Listener))
261 registerListenerServices();
263 if (isEnabled(RequestMapping)) {
264 registerAccessControls();
265 registerRequestMappers();
268 if (isEnabled(Caching))
269 registerSessionCaches();
272 if (isEnabled(OutOfProcess))
273 m_artifactResolver = new ArtifactResolver();
275 srand(static_cast<unsigned int>(std::time(nullptr)));
277 log.info("%s library initialization complete", PACKAGE_STRING);
281 void SPConfig::term()
286 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
287 log.info("%s library shutting down", PACKAGE_STRING);
289 setServiceProvider(nullptr);
291 m_configDoc->release();
292 m_configDoc = nullptr;
294 setArtifactResolver(nullptr);
297 ArtifactResolutionServiceManager.deregisterFactories();
298 AssertionConsumerServiceManager.deregisterFactories();
299 LogoutInitiatorManager.deregisterFactories();
300 ManageNameIDServiceManager.deregisterFactories();
301 SessionInitiatorManager.deregisterFactories();
302 SingleLogoutServiceManager.deregisterFactories();
303 HandlerManager.deregisterFactories();
304 ServiceProviderManager.deregisterFactories();
305 Attribute::deregisterFactories();
308 SecurityPolicyProviderManager.deregisterFactories();
309 if (isEnabled(AttributeResolution)) {
310 MatchFunctorManager.deregisterFactories();
311 AttributeFilterManager.deregisterFactories();
312 AttributeDecoderManager.deregisterFactories();
313 AttributeExtractorManager.deregisterFactories();
314 AttributeResolverManager.deregisterFactories();
318 if (isEnabled(Listener))
319 ListenerServiceManager.deregisterFactories();
321 if (isEnabled(RequestMapping)) {
322 AccessControlManager.deregisterFactories();
323 RequestMapperManager.deregisterFactories();
326 if (isEnabled(Caching))
327 SessionCacheManager.deregisterFactories();
330 SAMLConfig::getConfig().term();
332 XMLToolingConfig::getConfig().term();
334 log.info("%s library shutdown complete", PACKAGE_STRING);
337 bool SPConfig::instantiate(const char* config, bool rethrow)
340 NDC ndc("instantiate");
343 config = getenv("SHIBSP_CONFIG");
345 config = SHIBSP_CONFIG;
347 xercesc::DOMDocument* dummydoc;
348 if (*config == '"' || *config == '\'') {
349 throw ConfigurationException("The value of SHIBSP_CONFIG started with a quote.");
351 else if (*config != '<') {
354 string resolved(config);
355 stringstream snippet;
358 << XMLToolingConfig::getConfig().getPathResolver()->resolve(resolved, PathResolver::XMLTOOLING_CFG_FILE)
359 << "' validate='1'/>";
360 dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
361 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
362 setServiceProvider(ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER, dummydoc->getDocumentElement()));
364 m_configDoc->release();
365 m_configDoc = docjanitor.release();
368 stringstream snippet(config);
369 dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
370 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
371 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
372 auto_ptr_char type(dummydoc->getDocumentElement()->getAttributeNS(nullptr,_type));
373 if (type.get() && *type.get())
374 setServiceProvider(ServiceProviderManager.newPlugin(type.get(), dummydoc->getDocumentElement()));
376 throw ConfigurationException("The supplied XML bootstrapping configuration did not include a type attribute.");
378 m_configDoc->release();
379 m_configDoc = docjanitor.release();
382 getServiceProvider()->init();
385 catch (exception& ex) {
388 Category::getInstance(SHIBSP_LOGCAT".Config").fatal("caught exception while loading configuration: %s", ex.what());
393 TransactionLog::TransactionLog() : log(logging::Category::getInstance(SHIBSP_TX_LOGCAT)), m_lock(Mutex::create())
397 TransactionLog::~TransactionLog()
402 Lockable* TransactionLog::lock()
408 void TransactionLog::unlock()