~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/authentication.cc

pandora-build v0.100 - Fixes several bugs found by cb1kenobi. Add several thoughts from folks at LCA.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <drizzled/server_includes.h>
2
 
#include <drizzled/authentication.h>
3
 
 
4
 
static bool are_plugins_loaded= false;
5
 
 
6
 
static bool authenticate_by(THD *thd, plugin_ref plugin, void* p_data)
7
 
{
8
 
  const char *password= (const char *)p_data;
9
 
  authentication_st *auth= plugin_data(plugin, authentication_st *);
10
 
 
11
 
  (void)p_data;
12
 
 
13
 
  if (auth && auth->authenticate)
14
 
  {
15
 
    if (auth->authenticate(thd, password))
16
 
      return true;
17
 
  }
18
 
 
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
#include "drizzled/plugin/authentication.h"
 
23
#include "drizzled/errmsg_print.h"
 
24
#include "drizzled/plugin/registry.h"
 
25
#include "drizzled/gettext.h"
 
26
 
 
27
#include <vector>
 
28
 
 
29
using namespace std;
 
30
 
 
31
namespace drizzled
 
32
{
 
33
 
 
34
std::vector<plugin::Authentication *> all_authentication;
 
35
 
 
36
 
 
37
bool plugin::Authentication::addPlugin(plugin::Authentication *auth)
 
38
{
 
39
  if (auth != NULL)
 
40
    all_authentication.push_back(auth);
19
41
  return false;
20
42
}
21
43
 
22
 
bool authenticate_user(THD *thd, const char *password)
 
44
void plugin::Authentication::removePlugin(plugin::Authentication *auth)
 
45
{
 
46
  if (auth != NULL)
 
47
    all_authentication.erase(find(all_authentication.begin(),
 
48
                                  all_authentication.end(),
 
49
                                  auth));
 
50
}
 
51
 
 
52
class AuthenticateBy : public unary_function<plugin::Authentication *, bool>
 
53
{
 
54
  Session *session;
 
55
  const char *password;
 
56
public:
 
57
  AuthenticateBy(Session *session_arg, const char *password_arg) :
 
58
    unary_function<plugin::Authentication *, bool>(),
 
59
    session(session_arg), password(password_arg) {}
 
60
 
 
61
  inline result_type operator()(argument_type auth)
 
62
  {
 
63
    return auth->authenticate(session, password);
 
64
  }
 
65
};
 
66
 
 
67
bool plugin::Authentication::isAuthenticated(Session *session,
 
68
                                             const char *password)
23
69
{
24
70
  /* If we never loaded any auth plugins, just return true */
25
 
  if (are_plugins_loaded != true)
 
71
  if (all_authentication.size() == 0)
26
72
    return true;
27
73
 
28
 
  printf("PASSWORD :%s:\n", password);
29
 
 
30
 
  return plugin_foreach(thd, authenticate_by, DRIZZLE_AUTH_PLUGIN, (void *)password);
31
 
}
32
 
 
33
 
 
34
 
int authentication_initializer(st_plugin_int *plugin)
35
 
{
36
 
  authentication_st *authen;
37
 
 
38
 
  if ((authen= (authentication_st *)malloc(sizeof(authentication_st))) == 0)
39
 
      return(1);
40
 
 
41
 
  memset(authen, 0, sizeof(authentication_st));
42
 
 
43
 
  if (plugin->plugin->init)
44
 
  {
45
 
    if (plugin->plugin->init(authen))
46
 
    {
47
 
      sql_print_error("Plugin '%s' init function returned error.",
48
 
                      plugin->name.str);
49
 
      goto err;
50
 
    }
51
 
  }
52
 
 
53
 
  plugin->data= (void *)authen;
54
 
  are_plugins_loaded= true;
55
 
 
56
 
  return(0);
57
 
err:
58
 
  free(authen);
59
 
  return(1);
60
 
}
61
 
 
62
 
int authentication_finalizer(st_plugin_int *plugin)
63
 
{
64
 
  authentication_st *authen= (authentication_st *)plugin->data;
65
 
 
66
 
  assert(authen);
67
 
  if (authen && plugin->plugin->deinit)
68
 
    plugin->plugin->deinit(authen);
69
 
 
70
 
  free(authen);
71
 
 
72
 
  return(0);
73
 
}
 
74
  /* Use find_if instead of foreach so that we can collect return codes */
 
75
  vector<plugin::Authentication *>::iterator iter=
 
76
    find_if(all_authentication.begin(), all_authentication.end(),
 
77
            AuthenticateBy(session, password));
 
78
  /* If iter is == end() here, that means that all of the plugins returned
 
79
   * false, which in this case means they all succeeded. Since we want to 
 
80
   * return false on success, we return the value of the two being != 
 
81
   */
 
82
  return iter != all_authentication.end();
 
83
}
 
84
 
 
85
} /* namespace drizzled */