6058eff2bc89bf9b8bb76720675560e2fcb9e136
[shibboleth/sp.git] / shibsp / impl / XMLAccessControl.cpp
1 /*\r
2  *  Copyright 2001-2007 Internet2\r
3  * \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
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  */\r
16 \r
17 /**\r
18  * XMLAccessControl.cpp\r
19  *\r
20  * XML-based access control syntax\r
21  */\r
22 \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
29 \r
30 #include <xmltooling/util/ReloadableXMLFile.h>\r
31 #include <xmltooling/util/XMLHelper.h>\r
32 #include <xercesc/util/XMLUniDefs.hpp>\r
33 \r
34 #ifndef HAVE_STRCASECMP\r
35 # define strcasecmp _stricmp\r
36 #endif\r
37 \r
38 using namespace shibsp;\r
39 using namespace xmltooling;\r
40 using namespace std;\r
41 \r
42 namespace {\r
43     \r
44     class Rule : public AccessControl\r
45     {\r
46     public:\r
47         Rule(const DOMElement* e);\r
48         ~Rule() {}\r
49 \r
50         Lockable* lock() {return this;}\r
51         void unlock() {}\r
52 \r
53         bool authorized(const SPRequest& request, const Session* session) const;\r
54     \r
55     private:\r
56         string m_alias;\r
57         vector <string> m_vals;\r
58     };\r
59     \r
60     class Operator : public AccessControl\r
61     {\r
62     public:\r
63         Operator(const DOMElement* e);\r
64         ~Operator();\r
65 \r
66         Lockable* lock() {return this;}\r
67         void unlock() {}\r
68 \r
69         bool authorized(const SPRequest& request, const Session* session) const;\r
70         \r
71     private:\r
72         enum operator_t { OP_NOT, OP_AND, OP_OR } m_op;\r
73         vector<AccessControl*> m_operands;\r
74     };\r
75 \r
76 #if defined (_MSC_VER)\r
77     #pragma warning( push )\r
78     #pragma warning( disable : 4250 )\r
79 #endif\r
80 \r
81     class XMLAccessControl : public AccessControl, public ReloadableXMLFile\r
82     {\r
83     public:\r
84         XMLAccessControl(const DOMElement* e) : ReloadableXMLFile(e), m_rootAuthz(NULL) {\r
85             load(); // guarantees an exception or the policy is loaded\r
86         }\r
87         \r
88         ~XMLAccessControl() {\r
89             delete m_rootAuthz;\r
90         }\r
91 \r
92         bool authorized(const SPRequest& request, const Session* session) const;\r
93 \r
94     protected:\r
95         pair<bool,DOMElement*> load();\r
96 \r
97     private:\r
98         AccessControl* m_rootAuthz;\r
99     };\r
100 \r
101 #if defined (_MSC_VER)\r
102     #pragma warning( pop )\r
103 #endif\r
104 \r
105     AccessControl* SHIBSP_DLLLOCAL XMLAccessControlFactory(const DOMElement* const & e)\r
106     {\r
107         return new XMLAccessControl(e);\r
108     }\r
109 \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
116 }\r
117 \r
118 void SHIBSP_API shibsp::registerAccessControls()\r
119 {\r
120     SPConfig::getConfig().AccessControlManager.registerFactory(XML_ACCESS_CONTROL, XMLAccessControlFactory);\r
121 }\r
122 \r
123 Rule::Rule(const DOMElement* e)\r
124 {\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
128     m_alias=req.get();\r
129     \r
130     xmltooling::auto_ptr_char vals(e->hasChildNodes() ? e->getFirstChild()->getNodeValue() : NULL);\r
131 #ifdef HAVE_STRTOK_R\r
132     char* pos=NULL;\r
133     const char* token=strtok_r(const_cast<char*>(vals.get()),"/",&pos);\r
134 #else\r
135     const char* token=strtok(const_cast<char*>(vals.get()),"/");\r
136 #endif\r
137     while (token) {\r
138         m_vals.push_back(token);\r
139 #ifdef HAVE_STRTOK_R\r
140         token=strtok_r(NULL,"/",&pos);\r
141 #else\r
142         token=strtok(NULL,"/");\r
143 #endif\r
144     }\r
145 }\r
146 \r
147 bool Rule::authorized(const SPRequest& request, const Session* session) const\r
148 {\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
151 \r
152     // Map alias in rule to the attribute.\r
153     if (!session) {\r
154         request.log(SPRequest::SPWarn, "AccessControl plugin not given a valid session to evaluate, are you using lazy sessions?");\r
155         return false;\r
156     }\r
157     \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
162         return false;\r
163     }\r
164 \r
165     bool caseSensitive = attr->second->isCaseSensitive();\r
166 \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
173                 return true;\r
174             }\r
175         }\r
176     }\r
177 \r
178     return false;\r
179 }\r
180 \r
181 Operator::Operator(const DOMElement* e)\r
182 {\r
183     if (XMLString::equals(e->getLocalName(),NOT))\r
184         m_op=OP_NOT;\r
185     else if (XMLString::equals(e->getLocalName(),AND))\r
186         m_op=OP_AND;\r
187     else if (XMLString::equals(e->getLocalName(),OR))\r
188         m_op=OP_OR;\r
189     else\r
190         throw ConfigurationException("Unrecognized operator in access control rule");\r
191     \r
192     try {\r
193         e=XMLHelper::getFirstChildElement(e);\r
194         if (XMLString::equals(e->getLocalName(),_Rule))\r
195             m_operands.push_back(new Rule(e));\r
196         else\r
197             m_operands.push_back(new Operator(e));\r
198         \r
199         if (m_op==OP_NOT)\r
200             return;\r
201         \r
202         e=XMLHelper::getNextSiblingElement(e);\r
203         while (e) {\r
204             if (XMLString::equals(e->getLocalName(),_Rule))\r
205                 m_operands.push_back(new Rule(e));\r
206             else\r
207                 m_operands.push_back(new Operator(e));\r
208             e=XMLHelper::getNextSiblingElement(e);\r
209         }\r
210     }\r
211     catch (exception&) {\r
212         for_each(m_operands.begin(),m_operands.end(),xmltooling::cleanup<AccessControl>());\r
213         throw;\r
214     }\r
215 }\r
216 \r
217 Operator::~Operator()\r
218 {\r
219     for_each(m_operands.begin(),m_operands.end(),xmltooling::cleanup<AccessControl>());\r
220 }\r
221 \r
222 bool Operator::authorized(const SPRequest& request, const Session* session) const\r
223 {\r
224     switch (m_op) {\r
225         case OP_NOT:\r
226             return !m_operands[0]->authorized(request,session);\r
227         \r
228         case OP_AND:\r
229         {\r
230             for (vector<AccessControl*>::const_iterator i=m_operands.begin(); i!=m_operands.end(); i++) {\r
231                 if (!(*i)->authorized(request,session))\r
232                     return false;\r
233             }\r
234             return true;\r
235         }\r
236         \r
237         case OP_OR:\r
238         {\r
239             for (vector<AccessControl*>::const_iterator i=m_operands.begin(); i!=m_operands.end(); i++) {\r
240                 if ((*i)->authorized(request,session))\r
241                     return true;\r
242             }\r
243             return false;\r
244         }\r
245     }\r
246     request.log(SPRequest::SPWarn,"unknown operation in access control policy, denying access");\r
247     return false;\r
248 }\r
249 \r
250 pair<bool,DOMElement*> XMLAccessControl::load()\r
251 {\r
252     // Load from source using base class.\r
253     pair<bool,DOMElement*> raw = ReloadableXMLFile::load();\r
254     \r
255     // If we own it, wrap it.\r
256     XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);\r
257 \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
261     \r
262     AccessControl* authz;\r
263     if (XMLString::equals(raw.second->getLocalName(),_Rule))\r
264         authz=new Rule(raw.second);\r
265     else\r
266         authz=new Operator(raw.second);\r
267 \r
268     delete m_rootAuthz;\r
269     m_rootAuthz = authz;\r
270     return make_pair(false,(DOMElement*)NULL);\r
271 }\r
272 \r
273 bool XMLAccessControl::authorized(const SPRequest& request, const Session* session) const\r
274 {\r
275     return m_rootAuthz ? m_rootAuthz->authorized(request,session) : false;\r
276 }\r