~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to drizzled/plugin/authentication.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/error.h"
 
24
#include "drizzled/plugin/registry.h"
 
25
#include "drizzled/gettext.h"
 
26
#include "drizzled/security_context.h"
 
27
 
 
28
#include <vector>
 
29
 
 
30
using namespace std;
 
31
 
 
32
namespace drizzled
 
33
{
 
34
 
 
35
std::vector<plugin::Authentication *> all_authentication;
 
36
 
 
37
 
 
38
bool plugin::Authentication::addPlugin(plugin::Authentication *auth)
 
39
{
 
40
  if (auth != NULL)
 
41
    all_authentication.push_back(auth);
 
42
  return false;
 
43
}
 
44
 
 
45
void plugin::Authentication::removePlugin(plugin::Authentication *auth)
 
46
{
 
47
  if (auth != NULL)
 
48
    all_authentication.erase(find(all_authentication.begin(),
 
49
                                  all_authentication.end(),
 
50
                                  auth));
 
51
}
 
52
 
 
53
class AuthenticateBy : public unary_function<plugin::Authentication *, bool>
 
54
{
 
55
  const SecurityContext &sctx;
 
56
  const string &password;
 
57
public:
 
58
  AuthenticateBy(const SecurityContext &sctx_arg, const string &password_arg) :
 
59
    unary_function<plugin::Authentication *, bool>(),
 
60
    sctx(sctx_arg), password(password_arg) {}
 
61
 
 
62
  inline result_type operator()(argument_type auth)
 
63
  {
 
64
    return auth->authenticate(sctx, password);
 
65
  }
 
66
};
 
67
 
 
68
bool plugin::Authentication::isAuthenticated(const SecurityContext &sctx,
 
69
                                             const string &password)
 
70
{
 
71
  /* If we never loaded any auth plugins, just return true */
 
72
  if (all_authentication.empty())
 
73
    return true;
 
74
 
 
75
  /* Use find_if instead of foreach so that we can collect return codes */
 
76
  vector<plugin::Authentication *>::iterator iter=
 
77
    find_if(all_authentication.begin(), all_authentication.end(),
 
78
            AuthenticateBy(sctx, password));
 
79
 
 
80
  /* If iter is == end() here, that means that all of the plugins returned
 
81
   * false, which in this case means they all succeeded. Since we want to 
 
82
   * return false on success, we return the value of the two being != 
 
83
   */
 
84
  if (iter != all_authentication.end())
 
85
  {
 
86
    my_error(ER_ACCESS_DENIED_ERROR, MYF(0),
 
87
             sctx.getUser().c_str(),
 
88
             sctx.getIp().c_str(),
 
89
             password.empty() ? ER(ER_NO) : ER(ER_YES));
 
90
    return true;
 
91
  }
 
92
  return false;
 
93
}
 
94
 
 
95
} /* namespace drizzled */