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"
31
#include <boost/program_options.hpp>
32
#include <drizzled/module/option_map.h>
37
35
namespace po= boost::program_options;
38
namespace fs= boost::filesystem;
40
36
using namespace std;
41
37
using namespace drizzled;
43
39
namespace auth_file
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";
48
45
class AuthFile: public plugin::Authentication
50
const fs::path users_file;
54
AuthFile(string name_arg, fs::path users_file_arg);
49
AuthFile(string name_arg);
57
52
* Retrieve the last error encountered in the class.
72
67
* Base class method to check authentication for a user.
74
bool authenticate(const identifier::User &sctx, const string &password);
69
bool authenticate(const SecurityContext &sctx, const string &password);
77
72
* Verify the local and remote scrambled password match using the MySQL
94
89
* Cache or username:password entries from the file.
96
std::map<string, string> users;
91
map<string, string> users;
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),
112
106
bool AuthFile::loadFile(void)
114
ifstream file(users_file.string().c_str());
108
ifstream file(users_file);
116
110
if (!file.is_open())
118
112
error = "Could not open users file: ";
119
error += users_file.string();
140
134
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));
137
pair<map<string, string>::iterator, bool> result;
138
result = users.insert(pair<string, string>(username, password));
146
139
if (result.second == false)
148
141
error = "Duplicate entry found in users file: ";
202
195
return memcmp(local_scrambled_password, scrambled_password_check, SHA1_DIGEST_LENGTH) == 0;
205
bool AuthFile::authenticate(const identifier::User &sctx, const string &password)
198
bool AuthFile::authenticate(const SecurityContext &sctx, const string &password)
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())
211
if (sctx.getPasswordType() == identifier::User::MYSQL_HASH)
204
if (sctx.getPasswordType() == SecurityContext::MYSQL_HASH)
212
205
return verifyMySQLHash(user->second, sctx.getPasswordContext(), password);
214
207
if (password == user->second)
222
215
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())
227
errmsg_printf(error::ERROR, _("Could not load auth file: %s\n"),
217
if (vm.count("users"))
219
users_file= const_cast<char *>(vm["users"].as<string>().c_str());
222
AuthFile *auth_file = new AuthFile("auth_file");
223
if (!auth_file->loadFile())
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;
233
231
context.add(auth_file);
234
context.registerVariable(new sys_var_const_string_val("users", vm["users"].as<string>()));
235
static DRIZZLE_SYSVAR_STR(users,
238
N_("File to load for usernames and passwords"),
239
NULL, /* check func */
240
NULL, /* update func*/
241
DEFAULT_USERS_FILE /* default */);
243
static drizzle_sys_var* sys_variables[]=
245
DRIZZLE_SYSVAR(users),
240
249
static void init_options(drizzled::module::option_context &context)
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"));
247
256
} /* namespace auth_file */
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);