7681e0317c71fba5e13d945335407e56efe38b81
[shibboleth/cpp-sp.git] / shibsp / impl / ChainingAccessControl.cpp
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 /**
22  * ChainingAccessControl.cpp
23  *
24  * Access control plugin that combines other plugins.
25  */
26
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "AccessControl.h"
30 #include "SessionCache.h"
31 #include "SPRequest.h"
32
33 #include <algorithm>
34 #include <boost/ptr_container/ptr_vector.hpp>
35 #include <xmltooling/unicode.h>
36 #include <xmltooling/util/XMLHelper.h>
37 #include <xercesc/util/XMLUniDefs.hpp>
38
39 using namespace shibsp;
40 using namespace xmltooling;
41 using namespace boost;
42 using namespace std;
43
44 namespace shibsp {
45
46     class ChainingAccessControl : public AccessControl
47     {
48     public:
49         ChainingAccessControl(const DOMElement* e);
50
51         ~ChainingAccessControl() {}
52
53         Lockable* lock() {
54             for_each(m_ac.begin(), m_ac.end(), mem_fun_ref<Lockable*,Lockable>(&Lockable::lock));
55             return this;
56         }
57         void unlock() {
58             for_each(m_ac.begin(), m_ac.end(), mem_fun_ref<void,Lockable>(&Lockable::unlock));
59         }
60
61         aclresult_t authorized(const SPRequest& request, const Session* session) const;
62
63     private:
64         enum operator_t { OP_AND, OP_OR } m_op;
65         ptr_vector<AccessControl> m_ac;
66     };
67
68     AccessControl* SHIBSP_DLLLOCAL ChainingAccessControlFactory(const DOMElement* const & e)
69     {
70         return new ChainingAccessControl(e);
71     }
72
73     static const XMLCh _AccessControl[] =   UNICODE_LITERAL_13(A,c,c,e,s,s,C,o,n,t,r,o,l);
74     static const XMLCh _operator[] =        UNICODE_LITERAL_8(o,p,e,r,a,t,o,r);
75     static const XMLCh _type[] =            UNICODE_LITERAL_4(t,y,p,e);
76     static const XMLCh AND[] =              UNICODE_LITERAL_3(A,N,D);
77     static const XMLCh OR[] =               UNICODE_LITERAL_2(O,R);
78
79     extern AccessControl* SHIBSP_DLLLOCAL XMLAccessControlFactory(const DOMElement* const & e);
80 }
81
82 void SHIBSP_API shibsp::registerAccessControls()
83 {
84     SPConfig& conf=SPConfig::getConfig();
85     conf.AccessControlManager.registerFactory(CHAINING_ACCESS_CONTROL, ChainingAccessControlFactory);
86     conf.AccessControlManager.registerFactory(XML_ACCESS_CONTROL, XMLAccessControlFactory);
87     conf.AccessControlManager.registerFactory("edu.internet2.middleware.shibboleth.sp.provider.XMLAccessControl", XMLAccessControlFactory);
88 }
89
90 AccessControl::AccessControl()
91 {
92 }
93
94 AccessControl::~AccessControl()
95 {
96 }
97
98 ChainingAccessControl::ChainingAccessControl(const DOMElement* e) : m_op(OP_AND)
99 {
100     const XMLCh* op = e ? e->getAttributeNS(nullptr, _operator) : nullptr;
101     if (XMLString::equals(op, OR))
102         m_op = OP_OR;
103     else if (op && *op && !XMLString::equals(op, AND))
104         throw ConfigurationException("Missing or unrecognized operator in Chaining AccessControl configuration.");
105
106     e = XMLHelper::getFirstChildElement(e, _AccessControl);
107     while (e) {
108         string t(XMLHelper::getAttrString(e, nullptr, _type));
109         if (!t.empty()) {
110             Category::getInstance(SHIBSP_LOGCAT".AccessControl.Chaining").info("building AccessControl provider of type (%s)...", t.c_str());
111             auto_ptr<AccessControl> np(SPConfig::getConfig().AccessControlManager.newPlugin(t.c_str(), e));
112             m_ac.push_back(np.get());
113             np.release();
114         }
115         e = XMLHelper::getNextSiblingElement(e, _AccessControl);
116     }
117     if (m_ac.empty())
118         throw ConfigurationException("Chaining AccessControl plugin requires at least one child plugin.");
119 }
120
121 AccessControl::aclresult_t ChainingAccessControl::authorized(const SPRequest& request, const Session* session) const
122 {
123     switch (m_op) {
124         case OP_AND:
125         {
126             for (ptr_vector<AccessControl>::const_iterator i = m_ac.begin(); i != m_ac.end(); ++i) {
127                 if (i->authorized(request, session) != shib_acl_true) {
128                     request.log(SPRequest::SPDebug, "embedded AccessControl plugin unsuccessful, denying access");
129                     return shib_acl_false;
130                 }
131             }
132             return shib_acl_true;
133         }
134
135         case OP_OR:
136         {
137             for (ptr_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;
140             }
141             request.log(SPRequest::SPDebug, "all embedded AccessControl plugins unsuccessful, denying access");
142             return shib_acl_false;
143         }
144     }
145     request.log(SPRequest::SPWarn, "unknown operation in access control policy, denying access");
146     return shib_acl_false;
147 }