~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/auth_file/auth_file.cc

  • Committer: Stewart Smith
  • Date: 2010-11-03 03:27:09 UTC
  • mto: (1902.1.1 build) (1910.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1903.
  • Revision ID: stewart@flamingspork.com-20101103032709-oyvfrc6eb8fzj0mr
fix docs warning: docs/unlock.rst:2: (WARNING/2) Title underline too short.

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