boost changes and header fixes
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / MetadataProvider.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * MetadataProvider.cpp
23  *
24  * Supplies an individual source of metadata.
25  */
26
27 #include "internal.h"
28 #include "saml2/metadata/MetadataFilter.h"
29 #include "saml2/metadata/MetadataProvider.h"
30
31 #include <algorithm>
32 #include <boost/lambda/lambda.hpp>
33 #include <xercesc/util/XMLUniDefs.hpp>
34 #include <xmltooling/logging.h>
35 #include <xmltooling/unicode.h>
36 #include <xmltooling/util/NDC.h>
37 #include <xmltooling/util/XMLHelper.h>
38
39 using namespace opensaml::saml2md;
40 using namespace opensaml;
41 using namespace xmltooling::logging;
42 using namespace xmltooling;
43 using namespace boost::lambda;
44 using namespace boost;
45 using namespace std;
46
47 namespace opensaml {
48     namespace saml2md {
49         SAML_DLLLOCAL PluginManager<MetadataProvider,string,const DOMElement*>::Factory XMLMetadataProviderFactory;
50         SAML_DLLLOCAL PluginManager<MetadataProvider,string,const DOMElement*>::Factory DynamicMetadataProviderFactory;
51         SAML_DLLLOCAL PluginManager<MetadataProvider,string,const DOMElement*>::Factory ChainingMetadataProviderFactory;
52         SAML_DLLLOCAL PluginManager<MetadataProvider,string,const DOMElement*>::Factory FolderMetadataProviderFactory;
53         SAML_DLLLOCAL PluginManager<MetadataProvider,string,const DOMElement*>::Factory NullMetadataProviderFactory;
54         SAML_DLLLOCAL PluginManager<MetadataFilter,string,const DOMElement*>::Factory BlacklistMetadataFilterFactory;
55         SAML_DLLLOCAL PluginManager<MetadataFilter,string,const DOMElement*>::Factory WhitelistMetadataFilterFactory;
56         SAML_DLLLOCAL PluginManager<MetadataFilter,string,const DOMElement*>::Factory SignatureMetadataFilterFactory;
57         SAML_DLLLOCAL PluginManager<MetadataFilter,string,const DOMElement*>::Factory RequireValidUntilMetadataFilterFactory;
58         SAML_DLLLOCAL PluginManager<MetadataFilter,string,const DOMElement*>::Factory EntityRoleMetadataFilterFactory;
59     };
60 };
61
62 void SAML_API opensaml::saml2md::registerMetadataProviders()
63 {
64     SAMLConfig& conf=SAMLConfig::getConfig();
65     conf.MetadataProviderManager.registerFactory(XML_METADATA_PROVIDER, XMLMetadataProviderFactory);
66     conf.MetadataProviderManager.registerFactory(DYNAMIC_METADATA_PROVIDER, DynamicMetadataProviderFactory);
67     conf.MetadataProviderManager.registerFactory(CHAINING_METADATA_PROVIDER, ChainingMetadataProviderFactory);
68     conf.MetadataProviderManager.registerFactory(FOLDER_METADATA_PROVIDER, FolderMetadataProviderFactory);
69     conf.MetadataProviderManager.registerFactory(NULL_METADATA_PROVIDER, NullMetadataProviderFactory);
70 }
71
72 void SAML_API opensaml::saml2md::registerMetadataFilters()
73 {
74     SAMLConfig::getConfig().MetadataFilterManager.registerFactory(BLACKLIST_METADATA_FILTER, BlacklistMetadataFilterFactory);
75     SAMLConfig::getConfig().MetadataFilterManager.registerFactory(WHITELIST_METADATA_FILTER, WhitelistMetadataFilterFactory);
76     SAMLConfig::getConfig().MetadataFilterManager.registerFactory(SIGNATURE_METADATA_FILTER, SignatureMetadataFilterFactory);
77     SAMLConfig::getConfig().MetadataFilterManager.registerFactory(REQUIREVALIDUNTIL_METADATA_FILTER, RequireValidUntilMetadataFilterFactory);
78     // additional name matching Java code
79     SAMLConfig::getConfig().MetadataFilterManager.registerFactory("RequiredValidUntil", RequireValidUntilMetadataFilterFactory);
80     SAMLConfig::getConfig().MetadataFilterManager.registerFactory(ENTITYROLE_METADATA_FILTER, EntityRoleMetadataFilterFactory);
81 }
82
83 static const XMLCh _MetadataFilter[] =  UNICODE_LITERAL_14(M,e,t,a,d,a,t,a,F,i,l,t,e,r);
84 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);
85 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);
86 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);
87 static const XMLCh Exclude[] =          UNICODE_LITERAL_7(E,x,c,l,u,d,e);
88 static const XMLCh Include[] =          UNICODE_LITERAL_7(I,n,c,l,u,d,e);
89 static const XMLCh type[] =             UNICODE_LITERAL_4(t,y,p,e);
90
91 MetadataProvider::MetadataProvider(const DOMElement* e)
92 {
93 #ifdef _DEBUG
94     NDC ndc("MetadataProvider");
95 #endif
96     Category& log = Category::getInstance(SAML_LOGCAT".Metadata");
97     SAMLConfig& conf = SAMLConfig::getConfig();
98
99     // Locate any default recognized filters and plugins.
100     try {
101         DOMElement* child = XMLHelper::getFirstChildElement(e);
102         while (child) {
103             if (XMLString::equals(child->getLocalName(), _MetadataFilter)) {
104                 string t = XMLHelper::getAttrString(child, nullptr, type);
105                 if (!t.empty()) {
106                     log.info("building MetadataFilter of type %s", t.c_str());
107                     m_filters.push_back(conf.MetadataFilterManager.newPlugin(t.c_str(), child));
108                 }
109             }
110             else if (XMLString::equals(child->getLocalName(), SigFilter)) {
111                 log.info("building MetadataFilter of type %s", SIGNATURE_METADATA_FILTER);
112                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(SIGNATURE_METADATA_FILTER, child));
113             }
114             else if (XMLString::equals(child->getLocalName(), Whitelist)) {
115                 log.info("building MetadataFilter of type %s", WHITELIST_METADATA_FILTER);
116                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(WHITELIST_METADATA_FILTER, child));
117             }
118             else if (XMLString::equals(child->getLocalName(), Blacklist)) {
119                 log.info("building MetadataFilter of type %s", BLACKLIST_METADATA_FILTER);
120                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(BLACKLIST_METADATA_FILTER, child));
121             }
122             else if (XMLString::equals(child->getLocalName(), Include)) {
123                 log.info("building MetadataFilter of type %s", WHITELIST_METADATA_FILTER);
124                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(WHITELIST_METADATA_FILTER, e));
125             }
126             else if (XMLString::equals(child->getLocalName(), Exclude)) {
127                 log.info("building MetadataFilter of type %s", BLACKLIST_METADATA_FILTER);
128                 m_filters.push_back(conf.MetadataFilterManager.newPlugin(BLACKLIST_METADATA_FILTER, e));
129             }
130             child = XMLHelper::getNextSiblingElement(child);
131         }
132     }
133     catch (XMLToolingException& ex) {
134         log.error("caught exception while installing filters: %s", ex.what());
135         throw;
136     }
137 }
138
139 MetadataProvider::~MetadataProvider()
140 {
141 }
142
143 const char* MetadataProvider::getId() const
144 {
145     return nullptr;
146 }
147
148 void MetadataProvider::addMetadataFilter(MetadataFilter* newFilter)
149 {
150     m_filters.push_back(newFilter);
151 }
152
153 MetadataFilter* MetadataProvider::removeMetadataFilter(MetadataFilter* oldFilter)
154 {
155     ptr_vector<MetadataFilter>::iterator i = find_if(m_filters.begin(), m_filters.end(), (&_1 == oldFilter));
156     if (i != m_filters.end()) {
157         return m_filters.release(i).release();
158     }
159     return nullptr;
160 }
161
162 void MetadataProvider::doFilters(XMLObject& xmlObject) const
163 {
164 #ifdef _DEBUG
165     NDC ndc("doFilters");
166 #endif
167     Category& log=Category::getInstance(SAML_LOGCAT".Metadata");
168     for (ptr_vector<MetadataFilter>::const_iterator i = m_filters.begin(); i != m_filters.end(); i++) {
169         log.info("applying metadata filter (%s)", i->getId());
170         i->doFilter(xmlObject);
171     }
172 }
173
174 void MetadataProvider::outputStatus(ostream& os) const
175 {
176 }
177
178 const EntitiesDescriptor* MetadataProvider::getEntitiesDescriptor(const XMLCh* name, bool strict) const
179 {
180     auto_ptr_char temp(name);
181     return getEntitiesDescriptor(temp.get(),strict);
182 }
183
184 MetadataProvider::Criteria::Criteria()
185     : entityID_unicode(nullptr), entityID_ascii(nullptr), artifact(nullptr), role(nullptr), protocol(nullptr), protocol2(nullptr), validOnly(true)
186 {
187 }
188
189 MetadataProvider::Criteria::Criteria(const XMLCh* id, const xmltooling::QName* q, const XMLCh* prot, bool valid)
190     : entityID_unicode(id), entityID_ascii(nullptr), artifact(nullptr), role(q), protocol(prot), protocol2(nullptr), validOnly(valid)
191 {
192 }
193
194 MetadataProvider::Criteria::Criteria(const char* id, const xmltooling::QName* q, const XMLCh* prot, bool valid)
195     : entityID_unicode(nullptr), entityID_ascii(id), artifact(nullptr), role(q), protocol(prot), protocol2(nullptr), validOnly(valid)
196 {
197 }
198
199 MetadataProvider::Criteria::Criteria(const SAMLArtifact* a, const xmltooling::QName* q, const XMLCh* prot, bool valid)
200     : entityID_unicode(nullptr), entityID_ascii(nullptr), artifact(a), role(q), protocol(prot), protocol2(nullptr), validOnly(valid)
201 {
202 }
203
204 MetadataProvider::Criteria::~Criteria()
205 {
206 }
207
208 void MetadataProvider::Criteria::reset()
209 {
210     entityID_unicode=nullptr;
211     entityID_ascii=nullptr;
212     artifact=nullptr;
213     role=nullptr;
214     protocol=nullptr;
215     protocol2=nullptr;
216     validOnly=true;
217 }
218
219 MetadataFilter::MetadataFilter()
220 {
221 }
222
223 MetadataFilter::~MetadataFilter()
224 {
225 }