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