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
30
#include <boost/regex.hpp>
31
#include <boost/unordered_map.hpp>
32
#include <boost/thread/mutex.hpp>
34
#include <drizzled/configmake.h>
35
#include <drizzled/plugin/authorization.h>
37
namespace fs= boost::filesystem;
39
namespace regex_policy
42
static const fs::path DEFAULT_POLICY_FILE= SYSCONFDIR "/drizzle.policy";
44
static const char *comment_regex = "^[[:space:]]*#.*$";
45
static const char *empty_regex = "^[[:space:]]*$";
46
static const char *table_match_regex = "^([^ ]+) table\\=([^ ]+) (ACCEPT|DENY)$";
47
static const char *process_match_regex = "^([^ ]+) process\\=([^ ]+) (ACCEPT|DENY)$";
48
static const char *schema_match_regex = "^([^ ]+) schema\\=([^ ]+) (ACCEPT|DENY)$";
49
/* These correspond to the parenthesis above and must stay in sync */
50
static const int MATCH_REGEX_USER_POS= 1;
51
static const int MATCH_REGEX_OBJECT_POS= 2;
52
static const int MATCH_REGEX_ACTION_POS= 3;
62
const std::string user;
63
const std::string object;
64
const boost::regex user_re;
65
const boost::regex object_re;
68
PolicyItem(const std::string &u, const std::string &obj, const std::string &act) :
76
action = POLICY_ACCEPT;
78
else if (act == "DENY")
84
throw std::exception();
87
bool userMatches(std::string &str);
88
bool objectMatches(std::string &object_id);
90
const std::string&getUser() const
94
const std::string&getObject() const
98
const char *getAction() const
100
return action == POLICY_ACCEPT ? "ACCEPT" : "DENY";
104
typedef std::list<PolicyItem *> PolicyItemList;
105
typedef boost::unordered_map<std::string, bool> CheckMap;
107
static boost::mutex check_cache_mutex;
114
bool has_cached_result;
116
CheckMap **check_cache;
118
CheckItem(const std::string &u, const std::string &obj, CheckMap **check_cache);
119
bool operator()(PolicyItem *p);
120
bool hasCachedResult() const
122
return has_cached_result;
124
bool getCachedResult() const
126
return cached_result;
128
void setCachedResult(bool result);
131
inline bool PolicyItem::userMatches(std::string &str)
133
return boost::regex_match(str, user_re);
136
inline bool PolicyItem::objectMatches(std::string &object_id)
138
return boost::regex_match(object_id, object_re);
141
inline bool PolicyItem::isRestricted()
143
return action == POLICY_DENY ? true : false;
146
void clearPolicyItemList(PolicyItemList policies);
149
public drizzled::plugin::Authorization
152
Policy(const fs::path &f_path) :
153
drizzled::plugin::Authorization("Regex Policy"), policy_file(f_path), error(),
154
table_check_cache(NULL), schema_check_cache(NULL), process_check_cache(NULL)
157
virtual bool restrictSchema(const drizzled::identifier::User &user_ctx,
158
const drizzled::identifier::Schema& schema);
160
virtual bool restrictProcess(const drizzled::identifier::User &user_ctx,
161
const drizzled::identifier::User &session_ctx);
163
virtual bool restrictTable(const drizzled::identifier::User& user_ctx,
164
const drizzled::identifier::Table& table);
167
std::stringstream &getError() { return error; }
170
bool restrictObject(const drizzled::identifier::User &user_ctx,
171
const std::string &obj, const PolicyItemList &policies,
172
CheckMap **check_cache);
173
fs::path policy_file;
174
std::stringstream error;
175
PolicyItemList table_policies;
176
PolicyItemList schema_policies;
177
PolicyItemList process_policies;
178
CheckMap *table_check_cache;
179
CheckMap *schema_check_cache;
180
CheckMap *process_check_cache;
183
} /* namespace regex_policy */