boost changes and header fixes
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / EntityRoleMetadataFilter.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  * EntityRoleMetadataFilter.cpp
23  *
24  * Removes non-whitelisted roles 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/ptr_container/ptr_set.hpp>
32 #include <xmltooling/logging.h>
33 #include <xmltooling/util/NDC.h>
34
35 using namespace opensaml::saml2md;
36 using namespace xmltooling::logging;
37 using namespace xmltooling;
38 using namespace boost;
39 using namespace std;
40
41 namespace opensaml {
42     namespace saml2md {
43
44         class SAML_DLLLOCAL EntityRoleMetadataFilter : public MetadataFilter
45         {
46         public:
47             EntityRoleMetadataFilter(const DOMElement* e);
48             ~EntityRoleMetadataFilter() {}
49
50             const char* getId() const { return ENTITYROLE_METADATA_FILTER; }
51             void doFilter(XMLObject& xmlObject) const;
52
53         private:
54             void doFilter(EntityDescriptor& entity) const;
55             void doFilter(EntitiesDescriptor& entities) const;
56
57             bool m_removeRolelessEntityDescriptors, m_removeEmptyEntitiesDescriptors;
58             ptr_set<xmltooling::QName> m_roles;
59             bool m_idp, m_sp, m_authn, m_attr, m_pdp, m_authnq, m_attrq, m_authzq;
60         };
61
62         MetadataFilter* SAML_DLLLOCAL EntityRoleMetadataFilterFactory(const DOMElement* const & e)
63         {
64             return new EntityRoleMetadataFilter(e);
65         }
66
67     };
68 };
69
70 static const XMLCh RetainedRole[] =                     UNICODE_LITERAL_12(R,e,t,a,i,n,e,d,R,o,l,e);
71 static const XMLCh removeRolelessEntityDescriptors[] =  UNICODE_LITERAL_31(r,e,m,o,v,e,R,o,l,e,l,e,s,s,E,n,t,i,t,y,D,e,s,c,r,i,p,t,o,r,s);
72 static const XMLCh removeEmptyEntitiesDescriptors[] =   UNICODE_LITERAL_30(r,e,m,o,v,e,E,m,p,t,y,E,n,t,i,t,i,e,s,D,e,s,c,r,i,p,t,o,r,s);
73
74 EntityRoleMetadataFilter::EntityRoleMetadataFilter(const DOMElement* e)
75     : m_removeRolelessEntityDescriptors(XMLHelper::getAttrBool(e, true, removeRolelessEntityDescriptors)),
76         m_removeEmptyEntitiesDescriptors(XMLHelper::getAttrBool(e, true, removeEmptyEntitiesDescriptors)),
77         m_idp(false), m_sp(false), m_authn(false), m_attr(false), m_pdp(false), m_authnq(false), m_attrq(false), m_authzq(false)
78 {
79     e = XMLHelper::getFirstChildElement(e, RetainedRole);
80     while (e) {
81         auto_ptr<xmltooling::QName> q(XMLHelper::getNodeValueAsQName(e));
82         if (q.get()) {
83             if (*q == IDPSSODescriptor::ELEMENT_QNAME)
84                 m_idp = true;
85             else if (*q == SPSSODescriptor::ELEMENT_QNAME)
86                 m_sp = true;
87             else if (*q == AuthnAuthorityDescriptor::ELEMENT_QNAME)
88                 m_authn = true;
89             else if (*q == AttributeAuthorityDescriptor::ELEMENT_QNAME)
90                 m_attr = true;
91             else if (*q == PDPDescriptor::ELEMENT_QNAME)
92                 m_pdp = true;
93             else if (*q == AuthnQueryDescriptorType::TYPE_QNAME)
94                 m_authnq = true;
95             else if (*q == AttributeQueryDescriptorType::TYPE_QNAME)
96                 m_attrq = true;
97             else if (*q == AuthzDecisionQueryDescriptorType::TYPE_QNAME)
98                 m_authzq = true;
99             else
100                 m_roles.insert(q);
101         }
102         e = XMLHelper::getNextSiblingElement(e, RetainedRole);
103     }
104 }
105
106 void EntityRoleMetadataFilter::doFilter(XMLObject& xmlObject) const
107 {
108 #ifdef _DEBUG
109     NDC ndc("doFilter");
110 #endif
111
112     try {
113         doFilter(dynamic_cast<EntitiesDescriptor&>(xmlObject));
114         return;
115     }
116     catch (bad_cast&) {
117     }
118
119     try {
120         doFilter(dynamic_cast<EntityDescriptor&>(xmlObject));
121         return;
122     }
123     catch (bad_cast&) {
124     }
125
126     throw MetadataFilterException("EntityRoleWhiteList MetadataFilter was given an improper metadata instance to filter.");
127 }
128
129 void EntityRoleMetadataFilter::doFilter(EntitiesDescriptor& entities) const
130 {
131     Category& log=Category::getInstance(SAML_LOGCAT".MetadataFilter.EntityRoleWhiteList");
132
133     VectorOf(EntityDescriptor) v = entities.getEntityDescriptors();
134     for (VectorOf(EntityDescriptor)::size_type i = 0; i < v.size(); ) {
135         doFilter(*v[i]);
136         if (m_removeRolelessEntityDescriptors) {
137             const EntityDescriptor& e = const_cast<const EntityDescriptor&>(*v[i]);
138             if (e.getIDPSSODescriptors().empty() &&
139                     e.getSPSSODescriptors().empty() &&
140                     e.getAuthnAuthorityDescriptors().empty() &&
141                     e.getAttributeAuthorityDescriptors().empty() &&
142                     e.getPDPDescriptors().empty() &&
143                     e.getAuthnQueryDescriptorTypes().empty() &&
144                     e.getAttributeQueryDescriptorTypes().empty() &&
145                     e.getAuthzDecisionQueryDescriptorTypes().empty() &&
146                     e.getRoleDescriptors().empty()) {
147                 auto_ptr_char temp(e.getEntityID());
148                 log.debug("filtering out role-less entity (%s)", temp.get());
149                 v.erase(v.begin() + i);
150                 continue;
151             }
152         }
153         i++;
154     }
155
156     VectorOf(EntitiesDescriptor) groups = entities.getEntitiesDescriptors();
157     for (VectorOf(EntitiesDescriptor)::size_type j = 0; j < groups.size(); ) {
158         EntitiesDescriptor* group = groups[j];
159         doFilter(*group);
160         if (m_removeEmptyEntitiesDescriptors && group->getEntitiesDescriptors().empty() && group->getEntityDescriptors().empty()) {
161             auto_ptr_char temp(entities.getName());
162             auto_ptr_char temp2(group->getName());
163             log.debug(
164                 "filtering out empty EntitiesDescriptor (%s) from EntitiesDescriptor (%s)",
165                 temp2.get() ? temp2.get() : "unnamed",
166                 temp.get() ? temp.get() : "unnamed"
167                 );
168             groups.erase(groups.begin() + j);
169         }
170         else {
171             j++;
172         }
173     }
174 }
175
176 void EntityRoleMetadataFilter::doFilter(EntityDescriptor& entity) const
177 {
178     if (!m_idp)
179         entity.getIDPSSODescriptors().clear();
180     if (!m_sp)
181         entity.getSPSSODescriptors().clear();
182     if (!m_authn)
183         entity.getAuthnAuthorityDescriptors().clear();
184     if (!m_attr)
185         entity.getAttributeAuthorityDescriptors().clear();
186     if (!m_pdp)
187         entity.getPDPDescriptors().clear();
188     if (!m_authnq)
189         entity.getAuthnQueryDescriptorTypes().clear();
190     if (!m_attrq)
191         entity.getAttributeQueryDescriptorTypes().clear();
192     if (!m_authzq)
193         entity.getAuthzDecisionQueryDescriptorTypes().clear();
194
195     VectorOf(RoleDescriptor) v = entity.getRoleDescriptors();
196     for (VectorOf(RoleDescriptor)::size_type i = 0; i < v.size(); ) {
197         const xmltooling::QName* type = v[i]->getSchemaType();
198         if (!type || m_roles.find(*type) != m_roles.end())
199             v.erase(v.begin() + i);
200         else
201             i++;
202     }
203 }