https://issues.shibboleth.net/jira/browse/SSPCPP-342
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / EntityAttributesEntityMatcher.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  * EntityAttributesEntityMatcher.cpp
23  *
24  * EntityMatcher that applies a set of input attributes.
25  */
26
27 #include "internal.h"
28 #include "saml2/metadata/EntityMatcher.h"
29 #include "saml2/metadata/Metadata.h"
30
31 #include <boost/shared_ptr.hpp>
32 #include <boost/iterator/indirect_iterator.hpp>
33 #include <boost/lambda/bind.hpp>
34 #include <boost/lambda/casts.hpp>
35 #include <boost/lambda/lambda.hpp>
36 #include <xercesc/util/XMLUniDefs.hpp>
37 #include <xmltooling/util/XMLHelper.h>
38
39 using namespace opensaml::saml2md;
40 using namespace opensaml::saml2;
41 using namespace opensaml;
42 using namespace xmltooling;
43 using namespace boost::lambda;
44 using namespace boost;
45 using namespace std;
46
47 namespace opensaml {
48     namespace saml2md {
49         class SAML_DLLLOCAL EntityAttributesEntityMatcher : public EntityMatcher
50         {
51         public:
52             EntityAttributesEntityMatcher(const DOMElement* e);
53             ~EntityAttributesEntityMatcher() {}
54
55             bool matches(const EntityDescriptor& entity) const;
56
57         private:
58             bool _matches(const EntityAttributes*, const Attribute*) const;
59
60             bool m_trimTags;
61             vector< boost::shared_ptr<Attribute> > m_tags;
62         };
63
64         EntityMatcher* SAML_DLLLOCAL EntityAttributesEntityMatcherFactory(const DOMElement* const & e)
65         {
66             return new EntityAttributesEntityMatcher(e);
67         }
68
69         static const XMLCh trimTags[] = UNICODE_LITERAL_8(t,r,i,m,T,a,g,s);
70     };
71 };
72
73
74 EntityAttributesEntityMatcher::EntityAttributesEntityMatcher(const DOMElement* e)
75     : m_trimTags(XMLHelper::getAttrBool(e, false, trimTags))
76 {
77     DOMElement* child = XMLHelper::getFirstChildElement(e, samlconstants::SAML20_NS, Attribute::LOCAL_NAME);
78     while (child) {
79         boost::shared_ptr<XMLObject> obj(AttributeBuilder::buildOneFromElement(child));
80         m_tags.push_back(boost::shared_dynamic_cast<Attribute>(obj));
81         child = XMLHelper::getNextSiblingElement(child, samlconstants::SAML20_NS, Attribute::LOCAL_NAME);
82     }
83
84     if (m_tags.empty())
85         throw XMLToolingException("EntityAttributes EntityMatcher requires at least one saml2:Attribute to match.");
86 }
87
88 bool EntityAttributesEntityMatcher::matches(const EntityDescriptor& entity) const
89 {
90     // Check for a tag match in the EntityAttributes extension of the entity and its parent(s).
91     const Extensions* exts = entity.getExtensions();
92     if (exts) {
93         const vector<XMLObject*>& children = exts->getUnknownXMLObjects();
94         const XMLObject* xo = find_if(children, ll_dynamic_cast<EntityAttributes*>(_1) != ((EntityAttributes*)nullptr));
95         if (xo) {
96             // If we find a matching tag, we win. Each tag is treated in OR fashion.
97             if (find_if(m_tags.begin(), m_tags.end(),
98                 lambda::bind(&EntityAttributesEntityMatcher::_matches, this, dynamic_cast<const EntityAttributes*>(xo),
99                     lambda::bind(&boost::shared_ptr<Attribute>::get, _1))) != m_tags.end()) {
100                 return true;
101             }
102         }
103     }
104
105     const EntitiesDescriptor* group = dynamic_cast<EntitiesDescriptor*>(entity.getParent());
106     while (group) {
107         exts = group->getExtensions();
108         if (exts) {
109             const vector<XMLObject*>& children = exts->getUnknownXMLObjects();
110             const XMLObject* xo = find_if(children, ll_dynamic_cast<EntityAttributes*>(_1) != ((EntityAttributes*)nullptr));
111             if (xo) {
112                 // If we find a matching tag, we win. Each tag is treated in OR fashion.
113                 if (find_if(m_tags.begin(), m_tags.end(),
114                     lambda::bind(&EntityAttributesEntityMatcher::_matches, this, dynamic_cast<const EntityAttributes*>(xo),
115                         lambda::bind(&boost::shared_ptr<Attribute>::get, _1))) != m_tags.end()) {
116                     return true;
117                 }
118             }
119         }
120         group = dynamic_cast<EntitiesDescriptor*>(group->getParent());
121     }
122
123     return false;
124 }
125
126 bool EntityAttributesEntityMatcher::_matches(const EntityAttributes* ea, const Attribute* tag) const
127 {
128     const vector<Attribute*>& attrs = ea->getAttributes();
129     const vector<XMLObject*>& tagvals = tag->getAttributeValues();
130     if (!attrs.empty() && !tagvals.empty()) {
131         // Track whether we've found every tag value.
132         vector<bool> flags(tagvals.size());
133
134         // Check each attribute/tag in the candidate.
135         for (indirect_iterator<vector<Attribute*>::const_iterator> a = make_indirect_iterator(attrs.begin());
136                 a != make_indirect_iterator(attrs.end()); ++a) {
137             // Compare Name and NameFormat for a matching tag.
138             if (XMLString::equals(a->getName(), tag->getName()) &&
139                 (!tag->getNameFormat() || XMLString::equals(tag->getNameFormat(), Attribute::UNSPECIFIED) ||
140                     XMLString::equals(tag->getNameFormat(), a->getNameFormat()))) {
141                 // Check each tag value's simple content for a match.
142                 for (vector<XMLObject*>::size_type tagindex = 0; tagindex < tagvals.size(); ++tagindex) {
143                     const XMLObject* tagval = tagvals[tagindex];
144                     const XMLCh* tagvalstr = (tagval->getDOM()) ? tagval->getDOM()->getTextContent() : tagval->getTextContent();
145                     const vector<XMLObject*>& cvals = const_cast<const Attribute&>(*a).getAttributeValues();
146                     for (indirect_iterator<vector<XMLObject*>::const_iterator> cval = make_indirect_iterator(cvals.begin());
147                             cval != make_indirect_iterator(cvals.end()); ++cval) {
148                         const XMLCh* cvalstr = cval->getDOM() ? cval->getDOM()->getTextContent() : cval->getTextContent();
149                         if (tagvalstr && cvalstr) {
150                             if (XMLString::equals(tagvalstr, cvalstr)) {
151                                 flags[tagindex] = true;
152                                 break;
153                             }
154                             else if (m_trimTags) {
155                                 XMLCh* dup = XMLString::replicate(cvalstr);
156                                 XMLString::trim(dup);
157                                 if (XMLString::equals(tagvalstr, dup)) {
158                                     XMLString::release(&dup);
159                                     flags[tagindex] = true;
160                                     break;
161                                 }
162                                 XMLString::release(&dup);
163                             }
164                         }
165                     }
166                 }
167             }
168         }
169
170         if (find(flags.begin(), flags.end(), false) == flags.end())
171             return true;
172     }
173     return false;
174 }