2 * Copyright 2009-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 * ChainingAccessControl.cpp
20 * Access control plugin that combines other plugins.
24 #include "exceptions.h"
25 #include "AccessControl.h"
26 #include "SessionCache.h"
27 #include "SPRequest.h"
30 #include <xmltooling/unicode.h>
31 #include <xmltooling/util/XMLHelper.h>
32 #include <xercesc/util/XMLUniDefs.hpp>
34 using namespace shibsp;
35 using namespace xmltooling;
40 class ChainingAccessControl : public AccessControl
43 ChainingAccessControl(const DOMElement* e);
45 ~ChainingAccessControl() {
46 for_each(m_ac.begin(), m_ac.end(), xmltooling::cleanup<AccessControl>());
50 for_each(m_ac.begin(), m_ac.end(), mem_fun<Lockable*,Lockable>(&Lockable::lock));
54 for_each(m_ac.begin(), m_ac.end(), mem_fun<void,Lockable>(&Lockable::unlock));
57 aclresult_t authorized(const SPRequest& request, const Session* session) const;
60 enum operator_t { OP_AND, OP_OR } m_op;
61 vector<AccessControl*> m_ac;
64 AccessControl* SHIBSP_DLLLOCAL ChainingAccessControlFactory(const DOMElement* const & e)
66 return new ChainingAccessControl(e);
69 static const XMLCh _AccessControl[] = UNICODE_LITERAL_13(A,c,c,e,s,s,C,o,n,t,r,o,l);
70 static const XMLCh _operator[] = UNICODE_LITERAL_8(o,p,e,r,a,t,o,r);
71 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
72 static const XMLCh AND[] = UNICODE_LITERAL_3(A,N,D);
73 static const XMLCh OR[] = UNICODE_LITERAL_2(O,R);
75 extern AccessControl* SHIBSP_DLLLOCAL XMLAccessControlFactory(const DOMElement* const & e);
78 void SHIBSP_API shibsp::registerAccessControls()
80 SPConfig& conf=SPConfig::getConfig();
81 conf.AccessControlManager.registerFactory(CHAINING_ACCESS_CONTROL, ChainingAccessControlFactory);
82 conf.AccessControlManager.registerFactory(XML_ACCESS_CONTROL, XMLAccessControlFactory);
83 conf.AccessControlManager.registerFactory("edu.internet2.middleware.shibboleth.sp.provider.XMLAccessControl", XMLAccessControlFactory);
86 AccessControl::AccessControl()
90 AccessControl::~AccessControl()
94 ChainingAccessControl::ChainingAccessControl(const DOMElement* e)
96 const XMLCh* op = e ? e->getAttributeNS(nullptr, _operator) : nullptr;
97 if (XMLString::equals(op, AND))
99 else if (XMLString::equals(op, OR))
102 throw ConfigurationException("Missing or unrecognized operator in Chaining AccessControl configuration.");
105 e = e ? XMLHelper::getFirstChildElement(e, _AccessControl) : nullptr;
107 auto_ptr_char type(e->getAttributeNS(nullptr, _type));
108 if (type.get() && *type.get()) {
109 Category::getInstance(SHIBSP_LOGCAT".AccessControl.Chaining").info("building AccessControl provider of type (%s)...", type.get());
110 m_ac.push_back(SPConfig::getConfig().AccessControlManager.newPlugin(type.get(), e));
112 e = XMLHelper::getNextSiblingElement(e, _AccessControl);
116 for_each(m_ac.begin(), m_ac.end(), xmltooling::cleanup<AccessControl>());
120 throw ConfigurationException("Chaining AccessControl plugin requires at least one child plugin.");
123 AccessControl::aclresult_t ChainingAccessControl::authorized(const SPRequest& request, const Session* session) const
128 for (vector<AccessControl*>::const_iterator i=m_ac.begin(); i!=m_ac.end(); ++i) {
129 if ((*i)->authorized(request, session) != shib_acl_true)
130 return shib_acl_false;
132 return shib_acl_true;
137 for (vector<AccessControl*>::const_iterator i=m_ac.begin(); i!=m_ac.end(); ++i) {
138 if ((*i)->authorized(request,session) == shib_acl_true)
139 return shib_acl_true;
141 return shib_acl_false;
144 request.log(SPRequest::SPWarn, "unknown operation in access control policy, denying access");
145 return shib_acl_false;