s4:dsdb Fix up after the MAP_ constants became LDB_MAP_
[ira/wip.git] / source4 / dsdb / samdb / ldb_modules / simple_ldap_map.c
index 970106787be825342271366086823c5e4ae9e5d7..2ab448662aa4d0a61ff3ef5a1ccf669833f54fa5 100644 (file)
 */
 
 #include "includes.h"
-#include "ldb/include/ldb.h"
-#include "ldb/include/ldb_private.h"
-#include "ldb/include/ldb_errors.h"
+#include "ldb/include/ldb_module.h"
 #include "ldb/ldb_map/ldb_map.h"
 
 #include "librpc/gen_ndr/ndr_misc.h"
 #include "librpc/ndr/libndr.h"
+#include "dsdb/samdb/samdb.h"
+#include "../../../lib/ldb/include/ldb_handlers.h"
 
 struct entryuuid_private {
+       struct ldb_context *ldb;
        struct ldb_dn **base_dns;
 };
 
 static struct ldb_val encode_guid(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
 {
        struct GUID guid;
-       NTSTATUS status = GUID_from_string((char *)val->data, &guid);
+       NTSTATUS status = GUID_from_data_blob(val, &guid);
        enum ndr_err_code ndr_err;
        struct ldb_val out = data_blob(NULL, 0);
 
@@ -60,27 +61,13 @@ static struct ldb_val encode_guid(struct ldb_module *module, TALLOC_CTX *ctx, co
 
 static struct ldb_val guid_always_string(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
 {
-       struct GUID *guid;
        struct ldb_val out = data_blob(NULL, 0);
-       if (val->length >= 32 && val->data[val->length] == '\0') {
-               ldb_handler_copy(module->ldb, ctx, val, &out);
-       } else {
-               enum ndr_err_code ndr_err;
-
-               guid = talloc(ctx, struct GUID);
-               if (guid == NULL) {
-                       return out;
-               }
-               ndr_err = ndr_pull_struct_blob(val, guid, NULL, guid,
-                                              (ndr_pull_flags_fn_t)ndr_pull_GUID);
-               if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-                       talloc_free(guid);
-                       return out;
-               }
-               out = data_blob_string_const(GUID_string(ctx, guid));
-               talloc_free(guid);
+       struct GUID guid;
+       NTSTATUS status = GUID_from_data_blob(val, &guid);
+       if (!NT_STATUS_IS_OK(status)) {
+               return out;
        }
-       return out;
+       return data_blob_string_const(GUID_string(ctx, &guid));
 }
 
 static struct ldb_val encode_ns_guid(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
@@ -105,34 +92,19 @@ static struct ldb_val encode_ns_guid(struct ldb_module *module, TALLOC_CTX *ctx,
 static struct ldb_val guid_ns_string(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
 {
        struct ldb_val out = data_blob(NULL, 0);
-       if (val->length >= 32 && val->data[val->length] == '\0') {
-               struct GUID guid;
-               GUID_from_string((char *)val->data, &guid);
-               out = data_blob_string_const(NS_GUID_string(ctx, &guid));
-       } else {
-               enum ndr_err_code ndr_err;
-               struct GUID *guid_p;
-               guid_p = talloc(ctx, struct GUID);
-               if (guid_p == NULL) {
-                       return out;
-               }
-               ndr_err = ndr_pull_struct_blob(val, guid_p, NULL, guid_p,
-                                              (ndr_pull_flags_fn_t)ndr_pull_GUID);
-               if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-                       talloc_free(guid_p);
-                       return out;
-               }
-               out = data_blob_string_const(NS_GUID_string(ctx, guid_p));
-               talloc_free(guid_p);
+       struct GUID guid;
+       NTSTATUS status = GUID_from_data_blob(val, &guid);
+       if (!NT_STATUS_IS_OK(status)) {
+               return out;
        }
-       return out;
+       return data_blob_string_const(NS_GUID_string(ctx, &guid));
 }
 
 /* The backend holds binary sids, so just copy them back */
 static struct ldb_val val_copy(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
 {
        struct ldb_val out = data_blob(NULL, 0);
-       ldb_handler_copy(module->ldb, ctx, val, &out);
+       out = ldb_val_dup(ctx, val);
 
        return out;
 }
@@ -140,23 +112,52 @@ static struct ldb_val val_copy(struct ldb_module *module, TALLOC_CTX *ctx, const
 /* Ensure we always convert sids into binary, so the backend doesn't have to know about both forms */
 static struct ldb_val sid_always_binary(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
 {
+       struct ldb_context *ldb = ldb_module_get_ctx(module);
        struct ldb_val out = data_blob(NULL, 0);
-       const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(module->ldb, "objectSid");
+       const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, "objectSid");
 
-       if (a->syntax->canonicalise_fn(module->ldb, ctx, val, &out) != LDB_SUCCESS) {
+       if (a->syntax->canonicalise_fn(ldb, ctx, val, &out) != LDB_SUCCESS) {
                return data_blob(NULL, 0);
        }
 
        return out;
 }
 
+/* Ensure we always convert sids into string, so the backend doesn't have to know about both forms */
+static struct ldb_val sid_always_string(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
+{
+       struct ldb_context *ldb = ldb_module_get_ctx(module);
+       struct ldb_val out = data_blob(NULL, 0);
+
+       if (ldif_comparision_objectSid_isString(val)) {
+               if (ldb_handler_copy(ldb, ctx, val, &out) != LDB_SUCCESS) {
+                       return data_blob(NULL, 0);
+               }
+
+       } else {
+               if (ldif_write_objectSid(ldb, ctx, val, &out) != LDB_SUCCESS) {
+                       return data_blob(NULL, 0);
+               }
+       }
+       return out;
+}
+
 /* Ensure we always convert objectCategory into a DN */
 static struct ldb_val objectCategory_always_dn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
 {
+       struct ldb_context *ldb = ldb_module_get_ctx(module);
+       struct ldb_dn *dn;
        struct ldb_val out = data_blob(NULL, 0);
-       const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(module->ldb, "objectCategory");
+       const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, "objectCategory");
+
+       dn = ldb_dn_from_ldb_val(ctx, ldb, val);
+       if (dn && ldb_dn_validate(dn)) {
+               talloc_free(dn);
+               return val_copy(module, ctx, val);
+       }
+       talloc_free(dn);
 
-       if (a->syntax->canonicalise_fn(module->ldb, ctx, val, &out) != LDB_SUCCESS) {
+       if (a->syntax->canonicalise_fn(ldb, ctx, val, &out) != LDB_SUCCESS) {
                return data_blob(NULL, 0);
        }
 
@@ -165,19 +166,12 @@ static struct ldb_val objectCategory_always_dn(struct ldb_module *module, TALLOC
 
 static struct ldb_val normalise_to_signed32(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
 {
-       long long int signed_ll = strtoll((const char *)val->data, NULL, 10);
-       if (signed_ll >= 0x80000000LL) {
-               union {
-                       int32_t signed_int;
-                       uint32_t unsigned_int;
-               } u = {
-                       .unsigned_int = strtoul((const char *)val->data, NULL, 10)
-               };
-
-               struct ldb_val out = data_blob_string_const(talloc_asprintf(ctx, "%d", u.signed_int));
-               return out;
-       }
-       return val_copy(module, ctx, val);
+       struct ldb_val out;
+       /* We've to use "strtoll" here to have the intended overflows.
+        * Otherwise we may get "LONG_MAX" and the conversion is wrong. */
+       int32_t i = (int32_t) strtoll((char *)val->data, NULL, 0);
+       out = data_blob_string_const(talloc_asprintf(ctx, "%d", i));
+       return out;
 }
 
 static struct ldb_val usn_to_entryCSN(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
@@ -259,7 +253,7 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        /* objectGUID */
        {
                .local_name = "objectGUID",
-               .type = MAP_CONVERT,
+               .type = LDB_MAP_CONVERT,
                .u = {
                        .convert = {
                                .remote_name = "entryUUID", 
@@ -271,7 +265,7 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        /* invocationId */
        {
                .local_name = "invocationId",
-               .type = MAP_CONVERT,
+               .type = LDB_MAP_CONVERT,
                .u = {
                        .convert = {
                                .remote_name = "invocationId", 
@@ -283,7 +277,7 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        /* objectSid */
        {
                .local_name = "objectSid",
-               .type = MAP_CONVERT,
+               .type = LDB_MAP_CONVERT,
                .u = {
                        .convert = {
                                .remote_name = "objectSid", 
@@ -294,7 +288,7 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        },
        {
                .local_name = "name",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "samba4RDN"
@@ -303,7 +297,7 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        },
        {
                .local_name = "whenCreated",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "createTimestamp"
@@ -312,7 +306,7 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        },
        {
                .local_name = "whenChanged",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "modifyTimestamp"
@@ -321,7 +315,7 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        },
        {
                .local_name = "objectClasses",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "samba4ObjectClasses"
@@ -330,7 +324,7 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        },
        {
                .local_name = "dITContentRules",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "samba4DITContentRules"
@@ -339,25 +333,16 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        },
        {
                .local_name = "attributeTypes",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "samba4AttributeTypes"
                         }
                }
        },
-       {
-               .local_name = "sambaPassword",
-               .type = MAP_RENAME,
-               .u = {
-                       .rename = {
-                                .remote_name = "userPassword"
-                        }
-               }
-       },
        {
                .local_name = "objectCategory",
-               .type = MAP_CONVERT,
+               .type = LDB_MAP_CONVERT,
                .u = {
                        .convert = {
                                .remote_name = "objectCategory", 
@@ -368,38 +353,71 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        },
        {
                .local_name = "distinguishedName",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "entryDN"
                         }
                }
        },
+       {
+               .local_name = "primaryGroupID",
+               .type = LDB_MAP_CONVERT,
+               .u = {
+                       .convert = {
+                                .remote_name = "primaryGroupID",
+                                .convert_local = normalise_to_signed32,
+                                .convert_remote = val_copy,
+                       }
+               }
+       },
        {
                .local_name = "groupType",
-               .type = MAP_CONVERT,
+               .type = LDB_MAP_CONVERT,
                .u = {
                        .convert = {
                                 .remote_name = "groupType",
                                 .convert_local = normalise_to_signed32,
                                 .convert_remote = val_copy,
-                        },
+                        }
+               }
+       },
+       {
+               .local_name = "userAccountControl",
+               .type = LDB_MAP_CONVERT,
+               .u = {
+                       .convert = {
+                                .remote_name = "userAccountControl",
+                                .convert_local = normalise_to_signed32,
+                                .convert_remote = val_copy,
+                        }
                }
        },
        {
                .local_name = "sAMAccountType",
-               .type = MAP_CONVERT,
+               .type = LDB_MAP_CONVERT,
                .u = {
                        .convert = {
                                 .remote_name = "sAMAccountType",
                                 .convert_local = normalise_to_signed32,
                                 .convert_remote = val_copy,
-                        },
+                        }
+               }
+       },
+       {
+               .local_name = "systemFlags",
+               .type = LDB_MAP_CONVERT,
+               .u = {
+                       .convert = {
+                                .remote_name = "systemFlags",
+                                .convert_local = normalise_to_signed32,
+                                .convert_remote = val_copy,
+                        }
                }
        },
        {
                .local_name = "usnChanged",
-               .type = MAP_CONVERT,
+               .type = LDB_MAP_CONVERT,
                .u = {
                        .convert = {
                                 .remote_name = "entryCSN",
@@ -410,7 +428,7 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        },
        {
                .local_name = "usnCreated",
-               .type = MAP_CONVERT,
+               .type = LDB_MAP_CONVERT,
                .u = {
                        .convert = {
                                 .remote_name = "createTimestamp",
@@ -421,7 +439,7 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        },
        {
                .local_name = "*",
-               .type = MAP_KEEP,
+               .type = LDB_MAP_KEEP,
        },
        {
                .local_name = NULL,
@@ -448,6 +466,7 @@ static const char * const entryuuid_wildcard_attributes[] = {
        "whenChanged",
        "usnCreated",
        "usnChanged",
+       "memberOf",
        NULL
 };
 
@@ -456,30 +475,30 @@ static const struct ldb_map_attribute nsuniqueid_attributes[] =
        /* objectGUID */
        {
                .local_name = "objectGUID",
-               .type = MAP_CONVERT,
+               .type = LDB_MAP_CONVERT,
                .u = {
                        .convert = {
                                .remote_name = "nsuniqueid", 
                                .convert_local = guid_ns_string,
                                .convert_remote = encode_ns_guid,
-                       },
-               },
+                       }
+               }
        },
        /* objectSid */ 
        {
                .local_name = "objectSid",
-               .type = MAP_CONVERT,
+               .type = LDB_MAP_CONVERT,
                .u = {
                        .convert = {
-                               .remote_name = "objectSid", 
-                               .convert_local = sid_always_binary,
-                               .convert_remote = val_copy,
-                       },
-               },
+                               .remote_name = "sambaSID", 
+                               .convert_local = sid_always_string,
+                               .convert_remote = sid_always_binary,
+                       }
+               }
        },
        {
                .local_name = "whenCreated",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "createTimestamp"
@@ -488,95 +507,276 @@ static const struct ldb_map_attribute nsuniqueid_attributes[] =
        },
        {
                .local_name = "whenChanged",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "modifyTimestamp"
                         }
                }
        },
-       {
-               .local_name = "sambaPassword",
-               .type = MAP_RENAME,
-               .u = {
-                       .rename = {
-                                .remote_name = "userPassword"
-                        }
-               }
-       },
        {
                .local_name = "objectCategory",
-               .type = MAP_CONVERT,
+               .type = LDB_MAP_CONVERT,
                .u = {
                        .convert = {
                                .remote_name = "objectCategory", 
                                .convert_local = objectCategory_always_dn,
                                .convert_remote = val_copy,
-                       },
-               },
+                       }
+               }
        },
        {
                .local_name = "distinguishedName",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "entryDN"
                         }
                }
        },
+       {
+               .local_name = "primaryGroupID",
+               .type = LDB_MAP_CONVERT,
+               .u = {
+                       .convert = {
+                                .remote_name = "primaryGroupID",
+                                .convert_local = normalise_to_signed32,
+                                .convert_remote = val_copy,
+                       }
+               }
+       },
        {
                .local_name = "groupType",
-               .type = MAP_CONVERT,
+               .type = LDB_MAP_CONVERT,
                .u = {
                        .convert = {
-                                .remote_name = "groupType",
+                                .remote_name = "sambaGroupType",
                                 .convert_local = normalise_to_signed32,
                                 .convert_remote = val_copy,
-                        },
+                        }
+               }
+       },
+       {
+               .local_name = "userAccountControl",
+               .type = LDB_MAP_CONVERT,
+               .u = {
+                       .convert = {
+                                .remote_name = "userAccountControl",
+                                .convert_local = normalise_to_signed32,
+                                .convert_remote = val_copy,
+                        }
                }
        },
        {
                .local_name = "sAMAccountType",
-               .type = MAP_CONVERT,
+               .type = LDB_MAP_CONVERT,
                .u = {
                        .convert = {
                                 .remote_name = "sAMAccountType",
                                 .convert_local = normalise_to_signed32,
                                 .convert_remote = val_copy,
-                        },
+                        }
+               }
+       },
+       {
+               .local_name = "systemFlags",
+               .type = LDB_MAP_CONVERT,
+               .u = {
+                       .convert = {
+                                .remote_name = "systemFlags",
+                                .convert_local = normalise_to_signed32,
+                                .convert_remote = val_copy,
+                        }
                }
        },
        {
                .local_name = "usnChanged",
-               .type = MAP_CONVERT,
+               .type = LDB_MAP_CONVERT,
                .u = {
                        .convert = {
                                 .remote_name = "modifyTimestamp",
                                 .convert_local = usn_to_timestamp,
                                 .convert_remote = timestamp_to_usn,
-                        },
-               },
+                        }
+               }
        },
        {
                .local_name = "usnCreated",
-               .type = MAP_CONVERT,
+               .type = LDB_MAP_CONVERT,
                .u = {
                        .convert = {
                                 .remote_name = "createTimestamp",
                                 .convert_local = usn_to_timestamp,
                                 .convert_remote = timestamp_to_usn,
-                        },
-               },
+                        }
+               }
+       },
+       {
+               .local_name = "unixHomeDirectory",
+               .type = LDB_MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "homeDirectory"
+                        }
+               }
+       },
+       {
+               .local_name = "pwdLastSet",
+               .type = LDB_MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaPwdLastSet"
+                        }
+               }
+       },
+       {
+               .local_name = "lastLogon",
+               .type = LDB_MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaLogonTime"
+                        }
+               }
+       },
+       {
+               .local_name = "lastLogoff",
+               .type = LDB_MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaLogoffTime"
+                        }
+               }
+       },
+       {
+               .local_name = "badPwdCount",
+               .type = LDB_MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaBadPasswordCount"
+                        }
+               }
+       },
+       {
+               .local_name = "logonHours",
+               .type = LDB_MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaLogonHours"
+                        }
+               }
+       },
+       {
+               .local_name = "homeDrive",
+               .type = LDB_MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaHomeDrive"
+                        }
+               }
+       },
+       {
+               .local_name = "scriptPath",
+               .type = LDB_MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaLogonScript"
+                        }
+               }
+       },
+       {
+               .local_name = "profilePath",
+               .type = LDB_MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaProfilePath"
+                        }
+               }
+       },
+       {
+               .local_name = "userWorkstations",
+               .type = LDB_MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaUserWorkstations"
+                        }
+               }
+       },
+       {
+               .local_name = "homeDirectory",
+               .type = LDB_MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaHomePath"
+                        }
+               }
+       },
+       {
+               .local_name = "nextRid",
+               .type = LDB_MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaNextRid"
+                        }
+               }
+       },
+       {
+               .local_name = "privilegeDisplayName",
+               .type = LDB_MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaPrivName"
+                        }
+               }
        },
        {
                .local_name = "*",
-               .type = MAP_KEEP,
+               .type = LDB_MAP_KEEP,
        },
        {
                .local_name = NULL,
        }
 };
 
