Imported Upstream version 2.4+dfsg
[shibboleth/sp.git] / shibsp / attribute / filtering / impl / AttributeScopeStringFunctor.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  * AttributeScopeStringFunctor.cpp
19  * 
20  * A match function that matches the scope of an attribute value against
21  * the 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 ignoreCase[] =   UNICODE_LITERAL_10(i,g,n,o,r,e,C,a,s,e);
41     static const XMLCh value[] =        UNICODE_LITERAL_5(v,a,l,u,e);
42
43     /**
44      * A match function that matches the scope of an attribute value against the specified value.
45      */
46     class SHIBSP_DLLLOCAL AttributeScopeStringFunctor : public MatchFunctor
47     {
48         string m_attributeID;
49         char* m_value;
50         bool m_ignoreCase;
51
52         bool hasScope(const FilteringContext& filterContext) const;
53
54     public:
55         AttributeScopeStringFunctor(const DOMElement* e)
56             : m_attributeID(XMLHelper::getAttrString(e, nullptr, attributeID)),
57                 m_value(e ? xmltooling::toUTF8(e->getAttributeNS(nullptr,value)) : nullptr),
58                 m_ignoreCase(XMLHelper::getAttrBool(e, false, ignoreCase)) {
59             if (!m_value || !*m_value) {
60                 delete[] m_value;
61                 throw ConfigurationException("AttributeScopeString MatchFunctor requires non-empty value attribute.");
62             }
63         }
64
65         virtual ~AttributeScopeStringFunctor() {
66             delete[] m_value;
67         }
68
69         bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
70             if (m_attributeID.empty())
71                 throw AttributeFilteringException("No attributeID specified.");
72             return hasScope(filterContext);
73         }
74
75         bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
76             if (m_attributeID.empty() || m_attributeID == attribute.getId()) {
77                 if (m_ignoreCase) {
78 #ifdef HAVE_STRCASECMP
79                     return !strcasecmp(attribute.getScope(index), m_value);
80 #else
81                     return !stricmp(attribute.getScope(index), m_value);
82 #endif
83                 }
84                 else
85                     return !strcmp(attribute.getScope(index), m_value);
86             }
87             return hasScope(filterContext);
88         }
89     };
90
91     MatchFunctor* SHIBSP_DLLLOCAL AttributeScopeStringFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
92     {
93         return new AttributeScopeStringFunctor(p.second);
94     }
95
96 };
97
98 bool AttributeScopeStringFunctor::hasScope(const FilteringContext& filterContext) const
99 {
100     size_t count;
101     pair<multimap<string,Attribute*>::const_iterator,multimap<string,Attribute*>::const_iterator> attrs =
102         filterContext.getAttributes().equal_range(m_attributeID);
103     for (; attrs.first != attrs.second; ++attrs.first) {
104         count = attrs.first->second->valueCount();
105         for (size_t index = 0; index < count; ++index) {
106             if (m_ignoreCase) {
107 #ifdef HAVE_STRCASECMP
108                 if (!strcasecmp(attrs.first->second->getScope(index), m_value))
109                     return true;
110 #else
111                 if (!stricmp(attrs.first->second->getScope(index), m_value))
112                     return true;
113 #endif
114             }
115             else {
116                 if (!strcmp(attrs.first->second->getScope(index), m_value))
117                     return true;
118             }
119         }
120     }
121     return false;
122 }