Implement metadata lookup by artifact, refactored metadata indexing.
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / MetadataProvider.cpp
1 /*
2  *  Copyright 2001-2006 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  * MetadataProvider.cpp
19  * 
20  * Registration of factories for built-in providers
21  */
22
23 #include "internal.h"
24 #include "SAMLArtifact.h"
25 #include "saml2/metadata/MetadataProvider.h"
26
27 #include <log4cpp/Category.hh>
28 #include <xmltooling/util/NDC.h>
29
30 using namespace opensaml::saml2md;
31 using namespace opensaml;
32 using namespace xmltooling;
33 using namespace log4cpp;
34 using namespace std;
35
36 namespace opensaml {
37     namespace saml2md {
38         SAML_DLLLOCAL PluginManager<MetadataProvider,const DOMElement*>::Factory FilesystemMetadataProviderFactory; 
39         SAML_DLLLOCAL PluginManager<MetadataFilter,const DOMElement*>::Factory BlacklistMetadataFilterFactory; 
40         SAML_DLLLOCAL PluginManager<MetadataFilter,const DOMElement*>::Factory WhitelistMetadataFilterFactory; 
41     };
42 };
43
44 void SAML_API opensaml::saml2md::registerMetadataProviders()
45 {
46     SAMLConfig& conf=SAMLConfig::getConfig();
47     conf.MetadataProviderManager.registerFactory(FILESYSTEM_METADATA_PROVIDER, FilesystemMetadataProviderFactory);
48     conf.MetadataProviderManager.registerFactory("edu.internet2.middleware.shibboleth.metadata.provider.XMLMetadata", FilesystemMetadataProviderFactory);
49     conf.MetadataProviderManager.registerFactory("edu.internet2.middleware.shibboleth.common.provider.XMLMetadata", FilesystemMetadataProviderFactory);
50 }
51
52 void SAML_API opensaml::saml2md::registerMetadataFilters()
53 {
54     SAMLConfig::getConfig().MetadataFilterManager.registerFactory(BLACKLIST_METADATA_FILTER, BlacklistMetadataFilterFactory);
55     SAMLConfig::getConfig().MetadataFilterManager.registerFactory(WHITELIST_METADATA_FILTER, WhitelistMetadataFilterFactory);
56 }
57
58 static const XMLCh Blacklist[] =                    UNICODE_LITERAL_23(B,l,a,c,k,l,i,s,t,M,e,t,a,d,a,t,a,F,i,l,t,e,r);
59 static const XMLCh Exclude[] =                      UNICODE_LITERAL_7(E,x,c,l,u,d,e);
60 static const XMLCh Include[] =                      UNICODE_LITERAL_7(I,n,c,l,u,d,e);
61 static const XMLCh GenericMetadataFilter[] =        UNICODE_LITERAL_14(M,e,t,a,d,a,t,a,F,i,l,t,e,r);
62 static const XMLCh type[] =                         UNICODE_LITERAL_4(t,y,p,e);
63 static const XMLCh Whitelist[] =                    UNICODE_LITERAL_23(W,h,i,t,e,l,i,s,t,M,e,t,a,d,a,t,a,F,i,l,t,e,r);
64
65 MetadataProvider::MetadataProvider(const DOMElement* e)
66 {
67 #ifdef _DEBUG
68     NDC ndc("MetadataProvider");
69 #endif
70     SAMLConfig& conf=SAMLConfig::getConfig();
71     
72     // Locate any default recognized filters.
73     try {
74         DOMElement* child = e ? XMLHelper::getFirstChildElement(e) : NULL;
75         while (child) {
76             if (XMLString::equals(child->getLocalName(),GenericMetadataFilter)) {
77                 auto_ptr_char t(child->getAttributeNS(NULL,type));
78                 if (t.get())
79                     m_filters.push_back(conf.MetadataFilterManager.newPlugin(t.get(),child));
80             }
81             else if (XMLString::equals(child->getLocalName(),Whitelist)) {
82                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(WHITELIST_METADATA_FILTER,child));
83             }
84             else if (XMLString::equals(child->getLocalName(),Blacklist)) {
85                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(BLACKLIST_METADATA_FILTER,child));
86             }
87             else if (XMLString::equals(child->getLocalName(),Include)) {
88                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(WHITELIST_METADATA_FILTER,e));
89             }
90             else if (XMLString::equals(child->getLocalName(),Exclude)) {
91                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(BLACKLIST_METADATA_FILTER,e));
92             }
93             child = XMLHelper::getNextSiblingElement(child);
94         }
95     }
96     catch (XMLToolingException& ex) {
97         Category::getInstance(SAML_LOGCAT".Metadata").error("caught exception while installing filters: %s", ex.what());
98         for_each(m_filters.begin(),m_filters.end(),xmltooling::cleanup<MetadataFilter>());
99         throw;
100     }
101 }
102
103 MetadataProvider::~MetadataProvider()
104 {
105     for_each(m_filters.begin(),m_filters.end(),xmltooling::cleanup<MetadataFilter>());
106 }
107
108 void MetadataProvider::doFilters(XMLObject& xmlObject) const
109 {
110 #ifdef _DEBUG
111     NDC ndc("doFilters");
112 #endif
113     Category& log=Category::getInstance(SAML_LOGCAT".Metadata");
114     for (std::vector<MetadataFilter*>::const_iterator i=m_filters.begin(); i!=m_filters.end(); i++) {
115         log.info("applying metadata filter (%s)", (*i)->getId());
116         (*i)->doFilter(xmlObject);
117     }
118 }
119
120 void MetadataProvider::index(EntityDescriptor* site, time_t validUntil)
121 {
122     if (validUntil < site->getValidUntilEpoch())
123         site->setValidUntil(validUntil);
124
125     auto_ptr_char id(site->getEntityID());
126     if (id.get()) {
127         m_sites.insert(make_pair(id.get(),site));
128     }
129     
130     // Process each IdP role.
131     const vector<IDPSSODescriptor*>& roles=const_cast<const EntityDescriptor*>(site)->getIDPSSODescriptors();
132     for (vector<IDPSSODescriptor*>::const_iterator i=roles.begin(); i!=roles.end(); i++) {
133         // SAML 1.x?
134         if ((*i)->hasSupport(SAMLConstants::SAML10_PROTOCOL_ENUM) || (*i)->hasSupport(SAMLConstants::SAML11_PROTOCOL_ENUM)) {
135             // Check for SourceID extension element.
136             const Extensions* exts=(*i)->getExtensions();
137             if (exts) {
138                 const list<XMLObject*>& children=exts->getXMLObjects();
139                 for (list<XMLObject*>::const_iterator ext=children.begin(); ext!=children.end(); ext++) {
140                     SourceID* sid=dynamic_cast<SourceID*>(*ext);
141                     if (sid) {
142                         auto_ptr_char sourceid(sid->getID());
143                         if (sourceid.get()) {
144                             m_sources.insert(pair<string,const EntityDescriptor*>(sourceid.get(),site));
145                             break;
146                         }
147                     }
148                 }
149             }
150             
151             // Hash the ID.
152             m_sources.insert(
153                 pair<string,const EntityDescriptor*>(SAMLConfig::getConfig().hashSHA1(id.get(), true),site)
154                 );
155                 
156             // Load endpoints for type 0x0002 artifacts.
157             const vector<ArtifactResolutionService*>& locs=const_cast<const IDPSSODescriptor*>(*i)->getArtifactResolutionServices();
158             for (vector<ArtifactResolutionService*>::const_iterator loc=locs.begin(); loc!=locs.end(); loc++) {
159                 auto_ptr_char location((*loc)->getLocation());
160                 if (location.get())
161                     m_sources.insert(pair<string,const EntityDescriptor*>(location.get(),site));
162             }
163         }
164         
165         // SAML 2.0?
166         if ((*i)->hasSupport(SAMLConstants::SAML20P_NS)) {
167             // Hash the ID.
168             m_sources.insert(
169                 pair<string,const EntityDescriptor*>(SAMLConfig::getConfig().hashSHA1(id.get(), true),site)
170                 );
171         }
172     }
173 }
174
175 void MetadataProvider::index(EntitiesDescriptor* group, time_t validUntil)
176 {
177     if (validUntil < group->getValidUntilEpoch())
178         group->setValidUntil(validUntil);
179
180     auto_ptr_char name(group->getName());
181     if (name.get()) {
182         m_groups.insert(make_pair(name.get(),group));
183     }
184     
185     const vector<EntitiesDescriptor*>& groups=const_cast<const EntitiesDescriptor*>(group)->getEntitiesDescriptors();
186     for (vector<EntitiesDescriptor*>::const_iterator i=groups.begin(); i!=groups.end(); i++)
187         index(*i,group->getValidUntilEpoch());
188
189     const vector<EntityDescriptor*>& sites=const_cast<const EntitiesDescriptor*>(group)->getEntityDescriptors();
190     for (vector<EntityDescriptor*>::const_iterator j=sites.begin(); j!=sites.end(); j++)
191         index(*j,group->getValidUntilEpoch());
192 }
193
194 void MetadataProvider::clearIndex()
195 {
196     m_sources.clear();
197     m_sites.clear();
198     m_groups.clear();
199 }
200
201 const EntitiesDescriptor* MetadataProvider::getEntitiesDescriptor(const char* name, bool strict) const
202 {
203     pair<groupmap_t::const_iterator,groupmap_t::const_iterator> range=m_groups.equal_range(name);
204
205     time_t now=time(NULL);
206     for (groupmap_t::const_iterator i=range.first; i!=range.second; i++)
207         if (now < i->second->getValidUntilEpoch())
208             return i->second;
209     
210     if (!strict && range.first!=range.second)
211         return range.first->second;
212         
213     return NULL;
214 }
215
216 const EntitiesDescriptor* MetadataProvider::getEntitiesDescriptor(const XMLCh* name, bool strict) const
217 {
218     auto_ptr_char temp(name);
219     return getEntitiesDescriptor(temp.get(),strict);
220 }
221
222 const EntityDescriptor* MetadataProvider::getEntityDescriptor(const char* name, bool strict) const
223 {
224     pair<sitemap_t::const_iterator,sitemap_t::const_iterator> range=m_sites.equal_range(name);
225
226     time_t now=time(NULL);
227     for (sitemap_t::const_iterator i=range.first; i!=range.second; i++)
228         if (now < i->second->getValidUntilEpoch())
229             return i->second;
230     
231     if (!strict && range.first!=range.second)
232         return range.first->second;
233         
234     return NULL;
235 }
236
237 const EntityDescriptor* MetadataProvider::getEntityDescriptor(const XMLCh* name, bool strict) const
238 {
239     auto_ptr_char temp(name);
240     return getEntityDescriptor(temp.get(),strict);
241 }
242
243 const EntityDescriptor* MetadataProvider::getEntityDescriptor(const SAMLArtifact* artifact) const
244 {
245     pair<sitemap_t::const_iterator,sitemap_t::const_iterator> range=m_sources.equal_range(artifact->getSource());
246
247     time_t now=time(NULL);
248     for (sitemap_t::const_iterator i=range.first; i!=range.second; i++)
249         if (now < i->second->getValidUntilEpoch())
250             return i->second;
251
252     return NULL;
253 }