diff options
author | Matthias Dieter Wallnöfer <mdw@samba.org> | 2012-05-04 08:51:41 +0200 |
---|---|---|
committer | Andrew Bartlett <abartlet@samba.org> | 2012-08-22 01:31:55 +0200 |
commit | 5f8006cb64c6537f3004e91319d071a603e4468e (patch) | |
tree | 742a3d1723372ba636ae47e8b42f39f4197ce78f | |
parent | 166a7d37f7bfc7b22163e1d38a0bb0e47c2f6622 (diff) | |
download | samba-5f8006cb64c6537f3004e91319d071a603e4468e.tar.gz samba-5f8006cb64c6537f3004e91319d071a603e4468e.tar.bz2 samba-5f8006cb64c6537f3004e91319d071a603e4468e.zip |
s4:dsdb_sort_objectClass_attr - simplify memory context handling
Do only require the out memory context and build the temporary one in
the body of the function. This greatly simplifies the callers.
Signed-off-by: Andrew Bartlett <abartlet@samba.org>
-rw-r--r-- | source4/dsdb/pydsdb.c | 3 | ||||
-rw-r--r-- | source4/dsdb/samdb/ldb_modules/objectclass.c | 29 | ||||
-rw-r--r-- | source4/dsdb/schema/schema_query.c | 28 |
3 files changed, 23 insertions, 37 deletions
diff --git a/source4/dsdb/pydsdb.c b/source4/dsdb/pydsdb.c index 39229f487f..99e239e60c 100644 --- a/source4/dsdb/pydsdb.c +++ b/source4/dsdb/pydsdb.c @@ -681,8 +681,7 @@ static PyObject *py_dsdb_normalise_attributes(PyObject *self, PyObject *args) /* Normalise "objectClass" attribute if needed */ if (ldb_attr_cmp(a->lDAPDisplayName, "objectClass") == 0) { int iret; - iret = dsdb_sort_objectClass_attr(ldb, schema, tmp_ctx, el, - tmp_ctx, el); + iret = dsdb_sort_objectClass_attr(ldb, schema, el, tmp_ctx, el); if (iret != LDB_SUCCESS) { PyErr_SetString(PyExc_RuntimeError, ldb_errstring(ldb)); talloc_free(tmp_ctx); diff --git a/source4/dsdb/samdb/ldb_modules/objectclass.c b/source4/dsdb/samdb/ldb_modules/objectclass.c index 7d34b4e8c3..074360086f 100644 --- a/source4/dsdb/samdb/ldb_modules/objectclass.c +++ b/source4/dsdb/samdb/ldb_modules/objectclass.c @@ -383,7 +383,6 @@ static int objectclass_do_add(struct oc_context *ac) struct ldb_request *add_req; struct ldb_message_element *objectclass_element, *el; struct ldb_message *msg; - TALLOC_CTX *mem_ctx; const char *rdn_name = NULL; char *value; const struct dsdb_class *objectclass; @@ -448,22 +447,14 @@ static int objectclass_do_add(struct oc_context *ac) return LDB_ERR_CONSTRAINT_VIOLATION; } - mem_ctx = talloc_new(ac); - if (mem_ctx == NULL) { - return ldb_module_oom(ac->module); - } - /* Now do the sorting */ - ret = dsdb_sort_objectClass_attr(ldb, ac->schema, mem_ctx, + ret = dsdb_sort_objectClass_attr(ldb, ac->schema, objectclass_element, msg, objectclass_element); if (ret != LDB_SUCCESS) { - talloc_free(mem_ctx); return ret; } - talloc_free(mem_ctx); - /* * Get the new top-most structural object class and check for * unrelated structural classes @@ -823,7 +814,6 @@ static int objectclass_do_mod(struct oc_context *ac) struct ldb_message_element *oc_el_entry, *oc_el_change; struct ldb_val *vals; struct ldb_message *msg; - TALLOC_CTX *mem_ctx; const struct dsdb_class *objectclass; unsigned int i, j, k; bool found; @@ -851,11 +841,6 @@ static int objectclass_do_mod(struct oc_context *ac) msg->dn = ac->req->op.mod.message->dn; - mem_ctx = talloc_new(ac); - if (mem_ctx == NULL) { - return ldb_module_oom(ac->module); - } - /* We've to walk over all "objectClass" message elements */ for (k = 0; k < ac->req->op.mod.message->num_elements; k++) { if (ldb_attr_cmp(ac->req->op.mod.message->elements[k].name, @@ -876,7 +861,6 @@ static int objectclass_do_mod(struct oc_context *ac) "objectclass: cannot re-add an existing objectclass: '%.*s'!", (int)oc_el_change->values[i].length, (const char *)oc_el_change->values[i].data); - talloc_free(mem_ctx); return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS; } } @@ -886,7 +870,6 @@ static int objectclass_do_mod(struct oc_context *ac) struct ldb_val, oc_el_entry->num_values + 1); if (vals == NULL) { - talloc_free(mem_ctx); return ldb_module_oom(ac->module); } oc_el_entry->values = vals; @@ -933,7 +916,6 @@ static int objectclass_do_mod(struct oc_context *ac) "objectclass: cannot delete this objectclass: '%.*s'!", (int)oc_el_change->values[i].length, (const char *)oc_el_change->values[i].data); - talloc_free(mem_ctx); return LDB_ERR_NO_SUCH_ATTRIBUTE; } } @@ -942,10 +924,9 @@ static int objectclass_do_mod(struct oc_context *ac) } /* Now do the sorting */ - ret = dsdb_sort_objectClass_attr(ldb, ac->schema, mem_ctx, - oc_el_entry, msg, oc_el_entry); + ret = dsdb_sort_objectClass_attr(ldb, ac->schema, oc_el_entry, + msg, oc_el_entry); if (ret != LDB_SUCCESS) { - talloc_free(mem_ctx); return ret; } @@ -958,7 +939,6 @@ static int objectclass_do_mod(struct oc_context *ac) if (objectclass == NULL) { ldb_set_errstring(ldb, "objectclass: cannot delete all structural objectclasses!"); - talloc_free(mem_ctx); return LDB_ERR_OBJECT_CLASS_VIOLATION; } @@ -967,13 +947,10 @@ static int objectclass_do_mod(struct oc_context *ac) objectclass, oc_el_entry); if (ret != LDB_SUCCESS) { - talloc_free(mem_ctx); return ret; } } - talloc_free(mem_ctx); - /* Now add the new object class attribute to the change message */ ret = ldb_msg_add(msg, oc_el_entry, LDB_FLAG_MOD_REPLACE); if (ret != LDB_SUCCESS) { diff --git a/source4/dsdb/schema/schema_query.c b/source4/dsdb/schema/schema_query.c index ce46fa6315..013878d1f1 100644 --- a/source4/dsdb/schema/schema_query.c +++ b/source4/dsdb/schema/schema_query.c @@ -451,14 +451,12 @@ const struct GUID *attribute_schemaid_guid_by_lDAPDisplayName(const struct dsdb_ * into correct order and validate that all object classes specified actually * exist in the schema. * The output is written in an existing LDB message element - * "out_objectclass_element" where the values will be allocated on - * "out_mem_ctx". + * "out_objectclass_element" where the values will be allocated on "mem_ctx". */ int dsdb_sort_objectClass_attr(struct ldb_context *ldb, const struct dsdb_schema *schema, - TALLOC_CTX *mem_ctx, const struct ldb_message_element *objectclass_element, - TALLOC_CTX *out_mem_ctx, + TALLOC_CTX *mem_ctx, struct ldb_message_element *out_objectclass_element) { unsigned int i, lowest; @@ -469,6 +467,12 @@ int dsdb_sort_objectClass_attr(struct ldb_context *ldb, *poss_parent = NULL, *new_parent = NULL, *current_lowest = NULL, *current_lowest_struct = NULL; struct ldb_message_element *el; + TALLOC_CTX *tmp_mem_ctx; + + tmp_mem_ctx = talloc_new(mem_ctx); + if (tmp_mem_ctx == NULL) { + return ldb_oom(ldb); + } /* * DESIGN: @@ -504,8 +508,9 @@ int dsdb_sort_objectClass_attr(struct ldb_context *ldb, * except for 'top', which is special */ for (i=0; i < objectclass_element->num_values; i++) { - current = talloc(mem_ctx, struct class_list); + current = talloc(tmp_mem_ctx, struct class_list); if (!current) { + talloc_free(tmp_mem_ctx); return ldb_oom(ldb); } current->objectclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &objectclass_element->values[i]); @@ -513,11 +518,13 @@ int dsdb_sort_objectClass_attr(struct ldb_context *ldb, ldb_asprintf_errstring(ldb, "objectclass %.*s is not a valid objectClass in schema", (int)objectclass_element->values[i].length, (const char *)objectclass_element->values[i].data); /* This looks weird, but windows apparently returns this for invalid objectClass values */ + talloc_free(tmp_mem_ctx); return LDB_ERR_NO_SUCH_ATTRIBUTE; } else if (current->objectclass->isDefunct) { ldb_asprintf_errstring(ldb, "objectclass %.*s marked as isDefunct objectClass in schema - not valid for new objects", (int)objectclass_element->values[i].length, (const char *)objectclass_element->values[i].data); /* This looks weird, but windows apparently returns this for invalid objectClass values */ + talloc_free(tmp_mem_ctx); return LDB_ERR_NO_SUCH_ATTRIBUTE; } @@ -529,7 +536,7 @@ int dsdb_sort_objectClass_attr(struct ldb_context *ldb, /* Add top here, to prevent duplicates */ - current = talloc(mem_ctx, struct class_list); + current = talloc(tmp_mem_ctx, struct class_list); current->objectclass = dsdb_class_by_lDAPDisplayName(schema, "top"); DLIST_ADD_END(sorted, current, struct class_list *); @@ -545,7 +552,7 @@ int dsdb_sort_objectClass_attr(struct ldb_context *ldb, continue; } - new_parent = talloc(mem_ctx, struct class_list); + new_parent = talloc(tmp_mem_ctx, struct class_list); new_parent->objectclass = dsdb_class_by_lDAPDisplayName(schema, current->objectclass->subClassOf); DLIST_ADD_END(unsorted, new_parent, struct class_list *); } @@ -583,16 +590,18 @@ int dsdb_sort_objectClass_attr(struct ldb_context *ldb, el = out_objectclass_element; el->flags = objectclass_element->flags; - el->name = talloc_strdup(out_mem_ctx, objectclass_element->name); + el->name = talloc_strdup(mem_ctx, objectclass_element->name); if (el->name == NULL) { + talloc_free(tmp_mem_ctx); return ldb_oom(ldb); } el->num_values = 0; el->values = NULL; for (current = sorted; current != NULL; current = current->next) { - el->values = talloc_realloc(out_mem_ctx, el->values, + el->values = talloc_realloc(mem_ctx, el->values, struct ldb_val, el->num_values + 1); if (el->values == NULL) { + talloc_free(tmp_mem_ctx); return ldb_oom(ldb); } el->values[el->num_values] = data_blob_string_const(current->objectclass->lDAPDisplayName); @@ -600,5 +609,6 @@ int dsdb_sort_objectClass_attr(struct ldb_context *ldb, ++(el->num_values); } + talloc_free(tmp_mem_ctx); return LDB_SUCCESS; } |