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"
35
std::vector<plugin::Authorization *> authorization_plugins;
38
bool plugin::Authorization::addPlugin(plugin::Authorization *auth)
41
authorization_plugins.push_back(auth);
46
void plugin::Authorization::removePlugin(plugin::Authorization *auth)
50
authorization_plugins.erase(std::find(authorization_plugins.begin(),
51
authorization_plugins.end(),
59
class RestrictDbFunctor :
60
public std::unary_function<plugin::Authorization *, bool>
62
const SecurityContext &user_ctx;
63
SchemaIdentifier::const_reference schema;
66
RestrictDbFunctor(const SecurityContext &user_ctx_arg,
67
SchemaIdentifier::const_reference schema_arg) :
68
std::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 std::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
std::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 std::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
std::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 std::unary_function<SchemaIdentifier&, bool>
120
const SecurityContext &user_ctx;
122
PruneSchemaFunctor(const SecurityContext &user_ctx_arg) :
123
std::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::const_reference 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
std::vector<plugin::Authorization *>::const_iterator iter=
145
std::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())
160
schema_identifier.getSQLPath(path);
162
my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
163
user_ctx.getUser().c_str(),
164
user_ctx.getIp().c_str(),
172
bool plugin::Authorization::isAuthorized(const SecurityContext &user_ctx,
173
TableIdentifier &table,
176
/* If we never loaded any authorization plugins, just return true */
177
if (authorization_plugins.empty())
180
/* Use find_if instead of foreach so that we can collect return codes */
181
std::vector<plugin::Authorization *>::const_iterator iter=
182
std::find_if(authorization_plugins.begin(),
183
authorization_plugins.end(),
184
RestrictTableFunctor(user_ctx, table));
187
* If iter is == end() here, that means that all of the plugins returned
188
* false, which means that that each of them believe the user is authorized
189
* to view the resource in question.
191
if (iter != authorization_plugins.end())
196
table.getSQLPath(path);
198
my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
199
user_ctx.getUser().c_str(),
200
user_ctx.getIp().c_str(),
208
bool plugin::Authorization::isAuthorized(const SecurityContext &user_ctx,
209
const Session *session,
212
const SecurityContext &session_ctx= session->getSecurityContext();
214
/* If we never loaded any authorization plugins, just return true */
215
if (authorization_plugins.empty())
218
/* Use find_if instead of foreach so that we can collect return codes */
219
std::vector<plugin::Authorization *>::const_iterator iter=
220
std::find_if(authorization_plugins.begin(),
221
authorization_plugins.end(),
222
RestrictProcessFunctor(user_ctx, session_ctx));
225
* If iter is == end() here, that means that all of the plugins returned
226
* false, which means that that each of them believe the user is authorized
227
* to view the resource in question.
230
if (iter != authorization_plugins.end())
234
my_error(ER_KILL_DENIED_ERROR, MYF(0), session->thread_id);
241
void plugin::Authorization::pruneSchemaNames(const SecurityContext &user_ctx,
242
SchemaIdentifier::vector &set_of_schemas)
244
/* If we never loaded any authorization plugins, just return true */
245
if (authorization_plugins.empty())
248
set_of_schemas.erase(std::remove_if(set_of_schemas.begin(),
249
set_of_schemas.end(),
250
PruneSchemaFunctor(user_ctx)),
251
set_of_schemas.end());
254
} /* namespace drizzled */