s4 - Mapped AD schema to existing FDS schema.
[samba.git] / source4 / dsdb / samdb / ldb_modules / simple_ldap_map.c
index c353914e2cda3d8bec0e6ea4aca27635254acd52..d923e55484a11ca0080e0b317f812b4c81505ab1 100644 (file)
@@ -27,9 +27,7 @@
 */
 
 #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"
@@ -44,7 +42,7 @@ struct entryuuid_private {
 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);
 
@@ -62,27 +60,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)
@@ -107,34 +91,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;
 }
@@ -142,10 +111,11 @@ 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);
        }
 
@@ -155,18 +125,19 @@ static struct ldb_val sid_always_binary(struct ldb_module *module, TALLOC_CTX *c
 /* 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, module->ldb, val);
+       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);
        }
 
@@ -175,19 +146,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)
@@ -269,7 +233,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", 
@@ -281,7 +245,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", 
@@ -293,7 +257,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", 
@@ -304,7 +268,7 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        },
        {
                .local_name = "name",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "samba4RDN"
@@ -313,7 +277,7 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        },
        {
                .local_name = "whenCreated",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "createTimestamp"
@@ -322,7 +286,7 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        },
        {
                .local_name = "whenChanged",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "modifyTimestamp"
@@ -331,7 +295,7 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        },
        {
                .local_name = "objectClasses",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "samba4ObjectClasses"
@@ -340,7 +304,7 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        },
        {
                .local_name = "dITContentRules",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "samba4DITContentRules"
@@ -349,7 +313,7 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        },
        {
                .local_name = "attributeTypes",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "samba4AttributeTypes"
@@ -358,7 +322,7 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        },
        {
                .local_name = "objectCategory",
-               .type = MAP_CONVERT,
+               .type = LDB_MAP_CONVERT,
                .u = {
                        .convert = {
                                .remote_name = "objectCategory", 
@@ -369,38 +333,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",
@@ -411,7 +408,7 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        },
        {
                .local_name = "usnCreated",
-               .type = MAP_CONVERT,
+               .type = LDB_MAP_CONVERT,
                .u = {
                        .convert = {
                                 .remote_name = "createTimestamp",
@@ -422,7 +419,7 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
        },
        {
                .local_name = "*",
-               .type = MAP_KEEP,
+               .type = LDB_MAP_KEEP,
        },
        {
                .local_name = NULL,
@@ -458,30 +455,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,
-                       },
-               },
+                       }
+               }
        },
        {
                .local_name = "whenCreated",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "createTimestamp"
@@ -490,7 +487,7 @@ static const struct ldb_map_attribute nsuniqueid_attributes[] =
        },
        {
                .local_name = "whenChanged",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "modifyTimestamp"
@@ -499,77 +496,267 @@ static const struct ldb_map_attribute nsuniqueid_attributes[] =
        },
        {
                .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 = MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "homeDirectory"
+                        }
+               }
+       },
+       {
+               .local_name = "pwdLastSet",
+               .type = MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaPwdLastSet"
+                        }
+               }
+       },
+       {
+               .local_name = "lastLogon",
+               .type = MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaLogonTime"
+                        }
+               }
+       },
+       {
+               .local_name = "lastLogoff",
+               .type = MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaLogoffTime"
+                        }
+               }
+       },
+       {
+               .local_name = "badPwdCount",
+               .type = MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaBadPasswordCount"
+                        }
+               }
+       },
+       {
+               .local_name = "logonHours",
+               .type = MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaLogonHours"
+                        }
+               }
+       },
+       {
+               .local_name = "homeDrive",
+               .type = MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaHomeDrive"
+                        }
+               }
+       },
+       {
+               .local_name = "scriptPath",
+               .type = MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaLogonScript"
+                        }
+               }
+       },
+       {
+               .local_name = "profilePath",
+               .type = MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaProfilePath"
+                        }
+               }
+       },
+       {
+               .local_name = "userWorkstations",
+               .type = MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaUserWorkstations"
+                        }
+               }
+       },
+       {
+               .local_name = "homeDirectory",
+               .type = MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaHomePath"
+                        }
+               }
+       },
+       {
+               .local_name = "nextRid",
+               .type = MAP_RENAME,
+               .u = {
+                       .rename = {
+                                .remote_name = "sambaNextRid"
+                        }
+               }
+       },
+       {
+               .local_name = "privilegeDisplayName",
+               .type = 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[] = {
@@ -596,7 +783,7 @@ static int entryuuid_init(struct ldb_module *module)
 static int nsuniqueid_init(struct ldb_module *module)
 {
         int ret;
-       ret = ldb_map_init(module, nsuniqueid_attributes, NULL, nsuniqueid_wildcard_attributes, "extensibleObject", NULL);
+       ret = ldb_map_init(module, nsuniqueid_attributes, nsuniqueid_objectclasses, nsuniqueid_wildcard_attributes, "extensibleObject", NULL);
         if (ret != LDB_SUCCESS)
                 return ret;
 
@@ -632,6 +819,7 @@ static int get_seq_callback(struct ldb_request *req,
 
 static int entryuuid_sequence_number(struct ldb_module *module, struct ldb_request *req)
 {
+       struct ldb_context *ldb;
        int ret;
        struct map_private *map_private;
        struct entryuuid_private *entryuuid_private;
@@ -649,16 +837,18 @@ static int entryuuid_sequence_number(struct ldb_module *module, struct ldb_reque
        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(module->private_data, struct map_private);
+       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);
 
        /* 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(module->ldb, LDB_DEBUG_FATAL,
+               ldb_debug_set(ldb, LDB_DEBUG_FATAL,
                              "entryuuid_sequence_number: no current partition control found");
                return LDB_ERR_CONSTRAINT_VIOLATION;
        }
@@ -667,7 +857,7 @@ static int entryuuid_sequence_number(struct ldb_module *module, struct ldb_reque
                                    struct dsdb_control_current_partition);
        SMB_ASSERT(partition && partition->version == DSDB_CONTROL_CURRENT_PARTITION_VERSION);
 
-       ret = ldb_build_search_req(&search_req, module->ldb, req,
+       ret = ldb_build_search_req(&search_req, ldb, req,
                                   partition->dn, LDB_SCOPE_BASE,
                                   NULL, contextCSN_attr, NULL,
                                   &seq_num, get_seq_callback,