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