3 * Copyright 2001-2009 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 <saml/SAMLConfig.h>
62 #include <xercesc/util/XMLUniDefs.hpp>
63 #include <xmltooling/XMLToolingConfig.h>
64 #include <xmltooling/util/NDC.h>
65 #include <xmltooling/util/ParserPool.h>
66 #include <xmltooling/util/PathResolver.h>
67 #include <xmltooling/util/TemplateEngine.h>
68 #include <xmltooling/util/Threads.h>
69 #include <xmltooling/util/XMLHelper.h>
71 using namespace shibsp;
72 using namespace opensaml;
73 using namespace xmltooling;
76 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
77 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
78 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
79 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
80 DECL_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
81 DECL_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
84 DECL_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
85 DECL_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
86 DECL_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
87 DECL_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
88 DECL_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
89 DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
96 SPConfig& SPConfig::getConfig()
101 SPConfig::SPConfig() : attribute_value_delimeter(';'), m_serviceProvider(NULL),
103 m_artifactResolver(NULL),
105 m_features(0), m_configDoc(NULL)
109 SPConfig::~SPConfig()
113 void SPConfig::setFeatures(unsigned long enabled)
115 m_features = enabled;
118 bool SPConfig::isEnabled(components_t feature)
120 return (m_features & feature)>0;
123 ServiceProvider* SPConfig::getServiceProvider() const
125 return m_serviceProvider;
128 void SPConfig::setServiceProvider(ServiceProvider* serviceProvider)
130 delete m_serviceProvider;
131 m_serviceProvider = serviceProvider;
135 void SPConfig::setArtifactResolver(MessageDecoder::ArtifactResolver* artifactResolver)
137 delete m_artifactResolver;
138 m_artifactResolver = artifactResolver;
141 const MessageDecoder::ArtifactResolver* SPConfig::getArtifactResolver() const
143 return m_artifactResolver;
147 bool SPConfig::init(const char* catalog_path, const char* inst_prefix)
153 inst_prefix = getenv("SHIBSP_PREFIX");
155 inst_prefix = SHIBSP_PREFIX;
156 std::string inst_prefix2;
157 while (*inst_prefix) {
158 inst_prefix2.push_back((*inst_prefix=='\\') ? ('/') : (*inst_prefix));
162 const char* loglevel=getenv("SHIBSP_LOGGING");
164 loglevel = SHIBSP_LOGGING;
165 std::string ll(loglevel);
166 PathResolver localpr;
167 localpr.setDefaultPrefix(inst_prefix2.c_str());
168 inst_prefix = getenv("SHIBSP_CFGDIR");
170 inst_prefix = SHIBSP_CFGDIR;
171 localpr.setCfgDir(inst_prefix);
172 XMLToolingConfig::getConfig().log_config(localpr.resolve(ll, PathResolver::XMLTOOLING_CFG_FILE, PACKAGE_NAME).c_str());
174 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
175 log.debug("%s library initialization started", PACKAGE_STRING);
178 catalog_path = getenv("SHIBSP_SCHEMAS");
180 catalog_path = SHIBSP_SCHEMAS;
181 XMLToolingConfig::getConfig().catalog_path = catalog_path;
184 if (!SAMLConfig::getConfig().init()) {
185 log.fatal("failed to initialize OpenSAML library");
189 if (!XMLToolingConfig::getConfig().init()) {
190 log.fatal("failed to initialize XMLTooling library");
194 PathResolver* pr = XMLToolingConfig::getConfig().getPathResolver();
195 pr->setDefaultPackageName(PACKAGE_NAME);
196 pr->setDefaultPrefix(inst_prefix2.c_str());
197 pr->setCfgDir(inst_prefix);
198 inst_prefix = getenv("SHIBSP_LIBDIR");
200 inst_prefix = SHIBSP_LIBDIR;
201 pr->setLibDir(inst_prefix);
202 inst_prefix = getenv("SHIBSP_LOGDIR");
204 inst_prefix = SHIBSP_LOGDIR;
205 pr->setLogDir(inst_prefix);
206 inst_prefix = getenv("SHIBSP_RUNDIR");
208 inst_prefix = SHIBSP_RUNDIR;
209 pr->setRunDir(inst_prefix);
210 inst_prefix = getenv("SHIBSP_XMLDIR");
212 inst_prefix = SHIBSP_XMLDIR;
213 pr->setXMLDir(inst_prefix);
215 XMLToolingConfig::getConfig().setTemplateEngine(new TemplateEngine());
216 XMLToolingConfig::getConfig().getTemplateEngine()->setTagPrefix("shibmlp");
218 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
219 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
220 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
221 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
222 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
223 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
226 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
227 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
228 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
229 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
230 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
231 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
235 if (isEnabled(Metadata))
236 registerMetadataExtClasses();
237 if (isEnabled(Trust))
238 registerPKIXTrustEngine();
241 registerAttributeFactories();
243 registerSessionInitiators();
244 registerServiceProviders();
247 if (isEnabled(AttributeResolution)) {
248 registerAttributeExtractors();
249 registerAttributeDecoders();
250 registerAttributeResolvers();
251 registerAttributeFilters();
252 registerMatchFunctors();
256 if (isEnabled(Listener))
257 registerListenerServices();
259 if (isEnabled(RequestMapping)) {
260 registerAccessControls();
261 registerRequestMappers();
264 if (isEnabled(Caching))
265 registerSessionCaches();
268 if (isEnabled(OutOfProcess))
269 m_artifactResolver = new ArtifactResolver();
271 srand(static_cast<unsigned int>(std::time(NULL)));
273 log.info("%s library initialization complete", PACKAGE_STRING);
277 void SPConfig::term()
282 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
283 log.info("%s library shutting down", PACKAGE_STRING);
285 setServiceProvider(NULL);
287 m_configDoc->release();
290 setArtifactResolver(NULL);
293 ArtifactResolutionServiceManager.deregisterFactories();
294 AssertionConsumerServiceManager.deregisterFactories();
295 LogoutInitiatorManager.deregisterFactories();
296 ManageNameIDServiceManager.deregisterFactories();
297 SessionInitiatorManager.deregisterFactories();
298 SingleLogoutServiceManager.deregisterFactories();
299 HandlerManager.deregisterFactories();
300 ServiceProviderManager.deregisterFactories();
301 Attribute::deregisterFactories();
304 if (isEnabled(AttributeResolution)) {
305 MatchFunctorManager.deregisterFactories();
306 AttributeFilterManager.deregisterFactories();
307 AttributeDecoderManager.deregisterFactories();
308 AttributeExtractorManager.deregisterFactories();
309 AttributeResolverManager.deregisterFactories();
313 if (isEnabled(Listener))
314 ListenerServiceManager.deregisterFactories();
316 if (isEnabled(RequestMapping)) {
317 AccessControlManager.deregisterFactories();
318 RequestMapperManager.deregisterFactories();
321 if (isEnabled(Caching))
322 SessionCacheManager.deregisterFactories();
325 SAMLConfig::getConfig().term();
327 XMLToolingConfig::getConfig().term();
329 log.info("%s library shutdown complete", PACKAGE_STRING);
332 bool SPConfig::instantiate(const char* config, bool rethrow)
335 NDC ndc("instantiate");
338 config = getenv("SHIBSP_CONFIG");
340 config = SHIBSP_CONFIG;
342 xercesc::DOMDocument* dummydoc;
343 if (*config == '"' || *config == '\'') {
344 throw ConfigurationException("The value of SHIBSP_CONFIG started with a quote.");
346 else if (*config != '<') {
349 string resolved(config);
350 stringstream snippet;
353 << XMLToolingConfig::getConfig().getPathResolver()->resolve(resolved, PathResolver::XMLTOOLING_CFG_FILE)
354 << "' validate='1'/>";
355 dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
356 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
357 setServiceProvider(ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER, dummydoc->getDocumentElement()));
359 m_configDoc->release();
360 m_configDoc = docjanitor.release();
363 stringstream snippet(config);
364 dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
365 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
366 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
367 auto_ptr_char type(dummydoc->getDocumentElement()->getAttributeNS(NULL,_type));
368 if (type.get() && *type.get())
369 setServiceProvider(ServiceProviderManager.newPlugin(type.get(), dummydoc->getDocumentElement()));
371 throw ConfigurationException("The supplied XML bootstrapping configuration did not include a type attribute.");
373 m_configDoc->release();
374 m_configDoc = docjanitor.release();
377 getServiceProvider()->init();
380 catch (exception& ex) {
383 Category::getInstance(SHIBSP_LOGCAT".Config").fatal("caught exception while loading configuration: %s", ex.what());
388 TransactionLog::TransactionLog() : log(logging::Category::getInstance(SHIBSP_TX_LOGCAT)), m_lock(Mutex::create())
392 TransactionLog::~TransactionLog()
397 Lockable* TransactionLog::lock()
403 void TransactionLog::unlock()