~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/authorization.cc

  • Committer: Monty Taylor
  • Date: 2010-10-21 23:10:12 UTC
  • mto: (1879.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1880.
  • Revision ID: mordred@inaugust.com-20101021231012-uhsebiqo23xi0ygy
Updated AUTHORS list with everyone from bzr logs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include <config.h>
 
21
#include "config.h"
22
22
 
23
23
#include <vector>
24
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>
 
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
using namespace std;
30
33
 
31
34
namespace drizzled
32
35
{
33
36
 
34
 
std::vector<plugin::Authorization *> authorization_plugins;
 
37
vector<plugin::Authorization *> authorization_plugins;
35
38
 
36
39
 
37
40
bool plugin::Authorization::addPlugin(plugin::Authorization *auth)
38
41
{
39
42
  if (auth != NULL)
40
43
    authorization_plugins.push_back(auth);
41
 
 
42
44
  return false;
43
45
}
44
46
 
46
48
{
47
49
  if (auth != NULL)
48
50
  {
49
 
    authorization_plugins.erase(std::find(authorization_plugins.begin(),
50
 
                                          authorization_plugins.end(),
51
 
                                          auth));
 
51
    authorization_plugins.erase(find(authorization_plugins.begin(),
 
52
                                     authorization_plugins.end(),
 
53
                                     auth));
52
54
  }
53
55
}
54
56
 
56
58
{
57
59
 
58
60
class RestrictDbFunctor :
59
 
  public std::unary_function<plugin::Authorization *, bool>
 
61
  public unary_function<plugin::Authorization *, bool>
60
62
{
61
 
  const identifier::User &user_ctx;
62
 
  identifier::Schema::const_reference schema;
63
 
 
 
63
  const SecurityContext &user_ctx;
 
64
  SchemaIdentifier &schema;
64
65
public:
65
 
  RestrictDbFunctor(const identifier::User &user_ctx_arg,
66
 
                    identifier::Schema::const_reference schema_arg) :
67
 
    std::unary_function<plugin::Authorization *, bool>(),
 
66
  RestrictDbFunctor(const SecurityContext &user_ctx_arg,
 
67
                    SchemaIdentifier &schema_arg) :
 
68
    unary_function<plugin::Authorization *, bool>(),
68
69
    user_ctx(user_ctx_arg),
69
70
    schema(schema_arg)
70
71
  { }
76
77
};
77
78
 
78
79
class RestrictTableFunctor :
79
 
  public std::unary_function<plugin::Authorization *, bool>
 
80
  public unary_function<plugin::Authorization *, bool>
80
81
{
81
 
  identifier::User::const_reference user_ctx;
82
 
  identifier::Table::const_reference table;
 
82
  const SecurityContext &user_ctx;
 
83
  TableIdentifier &table;
83
84
public:
84
 
  RestrictTableFunctor(identifier::User::const_reference user_ctx_arg,
85
 
                       identifier::Table::const_reference table_arg) :
86
 
    std::unary_function<plugin::Authorization *, bool>(),
 
85
  RestrictTableFunctor(const SecurityContext &user_ctx_arg,
 
86
                       TableIdentifier &table_arg) :
 
87
    unary_function<plugin::Authorization *, bool>(),
87
88
    user_ctx(user_ctx_arg),
88
89
    table(table_arg)
89
90
  { }
95
96
};
96
97
 
97
98
class RestrictProcessFunctor :
98
 
  public std::unary_function<plugin::Authorization *, bool>
 
99
  public unary_function<plugin::Authorization *, bool>
99
100
{
100
 
  const identifier::User &user_ctx;
101
 
  const identifier::User &session_ctx;
 
101
  const SecurityContext &user_ctx;
 
102
  const SecurityContext &session_ctx;
102
103
public:
103
 
  RestrictProcessFunctor(const identifier::User &user_ctx_arg,
104
 
                         const identifier::User &session_ctx_arg) :
105
 
    std::unary_function<plugin::Authorization *, bool>(),
 
104
  RestrictProcessFunctor(const SecurityContext &user_ctx_arg,
 
105
                         const SecurityContext &session_ctx_arg) :
 
106
    unary_function<plugin::Authorization *, bool>(),
106
107
    user_ctx(user_ctx_arg),
107
108
    session_ctx(session_ctx_arg)
108
109
  { }
114
115
};
115
116
 
116
117
class PruneSchemaFunctor :
117
 
  public std::unary_function<identifier::Schema&, bool>
 
118
  public unary_function<SchemaIdentifier&, bool>
118
119
{
119
 
  drizzled::identifier::User::const_reference user_ctx;
 
120
  const SecurityContext &user_ctx;
120
121
public:
121
 
  PruneSchemaFunctor(drizzled::identifier::User::const_reference user_ctx_arg) :
122
 
    std::unary_function<identifier::Schema&, bool>(),
 
122
  PruneSchemaFunctor(const SecurityContext &user_ctx_arg) :
 
123
    unary_function<SchemaIdentifier&, bool>(),
123
124
    user_ctx(user_ctx_arg)
124
125
  { }
125
126
 
131
132
 
132
133
} /* namespace */
133
134
 
134
 
bool plugin::Authorization::isAuthorized(identifier::User::const_reference user_ctx,
135
 
                                         identifier::Schema::const_reference schema_identifier,
136
 
                                         bool send_error)
137
 
{
138
 
  /* If we never loaded any authorization plugins, just return true */
139
 
  if (authorization_plugins.empty())
140
 
    return true;
141
 
 
142
 
  /* Use find_if instead of foreach so that we can collect return codes */
143
 
  std::vector<plugin::Authorization *>::const_iterator iter=
144
 
    std::find_if(authorization_plugins.begin(),
145
 
                 authorization_plugins.end(),
146
 
                 RestrictDbFunctor(user_ctx, schema_identifier));
147
 
 
148
 
 
149
 
  /*
150
 
   * If iter is == end() here, that means that all of the plugins returned
151
 
   * false, which means that that each of them believe the user is authorized
152
 
   * to view the resource in question.
153
 
   */
154
 
  if (iter != authorization_plugins.end())
155
 
  {
156
 
    if (send_error)
157
 
    {
158
 
      error::access(user_ctx, schema_identifier);
159
 
    }
160
 
    return false;
161
 
  }
162
 
  return true;
163
 
}
164
 
 
165
 
bool plugin::Authorization::isAuthorized(drizzled::identifier::User::const_reference user_ctx,
166
 
                                         identifier::Table::const_reference table_identifier,
167
 
                                         bool send_error)
168
 
{
169
 
  /* If we never loaded any authorization plugins, just return true */
170
 
  if (authorization_plugins.empty())
171
 
    return true;
172
 
 
173
 
  /* Use find_if instead of foreach so that we can collect return codes */
174
 
  std::vector<plugin::Authorization *>::const_iterator iter=
175
 
    std::find_if(authorization_plugins.begin(),
176
 
            authorization_plugins.end(),
177
 
            RestrictTableFunctor(user_ctx, table_identifier));
178
 
 
179
 
  /*
180
 
   * If iter is == end() here, that means that all of the plugins returned
181
 
   * false, which means that that each of them believe the user is authorized
182
 
   * to view the resource in question.
183
 
   */
184
 
  if (iter != authorization_plugins.end())
185
 
  {
186
 
    if (send_error)
187
 
    {
188
 
      error::access(user_ctx, table_identifier);
189
 
    }
190
 
    return false;
191
 
  }
192
 
  return true;
193
 
}
194
 
 
195
 
bool plugin::Authorization::isAuthorized(drizzled::identifier::User::const_reference user_ctx,
196
 
                                         Session::const_reference session,
197
 
                                         bool send_error)
198
 
{
199
 
  /* If we never loaded any authorization plugins, just return true */
200
 
  if (authorization_plugins.empty())
201
 
    return true;
202
 
  
203
 
  // To make sure we hold the user structure we need to have a shred_ptr so
204
 
  // that we increase the count on the object.
205
 
  drizzled::identifier::User::const_shared_ptr session_ctx= session.user();
206
 
 
207
 
 
208
 
  /* Use find_if instead of foreach so that we can collect return codes */
209
 
  std::vector<plugin::Authorization *>::const_iterator iter=
210
 
    std::find_if(authorization_plugins.begin(),
211
 
                 authorization_plugins.end(),
212
 
                 RestrictProcessFunctor(user_ctx, *session_ctx));
213
 
 
214
 
  /*
215
 
   * If iter is == end() here, that means that all of the plugins returned
216
 
   * false, which means that that each of them believe the user is authorized
217
 
   * to view the resource in question.
218
 
   */
219
 
 
220
 
  if (iter != authorization_plugins.end())
221
 
  {
222
 
    if (send_error)
223
 
    {
224
 
      my_error(ER_KILL_DENIED_ERROR, MYF(0), session.thread_id);
225
 
    }
226
 
    return false;
227
 
  }
228
 
 
229
 
  return true;
230
 
}
231
 
 
232
 
void plugin::Authorization::pruneSchemaNames(drizzled::identifier::User::const_reference user_ctx,
233
 
                                             identifier::Schema::vector &set_of_schemas)
 
135
bool plugin::Authorization::isAuthorized(const SecurityContext &user_ctx,
 
136
                                         SchemaIdentifier &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
  vector<plugin::Authorization *>::const_iterator iter=
 
145
    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
      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());
 
163
    }
 
164
    return false;
 
165
  }
 
166
  return true;
 
167
}
 
