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