1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Monty Taylor
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.
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.
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
25
#include "drizzled/plugin/authorization.h"
26
#include "drizzled/security_context.h"
27
#include "drizzled/identifier.h"
28
#include "drizzled/error.h"
29
#include "drizzled/session.h"
30
#include "drizzled/gettext.h"
37
vector<plugin::Authorization *> authorization_plugins;
40
bool plugin::Authorization::addPlugin(plugin::Authorization *auth)
43
authorization_plugins.push_back(auth);
47
void plugin::Authorization::removePlugin(plugin::Authorization *auth)
51
authorization_plugins.erase(find(authorization_plugins.begin(),
52
authorization_plugins.end(),
60
class RestrictDbFunctor :
61
public unary_function<plugin::Authorization *, bool>
63
const SecurityContext &user_ctx;
64
SchemaIdentifier &schema;
66
RestrictDbFunctor(const SecurityContext &user_ctx_arg,
67
SchemaIdentifier &schema_arg) :
68
unary_function<plugin::Authorization *, bool>(),
69
user_ctx(user_ctx_arg),
73
inline result_type operator()(argument_type auth)
75
return auth->restrictSchema(user_ctx, schema);
79
class RestrictTableFunctor :
80
public unary_function<plugin::Authorization *, bool>
82
const SecurityContext &user_ctx;
83
TableIdentifier &table;
85
RestrictTableFunctor(const SecurityContext &user_ctx_arg,
86
TableIdentifier &table_arg) :
87
unary_function<plugin::Authorization *, bool>(),
88
user_ctx(user_ctx_arg),
92
inline result_type operator()(argument_type auth)
94
return auth->restrictTable(user_ctx, table);
98
class RestrictProcessFunctor :
99
public unary_function<plugin::Authorization *, bool>
101
const SecurityContext &user_ctx;
102
const SecurityContext &session_ctx;
104
RestrictProcessFunctor(const SecurityContext &user_ctx_arg,
105
const SecurityContext &session_ctx_arg) :
106
unary_function<plugin::Authorization *, bool>(),
107
user_ctx(user_ctx_arg),
108
session_ctx(session_ctx_arg)
111
inline result_type operator()(argument_type auth)
113
return auth->restrictProcess(user_ctx, session_ctx);
117
class PruneSchemaFunctor :
118
public unary_function<SchemaIdentifier&, bool>
120
const SecurityContext &user_ctx;
122
PruneSchemaFunctor(const SecurityContext &user_ctx_arg) :
123
unary_function<SchemaIdentifier&, bool>(),
124
user_ctx(user_ctx_arg)
127
inline result_type operator()(argument_type auth)
129
return not plugin::Authorization::isAuthorized(user_ctx, auth, false);
135
bool plugin::Authorization::isAuthorized(const SecurityContext &user_ctx,
136
SchemaIdentifier &schema_identifier,
139
/* If we never loaded any authorization plugins, just return true */
140
if (authorization_plugins.empty())
143
/* Use find_if instead of foreach so that we can collect return codes */
144
vector<plugin::Authorization *>::const_iterator iter=
145
find_if(authorization_plugins.begin(),
146
authorization_plugins.end(),
147
RestrictDbFunctor(user_ctx, schema_identifier));
151
* If iter is == end() here, that means that all of the plugins returned
152
* false, which means that that each of them believe the user is authorized
153
* to view the resource in question.
155
if (iter != authorization_plugins.end())
159
my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
160
user_ctx.getUser().c_str(),
161
user_ctx.getIp().c_str(),
162
schema_identifier.getSQLPath().c_str());
169
bool plugin::Authorization::isAuthorized(const SecurityContext &user_ctx,
170
TableIdentifier &table,
173
/* If we never loaded any authorization plugins, just return true */
174
if (authorization_plugins.empty())
177
/* Use find_if instead of foreach so that we can collect return codes */
178
vector<plugin::Authorization *>::const_iterator iter=
179
find_if(authorization_plugins.begin(),
180
authorization_plugins.end(),
181
RestrictTableFunctor(user_ctx, table));
184
* If iter is == end() here, that means that all of the plugins returned
185
* false, which means that that each of them believe the user is authorized
186
* to view the resource in question.
188
if (iter != authorization_plugins.end())
192
my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
193
user_ctx.getUser().c_str(),
194
user_ctx.getIp().c_str(),
195
table.getSQLPath().c_str());
202
bool plugin::Authorization::isAuthorized(const SecurityContext &user_ctx,
203
const Session *session,
206
const SecurityContext &session_ctx= session->getSecurityContext();
208
/* If we never loaded any authorization plugins, just return true */
209
if (authorization_plugins.empty())
212
/* Use find_if instead of foreach so that we can collect return codes */
213
vector<plugin::Authorization *>::const_iterator iter=
214
find_if(authorization_plugins.begin(),
215
authorization_plugins.end(),
216
RestrictProcessFunctor(user_ctx, session_ctx));
219
* If iter is == end() here, that means that all of the plugins returned
220
* false, which means that that each of them believe the user is authorized
221
* to view the resource in question.
224
if (iter != authorization_plugins.end())
228
my_error(ER_KILL_DENIED_ERROR, MYF(0), session->thread_id);
235
void plugin::Authorization::pruneSchemaNames(const SecurityContext &user_ctx,
236
SchemaIdentifiers &set_of_schemas)
238
/* If we never loaded any authorization plugins, just return true */
239
if (authorization_plugins.empty())
242
set_of_schemas.erase(remove_if(set_of_schemas.begin(),
243
set_of_schemas.end(),
244
PruneSchemaFunctor(user_ctx)),
245
set_of_schemas.end());
248
} /* namespace drizzled */