VS10 solution files, convert from NULL macro to nullptr.
[shibboleth/sp.git] / shibsp / impl / ChainingAccessControl.cpp
1 /*
2  *  Copyright 2009-2010 Internet2
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * ChainingAccessControl.cpp
19  *
20  * Access control plugin that combines other plugins.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "AccessControl.h"
26 #include "SessionCache.h"
27 #include "SPRequest.h"
28
29 #include <algorithm>
30 #include <xmltooling/unicode.h>
31 #include <xmltooling/util/XMLHelper.h>
32 #include <xercesc/util/XMLUniDefs.hpp>
33
34 using namespace shibsp;
35 using namespace xmltooling;
36 using namespace std;
37
38 namespace shibsp {
39
40     class ChainingAccessControl : public AccessControl
41     {
42     public:
43         ChainingAccessControl(const DOMElement* e);
44
45         ~ChainingAccessControl() {
46             for_each(m_ac.begin(), m_ac.end(), xmltooling::cleanup<AccessControl>());
47         }
48
49         Lockable* lock() {
50             for_each(m_ac.begin(), m_ac.end(), mem_fun<Lockable*,Lockable>(&Lockable::lock));
51             return this;
52         }
53         void unlock() {
54             for_each(m_ac.begin(), m_ac.end(), mem_fun<void,Lockable>(&Lockable::unlock));
55         }
56
57         aclresult_t authorized(const SPRequest& request, const Session* session) const;
58
59     private:
60         enum operator_t { OP_AND, OP_OR } m_op;
61         vector<AccessControl*> m_ac;
62     };
63
64     AccessControl* SHIBSP_DLLLOCAL ChainingAccessControlFactory(const DOMElement* const & e)
65     {
66         return new ChainingAccessControl(e);
67     }
68
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);
74
75     extern AccessControl* SHIBSP_DLLLOCAL XMLAccessControlFactory(const DOMElement* const & e);
76 }
77
78 void SHIBSP_API shibsp::registerAccessControls()
79 {
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);
84 }
85
86 AccessControl::AccessControl()
87 {
88 }
89
90 AccessControl::~AccessControl()
91 {
92 }
93
94 ChainingAccessControl::ChainingAccessControl(const DOMElement* e)
95 {
96     const XMLCh* op = e ? e->getAttributeNS(nullptr, _operator) : nullptr;
97     if (XMLString::equals(op, AND))
98         m_op=OP_AND;
99     else if (XMLString::equals(op, OR))
100         m_op=OP_OR;
101     else
102         throw ConfigurationException("Missing or unrecognized operator in Chaining AccessControl configuration.");
103
104     try {
105         e = e ? XMLHelper::getFirstChildElement(e, _AccessControl) : nullptr;
106         while (e) {
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));
111             }
112             e = XMLHelper::getNextSiblingElement(e, _AccessControl);
113         }
114     }
115     catch (exception&) {
116         for_each(m_ac.begin(), m_ac.end(), xmltooling::cleanup<AccessControl>());
117         throw;
118     }
119     if (m_ac.empty())
120         throw ConfigurationException("Chaining AccessControl plugin requires at least one child plugin.");
121 }
122
123 AccessControl::aclresult_t ChainingAccessControl::authorized(const SPRequest& request, const Session* session) const
124 {
125     switch (m_op) {
126         case OP_AND:
127         {
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;
131             }
132             return shib_acl_true;
133         }
134
135         case OP_OR:
136         {
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;
140             }
141             return shib_acl_false;
142         }
143     }
144     request.log(SPRequest::SPWarn, "unknown operation in access control policy, denying access");
145     return shib_acl_false;
146 }