~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/authentication.cc

  • Committer: Brian Aker
  • Date: 2009-04-27 14:36:40 UTC
  • Revision ID: brian@gaz-20090427143640-f6zjmtt9vm55qgm2
Patch on show processlist from  davi@apache.org

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 <libdrizzle/gettext.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;
24
32
 
25
33
static bool are_plugins_loaded= false;
26
34
 
27
 
static bool authenticate_by(THD *thd, 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
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)
35
57
  {
36
 
    if (auth->authenticate(thd, password))
37
 
      return true;
 
58
    return auth->authenticate(session, password);
38
59
  }
39
 
 
40
 
  return false;
41
 
}
42
 
 
43
 
bool authenticate_user(THD *thd, const char *password)
 
60
};
 
61
 
 
62
bool authenticate_user(Session *session, const char *password)
44
63
{
45
64
  /* If we never loaded any auth plugins, just return true */
46
65
  if (are_plugins_loaded != true)
47
66
    return true;
48
67
 
49
 
  return plugin_foreach(thd, 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
 
  if ((authen= (authentication_st *)malloc(sizeof(authentication_st))) == 0)
58
 
      return(1);
59
 
 
60
 
  memset(authen, 0, sizeof(authentication_st));
61
 
 
62
 
  if (plugin->plugin->init)
63
 
  {
64
 
    if (plugin->plugin->init(authen))
65
 
    {
66
 
      sql_print_error(_("Plugin '%s' init function returned error."),
67
 
                      plugin->name.str);
68
 
      goto err;
69
 
    }
70
 
  }
71
 
 
72
 
  plugin->data= (void *)authen;
73
 
  are_plugins_loaded= true;
74
 
 
75
 
  return(0);
76
 
err:
77
 
  free(authen);
78
 
  return(1);
79
 
}
80
 
 
81
 
int authentication_finalizer(st_plugin_int *plugin)
82
 
{
83
 
  authentication_st *authen= (authentication_st *)plugin->data;
84
 
 
85
 
  assert(authen);
86
 
  if (authen && plugin->plugin->deinit)
87
 
    plugin->plugin->deinit(authen);
88
 
 
89
 
  free(authen);
90
 
 
91
 
  return(0);
92
 
}
 
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