s4:provision - Removed dependency on full Samba 3 schema from FDS
[samba.git] / source4 / lib / ldb-samba / ldif_handlers.c
index 57f0533df6b1926a7516f83410e7f39bb412e489..a74687173adfd74bc7f4b98bb4bd746e48ed3017 100644 (file)
@@ -2,7 +2,7 @@
    ldb database library - ldif handlers for Samba
 
    Copyright (C) Andrew Tridgell 2005
-   Copyright (C) Andrew Bartlett 2006-2007
+   Copyright (C) Andrew Bartlett 2006-2009
    Copyright (C) Matthias Dieter Wallnöfer 2009
      ** NOTE! The following LGPL license applies to the ldb
      ** library. This does NOT imply that all of Samba is released
@@ -33,6 +33,7 @@
 #include "librpc/ndr/libndr.h"
 #include "libcli/security/security.h"
 #include "param/param.h"
+#include "../lib/util/asn1.h"
 
 /*
   use ndr_print_* to convert a NDR formatted blob to a ldif formatted blob
@@ -54,7 +55,9 @@ static int ldif_write_NDR(struct ldb_context *ldb, void *mem_ctx,
                                   p, pull_fn);
        if (err != NDR_ERR_SUCCESS) {
                talloc_free(p);
-               return ldb_handler_copy(ldb, mem_ctx, in, out);
+               out->data = (uint8_t *)talloc_strdup(mem_ctx, "<Unable to decode binary data>");
+               out->length = strlen((const char *)out->data);
+               return 0;
        }
        out->data = (uint8_t *)ndr_print_struct_string(mem_ctx, print_fn, "NDR", p);
        talloc_free(p);
@@ -89,7 +92,7 @@ static int ldif_read_objectSid(struct ldb_context *ldb, void *mem_ctx,
 /*
   convert a NDR formatted blob to a ldif formatted objectSid
 */
