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>
28
#include <boost/filesystem.hpp>
30
#include "drizzled/configmake.h"
31
#include "drizzled/plugin/authentication.h"
32
#include "drizzled/identifier.h"
33
#include "drizzled/util/convert.h"
34
#include "drizzled/algorithm/sha1.h"
35
#include "drizzled/module/option_map.h"
37
namespace po= boost::program_options;
38
namespace fs= boost::filesystem;
41
using namespace drizzled;
46
static const fs::path DEFAULT_USERS_FILE= SYSCONFDIR "/drizzle.users";
48
class AuthFile: public plugin::Authentication
50
const fs::path users_file;
54
AuthFile(string name_arg, fs::path users_file_arg);
57
* Retrieve the last error encountered in the class.
59
string& getError(void);
62
* Load the users file into a map cache.
64
* @return True on success, false on error. If false is returned an error
65
* is set and can be retrieved with getError().
72
* Base class method to check authentication for a user.
74
bool authenticate(const identifier::User &sctx, const string &password);
77
* Verify the local and remote scrambled password match using the MySQL
80
* @param[in] password Plain text password that is stored locally.
81
* @param[in] scramble_bytes The random bytes that the server sent to the
82
* client for scrambling the password.
83
* @param[in] scrambled_password The result of the client scrambling the
85
* @return True if the password matched, false if not.
87
bool verifyMySQLHash(const string &password,
88
const string &scramble_bytes,
89
const string &scrambled_password);
94
* Cache or username:password entries from the file.
96
std::map<string, string> users;
99
AuthFile::AuthFile(string name_arg, fs::path users_file_arg):
100
plugin::Authentication(name_arg),
101
users_file(users_file_arg),
107
string& AuthFile::getError(void)
112
bool AuthFile::loadFile(void)
114
ifstream file(users_file.string().c_str());
118
error = "Could not open users file: ";
119
error += users_file.string();
128
/* Ignore blank lines and lines starting with '#'. */
129
if (line == "" || line[line.find_first_not_of(" \t")] == '#')
134
size_t password_offset = line.find(":");
135
if (password_offset == string::npos)
139
username = string(line, 0, password_offset);
140
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));
146
if (result.second == false)
148
error = "Duplicate entry found in users file: ";
159
bool AuthFile::verifyMySQLHash(const string &password,
160
const string &scramble_bytes,
161
const string &scrambled_password)
163
if (scramble_bytes.size() != SHA1_DIGEST_LENGTH ||
164
scrambled_password.size() != SHA1_DIGEST_LENGTH)
170
uint8_t local_scrambled_password[SHA1_DIGEST_LENGTH];
171
uint8_t temp_hash[SHA1_DIGEST_LENGTH];
172
uint8_t scrambled_password_check[SHA1_DIGEST_LENGTH];
174
/* Generate the double SHA1 hash for the password stored locally first. */
176
SHA1Update(&ctx, reinterpret_cast<const uint8_t *>(password.c_str()),
178
SHA1Final(temp_hash, &ctx);
181
SHA1Update(&ctx, temp_hash, SHA1_DIGEST_LENGTH);
182
SHA1Final(local_scrambled_password, &ctx);
184
/* Hash the scramble that was sent to client with the local password. */
186
SHA1Update(&ctx, reinterpret_cast<const uint8_t*>(scramble_bytes.c_str()),
188
SHA1Update(&ctx, local_scrambled_password, SHA1_DIGEST_LENGTH);
189
SHA1Final(temp_hash, &ctx);
191
/* Next, XOR the result with what the client sent to get the original
192
single-hashed password. */
193
for (int x= 0; x < SHA1_DIGEST_LENGTH; x++)
194
temp_hash[x]= temp_hash[x] ^ scrambled_password[x];
196
/* Hash this result once more to get the double-hashed password again. */
198
SHA1Update(&ctx, temp_hash, SHA1_DIGEST_LENGTH);
199
SHA1Final(scrambled_password_check, &ctx);
201
/* These should match for a successful auth. */
202
return memcmp(local_scrambled_password, scrambled_password_check, SHA1_DIGEST_LENGTH) == 0;
205
bool AuthFile::authenticate(const identifier::User &sctx, const string &password)
207
std::map<std::string, std::string>::const_iterator user= users.find(sctx.username());
208
if (user == users.end())
211
if (sctx.getPasswordType() == identifier::User::MYSQL_HASH)
212
return verifyMySQLHash(user->second, sctx.getPasswordContext(), password);
214
if (password == user->second)
220
static int init(module::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())
227
errmsg_printf(error::ERROR, _("Could not load auth file: %s\n"),
228
auth_file->getError().c_str());
233
context.add(auth_file);
234
context.registerVariable(new sys_var_const_string_val("users", vm["users"].as<string>()));
240
static void init_options(drizzled::module::option_context &context)
243
po::value<string>()->default_value(DEFAULT_USERS_FILE.string()),
244
N_("File to load for usernames and passwords"));
247
} /* namespace auth_file */
249
DRIZZLE_PLUGIN(auth_file::init, NULL, auth_file::init_options);