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/filtering/AttributeFilter.h"
\r
34 #include "attribute/resolver/AttributeExtractor.h"
\r
35 #include "attribute/resolver/AttributeResolver.h"
\r
36 #include "handler/SessionInitiator.h"
\r
37 #include "remoting/ListenerService.h"
\r
38 #include "security/PKIXTrustEngine.h"
\r
39 #include "util/DOMPropertySet.h"
\r
40 #include "util/SPConstants.h"
\r
42 #include <sys/types.h>
\r
43 #include <sys/stat.h>
\r
44 #include <log4cpp/Category.hh>
\r
45 #include <log4cpp/PropertyConfigurator.hh>
\r
46 #include <saml/SAMLConfig.h>
\r
47 #include <saml/binding/ArtifactMap.h>
\r
48 #include <saml/saml1/core/Assertions.h>
\r
49 #include <saml/saml2/metadata/ChainingMetadataProvider.h>
\r
50 #include <xmltooling/XMLToolingConfig.h>
\r
51 #include <xmltooling/security/ChainingTrustEngine.h>
\r
52 #include <xmltooling/util/NDC.h>
\r
53 #include <xmltooling/util/ReloadableXMLFile.h>
\r
54 #include <xmltooling/util/ReplayCache.h>
\r
56 using namespace shibsp;
\r
57 using namespace opensaml::saml2;
\r
58 using namespace opensaml::saml2md;
\r
59 using namespace opensaml;
\r
60 using namespace xmltooling;
\r
61 using namespace log4cpp;
\r
62 using namespace std;
\r
66 #if defined (_MSC_VER)
\r
67 #pragma warning( push )
\r
68 #pragma warning( disable : 4250 )
\r
71 static vector<const Handler*> g_noHandlers;
\r
73 // Application configuration wrapper
\r
74 class SHIBSP_DLLLOCAL XMLApplication : public virtual Application, public DOMPropertySet, public DOMNodeFilter
\r
77 XMLApplication(const ServiceProvider*, const DOMElement* e, const XMLApplication* base=NULL);
\r
78 ~XMLApplication() { cleanup(); }
\r
81 const ServiceProvider& getServiceProvider() const {return *m_sp;}
\r
82 const char* getId() const {return getString("id").second;}
\r
83 const char* getHash() const {return m_hash.c_str();}
\r
85 MetadataProvider* getMetadataProvider(bool required=true) const {
\r
86 if (required && !m_base && !m_metadata)
\r
87 throw ConfigurationException("No MetadataProvider available.");
\r
88 return (!m_metadata && m_base) ? m_base->getMetadataProvider() : m_metadata;
\r
90 TrustEngine* getTrustEngine(bool required=true) const {
\r
91 if (required && !m_base && !m_trust)
\r
92 throw ConfigurationException("No TrustEngine available.");
\r
93 return (!m_trust && m_base) ? m_base->getTrustEngine() : m_trust;
\r
95 AttributeExtractor* getAttributeExtractor() const {
\r
96 return (!m_attrExtractor && m_base) ? m_base->getAttributeExtractor() : m_attrExtractor;
\r
98 AttributeFilter* getAttributeFilter() const {
\r
99 return (!m_attrFilter && m_base) ? m_base->getAttributeFilter() : m_attrFilter;
\r
101 AttributeResolver* getAttributeResolver() const {
\r
102 return (!m_attrResolver && m_base) ? m_base->getAttributeResolver() : m_attrResolver;
\r
104 const set<string>& getRemoteUserAttributeIds() const {
\r
105 return (m_attributeIds.empty() && m_base) ? m_base->getRemoteUserAttributeIds() : m_attributeIds;
\r
107 CredentialResolver* getCredentialResolver() const {
\r
108 return (!m_credResolver && m_base) ? m_base->getCredentialResolver() : m_credResolver;
\r
110 const PropertySet* getRelyingParty(const EntityDescriptor* provider) const;
\r
112 const SessionInitiator* getDefaultSessionInitiator() const;
\r
113 const SessionInitiator* getSessionInitiatorById(const char* id) const;
\r
114 const Handler* getDefaultAssertionConsumerService() const;
\r
115 const Handler* getAssertionConsumerServiceByIndex(unsigned short index) const;
\r
116 const vector<const Handler*>& getAssertionConsumerServicesByBinding(const XMLCh* binding) const;
\r
117 const Handler* getHandler(const char* path) const;
\r
119 const vector<const XMLCh*>& getAudiences() const {
\r
120 return (m_audiences.empty() && m_base) ? m_base->getAudiences() : m_audiences;
\r
123 // Provides filter to exclude special config elements.
\r
124 short acceptNode(const DOMNode* node) const;
\r
128 const ServiceProvider* m_sp; // this is ok because its locking scope includes us
\r
129 const XMLApplication* m_base;
\r
131 MetadataProvider* m_metadata;
\r
132 TrustEngine* m_trust;
\r
133 AttributeExtractor* m_attrExtractor;
\r
134 AttributeFilter* m_attrFilter;
\r
135 AttributeResolver* m_attrResolver;
\r
136 CredentialResolver* m_credResolver;
\r
137 vector<const XMLCh*> m_audiences;
\r
138 set<string> m_attributeIds;
\r
140 // manage handler objects
\r
141 vector<Handler*> m_handlers;
\r
143 // maps location (path info) to applicable handlers
\r
144 map<string,const Handler*> m_handlerMap;
\r
146 // maps unique indexes to consumer services
\r
147 map<unsigned int,const Handler*> m_acsIndexMap;
\r
149 // pointer to default consumer service
\r
150 const Handler* m_acsDefault;
\r
152 // maps binding strings to supporting consumer service(s)
\r
153 #ifdef HAVE_GOOD_STL
\r
154 typedef map<xstring,vector<const Handler*> > ACSBindingMap;
\r
156 typedef map<string,vector<const Handler*> > ACSBindingMap;
\r
158 ACSBindingMap m_acsBindingMap;
\r
160 // pointer to default session initiator
\r
161 const SessionInitiator* m_sessionInitDefault;
\r
163 // maps unique ID strings to session initiators
\r
164 map<string,const SessionInitiator*> m_sessionInitMap;
\r
166 // RelyingParty properties
\r
167 DOMPropertySet* m_partyDefault;
\r
168 #ifdef HAVE_GOOD_STL
\r
169 map<xstring,PropertySet*> m_partyMap;
\r
171 map<const XMLCh*,PropertySet*> m_partyMap;
\r
175 // Top-level configuration implementation
\r
176 class SHIBSP_DLLLOCAL XMLConfig;
\r
177 class SHIBSP_DLLLOCAL XMLConfigImpl : public DOMPropertySet, public DOMNodeFilter
\r
180 XMLConfigImpl(const DOMElement* e, bool first, const XMLConfig* outer, Category& log);
\r
183 RequestMapper* m_requestMapper;
\r
184 map<string,Application*> m_appmap;
\r
185 map< string,pair< PropertySet*,vector<const SecurityPolicyRule*> > > m_policyMap;
\r
187 // Provides filter to exclude special config elements.
\r
188 short acceptNode(const DOMNode* node) const;
\r
190 void setDocument(DOMDocument* doc) {
\r
195 void doExtensions(const DOMElement* e, const char* label, Category& log);
\r
197 const XMLConfig* m_outer;
\r
198 DOMDocument* m_document;
\r
201 class SHIBSP_DLLLOCAL XMLConfig : public ServiceProvider, public ReloadableXMLFile
\r
204 XMLConfig(const DOMElement* e) : ReloadableXMLFile(e, Category::getInstance(SHIBSP_LOGCAT".Config")),
\r
205 m_impl(NULL), m_listener(NULL), m_sessionCache(NULL), m_tranLog(NULL) {
\r
214 delete m_sessionCache;
\r
217 XMLToolingConfig::getConfig().setReplayCache(NULL);
\r
218 SAMLConfig::getConfig().setArtifactMap(NULL);
\r
219 for_each(m_storage.begin(), m_storage.end(), cleanup_pair<string,StorageService>());
\r
223 void setParent(const PropertySet* parent) {return m_impl->setParent(parent);}
\r
224 pair<bool,bool> getBool(const char* name, const char* ns=NULL) const {return m_impl->getBool(name,ns);}
\r
225 pair<bool,const char*> getString(const char* name, const char* ns=NULL) const {return m_impl->getString(name,ns);}
\r
226 pair<bool,const XMLCh*> getXMLString(const char* name, const char* ns=NULL) const {return m_impl->getXMLString(name,ns);}
\r
227 pair<bool,unsigned int> getUnsignedInt(const char* name, const char* ns=NULL) const {return m_impl->getUnsignedInt(name,ns);}
\r
228 pair<bool,int> getInt(const char* name, const char* ns=NULL) const {return m_impl->getInt(name,ns);}
\r
229 const PropertySet* getPropertySet(const char* name, const char* ns="urn:mace:shibboleth:2.0:native:sp:config") const {return m_impl->getPropertySet(name,ns);}
\r
230 const DOMElement* getElement() const {return m_impl->getElement();}
\r
233 TransactionLog* getTransactionLog() const {
\r
236 throw ConfigurationException("No TransactionLog available.");
\r
239 StorageService* getStorageService(const char* id) const {
\r
241 map<string,StorageService*>::const_iterator i=m_storage.find(id);
\r
242 if (i!=m_storage.end())
\r
248 ListenerService* getListenerService(bool required=true) const {
\r
249 if (required && !m_listener)
\r
250 throw ConfigurationException("No ListenerService available.");
\r
254 SessionCache* getSessionCache(bool required=true) const {
\r
255 if (required && !m_sessionCache)
\r
256 throw ConfigurationException("No SessionCache available.");
\r
257 return m_sessionCache;
\r
260 RequestMapper* getRequestMapper(bool required=true) const {
\r
261 if (required && !m_impl->m_requestMapper)
\r
262 throw ConfigurationException("No RequestMapper available.");
\r
263 return m_impl->m_requestMapper;
\r
266 const Application* getApplication(const char* applicationId) const {
\r
267 map<string,Application*>::const_iterator i=m_impl->m_appmap.find(applicationId);
\r
268 return (i!=m_impl->m_appmap.end()) ? i->second : NULL;
\r
271 const PropertySet* getPolicySettings(const char* id) const {
\r
272 map<string,pair<PropertySet*,vector<const SecurityPolicyRule*> > >::const_iterator i = m_impl->m_policyMap.find(id);
\r
273 if (i!=m_impl->m_policyMap.end())
\r
274 return i->second.first;
\r
275 throw ConfigurationException("Security Policy ($1) not found, check <SecurityPolicies> element.", params(1,id));
\r
278 const vector<const SecurityPolicyRule*>& getPolicyRules(const char* id) const {
\r
279 map<string,pair<PropertySet*,vector<const SecurityPolicyRule*> > >::const_iterator i = m_impl->m_policyMap.find(id);
\r
280 if (i!=m_impl->m_policyMap.end())
\r
281 return i->second.second;
\r
282 throw ConfigurationException("Security Policy ($1) not found, check <SecurityPolicies> element.", params(1,id));
\r
286 pair<bool,DOMElement*> load();
\r
289 friend class XMLConfigImpl;
\r
290 XMLConfigImpl* m_impl;
\r
291 mutable ListenerService* m_listener;
\r
292 mutable SessionCache* m_sessionCache;
\r
293 mutable TransactionLog* m_tranLog;
\r
294 mutable map<string,StorageService*> m_storage;
\r
297 #if defined (_MSC_VER)
\r
298 #pragma warning( pop )
\r
301 static const XMLCh _Application[] = UNICODE_LITERAL_11(A,p,p,l,i,c,a,t,i,o,n);
\r
302 static const XMLCh Applications[] = UNICODE_LITERAL_12(A,p,p,l,i,c,a,t,i,o,n,s);
\r
303 static const XMLCh _ArtifactMap[] = UNICODE_LITERAL_11(A,r,t,i,f,a,c,t,M,a,p);
\r
304 static const XMLCh _AttributeExtractor[] = UNICODE_LITERAL_18(A,t,t,r,i,b,u,t,e,E,x,t,r,a,c,t,o,r);
\r
305 static const XMLCh _AttributeFilter[] = UNICODE_LITERAL_15(A,t,t,r,i,b,u,t,e,F,i,l,t,e,r);
\r
306 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
307 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
308 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
309 static const XMLCh fatal[] = UNICODE_LITERAL_5(f,a,t,a,l);
\r
310 static const XMLCh _Handler[] = UNICODE_LITERAL_7(H,a,n,d,l,e,r);
\r
311 static const XMLCh _id[] = UNICODE_LITERAL_2(i,d);
\r
312 static const XMLCh Implementation[] = UNICODE_LITERAL_14(I,m,p,l,e,m,e,n,t,a,t,i,o,n);
\r
313 static const XMLCh InProcess[] = UNICODE_LITERAL_9(I,n,P,r,o,c,e,s,s);
\r
314 static const XMLCh Library[] = UNICODE_LITERAL_7(L,i,b,r,a,r,y);
\r
315 static const XMLCh Listener[] = UNICODE_LITERAL_8(L,i,s,t,e,n,e,r);
\r
316 static const XMLCh logger[] = UNICODE_LITERAL_6(l,o,g,g,e,r);
\r
317 static const XMLCh MemoryListener[] = UNICODE_LITERAL_14(M,e,m,o,r,y,L,i,s,t,e,n,e,r);
\r
318 static const XMLCh _MetadataProvider[] = UNICODE_LITERAL_16(M,e,t,a,d,a,t,a,P,r,o,v,i,d,e,r);
\r
319 static const XMLCh OutOfProcess[] = UNICODE_LITERAL_12(O,u,t,O,f,P,r,o,c,e,s,s);
\r
320 static const XMLCh _path[] = UNICODE_LITERAL_4(p,a,t,h);
\r
321 static const XMLCh Policy[] = UNICODE_LITERAL_6(P,o,l,i,c,y);
\r
322 static const XMLCh RelyingParty[] = UNICODE_LITERAL_12(R,e,l,y,i,n,g,P,a,r,t,y);
\r
323 static const XMLCh _ReplayCache[] = UNICODE_LITERAL_11(R,e,p,l,a,y,C,a,c,h,e);
\r
324 static const XMLCh _RequestMapper[] = UNICODE_LITERAL_13(R,e,q,u,e,s,t,M,a,p,p,e,r);
\r
325 static const XMLCh Rule[] = UNICODE_LITERAL_4(R,u,l,e);
\r
326 static const XMLCh SecurityPolicies[] = UNICODE_LITERAL_16(S,e,c,u,r,i,t,y,P,o,l,i,c,i,e,s);
\r
327 static const XMLCh _SessionCache[] = UNICODE_LITERAL_12(S,e,s,s,i,o,n,C,a,c,h,e);
\r
328 static const XMLCh _SessionInitiator[] = UNICODE_LITERAL_16(S,e,s,s,i,o,n,I,n,i,t,i,a,t,o,r);
\r
329 static const XMLCh _StorageService[] = UNICODE_LITERAL_14(S,t,o,r,a,g,e,S,e,r,v,i,c,e);
\r
330 static const XMLCh TCPListener[] = UNICODE_LITERAL_11(T,C,P,L,i,s,t,e,n,e,r);
\r
331 static const XMLCh _TrustEngine[] = UNICODE_LITERAL_11(T,r,u,s,t,E,n,g,i,n,e);
\r
332 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
\r
333 static const XMLCh UnixListener[] = UNICODE_LITERAL_12(U,n,i,x,L,i,s,t,e,n,e,r);
\r
335 class SHIBSP_DLLLOCAL PolicyNodeFilter : public DOMNodeFilter
\r
338 short acceptNode(const DOMNode* node) const {
\r
339 if (XMLHelper::isNodeNamed(node,shibspconstants::SHIB2SPCONFIG_NS,Rule))
\r
340 return FILTER_REJECT;
\r
341 return FILTER_ACCEPT;
\r
347 ServiceProvider* XMLServiceProviderFactory(const DOMElement* const & e)
\r
349 return new XMLConfig(e);
\r
353 XMLApplication::XMLApplication(
\r
354 const ServiceProvider* sp,
\r
355 const DOMElement* e,
\r
356 const XMLApplication* base
\r
357 ) : m_sp(sp), m_base(base), m_metadata(NULL), m_trust(NULL), m_attrExtractor(NULL), m_attrFilter(NULL), m_attrResolver(NULL),
\r
358 m_credResolver(NULL), m_partyDefault(NULL), m_sessionInitDefault(NULL), m_acsDefault(NULL)
\r
361 xmltooling::NDC ndc("XMLApplication");
\r
363 Category& log=Category::getInstance(SHIBSP_LOGCAT".Application");
\r
366 // First load any property sets.
\r
371 SPConfig& conf=SPConfig::getConfig();
\r
372 SAMLConfig& samlConf=SAMLConfig::getConfig();
\r
373 XMLToolingConfig& xmlConf=XMLToolingConfig::getConfig();
\r
376 m_hash+=getString("entityID").second;
\r
377 m_hash=samlConf.hashSHA1(m_hash.c_str(), true);
\r
379 pair<bool,const char*> attributes = getString("REMOTE_USER");
\r
380 if (attributes.first) {
\r
381 char* dup = strdup(attributes.second);
\r
384 while (start && *start) {
\r
385 while (*start && isspace(*start))
\r
389 pos = strchr(start,' ');
\r
392 m_attributeIds.insert(start);
\r
393 start = pos ? pos+1 : NULL;
\r
398 const PropertySet* sessions = getPropertySet("Sessions");
\r
400 // Process handlers.
\r
401 Handler* handler=NULL;
\r
402 bool hardACS=false, hardSessionInit=false;
\r
403 const DOMElement* child = sessions ? XMLHelper::getFirstChildElement(sessions->getElement()) : NULL;
\r
406 // A handler is based on the Binding property in conjunction with the element name.
\r
407 // If it's an ACS or SI, also handle index/id mappings and defaulting.
\r
408 if (XMLHelper::isNodeNamed(child,samlconstants::SAML20MD_NS,AssertionConsumerService::LOCAL_NAME)) {
\r
409 auto_ptr_char bindprop(child->getAttributeNS(NULL,EndpointType::BINDING_ATTRIB_NAME));
\r
410 if (!bindprop.get() || !*(bindprop.get())) {
\r
411 log.warn("md:AssertionConsumerService element has no Binding attribute, skipping it...");
\r
412 child = XMLHelper::getNextSiblingElement(child);
\r
415 handler=conf.AssertionConsumerServiceManager.newPlugin(bindprop.get(),make_pair(child, getId()));
\r
416 // Map by binding (may be > 1 per binding, e.g. SAML 1.0 vs 1.1)
\r
417 #ifdef HAVE_GOOD_STL
\r
418 m_acsBindingMap[handler->getXMLString("Binding").second].push_back(handler);
\r
420 m_acsBindingMap[handler->getString("Binding").second].push_back(handler);
\r
422 m_acsIndexMap[handler->getUnsignedInt("index").second]=handler;
\r
425 pair<bool,bool> defprop=handler->getBool("isDefault");
\r
426 if (defprop.first) {
\r
427 if (defprop.second) {
\r
429 m_acsDefault=handler;
\r
432 else if (!m_acsDefault)
\r
433 m_acsDefault=handler;
\r
436 else if (XMLString::equals(child->getLocalName(),_SessionInitiator)) {
\r
437 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
438 if (!type.get() || !*(type.get())) {
\r
439 log.warn("SessionInitiator element has no type attribute, skipping it...");
\r
440 child = XMLHelper::getNextSiblingElement(child);
\r
443 SessionInitiator* sihandler=conf.SessionInitiatorManager.newPlugin(type.get(),make_pair(child, getId()));
\r
445 pair<bool,const char*> si_id=handler->getString("id");
\r
446 if (si_id.first && si_id.second)
\r
447 m_sessionInitMap[si_id.second]=sihandler;
\r
448 if (!hardSessionInit) {
\r
449 pair<bool,bool> defprop=handler->getBool("isDefault");
\r
450 if (defprop.first) {
\r
451 if (defprop.second) {
\r
452 hardSessionInit=true;
\r
453 m_sessionInitDefault=sihandler;
\r
456 else if (!m_sessionInitDefault)
\r
457 m_sessionInitDefault=sihandler;
\r
460 else if (XMLHelper::isNodeNamed(child,samlconstants::SAML20MD_NS,SingleLogoutService::LOCAL_NAME)) {
\r
461 auto_ptr_char bindprop(child->getAttributeNS(NULL,EndpointType::BINDING_ATTRIB_NAME));
\r
462 if (!bindprop.get() || !*(bindprop.get())) {
\r
463 log.warn("md:SingleLogoutService element has no Binding attribute, skipping it...");
\r
464 child = XMLHelper::getNextSiblingElement(child);
\r
467 handler=conf.SingleLogoutServiceManager.newPlugin(bindprop.get(),make_pair(child, getId()));
\r
469 else if (XMLHelper::isNodeNamed(child,samlconstants::SAML20MD_NS,ManageNameIDService::LOCAL_NAME)) {
\r
470 auto_ptr_char bindprop(child->getAttributeNS(NULL,EndpointType::BINDING_ATTRIB_NAME));
\r
471 if (!bindprop.get() || !*(bindprop.get())) {
\r
472 log.warn("md:ManageNameIDService element has no Binding attribute, skipping it...");
\r
473 child = XMLHelper::getNextSiblingElement(child);
\r
476 handler=conf.ManageNameIDServiceManager.newPlugin(bindprop.get(),make_pair(child, getId()));
\r
479 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
480 if (!type.get() || !*(type.get())) {
\r
481 log.warn("Handler element has no type attribute, skipping it...");
\r
482 child = XMLHelper::getNextSiblingElement(child);
\r
485 handler=conf.HandlerManager.newPlugin(type.get(),make_pair(child, getId()));
\r
488 // Save off the objects after giving the property set to the handler for its use.
\r
489 m_handlers.push_back(handler);
\r
491 // Insert into location map.
\r
492 pair<bool,const char*> location=handler->getString("Location");
\r
493 if (location.first && *location.second == '/')
\r
494 m_handlerMap[location.second]=handler;
\r
495 else if (location.first)
\r
496 m_handlerMap[string("/") + location.second]=handler;
\r
499 catch (exception& ex) {
\r
500 log.error("caught exception processing handler element: %s", ex.what());
\r
503 child = XMLHelper::getNextSiblingElement(child);
\r
506 DOMNodeList* nlist=e->getElementsByTagNameNS(samlconstants::SAML20_NS,Audience::LOCAL_NAME);
\r
507 for (XMLSize_t i=0; nlist && i<nlist->getLength(); i++)
\r
508 if (nlist->item(i)->getParentNode()->isSameNode(e) && nlist->item(i)->hasChildNodes())
\r
509 m_audiences.push_back(nlist->item(i)->getFirstChild()->getNodeValue());
\r
511 // Always include our own entityID as an audience.
\r
512 m_audiences.push_back(getXMLString("entityID").second);
\r
514 if (conf.isEnabled(SPConfig::Metadata)) {
\r
515 child = XMLHelper::getFirstChildElement(e,_MetadataProvider);
\r
517 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
518 log.info("building MetadataProvider of type %s...",type.get());
\r
520 auto_ptr<MetadataProvider> mp(samlConf.MetadataProviderManager.newPlugin(type.get(),child));
\r
522 m_metadata = mp.release();
\r
524 catch (exception& ex) {
\r
525 log.crit("error building/initializing MetadataProvider: %s", ex.what());
\r
530 if (conf.isEnabled(SPConfig::Trust)) {
\r
531 child = XMLHelper::getFirstChildElement(e,_TrustEngine);
\r
533 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
534 log.info("building TrustEngine of type %s...",type.get());
\r
536 m_trust = xmlConf.TrustEngineManager.newPlugin(type.get(),child);
\r
538 catch (exception& ex) {
\r
539 log.crit("error building TrustEngine: %s", ex.what());
\r
544 if (conf.isEnabled(SPConfig::AttributeResolution)) {
\r
545 child = XMLHelper::getFirstChildElement(e,_AttributeExtractor);
\r
547 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
548 log.info("building AttributeExtractor of type %s...",type.get());
\r
550 m_attrExtractor = conf.AttributeExtractorManager.newPlugin(type.get(),child);
\r
552 catch (exception& ex) {
\r
553 log.crit("error building AttributeExtractor: %s", ex.what());
\r
557 child = XMLHelper::getFirstChildElement(e,_AttributeFilter);
\r
559 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
560 log.info("building AttributeFilter of type %s...",type.get());
\r
562 m_attrFilter = conf.AttributeFilterManager.newPlugin(type.get(),child);
\r
564 catch (exception& ex) {
\r
565 log.crit("error building AttributeFilter: %s", ex.what());
\r
569 child = XMLHelper::getFirstChildElement(e,_AttributeResolver);
\r
571 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
572 log.info("building AttributeResolver of type %s...",type.get());
\r
574 m_attrResolver = conf.AttributeResolverManager.newPlugin(type.get(),child);
\r
576 catch (exception& ex) {
\r
577 log.crit("error building AttributeResolver: %s", ex.what());
\r
582 if (conf.isEnabled(SPConfig::Credentials)) {
\r
583 child = XMLHelper::getFirstChildElement(e,_CredentialResolver);
\r
585 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
586 log.info("building CredentialResolver of type %s...",type.get());
\r
588 m_credResolver = xmlConf.CredentialResolverManager.newPlugin(type.get(),child);
\r
590 catch (exception& ex) {
\r
591 log.crit("error building CredentialResolver: %s", ex.what());
\r
597 // Finally, load relying parties.
\r
598 child = XMLHelper::getFirstChildElement(e,DefaultRelyingParty);
\r
600 m_partyDefault=new DOMPropertySet();
\r
601 m_partyDefault->load(child,log,this);
\r
602 child = XMLHelper::getFirstChildElement(child,RelyingParty);
\r
604 auto_ptr<DOMPropertySet> rp(new DOMPropertySet());
\r
605 rp->load(child,log,this);
\r
606 m_partyMap[child->getAttributeNS(NULL,saml2::Attribute::NAME_ATTRIB_NAME)]=rp.release();
\r
607 child = XMLHelper::getNextSiblingElement(child,RelyingParty);
\r
611 if (conf.isEnabled(SPConfig::OutOfProcess)) {
\r
612 // Really finally, build local browser profile and binding objects.
\r
613 // TODO: may need some bits here...
\r
616 catch (exception&) {
\r
628 void XMLApplication::cleanup()
\r
630 delete m_partyDefault;
\r
631 #ifdef HAVE_GOOD_STL
\r
632 for_each(m_partyMap.begin(),m_partyMap.end(),cleanup_pair<xstring,PropertySet>());
\r
634 for_each(m_partyMap.begin(),m_partyMap.end(),cleanup_pair<const XMLCh*,PropertySet>());
\r
636 for_each(m_handlers.begin(),m_handlers.end(),xmltooling::cleanup<Handler>());
\r
637 delete m_credResolver;
\r
638 delete m_attrResolver;
\r
639 delete m_attrFilter;
\r
640 delete m_attrExtractor;
\r
645 short XMLApplication::acceptNode(const DOMNode* node) const
\r
647 if (XMLHelper::isNodeNamed(node,samlconstants::SAML20_NS,saml2::Attribute::LOCAL_NAME))
\r
648 return FILTER_REJECT;
\r
649 else if (XMLHelper::isNodeNamed(node,samlconstants::SAML20_NS,Audience::LOCAL_NAME))
\r
650 return FILTER_REJECT;
\r
651 const XMLCh* name=node->getLocalName();
\r
652 if (XMLString::equals(name,_Application) ||
\r
653 XMLString::equals(name,AssertionConsumerService::LOCAL_NAME) ||
\r
654 XMLString::equals(name,SingleLogoutService::LOCAL_NAME) ||
\r
655 XMLString::equals(name,ManageNameIDService::LOCAL_NAME) ||
\r
656 XMLString::equals(name,_SessionInitiator) ||
\r
657 XMLString::equals(name,DefaultRelyingParty) ||
\r
658 XMLString::equals(name,RelyingParty) ||
\r
659 XMLString::equals(name,_MetadataProvider) ||
\r
660 XMLString::equals(name,_TrustEngine) ||
\r
661 XMLString::equals(name,_CredentialResolver) ||
\r
662 XMLString::equals(name,_AttributeFilter) ||
\r
663 XMLString::equals(name,_AttributeExtractor) ||
\r
664 XMLString::equals(name,_AttributeResolver))
\r
665 return FILTER_REJECT;
\r
667 return FILTER_ACCEPT;
\r
670 const PropertySet* XMLApplication::getRelyingParty(const EntityDescriptor* provider) const
\r
672 if (!m_partyDefault && m_base)
\r
673 return m_base->getRelyingParty(provider);
\r
674 else if (!provider)
\r
675 return m_partyDefault;
\r
677 #ifdef HAVE_GOOD_STL
\r
678 map<xstring,PropertySet*>::const_iterator i=m_partyMap.find(provider->getEntityID());
\r
679 if (i!=m_partyMap.end())
\r
681 const EntitiesDescriptor* group=dynamic_cast<const EntitiesDescriptor*>(provider->getParent());
\r
683 if (group->getName()) {
\r
684 i=m_partyMap.find(group->getName());
\r
685 if (i!=m_partyMap.end())
\r
688 group=dynamic_cast<const EntitiesDescriptor*>(group->getParent());
\r
691 map<const XMLCh*,PropertySet*>::const_iterator i=m_partyMap.begin();
\r
692 for (; i!=m_partyMap.end(); i++) {
\r
693 if (XMLString::equals(i->first,provider->getId()))
\r
695 const EntitiesDescriptor* group=dynamic_cast<const EntitiesDescriptor*>(provider->getParent());
\r
697 if (XMLString::equals(i->first,group->getName()))
\r
699 group=dynamic_cast<const EntitiesDescriptor*>(group->getParent());
\r
703 return m_partyDefault;
\r
706 const SessionInitiator* XMLApplication::getDefaultSessionInitiator() const
\r
708 if (m_sessionInitDefault) return m_sessionInitDefault;
\r
709 return m_base ? m_base->getDefaultSessionInitiator() : NULL;
\r
712 const SessionInitiator* XMLApplication::getSessionInitiatorById(const char* id) const
\r
714 map<string,const SessionInitiator*>::const_iterator i=m_sessionInitMap.find(id);
\r
715 if (i!=m_sessionInitMap.end()) return i->second;
\r
716 return m_base ? m_base->getSessionInitiatorById(id) : NULL;
\r
719 const Handler* XMLApplication::getDefaultAssertionConsumerService() const
\r
721 if (m_acsDefault) return m_acsDefault;
\r
722 return m_base ? m_base->getDefaultAssertionConsumerService() : NULL;
\r
725 const Handler* XMLApplication::getAssertionConsumerServiceByIndex(unsigned short index) const
\r
727 map<unsigned int,const Handler*>::const_iterator i=m_acsIndexMap.find(index);
\r
728 if (i!=m_acsIndexMap.end()) return i->second;
\r
729 return m_base ? m_base->getAssertionConsumerServiceByIndex(index) : NULL;
\r
732 const vector<const Handler*>& XMLApplication::getAssertionConsumerServicesByBinding(const XMLCh* binding) const
\r
734 #ifdef HAVE_GOOD_STL
\r
735 ACSBindingMap::const_iterator i=m_acsBindingMap.find(binding);
\r
737 auto_ptr_char temp(binding);
\r
738 ACSBindingMap::const_iterator i=m_acsBindingMap.find(temp.get());
\r
740 if (i!=m_acsBindingMap.end())
\r
742 return m_base ? m_base->getAssertionConsumerServicesByBinding(binding) : g_noHandlers;
\r
745 const Handler* XMLApplication::getHandler(const char* path) const
\r
748 map<string,const Handler*>::const_iterator i=m_handlerMap.find(wrap.substr(0,wrap.find('?')));
\r
749 if (i!=m_handlerMap.end())
\r
751 return m_base ? m_base->getHandler(path) : NULL;
\r
754 short XMLConfigImpl::acceptNode(const DOMNode* node) const
\r
756 if (!XMLString::equals(node->getNamespaceURI(),shibspconstants::SHIB2SPCONFIG_NS))
\r
757 return FILTER_ACCEPT;
\r
758 const XMLCh* name=node->getLocalName();
\r
759 if (XMLString::equals(name,Applications) ||
\r
760 XMLString::equals(name,_ArtifactMap) ||
\r
761 XMLString::equals(name,Extensions::LOCAL_NAME) ||
\r
762 XMLString::equals(name,Implementation) ||
\r
763 XMLString::equals(name,Listener) ||
\r
764 XMLString::equals(name,MemoryListener) ||
\r
765 XMLString::equals(name,Policy) ||
\r
766 XMLString::equals(name,_RequestMapper) ||
\r
767 XMLString::equals(name,_ReplayCache) ||
\r
768 XMLString::equals(name,_SessionCache) ||
\r
769 XMLString::equals(name,_StorageService) ||
\r
770 XMLString::equals(name,TCPListener) ||
\r
771 XMLString::equals(name,UnixListener))
\r
772 return FILTER_REJECT;
\r
774 return FILTER_ACCEPT;
\r
777 void XMLConfigImpl::doExtensions(const DOMElement* e, const char* label, Category& log)
\r
779 const DOMElement* exts=XMLHelper::getFirstChildElement(e,Extensions::LOCAL_NAME);
\r
781 exts=XMLHelper::getFirstChildElement(exts,Library);
\r
783 auto_ptr_char path(exts->getAttributeNS(NULL,_path));
\r
786 XMLToolingConfig::getConfig().load_library(path.get(),(void*)exts);
\r
787 log.debug("loaded %s extension library (%s)", label, path.get());
\r
790 catch (exception& e) {
\r
791 const XMLCh* fatal=exts->getAttributeNS(NULL,fatal);
\r
792 if (fatal && (*fatal==chLatin_t || *fatal==chDigit_1)) {
\r
793 log.fatal("unable to load mandatory %s extension library %s: %s", label, path.get(), e.what());
\r
797 log.crit("unable to load optional %s extension library %s: %s", label, path.get(), e.what());
\r
800 exts=XMLHelper::getNextSiblingElement(exts,Library);
\r
805 XMLConfigImpl::XMLConfigImpl(const DOMElement* e, bool first, const XMLConfig* outer, Category& log)
\r
806 : m_requestMapper(NULL), m_outer(outer), m_document(NULL)
\r
809 xmltooling::NDC ndc("XMLConfigImpl");
\r
813 SPConfig& conf=SPConfig::getConfig();
\r
814 SAMLConfig& samlConf=SAMLConfig::getConfig();
\r
815 XMLToolingConfig& xmlConf=XMLToolingConfig::getConfig();
\r
816 const DOMElement* SHAR=XMLHelper::getFirstChildElement(e,OutOfProcess);
\r
817 const DOMElement* SHIRE=XMLHelper::getFirstChildElement(e,InProcess);
\r
819 // Initialize log4cpp manually in order to redirect log messages as soon as possible.
\r
820 if (conf.isEnabled(SPConfig::Logging)) {
\r
821 const XMLCh* logconf=NULL;
\r
822 if (conf.isEnabled(SPConfig::OutOfProcess))
\r
823 logconf=SHAR->getAttributeNS(NULL,logger);
\r
824 else if (conf.isEnabled(SPConfig::InProcess))
\r
825 logconf=SHIRE->getAttributeNS(NULL,logger);
\r
826 if (!logconf || !*logconf)
\r
827 logconf=e->getAttributeNS(NULL,logger);
\r
828 if (logconf && *logconf) {
\r
829 auto_ptr_char logpath(logconf);
\r
830 log.debug("loading new logging configuration from (%s), check log destination for status of configuration",logpath.get());
\r
831 XMLToolingConfig::getConfig().log_config(logpath.get());
\r
835 m_outer->m_tranLog = new TransactionLog();
\r
838 // First load any property sets.
\r
841 const DOMElement* child;
\r
844 // Much of the processing can only occur on the first instantiation.
\r
847 pair<bool,unsigned int> skew=getUnsignedInt("clockSkew");
\r
849 xmlConf.clock_skew_secs=skew.second;
\r
852 doExtensions(e, "global", log);
\r
853 if (conf.isEnabled(SPConfig::OutOfProcess))
\r
854 doExtensions(SHAR, "out of process", log);
\r
856 if (conf.isEnabled(SPConfig::InProcess))
\r
857 doExtensions(SHIRE, "in process", log);
\r
859 // Instantiate the ListenerService and SessionCache objects.
\r
860 if (conf.isEnabled(SPConfig::Listener)) {
\r
861 child=XMLHelper::getFirstChildElement(SHAR,UnixListener);
\r
863 plugtype=UNIX_LISTENER_SERVICE;
\r
865 child=XMLHelper::getFirstChildElement(SHAR,TCPListener);
\r
867 plugtype=TCP_LISTENER_SERVICE;
\r
869 child=XMLHelper::getFirstChildElement(SHAR,MemoryListener);
\r
871 plugtype=MEMORY_LISTENER_SERVICE;
\r
873 child=XMLHelper::getFirstChildElement(SHAR,Listener);
\r
875 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
877 plugtype=type.get();
\r
883 log.info("building ListenerService of type %s...", plugtype.c_str());
\r
884 m_outer->m_listener = conf.ListenerServiceManager.newPlugin(plugtype.c_str(),child);
\r
887 log.fatal("can't build ListenerService, missing conf:Listener element?");
\r
888 throw ConfigurationException("Can't build ListenerService, missing conf:Listener element?");
\r
892 if (conf.isEnabled(SPConfig::Caching)) {
\r
893 if (conf.isEnabled(SPConfig::OutOfProcess)) {
\r
894 // First build any StorageServices.
\r
896 child=XMLHelper::getFirstChildElement(SHAR,_StorageService);
\r
898 auto_ptr_char id(child->getAttributeNS(NULL,_id));
\r
899 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
901 log.info("building StorageService (%s) of type %s...", id.get(), type.get());
\r
902 m_outer->m_storage[id.get()] = xmlConf.StorageServiceManager.newPlugin(type.get(),child);
\r
903 if (!strcmp(type.get(),MEMORY_STORAGE_SERVICE))
\r
904 inmemID = id.get();
\r
906 catch (exception& ex) {
\r
907 log.crit("failed to instantiate StorageService (%s): %s", id.get(), ex.what());
\r
909 child=XMLHelper::getNextSiblingElement(child,_StorageService);
\r
912 child=XMLHelper::getFirstChildElement(SHAR,_SessionCache);
\r
914 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
915 log.info("building SessionCache of type %s...",type.get());
\r
916 m_outer->m_sessionCache=conf.SessionCacheManager.newPlugin(type.get(),child);
\r
919 log.warn("SessionCache unspecified, building SessionCache of type %s...",STORAGESERVICE_SESSION_CACHE);
\r
920 if (inmemID.empty()) {
\r
921 inmemID = "memory";
\r
922 log.info("no StorageServices configured, providing in-memory version for session cache");
\r
923 m_outer->m_storage[inmemID] = xmlConf.StorageServiceManager.newPlugin(MEMORY_STORAGE_SERVICE,NULL);
\r
925 child = e->getOwnerDocument()->createElementNS(NULL,_SessionCache);
\r
926 auto_ptr_XMLCh ssid(inmemID.c_str());
\r
927 const_cast<DOMElement*>(child)->setAttributeNS(NULL,_StorageService,ssid.get());
\r
928 m_outer->m_sessionCache=conf.SessionCacheManager.newPlugin(STORAGESERVICE_SESSION_CACHE,child);
\r
932 StorageService* replaySS=NULL;
\r
933 child=XMLHelper::getFirstChildElement(SHAR,_ReplayCache);
\r
935 auto_ptr_char ssid(child->getAttributeNS(NULL,_StorageService));
\r
936 if (ssid.get() && *ssid.get()) {
\r
937 if (m_outer->m_storage.count(ssid.get()))
\r
938 replaySS = m_outer->m_storage[ssid.get()];
\r
940 log.info("building ReplayCache on top of StorageService (%s)...", ssid.get());
\r
942 log.crit("unable to locate StorageService (%s) in configuration", ssid.get());
\r
946 log.info("building ReplayCache using in-memory StorageService...");
\r
947 if (inmemID.empty()) {
\r
948 inmemID = "memory";
\r
949 log.info("no StorageServices configured, providing in-memory version for legacy config");
\r
950 m_outer->m_storage[inmemID] = xmlConf.StorageServiceManager.newPlugin(MEMORY_STORAGE_SERVICE,NULL);
\r
952 replaySS = m_outer->m_storage[inmemID];
\r
954 xmlConf.setReplayCache(new ReplayCache(replaySS));
\r
957 child=XMLHelper::getFirstChildElement(SHAR,_ArtifactMap);
\r
959 auto_ptr_char ssid(child->getAttributeNS(NULL,_StorageService));
\r
960 if (ssid.get() && *ssid.get() && m_outer->m_storage.count(ssid.get())) {
\r
961 log.info("building ArtifactMap on top of StorageService (%s)...", ssid.get());
\r
962 samlConf.setArtifactMap(new ArtifactMap(child, m_outer->m_storage[ssid.get()]));
\r
965 if (samlConf.getArtifactMap()==NULL) {
\r
966 log.info("building in-memory ArtifactMap...");
\r
967 samlConf.setArtifactMap(new ArtifactMap(child));
\r
971 child=XMLHelper::getFirstChildElement(SHIRE,_SessionCache);
\r
973 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
974 log.info("building SessionCache of type %s...",type.get());
\r
975 m_outer->m_sessionCache=conf.SessionCacheManager.newPlugin(type.get(),child);
\r
978 log.warn("SessionCache unspecified, building SessionCache of type %s...",REMOTED_SESSION_CACHE);
\r
979 m_outer->m_sessionCache=conf.SessionCacheManager.newPlugin(REMOTED_SESSION_CACHE,child);
\r
983 } // end of first-time-only stuff
\r
985 // Back to the fully dynamic stuff...next up is the RequestMapper.
\r
986 if (conf.isEnabled(SPConfig::RequestMapping)) {
\r
987 child=XMLHelper::getFirstChildElement(SHIRE,_RequestMapper);
\r
989 auto_ptr_char type(child->getAttributeNS(NULL,_type));
\r
990 log.info("building RequestMapper of type %s...",type.get());
\r
991 m_requestMapper=conf.RequestMapperManager.newPlugin(type.get(),child);
\r
995 // Load security policies.
\r
996 child = XMLHelper::getLastChildElement(e,SecurityPolicies);
\r
998 PolicyNodeFilter filter;
\r
999 child = XMLHelper::getFirstChildElement(child,Policy);
\r
1001 auto_ptr_char id(child->getAttributeNS(NULL,_id));
\r
1002 pair< PropertySet*,vector<const SecurityPolicyRule*> >& rules = m_policyMap[id.get()];
\r
1003 rules.first = NULL;
\r
1004 auto_ptr<DOMPropertySet> settings(new DOMPropertySet());
\r
1005 settings->load(child, log, &filter);
\r
1006 rules.first = settings.release();
\r
1007 const DOMElement* rule = XMLHelper::getFirstChildElement(child,Rule);
\r
1009 auto_ptr_char type(rule->getAttributeNS(NULL,_type));
\r
1011 rules.second.push_back(samlConf.SecurityPolicyRuleManager.newPlugin(type.get(),rule));
\r
1013 catch (exception& ex) {
\r
1014 log.crit("error instantiating policy rule (%s) in policy (%s): %s", type.get(), id.get(), ex.what());
\r
1016 rule = XMLHelper::getNextSiblingElement(rule,Rule);
\r
1018 child = XMLHelper::getNextSiblingElement(child,Policy);
\r
1022 // Load the default application. This actually has a fixed ID of "default". ;-)
\r
1023 child=XMLHelper::getLastChildElement(e,Applications);
\r
1025 log.fatal("can't build default Application object, missing conf:Applications element?");
\r
1026 throw ConfigurationException("can't build default Application object, missing conf:Applications element?");
\r
1028 XMLApplication* defapp=new XMLApplication(m_outer,child);
\r
1029 m_appmap[defapp->getId()]=defapp;
\r
1031 // Load any overrides.
\r
1032 child = XMLHelper::getFirstChildElement(child,_Application);
\r
1034 auto_ptr<XMLApplication> iapp(new XMLApplication(m_outer,child,defapp));
\r
1035 if (m_appmap.count(iapp->getId()))
\r
1036 log.crit("found conf:Application element with duplicate id attribute (%s), skipping it", iapp->getId());
\r
1038 m_appmap[iapp->getId()]=iapp.release();
\r
1040 child = XMLHelper::getNextSiblingElement(child,_Application);
\r
1043 catch (exception&) {
\r
1044 this->~XMLConfigImpl();
\r
1049 this->~XMLConfigImpl();
\r
1055 XMLConfigImpl::~XMLConfigImpl()
\r
1057 for_each(m_appmap.begin(),m_appmap.end(),cleanup_pair<string,Application>());
\r
1058 for (map< string,pair<PropertySet*,vector<const SecurityPolicyRule*> > >::iterator i=m_policyMap.begin(); i!=m_policyMap.end(); ++i) {
\r
1059 delete i->second.first;
\r
1060 for_each(i->second.second.begin(), i->second.second.end(), xmltooling::cleanup<SecurityPolicyRule>());
\r
1062 delete m_requestMapper;
\r
1064 m_document->release();
\r
1067 pair<bool,DOMElement*> XMLConfig::load()
\r
1069 // Load from source using base class.
\r
1070 pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
\r
1072 // If we own it, wrap it.
\r
1073 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
\r
1075 XMLConfigImpl* impl = new XMLConfigImpl(raw.second,(m_impl==NULL),this,m_log);
\r
1077 // If we held the document, transfer it to the impl. If we didn't, it's a no-op.
\r
1078 impl->setDocument(docjanitor.release());
\r
1083 return make_pair(false,(DOMElement*)NULL);
\r