~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/auth_file/auth_file.cc

  • Committer: Monty Taylor
  • Date: 2010-11-08 18:26:08 UTC
  • mto: This revision was merged to the branch mainline in revision 1931.
  • Revision ID: mordred@inaugust.com-20101108182608-lci86acl7r53sbi3
Replaced auto_ptr with scoped_ptr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include <config.h>
 
20
#include "config.h"
21
21
 
22
22
#include <fstream>
23
23
#include <map>
25
25
#include <iostream>
26
26
 
27
27
#include <boost/program_options.hpp>
28
 
#include <boost/filesystem.hpp>
29
28
 
30
 
#include <drizzled/configmake.h>
31
 
#include <drizzled/plugin/authentication.h>
32
 
#include <drizzled/identifier.h>
33
 
#include <drizzled/util/convert.h>
34
 
#include <drizzled/algorithm/sha1.h>
35
 
#include <drizzled/module/option_map.h>
 
29
#include "drizzled/configmake.h"
 
30
#include "drizzled/plugin/authentication.h"
 
31
#include "drizzled/security_context.h"
 
32
#include "drizzled/util/convert.h"
 
33
#include "drizzled/algorithm/sha1.h"
 
34
#include "drizzled/module/option_map.h"
36
35
 
37
36
namespace po= boost::program_options;
38
 
namespace fs= boost::filesystem;
39
 
 
40
37
using namespace std;
41
38
using namespace drizzled;
42
39
 
43
40
namespace auth_file
44
41
{
45
42
 
46
 
static const fs::path DEFAULT_USERS_FILE= SYSCONFDIR "/drizzle.users";
 
43
static const char DEFAULT_USERS_FILE[]= SYSCONFDIR "/drizzle.users";
47
44
 
48
45
class AuthFile: public plugin::Authentication
49
46
{
50
 
  const fs::path users_file;
 
47
  const std::string users_file;
51
48
 
52
49
public:
53
50
 
54
 
  AuthFile(string name_arg, fs::path users_file_arg);
 
51
  AuthFile(string name_arg, string users_file_arg);
55
52
 
56
53
  /**
57
54
   * Retrieve the last error encountered in the class.
71
68
  /**
72
69
   * Base class method to check authentication for a user.
73
70
   */
74
 
  bool authenticate(const identifier::User &sctx, const string &password);
 
71
  bool authenticate(const SecurityContext &sctx, const string &password);
75
72
 
76
73
  /**
77
74
   * Verify the local and remote scrambled password match using the MySQL
93
90
  /**
94
91
   * Cache or username:password entries from the file.
95
92
   */
96
 
  std::map<string, string> users;
 
93
  map<string, string> users;
97
94
};
98
95
 
99
 
AuthFile::AuthFile(string name_arg, fs::path users_file_arg):
 
96
AuthFile::AuthFile(string name_arg, string users_file_arg):
100
97
  plugin::Authentication(name_arg),
101
98
  users_file(users_file_arg),
102
99
  error(),
111
108
 
112
109
bool AuthFile::loadFile(void)
113
110
{
114
 
  ifstream file(users_file.string().c_str());
 
111
  ifstream file(users_file.c_str());
115
112
 
116
113
  if (!file.is_open())
117
114
  {
118
115
    error = "Could not open users file: ";
119
 
    error += users_file.string();
 
116
    error += users_file;
120
117
    return false;
121
118
  }
122
119
 
140
137
      password = string(line, password_offset + 1);
141
138
    }
142
139
 
143
 
    std::pair<std::map<std::string, std::string>::iterator, bool> result=
144
 
      users.insert(std::pair<std::string, std::string>(username, password));
145
 
 
 
140
    pair<map<string, string>::iterator, bool> result=
 
141
      users.insert(pair<string, string>(username, password));
146
142
    if (result.second == false)
147
143
    {
148
144
      error = "Duplicate entry found in users file: ";
202
198
  return memcmp(local_scrambled_password, scrambled_password_check, SHA1_DIGEST_LENGTH) == 0;
203
199
}
204
200
 
205
 
bool AuthFile::authenticate(const identifier::User &sctx, const string &password)
 
201
bool AuthFile::authenticate(const SecurityContext &sctx, const string &password)
206
202
{
207
 
  std::map<std::string, std::string>::const_iterator user= users.find(sctx.username());
 
203
  map<string, string>::const_iterator user = users.find(sctx.getUser());
208
204
  if (user == users.end())
209
205
    return false;
210
206
 
211
 
  if (sctx.getPasswordType() == identifier::User::MYSQL_HASH)
 
207
  if (sctx.getPasswordType() == SecurityContext::MYSQL_HASH)
212
208
    return verifyMySQLHash(user->second, sctx.getPasswordContext(), password);
213
209
 
214
210
  if (password == user->second)
221
217
{
222
218
  const module::option_map &vm= context.getOptions();
223
219
 
224
 
  AuthFile *auth_file = new AuthFile("auth_file", fs::path(vm["users"].as<string>()));
225
 
  if (not auth_file->loadFile())
 
220
  AuthFile *auth_file = new AuthFile("auth_file", vm["users"].as<string>());
 
221
  if (!auth_file->loadFile())
226
222
  {
227
 
    errmsg_printf(error::ERROR, _("Could not load auth file: %s\n"),
 
223
    errmsg_printf(ERRMSG_LVL_ERROR, _("Could not load auth file: %s\n"),
228
224
                  auth_file->getError().c_str());
229
225
    delete auth_file;
230
226
    return 1;
232
228
 
233
229
  context.add(auth_file);
234
230
  context.registerVariable(new sys_var_const_string_val("users", vm["users"].as<string>()));
235
 
 
236
231
  return 0;
237
232
}
238
233
 
240
235
static void init_options(drizzled::module::option_context &context)
241
236
{
242
237
  context("users", 
243
 
          po::value<string>()->default_value(DEFAULT_USERS_FILE.string()),
 
238
          po::value<string>()->default_value(DEFAULT_USERS_FILE),
244
239
          N_("File to load for usernames and passwords"));
245
240
}
246
241