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>
59 # include <xmltooling/XMLToolingConfig.h>
63 #include <xercesc/util/XMLUniDefs.hpp>
64 #include <xmltooling/util/NDC.h>
65 #include <xmltooling/util/PathResolver.h>
66 #include <xmltooling/util/TemplateEngine.h>
67 #include <xmltooling/util/XMLHelper.h>
69 using namespace shibsp;
70 using namespace opensaml;
71 using namespace xmltooling;
74 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
75 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
76 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
77 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
78 DECL_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
79 DECL_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
82 DECL_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
83 DECL_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
84 DECL_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
85 DECL_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
86 DECL_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
87 DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
94 SPConfig& SPConfig::getConfig()
99 void SPConfig::setServiceProvider(ServiceProvider* serviceProvider)
101 delete m_serviceProvider;
102 m_serviceProvider = serviceProvider;
105 bool SPConfig::init(const char* catalog_path, const char* inst_prefix)
111 inst_prefix = getenv("SHIBSP_PREFIX");
113 inst_prefix = SHIBSP_PREFIX;
114 std::string inst_prefix2;
115 while (*inst_prefix) {
116 inst_prefix2.push_back((*inst_prefix=='\\') ? ('/') : (*inst_prefix));
120 const char* loglevel=getenv("SHIBSP_LOGGING");
122 loglevel = SHIBSP_LOGGING;
123 std::string ll(loglevel);
124 PathResolver localpr;
125 localpr.setDefaultPrefix(inst_prefix2.c_str());
126 inst_prefix = getenv("SHIBSP_CFGDIR");
128 inst_prefix = SHIBSP_CFGDIR;
129 localpr.setCfgDir(inst_prefix);
130 XMLToolingConfig::getConfig().log_config(localpr.resolve(ll, PathResolver::XMLTOOLING_CFG_FILE, PACKAGE_NAME).c_str());
132 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
133 log.debug("%s library initialization started", PACKAGE_STRING);
136 catalog_path = getenv("SHIBSP_SCHEMAS");
138 catalog_path = SHIBSP_SCHEMAS;
139 XMLToolingConfig::getConfig().catalog_path = catalog_path;
142 if (!SAMLConfig::getConfig().init()) {
143 log.fatal("failed to initialize OpenSAML library");
147 if (!XMLToolingConfig::getConfig().init()) {
148 log.fatal("failed to initialize XMLTooling library");
152 PathResolver* pr = XMLToolingConfig::getConfig().getPathResolver();
153 pr->setDefaultPackageName(PACKAGE_NAME);
154 pr->setDefaultPrefix(inst_prefix2.c_str());
155 pr->setCfgDir(inst_prefix);
156 inst_prefix = getenv("SHIBSP_LIBDIR");
158 inst_prefix = SHIBSP_LIBDIR;
159 pr->setLibDir(inst_prefix);
160 inst_prefix = getenv("SHIBSP_LOGDIR");
162 inst_prefix = SHIBSP_LOGDIR;
163 pr->setLogDir(inst_prefix);
164 inst_prefix = getenv("SHIBSP_RUNDIR");
166 inst_prefix = SHIBSP_RUNDIR;
167 pr->setRunDir(inst_prefix);
168 inst_prefix = getenv("SHIBSP_XMLDIR");
170 inst_prefix = SHIBSP_XMLDIR;
171 pr->setXMLDir(inst_prefix);
173 XMLToolingConfig::getConfig().setTemplateEngine(new TemplateEngine());
174 XMLToolingConfig::getConfig().getTemplateEngine()->setTagPrefix("shibmlp");
176 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
177 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
178 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
179 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
180 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
181 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
184 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
185 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
186 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
187 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
188 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
189 REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
193 if (isEnabled(Metadata))
194 registerMetadataExtClasses();
195 if (isEnabled(Trust))
196 registerPKIXTrustEngine();
199 registerAttributeFactories();
201 registerSessionInitiators();
202 registerServiceProviders();
205 if (isEnabled(AttributeResolution)) {
206 registerAttributeExtractors();
207 registerAttributeDecoders();
208 registerAttributeResolvers();
209 registerAttributeFilters();
210 registerMatchFunctors();
214 if (isEnabled(Listener))
215 registerListenerServices();
217 if (isEnabled(RequestMapping)) {
218 registerAccessControls();
219 registerRequestMappers();
222 if (isEnabled(Caching))
223 registerSessionCaches();
226 if (isEnabled(OutOfProcess))
227 m_artifactResolver = new ArtifactResolver();
229 srand(static_cast<unsigned int>(std::time(NULL)));
231 log.info("%s library initialization complete", PACKAGE_STRING);
235 void SPConfig::term()
240 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
241 log.info("%s library shutting down", PACKAGE_STRING);
243 setServiceProvider(NULL);
245 m_configDoc->release();
248 setArtifactResolver(NULL);
251 ArtifactResolutionServiceManager.deregisterFactories();
252 AssertionConsumerServiceManager.deregisterFactories();
253 LogoutInitiatorManager.deregisterFactories();
254 ManageNameIDServiceManager.deregisterFactories();
255 SessionInitiatorManager.deregisterFactories();
256 SingleLogoutServiceManager.deregisterFactories();
257 HandlerManager.deregisterFactories();
258 ServiceProviderManager.deregisterFactories();
259 Attribute::deregisterFactories();
262 if (isEnabled(AttributeResolution)) {
263 MatchFunctorManager.deregisterFactories();
264 AttributeFilterManager.deregisterFactories();
265 AttributeDecoderManager.deregisterFactories();
266 AttributeExtractorManager.deregisterFactories();
267 AttributeResolverManager.deregisterFactories();
271 if (isEnabled(Listener))
272 ListenerServiceManager.deregisterFactories();
274 if (isEnabled(RequestMapping)) {
275 AccessControlManager.deregisterFactories();
276 RequestMapperManager.deregisterFactories();
279 if (isEnabled(Caching))
280 SessionCacheManager.deregisterFactories();
283 SAMLConfig::getConfig().term();
285 XMLToolingConfig::getConfig().term();
287 log.info("%s library shutdown complete", PACKAGE_STRING);
290 bool SPConfig::instantiate(const char* config, bool rethrow)
293 NDC ndc("instantiate");
296 config = getenv("SHIBSP_CONFIG");
298 config = SHIBSP_CONFIG;
300 xercesc::DOMDocument* dummydoc;
301 if (*config == '"' || *config == '\'') {
302 throw ConfigurationException("The value of SHIBSP_CONFIG started with a quote.");
304 else if (*config != '<') {
307 string resolved(config);
308 stringstream snippet;
311 << XMLToolingConfig::getConfig().getPathResolver()->resolve(resolved, PathResolver::XMLTOOLING_CFG_FILE)
312 << "' validate='1'/>";
313 dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
314 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
315 setServiceProvider(ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER, dummydoc->getDocumentElement()));
317 m_configDoc->release();
318 m_configDoc = docjanitor.release();
321 stringstream snippet(config);
322 dummydoc = XMLToolingConfig::getConfig().getParser().parse(snippet);
323 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
324 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
325 auto_ptr_char type(dummydoc->getDocumentElement()->getAttributeNS(NULL,_type));
326 if (type.get() && *type.get())
327 setServiceProvider(ServiceProviderManager.newPlugin(type.get(), dummydoc->getDocumentElement()));
329 throw ConfigurationException("The supplied XML bootstrapping configuration did not include a type attribute.");
331 m_configDoc->release();
332 m_configDoc = docjanitor.release();
335 getServiceProvider()->init();
338 catch (exception& ex) {
341 Category::getInstance(SHIBSP_LOGCAT".Config").fatal("caught exception while loading configuration: %s", ex.what());