Change license header.
[shibboleth/sp.git] / shibsp / attribute / filtering / impl / AttributeScopeStringFunctor.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * AttributeScopeStringFunctor.cpp
23  * 
24  * A match function that matches the scope of an attribute value against
25  * the specified value.
26  */
27
28 #include "internal.h"
29 #include "exceptions.h"
30 #include "attribute/Attribute.h"
31 #include "attribute/filtering/FilteringContext.h"
32 #include "attribute/filtering/FilterPolicyContext.h"
33 #include "attribute/filtering/MatchFunctor.h"
34
35 #include <xmltooling/util/XMLHelper.h>
36
37 using namespace shibsp;
38 using namespace std;
39 using xmltooling::XMLHelper;
40
41 namespace shibsp {
42
43     static const XMLCh attributeID[] =  UNICODE_LITERAL_11(a,t,t,r,i,b,u,t,e,I,D);
44     static const XMLCh ignoreCase[] =   UNICODE_LITERAL_10(i,g,n,o,r,e,C,a,s,e);
45     static const XMLCh value[] =        UNICODE_LITERAL_5(v,a,l,u,e);
46
47     /**
48      * A match function that matches the scope of an attribute value against the specified value.
49      */
50     class SHIBSP_DLLLOCAL AttributeScopeStringFunctor : public MatchFunctor
51     {
52         string m_attributeID;
53         char* m_value;
54         bool m_ignoreCase;
55
56         bool hasScope(const FilteringContext& filterContext) const;
57
58     public:
59         AttributeScopeStringFunctor(const DOMElement* e)
60             : m_attributeID(XMLHelper::getAttrString(e, nullptr, attributeID)),
61                 m_value(e ? xmltooling::toUTF8(e->getAttributeNS(nullptr,value)) : nullptr),
62                 m_ignoreCase(XMLHelper::getAttrBool(e, false, ignoreCase)) {
63             if (!m_value || !*m_value) {
64                 delete[] m_value;
65                 throw ConfigurationException("AttributeScopeString MatchFunctor requires non-empty value attribute.");
66             }
67         }
68
69         virtual ~AttributeScopeStringFunctor() {
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 hasScope(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                 if (m_ignoreCase) {
82 #ifdef HAVE_STRCASECMP
83                     return !strcasecmp(attribute.getScope(index), m_value);
84 #else
85                     return !stricmp(attribute.getScope(index), m_value);
86 #endif
87                 }
88                 else
89                     return !strcmp(attribute.getScope(index), m_value);
90             }
91             return hasScope(filterContext);
92         }
93     };
94
95     MatchFunctor* SHIBSP_DLLLOCAL AttributeScopeStringFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
96     {
97         return new AttributeScopeStringFunctor(p.second);
98     }
99
100 };
101
102 bool AttributeScopeStringFunctor::hasScope(const FilteringContext& filterContext) const
103 {
104     size_t count;
105     pair<multimap<string,Attribute*>::const_iterator,multimap<string,Attribute*>::const_iterator> attrs =
106         filterContext.getAttributes().equal_range(m_attributeID);
107     for (; attrs.first != attrs.second; ++attrs.first) {
108         count = attrs.first->second->valueCount();
109         for (size_t index = 0; index < count; ++index) {
110             if (m_ignoreCase) {
111 #ifdef HAVE_STRCASECMP
112                 if (!strcasecmp(attrs.first->second->getScope(index), m_value))
113                     return true;
114 #else
115                 if (!stricmp(attrs.first->second->getScope(index), m_value))
116                     return true;
117 #endif
118             }
119             else {
120                 if (!strcmp(attrs.first->second->getScope(index), m_value))
121                     return true;
122             }
123         }
124     }
125     return false;
126 }