4 Copyright (C) Andrew Bartlett 2005
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; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * Component: ldb kludge ACL module
26 * Description: Simple module to enforce a simple form of access
27 * control, sufficient for securing a default Samba4
30 * Author: Andrew Bartlett
34 #include "ldb/include/ldb.h"
35 #include "ldb/include/ldb_errors.h"
36 #include "ldb/include/ldb_private.h"
37 #include "auth/auth.h"
41 * - System can read passwords
42 * - Administrators can write anything
43 * - Users can read anything that is not a password
47 const char *password_attribs[] = {
57 struct kludge_private_data {
58 const char **password_attrs;
61 static enum user_is what_is_user(struct ldb_module *module)
63 struct auth_session_info *session_info
64 = ldb_get_opaque(module->ldb, "sessionInfo");
69 if (is_system_token(session_info->security_token)) {
73 if (is_administrator_token(session_info->security_token)) {
76 if (is_authenticated_token(session_info->security_token)) {
79 if (is_anonymous_token(session_info->security_token)) {
86 static int kludge_acl_search(struct ldb_module *module, struct ldb_request *req)
88 enum user_is user_type;
89 int ret = ldb_next_request(module, req);
90 struct ldb_message *msg;
91 struct kludge_private_data *data = talloc_get_type(module->private_data, struct kludge_private_data);
94 if (ret != LDB_SUCCESS) {
98 user_type = what_is_user(module);
104 /* For every message, remove password attributes */
105 for (i=0; i < req->op.search.res->count; i++) {
106 msg = req->op.search.res->msgs[i];
107 for (j=0; data->password_attrs[j]; j++) {
108 ldb_msg_remove_attr(msg, data->password_attrs[j]);
115 /* ANY change type */
116 static int kludge_acl_change(struct ldb_module *module, struct ldb_request *req){
117 enum user_is user_type = what_is_user(module);
121 return ldb_next_request(module, req);
123 ldb_set_errstring(module,
124 talloc_asprintf(req, "kludge_acl_change: "
125 "attempted database modify not permitted. User is not SYSTEM or an administrator"));
126 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
130 /* start a transaction */
131 static int kludge_acl_start_trans(struct ldb_module *module)
133 return ldb_next_start_trans(module);
136 /* end a transaction */
137 static int kludge_acl_end_trans(struct ldb_module *module)
139 return ldb_next_end_trans(module);
142 /* delete a transaction */
143 static int kludge_acl_del_trans(struct ldb_module *module)
145 return ldb_next_del_trans(module);
148 static int kludge_acl_request(struct ldb_module *module, struct ldb_request *req)
150 switch (req->operation) {
153 return kludge_acl_search(module, req);
154 case LDB_REQ_REGISTER:
155 return ldb_next_request(module, req);
157 /* anything else must be a change of some kind */
158 return kludge_acl_change(module, req);
162 static int kludge_acl_init_2(struct ldb_module *module)
165 TALLOC_CTX *mem_ctx = talloc_new(module);
166 const char *attrs[] = { "attribute", NULL };
167 struct ldb_result *res;
168 struct ldb_message *msg;
169 struct ldb_message_element *password_attributes;
171 struct kludge_private_data *data = talloc_get_type(module->private_data, struct kludge_private_data);
172 data->password_attrs = NULL;
175 return LDB_ERR_OPERATIONS_ERROR;
178 ret = ldb_search(module->ldb, ldb_dn_explode(mem_ctx, "@KLUDGEACL"),
182 if (ret != LDB_SUCCESS) {
183 talloc_free(mem_ctx);
186 if (res->count == 0) {
187 talloc_free(mem_ctx);
188 data->password_attrs = NULL;
192 if (res->count > 1) {
193 return LDB_ERR_CONSTRAINT_VIOLAION;
198 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
199 if (!password_attributes) {
202 data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
203 if (!data->password_attrs) {
204 return LDB_ERR_OPERATIONS_ERROR;
206 for (i=0; i < password_attributes->num_values; i++) {
207 data->password_attrs[i] = (const char *)password_attributes->values[i].data;
208 talloc_steal(data->password_attrs, password_attributes->values[i].data);
210 data->password_attrs[i] = NULL;
214 static const struct ldb_module_ops kludge_acl_ops = {
215 .name = "kludge_acl",
216 .request = kludge_acl_request,
217 .start_transaction = kludge_acl_start_trans,
218 .end_transaction = kludge_acl_end_trans,
219 .del_transaction = kludge_acl_del_trans,
220 .second_stage_init = kludge_acl_init_2
223 struct ldb_module *kludge_acl_module_init(struct ldb_context *ldb, const char *options[])
225 struct ldb_module *ctx;
226 struct kludge_private_data *data;
228 ctx = talloc(ldb, struct ldb_module);
232 data = talloc(ctx, struct kludge_private_data);
238 data->password_attrs = NULL;
239 ctx->private_data = data;
242 ctx->prev = ctx->next = NULL;
243 ctx->ops = &kludge_acl_ops;