2 * Copyright 2001-2009 Internet2
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
18 * XMLAccessControl.cpp
\r
20 * XML-based access control syntax
\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
30 #include <algorithm>
\r
31 #include <xmltooling/unicode.h>
\r
32 #include <xmltooling/util/ReloadableXMLFile.h>
\r
33 #include <xmltooling/util/XMLHelper.h>
\r
34 #include <xercesc/util/XMLUniDefs.hpp>
\r
35 #include <xercesc/util/regx/RegularExpression.hpp>
\r
37 #ifndef HAVE_STRCASECMP
\r
38 # define strcasecmp _stricmp
\r
41 using namespace shibsp;
\r
42 using namespace xmltooling;
\r
43 using namespace std;
\r
47 class Rule : public AccessControl
\r
50 Rule(const DOMElement* e);
\r
53 Lockable* lock() {return this;}
\r
56 aclresult_t authorized(const SPRequest& request, const Session* session) const;
\r
60 vector <string> m_vals;
\r
63 class RuleRegex : public AccessControl
\r
66 RuleRegex(const DOMElement* e);
\r
71 Lockable* lock() {return this;}
\r
74 aclresult_t authorized(const SPRequest& request, const Session* session) const;
\r
78 auto_arrayptr<char> m_exp;
\r
79 RegularExpression* m_re;
\r
82 class Operator : public AccessControl
\r
85 Operator(const DOMElement* e);
\r
88 Lockable* lock() {return this;}
\r
91 aclresult_t authorized(const SPRequest& request, const Session* session) const;
\r
94 enum operator_t { OP_NOT, OP_AND, OP_OR } m_op;
\r
95 vector<AccessControl*> m_operands;
\r
98 #if defined (_MSC_VER)
\r
99 #pragma warning( push )
\r
100 #pragma warning( disable : 4250 )
\r
103 class XMLAccessControl : public AccessControl, public ReloadableXMLFile
\r
106 XMLAccessControl(const DOMElement* e)
\r
107 : ReloadableXMLFile(e, Category::getInstance(SHIBSP_LOGCAT".AccessControl.XML")), m_rootAuthz(NULL) {
\r
108 load(); // guarantees an exception or the policy is loaded
\r
111 ~XMLAccessControl() {
\r
112 delete m_rootAuthz;
\r
115 aclresult_t authorized(const SPRequest& request, const Session* session) const;
\r
118 pair<bool,DOMElement*> load();
\r
121 AccessControl* m_rootAuthz;
\r
124 #if defined (_MSC_VER)
\r
125 #pragma warning( pop )
\r
128 AccessControl* SHIBSP_DLLLOCAL XMLAccessControlFactory(const DOMElement* const & e)
\r
130 return new XMLAccessControl(e);
\r
133 static const XMLCh _AccessControl[] = UNICODE_LITERAL_13(A,c,c,e,s,s,C,o,n,t,r,o,l);
\r
134 static const XMLCh ignoreCase[] = UNICODE_LITERAL_10(i,g,n,o,r,e,C,a,s,e);
\r
135 static const XMLCh ignoreOption[] = UNICODE_LITERAL_1(i);
\r
136 static const XMLCh _list[] = UNICODE_LITERAL_4(l,i,s,t);
\r
137 static const XMLCh require[] = UNICODE_LITERAL_7(r,e,q,u,i,r,e);
\r
138 static const XMLCh NOT[] = UNICODE_LITERAL_3(N,O,T);
\r
139 static const XMLCh AND[] = UNICODE_LITERAL_3(A,N,D);
\r
140 static const XMLCh OR[] = UNICODE_LITERAL_2(O,R);
\r
141 static const XMLCh _Rule[] = UNICODE_LITERAL_4(R,u,l,e);
\r
142 static const XMLCh _RuleRegex[] = UNICODE_LITERAL_9(R,u,l,e,R,e,g,e,x);
\r
145 Rule::Rule(const DOMElement* e)
\r
147 auto_ptr_char req(e->getAttributeNS(NULL,require));
\r
148 if (!req.get() || !*req.get())
\r
149 throw ConfigurationException("Access control rule missing require attribute");
\r
152 auto_arrayptr<char> vals(toUTF8(e->hasChildNodes() ? e->getFirstChild()->getNodeValue() : NULL));
\r
156 const XMLCh* flag = e->getAttributeNS(NULL,_list);
\r
157 if (flag && (*flag == chLatin_f || *flag == chDigit_0)) {
\r
159 m_vals.push_back(vals.get());
\r
163 #ifdef HAVE_STRTOK_R
\r
165 const char* token=strtok_r(const_cast<char*>(vals.get())," ",&pos);
\r
167 const char* token=strtok(const_cast<char*>(vals.get())," ");
\r
170 m_vals.push_back(token);
\r
171 #ifdef HAVE_STRTOK_R
\r
172 token=strtok_r(NULL," ",&pos);
\r
174 token=strtok(NULL," ");
\r
179 AccessControl::aclresult_t Rule::authorized(const SPRequest& request, const Session* session) const
\r
181 // We can make this more complex later using pluggable comparison functions,
\r
182 // but for now, just a straight port to the new Attribute API.
\r
184 // Map alias in rule to the attribute.
\r
186 request.log(SPRequest::SPWarn, "AccessControl plugin not given a valid session to evaluate, are you using lazy sessions?");
\r
187 return shib_acl_false;
\r
190 if (m_alias == "valid-user") {
\r
192 request.log(SPRequest::SPDebug,"AccessControl plugin accepting valid-user based on active session");
\r
193 return shib_acl_true;
\r
195 return shib_acl_false;
\r
197 if (m_alias == "user") {
\r
198 for (vector<string>::const_iterator i=m_vals.begin(); i!=m_vals.end(); ++i) {
\r
199 if (*i == request.getRemoteUser()) {
\r
200 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting REMOTE_USER (") + *i + "), authz granted");
\r
201 return shib_acl_true;
\r
204 return shib_acl_false;
\r
206 else if (m_alias == "authnContextClassRef") {
\r
207 const char* ref = session->getAuthnContextClassRef();
\r
208 for (vector<string>::const_iterator i=m_vals.begin(); ref && i!=m_vals.end(); ++i) {
\r
209 if (!strcmp(i->c_str(),ref)) {
\r
210 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting authnContextClassRef (") + *i + "), authz granted");
\r
211 return shib_acl_true;
\r
214 return shib_acl_false;
\r
216 else if (m_alias == "authnContextDeclRef") {
\r
217 const char* ref = session->getAuthnContextDeclRef();
\r
218 for (vector<string>::const_iterator i=m_vals.begin(); ref && i!=m_vals.end(); ++i) {
\r
219 if (!strcmp(i->c_str(),ref)) {
\r
220 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting authnContextDeclRef (") + *i + "), authz granted");
\r
221 return shib_acl_true;
\r
224 return shib_acl_false;
\r
227 // Find the attribute(s) matching the require rule.
\r
228 pair<multimap<string,const Attribute*>::const_iterator, multimap<string,const Attribute*>::const_iterator> attrs =
\r
229 session->getIndexedAttributes().equal_range(m_alias);
\r
230 if (attrs.first == attrs.second) {
\r
231 request.log(SPRequest::SPWarn, string("rule requires attribute (") + m_alias + "), not found in session");
\r
232 return shib_acl_false;
\r
235 for (; attrs.first != attrs.second; ++attrs.first) {
\r
236 bool caseSensitive = attrs.first->second->isCaseSensitive();
\r
238 // Now we have to intersect the attribute's values against the rule's list.
\r
239 const vector<string>& vals = attrs.first->second->getSerializedValues();
\r
240 for (vector<string>::const_iterator i=m_vals.begin(); i!=m_vals.end(); ++i) {
\r
241 for (vector<string>::const_iterator j=vals.begin(); j!=vals.end(); ++j) {
\r
242 if ((caseSensitive && *i == *j) || (!caseSensitive && !strcasecmp(i->c_str(),j->c_str()))) {
\r
243 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting (") + *j + "), authz granted");
\r
244 return shib_acl_true;
\r
250 return shib_acl_false;
\r
253 RuleRegex::RuleRegex(const DOMElement* e) : m_exp(toUTF8(e->hasChildNodes() ? e->getFirstChild()->getNodeValue() : NULL))
\r
255 auto_ptr_char req(e->getAttributeNS(NULL,require));
\r
256 if (!req.get() || !*req.get() || !m_exp.get() || !*m_exp.get())
\r
257 throw ConfigurationException("Access control rule missing require attribute or element content.");
\r
260 const XMLCh* flag = e->getAttributeNS(NULL,ignoreCase);
\r
261 bool ignore = (flag && (*flag == chLatin_t || *flag == chDigit_1));
\r
263 m_re = new RegularExpression(e->getFirstChild()->getNodeValue(), (ignore ? ignoreOption : &chNull));
\r
265 catch (XMLException& ex) {
\r
266 auto_ptr_char tmp(ex.getMessage());
\r
267 throw ConfigurationException("Caught exception while parsing RuleRegex regular expression: $1", params(1,tmp.get()));
\r
271 AccessControl::aclresult_t RuleRegex::authorized(const SPRequest& request, const Session* session) const
\r
273 // Map alias in rule to the attribute.
\r
275 request.log(SPRequest::SPWarn, "AccessControl plugin not given a valid session to evaluate, are you using lazy sessions?");
\r
276 return shib_acl_false;
\r
279 if (m_alias == "valid-user") {
\r
281 request.log(SPRequest::SPDebug,"AccessControl plugin accepting valid-user based on active session");
\r
282 return shib_acl_true;
\r
284 return shib_acl_false;
\r
288 if (m_alias == "user") {
\r
289 if (m_re->matches(request.getRemoteUser().c_str())) {
\r
290 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting REMOTE_USER (") + m_exp.get() + "), authz granted");
\r
291 return shib_acl_true;
\r
293 return shib_acl_false;
\r
295 else if (m_alias == "authnContextClassRef") {
\r
296 if (session->getAuthnContextClassRef() && m_re->matches(session->getAuthnContextClassRef())) {
\r
297 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting authnContextClassRef (") + m_exp.get() + "), authz granted");
\r
298 return shib_acl_true;
\r
300 return shib_acl_false;
\r
302 else if (m_alias == "authnContextDeclRef") {
\r
303 if (session->getAuthnContextDeclRef() && m_re->matches(session->getAuthnContextDeclRef())) {
\r
304 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting authnContextDeclRef (") + m_exp.get() + "), authz granted");
\r
305 return shib_acl_true;
\r
307 return shib_acl_false;
\r
310 // Find the attribute(s) matching the require rule.
\r
311 pair<multimap<string,const Attribute*>::const_iterator, multimap<string,const Attribute*>::const_iterator> attrs =
\r
312 session->getIndexedAttributes().equal_range(m_alias);
\r
313 if (attrs.first == attrs.second) {
\r
314 request.log(SPRequest::SPWarn, string("rule requires attribute (") + m_alias + "), not found in session");
\r
315 return shib_acl_false;
\r
318 for (; attrs.first != attrs.second; ++attrs.first) {
\r
319 // Now we have to intersect the attribute's values against the regular expression.
\r
320 const vector<string>& vals = attrs.first->second->getSerializedValues();
\r
321 for (vector<string>::const_iterator j=vals.begin(); j!=vals.end(); ++j) {
\r
322 if (m_re->matches(j->c_str())) {
\r
323 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting (") + m_exp.get() + "), authz granted");
\r
324 return shib_acl_true;
\r
329 catch (XMLException& ex) {
\r
330 auto_ptr_char tmp(ex.getMessage());
\r
331 request.log(SPRequest::SPError, string("caught exception while parsing RuleRegex regular expression: ") + tmp.get());
\r
334 return shib_acl_false;
\r
337 Operator::Operator(const DOMElement* e)
\r
339 if (XMLString::equals(e->getLocalName(),NOT))
\r
341 else if (XMLString::equals(e->getLocalName(),AND))
\r
343 else if (XMLString::equals(e->getLocalName(),OR))
\r
346 throw ConfigurationException("Unrecognized operator in access control rule");
\r
349 e=XMLHelper::getFirstChildElement(e);
\r
350 if (XMLString::equals(e->getLocalName(),_Rule))
\r
351 m_operands.push_back(new Rule(e));
\r
352 else if (XMLString::equals(e->getLocalName(),_RuleRegex))
\r
353 m_operands.push_back(new RuleRegex(e));
\r
355 m_operands.push_back(new Operator(e));
\r
360 e=XMLHelper::getNextSiblingElement(e);
\r
362 if (XMLString::equals(e->getLocalName(),_Rule))
\r
363 m_operands.push_back(new Rule(e));
\r
364 else if (XMLString::equals(e->getLocalName(),_RuleRegex))
\r
365 m_operands.push_back(new RuleRegex(e));
\r
367 m_operands.push_back(new Operator(e));
\r
368 e=XMLHelper::getNextSiblingElement(e);
\r
371 catch (exception&) {
\r
372 for_each(m_operands.begin(),m_operands.end(),xmltooling::cleanup<AccessControl>());
\r
377 Operator::~Operator()
\r
379 for_each(m_operands.begin(),m_operands.end(),xmltooling::cleanup<AccessControl>());
\r
382 AccessControl::aclresult_t Operator::authorized(const SPRequest& request, const Session* session) const
\r
386 switch (m_operands.front()->authorized(request,session)) {
\r
387 case shib_acl_true:
\r
388 return shib_acl_false;
\r
389 case shib_acl_false:
\r
390 return shib_acl_true;
\r
392 return shib_acl_indeterminate;
\r
397 for (vector<AccessControl*>::const_iterator i=m_operands.begin(); i!=m_operands.end(); i++) {
\r
398 if ((*i)->authorized(request,session) != shib_acl_true)
\r
399 return shib_acl_false;
\r
401 return shib_acl_true;
\r
406 for (vector<AccessControl*>::const_iterator i=m_operands.begin(); i!=m_operands.end(); i++) {
\r
407 if ((*i)->authorized(request,session) == shib_acl_true)
\r
408 return shib_acl_true;
\r
410 return shib_acl_false;
\r
413 request.log(SPRequest::SPWarn,"unknown operation in access control policy, denying access");
\r
414 return shib_acl_false;
\r
417 pair<bool,DOMElement*> XMLAccessControl::load()
\r
419 // Load from source using base class.
\r
420 pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
\r
422 // If we own it, wrap it.
\r
423 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
\r
425 // Check for AccessControl wrapper and drop a level.
\r
426 if (XMLString::equals(raw.second->getLocalName(),_AccessControl))
\r
427 raw.second = XMLHelper::getFirstChildElement(raw.second);
\r
429 AccessControl* authz;
\r
430 if (XMLString::equals(raw.second->getLocalName(),_Rule))
\r
431 authz=new Rule(raw.second);
\r
432 else if (XMLString::equals(raw.second->getLocalName(),_RuleRegex))
\r
433 authz=new RuleRegex(raw.second);
\r
435 authz=new Operator(raw.second);
\r
437 delete m_rootAuthz;
\r
438 m_rootAuthz = authz;
\r
439 return make_pair(false,(DOMElement*)NULL);
\r
442 AccessControl::aclresult_t XMLAccessControl::authorized(const SPRequest& request, const Session* session) const
\r
444 return m_rootAuthz ? m_rootAuthz->authorized(request,session) : shib_acl_false;
\r