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