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