Inject logging category into base class.
[shibboleth/sp.git] / shibsp / impl / XMLAccessControl.cpp
1 /*\r
2  *  Copyright 2001-2007 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 /**\r
18  * XMLAccessControl.cpp\r
19  *\r
20  * XML-based access control syntax\r
21  */\r
22 \r
23 #include "internal.h"\r
24 #include "exceptions.h"\r
25 #include "AccessControl.h"\r
26 #include "SessionCache.h"\r
27 #include "SPRequest.h"\r
28 #include "attribute/Attribute.h"\r
29 \r
30 #include <xmltooling/util/ReloadableXMLFile.h>\r
31 #include <xmltooling/util/XMLHelper.h>\r
32 #include <xercesc/util/XMLUniDefs.hpp>\r
33 \r
34 #ifndef HAVE_STRCASECMP\r
35 # define strcasecmp _stricmp\r
36 #endif\r
37 \r
38 using namespace shibsp;\r
39 using namespace xmltooling;\r
40 using namespace std;\r
41 \r
42 namespace {\r
43     \r
44     class Rule : public AccessControl\r
45     {\r
46     public:\r
47         Rule(const DOMElement* e);\r
48         ~Rule() {}\r
49 \r
50         Lockable* lock() {return this;}\r
51         void unlock() {}\r
52 \r
53         bool authorized(const SPRequest& request, const Session* session) const;\r
54     \r
55     private:\r
56         string m_alias;\r
57         vector <string> m_vals;\r
58     };\r
59     \r
60     class Operator : public AccessControl\r
61     {\r
62     public:\r
63         Operator(const DOMElement* e);\r
64         ~Operator();\r
65 \r
66         Lockable* lock() {return this;}\r
67         void unlock() {}\r
68 \r
69         bool authorized(const SPRequest& request, const Session* session) const;\r
70         \r
71     private:\r
72         enum operator_t { OP_NOT, OP_AND, OP_OR } m_op;\r
73         vector<AccessControl*> m_operands;\r
74     };\r
75 \r
76 #if defined (_MSC_VER)\r
77     #pragma warning( push )\r
78     #pragma warning( disable : 4250 )\r
79 #endif\r
80 \r
81     class XMLAccessControl : public AccessControl, public ReloadableXMLFile\r
82     {\r
83     public:\r
84         XMLAccessControl(const DOMElement* e)\r
85                 : ReloadableXMLFile(e, log4cpp::Category::getInstance(SHIBSP_LOGCAT".AccessControl")), m_rootAuthz(NULL) {\r
86             load(); // guarantees an exception or the policy is loaded\r
87         }\r
88         \r
89         ~XMLAccessControl() {\r
90             delete m_rootAuthz;\r
91         }\r
92 \r
93         bool authorized(const SPRequest& request, const Session* session) const;\r
94 \r
95     protected:\r
96         pair<bool,DOMElement*> load();\r
97 \r
98     private:\r
99         AccessControl* m_rootAuthz;\r
100     };\r
101 \r
102 #if defined (_MSC_VER)\r
103     #pragma warning( pop )\r
104 #endif\r
105 \r
106     AccessControl* SHIBSP_DLLLOCAL XMLAccessControlFactory(const DOMElement* const & e)\r
107     {\r
108         return new XMLAccessControl(e);\r
109     }\r
110 \r
111     static const XMLCh _AccessControl[] =    UNICODE_LITERAL_13(A,c,c,e,s,s,C,o,n,t,r,o,l);\r
112     static const XMLCh require[] =          UNICODE_LITERAL_7(r,e,q,u,i,r,e);\r
113     static const XMLCh NOT[] =              UNICODE_LITERAL_3(N,O,T);\r
114     static const XMLCh AND[] =              UNICODE_LITERAL_3(A,N,D);\r
115     static const XMLCh OR[] =               UNICODE_LITERAL_2(O,R);\r
116     static const XMLCh _Rule[] =            UNICODE_LITERAL_4(R,u,l,e);\r
117 }\r
118 \r
119 void SHIBSP_API shibsp::registerAccessControls()\r
120 {\r
121     SPConfig& conf=SPConfig::getConfig();\r
122     conf.AccessControlManager.registerFactory(XML_ACCESS_CONTROL, XMLAccessControlFactory);\r
123     conf.AccessControlManager.registerFactory("edu.internet2.middleware.shibboleth.sp.provider.XMLAccessControl", XMLAccessControlFactory);\r
124 }\r
125 \r
126 Rule::Rule(const DOMElement* e)\r
127 {\r
128     xmltooling::auto_ptr_char req(e->getAttributeNS(NULL,require));\r
129     if (!req.get() || !*req.get())\r
130         throw ConfigurationException("Access control rule missing require attribute");\r
131     m_alias=req.get();\r
132     \r
133     xmltooling::auto_ptr_char vals(e->hasChildNodes() ? e->getFirstChild()->getNodeValue() : NULL);\r
134 #ifdef HAVE_STRTOK_R\r
135     char* pos=NULL;\r
136     const char* token=strtok_r(const_cast<char*>(vals.get()),"/",&pos);\r
137 #else\r
138     const char* token=strtok(const_cast<char*>(vals.get()),"/");\r
139 #endif\r
140     while (token) {\r
141         m_vals.push_back(token);\r
142 #ifdef HAVE_STRTOK_R\r
143         token=strtok_r(NULL,"/",&pos);\r
144 #else\r
145         token=strtok(NULL,"/");\r
146 #endif\r
147     }\r
148 }\r
149 \r
150 bool Rule::authorized(const SPRequest& request, const Session* session) const\r
151 {\r
152     // We can make this more complex later using pluggable comparison functions,\r
153     // but for now, just a straight port to the new Attribute API.\r
154 \r
155     // Map alias in rule to the attribute.\r
156     if (!session) {\r
157         request.log(SPRequest::SPWarn, "AccessControl plugin not given a valid session to evaluate, are you using lazy sessions?");\r
158         return false;\r
159     }\r
160     \r
161     // Find the attribute matching the require rule.\r
162     map<string,const Attribute*>::const_iterator attr = session->getAttributes().find(m_alias);\r
163     if (attr == session->getAttributes().end()) {\r
164         request.log(SPRequest::SPWarn, string("rule requires attribute (") + m_alias + "), not found in session");\r
165         return false;\r
166     }\r
167 \r
168     bool caseSensitive = attr->second->isCaseSensitive();\r
169 \r
170     // Now we have to intersect the attribute's values against the rule's list.\r
171     const vector<string>& vals = attr->second->getSerializedValues();\r
172     for (vector<string>::const_iterator i=m_vals.begin(); i!=m_vals.end(); ++i) {\r
173         for (vector<string>::const_iterator j=vals.begin(); j!=vals.end(); ++j) {\r
174             if ((caseSensitive && *i == *j) || (!caseSensitive && !strcasecmp(i->c_str(),j->c_str()))) {\r
175                 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting ") + *j + ", authz granted");\r
176                 return true;\r
177             }\r
178         }\r
179     }\r
180 \r
181     return false;\r
182 }\r
183 \r
184 Operator::Operator(const DOMElement* e)\r
185 {\r
186     if (XMLString::equals(e->getLocalName(),NOT))\r
187         m_op=OP_NOT;\r
188     else if (XMLString::equals(e->getLocalName(),AND))\r
189         m_op=OP_AND;\r
190     else if (XMLString::equals(e->getLocalName(),OR))\r
191         m_op=OP_OR;\r
192     else\r
193         throw ConfigurationException("Unrecognized operator in access control rule");\r
194     \r
195     try {\r
196         e=XMLHelper::getFirstChildElement(e);\r
197         if (XMLString::equals(e->getLocalName(),_Rule))\r
198             m_operands.push_back(new Rule(e));\r
199         else\r
200             m_operands.push_back(new Operator(e));\r
201         \r
202         if (m_op==OP_NOT)\r
203             return;\r
204         \r
205         e=XMLHelper::getNextSiblingElement(e);\r
206         while (e) {\r
207             if (XMLString::equals(e->getLocalName(),_Rule))\r
208                 m_operands.push_back(new Rule(e));\r
209             else\r
210                 m_operands.push_back(new Operator(e));\r
211             e=XMLHelper::getNextSiblingElement(e);\r
212         }\r
213     }\r
214     catch (exception&) {\r
215         for_each(m_operands.begin(),m_operands.end(),xmltooling::cleanup<AccessControl>());\r
216         throw;\r
217     }\r
218 }\r
219 \r
220 Operator::~Operator()\r
221 {\r
222     for_each(m_operands.begin(),m_operands.end(),xmltooling::cleanup<AccessControl>());\r
223 }\r
224 \r
225 bool Operator::authorized(const SPRequest& request, const Session* session) const\r
226 {\r
227     switch (m_op) {\r
228         case OP_NOT:\r
229             return !m_operands[0]->authorized(request,session);\r
230         \r
231         case OP_AND:\r
232         {\r
233             for (vector<AccessControl*>::const_iterator i=m_operands.begin(); i!=m_operands.end(); i++) {\r
234                 if (!(*i)->authorized(request,session))\r
235                     return false;\r
236             }\r
237             return true;\r
238         }\r
239         \r
240         case OP_OR:\r
241         {\r
242             for (vector<AccessControl*>::const_iterator i=m_operands.begin(); i!=m_operands.end(); i++) {\r
243                 if ((*i)->authorized(request,session))\r
244                     return true;\r
245             }\r
246             return false;\r
247         }\r
248     }\r
249     request.log(SPRequest::SPWarn,"unknown operation in access control policy, denying access");\r
250     return false;\r
251 }\r
252 \r
253 pair<bool,DOMElement*> XMLAccessControl::load()\r
254 {\r
255     // Load from source using base class.\r
256     pair<bool,DOMElement*> raw = ReloadableXMLFile::load();\r
257     \r
258     // If we own it, wrap it.\r
259     XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);\r
260 \r
261     // Check for AccessControl wrapper and drop a level.\r
262     if (XMLString::equals(raw.second->getLocalName(),_AccessControl))\r
263         raw.second = XMLHelper::getFirstChildElement(raw.second);\r
264     \r
265     AccessControl* authz;\r
266     if (XMLString::equals(raw.second->getLocalName(),_Rule))\r
267         authz=new Rule(raw.second);\r
268     else\r
269         authz=new Operator(raw.second);\r
270 \r
271     delete m_rootAuthz;\r
272     m_rootAuthz = authz;\r
273     return make_pair(false,(DOMElement*)NULL);\r
274 }\r
275 \r
276 bool XMLAccessControl::authorized(const SPRequest& request, const Session* session) const\r
277 {\r
278     return m_rootAuthz ? m_rootAuthz->authorized(request,session) : false;\r
279 }\r