Change license header.
[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 <xmltooling/unicode.h>
35 #include <xmltooling/util/XMLHelper.h>
36 #include <xercesc/util/XMLUniDefs.hpp>
37
38 using namespace shibsp;
39 using namespace xmltooling;
40 using namespace std;
41
42 namespace shibsp {
43
44     class ChainingAccessControl : public AccessControl
45     {
46     public:
47         ChainingAccessControl(const DOMElement* e);
48
49         ~ChainingAccessControl() {
50             for_each(m_ac.begin(), m_ac.end(), xmltooling::cleanup<AccessControl>());
51         }
52
53         Lockable* lock() {
54             for_each(m_ac.begin(), m_ac.end(), mem_fun<Lockable*,Lockable>(&Lockable::lock));
55             return this;
56         }
57         void unlock() {
58             for_each(m_ac.begin(), m_ac.end(), mem_fun<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         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)
99 {
100     const XMLCh* op = e ? e->getAttributeNS(nullptr, _operator) : nullptr;
101     if (XMLString::equals(op, AND))
102         m_op=OP_AND;
103     else if (XMLString::equals(op, OR))
104         m_op=OP_OR;
105     else
106         throw ConfigurationException("Missing or unrecognized operator in Chaining AccessControl configuration.");
107
108     try {
109         e = XMLHelper::getFirstChildElement(e, _AccessControl);
110         while (e) {
111             string t(XMLHelper::getAttrString(e, nullptr, _type));
112             if (!t.empty()) {
113                 Category::getInstance(SHIBSP_LOGCAT".AccessControl.Chaining").info("building AccessControl provider of type (%s)...", t.c_str());
114                 m_ac.push_back(SPConfig::getConfig().AccessControlManager.newPlugin(t.c_str(), e));
115             }
116             e = XMLHelper::getNextSiblingElement(e, _AccessControl);
117         }
118     }
119     catch (exception&) {
120         for_each(m_ac.begin(), m_ac.end(), xmltooling::cleanup<AccessControl>());
121         throw;
122     }
123     if (m_ac.empty())
124         throw ConfigurationException("Chaining AccessControl plugin requires at least one child plugin.");
125 }
126
127 AccessControl::aclresult_t ChainingAccessControl::authorized(const SPRequest& request, const Session* session) const
128 {
129     switch (m_op) {
130         case OP_AND:
131         {
132             for (vector<AccessControl*>::const_iterator i=m_ac.begin(); i!=m_ac.end(); ++i) {
133                 if ((*i)->authorized(request, session) != shib_acl_true) {
134                     request.log(SPRequest::SPDebug, "embedded AccessControl plugin unsuccessful, denying access");
135                     return shib_acl_false;
136                 }
137             }
138             return shib_acl_true;
139         }
140
141         case OP_OR:
142         {
143             for (vector<AccessControl*>::const_iterator i=m_ac.begin(); i!=m_ac.end(); ++i) {
144                 if ((*i)->authorized(request,session) == shib_acl_true)
145                     return shib_acl_true;
146             }
147             request.log(SPRequest::SPDebug, "all embedded AccessControl plugins unsuccessful, denying access");
148             return shib_acl_false;
149         }
150     }
151     request.log(SPRequest::SPWarn, "unknown operation in access control policy, denying access");
152     return shib_acl_false;
153 }