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