Small adjustments to filters
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / WhitelistMetadataFilter.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  * WhitelistMetadataFilter.cpp
23  *
24  * Removes non-whitelisted entities from a metadata instance
25  */
26
27 #include "internal.h"
28 #include "saml2/metadata/Metadata.h"
29 #include "saml2/metadata/MetadataFilter.h"
30
31 #include <boost/bind.hpp>
32 #include <boost/iterator/indirect_iterator.hpp>
33 #include <xmltooling/logging.h>
34 #include <xmltooling/util/NDC.h>
35
36 using namespace opensaml::saml2md;
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
45         class SAML_DLLLOCAL WhitelistMetadataFilter : public MetadataFilter
46         {
47         public:
48             WhitelistMetadataFilter(const DOMElement* e);
49             ~WhitelistMetadataFilter() {}
50
51             const char* getId() const { return WHITELIST_METADATA_FILTER; }
52             void doFilter(XMLObject& xmlObject) const;
53
54         private:
55             void doFilter(EntitiesDescriptor& entities) const;
56
57             bool found(const XMLCh* id) const {
58                 if (!id)
59                     return false;
60                 return m_set.count(id)==1;
61             }
62
63             set<xstring> m_set;
64         };
65
66         MetadataFilter* SAML_DLLLOCAL WhitelistMetadataFilterFactory(const DOMElement* const & e)
67         {
68             return new WhitelistMetadataFilter(e);
69         }
70
71     };
72 };
73
74 static const XMLCh Include[] =  UNICODE_LITERAL_7(I,n,c,l,u,d,e);
75
76 WhitelistMetadataFilter::WhitelistMetadataFilter(const DOMElement* e)
77 {
78     e = XMLHelper::getFirstChildElement(e);
79     while (e) {
80         if (XMLString::equals(e->getLocalName(), Include) && e->hasChildNodes()) {
81             m_set.insert(e->getFirstChild()->getTextContent());
82         }
83         e = XMLHelper::getNextSiblingElement(e);
84     }
85 }
86
87 void WhitelistMetadataFilter::doFilter(XMLObject& xmlObject) const
88 {
89 #ifdef _DEBUG
90     NDC ndc("doFilter");
91 #endif
92
93     try {
94         doFilter(dynamic_cast<EntitiesDescriptor&>(xmlObject));
95         return;
96     }
97     catch (bad_cast&) {
98     }
99
100     try {
101         EntityDescriptor& entity = dynamic_cast<EntityDescriptor&>(xmlObject);
102         if (!found(entity.getEntityID()))
103             throw MetadataFilterException(WHITELIST_METADATA_FILTER" MetadataFilter instructed to filter the root/only entity in the metadata.");
104         return;
105     }
106     catch (bad_cast&) {
107     }
108
109     throw MetadataFilterException(WHITELIST_METADATA_FILTER" MetadataFilter was given an improper metadata instance to filter.");
110 }
111
112 void WhitelistMetadataFilter::doFilter(EntitiesDescriptor& entities) const
113 {
114     Category& log=Category::getInstance(SAML_LOGCAT".MetadataFilter."WHITELIST_METADATA_FILTER);
115
116     VectorOf(EntityDescriptor) v=entities.getEntityDescriptors();
117     for (VectorOf(EntityDescriptor)::size_type i=0; i<v.size(); ) {
118         const XMLCh* id=v[i]->getEntityID();
119         if (!found(id)) {
120             auto_ptr_char id2(id);
121             log.info("filtering out non-whitelisted entity (%s)", id2.get());
122             v.erase(v.begin() + i);
123         }
124         else {
125             i++;
126         }
127     }
128
129     const vector<EntitiesDescriptor*>& groups=const_cast<const EntitiesDescriptor&>(entities).getEntitiesDescriptors();
130     for_each(
131         make_indirect_iterator(groups.begin()), make_indirect_iterator(groups.end()),
132         boost::bind(
133             static_cast<void (WhitelistMetadataFilter::*)(EntitiesDescriptor&) const>(&WhitelistMetadataFilter::doFilter), this, _1
134             )
135         );
136 }