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