27
#include <boost/program_options.hpp>
28
#include <boost/filesystem.hpp>
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"
37
namespace po= boost::program_options;
38
namespace fs= boost::filesystem;
40
32
using namespace std;
41
33
using namespace drizzled;
43
35
namespace auth_file
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";
48
41
class AuthFile: public plugin::Authentication
50
const fs::path users_file;
54
AuthFile(string name_arg, fs::path users_file_arg);
45
AuthFile(string name_arg);
57
48
* Retrieve the last error encountered in the class.
72
63
* Base class method to check authentication for a user.
74
bool authenticate(const identifier::User &sctx, const string &password);
65
bool authenticate(const SecurityContext &sctx, const string &password);
77
68
* Verify the local and remote scrambled password match using the MySQL
94
85
* Cache or username:password entries from the file.
96
std::map<string, string> users;
87
map<string, string> users;
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),
112
102
bool AuthFile::loadFile(void)
114
ifstream file(users_file.string().c_str());
104
ifstream file(users_file);
116
106
if (!file.is_open())
118
108
error = "Could not open users file: ";
119
error += users_file.string();
140
130
password = string(line, password_offset + 1);
143
std::pair<std::map<std::string, std::string>::iterator, bool> result=
144
users.insert(std::pair<std::string, std::string>(username, password));
133
pair<map<string, string>::iterator, bool> result;
134
result = users.insert(pair<string, string>(username, password));
146
135
if (result.second == false)
148
137
error = "Duplicate entry found in users file: ";
202
191
return memcmp(local_scrambled_password, scrambled_password_check, SHA1_DIGEST_LENGTH) == 0;
205
bool AuthFile::authenticate(const identifier::User &sctx, const string &password)
194
bool AuthFile::authenticate(const SecurityContext &sctx, const string &password)
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())
211
if (sctx.getPasswordType() == identifier::User::MYSQL_HASH)
200
if (sctx.getPasswordType() == SecurityContext::MYSQL_HASH)
212
201
return verifyMySQLHash(user->second, sctx.getPasswordContext(), password);
214
203
if (password == user->second)
220
static int init(module::Context &context)
209
static int init(plugin::Context &context)
222
const module::option_map &vm= context.getOptions();
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())
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;
233
220
context.add(auth_file);
234
context.registerVariable(new sys_var_const_string_val("users", vm["users"].as<string>()));
224
static DRIZZLE_SYSVAR_STR(users,
227
N_("File to load for usernames and passwords"),
228
NULL, /* check func */
229
NULL, /* update func*/
230
DEFAULT_USERS_FILE /* default */);
240
static void init_options(drizzled::module::option_context &context)
232
static drizzle_sys_var* sys_variables[]=
243
po::value<string>()->default_value(DEFAULT_USERS_FILE.string()),
244
N_("File to load for usernames and passwords"));
234
DRIZZLE_SYSVAR(users),
247
238
} /* namespace auth_file */
249
DRIZZLE_PLUGIN(auth_file::init, NULL, auth_file::init_options);
240
DRIZZLE_PLUGIN(auth_file::init, auth_file::sys_variables);