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 "attribute/Attribute.h"
45 #include "handler/SessionInitiator.h"
46 #include "remoting/ListenerService.h"
49 # include "attribute/AttributeDecoder.h"
50 # include "attribute/filtering/AttributeFilter.h"
51 # include "attribute/filtering/MatchFunctor.h"
52 # include "attribute/resolver/AttributeExtractor.h"
53 # include "attribute/resolver/AttributeResolver.h"
54 # include "binding/ArtifactResolver.h"
55 # include "metadata/MetadataExt.h"
56 # include "security/PKIXTrustEngine.h"
57 # include <saml/SAMLConfig.h>
61 #include <xercesc/util/XMLUniDefs.hpp>
62 #include <xmltooling/XMLToolingConfig.h>
63 #include <xmltooling/util/NDC.h>
64 #include <xmltooling/util/PathResolver.h>
65 #include <xmltooling/util/TemplateEngine.h>
66 #include <xmltooling/util/XMLHelper.h>
68 using namespace shibsp;
69 using namespace opensaml;
70 using namespace xmltooling;
73 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
74 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
75 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
76 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
77 DECL_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
78 DECL_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
81 DECL_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
82 DECL_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
83 DECL_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
84 DECL_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
85 DECL_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
86 DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
93 SPConfig& SPConfig::getConfig()
98 void SPConfig::setServiceProvider(ServiceProvider* serviceProvider)
100 delete m_serviceProvider;
101 m_serviceProvider = serviceProvider;
104 bool SPConfig::init(const char* catalog_path, const char* inst_prefix)
110 inst_prefix = getenv("SHIBSP_PREFIX");
112 inst_prefix = SHIBSP_PREFIX;
113 std::string inst_prefix2;
114 while (*inst_prefix) {
115 inst_prefix2.push_back((*inst_prefix=='\\') ? ('/') : (*inst_prefix));
119 const char* loglevel=getenv("SHIBSP_LOGGING");
121 loglevel = SHIBSP_LOGGING;
122 std::string ll(loglevel);
123 PathResolver localpr;
124 localpr.setDefaultPrefix(inst_prefix2.c_str());
125 inst_prefix = getenv("SHIBSP_CFGDIR");
127 inst_prefix = SHIBSP_CFGDIR;
128 localpr.setCfgDir(inst_prefix);
129 XMLToolingConfig::getConfig().log_config(localpr.resolve(ll, PathResolver::XMLTOOLING_CFG_FILE, PACKAGE_NAME).c_str());
131 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
132 log.debug("%s library initialization started", PACKAGE_STRING);
135 catalog_path = getenv("SHIBSP_SCHEMAS");
137 catalog_path = SHIBSP_SCHEMAS;
138 XMLToolingConfig::getConfig().catalog_path = catalog_path;
141 if (!SAMLConfig::getConfig().init()) {
142 log.fatal("failed to initialize OpenSAML library");
146 if (!XMLToolingConfig::getConfig().init()) {
147 log.fatal("failed to initialize XMLTooling library");
151 PathResolver* pr = XMLToolingConfig::getConfig().getPathResolver();
152 pr->setDefaultPackageName(PACKAGE_NAME);
153 pr->setDefaultPrefix(inst_prefix2.c_str());
154 pr->setCfgDir(inst_prefix);
155 inst_prefix = getenv("SHIBSP_LIBDIR");
157 inst_prefix = SHIBSP_LIBDIR;
158 pr->setLibDir(inst_prefix);
159 inst_prefix = getenv("SHIBSP_LOGDIR");
161 inst_prefix = SHIBSP_LOGDIR;
162 pr->setLogDir(inst_prefix);
163 inst_prefix = getenv("SHIBSP_RUNDIR");
165 inst_prefix = SHIBSP_RUNDIR;
166 pr->setRunDir(inst_prefix);
167 inst_prefix = getenv("SHIBSP_XMLDIR");
169 inst_prefix = SHIBSP_XMLDIR;
170 pr->setXMLDir(inst_prefix);
172 XMLToolingConfig::getConfig().setTemplateEngine(new TemplateEngine());
173 XMLToolingConfig::getConfig().getTemplateEngine()->setTagPrefix("shibmlp");
175 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
176 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
177 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
178 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
179 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
180 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
183 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
184 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
185 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
186 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
187 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
188 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
192 if (isEnabled(Metadata))
193 registerMetadataExtClasses();
194 if (isEnabled(Trust))
195 registerPKIXTrustEngine();
198 registerAttributeFactories();
200 registerSessionInitiators();
201 registerServiceProviders();
204 if (isEnabled(AttributeResolution)) {
205 registerAttributeExtractors();
206 registerAttributeDecoders();
207 registerAttributeResolvers();
208 registerAttributeFilters();
209 registerMatchFunctors();
213 if (isEnabled(Listener))
214 registerListenerServices();
216 if (isEnabled(RequestMapping)) {
217 registerAccessControls();
218 registerRequestMappers();
221 if (isEnabled(Caching))
222 registerSessionCaches();
225 if (isEnabled(OutOfProcess))
226 m_artifactResolver = new ArtifactResolver();
228 srand(static_cast<unsigned int>(std::time(NULL)));
230 log.info("%s library initialization complete", PACKAGE_STRING);
234 void SPConfig::term()
239 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
240 log.info("%s library shutting down", PACKAGE_STRING);
242 setServiceProvider(NULL);
244 m_configDoc->release();
247 setArtifactResolver(NULL);
250 ArtifactResolutionServiceManager.deregisterFactories();
251 AssertionConsumerServiceManager.deregisterFactories();
252 LogoutInitiatorManager.deregisterFactories();
253 ManageNameIDServiceManager.deregisterFactories();
254 SessionInitiatorManager.deregisterFactories();
255 SingleLogoutServiceManager.deregisterFactories();
256 HandlerManager.deregisterFactories();
257 ServiceProviderManager.deregisterFactories();
258 Attribute::deregisterFactories();
261 if (isEnabled(AttributeResolution)) {
262 MatchFunctorManager.deregisterFactories();
263 AttributeFilterManager.deregisterFactories();
264 AttributeDecoderManager.deregisterFactories();
265 AttributeExtractorManager.deregisterFactories();
266 AttributeResolverManager.deregisterFactories();
270 if (isEnabled(Listener))
271 ListenerServiceManager.deregisterFactories();
273 if (isEnabled(RequestMapping)) {
274 AccessControlManager.deregisterFactories();
275 RequestMapperManager.deregisterFactories();
278 if (isEnabled(Caching))
279 SessionCacheManager.deregisterFactories();
282 SAMLConfig::getConfig().term();
284 XMLToolingConfig::getConfig().term();
286 log.info("%s library shutdown complete", PACKAGE_STRING);
289 bool SPConfig::instantiate(const char* config, bool rethrow)
292 NDC ndc("instantiate");
295 config = getenv("SHIBSP_CONFIG");
297 config = SHIBSP_CONFIG;
299 xercesc::DOMDocument* dummydoc;
300 if (*config == '"' || *config == '\'') {
301 throw ConfigurationException("The value of SHIBSP_CONFIG started with a quote.");
303 else if (*config != '<') {
306 string resolved(config);
307 stringstream snippet;
310 << XMLToolingConfig::getConfig().getPathResolver()->resolve(resolved, PathResolver::XMLTOOLING_CFG_FILE)
311 << "' validate='1'/>";
312 dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
313 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
314 setServiceProvider(ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER, dummydoc->getDocumentElement()));
316 m_configDoc->release();
317 m_configDoc = docjanitor.release();
320 stringstream snippet(config);
321 dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
322 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
323 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
324 auto_ptr_char type(dummydoc->getDocumentElement()->getAttributeNS(NULL,_type));
325 if (type.get() && *type.get())
326 setServiceProvider(ServiceProviderManager.newPlugin(type.get(), dummydoc->getDocumentElement()));
328 throw ConfigurationException("The supplied XML bootstrapping configuration did not include a type attribute.");
330 m_configDoc->release();
331 m_configDoc = docjanitor.release();
334 getServiceProvider()->init();
337 catch (exception& ex) {
340 Category::getInstance(SHIBSP_LOGCAT".Config").fatal("caught exception while loading configuration: %s", ex.what());