s4:security Change struct security_token->sids from struct dom_sid * to struct dom_sid
[kai/samba.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                 return ldb_next_request(module, req);
628         }
629
630         schema = dsdb_get_schema(ldb, req);
631         if (!schema) {
632                 return ldb_operr(ldb);
633         }
634
635         oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
636         if (!oc_el || oc_el->num_values == 0) {
637                 DEBUG(10,("acl:operation error %s\n", ldb_dn_get_linearized(req->op.add.message->dn)));
638                 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
639         }
640
641         guid = class_schemaid_guid_by_lDAPDisplayName(schema,
642                                                       (char *)oc_el->values[oc_el->num_values-1].data);
643         ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, guid);
644         if (ret != LDB_SUCCESS) {
645                 return ret;
646         }
647         return ldb_next_request(module, req);
648 }
649
650 /* checks for validated writes */
651 static int acl_check_extended_right(TALLOC_CTX *mem_ctx,
652                                     struct security_descriptor *sd,
653                                     struct security_token *token,
654                                     const char *ext_right,
655                                     uint32_t right_type,
656                                     struct dom_sid *sid)
657 {
658         struct GUID right;
659         NTSTATUS status;
660         uint32_t access_granted;
661         struct object_tree *root = NULL;
662         struct object_tree *new_node = NULL;
663         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
664
665         GUID_from_string(ext_right, &right);
666
667         if (!insert_in_object_tree(tmp_ctx, &right, right_type,
668                                    &root, &new_node)) {
669                 DEBUG(10, ("acl_ext_right: cannot add to object tree\n"));
670                 talloc_free(tmp_ctx);
671                 return LDB_ERR_OPERATIONS_ERROR;
672         }
673         status = sec_access_check_ds(sd, token,
674                                      right_type,
675                                      &access_granted,
676                                      root,
677                                      sid);
678
679         if (!NT_STATUS_IS_OK(status)) {
680                 talloc_free(tmp_ctx);
681                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
682         }
683         talloc_free(tmp_ctx);
684         return LDB_SUCCESS;
685 }
686
687
688 /* ckecks if modifications are allowed on "Member" attribute */
689 static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
690                                      struct ldb_module *module,
691                                      struct ldb_request *req,
692                                      struct security_descriptor *sd,
693                                      struct dom_sid *sid,
694                                      const struct GUID *oc_guid,
695                                      const struct dsdb_attribute *attr)
696 {
697         int ret;
698         unsigned int i;
699         struct ldb_context *ldb = ldb_module_get_ctx(module);
700         struct ldb_dn *user_dn;
701         struct ldb_message_element *member_el;
702         /* if we have wp, we can do whatever we like */
703         if (acl_check_access_on_attribute(module,
704                                           mem_ctx,
705                                           sd,
706                                           sid,
707                                           SEC_ADS_WRITE_PROP,
708                                           attr) == LDB_SUCCESS) {
709                 return LDB_SUCCESS;
710         }
711         /* if we are adding/deleting ourselves, check for self membership */
712         ret = dsdb_find_dn_by_sid(ldb, mem_ctx, 
713                                   &acl_user_token(module)->sids[PRIMARY_USER_SID_INDEX], 
714                                   &user_dn);
715         if (ret != LDB_SUCCESS) {
716                 return ret;
717         }
718         member_el = ldb_msg_find_element(req->op.mod.message, "member");
719         if (!member_el) {
720                 return ldb_operr(ldb);
721         }
722         /* user can only remove oneself */
723         if (member_el->num_values == 0) {
724                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
725         }
726         for (i = 0; i < member_el->num_values; i++) {
727                 if (strcasecmp((const char *)member_el->values[i].data,
728                                ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
729                         return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
730                 }
731         }
732         ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
733                                        GUID_DRS_SELF_MEMBERSHIP,
734                                        SEC_ADS_SELF_WRITE,
735                                        sid);
736         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
737                 dsdb_acl_debug(sd, acl_user_token(module),
738                                req->op.mod.message->dn,
739                                true,
740                                10);
741         }
742         return ret;
743 }
744
745 static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
746                                      struct ldb_module *module,
747                                      struct ldb_request *req,
748                                      struct security_descriptor *sd,
749                                      struct dom_sid *sid,
750                                      const struct GUID *oc_guid)
751 {
752         int ret = LDB_SUCCESS;
753         unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
754         struct ldb_message_element *el;
755         struct ldb_message *msg;
756         const char *passwordAttrs[] = { "userPassword", "clearTextPassword",
757                                         "unicodePwd", "dBCSPwd", NULL }, **l;
758         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
759
760         msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
761         if (msg == NULL) {
762                 return ldb_module_oom(module);
763         }
764         for (l = passwordAttrs; *l != NULL; l++) {
765                 while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
766                         if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
767                                 ++del_attr_cnt;
768                         }
769                         if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
770                                 ++add_attr_cnt;
771                         }
772                         if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) {
773                                 ++rep_attr_cnt;
774                         }
775                         ldb_msg_remove_element(msg, el);
776                 }
777         }
778         /* a single delete will be handled by password hash
779            later in the stack, so we let it though here */
780         if (del_attr_cnt > 0 && add_attr_cnt == 0) {
781                 talloc_free(tmp_ctx);
782                 return LDB_SUCCESS;
783         }
784
785         if (ldb_request_get_control(req,
786                                     DSDB_CONTROL_PASSWORD_CHANGE_OID) != NULL) {
787                 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
788                  * have a user password change and not a set as the message
789                  * looks like. In it's value blob it contains the NT and/or LM
790                  * hash of the old password specified by the user.
791                  * This control is used by the SAMR and "kpasswd" password
792                  * change mechanisms. */
793                 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
794                                                GUID_DRS_USER_CHANGE_PASSWORD,
795                                                SEC_ADS_CONTROL_ACCESS,
796                                                sid);
797         }
798         else if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
799                 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
800                                                GUID_DRS_FORCE_CHANGE_PASSWORD,
801                                                SEC_ADS_CONTROL_ACCESS,
802                                                sid);
803         }
804         else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
805                 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
806                                                GUID_DRS_USER_CHANGE_PASSWORD,
807                                                SEC_ADS_CONTROL_ACCESS,
808                                                sid);
809                 /* Very strange, but we get constraint violation in this case */
810                 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
811                         ret = LDB_ERR_CONSTRAINT_VIOLATION;
812                 }
813         }
814         if (ret != LDB_SUCCESS) {
815                 dsdb_acl_debug(sd, acl_user_token(module),
816                                req->op.mod.message->dn,
817                                true,
818                                10);
819         }
820         talloc_free(tmp_ctx);
821         return ret;
822 }
823
824 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
825 {
826         int ret;
827         struct ldb_context *ldb = ldb_module_get_ctx(module);
828         const struct dsdb_schema *schema;
829         unsigned int i;
830         const struct GUID *guid;
831         uint32_t access_granted;
832         struct object_tree *root = NULL;
833         struct object_tree *new_node = NULL;
834         NTSTATUS status;
835         struct ldb_result *acl_res;
836         struct security_descriptor *sd;
837         struct dom_sid *sid = NULL;
838         struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
839         TALLOC_CTX *tmp_ctx = talloc_new(req);
840         static const char *acl_attrs[] = {
841                 "nTSecurityDescriptor",
842                 "objectClass",
843                 "objectSid",
844                 NULL
845         };
846
847         if (as_system != NULL) {
848                 as_system->critical = 0;
849         }
850
851         /* Don't print this debug statement if elements[0].name is going to be NULL */
852         if(req->op.mod.message->num_elements > 0)
853         {
854                 DEBUG(10, ("ldb:acl_modify: %s\n", req->op.mod.message->elements[0].name));
855         }
856         if (dsdb_module_am_system(module) || as_system) {
857                 return ldb_next_request(module, req);
858         }
859         if (ldb_dn_is_special(req->op.mod.message->dn)) {
860                 return ldb_next_request(module, req);
861         }
862         ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, req->op.mod.message->dn,
863                                     acl_attrs,
864                                     DSDB_FLAG_NEXT_MODULE);
865
866         if (ret != LDB_SUCCESS) {
867                 goto fail;
868         }
869
870         schema = dsdb_get_schema(ldb, tmp_ctx);
871         if (!schema) {
872                 ret = LDB_ERR_OPERATIONS_ERROR;
873                 goto fail;
874         }
875
876         ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
877         if (ret != LDB_SUCCESS) {
878                 DEBUG(10, ("acl_modify: cannot get descriptor\n"));
879                 goto fail;
880         }
881         /* Theoretically we pass the check if the object has no sd */
882         if (!sd) {
883                 goto success;
884         }
885
886         guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
887         if (!guid) {
888                 DEBUG(10, ("acl_modify: cannot get guid\n"));
889                 goto fail;
890         }
891         sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
892         if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
893                                    &root, &new_node)) {
894                 DEBUG(10, ("acl_modify: cannot add to object tree\n"));
895                 goto fail;
896         }
897         for (i=0; i < req->op.mod.message->num_elements; i++){
898                 const struct dsdb_attribute *attr;
899                 attr = dsdb_attribute_by_lDAPDisplayName(schema,
900                                                                  req->op.mod.message->elements[i].name);
901
902                 if (ldb_attr_cmp("nTSecurityDescriptor", req->op.mod.message->elements[i].name) == 0) {
903                         status = sec_access_check_ds(sd, acl_user_token(module),
904                                              SEC_STD_WRITE_DAC,
905                                              &access_granted,
906                                              NULL,
907                                              sid);
908
909                         if (!NT_STATUS_IS_OK(status)) {
910                                 DEBUG(10, ("Object %s has no write dacl access\n",
911                                            ldb_dn_get_linearized(req->op.mod.message->dn)));
912                                 dsdb_acl_debug(sd,
913                                                acl_user_token(module),
914                                                req->op.mod.message->dn,
915                                                true,
916                                                10);
917                                 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
918                                 goto fail;
919                         }
920                 }
921                 else if (ldb_attr_cmp("member", req->op.mod.message->elements[i].name) == 0) {
922                         ret = acl_check_self_membership(tmp_ctx,
923                                                         module,
924                                                         req,
925                                                         sd,
926                                                         sid,
927                                                         guid,
928                                                         attr);
929                         if (ret != LDB_SUCCESS) {
930                                 goto fail;
931                         }
932                 }
933                 else if (ldb_attr_cmp("dBCSPwd", req->op.mod.message->elements[i].name) == 0) {
934                         /* this one is not affected by any rights, we should let it through
935                            so that passwords_hash returns the correct error */
936                         continue;
937                 }
938                 else if (ldb_attr_cmp("unicodePwd", req->op.mod.message->elements[i].name) == 0 ||
939                          ldb_attr_cmp("userPassword", req->op.mod.message->elements[i].name) == 0 ||
940                          ldb_attr_cmp("clearTextPassword", req->op.mod.message->elements[i].name) == 0) {
941                         ret = acl_check_password_rights(tmp_ctx,
942                                                         module,
943                                                         req,
944                                                         sd,
945                                                         sid,
946                                                         guid);
947                         if (ret != LDB_SUCCESS) {
948                                 goto fail;
949                         }
950                 } else {
951
952                 /* This basic attribute existence check with the right errorcode
953                  * is needed since this module is the first one which requests
954                  * schema attribute informations.
955                  * The complete attribute checking is done in the
956                  * "objectclass_attrs" module behind this one.
957                  */
958                         if (!attr) {
959                                 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' on entry '%s' was not found in the schema!",
960                                                        req->op.mod.message->elements[i].name,
961                                                ldb_dn_get_linearized(req->op.mod.message->dn));
962                                 ret =  LDB_ERR_NO_SUCH_ATTRIBUTE;
963                                 goto fail;
964                         }
965                         if (!insert_in_object_tree(tmp_ctx,
966                                                    &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
967                                                    &new_node, &new_node)) {
968                                 DEBUG(10, ("acl_modify: cannot add to object tree securityGUID\n"));
969                                 ret = LDB_ERR_OPERATIONS_ERROR;
970                                 goto fail;
971                         }
972
973                         if (!insert_in_object_tree(tmp_ctx,
974                                                    &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
975                                 DEBUG(10, ("acl_modify: cannot add to object tree attributeGUID\n"));
976                                 ret = LDB_ERR_OPERATIONS_ERROR;
977                                 goto fail;
978                         }
979                 }
980         }
981
982         if (root->num_of_children > 0) {
983                 status = sec_access_check_ds(sd, acl_user_token(module),
984                                              SEC_ADS_WRITE_PROP,
985                                              &access_granted,
986                                              root,
987                                              sid);
988
989                 if (!NT_STATUS_IS_OK(status)) {
990                         DEBUG(10, ("Object %s has no write property access\n",
991                                    ldb_dn_get_linearized(req->op.mod.message->dn)));
992                         dsdb_acl_debug(sd,
993                                   acl_user_token(module),
994                                   req->op.mod.message->dn,
995                                   true,
996                                   10);
997                         ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
998                         goto fail;
999                 }
1000         }
1001
1002 success:
1003         talloc_free(tmp_ctx);
1004         return ldb_next_request(module, req);
1005 fail:
1006         talloc_free(tmp_ctx);
1007         return ret;
1008 }
1009
1010 /* similar to the modify for the time being.
1011  * We need to concider the special delete tree case, though - TODO */
1012 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
1013 {
1014         int ret;
1015         struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.del.dn);
1016         struct ldb_context *ldb;
1017         struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1018
1019         if (as_system != NULL) {
1020                 as_system->critical = 0;
1021         }
1022
1023         DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
1024         if (dsdb_module_am_system(module) || as_system) {
1025                 return ldb_next_request(module, req);
1026         }
1027
1028         if (ldb_dn_is_special(req->op.del.dn)) {
1029                 return ldb_next_request(module, req);
1030         }
1031         ldb = ldb_module_get_ctx(module);
1032         /* first check if we have delete object right */
1033         ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn, SEC_STD_DELETE, NULL);
1034         if (ret == LDB_SUCCESS) {
1035                 return ldb_next_request(module, req);
1036         }
1037
1038         /* Nope, we don't have delete object. Lets check if we have delete child on the parent */
1039         /* No parent, so check fails */
1040         /* FIXME: this has to be made dynamic at some point */
1041         if ((ldb_dn_compare(req->op.del.dn, (ldb_get_schema_basedn(ldb))) == 0) ||
1042             (ldb_dn_compare(req->op.del.dn, (ldb_get_config_basedn(ldb))) == 0) ||
1043             (ldb_dn_compare(req->op.del.dn, (ldb_get_default_basedn(ldb))) == 0)) {
1044                 DEBUG(10,("acl:deleting an NC\n"));
1045                 return ldb_module_done(req, NULL, NULL, LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS);
1046         }
1047
1048         ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_DELETE_CHILD, NULL);
1049         if (ret != LDB_SUCCESS) {
1050                 return ret;
1051         }
1052         return ldb_next_request(module, req);
1053 }
1054
1055 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
1056 {
1057         int ret;
1058         struct ldb_dn *oldparent = ldb_dn_get_parent(req, req->op.rename.olddn);
1059         struct ldb_dn *newparent = ldb_dn_get_parent(req, req->op.rename.newdn);
1060         const struct dsdb_schema *schema;
1061         struct ldb_context *ldb;
1062         struct security_descriptor *sd = NULL;
1063         struct dom_sid *sid = NULL;
1064         struct ldb_result *acl_res;
1065         const struct GUID *guid;
1066         struct object_tree *root = NULL;
1067         struct object_tree *new_node = NULL;
1068         struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1069         TALLOC_CTX *tmp_ctx = talloc_new(req);
1070         NTSTATUS status;
1071         uint32_t access_granted;
1072         const char *rdn_name;
1073         static const char *acl_attrs[] = {
1074                 "nTSecurityDescriptor",
1075                 "objectClass",
1076                 "objectSid",
1077                 NULL
1078         };
1079
1080         if (as_system != NULL) {
1081                 as_system->critical = 0;
1082         }
1083
1084         DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
1085         if (dsdb_module_am_system(module) || as_system) {
1086                 return ldb_next_request(module, req);
1087         }
1088         if (ldb_dn_is_special(req->op.rename.olddn)) {
1089                 return ldb_next_request(module, req);
1090         }
1091         ldb = ldb_module_get_ctx(module);
1092
1093         ret = dsdb_module_search_dn(module, req, &acl_res, req->op.rename.olddn,
1094                                     acl_attrs,
1095                                     DSDB_FLAG_NEXT_MODULE |
1096                                     DSDB_SEARCH_SHOW_DELETED);
1097         /* we sould be able to find the parent */
1098         if (ret != LDB_SUCCESS) {
1099                 DEBUG(10,("acl: failed to find object %s\n",
1100                           ldb_dn_get_linearized(req->op.rename.olddn)));
1101                 return ret;
1102         }
1103
1104         schema = dsdb_get_schema(ldb, acl_res);
1105         if (!schema) {
1106                 talloc_free(acl_res);
1107                 return ldb_operr(ldb);
1108         }
1109
1110         guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
1111         if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1112                                    &root, &new_node)) {
1113                 return ldb_operr(ldb);
1114         };
1115
1116         guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1117                                                           "name");
1118         if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1119                                    &new_node, &new_node)) {
1120                 return ldb_operr(ldb);
1121         };
1122
1123         rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
1124         if (rdn_name == NULL) {
1125                 return ldb_operr(ldb);
1126         }
1127         guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1128                                                           rdn_name);
1129         if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1130                                    &new_node, &new_node)) {
1131                 return ldb_operr(ldb);
1132         };
1133
1134         ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1135
1136         if (ret != LDB_SUCCESS) {
1137                 return ldb_operr(ldb);
1138         }
1139         /* Theoretically we pass the check if the object has no sd */
1140         if (!sd) {
1141                 return LDB_SUCCESS;
1142         }
1143         sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1144         status = sec_access_check_ds(sd, acl_user_token(module),
1145                                      SEC_ADS_WRITE_PROP,
1146                                      &access_granted,
1147                                      root,
1148                                      sid);
1149
1150         if (!NT_STATUS_IS_OK(status)) {
1151                 DEBUG(10, ("Object %s has no wp on name\n",
1152                            ldb_dn_get_linearized(req->op.rename.olddn)));
1153                 dsdb_acl_debug(sd,
1154                           acl_user_token(module),
1155                           req->op.rename.olddn,
1156                           true,
1157                           10);
1158                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1159         }
1160
1161         if (ldb_dn_compare(oldparent, newparent) == 0) {
1162                 /* regular rename, not move, nothing more to do */
1163                 return ldb_next_request(module, req);
1164         }
1165
1166         /* What exactly to do in this case? It would fail anyway.. */
1167         /* FIXME: this has to be made dynamic at some point */
1168         if ((ldb_dn_compare(req->op.rename.newdn, (ldb_get_schema_basedn(ldb))) == 0) ||
1169             (ldb_dn_compare(req->op.rename.newdn, (ldb_get_config_basedn(ldb))) == 0) ||
1170             (ldb_dn_compare(req->op.rename.newdn, (ldb_get_default_basedn(ldb))) == 0)) {
1171                 DEBUG(10,("acl:moving as an NC\n"));
1172                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1173         }
1174         /* new parent should have create child */
1175         talloc_free(tmp_ctx);
1176         tmp_ctx = talloc_new(req);
1177         root = NULL;
1178         new_node = NULL;
1179         guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
1180         if (!guid) {
1181                 DEBUG(10,("acl:renamed object has no object class\n"));
1182                 return ldb_module_done(req, NULL, NULL,  LDB_ERR_OPERATIONS_ERROR);
1183         }
1184
1185         ret = dsdb_module_check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, guid);
1186         if (ret != LDB_SUCCESS) {
1187                 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1188                 return ret;
1189         }
1190         /* do we have delete object on the object? */
1191
1192         status = sec_access_check_ds(sd, acl_user_token(module),
1193                                      SEC_STD_DELETE,
1194                                      &access_granted,
1195                                      NULL,
1196                                      sid);
1197
1198         if (NT_STATUS_IS_OK(status)) {
1199                 return ldb_next_request(module, req);
1200         }
1201         /* what about delete child on the current parent */
1202         ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL);
1203         if (ret != LDB_SUCCESS) {
1204                 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1205                 return ldb_module_done(req, NULL, NULL, ret);
1206         }
1207         return ldb_next_request(module, req);
1208 }
1209
1210 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1211 {
1212         struct ldb_context *ldb;
1213         struct acl_context *ac;
1214         struct acl_private *data;
1215         struct ldb_result *acl_res;
1216         static const char *acl_attrs[] = {
1217                 "objectClass",
1218                 "nTSecurityDescriptor",
1219                 "objectSid",
1220                 NULL
1221         };
1222         int ret, i;
1223
1224         ac = talloc_get_type(req->context, struct acl_context);
1225         data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1226         ldb = ldb_module_get_ctx(ac->module);
1227
1228         if (!ares) {
1229                 return ldb_module_done(ac->req, NULL, NULL,
1230                                        LDB_ERR_OPERATIONS_ERROR);
1231         }
1232         if (ares->error != LDB_SUCCESS) {
1233                 return ldb_module_done(ac->req, ares->controls,
1234                                        ares->response, ares->error);
1235         }
1236
1237         switch (ares->type) {
1238         case LDB_REPLY_ENTRY:
1239                 if (ac->allowedAttributes 
1240                     || ac->allowedChildClasses
1241                     || ac->allowedChildClassesEffective
1242                     || ac->allowedAttributesEffective
1243                     || ac->sDRightsEffective) {
1244                         ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn, 
1245                                                     acl_attrs,
1246                                                     DSDB_FLAG_NEXT_MODULE);
1247                         if (ret != LDB_SUCCESS) {
1248                                 return ldb_module_done(ac->req, NULL, NULL, ret);
1249                         }
1250                         if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1251                                 ret = acl_allowedAttributes(ac->module, ac->schema, acl_res->msgs[0], ares->message, ac);
1252                                 if (ret != LDB_SUCCESS) {
1253                                         return ldb_module_done(ac->req, NULL, NULL, ret);
1254                                 }
1255                         }
1256                         if (ac->allowedChildClasses) {
1257                                 ret = acl_childClasses(ac->module, ac->schema, acl_res->msgs[0],
1258                                                        ares->message, "allowedChildClasses");
1259                                 if (ret != LDB_SUCCESS) {
1260                                         return ldb_module_done(ac->req, NULL, NULL, ret);
1261                                 }
1262                         }
1263                         if (ac->allowedChildClassesEffective) {
1264                                 ret = acl_childClassesEffective(ac->module, ac->schema,
1265                                                                 acl_res->msgs[0], ares->message, ac);
1266                                 if (ret != LDB_SUCCESS) {
1267                                         return ldb_module_done(ac->req, NULL, NULL, ret);
1268                                 }
1269                         }
1270                         if (ac->sDRightsEffective) {
1271                                 ret = acl_sDRightsEffective(ac->module, 
1272                                                             acl_res->msgs[0], ares->message, ac);
1273                                 if (ret != LDB_SUCCESS) {
1274                                         return ldb_module_done(ac->req, NULL, NULL, ret);
1275                                 }
1276                         }
1277                 }
1278                 if (data && data->password_attrs) {
1279                         if (!ac->am_system) {
1280                                 for (i = 0; data->password_attrs[i]; i++) {
1281                                         ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1282                                 }
1283                         }
1284                 }
1285                 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1286
1287         case LDB_REPLY_REFERRAL:
1288                 return ldb_module_send_referral(ac->req, ares->referral);
1289
1290         case LDB_REPLY_DONE:
1291                 return ldb_module_done(ac->req, ares->controls,
1292                                        ares->response, LDB_SUCCESS);
1293
1294         }
1295         return LDB_SUCCESS;
1296 }
1297
1298 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1299 {
1300         struct ldb_context *ldb;
1301         struct acl_context *ac;
1302         struct ldb_request *down_req;
1303         struct acl_private *data;
1304         int ret, i;
1305
1306         ldb = ldb_module_get_ctx(module);
1307
1308         ac = talloc_zero(req, struct acl_context);
1309         if (ac == NULL) {
1310                 return ldb_oom(ldb);
1311         }
1312         data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1313
1314         ac->module = module;
1315         ac->req = req;
1316         ac->am_system = dsdb_module_am_system(module);
1317         ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1318         ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1319         ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1320         ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1321         ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1322         ac->schema = dsdb_get_schema(ldb, ac);
1323
1324         /* replace any attributes in the parse tree that are private,
1325            so we don't allow a search for 'userPassword=penguin',
1326            just as we would not allow that attribute to be returned */
1327         if (ac->am_system) {
1328                 /* FIXME: We should copy the tree and keep the original unmodified. */
1329                 /* remove password attributes */
1330                 if (data && data->password_attrs) {
1331                         for (i = 0; data->password_attrs[i]; i++) {
1332                                 ldb_parse_tree_attr_replace(req->op.search.tree,
1333                                                             data->password_attrs[i],
1334                                                             "kludgeACLredactedattribute");
1335                         }
1336                 }
1337         }
1338         ret = ldb_build_search_req_ex(&down_req,
1339                                       ldb, ac,
1340                                       req->op.search.base,
1341                                       req->op.search.scope,
1342                                       req->op.search.tree,
1343                                       req->op.search.attrs,
1344                                       req->controls,
1345                                       ac, acl_search_callback,
1346                                       req);
1347         if (ret != LDB_SUCCESS) {
1348                 return ret;
1349         }
1350         /* perform the search */
1351         return ldb_next_request(module, down_req);
1352 }
1353
1354 static const char *acl_user_name(TALLOC_CTX *mem_ctx, struct ldb_module *module)
1355 {
1356         struct ldb_context *ldb = ldb_module_get_ctx(module);
1357         struct auth_session_info *session_info
1358                 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
1359         if (!session_info) {
1360                 return "UNKNOWN (NULL)";
1361         }
1362
1363         return talloc_asprintf(mem_ctx, "%s\\%s",
1364                                session_info->server_info->domain_name,
1365                                session_info->server_info->account_name);
1366 }
1367
1368 static int acl_extended(struct ldb_module *module, struct ldb_request *req)
1369 {
1370         struct ldb_context *ldb = ldb_module_get_ctx(module);
1371         struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1372
1373         /* allow everybody to read the sequence number */
1374         if (strcmp(req->op.extended.oid,
1375                    LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1376                 return ldb_next_request(module, req);
1377         }
1378
1379         if (dsdb_module_am_system(module) ||
1380             dsdb_module_am_administrator(module) || as_system) {
1381                 return ldb_next_request(module, req);
1382         } else {
1383                 ldb_asprintf_errstring(ldb,
1384                                        "acl_extended: "
1385                                        "attempted database modify not permitted. "
1386                                        "User %s is not SYSTEM or an administrator",
1387                                        acl_user_name(req, module));
1388                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1389         }
1390 }
1391
1392 _PUBLIC_ const struct ldb_module_ops ldb_acl_module_ops = {
1393         .name              = "acl",
1394         .search            = acl_search,
1395         .add               = acl_add,
1396         .modify            = acl_modify,
1397         .del               = acl_delete,
1398         .rename            = acl_rename,
1399         .extended          = acl_extended,
1400         .init_context      = acl_module_init
1401 };