~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/auth_file/auth_file.cc

Reverted 1103

Show diffs side-by-side

added added

removed removed

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