~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/authentication.cc

  • Committer: Brian Aker
  • Date: 2009-01-17 02:46:52 UTC
  • Revision ID: brian@gir-3.local-20090117024652-4ducefje08ajbs1q
Refactor append_identifier and remove dead OPTION_QUOTE_SHOW_CREATE option
(we always quote).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
 
4
 *  Copyright (C) 2008 Brian Aker
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include "config.h"
22
 
#include "drizzled/plugin/authentication.h"
23
 
#include "drizzled/error.h"
24
 
#include "drizzled/plugin/registry.h"
25
 
#include "drizzled/gettext.h"
26
 
#include "drizzled/security_context.h"
27
 
 
28
 
#include <vector>
29
 
 
30
 
using namespace std;
31
 
 
32
 
namespace drizzled
33
 
{
34
 
 
35
 
std::vector<plugin::Authentication *> all_authentication;
36
 
 
37
 
 
38
 
bool plugin::Authentication::addPlugin(plugin::Authentication *auth)
39
 
{
40
 
  if (auth != NULL)
41
 
    all_authentication.push_back(auth);
 
21
#include <drizzled/server_includes.h>
 
22
#include <drizzled/authentication.h>
 
23
#include <drizzled/gettext.h>
 
24
#include <drizzled/errmsg_print.h>
 
25
 
 
26
static bool are_plugins_loaded= false;
 
27
 
 
28
static bool authenticate_by(Session *session, plugin_ref plugin, void* p_data)
 
29
{
 
30
  const char *password= (const char *)p_data;
 
31
  authentication_st *auth= plugin_data(plugin, authentication_st *);
 
32
 
 
33
  (void)p_data;
 
34
 
 
35
  if (auth && auth->authenticate)
 
36
  {
 
37
    if (auth->authenticate(session, password))
 
38
      return true;
 
39
  }
 
40
 
42
41
  return false;
43
42
}
44
43
 
45
 
void plugin::Authentication::removePlugin(plugin::Authentication *auth)
46
 
{
47
 
  if (auth != NULL)
48
 
    all_authentication.erase(find(all_authentication.begin(),
49
 
                                  all_authentication.end(),
50
 
                                  auth));
51
 
}
52
 
 
53
 
class AuthenticateBy : public unary_function<plugin::Authentication *, bool>
54
 
{
55
 
  const SecurityContext &sctx;
56
 
  const string &password;
57
 
public:
58
 
  AuthenticateBy(const SecurityContext &sctx_arg, const string &password_arg) :
59
 
    unary_function<plugin::Authentication *, bool>(),
60
 
    sctx(sctx_arg), password(password_arg) {}
61
 
 
62
 
  inline result_type operator()(argument_type auth)
63
 
  {
64
 
    return auth->authenticate(sctx, password);
65
 
  }
66
 
};
67
 
 
68
 
bool plugin::Authentication::isAuthenticated(const SecurityContext &sctx,
69
 
                                             const string &password)
 
44
bool authenticate_user(Session *session, const char *password)
70
45
{
71
46
  /* If we never loaded any auth plugins, just return true */
72
 
  if (all_authentication.empty())
 
47
  if (are_plugins_loaded != true)
73
48
    return true;
74
49
 
75
 
  /* Use find_if instead of foreach so that we can collect return codes */
76
 
  vector<plugin::Authentication *>::iterator iter=
77
 
    find_if(all_authentication.begin(), all_authentication.end(),
78
 
            AuthenticateBy(sctx, password));
79
 
 
80
 
  /* We only require one plugin to return success in order to authenticate.
81
 
   * If iter is == end() here, that means that all of the plugins returned
82
 
   * false, which means they all failed.
83
 
   */
84
 
  if (iter == all_authentication.end())
 
50
  return plugin_foreach(session, authenticate_by, DRIZZLE_AUTH_PLUGIN, (void *)password);
 
51
}
 
52
 
 
53
 
 
54
int authentication_initializer(st_plugin_int *plugin)
 
55
{
 
56
  authentication_st *authen;
 
57
 
 
58
  authen= new authentication_st;
 
59
 
 
60
  if (authen == NULL)
 
61
    return 1;
 
62
 
 
63
  memset(authen, 0, sizeof(authentication_st));
 
64
 
 
65
  if (plugin->plugin->init)
85
66
  {
86
 
    my_error(ER_ACCESS_DENIED_ERROR, MYF(0),
87
 
             sctx.getUser().c_str(),
88
 
             sctx.getIp().c_str(),
89
 
             password.empty() ? ER(ER_NO) : ER(ER_YES));
90
 
    return false;
 
67
    if (plugin->plugin->init(authen))
 
68
    {
 
69
      errmsg_printf(ERRMSG_LVL_ERROR, _("Plugin '%s' init function returned error."),
 
70
                      plugin->name.str);
 
71
      goto err;
 
72
    }
91
73
  }
92
 
  return true;
93
 
}
94
 
 
95
 
} /* namespace drizzled */
 
74
 
 
75
  plugin->data= (void *)authen;
 
76
  are_plugins_loaded= true;
 
77
 
 
78
  return(0);
 
79
err:
 
80
  delete authen;
 
81
  return(1);
 
82
}
 
83
 
 
84
int authentication_finalizer(st_plugin_int *plugin)
 
85
{
 
86
  authentication_st *authen= (authentication_st *)plugin->data;
 
87
 
 
88
  assert(authen);
 
89
  if (authen && plugin->plugin->deinit)
 
90
    plugin->plugin->deinit(authen);
 
91
 
 
92
  delete authen;
 
93
 
 
94
  return(0);
 
95
}