1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Monty Taylor <mordred@inaugust.com>
5
* Copyright (C) 2011 Canonical, Ltd.
6
* Author: Clint Byrum <clint.byrum@canonical.com>
8
* Copied from simple_user_policy
10
* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; version 2 of the License.
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29
#include <boost/regex.hpp>
30
#include <boost/unordered_map.hpp>
31
#include <boost/thread/mutex.hpp>
33
#include <drizzled/configmake.h>
34
#include <drizzled/plugin/authorization.h>
36
namespace fs= boost::filesystem;
38
namespace regex_policy {
40
static const fs::path DEFAULT_POLICY_FILE= SYSCONFDIR "/drizzle.policy";
42
static const char *comment_regex = "^[[:space:]]*#.*$";
43
static const char *empty_regex = "^[[:space:]]*$";
44
static const char *table_match_regex = "^([^ ]+) table\\=([^ ]+) (ACCEPT|DENY)$";
45
static const char *process_match_regex = "^([^ ]+) process\\=([^ ]+) (ACCEPT|DENY)$";
46
static const char *schema_match_regex = "^([^ ]+) schema\\=([^ ]+) (ACCEPT|DENY)$";
47
/* These correspond to the parenthesis above and must stay in sync */
48
static const int MATCH_REGEX_USER_POS= 1;
49
static const int MATCH_REGEX_OBJECT_POS= 2;
50
static const int MATCH_REGEX_ACTION_POS= 3;
60
const std::string user;
61
const std::string object;
62
const boost::regex user_re;
63
const boost::regex object_re;
66
PolicyItem(const std::string &u, const std::string &obj, const std::string &act) :
74
action = POLICY_ACCEPT;
76
else if (act == "DENY")
82
throw std::exception();
85
bool userMatches(std::string &str);
86
bool objectMatches(std::string &object_id);
87
bool isRestricted() const;
88
const std::string&getUser() const
92
const std::string&getObject() const
96
const char *getAction() const
98
return action == POLICY_ACCEPT ? "ACCEPT" : "DENY";
102
typedef std::list<PolicyItem *> PolicyItemList;
103
typedef boost::unordered_map<std::string, bool> CheckMap;
105
static boost::mutex check_cache_mutex;
112
bool has_cached_result;
114
CheckMap **check_cache;
116
CheckItem(const std::string &u, const std::string &obj, CheckMap **check_cache);
117
bool operator()(PolicyItem *p);
118
bool hasCachedResult() const
120
return has_cached_result;
122
bool getCachedResult() const
124
return cached_result;
126
void setCachedResult(bool result);
129
inline bool PolicyItem::userMatches(std::string &str)
131
return boost::regex_match(str, user_re);
134
inline bool PolicyItem::objectMatches(std::string &object_id)
136
return boost::regex_match(object_id, object_re);
139
inline bool PolicyItem::isRestricted() const
141
return action == POLICY_DENY;
144
void clearPolicyItemList(PolicyItemList policies);
147
public drizzled::plugin::Authorization
150
Policy(const fs::path &f_path) :
151
drizzled::plugin::Authorization("Regex Policy"), policy_file(f_path), error(),
152
table_check_cache(NULL), schema_check_cache(NULL), process_check_cache(NULL)
155
virtual bool restrictSchema(const drizzled::identifier::User &user_ctx,
156
const drizzled::identifier::Schema& schema);
158
virtual bool restrictProcess(const drizzled::identifier::User &user_ctx,
159
const drizzled::identifier::User &session_ctx);
161
virtual bool restrictTable(const drizzled::identifier::User& user_ctx,
162
const drizzled::identifier::Table& table);
165
std::stringstream &getError() { return error; }
168
bool restrictObject(const drizzled::identifier::User &user_ctx,
169
const std::string &obj, const PolicyItemList &policies,
170
CheckMap **check_cache);
171
fs::path policy_file;
172
std::stringstream error;
173
PolicyItemList table_policies;
174
PolicyItemList schema_policies;
175
PolicyItemList process_policies;
176
CheckMap *table_check_cache;
177
CheckMap *schema_check_cache;
178
CheckMap *process_check_cache;
181
} /* namespace regex_policy */