+/* This objectClass conflicts with builtin classes on FDS */
+const struct ldb_map_objectclass nsuniqueid_objectclasses[] =
+{
+       {
+               .local_name = "domain",
+               .remote_name = "samba4Domain"
+       },
+       {
+               .local_name = "rFC822LocalPart",
+               .remote_name = "samba4RFC822LocalPart"
+       },
+       {
+               .local_name = "mailRecipient",
+               .remote_name = "samba4MailRecipient"
+       },
+       {
+               .local_name = "nisMap",
+               .remote_name = "samba4NisMap"
+       },
+       {
+               .local_name = "person",
+               .remote_name = "samba4Person"
+       },
+       {
+               .local_name = "organizationalPerson",
+               .remote_name = "samba4OrganizationalPerson"
+       },
+       {
+               .local_name = "residentialPerson",
+               .remote_name = "samba4ResidentialPerson"
+       },
+       {
+               .local_name = "inetOrgPerson",
+               .remote_name = "samba4InetOrgPerson"
+       },
+       {
+               .local_name = NULL
+       }
+};
+
 /* These things do not show up in wildcard searches in OpenLDAP, but
  * we need them to show up in the AD-like view */
 static const char * const nsuniqueid_wildcard_attributes[] = {
@@ -588,96 +788,14 @@ static const char * const nsuniqueid_wildcard_attributes[] = {
        NULL
 };
 
-static int get_remote_rootdse(struct ldb_context *ldb, void *context, 
-                      struct ldb_reply *ares) 
-{
-       struct entryuuid_private *entryuuid_private;
-       entryuuid_private = talloc_get_type(context,
-                                           struct entryuuid_private);
-       if (ares->type == LDB_REPLY_ENTRY) {
-               int i;
-               struct ldb_message_element *el = ldb_msg_find_element(ares->message, "namingContexts");
-               entryuuid_private->base_dns = talloc_realloc(entryuuid_private, entryuuid_private->base_dns, struct ldb_dn *, 
-                                                            el->num_values + 1);
-               for (i=0; i < el->num_values; i++) {
-                       if (!entryuuid_private->base_dns) {
-                               return LDB_ERR_OPERATIONS_ERROR;
-                       }
-                       entryuuid_private->base_dns[i] = ldb_dn_new(entryuuid_private->base_dns, ldb, (const char *)el->values[i].data);
-                       if ( ! ldb_dn_validate(entryuuid_private->base_dns[i])) {
-                               return LDB_ERR_OPERATIONS_ERROR;
-                       }
-               }
-               entryuuid_private->base_dns[i] = NULL;
-       }
-
-       return LDB_SUCCESS;
-}
-
-static int find_base_dns(struct ldb_module *module, 
-                         struct entryuuid_private *entryuuid_private) 
-{
-       int ret;
-       struct ldb_request *req;
-       const char *naming_context_attr[] = {
-               "namingContexts",
-               NULL
-       };
-       req = talloc(entryuuid_private, struct ldb_request);
-       if (req == NULL) {
-               ldb_set_errstring(module->ldb, "Out of Memory");
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-
-       req->operation = LDB_SEARCH;
-       req->op.search.base = ldb_dn_new(req, module->ldb, NULL);
-       req->op.search.scope = LDB_SCOPE_BASE;
-
-       req->op.search.tree = ldb_parse_tree(req, "objectClass=*");
-       if (req->op.search.tree == NULL) {
-               ldb_set_errstring(module->ldb, "Unable to parse search expression");
-               talloc_free(req);
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-
-       req->op.search.attrs = naming_context_attr;
-       req->controls = NULL;
-       req->context = entryuuid_private;
-       req->callback = get_remote_rootdse;
-       ldb_set_timeout(module->ldb, req, 0); /* use default timeout */
-
-       ret = ldb_next_request(module, req);
-       
-       if (ret == LDB_SUCCESS) {
-               ret = ldb_wait(req->handle, LDB_WAIT_ALL);
-       }
-       
-       talloc_free(req);
-       if (ret != LDB_SUCCESS) {
-               return ret;
-       }
-
-       return LDB_SUCCESS;
-}
-
 /* the context init function */
 static int entryuuid_init(struct ldb_module *module)
 {
         int ret;
-       struct map_private *map_private;
-       struct entryuuid_private *entryuuid_private;
-
-       ret = ldb_map_init(module, entryuuid_attributes, entryuuid_objectclasses, entryuuid_wildcard_attributes, NULL);
+       ret = ldb_map_init(module, entryuuid_attributes, entryuuid_objectclasses, entryuuid_wildcard_attributes, "samba4Top", NULL);
         if (ret != LDB_SUCCESS)
                 return ret;
 
-       map_private = talloc_get_type(module->private_data, struct map_private);
-
-       entryuuid_private = talloc_zero(map_private, struct entryuuid_private);
-       map_private->caller_private = entryuuid_private;
-
-       ret = find_base_dns(module, entryuuid_private);
-
        return ldb_next_init(module);
 }
 
@@ -685,149 +803,153 @@ static int entryuuid_init(struct ldb_module *module)
 static int nsuniqueid_init(struct ldb_module *module)
 {
         int ret;
-       struct map_private *map_private;
-       struct entryuuid_private *entryuuid_private;
-
-       ret = ldb_map_init(module, nsuniqueid_attributes, NULL, nsuniqueid_wildcard_attributes, NULL);
+       ret = ldb_map_init(module, nsuniqueid_attributes, nsuniqueid_objectclasses, nsuniqueid_wildcard_attributes, "extensibleObject", NULL);
         if (ret != LDB_SUCCESS)
                 return ret;
 
-       map_private = talloc_get_type(module->private_data, struct map_private);
-
-       entryuuid_private = talloc_zero(map_private, struct entryuuid_private);
-       map_private->caller_private = entryuuid_private;
-
-       ret = find_base_dns(module, entryuuid_private);
-
        return ldb_next_init(module);
 }
 
-static int get_seq(struct ldb_context *ldb, void *context, 
-                  struct ldb_reply *ares) 
+static int get_seq_callback(struct ldb_request *req,
+                           struct ldb_reply *ares)
 {
-       unsigned long long *max_seq = (unsigned long long *)context;
-       unsigned long long seq;
+       unsigned long long *seq = (unsigned long long *)req->context;
+
+       if (!ares) {
+               return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
+       }
+       if (ares->error != LDB_SUCCESS) {
+               return ldb_request_done(req, ares->error);
+       }
+
        if (ares->type == LDB_REPLY_ENTRY) {
                struct ldb_message_element *el = ldb_msg_find_element(ares->message, "contextCSN");
                if (el) {
-                       seq = entryCSN_to_usn_int(ares, &el->values[0]);
-                       *max_seq = MAX(seq, *max_seq);
+                       *seq = entryCSN_to_usn_int(ares, &el->values[0]);
                }
        }
 
+       if (ares->type == LDB_REPLY_DONE) {
+               return ldb_request_done(req, LDB_SUCCESS);
+       }
+
+       talloc_free(ares);
        return LDB_SUCCESS;
 }
 
 static int entryuuid_sequence_number(struct ldb_module *module, struct ldb_request *req)
 {
-       int i, ret;
+       struct ldb_context *ldb;
+       int ret;
        struct map_private *map_private;
        struct entryuuid_private *entryuuid_private;
-       unsigned long long max_seq = 0;
+       unsigned long long seq_num = 0;
        struct ldb_request *search_req;
-       map_private = talloc_get_type(module->private_data, struct map_private);
+
+       const struct ldb_control *partition_ctrl;
+       const struct dsdb_control_current_partition *partition;
+       static const char *contextCSN_attr[] = {
+               "contextCSN", NULL
+       };
+
+       struct ldb_seqnum_request *seq;
+       struct ldb_seqnum_result *seqr;
+       struct ldb_extended *ext;
+
+       ldb = ldb_module_get_ctx(module);
+
+       seq = talloc_get_type(req->op.extended.data, struct ldb_seqnum_request);
+
+       map_private = talloc_get_type(ldb_module_get_private(module), struct map_private);
 
        entryuuid_private = talloc_get_type(map_private->caller_private, struct entryuuid_private);
 
-       /* Search the baseDNs for a sequence number */
-       for (i=0; entryuuid_private && 
-                    entryuuid_private->base_dns && 
-                    entryuuid_private->base_dns[i];
-               i++) {
-               static const char *contextCSN_attr[] = {
-                       "contextCSN", NULL
-               };
-               search_req = talloc(req, struct ldb_request);
-               if (search_req == NULL) {
-                       ldb_set_errstring(module->ldb, "Out of Memory");
-                       return LDB_ERR_OPERATIONS_ERROR;
-               }
-               
-               search_req->operation = LDB_SEARCH;
-               search_req->op.search.base = entryuuid_private->base_dns[i];
-               search_req->op.search.scope = LDB_SCOPE_BASE;
-               
-               search_req->op.search.tree = ldb_parse_tree(search_req, "objectClass=*");
-               if (search_req->op.search.tree == NULL) {
-                       ldb_set_errstring(module->ldb, "Unable to parse search expression");
-                       talloc_free(search_req);
-                       return LDB_ERR_OPERATIONS_ERROR;
-               }
-               
-               search_req->op.search.attrs = contextCSN_attr;
-               search_req->controls = NULL;
-               search_req->context = &max_seq;
-               search_req->callback = get_seq;
-               ldb_set_timeout(module->ldb, search_req, 0); /* use default timeout */
-               
-               ret = ldb_next_request(module, search_req);
-               
-               if (ret == LDB_SUCCESS) {
-                       ret = ldb_wait(search_req->handle, LDB_WAIT_ALL);
-               }
-               
-               talloc_free(search_req);
-               if (ret != LDB_SUCCESS) {
-                       return ret;
-               }
+       /* All this to get the DN of the parition, so we can search the right thing */
+       partition_ctrl = ldb_request_get_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID);
+       if (!partition_ctrl) {
+               ldb_debug_set(ldb, LDB_DEBUG_FATAL,
+                             "entryuuid_sequence_number: no current partition control found");
+               return LDB_ERR_CONSTRAINT_VIOLATION;
+       }
+
+       partition = talloc_get_type(partition_ctrl->data,
+                                   struct dsdb_control_current_partition);
+       SMB_ASSERT(partition && partition->version == DSDB_CONTROL_CURRENT_PARTITION_VERSION);
+
+       ret = ldb_build_search_req(&search_req, ldb, req,
+                                  partition->dn, LDB_SCOPE_BASE,
+                                  NULL, contextCSN_attr, NULL,
+                                  &seq_num, get_seq_callback,
+                                  NULL);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+
+       ret = ldb_next_request(module, search_req);
+
+       if (ret == LDB_SUCCESS) {
+               ret = ldb_wait(search_req->handle, LDB_WAIT_ALL);
        }
 
-       switch (req->op.seq_num.type) {
+       talloc_free(search_req);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+
+       ext = talloc_zero(req, struct ldb_extended);
+       if (!ext) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+       seqr = talloc_zero(req, struct ldb_seqnum_result);
+       if (seqr == NULL) {
+               talloc_free(ext);
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+       ext->oid = LDB_EXTENDED_SEQUENCE_NUMBER;
+       ext->data = seqr;
+
+       switch (seq->type) {
        case LDB_SEQ_HIGHEST_SEQ:
-               req->op.seq_num.seq_num = max_seq;
+               seqr->seq_num = seq_num;
                break;
        case LDB_SEQ_NEXT:
-               req->op.seq_num.seq_num = max_seq;
-               req->op.seq_num.seq_num++;
+               seqr->seq_num = seq_num;
+               seqr->seq_num++;
                break;
        case LDB_SEQ_HIGHEST_TIMESTAMP:
        {
-               req->op.seq_num.seq_num = (max_seq >> 24);
+               seqr->seq_num = (seq_num >> 24);
                break;
        }
        }
-       req->op.seq_num.flags = 0;
-       req->op.seq_num.flags |= LDB_SEQ_TIMESTAMP_SEQUENCE;
-       req->op.seq_num.flags |= LDB_SEQ_GLOBAL_SEQUENCE;
-       return LDB_SUCCESS;
+       seqr->flags = 0;
+       seqr->flags |= LDB_SEQ_TIMESTAMP_SEQUENCE;
+       seqr->flags |= LDB_SEQ_GLOBAL_SEQUENCE;
+
+       /* send request done */
+       return ldb_module_done(req, NULL, ext, LDB_SUCCESS);
 }
 
-static struct ldb_module_ops entryuuid_ops = {
+static int entryuuid_extended(struct ldb_module *module, struct ldb_request *req)
+{
+       if (strcmp(req->op.extended.oid, LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
+               return entryuuid_sequence_number(module, req);
+       }
+
+       return ldb_next_request(module, req);
+}
+
+_PUBLIC_ const struct ldb_module_ops ldb_entryuuid_module_ops = {
        .name              = "entryuuid",
        .init_context      = entryuuid_init,
-       .sequence_number   = entryuuid_sequence_number
+       .extended          = entryuuid_extended,
+       LDB_MAP_OPS
 };
 
-static struct ldb_module_ops nsuniqueid_ops = {
+_PUBLIC_ const struct ldb_module_ops ldb_nsuniqueid_module_ops = {
        .name              = "nsuniqueid",
        .init_context      = nsuniqueid_init,
-       .sequence_number   = entryuuid_sequence_number
+       .extended          = entryuuid_extended,
+       LDB_MAP_OPS
 };
-
-/* the init function */
-int ldb_simple_ldap_map_module_init(void)
-{
-       int ret;
-       struct ldb_module_ops ops = ldb_map_get_ops();
-       entryuuid_ops.add       = ops.add;
-       entryuuid_ops.modify    = ops.modify;
-       entryuuid_ops.del       = ops.del;
-       entryuuid_ops.rename    = ops.rename;
-       entryuuid_ops.search    = ops.search;
-       entryuuid_ops.wait      = ops.wait;
-       ret = ldb_register_module(&entryuuid_ops);
-
-       if (ret) {
-               return ret;
-       }
-
-       nsuniqueid_ops.add      = ops.add;
-       nsuniqueid_ops.modify   = ops.modify;
-       nsuniqueid_ops.del      = ops.del;
-       nsuniqueid_ops.rename   = ops.rename;
-       nsuniqueid_ops.search   = ops.search;
-       nsuniqueid_ops.wait     = ops.wait;
-       ret = ldb_register_module(&nsuniqueid_ops);
-
-       return ret;
-}