-static int ldif_write_objectSid(struct ldb_context *ldb, void *mem_ctx,
+int ldif_write_objectSid(struct ldb_context *ldb, void *mem_ctx,
                                const struct ldb_val *in, struct ldb_val *out)
 {
        struct dom_sid *sid;
@@ -113,7 +116,7 @@ static int ldif_write_objectSid(struct ldb_context *ldb, void *mem_ctx,
        return 0;
 }
 
-static bool ldif_comparision_objectSid_isString(const struct ldb_val *v)
+bool ldif_comparision_objectSid_isString(const struct ldb_val *v)
 {
        if (v->length < 3) {
                return false;
@@ -430,6 +433,7 @@ static int ldif_canonicalise_objectCategory(struct ldb_context *ldb, void *mem_c
        }
 
        if (!schema) {
+               talloc_free(tmp_ctx);
                *out = data_blob_talloc(mem_ctx, in->data, in->length);
                if (in->data && !out->data) {
                        return LDB_ERR_OPERATIONS_ERROR;
@@ -473,26 +477,8 @@ static int ldif_comparison_objectCategory(struct ldb_context *ldb, void *mem_ctx
                                          const struct ldb_val *v1,
                                          const struct ldb_val *v2)
 {
-
-       int ret, ret1, ret2;
-       struct ldb_val v1_canon, v2_canon;
-       TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
-
-       /* I could try and bail if tmp_ctx was NULL, but what return
-        * value would I use?
-        *
-        * It seems easier to continue on the NULL context 
-        */
-       ret1 = ldif_canonicalise_objectCategory(ldb, tmp_ctx, v1, &v1_canon);
-       ret2 = ldif_canonicalise_objectCategory(ldb, tmp_ctx, v2, &v2_canon);
-
-       if (ret1 == LDB_SUCCESS && ret2 == LDB_SUCCESS) {
-               ret = data_blob_cmp(&v1_canon, &v2_canon);
-       } else {
-               ret = data_blob_cmp(v1, v2);
-       }
-       talloc_free(tmp_ctx);
-       return ret;
+       return ldb_any_comparison(ldb, mem_ctx, ldif_canonicalise_objectCategory,
+                                 v1, v2);
 }
 
 /*
@@ -504,6 +490,7 @@ static int ldif_read_prefixMap(struct ldb_context *ldb, void *mem_ctx,
        struct prefixMapBlob *blob;
        enum ndr_err_code ndr_err;
        char *string, *line, *p, *oid;
+       DATA_BLOB oid_blob;
 
        TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
 
@@ -560,8 +547,12 @@ static int ldif_read_prefixMap(struct ldb_context *ldb, void *mem_ctx,
                /* we know there must be at least ":" */
                oid++;
 
-               blob->ctr.dsdb.mappings[blob->ctr.dsdb.num_mappings].oid.oid
-                       = talloc_strdup(blob->ctr.dsdb.mappings, oid);
+               if (!ber_write_partial_OID_String(blob->ctr.dsdb.mappings, &oid_blob, oid)) {
+                       talloc_free(tmp_ctx);
+                       return -1;
+               }
+               blob->ctr.dsdb.mappings[blob->ctr.dsdb.num_mappings].oid.length = oid_blob.length;
+               blob->ctr.dsdb.mappings[blob->ctr.dsdb.num_mappings].oid.binary_oid = oid_blob.data;
 
                blob->ctr.dsdb.num_mappings++;
 
@@ -612,32 +603,47 @@ static int ldif_write_prefixMap(struct ldb_context *ldb, void *mem_ctx,
                                           blob,
                                           (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-               talloc_free(blob);
-               return -1;
+               goto failed;
        }
        if (blob->version != PREFIX_MAP_VERSION_DSDB) {
-               return -1;
+               goto failed;
        }
        string = talloc_strdup(mem_ctx, "");
        if (string == NULL) {
-               return -1;
+               goto failed;
        }
 
        for (i=0; i < blob->ctr.dsdb.num_mappings; i++) {
+               DATA_BLOB oid_blob;
+               const char *partial_oid = NULL;
+
                if (i > 0) {
                        string = talloc_asprintf_append(string, ";"); 
                }
+
+               oid_blob = data_blob_const(blob->ctr.dsdb.mappings[i].oid.binary_oid,
+                                          blob->ctr.dsdb.mappings[i].oid.length);
+               if (!ber_read_partial_OID_String(blob, oid_blob, &partial_oid)) {
+                       DEBUG(0, ("ber_read_partial_OID failed on prefixMap item with id: 0x%X",
+                                 blob->ctr.dsdb.mappings[i].id_prefix));
+                       goto failed;
+               }
                string = talloc_asprintf_append(string, "%u:%s", 
                                                   blob->ctr.dsdb.mappings[i].id_prefix,
-                                                  blob->ctr.dsdb.mappings[i].oid.oid);
+                                                  partial_oid);
+               talloc_free(discard_const(partial_oid));
                if (string == NULL) {
-                       return -1;
+                       goto failed;
                }
        }
 
        talloc_free(blob);
        *out = data_blob_string_const(string);
        return 0;
+
+failed:
+       talloc_free(blob);
+       return -1;
 }
 
 static bool ldif_comparision_prefixMap_isString(const struct ldb_val *v)
@@ -669,26 +675,8 @@ static int ldif_comparison_prefixMap(struct ldb_context *ldb, void *mem_ctx,
                                     const struct ldb_val *v1,
                                     const struct ldb_val *v2)
 {
-
-       int ret, ret1, ret2;
-       struct ldb_val v1_canon, v2_canon;
-       TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
-
-       /* I could try and bail if tmp_ctx was NULL, but what return
-        * value would I use?
-        *
-        * It seems easier to continue on the NULL context 
-        */
-       ret1 = ldif_canonicalise_prefixMap(ldb, tmp_ctx, v1, &v1_canon);
-       ret2 = ldif_canonicalise_prefixMap(ldb, tmp_ctx, v2, &v2_canon);
-
-       if (ret1 == LDB_SUCCESS && ret2 == LDB_SUCCESS) {
-               ret = data_blob_cmp(&v1_canon, &v2_canon);
-       } else {
-               ret = data_blob_cmp(v1, v2);
-       }
-       talloc_free(tmp_ctx);
-       return ret;
+       return ldb_any_comparison(ldb, mem_ctx, ldif_canonicalise_prefixMap,
+                                 v1, v2);
 }
 
 /* Canonicalisation of two 32-bit integers */
@@ -732,11 +720,35 @@ static int ldif_write_repsFromTo(struct ldb_context *ldb, void *mem_ctx,
                              (ndr_print_fn_t)ndr_print_repsFromToBlob);
 }
 
+/*
+  convert a NDR formatted blob to a ldif formatted replPropertyMetaData
+*/
+static int ldif_write_replPropertyMetaData(struct ldb_context *ldb, void *mem_ctx,
+                                          const struct ldb_val *in, struct ldb_val *out)
+{
+       return ldif_write_NDR(ldb, mem_ctx, in, out, 
+                             sizeof(struct replPropertyMetaDataBlob),
+                             (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob,
+                             (ndr_print_fn_t)ndr_print_replPropertyMetaDataBlob);
+}
+
+/*
+  convert a NDR formatted blob to a ldif formatted replUpToDateVector
+*/
+static int ldif_write_replUpToDateVector(struct ldb_context *ldb, void *mem_ctx,
+                                        const struct ldb_val *in, struct ldb_val *out)
+{
+       return ldif_write_NDR(ldb, mem_ctx, in, out, 
+                             sizeof(struct replUpToDateVectorBlob),
+                             (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob,
+                             (ndr_print_fn_t)ndr_print_replUpToDateVectorBlob);
+}
+
 
 static int extended_dn_write_hex(struct ldb_context *ldb, void *mem_ctx,
                                 const struct ldb_val *in, struct ldb_val *out)
 {
-       *out = data_blob_string_const(data_blob_hex_string(mem_ctx, in));
+       *out = data_blob_string_const(data_blob_hex_string_lower(mem_ctx, in));
        if (!out->data) {
                return -1;
        }
@@ -786,6 +798,30 @@ static const struct ldb_schema_syntax samba_syntaxes[] = {
                .ldif_write_fn    = ldif_write_repsFromTo,
                .canonicalise_fn  = ldb_handler_copy,
                .comparison_fn    = ldb_comparison_binary
+       },{
+               .name             = LDB_SYNTAX_SAMBA_REPLPROPERTYMETADATA,
+               .ldif_read_fn     = ldb_handler_copy,
+               .ldif_write_fn    = ldif_write_replPropertyMetaData,
+               .canonicalise_fn  = ldb_handler_copy,
+               .comparison_fn    = ldb_comparison_binary
+       },{
+               .name             = LDB_SYNTAX_SAMBA_REPLUPTODATEVECTOR,
+               .ldif_read_fn     = ldb_handler_copy,
+               .ldif_write_fn    = ldif_write_replUpToDateVector,
+               .canonicalise_fn  = ldb_handler_copy,
+               .comparison_fn    = ldb_comparison_binary
+       },{
+               .name             = DSDB_SYNTAX_BINARY_DN,
+               .ldif_read_fn     = ldb_handler_copy,
+               .ldif_write_fn    = ldb_handler_copy,
+               .canonicalise_fn  = dsdb_dn_binary_canonicalise,
+               .comparison_fn    = dsdb_dn_binary_comparison
+       },{
+               .name             = DSDB_SYNTAX_STRING_DN,
+               .ldif_read_fn     = ldb_handler_copy,
+               .ldif_write_fn    = ldb_handler_copy,
+               .canonicalise_fn  = dsdb_dn_string_canonicalise,
+               .comparison_fn    = dsdb_dn_string_comparison
        },
 };
 
@@ -819,6 +855,7 @@ static const struct {
        { "objectGUID",                 LDB_SYNTAX_SAMBA_GUID },
        { "invocationId",               LDB_SYNTAX_SAMBA_GUID },
        { "schemaIDGUID",               LDB_SYNTAX_SAMBA_GUID },
+       { "oMSyntax",                   LDB_SYNTAX_SAMBA_INT32 },
        { "attributeSecurityGUID",      LDB_SYNTAX_SAMBA_GUID },
        { "parentGUID",                 LDB_SYNTAX_SAMBA_GUID },
        { "siteGUID",                   LDB_SYNTAX_SAMBA_GUID },
@@ -830,6 +867,8 @@ static const struct {
        { "prefixMap",                  LDB_SYNTAX_SAMBA_PREFIX_MAP },
        { "repsFrom",                   LDB_SYNTAX_SAMBA_REPSFROMTO },
        { "repsTo",                     LDB_SYNTAX_SAMBA_REPSFROMTO },
+       { "replPropertyMetaData",       LDB_SYNTAX_SAMBA_REPLPROPERTYMETADATA },
+       { "replUpToDateVector",         LDB_SYNTAX_SAMBA_REPLUPTODATEVECTOR },
 };
 
 const struct ldb_schema_syntax *ldb_samba_syntax_by_name(struct ldb_context *ldb, const char *name)