2 * Copyright 2001-2005 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * shib-ini.h -- config file handling, now XML-based
25 #include <shib/shib-threads.h>
26 #include <log4cpp/Category.hh>
27 #include <log4cpp/PropertyConfigurator.hh>
29 #include <sys/types.h>
34 using namespace shibboleth;
35 using namespace shibtarget;
36 using namespace log4cpp;
38 namespace shibtarget {
40 // Application configuration wrapper
41 class XMLApplication : public virtual IApplication, public XMLPropertySet, public DOMNodeFilter
44 XMLApplication(const IConfig*, const Iterator<ICredentials*>& creds, const DOMElement* e, const XMLApplication* base=NULL);
45 ~XMLApplication() { cleanup(); }
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;
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;
74 // Provides filter to exclude special config elements.
75 short acceptNode(const DOMNode* node) const;
79 const IConfig* m_ini; // this is ok because its locking scope includes us
80 const XMLApplication* m_base;
82 vector<SAMLAttributeDesignator*> m_designators;
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;
97 map<xstring,XMLPropertySet*> m_credMap;
99 map<const XMLCh*,XMLPropertySet*> m_credMap;
103 // Top-level configuration implementation
105 class XMLConfigImpl : public ReloadableXMLFileImpl, public XMLPropertySet, public DOMNodeFilter
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); }
114 IRequestMapper* m_requestMapper;
115 map<string,IApplication*> m_appmap;
116 vector<ICredentials*> m_creds;
118 // Provides filter to exclude special config elements.
119 short acceptNode(const DOMNode* node) const;
122 void init(bool first);
123 const XMLConfig* m_outer;
126 class XMLConfig : public IConfig, public ReloadableXMLFile
129 XMLConfig(const DOMElement* e) : ReloadableXMLFile(e), m_listener(NULL), m_sessionCache(NULL), m_replayCache(NULL) {}
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();}
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;
150 Iterator<ICredentials*> getCredentialsProviders() const {return static_cast<XMLConfigImpl*>(m_impl)->m_creds;}
153 virtual ReloadableXMLFileImpl* newImplementation(const char* pathname, bool first=true) const;
154 virtual ReloadableXMLFileImpl* newImplementation(const DOMElement* e, bool first=true) const;
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;
165 IConfig* STConfig::ShibTargetConfigFactory(const DOMElement* e)
167 auto_ptr<XMLConfig> ret(new XMLConfig(e));
168 ret->getImplementation();
169 return ret.release();
172 XMLPropertySet::~XMLPropertySet()
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++)
180 void XMLPropertySet::load(
183 DOMNodeFilter* filter,
184 const std::map<std::string,std::string>* remapper
188 saml::NDC ndc("load");
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))
198 char* val=XMLString::transcode(a->getNodeValue());
200 auto_ptr_char ns(a->getNamespaceURI());
201 auto_ptr_char name(a->getLocalName());
202 const char* realname=name.get();
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();
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);
215 m_map[realname]=pair<char*,const XMLCh*>(val,a->getNodeValue());
216 log.debug("added property %s (%s)",realname,val);
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
226 e=static_cast<DOMElement*>(walker->firstChild());
228 auto_ptr_char ns(e->getNamespaceURI());
229 auto_ptr_char name(e->getLocalName());
230 const char* realname=name.get();
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();
240 key=string("{") + ns.get() + '}' + realname;
243 if (m_nested.find(key)!=m_nested.end())
244 log.warn("load() skipping duplicate property set: %s",key.c_str());
246 XMLPropertySet* set=new XMLPropertySet();
247 set->load(e,log,filter,remapper);
249 log.debug("added nested property set: %s",key.c_str());
251 e=static_cast<DOMElement*>(walker->nextSibling());
256 pair<bool,bool> XMLPropertySet::getBool(const char* name, const char* ns) const
258 pair<bool,bool> ret=pair<bool,bool>(false,false);
259 map<string,pair<char*,const XMLCh*> >::const_iterator i;
262 i=m_map.find(string("{") + ns + '}' + name);
266 if (i!=m_map.end()) {
268 ret.second=(!strcmp(i->second.first,"true") || !strcmp(i->second.first,"1"));
273 pair<bool,const char*> XMLPropertySet::getString(const char* name, const char* ns) const
275 pair<bool,const char*> ret=pair<bool,const char*>(false,NULL);
276 map<string,pair<char*,const XMLCh*> >::const_iterator i;
279 i=m_map.find(string("{") + ns + '}' + name);
283 if (i!=m_map.end()) {
285 ret.second=i->second.first;
290 pair<bool,const XMLCh*> XMLPropertySet::getXMLString(const char* name, const char* ns) const
292 pair<bool,const XMLCh*> ret=pair<bool,const XMLCh*>(false,NULL);
293 map<string,pair<char*,const XMLCh*> >::const_iterator i;
296 i=m_map.find(string("{") + ns + '}' + name);
300 if (i!=m_map.end()) {
302 ret.second=i->second.second;
307 pair<bool,unsigned int> XMLPropertySet::getUnsignedInt(const char* name, const char* ns) const
309 pair<bool,unsigned int> ret=pair<bool,unsigned int>(false,0);
310 map<string,pair<char*,const XMLCh*> >::const_iterator i;
313 i=m_map.find(string("{") + ns + '}' + name);
317 if (i!=m_map.end()) {
319 ret.second=strtol(i->second.first,NULL,10);
324 pair<bool,int> XMLPropertySet::getInt(const char* name, const char* ns) const
326 pair<bool,int> ret=pair<bool,int>(false,0);
327 map<string,pair<char*,const XMLCh*> >::const_iterator i;
330 i=m_map.find(string("{") + ns + '}' + name);
334 if (i!=m_map.end()) {
336 ret.second=atoi(i->second.first);
341 const IPropertySet* XMLPropertySet::getPropertySet(const char* name, const char* ns) const
343 map<string,IPropertySet*>::const_iterator i;
346 i=m_nested.find(string("{") + ns + '}' + name);
348 i=m_nested.find(name);
350 return (i!=m_nested.end()) ? i->second : NULL;
353 XMLApplication::XMLApplication(
355 const Iterator<ICredentials*>& creds,
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)
362 saml::NDC ndc("XMLApplication");
364 Category& log=Category::getInstance("shibtarget.XMLApplication");
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");
381 m_hash+=getString("providerId").second;
382 m_hash=SAMLArtifact::toHex(SAMLArtifactType0001::generateSourceId(m_hash.c_str()));
384 ShibTargetConfig& conf=ShibTargetConfig::getConfig();
385 SAMLConfig& shibConf=SAMLConfig::getConfig();
387 if (conf.isEnabled(ShibTargetConfig::RequestMapper)) {
389 bool hardACS=false, hardSessionInit=false;
390 DOMElement* handler=saml::XML::getFirstChildElement(propcheck->getElement());
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;
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;
400 pair<bool,bool> defprop=hprops->getBool("isDefault");
402 if (defprop.second) {
407 else if (!m_acsDefault)
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");
418 if (defprop.second) {
419 hardSessionInit=true;
420 m_sessionInitDefault=hprops;
423 else if (!m_sessionInitDefault)
424 m_sessionInitDefault=hprops;
427 handler=saml::XML::getNextSiblingElement(handler);
431 // Process general configuration elements.
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))));
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());
443 // Always include our own providerId as an audience.
444 m_audiences.push_back(getXMLString("providerId").second);
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());
453 IPlugIn* plugin=shibConf.getPlugMgr().newPlugin(type.get(),static_cast<DOMElement*>(nlist->item(i)));
454 IAAP* aap=dynamic_cast<IAAP*>(plugin);
456 m_aaps.push_back(aap);
459 log.crit("plugin was not an AAP provider");
462 catch (SAMLException& ex) {
463 log.crit("error building AAP provider: %s",ex.what());
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());
476 IPlugIn* plugin=shibConf.getPlugMgr().newPlugin(type.get(),static_cast<DOMElement*>(nlist->item(i)));
477 IMetadata* md=dynamic_cast<IMetadata*>(plugin);
479 m_metadatas.push_back(md);
482 log.crit("plugin was not a metadata provider");
485 catch (SAMLException& ex) {
486 log.crit("error building metadata provider: %s",ex.what());
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());
496 IPlugIn* plugin=shibConf.getPlugMgr().newPlugin(type.get(),static_cast<DOMElement*>(nlist->item(i)));
497 IMetadata* md=dynamic_cast<IMetadata*>(plugin);
499 m_metadatas.push_back(md);
502 log.crit("plugin was not a metadata provider");
505 catch (SAMLException& ex) {
506 log.crit("error building metadata provider: %s",ex.what());
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());
519 IPlugIn* plugin=shibConf.getPlugMgr().newPlugin(type.get(),static_cast<DOMElement*>(nlist->item(i)));
520 ITrust* trust=dynamic_cast<ITrust*>(plugin);
522 m_trusts.push_back(trust);
525 log.crit("plugin was not a trust provider");
528 catch (SAMLException& ex) {
529 log.crit("error building trust provider: %s",ex.what());
535 // Finally, load credential mappings.
536 const DOMElement* cu=saml::XML::getFirstChildElement(e,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(CredentialUse));
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));
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));
549 if (conf.isEnabled(ShibTargetConfig::Caching)) {
550 // Really finally, build local browser profile and binding objects.
551 m_profile=new ShibBrowserProfile(
552 getMetadataProviders(),
555 m_bindingHook=new ShibHTTPHook(
559 m_binding=SAMLBinding::getInstance(SAMLBinding::SOAP);
560 SAMLSOAPHTTPBinding* bptr=dynamic_cast<SAMLSOAPHTTPBinding*>(m_binding);
562 log.fatal("binding implementation was not SOAP over HTTP");
563 throw UnsupportedExtensionException("binding implementation was not SOAP over HTTP");
565 bptr->addHook(m_bindingHook,m_bindingHook); // the hook is its own global context
568 catch (SAMLException& e) {
569 log.errorStream() << "Error while processing applicaton element: " << e.what() << CategoryStream::ENDLINE;
575 log.error("Unexpected error while processing application element");
582 void XMLApplication::cleanup()
584 delete m_bindingHook;
587 map<string,XMLPropertySet*>::iterator h=m_handlerMap.begin();
588 while (h!=m_handlerMap.end()) {
592 delete m_credDefault;
594 map<xstring,XMLPropertySet*>::iterator c=m_credMap.begin();
596 map<const XMLCh*,XMLPropertySet*>::iterator c=m_credMap.begin();
598 while (c!=m_credMap.end()) {
602 Iterator<SAMLAttributeDesignator*> i(m_designators);
605 Iterator<IAAP*> j(m_aaps);
608 Iterator<IMetadata*> k(m_metadatas);
611 Iterator<ITrust*> l(m_trusts);
616 short XMLApplication::acceptNode(const DOMNode* node) const
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;
636 return FILTER_ACCEPT;
639 pair<bool,bool> XMLApplication::getBool(const char* name, const char* ns) const
641 pair<bool,bool> ret=XMLPropertySet::getBool(name,ns);
644 return m_base ? m_base->getBool(name,ns) : ret;
647 pair<bool,const char*> XMLApplication::getString(const char* name, const char* ns) const
649 pair<bool,const char*> ret=XMLPropertySet::getString(name,ns);
652 return m_base ? m_base->getString(name,ns) : ret;
655 pair<bool,const XMLCh*> XMLApplication::getXMLString(const char* name, const char* ns) const
657 pair<bool,const XMLCh*> ret=XMLPropertySet::getXMLString(name,ns);
660 return m_base ? m_base->getXMLString(name,ns) : ret;
663 pair<bool,unsigned int> XMLApplication::getUnsignedInt(const char* name, const char* ns) const
665 pair<bool,unsigned int> ret=XMLPropertySet::getUnsignedInt(name,ns);
668 return m_base ? m_base->getUnsignedInt(name,ns) : ret;
671 pair<bool,int> XMLApplication::getInt(const char* name, const char* ns) const
673 pair<bool,int> ret=XMLPropertySet::getInt(name,ns);
676 return m_base ? m_base->getInt(name,ns) : ret;
679 const IPropertySet* XMLApplication::getPropertySet(const char* name, const char* ns) const
681 const IPropertySet* ret=XMLPropertySet::getPropertySet(name,ns);
684 return m_base->getPropertySet(name,ns);
687 Iterator<SAMLAttributeDesignator*> XMLApplication::getAttributeDesignators() const
689 if (!m_designators.empty() || !m_base)
690 return m_designators;
691 return m_base->getAttributeDesignators();
694 Iterator<IAAP*> XMLApplication::getAAPProviders() const
696 return (m_aaps.empty() && m_base) ? m_base->getAAPProviders() : m_aaps;
699 Iterator<IMetadata*> XMLApplication::getMetadataProviders() const
701 return (m_metadatas.empty() && m_base) ? m_base->getMetadataProviders() : m_metadatas;
704 Iterator<ITrust*> XMLApplication::getTrustProviders() const
706 return (m_trusts.empty() && m_base) ? m_base->getTrustProviders() : m_trusts;
709 Iterator<const XMLCh*> XMLApplication::getAudiences() const
711 return (m_audiences.empty() && m_base) ? m_base->getAudiences() : m_audiences;
714 const IPropertySet* XMLApplication::getCredentialUse(const IEntityDescriptor* provider) const
716 if (!m_credDefault && m_base)
717 return m_base->getCredentialUse(provider);
720 map<xstring,XMLPropertySet*>::const_iterator i=m_credMap.find(provider->getId());
721 if (i!=m_credMap.end())
723 const IEntitiesDescriptor* group=provider->getEntitiesDescriptor();
725 if (group->getName()) {
726 i=m_credMap.find(group->getName());
727 if (i!=m_credMap.end())
730 group=group->getEntitiesDescriptor();
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()))
737 const IEntitiesDescriptor* group=provider->getEntitiesDescriptor();
739 if (!XMLString::compareString(i->first,group->getName()))
741 group=group->getEntitiesDescriptor();
745 return m_credDefault;
748 const IPropertySet* XMLApplication::getDefaultSessionInitiator() const
750 if (m_sessionInitDefault) return m_sessionInitDefault;
751 return m_base ? m_base->getDefaultSessionInitiator() : NULL;
754 const IPropertySet* XMLApplication::getSessionInitiatorById(const char* id) const
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;
761 const IPropertySet* XMLApplication::getDefaultAssertionConsumerService() const
763 if (m_acsDefault) return m_acsDefault;
764 return m_base ? m_base->getDefaultAssertionConsumerService() : NULL;
767 const IPropertySet* XMLApplication::getAssertionConsumerServiceByIndex(unsigned short index) const
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;
774 const IPropertySet* XMLApplication::getHandlerConfig(const char* path) const
777 map<string,XMLPropertySet*>::const_iterator i=m_handlerMap.find(wrap.substr(0,wrap.find('?')));
778 if (i!=m_handlerMap.end())
780 return m_base ? m_base->getHandlerConfig(path) : NULL;
783 ReloadableXMLFileImpl* XMLConfig::newImplementation(const char* pathname, bool first) const
785 return new XMLConfigImpl(pathname,first,this);
788 ReloadableXMLFileImpl* XMLConfig::newImplementation(const DOMElement* e, bool first) const
790 return new XMLConfigImpl(e,first,this);
793 short XMLConfigImpl::acceptNode(const DOMNode* node) const
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;
815 return FILTER_ACCEPT;
818 void XMLConfigImpl::init(bool first)
821 saml::NDC ndc("init");
823 Category& log=Category::getInstance("shibtarget.Config");
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)");
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));
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));
839 SHIRE=saml::XML::getFirstChildElement(ReloadableXMLFileImpl::m_root,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Local));
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());
854 PropertyConfigurator::configure(logpath.get());
856 catch (ConfigureFailure& e) {
857 log.error("Error reading logging configuration: %s",e.what());
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);
868 // Much of the processing can only occur on the first instantiation.
870 // Now load any extensions to insure any needed plugins are registered.
872 saml::XML::getFirstChildElement(ReloadableXMLFileImpl::m_root,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Extensions));
874 exts=saml::XML::getFirstChildElement(exts,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Library));
876 auto_ptr_char path(exts->getAttributeNS(NULL,SHIBT_L(path)));
878 SAMLConfig::getConfig().saml_register_extension(path.get(),exts);
879 log.debug("loaded global extension library %s",path.get());
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());
888 log.crit("unable to load optional global extension library %s: %s", path.get(), e.what());
890 exts=saml::XML::getNextSiblingElement(exts,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Library));
894 if (conf.isEnabled(ShibTargetConfig::GlobalExtensions)) {
895 exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Extensions));
897 exts=saml::XML::getFirstChildElement(exts,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Library));
899 auto_ptr_char path(exts->getAttributeNS(NULL,SHIBT_L(path)));
901 SAMLConfig::getConfig().saml_register_extension(path.get(),exts);
902 log.debug("loaded Global extension library %s",path.get());
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());
911 log.crit("unable to load optional Global extension library %s: %s", path.get(), e.what());
913 exts=saml::XML::getNextSiblingElement(exts,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Library));
918 if (conf.isEnabled(ShibTargetConfig::LocalExtensions)) {
919 exts=saml::XML::getFirstChildElement(SHIRE,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Extensions));
921 exts=saml::XML::getFirstChildElement(exts,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Library));
923 auto_ptr_char path(exts->getAttributeNS(NULL,SHIBT_L(path)));
925 SAMLConfig::getConfig().saml_register_extension(path.get(),exts);
926 log.debug("loaded Local extension library %s",path.get());
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());
935 log.crit("unable to load optional Local extension library %s: %s", path.get(), e.what());
937 exts=saml::XML::getNextSiblingElement(exts,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Library));
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));
947 log.info("building Listener of type %s...",shibtarget::XML::UnixListenerType);
948 plugin=shibConf.getPlugMgr().newPlugin(shibtarget::XML::UnixListenerType,exts);
951 exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(TCPListener));
953 log.info("building Listener of type %s...",shibtarget::XML::TCPListenerType);
954 plugin=shibConf.getPlugMgr().newPlugin(shibtarget::XML::TCPListenerType,exts);
957 exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Listener));
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);
964 log.fatal("can't build Listener object, missing conf:Listener element?");
965 throw ConfigurationException("can't build Listener object, missing conf:Listener element?");
970 IListener* listen=dynamic_cast<IListener*>(plugin);
972 m_outer->m_listener=listen;
975 log.fatal("plugin was not a Listener object");
976 throw UnsupportedExtensionException("plugin was not a Listener object");
981 if (conf.isEnabled(ShibTargetConfig::Caching)) {
982 IPlugIn* plugin=NULL;
983 exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(MemorySessionCache));
985 log.info("building Session Cache of type %s...",shibtarget::XML::MemorySessionCacheType);
986 plugin=shibConf.getPlugMgr().newPlugin(shibtarget::XML::MemorySessionCacheType,exts);
989 exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(MySQLSessionCache));
991 log.info("building Session Cache of type %s...",shibtarget::XML::MySQLSessionCacheType);
992 plugin=shibConf.getPlugMgr().newPlugin(shibtarget::XML::MySQLSessionCacheType,exts);
995 exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(SessionCache));
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);
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?");
1008 ISessionCache* cache=dynamic_cast<ISessionCache*>(plugin);
1010 m_outer->m_sessionCache=cache;
1013 log.fatal("plugin was not a Session Cache object");
1014 throw UnsupportedExtensionException("plugin was not a Session Cache object");
1019 exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(MySQLReplayCache));
1021 log.info("building Replay Cache of type %s...",shibtarget::XML::MySQLReplayCacheType);
1022 m_outer->m_replayCache=IReplayCache::getInstance(shibtarget::XML::MySQLReplayCacheType,exts);
1025 exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(ReplayCache));
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);
1032 // OpenSAML default provider.
1033 log.info("building default Replay Cache...");
1034 m_outer->m_replayCache=IReplayCache::getInstance();
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());
1045 IPlugIn* plugin=shibConf.getPlugMgr().newPlugin(type.get(),static_cast<DOMElement*>(nlist->item(i)));
1047 IAttributeFactory* fact=dynamic_cast<IAttributeFactory*>(plugin);
1049 m_outer->m_attrFactories.push_back(fact);
1050 ShibConfig::getConfig().regAttributeMapping(
1051 static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,L(AttributeName)),
1057 log.crit("plugin was not an Attribute factory");
1061 catch (SAMLException& ex) {
1062 log.crit("error building Attribute factory: %s",ex.what());
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));
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);
1075 IRequestMapper* reqmap=dynamic_cast<IRequestMapper*>(plugin);
1077 m_requestMapper=reqmap;
1080 log.fatal("plugin was not a Request Mapper object");
1081 throw UnsupportedExtensionException("plugin was not a Request Mapper object");
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?");
1091 // Now we load any credentials providers.
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());
1099 IPlugIn* plugin=shibConf.getPlugMgr().newPlugin(type.get(),static_cast<DOMElement*>(nlist->item(i)));
1101 ICredentials* creds=dynamic_cast<ICredentials*>(plugin);
1103 m_creds.push_back(creds);
1106 log.crit("plugin was not a credentials provider");
1110 catch (SAMLException& ex) {
1111 log.crit("error building credentials provider: %s",ex.what());
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)
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?");
1124 XMLApplication* defapp=new XMLApplication(m_outer, m_creds, app);
1125 m_appmap[defapp->getId()]=defapp;
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");
1135 m_appmap[iapp->getId()]=iapp;
1138 catch (SAMLException& e) {
1139 log.errorStream() << "Error while loading SP configuration: " << e.what() << CategoryStream::ENDLINE;
1144 log.error("Unexpected error while loading SP configuration");
1150 XMLConfigImpl::~XMLConfigImpl()
1152 delete m_requestMapper;
1153 for (map<string,IApplication*>::iterator i=m_appmap.begin(); i!=m_appmap.end(); i++)
1155 for (vector<ICredentials*>::iterator j=m_creds.begin(); j!=m_creds.end(); j++)
1159 XMLConfig::~XMLConfig()
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++)