2 * Copyright 2001-2007 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
18 * XMLServiceProvider.cpp
\r
20 * XML-based SP configuration and mgmt
\r
23 #include "internal.h"
\r
24 #include "exceptions.h"
\r
25 #include "AccessControl.h"
\r
26 #include "Application.h"
\r
27 #include "RequestMapper.h"
\r
28 #include "ServiceProvider.h"
\r
29 #include "SessionCache.h"
\r
30 #include "SPConfig.h"
\r
31 #include "SPRequest.h"
\r
32 #include "TransactionLog.h"
\r
33 #include "attribute/resolver/AttributeResolver.h"
\r
34 #include "handler/SessionInitiator.h"
\r
35 #include "remoting/ListenerService.h"
\r
36 #include "security/PKIXTrustEngine.h"
\r
37 #include "util/DOMPropertySet.h"
\r
38 #include "util/SPConstants.h"
\r
40 #include <sys/types.h>
\r
41 #include <sys/stat.h>
\r
42 #include <log4cpp/Category.hh>
\r
43 #include <log4cpp/PropertyConfigurator.hh>
\r
44 #include <saml/SAMLConfig.h>
\r
45 #include <saml/binding/ArtifactMap.h>
\r
46 #include <saml/saml1/core/Assertions.h>
\r
47 #include <saml/saml2/metadata/ChainingMetadataProvider.h>
\r
48 #include <xmltooling/XMLToolingConfig.h>
\r
49 #include <xmltooling/security/ChainingTrustEngine.h>
\r
50 #include <xmltooling/util/NDC.h>
\r
51 #include <xmltooling/util/ReloadableXMLFile.h>
\r
52 #include <xmltooling/util/ReplayCache.h>
\r
54 using namespace shibsp;
\r
55 using namespace opensaml::saml2;
\r
56 using namespace opensaml::saml2md;
\r
57 using namespace opensaml;
\r
58 using namespace xmltooling;
\r
59 using namespace log4cpp;
\r
60 using namespace std;
\r
64 #if defined (_MSC_VER)
\r
65 #pragma warning( push )
\r
66 #pragma warning( disable : 4250 )
\r
69 static vector<const Handler*> g_noHandlers;
\r
71 // Application configuration wrapper
\r
72 class SHIBSP_DLLLOCAL XMLApplication : public virtual Application, public DOMPropertySet, public DOMNodeFilter
\r
75 XMLApplication(const ServiceProvider*, const DOMElement* e, const XMLApplication* base=NULL);
\r
76 ~XMLApplication() { cleanup(); }
\r
79 const ServiceProvider& getServiceProvider() const {return *m_sp;}
\r
80 const char* getId() const {return getString("id").second;}
\r
81 const char* getHash() const {return m_hash.c_str();}
\r
83 MetadataProvider* getMetadataProvider() const {
\r
84 return (!m_metadata && m_base) ? m_base->getMetadataProvider() : m_metadata;
\r
86 TrustEngine* getTrustEngine() const {
\r
87 return (!m_trust && m_base) ? m_base->getTrustEngine() : m_trust;
\r
89 AttributeResolver* getAttributeResolver() const {
\r
90 return (!m_attrResolver && m_base) ? m_base->getAttributeResolver() : m_attrResolver;
\r
92 const set<string>* getAttributeIds() const {
\r
93 return (m_attributeIds.empty() && m_base) ? m_base->getAttributeIds() : (m_attributeIds.empty() ? NULL : &m_attributeIds);
\r
95 CredentialResolver* getCredentialResolver() const {
\r
96 return (!m_credResolver && m_base) ? m_base->getCredentialResolver() : m_credResolver;
\r
98 const PropertySet* getRelyingParty(const EntityDescriptor* provider) const;
\r
100 const SessionInitiator* getDefaultSessionInitiator() const;
\r
101 const SessionInitiator* getSessionInitiatorById(const char* id) const;
\r
102 const Handler* getDefaultAssertionConsumerService() const;
\r
103 const Handler* getAssertionConsumerServiceByIndex(unsigned short index) const;
\r
104 const vector<const Handler*>& getAssertionConsumerServicesByBinding(const XMLCh* binding) const;
\r
105 const Handler* getHandler(const char* path) const;
\r
107 const vector<const XMLCh*>& getAudiences() const {
\r
108 return (m_audiences.empty() && m_base) ? m_base->getAudiences() : m_audiences;
\r
111 // Provides filter to exclude special config elements.
\r
112 short acceptNode(const DOMNode* node) const;
\r
116 const ServiceProvider* m_sp; // this is ok because its locking scope includes us
\r
117 const XMLApplication* m_base;
\r
119 MetadataProvider* m_metadata;
\r
120 TrustEngine* m_trust;
\r
121 AttributeResolver* m_attrResolver;
\r
122 CredentialResolver* m_credResolver;
\r
123 vector<const XMLCh*> m_audiences;
\r
124 set<string> m_attributeIds;
\r
126 // manage handler objects
\r
127 vector<Handler*> m_handlers;
\r
129 // maps location (path info) to applicable handlers
\r
130 map<string,const Handler*> m_handlerMap;
\r
132 // maps unique indexes to consumer services
\r
133 map<unsigned int,const Handler*> m_acsIndexMap;
\r
135 // pointer to default consumer service
\r
136 const Handler* m_acsDefault;
\r
138 // maps binding strings to supporting consumer service(s)
\r
139 #ifdef HAVE_GOOD_STL
\r
140 typedef map<xstring,vector<const Handler*> > ACSBindingMap;
\r
142 typedef map<string,vector<const Handler*> > ACSBindingMap;
\r
144 ACSBindingMap m_acsBindingMap;
\r
146 // pointer to default session initiator
\r
147 const SessionInitiator* m_sessionInitDefault;
\r
149 // maps unique ID strings to session initiators
\r
150 map<string,const SessionInitiator*> m_sessionInitMap;
\r
152 // RelyingParty properties
\r
153 DOMPropertySet* m_partyDefault;
\r
154 #ifdef HAVE_GOOD_STL
\r
155 map<xstring,PropertySet*> m_partyMap;
\r
157 map<const XMLCh*,PropertySet*> m_partyMap;
\r
161 // Top-level configuration implementation
\r
162 class SHIBSP_DLLLOCAL XMLConfig;
\r
163 class SHIBSP_DLLLOCAL XMLConfigImpl : public DOMPropertySet, public DOMNodeFilter
\r
166 XMLConfigImpl(const DOMElement* e, bool first, const XMLConfig* outer);
\r
169 RequestMapper* m_requestMapper;
\r
170 map<string,Application*> m_appmap;
\r
171 map< string,pair< PropertySet*,vector<const SecurityPolicyRule*> > > m_policyMap;
\r
173 // Provides filter to exclude special config elements.
\r
174 short acceptNode(const DOMNode* node) const;
\r
176 void setDocument(DOMDocument* doc) {
\r
181 void doExtensions(const DOMElement* e, const char* label, Category& log);
\r
183 const XMLConfig* m_outer;
\r
184 DOMDocument* m_document;
\r
187 class SHIBSP_DLLLOCAL XMLConfig : public ServiceProvider, public ReloadableXMLFile
\r
190 XMLConfig(const DOMElement* e)
\r
191 : ReloadableXMLFile(e), m_impl(NULL), m_listener(NULL), m_sessionCache(NULL), m_tranLog(NULL) {
\r
200 delete m_sessionCache;
\r
203 XMLToolingConfig::getConfig().setReplayCache(NULL);
\r
204 SAMLConfig::getConfig().setArtifactMap(NULL);
\r
205 for_each(m_storage.begin(), m_storage.end(), cleanup_pair<string,StorageService>());
\r
209 void setParent(const PropertySet* parent) {return m_impl->setParent(parent);}
\r
210 pair<bool,bool> getBool(const char* name, const char* ns=NULL) const {return m_impl->getBool(name,ns);}
\r
211 pair<bool,const char*> getString(const char* name, const char* ns=NULL) const {return m_impl->getString(name,ns);}
\r
212 pair<bool,const XMLCh*> getXMLString(const char* name, const char* ns=NULL) const {return m_impl->getXMLString(name,ns);}
\r
213 pair<bool,unsigned int> getUnsignedInt(const char* name, const char* ns=NULL) const {return m_impl->getUnsignedInt(name,ns);}
\r
214 pair<bool,int> getInt(const char* name, const char* ns=NULL) const {return m_impl->getInt(name,ns);}
\r
215 const PropertySet* getPropertySet(const char* name, const char* ns="urn:mace:shibboleth:sp:config:2.0") const {return m_impl->getPropertySet(name,ns);}
\r
216 const DOMElement* getElement() const {return m_impl->getElement();}
\r
219 TransactionLog* getTransactionLog() const {
\r
222 throw ConfigurationException("No TransactionLog available.");
\r
225 StorageService* getStorageService(const char* id) const {
\r
227 map<string,StorageService*>::const_iterator i=m_storage.find(id);
\r
228 if (i!=m_storage.end())
\r
234 ListenerService* getListenerService(bool required=true) const {
\r
235 if (required && !m_listener)
\r
236 throw ConfigurationException("No ListenerService available.");
\r
240 SessionCache* getSessionCache(bool required=true) const {
\r
241 if (required && !m_sessionCache)
\r
242 throw ConfigurationException("No SessionCache available.");
\r
243 return m_sessionCache;
\r
246 RequestMapper* getRequestMapper(bool required=true) const {
\r
247 if (required && !m_impl->m_requestMapper)
\r
248 throw ConfigurationException("No RequestMapper available.");
\r
249 return m_impl->m_requestMapper;
\r
252 const Application* getApplication(const char* applicationId) const {
\r
253 map<string,Application*>::const_iterator i=m_impl->m_appmap.find(applicationId);
\r
254 return (i!=m_impl->m_appmap.end()) ? i->second : NULL;
\r
257 const PropertySet* getPolicySettings(const char* id) const {
\r
258 map<string,pair<PropertySet*,vector<const SecurityPolicyRule*> > >::const_iterator i = m_impl->m_policyMap.find(id);
\r
259 if (i!=m_impl->m_policyMap.end())
\r
260 return i->second.first;
\r
261 throw ConfigurationException("Security Policy ($1) not found, check <SecurityPolicies> element.", params(1,id));
\r
264 const vector<const SecurityPolicyRule*>& getPolicyRules(const char* id) const {
\r
265 map<string,pair<PropertySet*,vector<const SecurityPolicyRule*> > >::const_iterator i = m_impl->m_policyMap.find(id);
\r
266 if (i!=m_impl->m_policyMap.end())
\r
267 return i->second.second;
\r
268 throw ConfigurationException("Security Policy ($1) not found, check <SecurityPolicies> element.", params(1,id));
\r
272 pair<bool,DOMElement*> load();
\r
275 friend class XMLConfigImpl;
\r
276 XMLConfigImpl* m_impl;
\r
277 mutable ListenerService* m_listener;
\r
278 mutable SessionCache* m_sessionCache;
\r
279 mutable TransactionLog* m_tranLog;
\r
280 mutable map<string,StorageService*> m_storage;
\r
283 #if defined (_MSC_VER)
\r
284 #pragma warning( pop )
\r
287 static const XMLCh _Application[] = UNICODE_LITERAL_11(A,p,p,l,i,c,a,t,i,o,n);
\r
288 static const XMLCh Applications[] = UNICODE_LITERAL_12(A,p,p,l,i,c,a,t,i,o,n,s);
\r
289 static const XMLCh _ArtifactMap[] = UNICODE_LITERAL_11(A,r,t,i,f,a,c,t,M,a,p);
\r
290 static const XMLCh _AttributeResolver[] = UNICODE_LITERAL_17(A,t,t,r,i,b,u,t,e,R,e,s,o,l,v,e,r);
\r
291 static const XMLCh _CredentialResolver[] = UNICODE_LITERAL_18(C,r,e,d,e,n,t,i,a,l,R,e,s,o,l,v,e,r);
\r
292 static const XMLCh DefaultRelyingParty[] = UNICODE_LITERAL_19(D,e,f,a,u,l,t,R,e,l,y,i,n,g,P,a,r,t,y);
\r
293 static const XMLCh fatal[] = UNICODE_LITERAL_5(f,a,t,a,l);
\r
294 static const XMLCh _Handler[] = UNICODE_LITERAL_7(H,a,n,d,l,e,r);
\r
295 static const XMLCh _id[] = UNICODE_LITERAL_2(i,d);
\r
296 static const XMLCh Implementation[] = UNICODE_LITERAL_14(I,m,p,l,e,m,e,n,t,a,t,i,o,n);
\r
297 static const XMLCh InProcess[] = UNICODE_LITERAL_9(I,n,P,r,o,c,e,s,s);
\r
298 static const XMLCh Library[] = UNICODE_LITERAL_7(L,i,b,r,a,r,y);
\r
299 static const XMLCh Listener[] = UNICODE_LITERAL_8(L,i,s,t,e,n,e,r);
\r
300 static const XMLCh logger[] = UNICODE_LITERAL_6(l,o,g,g,e,r);
\r
301 static const XMLCh MemoryListener[] = UNICODE_LITERAL_14(M,e,m,o,r,y,L,i,s,t,e,n,e,r);
\r
302 static const XMLCh _MetadataProvider[] = UNICODE_LITERAL_16(M,e,t,a,d,a,t,a,P,r,o,v,i,d,e,r);
\r
303 static const XMLCh OutOfProcess[] = UNICODE_LITERAL_12(O,u,t,O,f,P,r,o,c,e,s,s);
\r
304 static const XMLCh _path[] = UNICODE_LITERAL_4(p,a,t,h);
\r
305 static const XMLCh Policy[] = UNICODE_LITERAL_6(P,o,l,i,c,y);
\r
306 static const XMLCh RelyingParty[] = UNICODE_LITERAL_12(R,e,l,y,i,n,g,P,a,r,t,y);
\r
307 static const XMLCh _ReplayCache[] = UNICODE_LITERAL_11(R,e,p,l,a,y,C,a,c,h,e);
\r
308 static const XMLCh _RequestMapper[] = UNICODE_LITERAL_13(R,e,q,u,e,s,t,M,a,p,p,e,r);
\r
309 static const XMLCh Rule[] = UNICODE_LITERAL_4(R,u,l,e);
\r
310 static const XMLCh SecurityPolicies[] = UNICODE_LITERAL_16(S,e,c,u,r,i,t,y,P,o,l,i,c,i,e,s);
\r
311 static const XMLCh _SessionCache[] = UNICODE_LITERAL_12(S,e,s,s,i,o,n,C,a,c,h,e);
\r
312 static const XMLCh _SessionInitiator[] = UNICODE_LITERAL_16(S,e,s,s,i,o,n,I,n,i,t,i,a,t,o,r);
\r
313 static const XMLCh _StorageService[] = UNICODE_LITERAL_14(S,t,o,r,a,g,e,S,e,r,v,i,c,e);
\r
314 static const XMLCh TCPListener[] = UNICODE_LITERAL_11(T,C,P,L,i,s,t,e,n,e,r);
\r
315 static const XMLCh _TrustEngine[] = UNICODE_LITERAL_11(T,r,u,s,t,E,n,g,i,n,e);
\r
316 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
\r
317 static const XMLCh UnixListener[] = UNICODE_LITERAL_12(U,n,i,x,L,i,s,t,e,n,e,r);
\r
319 class SHIBSP_DLLLOCAL PolicyNodeFilter : public DOMNodeFilter
\r
322 short acceptNode(const DOMNode* node) const {
\r
323 if (XMLHelper::isNodeNamed(node,shibspconstants::SHIB2SPCONFIG_NS,Rule))
\r
324 return FILTER_REJECT;
\r
325 return FILTER_ACCEPT;
\r
331 ServiceProvider* XMLServiceProviderFactory(const DOMElement* const & e)
\r
333 return new XMLConfig(e);
\r
337 XMLApplication::XMLApplication(
\r
338 const ServiceProvider* sp,
\r
339 const DOMElement* e,
\r
340 const XMLApplication* base
\r
341 ) : m_sp(sp), m_base(base), m_metadata(NULL), m_trust(NULL), m_attrResolver(NULL), m_credResolver(NULL),
\r
342 m_partyDefault(NULL), m_sessionInitDefault(NULL), m_acsDefault(NULL)
\r
345 xmltooling::NDC ndc("XMLApplication");
\r
347 Category& log=Category::getInstance(SHIBSP_LOGCAT".Application");
\r
350 // First load any property sets.
\r
355 SPConfig& conf=SPConfig::getConfig();
\r
356 SAMLConfig& samlConf=SAMLConfig::getConfig();
\r
357 XMLToolingConfig& xmlConf=XMLToolingConfig::getConfig();
\r
360 m_hash+=getString("entityID").second;
\r
361 m_hash=samlConf.hashSHA1(m_hash.c_str(), true);
\r
363 pair<bool,const char*> attributes = getString("attributeIds");
\r
364 if (attributes.first) {
\r
365 char* dup = strdup(attributes.second);
\r
368 while (start && *start) {
\r
369 while (*start && isspace(*start))
\r
373 pos = strchr(start,' ');
\r
376 m_attributeIds.insert(start);
\r
377 start = pos ? pos+1 : NULL;
\r
382 const PropertySet* sessions = getPropertySet("Sessions");
\r
384 // Process handlers.
\r
385 Handler* handler=NULL;
\r
386 bool hardACS=false, hardSessionInit=false;
\r
387 const DOMElement* child = sessions ? XMLHelper::getFirstChildElement(sessions->getElement()) : NULL;
\r
390 // A handler is based on the Binding property in conjunction with the element name.
\r
391 // If it's an ACS or SI, also handle index/id mappings and defaulting.
\r
392 if (XMLHelper::isNodeNamed(child,samlconstants::SAML20MD_NS,AssertionConsumerService::LOCAL_NAME)) {
\r
393 auto_ptr_char bindprop(child->getAttributeNS(NULL,EndpointType::BINDING_ATTRIB_NAME));
\r
394 if (!bindprop.get() || !*(bindprop.get())) {
\r
395 log.warn("md:AssertionConsumerService element has no Binding attribute, skipping it...");
\r
396 child = XMLHelper::getNextSiblingElement(child);
\r
399 handler=conf.AssertionConsumerServiceManager.newPlugin(bindprop.get(),make_pair(child, getId()));
\r
400 // Map by binding (may be > 1 per binding, e.g. SAML 1.0 vs 1.1)
\r
401 #ifdef HAVE_GOOD_STL
\r
402 m_acsBindingMap[handler->getXMLString("Binding").second].push_back(handler);
\r
404 m_acsBindingMap[handler->getString("Binding").second].push_back(handler);
\r
406 m_acsIndexMap[handler->getUnsignedInt("index").second]=handler;
\r
409 pair<bool,bool> defprop=handler->getBool("isDefault");
\r
410 if (defprop.first) {
\r
411 if (defprop.second) {
\r
413 m_acsDefault=handler;
\r
416 else if (!m_acsDefault)
\r
417 m_acsDefault=handler;
\r
420 else if (XMLString::equals(child->getLocalName(),_SessionInitiator)) {
\r
421 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
422 if (!type.get() || !*(type.get())) {
\r
423 log.warn("SessionInitiator element has no type attribute, skipping it...");
\r
424 child = XMLHelper::getNextSiblingElement(child);
\r
427 SessionInitiator* sihandler=conf.SessionInitiatorManager.newPlugin(type.get(),make_pair(child, getId()));
\r
429 pair<bool,const char*> si_id=handler->getString("id");
\r
430 if (si_id.first && si_id.second)
\r
431 m_sessionInitMap[si_id.second]=sihandler;
\r
432 if (!hardSessionInit) {
\r
433 pair<bool,bool> defprop=handler->getBool("isDefault");
\r
434 if (defprop.first) {
\r
435 if (defprop.second) {
\r
436 hardSessionInit=true;
\r
437 m_sessionInitDefault=sihandler;
\r
440 else if (!m_sessionInitDefault)
\r
441 m_sessionInitDefault=sihandler;
\r
444 else if (XMLHelper::isNodeNamed(child,samlconstants::SAML20MD_NS,SingleLogoutService::LOCAL_NAME)) {
\r
445 auto_ptr_char bindprop(child->getAttributeNS(NULL,EndpointType::BINDING_ATTRIB_NAME));
\r
446 if (!bindprop.get() || !*(bindprop.get())) {
\r
447 log.warn("md:SingleLogoutService element has no Binding attribute, skipping it...");
\r
448 child = XMLHelper::getNextSiblingElement(child);
\r
451 handler=conf.SingleLogoutServiceManager.newPlugin(bindprop.get(),make_pair(child, getId()));
\r
453 else if (XMLHelper::isNodeNamed(child,samlconstants::SAML20MD_NS,ManageNameIDService::LOCAL_NAME)) {
\r
454 auto_ptr_char bindprop(child->getAttributeNS(NULL,EndpointType::BINDING_ATTRIB_NAME));
\r
455 if (!bindprop.get() || !*(bindprop.get())) {
\r
456 log.warn("md:ManageNameIDService element has no Binding attribute, skipping it...");
\r
457 child = XMLHelper::getNextSiblingElement(child);
\r
460 handler=conf.ManageNameIDServiceManager.newPlugin(bindprop.get(),make_pair(child, getId()));
\r
463 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
464 if (!type.get() || !*(type.get())) {
\r
465 log.warn("Handler element has no type attribute, skipping it...");
\r
466 child = XMLHelper::getNextSiblingElement(child);
\r
469 handler=conf.HandlerManager.newPlugin(type.get(),make_pair(child, getId()));
\r
472 // Save off the objects after giving the property set to the handler for its use.
\r
473 m_handlers.push_back(handler);
\r
475 // Insert into location map.
\r
476 pair<bool,const char*> location=handler->getString("Location");
\r
477 if (location.first && *location.second == '/')
\r
478 m_handlerMap[location.second]=handler;
\r
479 else if (location.first)
\r
480 m_handlerMap[string("/") + location.second]=handler;
\r
483 catch (exception& ex) {
\r
484 log.error("caught exception processing handler element: %s", ex.what());
\r
487 child = XMLHelper::getNextSiblingElement(child);
\r
490 DOMNodeList* nlist=e->getElementsByTagNameNS(samlconstants::SAML20_NS,Audience::LOCAL_NAME);
\r
491 for (XMLSize_t i=0; nlist && i<nlist->getLength(); i++)
\r
492 if (nlist->item(i)->getParentNode()->isSameNode(e) && nlist->item(i)->hasChildNodes())
\r
493 m_audiences.push_back(nlist->item(i)->getFirstChild()->getNodeValue());
\r
495 // Always include our own entityID as an audience.
\r
496 m_audiences.push_back(getXMLString("entityID").second);
\r
498 if (conf.isEnabled(SPConfig::Metadata)) {
\r
499 child = XMLHelper::getFirstChildElement(e,_MetadataProvider);
\r
501 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
502 log.info("building MetadataProvider of type %s...",type.get());
\r
504 auto_ptr<MetadataProvider> mp(samlConf.MetadataProviderManager.newPlugin(type.get(),child));
\r
506 m_metadata = mp.release();
\r
508 catch (exception& ex) {
\r
509 log.crit("error building/initializing MetadataProvider: %s", ex.what());
\r
514 if (conf.isEnabled(SPConfig::Trust)) {
\r
515 child = XMLHelper::getFirstChildElement(e,_TrustEngine);
\r
517 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
518 log.info("building TrustEngine of type %s...",type.get());
\r
520 m_trust = xmlConf.TrustEngineManager.newPlugin(type.get(),child);
\r
522 catch (exception& ex) {
\r
523 log.crit("error building TrustEngine: %s", ex.what());
\r
528 if (conf.isEnabled(SPConfig::AttributeResolution)) {
\r
529 child = XMLHelper::getFirstChildElement(e,_AttributeResolver);
\r
531 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
532 log.info("building AttributeResolver of type %s...",type.get());
\r
534 m_attrResolver = conf.AttributeResolverManager.newPlugin(type.get(),child);
\r
536 catch (exception& ex) {
\r
537 log.crit("error building AttributeResolver: %s", ex.what());
\r
542 if (conf.isEnabled(SPConfig::Credentials)) {
\r
543 child = XMLHelper::getFirstChildElement(e,_CredentialResolver);
\r
545 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
546 log.info("building CredentialResolver of type %s...",type.get());
\r
548 m_credResolver = xmlConf.CredentialResolverManager.newPlugin(type.get(),child);
\r
550 catch (exception& ex) {
\r
551 log.crit("error building CredentialResolver: %s", ex.what());
\r
557 // Finally, load relying parties.
\r
558 child = XMLHelper::getFirstChildElement(e,DefaultRelyingParty);
\r
560 m_partyDefault=new DOMPropertySet();
\r
561 m_partyDefault->load(child,log,this);
\r
562 child = XMLHelper::getFirstChildElement(child,RelyingParty);
\r
564 auto_ptr<DOMPropertySet> rp(new DOMPropertySet());
\r
565 rp->load(child,log,this);
\r
566 m_partyMap[child->getAttributeNS(NULL,saml2::Attribute::NAME_ATTRIB_NAME)]=rp.release();
\r
567 child = XMLHelper::getNextSiblingElement(child,RelyingParty);
\r
571 if (conf.isEnabled(SPConfig::OutOfProcess)) {
\r
572 // Really finally, build local browser profile and binding objects.
\r
573 // TODO: may need some bits here...
\r
576 catch (exception&) {
\r
588 void XMLApplication::cleanup()
\r
590 delete m_partyDefault;
\r
591 #ifdef HAVE_GOOD_STL
\r
592 for_each(m_partyMap.begin(),m_partyMap.end(),cleanup_pair<xstring,PropertySet>());
\r
594 for_each(m_partyMap.begin(),m_partyMap.end(),cleanup_pair<const XMLCh*,PropertySet>());
\r
596 for_each(m_handlers.begin(),m_handlers.end(),xmltooling::cleanup<Handler>());
\r
597 delete m_credResolver;
\r
598 delete m_attrResolver;
\r
603 short XMLApplication::acceptNode(const DOMNode* node) const
\r
605 if (XMLHelper::isNodeNamed(node,samlconstants::SAML20_NS,saml2::Attribute::LOCAL_NAME))
\r
606 return FILTER_REJECT;
\r
607 else if (XMLHelper::isNodeNamed(node,samlconstants::SAML20_NS,Audience::LOCAL_NAME))
\r
608 return FILTER_REJECT;
\r
609 const XMLCh* name=node->getLocalName();
\r
610 if (XMLString::equals(name,_Application) ||
\r
611 XMLString::equals(name,AssertionConsumerService::LOCAL_NAME) ||
\r
612 XMLString::equals(name,SingleLogoutService::LOCAL_NAME) ||
\r
613 XMLString::equals(name,ManageNameIDService::LOCAL_NAME) ||
\r
614 XMLString::equals(name,_SessionInitiator) ||
\r
615 XMLString::equals(name,DefaultRelyingParty) ||
\r
616 XMLString::equals(name,RelyingParty) ||
\r
617 XMLString::equals(name,_MetadataProvider) ||
\r
618 XMLString::equals(name,_TrustEngine) ||
\r
619 XMLString::equals(name,_CredentialResolver) ||
\r
620 XMLString::equals(name,_AttributeResolver))
\r
621 return FILTER_REJECT;
\r
623 return FILTER_ACCEPT;
\r
626 const PropertySet* XMLApplication::getRelyingParty(const EntityDescriptor* provider) const
\r
628 if (!m_partyDefault && m_base)
\r
629 return m_base->getRelyingParty(provider);
\r
630 else if (!provider)
\r
631 return m_partyDefault;
\r
633 #ifdef HAVE_GOOD_STL
\r
634 map<xstring,PropertySet*>::const_iterator i=m_partyMap.find(provider->getEntityID());
\r
635 if (i!=m_partyMap.end())
\r
637 const EntitiesDescriptor* group=dynamic_cast<const EntitiesDescriptor*>(provider->getParent());
\r
639 if (group->getName()) {
\r
640 i=m_partyMap.find(group->getName());
\r
641 if (i!=m_partyMap.end())
\r
644 group=dynamic_cast<const EntitiesDescriptor*>(group->getParent());
\r
647 map<const XMLCh*,PropertySet*>::const_iterator i=m_partyMap.begin();
\r
648 for (; i!=m_partyMap.end(); i++) {
\r
649 if (XMLString::equals(i->first,provider->getId()))
\r
651 const EntitiesDescriptor* group=dynamic_cast<const EntitiesDescriptor*>(provider->getParent());
\r
653 if (XMLString::equals(i->first,group->getName()))
\r
655 group=dynamic_cast<const EntitiesDescriptor*>(group->getParent());
\r
659 return m_partyDefault;
\r
662 const SessionInitiator* XMLApplication::getDefaultSessionInitiator() const
\r
664 if (m_sessionInitDefault) return m_sessionInitDefault;
\r
665 return m_base ? m_base->getDefaultSessionInitiator() : NULL;
\r
668 const SessionInitiator* XMLApplication::getSessionInitiatorById(const char* id) const
\r
670 map<string,const SessionInitiator*>::const_iterator i=m_sessionInitMap.find(id);
\r
671 if (i!=m_sessionInitMap.end()) return i->second;
\r
672 return m_base ? m_base->getSessionInitiatorById(id) : NULL;
\r
675 const Handler* XMLApplication::getDefaultAssertionConsumerService() const
\r
677 if (m_acsDefault) return m_acsDefault;
\r
678 return m_base ? m_base->getDefaultAssertionConsumerService() : NULL;
\r
681 const Handler* XMLApplication::getAssertionConsumerServiceByIndex(unsigned short index) const
\r
683 map<unsigned int,const Handler*>::const_iterator i=m_acsIndexMap.find(index);
\r
684 if (i!=m_acsIndexMap.end()) return i->second;
\r
685 return m_base ? m_base->getAssertionConsumerServiceByIndex(index) : NULL;
\r
688 const vector<const Handler*>& XMLApplication::getAssertionConsumerServicesByBinding(const XMLCh* binding) const
\r
690 #ifdef HAVE_GOOD_STL
\r
691 ACSBindingMap::const_iterator i=m_acsBindingMap.find(binding);
\r
693 auto_ptr_char temp(binding);
\r
694 ACSBindingMap::const_iterator i=m_acsBindingMap.find(temp.get());
\r
696 if (i!=m_acsBindingMap.end())
\r
698 return m_base ? m_base->getAssertionConsumerServicesByBinding(binding) : g_noHandlers;
\r
701 const Handler* XMLApplication::getHandler(const char* path) const
\r
704 map<string,const Handler*>::const_iterator i=m_handlerMap.find(wrap.substr(0,wrap.find('?')));
\r
705 if (i!=m_handlerMap.end())
\r
707 return m_base ? m_base->getHandler(path) : NULL;
\r
710 short XMLConfigImpl::acceptNode(const DOMNode* node) const
\r
712 if (!XMLString::equals(node->getNamespaceURI(),shibspconstants::SHIB2SPCONFIG_NS))
\r
713 return FILTER_ACCEPT;
\r
714 const XMLCh* name=node->getLocalName();
\r
715 if (XMLString::equals(name,Applications) ||
\r
716 XMLString::equals(name,_ArtifactMap) ||
\r
717 XMLString::equals(name,Extensions::LOCAL_NAME) ||
\r
718 XMLString::equals(name,Implementation) ||
\r
719 XMLString::equals(name,Listener) ||
\r
720 XMLString::equals(name,MemoryListener) ||
\r
721 XMLString::equals(name,Policy) ||
\r
722 XMLString::equals(name,_RequestMapper) ||
\r
723 XMLString::equals(name,_ReplayCache) ||
\r
724 XMLString::equals(name,_SessionCache) ||
\r
725 XMLString::equals(name,_StorageService) ||
\r
726 XMLString::equals(name,TCPListener) ||
\r
727 XMLString::equals(name,UnixListener))
\r
728 return FILTER_REJECT;
\r
730 return FILTER_ACCEPT;
\r
733 void XMLConfigImpl::doExtensions(const DOMElement* e, const char* label, Category& log)
\r
735 const DOMElement* exts=XMLHelper::getFirstChildElement(e,Extensions::LOCAL_NAME);
\r
737 exts=XMLHelper::getFirstChildElement(exts,Library);
\r
739 auto_ptr_char path(exts->getAttributeNS(NULL,_path));
\r
742 XMLToolingConfig::getConfig().load_library(path.get(),(void*)exts);
\r
743 log.debug("loaded %s extension library (%s)", label, path.get());
\r
746 catch (exception& e) {
\r
747 const XMLCh* fatal=exts->getAttributeNS(NULL,fatal);
\r
748 if (fatal && (*fatal==chLatin_t || *fatal==chDigit_1)) {
\r
749 log.fatal("unable to load mandatory %s extension library %s: %s", label, path.get(), e.what());
\r
753 log.crit("unable to load optional %s extension library %s: %s", label, path.get(), e.what());
\r
756 exts=XMLHelper::getNextSiblingElement(exts,Library);
\r
761 XMLConfigImpl::XMLConfigImpl(const DOMElement* e, bool first, const XMLConfig* outer) : m_requestMapper(NULL), m_outer(outer), m_document(NULL)
\r
764 xmltooling::NDC ndc("XMLConfigImpl");
\r
766 Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
\r
769 SPConfig& conf=SPConfig::getConfig();
\r
770 SAMLConfig& samlConf=SAMLConfig::getConfig();
\r
771 XMLToolingConfig& xmlConf=XMLToolingConfig::getConfig();
\r
772 const DOMElement* SHAR=XMLHelper::getFirstChildElement(e,OutOfProcess);
\r
773 const DOMElement* SHIRE=XMLHelper::getFirstChildElement(e,InProcess);
\r
775 // Initialize log4cpp manually in order to redirect log messages as soon as possible.
\r
776 if (conf.isEnabled(SPConfig::Logging)) {
\r
777 const XMLCh* logconf=NULL;
\r
778 if (conf.isEnabled(SPConfig::OutOfProcess))
\r
779 logconf=SHAR->getAttributeNS(NULL,logger);
\r
780 else if (conf.isEnabled(SPConfig::InProcess))
\r
781 logconf=SHIRE->getAttributeNS(NULL,logger);
\r
782 if (!logconf || !*logconf)
\r
783 logconf=e->getAttributeNS(NULL,logger);
\r
784 if (logconf && *logconf) {
\r
785 auto_ptr_char logpath(logconf);
\r
786 log.debug("loading new logging configuration from (%s), check log destination for status of configuration",logpath.get());
\r
787 XMLToolingConfig::getConfig().log_config(logpath.get());
\r
791 m_outer->m_tranLog = new TransactionLog();
\r
794 // First load any property sets.
\r
797 const DOMElement* child;
\r
800 // Much of the processing can only occur on the first instantiation.
\r
803 pair<bool,unsigned int> skew=getUnsignedInt("clockSkew");
\r
805 xmlConf.clock_skew_secs=skew.second;
\r
808 doExtensions(e, "global", log);
\r
809 if (conf.isEnabled(SPConfig::OutOfProcess))
\r
810 doExtensions(SHAR, "out of process", log);
\r
812 if (conf.isEnabled(SPConfig::InProcess))
\r
813 doExtensions(SHIRE, "in process", log);
\r
815 // Instantiate the ListenerService and SessionCache objects.
\r
816 if (conf.isEnabled(SPConfig::Listener)) {
\r
817 child=XMLHelper::getFirstChildElement(SHAR,UnixListener);
\r
819 plugtype=UNIX_LISTENER_SERVICE;
\r
821 child=XMLHelper::getFirstChildElement(SHAR,TCPListener);
\r
823 plugtype=TCP_LISTENER_SERVICE;
\r
825 child=XMLHelper::getFirstChildElement(SHAR,MemoryListener);
\r
827 plugtype=MEMORY_LISTENER_SERVICE;
\r
829 child=XMLHelper::getFirstChildElement(SHAR,Listener);
\r
831 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
833 plugtype=type.get();
\r
839 log.info("building ListenerService of type %s...", plugtype.c_str());
\r
840 m_outer->m_listener = conf.ListenerServiceManager.newPlugin(plugtype.c_str(),child);
\r
843 log.fatal("can't build ListenerService, missing conf:Listener element?");
\r
844 throw ConfigurationException("Can't build ListenerService, missing conf:Listener element?");
\r
848 if (conf.isEnabled(SPConfig::Caching)) {
\r
849 if (conf.isEnabled(SPConfig::OutOfProcess)) {
\r
850 // First build any StorageServices.
\r
852 child=XMLHelper::getFirstChildElement(SHAR,_StorageService);
\r
854 auto_ptr_char id(child->getAttributeNS(NULL,_id));
\r
855 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
857 log.info("building StorageService (%s) of type %s...", id.get(), type.get());
\r
858 m_outer->m_storage[id.get()] = xmlConf.StorageServiceManager.newPlugin(type.get(),child);
\r
859 if (!strcmp(type.get(),MEMORY_STORAGE_SERVICE))
\r
860 inmemID = id.get();
\r
862 catch (exception& ex) {
\r
863 log.crit("failed to instantiate StorageService (%s): %s", id.get(), ex.what());
\r
865 child=XMLHelper::getNextSiblingElement(child,_StorageService);
\r
868 child=XMLHelper::getFirstChildElement(SHAR,_SessionCache);
\r
870 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
871 log.info("building SessionCache of type %s...",type.get());
\r
872 m_outer->m_sessionCache=conf.SessionCacheManager.newPlugin(type.get(),child);
\r
875 log.warn("SessionCache unspecified, building SessionCache of type %s...",STORAGESERVICE_SESSION_CACHE);
\r
876 if (inmemID.empty()) {
\r
877 inmemID = "memory";
\r
878 log.info("no StorageServices configured, providing in-memory version for session cache");
\r
879 m_outer->m_storage[inmemID] = xmlConf.StorageServiceManager.newPlugin(MEMORY_STORAGE_SERVICE,NULL);
\r
881 child = e->getOwnerDocument()->createElementNS(NULL,_SessionCache);
\r
882 auto_ptr_XMLCh ssid(inmemID.c_str());
\r
883 const_cast<DOMElement*>(child)->setAttributeNS(NULL,_StorageService,ssid.get());
\r
884 m_outer->m_sessionCache=conf.SessionCacheManager.newPlugin(STORAGESERVICE_SESSION_CACHE,child);
\r
888 StorageService* replaySS=NULL;
\r
889 child=XMLHelper::getFirstChildElement(SHAR,_ReplayCache);
\r
891 auto_ptr_char ssid(child->getAttributeNS(NULL,_StorageService));
\r
892 if (ssid.get() && *ssid.get()) {
\r
893 if (m_outer->m_storage.count(ssid.get()))
\r
894 replaySS = m_outer->m_storage[ssid.get()];
\r
896 log.info("building ReplayCache on top of StorageService (%s)...", ssid.get());
\r
898 log.crit("unable to locate StorageService (%s) in configuration", ssid.get());
\r
902 log.info("building ReplayCache using in-memory StorageService...");
\r
903 if (inmemID.empty()) {
\r
904 inmemID = "memory";
\r
905 log.info("no StorageServices configured, providing in-memory version for legacy config");
\r
906 m_outer->m_storage[inmemID] = xmlConf.StorageServiceManager.newPlugin(MEMORY_STORAGE_SERVICE,NULL);
\r
908 replaySS = m_outer->m_storage[inmemID];
\r
910 xmlConf.setReplayCache(new ReplayCache(replaySS));
\r
913 child=XMLHelper::getFirstChildElement(SHAR,_ArtifactMap);
\r
915 auto_ptr_char ssid(child->getAttributeNS(NULL,_StorageService));
\r
916 if (ssid.get() && *ssid.get() && m_outer->m_storage.count(ssid.get())) {
\r
917 log.info("building ArtifactMap on top of StorageService (%s)...", ssid.get());
\r
918 samlConf.setArtifactMap(new ArtifactMap(child, m_outer->m_storage[ssid.get()]));
\r
921 if (samlConf.getArtifactMap()==NULL) {
\r
922 log.info("building in-memory ArtifactMap...");
\r
923 samlConf.setArtifactMap(new ArtifactMap(child));
\r
927 child=XMLHelper::getFirstChildElement(SHIRE,_SessionCache);
\r
929 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
930 log.info("building SessionCache of type %s...",type.get());
\r
931 m_outer->m_sessionCache=conf.SessionCacheManager.newPlugin(type.get(),child);
\r
934 log.warn("SessionCache unspecified, building SessionCache of type %s...",REMOTED_SESSION_CACHE);
\r
935 m_outer->m_sessionCache=conf.SessionCacheManager.newPlugin(REMOTED_SESSION_CACHE,child);
\r
939 } // end of first-time-only stuff
\r
941 // Back to the fully dynamic stuff...next up is the RequestMapper.
\r
942 if (conf.isEnabled(SPConfig::RequestMapping)) {
\r
943 child=XMLHelper::getFirstChildElement(SHIRE,_RequestMapper);
\r
945 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
946 log.info("building RequestMapper of type %s...",type.get());
\r
947 m_requestMapper=conf.RequestMapperManager.newPlugin(type.get(),child);
\r
951 // Load security policies.
\r
952 child = XMLHelper::getLastChildElement(e,SecurityPolicies);
\r
954 PolicyNodeFilter filter;
\r
955 child = XMLHelper::getFirstChildElement(child,Policy);
\r
957 auto_ptr_char id(child->getAttributeNS(NULL,_id));
\r
958 pair< PropertySet*,vector<const SecurityPolicyRule*> >& rules = m_policyMap[id.get()];
\r
959 rules.first = NULL;
\r
960 auto_ptr<DOMPropertySet> settings(new DOMPropertySet());
\r
961 settings->load(child, log, &filter);
\r
962 rules.first = settings.release();
\r
963 const DOMElement* rule = XMLHelper::getFirstChildElement(child,Rule);
\r
965 auto_ptr_char type(rule->getAttributeNS(NULL,_type));
\r
967 rules.second.push_back(samlConf.SecurityPolicyRuleManager.newPlugin(type.get(),rule));
\r
969 catch (exception& ex) {
\r
970 log.crit("error instantiating policy rule (%s) in policy (%s): %s", type.get(), id.get(), ex.what());
\r
972 rule = XMLHelper::getNextSiblingElement(rule,Rule);
\r
974 child = XMLHelper::getNextSiblingElement(child,Policy);
\r
978 // Load the default application. This actually has a fixed ID of "default". ;-)
\r
979 child=XMLHelper::getLastChildElement(e,Applications);
\r
981 log.fatal("can't build default Application object, missing conf:Applications element?");
\r
982 throw ConfigurationException("can't build default Application object, missing conf:Applications element?");
\r
984 XMLApplication* defapp=new XMLApplication(m_outer,child);
\r
985 m_appmap[defapp->getId()]=defapp;
\r
987 // Load any overrides.
\r
988 child = XMLHelper::getFirstChildElement(child,_Application);
\r
990 auto_ptr<XMLApplication> iapp(new XMLApplication(m_outer,child,defapp));
\r
991 if (m_appmap.count(iapp->getId()))
\r
992 log.crit("found conf:Application element with duplicate id attribute (%s), skipping it", iapp->getId());
\r
994 m_appmap[iapp->getId()]=iapp.release();
\r
996 child = XMLHelper::getNextSiblingElement(child,_Application);
\r
999 catch (exception&) {
\r
1000 this->~XMLConfigImpl();
\r
1005 this->~XMLConfigImpl();
\r
1011 XMLConfigImpl::~XMLConfigImpl()
\r
1013 for_each(m_appmap.begin(),m_appmap.end(),cleanup_pair<string,Application>());
\r
1014 for (map< string,pair<PropertySet*,vector<const SecurityPolicyRule*> > >::iterator i=m_policyMap.begin(); i!=m_policyMap.end(); ++i) {
\r
1015 delete i->second.first;
\r
1016 for_each(i->second.second.begin(), i->second.second.end(), xmltooling::cleanup<SecurityPolicyRule>());
\r
1018 delete m_requestMapper;
\r
1020 m_document->release();
\r
1023 pair<bool,DOMElement*> XMLConfig::load()
\r
1025 // Load from source using base class.
\r
1026 pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
\r
1028 // If we own it, wrap it.
\r
1029 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
\r
1031 XMLConfigImpl* impl = new XMLConfigImpl(raw.second,(m_impl==NULL),this);
\r
1033 // If we held the document, transfer it to the impl. If we didn't, it's a no-op.
\r
1034 impl->setDocument(docjanitor.release());
\r
1039 return make_pair(false,(DOMElement*)NULL);
\r