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