Imported Upstream version 2.4+dfsg
[shibboleth/sp.git] / shibsp / attribute / filtering / impl / AttributeValueStringFunctor.cpp
1 /*
2  *  Copyright 2001-2010 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  * AttributeValueStringFunctor.cpp
19  * 
20  * A match function that matches the value of an attribute against the
21  * specified value.
22  */
23
24 #include "internal.h"
25 #include "exceptions.h"
26 #include "attribute/Attribute.h"
27 #include "attribute/filtering/FilteringContext.h"
28 #include "attribute/filtering/FilterPolicyContext.h"
29 #include "attribute/filtering/MatchFunctor.h"
30
31 #include <xmltooling/util/XMLHelper.h>
32
33 using namespace shibsp;
34 using namespace std;
35 using xmltooling::XMLHelper;
36
37 namespace shibsp {
38
39     static const XMLCh attributeID[] =  UNICODE_LITERAL_11(a,t,t,r,i,b,u,t,e,I,D);
40     static const XMLCh value[] =        UNICODE_LITERAL_5(v,a,l,u,e);
41     static const XMLCh ignoreCase[] =   UNICODE_LITERAL_10(i,g,n,o,r,e,C,a,s,e);
42
43     /**
44      * A match function that matches the value of an attribute against the specified value.
45      */
46     class SHIBSP_DLLLOCAL AttributeValueStringFunctor : public MatchFunctor
47     {
48         string m_attributeID;
49         char* m_value;
50
51         bool hasValue(const FilteringContext& filterContext) const;
52         bool matches(const Attribute& attribute, size_t index) const;
53
54     public:
55         AttributeValueStringFunctor(const DOMElement* e)
56             : m_value(e ? xmltooling::toUTF8(e->getAttributeNS(nullptr,value)) : nullptr),
57                 m_attributeID(XMLHelper::getAttrString(e, nullptr, attributeID)) {
58             if (!m_value || !*m_value) {
59                 delete[] m_value;
60                 throw ConfigurationException("AttributeValueString MatchFunctor requires non-empty value attribute.");
61             }
62             if (e && e->hasAttributeNS(nullptr,ignoreCase)) {
63                 xmltooling::logging::Category::getInstance(SHIBSP_LOGCAT".AttributeFilter").warn(
64                     "ignoreCase property ignored by AttributeValueString MatchFunctor in favor of attribute's caseSensitive property"
65                     );
66             }
67         }
68
69         virtual ~AttributeValueStringFunctor() {
70             delete[] m_value;
71         }
72
73         bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
74             if (m_attributeID.empty())
75                 throw AttributeFilteringException("No attributeID specified.");
76             return hasValue(filterContext);
77         }
78
79         bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
80             if (m_attributeID.empty() || m_attributeID == attribute.getId())
81                 return matches(attribute, index);
82             return hasValue(filterContext);
83         }
84     };
85
86     MatchFunctor* SHIBSP_DLLLOCAL AttributeValueStringFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
87     {
88         return new AttributeValueStringFunctor(p.second);
89     }
90
91 };
92
93 bool AttributeValueStringFunctor::hasValue(const FilteringContext& filterContext) const
94 {
95     size_t count;
96     pair<multimap<string,Attribute*>::const_iterator,multimap<string,Attribute*>::const_iterator> attrs =
97         filterContext.getAttributes().equal_range(m_attributeID);
98     for (; attrs.first != attrs.second; ++attrs.first) {
99         count = attrs.first->second->valueCount();
100         for (size_t index = 0; index < count; ++index) {
101             if (matches(*(attrs.first->second), index))
102                 return true;
103         }
104     }
105     return false;
106 }
107
108 bool AttributeValueStringFunctor::matches(const Attribute& attribute, size_t index) const
109 {
110     const char* val = attribute.getString(index);
111     if (!val)
112         return false;
113     if (attribute.isCaseSensitive())
114         return !strcmp(m_value, val);
115
116 #ifdef HAVE_STRCASECMP
117     return !strcasecmp(m_value, val);
118 #else
119     return !stricmp(m_value, val);
120 #endif
121 }