drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
auth_file.cc
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2010 Eric Day
5 *
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.
9 *
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.
14 *
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
18 */
19
20#include <config.h>
21
22#include <fstream>
23#include <map>
24#include <string>
25#include <iostream>
26
27#include <boost/program_options.hpp>
28#include <boost/filesystem.hpp>
29
30#include <drizzled/item.h>
31#include <drizzled/configmake.h>
32#include <drizzled/plugin/authentication.h>
33#include <drizzled/identifier.h>
34#include <drizzled/util/convert.h>
37
38namespace po= boost::program_options;
39namespace fs= boost::filesystem;
40
41using namespace std;
42using namespace drizzled;
43
44namespace auth_file {
45
46static const fs::path DEFAULT_USERS_FILE= SYSCONFDIR "/drizzle.users";
47typedef std::map<string, string> users_t;
48bool updateUsersFile(Session *, set_var *);
49bool parseUsersFile(std::string, users_t&);
50
51class AuthFile : public plugin::Authentication
52{
53public:
54 AuthFile(std::string users_file_arg);
55
59 const string& getError() const;
60
61 std::string& getUsersFile();
62 bool setUsersFile(std::string& usersFile);
63
64private:
65
69 bool authenticate(const identifier::User &sctx, const string &password);
70
82 bool verifyMySQLHash(const string &password,
83 const string &scramble_bytes,
84 const string &scrambled_password);
85
86 string error;
87 fs::path users_file;
88 std::string sysvar_users_file;
89
93 users_t users;
98 void setUsers(users_t);
103 void clearUsers();
104};
105AuthFile *auth_file= NULL;
106
107AuthFile::AuthFile(std::string users_file_arg) :
108 plugin::Authentication("auth_file"),
109 users_file(users_file_arg), sysvar_users_file(users_file_arg)
110{
111}
112
113const string& AuthFile::getError() const
114{
115 return error;
116}
117
118std::string& AuthFile::getUsersFile()
119{
120 return sysvar_users_file;
121}
122
123bool AuthFile::setUsersFile(std::string& usersFile)
124{
125 if (usersFile.empty())
126 {
127 errmsg_printf(error::ERROR, _("users file cannot be an empty string"));
128 return false; // error
129 }
130 users_t users_dummy;
131 if(parseUsersFile(usersFile, users_dummy))
132 {
133 this->clearUsers();
134 this->setUsers(users_dummy);
135 sysvar_users_file= usersFile;
136 fs::path newUsersFile(getUsersFile());
137 users_file= newUsersFile;
138 return true; //success
139 }
140 return false; // error
141}
142
143bool AuthFile::verifyMySQLHash(const string &password,
144 const string &scramble_bytes,
145 const string &scrambled_password)
146{
147 if (scramble_bytes.size() != SHA1_DIGEST_LENGTH || scrambled_password.size() != SHA1_DIGEST_LENGTH)
148 {
149 return false;
150 }
151
152 SHA1_CTX ctx;
153 uint8_t local_scrambled_password[SHA1_DIGEST_LENGTH];
154 uint8_t temp_hash[SHA1_DIGEST_LENGTH];
155 uint8_t scrambled_password_check[SHA1_DIGEST_LENGTH];
156
157 /* Generate the double SHA1 hash for the password stored locally first. */
158 SHA1Init(&ctx);
159 SHA1Update(&ctx, reinterpret_cast<const uint8_t *>(password.c_str()), password.size());
160 SHA1Final(temp_hash, &ctx);
161
162 SHA1Init(&ctx);
163 SHA1Update(&ctx, temp_hash, SHA1_DIGEST_LENGTH);
164 SHA1Final(local_scrambled_password, &ctx);
165
166 /* Hash the scramble that was sent to client with the local password. */
167 SHA1Init(&ctx);
168 SHA1Update(&ctx, reinterpret_cast<const uint8_t*>(scramble_bytes.c_str()), SHA1_DIGEST_LENGTH);
169 SHA1Update(&ctx, local_scrambled_password, SHA1_DIGEST_LENGTH);
170 SHA1Final(temp_hash, &ctx);
171
172 /* Next, XOR the result with what the client sent to get the original
173 single-hashed password. */
174 for (int x= 0; x < SHA1_DIGEST_LENGTH; x++)
175 temp_hash[x]= temp_hash[x] ^ scrambled_password[x];
176
177 /* Hash this result once more to get the double-hashed password again. */
178 SHA1Init(&ctx);
179 SHA1Update(&ctx, temp_hash, SHA1_DIGEST_LENGTH);
180 SHA1Final(scrambled_password_check, &ctx);
181
182 /* These should match for a successful auth. */
183 return memcmp(local_scrambled_password, scrambled_password_check, SHA1_DIGEST_LENGTH) == 0;
184}
185
186bool AuthFile::authenticate(const identifier::User &sctx, const string &password)
187{
188 string* user= find_ptr(users, sctx.username());
189 if (not user)
190 return false;
191 return sctx.getPasswordType() == identifier::User::MYSQL_HASH
192 ? verifyMySQLHash(*user, sctx.getPasswordContext(), password)
193 : password == *user;
194}
195
196void AuthFile::setUsers(users_t users_dummy)
197{
198 users.insert(users_dummy.begin(), users_dummy.end());
199}
200
202{
203 users.clear();
204}
205
211bool updateUsersFile(Session *, set_var* var)
212{
213 if (not var->value->str_value.empty())
214 {
215 std::string newUsersFile(var->value->str_value.data());
216 if (auth_file->setUsersFile(newUsersFile))
217 return false; //success
218 else
219 return true; // error
220 }
221 errmsg_printf(error::ERROR, _("auth_file file cannot be NULL"));
222 return true; // error
223}
224
231bool parseUsersFile(std::string new_users_file, users_t& users_dummy)
232{
233 ifstream file(new_users_file.c_str());
234
235 if (!file.is_open())
236 {
237 errmsg_printf(error::ERROR, _("Could not open users file: %s"), new_users_file.c_str());
238 return false;
239 }
240
241 string line;
242 try
243 {
244 while (getline(file, line))
245 {
246 /* Ignore blank lines and lines starting with '#'. */
247 if (line.empty() || line[line.find_first_not_of(" \t")] == '#')
248 continue;
249
250 string username;
251 string password;
252 size_t password_offset = line.find(":");
253 if (password_offset == string::npos)
254 username = line;
255 else
256 {
257 username = string(line, 0, password_offset);
258 password = string(line, password_offset + 1);
259 }
260
261 if (not users_dummy.insert(pair<string, string>(username, password)).second)
262 {
263 errmsg_printf(error::ERROR, _("Duplicate entry found in users file: %s"), username.c_str());
264 return false;
265 }
266 }
267 return true;
268 }
269 catch (const std::exception &e)
270 {
271 /* On any non-EOF break, unparseable line */
272 errmsg_printf(error::ERROR, _("Unable to parse users file %s:%s"), new_users_file.c_str(), e.what());
273 return false;
274 }
275}
276
277static int init(module::Context &context)
278{
279 const module::option_map &vm= context.getOptions();
280
281 auth_file= new AuthFile(vm["users"].as<string>());
282 if (!auth_file->setUsersFile(auth_file->getUsersFile()))
283 {
284 errmsg_printf(error::ERROR, _("Could not load auth file: %s\n"), auth_file->getError().c_str());
285 delete auth_file;
286 return 1;
287 }
288
289 context.add(auth_file);
290 context.registerVariable(new sys_var_std_string("users", auth_file->getUsersFile(), NULL, &updateUsersFile));
291
292 return 0;
293}
294
295
296static void init_options(drizzled::module::option_context &context)
297{
298 context("users",
299 po::value<string>()->default_value(DEFAULT_USERS_FILE.string()),
300 N_("File to load for usernames and passwords"));
301}
302
303} /* namespace auth_file */
304
305DRIZZLE_DECLARE_PLUGIN
306{
307 DRIZZLE_VERSION_ID,
308 "auth_file",
309 "0.1",
310 "Eric Day",
311 N_("Authentication against a plain text file"),
312 PLUGIN_LICENSE_GPL,
313 auth_file::init,
314 NULL,
315 auth_file::init_options
316}
317DRIZZLE_DECLARE_PLUGIN_END;
bool authenticate(const identifier::User &sctx, const string &password)
Definition auth_file.cc:186
const string & getError() const
Definition auth_file.cc:113
bool verifyMySQLHash(const string &password, const string &scramble_bytes, const string &scrambled_password)
Definition auth_file.cc:143
void setUsers(users_t)
Definition auth_file.cc:196
A set of Session members describing the current authenticated user.
TODO: Rename this file - func.h is stupid.
STL namespace.
An Proxy Wrapper around boost::program_options::variables_map.
SHA1 Declarations.