From b7c8e020a6f7221d6d10f2dd7610a232edeedf83 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Thu, 29 May 2008 18:38:17 +1000 Subject: Print prefixMap in a human-readable format. This should allow the prefixMap to be edited, until we find the right way to autogenerate it. Andrew Bartlett (This used to be commit 24ae9a55ec326807afd8d5bfa0a422a6668bd7c3) --- source4/lib/ldb-samba/ldif_handlers.c | 177 ++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) (limited to 'source4/lib/ldb-samba/ldif_handlers.c') diff --git a/source4/lib/ldb-samba/ldif_handlers.c b/source4/lib/ldb-samba/ldif_handlers.c index 5c29d001c2..fb9ac8f8da 100644 --- a/source4/lib/ldb-samba/ldif_handlers.c +++ b/source4/lib/ldb-samba/ldif_handlers.c @@ -26,7 +26,9 @@ #include "dsdb/samdb/samdb.h" #include "librpc/gen_ndr/ndr_security.h" #include "librpc/gen_ndr/ndr_misc.h" +#include "librpc/gen_ndr/ndr_drsblobs.h" #include "libcli/security/security.h" +#include "param/param.h" /* convert a ldif formatted objectSid to a NDR formatted blob @@ -371,10 +373,178 @@ static int ldif_comparison_objectCategory(struct ldb_context *ldb, void *mem_ctx return ret; } +/* + convert a ldif formatted prefixMap to a NDR formatted blob +*/ +static int ldif_read_prefixMap(struct ldb_context *ldb, void *mem_ctx, + const struct ldb_val *in, struct ldb_val *out) +{ + struct prefixMapBlob *blob; + enum ndr_err_code ndr_err; + char *string, *line, *p, *oid; + + blob = talloc_zero(mem_ctx, struct prefixMapBlob); + if (blob == NULL) { + return -1; + } + + string = (const char *)in->data; + + line = string; + while (line && line[0]) { + p=strchr(line, ';'); + if (p) { + p[0] = '\0'; + } else { + p=strchr(string, '\n'); + if (p) { + p[0] = '\0'; + } + } + + blob->ctr.dsdb.mappings = talloc_realloc(blob, + blob->ctr.dsdb.mappings, + struct drsuapi_DsReplicaOIDMapping, + blob->ctr.dsdb.num_mappings+1); + if (!blob->ctr.dsdb.mappings) { + return -1; + } + + blob->ctr.dsdb.mappings[blob->ctr.dsdb.num_mappings].id_prefix = strtoul(p, &oid, 10); + + if (oid[0] != ':') { + return -1; + } + + /* 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); + + blob->ctr.dsdb.num_mappings++; + + if (p) { + line = p++; + } else { + line = NULL; + } + } + + ndr_err = ndr_push_struct_blob(out, mem_ctx, + lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), + blob, + (ndr_push_flags_fn_t)ndr_push_prefixMapBlob); + talloc_free(blob); + if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) { + return -1; + } + return 0; +} + +/* + convert a NDR formatted blob to a ldif formatted prefixMap +*/ +static int ldif_write_prefixMap(struct ldb_context *ldb, void *mem_ctx, + const struct ldb_val *in, struct ldb_val *out) +{ + struct prefixMapBlob *blob; + enum ndr_err_code ndr_err; + uint32_t i; + + blob = talloc(mem_ctx, struct prefixMapBlob); + if (blob == NULL) { + return -1; + } + ndr_err = ndr_pull_struct_blob(in, blob, + lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), + blob, + (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob); + if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) { + talloc_free(blob); + return -1; + } + if (blob->version != PREFIX_MAP_VERSION_DSDB) { + return -1; + } + out->data = talloc_strdup(mem_ctx, ""); + if (out->data == NULL) { + return -1; + } + + for (i=0; i < blob->ctr.dsdb.num_mappings; i++) { + if (i > 0) { + out->data = talloc_asprintf_append(out->data, ";"); + } + out->data = talloc_asprintf_append(out->data, "%u: %s", + blob->ctr.dsdb.mappings[i].id_prefix, + blob->ctr.dsdb.mappings[i].oid.oid); + if (out->data == NULL) { + return -1; + } + } + + talloc_free(blob); + out->length = strlen((const char *)out->data); + return 0; +} + +static bool ldif_comparision_prefixMap_isString(const struct ldb_val *v) +{ + if (v->length < 4) { + return true; + } + + if (IVAL(v->data, 0) == PREFIX_MAP_VERSION_DSDB) { + return false; + } + + return true; +} + +/* + canonicalise a prefixMap +*/ +static int ldif_canonicalise_prefixMap(struct ldb_context *ldb, void *mem_ctx, + const struct ldb_val *in, struct ldb_val *out) +{ + if (ldif_comparision_prefixMap_isString(in)) { + return ldif_read_prefixMap(ldb, mem_ctx, in, out); + } + return ldb_handler_copy(ldb, mem_ctx, in, out); +} + +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; +} + #define LDB_SYNTAX_SAMBA_SID "LDB_SYNTAX_SAMBA_SID" #define LDB_SYNTAX_SAMBA_SECURITY_DESCRIPTOR "LDB_SYNTAX_SAMBA_SECURITY_DESCRIPTOR" #define LDB_SYNTAX_SAMBA_GUID "LDB_SYNTAX_SAMBA_GUID" #define LDB_SYNTAX_SAMBA_OBJECT_CATEGORY "LDB_SYNTAX_SAMBA_OBJECT_CATEGORY" +#define LDB_SYNTAX_SAMBA_PREFIX_MAP "LDB_SYNTAX_SAMBA_PREFIX_MAP" static const struct ldb_schema_syntax samba_syntaxes[] = { { @@ -401,6 +571,12 @@ static const struct ldb_schema_syntax samba_syntaxes[] = { .ldif_write_fn = ldb_handler_copy, .canonicalise_fn= ldif_canonicalise_objectCategory, .comparison_fn = ldif_comparison_objectCategory + },{ + .name = LDB_SYNTAX_SAMBA_PREFIX_MAP, + .ldif_read_fn = ldif_read_prefixMap, + .ldif_write_fn = ldif_write_prefixMap, + .canonicalise_fn= ldif_canonicalise_prefixMap, + .comparison_fn = ldif_comparison_prefixMap } }; @@ -435,6 +611,7 @@ static const struct { { "masteredBy", LDB_SYNTAX_DN }, { "msDs-masteredBy", LDB_SYNTAX_DN }, { "fSMORoleOwner", LDB_SYNTAX_DN }, + { "prefixMap", LDB_SYNTAX_SAMBA_PREFIX_MAP } }; /* -- cgit From 5bc1d81a1df9d7de8d357c521edc9e3333e821a3 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Thu, 29 May 2008 20:01:32 +1000 Subject: Finish the LDIF parsers for the prefixMap attribute. Andrew Bartlett (This used to be commit a6f59b1c7a81c7be4e9f83786fc4e1f454e6df0f) --- source4/lib/ldb-samba/ldif_handlers.c | 45 +++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 12 deletions(-) (limited to 'source4/lib/ldb-samba/ldif_handlers.c') diff --git a/source4/lib/ldb-samba/ldif_handlers.c b/source4/lib/ldb-samba/ldif_handlers.c index fb9ac8f8da..03acf8b3f3 100644 --- a/source4/lib/ldb-samba/ldif_handlers.c +++ b/source4/lib/ldb-samba/ldif_handlers.c @@ -383,12 +383,25 @@ static int ldif_read_prefixMap(struct ldb_context *ldb, void *mem_ctx, enum ndr_err_code ndr_err; char *string, *line, *p, *oid; - blob = talloc_zero(mem_ctx, struct prefixMapBlob); + TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx); + + if (tmp_ctx == NULL) { + return -1; + } + + blob = talloc_zero(tmp_ctx, struct prefixMapBlob); if (blob == NULL) { + talloc_free(blob); return -1; } + + blob->version = PREFIX_MAP_VERSION_DSDB; - string = (const char *)in->data; + string = talloc_strndup(mem_ctx, (const char *)in->data, in->length); + if (string == NULL) { + talloc_free(blob); + return -1; + } line = string; while (line && line[0]) { @@ -396,23 +409,29 @@ static int ldif_read_prefixMap(struct ldb_context *ldb, void *mem_ctx, if (p) { p[0] = '\0'; } else { - p=strchr(string, '\n'); + p=strchr(line, '\n'); if (p) { p[0] = '\0'; } } + /* allow a traling seperator */ + if (line == p) { + break; + } blob->ctr.dsdb.mappings = talloc_realloc(blob, blob->ctr.dsdb.mappings, struct drsuapi_DsReplicaOIDMapping, blob->ctr.dsdb.num_mappings+1); if (!blob->ctr.dsdb.mappings) { + talloc_free(tmp_ctx); return -1; } - blob->ctr.dsdb.mappings[blob->ctr.dsdb.num_mappings].id_prefix = strtoul(p, &oid, 10); + blob->ctr.dsdb.mappings[blob->ctr.dsdb.num_mappings].id_prefix = strtoul(line, &oid, 10); if (oid[0] != ':') { + talloc_free(tmp_ctx); return -1; } @@ -424,8 +443,9 @@ static int ldif_read_prefixMap(struct ldb_context *ldb, void *mem_ctx, blob->ctr.dsdb.num_mappings++; + /* Now look past the terminator we added above */ if (p) { - line = p++; + line = p + 1; } else { line = NULL; } @@ -435,7 +455,7 @@ static int ldif_read_prefixMap(struct ldb_context *ldb, void *mem_ctx, lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), blob, (ndr_push_flags_fn_t)ndr_push_prefixMapBlob); - talloc_free(blob); + talloc_free(tmp_ctx); if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) { return -1; } @@ -450,6 +470,7 @@ static int ldif_write_prefixMap(struct ldb_context *ldb, void *mem_ctx, { struct prefixMapBlob *blob; enum ndr_err_code ndr_err; + char *string; uint32_t i; blob = talloc(mem_ctx, struct prefixMapBlob); @@ -467,25 +488,25 @@ static int ldif_write_prefixMap(struct ldb_context *ldb, void *mem_ctx, if (blob->version != PREFIX_MAP_VERSION_DSDB) { return -1; } - out->data = talloc_strdup(mem_ctx, ""); - if (out->data == NULL) { + string = talloc_strdup(mem_ctx, ""); + if (string == NULL) { return -1; } for (i=0; i < blob->ctr.dsdb.num_mappings; i++) { if (i > 0) { - out->data = talloc_asprintf_append(out->data, ";"); + string = talloc_asprintf_append(string, ";"); } - out->data = talloc_asprintf_append(out->data, "%u: %s", + string = talloc_asprintf_append(string, "%u: %s", blob->ctr.dsdb.mappings[i].id_prefix, blob->ctr.dsdb.mappings[i].oid.oid); - if (out->data == NULL) { + if (string == NULL) { return -1; } } talloc_free(blob); - out->length = strlen((const char *)out->data); + *out = data_blob_string_const(string); return 0; } -- cgit From 617ef56aa3378c384026b72871af5a7253b8df33 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Thu, 29 May 2008 20:16:18 +1000 Subject: Remove extra spaces on prefixMap input and output. Metze requested that the format not include spaces, and the input parser already expects this. Andrew Bartlett (This used to be commit 3b1f5d10360ed1b26980d748a7c9be6db5977bd3) --- source4/lib/ldb-samba/ldif_handlers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source4/lib/ldb-samba/ldif_handlers.c') diff --git a/source4/lib/ldb-samba/ldif_handlers.c b/source4/lib/ldb-samba/ldif_handlers.c index 03acf8b3f3..1f718cc1c5 100644 --- a/source4/lib/ldb-samba/ldif_handlers.c +++ b/source4/lib/ldb-samba/ldif_handlers.c @@ -497,7 +497,7 @@ static int ldif_write_prefixMap(struct ldb_context *ldb, void *mem_ctx, if (i > 0) { string = talloc_asprintf_append(string, ";"); } - string = talloc_asprintf_append(string, "%u: %s", + string = talloc_asprintf_append(string, "%u:%s", blob->ctr.dsdb.mappings[i].id_prefix, blob->ctr.dsdb.mappings[i].oid.oid); if (string == NULL) { -- cgit