s4:acl LDB module - fix counter type
[metze/samba/wip.git] / source4 / dsdb / samdb / ldb_modules / acl.c
1 /*
2   ldb database library
3
4   Copyright (C) Simo Sorce 2006-2008
5   Copyright (C) Nadezhda Ivanova 2009
6   Copyright (C) Anatoliy Atanasov  2009
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 3 of the License, or
11   (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 /*
23  *  Name: ldb
24  *
25  *  Component: ldb ACL module
26  *
27  *  Description: Module that performs authorisation access checks based on the
28  *               account's security context and the DACL of the object being polled.
29  *               Only DACL checks implemented at this point
30  *
31  *  Authors: Nadezhda Ivanova, Anatoliy Atanasov
32  */
33
34 #include "includes.h"
35 #include "ldb_module.h"
36 #include "auth/auth.h"
37 #include "libcli/security/security.h"
38 #include "dsdb/samdb/samdb.h"
39 #include "librpc/gen_ndr/ndr_security.h"
40 #include "param/param.h"
41 #include "dsdb/samdb/ldb_modules/util.h"
42 #include "lib/util/tsort.h"
43
44 struct extended_access_check_attribute {
45         const char *oa_name;
46         const uint32_t requires_rights;
47 };
48
49 struct acl_private {
50         bool acl_perform;
51         const char **password_attrs;
52 };
53
54 struct acl_context {
55         struct ldb_module *module;
56         struct ldb_request *req;
57         bool am_system;
58         bool allowedAttributes;
59         bool allowedAttributesEffective;
60         bool allowedChildClasses;
61         bool allowedChildClassesEffective;
62         bool sDRightsEffective;
63         const char * const *attrs;
64         struct dsdb_schema *schema;
65 };
66
67 bool is_root_base_dn(struct ldb_context *ldb, struct ldb_dn *dn_to_check)
68 {
69         int result;
70         struct ldb_dn *root_base_dn = ldb_get_root_basedn(ldb);
71         result = ldb_dn_compare(root_base_dn,dn_to_check);
72         return (result==0);
73 }
74
75 static struct security_token *acl_user_token(struct ldb_module *module)
76 {
77         struct ldb_context *ldb = ldb_module_get_ctx(module);
78         struct auth_session_info *session_info
79                 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
80         if(!session_info) {
81                 return NULL;
82         }
83         return session_info->security_token;
84 }
85
86 /* performs an access check from inside the module stack
87  * given the dn of the object to be checked, the required access
88  * guid is either the guid of the extended right, or NULL
89  */
90
91 int dsdb_module_check_access_on_dn(struct ldb_module *module,
92                                    TALLOC_CTX *mem_ctx,
93                                    struct ldb_dn *dn,
94                                    uint32_t access,
95                                    const struct GUID *guid)
96 {
97         int ret;
98         struct ldb_result *acl_res;
99         static const char *acl_attrs[] = {
100                 "nTSecurityDescriptor",
101                 "objectSid",
102                 NULL
103         };
104         struct ldb_context *ldb = ldb_module_get_ctx(module);
105         struct auth_session_info *session_info
106                 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
107         if(!session_info) {
108                 return LDB_ERR_OPERATIONS_ERROR;
109         }
110         ret = dsdb_module_search_dn(module, mem_ctx, &acl_res, dn,
111                                     acl_attrs, DSDB_SEARCH_SHOW_DELETED);
112         if (ret != LDB_SUCCESS) {
113                 DEBUG(10,("access_check: failed to find object %s\n", ldb_dn_get_linearized(dn)));
114                 return ret;
115         }
116         return dsdb_check_access_on_dn_internal(acl_res,
117                                                 mem_ctx,
118                                                 session_info->security_token,
119                                                 dn,
120                                                 access,
121                                                 guid);
122 }
123
124
125 static int acl_module_init(struct ldb_module *module)
126 {
127         struct ldb_context *ldb;
128         struct acl_private *data;
129         int ret, i;
130         TALLOC_CTX *mem_ctx = talloc_new(module);
131         static const char *attrs[] = { "passwordAttribute", NULL };
132         struct ldb_result *res;
133         struct ldb_message *msg;
134         struct ldb_message_element *password_attributes;
135
136         ldb = ldb_module_get_ctx(module);
137
138         ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
139         if (ret != LDB_SUCCESS) {
140                 ldb_debug(ldb, LDB_DEBUG_ERROR,
141                           "acl_module_init: Unable to register control with rootdse!\n");
142                 return LDB_ERR_OPERATIONS_ERROR;
143         }
144
145         data = talloc(module, struct acl_private);
146         if (data == NULL) {
147                 ldb_oom(ldb);
148                 return LDB_ERR_OPERATIONS_ERROR;
149         }
150
151         data->password_attrs = NULL;
152         data->acl_perform = lp_parm_bool(ldb_get_opaque(ldb, "loadparm"),
153                                          NULL, "acl", "perform", false);
154         ldb_module_set_private(module, data);
155
156         if (!mem_ctx) {
157                 ldb_oom(ldb);
158                 return LDB_ERR_OPERATIONS_ERROR;
159         }
160
161         ret = dsdb_module_search_dn(module, mem_ctx, &res,
162                                     ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
163                                     attrs, 0);
164         if (ret != LDB_SUCCESS) {
165                 goto done;
166         }
167         if (res->count == 0) {
168                 goto done;
169         }
170
171         if (res->count > 1) {
172                 talloc_free(mem_ctx);
173                 return LDB_ERR_CONSTRAINT_VIOLATION;
174         }
175
176         msg = res->msgs[0];
177
178         password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
179         if (!password_attributes) {
180                 goto done;
181         }
182         data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
183         if (!data->password_attrs) {
184                 talloc_free(mem_ctx);
185                 ldb_oom(ldb);
186                 return LDB_ERR_OPERATIONS_ERROR;
187         }
188         for (i=0; i < password_attributes->num_values; i++) {
189                 data->password_attrs[i] = (const char *)password_attributes->values[i].data;
190                 talloc_steal(data->password_attrs, password_attributes->values[i].data);
191         }
192         data->password_attrs[i] = NULL;
193
194 done:
195         talloc_free(mem_ctx);
196         return ldb_next_init(module);
197 }
198
199 static const struct GUID *get_oc_guid_from_message(struct ldb_module *module,
200                                                    const struct dsdb_schema *schema,
201                                                    struct ldb_message *msg)
202 {
203         struct ldb_message_element *oc_el;
204
205         oc_el = ldb_msg_find_element(msg, "objectClass");
206         if (!oc_el) {
207                 return NULL;
208         }
209
210         return class_schemaid_guid_by_lDAPDisplayName(schema,
211                                                       (char *)oc_el->values[oc_el->num_values-1].data);
212 }
213
214 static int acl_check_access_on_attribute(struct ldb_module *module,
215                                          TALLOC_CTX *mem_ctx,
216                                          struct security_descriptor *sd,
217                                          struct dom_sid *rp_sid,
218                                          uint32_t access,
219                                          const struct dsdb_attribute *attr)
220 {
221         int ret;
222         NTSTATUS status;
223         uint32_t access_granted;
224         struct object_tree *root = NULL;
225         struct object_tree *new_node = NULL;
226         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
227         struct security_token *token = acl_user_token(module);
228         if (attr) {
229                 if (!GUID_all_zero(&attr->attributeSecurityGUID)) {
230                         if (!insert_in_object_tree(tmp_ctx,
231                                                    &attr->attributeSecurityGUID, access,
232                                                    &root, &new_node)) {
233                                 DEBUG(10, ("acl_search: cannot add to object tree securityGUID\n"));
234                                 goto fail;
235                         }
236
237                         if (!insert_in_object_tree(tmp_ctx,
238                                                    &attr->schemaIDGUID, access, &new_node, &new_node)) {
239                                 DEBUG(10, ("acl_search: cannot add to object tree attributeGUID\n"));
240                                 goto fail;
241                         }
242                 }
243                 else {
244                         if (!insert_in_object_tree(tmp_ctx,
245                                                    &attr->schemaIDGUID, access, &root, &new_node)) {
246                                 DEBUG(10, ("acl_search: cannot add to object tree attributeGUID\n"));
247                                 goto fail;
248                         }
249                 }
250         }
251         status = sec_access_check_ds(sd, token,
252                                      access,
253                                      &access_granted,
254                                      root,
255                                      rp_sid);
256         if (!NT_STATUS_IS_OK(status)) {
257                 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
258         }
259         else {
260                 ret = LDB_SUCCESS;
261         }
262         return ret;
263 fail:
264         return LDB_ERR_OPERATIONS_ERROR;
265 }
266
267 static int acl_check_access_on_class(struct ldb_module *module,
268                                      const struct dsdb_schema *schema,
269                                      TALLOC_CTX *mem_ctx,
270                                      struct security_descriptor *sd,
271                                      struct dom_sid *rp_sid,
272                                      uint32_t access,
273                                      const char *class_name)
274 {
275         int ret;
276         NTSTATUS status;
277         uint32_t access_granted;
278         struct object_tree *root = NULL;
279         struct object_tree *new_node = NULL;
280         const struct GUID *guid;
281         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
282         struct security_token *token = acl_user_token(module);
283         if (class_name) {
284                 guid = class_schemaid_guid_by_lDAPDisplayName(schema, class_name);
285                 if (!guid) {
286                         DEBUG(10, ("acl_search: cannot find class %s\n",
287                                    class_name));
288                         goto fail;
289                 }
290                 if (!insert_in_object_tree(tmp_ctx,
291                                            guid, access,
292                                            &root, &new_node)) {
293                         DEBUG(10, ("acl_search: cannot add to object tree guid\n"));
294                         goto fail;
295                 }
296         }
297         status = sec_access_check_ds(sd, token,
298                                      access,
299                                      &access_granted,
300                                      root,
301                                      rp_sid);
302         if (!NT_STATUS_IS_OK(status)) {
303                 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
304         }
305         else {
306                 ret = LDB_SUCCESS;
307         }
308         return ret;
309 fail:
310         return LDB_ERR_OPERATIONS_ERROR;
311 }
312
313 static int acl_allowedAttributes(struct ldb_module *module,
314                                  const struct dsdb_schema *schema,
315                                  struct ldb_message *sd_msg,
316                                  struct ldb_message *msg,
317                                  struct acl_context *ac)
318 {
319         struct ldb_message_element *oc_el;
320         struct ldb_context *ldb = ldb_module_get_ctx(module);
321         TALLOC_CTX *mem_ctx;
322         const char **attr_list;
323         int i, ret;
324
325         /* If we don't have a schema yet, we can't do anything... */
326         if (schema == NULL) {
327                 ldb_asprintf_errstring(ldb, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
328                 return LDB_ERR_OPERATIONS_ERROR;
329         }
330
331         /* Must remove any existing attribute */
332         if (ac->allowedAttributes) {
333                 ldb_msg_remove_attr(msg, "allowedAttributes");
334         }
335
336         mem_ctx = talloc_new(msg);
337         if (!mem_ctx) {
338                 ldb_oom(ldb);
339                 return LDB_ERR_OPERATIONS_ERROR;
340         }
341
342         oc_el = ldb_msg_find_element(sd_msg, "objectClass");
343         attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
344         if (!attr_list) {
345                 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
346                 talloc_free(mem_ctx);
347                 return LDB_ERR_OPERATIONS_ERROR;
348         }
349         if (ac->allowedAttributes) {
350                 for (i=0; attr_list && attr_list[i]; i++) {
351                         ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
352                 }
353         }
354         if (ac->allowedAttributesEffective) {
355                 struct security_descriptor *sd;
356                 struct dom_sid *sid = NULL;
357                 struct ldb_control *as_system = ldb_request_get_control(ac->req,
358                                                                         LDB_CONTROL_AS_SYSTEM_OID);
359
360                 if (as_system != NULL) {
361                         as_system->critical = 0;
362                 }
363
364                 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
365                 if (ac->am_system || as_system) {
366                         for (i=0; attr_list && attr_list[i]; i++) {
367                                 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
368                         }
369                         return LDB_SUCCESS;
370                 }
371
372                 ret = dsdb_get_sd_from_ldb_message(mem_ctx, sd_msg, &sd);
373
374                 if (ret != LDB_SUCCESS) {
375                         return ret;
376                 }
377
378                 sid = samdb_result_dom_sid(mem_ctx, sd_msg, "objectSid");
379                 for (i=0; attr_list && attr_list[i]; i++) {
380                         const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
381                                                                                         attr_list[i]);
382                         if (!attr) {
383                                 return LDB_ERR_OPERATIONS_ERROR;
384                         }
385                         /* remove constructed attributes */
386                         if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
387                             || attr->systemOnly
388                             || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
389                                 continue;
390                         }
391                         ret = acl_check_access_on_attribute(module,
392                                                             msg,
393                                                             sd,
394                                                             sid,
395                                                             SEC_ADS_WRITE_PROP,
396                                                             attr);
397                         if (ret == LDB_SUCCESS) {
398                                 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
399                         }
400                 }
401         }
402         return LDB_SUCCESS;
403 }
404
405 static int acl_childClasses(struct ldb_module *module,
406                             const struct dsdb_schema *schema,
407                             struct ldb_message *sd_msg,
408                             struct ldb_message *msg,
409                             const char *attrName)
410 {
411         struct ldb_message_element *oc_el;
412         struct ldb_message_element *allowedClasses;
413         const struct dsdb_class *sclass;
414         unsigned int i, j;
415         int ret;
416
417         /* If we don't have a schema yet, we can't do anything... */
418         if (schema == NULL) {
419                 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
420                 return LDB_ERR_OPERATIONS_ERROR;
421         }
422
423         /* Must remove any existing attribute, or else confusion reins */
424         ldb_msg_remove_attr(msg, attrName);
425         ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
426         if (ret != LDB_SUCCESS) {
427                 return ret;
428         }
429
430         oc_el = ldb_msg_find_element(sd_msg, "objectClass");
431
432         for (i=0; oc_el && i < oc_el->num_values; i++) {
433                 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
434                 if (!sclass) {
435                         /* We don't know this class?  what is going on? */
436                         continue;
437                 }
438
439                 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
440                         ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
441                 }
442         }
443         if (allowedClasses->num_values > 1) {
444                 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
445                 for (i=1 ; i < allowedClasses->num_values; i++) {
446                         struct ldb_val *val1 = &allowedClasses->values[i-1];
447                         struct ldb_val *val2 = &allowedClasses->values[i];
448                         if (data_blob_cmp(val1, val2) == 0) {
449                                 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
450                                 allowedClasses->num_values--;
451                                 i--;
452                         }
453                 }
454         }
455
456         return LDB_SUCCESS;
457 }
458
459 static int acl_childClassesEffective(struct ldb_module *module,
460                                      const struct dsdb_schema *schema,
461                                      struct ldb_message *sd_msg,
462                                      struct ldb_message *msg,
463                                      struct acl_context *ac)
464 {
465         struct ldb_message_element *oc_el;
466         struct ldb_message_element *allowedClasses = NULL;
467         const struct dsdb_class *sclass;
468         struct security_descriptor *sd;
469         struct ldb_control *as_system = ldb_request_get_control(ac->req,
470                                                                 LDB_CONTROL_AS_SYSTEM_OID);
471         struct dom_sid *sid = NULL;
472         unsigned int i, j;
473         int ret;
474
475         if (as_system != NULL) {
476                 as_system->critical = 0;
477         }
478
479         if (ac->am_system || as_system) {
480                 return acl_childClasses(module, schema, sd_msg, msg, "allowedChildClassesEffective");
481         }
482
483         /* If we don't have a schema yet, we can't do anything... */
484         if (schema == NULL) {
485                 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
486                 return LDB_ERR_OPERATIONS_ERROR;
487         }
488
489         /* Must remove any existing attribute, or else confusion reins */
490         ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
491
492         oc_el = ldb_msg_find_element(sd_msg, "objectClass");
493         ret = dsdb_get_sd_from_ldb_message(msg, sd_msg, &sd);
494         if (ret != LDB_SUCCESS) {
495                 return ret;
496         }
497
498         sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
499         for (i=0; oc_el && i < oc_el->num_values; i++) {
500                 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
501                 if (!sclass) {
502                         /* We don't know this class?  what is going on? */
503                         continue;
504                 }
505
506                 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
507                         ret = acl_check_access_on_class(module,
508                                                         schema,
509                                                         msg,
510                                                         sd,
511                                                         sid,
512                                                         SEC_ADS_CREATE_CHILD,
513                                                         sclass->possibleInferiors[j]);
514                         if (ret == LDB_SUCCESS) {
515                                 ldb_msg_add_string(msg, "allowedChildClassesEffective",
516                                                    sclass->possibleInferiors[j]);
517                         }
518                 }
519         }
520         allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
521         if (!allowedClasses) {
522                 return LDB_SUCCESS;
523         }
524
525         if (allowedClasses->num_values > 1) {
526                 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
527                 for (i=1 ; i < allowedClasses->num_values; i++) {
528                         struct ldb_val *val1 = &allowedClasses->values[i-1];
529                         struct ldb_val *val2 = &allowedClasses->values[i];
530                         if (data_blob_cmp(val1, val2) == 0) {
531                                 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
532                                 allowedClasses->num_values--;
533                                 i--;
534                         }
535                 }
536         }
537         return LDB_SUCCESS;
538 }
539
540 static int acl_sDRightsEffective(struct ldb_module *module,
541                                  struct ldb_message *sd_msg,
542                                  struct ldb_message *msg,
543                                  struct acl_context *ac)
544 {
545         struct ldb_message_element *rightsEffective;
546         int ret;
547         struct security_descriptor *sd;
548         struct ldb_control *as_system = ldb_request_get_control(ac->req,
549                                                                 LDB_CONTROL_AS_SYSTEM_OID);
550         struct dom_sid *sid = NULL;
551         uint32_t flags = 0;
552
553         if (as_system != NULL) {
554                 as_system->critical = 0;
555         }
556
557         /* Must remove any existing attribute, or else confusion reins */
558         ldb_msg_remove_attr(msg, "sDRightsEffective");
559         ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
560         if (ret != LDB_SUCCESS) {
561                 return ret;
562         }
563         if (ac->am_system || as_system) {
564                 flags = SECINFO_OWNER | SECINFO_GROUP |  SECINFO_SACL |  SECINFO_DACL;
565         }
566         else {
567                 /* Get the security descriptor from the message */
568                 ret = dsdb_get_sd_from_ldb_message(msg, sd_msg, &sd);
569                 if (ret != LDB_SUCCESS) {
570                         return ret;
571                 }
572                 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
573                 ret = acl_check_access_on_attribute(module,
574                                                     msg,
575                                                     sd,
576                                                     sid,
577                                                     SEC_STD_WRITE_OWNER,
578                                                     NULL);
579                 if (ret == LDB_SUCCESS) {
580                         flags |= SECINFO_OWNER | SECINFO_GROUP;
581                 }
582                 ret = acl_check_access_on_attribute(module,
583                                                     msg,
584                                                     sd,
585                                                     sid,
586                                                     SEC_STD_WRITE_DAC,
587                                                     NULL);
588                 if (ret == LDB_SUCCESS) {
589                         flags |= SECINFO_DACL;
590                 }
591                 ret = acl_check_access_on_attribute(module,
592                                                     msg,
593                                                     sd,
594                                                     sid,
595                                                     SEC_FLAG_SYSTEM_SECURITY,
596                                                     NULL);
597                 if (ret == LDB_SUCCESS) {
598                         flags |= SECINFO_SACL;
599                 }
600         }
601         ldb_msg_add_fmt(msg, "sDRightsEffective", "%u", flags);
602         return LDB_SUCCESS;
603 }
604
605 static int acl_add(struct ldb_module *module, struct ldb_request *req)
606 {
607         int ret;
608         struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.add.message->dn);
609         struct ldb_context *ldb;
610         const struct dsdb_schema *schema;
611         struct ldb_message_element *oc_el;
612         const struct GUID *guid;
613         struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
614
615         if (as_system != NULL) {
616                 as_system->critical = 0;
617         }
618
619         if (dsdb_module_am_system(module) || as_system) {
620                 return ldb_next_request(module, req);
621         }
622
623         if (ldb_dn_is_special(req->op.add.message->dn)) {
624                 return ldb_next_request(module, req);
625         }
626         ldb = ldb_module_get_ctx(module);
627         /* Creating an NC. There is probably something we should do here,
628          * but we will establish that later */
629         /* FIXME: this has to be made dynamic at some point */
630         if ((ldb_dn_compare(req->op.add.message->dn, (ldb_get_schema_basedn(ldb))) == 0) ||
631             (ldb_dn_compare(req->op.add.message->dn, (ldb_get_config_basedn(ldb))) == 0) ||
632             (ldb_dn_compare(req->op.add.message->dn, (ldb_get_default_basedn(ldb))) == 0) ||
633             (ldb_dn_compare(req->op.add.message->dn, (ldb_get_root_basedn(ldb))) == 0)) {
634                 return ldb_next_request(module, req);
635         }
636
637         schema = dsdb_get_schema(ldb, req);
638         if (!schema) {
639                 return LDB_ERR_OPERATIONS_ERROR;
640         }
641
642         oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
643         if (!oc_el || oc_el->num_values == 0) {
644                 DEBUG(10,("acl:operation error %s\n", ldb_dn_get_linearized(req->op.add.message->dn)));
645                 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
646         }
647
648         guid = class_schemaid_guid_by_lDAPDisplayName(schema,
649                                                       (char *)oc_el->values[oc_el->num_values-1].data);
650         ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, guid);
651         if (ret != LDB_SUCCESS) {
652                 return ret;
653         }
654         return ldb_next_request(module, req);
655 }
656
657 /* checks for validated writes */
658 static int acl_check_self_write(struct ldb_request *req,
659                                 struct security_descriptor *sd,
660                                 struct security_token *token,
661                                 const char *self_write,
662                                 struct dom_sid *sid)
663 {
664         struct GUID right;
665         NTSTATUS status;
666         uint32_t access_granted;
667         struct object_tree *root = NULL;
668         struct object_tree *new_node = NULL;
669         TALLOC_CTX *tmp_ctx = talloc_new(req);
670
671         GUID_from_string(self_write, &right);
672
673         if (!insert_in_object_tree(tmp_ctx, &right, SEC_ADS_SELF_WRITE,
674                                    &root, &new_node)) {
675                 DEBUG(10, ("acl_modify: cannot add to object tree\n"));
676                 talloc_free(tmp_ctx);
677                 return LDB_ERR_OPERATIONS_ERROR;
678         }
679         status = sec_access_check_ds(sd, token,
680                                      SEC_ADS_SELF_WRITE,
681                                      &access_granted,
682                                      root,
683                                      sid);
684
685         if (!NT_STATUS_IS_OK(status)) {
686                 DEBUG(10, ("Object %s has no self membershipself write right\n",
687                            ldb_dn_get_linearized(req->op.mod.message->dn)));
688                 dsdb_acl_debug(sd, token,
689                                req->op.mod.message->dn,
690                                true,
691                                10);
692                 talloc_free(tmp_ctx);
693                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
694         }
695
696         return LDB_SUCCESS;
697 }
698
699 /* ckecks if modifications are allowed on "Member" attribute */
700 static int acl_check_self_membership(struct ldb_module *module,
701                                      struct ldb_request *req,
702                                      struct security_descriptor *sd,
703                                      struct dom_sid *sid,
704                                      const struct GUID *oc_guid,
705                                      const struct dsdb_attribute *attr)
706 {
707         int ret;
708         unsigned int i;
709         TALLOC_CTX *tmp_ctx = talloc_new(req);
710         struct ldb_context *ldb = ldb_module_get_ctx(module);
711         struct ldb_dn *user_dn;
712         struct ldb_message_element *member_el;
713         /* if we have wp, we can do whatever we like */
714         if (acl_check_access_on_attribute(module,
715                                           req,
716                                           sd,
717                                           sid,
718                                           SEC_ADS_WRITE_PROP,
719                                           attr) == LDB_SUCCESS) {
720                 return LDB_SUCCESS;
721         }
722         /* if we are adding/deleting ourselves, check for self membership */
723         ret = dsdb_find_dn_by_sid(ldb, req, acl_user_token(module)->user_sid, &user_dn);
724         if (ret != LDB_SUCCESS) {
725                 return ret;
726         }
727         member_el = ldb_msg_find_element(req->op.mod.message, "Member");
728         if (!member_el) {
729                 return LDB_ERR_OPERATIONS_ERROR;
730         }
731         /* user can only remove oneself */
732         if (member_el->num_values == 0) {
733                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
734         }
735         for (i = 0; i < member_el->num_values; i++) {
736                 if (strcasecmp((const char *)member_el->values[i].data,
737                                ldb_dn_get_extended_linearized(tmp_ctx, user_dn, 1)) != 0) {
738                         return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
739                 }
740         }
741         talloc_free(tmp_ctx);
742         return acl_check_self_write(req, sd, acl_user_token(module),
743                                     GUID_DRS_SELF_MEMBERSHIP,
744                                     sid);
745 }
746
747 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
748 {
749         int ret;
750         struct ldb_context *ldb = ldb_module_get_ctx(module);
751         const struct dsdb_schema *schema;
752         unsigned int i;
753         bool modify_sd = false;
754         const struct GUID *guid;
755         uint32_t access_granted;
756         struct object_tree *root = NULL;
757         struct object_tree *new_node = NULL;
758         NTSTATUS status;
759         struct ldb_result *acl_res;
760         struct security_descriptor *sd;
761         struct dom_sid *sid = NULL;
762         struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
763         TALLOC_CTX *tmp_ctx = talloc_new(req);
764         static const char *acl_attrs[] = {
765                 "nTSecurityDescriptor",
766                 "objectClass",
767                 "objectSid",
768                 NULL
769         };
770
771         if (as_system != NULL) {
772                 as_system->critical = 0;
773         }
774
775         /* Don't print this debug statement if elements[0].name is going to be NULL */
776         if(req->op.mod.message->num_elements > 0)
777         {
778                 DEBUG(10, ("ldb:acl_modify: %s\n", req->op.mod.message->elements[0].name));
779         }
780         if (dsdb_module_am_system(module) || as_system) {
781                 return ldb_next_request(module, req);
782         }
783         if (ldb_dn_is_special(req->op.mod.message->dn)) {
784                 return ldb_next_request(module, req);
785         }
786         ret = dsdb_module_search_dn(module, req, &acl_res, req->op.mod.message->dn,
787                                     acl_attrs, 0);
788
789         if (ret != LDB_SUCCESS) {
790                 return ret;
791         }
792
793         schema = dsdb_get_schema(ldb, acl_res);
794         if (!schema) {
795                 talloc_free(acl_res);
796                 return LDB_ERR_OPERATIONS_ERROR;
797         }
798
799         ret = dsdb_get_sd_from_ldb_message(req, acl_res->msgs[0], &sd);
800         if (ret != LDB_SUCCESS) {
801                 DEBUG(10, ("acl_modify: cannot get descriptor\n"));
802                 return ret;
803         }
804         /* Theoretically we pass the check if the object has no sd */
805         if (!sd) {
806                 return LDB_SUCCESS;
807         }
808
809         guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
810         if (!guid) {
811                 DEBUG(10, ("acl_modify: cannot get guid\n"));
812                 goto fail;
813         }
814         sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
815         if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
816                                    &root, &new_node)) {
817                 DEBUG(10, ("acl_modify: cannot add to object tree\n"));
818                 goto fail;
819         }
820         for (i=0; i < req->op.mod.message->num_elements; i++){
821                 const struct dsdb_attribute *attr;
822                 /* clearTextPassword is not in schema */
823                 if (ldb_attr_cmp("clearTextPassword", req->op.mod.message->elements[i].name) == 0) {
824                         attr = dsdb_attribute_by_lDAPDisplayName(schema, "unicodePwd");
825                 } else {
826                         attr = dsdb_attribute_by_lDAPDisplayName(schema,
827                                                                  req->op.mod.message->elements[i].name);
828                 }
829
830                 /* This basic attribute existence check with the right errorcode
831                  * is needed since this module is the first one which requests
832                  * schema attribute informations.
833                  * The complete attribute checking is done in the
834                  * "objectclass_attrs" module behind this one.
835                  */
836                 if (!attr) {
837                         ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' on entry '%s' was not found in the schema!",
838                                                req->op.mod.message->elements[i].name,
839                                                ldb_dn_get_linearized(req->op.mod.message->dn));
840                         talloc_free(tmp_ctx);
841                         return LDB_ERR_NO_SUCH_ATTRIBUTE;
842                 }
843
844                 if (ldb_attr_cmp("nTSecurityDescriptor", req->op.mod.message->elements[i].name) == 0) {
845                         modify_sd = true;
846                 }
847                 else if (ldb_attr_cmp("Member", req->op.mod.message->elements[i].name) == 0) {
848                         ret = acl_check_self_membership(module,
849                                                         req,
850                                                         sd,
851                                                         sid,
852                                                         guid,
853                                                         attr);
854                         if (ret != LDB_SUCCESS) {
855                                 return ret;
856                         }
857                 } else {
858                         if (!insert_in_object_tree(tmp_ctx,
859                                                    &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
860                                                    &new_node, &new_node)) {
861                                 DEBUG(10, ("acl_modify: cannot add to object tree securityGUID\n"));
862                                 goto fail;
863                         }
864
865                         if (!insert_in_object_tree(tmp_ctx,
866                                                    &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
867                                 DEBUG(10, ("acl_modify: cannot add to object tree attributeGUID\n"));
868                                 goto fail;
869                         }
870                 }
871         }
872
873         if (root->num_of_children > 0) {
874                 status = sec_access_check_ds(sd, acl_user_token(module),
875                                              SEC_ADS_WRITE_PROP,
876                                              &access_granted,
877                                              root,
878                                              sid);
879
880                 if (!NT_STATUS_IS_OK(status)) {
881                         DEBUG(10, ("Object %s has no write property access\n",
882                                    ldb_dn_get_linearized(req->op.mod.message->dn)));
883                         dsdb_acl_debug(sd,
884                                   acl_user_token(module),
885                                   req->op.mod.message->dn,
886                                   true,
887                                   10);
888                         talloc_free(tmp_ctx);
889                         return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
890                 }
891         }
892         if (modify_sd) {
893                 status = sec_access_check_ds(sd, acl_user_token(module),
894                                              SEC_STD_WRITE_DAC,
895                                              &access_granted,
896                                              NULL,
897                                              sid);
898
899                 if (!NT_STATUS_IS_OK(status)) {
900                         DEBUG(10, ("Object %s has no write dacl access\n",
901                                    ldb_dn_get_linearized(req->op.mod.message->dn)));
902                         dsdb_acl_debug(sd,
903                                   acl_user_token(module),
904                                   req->op.mod.message->dn,
905                                   true,
906                                   10);
907                         talloc_free(tmp_ctx);
908                         return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
909                 }
910         }
911
912         talloc_free(tmp_ctx);
913         return ldb_next_request(module, req);
914 fail:
915         talloc_free(tmp_ctx);
916         return LDB_ERR_OPERATIONS_ERROR;
917 }
918
919 /* similar to the modify for the time being.
920  * We need to concider the special delete tree case, though - TODO */
921 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
922 {
923         int ret;
924         struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.del.dn);
925         struct ldb_context *ldb;
926         struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
927
928         if (as_system != NULL) {
929                 as_system->critical = 0;
930         }
931
932         DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
933         if (dsdb_module_am_system(module) || as_system) {
934                 return ldb_next_request(module, req);
935         }
936
937         if (ldb_dn_is_special(req->op.del.dn)) {
938                 return ldb_next_request(module, req);
939         }
940         ldb = ldb_module_get_ctx(module);
941         /* first check if we have delete object right */
942         ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn, SEC_STD_DELETE, NULL);
943         if (ret == LDB_SUCCESS) {
944                 return ldb_next_request(module, req);
945         }
946
947         /* Nope, we don't have delete object. Lets check if we have delete child on the parent */
948         /* No parent, so check fails */
949         /* FIXME: this has to be made dynamic at some point */
950         if ((ldb_dn_compare(req->op.del.dn, (ldb_get_schema_basedn(ldb))) == 0) ||
951             (ldb_dn_compare(req->op.del.dn, (ldb_get_config_basedn(ldb))) == 0) ||
952             (ldb_dn_compare(req->op.del.dn, (ldb_get_default_basedn(ldb))) == 0) ||
953             (ldb_dn_compare(req->op.del.dn, (ldb_get_root_basedn(ldb))) == 0)) {
954                 DEBUG(10,("acl:deleting an NC\n"));
955                 return ldb_module_done(req, NULL, NULL, LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS);
956         }
957
958         ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_DELETE_CHILD, NULL);
959         if (ret != LDB_SUCCESS) {
960                 return ret;
961         }
962         return ldb_next_request(module, req);
963 }
964
965 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
966 {
967         int ret;
968         struct ldb_dn *oldparent = ldb_dn_get_parent(req, req->op.rename.olddn);
969         struct ldb_dn *newparent = ldb_dn_get_parent(req, req->op.rename.newdn);
970         const struct dsdb_schema *schema;
971         struct ldb_context *ldb;
972         struct security_descriptor *sd = NULL;
973         struct dom_sid *sid = NULL;
974         struct ldb_result *acl_res;
975         const struct GUID *guid;
976         struct object_tree *root = NULL;
977         struct object_tree *new_node = NULL;
978         struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
979         TALLOC_CTX *tmp_ctx = talloc_new(req);
980         NTSTATUS status;
981         uint32_t access_granted;
982         const char *rdn_name;
983         static const char *acl_attrs[] = {
984                 "nTSecurityDescriptor",
985                 "objectClass",
986                 "objectSid",
987                 NULL
988         };
989
990         if (as_system != NULL) {
991                 as_system->critical = 0;
992         }
993
994         DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
995         if (dsdb_module_am_system(module) || as_system) {
996                 return ldb_next_request(module, req);
997         }
998         if (ldb_dn_is_special(req->op.rename.olddn)) {
999                 return ldb_next_request(module, req);
1000         }
1001         ldb = ldb_module_get_ctx(module);
1002
1003         ret = dsdb_module_search_dn(module, req, &acl_res, req->op.rename.olddn,
1004                                     acl_attrs, DSDB_SEARCH_SHOW_DELETED);
1005         /* we sould be able to find the parent */
1006         if (ret != LDB_SUCCESS) {
1007                 DEBUG(10,("acl: failed to find object %s\n",
1008                           ldb_dn_get_linearized(req->op.rename.olddn)));
1009                 return ret;
1010         }
1011
1012         schema = dsdb_get_schema(ldb, acl_res);
1013         if (!schema) {
1014                 talloc_free(acl_res);
1015                 return LDB_ERR_OPERATIONS_ERROR;
1016         }
1017
1018         guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
1019         if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1020                                    &root, &new_node)) {
1021                 return LDB_ERR_OPERATIONS_ERROR;
1022         };
1023
1024         guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1025                                                           "name");
1026         if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1027                                    &new_node, &new_node)) {
1028                 return LDB_ERR_OPERATIONS_ERROR;
1029         };
1030
1031         rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
1032         if (rdn_name == NULL) {
1033                 return LDB_ERR_OPERATIONS_ERROR;
1034         }
1035         guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1036                                                           rdn_name);
1037         if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1038                                    &new_node, &new_node)) {
1039                 return LDB_ERR_OPERATIONS_ERROR;
1040         };
1041
1042         ret = dsdb_get_sd_from_ldb_message(req, acl_res->msgs[0], &sd);
1043
1044         if (ret != LDB_SUCCESS) {
1045                 return LDB_ERR_OPERATIONS_ERROR;
1046         }
1047         /* Theoretically we pass the check if the object has no sd */
1048         if (!sd) {
1049                 return LDB_SUCCESS;
1050         }
1051         sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1052         status = sec_access_check_ds(sd, acl_user_token(module),
1053                                      SEC_ADS_WRITE_PROP,
1054                                      &access_granted,
1055                                      root,
1056                                      sid);
1057
1058         if (!NT_STATUS_IS_OK(status)) {
1059                 DEBUG(10, ("Object %s has no wp on name\n",
1060                            ldb_dn_get_linearized(req->op.rename.olddn)));
1061                 dsdb_acl_debug(sd,
1062                           acl_user_token(module),
1063                           req->op.rename.olddn,
1064                           true,
1065                           10);
1066                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1067         }
1068
1069         if (ldb_dn_compare(oldparent, newparent) == 0) {
1070                 /* regular rename, not move, nothing more to do */
1071                 return ldb_next_request(module, req);
1072         }
1073
1074         /* What exactly to do in this case? It would fail anyway.. */
1075         /* FIXME: this has to be made dynamic at some point */
1076         if ((ldb_dn_compare(req->op.rename.newdn, (ldb_get_schema_basedn(ldb))) == 0) ||
1077             (ldb_dn_compare(req->op.rename.newdn, (ldb_get_config_basedn(ldb))) == 0) ||
1078             (ldb_dn_compare(req->op.rename.newdn, (ldb_get_default_basedn(ldb))) == 0) ||
1079             (ldb_dn_compare(req->op.rename.newdn, (ldb_get_root_basedn(ldb))) == 0)) {
1080                 DEBUG(10,("acl:moving as an NC\n"));
1081                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1082         }
1083         /* new parent should have create child */
1084         talloc_free(tmp_ctx);
1085         tmp_ctx = talloc_new(req);
1086         root = NULL;
1087         new_node = NULL;
1088         guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
1089         if (!guid) {
1090                 DEBUG(10,("acl:renamed object has no object class\n"));
1091                 return ldb_module_done(req, NULL, NULL,  LDB_ERR_OPERATIONS_ERROR);
1092         }
1093
1094         ret = dsdb_module_check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, guid);
1095         if (ret != LDB_SUCCESS) {
1096                 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1097                 return ret;
1098         }
1099         /* do we have delete object on the object? */
1100
1101         status = sec_access_check_ds(sd, acl_user_token(module),
1102                                      SEC_STD_DELETE,
1103                                      &access_granted,
1104                                      NULL,
1105                                      sid);
1106
1107         if (NT_STATUS_IS_OK(status)) {
1108                 return ldb_next_request(module, req);
1109         }
1110         /* what about delete child on the current parent */
1111         ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL);
1112         if (ret != LDB_SUCCESS) {
1113                 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1114                 return ldb_module_done(req, NULL, NULL, ret);
1115         }
1116         return ldb_next_request(module, req);
1117 }
1118
1119 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1120 {
1121         struct ldb_context *ldb;
1122         struct acl_context *ac;
1123         struct acl_private *data;
1124         struct ldb_result *acl_res;
1125         static const char *acl_attrs[] = {
1126                 "objectClass",
1127                 "nTSecurityDescriptor",
1128                 "objectSid",
1129                 NULL
1130         };
1131         int ret, i;
1132
1133         ac = talloc_get_type(req->context, struct acl_context);
1134         data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1135         ldb = ldb_module_get_ctx(ac->module);
1136
1137         if (!ares) {
1138                 return ldb_module_done(ac->req, NULL, NULL,
1139                                        LDB_ERR_OPERATIONS_ERROR);
1140         }
1141         if (ares->error != LDB_SUCCESS) {
1142                 return ldb_module_done(ac->req, ares->controls,
1143                                        ares->response, ares->error);
1144         }
1145
1146         switch (ares->type) {
1147         case LDB_REPLY_ENTRY:
1148                 if (ac->allowedAttributes 
1149                     || ac->allowedChildClasses
1150                     || ac->allowedChildClassesEffective
1151                     || ac->allowedAttributesEffective
1152                     || ac->sDRightsEffective) {
1153                         ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn, 
1154                                                     acl_attrs, 0);
1155                         if (ret != LDB_SUCCESS) {
1156                                 return ldb_module_done(ac->req, NULL, NULL, ret);
1157                         }
1158                         if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1159                                 ret = acl_allowedAttributes(ac->module, ac->schema, acl_res->msgs[0], ares->message, ac);
1160                                 if (ret != LDB_SUCCESS) {
1161                                         return ldb_module_done(ac->req, NULL, NULL, ret);
1162                                 }
1163                         }
1164                         if (ac->allowedChildClasses) {
1165                                 ret = acl_childClasses(ac->module, ac->schema, acl_res->msgs[0],
1166                                                        ares->message, "allowedChildClasses");
1167                                 if (ret != LDB_SUCCESS) {
1168                                         return ldb_module_done(ac->req, NULL, NULL, ret);
1169                                 }
1170                         }
1171                         if (ac->allowedChildClassesEffective) {
1172                                 ret = acl_childClassesEffective(ac->module, ac->schema,
1173                                                                 acl_res->msgs[0], ares->message, ac);
1174                                 if (ret != LDB_SUCCESS) {
1175                                         return ldb_module_done(ac->req, NULL, NULL, ret);
1176                                 }
1177                         }
1178                         if (ac->sDRightsEffective) {
1179                                 ret = acl_sDRightsEffective(ac->module, 
1180                                                             acl_res->msgs[0], ares->message, ac);
1181                                 if (ret != LDB_SUCCESS) {
1182                                         return ldb_module_done(ac->req, NULL, NULL, ret);
1183                                 }
1184                         }
1185                 }
1186                 if (data && data->password_attrs) {
1187                         if (!ac->am_system) {
1188                                 for (i = 0; data->password_attrs[i]; i++) {
1189                                         ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1190                                 }
1191                         }
1192                 }
1193                 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1194
1195         case LDB_REPLY_REFERRAL:
1196                 return ldb_module_send_referral(ac->req, ares->referral);
1197
1198         case LDB_REPLY_DONE:
1199                 return ldb_module_done(ac->req, ares->controls,
1200                                        ares->response, LDB_SUCCESS);
1201
1202         }
1203         return LDB_SUCCESS;
1204 }
1205
1206 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1207 {
1208         struct ldb_context *ldb;
1209         struct acl_context *ac;
1210         struct ldb_request *down_req;
1211         struct acl_private *data;
1212         int ret, i;
1213
1214         ldb = ldb_module_get_ctx(module);
1215
1216         ac = talloc_zero(req, struct acl_context);
1217         if (ac == NULL) {
1218                 ldb_oom(ldb);
1219                 return LDB_ERR_OPERATIONS_ERROR;
1220         }
1221         data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1222
1223         ac->module = module;
1224         ac->req = req;
1225         ac->am_system = dsdb_module_am_system(module);
1226         ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1227         ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1228         ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1229         ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1230         ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1231         ac->schema = dsdb_get_schema(ldb, ac);
1232
1233         /* replace any attributes in the parse tree that are private,
1234            so we don't allow a search for 'userPassword=penguin',
1235            just as we would not allow that attribute to be returned */
1236         if (ac->am_system) {
1237                 /* FIXME: We should copy the tree and keep the original unmodified. */
1238                 /* remove password attributes */
1239                 if (data && data->password_attrs) {
1240                         for (i = 0; data->password_attrs[i]; i++) {
1241                                 ldb_parse_tree_attr_replace(req->op.search.tree,
1242                                                             data->password_attrs[i],
1243                                                             "kludgeACLredactedattribute");
1244                         }
1245                 }
1246         }
1247         ret = ldb_build_search_req_ex(&down_req,
1248                                       ldb, ac,
1249                                       req->op.search.base,
1250                                       req->op.search.scope,
1251                                       req->op.search.tree,
1252                                       req->op.search.attrs,
1253                                       req->controls,
1254                                       ac, acl_search_callback,
1255                                       req);
1256         if (ret != LDB_SUCCESS) {
1257                 return ret;
1258         }
1259         /* perform the search */
1260         return ldb_next_request(module, down_req);
1261 }
1262
1263 _PUBLIC_ const struct ldb_module_ops ldb_acl_module_ops = {
1264         .name              = "acl",
1265         .search            = acl_search,
1266         .add               = acl_add,
1267         .modify            = acl_modify,
1268         .del               = acl_delete,
1269         .rename            = acl_rename,
1270         .init_context      = acl_module_init
1271 };