drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
authorization.cc
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2010 Monty Taylor
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <config.h>
22
23#include <vector>
24
25#include <drizzled/plugin/authorization.h>
26#include <drizzled/identifier.h>
27#include <drizzled/error.h>
28#include <drizzled/session.h>
29#include <drizzled/gettext.h>
30
31namespace drizzled
32{
33
34std::vector<plugin::Authorization *> authorization_plugins;
35
36
38{
39 authorization_plugins.push_back(&auth);
40
41 return false;
42}
43
44void plugin::Authorization::removePlugin(plugin::Authorization &auth)
45{
46 authorization_plugins.erase(std::find(authorization_plugins.begin(),
47 authorization_plugins.end(),
48 &auth));
49}
50
51namespace
52{
53
54class RestrictDbFunctor
55{
56 const identifier::User &user_ctx;
57 const identifier::Schema& schema;
58
59public:
60 RestrictDbFunctor(const identifier::User &user_ctx_arg,
61 const identifier::Schema& schema_arg) :
62 user_ctx(user_ctx_arg),
63 schema(schema_arg)
64 { }
65
66 inline bool operator()(plugin::Authorization *auth)
67 {
68 return auth->restrictSchema(user_ctx, schema);
69 }
70};
71
72class RestrictTableFunctor
73{
74 const identifier::User& user_ctx;
75 const identifier::Table& table;
76public:
77 RestrictTableFunctor(const identifier::User& user_ctx_arg,
78 const identifier::Table& table_arg) :
79 user_ctx(user_ctx_arg),
80 table(table_arg)
81 { }
82
83 inline bool operator()(plugin::Authorization *auth)
84 {
85 return auth->restrictTable(user_ctx, table);
86 }
87};
88
89class RestrictProcessFunctor
90{
91 const identifier::User &user_ctx;
92 const identifier::User &session_ctx;
93public:
94 RestrictProcessFunctor(const identifier::User &user_ctx_arg,
95 const identifier::User &session_ctx_arg) :
96 user_ctx(user_ctx_arg),
97 session_ctx(session_ctx_arg)
98 { }
99
100 inline bool operator()(plugin::Authorization *auth)
101 {
102 return auth->restrictProcess(user_ctx, session_ctx);
103 }
104};
105
106class PruneSchemaFunctor
107{
108 const drizzled::identifier::User& user_ctx;
109public:
110 PruneSchemaFunctor(const drizzled::identifier::User& user_ctx_arg) :
111 user_ctx(user_ctx_arg)
112 { }
113
114 inline bool operator()(identifier::Schema& auth)
115 {
116 return not plugin::Authorization::isAuthorized(user_ctx, auth, false);
117 }
118};
119
120} /* namespace */
121
123 const identifier::Schema& schema_identifier,
124 bool send_error)
125{
126 /* If we never loaded any authorization plugins, just return true */
127 if (authorization_plugins.empty())
128 return true;
129
130 /* Use find_if instead of foreach so that we can collect return codes */
131 std::vector<plugin::Authorization *>::const_iterator iter=
132 std::find_if(authorization_plugins.begin(),
133 authorization_plugins.end(),
134 RestrictDbFunctor(user_ctx, schema_identifier));
135
136
137 /*
138 * If iter is == end() here, that means that all of the plugins returned
139 * false, which means that that each of them believe the user is authorized
140 * to view the resource in question.
141 */
142 if (iter != authorization_plugins.end())
143 {
144 if (send_error)
145 {
146 error::access(user_ctx, schema_identifier);
147 }
148 return false;
149 }
150 return true;
151}
152
154 const identifier::Table& table_identifier,
155 bool send_error)
156{
157 /* If we never loaded any authorization plugins, just return true */
158 if (authorization_plugins.empty())
159 return true;
160
161 /* Use find_if instead of foreach so that we can collect return codes */
162 std::vector<plugin::Authorization *>::const_iterator iter=
163 std::find_if(authorization_plugins.begin(),
164 authorization_plugins.end(),
165 RestrictTableFunctor(user_ctx, table_identifier));
166
167 /*
168 * If iter is == end() here, that means that all of the plugins returned
169 * false, which means that that each of them believe the user is authorized
170 * to view the resource in question.
171 */
172 if (iter != authorization_plugins.end())
173 {
174 if (send_error)
175 {
176 error::access(user_ctx, table_identifier);
177 }
178 return false;
179 }
180 return true;
181}
182
184 const Session& session,
185 bool send_error)
186{
187 /* If we never loaded any authorization plugins, just return true */
188 if (authorization_plugins.empty())
189 return true;
190
191 // To make sure we hold the user structure we need to have a shred_ptr so
192 // that we increase the count on the object.
193 drizzled::identifier::user::ptr session_ctx= session.user();
194
195
196 /* Use find_if instead of foreach so that we can collect return codes */
197 std::vector<plugin::Authorization *>::const_iterator iter=
198 std::find_if(authorization_plugins.begin(),
199 authorization_plugins.end(),
200 RestrictProcessFunctor(user_ctx, *session_ctx));
201
202 /*
203 * If iter is == end() here, that means that all of the plugins returned
204 * false, which means that that each of them believe the user is authorized
205 * to view the resource in question.
206 */
207
208 if (iter != authorization_plugins.end())
209 {
210 if (send_error)
211 {
212 my_error(ER_KILL_DENIED_ERROR, MYF(0), session.thread_id);
213 }
214 return false;
215 }
216
217 return true;
218}
219
221 identifier::schema::vector &set_of_schemas)
222{
223 /* If we never loaded any authorization plugins, just return true */
224 if (authorization_plugins.empty())
225 return;
226
227 set_of_schemas.erase(std::remove_if(set_of_schemas.begin(),
228 set_of_schemas.end(),
229 PruneSchemaFunctor(user_ctx)),
230 set_of_schemas.end());
231}
232
233} /* namespace drizzled */
A set of Session members describing the current authenticated user.
static bool isAuthorized(const drizzled::identifier::User &user_ctx, const identifier::Schema &schema_identifier, bool send_error=true)
static void pruneSchemaNames(const drizzled::identifier::User &user_ctx, identifier::schema::vector &set_of_schemas)
static bool addPlugin(plugin::Authorization &auth)
TODO: Rename this file - func.h is stupid.