build: avoid util.h as a public header name due to conflict with MacOS
[nivanova/samba-autobuild/.git] / source4 / dsdb / samdb / ldb_modules / extended_dn_out.c
index 6fbd0559db7f67236c45c8fc409fea14f0b8fd7e..9e1bec98a54e714b191b1bc9eaca26044c2d8d39 100644 (file)
@@ -2,7 +2,7 @@
    ldb database library
 
    Copyright (C) Simo Sorce 2005-2008
-   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007-2008
+   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007-2009
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
  */
 
 #include "includes.h"
-#include "ldb/include/ldb.h"
-#include "ldb/include/ldb_errors.h"
-#include "ldb/include/ldb_module.h"
+#include <ldb.h>
+#include <ldb_errors.h>
+#include <ldb_module.h>
+#include "libcli/security/security.h"
 #include "librpc/gen_ndr/ndr_misc.h"
+#include "librpc/gen_ndr/ndr_security.h"
 #include "librpc/ndr/libndr.h"
 #include "dsdb/samdb/samdb.h"
+#include "dsdb/samdb/ldb_modules/util.h"
 
 struct extended_dn_out_private {
        bool dereference;
        bool normalise;
        struct dsdb_openldap_dereference_control *dereference_control;
+       const char **attrs;
 };
 
