~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/authentication.cc

Merged from Toru - removal of my_time_t.

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, Inc.
 
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>
 
21
#include <drizzled/server_includes.h>
 
22
#include <drizzled/authentication.h>
24
23
#include <drizzled/gettext.h>
25
 
#include <drizzled/identifier.h>
26
 
 
27
 
#include <vector>
28
 
 
29
 
namespace drizzled
30
 
{
31
 
 
32
 
std::vector<plugin::Authentication *> all_authentication;
33
 
 
34
 
 
35
 
bool plugin::Authentication::addPlugin(plugin::Authentication *auth)
36
 
{
37
 
  if (auth != NULL)
38
 
    all_authentication.push_back(auth);
 
24
 
 
25
static bool are_plugins_loaded= false;
 
26
 
 
27
static bool authenticate_by(Session *session, plugin_ref plugin, void* p_data)
 
28
{
 
29
  const char *password= (const char *)p_data;
 
30
  authentication_st *auth= plugin_data(plugin, authentication_st *);
 
31
 
 
32
  (void)p_data;
 
33
 
 
34
  if (auth && auth->authenticate)
 
35
  {
 
36
    if (auth->authenticate(session, password))
 
37
      return true;
 
38
  }
39
39
 
40
40
  return false;
41
41
}
42
42
 
43
 
void plugin::Authentication::removePlugin(plugin::Authentication *auth)
44
 
{
45
 
  if (auth != NULL)
46
 
    all_authentication.erase(std::find(all_authentication.begin(),
47
 
                                       all_authentication.end(),
48
 
                                       auth));
49
 
}
50
 
 
51
 
class AuthenticateBy : public std::unary_function<plugin::Authentication *, bool>
52
 
{
53
 
  const identifier::User &sctx;
54
 
  const std::string &password;
55
 
 
56
 
public:
57
 
  AuthenticateBy(const identifier::User &sctx_arg, const std::string &password_arg) :
58
 
    std::unary_function<plugin::Authentication *, bool>(),
59
 
    sctx(sctx_arg), password(password_arg) {}
60
 
 
61
 
  inline result_type operator()(argument_type auth)
62
 
  {
63
 
    return auth->authenticate(sctx, password);
64
 
  }
65
 
};
66
 
 
67
 
bool plugin::Authentication::isAuthenticated(drizzled::identifier::User::const_reference sctx,
68
 
                                             const std::string &password)
 
43
bool authenticate_user(Session *session, const char *password)
69
44
{
70
45
  /* If we never loaded any auth plugins, just return true */
71
 
  if (all_authentication.empty())
 
46
  if (are_plugins_loaded != true)
72
47
    return true;
73
48
 
74
 
  /* Use find_if instead of foreach so that we can collect return codes */
75
 
  std::vector<plugin::Authentication *>::iterator iter=
76
 
    std::find_if(all_authentication.begin(), all_authentication.end(),
77
 
                 AuthenticateBy(sctx, password));
78
 
 
79
 
  /* We only require one plugin to return success in order to authenticate.
80
 
   * If iter is == end() here, that means that all of the plugins returned
81
 
   * false, which means they all failed.
82
 
   */
83
 
  if (iter == all_authentication.end())
 
49
  return plugin_foreach(session, authenticate_by, DRIZZLE_AUTH_PLUGIN, (void *)password);
 
50
}
 
51
 
 
52
 
 
53
int authentication_initializer(st_plugin_int *plugin)
 
54
{
 
55
  authentication_st *authen;
 
56
 
 
57
  authen= new authentication_st;
 
58
 
 
59
  if (authen == NULL)
 
60
    return 1;
 
61
 
 
62
  memset(authen, 0, sizeof(authentication_st));
 
63
 
 
64
  if (plugin->plugin->init)
84
65
  {
85
 
    error::access(sctx);
86
 
 
87
 
    return false;
 
66
    if (plugin->plugin->init(authen))
 
67
    {
 
68
      sql_print_error(_("Plugin '%s' init function returned error."),
 
69
                      plugin->name.str);
 
70
      goto err;
 
71
    }
88
72
  }
89
73
 
90
 
  return true;
91
 
}
92
 
 
93
 
} /* namespace drizzled */
 
74
  plugin->data= (void *)authen;
 
75
  are_plugins_loaded= true;
 
76
 
 
77
  return(0);
 
78
err:
 
79
  delete authen;
 
80
  return(1);
 
81
}
 
82
 
 
83
int authentication_finalizer(st_plugin_int *plugin)
 
84
{
 
85
  authentication_st *authen= (authentication_st *)plugin->data;
 
86
 
 
87
  assert(authen);
 
88
  if (authen && plugin->plugin->deinit)
 
89
    plugin->plugin->deinit(authen);
 
90
 
 
91
  delete authen;
 
92
 
 
93
  return(0);
 
94
}