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