s4:dsdb: Fix warnings about not set / set but unused / shadowed variables
[metze/samba/wip.git] / source4 / dsdb / samdb / ldb_modules / simple_ldap_map.c
index d923e55484a11ca0080e0b317f812b4c81505ab1..97016b5f62614a0046ad1362c38f398aca0bd772 100644 (file)
 */
 
 #include "includes.h"
-#include "ldb/include/ldb_module.h"
+#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 "dsdb/common/util.h"
+#include <ldb_handlers.h>
 
 struct entryuuid_private {
        struct ldb_context *ldb;
@@ -43,16 +45,14 @@ static struct ldb_val encode_guid(struct ldb_module *module, TALLOC_CTX *ctx, co
 {
        struct GUID guid;
        NTSTATUS status = GUID_from_data_blob(val, &guid);
-       enum ndr_err_code ndr_err;
        struct ldb_val out = data_blob(NULL, 0);
 
        if (!NT_STATUS_IS_OK(status)) {
                return out;
        }
-       ndr_err = ndr_push_struct_blob(&out, ctx, NULL, &guid,
-                                      (ndr_push_flags_fn_t)ndr_push_GUID);
-       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-               return out;
+       status = GUID_to_ndr_blob(&guid, ctx, &out);
+       if (!NT_STATUS_IS_OK(status)) {
+               return data_blob(NULL, 0);
        }
 
        return out;
@@ -73,16 +73,14 @@ static struct ldb_val encode_ns_guid(struct ldb_module *module, TALLOC_CTX *ctx,
 {
        struct GUID guid;
        NTSTATUS status = NS_GUID_from_string((char *)val->data, &guid);
-       enum ndr_err_code ndr_err;
        struct ldb_val out = data_blob(NULL, 0);
 
        if (!NT_STATUS_IS_OK(status)) {
                return out;
        }
-       ndr_err = ndr_push_struct_blob(&out, ctx, NULL, &guid,
-                                      (ndr_push_flags_fn_t)ndr_push_GUID);
-       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-               return out;
+       status = GUID_to_ndr_blob(&guid, ctx, &out);
+       if (!NT_STATUS_IS_OK(status)) {
+               return data_blob(NULL, 0);
        }
 
        return out;
@@ -122,6 +120,25 @@ static struct ldb_val sid_always_binary(struct ldb_module *module, TALLOC_CTX *c
        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)
 {
@@ -131,7 +148,7 @@ static struct ldb_val objectCategory_always_dn(struct ldb_module *module, TALLOC
        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)) {
+       if (ldb_dn_validate(dn)) {
                talloc_free(dn);
                return val_copy(module, ctx, val);
        }
@@ -165,7 +182,7 @@ static struct ldb_val usn_to_entryCSN(struct ldb_module *module, TALLOC_CTX *ctx
 
 static unsigned long long entryCSN_to_usn_int(TALLOC_CTX *ctx, const struct ldb_val *val) 
 {
-       char *entryCSN = talloc_strdup(ctx, (const char *)val->data);
+       char *entryCSN = talloc_strndup(ctx, (const char *)val->data, val->length);
        char *mod_per_sec;
        time_t t;
        unsigned long long usn;
@@ -216,10 +233,10 @@ static struct ldb_val usn_to_timestamp(struct ldb_module *module, TALLOC_CTX *ct
 static struct ldb_val timestamp_to_usn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
 {
        struct ldb_val out;
-       time_t t;
+       time_t t=0;
        unsigned long long usn;
 
-       t = ldb_string_to_time((const char *)val->data);
+       ldb_val_to_time(val, &t);
        
        usn = ((unsigned long long)t <<24);
 
@@ -266,12 +283,24 @@ static const struct ldb_map_attribute entryuuid_attributes[] =
                        },
                },
        },
+       /* securityIdentifier */
+       {
+               .local_name = "securityIdentifier",
+               .type = LDB_MAP_CONVERT,
+               .u = {
+                       .convert = {
+                               .remote_name = "securityIdentifier",
+                               .convert_local = sid_always_binary,
+                               .convert_remote = val_copy,
+                       },
+               },
+       },
        {
                .local_name = "name",
                .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
-                                .remote_name = "samba4RDN"
+                                .remote_name = "rdnValue"
                         }
                }
        },
@@ -470,12 +499,24 @@ static const struct ldb_map_attribute nsuniqueid_attributes[] =
                .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,
                        }
                }
        },
+       /* securityIdentifier */
+       {
+               .local_name = "securityIdentifier",
+               .type = LDB_MAP_CONVERT,
+               .u = {
+                       .convert = {
+                               .remote_name = "securityIdentifier",
+                               .convert_local = sid_always_binary,
+                               .convert_remote = val_copy,
+                       },
+               },
+       },
        {
                .local_name = "whenCreated",
                .type = LDB_MAP_RENAME,
@@ -591,18 +632,9 @@ static const struct ldb_map_attribute nsuniqueid_attributes[] =
                         }
                }
        },
