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.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * XMLAccessControl.cpp
24 * XML-based access control syntax.
28 #include "exceptions.h"
29 #include "AccessControl.h"
30 #include "SessionCache.h"
31 #include "SPRequest.h"
32 #include "attribute/Attribute.h"
35 #include <boost/scoped_ptr.hpp>
36 #include <boost/algorithm/string.hpp>
37 #include <boost/lambda/bind.hpp>
38 #include <boost/lambda/lambda.hpp>
39 #include <boost/ptr_container/ptr_vector.hpp>
40 #include <xmltooling/unicode.h>
41 #include <xmltooling/util/ReloadableXMLFile.h>
42 #include <xmltooling/util/Threads.h>
43 #include <xmltooling/util/XMLHelper.h>
44 #include <xercesc/util/XMLUniDefs.hpp>
45 #include <xercesc/util/regx/RegularExpression.hpp>
47 #ifndef HAVE_STRCASECMP
48 # define strcasecmp _stricmp
51 using namespace shibsp;
52 using namespace xmltooling;
53 using namespace boost::lambda;
54 using namespace boost;
59 class Rule : public AccessControl
62 Rule(const DOMElement* e);
65 Lockable* lock() {return this;}
68 aclresult_t authorized(const SPRequest& request, const Session* session) const;
75 class RuleRegex : public AccessControl
78 RuleRegex(const DOMElement* e);
81 Lockable* lock() {return this;}
84 aclresult_t authorized(const SPRequest& request, const Session* session) const;
88 auto_arrayptr<char> m_exp;
89 scoped_ptr<RegularExpression> m_re;
92 class Operator : public AccessControl
95 Operator(const DOMElement* e);
98 Lockable* lock() {return this;}
101 aclresult_t authorized(const SPRequest& request, const Session* session) const;
104 enum operator_t { OP_NOT, OP_AND, OP_OR } m_op;
105 ptr_vector<AccessControl> m_operands;
108 #if defined (_MSC_VER)
109 #pragma warning( push )
110 #pragma warning( disable : 4250 )
113 class XMLAccessControl : public AccessControl, public ReloadableXMLFile
116 XMLAccessControl(const DOMElement* e)
117 : ReloadableXMLFile(e, Category::getInstance(SHIBSP_LOGCAT".AccessControl.XML")) {
118 background_load(); // guarantees an exception or the policy is loaded
121 ~XMLAccessControl() {
125 aclresult_t authorized(const SPRequest& request, const Session* session) const;
128 pair<bool,DOMElement*> background_load();
131 scoped_ptr<AccessControl> m_rootAuthz;
134 #if defined (_MSC_VER)
135 #pragma warning( pop )
138 AccessControl* SHIBSP_DLLLOCAL XMLAccessControlFactory(const DOMElement* const & e)
140 return new XMLAccessControl(e);
143 static const XMLCh _AccessControl[] = UNICODE_LITERAL_13(A,c,c,e,s,s,C,o,n,t,r,o,l);
144 static const XMLCh ignoreCase[] = UNICODE_LITERAL_10(i,g,n,o,r,e,C,a,s,e);
145 static const XMLCh ignoreOption[] = UNICODE_LITERAL_1(i);
146 static const XMLCh _list[] = UNICODE_LITERAL_4(l,i,s,t);
147 static const XMLCh require[] = UNICODE_LITERAL_7(r,e,q,u,i,r,e);
148 static const XMLCh NOT[] = UNICODE_LITERAL_3(N,O,T);
149 static const XMLCh AND[] = UNICODE_LITERAL_3(A,N,D);
150 static const XMLCh OR[] = UNICODE_LITERAL_2(O,R);
151 static const XMLCh _Rule[] = UNICODE_LITERAL_4(R,u,l,e);
152 static const XMLCh _RuleRegex[] = UNICODE_LITERAL_9(R,u,l,e,R,e,g,e,x);
155 Rule::Rule(const DOMElement* e) : m_alias(XMLHelper::getAttrString(e, nullptr, require))
158 throw ConfigurationException("Access control rule missing require attribute");
160 auto_arrayptr<char> vals(toUTF8(e->hasChildNodes() ? e->getFirstChild()->getNodeValue() : nullptr));
164 bool listflag = XMLHelper::getAttrBool(e, true, _list);
167 m_vals.insert(vals.get());
171 string temp(vals.get());
172 split(m_vals, temp, boost::is_space(), algorithm::token_compress_on);
175 AccessControl::aclresult_t Rule::authorized(const SPRequest& request, const Session* session) const
177 // We can make this more complex later using pluggable comparison functions,
178 // but for now, just a straight port to the new Attribute API.
180 // Map alias in rule to the attribute.
182 request.log(SPRequest::SPWarn, "AccessControl plugin not given a valid session to evaluate, are you using lazy sessions?");
183 return shib_acl_false;
186 if (m_alias == "valid-user") {
188 request.log(SPRequest::SPDebug,"AccessControl plugin accepting valid-user based on active session");
189 return shib_acl_true;
191 return shib_acl_false;
193 if (m_alias == "user") {
194 if (m_vals.find(request.getRemoteUser()) != m_vals.end()) {
195 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting REMOTE_USER (") + request.getRemoteUser() + "), authz granted");
196 return shib_acl_true;
198 return shib_acl_false;
200 else if (m_alias == "authnContextClassRef") {
201 const char* ref = session->getAuthnContextClassRef();
202 if (ref && m_vals.find(ref) != m_vals.end()) {
203 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting authnContextClassRef (") + ref + "), authz granted");
204 return shib_acl_true;
206 return shib_acl_false;
208 else if (m_alias == "authnContextDeclRef") {
209 const char* ref = session->getAuthnContextDeclRef();
210 if (ref && m_vals.find(ref) != m_vals.end()) {
211 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting authnContextDeclRef (") + ref + "), authz granted");
212 return shib_acl_true;
214 return shib_acl_false;
217 // Find the attribute(s) matching the require rule.
218 pair<multimap<string,const Attribute*>::const_iterator, multimap<string,const Attribute*>::const_iterator> attrs =
219 session->getIndexedAttributes().equal_range(m_alias);
220 if (attrs.first == attrs.second) {
221 request.log(SPRequest::SPWarn, string("rule requires attribute (") + m_alias + "), not found in session");
222 return shib_acl_false;
225 for (; attrs.first != attrs.second; ++attrs.first) {
226 bool caseSensitive = attrs.first->second->isCaseSensitive();
228 // Now we have to intersect the attribute's values against the rule's list.
229 const vector<string>& vals = attrs.first->second->getSerializedValues();
230 for (set<string>::const_iterator i = m_vals.begin(); i != m_vals.end(); ++i) {
231 for (vector<string>::const_iterator j = vals.begin(); j != vals.end(); ++j) {
232 if ((caseSensitive && *i == *j) || (!caseSensitive && !strcasecmp(i->c_str(),j->c_str()))) {
233 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting (") + *j + "), authz granted");
234 return shib_acl_true;
240 return shib_acl_false;
243 RuleRegex::RuleRegex(const DOMElement* e)
244 : m_alias(XMLHelper::getAttrString(e, nullptr, require)),
245 m_exp(toUTF8(e->hasChildNodes() ? e->getFirstChild()->getNodeValue() : nullptr))
247 if (m_alias.empty() || !m_exp.get() || !*m_exp.get())
248 throw ConfigurationException("Access control rule missing require attribute or element content.");
250 bool ignore = XMLHelper::getAttrBool(e, false, ignoreCase);
252 m_re.reset(new RegularExpression(e->getFirstChild()->getNodeValue(), (ignore ? ignoreOption : &chNull)));
254 catch (XMLException& ex) {
255 auto_ptr_char tmp(ex.getMessage());
256 throw ConfigurationException("Caught exception while parsing RuleRegex regular expression: $1", params(1,tmp.get()));
260 AccessControl::aclresult_t RuleRegex::authorized(const SPRequest& request, const Session* session) const
262 // Map alias in rule to the attribute.
264 request.log(SPRequest::SPWarn, "AccessControl plugin not given a valid session to evaluate, are you using lazy sessions?");
265 return shib_acl_false;
268 if (m_alias == "valid-user") {
270 request.log(SPRequest::SPDebug,"AccessControl plugin accepting valid-user based on active session");
271 return shib_acl_true;
273 return shib_acl_false;
277 if (m_alias == "user") {
278 if (m_re->matches(request.getRemoteUser().c_str())) {
279 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting REMOTE_USER (") + m_exp.get() + "), authz granted");
280 return shib_acl_true;
282 return shib_acl_false;
284 else if (m_alias == "authnContextClassRef") {
285 if (session->getAuthnContextClassRef() && m_re->matches(session->getAuthnContextClassRef())) {
286 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting authnContextClassRef (") + m_exp.get() + "), authz granted");
287 return shib_acl_true;
289 return shib_acl_false;
291 else if (m_alias == "authnContextDeclRef") {
292 if (session->getAuthnContextDeclRef() && m_re->matches(session->getAuthnContextDeclRef())) {
293 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting authnContextDeclRef (") + m_exp.get() + "), authz granted");
294 return shib_acl_true;
296 return shib_acl_false;
299 // Find the attribute(s) matching the require rule.
300 pair<multimap<string,const Attribute*>::const_iterator, multimap<string,const Attribute*>::const_iterator> attrs =
301 session->getIndexedAttributes().equal_range(m_alias);
302 if (attrs.first == attrs.second) {
303 request.log(SPRequest::SPWarn, string("rule requires attribute (") + m_alias + "), not found in session");
304 return shib_acl_false;
307 for (; attrs.first != attrs.second; ++attrs.first) {
308 // Now we have to intersect the attribute's values against the regular expression.
309 const vector<string>& vals = attrs.first->second->getSerializedValues();
310 for (vector<string>::const_iterator j = vals.begin(); j != vals.end(); ++j) {
311 if (m_re->matches(j->c_str())) {
312 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting (") + m_exp.get() + "), authz granted");
313 return shib_acl_true;
318 catch (XMLException& ex) {
319 auto_ptr_char tmp(ex.getMessage());
320 request.log(SPRequest::SPError, string("caught exception while parsing RuleRegex regular expression: ") + tmp.get());
323 return shib_acl_false;
326 Operator::Operator(const DOMElement* e)
328 if (XMLString::equals(e->getLocalName(),NOT))
330 else if (XMLString::equals(e->getLocalName(),AND))
332 else if (XMLString::equals(e->getLocalName(),OR))
335 throw ConfigurationException("Unrecognized operator in access control rule");
337 e=XMLHelper::getFirstChildElement(e);
338 if (XMLString::equals(e->getLocalName(),_Rule))
339 m_operands.push_back(new Rule(e));
340 else if (XMLString::equals(e->getLocalName(),_RuleRegex))
341 m_operands.push_back(new RuleRegex(e));
343 m_operands.push_back(new Operator(e));
348 e=XMLHelper::getNextSiblingElement(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));
355 m_operands.push_back(new Operator(e));
356 e=XMLHelper::getNextSiblingElement(e);
360 AccessControl::aclresult_t Operator::authorized(const SPRequest& request, const Session* session) const
364 switch (m_operands.front().authorized(request,session)) {
366 return shib_acl_false;
368 return shib_acl_true;
370 return shib_acl_indeterminate;
375 // Look for a rule that returns non-true.
376 ptr_vector<AccessControl>::const_iterator i = find_if(
377 m_operands.begin(), m_operands.end(),
378 lambda::bind(&AccessControl::authorized, _1, boost::ref(request), session) != shib_acl_true
380 return (i != m_operands.end()) ? shib_acl_false : shib_acl_true;
385 // Look for a rule that returns true.
386 ptr_vector<AccessControl>::const_iterator i = find_if(
387 m_operands.begin(), m_operands.end(),
388 lambda::bind(&AccessControl::authorized, _1, boost::ref(request), session) == shib_acl_true
390 return (i != m_operands.end()) ? shib_acl_true : shib_acl_false;
393 request.log(SPRequest::SPWarn,"unknown operation in access control policy, denying access");
394 return shib_acl_false;
397 pair<bool,DOMElement*> XMLAccessControl::background_load()
399 // Load from source using base class.
400 pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
402 // If we own it, wrap it.
403 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : nullptr);
405 // Check for AccessControl wrapper and drop a level.
406 if (XMLString::equals(raw.second->getLocalName(),_AccessControl))
407 raw.second = XMLHelper::getFirstChildElement(raw.second);
409 scoped_ptr<AccessControl> authz;
410 if (XMLString::equals(raw.second->getLocalName(),_Rule))
411 authz.reset(new Rule(raw.second));
412 else if (XMLString::equals(raw.second->getLocalName(),_RuleRegex))
413 authz.reset(new RuleRegex(raw.second));
415 authz.reset(new Operator(raw.second));
417 // Perform the swap inside a lock.
420 SharedLock locker(m_lock, false);
421 m_rootAuthz.swap(authz);
423 return make_pair(false,(DOMElement*)nullptr);
426 AccessControl::aclresult_t XMLAccessControl::authorized(const SPRequest& request, const Session* session) const
428 return m_rootAuthz ? m_rootAuthz->authorized(request,session) : shib_acl_false;