1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Eric Day
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27
#include <boost/program_options.hpp>
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
namespace po= boost::program_options;
38
using namespace drizzled;
43
static const char DEFAULT_USERS_FILE[]= SYSCONFDIR "/drizzle.users";
45
class AuthFile: public plugin::Authentication
47
const std::string users_file;
51
AuthFile(string name_arg, string users_file_arg);
54
* Retrieve the last error encountered in the class.
56
string& getError(void);
59
* Load the users file into a map cache.
61
* @return True on success, false on error. If false is returned an error
62
* is set and can be retrieved with getError().
69
* Base class method to check authentication for a user.
71
bool authenticate(const SecurityContext &sctx, const string &password);
74
* Verify the local and remote scrambled password match using the MySQL
77
* @param[in] password Plain text password that is stored locally.
78
* @param[in] scramble_bytes The random bytes that the server sent to the
79
* client for scrambling the password.
80
* @param[in] scrambled_password The result of the client scrambling the
82
* @return True if the password matched, false if not.
84
bool verifyMySQLHash(const string &password,
85
const string &scramble_bytes,
86
const string &scrambled_password);
91
* Cache or username:password entries from the file.
93
map<string, string> users;
96
AuthFile::AuthFile(string name_arg, string users_file_arg):
97
plugin::Authentication(name_arg),
98
users_file(users_file_arg),
104
string& AuthFile::getError(void)
109
bool AuthFile::loadFile(void)
111
ifstream file(users_file.c_str());
115
error = "Could not open users file: ";
125
/* Ignore blank lines and lines starting with '#'. */
126
if (line == "" || line[line.find_first_not_of(" \t")] == '#')
131
size_t password_offset = line.find(":");
132
if (password_offset == string::npos)
136
username = string(line, 0, password_offset);
137
password = string(line, password_offset + 1);
140
pair<map<string, string>::iterator, bool> result=
141
users.insert(pair<string, string>(username, password));
142
if (result.second == false)
144
error = "Duplicate entry found in users file: ";
155
bool AuthFile::verifyMySQLHash(const string &password,
156
const string &scramble_bytes,
157
const string &scrambled_password)
159
if (scramble_bytes.size() != SHA1_DIGEST_LENGTH ||
160
scrambled_password.size() != SHA1_DIGEST_LENGTH)
166
uint8_t local_scrambled_password[SHA1_DIGEST_LENGTH];
167
uint8_t temp_hash[SHA1_DIGEST_LENGTH];
168
uint8_t scrambled_password_check[SHA1_DIGEST_LENGTH];
170
/* Generate the double SHA1 hash for the password stored locally first. */
172
SHA1Update(&ctx, reinterpret_cast<const uint8_t *>(password.c_str()),
174
SHA1Final(temp_hash, &ctx);
177
SHA1Update(&ctx, temp_hash, SHA1_DIGEST_LENGTH);
178
SHA1Final(local_scrambled_password, &ctx);
180
/* Hash the scramble that was sent to client with the local password. */
182
SHA1Update(&ctx, reinterpret_cast<const uint8_t*>(scramble_bytes.c_str()),
184
SHA1Update(&ctx, local_scrambled_password, SHA1_DIGEST_LENGTH);
185
SHA1Final(temp_hash, &ctx);
187
/* Next, XOR the result with what the client sent to get the original
188
single-hashed password. */
189
for (int x= 0; x < SHA1_DIGEST_LENGTH; x++)
190
temp_hash[x]= temp_hash[x] ^ scrambled_password[x];
192
/* Hash this result once more to get the double-hashed password again. */
194
SHA1Update(&ctx, temp_hash, SHA1_DIGEST_LENGTH);
195
SHA1Final(scrambled_password_check, &ctx);
197
/* These should match for a successful auth. */
198
return memcmp(local_scrambled_password, scrambled_password_check, SHA1_DIGEST_LENGTH) == 0;
201
bool AuthFile::authenticate(const SecurityContext &sctx, const string &password)
203
map<string, string>::const_iterator user = users.find(sctx.getUser());
204
if (user == users.end())
207
if (sctx.getPasswordType() == SecurityContext::MYSQL_HASH)
208
return verifyMySQLHash(user->second, sctx.getPasswordContext(), password);
210
if (password == user->second)
216
static int init(module::Context &context)
218
const module::option_map &vm= context.getOptions();
220
AuthFile *auth_file = new AuthFile("auth_file", vm["users"].as<string>());
221
if (!auth_file->loadFile())
223
errmsg_printf(ERRMSG_LVL_ERROR, _("Could not load auth file: %s\n"),
224
auth_file->getError().c_str());
229
context.add(auth_file);
230
context.registerVariable(new sys_var_const_string_val("users", vm["users"].as<string>()));
235
static void init_options(drizzled::module::option_context &context)
238
po::value<string>()->default_value(DEFAULT_USERS_FILE),
239
N_("File to load for usernames and passwords"));
242
} /* namespace auth_file */
244
DRIZZLE_PLUGIN(auth_file::init, NULL, auth_file::init_options);