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