-       {
-               .local_name = "unixHomeDirectory",
-               .type = MAP_RENAME,
-               .u = {
-                       .rename = {
-                                .remote_name = "homeDirectory"
-                        }
-               }
-       },
        {
                .local_name = "pwdLastSet",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "sambaPwdLastSet"
@@ -611,7 +643,7 @@ static const struct ldb_map_attribute nsuniqueid_attributes[] =
        },
        {
                .local_name = "lastLogon",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "sambaLogonTime"
@@ -620,7 +652,7 @@ static const struct ldb_map_attribute nsuniqueid_attributes[] =
        },
        {
                .local_name = "lastLogoff",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "sambaLogoffTime"
@@ -629,7 +661,7 @@ static const struct ldb_map_attribute nsuniqueid_attributes[] =
        },
        {
                .local_name = "badPwdCount",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "sambaBadPasswordCount"
@@ -638,7 +670,7 @@ static const struct ldb_map_attribute nsuniqueid_attributes[] =
        },
        {
                .local_name = "logonHours",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "sambaLogonHours"
@@ -647,7 +679,7 @@ static const struct ldb_map_attribute nsuniqueid_attributes[] =
        },
        {
                .local_name = "homeDrive",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "sambaHomeDrive"
@@ -656,7 +688,7 @@ static const struct ldb_map_attribute nsuniqueid_attributes[] =
        },
        {
                .local_name = "scriptPath",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "sambaLogonScript"
@@ -665,7 +697,7 @@ static const struct ldb_map_attribute nsuniqueid_attributes[] =
        },
        {
                .local_name = "profilePath",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "sambaProfilePath"
@@ -674,7 +706,7 @@ static const struct ldb_map_attribute nsuniqueid_attributes[] =
        },
        {
                .local_name = "userWorkstations",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "sambaUserWorkstations"
@@ -683,7 +715,7 @@ static const struct ldb_map_attribute nsuniqueid_attributes[] =
        },
        {
                .local_name = "homeDirectory",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "sambaHomePath"
@@ -692,7 +724,7 @@ static const struct ldb_map_attribute nsuniqueid_attributes[] =
        },
        {
                .local_name = "nextRid",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "sambaNextRid"
@@ -701,7 +733,7 @@ static const struct ldb_map_attribute nsuniqueid_attributes[] =
        },
        {
                .local_name = "privilegeDisplayName",
-               .type = MAP_RENAME,
+               .type = LDB_MAP_RENAME,
                .u = {
                        .rename = {
                                 .remote_name = "sambaPrivName"
@@ -720,38 +752,6 @@ static const struct ldb_map_attribute nsuniqueid_attributes[] =
 /* 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
        }
@@ -822,7 +822,6 @@ static int entryuuid_sequence_number(struct ldb_module *module, struct ldb_reque
        struct ldb_context *ldb;
        int ret;
        struct map_private *map_private;
-       struct entryuuid_private *entryuuid_private;
        unsigned long long seq_num = 0;
        struct ldb_request *search_req;
 
@@ -843,25 +842,28 @@ static int entryuuid_sequence_number(struct ldb_module *module, struct ldb_reque
 
        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(ldb, LDB_DEBUG_FATAL,
-                             "entryuuid_sequence_number: no current partition control found");
-               return LDB_ERR_CONSTRAINT_VIOLATION;
+                             "entryuuid_sequence_number: no current partition control found!");
+               return LDB_ERR_PROTOCOL_ERROR;
        }
 
        partition = talloc_get_type(partition_ctrl->data,
                                    struct dsdb_control_current_partition);
-       SMB_ASSERT(partition && partition->version == DSDB_CONTROL_CURRENT_PARTITION_VERSION);
+       if ((partition == NULL) || (partition->version != DSDB_CONTROL_CURRENT_PARTITION_VERSION)) {
+               ldb_debug_set(ldb, LDB_DEBUG_FATAL,
+                             "entryuuid_sequence_number: current partition control with wrong data!");
+               return LDB_ERR_PROTOCOL_ERROR;
+       }
 
        ret = ldb_build_search_req(&search_req, ldb, req,
                                   partition->dn, LDB_SCOPE_BASE,
                                   NULL, contextCSN_attr, NULL,
                                   &seq_num, get_seq_callback,
                                   NULL);
+       LDB_REQ_SET_LOCATION(search_req);
        if (ret != LDB_SUCCESS) {
                return ret;
        }
@@ -879,12 +881,12 @@ static int entryuuid_sequence_number(struct ldb_module *module, struct ldb_reque
 
        ext = talloc_zero(req, struct ldb_extended);
        if (!ext) {
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb);
        }
        seqr = talloc_zero(req, struct ldb_seqnum_result);
        if (seqr == NULL) {
                talloc_free(ext);
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_oom(ldb);
        }
        ext->oid = LDB_EXTENDED_SEQUENCE_NUMBER;
        ext->data = seqr;
@@ -898,13 +900,10 @@ static int entryuuid_sequence_number(struct ldb_module *module, struct ldb_reque
                seqr->seq_num++;
                break;
        case LDB_SEQ_HIGHEST_TIMESTAMP:
-       {
-               seqr->seq_num = (seq_num >> 24);
-               break;
-       }
+               return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR, "LDB_SEQ_HIGHEST_TIMESTAMP not supported");
        }
+
        seqr->flags = 0;
-       seqr->flags |= LDB_SEQ_TIMESTAMP_SEQUENCE;
        seqr->flags |= LDB_SEQ_GLOBAL_SEQUENCE;
 
        /* send request done */
@@ -920,16 +919,34 @@ static int entryuuid_extended(struct ldb_module *module, struct ldb_request *req
        return ldb_next_request(module, req);
 }
 
-_PUBLIC_ const struct ldb_module_ops ldb_entryuuid_module_ops = {
+static const struct ldb_module_ops ldb_entryuuid_module_ops = {
        .name              = "entryuuid",
        .init_context      = entryuuid_init,
        .extended          = entryuuid_extended,
        LDB_MAP_OPS
 };
 
-_PUBLIC_ const struct ldb_module_ops ldb_nsuniqueid_module_ops = {
+static const struct ldb_module_ops ldb_nsuniqueid_module_ops = {
        .name              = "nsuniqueid",
        .init_context      = nsuniqueid_init,
        .extended          = entryuuid_extended,
        LDB_MAP_OPS
 };
+
+/*
+  initialise the module
+ */
+_PUBLIC_ int ldb_simple_ldap_map_module_init(const char *version)
+{
+       int ret;
+       LDB_MODULE_CHECK_VERSION(version);
+       ret = ldb_register_module(&ldb_entryuuid_module_ops);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       ret = ldb_register_module(&ldb_nsuniqueid_module_ops);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       return LDB_SUCCESS;
+}