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