Convert from NULL macro to nullptr, remove unused zlib code.
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / MetadataProvider.cpp
1 /*
2  *  Copyright 2001-2010 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  * Supplies an individual source of metadata.
21  */
22
23 #include "internal.h"
24 #include "saml2/metadata/MetadataFilter.h"
25 #include "saml2/metadata/MetadataProvider.h"
26
27 #include <algorithm>
28 #include <xercesc/util/XMLUniDefs.hpp>
29 #include <xmltooling/logging.h>
30 #include <xmltooling/unicode.h>
31 #include <xmltooling/util/NDC.h>
32 #include <xmltooling/util/XMLHelper.h>
33
34 using namespace opensaml::saml2md;
35 using namespace opensaml;
36 using namespace xmltooling::logging;
37 using namespace xmltooling;
38 using namespace std;
39
40 namespace opensaml {
41     namespace saml2md {
42         SAML_DLLLOCAL PluginManager<MetadataProvider,string,const DOMElement*>::Factory XMLMetadataProviderFactory;
43         SAML_DLLLOCAL PluginManager<MetadataProvider,string,const DOMElement*>::Factory DynamicMetadataProviderFactory;
44         SAML_DLLLOCAL PluginManager<MetadataProvider,string,const DOMElement*>::Factory ChainingMetadataProviderFactory;
45         SAML_DLLLOCAL PluginManager<MetadataProvider,string,const DOMElement*>::Factory NullMetadataProviderFactory;
46         SAML_DLLLOCAL PluginManager<MetadataFilter,string,const DOMElement*>::Factory BlacklistMetadataFilterFactory;
47         SAML_DLLLOCAL PluginManager<MetadataFilter,string,const DOMElement*>::Factory WhitelistMetadataFilterFactory;
48         SAML_DLLLOCAL PluginManager<MetadataFilter,string,const DOMElement*>::Factory SignatureMetadataFilterFactory;
49         SAML_DLLLOCAL PluginManager<MetadataFilter,string,const DOMElement*>::Factory RequireValidUntilMetadataFilterFactory;
50         SAML_DLLLOCAL PluginManager<MetadataFilter,string,const DOMElement*>::Factory EntityRoleMetadataFilterFactory;
51     };
52 };
53
54 void SAML_API opensaml::saml2md::registerMetadataProviders()
55 {
56     SAMLConfig& conf=SAMLConfig::getConfig();
57     conf.MetadataProviderManager.registerFactory(XML_METADATA_PROVIDER, XMLMetadataProviderFactory);
58     conf.MetadataProviderManager.registerFactory(DYNAMIC_METADATA_PROVIDER, DynamicMetadataProviderFactory);
59     conf.MetadataProviderManager.registerFactory(CHAINING_METADATA_PROVIDER, ChainingMetadataProviderFactory);
60     conf.MetadataProviderManager.registerFactory(NULL_METADATA_PROVIDER, NullMetadataProviderFactory);
61 }
62
63 void SAML_API opensaml::saml2md::registerMetadataFilters()
64 {
65     SAMLConfig::getConfig().MetadataFilterManager.registerFactory(BLACKLIST_METADATA_FILTER, BlacklistMetadataFilterFactory);
66     SAMLConfig::getConfig().MetadataFilterManager.registerFactory(WHITELIST_METADATA_FILTER, WhitelistMetadataFilterFactory);
67     SAMLConfig::getConfig().MetadataFilterManager.registerFactory(SIGNATURE_METADATA_FILTER, SignatureMetadataFilterFactory);
68     SAMLConfig::getConfig().MetadataFilterManager.registerFactory(REQUIREVALIDUNTIL_METADATA_FILTER, RequireValidUntilMetadataFilterFactory);
69     // additional name matching Java code
70     SAMLConfig::getConfig().MetadataFilterManager.registerFactory("RequiredValidUntil", RequireValidUntilMetadataFilterFactory);
71     SAMLConfig::getConfig().MetadataFilterManager.registerFactory(ENTITYROLE_METADATA_FILTER, EntityRoleMetadataFilterFactory);
72 }
73
74 static const XMLCh _MetadataFilter[] =  UNICODE_LITERAL_14(M,e,t,a,d,a,t,a,F,i,l,t,e,r);
75 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);
76 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);
77 static const XMLCh SigFilter[] =        UNICODE_LITERAL_23(S,i,g,n,a,t,u,r,e,M,e,t,a,d,a,t,a,F,i,l,t,e,r);
78 static const XMLCh Exclude[] =          UNICODE_LITERAL_7(E,x,c,l,u,d,e);
79 static const XMLCh Include[] =          UNICODE_LITERAL_7(I,n,c,l,u,d,e);
80 static const XMLCh type[] =             UNICODE_LITERAL_4(t,y,p,e);
81
82 MetadataProvider::MetadataProvider(const DOMElement* e)
83 {
84 #ifdef _DEBUG
85     NDC ndc("MetadataProvider");
86 #endif
87     Category& log = Category::getInstance(SAML_LOGCAT".Metadata");
88     SAMLConfig& conf=SAMLConfig::getConfig();
89
90     // Locate any default recognized filters and plugins.
91     try {
92         DOMElement* child = e ? XMLHelper::getFirstChildElement(e) : nullptr;
93         while (child) {
94             if (XMLString::equals(child->getLocalName(),_MetadataFilter)) {
95                 auto_ptr_char t(child->getAttributeNS(nullptr,type));
96                 if (t.get() && *t.get()) {
97                     log.info("building MetadataFilter of type %s", t.get());
98                     m_filters.push_back(conf.MetadataFilterManager.newPlugin(t.get(),child));
99                 }
100             }
101             else if (XMLString::equals(child->getLocalName(),SigFilter)) {
102                 log.info("building MetadataFilter of type %s", SIGNATURE_METADATA_FILTER);
103                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(SIGNATURE_METADATA_FILTER,child));
104             }
105             else if (XMLString::equals(child->getLocalName(),Whitelist)) {
106                 log.info("building MetadataFilter of type %s", WHITELIST_METADATA_FILTER);
107                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(WHITELIST_METADATA_FILTER,child));
108             }
109             else if (XMLString::equals(child->getLocalName(),Blacklist)) {
110                 log.info("building MetadataFilter of type %s", BLACKLIST_METADATA_FILTER);
111                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(BLACKLIST_METADATA_FILTER,child));
112             }
113             else if (XMLString::equals(child->getLocalName(),Include)) {
114                 log.info("building MetadataFilter of type %s", WHITELIST_METADATA_FILTER);
115                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(WHITELIST_METADATA_FILTER,e));
116             }
117             else if (XMLString::equals(child->getLocalName(),Exclude)) {
118                 log.info("building MetadataFilter of type %s", BLACKLIST_METADATA_FILTER);
119                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(BLACKLIST_METADATA_FILTER,e));
120             }
121             child = XMLHelper::getNextSiblingElement(child);
122         }
123     }
124     catch (XMLToolingException& ex) {
125         log.error("caught exception while installing filters: %s", ex.what());
126         for_each(m_filters.begin(),m_filters.end(),xmltooling::cleanup<MetadataFilter>());
127         throw;
128     }
129 }
130
131 MetadataProvider::~MetadataProvider()
132 {
133     for_each(m_filters.begin(),m_filters.end(),xmltooling::cleanup<MetadataFilter>());
134 }
135
136 void MetadataProvider::addMetadataFilter(MetadataFilter* newFilter)
137 {
138     m_filters.push_back(newFilter);
139 }
140
141 MetadataFilter* MetadataProvider::removeMetadataFilter(MetadataFilter* oldFilter)
142 {
143     for (vector<MetadataFilter*>::iterator i=m_filters.begin(); i!=m_filters.end(); i++) {
144         if (oldFilter==(*i)) {
145             m_filters.erase(i);
146             return oldFilter;
147         }
148     }
149     return nullptr;
150 }
151
152 void MetadataProvider::doFilters(XMLObject& xmlObject) const
153 {
154 #ifdef _DEBUG
155     NDC ndc("doFilters");
156 #endif
157     Category& log=Category::getInstance(SAML_LOGCAT".Metadata");
158     for (std::vector<MetadataFilter*>::const_iterator i=m_filters.begin(); i!=m_filters.end(); i++) {
159         log.info("applying metadata filter (%s)", (*i)->getId());
160         (*i)->doFilter(xmlObject);
161     }
162 }
163
164 const EntitiesDescriptor* MetadataProvider::getEntitiesDescriptor(const XMLCh* name, bool strict) const
165 {
166     auto_ptr_char temp(name);
167     return getEntitiesDescriptor(temp.get(),strict);
168 }
169
170 MetadataProvider::Criteria::Criteria()
171     : entityID_unicode(nullptr), entityID_ascii(nullptr), artifact(nullptr), role(nullptr), protocol(nullptr), protocol2(nullptr), validOnly(true)
172 {
173 }
174
175 MetadataProvider::Criteria::Criteria(const XMLCh* id, const xmltooling::QName* q, const XMLCh* prot, bool valid)
176     : entityID_unicode(id), entityID_ascii(nullptr), artifact(nullptr), role(q), protocol(prot), protocol2(nullptr), validOnly(valid)
177 {
178 }
179
180 MetadataProvider::Criteria::Criteria(const char* id, const xmltooling::QName* q, const XMLCh* prot, bool valid)
181     : entityID_unicode(nullptr), entityID_ascii(id), artifact(nullptr), role(q), protocol(prot), protocol2(nullptr), validOnly(valid)
182 {
183 }
184
185 MetadataProvider::Criteria::Criteria(const SAMLArtifact* a, const xmltooling::QName* q, const XMLCh* prot, bool valid)
186     : entityID_unicode(nullptr), entityID_ascii(nullptr), artifact(a), role(q), protocol(prot), protocol2(nullptr), validOnly(valid)
187 {
188 }
189
190 MetadataProvider::Criteria::~Criteria()
191 {
192 }
193
194 void MetadataProvider::Criteria::reset()
195 {
196     entityID_unicode=nullptr;
197     entityID_ascii=nullptr;
198     artifact=nullptr;
199     role=nullptr;
200     protocol=nullptr;
201     protocol2=nullptr;
202     validOnly=true;
203 }
204
205 MetadataFilter::MetadataFilter()
206 {
207 }
208
209 MetadataFilter::~MetadataFilter()
210 {
211 }