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/SessionInitiator.h"
47 #include "remoting/ListenerService.h"
50 # include "attribute/AttributeDecoder.h"
51 # include "attribute/filtering/AttributeFilter.h"
52 # include "attribute/filtering/MatchFunctor.h"
53 # include "attribute/resolver/AttributeExtractor.h"
54 # include "attribute/resolver/AttributeResolver.h"
55 # include "binding/ArtifactResolver.h"
56 # include "metadata/MetadataExt.h"
57 # include "security/PKIXTrustEngine.h"
58 # include "security/SecurityPolicyProvider.h"
59 # include <saml/SAMLConfig.h>
63 #include <xercesc/util/XMLUniDefs.hpp>
64 #include <xmltooling/XMLToolingConfig.h>
65 #include <xmltooling/util/NDC.h>
66 #include <xmltooling/util/ParserPool.h>
67 #include <xmltooling/util/PathResolver.h>
68 #include <xmltooling/util/TemplateEngine.h>
69 #include <xmltooling/util/Threads.h>
70 #include <xmltooling/util/XMLHelper.h>
72 using namespace shibsp;
73 using namespace opensaml;
74 using namespace xmltooling;
77 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
78 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
79 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
80 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
81 DECL_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
82 DECL_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
85 DECL_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
86 DECL_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
87 DECL_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
88 DECL_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
89 DECL_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
90 DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
97 SPConfig& SPConfig::getConfig()
102 SPConfig::SPConfig() : attribute_value_delimeter(';'), m_serviceProvider(nullptr),
104 m_artifactResolver(nullptr),
106 m_features(0), m_configDoc(nullptr)
110 SPConfig::~SPConfig()
114 void SPConfig::setFeatures(unsigned long enabled)
116 m_features = enabled;
119 bool SPConfig::isEnabled(components_t feature)
121 return (m_features & feature)>0;
124 ServiceProvider* SPConfig::getServiceProvider() const
126 return m_serviceProvider;
129 void SPConfig::setServiceProvider(ServiceProvider* serviceProvider)
131 delete m_serviceProvider;
132 m_serviceProvider = serviceProvider;
136 void SPConfig::setArtifactResolver(MessageDecoder::ArtifactResolver* artifactResolver)
138 delete m_artifactResolver;
139 m_artifactResolver = artifactResolver;
142 const MessageDecoder::ArtifactResolver* SPConfig::getArtifactResolver() const
144 return m_artifactResolver;
148 bool SPConfig::init(const char* catalog_path, const char* inst_prefix)
154 inst_prefix = getenv("SHIBSP_PREFIX");
156 inst_prefix = SHIBSP_PREFIX;
157 std::string inst_prefix2;
158 while (*inst_prefix) {
159 inst_prefix2.push_back((*inst_prefix=='\\') ? ('/') : (*inst_prefix));
163 const char* loglevel=getenv("SHIBSP_LOGGING");
165 loglevel = SHIBSP_LOGGING;
166 std::string ll(loglevel);
167 PathResolver localpr;
168 localpr.setDefaultPrefix(inst_prefix2.c_str());
169 inst_prefix = getenv("SHIBSP_CFGDIR");
171 inst_prefix = SHIBSP_CFGDIR;
172 localpr.setCfgDir(inst_prefix);
173 XMLToolingConfig::getConfig().log_config(localpr.resolve(ll, PathResolver::XMLTOOLING_CFG_FILE, PACKAGE_NAME).c_str());
175 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
176 log.debug("%s library initialization started", PACKAGE_STRING);
179 catalog_path = getenv("SHIBSP_SCHEMAS");
181 catalog_path = SHIBSP_SCHEMAS;
182 XMLToolingConfig::getConfig().catalog_path = catalog_path;
185 if (!SAMLConfig::getConfig().init()) {
186 log.fatal("failed to initialize OpenSAML library");
190 if (!XMLToolingConfig::getConfig().init()) {
191 log.fatal("failed to initialize XMLTooling library");
195 PathResolver* pr = XMLToolingConfig::getConfig().getPathResolver();
196 pr->setDefaultPackageName(PACKAGE_NAME);
197 pr->setDefaultPrefix(inst_prefix2.c_str());
198 pr->setCfgDir(inst_prefix);
199 inst_prefix = getenv("SHIBSP_LIBDIR");
201 inst_prefix = SHIBSP_LIBDIR;
202 pr->setLibDir(inst_prefix);
203 inst_prefix = getenv("SHIBSP_LOGDIR");
205 inst_prefix = SHIBSP_LOGDIR;
206 pr->setLogDir(inst_prefix);
207 inst_prefix = getenv("SHIBSP_RUNDIR");
209 inst_prefix = SHIBSP_RUNDIR;
210 pr->setRunDir(inst_prefix);
211 inst_prefix = getenv("SHIBSP_XMLDIR");
213 inst_prefix = SHIBSP_XMLDIR;
214 pr->setXMLDir(inst_prefix);
216 XMLToolingConfig::getConfig().setTemplateEngine(new TemplateEngine());
217 XMLToolingConfig::getConfig().getTemplateEngine()->setTagPrefix("shibmlp");
219 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
220 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
221 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
222 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
223 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
224 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
227 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
228 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
229 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
230 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
231 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
232 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
236 if (isEnabled(Metadata))
237 registerMetadataExtClasses();
238 if (isEnabled(Trust))
239 registerPKIXTrustEngine();
242 registerAttributeFactories();
244 registerSessionInitiators();
245 registerServiceProviders();
248 if (isEnabled(AttributeResolution)) {
249 registerAttributeExtractors();
250 registerAttributeDecoders();
251 registerAttributeResolvers();
252 registerAttributeFilters();
253 registerMatchFunctors();
255 registerSecurityPolicyProviders();
258 if (isEnabled(Listener))
259 registerListenerServices();
261 if (isEnabled(RequestMapping)) {
262 registerAccessControls();
263 registerRequestMappers();
266 if (isEnabled(Caching))
267 registerSessionCaches();
270 if (isEnabled(OutOfProcess))
271 m_artifactResolver = new ArtifactResolver();
273 srand(static_cast<unsigned int>(std::time(nullptr)));
275 log.info("%s library initialization complete", PACKAGE_STRING);
279 void SPConfig::term()
284 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
285 log.info("%s library shutting down", PACKAGE_STRING);
287 setServiceProvider(nullptr);
289 m_configDoc->release();
290 m_configDoc = nullptr;
292 setArtifactResolver(nullptr);
295 ArtifactResolutionServiceManager.deregisterFactories();
296 AssertionConsumerServiceManager.deregisterFactories();
297 LogoutInitiatorManager.deregisterFactories();
298 ManageNameIDServiceManager.deregisterFactories();
299 SessionInitiatorManager.deregisterFactories();
300 SingleLogoutServiceManager.deregisterFactories();
301 HandlerManager.deregisterFactories();
302 ServiceProviderManager.deregisterFactories();
303 Attribute::deregisterFactories();
306 SecurityPolicyProviderManager.deregisterFactories();
307 if (isEnabled(AttributeResolution)) {
308 MatchFunctorManager.deregisterFactories();
309 AttributeFilterManager.deregisterFactories();
310 AttributeDecoderManager.deregisterFactories();
311 AttributeExtractorManager.deregisterFactories();
312 AttributeResolverManager.deregisterFactories();
316 if (isEnabled(Listener))
317 ListenerServiceManager.deregisterFactories();
319 if (isEnabled(RequestMapping)) {
320 AccessControlManager.deregisterFactories();
321 RequestMapperManager.deregisterFactories();
324 if (isEnabled(Caching))
325 SessionCacheManager.deregisterFactories();
328 SAMLConfig::getConfig().term();
330 XMLToolingConfig::getConfig().term();
332 log.info("%s library shutdown complete", PACKAGE_STRING);
335 bool SPConfig::instantiate(const char* config, bool rethrow)
338 NDC ndc("instantiate");
341 config = getenv("SHIBSP_CONFIG");
343 config = SHIBSP_CONFIG;
345 xercesc::DOMDocument* dummydoc;
346 if (*config == '"' || *config == '\'') {
347 throw ConfigurationException("The value of SHIBSP_CONFIG started with a quote.");
349 else if (*config != '<') {
352 string resolved(config);
353 stringstream snippet;
356 << XMLToolingConfig::getConfig().getPathResolver()->resolve(resolved, PathResolver::XMLTOOLING_CFG_FILE)
357 << "' validate='1'/>";
358 dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
359 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
360 setServiceProvider(ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER, dummydoc->getDocumentElement()));
362 m_configDoc->release();
363 m_configDoc = docjanitor.release();
366 stringstream snippet(config);
367 dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
368 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
369 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
370 auto_ptr_char type(dummydoc->getDocumentElement()->getAttributeNS(nullptr,_type));
371 if (type.get() && *type.get())
372 setServiceProvider(ServiceProviderManager.newPlugin(type.get(), dummydoc->getDocumentElement()));
374 throw ConfigurationException("The supplied XML bootstrapping configuration did not include a type attribute.");
376 m_configDoc->release();
377 m_configDoc = docjanitor.release();
380 getServiceProvider()->init();
383 catch (exception& ex) {
386 Category::getInstance(SHIBSP_LOGCAT".Config").fatal("caught exception while loading configuration: %s", ex.what());
391 TransactionLog::TransactionLog() : log(logging::Category::getInstance(SHIBSP_TX_LOGCAT)), m_lock(Mutex::create())
395 TransactionLog::~TransactionLog()
400 Lockable* TransactionLog::lock()
406 void TransactionLog::unlock()