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