~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/authorization.cc

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
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/security_context.h"
27
 
#include "drizzled/identifier.h"
28
 
#include "drizzled/error.h"
29
 
#include "drizzled/session.h"
30
 
#include "drizzled/gettext.h"
31
 
 
32
 
namespace drizzled
33
 
{
34
 
 
35
 
std::vector<plugin::Authorization *> authorization_plugins;
36
 
 
37
 
 
38
 
bool plugin::Authorization::addPlugin(plugin::Authorization *auth)
39
 
{
40
 
  if (auth != NULL)
41
 
    authorization_plugins.push_back(auth);
42
 
 
43
 
  return false;
44
 
}
45
 
 
46
 
void plugin::Authorization::removePlugin(plugin::Authorization *auth)
47
 
{
48
 
  if (auth != NULL)
49
 
  {
50
 
    authorization_plugins.erase(std::find(authorization_plugins.begin(),
51
 
                                          authorization_plugins.end(),
52
 
                                          auth));
53
 
  }
54
 
}
55
 
 
56
 
namespace
57
 
{
58
 
 
59
 
class RestrictDbFunctor :
60
 
  public std::unary_function<plugin::Authorization *, bool>
61
 
{
62
 
  const SecurityContext &user_ctx;
63
 
  SchemaIdentifier::const_reference schema;
64
 
 
65
 
public:
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),
70
 
    schema(schema_arg)
71
 
  { }
72
 
 
73
 
  inline result_type operator()(argument_type auth)
74
 
  {
75
 
    return auth->restrictSchema(user_ctx, schema);
76
 
  }
77
 
};
78
 
 
79
 
class RestrictTableFunctor :
80
 
  public std::unary_function<plugin::Authorization *, bool>
81
 
{
82
 
  const SecurityContext &user_ctx;
83
 
  TableIdentifier &table;
84
 
public:
85
 
  RestrictTableFunctor(const SecurityContext &user_ctx_arg,
86
 
                       TableIdentifier &table_arg) :
87
 
    std::unary_function<plugin::Authorization *, bool>(),
88
 
    user_ctx(user_ctx_arg),
89
 
    table(table_arg)
90
 
  { }
91
 
 
92
 
  inline result_type operator()(argument_type auth)
93
 
  {
94
 
    return auth->restrictTable(user_ctx, table);
95
 
  }
96
 
};
97
 
 
98
 
class RestrictProcessFunctor :
99
 
  public std::unary_function<plugin::Authorization *, bool>
100
 
{
101
 
  const SecurityContext &user_ctx;
102
 
  const SecurityContext &session_ctx;
103
 
public:
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)
109
 
  { }
110
 
 
111
 
  inline result_type operator()(argument_type auth)
112
 
  {
113
 
    return auth->restrictProcess(user_ctx, session_ctx);
114
 
  }
115
 
};
116
 
 
117
 
class PruneSchemaFunctor :
118
 
  public std::unary_function<SchemaIdentifier&, bool>
119
 
{
120
 
  const SecurityContext &user_ctx;
121
 
public:
122
 
  PruneSchemaFunctor(const SecurityContext &user_ctx_arg) :
123
 
    std::unary_function<SchemaIdentifier&, bool>(),
124
 
    user_ctx(user_ctx_arg)
125
 
  { }
126
 
 
127
 
  inline result_type operator()(argument_type auth)
128
 
  {
129
 
    return not plugin::Authorization::isAuthorized(user_ctx, auth, false);
130
 
  }
131
 
};
132
 
 
133
 
} /* namespace */
134
 
 
135
 
bool plugin::Authorization::isAuthorized(const SecurityContext &user_ctx,
136
 
                                         SchemaIdentifier::const_reference schema_identifier,
137
 
                                         bool send_error)
138
 
{
139
 
  /* If we never loaded any authorization plugins, just return true */
140
 
  if (authorization_plugins.empty())
141
 
    return true;
142
 
 
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));
148
 
 
149
 
 
150
 
  /*
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.
154
 
   */
155
 
  if (iter != authorization_plugins.end())
156
 
  {
157
 
    if (send_error)
158
 
    {
159
 
      std::string path;
160
 
      schema_identifier.getSQLPath(path);
161
 
 
162
 
      my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
163
 
               user_ctx.getUser().c_str(),
164
 
               user_ctx.getIp().c_str(),
165
 
               path.c_str());
166
 
    }
167
 
    return false;
168
 
  }
169
 
  return true;
170
 
}
171
 
 
172
 
bool plugin::Authorization::isAuthorized(const SecurityContext &user_ctx,
173
 
                                         TableIdentifier &table,
174
 
                                         bool send_error)
175
 
{
176
 
  /* If we never loaded any authorization plugins, just return true */
177
 
  if (authorization_plugins.empty())
178
 
    return true;
179
 
 
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));
185
 
 
186
 
  /*
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.
190
 
   */
191
 
  if (iter != authorization_plugins.end())
192
 
  {
193
 
    if (send_error)
194
 
    {
195
 
      std::string path;
196
 
      table.getSQLPath(path);
197
 
 
198
 
      my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
199
 
               user_ctx.getUser().c_str(),
200
 
               user_ctx.getIp().c_str(),
201
 
               path.c_str());
202
 
    }
203
 
    return false;
204
 
  }
205
 
  return true;
206
 
}
207
 
 
208
 
bool plugin::Authorization::isAuthorized(const SecurityContext &user_ctx,
209
 
                                         const Session *session,
210
 
                                         bool send_error)
211
 
{
212
 
  const SecurityContext &session_ctx= session->getSecurityContext();
213
 
 
214
 
  /* If we never loaded any authorization plugins, just return true */
215
 
  if (authorization_plugins.empty())
216
 
    return true;
217
 
 
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));
223
 
 
224
 
  /*
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.
228
 
   */
229
 
 
230
 
  if (iter != authorization_plugins.end())
231
 
  {
232
 
    if (send_error)
233
 
    {
234
 
      my_error(ER_KILL_DENIED_ERROR, MYF(0), session->thread_id);
235
 
    }
236
 
    return false;
237
 
  }
238
 
  return true;
239
 
}
240
 
 
241
 
void plugin::Authorization::pruneSchemaNames(const SecurityContext &user_ctx,
242
 
                                             SchemaIdentifier::vector &set_of_schemas)
243
 
{
244
 
  /* If we never loaded any authorization plugins, just return true */
245
 
  if (authorization_plugins.empty())
246
 
    return;
247
 
 
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());
252
 
}
253
 
 
254
 
} /* namespace drizzled */