7081e1337c17362de6219b9ee8377cb9cfc0553d
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / BlacklistMetadataFilter.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  * BlacklistMetadataFilter.cpp
23  * 
24  * Removes blacklisted entities from a metadata instance
25  */
26
27 #include "internal.h"
28 #include "saml2/metadata/EntityMatcher.h"
29 #include "saml2/metadata/Metadata.h"
30 #include "saml2/metadata/MetadataFilter.h"
31
32 #include <boost/scoped_ptr.hpp>
33 #include <xmltooling/logging.h>
34
35 using namespace opensaml::saml2md;
36 using namespace opensaml::saml2;
37 using namespace xmltooling::logging;
38 using namespace xmltooling;
39 using namespace boost;
40 using namespace std;
41
42 namespace opensaml {
43     namespace saml2md {
44         class SAML_DLLLOCAL BlacklistMetadataFilter : public MetadataFilter
45         {
46         public:
47             BlacklistMetadataFilter(const DOMElement* e);
48             ~BlacklistMetadataFilter() {}
49             
50             const char* getId() const { return BLACKLIST_METADATA_FILTER; }
51             void doFilter(XMLObject& xmlObject) const;
52
53         private:
54             void filterGroup(EntitiesDescriptor*) const;
55             bool included(const EntityDescriptor&) const;
56
57             set<xstring> m_entities;
58             scoped_ptr<EntityMatcher> m_matcher;
59         }; 
60
61         MetadataFilter* SAML_DLLLOCAL BlacklistMetadataFilterFactory(const DOMElement* const & e)
62         {
63             return new BlacklistMetadataFilter(e);
64         }
65
66         static const XMLCh Exclude[] = UNICODE_LITERAL_7(E,x,c,l,u,d,e);
67         static const XMLCh _matcher[] = UNICODE_LITERAL_7(m,a,t,c,h,e,r);
68     };
69 };
70
71
72 BlacklistMetadataFilter::BlacklistMetadataFilter(const DOMElement* e)
73 {
74     string matcher(XMLHelper::getAttrString(e, nullptr, _matcher));
75     if (!matcher.empty())
76         m_matcher.reset(SAMLConfig::getConfig().EntityMatcherManager.newPlugin(matcher.c_str(), e));
77
78     e = XMLHelper::getFirstChildElement(e, Exclude);
79     while (e) {
80         if (e->hasChildNodes()) {
81             const XMLCh* excl = e->getTextContent();
82             if (excl && *excl)
83                 m_entities.insert(excl);
84         }
85         e = XMLHelper::getNextSiblingElement(e, Exclude);
86     }
87 }
88
89 void BlacklistMetadataFilter::doFilter(XMLObject& xmlObject) const
90 {
91     EntitiesDescriptor* group = dynamic_cast<EntitiesDescriptor*>(&xmlObject);
92     if (group) {
93         if (group->getName() && !m_entities.empty() && m_entities.count(group->getName()) > 0)
94             throw MetadataFilterException(BLACKLIST_METADATA_FILTER " MetadataFilter instructed to filter the root group in the metadata.");
95         filterGroup(group);
96     }
97     else {
98         EntityDescriptor* entity = dynamic_cast<EntityDescriptor*>(&xmlObject);
99         if (entity) {
100             if (included(*entity))
101                 throw MetadataFilterException(BLACKLIST_METADATA_FILTER " MetadataFilter instructed to filter the root/only entity in the metadata.");
102         }
103         else {
104             throw MetadataFilterException(BLACKLIST_METADATA_FILTER " MetadataFilter was given an improper metadata instance to filter.");
105         }
106     }
107 }
108
109 void BlacklistMetadataFilter::filterGroup(EntitiesDescriptor* entities) const
110 {
111     Category& log = Category::getInstance(SAML_LOGCAT ".MetadataFilter." WHITELIST_METADATA_FILTER);
112
113     VectorOf(EntityDescriptor) v = entities->getEntityDescriptors();
114     for (VectorOf(EntityDescriptor)::size_type i = 0; i < v.size(); ) {
115         if (included(*v[i])) {
116             auto_ptr_char id(v[i]->getEntityID());
117             log.info("filtering out blacklisted entity (%s)", id.get());
118             v.erase(v.begin() + i);
119         }
120         else {
121             i++;
122         }
123     }
124
125     VectorOf(EntitiesDescriptor) w = entities->getEntitiesDescriptors();
126     for (VectorOf(EntitiesDescriptor)::size_type j = 0; j < w.size(); ) {
127         const XMLCh* name = w[j]->getName();
128         if (name && !m_entities.empty() && m_entities.count(name) > 0) {
129             auto_ptr_char name2(name);
130             log.info("filtering out blacklisted group (%s)", name2.get());
131             w.erase(w.begin() + j);
132         }
133         else {
134             filterGroup(w[j]);
135             j++;
136         }
137     }
138 }
139
140 bool BlacklistMetadataFilter::included(const EntityDescriptor& entity) const
141 {
142     // Check for entityID.
143     if (entity.getEntityID() && !m_entities.empty() && m_entities.count(entity.getEntityID()) > 0)
144         return true;
145
146     if (m_matcher && m_matcher->matches(entity))
147         return true;
148
149     return false;
150 }