2 * Copyright 2001-2010 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * XMLAccessControl.cpp
20 * XML-based access control syntax
24 #include "exceptions.h"
25 #include "AccessControl.h"
26 #include "SessionCache.h"
27 #include "SPRequest.h"
28 #include "attribute/Attribute.h"
31 #include <xmltooling/unicode.h>
32 #include <xmltooling/util/ReloadableXMLFile.h>
33 #include <xmltooling/util/Threads.h>
34 #include <xmltooling/util/XMLHelper.h>
35 #include <xercesc/util/XMLUniDefs.hpp>
36 #include <xercesc/util/regx/RegularExpression.hpp>
38 #ifndef HAVE_STRCASECMP
39 # define strcasecmp _stricmp
42 using namespace shibsp;
43 using namespace xmltooling;
48 class Rule : public AccessControl
51 Rule(const DOMElement* e);
54 Lockable* lock() {return this;}
57 aclresult_t authorized(const SPRequest& request, const Session* session) const;
61 vector <string> m_vals;
64 class RuleRegex : public AccessControl
67 RuleRegex(const DOMElement* e);
72 Lockable* lock() {return this;}
75 aclresult_t authorized(const SPRequest& request, const Session* session) const;
79 auto_arrayptr<char> m_exp;
80 RegularExpression* m_re;
83 class Operator : public AccessControl
86 Operator(const DOMElement* e);
89 Lockable* lock() {return this;}
92 aclresult_t authorized(const SPRequest& request, const Session* session) const;
95 enum operator_t { OP_NOT, OP_AND, OP_OR } m_op;
96 vector<AccessControl*> m_operands;
99 #if defined (_MSC_VER)
100 #pragma warning( push )
101 #pragma warning( disable : 4250 )
104 class XMLAccessControl : public AccessControl, public ReloadableXMLFile
107 XMLAccessControl(const DOMElement* e)
108 : ReloadableXMLFile(e, Category::getInstance(SHIBSP_LOGCAT".AccessControl.XML")), m_rootAuthz(NULL) {
109 background_load(); // guarantees an exception or the policy is loaded
112 ~XMLAccessControl() {
116 aclresult_t authorized(const SPRequest& request, const Session* session) const;
119 pair<bool,DOMElement*> background_load();
122 AccessControl* m_rootAuthz;
125 #if defined (_MSC_VER)
126 #pragma warning( pop )
129 AccessControl* SHIBSP_DLLLOCAL XMLAccessControlFactory(const DOMElement* const & e)
131 return new XMLAccessControl(e);
134 static const XMLCh _AccessControl[] = UNICODE_LITERAL_13(A,c,c,e,s,s,C,o,n,t,r,o,l);
135 static const XMLCh ignoreCase[] = UNICODE_LITERAL_10(i,g,n,o,r,e,C,a,s,e);
136 static const XMLCh ignoreOption[] = UNICODE_LITERAL_1(i);
137 static const XMLCh _list[] = UNICODE_LITERAL_4(l,i,s,t);
138 static const XMLCh require[] = UNICODE_LITERAL_7(r,e,q,u,i,r,e);
139 static const XMLCh NOT[] = UNICODE_LITERAL_3(N,O,T);
140 static const XMLCh AND[] = UNICODE_LITERAL_3(A,N,D);
141 static const XMLCh OR[] = UNICODE_LITERAL_2(O,R);
142 static const XMLCh _Rule[] = UNICODE_LITERAL_4(R,u,l,e);
143 static const XMLCh _RuleRegex[] = UNICODE_LITERAL_9(R,u,l,e,R,e,g,e,x);
146 Rule::Rule(const DOMElement* e)
148 auto_ptr_char req(e->getAttributeNS(NULL,require));
149 if (!req.get() || !*req.get())
150 throw ConfigurationException("Access control rule missing require attribute");
153 auto_arrayptr<char> vals(toUTF8(e->hasChildNodes() ? e->getFirstChild()->getNodeValue() : NULL));
157 const XMLCh* flag = e->getAttributeNS(NULL,_list);
158 if (flag && (*flag == chLatin_f || *flag == chDigit_0)) {
160 m_vals.push_back(vals.get());
166 const char* token=strtok_r(const_cast<char*>(vals.get())," ",&pos);
168 const char* token=strtok(const_cast<char*>(vals.get())," ");
171 m_vals.push_back(token);
173 token=strtok_r(NULL," ",&pos);
175 token=strtok(NULL," ");
180 AccessControl::aclresult_t Rule::authorized(const SPRequest& request, const Session* session) const
182 // We can make this more complex later using pluggable comparison functions,
183 // but for now, just a straight port to the new Attribute API.
185 // Map alias in rule to the attribute.
187 request.log(SPRequest::SPWarn, "AccessControl plugin not given a valid session to evaluate, are you using lazy sessions?");
188 return shib_acl_false;
191 if (m_alias == "valid-user") {
193 request.log(SPRequest::SPDebug,"AccessControl plugin accepting valid-user based on active session");
194 return shib_acl_true;
196 return shib_acl_false;
198 if (m_alias == "user") {
199 for (vector<string>::const_iterator i=m_vals.begin(); i!=m_vals.end(); ++i) {
200 if (*i == request.getRemoteUser()) {
201 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting REMOTE_USER (") + *i + "), authz granted");
202 return shib_acl_true;
205 return shib_acl_false;
207 else if (m_alias == "authnContextClassRef") {
208 const char* ref = session->getAuthnContextClassRef();
209 for (vector<string>::const_iterator i=m_vals.begin(); ref && i!=m_vals.end(); ++i) {
210 if (!strcmp(i->c_str(),ref)) {
211 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting authnContextClassRef (") + *i + "), authz granted");
212 return shib_acl_true;
215 return shib_acl_false;
217 else if (m_alias == "authnContextDeclRef") {
218 const char* ref = session->getAuthnContextDeclRef();
219 for (vector<string>::const_iterator i=m_vals.begin(); ref && i!=m_vals.end(); ++i) {
220 if (!strcmp(i->c_str(),ref)) {
221 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting authnContextDeclRef (") + *i + "), authz granted");
222 return shib_acl_true;
225 return shib_acl_false;
228 // Find the attribute(s) matching the require rule.
229 pair<multimap<string,const Attribute*>::const_iterator, multimap<string,const Attribute*>::const_iterator> attrs =
230 session->getIndexedAttributes().equal_range(m_alias);
231 if (attrs.first == attrs.second) {
232 request.log(SPRequest::SPWarn, string("rule requires attribute (") + m_alias + "), not found in session");
233 return shib_acl_false;
236 for (; attrs.first != attrs.second; ++attrs.first) {
237 bool caseSensitive = attrs.first->second->isCaseSensitive();
239 // Now we have to intersect the attribute's values against the rule's list.
240 const vector<string>& vals = attrs.first->second->getSerializedValues();
241 for (vector<string>::const_iterator i=m_vals.begin(); i!=m_vals.end(); ++i) {
242 for (vector<string>::const_iterator j=vals.begin(); j!=vals.end(); ++j) {
243 if ((caseSensitive && *i == *j) || (!caseSensitive && !strcasecmp(i->c_str(),j->c_str()))) {
244 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting (") + *j + "), authz granted");
245 return shib_acl_true;
251 return shib_acl_false;
254 RuleRegex::RuleRegex(const DOMElement* e) : m_exp(toUTF8(e->hasChildNodes() ? e->getFirstChild()->getNodeValue() : NULL))
256 auto_ptr_char req(e->getAttributeNS(NULL,require));
257 if (!req.get() || !*req.get() || !m_exp.get() || !*m_exp.get())
258 throw ConfigurationException("Access control rule missing require attribute or element content.");
261 const XMLCh* flag = e->getAttributeNS(NULL,ignoreCase);
262 bool ignore = (flag && (*flag == chLatin_t || *flag == chDigit_1));
264 m_re = new RegularExpression(e->getFirstChild()->getNodeValue(), (ignore ? ignoreOption : &chNull));
266 catch (XMLException& ex) {
267 auto_ptr_char tmp(ex.getMessage());
268 throw ConfigurationException("Caught exception while parsing RuleRegex regular expression: $1", params(1,tmp.get()));
272 AccessControl::aclresult_t RuleRegex::authorized(const SPRequest& request, const Session* session) const
274 // Map alias in rule to the attribute.
276 request.log(SPRequest::SPWarn, "AccessControl plugin not given a valid session to evaluate, are you using lazy sessions?");
277 return shib_acl_false;
280 if (m_alias == "valid-user") {
282 request.log(SPRequest::SPDebug,"AccessControl plugin accepting valid-user based on active session");
283 return shib_acl_true;
285 return shib_acl_false;
289 if (m_alias == "user") {
290 if (m_re->matches(request.getRemoteUser().c_str())) {
291 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting REMOTE_USER (") + m_exp.get() + "), authz granted");
292 return shib_acl_true;
294 return shib_acl_false;
296 else if (m_alias == "authnContextClassRef") {
297 if (session->getAuthnContextClassRef() && m_re->matches(session->getAuthnContextClassRef())) {
298 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting authnContextClassRef (") + m_exp.get() + "), authz granted");
299 return shib_acl_true;
301 return shib_acl_false;
303 else if (m_alias == "authnContextDeclRef") {
304 if (session->getAuthnContextDeclRef() && m_re->matches(session->getAuthnContextDeclRef())) {
305 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting authnContextDeclRef (") + m_exp.get() + "), authz granted");
306 return shib_acl_true;
308 return shib_acl_false;
311 // Find the attribute(s) matching the require rule.
312 pair<multimap<string,const Attribute*>::const_iterator, multimap<string,const Attribute*>::const_iterator> attrs =
313 session->getIndexedAttributes().equal_range(m_alias);
314 if (attrs.first == attrs.second) {
315 request.log(SPRequest::SPWarn, string("rule requires attribute (") + m_alias + "), not found in session");
316 return shib_acl_false;
319 for (; attrs.first != attrs.second; ++attrs.first) {
320 // Now we have to intersect the attribute's values against the regular expression.
321 const vector<string>& vals = attrs.first->second->getSerializedValues();
322 for (vector<string>::const_iterator j=vals.begin(); j!=vals.end(); ++j) {
323 if (m_re->matches(j->c_str())) {
324 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting (") + m_exp.get() + "), authz granted");
325 return shib_acl_true;
330 catch (XMLException& ex) {
331 auto_ptr_char tmp(ex.getMessage());
332 request.log(SPRequest::SPError, string("caught exception while parsing RuleRegex regular expression: ") + tmp.get());
335 return shib_acl_false;
338 Operator::Operator(const DOMElement* e)
340 if (XMLString::equals(e->getLocalName(),NOT))
342 else if (XMLString::equals(e->getLocalName(),AND))
344 else if (XMLString::equals(e->getLocalName(),OR))
347 throw ConfigurationException("Unrecognized operator in access control rule");
350 e=XMLHelper::getFirstChildElement(e);
351 if (XMLString::equals(e->getLocalName(),_Rule))
352 m_operands.push_back(new Rule(e));
353 else if (XMLString::equals(e->getLocalName(),_RuleRegex))
354 m_operands.push_back(new RuleRegex(e));
356 m_operands.push_back(new Operator(e));
361 e=XMLHelper::getNextSiblingElement(e);
363 if (XMLString::equals(e->getLocalName(),_Rule))
364 m_operands.push_back(new Rule(e));
365 else if (XMLString::equals(e->getLocalName(),_RuleRegex))
366 m_operands.push_back(new RuleRegex(e));
368 m_operands.push_back(new Operator(e));
369 e=XMLHelper::getNextSiblingElement(e);
373 for_each(m_operands.begin(),m_operands.end(),xmltooling::cleanup<AccessControl>());
378 Operator::~Operator()
380 for_each(m_operands.begin(),m_operands.end(),xmltooling::cleanup<AccessControl>());
383 AccessControl::aclresult_t Operator::authorized(const SPRequest& request, const Session* session) const
387 switch (m_operands.front()->authorized(request,session)) {
389 return shib_acl_false;
391 return shib_acl_true;
393 return shib_acl_indeterminate;
398 for (vector<AccessControl*>::const_iterator i=m_operands.begin(); i!=m_operands.end(); i++) {
399 if ((*i)->authorized(request,session) != shib_acl_true)
400 return shib_acl_false;
402 return shib_acl_true;
407 for (vector<AccessControl*>::const_iterator i=m_operands.begin(); i!=m_operands.end(); i++) {
408 if ((*i)->authorized(request,session) == shib_acl_true)
409 return shib_acl_true;
411 return shib_acl_false;
414 request.log(SPRequest::SPWarn,"unknown operation in access control policy, denying access");
415 return shib_acl_false;
418 pair<bool,DOMElement*> XMLAccessControl::background_load()
420 // Load from source using base class.
421 pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
423 // If we own it, wrap it.
424 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
426 // Check for AccessControl wrapper and drop a level.
427 if (XMLString::equals(raw.second->getLocalName(),_AccessControl))
428 raw.second = XMLHelper::getFirstChildElement(raw.second);
430 AccessControl* authz;
431 if (XMLString::equals(raw.second->getLocalName(),_Rule))
432 authz=new Rule(raw.second);
433 else if (XMLString::equals(raw.second->getLocalName(),_RuleRegex))
434 authz=new RuleRegex(raw.second);
436 authz=new Operator(raw.second);
438 // Perform the swap inside a lock.
441 SharedLock locker(m_lock, false);
445 return make_pair(false,(DOMElement*)NULL);
448 AccessControl::aclresult_t XMLAccessControl::authorized(const SPRequest& request, const Session* session) const
450 return m_rootAuthz ? m_rootAuthz->authorized(request,session) : shib_acl_false;