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