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 bool SPConfig::isEnabled(components_t feature)
126 return (m_features & feature)>0;
129 ServiceProvider* SPConfig::getServiceProvider() const
131 return m_serviceProvider;
134 void SPConfig::setServiceProvider(ServiceProvider* serviceProvider)
136 delete m_serviceProvider;
137 m_serviceProvider = serviceProvider;
141 void SPConfig::setArtifactResolver(MessageDecoder::ArtifactResolver* artifactResolver)
143 delete m_artifactResolver;
144 m_artifactResolver = artifactResolver;
147 const MessageDecoder::ArtifactResolver* SPConfig::getArtifactResolver() const
149 return m_artifactResolver;
153 bool SPConfig::init(const char* catalog_path, const char* inst_prefix)
159 inst_prefix = getenv("SHIBSP_PREFIX");
161 inst_prefix = SHIBSP_PREFIX;
162 std::string inst_prefix2;
163 while (*inst_prefix) {
164 inst_prefix2.push_back((*inst_prefix=='\\') ? ('/') : (*inst_prefix));
168 const char* loglevel=getenv("SHIBSP_LOGGING");
170 loglevel = SHIBSP_LOGGING;
171 std::string ll(loglevel);
172 PathResolver localpr;
173 localpr.setDefaultPrefix(inst_prefix2.c_str());
174 inst_prefix = getenv("SHIBSP_CFGDIR");
176 inst_prefix = SHIBSP_CFGDIR;
177 localpr.setCfgDir(inst_prefix);
178 XMLToolingConfig::getConfig().log_config(localpr.resolve(ll, PathResolver::XMLTOOLING_CFG_FILE, PACKAGE_NAME).c_str());
180 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
181 log.debug("%s library initialization started", PACKAGE_STRING);
184 catalog_path = getenv("SHIBSP_SCHEMAS");
186 catalog_path = SHIBSP_SCHEMAS;
187 XMLToolingConfig::getConfig().catalog_path = catalog_path;
190 XMLToolingConfig::getConfig().user_agent = string(PACKAGE_NAME) + '/' + PACKAGE_VERSION +
191 " OpenSAML/" + OPENSAML_FULLVERSIONDOT +
192 " XMLTooling/" + XMLTOOLING_FULLVERSIONDOT +
193 " XML-Security-C/" + XSEC_FULLVERSIONDOT +
194 " Xerces-C/" + XERCES_FULLVERSIONDOT +
195 #if defined(LOG4SHIB_VERSION)
196 " log4shib/" + LOG4SHIB_VERSION;
197 #elif defined(LOG4CPP_VERSION)
198 " log4cpp/" + LOG4CPP_VERSION;
200 if (!SAMLConfig::getConfig().init()) {
201 log.fatal("failed to initialize OpenSAML library");
205 XMLToolingConfig::getConfig().user_agent = string(PACKAGE_NAME) + '/' + PACKAGE_VERSION +
206 " XMLTooling/" + XMLTOOLING_FULLVERSIONDOT +
207 " Xerces-C/" + XERCES_FULLVERSIONDOT;
208 if (!XMLToolingConfig::getConfig().init()) {
209 log.fatal("failed to initialize XMLTooling library");
213 PathResolver* pr = XMLToolingConfig::getConfig().getPathResolver();
214 pr->setDefaultPackageName(PACKAGE_NAME);
215 pr->setDefaultPrefix(inst_prefix2.c_str());
216 pr->setCfgDir(inst_prefix);
217 inst_prefix = getenv("SHIBSP_LIBDIR");
219 inst_prefix = SHIBSP_LIBDIR;
220 pr->setLibDir(inst_prefix);
221 inst_prefix = getenv("SHIBSP_LOGDIR");
223 inst_prefix = SHIBSP_LOGDIR;
224 pr->setLogDir(inst_prefix);
225 inst_prefix = getenv("SHIBSP_RUNDIR");
227 inst_prefix = SHIBSP_RUNDIR;
228 pr->setRunDir(inst_prefix);
229 inst_prefix = getenv("SHIBSP_XMLDIR");
231 inst_prefix = SHIBSP_XMLDIR;
232 pr->setXMLDir(inst_prefix);
234 XMLToolingConfig::getConfig().setTemplateEngine(new TemplateEngine());
235 XMLToolingConfig::getConfig().getTemplateEngine()->setTagPrefix("shibmlp");
237 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
238 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
239 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
240 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
241 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
242 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
245 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
246 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
247 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
248 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
249 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
250 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
254 if (isEnabled(Metadata))
255 registerMetadataExtClasses();
256 if (isEnabled(Trust))
257 registerPKIXTrustEngine();
260 registerAttributeFactories();
262 if (isEnabled(Handlers)) {
264 registerLogoutInitiators();
265 registerSessionInitiators();
266 registerProtocolProviders();
269 registerServiceProviders();
272 if (isEnabled(AttributeResolution)) {
273 registerAttributeExtractors();
274 registerAttributeDecoders();
275 registerAttributeResolvers();
276 registerAttributeFilters();
277 registerMatchFunctors();
279 registerSecurityPolicyProviders();
282 if (isEnabled(Listener))
283 registerListenerServices();
285 if (isEnabled(RequestMapping)) {
286 registerAccessControls();
287 registerRequestMappers();
290 if (isEnabled(Caching))
291 registerSessionCaches();
294 if (isEnabled(OutOfProcess))
295 m_artifactResolver = new ArtifactResolver();
297 srand(static_cast<unsigned int>(std::time(nullptr)));
299 log.info("%s library initialization complete", PACKAGE_STRING);
303 void SPConfig::term()
308 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
309 log.info("%s library shutting down", PACKAGE_STRING);
311 setServiceProvider(nullptr);
313 m_configDoc->release();
314 m_configDoc = nullptr;
316 setArtifactResolver(nullptr);
319 if (isEnabled(Handlers)) {
320 ArtifactResolutionServiceManager.deregisterFactories();
321 AssertionConsumerServiceManager.deregisterFactories();
322 LogoutInitiatorManager.deregisterFactories();
323 ManageNameIDServiceManager.deregisterFactories();
324 SessionInitiatorManager.deregisterFactories();
325 SingleLogoutServiceManager.deregisterFactories();
326 HandlerManager.deregisterFactories();
327 ProtocolProviderManager.deregisterFactories();
330 ServiceProviderManager.deregisterFactories();
331 Attribute::deregisterFactories();
334 SecurityPolicyProviderManager.deregisterFactories();
335 if (isEnabled(AttributeResolution)) {
336 MatchFunctorManager.deregisterFactories();
337 AttributeFilterManager.deregisterFactories();
338 AttributeDecoderManager.deregisterFactories();
339 AttributeExtractorManager.deregisterFactories();
340 AttributeResolverManager.deregisterFactories();
344 if (isEnabled(Listener))
345 ListenerServiceManager.deregisterFactories();
347 if (isEnabled(RequestMapping)) {
348 AccessControlManager.deregisterFactories();
349 RequestMapperManager.deregisterFactories();
352 if (isEnabled(Caching))
353 SessionCacheManager.deregisterFactories();
356 SAMLConfig::getConfig().term();
358 XMLToolingConfig::getConfig().term();
360 log.info("%s library shutdown complete", PACKAGE_STRING);
363 bool SPConfig::instantiate(const char* config, bool rethrow)
366 NDC ndc("instantiate");
369 config = getenv("SHIBSP_CONFIG");
371 config = SHIBSP_CONFIG;
373 xercesc::DOMDocument* dummydoc;
374 if (*config == '"' || *config == '\'') {
375 throw ConfigurationException("The value of SHIBSP_CONFIG started with a quote.");
377 else if (*config != '<') {
380 string resolved(config);
381 stringstream snippet;
384 << XMLToolingConfig::getConfig().getPathResolver()->resolve(resolved, PathResolver::XMLTOOLING_CFG_FILE)
385 << "' validate='1'/>";
386 dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
387 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
388 setServiceProvider(ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER, dummydoc->getDocumentElement()));
390 m_configDoc->release();
391 m_configDoc = docjanitor.release();
394 stringstream snippet(config);
395 dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
396 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
397 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
398 auto_ptr_char type(dummydoc->getDocumentElement()->getAttributeNS(nullptr,_type));
399 if (type.get() && *type.get())
400 setServiceProvider(ServiceProviderManager.newPlugin(type.get(), dummydoc->getDocumentElement()));
402 throw ConfigurationException("The supplied XML bootstrapping configuration did not include a type attribute.");
404 m_configDoc->release();
405 m_configDoc = docjanitor.release();
408 getServiceProvider()->init();
411 catch (exception& ex) {
414 Category::getInstance(SHIBSP_LOGCAT".Config").fatal("caught exception while loading configuration: %s", ex.what());
419 TransactionLog::TransactionLog() : log(logging::Category::getInstance(SHIBSP_TX_LOGCAT)), m_lock(Mutex::create())
423 TransactionLog::~TransactionLog()
428 Lockable* TransactionLog::lock()
434 void TransactionLog::unlock()