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 <shibsp/DOMPropertySet.h>
26 #include <shibsp/SPConfig.h>
27 #include <shibsp/SPConstants.h>
28 #include <log4cpp/Category.hh>
29 #include <log4cpp/PropertyConfigurator.hh>
31 #include <sys/types.h>
34 using namespace shibsp;
35 using namespace shibtarget;
36 using namespace shibboleth;
38 using namespace log4cpp;
41 using xmltooling::TrustEngine;
42 using opensaml::saml2md::MetadataProvider;
44 namespace shibtarget {
46 // Application configuration wrapper
47 class XMLApplication : public virtual IApplication, public DOMPropertySet, public DOMNodeFilter
50 XMLApplication(const IConfig*, const Iterator<ICredentials*>& creds, const DOMElement* e, const XMLApplication* base=NULL);
51 ~XMLApplication() { cleanup(); }
54 pair<bool,bool> getBool(const char* name, const char* ns=NULL) const;
55 pair<bool,const char*> getString(const char* name, const char* ns=NULL) const;
56 pair<bool,const XMLCh*> getXMLString(const char* name, const char* ns=NULL) const;
57 pair<bool,unsigned int> getUnsignedInt(const char* name, const char* ns=NULL) const;
58 pair<bool,int> getInt(const char* name, const char* ns=NULL) const;
59 const PropertySet* getPropertySet(const char* name, const char* ns="urn:mace:shibboleth:target:config:1.0") const;
62 const char* getId() const {return getString("id").second;}
63 const char* getHash() const {return m_hash.c_str();}
64 Iterator<SAMLAttributeDesignator*> getAttributeDesignators() const;
65 Iterator<IAAP*> getAAPProviders() const;
66 Iterator<IMetadata*> getMetadataProviders() const;
67 Iterator<ITrust*> getTrustProviders() const;
68 Iterator<const XMLCh*> getAudiences() const;
69 const PropertySet* getCredentialUse(const IEntityDescriptor* provider) const;
71 const MetadataProvider* getMetadataProvider() const;
72 const TrustEngine* getTrustEngine() const;
74 const SAMLBrowserProfile* getBrowserProfile() const {return m_profile;}
75 const SAMLBinding* getBinding(const XMLCh* binding) const
76 {return XMLString::compareString(SAMLBinding::SOAP,binding) ? NULL : m_binding;}
77 SAMLBrowserProfile::ArtifactMapper* getArtifactMapper() const {return new STArtifactMapper(this);}
81 const IRoleDescriptor* role=NULL,
82 const Iterator<ITrust*>& trusts=EMPTY(ITrust*)
84 const IHandler* getDefaultSessionInitiator() const;
85 const IHandler* getSessionInitiatorById(const char* id) const;
86 const IHandler* getDefaultAssertionConsumerService() const;
87 const IHandler* getAssertionConsumerServiceByIndex(unsigned short index) const;
88 Iterator<const IHandler*> getAssertionConsumerServicesByBinding(const XMLCh* binding) const;
89 const IHandler* getHandler(const char* path) const;
91 // Provides filter to exclude special config elements.
92 short acceptNode(const DOMNode* node) const;
96 const IConfig* m_ini; // this is ok because its locking scope includes us
97 const XMLApplication* m_base;
99 vector<SAMLAttributeDesignator*> m_designators;
100 vector<IAAP*> m_aaps;
101 vector<IMetadata*> m_metadatas;
102 vector<ITrust*> m_trusts;
103 MetadataProvider* m_metadata;
104 TrustEngine* m_trust;
105 vector<const XMLCh*> m_audiences;
106 ShibBrowserProfile* m_profile;
107 SAMLBinding* m_binding;
108 ShibHTTPHook* m_bindingHook;
110 // vectors manage object life for handlers and their property sets
111 vector<IHandler*> m_handlers;
112 vector<PropertySet*> m_handlerProps;
114 // maps location (path info) to applicable handlers
115 map<string,const IHandler*> m_handlerMap;
117 // maps unique indexes to consumer services
118 map<unsigned int,const IHandler*> m_acsIndexMap;
120 // pointer to default consumer service
121 const IHandler* m_acsDefault;
123 // maps binding strings to supporting consumer service(s)
125 typedef map<xstring,vector<const IHandler*> > ACSBindingMap;
127 typedef map<string,vector<const IHandler*> > ACSBindingMap;
129 ACSBindingMap m_acsBindingMap;
131 // maps unique ID strings to session initiators
132 map<string,const IHandler*> m_sessionInitMap;
134 // pointer to default session initiator
135 const IHandler* m_sessionInitDefault;
137 DOMPropertySet* m_credDefault;
139 map<xstring,PropertySet*> m_credMap;
141 map<const XMLCh*,PropertySet*> m_credMap;
145 // Top-level configuration implementation
147 class XMLConfigImpl : public ReloadableXMLFileImpl, public DOMPropertySet, public DOMNodeFilter
150 XMLConfigImpl(const char* pathname, bool first, const XMLConfig* outer)
151 : ReloadableXMLFileImpl(pathname), m_outer(outer), m_requestMapper(NULL) { init(first); }
152 XMLConfigImpl(const DOMElement* e, bool first, const XMLConfig* outer)
153 : ReloadableXMLFileImpl(e), m_outer(outer), m_requestMapper(NULL) { init(first); }
156 IRequestMapper* m_requestMapper;
157 map<string,IApplication*> m_appmap;
158 vector<ICredentials*> m_creds;
159 vector<IAttributeFactory*> m_attrFactories;
161 // Provides filter to exclude special config elements.
162 short acceptNode(const DOMNode* node) const;
165 void init(bool first);
166 const XMLConfig* m_outer;
169 class XMLConfig : public IConfig, public ReloadableXMLFile
172 XMLConfig(const DOMElement* e) : ReloadableXMLFile(e), m_listener(NULL), m_sessionCache(NULL), m_replayCache(NULL) {}
176 delete m_sessionCache;
178 delete m_replayCache;
184 void init() { getImplementation(); }
187 pair<bool,bool> getBool(const char* name, const char* ns=NULL) const {return static_cast<XMLConfigImpl*>(m_impl)->getBool(name,ns);}
188 pair<bool,const char*> getString(const char* name, const char* ns=NULL) const {return static_cast<XMLConfigImpl*>(m_impl)->getString(name,ns);}
189 pair<bool,const XMLCh*> getXMLString(const char* name, const char* ns=NULL) const {return static_cast<XMLConfigImpl*>(m_impl)->getXMLString(name,ns);}
190 pair<bool,unsigned int> getUnsignedInt(const char* name, const char* ns=NULL) const {return static_cast<XMLConfigImpl*>(m_impl)->getUnsignedInt(name,ns);}
191 pair<bool,int> getInt(const char* name, const char* ns=NULL) const {return static_cast<XMLConfigImpl*>(m_impl)->getInt(name,ns);}
192 const PropertySet* getPropertySet(const char* name, const char* ns="urn:mace:shibboleth:target:config:1.0") const {return static_cast<XMLConfigImpl*>(m_impl)->getPropertySet(name,ns);}
193 const DOMElement* getElement() const {return static_cast<XMLConfigImpl*>(m_impl)->getElement();}
196 ListenerService* getListener() const {return m_listener;}
197 ISessionCache* getSessionCache() const {return m_sessionCache;}
198 IReplayCache* getReplayCache() const {return m_replayCache;}
199 IRequestMapper* getRequestMapper() const {return static_cast<XMLConfigImpl*>(m_impl)->m_requestMapper;}
200 const IApplication* getApplication(const char* applicationId) const {
201 map<string,IApplication*>::const_iterator i=static_cast<XMLConfigImpl*>(m_impl)->m_appmap.find(applicationId);
202 return (i!=static_cast<XMLConfigImpl*>(m_impl)->m_appmap.end()) ? i->second : NULL;
204 Iterator<ICredentials*> getCredentialsProviders() const {return static_cast<XMLConfigImpl*>(m_impl)->m_creds;}
207 virtual ReloadableXMLFileImpl* newImplementation(const char* pathname, bool first=true) const;
208 virtual ReloadableXMLFileImpl* newImplementation(const DOMElement* e, bool first=true) const;
211 friend class XMLConfigImpl;
212 mutable ListenerService* m_listener;
213 mutable ISessionCache* m_sessionCache;
214 mutable IReplayCache* m_replayCache;
218 IConfig* STConfig::ShibTargetConfigFactory(const DOMElement* e)
220 return new XMLConfig(e);
223 XMLApplication::XMLApplication(
225 const Iterator<ICredentials*>& creds,
227 const XMLApplication* base
228 ) : m_ini(ini), m_base(base), m_metadata(NULL), m_trust(NULL), m_profile(NULL), m_binding(NULL), m_bindingHook(NULL),
229 m_credDefault(NULL), m_sessionInitDefault(NULL), m_acsDefault(NULL)
232 NDC ndc("XMLApplication");
234 Category& log=Category::getInstance("shibtarget.XMLApplication");
237 // First load any property sets.
238 map<string,string> root_remap;
239 root_remap["shire"]="session";
240 root_remap["shireURL"]="handlerURL";
241 root_remap["shireSSL"]="handlerSSL";
242 load(e,log,this,&root_remap);
243 const PropertySet* propcheck=getPropertySet("Errors");
244 if (propcheck && !propcheck->getString("session").first)
245 throw ConfigurationException("<Errors> element requires 'session' (or deprecated 'shire') attribute");
246 propcheck=getPropertySet("Sessions");
247 if (propcheck && !propcheck->getString("handlerURL").first)
248 throw ConfigurationException("<Sessions> element requires 'handlerURL' (or deprecated 'shireURL') attribute");
251 m_hash+=getString("providerId").second;
252 m_hash=SAMLArtifact::toHex(SAMLArtifactType0001::generateSourceId(m_hash.c_str()));
254 SPConfig& conf=SPConfig::getConfig();
255 SAMLConfig& shibConf=SAMLConfig::getConfig();
258 bool hardACS=false, hardSessionInit=false;
259 DOMElement* handler=saml::XML::getFirstChildElement(propcheck->getElement());
261 // A handler is split across a property set and the plugin itself, which is based on the Binding property.
262 // We build both objects first and then insert them into various structures for lookup.
264 DOMPropertySet* hprops=new DOMPropertySet();
266 hprops->load(handler,log,this); // filter irrelevant for now, no embedded elements expected
267 const char* bindprop=hprops->getString("Binding").second;
269 throw ConfigurationException("Handler element has no Binding attribute, skipping it...");
270 IPlugIn* hplug=shibConf.getPlugMgr().newPlugin(bindprop,handler);
271 hobj=dynamic_cast<IHandler*>(hplug);
274 throw UnsupportedProfileException(
275 "Plugin for binding ($1) does not implement IHandler interface.",params(1,bindprop)
279 catch (SAMLException& ex) {
280 // If we get here, the handler's not built, so dispose of the property set.
281 log.error("caught exception processing a handler element: %s",ex.what());
286 const char* location=hprops ? hprops->getString("Location").second : NULL;
290 handler=saml::XML::getNextSiblingElement(handler);
294 // Save off the objects after giving the property set to the handler for its use.
295 hobj->setProperties(hprops);
296 m_handlers.push_back(hobj);
297 m_handlerProps.push_back(hprops);
299 // Insert into location map.
300 if (*location == '/')
301 m_handlerMap[location]=hobj;
303 m_handlerMap[string("/") + location]=hobj;
305 // If it's an ACS or SI, handle index/id mappings and defaulting.
306 if (saml::XML::isElementNamed(handler,shibtarget::XML::SAML2META_NS,SHIBT_L(AssertionConsumerService))) {
309 const XMLCh* binding=hprops->getXMLString("Binding").second;
311 const char* binding=hprops->getString("Binding").second;
313 if (m_acsBindingMap.count(binding)==0)
314 m_acsBindingMap[binding]=vector<const IHandler*>(1,hobj);
316 m_acsBindingMap[binding].push_back(hobj);
317 m_acsIndexMap[hprops->getUnsignedInt("index").second]=hobj;
320 pair<bool,bool> defprop=hprops->getBool("isDefault");
322 if (defprop.second) {
327 else if (!m_acsDefault)
331 else if (saml::XML::isElementNamed(handler,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(SessionInitiator))) {
332 pair<bool,const char*> si_id=hprops->getString("id");
333 if (si_id.first && si_id.second)
334 m_sessionInitMap[si_id.second]=hobj;
335 if (!hardSessionInit) {
336 pair<bool,bool> defprop=hprops->getBool("isDefault");
338 if (defprop.second) {
339 hardSessionInit=true;
340 m_sessionInitDefault=hobj;
343 else if (!m_sessionInitDefault)
344 m_sessionInitDefault=hobj;
347 handler=saml::XML::getNextSiblingElement(handler);
350 // If no handlers defined at the root, assume a legacy configuration.
351 if (!m_base && m_handlers.empty()) {
352 // A legacy config installs a SAML POST handler at the root handler location.
353 // We use the Sessions element itself as the PropertySet.
355 auto_ptr_char b1(shibspconstants::SHIB1_SESSIONINIT_PROFILE_URI);
356 IPlugIn* hplug=shibConf.getPlugMgr().newPlugin(b1.get(),propcheck->getElement());
357 IHandler* h1=dynamic_cast<IHandler*>(hplug);
360 throw UnsupportedProfileException(
361 "Plugin for binding ($1) does not implement IHandler interface.",params(1,b1.get())
364 h1->setProperties(propcheck);
365 m_handlers.push_back(h1);
366 m_sessionInitDefault=h1;
368 auto_ptr_char b2(SAMLBrowserProfile::BROWSER_POST);
369 hplug=shibConf.getPlugMgr().newPlugin(b2.get(),propcheck->getElement());
370 IHandler* h2=dynamic_cast<IHandler*>(hplug);
373 throw UnsupportedProfileException(
374 "Plugin for binding ($1) does not implement IHandler interface.",params(1,b2.get())
377 h2->setProperties(propcheck);
378 m_handlers.push_back(h2);
379 m_handlerMap[""] = h2;
383 // Process general configuration elements.
385 DOMNodeList* nlist=e->getElementsByTagNameNS(saml::XML::SAML_NS,L(AttributeDesignator));
386 for (i=0; nlist && i<nlist->getLength(); i++)
387 if (nlist->item(i)->getParentNode()->isSameNode(e))
388 m_designators.push_back(new SAMLAttributeDesignator(static_cast<DOMElement*>(nlist->item(i))));
390 nlist=e->getElementsByTagNameNS(saml::XML::SAML_NS,L(Audience));
391 for (i=0; nlist && i<nlist->getLength(); i++)
392 if (nlist->item(i)->getParentNode()->isSameNode(e))
393 m_audiences.push_back(nlist->item(i)->getFirstChild()->getNodeValue());
395 // Always include our own providerId as an audience.
396 m_audiences.push_back(getXMLString("providerId").second);
398 if (conf.isEnabled(SPConfig::AAP)) {
399 nlist=e->getElementsByTagNameNS(shibtarget::XML::SHIBTARGET_NS,SHIBT_L(AAPProvider));
400 for (i=0; nlist && i<nlist->getLength(); i++) {
401 if (nlist->item(i)->getParentNode()->isSameNode(e)) {
402 auto_ptr_char type(static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,SHIBT_L(type)));
403 log.info("building AAP provider of type %s...",type.get());
405 IPlugIn* plugin=shibConf.getPlugMgr().newPlugin(type.get(),static_cast<DOMElement*>(nlist->item(i)));
406 IAAP* aap=dynamic_cast<IAAP*>(plugin);
408 m_aaps.push_back(aap);
411 log.crit("plugin was not an AAP provider");
414 catch (SAMLException& ex) {
415 log.crit("error building AAP provider: %s",ex.what());
421 if (conf.isEnabled(SPConfig::Metadata)) {
422 nlist=e->getElementsByTagNameNS(shibtarget::XML::SHIBTARGET_NS,SHIBT_L(MetadataProvider));
423 for (i=0; nlist && i<nlist->getLength(); i++) {
424 if (nlist->item(i)->getParentNode()->isSameNode(e)) {
425 auto_ptr_char type(static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,SHIBT_L(type)));
426 log.info("building metadata provider of type %s...",type.get());
428 IPlugIn* plugin=shibConf.getPlugMgr().newPlugin(type.get(),static_cast<DOMElement*>(nlist->item(i)));
429 IMetadata* md=dynamic_cast<IMetadata*>(plugin);
431 m_metadatas.push_back(md);
434 log.crit("plugin was not a metadata provider");
437 catch (SAMLException& ex) {
438 log.crit("error building metadata provider: %s",ex.what());
442 nlist=e->getElementsByTagNameNS(shibtarget::XML::SHIBTARGET_NS,SHIBT_L(FederationProvider));
443 for (i=0; nlist && i<nlist->getLength(); i++) {
444 if (nlist->item(i)->getParentNode()->isSameNode(e)) {
445 auto_ptr_char type(static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,SHIBT_L(type)));
446 log.info("building metadata provider of type %s...",type.get());
448 IPlugIn* plugin=shibConf.getPlugMgr().newPlugin(type.get(),static_cast<DOMElement*>(nlist->item(i)));
449 IMetadata* md=dynamic_cast<IMetadata*>(plugin);
451 m_metadatas.push_back(md);
454 log.crit("plugin was not a metadata provider");
457 catch (SAMLException& ex) {
458 log.crit("error building metadata provider: %s",ex.what());
464 if (conf.isEnabled(SPConfig::Trust)) {
465 // First build the old plugins.
466 // TODO: remove this later
467 nlist=e->getElementsByTagNameNS(shibtarget::XML::SHIBTARGET_NS,SHIBT_L(TrustProvider));
468 for (i=0; nlist && i<nlist->getLength(); i++) {
469 if (nlist->item(i)->getParentNode()->isSameNode(e)) {
470 auto_ptr_char type(static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,SHIBT_L(type)));
471 log.info("building trust provider of type %s...",type.get());
473 IPlugIn* plugin=shibConf.getPlugMgr().newPlugin(type.get(),static_cast<DOMElement*>(nlist->item(i)));
474 ITrust* trust=dynamic_cast<ITrust*>(plugin);
476 m_trusts.push_back(trust);
479 log.crit("plugin was not a trust provider");
482 catch (SAMLException& ex) {
483 log.crit("error building trust provider: %s",ex.what());
489 // Finally, load credential mappings.
490 const DOMElement* cu=saml::XML::getFirstChildElement(e,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(CredentialUse));
492 m_credDefault=new DOMPropertySet();
493 m_credDefault->load(cu,log,this);
494 cu=saml::XML::getFirstChildElement(cu,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(RelyingParty));
496 DOMPropertySet* rp=new DOMPropertySet();
497 rp->load(cu,log,this);
498 m_credMap[cu->getAttributeNS(NULL,SHIBT_L(Name))]=rp;
499 cu=saml::XML::getNextSiblingElement(cu,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(RelyingParty));
503 if (conf.isEnabled(SPConfig::OutOfProcess)) {
504 // Really finally, build local browser profile and binding objects.
505 m_profile=new ShibBrowserProfile(
507 getMetadataProviders(),
510 m_bindingHook=new ShibHTTPHook(
514 m_binding=SAMLBinding::getInstance(SAMLBinding::SOAP);
515 SAMLSOAPHTTPBinding* bptr=dynamic_cast<SAMLSOAPHTTPBinding*>(m_binding);
517 log.fatal("binding implementation was not SOAP over HTTP");
518 throw UnsupportedExtensionException("binding implementation was not SOAP over HTTP");
520 bptr->addHook(m_bindingHook,m_bindingHook); // the hook is its own global context
523 catch (SAMLException& e) {
524 log.errorStream() << "Error while processing applicaton element: " << e.what() << CategoryStream::ENDLINE;
530 log.error("Unexpected error while processing application element");
537 void XMLApplication::cleanup()
539 delete m_bindingHook;
542 for_each(m_handlers.begin(),m_handlers.end(),xmltooling::cleanup<IHandler>());
544 delete m_credDefault;
546 for_each(m_credMap.begin(),m_credMap.end(),xmltooling::cleanup_pair<xstring,PropertySet>());
548 for_each(m_credMap.begin(),m_credMap.end(),xmltooling::cleanup_pair<const XMLCh*,PropertySet>());
550 for_each(m_designators.begin(),m_designators.end(),xmltooling::cleanup<SAMLAttributeDesignator>());
551 for_each(m_aaps.begin(),m_aaps.end(),xmltooling::cleanup<IAAP>());
552 for_each(m_metadatas.begin(),m_metadatas.end(),xmltooling::cleanup<IMetadata>());
553 for_each(m_trusts.begin(),m_trusts.end(),xmltooling::cleanup<ITrust>());
556 short XMLApplication::acceptNode(const DOMNode* node) const
558 if (saml::XML::isElementNamed(static_cast<const DOMElement*>(node),saml::XML::SAML_NS,L(AttributeDesignator)))
559 return FILTER_REJECT;
560 else if (saml::XML::isElementNamed(static_cast<const DOMElement*>(node),saml::XML::SAML_NS,L(Audience)))
561 return FILTER_REJECT;
562 const XMLCh* name=node->getLocalName();
563 if (!XMLString::compareString(name,SHIBT_L(Application)) ||
564 !XMLString::compareString(name,SHIBT_L(AssertionConsumerService)) ||
565 !XMLString::compareString(name,SHIBT_L(SingleLogoutService)) ||
566 !XMLString::compareString(name,SHIBT_L(DiagnosticService)) ||
567 !XMLString::compareString(name,SHIBT_L(SessionInitiator)) ||
568 !XMLString::compareString(name,SHIBT_L(AAPProvider)) ||
569 !XMLString::compareString(name,SHIBT_L(CredentialUse)) ||
570 !XMLString::compareString(name,SHIBT_L(RelyingParty)) ||
571 !XMLString::compareString(name,SHIBT_L(FederationProvider)) ||
572 !XMLString::compareString(name,SHIBT_L(MetadataProvider)) ||
573 !XMLString::compareString(name,SHIBT_L(TrustProvider)))
574 return FILTER_REJECT;
576 return FILTER_ACCEPT;
579 pair<bool,bool> XMLApplication::getBool(const char* name, const char* ns) const
581 pair<bool,bool> ret=DOMPropertySet::getBool(name,ns);
584 return m_base ? m_base->getBool(name,ns) : ret;
587 pair<bool,const char*> XMLApplication::getString(const char* name, const char* ns) const
589 pair<bool,const char*> ret=DOMPropertySet::getString(name,ns);
592 return m_base ? m_base->getString(name,ns) : ret;
595 pair<bool,const XMLCh*> XMLApplication::getXMLString(const char* name, const char* ns) const
597 pair<bool,const XMLCh*> ret=DOMPropertySet::getXMLString(name,ns);
600 return m_base ? m_base->getXMLString(name,ns) : ret;
603 pair<bool,unsigned int> XMLApplication::getUnsignedInt(const char* name, const char* ns) const
605 pair<bool,unsigned int> ret=DOMPropertySet::getUnsignedInt(name,ns);
608 return m_base ? m_base->getUnsignedInt(name,ns) : ret;
611 pair<bool,int> XMLApplication::getInt(const char* name, const char* ns) const
613 pair<bool,int> ret=DOMPropertySet::getInt(name,ns);
616 return m_base ? m_base->getInt(name,ns) : ret;
619 const PropertySet* XMLApplication::getPropertySet(const char* name, const char* ns) const
621 const PropertySet* ret=DOMPropertySet::getPropertySet(name,ns);
624 return m_base->getPropertySet(name,ns);
627 Iterator<SAMLAttributeDesignator*> XMLApplication::getAttributeDesignators() const
629 if (!m_designators.empty() || !m_base)
630 return m_designators;
631 return m_base->getAttributeDesignators();
634 Iterator<IAAP*> XMLApplication::getAAPProviders() const
636 return (m_aaps.empty() && m_base) ? m_base->getAAPProviders() : m_aaps;
639 Iterator<IMetadata*> XMLApplication::getMetadataProviders() const
641 return (m_metadatas.empty() && m_base) ? m_base->getMetadataProviders() : m_metadatas;
644 Iterator<ITrust*> XMLApplication::getTrustProviders() const
646 return (m_trusts.empty() && m_base) ? m_base->getTrustProviders() : m_trusts;
649 Iterator<const XMLCh*> XMLApplication::getAudiences() const
651 return (m_audiences.empty() && m_base) ? m_base->getAudiences() : m_audiences;
654 const PropertySet* XMLApplication::getCredentialUse(const IEntityDescriptor* provider) const
656 if (!m_credDefault && m_base)
657 return m_base->getCredentialUse(provider);
660 map<xstring,PropertySet*>::const_iterator i=m_credMap.find(provider->getId());
661 if (i!=m_credMap.end())
663 const IEntitiesDescriptor* group=provider->getEntitiesDescriptor();
665 if (group->getName()) {
666 i=m_credMap.find(group->getName());
667 if (i!=m_credMap.end())
670 group=group->getEntitiesDescriptor();
673 map<const XMLCh*,PropertySet*>::const_iterator i=m_credMap.begin();
674 for (; i!=m_credMap.end(); i++) {
675 if (!XMLString::compareString(i->first,provider->getId()))
677 const IEntitiesDescriptor* group=provider->getEntitiesDescriptor();
679 if (!XMLString::compareString(i->first,group->getName()))
681 group=group->getEntitiesDescriptor();
685 return m_credDefault;
688 const MetadataProvider* XMLApplication::getMetadataProvider() const
690 return (!m_metadata && m_base) ? m_base->getMetadataProvider() : m_metadata;
693 const TrustEngine* XMLApplication::getTrustEngine() const
695 return (!m_trust && m_base) ? m_base->getTrustEngine() : m_trust;
698 void XMLApplication::validateToken(SAMLAssertion* token, time_t ts, const IRoleDescriptor* role, const Iterator<ITrust*>& trusts) const
701 saml::NDC ndc("validateToken");
703 Category& log=Category::getInstance("shibtarget.XMLApplication");
705 // First we verify the time conditions, using the specified timestamp, if non-zero.
706 SAMLConfig& config=SAMLConfig::getConfig();
708 const SAMLDateTime* notBefore=token->getNotBefore();
709 if (notBefore && ts+config.clock_skew_secs < notBefore->getEpoch())
710 throw ExpiredAssertionException("Assertion is not yet valid.");
711 const SAMLDateTime* notOnOrAfter=token->getNotOnOrAfter();
712 if (notOnOrAfter && notOnOrAfter->getEpoch() <= ts-config.clock_skew_secs)
713 throw ExpiredAssertionException("Assertion is no longer valid.");
716 // Now we process conditions. Only audience restrictions at the moment.
717 Iterator<SAMLCondition*> conditions=token->getConditions();
718 while (conditions.hasNext()) {
719 SAMLCondition* cond=conditions.next();
720 const SAMLAudienceRestrictionCondition* ac=dynamic_cast<const SAMLAudienceRestrictionCondition*>(cond);
724 log.error("unrecognized Condition in assertion (%s)",os.str().c_str());
725 throw UnsupportedExtensionException("Assertion contains an unrecognized condition.");
727 else if (!ac->eval(getAudiences())) {
730 log.error("unacceptable AudienceRestrictionCondition in assertion (%s)",os.str().c_str());
731 throw UnsupportedProfileException("Assertion contains an unacceptable AudienceRestrictionCondition.");
736 log.warn("no metadata provided, so no signature validation was performed");
740 const PropertySet* credUse=getCredentialUse(role->getEntityDescriptor());
741 pair<bool,bool> signedAssertions=credUse ? credUse->getBool("signedAssertions") : make_pair(false,false);
744 if (token->isSigned() && !t.validate(*token,role))
745 throw TrustException("Assertion signature did not validate.");
746 else if (signedAssertions.first && signedAssertions.second)
747 throw TrustException("Assertion was unsigned, violating policy based on the issuer.");
750 const IHandler* XMLApplication::getDefaultSessionInitiator() const
752 if (m_sessionInitDefault) return m_sessionInitDefault;
753 return m_base ? m_base->getDefaultSessionInitiator() : NULL;
756 const IHandler* XMLApplication::getSessionInitiatorById(const char* id) const
758 map<string,const IHandler*>::const_iterator i=m_sessionInitMap.find(id);
759 if (i!=m_sessionInitMap.end()) return i->second;
760 return m_base ? m_base->getSessionInitiatorById(id) : NULL;
763 const IHandler* XMLApplication::getDefaultAssertionConsumerService() const
765 if (m_acsDefault) return m_acsDefault;
766 return m_base ? m_base->getDefaultAssertionConsumerService() : NULL;
769 const IHandler* XMLApplication::getAssertionConsumerServiceByIndex(unsigned short index) const
771 map<unsigned int,const IHandler*>::const_iterator i=m_acsIndexMap.find(index);
772 if (i!=m_acsIndexMap.end()) return i->second;
773 return m_base ? m_base->getAssertionConsumerServiceByIndex(index) : NULL;
776 Iterator<const IHandler*> XMLApplication::getAssertionConsumerServicesByBinding(const XMLCh* binding) const
779 ACSBindingMap::const_iterator i=m_acsBindingMap.find(binding);
781 auto_ptr_char temp(binding);
782 ACSBindingMap::const_iterator i=m_acsBindingMap.find(temp.get());
784 if (i!=m_acsBindingMap.end())
786 return m_base ? m_base->getAssertionConsumerServicesByBinding(binding) : EMPTY(const IHandler*);
789 const IHandler* XMLApplication::getHandler(const char* path) const
792 map<string,const IHandler*>::const_iterator i=m_handlerMap.find(wrap.substr(0,wrap.find('?')));
793 if (i!=m_handlerMap.end())
795 return m_base ? m_base->getHandler(path) : NULL;
798 ReloadableXMLFileImpl* XMLConfig::newImplementation(const char* pathname, bool first) const
800 return new XMLConfigImpl(pathname,first,this);
803 ReloadableXMLFileImpl* XMLConfig::newImplementation(const DOMElement* e, bool first) const
805 return new XMLConfigImpl(e,first,this);
808 short XMLConfigImpl::acceptNode(const DOMNode* node) const
810 if (XMLString::compareString(node->getNamespaceURI(),shibtarget::XML::SHIBTARGET_NS))
811 return FILTER_ACCEPT;
812 const XMLCh* name=node->getLocalName();
813 if (!XMLString::compareString(name,SHIBT_L(Applications)) ||
814 !XMLString::compareString(name,SHIBT_L(AttributeFactory)) ||
815 !XMLString::compareString(name,SHIBT_L(CredentialsProvider)) ||
816 !XMLString::compareString(name,SHIBT_L(Extensions)) ||
817 !XMLString::compareString(name,SHIBT_L(Implementation)) ||
818 !XMLString::compareString(name,SHIBT_L(Listener)) ||
819 !XMLString::compareString(name,SHIBT_L(MemorySessionCache)) ||
820 !XMLString::compareString(name,SHIBT_L(MySQLReplayCache)) ||
821 !XMLString::compareString(name,SHIBT_L(MySQLSessionCache)) ||
822 !XMLString::compareString(name,SHIBT_L(RequestMap)) ||
823 !XMLString::compareString(name,SHIBT_L(RequestMapProvider)) ||
824 !XMLString::compareString(name,SHIBT_L(ReplayCache)) ||
825 !XMLString::compareString(name,SHIBT_L(SessionCache)) ||
826 !XMLString::compareString(name,SHIBT_L(TCPListener)) ||
827 !XMLString::compareString(name,SHIBT_L(UnixListener)))
828 return FILTER_REJECT;
830 return FILTER_ACCEPT;
833 void XMLConfigImpl::init(bool first)
836 saml::NDC ndc("init");
838 Category& log=Category::getInstance("shibtarget.Config");
841 if (!saml::XML::isElementNamed(ReloadableXMLFileImpl::m_root,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(ShibbolethTargetConfig)) &&
842 !saml::XML::isElementNamed(ReloadableXMLFileImpl::m_root,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(SPConfig))) {
843 log.error("Construction requires a valid configuration file: (conf:SPConfig as root element)");
844 throw ConfigurationException("Construction requires a valid configuration file: (conf:SPConfig as root element)");
847 SAMLConfig& shibConf=SAMLConfig::getConfig();
848 SPConfig& conf=SPConfig::getConfig();
849 const DOMElement* SHAR=saml::XML::getFirstChildElement(ReloadableXMLFileImpl::m_root,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(SHAR));
851 SHAR=saml::XML::getFirstChildElement(ReloadableXMLFileImpl::m_root,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Global));
853 SHAR=saml::XML::getFirstChildElement(ReloadableXMLFileImpl::m_root,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(OutOfProcess));
854 const DOMElement* SHIRE=saml::XML::getFirstChildElement(ReloadableXMLFileImpl::m_root,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(SHIRE));
856 SHIRE=saml::XML::getFirstChildElement(ReloadableXMLFileImpl::m_root,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Local));
858 SHIRE=saml::XML::getFirstChildElement(ReloadableXMLFileImpl::m_root,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(InProcess));
860 // Initialize log4cpp manually in order to redirect log messages as soon as possible.
861 if (conf.isEnabled(SPConfig::Logging)) {
862 const XMLCh* logger=NULL;
863 if (conf.isEnabled(SPConfig::OutOfProcess))
864 logger=SHAR->getAttributeNS(NULL,SHIBT_L(logger));
865 else if (conf.isEnabled(SPConfig::InProcess))
866 logger=SHIRE->getAttributeNS(NULL,SHIBT_L(logger));
867 if (!logger || !*logger)
868 logger=ReloadableXMLFileImpl::m_root->getAttributeNS(NULL,SHIBT_L(logger));
869 if (logger && *logger) {
870 auto_ptr_char logpath(logger);
871 log.debug("loading new logging configuration from (%s), check log destination for status of configuration",logpath.get());
873 PropertyConfigurator::configure(logpath.get());
875 catch (ConfigureFailure& e) {
876 log.error("Error reading logging configuration: %s",e.what());
881 // First load any property sets.
882 map<string,string> root_remap;
883 root_remap["SHAR"]="OutOfProcess";
884 root_remap["SHIRE"]="InProcess";
885 root_remap["Global"]="OutOfProcess";
886 root_remap["Local"]="InProcess";
887 load(ReloadableXMLFileImpl::m_root,log,this,&root_remap);
889 // Much of the processing can only occur on the first instantiation.
891 // Now load any extensions to insure any needed plugins are registered.
893 saml::XML::getFirstChildElement(ReloadableXMLFileImpl::m_root,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Extensions));
895 exts=saml::XML::getFirstChildElement(exts,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Library));
897 auto_ptr_char path(exts->getAttributeNS(NULL,SHIBT_L(path)));
899 SAMLConfig::getConfig().saml_register_extension(path.get(),exts);
900 log.debug("loaded global extension library %s",path.get());
902 catch (SAMLException& e) {
903 const XMLCh* fatal=exts->getAttributeNS(NULL,SHIBT_L(fatal));
904 if (fatal && (*fatal==chLatin_t || *fatal==chDigit_1)) {
905 log.fatal("unable to load mandatory global extension library %s: %s", path.get(), e.what());
909 log.crit("unable to load optional global extension library %s: %s", path.get(), e.what());
911 exts=saml::XML::getNextSiblingElement(exts,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Library));
915 if (conf.isEnabled(SPConfig::OutOfProcess)) {
916 exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Extensions));
918 exts=saml::XML::getFirstChildElement(exts,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Library));
920 auto_ptr_char path(exts->getAttributeNS(NULL,SHIBT_L(path)));
922 SAMLConfig::getConfig().saml_register_extension(path.get(),exts);
923 log.debug("loaded Global extension library %s",path.get());
925 catch (SAMLException& e) {
926 const XMLCh* fatal=exts->getAttributeNS(NULL,SHIBT_L(fatal));
927 if (fatal && (*fatal==chLatin_t || *fatal==chDigit_1)) {
928 log.fatal("unable to load mandatory Global extension library %s: %s", path.get(), e.what());
932 log.crit("unable to load optional Global extension library %s: %s", path.get(), e.what());
934 exts=saml::XML::getNextSiblingElement(exts,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Library));
939 if (conf.isEnabled(SPConfig::InProcess)) {
940 exts=saml::XML::getFirstChildElement(SHIRE,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Extensions));
942 exts=saml::XML::getFirstChildElement(exts,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Library));
944 auto_ptr_char path(exts->getAttributeNS(NULL,SHIBT_L(path)));
946 SAMLConfig::getConfig().saml_register_extension(path.get(),exts);
947 log.debug("loaded Local extension library %s",path.get());
949 catch (SAMLException& e) {
950 const XMLCh* fatal=exts->getAttributeNS(NULL,SHIBT_L(fatal));
951 if (fatal && (*fatal==chLatin_t || *fatal==chDigit_1)) {
952 log.fatal("unable to load mandatory Local extension library %s: %s", path.get(), e.what());
956 log.crit("unable to load optional Local extension library %s: %s", path.get(), e.what());
958 exts=saml::XML::getNextSiblingElement(exts,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Library));
963 // Instantiate the ListenerService and SessionCache objects.
964 if (conf.isEnabled(SPConfig::Listener)) {
965 exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(UnixListener));
967 log.info("building Listener of type %s...",UNIX_LISTENER_SERVICE);
968 m_outer->m_listener=conf.ListenerServiceManager.newPlugin(UNIX_LISTENER_SERVICE,exts);
971 exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(TCPListener));
973 log.info("building Listener of type %s...",TCP_LISTENER_SERVICE);
974 m_outer->m_listener=conf.ListenerServiceManager.newPlugin(TCP_LISTENER_SERVICE,exts);
977 exts=saml::XML::getFirstChildElement(SHAR,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Listener));
979 auto_ptr_char type(exts->getAttributeNS(NULL,SHIBT_L(type)));
980 log.info("building Listener of type %s...",type.get());
981 m_outer->m_listener=conf.ListenerServiceManager.newPlugin(type.get(),exts);
984 log.fatal("can't build Listener object, missing conf:Listener element?");
985 throw ConfigurationException("can't build Listener object, missing conf:Listener element?");
991 if (conf.isEnabled(SPConfig::Caching)) {
992 IPlugIn* plugin=NULL;
993 const DOMElement* container=conf.isEnabled(SPConfig::OutOfProcess) ? SHAR : SHIRE;
994 exts=saml::XML::getFirstChildElement(container,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(MemorySessionCache));
996 log.info("building Session Cache of type %s...",shibtarget::XML::MemorySessionCacheType);
997 plugin=shibConf.getPlugMgr().newPlugin(shibtarget::XML::MemorySessionCacheType,exts);
1000 exts=saml::XML::getFirstChildElement(container,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(ODBCSessionCache));
1002 log.info("building Session Cache of type %s...",shibtarget::XML::ODBCSessionCacheType);
1003 plugin=shibConf.getPlugMgr().newPlugin(shibtarget::XML::ODBCSessionCacheType,exts);
1006 exts=saml::XML::getFirstChildElement(container,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(MySQLSessionCache));
1008 log.info("building Session Cache of type %s...",shibtarget::XML::MySQLSessionCacheType);
1009 plugin=shibConf.getPlugMgr().newPlugin(shibtarget::XML::MySQLSessionCacheType,exts);
1012 exts=saml::XML::getFirstChildElement(container,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(SessionCache));
1014 auto_ptr_char type(exts->getAttributeNS(NULL,SHIBT_L(type)));
1015 log.info("building Session Cache of type %s...",type.get());
1016 plugin=shibConf.getPlugMgr().newPlugin(type.get(),exts);
1019 log.info("session cache not specified, building Session Cache of type %s...",shibtarget::XML::MemorySessionCacheType);
1020 plugin=shibConf.getPlugMgr().newPlugin(shibtarget::XML::MemorySessionCacheType,exts);
1026 ISessionCache* cache=dynamic_cast<ISessionCache*>(plugin);
1028 m_outer->m_sessionCache=cache;
1031 log.fatal("plugin was not a Session Cache object");
1032 throw UnsupportedExtensionException("plugin was not a Session Cache object");
1037 container=conf.isEnabled(SPConfig::OutOfProcess) ? SHAR : SHIRE;
1038 exts=saml::XML::getFirstChildElement(container,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(ODBCReplayCache));
1040 log.info("building Replay Cache of type %s...",shibtarget::XML::ODBCReplayCacheType);
1041 m_outer->m_replayCache=IReplayCache::getInstance(shibtarget::XML::ODBCReplayCacheType,exts);
1044 exts=saml::XML::getFirstChildElement(container,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(MySQLReplayCache));
1046 log.info("building Replay Cache of type %s...",shibtarget::XML::MySQLReplayCacheType);
1047 m_outer->m_replayCache=IReplayCache::getInstance(shibtarget::XML::MySQLReplayCacheType,exts);
1050 exts=saml::XML::getFirstChildElement(container,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(ReplayCache));
1052 auto_ptr_char type(exts->getAttributeNS(NULL,SHIBT_L(type)));
1053 log.info("building Replay Cache of type %s...",type.get());
1054 m_outer->m_replayCache=IReplayCache::getInstance(type.get(),exts);
1057 // OpenSAML default provider.
1058 log.info("building default Replay Cache...");
1059 m_outer->m_replayCache=IReplayCache::getInstance();
1066 // Back to the fully dynamic stuff...next up is the Request Mapper.
1067 if (conf.isEnabled(SPConfig::RequestMapper)) {
1068 const DOMElement* child=saml::XML::getFirstChildElement(SHIRE,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(RequestMapProvider));
1070 auto_ptr_char type(child->getAttributeNS(NULL,SHIBT_L(type)));
1071 log.info("building Request Mapper of type %s...",type.get());
1072 IPlugIn* plugin=shibConf.getPlugMgr().newPlugin(type.get(),child);
1074 IRequestMapper* reqmap=dynamic_cast<IRequestMapper*>(plugin);
1076 m_requestMapper=reqmap;
1079 log.fatal("plugin was not a Request Mapper object");
1080 throw UnsupportedExtensionException("plugin was not a Request Mapper object");
1085 log.fatal("can't build Request Mapper object, missing conf:RequestMapProvider element?");
1086 throw ConfigurationException("can't build Request Mapper object, missing conf:RequestMapProvider element?");
1090 // Now we load any credentials providers.
1092 if (conf.isEnabled(SPConfig::Credentials)) {
1093 nlist=ReloadableXMLFileImpl::m_root->getElementsByTagNameNS(shibtarget::XML::SHIBTARGET_NS,SHIBT_L(CredentialsProvider));
1094 for (unsigned int i=0; nlist && i<nlist->getLength(); i++) {
1095 auto_ptr_char type(static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,SHIBT_L(type)));
1096 log.info("building credentials provider of type %s...",type.get());
1098 IPlugIn* plugin=shibConf.getPlugMgr().newPlugin(type.get(),static_cast<DOMElement*>(nlist->item(i)));
1100 ICredentials* creds=dynamic_cast<ICredentials*>(plugin);
1102 m_creds.push_back(creds);
1105 log.crit("plugin was not a credentials provider");
1109 catch (SAMLException& ex) {
1110 log.crit("error building credentials provider: %s",ex.what());
1115 // Now we load any attribute factories
1116 nlist=ReloadableXMLFileImpl::m_root->getElementsByTagNameNS(shibtarget::XML::SHIBTARGET_NS,SHIBT_L(AttributeFactory));
1117 for (unsigned int i=0; nlist && i<nlist->getLength(); i++) {
1118 auto_ptr_char type(static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,SHIBT_L(type)));
1119 log.info("building Attribute factory of type %s...",type.get());
1121 IPlugIn* plugin=shibConf.getPlugMgr().newPlugin(type.get(),static_cast<DOMElement*>(nlist->item(i)));
1123 IAttributeFactory* fact=dynamic_cast<IAttributeFactory*>(plugin);
1125 m_attrFactories.push_back(fact);
1126 ShibConfig::getConfig().regAttributeMapping(
1127 static_cast<DOMElement*>(nlist->item(i))->getAttributeNS(NULL,L(AttributeName)),
1133 log.crit("plugin was not an Attribute factory");
1137 catch (SAMLException& ex) {
1138 log.crit("error building Attribute factory: %s",ex.what());
1142 // Load the default application. This actually has a fixed ID of "default". ;-)
1143 const DOMElement* app=saml::XML::getFirstChildElement(
1144 ReloadableXMLFileImpl::m_root,shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Applications)
1147 log.fatal("can't build default Application object, missing conf:Applications element?");
1148 throw ConfigurationException("can't build default Application object, missing conf:Applications element?");
1150 XMLApplication* defapp=new XMLApplication(m_outer, m_creds, app);
1151 m_appmap[defapp->getId()]=defapp;
1153 // Load any overrides.
1154 nlist=app->getElementsByTagNameNS(shibtarget::XML::SHIBTARGET_NS,SHIBT_L(Application));
1155 for (unsigned int j=0; nlist && j<nlist->getLength(); j++) {
1156 auto_ptr<XMLApplication> iapp(new XMLApplication(m_outer,m_creds,static_cast<DOMElement*>(nlist->item(j)),defapp));
1157 if (m_appmap.find(iapp->getId())!=m_appmap.end())
1158 log.crit("found conf:Application element with duplicate Id attribute, ignoring it");
1160 m_appmap[iapp->getId()]=iapp.release();
1163 catch (xmltooling::XMLToolingException& e) {
1164 log.errorStream() << "Error while loading SP configuration: " << e.what() << CategoryStream::ENDLINE;
1165 throw ConfigurationException(e.what());
1167 catch (SAMLException& e) {
1168 log.errorStream() << "Error while loading SP configuration: " << e.what() << CategoryStream::ENDLINE;
1169 throw ConfigurationException(e.what());
1173 log.error("Unexpected error while loading SP configuration");
1179 XMLConfigImpl::~XMLConfigImpl()
1181 delete m_requestMapper;
1182 for_each(m_appmap.begin(),m_appmap.end(),xmltooling::cleanup_pair<string,IApplication>());
1183 for_each(m_creds.begin(),m_creds.end(),xmltooling::cleanup<ICredentials>());
1184 ShibConfig::getConfig().clearAttributeMappings();
1185 for_each(m_attrFactories.begin(),m_attrFactories.end(),xmltooling::cleanup<IAttributeFactory>());