2a98cd85e75c1f0648397c6c26cbb3b1c5f01135
[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 #include "system/kerberos.h"
44 #include "auth/kerberos/kerberos.h"
45
46 struct extended_access_check_attribute {
47         const char *oa_name;
48         const uint32_t requires_rights;
49 };
50
51 struct acl_private {
52         bool acl_search;
53         const char **password_attrs;
54         void *cached_schema_ptr;
55         uint64_t cached_schema_metadata_usn;
56         uint64_t cached_schema_loaded_usn;
57         const char **confidential_attrs;
58         bool userPassword_support;
59 };
60
61 struct acl_context {
62         struct ldb_module *module;
63         struct ldb_request *req;
64         bool am_system;
65         bool am_administrator;
66         bool modify_search;
67         bool constructed_attrs;
68         bool allowedAttributes;
69         bool allowedAttributesEffective;
70         bool allowedChildClasses;
71         bool allowedChildClassesEffective;
72         bool sDRightsEffective;
73         bool userPassword;
74         const char * const *attrs;
75         struct dsdb_schema *schema;
76 };
77
78 static int acl_module_init(struct ldb_module *module)
79 {
80         struct ldb_context *ldb;
81         struct acl_private *data;
82         int ret;
83         unsigned int i, n, j;
84         TALLOC_CTX *mem_ctx;
85         static const char * const attrs[] = { "passwordAttribute", NULL };
86         static const char * const secret_attrs[] = {
87                 DSDB_SECRET_ATTRIBUTES
88         };
89         struct ldb_result *res;
90         struct ldb_message *msg;
91         struct ldb_message_element *password_attributes;
92
93         ldb = ldb_module_get_ctx(module);
94
95         ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
96         if (ret != LDB_SUCCESS) {
97                 ldb_debug(ldb, LDB_DEBUG_ERROR,
98                           "acl_module_init: Unable to register control with rootdse!\n");
99                 return ldb_operr(ldb);
100         }
101
102         data = talloc_zero(module, struct acl_private);
103         if (data == NULL) {
104                 return ldb_oom(ldb);
105         }
106
107         data->acl_search = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"),
108                                         NULL, "acl", "search", true);
109         ldb_module_set_private(module, data);
110
111         data->userPassword_support = dsdb_user_password_support(module, module, NULL);
112         
113         mem_ctx = talloc_new(module);
114         if (!mem_ctx) {
115                 return ldb_oom(ldb);
116         }
117
118         ret = dsdb_module_search_dn(module, mem_ctx, &res,
119                                     ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
120                                     attrs,
121                                     DSDB_FLAG_NEXT_MODULE |
122                                     DSDB_FLAG_AS_SYSTEM,
123                                     NULL);
124         if (ret != LDB_SUCCESS) {
125                 goto done;
126         }
127         if (res->count == 0) {
128                 goto done;
129         }
130
131         if (res->count > 1) {
132                 talloc_free(mem_ctx);
133                 return LDB_ERR_CONSTRAINT_VIOLATION;
134         }
135
136         msg = res->msgs[0];
137
138         password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
139         if (!password_attributes) {
140                 goto done;
141         }
142         data->password_attrs = talloc_array(data, const char *,
143                         password_attributes->num_values +
144                         ARRAY_SIZE(secret_attrs) + 1);
145         if (!data->password_attrs) {
146                 talloc_free(mem_ctx);
147                 return ldb_oom(ldb);
148         }
149
150         n = 0;
151         for (i=0; i < password_attributes->num_values; i++) {
152                 data->password_attrs[n] = (const char *)password_attributes->values[i].data;
153                 talloc_steal(data->password_attrs, password_attributes->values[i].data);
154                 n++;
155         }
156
157         for (i=0; i < ARRAY_SIZE(secret_attrs); i++) {
158                 bool found = false;
159
160                 for (j=0; j < n; j++) {
161                         if (strcasecmp(data->password_attrs[j], secret_attrs[i]) == 0) {
162                                 found = true;
163                                 break;
164                         }
165                 }
166
167                 if (found) {
168                         continue;
169                 }
170
171                 data->password_attrs[n] = talloc_strdup(data->password_attrs,
172                                                         secret_attrs[i]);
173                 if (data->password_attrs[n] == NULL) {
174                         talloc_free(mem_ctx);
175                         return ldb_oom(ldb);
176                 }
177                 n++;
178         }
179         data->password_attrs[n] = NULL;
180
181 done:
182         talloc_free(mem_ctx);
183         return ldb_next_init(module);
184 }
185
186 static int acl_allowedAttributes(struct ldb_module *module,
187                                  const struct dsdb_schema *schema,
188                                  struct ldb_message *sd_msg,
189                                  struct ldb_message *msg,
190                                  struct acl_context *ac)
191 {
192         struct ldb_message_element *oc_el;
193         struct ldb_context *ldb = ldb_module_get_ctx(module);
194         TALLOC_CTX *mem_ctx;
195         const char **attr_list;
196         int i, ret;
197         const struct dsdb_class *objectclass;
198
199         /* If we don't have a schema yet, we can't do anything... */
200         if (schema == NULL) {
201                 ldb_asprintf_errstring(ldb, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
202                 return LDB_ERR_OPERATIONS_ERROR;
203         }
204
205         /* Must remove any existing attribute */
206         if (ac->allowedAttributes) {
207                 ldb_msg_remove_attr(msg, "allowedAttributes");
208         }
209
210         mem_ctx = talloc_new(msg);
211         if (!mem_ctx) {
212                 return ldb_oom(ldb);
213         }
214
215         oc_el = ldb_msg_find_element(sd_msg, "objectClass");
216         attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
217         if (!attr_list) {
218                 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
219                 talloc_free(mem_ctx);
220                 return LDB_ERR_OPERATIONS_ERROR;
221         }
222
223         /*
224          * Get the top-most structural object class for the ACL check
225          */
226         objectclass = dsdb_get_last_structural_class(ac->schema,
227                                                      oc_el);
228         if (objectclass == NULL) {
229                 ldb_asprintf_errstring(ldb, "acl_read: Failed to find a structural class for %s",
230                                        ldb_dn_get_linearized(sd_msg->dn));
231                 talloc_free(mem_ctx);
232                 return LDB_ERR_OPERATIONS_ERROR;
233         }
234
235         if (ac->allowedAttributes) {
236                 for (i=0; attr_list && attr_list[i]; i++) {
237                         ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
238                 }
239         }
240         if (ac->allowedAttributesEffective) {
241                 struct security_descriptor *sd;
242                 struct dom_sid *sid = NULL;
243                 struct ldb_control *as_system = ldb_request_get_control(ac->req,
244                                                                         LDB_CONTROL_AS_SYSTEM_OID);
245
246                 if (as_system != NULL) {
247                         as_system->critical = 0;
248                 }
249
250                 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
251                 if (ac->am_system || as_system) {
252                         for (i=0; attr_list && attr_list[i]; i++) {
253                                 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
254                         }
255                         return LDB_SUCCESS;
256                 }
257
258                 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), mem_ctx, sd_msg, &sd);
259
260                 if (ret != LDB_SUCCESS) {
261                         return ret;
262                 }
263
264                 sid = samdb_result_dom_sid(mem_ctx, sd_msg, "objectSid");
265                 for (i=0; attr_list && attr_list[i]; i++) {
266                         const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
267                                                                                         attr_list[i]);
268                         if (!attr) {
269                                 return ldb_operr(ldb);
270                         }
271                         /* remove constructed attributes */
272                         if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
273                             || attr->systemOnly
274                             || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
275                                 continue;
276                         }
277                         ret = acl_check_access_on_attribute(module,
278                                                             msg,
279                                                             sd,
280                                                             sid,
281                                                             SEC_ADS_WRITE_PROP,
282                                                             attr,
283                                                             objectclass);
284                         if (ret == LDB_SUCCESS) {
285                                 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
286                         }
287                 }
288         }
289         return LDB_SUCCESS;
290 }
291
292 static int acl_childClasses(struct ldb_module *module,
293                             const struct dsdb_schema *schema,
294                             struct ldb_message *sd_msg,
295                             struct ldb_message *msg,
296                             const char *attrName)
297 {
298         struct ldb_message_element *oc_el;
299         struct ldb_message_element *allowedClasses;
300         const struct dsdb_class *sclass;
301         unsigned int i, j;
302         int ret;
303
304         /* If we don't have a schema yet, we can't do anything... */
305         if (schema == NULL) {
306                 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
307                 return LDB_ERR_OPERATIONS_ERROR;
308         }
309
310         /* Must remove any existing attribute, or else confusion reins */
311         ldb_msg_remove_attr(msg, attrName);
312         ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
313         if (ret != LDB_SUCCESS) {
314                 return ret;
315         }
316
317         oc_el = ldb_msg_find_element(sd_msg, "objectClass");
318
319         for (i=0; oc_el && i < oc_el->num_values; i++) {
320                 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
321                 if (!sclass) {
322                         /* We don't know this class?  what is going on? */
323                         continue;
324                 }
325
326                 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
327                         ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
328                 }
329         }
330         if (allowedClasses->num_values > 1) {
331                 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
332                 for (i=1 ; i < allowedClasses->num_values; i++) {
333                         struct ldb_val *val1 = &allowedClasses->values[i-1];
334                         struct ldb_val *val2 = &allowedClasses->values[i];
335                         if (data_blob_cmp(val1, val2) == 0) {
336                                 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
337                                 allowedClasses->num_values--;
338                                 i--;
339                         }
340                 }
341         }
342
343         return LDB_SUCCESS;
344 }
345
346 static int acl_childClassesEffective(struct ldb_module *module,
347                                      const struct dsdb_schema *schema,
348                                      struct ldb_message *sd_msg,
349                                      struct ldb_message *msg,
350                                      struct acl_context *ac)
351 {
352         struct ldb_message_element *oc_el;
353         struct ldb_message_element *allowedClasses = NULL;
354         const struct dsdb_class *sclass;
355         struct security_descriptor *sd;
356         struct ldb_control *as_system = ldb_request_get_control(ac->req,
357                                                                 LDB_CONTROL_AS_SYSTEM_OID);
358         struct dom_sid *sid = NULL;
359         unsigned int i, j;
360         int ret;
361
362         if (as_system != NULL) {
363                 as_system->critical = 0;
364         }
365
366         if (ac->am_system || as_system) {
367                 return acl_childClasses(module, schema, sd_msg, msg, "allowedChildClassesEffective");
368         }
369
370         /* If we don't have a schema yet, we can't do anything... */
371         if (schema == NULL) {
372                 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
373                 return LDB_ERR_OPERATIONS_ERROR;
374         }
375
376         /* Must remove any existing attribute, or else confusion reins */
377         ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
378
379         oc_el = ldb_msg_find_element(sd_msg, "objectClass");
380         ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
381         if (ret != LDB_SUCCESS) {
382                 return ret;
383         }
384
385         sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
386         for (i=0; oc_el && i < oc_el->num_values; i++) {
387                 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
388                 if (!sclass) {
389                         /* We don't know this class?  what is going on? */
390                         continue;
391                 }
392
393                 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
394                         const struct dsdb_class *sc;
395
396                         sc = dsdb_class_by_lDAPDisplayName(schema,
397                                                            sclass->possibleInferiors[j]);
398                         if (!sc) {
399                                 /* We don't know this class?  what is going on? */
400                                 continue;
401                         }
402
403                         ret = acl_check_access_on_objectclass(module, ac,
404                                                               sd, sid,
405                                                               SEC_ADS_CREATE_CHILD,
406                                                               sc);
407                         if (ret == LDB_SUCCESS) {
408                                 ldb_msg_add_string(msg, "allowedChildClassesEffective",
409                                                    sclass->possibleInferiors[j]);
410                         }
411                 }
412         }
413         allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
414         if (!allowedClasses) {
415                 return LDB_SUCCESS;
416         }
417
418         if (allowedClasses->num_values > 1) {
419                 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
420                 for (i=1 ; i < allowedClasses->num_values; i++) {
421                         struct ldb_val *val1 = &allowedClasses->values[i-1];
422                         struct ldb_val *val2 = &allowedClasses->values[i];
423                         if (data_blob_cmp(val1, val2) == 0) {
424                                 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
425                                 allowedClasses->num_values--;
426                                 i--;
427                         }
428                 }
429         }
430         return LDB_SUCCESS;
431 }
432
433 static int acl_sDRightsEffective(struct ldb_module *module,
434                                  struct ldb_message *sd_msg,
435                                  struct ldb_message *msg,
436                                  struct acl_context *ac)
437 {
438         struct ldb_context *ldb = ldb_module_get_ctx(module);
439         struct ldb_message_element *rightsEffective;
440         int ret;
441         struct security_descriptor *sd;
442         struct ldb_control *as_system = ldb_request_get_control(ac->req,
443                                                                 LDB_CONTROL_AS_SYSTEM_OID);
444         struct dom_sid *sid = NULL;
445         uint32_t flags = 0;
446
447         if (as_system != NULL) {
448                 as_system->critical = 0;
449         }
450
451         /* Must remove any existing attribute, or else confusion reins */
452         ldb_msg_remove_attr(msg, "sDRightsEffective");
453         ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
454         if (ret != LDB_SUCCESS) {
455                 return ret;
456         }
457         if (ac->am_system || as_system) {
458                 flags = SECINFO_OWNER | SECINFO_GROUP |  SECINFO_SACL |  SECINFO_DACL;
459         } else {
460                 const struct dsdb_class *objectclass;
461                 const struct dsdb_attribute *attr;
462
463                 objectclass = dsdb_get_structural_oc_from_msg(ac->schema, sd_msg);
464                 if (objectclass == NULL) {
465                         return ldb_operr(ldb);
466                 }
467
468                 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
469                                                          "nTSecurityDescriptor");
470                 if (attr == NULL) {
471                         return ldb_operr(ldb);
472                 }
473
474                 /* Get the security descriptor from the message */
475                 ret = dsdb_get_sd_from_ldb_message(ldb, msg, sd_msg, &sd);
476                 if (ret != LDB_SUCCESS) {
477                         return ret;
478                 }
479                 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
480                 ret = acl_check_access_on_attribute(module,
481                                                     msg,
482                                                     sd,
483                                                     sid,
484                                                     SEC_STD_WRITE_OWNER,
485                                                     attr,
486                                                     objectclass);
487                 if (ret == LDB_SUCCESS) {
488                         flags |= SECINFO_OWNER | SECINFO_GROUP;
489                 }
490                 ret = acl_check_access_on_attribute(module,
491                                                     msg,
492                                                     sd,
493                                                     sid,
494                                                     SEC_STD_WRITE_DAC,
495                                                     attr,
496                                                     objectclass);
497                 if (ret == LDB_SUCCESS) {
498                         flags |= SECINFO_DACL;
499                 }
500                 ret = acl_check_access_on_attribute(module,
501                                                     msg,
502                                                     sd,
503                                                     sid,
504                                                     SEC_FLAG_SYSTEM_SECURITY,
505                                                     attr,
506                                                     objectclass);
507                 if (ret == LDB_SUCCESS) {
508                         flags |= SECINFO_SACL;
509                 }
510         }
511         return samdb_msg_add_uint(ldb_module_get_ctx(module), msg, msg,
512                                   "sDRightsEffective", flags);
513 }
514
515 static int acl_validate_spn_value(TALLOC_CTX *mem_ctx,
516                                   struct ldb_context *ldb,
517                                   const char *spn_value,
518                                   uint32_t userAccountControl,
519                                   const char *samAccountName,
520                                   const char *dnsHostName,
521                                   const char *netbios_name,
522                                   const char *ntds_guid)
523 {
524         int ret, princ_size;
525         krb5_context krb_ctx;
526         krb5_error_code kerr;
527         krb5_principal principal;
528         char *instanceName;
529         char *serviceType;
530         char *serviceName;
531         const char *forest_name = samdb_forest_name(ldb, mem_ctx);
532         const char *base_domain = samdb_default_domain_name(ldb, mem_ctx);
533         struct loadparm_context *lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
534                                                           struct loadparm_context);
535         bool is_dc = (userAccountControl & UF_SERVER_TRUST_ACCOUNT) ||
536                 (userAccountControl & UF_PARTIAL_SECRETS_ACCOUNT);
537
538         if (strcasecmp_m(spn_value, samAccountName) == 0) {
539                 /* MacOS X sets this value, and setting an SPN of your
540                  * own samAccountName is both pointless and safe */
541                 return LDB_SUCCESS;
542         }
543
544         kerr = smb_krb5_init_context_basic(mem_ctx,
545                                            lp_ctx,
546                                            &krb_ctx);
547         if (kerr != 0) {
548                 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
549                                  "Could not initialize kerberos context.");
550         }
551
552         ret = krb5_parse_name(krb_ctx, spn_value, &principal);
553         if (ret) {
554                 krb5_free_context(krb_ctx);
555                 return LDB_ERR_CONSTRAINT_VIOLATION;
556         }
557
558         princ_size = krb5_princ_size(krb_ctx, principal);
559         if (princ_size < 2) {
560                 DBG_WARNING("princ_size=%d\n", princ_size);
561                 goto fail;
562         }
563
564         instanceName = smb_krb5_principal_get_comp_string(mem_ctx, krb_ctx,
565                                                           principal, 1);
566         serviceType = smb_krb5_principal_get_comp_string(mem_ctx, krb_ctx,
567                                                          principal, 0);
568         if (krb5_princ_size(krb_ctx, principal) == 3) {
569                 serviceName = smb_krb5_principal_get_comp_string(mem_ctx, krb_ctx,
570                                                                  principal, 2);
571         } else {
572                 serviceName = NULL;
573         }
574
575         if (serviceName) {
576                 if (!is_dc) {
577                         DBG_WARNING("is_dc=false, serviceName=%s,"
578                                     "serviceType=%s\n", serviceName,
579                                   serviceType);
580                         goto fail;
581                 }
582                 if (strcasecmp(serviceType, "ldap") == 0) {
583                         if (strcasecmp(serviceName, netbios_name) != 0 &&
584                             strcasecmp(serviceName, forest_name) != 0) {
585                                 DBG_WARNING("serviceName=%s\n", serviceName);
586                                 goto fail;
587                         }
588
589                 } else if (strcasecmp(serviceType, "gc") == 0) {
590                         if (strcasecmp(serviceName, forest_name) != 0) {
591                                 DBG_WARNING("serviceName=%s\n", serviceName);
592                                 goto fail;
593                         }
594                 } else {
595                         if (strcasecmp(serviceName, base_domain) != 0 &&
596                             strcasecmp(serviceName, netbios_name) != 0) {
597                                 DBG_WARNING("serviceType=%s, "
598                                             "serviceName=%s\n",
599                                             serviceType, serviceName);
600                                 goto fail;
601                         }
602                 }
603         }
604         /* instanceName can be samAccountName without $ or dnsHostName
605          * or "ntds_guid._msdcs.forest_domain for DC objects */
606         if (strlen(instanceName) == (strlen(samAccountName) - 1)
607             && strncasecmp(instanceName, samAccountName,
608                            strlen(samAccountName) - 1) == 0) {
609                 goto success;
610         }
611         if ((dnsHostName != NULL) &&
612             (strcasecmp(instanceName, dnsHostName) == 0)) {
613                 goto success;
614         }
615         if (is_dc) {
616                 const char *guid_str;
617                 guid_str = talloc_asprintf(mem_ctx,"%s._msdcs.%s",
618                                            ntds_guid,
619                                            forest_name);
620                 if (strcasecmp(instanceName, guid_str) == 0) {
621                         goto success;
622                 }
623         }
624
625 fail:
626         krb5_free_principal(krb_ctx, principal);
627         krb5_free_context(krb_ctx);
628         ldb_debug_set(ldb, LDB_DEBUG_WARNING,
629                       "acl: spn validation failed for "
630                       "spn[%s] uac[0x%x] account[%s] hostname[%s] "
631                       "nbname[%s] ntds[%s] forest[%s] domain[%s]\n",
632                       spn_value, (unsigned)userAccountControl,
633                       samAccountName, dnsHostName,
634                       netbios_name, ntds_guid,
635                       forest_name, base_domain);
636         return LDB_ERR_CONSTRAINT_VIOLATION;
637
638 success:
639         krb5_free_principal(krb_ctx, principal);
640         krb5_free_context(krb_ctx);
641         return LDB_SUCCESS;
642 }
643
644 static int acl_check_spn(TALLOC_CTX *mem_ctx,
645                          struct ldb_module *module,
646                          struct ldb_request *req,
647                          struct security_descriptor *sd,
648                          struct dom_sid *sid,
649                          const struct dsdb_attribute *attr,
650                          const struct dsdb_class *objectclass)
651 {
652         int ret;
653         unsigned int i;
654         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
655         struct ldb_context *ldb = ldb_module_get_ctx(module);
656         struct ldb_result *acl_res;
657         struct ldb_result *netbios_res;
658         struct ldb_message_element *el;
659         struct ldb_dn *partitions_dn = samdb_partitions_dn(ldb, tmp_ctx);
660         uint32_t userAccountControl;
661         const char *samAccountName;
662         const char *dnsHostName;
663         const char *netbios_name;
664         struct GUID ntds;
665         char *ntds_guid = NULL;
666
667         static const char *acl_attrs[] = {
668                 "samAccountName",
669                 "dnsHostName",
670                 "userAccountControl",
671                 NULL
672         };
673         static const char *netbios_attrs[] = {
674                 "nETBIOSName",
675                 NULL
676         };
677
678         /* if we have wp, we can do whatever we like */
679         if (acl_check_access_on_attribute(module,
680                                           tmp_ctx,
681                                           sd,
682                                           sid,
683                                           SEC_ADS_WRITE_PROP,
684                                           attr, objectclass) == LDB_SUCCESS) {
685                 talloc_free(tmp_ctx);
686                 return LDB_SUCCESS;
687         }
688
689         ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
690                                        GUID_DRS_VALIDATE_SPN,
691                                        SEC_ADS_SELF_WRITE,
692                                        sid);
693
694         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
695                 dsdb_acl_debug(sd, acl_user_token(module),
696                                req->op.mod.message->dn,
697                                true,
698                                10);
699                 talloc_free(tmp_ctx);
700                 return ret;
701         }
702
703         ret = dsdb_module_search_dn(module, tmp_ctx,
704                                     &acl_res, req->op.mod.message->dn,
705                                     acl_attrs,
706                                     DSDB_FLAG_NEXT_MODULE |
707                                     DSDB_FLAG_AS_SYSTEM |
708                                     DSDB_SEARCH_SHOW_RECYCLED,
709                                     req);
710         if (ret != LDB_SUCCESS) {
711                 talloc_free(tmp_ctx);
712                 return ret;
713         }
714
715         userAccountControl = ldb_msg_find_attr_as_uint(acl_res->msgs[0], "userAccountControl", 0);
716         dnsHostName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "dnsHostName", NULL);
717         samAccountName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "samAccountName", NULL);
718
719         ret = dsdb_module_search(module, tmp_ctx,
720                                  &netbios_res, partitions_dn,
721                                  LDB_SCOPE_ONELEVEL,
722                                  netbios_attrs,
723                                  DSDB_FLAG_NEXT_MODULE |
724                                  DSDB_FLAG_AS_SYSTEM,
725                                  req,
726                                  "(ncName=%s)",
727                                  ldb_dn_get_linearized(ldb_get_default_basedn(ldb)));
728
729         netbios_name = ldb_msg_find_attr_as_string(netbios_res->msgs[0], "nETBIOSName", NULL);
730
731         el = ldb_msg_find_element(req->op.mod.message, "servicePrincipalName");
732         if (!el) {
733                 talloc_free(tmp_ctx);
734                 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
735                                          "Error finding element for servicePrincipalName.");
736         }
737
738         /* NTDSDSA objectGuid of object we are checking SPN for */
739         if (userAccountControl & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
740                 ret = dsdb_module_find_ntdsguid_for_computer(module, tmp_ctx,
741                                                              req->op.mod.message->dn, &ntds, req);
742                 if (ret != LDB_SUCCESS) {
743                         ldb_asprintf_errstring(ldb, "Failed to find NTDSDSA objectGuid for %s: %s",
744                                                ldb_dn_get_linearized(req->op.mod.message->dn),
745                                                ldb_strerror(ret));
746                         talloc_free(tmp_ctx);
747                         return LDB_ERR_OPERATIONS_ERROR;
748                 }
749                 ntds_guid = GUID_string(tmp_ctx, &ntds);
750         }
751
752         for (i=0; i < el->num_values; i++) {
753                 ret = acl_validate_spn_value(tmp_ctx,
754                                              ldb,
755                                              (char *)el->values[i].data,
756                                              userAccountControl,
757                                              samAccountName,
758                                              dnsHostName,
759                                              netbios_name,
760                                              ntds_guid);
761                 if (ret != LDB_SUCCESS) {
762                         talloc_free(tmp_ctx);
763                         return ret;
764                 }
765         }
766         talloc_free(tmp_ctx);
767         return LDB_SUCCESS;
768 }
769
770 static int acl_add(struct ldb_module *module, struct ldb_request *req)
771 {
772         int ret;
773         struct ldb_dn *parent;
774         struct ldb_context *ldb;
775         const struct dsdb_schema *schema;
776         const struct dsdb_class *objectclass;
777         struct ldb_control *as_system;
778         struct ldb_message_element *el;
779         unsigned int instanceType = 0;
780
781         if (ldb_dn_is_special(req->op.add.message->dn)) {
782                 return ldb_next_request(module, req);
783         }
784
785         as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
786         if (as_system != NULL) {
787                 as_system->critical = 0;
788         }
789
790         if (dsdb_module_am_system(module) || as_system) {
791                 return ldb_next_request(module, req);
792         }
793
794         ldb = ldb_module_get_ctx(module);
795
796         parent = ldb_dn_get_parent(req, req->op.add.message->dn);
797         if (parent == NULL) {
798                 return ldb_oom(ldb);
799         }
800
801         schema = dsdb_get_schema(ldb, req);
802         if (!schema) {
803                 return ldb_operr(ldb);
804         }
805
806         objectclass = dsdb_get_structural_oc_from_msg(schema, req->op.add.message);
807         if (!objectclass) {
808                 ldb_asprintf_errstring(ldb_module_get_ctx(module),
809                                        "acl: unable to find or validate structural objectClass on %s\n",
810                                        ldb_dn_get_linearized(req->op.add.message->dn));
811                 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
812         }
813
814         el = ldb_msg_find_element(req->op.add.message, "instanceType");
815         if ((el != NULL) && (el->num_values != 1)) {
816                 ldb_set_errstring(ldb, "acl: the 'instanceType' attribute is single-valued!");
817                 return LDB_ERR_UNWILLING_TO_PERFORM;
818         }
819
820         instanceType = ldb_msg_find_attr_as_uint(req->op.add.message,
821                                                  "instanceType", 0);
822         if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
823                 static const char *no_attrs[] = { NULL };
824                 struct ldb_result *partition_res;
825                 struct ldb_dn *partitions_dn;
826
827                 partitions_dn = samdb_partitions_dn(ldb, req);
828                 if (!partitions_dn) {
829                         ldb_set_errstring(ldb, "acl: CN=partitions dn could not be generated!");
830                         return LDB_ERR_UNWILLING_TO_PERFORM;
831                 }
832
833                 ret = dsdb_module_search(module, req, &partition_res,
834                                          partitions_dn, LDB_SCOPE_ONELEVEL,
835                                          no_attrs,
836                                          DSDB_FLAG_NEXT_MODULE |
837                                          DSDB_FLAG_AS_SYSTEM |
838                                          DSDB_SEARCH_ONE_ONLY |
839                                          DSDB_SEARCH_SHOW_RECYCLED,
840                                          req,
841                                          "(&(nCName=%s)(objectClass=crossRef))",
842                                          ldb_dn_get_linearized(req->op.add.message->dn));
843
844                 if (ret == LDB_SUCCESS) {
845                         /* Check that we can write to the crossRef object MS-ADTS 3.1.1.5.2.8.2 */
846                         ret = dsdb_module_check_access_on_dn(module, req, partition_res->msgs[0]->dn,
847                                                              SEC_ADS_WRITE_PROP,
848                                                              &objectclass->schemaIDGUID, req);
849                         if (ret != LDB_SUCCESS) {
850                                 ldb_asprintf_errstring(ldb_module_get_ctx(module),
851                                                        "acl: ACL check failed on crossRef object %s: %s\n",
852                                                        ldb_dn_get_linearized(partition_res->msgs[0]->dn),
853                                                        ldb_errstring(ldb));
854                                 return ret;
855                         }
856
857                         /*
858                          * TODO: Remaining checks, like if we are
859                          * the naming master etc need to be handled
860                          * in the instanceType module
861                          */
862                         return ldb_next_request(module, req);
863                 }
864
865                 /* Check that we can create a crossRef object MS-ADTS 3.1.1.5.2.8.2 */
866                 ret = dsdb_module_check_access_on_dn(module, req, partitions_dn,
867                                                      SEC_ADS_CREATE_CHILD,
868                                                      &objectclass->schemaIDGUID, req);
869                 if (ret == LDB_ERR_NO_SUCH_OBJECT &&
870                     ldb_request_get_control(req, LDB_CONTROL_RELAX_OID))
871                 {
872                         /* Allow provision bootstrap */
873                         ret = LDB_SUCCESS;
874                 }
875                 if (ret != LDB_SUCCESS) {
876                         ldb_asprintf_errstring(ldb_module_get_ctx(module),
877                                                "acl: ACL check failed on CN=Partitions crossRef container %s: %s\n",
878                                                ldb_dn_get_linearized(partitions_dn), ldb_errstring(ldb));
879                         return ret;
880                 }
881
882                 /*
883                  * TODO: Remaining checks, like if we are the naming
884                  * master and adding the crossRef object need to be
885                  * handled in the instanceType module
886                  */
887                 return ldb_next_request(module, req);
888         }
889
890         ret = dsdb_module_check_access_on_dn(module, req, parent,
891                                              SEC_ADS_CREATE_CHILD,
892                                              &objectclass->schemaIDGUID, req);
893         if (ret != LDB_SUCCESS) {
894                 ldb_asprintf_errstring(ldb_module_get_ctx(module),
895                                        "acl: unable to get access to %s\n",
896                                        ldb_dn_get_linearized(req->op.add.message->dn));
897                 return ret;
898         }
899         return ldb_next_request(module, req);
900 }
901
902 /* ckecks if modifications are allowed on "Member" attribute */
903 static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
904                                      struct ldb_module *module,
905                                      struct ldb_request *req,
906                                      struct security_descriptor *sd,
907                                      struct dom_sid *sid,
908                                      const struct dsdb_attribute *attr,
909                                      const struct dsdb_class *objectclass)
910 {
911         int ret;
912         unsigned int i;
913         struct ldb_context *ldb = ldb_module_get_ctx(module);
914         struct ldb_dn *user_dn;
915         struct ldb_message_element *member_el;
916         /* if we have wp, we can do whatever we like */
917         if (acl_check_access_on_attribute(module,
918                                           mem_ctx,
919                                           sd,
920                                           sid,
921                                           SEC_ADS_WRITE_PROP,
922                                           attr, objectclass) == LDB_SUCCESS) {
923                 return LDB_SUCCESS;
924         }
925         /* if we are adding/deleting ourselves, check for self membership */
926         ret = dsdb_find_dn_by_sid(ldb, mem_ctx, 
927                                   &acl_user_token(module)->sids[PRIMARY_USER_SID_INDEX], 
928                                   &user_dn);
929         if (ret != LDB_SUCCESS) {
930                 return ret;
931         }
932         member_el = ldb_msg_find_element(req->op.mod.message, "member");
933         if (!member_el) {
934                 return ldb_operr(ldb);
935         }
936         /* user can only remove oneself */
937         if (member_el->num_values == 0) {
938                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
939         }
940         for (i = 0; i < member_el->num_values; i++) {
941                 if (strcasecmp((const char *)member_el->values[i].data,
942                                ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
943                         return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
944                 }
945         }
946         ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
947                                        GUID_DRS_SELF_MEMBERSHIP,
948                                        SEC_ADS_SELF_WRITE,
949                                        sid);
950         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
951                 dsdb_acl_debug(sd, acl_user_token(module),
952                                req->op.mod.message->dn,
953                                true,
954                                10);
955         }
956         return ret;
957 }
958
959 static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
960                                      struct ldb_module *module,
961                                      struct ldb_request *req,
962                                      struct security_descriptor *sd,
963                                      struct dom_sid *sid,
964                                      const struct dsdb_class *objectclass,
965                                      bool userPassword)
966 {
967         int ret = LDB_SUCCESS;
968         unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
969         struct ldb_message_element *el;
970         struct ldb_message *msg;
971         const char *passwordAttrs[] = { "userPassword", "clearTextPassword",
972                                         "unicodePwd", "dBCSPwd", NULL }, **l;
973         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
974
975         msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
976         if (msg == NULL) {
977                 return ldb_module_oom(module);
978         }
979         for (l = passwordAttrs; *l != NULL; l++) {
980                 if ((!userPassword) && (ldb_attr_cmp(*l, "userPassword") == 0)) {
981                         continue;
982                 }
983
984                 while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
985                         if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
986                                 ++del_attr_cnt;
987                         }
988                         if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
989                                 ++add_attr_cnt;
990                         }
991                         if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) {
992                                 ++rep_attr_cnt;
993                         }
994                         ldb_msg_remove_element(msg, el);
995                 }
996         }
997
998         /* single deletes will be handled by the "password_hash" LDB module
999          * later in the stack, so we let it though here */
1000         if ((del_attr_cnt > 0) && (add_attr_cnt == 0) && (rep_attr_cnt == 0)) {
1001                 talloc_free(tmp_ctx);
1002                 return LDB_SUCCESS;
1003         }
1004
1005         if (ldb_request_get_control(req,
1006                                     DSDB_CONTROL_PASSWORD_CHANGE_OID) != NULL) {
1007                 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
1008                  * have a user password change and not a set as the message
1009                  * looks like. In it's value blob it contains the NT and/or LM
1010                  * hash of the old password specified by the user.
1011                  * This control is used by the SAMR and "kpasswd" password
1012                  * change mechanisms. */
1013                 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
1014                                                GUID_DRS_USER_CHANGE_PASSWORD,
1015                                                SEC_ADS_CONTROL_ACCESS,
1016                                                sid);
1017         }
1018         else if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
1019                 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
1020                                                GUID_DRS_FORCE_CHANGE_PASSWORD,
1021                                                SEC_ADS_CONTROL_ACCESS,
1022                                                sid);
1023         }
1024         else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
1025                 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
1026                                                GUID_DRS_USER_CHANGE_PASSWORD,
1027                                                SEC_ADS_CONTROL_ACCESS,
1028                                                sid);
1029                 /* Very strange, but we get constraint violation in this case */
1030                 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
1031                         ret = LDB_ERR_CONSTRAINT_VIOLATION;
1032                 }
1033         }
1034         if (ret != LDB_SUCCESS) {
1035                 dsdb_acl_debug(sd, acl_user_token(module),
1036                                req->op.mod.message->dn,
1037                                true,
1038                                10);
1039         }
1040         talloc_free(tmp_ctx);
1041         return ret;
1042 }
1043
1044
1045 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
1046 {
1047         int ret;
1048         struct ldb_context *ldb = ldb_module_get_ctx(module);
1049         const struct dsdb_schema *schema;
1050         unsigned int i;
1051         const struct dsdb_class *objectclass;
1052         struct ldb_result *acl_res;
1053         struct security_descriptor *sd;
1054         struct dom_sid *sid = NULL;
1055         struct ldb_control *as_system;
1056         struct ldb_control *is_undelete;
1057         bool userPassword;
1058         TALLOC_CTX *tmp_ctx;
1059         const struct ldb_message *msg = req->op.mod.message;
1060         static const char *acl_attrs[] = {
1061                 "nTSecurityDescriptor",
1062                 "objectClass",
1063                 "objectSid",
1064                 NULL
1065         };
1066
1067         if (ldb_dn_is_special(msg->dn)) {
1068                 return ldb_next_request(module, req);
1069         }
1070
1071         as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1072         if (as_system != NULL) {
1073                 as_system->critical = 0;
1074         }
1075
1076         is_undelete = ldb_request_get_control(req, DSDB_CONTROL_RESTORE_TOMBSTONE_OID);
1077
1078         /* Don't print this debug statement if elements[0].name is going to be NULL */
1079         if (msg->num_elements > 0) {
1080                 DEBUG(10, ("ldb:acl_modify: %s\n", msg->elements[0].name));
1081         }
1082         if (dsdb_module_am_system(module) || as_system) {
1083                 return ldb_next_request(module, req);
1084         }
1085
1086         tmp_ctx = talloc_new(req);
1087         if (tmp_ctx == NULL) {
1088                 return ldb_oom(ldb);
1089         }
1090
1091         ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, msg->dn,
1092                                     acl_attrs,
1093                                     DSDB_FLAG_NEXT_MODULE |
1094                                     DSDB_FLAG_AS_SYSTEM |
1095                                     DSDB_SEARCH_SHOW_RECYCLED,
1096                                     req);
1097
1098         if (ret != LDB_SUCCESS) {
1099                 goto fail;
1100         }
1101
1102         userPassword = dsdb_user_password_support(module, req, req);
1103
1104         schema = dsdb_get_schema(ldb, tmp_ctx);
1105         if (!schema) {
1106                 talloc_free(tmp_ctx);
1107                 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1108                                  "acl_modify: Error obtaining schema.");
1109         }
1110
1111         ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
1112         if (ret != LDB_SUCCESS) {
1113                 talloc_free(tmp_ctx);
1114                 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1115                                  "acl_modify: Error retrieving security descriptor.");
1116         }
1117         /* Theoretically we pass the check if the object has no sd */
1118         if (!sd) {
1119                 goto success;
1120         }
1121
1122         objectclass = dsdb_get_structural_oc_from_msg(schema, acl_res->msgs[0]);
1123         if (!objectclass) {
1124                 talloc_free(tmp_ctx);
1125                 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1126                                  "acl_modify: Error retrieving object class for GUID.");
1127         }
1128         sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1129         for (i=0; i < msg->num_elements; i++) {
1130                 const struct ldb_message_element *el = &msg->elements[i];
1131                 const struct dsdb_attribute *attr;
1132
1133                 /*
1134                  * This basic attribute existence check with the right errorcode
1135                  * is needed since this module is the first one which requests
1136                  * schema attribute information.
1137                  * The complete attribute checking is done in the
1138                  * "objectclass_attrs" module behind this one.
1139                  *
1140                  * NOTE: "clearTextPassword" is not defined in the schema.
1141                  */
1142                 attr = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
1143                 if (!attr && ldb_attr_cmp("clearTextPassword", el->name) != 0) {
1144                         ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' "
1145                                                "on entry '%s' was not found in the schema!",
1146                                                req->op.mod.message->elements[i].name,
1147                                        ldb_dn_get_linearized(req->op.mod.message->dn));
1148                         ret =  LDB_ERR_NO_SUCH_ATTRIBUTE;
1149                         goto fail;
1150                 }
1151
1152                 if (ldb_attr_cmp("nTSecurityDescriptor", el->name) == 0) {
1153                         uint32_t sd_flags = dsdb_request_sd_flags(req, NULL);
1154                         uint32_t access_mask = 0;
1155
1156                         if (sd_flags & (SECINFO_OWNER|SECINFO_GROUP)) {
1157                                 access_mask |= SEC_STD_WRITE_OWNER;
1158                         }
1159                         if (sd_flags & SECINFO_DACL) {
1160                                 access_mask |= SEC_STD_WRITE_DAC;
1161                         }
1162                         if (sd_flags & SECINFO_SACL) {
1163                                 access_mask |= SEC_FLAG_SYSTEM_SECURITY;
1164                         }
1165
1166                         ret = acl_check_access_on_attribute(module,
1167                                                             tmp_ctx,
1168                                                             sd,
1169                                                             sid,
1170                                                             access_mask,
1171                                                             attr,
1172                                                             objectclass);
1173                         if (ret != LDB_SUCCESS) {
1174                                 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1175                                                        "Object %s has no write dacl access\n",
1176                                                        ldb_dn_get_linearized(msg->dn));
1177                                 dsdb_acl_debug(sd,
1178                                                acl_user_token(module),
1179                                                msg->dn,
1180                                                true,
1181                                                10);
1182                                 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1183                                 goto fail;
1184                         }
1185                 } else if (ldb_attr_cmp("member", el->name) == 0) {
1186                         ret = acl_check_self_membership(tmp_ctx,
1187                                                         module,
1188                                                         req,
1189                                                         sd,
1190                                                         sid,
1191                                                         attr,
1192                                                         objectclass);
1193                         if (ret != LDB_SUCCESS) {
1194                                 goto fail;
1195                         }
1196                 } else if (ldb_attr_cmp("dBCSPwd", el->name) == 0) {
1197                         /* this one is not affected by any rights, we should let it through
1198                            so that passwords_hash returns the correct error */
1199                         continue;
1200                 } else if (ldb_attr_cmp("unicodePwd", el->name) == 0 ||
1201                            (userPassword && ldb_attr_cmp("userPassword", el->name) == 0) ||
1202                            ldb_attr_cmp("clearTextPassword", el->name) == 0) {
1203                         ret = acl_check_password_rights(tmp_ctx,
1204                                                         module,
1205                                                         req,
1206                                                         sd,
1207                                                         sid,
1208                                                         objectclass,
1209                                                         userPassword);
1210                         if (ret != LDB_SUCCESS) {
1211                                 goto fail;
1212                         }
1213                 } else if (ldb_attr_cmp("servicePrincipalName", el->name) == 0) {
1214                         ret = acl_check_spn(tmp_ctx,
1215                                             module,
1216                                             req,
1217                                             sd,
1218                                             sid,
1219                                             attr,
1220                                             objectclass);
1221                         if (ret != LDB_SUCCESS) {
1222                                 goto fail;
1223                         }
1224                 } else if (is_undelete != NULL && (ldb_attr_cmp("isDeleted", el->name) == 0)) {
1225                         /*
1226                          * in case of undelete op permissions on
1227                          * isDeleted are irrelevant and
1228                          * distinguishedName is removed by the
1229                          * tombstone_reanimate module
1230                          */
1231                         continue;
1232                 } else {
1233                         ret = acl_check_access_on_attribute(module,
1234                                                             tmp_ctx,
1235                                                             sd,
1236                                                             sid,
1237                                                             SEC_ADS_WRITE_PROP,
1238                                                             attr,
1239                                                             objectclass);
1240                         if (ret != LDB_SUCCESS) {
1241                                 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1242                                                        "Object %s has no write property access\n",
1243                                                        ldb_dn_get_linearized(msg->dn));
1244                                 dsdb_acl_debug(sd,
1245                                                acl_user_token(module),
1246                                                msg->dn,
1247                                                true,
1248                                                10);
1249                                 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1250                                 goto fail;
1251                         }
1252                 }
1253         }
1254
1255 success:
1256         talloc_free(tmp_ctx);
1257         return ldb_next_request(module, req);
1258 fail:
1259         talloc_free(tmp_ctx);
1260         return ret;
1261 }
1262
1263 /* similar to the modify for the time being.
1264  * We need to consider the special delete tree case, though - TODO */
1265 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
1266 {
1267         int ret;
1268         struct ldb_dn *parent;
1269         struct ldb_context *ldb;
1270         struct ldb_dn *nc_root;
1271         struct ldb_control *as_system;
1272         const struct dsdb_schema *schema;
1273         const struct dsdb_class *objectclass;
1274         struct security_descriptor *sd = NULL;
1275         struct dom_sid *sid = NULL;
1276         struct ldb_result *acl_res;
1277         static const char *acl_attrs[] = {
1278                 "nTSecurityDescriptor",
1279                 "objectClass",
1280                 "objectSid",
1281                 NULL
1282         };
1283
1284         if (ldb_dn_is_special(req->op.del.dn)) {
1285                 return ldb_next_request(module, req);
1286         }
1287
1288         as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1289         if (as_system != NULL) {
1290                 as_system->critical = 0;
1291         }
1292
1293         if (dsdb_module_am_system(module) || as_system) {
1294                 return ldb_next_request(module, req);
1295         }
1296
1297         DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
1298
1299         ldb = ldb_module_get_ctx(module);
1300
1301         parent = ldb_dn_get_parent(req, req->op.del.dn);
1302         if (parent == NULL) {
1303                 return ldb_oom(ldb);
1304         }
1305
1306         /* Make sure we aren't deleting a NC */
1307
1308         ret = dsdb_find_nc_root(ldb, req, req->op.del.dn, &nc_root);
1309         if (ret != LDB_SUCCESS) {
1310                 return ret;
1311         }
1312         if (ldb_dn_compare(nc_root, req->op.del.dn) == 0) {
1313                 talloc_free(nc_root);
1314                 DEBUG(10,("acl:deleting a NC\n"));
1315                 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1316                 return ldb_module_done(req, NULL, NULL,
1317                                        LDB_ERR_UNWILLING_TO_PERFORM);
1318         }
1319         talloc_free(nc_root);
1320
1321         ret = dsdb_module_search_dn(module, req, &acl_res,
1322                                     req->op.del.dn, acl_attrs,
1323                                     DSDB_FLAG_NEXT_MODULE |
1324                                     DSDB_FLAG_AS_SYSTEM |
1325                                     DSDB_SEARCH_SHOW_RECYCLED, req);
1326         /* we sould be able to find the parent */
1327         if (ret != LDB_SUCCESS) {
1328                 DEBUG(10,("acl: failed to find object %s\n",
1329                           ldb_dn_get_linearized(req->op.rename.olddn)));
1330                 return ret;
1331         }
1332
1333         ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1334         if (ret != LDB_SUCCESS) {
1335                 return ldb_operr(ldb);
1336         }
1337         if (!sd) {
1338                 return ldb_operr(ldb);
1339         }
1340
1341         schema = dsdb_get_schema(ldb, req);
1342         if (!schema) {
1343                 return ldb_operr(ldb);
1344         }
1345
1346         sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1347
1348         objectclass = dsdb_get_structural_oc_from_msg(schema, acl_res->msgs[0]);
1349         if (!objectclass) {
1350                 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1351                                  "acl_modify: Error retrieving object class for GUID.");
1352         }
1353
1354         if (ldb_request_get_control(req, LDB_CONTROL_TREE_DELETE_OID)) {
1355                 ret = acl_check_access_on_objectclass(module, req, sd, sid,
1356                                                       SEC_ADS_DELETE_TREE,
1357                                                       objectclass);
1358                 if (ret != LDB_SUCCESS) {
1359                         return ret;
1360                 }
1361
1362                 return ldb_next_request(module, req);
1363         }
1364
1365         /* First check if we have delete object right */
1366         ret = acl_check_access_on_objectclass(module, req, sd, sid,
1367                                               SEC_STD_DELETE,
1368                                               objectclass);
1369         if (ret == LDB_SUCCESS) {
1370                 return ldb_next_request(module, req);
1371         }
1372
1373         /* Nope, we don't have delete object. Lets check if we have delete
1374          * child on the parent */
1375         ret = dsdb_module_check_access_on_dn(module, req, parent,
1376                                              SEC_ADS_DELETE_CHILD,
1377                                              &objectclass->schemaIDGUID,
1378                                              req);
1379         if (ret != LDB_SUCCESS) {
1380                 return ret;
1381         }
1382
1383         return ldb_next_request(module, req);
1384 }
1385 static int acl_check_reanimate_tombstone(TALLOC_CTX *mem_ctx,
1386                                          struct ldb_module *module,
1387                                          struct ldb_request *req,
1388                                          struct ldb_dn *nc_root)
1389 {
1390         int ret;
1391         struct ldb_result *acl_res;
1392         struct security_descriptor *sd = NULL;
1393         struct dom_sid *sid = NULL;
1394         static const char *acl_attrs[] = {
1395                 "nTSecurityDescriptor",
1396                 "objectClass",
1397                 "objectSid",
1398                 NULL
1399         };
1400
1401         ret = dsdb_module_search_dn(module, mem_ctx, &acl_res,
1402                                     nc_root, acl_attrs,
1403                                     DSDB_FLAG_NEXT_MODULE |
1404                                     DSDB_FLAG_AS_SYSTEM |
1405                                     DSDB_SEARCH_SHOW_RECYCLED, req);
1406         if (ret != LDB_SUCCESS) {
1407                 DEBUG(10,("acl: failed to find object %s\n",
1408                           ldb_dn_get_linearized(nc_root)));
1409                 return ret;
1410         }
1411
1412         ret = dsdb_get_sd_from_ldb_message(mem_ctx, req, acl_res->msgs[0], &sd);
1413         sid = samdb_result_dom_sid(mem_ctx, acl_res->msgs[0], "objectSid");
1414         if (ret != LDB_SUCCESS || !sd) {
1415                 return ldb_operr(ldb_module_get_ctx(module));
1416         }
1417         return acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
1418                                         GUID_DRS_REANIMATE_TOMBSTONE,
1419                                         SEC_ADS_CONTROL_ACCESS, sid);
1420 }
1421
1422 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
1423 {
1424         int ret;
1425         struct ldb_dn *oldparent;
1426         struct ldb_dn *newparent;
1427         const struct dsdb_schema *schema;
1428         const struct dsdb_class *objectclass;
1429         const struct dsdb_attribute *attr = NULL;
1430         struct ldb_context *ldb;
1431         struct security_descriptor *sd = NULL;
1432         struct dom_sid *sid = NULL;
1433         struct ldb_result *acl_res;
1434         struct ldb_dn *nc_root;
1435         struct ldb_control *as_system;
1436         struct ldb_control *is_undelete;
1437         TALLOC_CTX *tmp_ctx;
1438         const char *rdn_name;
1439         static const char *acl_attrs[] = {
1440                 "nTSecurityDescriptor",
1441                 "objectClass",
1442                 "objectSid",
1443                 NULL
1444         };
1445
1446         if (ldb_dn_is_special(req->op.rename.olddn)) {
1447                 return ldb_next_request(module, req);
1448         }
1449
1450         as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1451         if (as_system != NULL) {
1452                 as_system->critical = 0;
1453         }
1454
1455         DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
1456         if (dsdb_module_am_system(module) || as_system) {
1457                 return ldb_next_request(module, req);
1458         }
1459
1460         ldb = ldb_module_get_ctx(module);
1461
1462         tmp_ctx = talloc_new(req);
1463         if (tmp_ctx == NULL) {
1464                 return ldb_oom(ldb);
1465         }
1466
1467         oldparent = ldb_dn_get_parent(tmp_ctx, req->op.rename.olddn);
1468         if (oldparent == NULL) {
1469                 return ldb_oom(ldb);
1470         }
1471         newparent = ldb_dn_get_parent(tmp_ctx, req->op.rename.newdn);
1472         if (newparent == NULL) {
1473                 return ldb_oom(ldb);
1474         }
1475
1476         /* Make sure we aren't renaming/moving a NC */
1477
1478         ret = dsdb_find_nc_root(ldb, req, req->op.rename.olddn, &nc_root);
1479         if (ret != LDB_SUCCESS) {
1480                 return ret;
1481         }
1482         if (ldb_dn_compare(nc_root, req->op.rename.olddn) == 0) {
1483                 talloc_free(nc_root);
1484                 DEBUG(10,("acl:renaming/moving a NC\n"));
1485                 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1486                 return ldb_module_done(req, NULL, NULL,
1487                                        LDB_ERR_UNWILLING_TO_PERFORM);
1488         }
1489
1490         /* special check for undelete operation */
1491         is_undelete = ldb_request_get_control(req, DSDB_CONTROL_RESTORE_TOMBSTONE_OID);
1492         if (is_undelete != NULL) {
1493                 is_undelete->critical = 0;
1494                 ret = acl_check_reanimate_tombstone(tmp_ctx, module, req, nc_root);
1495                 if (ret != LDB_SUCCESS) {
1496                         talloc_free(tmp_ctx);
1497                         return ret;
1498                 }
1499         }
1500         talloc_free(nc_root);
1501
1502         /* Look for the parent */
1503
1504         ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res,
1505                                     req->op.rename.olddn, acl_attrs,
1506                                     DSDB_FLAG_NEXT_MODULE |
1507                                     DSDB_FLAG_AS_SYSTEM |
1508                                     DSDB_SEARCH_SHOW_RECYCLED, req);
1509         /* we sould be able to find the parent */
1510         if (ret != LDB_SUCCESS) {
1511                 DEBUG(10,("acl: failed to find object %s\n",
1512                           ldb_dn_get_linearized(req->op.rename.olddn)));
1513                 talloc_free(tmp_ctx);
1514                 return ret;
1515         }
1516
1517         ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1518         if (ret != LDB_SUCCESS) {
1519                 talloc_free(tmp_ctx);
1520                 return ldb_operr(ldb);
1521         }
1522         if (!sd) {
1523                 talloc_free(tmp_ctx);
1524                 return ldb_operr(ldb);
1525         }
1526
1527         schema = dsdb_get_schema(ldb, acl_res);
1528         if (!schema) {
1529                 talloc_free(tmp_ctx);
1530                 return ldb_operr(ldb);
1531         }
1532
1533         sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1534
1535         objectclass = dsdb_get_structural_oc_from_msg(schema, acl_res->msgs[0]);
1536         if (!objectclass) {
1537                 talloc_free(tmp_ctx);
1538                 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1539                                  "acl_modify: Error retrieving object class for GUID.");
1540         }
1541
1542         attr = dsdb_attribute_by_lDAPDisplayName(schema, "name");
1543         if (attr == NULL) {
1544                 talloc_free(tmp_ctx);
1545                 return ldb_operr(ldb);
1546         }
1547
1548         ret = acl_check_access_on_attribute(module, tmp_ctx, sd, sid,
1549                                             SEC_ADS_WRITE_PROP,
1550                                             attr, objectclass);
1551         if (ret != LDB_SUCCESS) {
1552                 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1553                                        "Object %s has no wp on %s\n",
1554                                        ldb_dn_get_linearized(req->op.rename.olddn),
1555                                        attr->lDAPDisplayName);
1556                 dsdb_acl_debug(sd,
1557                           acl_user_token(module),
1558                           req->op.rename.olddn,
1559                           true,
1560                           10);
1561                 talloc_free(tmp_ctx);
1562                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1563         }
1564
1565         rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
1566         if (rdn_name == NULL) {
1567                 talloc_free(tmp_ctx);
1568                 return ldb_operr(ldb);
1569         }
1570
1571         attr = dsdb_attribute_by_lDAPDisplayName(schema, rdn_name);
1572         if (attr == NULL) {
1573                 talloc_free(tmp_ctx);
1574                 return ldb_operr(ldb);
1575         }
1576
1577         ret = acl_check_access_on_attribute(module, tmp_ctx, sd, sid,
1578                                             SEC_ADS_WRITE_PROP,
1579                                             attr, objectclass);
1580         if (ret != LDB_SUCCESS) {
1581                 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1582                                        "Object %s has no wp on %s\n",
1583                                        ldb_dn_get_linearized(req->op.rename.olddn),
1584                                        attr->lDAPDisplayName);
1585                 dsdb_acl_debug(sd,
1586                           acl_user_token(module),
1587                           req->op.rename.olddn,
1588                           true,
1589                           10);
1590                 talloc_free(tmp_ctx);
1591                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1592         }
1593
1594         if (ldb_dn_compare(oldparent, newparent) == 0) {
1595                 /* regular rename, not move, nothing more to do */
1596                 talloc_free(tmp_ctx);
1597                 return ldb_next_request(module, req);
1598         }
1599
1600         /* new parent should have create child */
1601         ret = dsdb_module_check_access_on_dn(module, req, newparent,
1602                                              SEC_ADS_CREATE_CHILD,
1603                                              &objectclass->schemaIDGUID, req);
1604         if (ret != LDB_SUCCESS) {
1605                 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1606                                        "acl:access_denied renaming %s",
1607                                        ldb_dn_get_linearized(req->op.rename.olddn));
1608                 talloc_free(tmp_ctx);
1609                 return ret;
1610         }
1611
1612         /* do we have delete object on the object? */
1613         /* this access is not necessary for undelete ops */
1614         if (is_undelete == NULL) {
1615                 ret = acl_check_access_on_objectclass(module, tmp_ctx, sd, sid,
1616                                                       SEC_STD_DELETE,
1617                                                       objectclass);
1618                 if (ret == LDB_SUCCESS) {
1619                         talloc_free(tmp_ctx);
1620                         return ldb_next_request(module, req);
1621                 }
1622                 /* what about delete child on the current parent */
1623                 ret = dsdb_module_check_access_on_dn(module, req, oldparent,
1624                                                      SEC_ADS_DELETE_CHILD,
1625                                                      &objectclass->schemaIDGUID,
1626                                                      req);
1627                 if (ret != LDB_SUCCESS) {
1628                         ldb_asprintf_errstring(ldb_module_get_ctx(module),
1629                                                "acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn));
1630                         talloc_free(tmp_ctx);
1631                         return ldb_module_done(req, NULL, NULL, ret);
1632                 }
1633         }
1634         talloc_free(tmp_ctx);
1635
1636         return ldb_next_request(module, req);
1637 }
1638
1639 static int acl_search_update_confidential_attrs(struct acl_context *ac,
1640                                                 struct acl_private *data)
1641 {
1642         struct dsdb_attribute *a;
1643         uint32_t n = 0;
1644
1645         if (data->acl_search) {
1646                 /*
1647                  * If acl:search is activated, the acl_read module
1648                  * protects confidential attributes.
1649                  */
1650                 return LDB_SUCCESS;
1651         }
1652
1653         if ((ac->schema == data->cached_schema_ptr) &&
1654             (ac->schema->loaded_usn == data->cached_schema_loaded_usn) &&
1655             (ac->schema->metadata_usn == data->cached_schema_metadata_usn))
1656         {
1657                 return LDB_SUCCESS;
1658         }
1659
1660         data->cached_schema_ptr = NULL;
1661         data->cached_schema_loaded_usn = 0;
1662         data->cached_schema_metadata_usn = 0;
1663         TALLOC_FREE(data->confidential_attrs);
1664
1665         if (ac->schema == NULL) {
1666                 return LDB_SUCCESS;
1667         }
1668
1669         for (a = ac->schema->attributes; a; a = a->next) {
1670                 const char **attrs = data->confidential_attrs;
1671
1672                 if (!(a->searchFlags & SEARCH_FLAG_CONFIDENTIAL)) {
1673                         continue;
1674                 }
1675
1676                 attrs = talloc_realloc(data, attrs, const char *, n + 2);
1677                 if (attrs == NULL) {
1678                         TALLOC_FREE(data->confidential_attrs);
1679                         return ldb_module_oom(ac->module);
1680                 }
1681
1682                 attrs[n] = a->lDAPDisplayName;
1683                 attrs[n+1] = NULL;
1684                 n++;
1685
1686                 data->confidential_attrs = attrs;
1687         }
1688
1689         data->cached_schema_ptr = ac->schema;
1690         data->cached_schema_loaded_usn = ac->schema->loaded_usn;
1691         data->cached_schema_metadata_usn = ac->schema->metadata_usn;
1692
1693         return LDB_SUCCESS;
1694 }
1695
1696 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1697 {
1698         struct acl_context *ac;
1699         struct acl_private *data;
1700         struct ldb_result *acl_res;
1701         static const char *acl_attrs[] = {
1702                 "objectClass",
1703                 "nTSecurityDescriptor",
1704                 "objectSid",
1705                 NULL
1706         };
1707         int ret;
1708         unsigned int i;
1709
1710         ac = talloc_get_type(req->context, struct acl_context);
1711         data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1712         if (!ares) {
1713                 return ldb_module_done(ac->req, NULL, NULL,
1714                                        LDB_ERR_OPERATIONS_ERROR);
1715         }
1716         if (ares->error != LDB_SUCCESS) {
1717                 return ldb_module_done(ac->req, ares->controls,
1718                                        ares->response, ares->error);
1719         }
1720
1721         switch (ares->type) {
1722         case LDB_REPLY_ENTRY:
1723                 if (ac->constructed_attrs) {
1724                         ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn, 
1725                                                     acl_attrs,
1726                                                     DSDB_FLAG_NEXT_MODULE |
1727                                                     DSDB_FLAG_AS_SYSTEM |
1728                                                     DSDB_SEARCH_SHOW_RECYCLED,
1729                                                     req);
1730                         if (ret != LDB_SUCCESS) {
1731                                 return ldb_module_done(ac->req, NULL, NULL, ret);
1732                         }
1733                 }
1734
1735                 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1736                         ret = acl_allowedAttributes(ac->module, ac->schema,
1737                                                     acl_res->msgs[0],
1738                                                     ares->message, ac);
1739                         if (ret != LDB_SUCCESS) {
1740                                 return ldb_module_done(ac->req, NULL, NULL, ret);
1741                         }
1742                 }
1743
1744                 if (ac->allowedChildClasses) {
1745                         ret = acl_childClasses(ac->module, ac->schema,
1746                                                acl_res->msgs[0],
1747                                                ares->message,
1748                                                "allowedChildClasses");
1749                         if (ret != LDB_SUCCESS) {
1750                                 return ldb_module_done(ac->req, NULL, NULL, ret);
1751                         }
1752                 }
1753
1754                 if (ac->allowedChildClassesEffective) {
1755                         ret = acl_childClassesEffective(ac->module, ac->schema,
1756                                                         acl_res->msgs[0],
1757                                                         ares->message, ac);
1758                         if (ret != LDB_SUCCESS) {
1759                                 return ldb_module_done(ac->req, NULL, NULL, ret);
1760                         }
1761                 }
1762
1763                 if (ac->sDRightsEffective) {
1764                         ret = acl_sDRightsEffective(ac->module,
1765                                                     acl_res->msgs[0],
1766                                                     ares->message, ac);
1767                         if (ret != LDB_SUCCESS) {
1768                                 return ldb_module_done(ac->req, NULL, NULL, ret);
1769                         }
1770                 }
1771
1772                 if (data == NULL) {
1773                         return ldb_module_send_entry(ac->req, ares->message,
1774                                                      ares->controls);
1775                 }
1776
1777                 if (ac->am_system) {
1778                         return ldb_module_send_entry(ac->req, ares->message,
1779                                                      ares->controls);
1780                 }
1781
1782                 if (data->password_attrs != NULL) {
1783                         for (i = 0; data->password_attrs[i]; i++) {
1784                                 if ((!ac->userPassword) &&
1785                                     (ldb_attr_cmp(data->password_attrs[i],
1786                                                   "userPassword") == 0))
1787                                 {
1788                                                 continue;
1789                                 }
1790
1791                                 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1792                         }
1793                 }
1794
1795                 if (ac->am_administrator) {
1796                         return ldb_module_send_entry(ac->req, ares->message,
1797                                                      ares->controls);
1798                 }
1799
1800                 ret = acl_search_update_confidential_attrs(ac, data);
1801                 if (ret != LDB_SUCCESS) {
1802                         return ret;
1803                 }
1804
1805                 if (data->confidential_attrs != NULL) {
1806                         for (i = 0; data->confidential_attrs[i]; i++) {
1807                                 ldb_msg_remove_attr(ares->message,
1808                                                     data->confidential_attrs[i]);
1809                         }
1810                 }
1811
1812                 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1813
1814         case LDB_REPLY_REFERRAL:
1815                 return ldb_module_send_referral(ac->req, ares->referral);
1816
1817         case LDB_REPLY_DONE:
1818                 return ldb_module_done(ac->req, ares->controls,
1819                                        ares->response, LDB_SUCCESS);
1820
1821         }
1822         return LDB_SUCCESS;
1823 }
1824
1825 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1826 {
1827         struct ldb_context *ldb;
1828         struct acl_context *ac;
1829         struct ldb_parse_tree *down_tree;
1830         struct ldb_request *down_req;
1831         struct acl_private *data;
1832         int ret;
1833         unsigned int i;
1834
1835         if (ldb_dn_is_special(req->op.search.base)) {
1836                 return ldb_next_request(module, req);
1837         }
1838
1839         ldb = ldb_module_get_ctx(module);
1840
1841         ac = talloc_zero(req, struct acl_context);
1842         if (ac == NULL) {
1843                 return ldb_oom(ldb);
1844         }
1845         data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1846
1847         ac->module = module;
1848         ac->req = req;
1849         ac->am_system = dsdb_module_am_system(module);
1850         ac->am_administrator = dsdb_module_am_administrator(module);
1851         ac->constructed_attrs = false;
1852         ac->modify_search = true;
1853         ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1854         ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1855         ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1856         ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1857         ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1858         ac->userPassword = true;
1859         ac->schema = dsdb_get_schema(ldb, ac);
1860
1861         ac->constructed_attrs |= ac->allowedAttributes;
1862         ac->constructed_attrs |= ac->allowedChildClasses;
1863         ac->constructed_attrs |= ac->allowedChildClassesEffective;
1864         ac->constructed_attrs |= ac->allowedAttributesEffective;
1865         ac->constructed_attrs |= ac->sDRightsEffective;
1866
1867         if (data == NULL) {
1868                 ac->modify_search = false;
1869         }
1870         if (ac->am_system) {
1871                 ac->modify_search = false;
1872         }
1873
1874         if (!ac->constructed_attrs && !ac->modify_search) {
1875                 talloc_free(ac);
1876                 return ldb_next_request(module, req);
1877         }
1878
1879         data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1880         if (data != NULL) {
1881                 ac->userPassword = data->userPassword_support;
1882         }
1883
1884         ret = acl_search_update_confidential_attrs(ac, data);
1885         if (ret != LDB_SUCCESS) {
1886                 return ret;
1887         }
1888
1889         down_tree = ldb_parse_tree_copy_shallow(ac, req->op.search.tree);
1890         if (down_tree == NULL) {
1891                 return ldb_oom(ldb);
1892         }
1893
1894         if (!ac->am_system && data->password_attrs) {
1895                 for (i = 0; data->password_attrs[i]; i++) {
1896                         if ((!ac->userPassword) &&
1897                             (ldb_attr_cmp(data->password_attrs[i],
1898                                           "userPassword") == 0))
1899                         {
1900                                 continue;
1901                         }
1902
1903                         ldb_parse_tree_attr_replace(down_tree,
1904                                                     data->password_attrs[i],
1905                                                     "kludgeACLredactedattribute");
1906                 }
1907         }
1908
1909         if (!ac->am_system && !ac->am_administrator && data->confidential_attrs) {
1910                 for (i = 0; data->confidential_attrs[i]; i++) {
1911                         ldb_parse_tree_attr_replace(down_tree,
1912                                                     data->confidential_attrs[i],
1913                                                     "kludgeACLredactedattribute");
1914                 }
1915         }
1916
1917         ret = ldb_build_search_req_ex(&down_req,
1918                                       ldb, ac,
1919                                       req->op.search.base,
1920                                       req->op.search.scope,
1921                                       down_tree,
1922                                       req->op.search.attrs,
1923                                       req->controls,
1924                                       ac, acl_search_callback,
1925                                       req);
1926         LDB_REQ_SET_LOCATION(down_req);
1927         if (ret != LDB_SUCCESS) {
1928                 return ret;
1929         }
1930         /* perform the search */
1931         return ldb_next_request(module, down_req);
1932 }
1933
1934 static int acl_extended(struct ldb_module *module, struct ldb_request *req)
1935 {
1936         struct ldb_context *ldb = ldb_module_get_ctx(module);
1937         struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1938
1939         /* allow everybody to read the sequence number */
1940         if (strcmp(req->op.extended.oid,
1941                    LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1942                 return ldb_next_request(module, req);
1943         }
1944
1945         if (dsdb_module_am_system(module) ||
1946             dsdb_module_am_administrator(module) || as_system) {
1947                 return ldb_next_request(module, req);
1948         } else {
1949                 ldb_asprintf_errstring(ldb,
1950                                        "acl_extended: "
1951                                        "attempted database modify not permitted. "
1952                                        "User %s is not SYSTEM or an administrator",
1953                                        acl_user_name(req, module));
1954                 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1955         }
1956 }
1957
1958 static const struct ldb_module_ops ldb_acl_module_ops = {
1959         .name              = "acl",
1960         .search            = acl_search,
1961         .add               = acl_add,
1962         .modify            = acl_modify,
1963         .del               = acl_delete,
1964         .rename            = acl_rename,
1965         .extended          = acl_extended,
1966         .init_context      = acl_module_init
1967 };
1968
1969 int ldb_acl_module_init(const char *version)
1970 {
1971         LDB_MODULE_CHECK_VERSION(version);
1972         return ldb_register_module(&ldb_acl_module_ops);
1973 }