Config changes and rework for new credential APIs.
[shibboleth/sp.git] / shibsp / impl / XMLServiceProvider.cpp
1 /*\r
2  *  Copyright 2001-2007 Internet2\r
3  * \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
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  */\r
16 \r
17 /**\r
18  * XMLServiceProvider.cpp\r
19  *\r
20  * XML-based SP configuration and mgmt\r
21  */\r
22 \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/Handler.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
39 \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
53 \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
61 \r
62 namespace {\r
63 \r
64 #if defined (_MSC_VER)\r
65     #pragma warning( push )\r
66     #pragma warning( disable : 4250 )\r
67 #endif\r
68 \r
69     static vector<const Handler*> g_noHandlers;\r
70 \r
71     // Application configuration wrapper\r
72     class SHIBSP_DLLLOCAL XMLApplication : public virtual Application, public DOMPropertySet, public DOMNodeFilter\r
73     {\r
74     public:\r
75         XMLApplication(const ServiceProvider*, const DOMElement* e, const XMLApplication* base=NULL);\r
76         ~XMLApplication() { cleanup(); }\r
77     \r
78         // PropertySet\r
79         pair<bool,bool> getBool(const char* name, const char* ns=NULL) const;\r
80         pair<bool,const char*> getString(const char* name, const char* ns=NULL) const;\r
81         pair<bool,const XMLCh*> getXMLString(const char* name, const char* ns=NULL) const;\r
82         pair<bool,unsigned int> getUnsignedInt(const char* name, const char* ns=NULL) const;\r
83         pair<bool,int> getInt(const char* name, const char* ns=NULL) const;\r
84         const PropertySet* getPropertySet(const char* name, const char* ns="urn:mace:shibboleth:sp:config:2.0") const;\r
85 \r
86         // Application\r
87         const ServiceProvider& getServiceProvider() const {return *m_sp;}\r
88         const char* getId() const {return getString("id").second;}\r
89         const char* getHash() const {return m_hash.c_str();}\r
90 \r
91         MetadataProvider* getMetadataProvider() const {\r
92             return (!m_metadata && m_base) ? m_base->getMetadataProvider() : m_metadata;\r
93         }\r
94         TrustEngine* getTrustEngine() const {\r
95             return (!m_trust && m_base) ? m_base->getTrustEngine() : m_trust;\r
96         }\r
97         AttributeResolver* getAttributeResolver() const {\r
98             return (!m_attrResolver && m_base) ? m_base->getAttributeResolver() : m_attrResolver;\r
99         }\r
100         CredentialResolver* getCredentialResolver() const {\r
101             return (!m_credResolver && m_base) ? m_base->getCredentialResolver() : m_credResolver;\r
102         }\r
103         const PropertySet* getRelyingParty(const EntityDescriptor* provider) const;\r
104 \r
105         const Handler* getDefaultSessionInitiator() const;\r
106         const Handler* getSessionInitiatorById(const char* id) const;\r
107         const Handler* getDefaultAssertionConsumerService() const;\r
108         const Handler* getAssertionConsumerServiceByIndex(unsigned short index) const;\r
109         const vector<const Handler*>& getAssertionConsumerServicesByBinding(const XMLCh* binding) const;\r
110         const Handler* getHandler(const char* path) const;\r
111 \r
112         const vector<const XMLCh*>& getAudiences() const {\r
113             return (m_audiences.empty() && m_base) ? m_base->getAudiences() : m_audiences;\r
114         }\r
115 \r
116         // Provides filter to exclude special config elements.\r
117         short acceptNode(const DOMNode* node) const;\r
118     \r
119     private:\r
120         void cleanup();\r
121         const ServiceProvider* m_sp;   // this is ok because its locking scope includes us\r
122         const XMLApplication* m_base;\r
123         string m_hash;\r
124         MetadataProvider* m_metadata;\r
125         TrustEngine* m_trust;\r
126         AttributeResolver* m_attrResolver;\r
127         CredentialResolver* m_credResolver;\r
128         vector<const XMLCh*> m_audiences;\r
129 \r
130         // manage handler objects\r
131         vector<Handler*> m_handlers;\r
132 \r
133         // maps location (path info) to applicable handlers\r
134         map<string,const Handler*> m_handlerMap;\r
135 \r
136         // maps unique indexes to consumer services\r
137         map<unsigned int,const Handler*> m_acsIndexMap;\r
138         \r
139         // pointer to default consumer service\r
140         const Handler* m_acsDefault;\r
141 \r
142         // maps binding strings to supporting consumer service(s)\r
143 #ifdef HAVE_GOOD_STL\r
144         typedef map<xstring,vector<const Handler*> > ACSBindingMap;\r
145 #else\r
146         typedef map<string,vector<const Handler*> > ACSBindingMap;\r
147 #endif\r
148         ACSBindingMap m_acsBindingMap;\r
149 \r
150         // maps unique ID strings to session initiators\r
151         map<string,const Handler*> m_sessionInitMap;\r
152 \r
153         // pointer to default session initiator\r
154         const Handler* m_sessionInitDefault;\r
155 \r
156         // RelyingParty properties\r
157         DOMPropertySet* m_partyDefault;\r
158 #ifdef HAVE_GOOD_STL\r
159         map<xstring,PropertySet*> m_partyMap;\r
160 #else\r
161         map<const XMLCh*,PropertySet*> m_partyMap;\r
162 #endif\r
163     };\r
164 \r
165     // Top-level configuration implementation\r
166     class SHIBSP_DLLLOCAL XMLConfig;\r
167     class SHIBSP_DLLLOCAL XMLConfigImpl : public DOMPropertySet, public DOMNodeFilter\r
168     {\r
169     public:\r
170         XMLConfigImpl(const DOMElement* e, bool first, const XMLConfig* outer);\r
171         ~XMLConfigImpl();\r
172         \r
173         RequestMapper* m_requestMapper;\r
174         map<string,Application*> m_appmap;\r
175         map< string,pair< PropertySet*,vector<const SecurityPolicyRule*> > > m_policyMap;\r
176         \r
177         // Provides filter to exclude special config elements.\r
178         short acceptNode(const DOMNode* node) const;\r
179 \r
180         void setDocument(DOMDocument* doc) {\r
181             m_document = doc;\r
182         }\r
183 \r
184     private:\r
185         void doExtensions(const DOMElement* e, const char* label, Category& log);\r
186 \r
187         const XMLConfig* m_outer;\r
188         DOMDocument* m_document;\r
189     };\r
190 \r
191     class SHIBSP_DLLLOCAL XMLConfig : public ServiceProvider, public ReloadableXMLFile\r
192     {\r
193     public:\r
194         XMLConfig(const DOMElement* e)\r
195             : ReloadableXMLFile(e), m_impl(NULL), m_listener(NULL), m_sessionCache(NULL), m_tranLog(NULL) {\r
196         }\r
197         \r
198         void init() {\r
199             load();\r
200         }\r
201 \r
202         ~XMLConfig() {\r
203             delete m_impl;\r
204             delete m_sessionCache;\r
205             delete m_listener;\r
206             delete m_tranLog;\r
207             XMLToolingConfig::getConfig().setReplayCache(NULL);\r
208             SAMLConfig::getConfig().setArtifactMap(NULL);\r
209             for_each(m_storage.begin(), m_storage.end(), cleanup_pair<string,StorageService>());\r
210         }\r
211 \r
212         // PropertySet\r
213         pair<bool,bool> getBool(const char* name, const char* ns=NULL) const {return m_impl->getBool(name,ns);}\r
214         pair<bool,const char*> getString(const char* name, const char* ns=NULL) const {return m_impl->getString(name,ns);}\r
215         pair<bool,const XMLCh*> getXMLString(const char* name, const char* ns=NULL) const {return m_impl->getXMLString(name,ns);}\r
216         pair<bool,unsigned int> getUnsignedInt(const char* name, const char* ns=NULL) const {return m_impl->getUnsignedInt(name,ns);}\r
217         pair<bool,int> getInt(const char* name, const char* ns=NULL) const {return m_impl->getInt(name,ns);}\r
218         const PropertySet* getPropertySet(const char* name, const char* ns="urn:mace:shibboleth:sp:config:2.0") const {return m_impl->getPropertySet(name,ns);}\r
219         const DOMElement* getElement() const {return m_impl->getElement();}\r
220 \r
221         // ServiceProvider\r
222         TransactionLog* getTransactionLog() const {\r
223             if (m_tranLog)\r
224                 return m_tranLog;\r
225             throw ConfigurationException("No TransactionLog available.");\r
226         }\r
227 \r
228         StorageService* getStorageService(const char* id) const {\r
229             if (id) {\r
230                 map<string,StorageService*>::const_iterator i=m_storage.find(id);\r
231                 if (i!=m_storage.end())\r
232                     return i->second;\r
233             }\r
234             return NULL;\r
235         }\r
236 \r
237         ListenerService* getListenerService(bool required=true) const {\r
238             if (required && !m_listener)\r
239                 throw ConfigurationException("No ListenerService available.");\r
240             return m_listener;\r
241         }\r
242 \r
243         SessionCache* getSessionCache(bool required=true) const {\r
244             if (required && !m_sessionCache)\r
245                 throw ConfigurationException("No SessionCache available.");\r
246             return m_sessionCache;\r
247         }\r
248 \r
249         RequestMapper* getRequestMapper(bool required=true) const {\r
250             if (required && !m_impl->m_requestMapper)\r
251                 throw ConfigurationException("No RequestMapper available.");\r
252             return m_impl->m_requestMapper;\r
253         }\r
254 \r
255         const Application* getApplication(const char* applicationId) const {\r
256             map<string,Application*>::const_iterator i=m_impl->m_appmap.find(applicationId);\r
257             return (i!=m_impl->m_appmap.end()) ? i->second : NULL;\r
258         }\r
259 \r
260         const PropertySet* getPolicySettings(const char* id) const {\r
261             map<string,pair<PropertySet*,vector<const SecurityPolicyRule*> > >::const_iterator i = m_impl->m_policyMap.find(id);\r
262             if (i!=m_impl->m_policyMap.end())\r
263                 return i->second.first;\r
264             throw ConfigurationException("Security Policy ($1) not found, check <SecurityPolicies> element.", params(1,id));\r
265         }\r
266 \r
267         const vector<const SecurityPolicyRule*>& getPolicyRules(const char* id) const {\r
268             map<string,pair<PropertySet*,vector<const SecurityPolicyRule*> > >::const_iterator i = m_impl->m_policyMap.find(id);\r
269             if (i!=m_impl->m_policyMap.end())\r
270                 return i->second.second;\r
271             throw ConfigurationException("Security Policy ($1) not found, check <SecurityPolicies> element.", params(1,id));\r
272         }\r
273 \r
274     protected:\r
275         pair<bool,DOMElement*> load();\r
276 \r
277     private:\r
278         friend class XMLConfigImpl;\r
279         XMLConfigImpl* m_impl;\r
280         mutable ListenerService* m_listener;\r
281         mutable SessionCache* m_sessionCache;\r
282         mutable TransactionLog* m_tranLog;\r
283         mutable map<string,StorageService*> m_storage;\r
284     };\r
285 \r
286 #if defined (_MSC_VER)\r
287     #pragma warning( pop )\r
288 #endif\r
289 \r
290     static const XMLCh _Application[] =         UNICODE_LITERAL_11(A,p,p,l,i,c,a,t,i,o,n);\r
291     static const XMLCh Applications[] =         UNICODE_LITERAL_12(A,p,p,l,i,c,a,t,i,o,n,s);\r
292     static const XMLCh _ArtifactMap[] =         UNICODE_LITERAL_11(A,r,t,i,f,a,c,t,M,a,p);\r
293     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
294     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
295     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
296     static const XMLCh fatal[] =                UNICODE_LITERAL_5(f,a,t,a,l);\r
297     static const XMLCh _Handler[] =             UNICODE_LITERAL_7(H,a,n,d,l,e,r);\r
298     static const XMLCh _id[] =                  UNICODE_LITERAL_2(i,d);\r
299     static const XMLCh Implementation[] =       UNICODE_LITERAL_14(I,m,p,l,e,m,e,n,t,a,t,i,o,n);\r
300     static const XMLCh InProcess[] =            UNICODE_LITERAL_9(I,n,P,r,o,c,e,s,s);\r
301     static const XMLCh Library[] =              UNICODE_LITERAL_7(L,i,b,r,a,r,y);\r
302     static const XMLCh Listener[] =             UNICODE_LITERAL_8(L,i,s,t,e,n,e,r);\r
303     static const XMLCh logger[] =               UNICODE_LITERAL_6(l,o,g,g,e,r);\r
304     static const XMLCh MemoryListener[] =       UNICODE_LITERAL_14(M,e,m,o,r,y,L,i,s,t,e,n,e,r);\r
305     static const XMLCh _MetadataProvider[] =    UNICODE_LITERAL_16(M,e,t,a,d,a,t,a,P,r,o,v,i,d,e,r);\r
306     static const XMLCh OutOfProcess[] =         UNICODE_LITERAL_12(O,u,t,O,f,P,r,o,c,e,s,s);\r
307     static const XMLCh _path[] =                UNICODE_LITERAL_4(p,a,t,h);\r
308     static const XMLCh Policy[] =               UNICODE_LITERAL_6(P,o,l,i,c,y);\r
309     static const XMLCh RelyingParty[] =         UNICODE_LITERAL_12(R,e,l,y,i,n,g,P,a,r,t,y);\r
310     static const XMLCh _ReplayCache[] =         UNICODE_LITERAL_11(R,e,p,l,a,y,C,a,c,h,e);\r
311     static const XMLCh _RequestMapper[] =       UNICODE_LITERAL_13(R,e,q,u,e,s,t,M,a,p,p,e,r);\r
312     static const XMLCh Rule[] =                 UNICODE_LITERAL_4(R,u,l,e);\r
313     static const XMLCh SecurityPolicies[] =     UNICODE_LITERAL_16(S,e,c,u,r,i,t,y,P,o,l,i,c,i,e,s);\r
314     static const XMLCh _SessionCache[] =        UNICODE_LITERAL_12(S,e,s,s,i,o,n,C,a,c,h,e);\r
315     static const XMLCh SessionInitiator[] =     UNICODE_LITERAL_16(S,e,s,s,i,o,n,I,n,i,t,i,a,t,o,r);\r
316     static const XMLCh _StorageService[] =      UNICODE_LITERAL_14(S,t,o,r,a,g,e,S,e,r,v,i,c,e);\r
317     static const XMLCh TCPListener[] =          UNICODE_LITERAL_11(T,C,P,L,i,s,t,e,n,e,r);\r
318     static const XMLCh _TrustEngine[] =         UNICODE_LITERAL_11(T,r,u,s,t,E,n,g,i,n,e);\r
319     static const XMLCh _type[] =                UNICODE_LITERAL_4(t,y,p,e);\r
320     static const XMLCh UnixListener[] =         UNICODE_LITERAL_12(U,n,i,x,L,i,s,t,e,n,e,r);\r
321 \r
322     class SHIBSP_DLLLOCAL PolicyNodeFilter : public DOMNodeFilter\r
323     {\r
324     public:\r
325         short acceptNode(const DOMNode* node) const {\r
326             if (XMLHelper::isNodeNamed(node,shibspconstants::SHIB2SPCONFIG_NS,Rule))\r
327                 return FILTER_REJECT;\r
328             return FILTER_ACCEPT;\r
329         }\r
330     };\r
331 };\r
332 \r
333 namespace shibsp {\r
334     ServiceProvider* XMLServiceProviderFactory(const DOMElement* const & e)\r
335     {\r
336         return new XMLConfig(e);\r
337     }\r
338 };\r
339 \r
340 XMLApplication::XMLApplication(\r
341     const ServiceProvider* sp,\r
342     const DOMElement* e,\r
343     const XMLApplication* base\r
344     ) : m_sp(sp), m_base(base), m_metadata(NULL), m_trust(NULL), m_attrResolver(NULL), m_credResolver(NULL),\r
345         m_partyDefault(NULL), m_sessionInitDefault(NULL), m_acsDefault(NULL)\r
346 {\r
347 #ifdef _DEBUG\r
348     xmltooling::NDC ndc("XMLApplication");\r
349 #endif\r
350     Category& log=Category::getInstance(SHIBSP_LOGCAT".Application");\r
351 \r
352     try {\r
353         // First load any property sets.\r
354         load(e,log,this);\r
355 \r
356         SPConfig& conf=SPConfig::getConfig();\r
357         SAMLConfig& samlConf=SAMLConfig::getConfig();\r
358         XMLToolingConfig& xmlConf=XMLToolingConfig::getConfig();\r
359 \r
360         m_hash=getId();\r
361         m_hash+=getString("providerId").second;\r
362         m_hash=samlConf.hashSHA1(m_hash.c_str(), true);\r
363 \r
364         const PropertySet* sessions = getPropertySet("Sessions");\r
365 \r
366         // Process handlers.\r
367         Handler* handler=NULL;\r
368         bool hardACS=false, hardSessionInit=false;\r
369         const DOMElement* child = sessions ? XMLHelper::getFirstChildElement(sessions->getElement()) : NULL;\r
370         while (child) {\r
371             try {\r
372                 // A handler is based on the Binding property in conjunction with the element name.\r
373                 // If it's an ACS or SI, also handle index/id mappings and defaulting.\r
374                 if (XMLHelper::isNodeNamed(child,samlconstants::SAML20MD_NS,AssertionConsumerService::LOCAL_NAME)) {\r
375                     auto_ptr_char bindprop(child->getAttributeNS(NULL,EndpointType::BINDING_ATTRIB_NAME));\r
376                     if (!bindprop.get() || !*(bindprop.get())) {\r
377                         log.warn("md:AssertionConsumerService element has no Binding attribute, skipping it...");\r
378                         child = XMLHelper::getNextSiblingElement(child);\r
379                         continue;\r
380                     }\r
381                     handler=conf.AssertionConsumerServiceManager.newPlugin(bindprop.get(),make_pair(child, getId()));\r
382                     // Map by binding (may be > 1 per binding, e.g. SAML 1.0 vs 1.1)\r
383 #ifdef HAVE_GOOD_STL\r
384                     m_acsBindingMap[handler->getXMLString("Binding").second].push_back(handler);\r
385 #else\r
386                     m_acsBindingMap[handler->getString("Binding").second].push_back(handler);\r
387 #endif\r
388                     m_acsIndexMap[handler->getUnsignedInt("index").second]=handler;\r
389                     \r
390                     if (!hardACS) {\r
391                         pair<bool,bool> defprop=handler->getBool("isDefault");\r
392                         if (defprop.first) {\r
393                             if (defprop.second) {\r
394                                 hardACS=true;\r
395                                 m_acsDefault=handler;\r
396                             }\r
397                         }\r
398                         else if (!m_acsDefault)\r
399                             m_acsDefault=handler;\r
400                     }\r
401                 }\r
402                 else if (XMLString::equals(child->getLocalName(),SessionInitiator)) {\r
403                     auto_ptr_char bindprop(child->getAttributeNS(NULL,EndpointType::BINDING_ATTRIB_NAME));\r
404                     if (!bindprop.get() || !*(bindprop.get())) {\r
405                         log.warn("SessionInitiator element has no Binding attribute, skipping it...");\r
406                         child = XMLHelper::getNextSiblingElement(child);\r
407                         continue;\r
408                     }\r
409                     handler=conf.SessionInitiatorManager.newPlugin(bindprop.get(),make_pair(child, getId()));\r
410                     pair<bool,const char*> si_id=handler->getString("id");\r
411                     if (si_id.first && si_id.second)\r
412                         m_sessionInitMap[si_id.second]=handler;\r
413                     if (!hardSessionInit) {\r
414                         pair<bool,bool> defprop=handler->getBool("isDefault");\r
415                         if (defprop.first) {\r
416                             if (defprop.second) {\r
417                                 hardSessionInit=true;\r
418                                 m_sessionInitDefault=handler;\r
419                             }\r
420                         }\r
421                         else if (!m_sessionInitDefault)\r
422                             m_sessionInitDefault=handler;\r
423                     }\r
424                 }\r
425                 else if (XMLHelper::isNodeNamed(child,samlconstants::SAML20MD_NS,SingleLogoutService::LOCAL_NAME)) {\r
426                     auto_ptr_char bindprop(child->getAttributeNS(NULL,EndpointType::BINDING_ATTRIB_NAME));\r
427                     if (!bindprop.get() || !*(bindprop.get())) {\r
428                         log.warn("md:SingleLogoutService element has no Binding attribute, skipping it...");\r
429                         child = XMLHelper::getNextSiblingElement(child);\r
430                         continue;\r
431                     }\r
432                     handler=conf.SingleLogoutServiceManager.newPlugin(bindprop.get(),make_pair(child, getId()));\r
433                 }\r
434                 else if (XMLHelper::isNodeNamed(child,samlconstants::SAML20MD_NS,ManageNameIDService::LOCAL_NAME)) {\r
435                     auto_ptr_char bindprop(child->getAttributeNS(NULL,EndpointType::BINDING_ATTRIB_NAME));\r
436                     if (!bindprop.get() || !*(bindprop.get())) {\r
437                         log.warn("md:ManageNameIDService element has no Binding attribute, skipping it...");\r
438                         child = XMLHelper::getNextSiblingElement(child);\r
439                         continue;\r
440                     }\r
441                     handler=conf.ManageNameIDServiceManager.newPlugin(bindprop.get(),make_pair(child, getId()));\r
442                 }\r
443                 else {\r
444                     auto_ptr_char type(child->getAttributeNS(NULL,_type));\r
445                     if (!type.get() || !*(type.get())) {\r
446                         log.warn("Handler element has no type attribute, skipping it...");\r
447                         child = XMLHelper::getNextSiblingElement(child);\r
448                         continue;\r
449                     }\r
450                     handler=conf.HandlerManager.newPlugin(type.get(),make_pair(child, getId()));\r
451                 }\r
452 \r
453                 // Save off the objects after giving the property set to the handler for its use.\r
454                 m_handlers.push_back(handler);\r
455 \r
456                 // Insert into location map.\r
457                 pair<bool,const char*> location=handler->getString("Location");\r
458                 if (location.first && *location.second == '/')\r
459                     m_handlerMap[location.second]=handler;\r
460                 else if (location.first)\r
461                     m_handlerMap[string("/") + location.second]=handler;\r
462 \r
463             }\r
464             catch (exception& ex) {\r
465                 log.error("caught exception processing handler element: %s", ex.what());\r
466             }\r
467             \r
468             child = XMLHelper::getNextSiblingElement(child);\r
469         }\r
470 \r
471         DOMNodeList* nlist=e->getElementsByTagNameNS(samlconstants::SAML20_NS,Audience::LOCAL_NAME);\r
472         for (XMLSize_t i=0; nlist && i<nlist->getLength(); i++)\r
473             if (nlist->item(i)->getParentNode()->isSameNode(e) && nlist->item(i)->hasChildNodes())\r
474                 m_audiences.push_back(nlist->item(i)->getFirstChild()->getNodeValue());\r
475 \r
476         // Always include our own providerId as an audience.\r
477         m_audiences.push_back(getXMLString("providerId").second);\r
478 \r
479         if (conf.isEnabled(SPConfig::Metadata)) {\r
480             child = XMLHelper::getFirstChildElement(e,_MetadataProvider);\r
481             if (child) {\r
482                 auto_ptr_char type(child->getAttributeNS(NULL,_type));\r
483                 log.info("building MetadataProvider of type %s...",type.get());\r
484                 try {\r
485                     auto_ptr<MetadataProvider> mp(samlConf.MetadataProviderManager.newPlugin(type.get(),child));\r
486                     mp->init();\r
487                     m_metadata = mp.release();\r
488                 }\r
489                 catch (exception& ex) {\r
490                     log.crit("error building/initializing MetadataProvider: %s", ex.what());\r
491                 }\r
492             }\r
493         }\r
494 \r
495         if (conf.isEnabled(SPConfig::Trust)) {\r
496             child = XMLHelper::getFirstChildElement(e,_TrustEngine);\r
497             if (child) {\r
498                 auto_ptr_char type(child->getAttributeNS(NULL,_type));\r
499                 log.info("building TrustEngine of type %s...",type.get());\r
500                 try {\r
501                     m_trust = xmlConf.TrustEngineManager.newPlugin(type.get(),child);\r
502                 }\r
503                 catch (exception& ex) {\r
504                     log.crit("error building TrustEngine: %s", ex.what());\r
505                 }\r
506             }\r
507         }\r
508 \r
509         if (conf.isEnabled(SPConfig::AttributeResolution)) {\r
510             child = XMLHelper::getFirstChildElement(e,_AttributeResolver);\r
511             if (child) {\r
512                 auto_ptr_char type(child->getAttributeNS(NULL,_type));\r
513                 log.info("building AttributeResolver of type %s...",type.get());\r
514                 try {\r
515                     m_attrResolver = conf.AttributeResolverManager.newPlugin(type.get(),child);\r
516                 }\r
517                 catch (exception& ex) {\r
518                     log.crit("error building AttributeResolver: %s", ex.what());\r
519                 }\r
520             }\r
521         }\r
522 \r
523         if (conf.isEnabled(SPConfig::Credentials)) {\r
524             child = XMLHelper::getFirstChildElement(e,_CredentialResolver);\r
525             if (child) {\r
526                 auto_ptr_char type(child->getAttributeNS(NULL,_type));\r
527                 log.info("building CredentialResolver of type %s...",type.get());\r
528                 try {\r
529                     m_credResolver = xmlConf.CredentialResolverManager.newPlugin(type.get(),child);\r
530                 }\r
531                 catch (exception& ex) {\r
532                     log.crit("error building CredentialResolver: %s", ex.what());\r
533                 }\r
534             }\r
535         }\r
536 \r
537 \r
538         // Finally, load relying parties.\r
539         child = XMLHelper::getFirstChildElement(e,DefaultRelyingParty);\r
540         if (child) {\r
541             m_partyDefault=new DOMPropertySet();\r
542             m_partyDefault->load(child,log,this);\r
543             child = XMLHelper::getFirstChildElement(child,RelyingParty);\r
544             while (child) {\r
545                 auto_ptr<DOMPropertySet> rp(new DOMPropertySet());\r
546                 rp->load(child,log,this);\r
547                 m_partyMap[child->getAttributeNS(NULL,saml2::Attribute::NAME_ATTRIB_NAME)]=rp.release();\r
548                 child = XMLHelper::getNextSiblingElement(child,RelyingParty);\r
549             }\r
550         }\r
551         \r
552         if (conf.isEnabled(SPConfig::OutOfProcess)) {\r
553             // Really finally, build local browser profile and binding objects.\r
554             // TODO: may need some bits here...\r
555         }\r
556     }\r
557     catch (exception&) {\r
558         cleanup();\r
559         throw;\r
560     }\r
561 #ifndef _DEBUG\r
562     catch (...) {\r
563         cleanup();\r
564         throw;\r
565     }\r
566 #endif\r
567 }\r
568 \r
569 void XMLApplication::cleanup()\r
570 {\r
571     delete m_partyDefault;\r
572 #ifdef HAVE_GOOD_STL\r
573     for_each(m_partyMap.begin(),m_partyMap.end(),cleanup_pair<xstring,PropertySet>());\r
574 #else\r
575     for_each(m_partyMap.begin(),m_partyMap.end(),cleanup_pair<const XMLCh*,PropertySet>());\r
576 #endif\r
577     for_each(m_handlers.begin(),m_handlers.end(),xmltooling::cleanup<Handler>());\r
578     delete m_credResolver;\r
579     delete m_attrResolver;\r
580     delete m_trust;\r
581     delete m_metadata;\r
582 }\r
583 \r
584 short XMLApplication::acceptNode(const DOMNode* node) const\r
585 {\r
586     if (XMLHelper::isNodeNamed(node,samlconstants::SAML20_NS,saml2::Attribute::LOCAL_NAME))\r
587         return FILTER_REJECT;\r
588     else if (XMLHelper::isNodeNamed(node,samlconstants::SAML20_NS,Audience::LOCAL_NAME))\r
589         return FILTER_REJECT;\r
590     const XMLCh* name=node->getLocalName();\r
591     if (XMLString::equals(name,_Application) ||\r
592         XMLString::equals(name,AssertionConsumerService::LOCAL_NAME) ||\r
593         XMLString::equals(name,SingleLogoutService::LOCAL_NAME) ||\r
594         XMLString::equals(name,ManageNameIDService::LOCAL_NAME) ||\r
595         XMLString::equals(name,SessionInitiator) ||\r
596         XMLString::equals(name,DefaultRelyingParty) ||\r
597         XMLString::equals(name,RelyingParty) ||\r
598         XMLString::equals(name,_MetadataProvider) ||\r
599         XMLString::equals(name,_TrustEngine) ||\r
600         XMLString::equals(name,_CredentialResolver) ||\r
601         XMLString::equals(name,_AttributeResolver))\r
602         return FILTER_REJECT;\r
603 \r
604     return FILTER_ACCEPT;\r
605 }\r
606 \r
607 pair<bool,bool> XMLApplication::getBool(const char* name, const char* ns) const\r
608 {\r
609     pair<bool,bool> ret=DOMPropertySet::getBool(name,ns);\r
610     if (ret.first)\r
611         return ret;\r
612     return m_base ? m_base->getBool(name,ns) : ret;\r
613 }\r
614 \r
615 pair<bool,const char*> XMLApplication::getString(const char* name, const char* ns) const\r
616 {\r
617     pair<bool,const char*> ret=DOMPropertySet::getString(name,ns);\r
618     if (ret.first)\r
619         return ret;\r
620     return m_base ? m_base->getString(name,ns) : ret;\r
621 }\r
622 \r
623 pair<bool,const XMLCh*> XMLApplication::getXMLString(const char* name, const char* ns) const\r
624 {\r
625     pair<bool,const XMLCh*> ret=DOMPropertySet::getXMLString(name,ns);\r
626     if (ret.first)\r
627         return ret;\r
628     return m_base ? m_base->getXMLString(name,ns) : ret;\r
629 }\r
630 \r
631 pair<bool,unsigned int> XMLApplication::getUnsignedInt(const char* name, const char* ns) const\r
632 {\r
633     pair<bool,unsigned int> ret=DOMPropertySet::getUnsignedInt(name,ns);\r
634     if (ret.first)\r
635         return ret;\r
636     return m_base ? m_base->getUnsignedInt(name,ns) : ret;\r
637 }\r
638 \r
639 pair<bool,int> XMLApplication::getInt(const char* name, const char* ns) const\r
640 {\r
641     pair<bool,int> ret=DOMPropertySet::getInt(name,ns);\r
642     if (ret.first)\r
643         return ret;\r
644     return m_base ? m_base->getInt(name,ns) : ret;\r
645 }\r
646 \r
647 const PropertySet* XMLApplication::getPropertySet(const char* name, const char* ns) const\r
648 {\r
649     const PropertySet* ret=DOMPropertySet::getPropertySet(name,ns);\r
650     if (ret || !m_base)\r
651         return ret;\r
652     return m_base->getPropertySet(name,ns);\r
653 }\r
654 \r
655 const PropertySet* XMLApplication::getRelyingParty(const EntityDescriptor* provider) const\r
656 {\r
657     if (!m_partyDefault && m_base)\r
658         return m_base->getRelyingParty(provider);\r
659     else if (!provider)\r
660         return m_partyDefault;\r
661         \r
662 #ifdef HAVE_GOOD_STL\r
663     map<xstring,PropertySet*>::const_iterator i=m_partyMap.find(provider->getEntityID());\r
664     if (i!=m_partyMap.end())\r
665         return i->second;\r
666     const EntitiesDescriptor* group=dynamic_cast<const EntitiesDescriptor*>(provider->getParent());\r
667     while (group) {\r
668         if (group->getName()) {\r
669             i=m_partyMap.find(group->getName());\r
670             if (i!=m_partyMap.end())\r
671                 return i->second;\r
672         }\r
673         group=dynamic_cast<const EntitiesDescriptor*>(group->getParent());\r
674     }\r
675 #else\r
676     map<const XMLCh*,PropertySet*>::const_iterator i=m_partyMap.begin();\r
677     for (; i!=m_partyMap.end(); i++) {\r
678         if (XMLString::equals(i->first,provider->getId()))\r
679             return i->second;\r
680         const EntitiesDescriptor* group=dynamic_cast<const EntitiesDescriptor*>(provider->getParent());\r
681         while (group) {\r
682             if (XMLString::equals(i->first,group->getName()))\r
683                 return i->second;\r
684             group=dynamic_cast<const EntitiesDescriptor*>(group->getParent());\r
685         }\r
686     }\r
687 #endif\r
688     return m_partyDefault;\r
689 }\r
690 \r
691 const Handler* XMLApplication::getDefaultSessionInitiator() const\r
692 {\r
693     if (m_sessionInitDefault) return m_sessionInitDefault;\r
694     return m_base ? m_base->getDefaultSessionInitiator() : NULL;\r
695 }\r
696 \r
697 const Handler* XMLApplication::getSessionInitiatorById(const char* id) const\r
698 {\r
699     map<string,const Handler*>::const_iterator i=m_sessionInitMap.find(id);\r
700     if (i!=m_sessionInitMap.end()) return i->second;\r
701     return m_base ? m_base->getSessionInitiatorById(id) : NULL;\r
702 }\r
703 \r
704 const Handler* XMLApplication::getDefaultAssertionConsumerService() const\r
705 {\r
706     if (m_acsDefault) return m_acsDefault;\r
707     return m_base ? m_base->getDefaultAssertionConsumerService() : NULL;\r
708 }\r
709 \r
710 const Handler* XMLApplication::getAssertionConsumerServiceByIndex(unsigned short index) const\r
711 {\r
712     map<unsigned int,const Handler*>::const_iterator i=m_acsIndexMap.find(index);\r
713     if (i!=m_acsIndexMap.end()) return i->second;\r
714     return m_base ? m_base->getAssertionConsumerServiceByIndex(index) : NULL;\r
715 }\r
716 \r
717 const vector<const Handler*>& XMLApplication::getAssertionConsumerServicesByBinding(const XMLCh* binding) const\r
718 {\r
719 #ifdef HAVE_GOOD_STL\r
720     ACSBindingMap::const_iterator i=m_acsBindingMap.find(binding);\r
721 #else\r
722     auto_ptr_char temp(binding);\r
723     ACSBindingMap::const_iterator i=m_acsBindingMap.find(temp.get());\r
724 #endif\r
725     if (i!=m_acsBindingMap.end())\r
726         return i->second;\r
727     return m_base ? m_base->getAssertionConsumerServicesByBinding(binding) : g_noHandlers;\r
728 }\r
729 \r
730 const Handler* XMLApplication::getHandler(const char* path) const\r
731 {\r
732     string wrap(path);\r
733     map<string,const Handler*>::const_iterator i=m_handlerMap.find(wrap.substr(0,wrap.find('?')));\r
734     if (i!=m_handlerMap.end())\r
735         return i->second;\r
736     return m_base ? m_base->getHandler(path) : NULL;\r
737 }\r
738 \r
739 short XMLConfigImpl::acceptNode(const DOMNode* node) const\r
740 {\r
741     if (!XMLString::equals(node->getNamespaceURI(),shibspconstants::SHIB2SPCONFIG_NS))\r
742         return FILTER_ACCEPT;\r
743     const XMLCh* name=node->getLocalName();\r
744     if (XMLString::equals(name,Applications) ||\r
745         XMLString::equals(name,_ArtifactMap) ||\r
746         XMLString::equals(name,Extensions::LOCAL_NAME) ||\r
747         XMLString::equals(name,Implementation) ||\r
748         XMLString::equals(name,Listener) ||\r
749         XMLString::equals(name,MemoryListener) ||\r
750         XMLString::equals(name,Policy) ||\r
751         XMLString::equals(name,_RequestMapper) ||\r
752         XMLString::equals(name,_ReplayCache) ||\r
753         XMLString::equals(name,_SessionCache) ||\r
754         XMLString::equals(name,_StorageService) ||\r
755         XMLString::equals(name,TCPListener) ||\r
756         XMLString::equals(name,UnixListener))\r
757         return FILTER_REJECT;\r
758 \r
759     return FILTER_ACCEPT;\r
760 }\r
761 \r
762 void XMLConfigImpl::doExtensions(const DOMElement* e, const char* label, Category& log)\r
763 {\r
764     const DOMElement* exts=XMLHelper::getFirstChildElement(e,Extensions::LOCAL_NAME);\r
765     if (exts) {\r
766         exts=XMLHelper::getFirstChildElement(exts,Library);\r
767         while (exts) {\r
768             auto_ptr_char path(exts->getAttributeNS(NULL,_path));\r
769             try {\r
770                 if (path.get()) {\r
771                     XMLToolingConfig::getConfig().load_library(path.get(),(void*)exts);\r
772                     log.debug("loaded %s extension library (%s)", label, path.get());\r
773                 }\r
774             }\r
775             catch (exception& e) {\r
776                 const XMLCh* fatal=exts->getAttributeNS(NULL,fatal);\r
777                 if (fatal && (*fatal==chLatin_t || *fatal==chDigit_1)) {\r
778                     log.fatal("unable to load mandatory %s extension library %s: %s", label, path.get(), e.what());\r
779                     throw;\r
780                 }\r
781                 else {\r
782                     log.crit("unable to load optional %s extension library %s: %s", label, path.get(), e.what());\r
783                 }\r
784             }\r
785             exts=XMLHelper::getNextSiblingElement(exts,Library);\r
786         }\r
787     }\r
788 }\r
789 \r
790 XMLConfigImpl::XMLConfigImpl(const DOMElement* e, bool first, const XMLConfig* outer) : m_requestMapper(NULL), m_outer(outer), m_document(NULL)\r
791 {\r
792 #ifdef _DEBUG\r
793     xmltooling::NDC ndc("XMLConfigImpl");\r
794 #endif\r
795     Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");\r
796 \r
797     try {\r
798         SPConfig& conf=SPConfig::getConfig();\r
799         SAMLConfig& samlConf=SAMLConfig::getConfig();\r
800         XMLToolingConfig& xmlConf=XMLToolingConfig::getConfig();\r
801         const DOMElement* SHAR=XMLHelper::getFirstChildElement(e,OutOfProcess);\r
802         const DOMElement* SHIRE=XMLHelper::getFirstChildElement(e,InProcess);\r
803 \r
804         // Initialize log4cpp manually in order to redirect log messages as soon as possible.\r
805         if (conf.isEnabled(SPConfig::Logging)) {\r
806             const XMLCh* logconf=NULL;\r
807             if (conf.isEnabled(SPConfig::OutOfProcess))\r
808                 logconf=SHAR->getAttributeNS(NULL,logger);\r
809             else if (conf.isEnabled(SPConfig::InProcess))\r
810                 logconf=SHIRE->getAttributeNS(NULL,logger);\r
811             if (!logconf || !*logconf)\r
812                 logconf=e->getAttributeNS(NULL,logger);\r
813             if (logconf && *logconf) {\r
814                 auto_ptr_char logpath(logconf);\r
815                 log.debug("loading new logging configuration from (%s), check log destination for status of configuration",logpath.get());\r
816                 XMLToolingConfig::getConfig().log_config(logpath.get());\r
817             }\r
818             \r
819             if (first)\r
820                 m_outer->m_tranLog = new TransactionLog();\r
821         }\r
822         \r
823         // First load any property sets.\r
824         load(e,log,this);\r
825 \r
826         const DOMElement* child;\r
827         string plugtype;\r
828 \r
829         // Much of the processing can only occur on the first instantiation.\r
830         if (first) {\r
831             // Set clock skew.\r
832             pair<bool,unsigned int> skew=getUnsignedInt("clockSkew");\r
833             if (skew.first)\r
834                 xmlConf.clock_skew_secs=skew.second;\r
835 \r
836             // Extensions\r
837             doExtensions(e, "global", log);\r
838             if (conf.isEnabled(SPConfig::OutOfProcess))\r
839                 doExtensions(SHAR, "out of process", log);\r
840 \r
841             if (conf.isEnabled(SPConfig::InProcess))\r
842                 doExtensions(SHIRE, "in process", log);\r
843             \r
844             // Instantiate the ListenerService and SessionCache objects.\r
845             if (conf.isEnabled(SPConfig::Listener)) {\r
846                 child=XMLHelper::getFirstChildElement(SHAR,UnixListener);\r
847                 if (child)\r
848                     plugtype=UNIX_LISTENER_SERVICE;\r
849                 else {\r
850                     child=XMLHelper::getFirstChildElement(SHAR,TCPListener);\r
851                     if (child)\r
852                         plugtype=TCP_LISTENER_SERVICE;\r
853                     else {\r
854                         child=XMLHelper::getFirstChildElement(SHAR,MemoryListener);\r
855                         if (child)\r
856                             plugtype=MEMORY_LISTENER_SERVICE;\r
857                         else {\r
858                             child=XMLHelper::getFirstChildElement(SHAR,Listener);\r
859                             if (child) {\r
860                                 auto_ptr_char type(child->getAttributeNS(NULL,_type));\r
861                                 if (type.get())\r
862                                     plugtype=type.get();\r
863                             }\r
864                         }\r
865                     }\r
866                 }\r
867                 if (child) {\r
868                     log.info("building ListenerService of type %s...", plugtype.c_str());\r
869                     m_outer->m_listener = conf.ListenerServiceManager.newPlugin(plugtype.c_str(),child);\r
870                 }\r
871                 else {\r
872                     log.fatal("can't build ListenerService, missing conf:Listener element?");\r
873                     throw ConfigurationException("Can't build ListenerService, missing conf:Listener element?");\r
874                 }\r
875             }\r
876 \r
877             if (conf.isEnabled(SPConfig::Caching)) {\r
878                 if (conf.isEnabled(SPConfig::OutOfProcess)) {\r
879                     // First build any StorageServices.\r
880                     string inmemID;\r
881                     child=XMLHelper::getFirstChildElement(SHAR,_StorageService);\r
882                     while (child) {\r
883                         auto_ptr_char id(child->getAttributeNS(NULL,_id));\r
884                         auto_ptr_char type(child->getAttributeNS(NULL,_type));\r
885                         try {\r
886                             log.info("building StorageService (%s) of type %s...", id.get(), type.get());\r
887                             m_outer->m_storage[id.get()] = xmlConf.StorageServiceManager.newPlugin(type.get(),child);\r
888                             if (!strcmp(type.get(),MEMORY_STORAGE_SERVICE))\r
889                                 inmemID = id.get();\r
890                         }\r
891                         catch (exception& ex) {\r
892                             log.crit("failed to instantiate StorageService (%s): %s", id.get(), ex.what());\r
893                         }\r
894                         child=XMLHelper::getNextSiblingElement(child,_StorageService);\r
895                     }\r
896                 \r
897                     child=XMLHelper::getFirstChildElement(SHAR,_SessionCache);\r
898                     if (child) {\r
899                         auto_ptr_char type(child->getAttributeNS(NULL,_type));\r
900                         log.info("building SessionCache of type %s...",type.get());\r
901                         m_outer->m_sessionCache=conf.SessionCacheManager.newPlugin(type.get(),child);\r
902                     }\r
903                     else {\r
904                         log.warn("SessionCache unspecified, building SessionCache of type %s...",STORAGESERVICE_SESSION_CACHE);\r
905                         if (inmemID.empty()) {\r
906                             inmemID = "memory";\r
907                             log.info("no StorageServices configured, providing in-memory version for session cache");\r
908                             m_outer->m_storage[inmemID] = xmlConf.StorageServiceManager.newPlugin(MEMORY_STORAGE_SERVICE,NULL);\r
909                         }\r
910                         child = e->getOwnerDocument()->createElementNS(NULL,_SessionCache);\r
911                         auto_ptr_XMLCh ssid(inmemID.c_str());\r
912                         const_cast<DOMElement*>(child)->setAttributeNS(NULL,_StorageService,ssid.get());\r
913                         m_outer->m_sessionCache=conf.SessionCacheManager.newPlugin(STORAGESERVICE_SESSION_CACHE,child);\r
914                     }\r
915 \r
916                     // Replay cache.\r
917                     StorageService* replaySS=NULL;\r
918                     child=XMLHelper::getFirstChildElement(SHAR,_ReplayCache);\r
919                     if (child) {\r
920                         auto_ptr_char ssid(child->getAttributeNS(NULL,_StorageService));\r
921                         if (ssid.get() && *ssid.get()) {\r
922                             if (m_outer->m_storage.count(ssid.get()))\r
923                                 replaySS = m_outer->m_storage[ssid.get()];\r
924                             if (replaySS)\r
925                                 log.info("building ReplayCache on top of StorageService (%s)...", ssid.get());\r
926                             else\r
927                                 log.crit("unable to locate StorageService (%s) in configuration", ssid.get());\r
928                         }\r
929                     }\r
930                     if (!replaySS) {\r
931                         log.info("building ReplayCache using in-memory StorageService...");\r
932                         if (inmemID.empty()) {\r
933                             inmemID = "memory";\r
934                             log.info("no StorageServices configured, providing in-memory version for legacy config");\r
935                             m_outer->m_storage[inmemID] = xmlConf.StorageServiceManager.newPlugin(MEMORY_STORAGE_SERVICE,NULL);\r
936                         }\r
937                         replaySS = m_outer->m_storage[inmemID];\r
938                     }\r
939                     xmlConf.setReplayCache(new ReplayCache(replaySS));\r
940                     \r
941                     // ArtifactMap\r
942                     child=XMLHelper::getFirstChildElement(SHAR,_ArtifactMap);\r
943                     if (child) {\r
944                         auto_ptr_char ssid(child->getAttributeNS(NULL,_StorageService));\r
945                         if (ssid.get() && *ssid.get() && m_outer->m_storage.count(ssid.get())) {\r
946                             log.info("building ArtifactMap on top of StorageService (%s)...", ssid.get());\r
947                             samlConf.setArtifactMap(new ArtifactMap(child, m_outer->m_storage[ssid.get()]));\r
948                         }\r
949                     }\r
950                     if (samlConf.getArtifactMap()==NULL) {\r
951                         log.info("building in-memory ArtifactMap...");\r
952                         samlConf.setArtifactMap(new ArtifactMap(child));\r
953                     }\r
954                 }\r
955                 else {\r
956                     child=XMLHelper::getFirstChildElement(SHIRE,_SessionCache);\r
957                     if (child) {\r
958                         auto_ptr_char type(child->getAttributeNS(NULL,_type));\r
959                         log.info("building SessionCache of type %s...",type.get());\r
960                         m_outer->m_sessionCache=conf.SessionCacheManager.newPlugin(type.get(),child);\r
961                     }\r
962                     else {\r
963                         log.warn("SessionCache unspecified, building SessionCache of type %s...",REMOTED_SESSION_CACHE);\r
964                         m_outer->m_sessionCache=conf.SessionCacheManager.newPlugin(REMOTED_SESSION_CACHE,child);\r
965                     }\r
966                 }\r
967             }\r
968         } // end of first-time-only stuff\r
969         \r
970         // Back to the fully dynamic stuff...next up is the RequestMapper.\r
971         if (conf.isEnabled(SPConfig::RequestMapping)) {\r
972             child=XMLHelper::getFirstChildElement(SHIRE,_RequestMapper);\r
973             if (child) {\r
974                 auto_ptr_char type(child->getAttributeNS(NULL,_type));\r
975                 log.info("building RequestMapper of type %s...",type.get());\r
976                 m_requestMapper=conf.RequestMapperManager.newPlugin(type.get(),child);\r
977             }\r
978         }\r
979         \r
980         // Load security policies.\r
981         child = XMLHelper::getLastChildElement(e,SecurityPolicies);\r
982         if (child) {\r
983             PolicyNodeFilter filter;\r
984             child = XMLHelper::getFirstChildElement(child,Policy);\r
985             while (child) {\r
986                 auto_ptr_char id(child->getAttributeNS(NULL,_id));\r
987                 pair< PropertySet*,vector<const SecurityPolicyRule*> >& rules = m_policyMap[id.get()];\r
988                 rules.first = NULL;\r
989                 auto_ptr<DOMPropertySet> settings(new DOMPropertySet());\r
990                 settings->load(child, log, &filter);\r
991                 rules.first = settings.release();\r
992                 const DOMElement* rule = XMLHelper::getFirstChildElement(child,Rule);\r
993                 while (rule) {\r
994                     auto_ptr_char type(rule->getAttributeNS(NULL,_type));\r
995                     try {\r
996                         rules.second.push_back(samlConf.SecurityPolicyRuleManager.newPlugin(type.get(),rule));\r
997                     }\r
998                     catch (exception& ex) {\r
999                         log.crit("error instantiating policy rule (%s) in policy (%s): %s", type.get(), id.get(), ex.what());\r
1000                     }\r
1001                     rule = XMLHelper::getNextSiblingElement(rule,Rule);\r
1002                 }\r
1003                 child = XMLHelper::getNextSiblingElement(child,Policy);\r
1004             }\r
1005         }\r
1006 \r
1007         // Load the default application. This actually has a fixed ID of "default". ;-)\r
1008         child=XMLHelper::getLastChildElement(e,Applications);\r
1009         if (!child) {\r
1010             log.fatal("can't build default Application object, missing conf:Applications element?");\r
1011             throw ConfigurationException("can't build default Application object, missing conf:Applications element?");\r
1012         }\r
1013         XMLApplication* defapp=new XMLApplication(m_outer,child);\r
1014         m_appmap[defapp->getId()]=defapp;\r
1015         \r
1016         // Load any overrides.\r
1017         child = XMLHelper::getFirstChildElement(child,_Application);\r
1018         while (child) {\r
1019             auto_ptr<XMLApplication> iapp(new XMLApplication(m_outer,child,defapp));\r
1020             if (m_appmap.count(iapp->getId()))\r
1021                 log.crit("found conf:Application element with duplicate id attribute (%s), skipping it", iapp->getId());\r
1022             else\r
1023                 m_appmap[iapp->getId()]=iapp.release();\r
1024 \r
1025             child = XMLHelper::getNextSiblingElement(child,_Application);\r
1026         }\r
1027     }\r
1028     catch (exception&) {\r
1029         this->~XMLConfigImpl();\r
1030         throw;\r
1031     }\r
1032 #ifndef _DEBUG\r
1033     catch (...) {\r
1034         this->~XMLConfigImpl();\r
1035         throw;\r
1036     }\r
1037 #endif\r
1038 }\r
1039 \r
1040 XMLConfigImpl::~XMLConfigImpl()\r
1041 {\r
1042     for_each(m_appmap.begin(),m_appmap.end(),cleanup_pair<string,Application>());\r
1043     for (map< string,pair<PropertySet*,vector<const SecurityPolicyRule*> > >::iterator i=m_policyMap.begin(); i!=m_policyMap.end(); ++i) {\r
1044         delete i->second.first;\r
1045         for_each(i->second.second.begin(), i->second.second.end(), xmltooling::cleanup<SecurityPolicyRule>());\r
1046     }\r
1047     delete m_requestMapper;\r
1048     if (m_document)\r
1049         m_document->release();\r
1050 }\r
1051 \r
1052 pair<bool,DOMElement*> XMLConfig::load()\r
1053 {\r
1054     // Load from source using base class.\r
1055     pair<bool,DOMElement*> raw = ReloadableXMLFile::load();\r
1056     \r
1057     // If we own it, wrap it.\r
1058     XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);\r
1059 \r
1060     XMLConfigImpl* impl = new XMLConfigImpl(raw.second,(m_impl==NULL),this);\r
1061     \r
1062     // If we held the document, transfer it to the impl. If we didn't, it's a no-op.\r
1063     impl->setDocument(docjanitor.release());\r
1064 \r
1065     delete m_impl;\r
1066     m_impl = impl;\r
1067 \r
1068     return make_pair(false,(DOMElement*)NULL);\r
1069 }\r