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