168
 
 
169
bool plugin::Authorization::isAuthorized(const SecurityContext &user_ctx,
 
170
                                         TableIdentifier &table,
 
171
                                         bool send_error)
 
172
{
 
173
  /* If we never loaded any authorization plugins, just return true */
 
174
  if (authorization_plugins.empty())
 
175
    return true;
 
176
 
 
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));
 
182
 
 
183
  /*
 
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.
 
187
   */
 
188
  if (iter != authorization_plugins.end())
 
189
  {
 
190
    if (send_error)
 
191
    {
 
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());
 
196
    }
 
197
    return false;
 
198
  }
 
199
  return true;
 
200
}
 
201
 
 
202
bool plugin::Authorization::isAuthorized(const SecurityContext &user_ctx,
 
203
                                         const Session *session,
 
204
                                         bool send_error)
 
205
{
 
206
  const SecurityContext &session_ctx= session->getSecurityContext();
 
207
 
 
208
  /* If we never loaded any authorization plugins, just return true */
 
209
  if (authorization_plugins.empty())
 
210
    return true;
 
211
 
 
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));
 
217
 
 
218
  /*
 
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.
 
222
   */
 
223
 
 
224
  if (iter != authorization_plugins.end())
 
225
  {
 
226
    if (send_error)
 
227
    {
 
228
      my_error(ER_KILL_DENIED_ERROR, MYF(0), session->thread_id);
 
229
    }
 
230
    return false;
 
231
  }
 
232
  return true;
 
233
}
 
234
 
 
235
void plugin::Authorization::pruneSchemaNames(const SecurityContext &user_ctx,
 
236
                                             SchemaIdentifiers &set_of_schemas)
234
237
{
235
238
  /* If we never loaded any authorization plugins, just return true */
236
239
  if (authorization_plugins.empty())
237
240
    return;
238
241
 
239
 
  set_of_schemas.erase(std::remove_if(set_of_schemas.begin(),
240
 
                                      set_of_schemas.end(),
241
 
                                      PruneSchemaFunctor(user_ctx)),
 
242
  set_of_schemas.erase(remove_if(set_of_schemas.begin(),
 
243
                                 set_of_schemas.end(),
 
244
                                 PruneSchemaFunctor(user_ctx)),
242
245
                       set_of_schemas.end());
243
246
}
244
247