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