~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/authorization.cc

  • Committer: Brian Aker
  • Date: 2010-02-11 22:56:25 UTC
  • Revision ID: brian@gaz-20100211225625-63v3e79p78blva2u
Remove WEIGHT_STRING() from parser (where it does not belong). If someone
wants to they can reimplement this as a straight function.

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/error.h"
28
 
#include "drizzled/session.h"
29
 
#include "drizzled/plugin/registry.h"
30
 
#include "drizzled/gettext.h"
31
 
 
32
 
using namespace std;
33
 
 
34
 
namespace drizzled
35
 
{
36
 
 
37
 
vector<plugin::Authorization *> authorization_plugins;
38
 
 
39
 
 
40
 
bool plugin::Authorization::addPlugin(plugin::Authorization *auth)
41
 
{
42
 
  if (auth != NULL)
43
 
    authorization_plugins.push_back(auth);
44
 
  return false;
45
 
}
46
 
 
47
 
void plugin::Authorization::removePlugin(plugin::Authorization *auth)
48
 
{
49
 
  if (auth != NULL)
50
 
  {
51
 
    authorization_plugins.erase(find(authorization_plugins.begin(),
52
 
                                     authorization_plugins.end(),
53
 
                                     auth));
54
 
  }
55
 
}
56
 
 
57
 
namespace
58
 
{
59
 
 
60
 
class RestrictDbFunctor :
61
 
  public unary_function<plugin::Authorization *, bool>
62
 
{
63
 
  const SecurityContext &user_ctx;
64
 
  const string &schema;
65
 
public:
66
 
  RestrictDbFunctor(const SecurityContext &user_ctx_arg,
67
 
                    const string &schema_arg) :
68
 
    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 unary_function<plugin::Authorization *, bool>
81
 
{
82
 
  const SecurityContext &user_ctx;
83
 
  const string &schema;
84
 
  const string &table;
85
 
public:
86
 
  RestrictTableFunctor(const SecurityContext &user_ctx_arg,
87
 
                       const string &schema_arg,
88
 
                       const string &table_arg) :
89
 
    unary_function<plugin::Authorization *, bool>(),
90
 
    user_ctx(user_ctx_arg),
91
 
    schema(schema_arg),
92
 
    table(table_arg)
93
 
  { }
94
 
 
95
 
  inline result_type operator()(argument_type auth)
96
 
  {
97
 
    return auth->restrictTable(user_ctx, schema, table);
98
 
  }
99
 
};
100
 
 
101
 
class RestrictProcessFunctor :
102
 
  public unary_function<plugin::Authorization *, bool>
103
 
{
104
 
  const SecurityContext &user_ctx;
105
 
  const SecurityContext &session_ctx;
106
 
public:
107
 
  RestrictProcessFunctor(const SecurityContext &user_ctx_arg,
108
 
                         const SecurityContext &session_ctx_arg) :
109
 
    unary_function<plugin::Authorization *, bool>(),
110
 
    user_ctx(user_ctx_arg),
111
 
    session_ctx(session_ctx_arg)
112
 
  { }
113
 
 
114
 
  inline result_type operator()(argument_type auth)
115
 
  {
116
 
    return auth->restrictProcess(user_ctx, session_ctx);
117
 
  }
118
 
};
119
 
 
120
 
} /* namespace */
121
 
 
122
 
bool plugin::Authorization::isAuthorized(const SecurityContext &user_ctx,
123
 
                                         const string &schema,
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
 
  vector<plugin::Authorization *>::const_iterator iter=
132
 
    find_if(authorization_plugins.begin(),
133
 
            authorization_plugins.end(),
134
 
            RestrictDbFunctor(user_ctx, schema));
135
 
 
136
 
  /*
137
 
   * If iter is == end() here, that means that all of the plugins returned
138
 
   * false, which means that that each of them believe the user is authorized
139
 
   * to view the resource in question.
140
 
   */
141
 
  if (iter != authorization_plugins.end())
142
 
  {
143
 
    if (send_error)
144
 
    {
145
 
      my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
146
 
               user_ctx.getUser().c_str(),
147
 
               user_ctx.getIp().c_str(),
148
 
               schema.c_str());
149
 
    }
150
 
    return false;
151
 
  }
152
 
  return true;
153
 
}
154
 
 
155
 
bool plugin::Authorization::isAuthorized(const SecurityContext &user_ctx,
156
 
                                         const string &schema,
157
 
                                         const string &table,
158
 
                                         bool send_error)
159
 
{
160
 
  /* If we never loaded any authorization plugins, just return true */
161
 
  if (authorization_plugins.empty())
162
 
    return true;
163
 
 
164
 
  /* Use find_if instead of foreach so that we can collect return codes */
165
 
  vector<plugin::Authorization *>::const_iterator iter=
166
 
    find_if(authorization_plugins.begin(),
167
 
            authorization_plugins.end(),
168
 
            RestrictTableFunctor(user_ctx, schema, table));
169
 
 
170
 
  /*
171
 
   * If iter is == end() here, that means that all of the plugins returned
172
 
   * false, which means that that each of them believe the user is authorized
173
 
   * to view the resource in question.
174
 
   */
175
 
  if (iter != authorization_plugins.end())
176
 
  {
177
 
    if (send_error)
178
 
    {
179
 
      my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
180
 
               user_ctx.getUser().c_str(),
181
 
               user_ctx.getIp().c_str(),
182
 
               schema.c_str());
183
 
    }
184
 
    return false;
185
 
  }
186
 
  return true;
187
 
}
188
 
 
189
 
