Fixed additional warnings.
[shibboleth/cpp-sp.git] / shib-target / shib-ini.cpp
1 /*
2  *  Copyright 2001-2005 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * shib-ini.h -- config file handling, now XML-based
19  *
20  * $Id$
21  */
22
23 #include "internal.h"
24
25 #include <shib/shib-threads.h>
26 #include <log4cpp/Category.hh>
27 #include <log4cpp/PropertyConfigurator.hh>
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31
32 using namespace std;
33 using namespace saml;
34 using namespace shibboleth;
35 using namespace shibtarget;
36 using namespace log4cpp;
37
38 namespace shibtarget {
39
40     // Application configuration wrapper
41     class XMLApplication : public virtual IApplication, public XMLPropertySet, public DOMNodeFilter
42     {
43     public:
44         XMLApplication(const IConfig*, const Iterator<ICredentials*>& creds, const DOMElement* e, const XMLApplication* base=NULL);
45         ~XMLApplication() { cleanup(); }
46     
47         // IPropertySet
48         pair<bool,bool> getBool(const char* name, const char* ns=NULL) const;
49         pair<bool,const char*> getString(const char* name, const char* ns=NULL) const;
50         pair<bool,const XMLCh*> getXMLString(const char* name, const char* ns=NULL) const;
51         pair<bool,unsigned int> getUnsignedInt(const char* name, const char* ns=NULL) const;
52         pair<bool,int> getInt(const char* name, const char* ns=NULL) const;
53         const IPropertySet* getPropertySet(const char* name, const char* ns="urn:mace:shibboleth:target:config:1.0") const;
54
55         // IApplication
56         const char* getId() const {return getString("id").second;}
57         const char* getHash() const {return m_hash.c_str();}
58         Iterator<SAMLAttributeDesignator*> getAttributeDesignators() const;
59         Iterator<IAAP*> getAAPProviders() const;
60         Iterator<IMetadata*> getMetadataProviders() const;
61         Iterator<ITrust*> getTrustProviders() const;
62         Iterator<const XMLCh*> getAudiences() const;
63         const IPropertySet* getCredentialUse(const IEntityDescriptor* provider) const;
64         const SAMLBrowserProfile* getBrowserProfile() const {return m_profile;}
65         const SAMLBinding* getBinding(const XMLCh* binding) const
66             {return XMLString::compareString(SAMLBinding::SOAP,binding) ? NULL : m_binding;}
67         SAMLBrowserProfile::ArtifactMapper* getArtifactMapper() const {return new STArtifactMapper(this);}
68         const IPropertySet* getDefaultSessionInitiator() const;
69         const IPropertySet* getSessionInitiatorById(const char* id) const;
70         const IPropertySet* getDefaultAssertionConsumerService() const;
71         const IPropertySet* getAssertionConsumerServiceByIndex(unsigned short index) const;
72         const IPropertySet* getHandlerConfig(const char* path) const;
73         
74         // Provides filter to exclude special config elements.
75         short acceptNode(const DOMNode* node) const;
76     
77     private:
78         void cleanup();
79         const IConfig* m_ini;   // this is ok because its locking scope includes us
80         const XMLApplication* m_base;
81         string m_hash;
82         vector<SAMLAttributeDesignator*> m_designators;
83         vector<IAAP*> m_aaps;
84         vector<IMetadata*> m_metadatas;
85         vector<ITrust*> m_trusts;
86         vector<const XMLCh*> m_audiences;
87         ShibBrowserProfile* m_profile;
88         SAMLBinding* m_binding;
89         ShibHTTPHook* m_bindingHook;
90         map<string,XMLPropertySet*> m_handlerMap;
91         map<unsigned int,const IPropertySet*> m_acsMap;
92         const IPropertySet* m_acsDefault;
93         map<string,const IPropertySet*> m_sessionInitMap;
94         const IPropertySet* m_sessionInitDefault;
95         XMLPropertySet* m_credDefault;
96 #ifdef HAVE_GOOD_STL
97         map<xstring,XMLPropertySet*> m_credMap;
98 #else
99         map<const XMLCh*,XMLPropertySet*> m_credMap;
100 #endif
101     };
102
103     // Top-level configuration implementation
104     class XMLConfig;
105     class XMLConfigImpl : public ReloadableXMLFileImpl, public XMLPropertySet, public DOMNodeFilter
106     {
107     public:
108         XMLConfigImpl(const char* pathname, bool first, const XMLConfig* outer)
109             : ReloadableXMLFileImpl(pathname), m_requestMapper(NULL), m_outer(outer) { init(first); }
110         XMLConfigImpl(const DOMElement* e, bool first, const XMLConfig* outer)
111             : ReloadableXMLFileImpl(e), m_requestMapper(NULL), m_outer(outer) { init(first); }
112         ~XMLConfigImpl();
113         
114         IRequestMapper* m_requestMapper;
115         map<string,IApplication*> m_appmap;
116         vector<ICredentials*> m_creds;
117         
118         // Provides filter to exclude special config elements.
119         short acceptNode(const DOMNode* node) const;
120
121     private:
122         void init(bool first);
123         const XMLConfig* m_outer;
124     };
125     
126     class XMLConfig : public IConfig, public ReloadableXMLFile
127     {
128     public:
129         XMLConfig(const DOMElement* e) : ReloadableXMLFile(e), m_listener(NULL), m_sessionCache(NULL), m_replayCache(NULL) {}
130         ~XMLConfig();
131
132         // IPropertySet
133         pair<bool,bool> getBool(const char* name, const char* ns=NULL) const {return static_cast<XMLConfigImpl*>(m_impl)->getBool(name,ns);}
134         pair<bool,const char*> getString(const char* name, const char* ns=NULL) const {return static_cast<XMLConfigImpl*>(m_impl)->getString(name,ns);}
135         pair<bool,const XMLCh*> getXMLString(const char* name, const char* ns=NULL) const {return static_cast<XMLConfigImpl*>(m_impl)->getXMLString(name,ns);}
136         pair<bool,unsigned int> getUnsignedInt(const char* name, const char* ns=NULL) const {return static_cast<XMLConfigImpl*>(m_impl)->getUnsignedInt(name,ns);}
137         pair<bool,int> getInt(const char* name, const char* ns=NULL) const {return static_cast<XMLConfigImpl*>(m_impl)->getInt(name,ns);}
138         const IPropertySet* getPropertySet(const char* name, const char* ns="urn:mace:shibboleth:target:config:1.0") const {return static_cast<XMLConfigImpl*>(m_impl)->getPropertySet(name,ns);}
139         const DOMElement* getElement() const {return static_cast<XMLConfigImpl*>(m_impl)->getElement();}
140
141         // IConfig
142         const IListener* getListener() const {return m_listener;}
143         ISessionCache* getSessionCache() const {return m_sessionCache;}
144         IReplayCache* getReplayCache() const {return m_replayCache;}
145         IRequestMapper* getRequestMapper() const {return static_cast<XMLConfigImpl*>(m_impl)->m_requestMapper;}
146         const IApplication* getApplication(const char* applicationId) const {
147             map<string,IApplication*>::const_iterator i=static_cast<XMLConfigImpl*>(m_impl)->m_appmap.find(applicationId);
148             return (i!=static_cast<XMLConfigImpl*>(m_impl)->m_appmap.end()) ? i->second : NULL;
149         }
150         Iterator<ICredentials*> getCredentialsProviders() const {return static_cast<XMLConfigImpl*>(m_impl)->m_creds;}
151
152     protected:
153         virtual ReloadableXMLFileImpl* newImplementation(const char* pathname, bool first=true) const;
154         virtual ReloadableXMLFileImpl* newImplementation(const DOMElement* e, bool first=true) const;
155
156     private:
157         friend class XMLConfigImpl;
158         mutable IListener* m_listener;
159         mutable ISessionCache* m_sessionCache;
160         mutable IReplayCache* m_replayCache;
161         mutable vector<IAttributeFactory*> m_attrFactories;
162     };
163 }
164
165 IConfig* STConfig::ShibTargetConfigFactory(const DOMElement* e)
166 {
167     auto_ptr<XMLConfig> ret(new XMLConfig(e));
168     ret->getImplementation();
169     return ret.release();
170 }
171
172 XMLPropertySet::~XMLPropertySet()
173 {
174     for (map<string,pair<char*,const XMLCh*> >::iterator i=m_map.begin(); i!=m_map.end(); i++)
175         XMLString::release(&(i->second.first));
176     for (map<string,IPropertySet*>::iterator j=m_nested.begin(); j!=m_nested.end(); j++)
177         delete j->second;
178 }
179
180 void XMLPropertySet::load(
181     const DOMElement* e,
182     Category& log,
183     DOMNodeFilter* filter,
184     const std::map<std::string,std::string>* remapper
185     )
186 {
187 #ifdef _DEBUG
188     saml::NDC ndc("load");
189 #endif
190     m_root=e;
191
192     // Process each attribute as a property.
193     DOMNamedNodeMap* attrs=m_root->getAttributes();
194     for (XMLSize_t i=0; i<attrs->getLength(); i++) {
195         DOMNode* a=attrs->item(i);
196         if (!XMLString::compareString(a->getNamespaceURI(),saml::XML::XMLNS_NS))
197             continue;
198         char* val=XMLString::transcode(a->getNodeValue());
199         if (val && *val) {
200             auto_ptr_char ns(a->getNamespaceURI());
201             auto_ptr_char name(a->getLocalName());
202             const char* realname=name.get();
203             if (remapper) {
204                 map<string,string>::const_iterator remap=remapper->find(realname);
205                 if (remap!=remapper->end()) {
206                     log.debug("remapping property (%s) to (%s)",realname,remap->second.c_str());
207                     realname=remap->second.c_str();
208                 }
209             }
210             if (ns.get()) {
211                 m_map[string("{") + ns.get() + '}' + realname]=pair<char*,const XMLCh*>(val,a->getNodeValue());
212                 log.debug("added property {%s}%s (%s)",ns.get(),realname,val);
213             }
214             else {
215                 m_map[realname]=pair<char*,const XMLCh*>(val,a->getNodeValue());
216                 log.debug("added property %s (%s)",realname,val);
217             }
218         }
219     }
220     
221     // Process non-excluded elements as nested sets.
222     DOMTreeWalker* walker=
223         static_cast<DOMDocumentTraversal*>(
224             m_root->getOwnerDocument())->createTreeWalker(const_cast<DOMElement*>(m_root),DOMNodeFilter::SHOW_ELEMENT,filter,false
225             );
226     e=static_cast<DOMElement*>(walker->firstChild());
227     while (e) {
228         auto_ptr_char ns(e->getNamespaceURI());
229         auto_ptr_char name(e->getLocalName());
230         const char* realname=name.get();
231         if (remapper) {
232             map<string,string>::const_iterator remap=remapper->find(realname);
233             if (remap!=remapper->end()) {
234                 log.debug("remapping property set (%s) to (%s)",realname,remap->second.c_str());
235                 realname=remap->second.c_str();
236             }
237         }
238         string key;
239         if (ns.get())
240             key=string("{") + ns.get() + '}' + realname;
241         else
242             key=realname;
243         if (m_nested.find(key)!=m_nested.end())
244             log.warn("load() skipping duplicate property set: %s",key.c_str());
245         else {
246             XMLPropertySet* set=new XMLPropertySet();
247             set->load(e,log,filter,remapper);
248             m_nested[key]=set;
249             log.debug("added nested property set: %s",key.c_str());
250         }
251         e=static_cast<DOMElement*>(walker->nextSibling());
252     }
253     walker->release();
254 }
255
256 pair<bool,bool> XMLPropertySet::getBool(const char* name, const char* ns) const
257 {
258     pair<bool,bool> ret=pair<bool,bool>(false,false);
259     map<string,pair<char*,const XMLCh*> >::const_iterator i;
260
261     if (ns)
262         i=m_map.find(string("{") + ns + '}' + name);
263     else
264         i=m_map.find(name);
265
266     if (i!=m_map.end()) {
267         ret.first=true;
268         ret.second=(!strcmp(i->second.first,"true") || !strcmp(i->second.first,"1"));
269     }
270     return ret;
271 }
272
273 pair<bool,const char*> XMLPropertySet::getString(const char* name, const char* ns) const
274 {
275     pair<bool,const char*> ret=pair<bool,const char*>(false,NULL);
276     map<string,pair<char*,const XMLCh*> >::const_iterator i;
277
278     if (ns)
279         i=m_map.find(string("{") + ns + '}' + name);
280     else
281         i=m_map.find(name);
282
283     if (i!=m_map.end()) {
284         ret.first=true;
285         ret.second=i->second.first;
286     }
287     return ret;
288 }
289
290 pair<bool,const XMLCh*> XMLPropertySet::getXMLString(const char* name, const char* ns) const
291 {
292     pair<bool,const XMLCh*> ret=pair<bool,const XMLCh*>(false,NULL);
293     map<string,pair<char*,const XMLCh*> >::const_iterator i;
294
295     if (ns)
296         i=m_map.find(string("{") + ns + '}' + name);
297     else
298         i=m_map.find(name);
299
300     if (i!=m_map.end()) {
301         ret.first=true;
302         ret.second=i->second.second;
303     }
304     return ret;
305 }
306
307 pair<bool,unsigned int> XMLPropertySet::getUnsignedInt(const char* name, const char* ns) const
308 {
309     pair<bool,unsigned int> ret=pair<bool,unsigned int>(false,0);
310     map<string,pair<char*,const XMLCh*> >::const_iterator i;
311
312     if (ns)
313         i=m_map.find(string("{") + ns + '}' + name);
314     else
315         i=m_map.find(name);
316
317     if (i!=m_map.end()) {
318         ret.first=true;
319         ret.second=strtol(i->second.first,NULL,10);
320     }
321     return ret;
322 }
323
324 pair<bool,int> XMLPropertySet::getInt(const char* name, const char* ns) const
325 {
326     pair<bool,int> ret=pair<bool,int>(false,0);
327     map<string,pair<char*,const XMLCh*> >::const_iterator i;
328
329     if (ns)
330         i=m_map.find(string("{") + ns + '}' + name);
331     else
332         i=m_map.find(name);
333
334     if (i!=m_map.end()) {
335         ret.first=true;
336         ret.second=atoi(i->second.first);
337     }
338     return ret;
339 }
340
341 const IPropertySet* XMLPropertySet::getPropertySet(const char* name, const char* ns) const
342 {
343     map<string,IPropertySet*>::const_iterator i;
344
345     if (ns)
346         i=m_nested.find(string("{") + ns + '}' + name);
347     else
348         i=m_nested.find(name);
349
350     return (i!=m_nested.end()) ? i->second : NULL;
351 }
352
353 XMLApplication::XMLApplication(
354     const IConfig* ini,
355     const Iterator<ICredentials*>& creds,
356     const DOMElement* e,
357     const XMLApplication* base
358     ) : m_ini(ini), m_base(base), m_profile(NULL), m_binding(NULL), m_bindingHook(NULL),
359         m_acsDefault(NULL), m_sessionInitDefault(NULL), m_credDefault(NULL)
360 {
361 #ifdef _DEBUG
362     saml::NDC ndc("XMLApplication");
363 #endif
364     Category& log=Category::getInstance("shibtarget.XMLApplication");
365
366     try {
367         // First load any property sets.
368         map<string,string> root_remap;
369         root_remap["shire"]="session";
370         root_remap["shireURL"]="handlerURL";
371         root_remap["shireSSL"]="handlerSSL";
372         load(e,log,this,&root_remap);
373         const IPropertySet* propcheck=getPropertySet("Errors");
374         if (propcheck && !propcheck->getString("session").first)
375             throw ConfigurationException("<Errors> element requires 'session' (or deprecated 'shire') attribute");
376         propcheck=getPropertySet("Sessions");
377         if (propcheck && !propcheck->getString("handlerURL").first)
378             throw ConfigurationException("<Sessions> element requires 'handlerURL' (or deprecated 'shireURL') attribute");
379
380         m_hash=getId();
381         m_hash+=getString("providerId").second;
382         m_hash=SAMLArtifact::toHex(SAMLArtifactType0001::generateSourceId(m_hash.c_str()));
383
384         ShibTargetConfig& conf=ShibTargetConfig::getConfig();
385         SAMLConfig& shibConf=SAMLConfig::getConfig();
386
387         if (conf.isEnabled(ShibTargetConfig::RequestMapper)) {
388             // Process handlers.
389             bool hardACS=false, hardSessionInit=false;
390             DOMElement* handler=saml::XML::getFirstChildElement(propcheck->getElement());
391             while (handler) {
392                 XMLPropertySet* hprops=new XMLPropertySet();
393                 hprops->load(handler,log,this); // filter irrelevant for now, no embedded elements expected
394                 m_handlerMap[hprops->getString("Location").second]=hprops;
395                 
396                 // If it's an ACS or SI, handle lookup mappings and defaulting.
397                 if (saml::XML::isElementNamed(handler,shibtarget::XML::SAML2META_NS,SHIBT_L(AssertionConsumerService))) {
398                     m_acsMap[hprops->getUnsignedInt("index").second]=hprops;
399                     if (!hardACS) {
400                         pair<bool,bool> defprop=hprops->getBool("isDefault");
401                         if (defprop.first) {
402                             if (defprop.second) {
403                                 hardACS=true;
404                                 m_acsDefault=hprops;
405                             }
406                         }
407                         else if (!m_acsDefault)
408                             m_acsDefault=hprops;
409                     }
410                 }
411                 else if (saml::XML::isElementNamed(handler,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(SessionInitiator))) {
412                     pair<bool,const char*> si_id=hprops->getString("id");
413                     if (si_id.first && si_id.second)
414                         m_sessionInitMap[si_id.second]=hprops;
415                     if (!hardSessionInit) {
416                         pair<bool,bool> defprop=hprops->getBool("isDefault");
417                         if (defprop.first) {
418                             if (defprop.second) {
419                                 hardSessionInit=true;
420                                 m_sessionInitDefault=hprops;
421                             }
422                         }
423                         else if (!m_sessionInitDefault)
424                             m_sessionInitDefault=hprops;
425                     }
426                 }
427                 handler=saml::XML::getNextSiblingElement(handler);
428             }
429         }
430         
431         // Process general configuration elements.
432         XMLSize_t i;
433         DOMNodeList* nlist=e->getElementsByTagNameNS(saml::XML::SAML_NS,L(AttributeDesignator));
434         for (i=0; nlist && i<nlist->getLength(); i++)
435             if (nlist->item(i)->getParentNode()->isSameNode(e))
436                 m_designators.push_back(new SAMLAttributeDesignator(static_cast<DOMElement*>(nlist->item(i))));
437
438         nlist=e->getElementsByTagNameNS(saml::XML::SAML_NS,L(Audience));
439         for (i=0; nlist && i<nlist->getLength(); i++)
440             if (nlist->item(i)->getParentNode()->isSameNode(e))
441                 m_audiences.push_back(nlist->item(i)->getFirstChild()->getNodeValue());
442
443         // Always include our own providerId as an audience.
444         m_audiences.push_back(getXMLString("providerId").second);
445
446         if (conf.isEnabled(ShibTargetConfig::AAP)) {
447             nlist=e->getElementsByTagNameNS(shibtarget::XML::SHIBTARGET_NS,SHIBT_L(AAPProvider));
448             for (i=0; nlist && i<nlist->getLength(); i++) {
449                 if (nlist->item(i)->getParentNode()->isSameNode(e)) {
450                     auto_ptr_char type(static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,SHIBT_L(type)));
451                     log.info("building AAP provider of type %s...",type.get());
452                     try {
453                         IPlugIn* plugin=shibConf.getPlugMgr().newPlugin(type.get(),static_cast<DOMElement*>(nlist->item(i)));
454                         IAAP* aap=dynamic_cast<IAAP*>(plugin);
455                         if (aap)
456                             m_aaps.push_back(aap);
457                         else {
458                             delete plugin;
459                             log.crit("plugin was not an AAP provider");
460                         }
461                     }
462                     catch (SAMLException& ex) {
463                         log.crit("error building AAP provider: %s",ex.what());
464                     }
465                 }
466             }
467         }
468
469         if (conf.isEnabled(ShibTargetConfig::Metadata)) {
470             nlist=e->getElementsByTagNameNS(shibtarget::XML::SHIBTARGET_NS,SHIBT_L(MetadataProvider));
471             for (i=0; nlist && i<nlist->getLength(); i++) {
472                 if (nlist->item(i)->getParentNode()->isSameNode(e)) {
473                     auto_ptr_char type(static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,SHIBT_L(type)));
474                     log.info("building metadata provider of type %s...",type.get());
475                     try {
476                         IPlugIn* plugin=shibConf.getPlugMgr().newPlugin(type.get(),static_cast<DOMElement*>(nlist->item(i)));
477                         IMetadata* md=dynamic_cast<IMetadata*>(plugin);
478                         if (md)
479                             m_metadatas.push_back(md);
480                         else {
481                             delete plugin;
482                             log.crit("plugin was not a metadata provider");
483                         }
484                     }
485                     catch (SAMLException& ex) {
486                         log.crit("error building metadata provider: %s",ex.what());
487                     }
488                 }
489             }
490             nlist=e->getElementsByTagNameNS(shibtarget::XML::SHIBTARGET_NS,SHIBT_L(FederationProvider));
491             for (i=0; nlist && i<nlist->getLength(); i++) {
492                 if (nlist->item(i)->getParentNode()->isSameNode(e)) {
493                     auto_ptr_char type(static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,SHIBT_L(type)));
494                     log.info("building metadata provider of type %s...",type.get());
495                     try {
496                         IPlugIn* plugin=shibConf.getPlugMgr().newPlugin(type.get(),static_cast<DOMElement*>(nlist->item(i)));
497                         IMetadata* md=dynamic_cast<IMetadata*>(plugin);
498                         if (md)
499                             m_metadatas.push_back(md);
500                         else {
501                             delete plugin;
502                             log.crit("plugin was not a metadata provider");
503                         }
504                     }
505                     catch (SAMLException& ex) {
506                         log.crit("error building metadata provider: %s",ex.what());
507                     }
508                 }
509             }
510         }
511
512         if (conf.isEnabled(ShibTargetConfig::Trust)) {
513             nlist=e->getElementsByTagNameNS(shibtarget::XML::SHIBTARGET_NS,SHIBT_L(TrustProvider));
514             for (i=0; nlist && i<nlist->getLength(); i++) {
515                 if (nlist->item(i)->getParentNode()->isSameNode(e)) {
516                     auto_ptr_char type(static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,SHIBT_L(type)));
517                     log.info("building trust provider of type %s...",type.get());
518                     try {
519                         IPlugIn* plugin=shibConf.getPlugMgr().newPlugin(type.get(),static_cast<DOMElement*>(nlist->item(i)));
520                         ITrust* trust=dynamic_cast<ITrust*>(plugin);
521                         if (trust)
522                             m_trusts.push_back(trust);
523                         else {
524                             delete plugin;
525                             log.crit("plugin was not a trust provider");
526                         }
527                     }
528                     catch (SAMLException& ex) {
529                         log.crit("error building trust provider: %s",ex.what());
530                     }
531                 }
532             }
533         }
534         
535         // Finally, load credential mappings.
536         const DOMElement* cu=saml::XML::getFirstChildElement(e,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(CredentialUse));
537         if (cu) {
538             m_credDefault=new XMLPropertySet();
539             m_credDefault->load(cu,log,this);
540             cu=saml::XML::getFirstChildElement(cu,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(RelyingParty));
541             while (cu) {
542                 XMLPropertySet* rp=new XMLPropertySet();
543                 rp->load(cu,log,this);
544                 m_credMap[cu->getAttributeNS(NULL,SHIBT_L(Name))]=rp;
545                 cu=saml::XML::getNextSiblingElement(cu,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(RelyingParty));
546             }
547         }
548         
549         if (conf.isEnabled(ShibTargetConfig::Caching)) {
550             // Really finally, build local browser profile and binding objects.
551             m_profile=new ShibBrowserProfile(
552                 getMetadataProviders(),
553                 getTrustProviders()
554                 );
555             m_bindingHook=new ShibHTTPHook(
556                 getTrustProviders(),
557                 creds
558                 );
559             m_binding=SAMLBinding::getInstance(SAMLBinding::SOAP);
560             SAMLSOAPHTTPBinding* bptr=dynamic_cast<SAMLSOAPHTTPBinding*>(m_binding);
561             if (!bptr) {
562                 log.fatal("binding implementation was not SOAP over HTTP");
563                 throw UnsupportedExtensionException("binding implementation was not SOAP over HTTP");
564             }
565             bptr->addHook(m_bindingHook,m_bindingHook); // the hook is its own global context
566         }
567     }
568     catch (SAMLException& e) {
569         log.errorStream() << "Error while processing applicaton element: " << e.what() << CategoryStream::ENDLINE;
570         cleanup();
571         throw;
572     }
573 #ifndef _DEBUG
574     catch (...) {
575         log.error("Unexpected error while processing application element");
576         cleanup();
577         throw;
578     }
579 #endif
580 }
581
582 void XMLApplication::cleanup()
583 {
584     delete m_bindingHook;
585     delete m_binding;
586     delete m_profile;
587     map<string,XMLPropertySet*>::iterator h=m_handlerMap.begin();
588     while (h!=m_handlerMap.end()) {
589         delete h->second;
590         h++;
591     }
592     delete m_credDefault;
593 #ifdef HAVE_GOOD_STL
594     map<xstring,XMLPropertySet*>::iterator c=m_credMap.begin();
595 #else
596     map<const XMLCh*,XMLPropertySet*>::iterator c=m_credMap.begin();
597 #endif
598     while (c!=m_credMap.end()) {
599         delete c->second;
600         c++;
601     }
602     Iterator<SAMLAttributeDesignator*> i(m_designators);
603     while (i.hasNext())
604         delete i.next();
605     Iterator<IAAP*> j(m_aaps);
606     while (j.hasNext())
607         delete j.next();
608     Iterator<IMetadata*> k(m_metadatas);
609     while (k.hasNext())
610         delete k.next();
611     Iterator<ITrust*> l(m_trusts);
612     while (l.hasNext())
613         delete l.next();
614 }
615
616 short XMLApplication::acceptNode(const DOMNode* node) const
617 {
618     if (saml::XML::isElementNamed(static_cast<const DOMElement*>(node),saml::XML::SAML_NS,L(AttributeDesignator)))
619         return FILTER_REJECT;
620     else if (saml::XML::isElementNamed(static_cast<const DOMElement*>(node),saml::XML::SAML_NS,L(Audience)))
621         return FILTER_REJECT;
622     const XMLCh* name=node->getLocalName();
623     if (!XMLString::compareString(name,SHIBT_L(Application)) ||
624         !XMLString::compareString(name,SHIBT_L(AssertionConsumerService)) ||
625         !XMLString::compareString(name,SHIBT_L(SingleLogoutService)) ||
626         !XMLString::compareString(name,SHIBT_L(DiagnosticService)) ||
627         !XMLString::compareString(name,SHIBT_L(SessionInitiator)) ||
628         !XMLString::compareString(name,SHIBT_L(AAPProvider)) ||
629         !XMLString::compareString(name,SHIBT_L(CredentialUse)) ||
630         !XMLString::compareString(name,SHIBT_L(RelyingParty)) ||
631         !XMLString::compareString(name,SHIBT_L(FederationProvider)) ||
632         !XMLString::compareString(name,SHIBT_L(MetadataProvider)) ||
633         !XMLString::compareString(name,SHIBT_L(TrustProvider)))
634         return FILTER_REJECT;
635
636     return FILTER_ACCEPT;
637 }
638
639 pair<bool,bool> XMLApplication::getBool(const char* name, const char* ns) const
640 {
641     pair<bool,bool> ret=XMLPropertySet::getBool(name,ns);
642     if (ret.first)
643         return ret;
644     return m_base ? m_base->getBool(name,ns) : ret;
645 }
646
647 pair<bool,const char*> XMLApplication::getString(const char* name, const char* ns) const
648 {
649     pair<bool,const char*> ret=XMLPropertySet::getString(name,ns);
650     if (ret.first)
651         return ret;
652     return m_base ? m_base->getString(name,ns) : ret;
653 }
654
655 pair<bool,const XMLCh*> XMLApplication::getXMLString(const char* name, const char* ns) const
656 {
657     pair<bool,const XMLCh*> ret=XMLPropertySet::getXMLString(name,ns);
658     if (ret.first)
659         return ret;
660     return m_base ? m_base->getXMLString(name,ns) : ret;
661 }
662
663 pair<bool,unsigned int> XMLApplication::getUnsignedInt(const char* name, const char* ns) const
664 {
665     pair<bool,unsigned int> ret=XMLPropertySet::getUnsignedInt(name,ns);
666     if (ret.first)
667         return ret;
668     return m_base ? m_base->getUnsignedInt(name,ns) : ret;
669 }
670
671 pair<bool,int> XMLApplication::getInt(const char* name, const char* ns) const
672 {
673     pair<bool,int> ret=XMLPropertySet::getInt(name,ns);
674     if (ret.first)
675         return ret;
676     return m_base ? m_base->getInt(name,ns) : ret;
677 }
678
679 const IPropertySet* XMLApplication::getPropertySet(const char* name, const char* ns) const
680 {
681     const IPropertySet* ret=XMLPropertySet::getPropertySet(name,ns);
682     if (ret || !m_base)
683         return ret;
684     return m_base->getPropertySet(name,ns);
685 }
686
687 Iterator<SAMLAttributeDesignator*> XMLApplication::getAttributeDesignators() const
688 {
689     if (!m_designators.empty() || !m_base)
690         return m_designators;
691     return m_base->getAttributeDesignators();
692 }
693
694 Iterator<IAAP*> XMLApplication::getAAPProviders() const
695 {
696     return (m_aaps.empty() && m_base) ? m_base->getAAPProviders() : m_aaps;
697 }
698
699 Iterator<IMetadata*> XMLApplication::getMetadataProviders() const
700 {
701     return (m_metadatas.empty() && m_base) ? m_base->getMetadataProviders() : m_metadatas;
702 }
703
704 Iterator<ITrust*> XMLApplication::getTrustProviders() const
705 {
706     return (m_trusts.empty() && m_base) ? m_base->getTrustProviders() : m_trusts;
707 }
708
709 Iterator<const XMLCh*> XMLApplication::getAudiences() const
710 {
711     return (m_audiences.empty() && m_base) ? m_base->getAudiences() : m_audiences;
712 }
713
714 const IPropertySet* XMLApplication::getCredentialUse(const IEntityDescriptor* provider) const
715 {
716     if (!m_credDefault && m_base)
717         return m_base->getCredentialUse(provider);
718         
719 #ifdef HAVE_GOOD_STL
720     map<xstring,XMLPropertySet*>::const_iterator i=m_credMap.find(provider->getId());
721     if (i!=m_credMap.end())
722         return i->second;
723     const IEntitiesDescriptor* group=provider->getEntitiesDescriptor();
724     while (group) {
725         if (group->getName()) {
726             i=m_credMap.find(group->getName());
727             if (i!=m_credMap.end())
728                 return i->second;
729         }
730         group=group->getEntitiesDescriptor();
731     }
732 #else
733     map<const XMLCh*,XMLPropertySet*>::const_iterator i=m_credMap.begin();
734     for (; i!=m_credMap.end(); i++) {
735         if (!XMLString::compareString(i->first,provider->getId()))
736             return i->second;
737         const IEntitiesDescriptor* group=provider->getEntitiesDescriptor();
738         while (group) {
739             if (!XMLString::compareString(i->first,group->getName()))
740                 return i->second;
741             group=group->getEntitiesDescriptor();
742         }
743     }
744 #endif
745     return m_credDefault;
746 }
747
748 const IPropertySet* XMLApplication::getDefaultSessionInitiator() const
749 {
750     if (m_sessionInitDefault) return m_sessionInitDefault;
751     return m_base ? m_base->getDefaultSessionInitiator() : NULL;
752 }
753
754 const IPropertySet* XMLApplication::getSessionInitiatorById(const char* id) const
755 {
756     map<string,const IPropertySet*>::const_iterator i=m_sessionInitMap.find(id);
757     if (i!=m_sessionInitMap.end()) return i->second;
758     return m_base ? m_base->getSessionInitiatorById(id) : NULL;
759 }
760
761 const IPropertySet* XMLApplication::getDefaultAssertionConsumerService() const
762 {
763     if (m_acsDefault) return m_acsDefault;
764     return m_base ? m_base->getDefaultAssertionConsumerService() : NULL;
765 }
766
767 const IPropertySet* XMLApplication::getAssertionConsumerServiceByIndex(unsigned short index) const
768 {
769     map<unsigned int,const IPropertySet*>::const_iterator i=m_acsMap.find(index);
770     if (i!=m_acsMap.end()) return i->second;
771     return m_base ? m_base->getAssertionConsumerServiceByIndex(index) : NULL;
772 }
773
774 const IPropertySet* XMLApplication::getHandlerConfig(const char* path) const
775 {
776     string wrap(path);
777     map<string,XMLPropertySet*>::const_iterator i=m_handlerMap.find(wrap.substr(0,wrap.find('?')));
778     if (i!=m_handlerMap.end())
779         return i->second;
780     return m_base ? m_base->getHandlerConfig(path) : NULL;
781 }
782
783 ReloadableXMLFileImpl* XMLConfig::newImplementation(const char* pathname, bool first) const
784 {
785     return new XMLConfigImpl(pathname,first,this);
786 }
787
788 ReloadableXMLFileImpl* XMLConfig::newImplementation(const DOMElement* e, bool first) const
789 {
790     return new XMLConfigImpl(e,first,this);
791 }
792
793 short XMLConfigImpl::acceptNode(const DOMNode* node) const
794 {
795     if (XMLString::compareString(node->getNamespaceURI(),shibtarget::XML::SHIBTARGET_NS))
796         return FILTER_ACCEPT;
797     const XMLCh* name=node->getLocalName();
798     if (!XMLString::compareString(name,SHIBT_L(Applications)) ||
799         !XMLString::compareString(name,SHIBT_L(AttributeFactory)) ||
800         !XMLString::compareString(name,SHIBT_L(CredentialsProvider)) ||
801         !XMLString::compareString(name,SHIBT_L(Extensions)) ||
802         !XMLString::compareString(name,SHIBT_L(Implementation)) ||
803         !XMLString::compareString(name,SHIBT_L(Listener)) ||
804         !XMLString::compareString(name,SHIBT_L(MemorySessionCache)) ||
805         !XMLString::compareString(name,SHIBT_L(MySQLReplayCache)) ||
806         !XMLString::compareString(name,SHIBT_L(MySQLSessionCache)) ||
807         !XMLString::compareString(name,SHIBT_L(RequestMap)) ||
808         !XMLString::compareString(name,SHIBT_L(RequestMapProvider)) ||
809         !XMLString::compareString(name,SHIBT_L(ReplayCache)) ||
810         !XMLString::compareString(name,SHIBT_L(SessionCache)) ||
811         !XMLString::compareString(name,SHIBT_L(TCPListener)) ||
812         !XMLString::compareString(name,SHIBT_L(UnixListener)))
813         return FILTER_REJECT;
814
815     return FILTER_ACCEPT;
816 }
817
818 void XMLConfigImpl::init(bool first)
819 {
820 #ifdef _DEBUG
821     saml::NDC ndc("init");
822 #endif
823     Category& log=Category::getInstance("shibtarget.Config");
824
825     try {
826         if (!saml::XML::isElementNamed(ReloadableXMLFileImpl::m_root,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(ShibbolethTargetConfig)) &&
827             !saml::XML::isElementNamed(ReloadableXMLFileImpl::m_root,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(SPConfig))) {
828             log.error("Construction requires a valid configuration file: (conf:SPConfig as root element)");
829             throw ConfigurationException("Construction requires a valid configuration file: (conf:SPConfig as root element)");
830         }
831
832         SAMLConfig& shibConf=SAMLConfig::getConfig();
833         ShibTargetConfig& conf=ShibTargetConfig::getConfig();
834         const DOMElement* SHAR=saml::XML::getFirstChildElement(ReloadableXMLFileImpl::m_root,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(SHAR));
835         if (!SHAR)
836             SHAR=saml::XML::getFirstChildElement(ReloadableXMLFileImpl::m_root,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Global));
837         const DOMElement* SHIRE=saml::XML::getFirstChildElement(ReloadableXMLFileImpl::m_root,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(SHIRE));
838         if (!SHIRE)
839             SHIRE=saml::XML::getFirstChildElement(ReloadableXMLFileImpl::m_root,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Local));
840
841         // Initialize log4cpp manually in order to redirect log messages as soon as possible.
842         if (conf.isEnabled(ShibTargetConfig::Logging)) {
843             const XMLCh* logger=NULL;
844             if (conf.isEnabled(ShibTargetConfig::GlobalExtensions))
845                 logger=SHAR->getAttributeNS(NULL,SHIBT_L(logger));
846             else if (conf.isEnabled(ShibTargetConfig::LocalExtensions))
847                 logger=SHIRE->getAttributeNS(NULL,SHIBT_L(logger));
848             if (!logger || !*logger)
849                 logger=ReloadableXMLFileImpl::m_root->getAttributeNS(NULL,SHIBT_L(logger));
850             if (logger && *logger) {
851                 auto_ptr_char logpath(logger);
852                 log.debug("loading new logging configuration from (%s), check log destination for status of configuration",logpath.get());
853                 try {
854                     PropertyConfigurator::configure(logpath.get());
855                 }
856                 catch (ConfigureFailure& e) {
857                     log.error("Error reading logging configuration: %s",e.what());
858                 }
859             }
860         }
861         
862         // First load any property sets.
863         map<string,string> root_remap;
864         root_remap["SHAR"]="Global";
865         root_remap["SHIRE"]="Local";
866         load(ReloadableXMLFileImpl::m_root,log,this,&root_remap);
867
868         // Much of the processing can only occur on the first instantiation.
869         if (first) {
870             // Now load any extensions to insure any needed plugins are registered.
871             DOMElement* exts=
872                 saml::XML::getFirstChildElement(ReloadableXMLFileImpl::m_root,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Extensions));
873             if (exts) {
874                 exts=saml::XML::getFirstChildElement(exts,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Library));
875                 while (exts) {
876                     auto_ptr_char path(exts->getAttributeNS(NULL,SHIBT_L(path)));
877                     try {
878                         SAMLConfig::getConfig().saml_register_extension(path.get(),exts);
879                         log.debug("loaded global extension library %s",path.get());
880                     }
881                     catch (SAMLException& e) {
882                         const XMLCh* fatal=exts->getAttributeNS(NULL,SHIBT_L(fatal));
883                         if (fatal && (*fatal==chLatin_t || *fatal==chDigit_1)) {
884                             log.fatal("unable to load mandatory global extension library %s: %s", path.get(), e.what());
885                             throw;
886                         }
887                         else
888                             log.crit("unable to load optional global extension library %s: %s", path.get(), e.what());
889                     }
890                     exts=saml::XML::getNextSiblingElement(exts,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Library));
891                 }
892             }
893             
894             if (conf.isEnabled(ShibTargetConfig::GlobalExtensions)) {
895                 exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Extensions));
896                 if (exts) {
897                     exts=saml::XML::getFirstChildElement(exts,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Library));
898                     while (exts) {
899                         auto_ptr_char path(exts->getAttributeNS(NULL,SHIBT_L(path)));
900                         try {
901                             SAMLConfig::getConfig().saml_register_extension(path.get(),exts);
902                             log.debug("loaded Global extension library %s",path.get());
903                         }
904                         catch (SAMLException& e) {
905                             const XMLCh* fatal=exts->getAttributeNS(NULL,SHIBT_L(fatal));
906                             if (fatal && (*fatal==chLatin_t || *fatal==chDigit_1)) {
907                                 log.fatal("unable to load mandatory Global extension library %s: %s", path.get(), e.what());
908                                 throw;
909                             }
910                             else
911                                 log.crit("unable to load optional Global extension library %s: %s", path.get(), e.what());
912                         }
913                         exts=saml::XML::getNextSiblingElement(exts,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Library));
914                     }
915                 }
916             }
917
918             if (conf.isEnabled(ShibTargetConfig::LocalExtensions)) {
919                 exts=saml::XML::getFirstChildElement(SHIRE,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Extensions));
920                 if (exts) {
921                     exts=saml::XML::getFirstChildElement(exts,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Library));
922                     while (exts) {
923                         auto_ptr_char path(exts->getAttributeNS(NULL,SHIBT_L(path)));
924                         try {
925                             SAMLConfig::getConfig().saml_register_extension(path.get(),exts);
926                             log.debug("loaded Local extension library %s",path.get());
927                         }
928                         catch (SAMLException& e) {
929                             const XMLCh* fatal=exts->getAttributeNS(NULL,SHIBT_L(fatal));
930                             if (fatal && (*fatal==chLatin_t || *fatal==chDigit_1)) {
931                                 log.fatal("unable to load mandatory Local extension library %s: %s", path.get(), e.what());
932                                 throw;
933                             }
934                             else
935                                 log.crit("unable to load optional Local extension library %s: %s", path.get(), e.what());
936                         }
937                         exts=saml::XML::getNextSiblingElement(exts,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Library));
938                     }
939                 }
940             }
941             
942             // Instantiate the Listener and SessionCache objects.
943             if (conf.isEnabled(ShibTargetConfig::Listener)) {
944                 IPlugIn* plugin=NULL;
945                 exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(UnixListener));
946                 if (exts) {
947                     log.info("building Listener of type %s...",shibtarget::XML::UnixListenerType);
948                     plugin=shibConf.getPlugMgr().newPlugin(shibtarget::XML::UnixListenerType,exts);
949                 }
950                 else {
951                     exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(TCPListener));
952                     if (exts) {
953                         log.info("building Listener of type %s...",shibtarget::XML::TCPListenerType);
954                         plugin=shibConf.getPlugMgr().newPlugin(shibtarget::XML::TCPListenerType,exts);
955                     }
956                     else {
957                         exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Listener));
958                         if (exts) {
959                             auto_ptr_char type(exts->getAttributeNS(NULL,SHIBT_L(type)));
960                             log.info("building Listener of type %s...",type.get());
961                             plugin=shibConf.getPlugMgr().newPlugin(type.get(),exts);
962                         }
963                         else {
964                             log.fatal("can't build Listener object, missing conf:Listener element?");
965                             throw ConfigurationException("can't build Listener object, missing conf:Listener element?");
966                         }
967                     }
968                 }
969                 if (plugin) {
970                     IListener* listen=dynamic_cast<IListener*>(plugin);
971                     if (listen)
972                         m_outer->m_listener=listen;
973                     else {
974                         delete plugin;
975                         log.fatal("plugin was not a Listener object");
976                         throw UnsupportedExtensionException("plugin was not a Listener object");
977                     }
978                 }
979             }
980
981             if (conf.isEnabled(ShibTargetConfig::Caching)) {
982                 IPlugIn* plugin=NULL;
983                 exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(MemorySessionCache));
984                 if (exts) {
985                     log.info("building Session Cache of type %s...",shibtarget::XML::MemorySessionCacheType);
986                     plugin=shibConf.getPlugMgr().newPlugin(shibtarget::XML::MemorySessionCacheType,exts);
987                 }
988                 else {
989                     exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(MySQLSessionCache));
990                     if (exts) {
991                         log.info("building Session Cache of type %s...",shibtarget::XML::MySQLSessionCacheType);
992                         plugin=shibConf.getPlugMgr().newPlugin(shibtarget::XML::MySQLSessionCacheType,exts);
993                     }
994                     else {
995                         exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(SessionCache));
996                         if (exts) {
997                             auto_ptr_char type(exts->getAttributeNS(NULL,SHIBT_L(type)));
998                             log.info("building Session Cache of type %s...",type.get());
999                             plugin=shibConf.getPlugMgr().newPlugin(type.get(),exts);
1000                         }
1001                         else {
1002                             log.fatal("can't build Session Cache object, missing conf:SessionCache element?");
1003                             throw ConfigurationException("can't build Session Cache object, missing conf:SessionCache element?");
1004                         }
1005                     }
1006                 }
1007                 if (plugin) {
1008                     ISessionCache* cache=dynamic_cast<ISessionCache*>(plugin);
1009                     if (cache)
1010                         m_outer->m_sessionCache=cache;
1011                     else {
1012                         delete plugin;
1013                         log.fatal("plugin was not a Session Cache object");
1014                         throw UnsupportedExtensionException("plugin was not a Session Cache object");
1015                     }
1016                 }
1017                 
1018                 // Replay cache.
1019                 exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(MySQLReplayCache));
1020                 if (exts) {
1021                     log.info("building Replay Cache of type %s...",shibtarget::XML::MySQLReplayCacheType);
1022                     m_outer->m_replayCache=IReplayCache::getInstance(shibtarget::XML::MySQLReplayCacheType,exts);
1023                 }
1024                 else {
1025                     exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(ReplayCache));
1026                     if (exts) {
1027                         auto_ptr_char type(exts->getAttributeNS(NULL,SHIBT_L(type)));
1028                         log.info("building Replay Cache of type %s...",type.get());
1029                         m_outer->m_replayCache=IReplayCache::getInstance(type.get(),exts);
1030                     }
1031                     else {
1032                         // OpenSAML default provider.
1033                         log.info("building default Replay Cache...");
1034                         m_outer->m_replayCache=IReplayCache::getInstance();
1035                     }
1036                 }
1037             }
1038
1039             // Now we load any attribute factories.
1040             DOMNodeList* nlist=ReloadableXMLFileImpl::m_root->getElementsByTagNameNS(shibtarget::XML::SHIBTARGET_NS,SHIBT_L(AttributeFactory));
1041             for (XMLSize_t i=0; nlist && i<nlist->getLength(); i++) {
1042                 auto_ptr_char type(static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,SHIBT_L(type)));
1043                 log.info("building Attribute factory of type %s...",type.get());
1044                 try {
1045                     IPlugIn* plugin=shibConf.getPlugMgr().newPlugin(type.get(),static_cast<DOMElement*>(nlist->item(i)));
1046                     if (plugin) {
1047                         IAttributeFactory* fact=dynamic_cast<IAttributeFactory*>(plugin);
1048                         if (fact) {
1049                             m_outer->m_attrFactories.push_back(fact);
1050                             ShibConfig::getConfig().regAttributeMapping(
1051                                 static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,L(AttributeName)),
1052                                 fact
1053                                 );
1054                         }
1055                         else {
1056                             delete plugin;
1057                             log.crit("plugin was not an Attribute factory");
1058                         }
1059                     }
1060                 }
1061                 catch (SAMLException& ex) {
1062                     log.crit("error building Attribute factory: %s",ex.what());
1063                 }
1064             }
1065         }
1066         
1067         // Back to the fully dynamic stuff...next up is the Request Mapper.
1068         if (conf.isEnabled(ShibTargetConfig::RequestMapper)) {
1069             const DOMElement* child=saml::XML::getFirstChildElement(SHIRE,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(RequestMapProvider));
1070             if (child) {
1071                 auto_ptr_char type(child->getAttributeNS(NULL,SHIBT_L(type)));
1072                 log.info("building Request Mapper of type %s...",type.get());
1073                 IPlugIn* plugin=shibConf.getPlugMgr().newPlugin(type.get(),child);
1074                 if (plugin) {
1075                     IRequestMapper* reqmap=dynamic_cast<IRequestMapper*>(plugin);
1076                     if (reqmap)
1077                         m_requestMapper=reqmap;
1078                     else {
1079                         delete plugin;
1080                         log.fatal("plugin was not a Request Mapper object");
1081                         throw UnsupportedExtensionException("plugin was not a Request Mapper object");
1082                     }
1083                 }
1084             }
1085             else {
1086                 log.fatal("can't build Request Mapper object, missing conf:RequestMapProvider element?");
1087                 throw ConfigurationException("can't build Request Mapper object, missing conf:RequestMapProvider element?");
1088             }
1089         }
1090         
1091         // Now we load any credentials providers.
1092         DOMNodeList* nlist;
1093         if (conf.isEnabled(ShibTargetConfig::Credentials)) {
1094             nlist=ReloadableXMLFileImpl::m_root->getElementsByTagNameNS(shibtarget::XML::SHIBTARGET_NS,SHIBT_L(CredentialsProvider));
1095             for (XMLSize_t i=0; nlist && i<nlist->getLength(); i++) {
1096                 auto_ptr_char type(static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,SHIBT_L(type)));
1097                 log.info("building credentials provider of type %s...",type.get());
1098                 try {
1099                     IPlugIn* plugin=shibConf.getPlugMgr().newPlugin(type.get(),static_cast<DOMElement*>(nlist->item(i)));
1100                     if (plugin) {
1101                         ICredentials* creds=dynamic_cast<ICredentials*>(plugin);
1102                         if (creds)
1103                             m_creds.push_back(creds);
1104                         else {
1105                             delete plugin;
1106                             log.crit("plugin was not a credentials provider");
1107                         }
1108                     }
1109                 }
1110                 catch (SAMLException& ex) {
1111                     log.crit("error building credentials provider: %s",ex.what());
1112                 }
1113             }
1114         }
1115
1116         // Load the default application. This actually has a fixed ID of "default". ;-)
1117         const DOMElement* app=saml::XML::getFirstChildElement(
1118             ReloadableXMLFileImpl::m_root,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Applications)
1119             );
1120         if (!app) {
1121             log.fatal("can't build default Application object, missing conf:Applications element?");
1122             throw ConfigurationException("can't build default Application object, missing conf:Applications element?");
1123         }
1124         XMLApplication* defapp=new XMLApplication(m_outer, m_creds, app);
1125         m_appmap[defapp->getId()]=defapp;
1126         
1127         // Load any overrides.
1128         nlist=app->getElementsByTagNameNS(shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Application));
1129         for (XMLSize_t j=0; nlist && j<nlist->getLength(); j++) {
1130             XMLApplication* iapp=new XMLApplication(m_outer,m_creds,static_cast<DOMElement*>(nlist->item(j)),defapp);
1131             if (m_appmap.find(iapp->getId())!=m_appmap.end()) {
1132                 log.fatal("found conf:Application element with duplicate Id attribute");
1133                 throw ConfigurationException("found conf:Application element with duplicate Id attribute");
1134             }
1135             m_appmap[iapp->getId()]=iapp;
1136         }
1137     }
1138     catch (SAMLException& e) {
1139         log.errorStream() << "Error while loading SP configuration: " << e.what() << CategoryStream::ENDLINE;
1140         throw;
1141     }
1142 #ifndef _DEBUG
1143     catch (...) {
1144         log.error("Unexpected error while loading SP configuration");
1145         throw;
1146     }
1147 #endif
1148 }
1149
1150 XMLConfigImpl::~XMLConfigImpl()
1151 {
1152     delete m_requestMapper;
1153     for (map<string,IApplication*>::iterator i=m_appmap.begin(); i!=m_appmap.end(); i++)
1154         delete i->second;
1155     for (vector<ICredentials*>::iterator j=m_creds.begin(); j!=m_creds.end(); j++)
1156         delete (*j);
1157 }
1158
1159 XMLConfig::~XMLConfig()
1160 {
1161     delete m_listener;
1162     delete m_sessionCache;
1163     delete m_replayCache;
1164     ShibConfig::getConfig().clearAttributeMappings();
1165     for (vector<IAttributeFactory*>::iterator k=m_attrFactories.begin(); k!=m_attrFactories.end(); k++)
1166         delete (*k);
1167 }