~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-04-22 02:46:23 UTC
  • mto: (1497.3.4 enable-dtrace)
  • mto: This revision was merged to the branch mainline in revision 1527.
  • Revision ID: mordred@inaugust.com-20100422024623-4urw8fi8eraci08p
Don't overwrite the pandora_vc_revinfo file if we don't have new
authoratative information.

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