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