bool plugin::Authorization::isAuthorized(const SecurityContext &user_ctx,
190
 
                                         const Session *session,
191
 
                                         bool send_error)
192
 
{
193
 
  const SecurityContext &session_ctx= session->getSecurityContext();
194
 
 
195
 
  /* If we never loaded any authorization plugins, just return true */
196
 
  if (authorization_plugins.empty())
197
 
    return true;
198
 
 
199
 
  /* Use find_if instead of foreach so that we can collect return codes */
200
 
  vector<plugin::Authorization *>::const_iterator iter=
201
 
    find_if(authorization_plugins.begin(),
202
 
            authorization_plugins.end(),
203
 
            RestrictProcessFunctor(user_ctx, session_ctx));
204
 
 
205
 
  /*
206
 
   * If iter is == end() here, that means that all of the plugins returned
207
 
   * false, which means that that each of them believe the user is authorized
208
 
   * to view the resource in question.
209
 
   */
210
 
 
211
 
  if (iter != authorization_plugins.end())
212
 
  {
213
 
    if (send_error)
214
 
    {
215
 
      my_error(ER_KILL_DENIED_ERROR, MYF(0), session->thread_id);
216
 
    }
217
 
    return false;
218
 
  }
219
 
  return true;
220
 
}
221
 
 
222
 
void plugin::Authorization::pruneSchemaNames(const SecurityContext &user_ctx,
223
 
                                             set<string> &set_of_names)
224
 
{
225
 
 
226
 
  set<string> pruned_set_of_names;
227
 
 
228
 
  /* If we never loaded any authorization plugins, just return true */
229
 
  if (authorization_plugins.empty())
230
 
    return;
231
 
 
232
 
  /**
233
 
   * @TODO: It would be stellar if we could find a way to do this with a
234
 
   * functor and an STL algoritm
235
 
   */
236
 
  for(set<string>::const_iterator iter= set_of_names.begin();
237
 
      iter != set_of_names.end();
238
 
      ++iter)
239
 
  {
240
 
    if (plugin::Authorization::isAuthorized(user_ctx, *iter, false))
241
 
    {
242
 
      pruned_set_of_names.insert(*iter);
243
 
    }
244
 
  }
245
 
  set_of_names.swap(pruned_set_of_names);
246
 
}
247
 
 
248
 
} /* namespace drizzled */