~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/auth_file/auth_file.cc

  • Committer: patrick crews
  • Date: 2010-09-29 15:15:19 UTC
  • mfrom: (1099.4.188 drizzle)
  • Revision ID: gleebix@gmail.com-20100929151519-6mrmzd1ciw2p9nws
Tags: 2010.09.1802
Update translations

Show diffs side-by-side

added added

removed removed

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