3 * Copyright 2001-2007 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>
58 # include <xmltooling/util/CurlNetAccessor.hpp>
60 # include <xmltooling/XMLToolingConfig.h>
64 #include <xercesc/util/XMLUniDefs.hpp>
65 #include <xmltooling/util/NDC.h>
66 #include <xmltooling/util/PathResolver.h>
67 #include <xmltooling/util/TemplateEngine.h>
68 #include <xmltooling/util/XMLHelper.h>
70 using namespace shibsp;
71 using namespace opensaml;
72 using namespace xmltooling;
75 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
76 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
77 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
78 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
79 DECL_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
80 DECL_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
83 DECL_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
84 DECL_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
85 DECL_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
86 DECL_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
87 DECL_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
88 DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
95 SPConfig& SPConfig::getConfig()
100 void SPConfig::setServiceProvider(ServiceProvider* serviceProvider)
102 delete m_serviceProvider;
103 m_serviceProvider = serviceProvider;
106 bool SPConfig::init(const char* catalog_path, const char* inst_prefix)
112 inst_prefix = getenv("SHIBSP_PREFIX");
114 inst_prefix = SHIBSP_PREFIX;
115 std::string inst_prefix2;
116 while (*inst_prefix) {
117 inst_prefix2.push_back((*inst_prefix=='\\') ? ('/') : (*inst_prefix));
121 const char* loglevel=getenv("SHIBSP_LOGGING");
123 loglevel = SHIBSP_LOGGING;
124 std::string ll(loglevel);
125 PathResolver localpr;
126 localpr.setDefaultPrefix(inst_prefix2.c_str());
127 XMLToolingConfig::getConfig().log_config(localpr.resolve(ll, PathResolver::XMLTOOLING_CFG_FILE, PACKAGE_NAME).c_str());
129 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
130 log.debug("%s library initialization started", PACKAGE_STRING);
133 catalog_path = getenv("SHIBSP_SCHEMAS");
135 catalog_path = SHIBSP_SCHEMAS;
136 XMLToolingConfig::getConfig().catalog_path = catalog_path;
139 if (!SAMLConfig::getConfig().init()) {
140 log.fatal("failed to initialize OpenSAML library");
143 XMLPlatformUtils::fgNetAccessor = new CurlNetAccessor();
145 if (!XMLToolingConfig::getConfig().init()) {
146 log.fatal("failed to initialize XMLTooling library");
150 XMLToolingConfig::getConfig().getPathResolver()->setDefaultPackageName(PACKAGE_NAME);
151 XMLToolingConfig::getConfig().getPathResolver()->setDefaultPrefix(inst_prefix2.c_str());
152 XMLToolingConfig::getConfig().setTemplateEngine(new TemplateEngine());
153 XMLToolingConfig::getConfig().getTemplateEngine()->setTagPrefix("shibmlp");
155 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
156 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
157 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
158 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
159 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
160 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
163 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
164 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
165 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
166 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
167 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
168 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
172 if (isEnabled(Metadata))
173 registerMetadataExtClasses();
174 if (isEnabled(Trust))
175 registerPKIXTrustEngine();
178 registerAttributeFactories();
180 registerSessionInitiators();
181 registerServiceProviders();
184 if (isEnabled(AttributeResolution)) {
185 registerAttributeExtractors();
186 registerAttributeDecoders();
187 registerAttributeResolvers();
188 registerAttributeFilters();
189 registerMatchFunctors();
193 if (isEnabled(Listener))
194 registerListenerServices();
196 if (isEnabled(RequestMapping)) {
197 registerAccessControls();
198 registerRequestMappers();
201 if (isEnabled(Caching))
202 registerSessionCaches();
205 if (isEnabled(OutOfProcess))
206 m_artifactResolver = new ArtifactResolver();
208 srand(static_cast<unsigned int>(std::time(NULL)));
210 log.info("%s library initialization complete", PACKAGE_STRING);
214 void SPConfig::term()
219 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
220 log.info("%s library shutting down", PACKAGE_STRING);
222 setServiceProvider(NULL);
224 setArtifactResolver(NULL);
227 ArtifactResolutionServiceManager.deregisterFactories();
228 AssertionConsumerServiceManager.deregisterFactories();
229 LogoutInitiatorManager.deregisterFactories();
230 ManageNameIDServiceManager.deregisterFactories();
231 SessionInitiatorManager.deregisterFactories();
232 SingleLogoutServiceManager.deregisterFactories();
233 HandlerManager.deregisterFactories();
234 ServiceProviderManager.deregisterFactories();
235 Attribute::deregisterFactories();
238 if (isEnabled(AttributeResolution)) {
239 MatchFunctorManager.deregisterFactories();
240 AttributeFilterManager.deregisterFactories();
241 AttributeDecoderManager.deregisterFactories();
242 AttributeExtractorManager.deregisterFactories();
243 AttributeResolverManager.deregisterFactories();
247 if (isEnabled(Listener))
248 ListenerServiceManager.deregisterFactories();
250 if (isEnabled(RequestMapping)) {
251 AccessControlManager.deregisterFactories();
252 RequestMapperManager.deregisterFactories();
255 if (isEnabled(Caching))
256 SessionCacheManager.deregisterFactories();
259 SAMLConfig::getConfig().term();
261 XMLToolingConfig::getConfig().term();
263 log.info("%s library shutdown complete", PACKAGE_STRING);
266 bool SPConfig::instantiate(const char* config, bool rethrow)
269 NDC ndc("instantiate");
272 config = getenv("SHIBSP_CONFIG");
274 config = SHIBSP_CONFIG;
276 xercesc::DOMDocument* dummydoc;
277 if (*config == '"' || *config == '\'') {
278 throw ConfigurationException("The value of SHIBSP_CONFIG started with a quote.");
280 else if (*config != '<') {
283 string resolved(config);
284 stringstream snippet;
287 << XMLToolingConfig::getConfig().getPathResolver()->resolve(resolved, PathResolver::XMLTOOLING_CFG_FILE)
288 << "' validate='1'/>";
289 dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
290 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
291 setServiceProvider(ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER, dummydoc->getDocumentElement()));
294 stringstream snippet(config);
295 dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
296 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
297 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
298 auto_ptr_char type(dummydoc->getDocumentElement()->getAttributeNS(NULL,_type));
299 if (type.get() && *type.get())
300 setServiceProvider(ServiceProviderManager.newPlugin(type.get(), dummydoc->getDocumentElement()));
302 throw ConfigurationException("The supplied XML bootstrapping configuration did not include a type attribute.");
305 getServiceProvider()->init();
308 catch (exception& ex) {
311 Category::getInstance(SHIBSP_LOGCAT".Config").fatal("caught exception while loading configuration: %s", ex.what());