~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/authentication.cc

  • Committer: Brian Aker
  • Date: 2009-04-07 20:09:30 UTC
  • mfrom: (971.1.17 mordred)
  • Revision ID: brian@gaz-20090407200930-27jkul7lkwkjs2to
Merge from Monty

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include <drizzled/gettext.h>
24
24
#include <drizzled/errmsg_print.h>
25
25
 
 
26
#include <vector>
 
27
 
 
28
using namespace std;
 
29
 
 
30
static vector<Authentication *> all_authentication;
 
31
 
26
32
static bool are_plugins_loaded= false;
27
33
 
28
 
static bool authenticate_by(Session *session, plugin_ref plugin, void* p_data)
29
 
{
30
 
  const char *password= (const char *)p_data;
31
 
  Authentication *auth= plugin_data(plugin, Authentication *);
32
 
 
33
 
  (void)p_data;
34
 
 
35
 
  if (auth)
 
34
static void add_authentication(Authentication *auth)
 
35
{
 
36
  all_authentication.push_back(auth);
 
37
}
 
38
 
 
39
static void remove_authentication(Authentication *auth)
 
40
{
 
41
  all_authentication.erase(find(all_authentication.begin(),
 
42
                                all_authentication.end(),
 
43
                                auth));
 
44
}
 
45
 
 
46
class AuthenticateBy : public unary_function<Authentication *, bool>
 
47
{
 
48
  Session *session;
 
49
  const char *password;
 
50
public:
 
51
  AuthenticateBy(Session *session_arg, const char *password_arg) :
 
52
    unary_function<Authentication *, bool>(),
 
53
    session(session_arg), password(password_arg) {}
 
54
 
 
55
  inline result_type operator()(argument_type auth)
36
56
  {
37
 
    if (auth->authenticate(session, password))
38
 
      return true;
 
57
    return auth->authenticate(session, password);
39
58
  }
40
 
 
41
 
  return false;
42
 
}
 
59
};
43
60
 
44
61
bool authenticate_user(Session *session, const char *password)
45
62
{
47
64
  if (are_plugins_loaded != true)
48
65
    return true;
49
66
 
50
 
  return plugin_foreach(session, authenticate_by, DRIZZLE_AUTH_PLUGIN, (void *)password);
 
67
  /* Use find_if instead of foreach so that we can collect return codes */
 
68
  vector<Authentication *>::iterator iter=
 
69
    find_if(all_authentication.begin(), all_authentication.end(),
 
70
            AuthenticateBy(session, password));
 
71
  /* If iter is == end() here, that means that all of the plugins returned
 
72
   * false, which in this case means they all succeeded. Since we want to 
 
73
   * return false on success, we return the value of the two being != 
 
74
   */
 
75
  return iter != all_authentication.end();
51
76
}
52
77
 
53
78
 
54
79
int authentication_initializer(st_plugin_int *plugin)
55
80
{
56
 
  Authentication *authen;
57
 
 
 
81
  Authentication *authen= NULL;
58
82
 
59
83
  if (plugin->plugin->init)
60
84
  {
63
87
      errmsg_printf(ERRMSG_LVL_ERROR,
64
88
                    _("Plugin '%s' init function returned error."),
65
89
                    plugin->name.str);
66
 
      goto err;
 
90
      return 1;
67
91
    }
68
92
  }
69
93
 
70
94
  if (authen == NULL)
71
95
    return 1;
72
96
 
73
 
  plugin->data= static_cast<void *>(authen);
 
97
  add_authentication(authen);
 
98
  plugin->data= authen;
74
99
  are_plugins_loaded= true;
75
100
 
76
 
  return(0);
77
 
err:
78
 
  delete authen;
79
 
  return(1);
 
101
  return 0;
80
102
}
81
103
 
82
104
int authentication_finalizer(st_plugin_int *plugin)
83
105
{
84
106
  Authentication *authen= static_cast<Authentication *>(plugin->data);
85
 
 
86
107
  assert(authen);
 
108
 
 
109
  remove_authentication(authen);
 
110
 
87
111
  if (authen && plugin->plugin->deinit)
88
112
    plugin->plugin->deinit(authen);
89
113