~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/authentication.cc

  • Committer: Brian Aker
  • Date: 2009-05-05 00:40:45 UTC
  • mfrom: (1003.2.7 mordred)
  • Revision ID: brian@gaz-20090505004045-yns4biv63thufj1b
Tags: 2009.05.1005
Merge Monty

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 <drizzled/server_includes.h>
22
 
#include <drizzled/authentication.h>
23
 
#include <drizzled/gettext.h>
24
 
#include <drizzled/errmsg_print.h>
 
21
#include "drizzled/server_includes.h"
 
22
#include "drizzled/authentication.h"
 
23
#include "drizzled/gettext.h"
 
24
#include "drizzled/errmsg_print.h"
 
25
#include "drizzled/plugin_registry.h"
 
26
 
 
27
#include <vector>
 
28
 
 
29
using namespace std;
 
30
 
 
31
static vector<Authentication *> all_authentication;
25
32
 
26
33
static bool are_plugins_loaded= false;
27
34
 
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)
 
35
void add_authentication(Authentication *auth)
 
36
{
 
37
  all_authentication.push_back(auth);
 
38
}
 
39
 
 
40
void remove_authentication(Authentication *auth)
 
41
{
 
42
  all_authentication.erase(find(all_authentication.begin(),
 
43
                                all_authentication.end(),
 
44
                                auth));
 
45
}
 
46
 
 
47
class AuthenticateBy : public unary_function<Authentication *, bool>
 
48
{
 
49
  Session *session;
 
50
  const char *password;
 
51
public:
 
52
  AuthenticateBy(Session *session_arg, const char *password_arg) :
 
53
    unary_function<Authentication *, bool>(),
 
54
    session(session_arg), password(password_arg) {}
 
55
 
 
56
  inline result_type operator()(argument_type auth)
36
57
  {
37
 
    if (auth->authenticate(session, password))
38
 
      return true;
 
58
    return auth->authenticate(session, password);
39
59
  }
40
 
 
41
 
  return false;
42
 
}
 
60
};
43
61
 
44
62
bool authenticate_user(Session *session, const char *password)
45
63
{
47
65
  if (are_plugins_loaded != true)
48
66
    return true;
49
67
 
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)
66
 
  {
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
 
    }
73
 
  }
74
 
 
75
 
  plugin->data= (void *)authen;
76
 
  are_plugins_loaded= true;
77
 
 
78
 
  plugin->state= PLUGIN_IS_READY;
79
 
 
80
 
  return(0);
81
 
err:
82
 
  delete authen;
83
 
  return(1);
84
 
}
85
 
 
86
 
int authentication_finalizer(st_plugin_int *plugin)
87
 
{
88
 
  authentication_st *authen= (authentication_st *)plugin->data;
89
 
 
90
 
  assert(authen);
91
 
  if (authen && plugin->plugin->deinit)
92
 
    plugin->plugin->deinit(authen);
93
 
 
94
 
  delete authen;
95
 
 
96
 
  return(0);
97
 
}
 
68
  /* Use find_if instead of foreach so that we can collect return codes */
 
69
  vector<Authentication *>::iterator iter=
 
70
    find_if(all_authentication.begin(), all_authentication.end(),
 
71
            AuthenticateBy(session, password));
 
72
  /* If iter is == end() here, that means that all of the plugins returned
 
73
   * false, which in this case means they all succeeded. Since we want to 
 
74
   * return false on success, we return the value of the two being != 
 
75
   */
 
76
  return iter != all_authentication.end();
 
77
}
 
78