2 * Copyright 2001-2007 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
18 * XMLAccessControl.cpp
\r
20 * XML-based access control syntax
\r
23 #include "internal.h"
\r
24 #include "exceptions.h"
\r
25 #include "AccessControl.h"
\r
26 #include "SessionCache.h"
\r
27 #include "SPRequest.h"
\r
28 #include "attribute/Attribute.h"
\r
30 #include <xmltooling/util/ReloadableXMLFile.h>
\r
31 #include <xmltooling/util/XMLHelper.h>
\r
32 #include <xercesc/util/XMLUniDefs.hpp>
\r
34 #ifndef HAVE_STRCASECMP
\r
35 # define strcasecmp _stricmp
\r
38 using namespace shibsp;
\r
39 using namespace xmltooling;
\r
40 using namespace std;
\r
44 class Rule : public AccessControl
\r
47 Rule(const DOMElement* e);
\r
50 Lockable* lock() {return this;}
\r
53 bool authorized(const SPRequest& request, const Session* session) const;
\r
57 vector <string> m_vals;
\r
60 class Operator : public AccessControl
\r
63 Operator(const DOMElement* e);
\r
66 Lockable* lock() {return this;}
\r
69 bool authorized(const SPRequest& request, const Session* session) const;
\r
72 enum operator_t { OP_NOT, OP_AND, OP_OR } m_op;
\r
73 vector<AccessControl*> m_operands;
\r
76 #if defined (_MSC_VER)
\r
77 #pragma warning( push )
\r
78 #pragma warning( disable : 4250 )
\r
81 class XMLAccessControl : public AccessControl, public ReloadableXMLFile
\r
84 XMLAccessControl(const DOMElement* e)
\r
85 : ReloadableXMLFile(e, log4cpp::Category::getInstance(SHIBSP_LOGCAT".AccessControl")), m_rootAuthz(NULL) {
\r
86 load(); // guarantees an exception or the policy is loaded
\r
89 ~XMLAccessControl() {
\r
93 bool authorized(const SPRequest& request, const Session* session) const;
\r
96 pair<bool,DOMElement*> load();
\r
99 AccessControl* m_rootAuthz;
\r
102 #if defined (_MSC_VER)
\r
103 #pragma warning( pop )
\r
106 AccessControl* SHIBSP_DLLLOCAL XMLAccessControlFactory(const DOMElement* const & e)
\r
108 return new XMLAccessControl(e);
\r
111 static const XMLCh _AccessControl[] = UNICODE_LITERAL_13(A,c,c,e,s,s,C,o,n,t,r,o,l);
\r
112 static const XMLCh require[] = UNICODE_LITERAL_7(r,e,q,u,i,r,e);
\r
113 static const XMLCh NOT[] = UNICODE_LITERAL_3(N,O,T);
\r
114 static const XMLCh AND[] = UNICODE_LITERAL_3(A,N,D);
\r
115 static const XMLCh OR[] = UNICODE_LITERAL_2(O,R);
\r
116 static const XMLCh _Rule[] = UNICODE_LITERAL_4(R,u,l,e);
\r
119 void SHIBSP_API shibsp::registerAccessControls()
\r
121 SPConfig& conf=SPConfig::getConfig();
\r
122 conf.AccessControlManager.registerFactory(XML_ACCESS_CONTROL, XMLAccessControlFactory);
\r
123 conf.AccessControlManager.registerFactory("edu.internet2.middleware.shibboleth.sp.provider.XMLAccessControl", XMLAccessControlFactory);
\r
126 Rule::Rule(const DOMElement* e)
\r
128 xmltooling::auto_ptr_char req(e->getAttributeNS(NULL,require));
\r
129 if (!req.get() || !*req.get())
\r
130 throw ConfigurationException("Access control rule missing require attribute");
\r
133 xmltooling::auto_ptr_char vals(e->hasChildNodes() ? e->getFirstChild()->getNodeValue() : NULL);
\r
134 #ifdef HAVE_STRTOK_R
\r
136 const char* token=strtok_r(const_cast<char*>(vals.get()),"/",&pos);
\r
138 const char* token=strtok(const_cast<char*>(vals.get()),"/");
\r
141 m_vals.push_back(token);
\r
142 #ifdef HAVE_STRTOK_R
\r
143 token=strtok_r(NULL,"/",&pos);
\r
145 token=strtok(NULL,"/");
\r
150 bool Rule::authorized(const SPRequest& request, const Session* session) const
\r
152 // We can make this more complex later using pluggable comparison functions,
\r
153 // but for now, just a straight port to the new Attribute API.
\r
155 // Map alias in rule to the attribute.
\r
157 request.log(SPRequest::SPWarn, "AccessControl plugin not given a valid session to evaluate, are you using lazy sessions?");
\r
161 // Find the attribute(s) matching the require rule.
\r
162 pair<multimap<string,Attribute*>::const_iterator, multimap<string,Attribute*>::const_iterator> attrs =
\r
163 session->getAttributes().equal_range(m_alias);
\r
164 if (attrs.first == attrs.second) {
\r
165 request.log(SPRequest::SPWarn, string("rule requires attribute (") + m_alias + "), not found in session");
\r
169 for (; attrs.first != attrs.second; ++attrs.first) {
\r
170 bool caseSensitive = attrs.first->second->isCaseSensitive();
\r
172 // Now we have to intersect the attribute's values against the rule's list.
\r
173 const vector<string>& vals = attrs.first->second->getSerializedValues();
\r
174 for (vector<string>::const_iterator i=m_vals.begin(); i!=m_vals.end(); ++i) {
\r
175 for (vector<string>::const_iterator j=vals.begin(); j!=vals.end(); ++j) {
\r
176 if ((caseSensitive && *i == *j) || (!caseSensitive && !strcasecmp(i->c_str(),j->c_str()))) {
\r
177 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting ") + *j + ", authz granted");
\r
187 Operator::Operator(const DOMElement* e)
\r
189 if (XMLString::equals(e->getLocalName(),NOT))
\r
191 else if (XMLString::equals(e->getLocalName(),AND))
\r
193 else if (XMLString::equals(e->getLocalName(),OR))
\r
196 throw ConfigurationException("Unrecognized operator in access control rule");
\r
199 e=XMLHelper::getFirstChildElement(e);
\r
200 if (XMLString::equals(e->getLocalName(),_Rule))
\r
201 m_operands.push_back(new Rule(e));
\r
203 m_operands.push_back(new Operator(e));
\r
208 e=XMLHelper::getNextSiblingElement(e);
\r
210 if (XMLString::equals(e->getLocalName(),_Rule))
\r
211 m_operands.push_back(new Rule(e));
\r
213 m_operands.push_back(new Operator(e));
\r
214 e=XMLHelper::getNextSiblingElement(e);
\r
217 catch (exception&) {
\r
218 for_each(m_operands.begin(),m_operands.end(),xmltooling::cleanup<AccessControl>());
\r
223 Operator::~Operator()
\r
225 for_each(m_operands.begin(),m_operands.end(),xmltooling::cleanup<AccessControl>());
\r
228 bool Operator::authorized(const SPRequest& request, const Session* session) const
\r
232 return !m_operands[0]->authorized(request,session);
\r
236 for (vector<AccessControl*>::const_iterator i=m_operands.begin(); i!=m_operands.end(); i++) {
\r
237 if (!(*i)->authorized(request,session))
\r
245 for (vector<AccessControl*>::const_iterator i=m_operands.begin(); i!=m_operands.end(); i++) {
\r
246 if ((*i)->authorized(request,session))
\r
252 request.log(SPRequest::SPWarn,"unknown operation in access control policy, denying access");
\r
256 pair<bool,DOMElement*> XMLAccessControl::load()
\r
258 // Load from source using base class.
\r
259 pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
\r
261 // If we own it, wrap it.
\r
262 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
\r
264 // Check for AccessControl wrapper and drop a level.
\r
265 if (XMLString::equals(raw.second->getLocalName(),_AccessControl))
\r
266 raw.second = XMLHelper::getFirstChildElement(raw.second);
\r
268 AccessControl* authz;
\r
269 if (XMLString::equals(raw.second->getLocalName(),_Rule))
\r
270 authz=new Rule(raw.second);
\r
272 authz=new Operator(raw.second);
\r
274 delete m_rootAuthz;
\r
275 m_rootAuthz = authz;
\r
276 return make_pair(false,(DOMElement*)NULL);
\r
279 bool XMLAccessControl::authorized(const SPRequest& request, const Session* session) const
\r
281 return m_rootAuthz ? m_rootAuthz->authorized(request,session) : false;
\r