43
44
namespace auth_file {
45
46
static const fs::path DEFAULT_USERS_FILE= SYSCONFDIR "/drizzle.users";
47
typedef std::map<string, string> users_t;
48
bool updateUsersFile(Session *, set_var *);
49
bool parseUsersFile(std::string, users_t&);
47
51
class AuthFile : public plugin::Authentication
50
AuthFile(fs::path users_file_arg);
54
AuthFile(std::string users_file_arg);
53
57
* Retrieve the last error encountered in the class.
55
59
const string& getError() const;
58
* Load the users file into a map cache.
60
* @return True on success, false on error. If false is returned an error
61
* is set and can be retrieved with getError().
61
std::string& getUsersFile();
62
bool setUsersFile(std::string& usersFile);
85
84
const string &scrambled_password);
88
const fs::path users_file;
88
std::string sysvar_users_file;
91
91
* Cache or username:password entries from the file.
93
typedef std::map<string, string> users_t;
95
* This method is called to update the users cache with the new
96
* username:password pairs given in new users file upon update.
98
void setUsers(users_t);
100
* This method is called to delete all the cached username:password pairs
101
* when users file is updated.
105
AuthFile *auth_file= NULL;
97
AuthFile::AuthFile(fs::path users_file_arg) :
107
AuthFile::AuthFile(std::string users_file_arg) :
98
108
plugin::Authentication("auth_file"),
99
users_file(users_file_arg)
109
users_file(users_file_arg), sysvar_users_file(users_file_arg)
108
bool AuthFile::loadFile()
110
ifstream file(users_file.string().c_str());
114
error = "Could not open users file: " + users_file.string();
119
while (getline(file, line))
121
/* Ignore blank lines and lines starting with '#'. */
122
if (line.empty() || line[line.find_first_not_of(" \t")] == '#')
127
size_t password_offset = line.find(":");
128
if (password_offset == string::npos)
132
username = string(line, 0, password_offset);
133
password = string(line, password_offset + 1);
136
if (not users.insert(pair<string, string>(username, password)).second)
138
error = "Duplicate entry found in users file: " + username;
118
std::string& AuthFile::getUsersFile()
120
return sysvar_users_file;
123
bool AuthFile::setUsersFile(std::string& usersFile)
125
if (usersFile.empty())
127
errmsg_printf(error::ERROR, _("users file cannot be an empty string"));
128
return false; // error
131
if(parseUsersFile(usersFile, users_dummy))
134
this->setUsers(users_dummy);
135
sysvar_users_file= usersFile;
136
fs::path newUsersFile(getUsersFile());
137
users_file= newUsersFile;
138
return true; //success
140
return false; // error
145
143
bool AuthFile::verifyMySQLHash(const string &password,
195
193
: password == *user;
196
void AuthFile::setUsers(users_t users_dummy)
198
users.insert(users_dummy.begin(), users_dummy.end());
201
void AuthFile::clearUsers()
207
* This function is called when the value of users file is changed in the system.
209
* @return False on success, True on error.
211
bool updateUsersFile(Session *, set_var* var)
213
if (not var->value->str_value.empty())
215
std::string newUsersFile(var->value->str_value.data());
216
if (auth_file->setUsersFile(newUsersFile))
217
return false; //success
219
return true; // error
221
errmsg_printf(error::ERROR, _("auth_file file cannot be NULL"));
222
return true; // error
226
* Parse the users file into a map cache.
228
* @return True on success, false on error. If an error is encountered, false is
229
* returned with error message printed.
231
bool parseUsersFile(std::string new_users_file, users_t& users_dummy)
233
ifstream file(new_users_file.c_str());
237
string error_msg= "Could not open users file: " + new_users_file;
238
errmsg_printf(error::ERROR, _(error_msg.c_str()));
245
while (getline(file, line))
247
/* Ignore blank lines and lines starting with '#'. */
248
if (line.empty() || line[line.find_first_not_of(" \t")] == '#')
253
size_t password_offset = line.find(":");
254
if (password_offset == string::npos)
258
username = string(line, 0, password_offset);
259
password = string(line, password_offset + 1);
262
if (not users_dummy.insert(pair<string, string>(username, password)).second)
264
string error_msg= "Duplicate entry found in users file: " + username;
265
errmsg_printf(error::ERROR, _(error_msg.c_str()));
271
catch (const std::exception &e)
273
/* On any non-EOF break, unparseable line */
274
string error_msg= "Unable to parse users file " + new_users_file + ":" + e.what();
275
errmsg_printf(error::ERROR, _(error_msg.c_str()));
198
280
static int init(module::Context &context)
200
282
const module::option_map &vm= context.getOptions();
202
AuthFile *auth_file = new AuthFile(fs::path(vm["users"].as<string>()));
203
if (not auth_file->loadFile())
284
auth_file= new AuthFile(vm["users"].as<string>());
285
if (!auth_file->setUsersFile(auth_file->getUsersFile()))
205
287
errmsg_printf(error::ERROR, _("Could not load auth file: %s\n"), auth_file->getError().c_str());
206
288
delete auth_file;