Imported Upstream version 2.3+dfsg
[shibboleth/sp.git] / shibsp / impl / XMLAccessControl.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  * XMLAccessControl.cpp
19  *
20  * XML-based access control syntax
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "AccessControl.h"
26 #include "SessionCache.h"
27 #include "SPRequest.h"
28 #include "attribute/Attribute.h"
29
30 #include <algorithm>
31 #include <xmltooling/unicode.h>
32 #include <xmltooling/util/ReloadableXMLFile.h>
33 #include <xmltooling/util/XMLHelper.h>
34 #include <xercesc/util/XMLUniDefs.hpp>
35 #include <xercesc/util/regx/RegularExpression.hpp>
36
37 #ifndef HAVE_STRCASECMP
38 # define strcasecmp _stricmp
39 #endif
40
41 using namespace shibsp;
42 using namespace xmltooling;
43 using namespace std;
44
45 namespace shibsp {
46
47     class Rule : public AccessControl
48     {
49     public:
50         Rule(const DOMElement* e);
51         ~Rule() {}
52
53         Lockable* lock() {return this;}
54         void unlock() {}
55
56         aclresult_t authorized(const SPRequest& request, const Session* session) const;
57
58     private:
59         string m_alias;
60         vector <string> m_vals;
61     };
62
63     class RuleRegex : public AccessControl
64     {
65     public:
66         RuleRegex(const DOMElement* e);
67         ~RuleRegex() {
68             delete m_re;
69         }
70
71         Lockable* lock() {return this;}
72         void unlock() {}
73
74         aclresult_t authorized(const SPRequest& request, const Session* session) const;
75
76     private:
77         string m_alias;
78         auto_arrayptr<char> m_exp;
79         RegularExpression* m_re;
80     };
81
82     class Operator : public AccessControl
83     {
84     public:
85         Operator(const DOMElement* e);
86         ~Operator();
87
88         Lockable* lock() {return this;}
89         void unlock() {}
90
91         aclresult_t authorized(const SPRequest& request, const Session* session) const;
92
93     private:
94         enum operator_t { OP_NOT, OP_AND, OP_OR } m_op;
95         vector<AccessControl*> m_operands;
96     };
97
98 #if defined (_MSC_VER)
99     #pragma warning( push )
100     #pragma warning( disable : 4250 )
101 #endif
102
103     class XMLAccessControl : public AccessControl, public ReloadableXMLFile
104     {
105     public:
106         XMLAccessControl(const DOMElement* e)
107                 : ReloadableXMLFile(e, Category::getInstance(SHIBSP_LOGCAT".AccessControl.XML")), m_rootAuthz(NULL) {
108             load(); // guarantees an exception or the policy is loaded
109         }
110
111         ~XMLAccessControl() {
112             delete m_rootAuthz;
113         }
114
115         aclresult_t authorized(const SPRequest& request, const Session* session) const;
116
117     protected:
118         pair<bool,DOMElement*> load();
119
120     private:
121         AccessControl* m_rootAuthz;
122     };
123
124 #if defined (_MSC_VER)
125     #pragma warning( pop )
126 #endif
127
128     AccessControl* SHIBSP_DLLLOCAL XMLAccessControlFactory(const DOMElement* const & e)
129     {
130         return new XMLAccessControl(e);
131     }
132
133     static const XMLCh _AccessControl[] =   UNICODE_LITERAL_13(A,c,c,e,s,s,C,o,n,t,r,o,l);
134     static const XMLCh ignoreCase[] =       UNICODE_LITERAL_10(i,g,n,o,r,e,C,a,s,e);
135     static const XMLCh ignoreOption[] =     UNICODE_LITERAL_1(i);
136     static const XMLCh _list[] =            UNICODE_LITERAL_4(l,i,s,t);
137     static const XMLCh require[] =          UNICODE_LITERAL_7(r,e,q,u,i,r,e);
138     static const XMLCh NOT[] =              UNICODE_LITERAL_3(N,O,T);
139     static const XMLCh AND[] =              UNICODE_LITERAL_3(A,N,D);
140     static const XMLCh OR[] =               UNICODE_LITERAL_2(O,R);
141     static const XMLCh _Rule[] =            UNICODE_LITERAL_4(R,u,l,e);
142     static const XMLCh _RuleRegex[] =       UNICODE_LITERAL_9(R,u,l,e,R,e,g,e,x);
143 }
144
145 Rule::Rule(const DOMElement* e)
146 {
147     auto_ptr_char req(e->getAttributeNS(NULL,require));
148     if (!req.get() || !*req.get())
149         throw ConfigurationException("Access control rule missing require attribute");
150     m_alias=req.get();
151
152     auto_arrayptr<char> vals(toUTF8(e->hasChildNodes() ? e->getFirstChild()->getNodeValue() : NULL));
153     if (!vals.get())
154         return;
155
156     const XMLCh* flag = e->getAttributeNS(NULL,_list);
157     if (flag && (*flag == chLatin_f || *flag == chDigit_0)) {
158         if (*vals.get())
159             m_vals.push_back(vals.get());
160         return;
161     }
162
163 #ifdef HAVE_STRTOK_R
164     char* pos=NULL;
165     const char* token=strtok_r(const_cast<char*>(vals.get())," ",&pos);
166 #else
167     const char* token=strtok(const_cast<char*>(vals.get())," ");
168 #endif
169     while (token) {
170         m_vals.push_back(token);
171 #ifdef HAVE_STRTOK_R
172         token=strtok_r(NULL," ",&pos);
173 #else
174         token=strtok(NULL," ");
175 #endif
176     }
177 }
178
179 AccessControl::aclresult_t Rule::authorized(const SPRequest& request, const Session* session) const
180 {
181     // We can make this more complex later using pluggable comparison functions,
182     // but for now, just a straight port to the new Attribute API.
183
184     // Map alias in rule to the attribute.
185     if (!session) {
186         request.log(SPRequest::SPWarn, "AccessControl plugin not given a valid session to evaluate, are you using lazy sessions?");
187         return shib_acl_false;
188     }
189
190     if (m_alias == "valid-user") {
191         if (session) {
192             request.log(SPRequest::SPDebug,"AccessControl plugin accepting valid-user based on active session");
193             return shib_acl_true;
194         }
195         return shib_acl_false;
196     }
197     if (m_alias == "user") {
198         for (vector<string>::const_iterator i=m_vals.begin(); i!=m_vals.end(); ++i) {
199             if (*i == request.getRemoteUser()) {
200                 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting REMOTE_USER (") + *i + "), authz granted");
201                 return shib_acl_true;
202             }
203         }
204         return shib_acl_false;
205     }
206     else if (m_alias == "authnContextClassRef") {
207         const char* ref = session->getAuthnContextClassRef();
208         for (vector<string>::const_iterator i=m_vals.begin(); ref && i!=m_vals.end(); ++i) {
209             if (!strcmp(i->c_str(),ref)) {
210                 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting authnContextClassRef (") + *i + "), authz granted");
211                 return shib_acl_true;
212             }
213         }
214         return shib_acl_false;
215     }
216     else if (m_alias == "authnContextDeclRef") {
217         const char* ref = session->getAuthnContextDeclRef();
218         for (vector<string>::const_iterator i=m_vals.begin(); ref && i!=m_vals.end(); ++i) {
219             if (!strcmp(i->c_str(),ref)) {
220                 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting authnContextDeclRef (") + *i + "), authz granted");
221                 return shib_acl_true;
222             }
223         }
224         return shib_acl_false;
225     }
226
227     // Find the attribute(s) matching the require rule.
228     pair<multimap<string,const Attribute*>::const_iterator, multimap<string,const Attribute*>::const_iterator> attrs =
229         session->getIndexedAttributes().equal_range(m_alias);
230     if (attrs.first == attrs.second) {
231         request.log(SPRequest::SPWarn, string("rule requires attribute (") + m_alias + "), not found in session");
232         return shib_acl_false;
233     }
234
235     for (; attrs.first != attrs.second; ++attrs.first) {
236         bool caseSensitive = attrs.first->second->isCaseSensitive();
237
238         // Now we have to intersect the attribute's values against the rule's list.
239         const vector<string>& vals = attrs.first->second->getSerializedValues();
240         for (vector<string>::const_iterator i=m_vals.begin(); i!=m_vals.end(); ++i) {
241             for (vector<string>::const_iterator j=vals.begin(); j!=vals.end(); ++j) {
242                 if ((caseSensitive && *i == *j) || (!caseSensitive && !strcasecmp(i->c_str(),j->c_str()))) {
243                     request.log(SPRequest::SPDebug, string("AccessControl plugin expecting (") + *j + "), authz granted");
244                     return shib_acl_true;
245                 }
246             }
247         }
248     }
249
250     return shib_acl_false;
251 }
252
253 RuleRegex::RuleRegex(const DOMElement* e) : m_exp(toUTF8(e->hasChildNodes() ? e->getFirstChild()->getNodeValue() : NULL))
254 {
255     auto_ptr_char req(e->getAttributeNS(NULL,require));
256     if (!req.get() || !*req.get() || !m_exp.get() || !*m_exp.get())
257         throw ConfigurationException("Access control rule missing require attribute or element content.");
258     m_alias=req.get();
259
260     const XMLCh* flag = e->getAttributeNS(NULL,ignoreCase);
261     bool ignore = (flag && (*flag == chLatin_t || *flag == chDigit_1));
262     try {
263         m_re = new RegularExpression(e->getFirstChild()->getNodeValue(), (ignore ? ignoreOption : &chNull));
264     }
265     catch (XMLException& ex) {
266         auto_ptr_char tmp(ex.getMessage());
267         throw ConfigurationException("Caught exception while parsing RuleRegex regular expression: $1", params(1,tmp.get()));
268     }
269 }
270
271 AccessControl::aclresult_t RuleRegex::authorized(const SPRequest& request, const Session* session) const
272 {
273     // Map alias in rule to the attribute.
274     if (!session) {
275         request.log(SPRequest::SPWarn, "AccessControl plugin not given a valid session to evaluate, are you using lazy sessions?");
276         return shib_acl_false;
277     }
278
279     if (m_alias == "valid-user") {
280         if (session) {
281             request.log(SPRequest::SPDebug,"AccessControl plugin accepting valid-user based on active session");
282             return shib_acl_true;
283         }
284         return shib_acl_false;
285     }
286
287     try {
288         if (m_alias == "user") {
289             if (m_re->matches(request.getRemoteUser().c_str())) {
290                 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting REMOTE_USER (") + m_exp.get() + "), authz granted");
291                 return shib_acl_true;
292             }
293             return shib_acl_false;
294         }
295         else if (m_alias == "authnContextClassRef") {
296             if (session->getAuthnContextClassRef() && m_re->matches(session->getAuthnContextClassRef())) {
297                 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting authnContextClassRef (") + m_exp.get() + "), authz granted");
298                 return shib_acl_true;
299             }
300             return shib_acl_false;
301         }
302         else if (m_alias == "authnContextDeclRef") {
303             if (session->getAuthnContextDeclRef() && m_re->matches(session->getAuthnContextDeclRef())) {
304                 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting authnContextDeclRef (") + m_exp.get() + "), authz granted");
305                 return shib_acl_true;
306             }
307             return shib_acl_false;
308         }
309
310         // Find the attribute(s) matching the require rule.
311         pair<multimap<string,const Attribute*>::const_iterator, multimap<string,const Attribute*>::const_iterator> attrs =
312             session->getIndexedAttributes().equal_range(m_alias);
313         if (attrs.first == attrs.second) {
314             request.log(SPRequest::SPWarn, string("rule requires attribute (") + m_alias + "), not found in session");
315             return shib_acl_false;
316         }
317
318         for (; attrs.first != attrs.second; ++attrs.first) {
319             // Now we have to intersect the attribute's values against the regular expression.
320             const vector<string>& vals = attrs.first->second->getSerializedValues();
321             for (vector<string>::const_iterator j=vals.begin(); j!=vals.end(); ++j) {
322                 if (m_re->matches(j->c_str())) {
323                     request.log(SPRequest::SPDebug, string("AccessControl plugin expecting (") + m_exp.get() + "), authz granted");
324                     return shib_acl_true;
325                 }
326             }
327         }
328     }
329     catch (XMLException& ex) {
330         auto_ptr_char tmp(ex.getMessage());
331         request.log(SPRequest::SPError, string("caught exception while parsing RuleRegex regular expression: ") + tmp.get());
332     }
333
334     return shib_acl_false;
335 }
336
337 Operator::Operator(const DOMElement* e)
338 {
339     if (XMLString::equals(e->getLocalName(),NOT))
340         m_op=OP_NOT;
341     else if (XMLString::equals(e->getLocalName(),AND))
342         m_op=OP_AND;
343     else if (XMLString::equals(e->getLocalName(),OR))
344         m_op=OP_OR;
345     else
346         throw ConfigurationException("Unrecognized operator in access control rule");
347
348     try {
349         e=XMLHelper::getFirstChildElement(e);
350         if (XMLString::equals(e->getLocalName(),_Rule))
351             m_operands.push_back(new Rule(e));
352         else if (XMLString::equals(e->getLocalName(),_RuleRegex))
353             m_operands.push_back(new RuleRegex(e));
354         else
355             m_operands.push_back(new Operator(e));
356
357         if (m_op==OP_NOT)
358             return;
359
360         e=XMLHelper::getNextSiblingElement(e);
361         while (e) {
362             if (XMLString::equals(e->getLocalName(),_Rule))
363                 m_operands.push_back(new Rule(e));
364             else if (XMLString::equals(e->getLocalName(),_RuleRegex))
365                 m_operands.push_back(new RuleRegex(e));
366             else
367                 m_operands.push_back(new Operator(e));
368             e=XMLHelper::getNextSiblingElement(e);
369         }
370     }
371     catch (exception&) {
372         for_each(m_operands.begin(),m_operands.end(),xmltooling::cleanup<AccessControl>());
373         throw;
374     }
375 }
376
377 Operator::~Operator()
378 {
379     for_each(m_operands.begin(),m_operands.end(),xmltooling::cleanup<AccessControl>());
380 }
381
382 AccessControl::aclresult_t Operator::authorized(const SPRequest& request, const Session* session) const
383 {
384     switch (m_op) {
385         case OP_NOT:
386             switch (m_operands.front()->authorized(request,session)) {
387                 case shib_acl_true:
388                     return shib_acl_false;
389                 case shib_acl_false:
390                     return shib_acl_true;
391                 default:
392                     return shib_acl_indeterminate;
393             }
394
395         case OP_AND:
396         {
397             for (vector<AccessControl*>::const_iterator i=m_operands.begin(); i!=m_operands.end(); i++) {
398                 if ((*i)->authorized(request,session) != shib_acl_true)
399                     return shib_acl_false;
400             }
401             return shib_acl_true;
402         }
403
404         case OP_OR:
405         {
406             for (vector<AccessControl*>::const_iterator i=m_operands.begin(); i!=m_operands.end(); i++) {
407                 if ((*i)->authorized(request,session) == shib_acl_true)
408                     return shib_acl_true;
409             }
410             return shib_acl_false;
411         }
412     }
413     request.log(SPRequest::SPWarn,"unknown operation in access control policy, denying access");
414     return shib_acl_false;
415 }
416
417 pair<bool,DOMElement*> XMLAccessControl::load()
418 {
419     // Load from source using base class.
420     pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
421
422     // If we own it, wrap it.
423     XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
424
425     // Check for AccessControl wrapper and drop a level.
426     if (XMLString::equals(raw.second->getLocalName(),_AccessControl))
427         raw.second = XMLHelper::getFirstChildElement(raw.second);
428
429     AccessControl* authz;
430     if (XMLString::equals(raw.second->getLocalName(),_Rule))
431         authz=new Rule(raw.second);
432     else if (XMLString::equals(raw.second->getLocalName(),_RuleRegex))
433         authz=new RuleRegex(raw.second);
434     else
435         authz=new Operator(raw.second);
436
437     delete m_rootAuthz;
438     m_rootAuthz = authz;
439     return make_pair(false,(DOMElement*)NULL);
440 }
441
442 AccessControl::aclresult_t XMLAccessControl::authorized(const SPRequest& request, const Session* session) const
443 {
444     return m_rootAuthz ? m_rootAuthz->authorized(request,session) : shib_acl_false;
445 }