-static bool is_attr_in_list(const char * const * attrs, const char *attr)
+/* Do the lazy init of the derererence control */
+
+static int extended_dn_out_dereference_setup_control(struct ldb_context *ldb, struct extended_dn_out_private *p)
 {
-       int i;
+       const struct dsdb_schema *schema;
+       struct dsdb_openldap_dereference_control *dereference_control;
+       struct dsdb_attribute *cur;
+
+       unsigned int i = 0;
+       if (p->dereference_control) {
+               return LDB_SUCCESS;
+       }
 
-       for (i = 0; attrs[i]; i++) {
-               if (ldb_attr_cmp(attrs[i], attr) == 0)
-                       return true;
+       schema = dsdb_get_schema(ldb, p);
+       if (!schema) {
+               /* No schema on this DB (yet) */
+               return LDB_SUCCESS;
        }
 
-       return false;
+       p->dereference_control = dereference_control
+               = talloc_zero(p, struct dsdb_openldap_dereference_control);
+
+       if (!p->dereference_control) {
+               return ldb_oom(ldb);
+       }
+
+       for (cur = schema->attributes; cur; cur = cur->next) {
+               if (cur->dn_format != DSDB_NORMAL_DN) {
+                       continue;
+               }
+               dereference_control->dereference
+                       = talloc_realloc(p, dereference_control->dereference,
+                                        struct dsdb_openldap_dereference *, i + 2);
+               if (!dereference_control) {
+                       return ldb_oom(ldb);
+               }
+               dereference_control->dereference[i] = talloc(dereference_control->dereference,
+                                        struct dsdb_openldap_dereference);
+               if (!dereference_control->dereference[i]) {
+                       return ldb_oom(ldb);
+               }
+               dereference_control->dereference[i]->source_attribute = cur->lDAPDisplayName;
+               dereference_control->dereference[i]->dereference_attribute = p->attrs;
+               i++;
+               dereference_control->dereference[i] = NULL;
+       }
+       return LDB_SUCCESS;
 }
 
 static char **copy_attrs(void *mem_ctx, const char * const * attrs)
 {
        char **nattrs;
-       int i, num;
+       unsigned int i, num;
 
        for (num = 0; attrs[num]; num++);
 
@@ -82,7 +123,7 @@ static char **copy_attrs(void *mem_ctx, const char * const * attrs)
 static bool add_attrs(void *mem_ctx, char ***attrs, const char *attr)
 {
        char **nattrs;
-       int num;
+       unsigned int num;
 
        for (num = 0; (*attrs)[num]; num++);
 
@@ -103,9 +144,7 @@ static bool add_attrs(void *mem_ctx, char ***attrs, const char *attr)
    cn=Adminstrator,cn=users,dc=samba,dc=example,dc=com becomes
    CN=Adminstrator,CN=users,DC=samba,DC=example,DC=com
 */
-
-
-static int fix_dn(struct ldb_dn *dn) 
+static int fix_dn(struct ldb_context *ldb, struct ldb_dn *dn)
 {
        int i, ret;
        char *upper_rdn_attr;
@@ -115,7 +154,7 @@ static int fix_dn(struct ldb_dn *dn)
                upper_rdn_attr = strupper_talloc(dn,
                                                 ldb_dn_get_component_name(dn, i));
                if (!upper_rdn_attr) {
-                       return LDB_ERR_OPERATIONS_ERROR;
+                       return ldb_oom(ldb);
                }
                
                /* And replace it with CN=foo (we need the attribute in upper case */
@@ -129,6 +168,7 @@ static int fix_dn(struct ldb_dn *dn)
        return LDB_SUCCESS;
 }
 
+
 /* Inject the extended DN components, so the DN cn=Adminstrator,cn=users,dc=samba,dc=example,dc=com becomes
    <GUID=541203ae-f7d6-47ef-8390-bfcf019f9583>;<SID=S-1-5-21-4177067393-1453636373-93818737-500>;cn=Adminstrator,cn=users,dc=samba,dc=example,dc=com */
 
@@ -143,7 +183,7 @@ static int inject_extended_dn_out(struct ldb_reply *ares,
        const DATA_BLOB *sid_blob;
 
        guid_blob = ldb_msg_find_ldb_val(ares->message, "objectGUID");
-       sid_blob = ldb_msg_find_ldb_val(ares->message, "objectSID");
+       sid_blob = ldb_msg_find_ldb_val(ares->message, "objectSid");
 
        if (!guid_blob) {
                ldb_set_errstring(ldb, "Did not find objectGUID to inject into extended DN");
@@ -166,7 +206,7 @@ static int inject_extended_dn_out(struct ldb_reply *ares,
        }
 
        if (sid_blob && remove_sid) {
-               ldb_msg_remove_attr(ares->message, "objectSID");
+               ldb_msg_remove_attr(ares->message, "objectSid");
        }
 
        return LDB_SUCCESS;
@@ -178,7 +218,7 @@ static int handle_dereference_openldap(struct ldb_dn *dn,
 {
        const struct ldb_val *entryUUIDblob, *sid_blob;
        struct ldb_message fake_msg; /* easier to use routines that expect an ldb_message */
-       int j;
+       unsigned int j;
        
        fake_msg.num_elements = 0;
                        
@@ -200,8 +240,6 @@ static int handle_dereference_openldap(struct ldb_dn *dn,
        entryUUIDblob = ldb_msg_find_ldb_val(&fake_msg, "entryUUID");
        if (entryUUIDblob) {
                NTSTATUS status;
-               enum ndr_err_code ndr_err;
-               
                struct ldb_val guid_blob;
                struct GUID guid;
                
@@ -210,18 +248,17 @@ static int handle_dereference_openldap(struct ldb_dn *dn,
                if (!NT_STATUS_IS_OK(status)) {
                        return LDB_ERR_INVALID_DN_SYNTAX;
                }
-               ndr_err = ndr_push_struct_blob(&guid_blob, NULL, NULL, &guid,
-                                              (ndr_push_flags_fn_t)ndr_push_GUID);
-               if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               status = GUID_to_ndr_blob(&guid, dn, &guid_blob);
+               if (!NT_STATUS_IS_OK(status)) {
                        return LDB_ERR_INVALID_DN_SYNTAX;
                }
                
                ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
        }
        
-       sid_blob = ldb_msg_find_ldb_val(&fake_msg, "objectSID");
+       sid_blob = ldb_msg_find_ldb_val(&fake_msg, "objectSid");
        
-       /* Look for the objectSID */
+       /* Look for the objectSid */
        if (sid_blob) {
                ldb_dn_set_extended_component(dn, "SID", sid_blob);
        }
@@ -234,7 +271,7 @@ static int handle_dereference_fds(struct ldb_dn *dn,
 {
        const struct ldb_val *nsUniqueIdBlob, *sidBlob;
        struct ldb_message fake_msg; /* easier to use routines that expect an ldb_message */
-       int j;
+       unsigned int j;
        
        fake_msg.num_elements = 0;
                        
@@ -257,8 +294,6 @@ static int handle_dereference_fds(struct ldb_dn *dn,
        nsUniqueIdBlob = ldb_msg_find_ldb_val(&fake_msg, "nsUniqueId");
        if (nsUniqueIdBlob) {
                NTSTATUS status;
-               enum ndr_err_code ndr_err;
-               
                struct ldb_val guid_blob;
                struct GUID guid;
                
@@ -267,20 +302,37 @@ static int handle_dereference_fds(struct ldb_dn *dn,
                if (!NT_STATUS_IS_OK(status)) {
                        return LDB_ERR_INVALID_DN_SYNTAX;
                }
-               ndr_err = ndr_push_struct_blob(&guid_blob, NULL, NULL, &guid,
-                                               (ndr_push_flags_fn_t)ndr_push_GUID);
-               if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               status = GUID_to_ndr_blob(&guid, dn, &guid_blob);
+               if (!NT_STATUS_IS_OK(status)) {
                        return LDB_ERR_INVALID_DN_SYNTAX;
                }
                
                ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
        }
        
-       /* Look for the objectSID */
+       /* Look for the objectSid */
 
-       sidBlob = ldb_msg_find_ldb_val(&fake_msg, "objectSID");
+       sidBlob = ldb_msg_find_ldb_val(&fake_msg, "sambaSID");
        if (sidBlob) {
-               ldb_dn_set_extended_component(dn, "SID", sidBlob);
+               enum ndr_err_code ndr_err;
+
+               struct ldb_val sid_blob;
+               struct dom_sid *sid;
+
+               sid = dom_sid_parse_length(NULL, sidBlob);
+
+               if (sid == NULL) {
+                       return LDB_ERR_INVALID_DN_SYNTAX;
+               }
+
+               ndr_err = ndr_push_struct_blob(&sid_blob, NULL, sid,
+                                               (ndr_push_flags_fn_t)ndr_push_dom_sid);
+               talloc_free(sid);
+               if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+                       return LDB_ERR_INVALID_DN_SYNTAX;
+               }
+
+               ldb_dn_set_extended_component(dn, "SID", &sid_blob);
        }
        return LDB_SUCCESS;
 }
@@ -296,6 +348,54 @@ struct extended_search_context {
        int extended_type;
 };
 
+
+/*
+   fix one-way links to have the right string DN, to cope with
+   renames of the target
+*/
+static int fix_one_way_link(struct extended_search_context *ac, struct ldb_dn *dn)
+{
+       struct GUID guid;
+       NTSTATUS status;
+       int ret;
+       struct ldb_dn *real_dn;
+
+       status = dsdb_get_extended_dn_guid(dn, &guid, "GUID");
+       if (!NT_STATUS_IS_OK(status)) {
+               /* this is a strange DN that doesn't have a GUID! just
+                  return the current DN string?? */
+               return LDB_SUCCESS;
+       }
+
+       ret = dsdb_module_dn_by_guid(ac->module, dn, &guid, &real_dn, ac->req);
+       if (ret != LDB_SUCCESS) {
+               /* it could be on another server, we need to leave the
+                  string DN alone */
+               return LDB_SUCCESS;
+       }
+
+       if (strcmp(ldb_dn_get_linearized(dn), ldb_dn_get_linearized(real_dn)) == 0) {
+               /* its already correct */
+               talloc_free(real_dn);
+               return LDB_SUCCESS;
+       }
+
+       /* fix the DN by replacing its components with those from the
+        * real DN
+        */
+       if (!ldb_dn_replace_components(dn, real_dn)) {
+               talloc_free(real_dn);
+               return ldb_operr(ldb_module_get_ctx(ac->module));
+       }
+       talloc_free(real_dn);
+
+       return LDB_SUCCESS;
+}
+
+
+/*
+  this is called to post-process the results from the search
+ */
 static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
                int (*handle_dereference)(struct ldb_dn *dn,
                                struct dsdb_openldap_dereference_result **dereference_attrs, 
@@ -304,13 +404,16 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
        struct extended_search_context *ac;
        struct ldb_control *control;
        struct dsdb_openldap_dereference_result_control *dereference_control = NULL;
-       int ret, i, j;
-       struct ldb_message *msg = ares->message;
+       int ret;
+       unsigned int i, j;
+       struct ldb_message *msg;
        struct extended_dn_out_private *p;
+       struct ldb_context *ldb;
+       bool have_reveal_control=false, checked_reveal_control=false;
 
        ac = talloc_get_type(req->context, struct extended_search_context);
        p = talloc_get_type(ldb_module_get_private(ac->module), struct extended_dn_out_private);
-
+       ldb = ldb_module_get_ctx(ac->module);
        if (!ares) {
                return ldb_module_done(ac->req, NULL, NULL,
                                        LDB_ERR_OPERATIONS_ERROR);
@@ -320,6 +423,8 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
                                        ares->response, ares->error);
        }
 
+       msg = ares->message;
+
        switch (ares->type) {
        case LDB_REPLY_REFERRAL:
                return ldb_module_send_referral(ac->req, ares->referral);
@@ -332,7 +437,7 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
        }
 
        if (p && p->normalise) {
-               ret = fix_dn(ares->message->dn);
+               ret = fix_dn(ldb, ares->message->dn);
                if (ret != LDB_SUCCESS) {
                        return ldb_module_done(ac->req, NULL, NULL, ret);
                }
@@ -341,7 +446,7 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
        if (ac->inject) {
                /* for each record returned post-process to add any derived
                   attributes that have been asked for */
-               ret = inject_extended_dn_out(ares, ldb_module_get_ctx(ac->module),
+               ret = inject_extended_dn_out(ares, ldb,
                                             ac->extended_type, ac->remove_guid,
                                             ac->remove_sid);
                if (ret != LDB_SUCCESS) {
@@ -357,12 +462,12 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
                                ret = ldb_msg_add_steal_string(ares->message, "distinguishedName", 
                                                               ldb_dn_get_extended_linearized(ares->message, ares->message->dn, ac->extended_type));
                        } else {
-                               ret = ldb_msg_add_string(ares->message, "distinguishedName", 
-                                                        ldb_dn_get_linearized(ares->message->dn));
+                               ret = ldb_msg_add_linearized_dn(ares->message,
+                                                               "distinguishedName",
+                                                               ares->message->dn);
                        }
                        if (ret != LDB_SUCCESS) {
-                               ldb_oom(ldb_module_get_ctx(ac->module));
-                               return LDB_ERR_OPERATIONS_ERROR;
+                               return ldb_oom(ldb);
                        }
                }
        }
@@ -375,9 +480,12 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
                }
        }
 
-       /* Walk the retruned elements (but only if we have a schema to interpret the list with) */
+       /* Walk the returned elements (but only if we have a schema to
+        * interpret the list with) */
        for (i = 0; ac->schema && i < msg->num_elements; i++) {
+               bool make_extended_dn;
                const struct dsdb_attribute *attribute;
+
                attribute = dsdb_attribute_by_lDAPDisplayName(ac->schema, msg->elements[i].name);
                if (!attribute) {
                        continue;
@@ -389,7 +497,7 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
                         * correct case */
                        msg->elements[i].name = talloc_strdup(msg->elements, attribute->lDAPDisplayName);
                        if (!msg->elements[i].name) {
-                               ldb_oom(ldb_module_get_ctx(ac->module));
+                               ldb_oom(ldb);
                                return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
                        }
                }
@@ -400,21 +508,68 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
                }
 
                /* Look to see if this attributeSyntax is a DN */
-               if (strcmp(attribute->attributeSyntax_oid, "2.5.5.1") != 0 &&
-                   strcmp(attribute->attributeSyntax_oid, "2.5.5.7") != 0) {
+               if (attribute->dn_format == DSDB_INVALID_DN) {
                        continue;
                }
 
+               make_extended_dn = ac->inject;
+
+               /* Always show plain DN in case of Object(OR-Name) syntax */
+               if (make_extended_dn) {
+                       make_extended_dn = (strcmp(attribute->syntax->ldap_oid, DSDB_SYNTAX_OR_NAME) != 0);
+               }
+
                for (j = 0; j < msg->elements[i].num_values; j++) {
                        const char *dn_str;
-                       struct ldb_dn *dn = ldb_dn_from_ldb_val(ac, ldb_module_get_ctx(ac->module), &msg->elements[i].values[j]);
-                       if (!dn || !ldb_dn_validate(dn)) {
+                       struct ldb_dn *dn;
+                       struct dsdb_dn *dsdb_dn = NULL;
+                       struct ldb_val *plain_dn = &msg->elements[i].values[j];         
+
+                       if (!checked_reveal_control) {
+                               have_reveal_control =
+                                       ldb_request_get_control(req, LDB_CONTROL_REVEAL_INTERNALS) != NULL;
+                               checked_reveal_control = true;
+                       }
+
+                       /* this is a fast method for detecting deleted
+                          linked attributes, working on the unparsed
+                          ldb_val */
+                       if (dsdb_dn_is_deleted_val(plain_dn) && !have_reveal_control) {
+                               /* it's a deleted linked attribute,
+                                 and we don't have the reveal control */
+                               memmove(&msg->elements[i].values[j],
+                                       &msg->elements[i].values[j+1],
+                                       (msg->elements[i].num_values-(j+1))*sizeof(struct ldb_val));
+                               msg->elements[i].num_values--;
+                               j--;
+                               continue;
+                       }
+
+
+                       dsdb_dn = dsdb_dn_parse(msg, ldb, plain_dn, attribute->syntax->ldap_oid);
+
+                       if (!dsdb_dn || !ldb_dn_validate(dsdb_dn->dn)) {
+                               ldb_asprintf_errstring(ldb, 
+                                                      "could not parse %.*s in %s on %s as a %s DN", 
+                                                      (int)plain_dn->length, plain_dn->data,
+                                                      msg->elements[i].name, ldb_dn_get_linearized(msg->dn),
+                                                      attribute->syntax->ldap_oid);
+                               talloc_free(dsdb_dn);
                                return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_INVALID_DN_SYNTAX);
                        }
+                       dn = dsdb_dn->dn;
+
+                       /* don't let users see the internal extended
+                          GUID components */
+                       if (!have_reveal_control) {
+                               const char *accept[] = { "GUID", "SID", "WKGUID", NULL };
+                               ldb_dn_extended_filter(dn, accept);
+                       }
 
                        if (p->normalise) {
-                               ret = fix_dn(dn);
+                               ret = fix_dn(ldb, dn);
                                if (ret != LDB_SUCCESS) {
+                                       talloc_free(dsdb_dn);
                                        return ldb_module_done(ac->req, NULL, NULL, ret);
                                }
                        }
@@ -433,29 +588,57 @@ static int extended_callback(struct ldb_request *req, struct ldb_reply *ares,
                                                         msg->elements[i].name,
                                                         &msg->elements[i].values[j]);
                                if (ret != LDB_SUCCESS) {
+                                       talloc_free(dsdb_dn);
+                                       return ldb_module_done(ac->req, NULL, NULL, ret);
+                               }
+                       }
+
+                       /* note that we don't fixup objectCategory as
+                          it should not be possible to move
+                          objectCategory elements in the schema */
+                       if (attribute->one_way_link &&
+                           strcasecmp(attribute->lDAPDisplayName, "objectCategory") != 0) {
+                               ret = fix_one_way_link(ac, dn);
+                               if (ret != LDB_SUCCESS) {
+                                       talloc_free(dsdb_dn);
                                        return ldb_module_done(ac->req, NULL, NULL, ret);
                                }
                        }
                        
-                       if (!ac->inject) {
-                               dn_str = talloc_steal(msg->elements[i].values, 
-                                                     ldb_dn_get_linearized(dn));
+                       if (make_extended_dn) {
+                               dn_str = dsdb_dn_get_extended_linearized(msg->elements[i].values,
+                                                                        dsdb_dn, ac->extended_type);
                        } else {
-                               dn_str = talloc_steal(msg->elements[i].values, 
-                                                     ldb_dn_get_extended_linearized(msg->elements[i].values, 
-                                                                                    dn, ac->extended_type));
+                               dn_str = dsdb_dn_get_linearized(msg->elements[i].values, 
+                                                               dsdb_dn);
                        }
+                       
                        if (!dn_str) {
-                               ldb_oom(ldb_module_get_ctx(ac->module));
+                               ldb_oom(ldb);
+                               talloc_free(dsdb_dn);
                                return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
                        }
                        msg->elements[i].values[j] = data_blob_string_const(dn_str);
-                       talloc_free(dn);
+                       talloc_free(dsdb_dn);
+               }
+               if (msg->elements[i].num_values == 0) {
+                       /* we've deleted all of the values from this
+                        * element - remove the element */
+                       memmove(&msg->elements[i],
+                               &msg->elements[i+1],
+                               (msg->num_elements-(i+1))*sizeof(struct ldb_message_element));
+                       msg->num_elements--;
+                       i--;
                }
        }
        return ldb_module_send_entry(ac->req, msg, ares->controls);
 }
 
+static int extended_callback_ldb(struct ldb_request *req, struct ldb_reply *ares)
+{
+       return extended_callback(req, ares, NULL);
+}
+
 static int extended_callback_openldap(struct ldb_request *req, struct ldb_reply *ares)
 {
        return extended_callback(req, ares, handle_dereference_openldap);
@@ -472,15 +655,21 @@ static int extended_dn_out_search(struct ldb_module *module, struct ldb_request
        struct ldb_control *control;
        struct ldb_control *storage_format_control;
        struct ldb_extended_dn_control *extended_ctrl = NULL;
-       struct ldb_control **saved_controls;
        struct extended_search_context *ac;
        struct ldb_request *down_req;
        char **new_attrs;
        const char * const *const_attrs;
+       struct ldb_context *ldb = ldb_module_get_ctx(module);
        int ret;
+       bool critical;
 
        struct extended_dn_out_private *p = talloc_get_type(ldb_module_get_private(module), struct extended_dn_out_private);
 
+       /* The schema manipulation does not apply to special DNs */
+       if (ldb_dn_is_special(req->op.search.base)) {
+               return ldb_next_request(module, req);
+       }
+
        /* check if there's an extended dn control */
        control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
        if (control && control->data) {
@@ -497,19 +686,18 @@ static int extended_dn_out_search(struct ldb_module *module, struct ldb_request
        if (!control && storage_format_control && storage_format_control->data) {
                extended_ctrl = talloc_get_type(storage_format_control->data, struct ldb_extended_dn_control);
                if (!extended_ctrl) {
-                       ldb_set_errstring(ldb_module_get_ctx(module), "extended_dn_out: extended_ctrl was of the wrong data type");
+                       ldb_set_errstring(ldb, "extended_dn_out: extended_ctrl was of the wrong data type");
                        return LDB_ERR_PROTOCOL_ERROR;
                }
        }
 
        ac = talloc_zero(req, struct extended_search_context);
        if (ac == NULL) {
-               ldb_oom(ldb_module_get_ctx(module));
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb);
        }
 
        ac->module = module;
-       ac->schema = dsdb_get_schema(ldb_module_get_ctx(module));
+       ac->schema = dsdb_get_schema(ldb, ac);
        ac->req = req;
        ac->inject = false;
        ac->remove_guid = false;
@@ -534,23 +722,22 @@ static int extended_dn_out_search(struct ldb_module *module, struct ldb_request
                        if (! is_attr_in_list(req->op.search.attrs, "objectGUID")) {
                                ac->remove_guid = true;
                        }
-                       if (! is_attr_in_list(req->op.search.attrs, "objectSID")) {
+                       if (! is_attr_in_list(req->op.search.attrs, "objectSid")) {
                                ac->remove_sid = true;
                        }
                        if (ac->remove_guid || ac->remove_sid) {
                                new_attrs = copy_attrs(ac, req->op.search.attrs);
                                if (new_attrs == NULL) {
-                                       ldb_oom(ldb_module_get_ctx(module));
-                                       return LDB_ERR_OPERATIONS_ERROR;
+                                       return ldb_oom(ldb);
                                }
 
                                if (ac->remove_guid) {
                                        if (!add_attrs(ac, &new_attrs, "objectGUID"))
-                                               return LDB_ERR_OPERATIONS_ERROR;
+                                               return ldb_operr(ldb);
                                }
                                if (ac->remove_sid) {
-                                       if (!add_attrs(ac, &new_attrs, "objectSID"))
-                                               return LDB_ERR_OPERATIONS_ERROR;
+                                       if (!add_attrs(ac, &new_attrs, "objectSid"))
+                                               return ldb_operr(ldb);
                                }
                                const_attrs = (const char * const *)new_attrs;
                        }
@@ -558,7 +745,7 @@ static int extended_dn_out_search(struct ldb_module *module, struct ldb_request
        }
 
        ret = ldb_build_search_req_ex(&down_req,
-                                     ldb_module_get_ctx(module), ac,
+                                     ldb, ac,
                                      req->op.search.base,
                                      req->op.search.scope,
                                      req->op.search.tree,
@@ -566,40 +753,47 @@ static int extended_dn_out_search(struct ldb_module *module, struct ldb_request
                                      req->controls,
                                      ac, callback,
                                      req);
+       LDB_REQ_SET_LOCATION(down_req);
        if (ret != LDB_SUCCESS) {
                return ret;
        }
 
-       /* Remove extended DN and storage format controls */
-
+       /* mark extended DN and storage format controls as done */
        if (control) {
-               /* save it locally and remove it from the list */
-               /* we do not need to replace them later as we
-                * are keeping the original req intact */
-               if (!save_controls(control, down_req, &saved_controls)) {
-                       return LDB_ERR_OPERATIONS_ERROR;
-               }
+               critical = control->critical;
+               control->critical = 0;
        }
 
        if (storage_format_control) {
-               /* save it locally and remove it from the list */
-               /* we do not need to replace them later as we
-                * are keeping the original req intact */
-               if (!save_controls(storage_format_control, down_req, &saved_controls)) {
-                       return LDB_ERR_OPERATIONS_ERROR;
-               }
+               storage_format_control->critical = 0;
        }
 
        /* Add in dereference control, if we were asked to, we are
         * using the 'dereference' mode (such as with an OpenLDAP
         * backend) and have the control prepared */
-       if (control && p && p->dereference && p->dereference_control) {
-               ret = ldb_request_add_control(down_req,
-                                             DSDB_OPENLDAP_DEREFERENCE_CONTROL,
-                                             false, p->dereference_control);
+       if (control && p && p->dereference) {
+               ret = extended_dn_out_dereference_setup_control(ldb, p);
                if (ret != LDB_SUCCESS) {
                        return ret;
                }
+
+               /* We should always have this, but before the schema
+                * is with us, things get tricky */
+               if (p->dereference_control) {
+
+                       /* This control must *not* be critical,
+                        * because if this particular request did not
+                        * return any dereferencable attributes in the
+                        * end, then OpenLDAP will reply with
+                        * unavailableCriticalExtension, rather than
+                        * just an empty return control */
+                       ret = ldb_request_add_control(down_req,
+                                                     DSDB_OPENLDAP_DEREFERENCE_CONTROL,
+                                                     false, p->dereference_control);
+                       if (ret != LDB_SUCCESS) {
+                               return ret;
+                       }
+               }
        }
 
        /* perform the search */
@@ -608,7 +802,7 @@ static int extended_dn_out_search(struct ldb_module *module, struct ldb_request
 
 static int extended_dn_out_ldb_search(struct ldb_module *module, struct ldb_request *req)
 {
-       return extended_dn_out_search(module, req, extended_callback_openldap);
+       return extended_dn_out_search(module, req, extended_callback_ldb);
 }
 
 static int extended_dn_out_openldap_search(struct ldb_module *module, struct ldb_request *req)
@@ -626,12 +820,25 @@ static int extended_dn_out_ldb_init(struct ldb_module *module)
        int ret;
 
        struct extended_dn_out_private *p = talloc(module, struct extended_dn_out_private);
+       struct dsdb_extended_dn_store_format *dn_format;
 
        ldb_module_set_private(module, p);
 
        if (!p) {
-               ldb_oom(ldb_module_get_ctx(module));
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb_module_get_ctx(module));
+       }
+
+       dn_format = talloc(p, struct dsdb_extended_dn_store_format);
+       if (!dn_format) {
+               talloc_free(p);
+               return ldb_oom(ldb_module_get_ctx(module));
+       }
+
+       dn_format->store_extended_dn_in_ldb = true;
+       ret = ldb_set_opaque(ldb_module_get_ctx(module), DSDB_EXTENDED_DN_STORE_FORMAT_OPAQUE_NAME, dn_format);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(p);
+               return ret;
        }
 
        p->dereference = false;
@@ -641,7 +848,7 @@ static int extended_dn_out_ldb_init(struct ldb_module *module)
        if (ret != LDB_SUCCESS) {
                ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
                        "extended_dn_out: Unable to register control with rootdse!\n");
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_operr(ldb_module_get_ctx(module));
        }
 
        return ldb_next_init(module);
@@ -649,22 +856,33 @@ static int extended_dn_out_ldb_init(struct ldb_module *module)
 
 static int extended_dn_out_dereference_init(struct ldb_module *module, const char *attrs[])
 {
-       int ret, i = 0;
+       int ret;
        struct extended_dn_out_private *p = talloc_zero(module, struct extended_dn_out_private);
-       struct dsdb_openldap_dereference_control *dereference_control;
-       struct dsdb_attribute *cur;
-
-       struct dsdb_schema *schema;
+       struct dsdb_extended_dn_store_format *dn_format;
 
        ldb_module_set_private(module, p);
 
        if (!p) {
-               ldb_oom(ldb_module_get_ctx(module));
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_module_oom(module);
+       }
+
+       dn_format = talloc(p, struct dsdb_extended_dn_store_format);
+       if (!dn_format) {
+               talloc_free(p);
+               return ldb_module_oom(module);
+       }
+
+       dn_format->store_extended_dn_in_ldb = false;
+
+       ret = ldb_set_opaque(ldb_module_get_ctx(module), DSDB_EXTENDED_DN_STORE_FORMAT_OPAQUE_NAME, dn_format);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(p);
+               return ret;
        }
 
        p->dereference = true;
 
+       p->attrs = attrs;
        /* At the moment, servers that need dereference also need the
         * DN and attribute names to be normalised */
        p->normalise = true;
@@ -672,61 +890,18 @@ static int extended_dn_out_dereference_init(struct ldb_module *module, const cha
        ret = ldb_mod_register_control(module, LDB_CONTROL_EXTENDED_DN_OID);
        if (ret != LDB_SUCCESS) {
                ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
-                       "extended_dn_out: Unable to register control with rootdse!\n");
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-
-       ret = ldb_next_init(module);
-
-       if (ret != LDB_SUCCESS) {
-               return ret;
+                         "extended_dn_out: Unable to register control with rootdse!\n");
+               return ldb_operr(ldb_module_get_ctx(module));
        }
 
-       schema = dsdb_get_schema(ldb_module_get_ctx(module));
-       if (!schema) {
-               /* No schema on this DB (yet) */
-               return LDB_SUCCESS;
-       }
-
-       p->dereference_control = dereference_control
-               = talloc_zero(p, struct dsdb_openldap_dereference_control);
-
-       if (!p->dereference_control) {
-               ldb_oom(ldb_module_get_ctx(module));
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-       
-       for (cur = schema->attributes; cur; cur = cur->next) {
-               if (strcmp(cur->syntax->attributeSyntax_oid, "2.5.5.1") != 0 &&
-                   strcmp(cur->syntax->attributeSyntax_oid, "2.5.5.7") != 0) {
-                       continue;
-               }
-               dereference_control->dereference
-                       = talloc_realloc(p, dereference_control->dereference,
-                                        struct dsdb_openldap_dereference *, i + 2);
-               if (!dereference_control) {
-                       ldb_oom(ldb_module_get_ctx(module));
-                       return LDB_ERR_OPERATIONS_ERROR;
-               }
-               dereference_control->dereference[i] = talloc(dereference_control->dereference,  
-                                        struct dsdb_openldap_dereference);
-               if (!dereference_control->dereference[i]) {
-                       ldb_oom(ldb_module_get_ctx(module));
-                       return LDB_ERR_OPERATIONS_ERROR;
-               }
-               dereference_control->dereference[i]->source_attribute = cur->lDAPDisplayName;
-               dereference_control->dereference[i]->dereference_attribute = attrs;
-               i++;
-               dereference_control->dereference[i] = NULL;
-       }
-       return LDB_SUCCESS;
+       return ldb_next_init(module);
 }
 
 static int extended_dn_out_openldap_init(struct ldb_module *module)
 {
        static const char *attrs[] = {
                "entryUUID",
-               "objectSID",
+               "objectSid",
                NULL
        };
 
@@ -737,27 +912,49 @@ static int extended_dn_out_fds_init(struct ldb_module *module)
 {
        static const char *attrs[] = {
                "nsUniqueId",
-               "objectSID",
+               "sambaSID",
                NULL
        };
 
        return extended_dn_out_dereference_init(module, attrs);
 }
 
-_PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_ldb_module_ops = {
+static const struct ldb_module_ops ldb_extended_dn_out_ldb_module_ops = {
        .name              = "extended_dn_out_ldb",
        .search            = extended_dn_out_ldb_search,
        .init_context      = extended_dn_out_ldb_init,
 };
 
-_PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_openldap_module_ops = {
+static const struct ldb_module_ops ldb_extended_dn_out_openldap_module_ops = {
        .name              = "extended_dn_out_openldap",
        .search            = extended_dn_out_openldap_search,
        .init_context      = extended_dn_out_openldap_init,
 };
 
-_PUBLIC_ const struct ldb_module_ops ldb_extended_dn_out_fds_module_ops = {
+static const struct ldb_module_ops ldb_extended_dn_out_fds_module_ops = {
        .name              = "extended_dn_out_fds",
        .search            = extended_dn_out_fds_search,
        .init_context      = extended_dn_out_fds_init,
 };
+
+/*
+  initialise the module
+ */
+_PUBLIC_ int ldb_extended_dn_out_module_init(const char *version)
+{
+       int ret;
+       LDB_MODULE_CHECK_VERSION(version);
+       ret = ldb_register_module(&ldb_extended_dn_out_ldb_module_ops);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       ret = ldb_register_module(&ldb_extended_dn_out_openldap_module_ops);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       ret = ldb_register_module(&ldb_extended_dn_out_fds_module_ops);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       return LDB_SUCCESS;
+}