s4:schema Rework dsdb_write_prefixes_from_schema_to_ldb() to use talloc
[gd/samba-autobuild/.git] / source4 / dsdb / schema / schema_init.c
index dfa745e305c62dce23616b0e58112124d8ddbaec..3b701ad31c5246c6af6d842cd61bcdbedab1a195 100644 (file)
@@ -340,8 +340,12 @@ WERROR dsdb_create_prefix_mapping(struct ldb_context *ldb, struct dsdb_schema *s
                return status;
        }
 
+       talloc_free(schema->prefixes);
+       schema->prefixes = talloc_steal(schema, prefixes);
+       schema->num_prefixes = num_prefixes;
+
        /* Update prefixMap in ldb*/
-       status = dsdb_write_prefixes_to_ldb(mem_ctx, ldb, num_prefixes, prefixes);
+       status = dsdb_write_prefixes_from_schema_to_ldb(mem_ctx, ldb, schema);
        if (!W_ERROR_IS_OK(status)) {
                DEBUG(0,("dsdb_create_prefix_mapping: dsdb_write_prefixes_to_ldb: %s\n",
                        win_errstr(status)));
@@ -349,6 +353,9 @@ WERROR dsdb_create_prefix_mapping(struct ldb_context *ldb, struct dsdb_schema *s
                return status;
        }
 
+       DEBUG(2,(__location__ " Added prefixMap %s - now have %u prefixes\n",
+                full_oid, num_prefixes));
+
        talloc_free(mem_ctx);
        return status;
 }
@@ -443,62 +450,69 @@ WERROR dsdb_find_prefix_for_oid(uint32_t num_prefixes, const struct dsdb_schema_
                return WERR_OK;
        }
 
+       DEBUG(5,(__location__ " Failed to find oid %s - have %u prefixes\n", in, num_prefixes));
+
        return WERR_DS_NO_MSDS_INTID;
 }
 
-WERROR dsdb_write_prefixes_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
-                                 uint32_t num_prefixes,
-                                 const struct dsdb_schema_oid_prefix *prefixes)
+WERROR dsdb_write_prefixes_from_schema_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
+                                                    const struct dsdb_schema *schema)
 {
-       struct ldb_message msg;
+       struct ldb_message *msg = ldb_msg_new(mem_ctx);
        struct ldb_dn *schema_dn;
-       struct ldb_message_element el;
        struct prefixMapBlob pm;
        struct ldb_val ndr_blob;
        enum ndr_err_code ndr_err;
        uint32_t i;
        int ret;
+
+       if (!msg) {
+               return WERR_NOMEM;
+       }
        
        schema_dn = samdb_schema_dn(ldb);
        if (!schema_dn) {
-               DEBUG(0,("dsdb_write_prefixes_to_ldb: no schema dn present\n"));        
+               DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: no schema dn present\n"));    
                return WERR_FOOBAR;
        }
 
        pm.version                      = PREFIX_MAP_VERSION_DSDB;
-       pm.ctr.dsdb.num_mappings        = num_prefixes;
-       pm.ctr.dsdb.mappings            = talloc_array(mem_ctx,
+       pm.ctr.dsdb.num_mappings        = schema->num_prefixes;
+       pm.ctr.dsdb.mappings            = talloc_array(msg,
                                                struct drsuapi_DsReplicaOIDMapping,
                                                pm.ctr.dsdb.num_mappings);
        if (!pm.ctr.dsdb.mappings) {
+               talloc_free(msg);
                return WERR_NOMEM;
        }
 
-       for (i=0; i < num_prefixes; i++) {
-               pm.ctr.dsdb.mappings[i].id_prefix = prefixes[i].id>>16;
-               pm.ctr.dsdb.mappings[i].oid.oid = talloc_strdup(pm.ctr.dsdb.mappings, prefixes[i].oid);
+       for (i=0; i < schema->num_prefixes; i++) {
+               pm.ctr.dsdb.mappings[i].id_prefix = schema->prefixes[i].id>>16;
+               pm.ctr.dsdb.mappings[i].oid.oid = talloc_strdup(pm.ctr.dsdb.mappings, schema->prefixes[i].oid);
        }
 
-       ndr_err = ndr_push_struct_blob(&ndr_blob, ldb,
+       ndr_err = ndr_push_struct_blob(&ndr_blob, msg,
                                       lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
                                       &pm,
                                       (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               talloc_free(msg);
                return WERR_FOOBAR;
        }
  
-       el.num_values = 1;
-       el.values = &ndr_blob;
-       el.flags = LDB_FLAG_MOD_REPLACE;
-       el.name = talloc_strdup(mem_ctx, "prefixMap");
-       msg.dn = ldb_dn_copy(mem_ctx, schema_dn);
-       msg.num_elements = 1;
-       msg.elements = &el;
+       msg->dn = schema_dn;
+       ret = ldb_msg_add_value(msg, "prefixMap", &ndr_blob, NULL);
+       if (ret != 0) {
+               talloc_free(msg);
+               DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: ldb_msg_add_value failed\n"));        
+               return WERR_NOMEM;
+       }
  
-       ret = ldb_modify( ldb, &msg );
+       ret = samdb_replace( ldb, msg, msg );
+       talloc_free(msg);
+
        if (ret != 0) {
-               DEBUG(0,("dsdb_write_prefixes_to_ldb: ldb_modify failed\n"));   
+               DEBUG(0,("dsdb_write_prefixes_from_schema_to_ldb: samdb_replace failed\n"));    
                return WERR_FOOBAR;
        }
  
@@ -644,14 +658,24 @@ static int dsdb_schema_setup_ldb_schema_attribute(struct ldb_context *ldb,
 }
 
 
-
 #define GET_STRING_LDB(msg, attr, mem_ctx, p, elem, strict) do { \
-       (p)->elem = samdb_result_string(msg, attr, NULL);\
-       if (strict && (p)->elem == NULL) { \
-               d_printf("%s: %s == NULL\n", __location__, attr); \
-               return WERR_INVALID_PARAM; \
-       } \
-       talloc_steal(mem_ctx, (p)->elem); \
+       const struct ldb_val *get_string_val = ldb_msg_find_ldb_val(msg, attr); \
+       if (get_string_val == NULL) { \
+               if (strict) {                                     \
+                       d_printf("%s: %s == NULL\n", __location__, attr); \
+                       return WERR_INVALID_PARAM;                      \
+               } else {                                                \
+                       (p)->elem = NULL;                               \
+               }                                                       \
+       } else {                                                        \
+               (p)->elem = talloc_strndup(mem_ctx,                     \
+                                          (const char *)get_string_val->data, \
+                                          get_string_val->length); \
+               if (!(p)->elem) {                                       \
+                       d_printf("%s: talloc_strndup failed for %s\n", __location__, attr); \
+                       return WERR_NOMEM;                              \
+               }                                                       \
+       }                                                               \
 } while (0)
 
 #define GET_STRING_LIST_LDB(msg, attr, mem_ctx, p, elem, strict) do {  \