2 * Copyright 2001-2007 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * XMLServiceProvider.cpp
20 * XML-based SP configuration and mgmt
24 #include "exceptions.h"
25 #include "AccessControl.h"
26 #include "Application.h"
27 #include "RequestMapper.h"
28 #include "ServiceProvider.h"
29 #include "SessionCache.h"
31 #include "handler/SessionInitiator.h"
32 #include "remoting/ListenerService.h"
33 #include "util/DOMPropertySet.h"
34 #include "util/SPConstants.h"
36 #if defined(XMLTOOLING_LOG4SHIB)
37 # include <log4shib/PropertyConfigurator.hh>
38 #elif defined(XMLTOOLING_LOG4CPP)
39 # include <log4cpp/PropertyConfigurator.hh>
41 # error "Supported logging library not available."
43 #include <xercesc/util/XMLUniDefs.hpp>
44 #include <xmltooling/XMLToolingConfig.h>
45 #include <xmltooling/util/NDC.h>
46 #include <xmltooling/util/ReloadableXMLFile.h>
47 #include <xmltooling/util/XMLHelper.h>
50 # include "TransactionLog.h"
51 # include "attribute/filtering/AttributeFilter.h"
52 # include "attribute/resolver/AttributeExtractor.h"
53 # include "attribute/resolver/AttributeResolver.h"
54 # include "security/PKIXTrustEngine.h"
55 # include <saml/SAMLConfig.h>
56 # include <saml/binding/ArtifactMap.h>
57 # include <saml/binding/SAMLArtifact.h>
58 # include <saml/saml1/core/Assertions.h>
59 # include <saml/saml2/binding/SAML2ArtifactType0004.h>
60 # include <saml/saml2/metadata/ChainingMetadataProvider.h>
61 # include <xmltooling/security/ChainingTrustEngine.h>
62 # include <xmltooling/util/ReplayCache.h>
63 using namespace opensaml::saml2;
64 using namespace opensaml::saml2p;
65 using namespace opensaml::saml2md;
66 using namespace opensaml;
69 using namespace shibsp;
70 using namespace xmltooling;
75 #if defined (_MSC_VER)
76 #pragma warning( push )
77 #pragma warning( disable : 4250 )
80 static vector<const Handler*> g_noHandlers;
82 // Application configuration wrapper
83 class SHIBSP_DLLLOCAL XMLApplication : public Application, public Remoted, public DOMPropertySet, public DOMNodeFilter
86 XMLApplication(const ServiceProvider*, const DOMElement* e, const XMLApplication* base=NULL);
87 ~XMLApplication() { cleanup(); }
89 const char* getHash() const {return m_hash.c_str();}
92 SAMLArtifact* generateSAML1Artifact(const EntityDescriptor* relyingParty) const {
93 throw ConfigurationException("No support for SAML 1.x artifact generation.");
95 SAML2Artifact* generateSAML2Artifact(const EntityDescriptor* relyingParty) const {
96 pair<bool,int> index = make_pair(false,0);
97 const PropertySet* props = getRelyingParty(relyingParty);
99 index = getInt("artifactEndpointIndex");
101 index = getArtifactEndpointIndex();
102 return new SAML2ArtifactType0004(SAMLConfig::getConfig().hashSHA1(getString("entityID").second),index.first ? index.second : 1);
105 MetadataProvider* getMetadataProvider(bool required=true) const {
106 if (required && !m_base && !m_metadata)
107 throw ConfigurationException("No MetadataProvider available.");
108 return (!m_metadata && m_base) ? m_base->getMetadataProvider() : m_metadata;
110 TrustEngine* getTrustEngine(bool required=true) const {
111 if (required && !m_base && !m_trust)
112 throw ConfigurationException("No TrustEngine available.");
113 return (!m_trust && m_base) ? m_base->getTrustEngine() : m_trust;
115 AttributeExtractor* getAttributeExtractor() const {
116 return (!m_attrExtractor && m_base) ? m_base->getAttributeExtractor() : m_attrExtractor;
118 AttributeFilter* getAttributeFilter() const {
119 return (!m_attrFilter && m_base) ? m_base->getAttributeFilter() : m_attrFilter;
121 AttributeResolver* getAttributeResolver() const {
122 return (!m_attrResolver && m_base) ? m_base->getAttributeResolver() : m_attrResolver;
124 CredentialResolver* getCredentialResolver() const {
125 return (!m_credResolver && m_base) ? m_base->getCredentialResolver() : m_credResolver;
127 const PropertySet* getRelyingParty(const EntityDescriptor* provider) const;
128 const vector<const XMLCh*>& getAudiences() const {
129 return (m_audiences.empty() && m_base) ? m_base->getAudiences() : m_audiences;
132 string getNotificationURL(const char* resource, bool front, unsigned int index) const;
134 const set<string>& getRemoteUserAttributeIds() const {
135 return (m_remoteUsers.empty() && m_base) ? m_base->getRemoteUserAttributeIds() : m_remoteUsers;
138 const SessionInitiator* getDefaultSessionInitiator() const;
139 const SessionInitiator* getSessionInitiatorById(const char* id) const;
140 const Handler* getDefaultAssertionConsumerService() const;
141 const Handler* getAssertionConsumerServiceByIndex(unsigned short index) const;
142 const vector<const Handler*>& getAssertionConsumerServicesByBinding(const XMLCh* binding) const;
143 const Handler* getHandler(const char* path) const;
144 void getHandlers(vector<const Handler*>& handlers) const;
146 void receive(DDF& in, ostream& out) {
147 // Only current function is to return the headers to clear.
149 DDF ret=DDF(NULL).list();
150 DDFJanitor jret(ret);
151 for (vector< pair<string,string> >::const_iterator i = m_unsetHeaders.begin(); i!=m_unsetHeaders.end(); ++i) {
152 header = DDF(i->first.c_str()).string(i->second.c_str());
158 // Provides filter to exclude special config elements.
159 short acceptNode(const DOMNode* node) const;
163 const XMLApplication* m_base;
166 MetadataProvider* m_metadata;
167 TrustEngine* m_trust;
168 AttributeExtractor* m_attrExtractor;
169 AttributeFilter* m_attrFilter;
170 AttributeResolver* m_attrResolver;
171 CredentialResolver* m_credResolver;
172 vector<const XMLCh*> m_audiences;
174 // RelyingParty properties
175 DOMPropertySet* m_partyDefault;
177 map<xstring,PropertySet*> m_partyMap;
179 map<const XMLCh*,PropertySet*> m_partyMap;
182 set<string> m_remoteUsers;
183 vector<string> m_frontLogout,m_backLogout;
185 // manage handler objects
186 vector<Handler*> m_handlers;
188 // maps location (path info) to applicable handlers
189 map<string,const Handler*> m_handlerMap;
191 // maps unique indexes to consumer services
192 map<unsigned int,const Handler*> m_acsIndexMap;
194 // pointer to default consumer service
195 const Handler* m_acsDefault;
197 // maps binding strings to supporting consumer service(s)
199 typedef map<xstring,vector<const Handler*> > ACSBindingMap;
201 typedef map<string,vector<const Handler*> > ACSBindingMap;
203 ACSBindingMap m_acsBindingMap;
205 // pointer to default session initiator
206 const SessionInitiator* m_sessionInitDefault;
208 // maps unique ID strings to session initiators
209 map<string,const SessionInitiator*> m_sessionInitMap;
211 // pointer to default artifact resolution service
212 const Handler* m_artifactResolutionDefault;
214 pair<bool,int> getArtifactEndpointIndex() const {
215 if (m_artifactResolutionDefault) return m_artifactResolutionDefault->getInt("index");
216 return m_base ? m_base->getArtifactEndpointIndex() : make_pair(false,0);
220 // Top-level configuration implementation
221 class SHIBSP_DLLLOCAL XMLConfig;
222 class SHIBSP_DLLLOCAL XMLConfigImpl : public DOMPropertySet, public DOMNodeFilter
225 XMLConfigImpl(const DOMElement* e, bool first, const XMLConfig* outer, Category& log);
228 RequestMapper* m_requestMapper;
229 map<string,Application*> m_appmap;
231 map< string,pair< PropertySet*,vector<const SecurityPolicyRule*> > > m_policyMap;
232 map< string, vector< pair< string, pair<string,string> > > > m_transportOptionMap;
235 // Provides filter to exclude special config elements.
236 short acceptNode(const DOMNode* node) const;
238 void setDocument(DOMDocument* doc) {
243 void doExtensions(const DOMElement* e, const char* label, Category& log);
246 const XMLConfig* m_outer;
247 DOMDocument* m_document;
250 class SHIBSP_DLLLOCAL XMLConfig : public ServiceProvider, public ReloadableXMLFile
256 XMLConfig(const DOMElement* e) : ReloadableXMLFile(e, Category::getInstance(SHIBSP_LOGCAT".Config")),
257 m_impl(NULL), m_listener(NULL), m_sessionCache(NULL)
270 delete m_sessionCache;
274 SAMLConfig::getConfig().setArtifactMap(NULL);
275 XMLToolingConfig::getConfig().setReplayCache(NULL);
276 for_each(m_storage.begin(), m_storage.end(), cleanup_pair<string,StorageService>());
281 const PropertySet* getParent() const { return m_impl->getParent(); }
282 void setParent(const PropertySet* parent) {return m_impl->setParent(parent);}
283 pair<bool,bool> getBool(const char* name, const char* ns=NULL) const {return m_impl->getBool(name,ns);}
284 pair<bool,const char*> getString(const char* name, const char* ns=NULL) const {return m_impl->getString(name,ns);}
285 pair<bool,const XMLCh*> getXMLString(const char* name, const char* ns=NULL) const {return m_impl->getXMLString(name,ns);}
286 pair<bool,unsigned int> getUnsignedInt(const char* name, const char* ns=NULL) const {return m_impl->getUnsignedInt(name,ns);}
287 pair<bool,int> getInt(const char* name, const char* ns=NULL) const {return m_impl->getInt(name,ns);}
288 void getAll(map<string,const char*>& properties) const {return m_impl->getAll(properties);}
289 const PropertySet* getPropertySet(const char* name, const char* ns="urn:mace:shibboleth:2.0:native:sp:config") const {return m_impl->getPropertySet(name,ns);}
290 const DOMElement* getElement() const {return m_impl->getElement();}
295 void receive(DDF& in, ostream& out);
297 TransactionLog* getTransactionLog() const {
300 throw ConfigurationException("No TransactionLog available.");
303 StorageService* getStorageService(const char* id) const {
305 map<string,StorageService*>::const_iterator i=m_storage.find(id);
306 if (i!=m_storage.end())
313 ListenerService* getListenerService(bool required=true) const {
314 if (required && !m_listener)
315 throw ConfigurationException("No ListenerService available.");
319 SessionCache* getSessionCache(bool required=true) const {
320 if (required && !m_sessionCache)
321 throw ConfigurationException("No SessionCache available.");
322 return m_sessionCache;
325 RequestMapper* getRequestMapper(bool required=true) const {
326 if (required && !m_impl->m_requestMapper)
327 throw ConfigurationException("No RequestMapper available.");
328 return m_impl->m_requestMapper;
331 const Application* getApplication(const char* applicationId) const {
332 map<string,Application*>::const_iterator i=m_impl->m_appmap.find(applicationId);
333 return (i!=m_impl->m_appmap.end()) ? i->second : NULL;
337 const PropertySet* getPolicySettings(const char* id) const {
338 map<string,pair<PropertySet*,vector<const SecurityPolicyRule*> > >::const_iterator i = m_impl->m_policyMap.find(id);
339 if (i!=m_impl->m_policyMap.end())
340 return i->second.first;
341 throw ConfigurationException("Security Policy ($1) not found, check <SecurityPolicies> element.", params(1,id));
344 const vector<const SecurityPolicyRule*>& getPolicyRules(const char* id) const {
345 map<string,pair<PropertySet*,vector<const SecurityPolicyRule*> > >::const_iterator i = m_impl->m_policyMap.find(id);
346 if (i!=m_impl->m_policyMap.end())
347 return i->second.second;
348 throw ConfigurationException("Security Policy ($1) not found, check <SecurityPolicies> element.", params(1,id));
351 bool setTransportOptions(const char* id, SOAPTransport& transport) const {
353 map< string, vector< pair< string, pair<string,string> > > >::const_iterator p =
354 m_impl->m_transportOptionMap.find(id);
355 if (p == m_impl->m_transportOptionMap.end())
357 vector< pair< string, pair<string,string> > >::const_iterator opt;
358 for (opt = p->second.begin(); opt != p->second.end(); ++opt) {
359 if (!transport.setProviderOption(opt->first.c_str(), opt->second.first.c_str(), opt->second.second.c_str())) {
360 m_log.error("failed to set SOAPTransport option (%s)", opt->second.first.c_str());
369 pair<bool,DOMElement*> load();
372 friend class XMLConfigImpl;
373 XMLConfigImpl* m_impl;
374 mutable ListenerService* m_listener;
375 mutable SessionCache* m_sessionCache;
377 mutable TransactionLog* m_tranLog;
378 mutable map<string,StorageService*> m_storage;
382 #if defined (_MSC_VER)
383 #pragma warning( pop )
386 static const XMLCh _Application[] = UNICODE_LITERAL_11(A,p,p,l,i,c,a,t,i,o,n);
387 static const XMLCh Applications[] = UNICODE_LITERAL_12(A,p,p,l,i,c,a,t,i,o,n,s);
388 static const XMLCh _ArtifactMap[] = UNICODE_LITERAL_11(A,r,t,i,f,a,c,t,M,a,p);
389 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);
390 static const XMLCh _AttributeFilter[] = UNICODE_LITERAL_15(A,t,t,r,i,b,u,t,e,F,i,l,t,e,r);
391 static const XMLCh _AttributeResolver[] = UNICODE_LITERAL_17(A,t,t,r,i,b,u,t,e,R,e,s,o,l,v,e,r);
392 static const XMLCh _AssertionConsumerService[] = UNICODE_LITERAL_24(A,s,s,e,r,t,i,o,n,C,o,n,s,u,m,e,r,S,e,r,v,i,c,e);
393 static const XMLCh _ArtifactResolutionService[] =UNICODE_LITERAL_25(A,r,t,i,f,a,c,t,R,e,s,o,l,u,t,i,o,n,S,e,r,v,i,c,e);
394 static const XMLCh _Audience[] = UNICODE_LITERAL_8(A,u,d,i,e,n,c,e);
395 static const XMLCh Binding[] = UNICODE_LITERAL_7(B,i,n,d,i,n,g);
396 static const XMLCh Channel[]= UNICODE_LITERAL_7(C,h,a,n,n,e,l);
397 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);
398 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);
399 static const XMLCh _Extensions[] = UNICODE_LITERAL_10(E,x,t,e,n,s,i,o,n,s);
400 static const XMLCh _fatal[] = UNICODE_LITERAL_5(f,a,t,a,l);
401 static const XMLCh _Handler[] = UNICODE_LITERAL_7(H,a,n,d,l,e,r);
402 static const XMLCh _id[] = UNICODE_LITERAL_2(i,d);
403 static const XMLCh InProcess[] = UNICODE_LITERAL_9(I,n,P,r,o,c,e,s,s);
404 static const XMLCh Library[] = UNICODE_LITERAL_7(L,i,b,r,a,r,y);
405 static const XMLCh Listener[] = UNICODE_LITERAL_8(L,i,s,t,e,n,e,r);
406 static const XMLCh Location[] = UNICODE_LITERAL_8(L,o,c,a,t,i,o,n);
407 static const XMLCh logger[] = UNICODE_LITERAL_6(l,o,g,g,e,r);
408 static const XMLCh _LogoutInitiator[] = UNICODE_LITERAL_15(L,o,g,o,u,t,I,n,i,t,i,a,t,o,r);
409 static const XMLCh _ManageNameIDService[] = UNICODE_LITERAL_19(M,a,n,a,g,e,N,a,m,e,I,D,S,e,r,v,i,c,e);
410 static const XMLCh _MetadataProvider[] = UNICODE_LITERAL_16(M,e,t,a,d,a,t,a,P,r,o,v,i,d,e,r);
411 static const XMLCh Notify[] = UNICODE_LITERAL_6(N,o,t,i,f,y);
412 static const XMLCh _option[] = UNICODE_LITERAL_6(o,p,t,i,o,n);
413 static const XMLCh OutOfProcess[] = UNICODE_LITERAL_12(O,u,t,O,f,P,r,o,c,e,s,s);
414 static const XMLCh _path[] = UNICODE_LITERAL_4(p,a,t,h);
415 static const XMLCh Policy[] = UNICODE_LITERAL_6(P,o,l,i,c,y);
416 static const XMLCh _provider[] = UNICODE_LITERAL_8(p,r,o,v,i,d,e,r);
417 static const XMLCh RelyingParty[] = UNICODE_LITERAL_12(R,e,l,y,i,n,g,P,a,r,t,y);
418 static const XMLCh _ReplayCache[] = UNICODE_LITERAL_11(R,e,p,l,a,y,C,a,c,h,e);
419 static const XMLCh _RequestMapper[] = UNICODE_LITERAL_13(R,e,q,u,e,s,t,M,a,p,p,e,r);
420 static const XMLCh Rule[] = UNICODE_LITERAL_4(R,u,l,e);
421 static const XMLCh SecurityPolicies[] = UNICODE_LITERAL_16(S,e,c,u,r,i,t,y,P,o,l,i,c,i,e,s);
422 static const XMLCh _SessionCache[] = UNICODE_LITERAL_12(S,e,s,s,i,o,n,C,a,c,h,e);
423 static const XMLCh _SessionInitiator[] = UNICODE_LITERAL_16(S,e,s,s,i,o,n,I,n,i,t,i,a,t,o,r);
424 static const XMLCh _SingleLogoutService[] = UNICODE_LITERAL_19(S,i,n,g,l,e,L,o,g,o,u,t,S,e,r,v,i,c,e);
425 static const XMLCh Site[] = UNICODE_LITERAL_4(S,i,t,e);
426 static const XMLCh _StorageService[] = UNICODE_LITERAL_14(S,t,o,r,a,g,e,S,e,r,v,i,c,e);
427 static const XMLCh TCPListener[] = UNICODE_LITERAL_11(T,C,P,L,i,s,t,e,n,e,r);
428 static const XMLCh TransportOption[] = UNICODE_LITERAL_15(T,r,a,n,s,p,o,r,t,O,p,t,i,o,n);
429 static const XMLCh _TrustEngine[] = UNICODE_LITERAL_11(T,r,u,s,t,E,n,g,i,n,e);
430 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
431 static const XMLCh UnixListener[] = UNICODE_LITERAL_12(U,n,i,x,L,i,s,t,e,n,e,r);
434 class SHIBSP_DLLLOCAL PolicyNodeFilter : public DOMNodeFilter
437 short acceptNode(const DOMNode* node) const {
438 return FILTER_REJECT;
445 ServiceProvider* XMLServiceProviderFactory(const DOMElement* const & e)
447 return new XMLConfig(e);
451 XMLApplication::XMLApplication(
452 const ServiceProvider* sp,
454 const XMLApplication* base
455 ) : Application(sp), m_base(base),
457 m_metadata(NULL), m_trust(NULL),
458 m_attrExtractor(NULL), m_attrFilter(NULL), m_attrResolver(NULL),
459 m_credResolver(NULL), m_partyDefault(NULL),
461 m_acsDefault(NULL), m_sessionInitDefault(NULL), m_artifactResolutionDefault(NULL)
464 xmltooling::NDC ndc("XMLApplication");
466 Category& log=Category::getInstance(SHIBSP_LOGCAT".Application");
469 // First load any property sets.
474 SPConfig& conf=SPConfig::getConfig();
476 SAMLConfig& samlConf=SAMLConfig::getConfig();
477 XMLToolingConfig& xmlConf=XMLToolingConfig::getConfig();
480 // This used to be an actual hash, but now it's just a hex-encode to avoid xmlsec.
481 static char DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
482 string tohash=getId();
483 tohash+=getString("entityID").second;
484 for (const char* ch = tohash.c_str(); *ch; ++ch) {
485 m_hash += (DIGITS[((unsigned char)(0xF0 & *ch)) >> 4 ]);
486 m_hash += (DIGITS[0x0F & *ch]);
489 // Load attribute ID lists for REMOTE_USER and header clearing.
490 if (conf.isEnabled(SPConfig::InProcess)) {
491 pair<bool,const char*> attributes = getString("REMOTE_USER");
492 if (attributes.first) {
493 char* dup = strdup(attributes.second);
496 while (start && *start) {
497 while (*start && isspace(*start))
501 pos = strchr(start,' ');
504 m_remoteUsers.insert(start);
505 start = pos ? pos+1 : NULL;
510 attributes = getString("unsetHeaders");
511 if (attributes.first) {
512 string transformedprefix("HTTP_");
514 pair<bool,const char*> prefix = getString("metadataAttributePrefix");
518 transformedprefix += (isalnum(*pch) ? toupper(*pch) : '_');
522 char* dup = strdup(attributes.second);
525 while (start && *start) {
526 while (*start && isspace(*start))
530 pos = strchr(start,' ');
537 transformed += (isalnum(*pch) ? toupper(*pch) : '_');
540 m_unsetHeaders.push_back(pair<string,string>(start,string("HTTP_") + transformed));
542 m_unsetHeaders.push_back(pair<string,string>(string(prefix.second) + start, transformedprefix + transformed));
543 start = pos ? pos+1 : NULL;
546 m_unsetHeaders.push_back(pair<string,string>("Shib-Application-ID","HTTP_SHIB_APPLICATION_ID"));
550 Handler* handler=NULL;
551 const PropertySet* sessions = getPropertySet("Sessions");
553 // Process assertion export handler.
554 pair<bool,const char*> location = sessions ? sessions->getString("exportLocation") : pair<bool,const char*>(false,NULL);
555 if (location.first) {
557 DOMElement* exportElement = e->getOwnerDocument()->createElementNS(shibspconstants::SHIB2SPCONFIG_NS,_Handler);
558 exportElement->setAttributeNS(NULL,Location,sessions->getXMLString("exportLocation").second);
559 pair<bool,const XMLCh*> exportACL = sessions->getXMLString("exportACL");
560 if (exportACL.first) {
561 static const XMLCh _acl[] = UNICODE_LITERAL_9(e,x,p,o,r,t,A,C,L);
562 exportElement->setAttributeNS(NULL,_acl,exportACL.second);
564 handler = conf.HandlerManager.newPlugin(
565 samlconstants::SAML20_BINDING_URI, pair<const DOMElement*,const char*>(exportElement, getId())
567 m_handlers.push_back(handler);
569 // Insert into location map. If it contains the handlerURL, we skip past that part.
570 const char* pch = strstr(location.second, sessions->getString("handlerURL").second);
572 location.second = pch + strlen(sessions->getString("handlerURL").second);
573 if (*location.second == '/')
574 m_handlerMap[location.second]=handler;
576 m_handlerMap[string("/") + location.second]=handler;
578 catch (exception& ex) {
579 log.error("caught exception installing assertion lookup handler: %s", ex.what());
583 // Process other handlers.
584 bool hardACS=false, hardSessionInit=false, hardArt=false;
585 const DOMElement* child = sessions ? XMLHelper::getFirstChildElement(sessions->getElement()) : NULL;
588 // A handler is based on the Binding property in conjunction with the element name.
589 // If it's an ACS or SI, also handle index/id mappings and defaulting.
590 if (XMLString::equals(child->getLocalName(),_AssertionConsumerService)) {
591 auto_ptr_char bindprop(child->getAttributeNS(NULL,Binding));
592 if (!bindprop.get() || !*(bindprop.get())) {
593 log.warn("md:AssertionConsumerService element has no Binding attribute, skipping it...");
594 child = XMLHelper::getNextSiblingElement(child);
597 handler=conf.AssertionConsumerServiceManager.newPlugin(bindprop.get(),make_pair(child, getId()));
598 // Map by binding (may be > 1 per binding, e.g. SAML 1.0 vs 1.1)
600 m_acsBindingMap[handler->getXMLString("Binding").second].push_back(handler);
602 m_acsBindingMap[handler->getString("Binding").second].push_back(handler);
604 m_acsIndexMap[handler->getUnsignedInt("index").second]=handler;
607 pair<bool,bool> defprop=handler->getBool("isDefault");
609 if (defprop.second) {
611 m_acsDefault=handler;
614 else if (!m_acsDefault)
615 m_acsDefault=handler;
618 else if (XMLString::equals(child->getLocalName(),_SessionInitiator)) {
619 auto_ptr_char type(child->getAttributeNS(NULL,_type));
620 if (!type.get() || !*(type.get())) {
621 log.warn("SessionInitiator element has no type attribute, skipping it...");
622 child = XMLHelper::getNextSiblingElement(child);
625 SessionInitiator* sihandler=conf.SessionInitiatorManager.newPlugin(type.get(),make_pair(child, getId()));
627 pair<bool,const char*> si_id=handler->getString("id");
628 if (si_id.first && si_id.second)
629 m_sessionInitMap[si_id.second]=sihandler;
630 if (!hardSessionInit) {
631 pair<bool,bool> defprop=handler->getBool("isDefault");
633 if (defprop.second) {
634 hardSessionInit=true;
635 m_sessionInitDefault=sihandler;
638 else if (!m_sessionInitDefault)
639 m_sessionInitDefault=sihandler;
642 else if (XMLString::equals(child->getLocalName(),_LogoutInitiator)) {
643 auto_ptr_char type(child->getAttributeNS(NULL,_type));
644 if (!type.get() || !*(type.get())) {
645 log.warn("LogoutInitiator element has no type attribute, skipping it...");
646 child = XMLHelper::getNextSiblingElement(child);
649 handler=conf.LogoutInitiatorManager.newPlugin(type.get(),make_pair(child, getId()));
651 else if (XMLString::equals(child->getLocalName(),_ArtifactResolutionService)) {
652 auto_ptr_char bindprop(child->getAttributeNS(NULL,Binding));
653 if (!bindprop.get() || !*(bindprop.get())) {
654 log.warn("md:ArtifactResolutionService element has no Binding attribute, skipping it...");
655 child = XMLHelper::getNextSiblingElement(child);
658 handler=conf.ArtifactResolutionServiceManager.newPlugin(bindprop.get(),make_pair(child, getId()));
661 pair<bool,bool> defprop=handler->getBool("isDefault");
663 if (defprop.second) {
665 m_artifactResolutionDefault=handler;
668 else if (!m_artifactResolutionDefault)
669 m_artifactResolutionDefault=handler;
672 else if (XMLString::equals(child->getLocalName(),_SingleLogoutService)) {
673 auto_ptr_char bindprop(child->getAttributeNS(NULL,Binding));
674 if (!bindprop.get() || !*(bindprop.get())) {
675 log.warn("md:SingleLogoutService element has no Binding attribute, skipping it...");
676 child = XMLHelper::getNextSiblingElement(child);
679 handler=conf.SingleLogoutServiceManager.newPlugin(bindprop.get(),make_pair(child, getId()));
681 else if (XMLString::equals(child->getLocalName(),_ManageNameIDService)) {
682 auto_ptr_char bindprop(child->getAttributeNS(NULL,Binding));
683 if (!bindprop.get() || !*(bindprop.get())) {
684 log.warn("md:ManageNameIDService element has no Binding attribute, skipping it...");
685 child = XMLHelper::getNextSiblingElement(child);
688 handler=conf.ManageNameIDServiceManager.newPlugin(bindprop.get(),make_pair(child, getId()));
691 auto_ptr_char type(child->getAttributeNS(NULL,_type));
692 if (!type.get() || !*(type.get())) {
693 log.warn("Handler element has no type attribute, skipping it...");
694 child = XMLHelper::getNextSiblingElement(child);
697 handler=conf.HandlerManager.newPlugin(type.get(),make_pair(child, getId()));
700 m_handlers.push_back(handler);
702 // Insert into location map.
703 location=handler->getString("Location");
704 if (location.first && *location.second == '/')
705 m_handlerMap[location.second]=handler;
706 else if (location.first)
707 m_handlerMap[string("/") + location.second]=handler;
710 catch (exception& ex) {
711 log.error("caught exception processing handler element: %s", ex.what());
714 child = XMLHelper::getNextSiblingElement(child);
718 DOMNodeList* nlist=e->getElementsByTagNameNS(shibspconstants::SHIB2SPCONFIG_NS,Notify);
719 for (XMLSize_t i=0; nlist && i<nlist->getLength(); i++) {
720 if (nlist->item(i)->getParentNode()->isSameNode(e)) {
721 const XMLCh* channel = static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,Channel);
722 auto_ptr_char loc(static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,Location));
723 if (loc.get() && *loc.get()) {
724 if (channel && *channel == chLatin_f)
725 m_frontLogout.push_back(loc.get());
727 m_backLogout.push_back(loc.get());
733 nlist=e->getElementsByTagNameNS(samlconstants::SAML20_NS,Audience::LOCAL_NAME);
734 for (XMLSize_t i=0; nlist && i<nlist->getLength(); i++)
735 if (nlist->item(i)->getParentNode()->isSameNode(e) && nlist->item(i)->hasChildNodes())
736 m_audiences.push_back(nlist->item(i)->getFirstChild()->getNodeValue());
738 // Always include our own entityID as an audience.
739 m_audiences.push_back(getXMLString("entityID").second);
741 if (conf.isEnabled(SPConfig::Metadata)) {
742 child = XMLHelper::getFirstChildElement(e,_MetadataProvider);
744 auto_ptr_char type(child->getAttributeNS(NULL,_type));
745 log.info("building MetadataProvider of type %s...",type.get());
747 auto_ptr<MetadataProvider> mp(samlConf.MetadataProviderManager.newPlugin(type.get(),child));
749 m_metadata = mp.release();
751 catch (exception& ex) {
752 log.crit("error building/initializing MetadataProvider: %s", ex.what());
757 if (conf.isEnabled(SPConfig::Trust)) {
758 child = XMLHelper::getFirstChildElement(e,_TrustEngine);
760 auto_ptr_char type(child->getAttributeNS(NULL,_type));
761 log.info("building TrustEngine of type %s...",type.get());
763 m_trust = xmlConf.TrustEngineManager.newPlugin(type.get(),child);
765 catch (exception& ex) {
766 log.crit("error building TrustEngine: %s", ex.what());
771 if (conf.isEnabled(SPConfig::AttributeResolution)) {
772 child = XMLHelper::getFirstChildElement(e,_AttributeExtractor);
774 auto_ptr_char type(child->getAttributeNS(NULL,_type));
775 log.info("building AttributeExtractor of type %s...",type.get());
777 m_attrExtractor = conf.AttributeExtractorManager.newPlugin(type.get(),child);
779 catch (exception& ex) {
780 log.crit("error building AttributeExtractor: %s", ex.what());
784 child = XMLHelper::getFirstChildElement(e,_AttributeFilter);
786 auto_ptr_char type(child->getAttributeNS(NULL,_type));
787 log.info("building AttributeFilter of type %s...",type.get());
789 m_attrFilter = conf.AttributeFilterManager.newPlugin(type.get(),child);
791 catch (exception& ex) {
792 log.crit("error building AttributeFilter: %s", ex.what());
796 child = XMLHelper::getFirstChildElement(e,_AttributeResolver);
798 auto_ptr_char type(child->getAttributeNS(NULL,_type));
799 log.info("building AttributeResolver of type %s...",type.get());
801 m_attrResolver = conf.AttributeResolverManager.newPlugin(type.get(),child);
803 catch (exception& ex) {
804 log.crit("error building AttributeResolver: %s", ex.what());
808 if (m_unsetHeaders.empty()) {
809 vector<string> unsetHeaders;
810 if (m_attrExtractor) {
811 Locker extlock(m_attrExtractor);
812 m_attrExtractor->getAttributeIds(unsetHeaders);
814 if (m_attrResolver) {
815 Locker reslock(m_attrResolver);
816 m_attrResolver->getAttributeIds(unsetHeaders);
818 if (unsetHeaders.empty()) {
820 m_unsetHeaders.insert(m_unsetHeaders.end(), m_base->m_unsetHeaders.begin(), m_base->m_unsetHeaders.end());
822 m_unsetHeaders.push_back(pair<string,string>("Shib-Application-ID","HTTP_SHIB_APPLICATION_ID"));
825 string transformedprefix("HTTP_");
827 pair<bool,const char*> prefix = getString("metadataAttributePrefix");
831 transformedprefix += (isalnum(*pch) ? toupper(*pch) : '_');
835 for (vector<string>::const_iterator hdr = unsetHeaders.begin(); hdr!=unsetHeaders.end(); ++hdr) {
839 transformed += (isalnum(*pch) ? toupper(*pch) : '_');
842 m_unsetHeaders.push_back(pair<string,string>(*hdr, string("HTTP_") + transformed));
844 m_unsetHeaders.push_back(pair<string,string>(string(prefix.second) + *hdr, transformedprefix + transformed));
846 m_unsetHeaders.push_back(pair<string,string>("Shib-Application-ID","HTTP_SHIB_APPLICATION_ID"));
851 if (conf.isEnabled(SPConfig::Credentials)) {
852 child = XMLHelper::getFirstChildElement(e,_CredentialResolver);
854 auto_ptr_char type(child->getAttributeNS(NULL,_type));
855 log.info("building CredentialResolver of type %s...",type.get());
857 m_credResolver = xmlConf.CredentialResolverManager.newPlugin(type.get(),child);
859 catch (exception& ex) {
860 log.crit("error building CredentialResolver: %s", ex.what());
865 // Finally, load relying parties.
866 child = XMLHelper::getFirstChildElement(e,DefaultRelyingParty);
868 m_partyDefault=new DOMPropertySet();
869 m_partyDefault->load(child,log,this);
870 child = XMLHelper::getFirstChildElement(child,RelyingParty);
872 auto_ptr<DOMPropertySet> rp(new DOMPropertySet());
873 rp->load(child,log,this);
874 rp->setParent(m_partyDefault);
875 m_partyMap[child->getAttributeNS(NULL,saml2::Attribute::NAME_ATTRIB_NAME)]=rp.release();
876 child = XMLHelper::getNextSiblingElement(child,RelyingParty);
881 // Out of process only, we register a listener endpoint.
882 if (!conf.isEnabled(SPConfig::InProcess)) {
883 ListenerService* listener = sp->getListenerService(false);
885 string addr=string(getId()) + "::getHeaders::Application";
886 listener->regListener(addr.c_str(),this);
889 log.info("no ListenerService available, Application remoting disabled");
904 void XMLApplication::cleanup()
906 ListenerService* listener=getServiceProvider().getListenerService(false);
907 if (listener && SPConfig::getConfig().isEnabled(SPConfig::OutOfProcess) && !SPConfig::getConfig().isEnabled(SPConfig::InProcess)) {
908 string addr=string(getId()) + "::getHeaders::Application";
909 listener->unregListener(addr.c_str(),this);
911 for_each(m_handlers.begin(),m_handlers.end(),xmltooling::cleanup<Handler>());
914 delete m_partyDefault;
915 m_partyDefault = NULL;
917 for_each(m_partyMap.begin(),m_partyMap.end(),cleanup_pair<xstring,PropertySet>());
919 for_each(m_partyMap.begin(),m_partyMap.end(),cleanup_pair<const XMLCh*,PropertySet>());
922 delete m_credResolver;
923 m_credResolver = NULL;
924 delete m_attrResolver;
925 m_attrResolver = NULL;
928 delete m_attrExtractor;
929 m_attrExtractor = NULL;
937 short XMLApplication::acceptNode(const DOMNode* node) const
939 const XMLCh* name=node->getLocalName();
940 if (XMLString::equals(name,_Application) ||
941 XMLString::equals(name,_Audience) ||
942 XMLString::equals(name,Notify) ||
943 XMLString::equals(name,_Handler) ||
944 XMLString::equals(name,_AssertionConsumerService) ||
945 XMLString::equals(name,_ArtifactResolutionService) ||
946 XMLString::equals(name,_LogoutInitiator) ||
947 XMLString::equals(name,_ManageNameIDService) ||
948 XMLString::equals(name,_SessionInitiator) ||
949 XMLString::equals(name,_SingleLogoutService) ||
950 XMLString::equals(name,DefaultRelyingParty) ||
951 XMLString::equals(name,RelyingParty) ||
952 XMLString::equals(name,_MetadataProvider) ||
953 XMLString::equals(name,_TrustEngine) ||
954 XMLString::equals(name,_CredentialResolver) ||
955 XMLString::equals(name,_AttributeFilter) ||
956 XMLString::equals(name,_AttributeExtractor) ||
957 XMLString::equals(name,_AttributeResolver))
958 return FILTER_REJECT;
960 return FILTER_ACCEPT;
965 const PropertySet* XMLApplication::getRelyingParty(const EntityDescriptor* provider) const
967 if (!m_partyDefault && m_base)
968 return m_base->getRelyingParty(provider);
970 return m_partyDefault;
973 map<xstring,PropertySet*>::const_iterator i=m_partyMap.find(provider->getEntityID());
974 if (i!=m_partyMap.end())
976 const EntitiesDescriptor* group=dynamic_cast<const EntitiesDescriptor*>(provider->getParent());
978 if (group->getName()) {
979 i=m_partyMap.find(group->getName());
980 if (i!=m_partyMap.end())
983 group=dynamic_cast<const EntitiesDescriptor*>(group->getParent());
986 map<const XMLCh*,PropertySet*>::const_iterator i=m_partyMap.begin();
987 for (; i!=m_partyMap.end(); i++) {
988 if (XMLString::equals(i->first,provider->getEntityID()))
990 const EntitiesDescriptor* group=dynamic_cast<const EntitiesDescriptor*>(provider->getParent());
992 if (XMLString::equals(i->first,group->getName()))
994 group=dynamic_cast<const EntitiesDescriptor*>(group->getParent());
998 return m_partyDefault;
1003 string XMLApplication::getNotificationURL(const char* resource, bool front, unsigned int index) const
1005 const vector<string>& locs = front ? m_frontLogout : m_backLogout;
1007 return m_base ? m_base->getNotificationURL(resource, front, index) : string();
1008 else if (index >= locs.size())
1011 #ifdef HAVE_STRCASECMP
1012 if (!resource || (strncasecmp(resource,"http://",7) && strncasecmp(resource,"https://",8)))
1014 if (!resource || (strnicmp(resource,"http://",7) && strnicmp(resource,"https://",8)))
1016 throw ConfigurationException("Request URL was not absolute.");
1018 const char* handler=locs[index].c_str();
1020 // Should never happen...
1021 if (!handler || (*handler!='/' && strncmp(handler,"http:",5) && strncmp(handler,"https:",6)))
1022 throw ConfigurationException(
1023 "Invalid Location property ($1) in Notify element for Application ($2)",
1024 params(2, handler ? handler : "null", getId())
1027 // The "Location" property can be in one of three formats:
1029 // 1) a full URI: http://host/foo/bar
1030 // 2) a hostless URI: http:///foo/bar
1031 // 3) a relative path: /foo/bar
1033 // # Protocol Host Path
1034 // 1 handler handler handler
1035 // 2 handler resource handler
1036 // 3 resource resource handler
1038 const char* path = NULL;
1040 // Decide whether to use the handler or the resource for the "protocol"
1042 if (*handler != '/') {
1050 // break apart the "protocol" string into protocol, host, and "the rest"
1051 const char* colon=strchr(prot,':');
1053 const char* slash=strchr(colon,'/');
1057 // Compute the actual protocol and store.
1058 string notifyURL(prot, colon-prot);
1060 // create the "host" from either the colon/slash or from the target string
1061 // If prot == handler then we're in either #1 or #2, else #3.
1062 // If slash == colon then we're in #2.
1063 if (prot != handler || slash == colon) {
1064 colon = strchr(resource, ':');
1065 colon += 3; // Get past the ://
1066 slash = strchr(colon, '/');
1068 string host(colon, (slash ? slash-colon : strlen(colon)));
1071 notifyURL += host + path;
1075 const SessionInitiator* XMLApplication::getDefaultSessionInitiator() const
1077 if (m_sessionInitDefault) return m_sessionInitDefault;
1078 return m_base ? m_base->getDefaultSessionInitiator() : NULL;
1081 const SessionInitiator* XMLApplication::getSessionInitiatorById(const char* id) const
1083 map<string,const SessionInitiator*>::const_iterator i=m_sessionInitMap.find(id);
1084 if (i!=m_sessionInitMap.end()) return i->second;
1085 return m_base ? m_base->getSessionInitiatorById(id) : NULL;
1088 const Handler* XMLApplication::getDefaultAssertionConsumerService() const
1090 if (m_acsDefault) return m_acsDefault;
1091 return m_base ? m_base->getDefaultAssertionConsumerService() : NULL;
1094 const Handler* XMLApplication::getAssertionConsumerServiceByIndex(unsigned short index) const
1096 map<unsigned int,const Handler*>::const_iterator i=m_acsIndexMap.find(index);
1097 if (i!=m_acsIndexMap.end()) return i->second;
1098 return m_base ? m_base->getAssertionConsumerServiceByIndex(index) : NULL;
1101 const vector<const Handler*>& XMLApplication::getAssertionConsumerServicesByBinding(const XMLCh* binding) const
1103 #ifdef HAVE_GOOD_STL
1104 ACSBindingMap::const_iterator i=m_acsBindingMap.find(binding);
1106 auto_ptr_char temp(binding);
1107 ACSBindingMap::const_iterator i=m_acsBindingMap.find(temp.get());
1109 if (i!=m_acsBindingMap.end())
1111 return m_base ? m_base->getAssertionConsumerServicesByBinding(binding) : g_noHandlers;
1114 const Handler* XMLApplication::getHandler(const char* path) const
1117 map<string,const Handler*>::const_iterator i=m_handlerMap.find(wrap.substr(0,wrap.find('?')));
1118 if (i!=m_handlerMap.end())
1120 return m_base ? m_base->getHandler(path) : NULL;
1123 void XMLApplication::getHandlers(vector<const Handler*>& handlers) const
1125 handlers.insert(handlers.end(), m_handlers.begin(), m_handlers.end());
1127 for (map<string,const Handler*>::const_iterator h = m_base->m_handlerMap.begin(); h != m_base->m_handlerMap.end(); ++h) {
1128 if (m_handlerMap.count(h->first) == 0)
1129 handlers.push_back(h->second);
1134 short XMLConfigImpl::acceptNode(const DOMNode* node) const
1136 if (!XMLString::equals(node->getNamespaceURI(),shibspconstants::SHIB2SPCONFIG_NS))
1137 return FILTER_ACCEPT;
1138 const XMLCh* name=node->getLocalName();
1139 if (XMLString::equals(name,Applications) ||
1140 XMLString::equals(name,_ArtifactMap) ||
1141 XMLString::equals(name,_Extensions) ||
1142 XMLString::equals(name,Listener) ||
1143 XMLString::equals(name,Policy) ||
1144 XMLString::equals(name,_RequestMapper) ||
1145 XMLString::equals(name,_ReplayCache) ||
1146 XMLString::equals(name,_SessionCache) ||
1147 XMLString::equals(name,Site) ||
1148 XMLString::equals(name,_StorageService) ||
1149 XMLString::equals(name,TCPListener) ||
1150 XMLString::equals(name,UnixListener))
1151 return FILTER_REJECT;
1153 return FILTER_ACCEPT;
1156 void XMLConfigImpl::doExtensions(const DOMElement* e, const char* label, Category& log)
1158 const DOMElement* exts=XMLHelper::getFirstChildElement(e,_Extensions);
1160 exts=XMLHelper::getFirstChildElement(exts,Library);
1162 auto_ptr_char path(exts->getAttributeNS(NULL,_path));
1165 XMLToolingConfig::getConfig().load_library(path.get(),(void*)exts);
1166 log.debug("loaded %s extension library (%s)", label, path.get());
1169 catch (exception& e) {
1170 const XMLCh* fatal=exts->getAttributeNS(NULL,_fatal);
1171 if (fatal && (*fatal==chLatin_t || *fatal==chDigit_1)) {
1172 log.fatal("unable to load mandatory %s extension library %s: %s", label, path.get(), e.what());
1176 log.crit("unable to load optional %s extension library %s: %s", label, path.get(), e.what());
1179 exts=XMLHelper::getNextSiblingElement(exts,Library);
1184 XMLConfigImpl::XMLConfigImpl(const DOMElement* e, bool first, const XMLConfig* outer, Category& log)
1185 : m_requestMapper(NULL), m_outer(outer), m_document(NULL)
1188 xmltooling::NDC ndc("XMLConfigImpl");
1192 SPConfig& conf=SPConfig::getConfig();
1194 SAMLConfig& samlConf=SAMLConfig::getConfig();
1196 XMLToolingConfig& xmlConf=XMLToolingConfig::getConfig();
1197 const DOMElement* SHAR=XMLHelper::getFirstChildElement(e,OutOfProcess);
1198 const DOMElement* SHIRE=XMLHelper::getFirstChildElement(e,InProcess);
1200 // Initialize log4cpp manually in order to redirect log messages as soon as possible.
1201 if (conf.isEnabled(SPConfig::Logging)) {
1202 const XMLCh* logconf=NULL;
1203 if (conf.isEnabled(SPConfig::OutOfProcess))
1204 logconf=SHAR->getAttributeNS(NULL,logger);
1205 else if (conf.isEnabled(SPConfig::InProcess))
1206 logconf=SHIRE->getAttributeNS(NULL,logger);
1207 if (!logconf || !*logconf)
1208 logconf=e->getAttributeNS(NULL,logger);
1209 if (logconf && *logconf) {
1210 auto_ptr_char logpath(logconf);
1211 log.debug("loading new logging configuration from (%s), check log destination for status of configuration",logpath.get());
1212 XMLToolingConfig::getConfig().log_config(logpath.get());
1217 m_outer->m_tranLog = new TransactionLog();
1221 // First load any property sets.
1224 const DOMElement* child;
1227 // Much of the processing can only occur on the first instantiation.
1230 pair<bool,unsigned int> skew=getUnsignedInt("clockSkew");
1232 xmlConf.clock_skew_secs=skew.second;
1235 doExtensions(e, "global", log);
1236 if (conf.isEnabled(SPConfig::OutOfProcess))
1237 doExtensions(SHAR, "out of process", log);
1239 if (conf.isEnabled(SPConfig::InProcess))
1240 doExtensions(SHIRE, "in process", log);
1242 // Instantiate the ListenerService and SessionCache objects.
1243 if (conf.isEnabled(SPConfig::Listener)) {
1244 child=XMLHelper::getFirstChildElement(e,UnixListener);
1246 plugtype=UNIX_LISTENER_SERVICE;
1248 child=XMLHelper::getFirstChildElement(e,TCPListener);
1250 plugtype=TCP_LISTENER_SERVICE;
1252 child=XMLHelper::getFirstChildElement(e,Listener);
1254 auto_ptr_char type(child->getAttributeNS(NULL,_type));
1256 plugtype=type.get();
1261 log.info("building ListenerService of type %s...", plugtype.c_str());
1262 m_outer->m_listener = conf.ListenerServiceManager.newPlugin(plugtype.c_str(), child);
1265 log.fatal("can't build ListenerService, missing conf:Listener element?");
1266 throw ConfigurationException("Can't build ListenerService, missing conf:Listener element?");
1271 if (m_outer->m_listener && conf.isEnabled(SPConfig::OutOfProcess) && !conf.isEnabled(SPConfig::InProcess)) {
1272 m_outer->m_listener->regListener("set::RelayState", m_outer->m_listener);
1273 m_outer->m_listener->regListener("get::RelayState", m_outer->m_listener);
1277 if (conf.isEnabled(SPConfig::Caching)) {
1278 if (conf.isEnabled(SPConfig::OutOfProcess)) {
1280 // First build any StorageServices.
1281 child=XMLHelper::getFirstChildElement(e,_StorageService);
1283 auto_ptr_char id(child->getAttributeNS(NULL,_id));
1284 auto_ptr_char type(child->getAttributeNS(NULL,_type));
1286 log.info("building StorageService (%s) of type %s...", id.get(), type.get());
1287 m_outer->m_storage[id.get()] = xmlConf.StorageServiceManager.newPlugin(type.get(),child);
1289 catch (exception& ex) {
1290 log.crit("failed to instantiate StorageService (%s): %s", id.get(), ex.what());
1292 child=XMLHelper::getNextSiblingElement(child,_StorageService);
1296 StorageService* replaySS=NULL;
1297 child=XMLHelper::getFirstChildElement(e,_ReplayCache);
1299 auto_ptr_char ssid(child->getAttributeNS(NULL,_StorageService));
1300 if (ssid.get() && *ssid.get()) {
1301 if (m_outer->m_storage.count(ssid.get()))
1302 replaySS = m_outer->m_storage[ssid.get()];
1304 log.info("building ReplayCache on top of StorageService (%s)...", ssid.get());
1306 log.warn("unable to locate StorageService (%s) for ReplayCache, using dedicated in-memory instance", ssid.get());
1308 xmlConf.setReplayCache(new ReplayCache(replaySS));
1311 log.warn("no ReplayCache built, missing conf:ReplayCache element?");
1315 child=XMLHelper::getFirstChildElement(e,_ArtifactMap);
1317 auto_ptr_char ssid(child->getAttributeNS(NULL,_StorageService));
1318 if (ssid.get() && *ssid.get() && m_outer->m_storage.count(ssid.get())) {
1319 log.info("building ArtifactMap on top of StorageService (%s)...", ssid.get());
1320 samlConf.setArtifactMap(new ArtifactMap(child, m_outer->m_storage[ssid.get()]));
1323 if (samlConf.getArtifactMap()==NULL) {
1324 log.info("building in-memory ArtifactMap...");
1325 samlConf.setArtifactMap(new ArtifactMap(child));
1329 child=XMLHelper::getFirstChildElement(e,_SessionCache);
1331 auto_ptr_char type(child->getAttributeNS(NULL,_type));
1332 log.info("building SessionCache of type %s...",type.get());
1333 m_outer->m_sessionCache=conf.SessionCacheManager.newPlugin(type.get(), child);
1336 log.fatal("can't build SessionCache, missing conf:SessionCache element?");
1337 throw ConfigurationException("Can't build SessionCache, missing conf:SessionCache element?");
1340 } // end of first-time-only stuff
1342 // Back to the fully dynamic stuff...next up is the RequestMapper.
1343 if (conf.isEnabled(SPConfig::RequestMapping)) {
1344 child=XMLHelper::getFirstChildElement(e,_RequestMapper);
1346 auto_ptr_char type(child->getAttributeNS(NULL,_type));
1347 log.info("building RequestMapper of type %s...",type.get());
1348 m_requestMapper=conf.RequestMapperManager.newPlugin(type.get(),child);
1351 log.fatal("can't build RequestMapper, missing conf:RequestMapper element?");
1352 throw ConfigurationException("Can't build RequestMapper, missing conf:RequestMapper element?");
1357 // Load security policies.
1358 child = XMLHelper::getLastChildElement(e,SecurityPolicies);
1360 PolicyNodeFilter filter;
1361 child = XMLHelper::getFirstChildElement(child,Policy);
1363 auto_ptr_char id(child->getAttributeNS(NULL,_id));
1364 pair< PropertySet*,vector<const SecurityPolicyRule*> >& rules = m_policyMap[id.get()];
1366 auto_ptr<DOMPropertySet> settings(new DOMPropertySet());
1367 settings->load(child, log, &filter);
1368 rules.first = settings.release();
1370 // Process Rule elements.
1371 const DOMElement* rule = XMLHelper::getFirstChildElement(child,Rule);
1373 auto_ptr_char type(rule->getAttributeNS(NULL,_type));
1375 rules.second.push_back(samlConf.SecurityPolicyRuleManager.newPlugin(type.get(),rule));
1377 catch (exception& ex) {
1378 log.crit("error instantiating policy rule (%s) in policy (%s): %s", type.get(), id.get(), ex.what());
1380 rule = XMLHelper::getNextSiblingElement(rule,Rule);
1383 // Process TransportOption elements.
1384 rule = XMLHelper::getFirstChildElement(child,TransportOption);
1386 if (rule->hasChildNodes()) {
1387 auto_ptr_char provider(rule->getAttributeNS(NULL,_provider));
1388 auto_ptr_char option(rule->getAttributeNS(NULL,_option));
1389 auto_ptr_char value(rule->getFirstChild()->getNodeValue());
1390 if (provider.get() && *provider.get() && option.get() && *option.get() && value.get() && *value.get()) {
1391 m_transportOptionMap[id.get()].push_back(
1392 make_pair(string(provider.get()), make_pair(string(option.get()), string(value.get())))
1396 rule = XMLHelper::getNextSiblingElement(rule,TransportOption);
1399 child = XMLHelper::getNextSiblingElement(child,Policy);
1404 // Load the default application. This actually has a fixed ID of "default". ;-)
1405 child=XMLHelper::getLastChildElement(e,Applications);
1407 log.fatal("can't build default Application object, missing conf:Applications element?");
1408 throw ConfigurationException("can't build default Application object, missing conf:Applications element?");
1410 XMLApplication* defapp=new XMLApplication(m_outer,child);
1411 m_appmap[defapp->getId()]=defapp;
1413 // Load any overrides.
1414 child = XMLHelper::getFirstChildElement(child,_Application);
1416 auto_ptr<XMLApplication> iapp(new XMLApplication(m_outer,child,defapp));
1417 if (m_appmap.count(iapp->getId()))
1418 log.crit("found conf:Application element with duplicate id attribute (%s), skipping it", iapp->getId());
1420 m_appmap[iapp->getId()]=iapp.release();
1422 child = XMLHelper::getNextSiblingElement(child,_Application);
1425 catch (exception&) {
1431 XMLConfigImpl::~XMLConfigImpl()
1436 void XMLConfigImpl::cleanup()
1438 for_each(m_appmap.begin(),m_appmap.end(),cleanup_pair<string,Application>());
1441 for (map< string,pair<PropertySet*,vector<const SecurityPolicyRule*> > >::iterator i=m_policyMap.begin(); i!=m_policyMap.end(); ++i) {
1442 delete i->second.first;
1443 for_each(i->second.second.begin(), i->second.second.end(), xmltooling::cleanup<SecurityPolicyRule>());
1445 m_policyMap.clear();
1447 delete m_requestMapper;
1448 m_requestMapper = NULL;
1450 m_document->release();
1455 void XMLConfig::receive(DDF& in, ostream& out)
1457 if (!strcmp(in.name(), "get::RelayState")) {
1458 const char* id = in["id"].string();
1459 const char* key = in["key"].string();
1461 throw ListenerException("Required parameters missing for RelayState recovery.");
1464 StorageService* storage = getStorageService(id);
1466 if (storage->readString("RelayState",key,&relayState)>0) {
1467 if (in["clear"].integer())
1468 storage->deleteString("RelayState",key);
1472 Category::getInstance(SHIBSP_LOGCAT".ServiceProvider").error(
1473 "Storage-backed RelayState with invalid StorageService ID (%s)", id
1477 // Repack for return to caller.
1478 DDF ret=DDF(NULL).string(relayState.c_str());
1479 DDFJanitor jret(ret);
1482 else if (!strcmp(in.name(), "set::RelayState")) {
1483 const char* id = in["id"].string();
1484 const char* value = in["value"].string();
1486 throw ListenerException("Required parameters missing for RelayState creation.");
1489 StorageService* storage = getStorageService(id);
1491 SAMLConfig::getConfig().generateRandomBytes(rsKey,20);
1492 rsKey = SAMLArtifact::toHex(rsKey);
1493 storage->createString("RelayState", rsKey.c_str(), value, time(NULL) + 600);
1496 Category::getInstance(SHIBSP_LOGCAT".ServiceProvider").error(
1497 "Storage-backed RelayState with invalid StorageService ID (%s)", id
1501 // Repack for return to caller.
1502 DDF ret=DDF(NULL).string(rsKey.c_str());
1503 DDFJanitor jret(ret);
1509 pair<bool,DOMElement*> XMLConfig::load()
1511 // Load from source using base class.
1512 pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
1514 // If we own it, wrap it.
1515 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
1517 XMLConfigImpl* impl = new XMLConfigImpl(raw.second,(m_impl==NULL),this,m_log);
1519 // If we held the document, transfer it to the impl. If we didn't, it's a no-op.
1520 impl->setDocument(docjanitor.release());
1525 return make_pair(false,(DOMElement*)NULL);