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