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) : ReloadableXMLFile(e), m_rootAuthz(NULL) {
\r
85 load(); // guarantees an exception or the policy is loaded
\r
88 ~XMLAccessControl() {
\r
92 bool authorized(const SPRequest& request, const Session* session) const;
\r
95 pair<bool,DOMElement*> load();
\r
98 AccessControl* m_rootAuthz;
\r
101 #if defined (_MSC_VER)
\r
102 #pragma warning( pop )
\r
105 AccessControl* SHIBSP_DLLLOCAL XMLAccessControlFactory(const DOMElement* const & e)
\r
107 return new XMLAccessControl(e);
\r
110 static const XMLCh _AccessControl[] = UNICODE_LITERAL_13(A,c,c,e,s,s,C,o,n,t,r,o,l);
\r
111 static const XMLCh require[] = UNICODE_LITERAL_7(r,e,q,u,i,r,e);
\r
112 static const XMLCh NOT[] = UNICODE_LITERAL_3(N,O,T);
\r
113 static const XMLCh AND[] = UNICODE_LITERAL_3(A,N,D);
\r
114 static const XMLCh OR[] = UNICODE_LITERAL_2(O,R);
\r
115 static const XMLCh _Rule[] = UNICODE_LITERAL_4(R,u,l,e);
\r
118 void SHIBSP_API shibsp::registerAccessControls()
\r
120 SPConfig::getConfig().AccessControlManager.registerFactory(XML_ACCESS_CONTROL, XMLAccessControlFactory);
\r
123 Rule::Rule(const DOMElement* e)
\r
125 xmltooling::auto_ptr_char req(e->getAttributeNS(NULL,require));
\r
126 if (!req.get() || !*req.get())
\r
127 throw ConfigurationException("Access control rule missing require attribute");
\r
130 xmltooling::auto_ptr_char vals(e->hasChildNodes() ? e->getFirstChild()->getNodeValue() : NULL);
\r
131 #ifdef HAVE_STRTOK_R
\r
133 const char* token=strtok_r(const_cast<char*>(vals.get()),"/",&pos);
\r
135 const char* token=strtok(const_cast<char*>(vals.get()),"/");
\r
138 m_vals.push_back(token);
\r
139 #ifdef HAVE_STRTOK_R
\r
140 token=strtok_r(NULL,"/",&pos);
\r
142 token=strtok(NULL,"/");
\r
147 bool Rule::authorized(const SPRequest& request, const Session* session) const
\r
149 // We can make this more complex later using pluggable comparison functions,
\r
150 // but for now, just a straight port to the new Attribute API.
\r
152 // Map alias in rule to the attribute.
\r
154 request.log(SPRequest::SPWarn, "AccessControl plugin not given a valid session to evaluate, are you using lazy sessions?");
\r
158 // Find the attribute matching the require rule.
\r
159 map<string,const Attribute*>::const_iterator attr = session->getAttributes().find(m_alias);
\r
160 if (attr == session->getAttributes().end()) {
\r
161 request.log(SPRequest::SPWarn, string("rule requires attribute (") + m_alias + "), not found in session");
\r
165 bool caseSensitive = attr->second->isCaseSensitive();
\r
167 // Now we have to intersect the attribute's values against the rule's list.
\r
168 const vector<string>& vals = attr->second->getSerializedValues();
\r
169 for (vector<string>::const_iterator i=m_vals.begin(); i!=m_vals.end(); ++i) {
\r
170 for (vector<string>::const_iterator j=vals.begin(); j!=vals.end(); ++j) {
\r
171 if ((caseSensitive && *i == *j) || (!caseSensitive && !strcasecmp(i->c_str(),j->c_str()))) {
\r
172 request.log(SPRequest::SPDebug, string("AccessControl plugin expecting ") + *j + ", authz granted");
\r
181 Operator::Operator(const DOMElement* e)
\r
183 if (XMLString::equals(e->getLocalName(),NOT))
\r
185 else if (XMLString::equals(e->getLocalName(),AND))
\r
187 else if (XMLString::equals(e->getLocalName(),OR))
\r
190 throw ConfigurationException("Unrecognized operator in access control rule");
\r
193 e=XMLHelper::getFirstChildElement(e);
\r
194 if (XMLString::equals(e->getLocalName(),_Rule))
\r
195 m_operands.push_back(new Rule(e));
\r
197 m_operands.push_back(new Operator(e));
\r
202 e=XMLHelper::getNextSiblingElement(e);
\r
204 if (XMLString::equals(e->getLocalName(),_Rule))
\r
205 m_operands.push_back(new Rule(e));
\r
207 m_operands.push_back(new Operator(e));
\r
208 e=XMLHelper::getNextSiblingElement(e);
\r
211 catch (exception&) {
\r
212 for_each(m_operands.begin(),m_operands.end(),xmltooling::cleanup<AccessControl>());
\r
217 Operator::~Operator()
\r
219 for_each(m_operands.begin(),m_operands.end(),xmltooling::cleanup<AccessControl>());
\r
222 bool Operator::authorized(const SPRequest& request, const Session* session) const
\r
226 return !m_operands[0]->authorized(request,session);
\r
230 for (vector<AccessControl*>::const_iterator i=m_operands.begin(); i!=m_operands.end(); i++) {
\r
231 if (!(*i)->authorized(request,session))
\r
239 for (vector<AccessControl*>::const_iterator i=m_operands.begin(); i!=m_operands.end(); i++) {
\r
240 if ((*i)->authorized(request,session))
\r
246 request.log(SPRequest::SPWarn,"unknown operation in access control policy, denying access");
\r
250 pair<bool,DOMElement*> XMLAccessControl::load()
\r
252 // Load from source using base class.
\r
253 pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
\r
255 // If we own it, wrap it.
\r
256 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
\r
258 // Check for AccessControl wrapper and drop a level.
\r
259 if (XMLString::equals(raw.second->getLocalName(),_AccessControl))
\r
260 raw.second = XMLHelper::getFirstChildElement(raw.second);
\r
262 AccessControl* authz;
\r
263 if (XMLString::equals(raw.second->getLocalName(),_Rule))
\r
264 authz=new Rule(raw.second);
\r
266 authz=new Operator(raw.second);
\r
268 delete m_rootAuthz;
\r
269 m_rootAuthz = authz;
\r
270 return make_pair(false,(DOMElement*)NULL);
\r
273 bool XMLAccessControl::authorized(const SPRequest& request, const Session* session) const
\r
275 return m_rootAuthz ? m_rootAuthz->authorized(request,session) : false;
\r