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