1432.1.1
by Eric Day
Added auth_file plugin. |
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 |
||
26 |
#include "drizzled/configmake.h" |
|
27 |
#include "drizzled/plugin/authentication.h" |
|
28 |
#include "drizzled/security_context.h" |
|
29 |
#include "drizzled/util/convert.h" |
|
30 |
#include "drizzled/algorithm/sha1.h" |
|
31 |
||
32 |
using namespace std; |
|
33 |
using namespace drizzled; |
|
34 |
||
35 |
namespace auth_file |
|
36 |
{
|
|
37 |
||
38 |
static char* users_file= NULL; |
|
39 |
static const char DEFAULT_USERS_FILE[]= SYSCONFDIR "/drizzle.users"; |
|
40 |
||
41 |
class AuthFile: public plugin::Authentication |
|
42 |
{
|
|
43 |
public: |
|
44 |
||
45 |
AuthFile(string name_arg); |
|
46 |
||
47 |
/**
|
|
48 |
* Retrieve the last error encountered in the class.
|
|
49 |
*/
|
|
50 |
string& getError(void); |
|
51 |
||
52 |
/**
|
|
53 |
* Load the users file into a map cache.
|
|
54 |
*
|
|
55 |
* @return True on success, false on error. If false is returned an error
|
|
56 |
* is set and can be retrieved with getError().
|
|
57 |
*/
|
|
58 |
bool loadFile(void); |
|
59 |
||
60 |
private: |
|
61 |
||
62 |
/**
|
|
63 |
* Base class method to check authentication for a user.
|
|
64 |
*/
|
|
65 |
bool authenticate(const SecurityContext &sctx, const string &password); |
|
66 |
||
67 |
/**
|
|
68 |
* Verify the local and remote scrambled password match using the MySQL
|
|
69 |
* hashing algorithm.
|
|
70 |
*
|
|
71 |
* @param[in] password Plain text password that is stored locally.
|
|
72 |
* @param[in] scramble_bytes The random bytes that the server sent to the
|
|
73 |
* client for scrambling the password.
|
|
74 |
* @param[in] scrambled_password The result of the client scrambling the
|
|
75 |
* password remotely.
|
|
76 |
* @return True if the password matched, false if not.
|
|
77 |
*/
|
|
78 |
bool verifyMySQLHash(const string &password, |
|
79 |
const string &scramble_bytes, |
|
80 |
const string &scrambled_password); |
|
81 |
||
82 |
string error; |
|
83 |
||
84 |
/**
|
|
85 |
* Cache or username:password entries from the file.
|
|
86 |
*/
|
|
87 |
map<string, string> users; |
|
88 |
};
|
|
89 |
||
90 |
AuthFile::AuthFile(string name_arg): |
|
91 |
plugin::Authentication(name_arg), |
|
92 |
error(), |
|
93 |
users() |
|
94 |
{
|
|
95 |
}
|
|
96 |
||
97 |
string& AuthFile::getError(void) |
|
98 |
{
|
|
99 |
return error; |
|
100 |
}
|
|
101 |
||
102 |
bool AuthFile::loadFile(void) |
|
103 |
{
|
|
104 |
ifstream file(users_file); |
|
105 |
||
106 |
if (!file.is_open()) |
|
107 |
{
|
|
108 |
error = "Could not open users file: "; |
|
109 |
error += users_file; |
|
110 |
return false; |
|
111 |
}
|
|
112 |
||
113 |
while (!file.eof()) |
|
114 |
{
|
|
115 |
string line; |
|
116 |
getline(file, line); |
|
117 |
||
118 |
/* Ignore blank lines and lines starting with '#'. */
|
|
119 |
if (line == "" || line[line.find_first_not_of(" \t")] == '#') |
|
120 |
continue; |
|
121 |
||
122 |
string username; |
|
123 |
string password; |
|
124 |
size_t password_offset = line.find(":"); |
|
125 |
if (password_offset == string::npos) |
|
126 |
username = line; |
|
127 |
else
|
|
128 |
{
|
|
129 |
username = string(line, 0, password_offset); |
|
130 |
password = string(line, password_offset + 1); |
|
131 |
}
|
|
132 |
||
133 |
pair<map<string, string>::iterator, bool> result; |
|
134 |
result = users.insert(pair<string, string>(username, password)); |
|
135 |
if (result.second == false) |
|
136 |
{
|
|
137 |
error = "Duplicate entry found in users file: "; |
|
138 |
error += username; |
|
139 |
file.close(); |
|
140 |
return false; |
|
141 |
}
|
|
142 |
}
|
|
143 |
||
144 |
file.close(); |
|
145 |
return true; |
|
146 |
}
|
|
147 |
||
148 |
bool AuthFile::verifyMySQLHash(const string &password, |
|
149 |
const string &scramble_bytes, |
|
150 |
const string &scrambled_password) |
|
151 |
{
|
|
152 |
if (scramble_bytes.size() != SHA1_DIGEST_LENGTH || |
|
153 |
scrambled_password.size() != SHA1_DIGEST_LENGTH) |
|
154 |
{
|
|
155 |
return false; |
|
156 |
}
|
|
157 |
||
158 |
SHA1_CTX ctx; |
|
159 |
uint8_t local_scrambled_password[SHA1_DIGEST_LENGTH]; |
|
160 |
uint8_t temp_hash[SHA1_DIGEST_LENGTH]; |
|
161 |
uint8_t scrambled_password_check[SHA1_DIGEST_LENGTH]; |
|
162 |
||
163 |
/* Generate the double SHA1 hash for the password stored locally first. */
|
|
164 |
SHA1Init(&ctx); |
|
165 |
SHA1Update(&ctx, reinterpret_cast<const uint8_t *>(password.c_str()), |
|
166 |
password.size()); |
|
167 |
SHA1Final(temp_hash, &ctx); |
|
168 |
||
169 |
SHA1Init(&ctx); |
|
170 |
SHA1Update(&ctx, temp_hash, SHA1_DIGEST_LENGTH); |
|
171 |
SHA1Final(local_scrambled_password, &ctx); |
|
172 |
||
173 |
/* Hash the scramble that was sent to client with the local password. */
|
|
174 |
SHA1Init(&ctx); |
|
175 |
SHA1Update(&ctx, reinterpret_cast<const uint8_t*>(scramble_bytes.c_str()), |
|
176 |
SHA1_DIGEST_LENGTH); |
|
177 |
SHA1Update(&ctx, local_scrambled_password, SHA1_DIGEST_LENGTH); |
|
178 |
SHA1Final(temp_hash, &ctx); |
|
179 |
||
180 |
/* Next, XOR the result with what the client sent to get the original
|
|
181 |
single-hashed password. */
|
|
182 |
for (int x= 0; x < SHA1_DIGEST_LENGTH; x++) |
|
183 |
temp_hash[x]= temp_hash[x] ^ scrambled_password[x]; |
|
184 |
||
185 |
/* Hash this result once more to get the double-hashed password again. */
|
|
186 |
SHA1Init(&ctx); |
|
187 |
SHA1Update(&ctx, temp_hash, SHA1_DIGEST_LENGTH); |
|
188 |
SHA1Final(scrambled_password_check, &ctx); |
|
189 |
||
190 |
/* These should match for a successful auth. */
|
|
191 |
return memcmp(local_scrambled_password, scrambled_password_check, SHA1_DIGEST_LENGTH) == 0; |
|
192 |
}
|
|
193 |
||
194 |
bool AuthFile::authenticate(const SecurityContext &sctx, const string &password) |
|
195 |
{
|
|
196 |
map<string, string>::const_iterator user = users.find(sctx.getUser()); |
|
197 |
if (user == users.end()) |
|
198 |
return false; |
|
199 |
||
200 |
if (sctx.getPasswordType() == SecurityContext::MYSQL_HASH) |
|
201 |
return verifyMySQLHash(user->second, sctx.getPasswordContext(), password); |
|
202 |
||
203 |
if (password == user->second) |
|
204 |
return true; |
|
205 |
||
206 |
return false; |
|
207 |
}
|
|
208 |
||
209 |
static int init(plugin::Context &context) |
|
210 |
{
|
|
211 |
AuthFile *auth_file = new AuthFile("auth_file"); |
|
212 |
if (!auth_file->loadFile()) |
|
213 |
{
|
|
214 |
errmsg_printf(ERRMSG_LVL_ERROR, _("Could not load auth file: %s\n"), |
|
215 |
auth_file->getError().c_str()); |
|
216 |
delete auth_file; |
|
217 |
return 1; |
|
218 |
}
|
|
219 |
||
220 |
context.add(auth_file); |
|
221 |
return 0; |
|
222 |
}
|
|
223 |
||
224 |
static DRIZZLE_SYSVAR_STR(users, |
|
225 |
users_file, |
|
226 |
PLUGIN_VAR_READONLY, |
|
227 |
N_("File to load for usernames and passwords"), |
|
228 |
NULL, /* check func */ |
|
229 |
NULL, /* update func*/ |
|
230 |
DEFAULT_USERS_FILE /* default */); |
|
231 |
||
232 |
static drizzle_sys_var* sys_variables[]= |
|
233 |
{
|
|
234 |
DRIZZLE_SYSVAR(users), |
|
235 |
NULL
|
|
236 |
};
|
|
237 |
||
238 |
} /* namespace auth_file */ |
|
239 |
||
240 |
DRIZZLE_PLUGIN(auth_file::init, auth_file::sys_variables); |