Merge ldb_search() and ldb_search_exp_fmt() into a simgle function.
[Samba.git] / source4 / dsdb / samdb / ldb_modules / kludge_acl.c
blob6836f95873ef81b61a0e8f7f41d2b0913157865b
1 /*
2 ldb database library
4 Copyright (C) Andrew Bartlett 2005
5 Copyright (C) Simo Sorce 2006
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * Name: ldb
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
28 * installation.
30 * Author: Andrew Bartlett
33 #include "includes.h"
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"
38 #include "libcli/security/security.h"
39 #include "dsdb/samdb/samdb.h"
41 /* Kludge ACL rules:
43 * - System can read passwords
44 * - Administrators can write anything
45 * - Users can read anything that is not a password
49 struct kludge_private_data {
50 const char **password_attrs;
53 static enum security_user_level what_is_user(struct ldb_module *module)
55 struct auth_session_info *session_info
56 = (struct auth_session_info *)ldb_get_opaque(module->ldb, "sessionInfo");
57 return security_session_user_level(session_info);
60 static const char *user_name(TALLOC_CTX *mem_ctx, struct ldb_module *module)
62 struct auth_session_info *session_info
63 = (struct auth_session_info *)ldb_get_opaque(module->ldb, "sessionInfo");
64 if (!session_info) {
65 return "UNKNOWN (NULL)";
68 return talloc_asprintf(mem_ctx, "%s\\%s",
69 session_info->server_info->domain_name,
70 session_info->server_info->account_name);
73 /* search */
74 struct kludge_acl_context {
76 struct ldb_module *module;
77 void *up_context;
78 int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
80 enum security_user_level user_type;
81 bool allowedAttributes;
82 bool allowedAttributesEffective;
83 bool allowedChildClasses;
84 bool allowedChildClassesEffective;
85 const char **attrs;
88 /* read all objectClasses */
90 static int kludge_acl_allowedAttributes(struct ldb_context *ldb, struct ldb_message *msg,
91 const char *attrName)
93 struct ldb_message_element *oc_el;
94 struct ldb_message_element *allowedAttributes;
95 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
96 TALLOC_CTX *mem_ctx;
97 char **objectclass_list, **attr_list;
98 int i, ret;
100 /* If we don't have a schema yet, we can't do anything... */
101 if (schema == NULL) {
102 return LDB_SUCCESS;
105 /* Must remove any existing attribute, or else confusion reins */
106 ldb_msg_remove_attr(msg, attrName);
107 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedAttributes);
108 if (ret != LDB_SUCCESS) {
109 return ret;
112 mem_ctx = talloc_new(msg);
113 if (!mem_ctx) {
114 ldb_oom(ldb);
115 return LDB_ERR_OPERATIONS_ERROR;
118 /* To ensure that oc_el is valid, we must look for it after
119 we alter the element array in ldb_msg_add_empty() */
120 oc_el = ldb_msg_find_element(msg, "objectClass");
122 objectclass_list = talloc_array(mem_ctx, char *, oc_el->num_values + 1);
123 if (!objectclass_list) {
124 ldb_oom(ldb);
125 talloc_free(mem_ctx);
126 return LDB_ERR_OPERATIONS_ERROR;
129 for (i=0; oc_el && i < oc_el->num_values; i++) {
130 objectclass_list[i] = (char *)oc_el->values[i].data;
132 objectclass_list[i] = NULL;
134 attr_list = dsdb_full_attribute_list(mem_ctx, schema, (const char **)objectclass_list, DSDB_SCHEMA_ALL);
135 if (!attr_list) {
136 ldb_asprintf_errstring(ldb, "kludge_acl: Failed to get list of attributes create %s attribute", attrName);
137 talloc_free(mem_ctx);
138 return LDB_ERR_OPERATIONS_ERROR;
141 for (i=0; attr_list && attr_list[i]; i++) {
142 ldb_msg_add_string(msg, attrName, attr_list[i]);
144 talloc_free(mem_ctx);
145 return 0;
148 /* read all objectClasses */
150 static int kludge_acl_childClasses(struct ldb_context *ldb, struct ldb_message *msg,
151 const char *attrName)
153 struct ldb_message_element *oc_el;
154 struct ldb_message_element *allowedClasses;
155 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
156 const struct dsdb_class *class;
157 int i, j, ret;
159 /* If we don't have a schema yet, we can't do anything... */
160 if (schema == NULL) {
161 return LDB_SUCCESS;
164 /* Must remove any existing attribute, or else confusion reins */
165 ldb_msg_remove_attr(msg, attrName);
166 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
167 if (ret != LDB_SUCCESS) {
168 return ret;
171 /* To ensure that oc_el is valid, we must look for it after
172 we alter the element array in ldb_msg_add_empty() */
173 oc_el = ldb_msg_find_element(msg, "objectClass");
175 for (i=0; oc_el && i < oc_el->num_values; i++) {
176 class = dsdb_class_by_lDAPDisplayName(schema, (const char *)oc_el->values[i].data);
177 if (!class) {
178 /* We don't know this class? what is going on? */
179 continue;
182 for (j=0; class->possibleInferiors && class->possibleInferiors[j]; j++) {
183 ldb_msg_add_string(msg, attrName, class->possibleInferiors[j]);
187 if (allowedClasses->num_values > 1) {
188 qsort(allowedClasses->values,
189 allowedClasses->num_values,
190 sizeof(*allowedClasses->values),
191 (comparison_fn_t)data_blob_cmp);
193 for (i=1 ; i < allowedClasses->num_values; i++) {
194 struct ldb_val *val1 = &allowedClasses->values[i-1];
195 struct ldb_val *val2 = &allowedClasses->values[i];
196 if (data_blob_cmp(val1, val2) == 0) {
197 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
198 allowedClasses->num_values--;
199 i--;
204 return 0;
208 /* find all attributes allowed by all these objectClasses */
210 static int kludge_acl_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
212 struct kludge_acl_context *ac;
213 struct kludge_private_data *data;
214 int i, ret;
216 ac = talloc_get_type(context, struct kludge_acl_context);
217 data = talloc_get_type(ac->module->private_data, struct kludge_private_data);
219 if (ares->type != LDB_REPLY_ENTRY) {
220 return ac->up_callback(ldb, ac->up_context, ares);
223 if (ac->allowedAttributes) {
224 ret = kludge_acl_allowedAttributes(ldb, ares->message, "allowedAttributes");
225 if (ret != LDB_SUCCESS) {
226 return ret;
230 if (ac->allowedChildClasses) {
231 ret = kludge_acl_childClasses(ldb, ares->message, "allowedChildClasses");
232 if (ret != LDB_SUCCESS) {
233 return ret;
237 if (data && data->password_attrs) /* if we are not initialized just get through */
239 switch (ac->user_type) {
240 case SECURITY_SYSTEM:
241 if (ac->allowedAttributesEffective) {
242 ret = kludge_acl_allowedAttributes(ldb, ares->message, "allowedAttributesEffective");
243 if (ret != LDB_SUCCESS) {
244 return ret;
247 if (ac->allowedChildClassesEffective) {
248 ret = kludge_acl_childClasses(ldb, ares->message, "allowedChildClassesEffective");
249 if (ret != LDB_SUCCESS) {
250 return ret;
253 break;
254 case SECURITY_ADMINISTRATOR:
255 if (ac->allowedAttributesEffective) {
256 ret = kludge_acl_allowedAttributes(ldb, ares->message, "allowedAttributesEffective");
257 if (ret != LDB_SUCCESS) {
258 return ret;
261 if (ac->allowedChildClassesEffective) {
262 ret = kludge_acl_childClasses(ldb, ares->message, "allowedChildClassesEffective");
263 if (ret != LDB_SUCCESS) {
264 return ret;
267 /* fall though */
268 default:
269 /* remove password attributes */
270 for (i = 0; data->password_attrs[i]; i++) {
271 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
276 if ((ac->allowedAttributes || ac->allowedAttributesEffective
277 || ac->allowedChildClasses || ac->allowedChildClassesEffective) &&
278 (!ldb_attr_in_list(ac->attrs, "objectClass") &&
279 !ldb_attr_in_list(ac->attrs, "*"))) {
280 ldb_msg_remove_attr(ares->message, "objectClass");
283 return ac->up_callback(ldb, ac->up_context, ares);
286 static int kludge_acl_search(struct ldb_module *module, struct ldb_request *req)
288 struct kludge_acl_context *ac;
289 struct ldb_request *down_req;
290 struct kludge_private_data *data;
291 int ret, i;
293 req->handle = NULL;
295 ac = talloc(req, struct kludge_acl_context);
296 if (ac == NULL) {
297 ldb_oom(module->ldb);
298 return LDB_ERR_OPERATIONS_ERROR;
301 data = talloc_get_type(module->private_data, struct kludge_private_data);
303 ac->module = module;
304 ac->up_context = req->context;
305 ac->up_callback = req->callback;
306 ac->user_type = what_is_user(module);
307 ac->attrs = req->op.search.attrs;
309 down_req = talloc_zero(req, struct ldb_request);
310 if (down_req == NULL) {
311 ldb_oom(module->ldb);
312 return LDB_ERR_OPERATIONS_ERROR;
315 down_req->operation = req->operation;
316 down_req->op.search.base = req->op.search.base;
317 down_req->op.search.scope = req->op.search.scope;
318 down_req->op.search.tree = req->op.search.tree;
319 down_req->op.search.attrs = req->op.search.attrs;
321 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
323 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
325 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
327 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
329 if (ac->allowedAttributes || ac->allowedAttributesEffective || ac->allowedChildClasses || ac->allowedChildClassesEffective) {
330 down_req->op.search.attrs
331 = ldb_attr_list_copy_add(down_req, down_req->op.search.attrs, "objectClass");
334 /* FIXME: I hink we should copy the tree and keep the original
335 * unmodified. SSS */
336 /* replace any attributes in the parse tree that are private,
337 so we don't allow a search for 'userPassword=penguin',
338 just as we would not allow that attribute to be returned */
339 switch (ac->user_type) {
340 case SECURITY_SYSTEM:
341 break;
342 default:
343 /* remove password attributes */
344 for (i = 0; data && data->password_attrs && data->password_attrs[i]; i++) {
345 ldb_parse_tree_attr_replace(down_req->op.search.tree,
346 data->password_attrs[i],
347 "kludgeACLredactedattribute");
351 down_req->controls = req->controls;
353 down_req->context = ac;
354 down_req->callback = kludge_acl_callback;
355 ldb_set_timeout_from_prev_req(module->ldb, req, down_req);
357 /* perform the search */
358 ret = ldb_next_request(module, down_req);
360 /* do not free down_req as the call results may be linked to it,
361 * it will be freed when the upper level request get freed */
362 if (ret == LDB_SUCCESS) {
363 req->handle = down_req->handle;
366 return ret;
369 /* ANY change type */
370 static int kludge_acl_change(struct ldb_module *module, struct ldb_request *req)
372 enum security_user_level user_type = what_is_user(module);
373 switch (user_type) {
374 case SECURITY_SYSTEM:
375 case SECURITY_ADMINISTRATOR:
376 return ldb_next_request(module, req);
377 default:
378 ldb_asprintf_errstring(module->ldb,
379 "kludge_acl_change: "
380 "attempted database modify not permitted. "
381 "User %s is not SYSTEM or an administrator",
382 user_name(req, module));
383 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
387 static int kludge_acl_init(struct ldb_module *module)
389 int ret, i;
390 TALLOC_CTX *mem_ctx = talloc_new(module);
391 static const char *attrs[] = { "passwordAttribute", NULL };
392 struct ldb_result *res;
393 struct ldb_message *msg;
394 struct ldb_message_element *password_attributes;
396 struct kludge_private_data *data;
398 data = talloc(module, struct kludge_private_data);
399 if (data == NULL) {
400 ldb_oom(module->ldb);
401 return LDB_ERR_OPERATIONS_ERROR;
404 data->password_attrs = NULL;
405 module->private_data = data;
407 if (!mem_ctx) {
408 ldb_oom(module->ldb);
409 return LDB_ERR_OPERATIONS_ERROR;
412 ret = ldb_search(module->ldb, mem_ctx, &res,
413 ldb_dn_new(mem_ctx, module->ldb, "@KLUDGEACL"),
414 LDB_SCOPE_BASE, attrs, NULL);
415 if (ret != LDB_SUCCESS) {
416 goto done;
418 if (res->count == 0) {
419 goto done;
422 if (res->count > 1) {
423 talloc_free(mem_ctx);
424 return LDB_ERR_CONSTRAINT_VIOLATION;
427 msg = res->msgs[0];
429 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
430 if (!password_attributes) {
431 goto done;
433 data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
434 if (!data->password_attrs) {
435 talloc_free(mem_ctx);
436 ldb_oom(module->ldb);
437 return LDB_ERR_OPERATIONS_ERROR;
439 for (i=0; i < password_attributes->num_values; i++) {
440 data->password_attrs[i] = (const char *)password_attributes->values[i].data;
441 talloc_steal(data->password_attrs, password_attributes->values[i].data);
443 data->password_attrs[i] = NULL;
445 done:
446 talloc_free(mem_ctx);
447 return ldb_next_init(module);
450 _PUBLIC_ const struct ldb_module_ops ldb_kludge_acl_module_ops = {
451 .name = "kludge_acl",
452 .search = kludge_acl_search,
453 .add = kludge_acl_change,
454 .modify = kludge_acl_change,
455 .del = kludge_acl_change,
456 .rename = kludge_acl_change,
457 .extended = kludge_acl_change,
458 .init_context = kludge_acl_init