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