From 65b967a706bb4ee2da1d4211c31c91d31a81e8f1 Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Fri, 16 Jul 2010 14:23:24 +0300 Subject: s4-ldb: Implement ldb_msg_difference() function to accept a memory context from client Old implementation from ldb_msg_diff() was moved into this this function but with changed interface so that a memory context may be passed. ldb_msg_diff() function is now based on ldb_msg_difference(), which fixes a hidden leak - internal ldb_msg object (returned from ldb_msg_canonicalize) wasn't freed and stays attached to ldb_context for the connection lifetime. Signed-off-by: Andrew Bartlett --- source4/lib/ldb/common/ldb_msg.c | 95 ++++++++++++++++++++++++++++++---------- source4/lib/ldb/include/ldb.h | 20 +++++++++ 2 files changed, 92 insertions(+), 23 deletions(-) (limited to 'source4/lib') diff --git a/source4/lib/ldb/common/ldb_msg.c b/source4/lib/ldb/common/ldb_msg.c index 4d0149af8f..9f96ae6cbf 100644 --- a/source4/lib/ldb/common/ldb_msg.c +++ b/source4/lib/ldb/common/ldb_msg.c @@ -582,35 +582,74 @@ struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb, } -/* - return a ldb_message representing the differences between msg1 and msg2. If you - then use this in a ldb_modify() call it can be used to save edits to a message -*/ +/** + * return a ldb_message representing the differences between msg1 and msg2. + * If you then use this in a ldb_modify() call, + * it can be used to save edits to a message + */ struct ldb_message *ldb_msg_diff(struct ldb_context *ldb, struct ldb_message *msg1, struct ldb_message *msg2) { + int ldb_ret; struct ldb_message *mod; - struct ldb_message_element *el; + + ldb_ret = ldb_msg_difference(ldb, ldb, msg1, msg2, &mod); + if (ldb_ret != LDB_SUCCESS) { + return NULL; + } + + return mod; +} + +/** + * return a ldb_message representing the differences between msg1 and msg2. + * If you then use this in a ldb_modify() call it can be used to save edits to a message + * + * Result message is constructed as follows: + * - LDB_FLAG_MOD_ADD - elements found only in msg2 + * - LDB_FLAG_MOD_REPLACE - elements in msg2 that have different value in msg1 + * Value for msg2 element is used + * - LDB_FLAG_MOD_DELETE - elements found only in msg2 + * + * @return LDB_SUCCESS or LDB_ERR_OPERATIONS_ERROR + */ +int ldb_msg_difference(struct ldb_context *ldb, + TALLOC_CTX *mem_ctx, + struct ldb_message *msg1, + struct ldb_message *msg2, + struct ldb_message **_msg_out) +{ + int ldb_res; unsigned int i; + struct ldb_message *mod; + struct ldb_message_element *el; + TALLOC_CTX *temp_ctx; - mod = ldb_msg_new(ldb); + temp_ctx = talloc_new(mem_ctx); + if (!temp_ctx) { + return LDB_ERR_OPERATIONS_ERROR; + } + + mod = ldb_msg_new(temp_ctx); if (mod == NULL) { - return NULL; + goto failed; } mod->dn = msg1->dn; mod->num_elements = 0; mod->elements = NULL; + /* canonicalize msg2 so we have no repeated elements */ msg2 = ldb_msg_canonicalize(ldb, msg2); if (msg2 == NULL) { - talloc_free(mod); - return NULL; + goto failed; } - - /* look in msg2 to find elements that need to be added - or modified */ + + /* steal msg2 into mod context as it is allocated in ldb's context */ + talloc_steal(mod, msg2); + + /* look in msg2 to find elements that need to be added or modified */ for (i=0;inum_elements;i++) { el = ldb_msg_find_element(msg1, msg2->elements[i].name); @@ -618,11 +657,11 @@ struct ldb_message *ldb_msg_diff(struct ldb_context *ldb, continue; } - if (ldb_msg_add(mod, - &msg2->elements[i], - el?LDB_FLAG_MOD_REPLACE:LDB_FLAG_MOD_ADD) != LDB_SUCCESS) { - talloc_free(mod); - return NULL; + ldb_res = ldb_msg_add(mod, + &msg2->elements[i], + el ? LDB_FLAG_MOD_REPLACE : LDB_FLAG_MOD_ADD); + if (ldb_res != LDB_SUCCESS) { + goto failed; } } @@ -630,18 +669,28 @@ struct ldb_message *ldb_msg_diff(struct ldb_context *ldb, for (i=0;inum_elements;i++) { el = ldb_msg_find_element(msg2, msg1->elements[i].name); if (el == NULL) { - if (ldb_msg_add_empty(mod, - msg1->elements[i].name, - LDB_FLAG_MOD_DELETE, NULL) != LDB_SUCCESS) { - talloc_free(mod); - return NULL; + ldb_res = ldb_msg_add_empty(mod, + msg1->elements[i].name, + LDB_FLAG_MOD_DELETE, NULL); + if (ldb_res != LDB_SUCCESS) { + goto failed; } } } - return mod; + /* steal resulting message into supplied context */ + talloc_steal(mem_ctx, mod); + *_msg_out = mod; + + talloc_free(temp_ctx); + return LDB_SUCCESS; + +failed: + talloc_free(temp_ctx); + return LDB_ERR_OPERATIONS_ERROR; } + int ldb_msg_sanity_check(struct ldb_context *ldb, const struct ldb_message *msg) { diff --git a/source4/lib/ldb/include/ldb.h b/source4/lib/ldb/include/ldb.h index 039f70895a..667c91b34d 100644 --- a/source4/lib/ldb/include/ldb.h +++ b/source4/lib/ldb/include/ldb.h @@ -1859,6 +1859,26 @@ struct ldb_message *ldb_msg_diff(struct ldb_context *ldb, struct ldb_message *msg1, struct ldb_message *msg2); +/** + * return a ldb_message representing the differences between msg1 and msg2. + * If you then use this in a ldb_modify() call, + * it can be used to save edits to a message + * + * Result message is constructed as follows: + * - LDB_FLAG_MOD_ADD - elements found only in msg2 + * - LDB_FLAG_MOD_REPLACE - elements in msg2 that have + * different value in msg1 + * Value for msg2 element is used + * - LDB_FLAG_MOD_DELETE - elements found only in msg2 + * + * @return LDB_SUCCESS or LDB_ERR_OPERATIONS_ERROR + */ +int ldb_msg_difference(struct ldb_context *ldb, + TALLOC_CTX *mem_ctx, + struct ldb_message *msg1, + struct ldb_message *msg2, + struct ldb_message **_msg_out); + /** Tries to find a certain string attribute in a message -- cgit From fa0db46af16080dd3a540072f7ad664c0b9270ca Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Fri, 16 Jul 2010 13:35:07 +0300 Subject: s4-tools: use ldb_msg_difference() in ldbedit - modify_record() Signed-off-by: Andrew Bartlett --- source4/lib/ldb/tools/ldbedit.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'source4/lib') diff --git a/source4/lib/ldb/tools/ldbedit.c b/source4/lib/ldb/tools/ldbedit.c index f28964b06a..bb23f56377 100644 --- a/source4/lib/ldb/tools/ldbedit.c +++ b/source4/lib/ldb/tools/ldbedit.c @@ -67,16 +67,17 @@ static int modify_record(struct ldb_context *ldb, struct ldb_message *msg2, struct ldb_control **req_ctrls) { + int ret; struct ldb_message *mod; - mod = ldb_msg_diff(ldb, msg1, msg2); - if (mod == NULL) { + if (ldb_msg_difference(ldb, ldb, msg1, msg2, &mod) != LDB_SUCCESS) { fprintf(stderr, "Failed to calculate message differences\n"); return -1; } - if (mod->num_elements == 0) { - return 0; + ret = mod->num_elements; + if (ret == 0) { + goto done; } if (options->verbose > 0) { @@ -86,10 +87,13 @@ static int modify_record(struct ldb_context *ldb, if (ldb_modify_ctrl(ldb, mod, req_ctrls) != 0) { fprintf(stderr, "failed to modify %s - %s\n", ldb_dn_get_linearized(msg1->dn), ldb_errstring(ldb)); - return -1; + ret = -1; + goto done; } - return mod->num_elements; +done: + talloc_free(mod); + return ret; } /* -- cgit From 8deae13313b87c0d7efa64e9334c06987ed90ac6 Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Fri, 16 Jul 2010 13:40:50 +0300 Subject: s4-pyldb: Use ldb_msg_difference() in py_ldb_msg_diff() Signed-off-by: Andrew Bartlett --- source4/lib/ldb/pyldb.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'source4/lib') diff --git a/source4/lib/ldb/pyldb.c b/source4/lib/ldb/pyldb.c index f27ab3dd95..19123c3c24 100644 --- a/source4/lib/ldb/pyldb.c +++ b/source4/lib/ldb/pyldb.c @@ -1035,9 +1035,11 @@ static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args) static PyObject *py_ldb_msg_diff(PyLdbObject *self, PyObject *args) { + int ldb_ret; PyObject *py_msg_old; PyObject *py_msg_new; struct ldb_message *diff; + struct ldb_context *ldb; PyObject *py_ret; if (!PyArg_ParseTuple(args, "OO", &py_msg_old, &py_msg_new)) @@ -1053,14 +1055,20 @@ static PyObject *py_ldb_msg_diff(PyLdbObject *self, PyObject *args) return NULL; } - diff = ldb_msg_diff(PyLdb_AsLdbContext(self), PyLdbMessage_AsMessage(py_msg_old), PyLdbMessage_AsMessage(py_msg_new)); - if (!diff) { + ldb = PyLdb_AsLdbContext(self); + ldb_ret = ldb_msg_difference(ldb, ldb, + PyLdbMessage_AsMessage(py_msg_old), + PyLdbMessage_AsMessage(py_msg_new), + &diff); + if (ldb_ret != LDB_SUCCESS) { PyErr_SetString(PyExc_RuntimeError, "Failed to generate the Ldb Message diff"); return NULL; } py_ret = PyLdbMessage_FromMessage(diff); + talloc_unlink(ldb, diff); + return py_ret; } -- cgit From a95fd4ef647ed6d4c81ab862e08e7c42ee2fe0d6 Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Fri, 16 Jul 2010 13:41:57 +0300 Subject: s4-ldb: Write more explanatory comment for ldb_msg_add() Signed-off-by: Andrew Bartlett --- source4/lib/ldb/common/ldb_msg.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'source4/lib') diff --git a/source4/lib/ldb/common/ldb_msg.c b/source4/lib/ldb/common/ldb_msg.c index 9f96ae6cbf..ca7b961953 100644 --- a/source4/lib/ldb/common/ldb_msg.c +++ b/source4/lib/ldb/common/ldb_msg.c @@ -150,9 +150,14 @@ int ldb_msg_add_empty( struct ldb_message *msg, return LDB_SUCCESS; } -/* - add an empty element to a message -*/ +/** + * Adds an element to a message. + * + * NOTE: Ownership of ldb_message_element fields + * is NOT transferred. Thus, if *el pointer + * is invalidated for some reason, this will + * corrupt *msg contents also + */ int ldb_msg_add(struct ldb_message *msg, const struct ldb_message_element *el, int flags) -- cgit From 8d523d46f5dfcbf5a428fd75b908fe5bd738e62c Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Fri, 16 Jul 2010 13:44:13 +0300 Subject: s4-ldb: Add separate function to add empty element into ldb_msg It just adds another element, nothing more. Caller is responsible to fill-in the added element and determine how to handle data allocation contexts. Signed-off-by: Andrew Bartlett --- source4/lib/ldb/common/ldb_msg.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'source4/lib') diff --git a/source4/lib/ldb/common/ldb_msg.c b/source4/lib/ldb/common/ldb_msg.c index ca7b961953..444ba0a7f4 100644 --- a/source4/lib/ldb/common/ldb_msg.c +++ b/source4/lib/ldb/common/ldb_msg.c @@ -114,6 +114,36 @@ struct ldb_val ldb_val_dup(void *mem_ctx, const struct ldb_val *v) return v2; } +/** + * Adds new empty element to msg->elements + */ +static int _ldb_msg_add_el(struct ldb_message *msg, + struct ldb_message_element **return_el) +{ + struct ldb_message_element *els; + + /* + * TODO: Find out a way to assert on input parameters. + * msg and return_el must be valid + */ + + els = talloc_realloc(msg, msg->elements, + struct ldb_message_element, msg->num_elements + 1); + if (!els) { + errno = ENOMEM; + return LDB_ERR_OPERATIONS_ERROR; + } + + ZERO_STRUCT(els[msg->num_elements]); + + msg->elements = els; + msg->num_elements++; + + *return_el = &els[msg->num_elements-1]; + + return LDB_SUCCESS; +} + /* add an empty element to a message */ -- cgit From 3944c81d08177e7fa360b1925648686c729e2773 Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Fri, 16 Jul 2010 13:46:05 +0300 Subject: s4-ldb: Use _ldb_msg_add_el() in ldb_msg_add_empty() Signed-off-by: Andrew Bartlett --- source4/lib/ldb/common/ldb_msg.c | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) (limited to 'source4/lib') diff --git a/source4/lib/ldb/common/ldb_msg.c b/source4/lib/ldb/common/ldb_msg.c index 444ba0a7f4..5681d5228e 100644 --- a/source4/lib/ldb/common/ldb_msg.c +++ b/source4/lib/ldb/common/ldb_msg.c @@ -144,37 +144,32 @@ static int _ldb_msg_add_el(struct ldb_message *msg, return LDB_SUCCESS; } -/* - add an empty element to a message -*/ -int ldb_msg_add_empty( struct ldb_message *msg, - const char *attr_name, - int flags, - struct ldb_message_element **return_el) +/** + * Add an empty element with a given name to a message + */ +int ldb_msg_add_empty(struct ldb_message *msg, + const char *attr_name, + int flags, + struct ldb_message_element **return_el) { - struct ldb_message_element *els; + int ret; + struct ldb_message_element *el; - els = talloc_realloc(msg, msg->elements, - struct ldb_message_element, msg->num_elements+1); - if (!els) { - errno = ENOMEM; - return LDB_ERR_OPERATIONS_ERROR; + ret = _ldb_msg_add_el(msg, &el); + if (ret != LDB_SUCCESS) { + return ret; } - els[msg->num_elements].values = NULL; - els[msg->num_elements].num_values = 0; - els[msg->num_elements].flags = flags; - els[msg->num_elements].name = talloc_strdup(els, attr_name); - if (!els[msg->num_elements].name) { + /* initialize newly added element */ + el->flags = flags; + el->name = talloc_strdup(msg->elements, attr_name); + if (!el->name) { errno = ENOMEM; return LDB_ERR_OPERATIONS_ERROR; } - msg->elements = els; - msg->num_elements++; - if (return_el) { - *return_el = &els[msg->num_elements-1]; + *return_el = el; } return LDB_SUCCESS; -- cgit From 48574ccc3f46a58940a06b524ff3be3c6da6b104 Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Fri, 16 Jul 2010 13:47:41 +0300 Subject: s4-ldb: Use _ldb_msg_add_el() in ldb_msg_add() Previous implementation was 'leaking' attribute name string, that is allocated by ldb_msg_add_empty() Signed-off-by: Andrew Bartlett --- source4/lib/ldb/common/ldb_msg.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'source4/lib') diff --git a/source4/lib/ldb/common/ldb_msg.c b/source4/lib/ldb/common/ldb_msg.c index 5681d5228e..a2a1ba8110 100644 --- a/source4/lib/ldb/common/ldb_msg.c +++ b/source4/lib/ldb/common/ldb_msg.c @@ -187,15 +187,21 @@ int ldb_msg_add(struct ldb_message *msg, const struct ldb_message_element *el, int flags) { + int ret; + struct ldb_message_element *el_new; /* We have to copy this, just in case *el is a pointer into * what ldb_msg_add_empty() is about to realloc() */ struct ldb_message_element el_copy = *el; - if (ldb_msg_add_empty(msg, el->name, flags, NULL) != LDB_SUCCESS) { - return LDB_ERR_OPERATIONS_ERROR; + + ret = _ldb_msg_add_el(msg, &el_new); + if (ret != LDB_SUCCESS) { + return ret; } - msg->elements[msg->num_elements-1] = el_copy; - msg->elements[msg->num_elements-1].flags = flags; + el_new->flags = flags; + el_new->name = el_copy.name; + el_new->num_values = el_copy.num_values; + el_new->values = el_copy.values; return LDB_SUCCESS; } -- cgit From e5a9469a88e039b558e13273ae637f874bbb42b3 Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Fri, 16 Jul 2010 13:55:42 +0300 Subject: s4-ldb: Add ldb_msg_normalize() to accept a memory context from client Previos implementation from ldb_msg_canonicalize() was moved into this function and now ldb_msg_canonicalize() is based on ldb_msg_normalize() Signed-off-by: Andrew Bartlett --- source4/lib/ldb/common/ldb_msg.c | 53 +++++++++++++++++++++++++++++++--------- source4/lib/ldb/include/ldb.h | 5 ++++ 2 files changed, 46 insertions(+), 12 deletions(-) (limited to 'source4/lib') diff --git a/source4/lib/ldb/common/ldb_msg.c b/source4/lib/ldb/common/ldb_msg.c index a2a1ba8110..a643004605 100644 --- a/source4/lib/ldb/common/ldb_msg.c +++ b/source4/lib/ldb/common/ldb_msg.c @@ -577,36 +577,64 @@ failed: } -/* - canonicalise a message, merging elements of the same name -*/ +/** + * Canonicalize a message, merging elements of the same name + */ struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb, const struct ldb_message *msg) +{ + int ret; + struct ldb_message *msg2; + + /* + * Preserve previous behavior and allocate + * *msg2 into *ldb context + */ + ret = ldb_msg_normalize(ldb, ldb, msg, &msg2); + if (ret != LDB_SUCCESS) { + return NULL; + } + + return msg2; +} + +/** + * Canonicalize a message, merging elements of the same name + */ +int ldb_msg_normalize(struct ldb_context *ldb, + TALLOC_CTX *mem_ctx, + const struct ldb_message *msg, + struct ldb_message **_msg_out) { unsigned int i; struct ldb_message *msg2; - msg2 = ldb_msg_copy(ldb, msg); - if (msg2 == NULL) return NULL; + msg2 = ldb_msg_copy(mem_ctx, msg); + if (msg2 == NULL) { + return LDB_ERR_OPERATIONS_ERROR; + } ldb_msg_sort_elements(msg2); - for (i=1;inum_elements;i++) { + for (i=1; i < msg2->num_elements; i++) { struct ldb_message_element *el1 = &msg2->elements[i-1]; struct ldb_message_element *el2 = &msg2->elements[i]; + if (ldb_msg_element_compare_name(el1, el2) == 0) { - el1->values = talloc_realloc(msg2->elements, el1->values, struct ldb_val, - el1->num_values + el2->num_values); + el1->values = talloc_realloc(msg2->elements, + el1->values, struct ldb_val, + el1->num_values + el2->num_values); if (el1->num_values + el2->num_values > 0 && el1->values == NULL) { - return NULL; + talloc_free(msg2); + return LDB_ERR_OPERATIONS_ERROR; } memcpy(el1->values + el1->num_values, el2->values, sizeof(struct ldb_val) * el2->num_values); el1->num_values += el2->num_values; talloc_free(discard_const_p(char, el2->name)); - if (i+1num_elements) { - memmove(el2, el2+1, sizeof(struct ldb_message_element) * + if ((i+1) < msg2->num_elements) { + memmove(el2, el2+1, sizeof(struct ldb_message_element) * (msg2->num_elements - (i+1))); } msg2->num_elements--; @@ -614,7 +642,8 @@ struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb, } } - return msg2; + *_msg_out = msg2; + return LDB_SUCCESS; } diff --git a/source4/lib/ldb/include/ldb.h b/source4/lib/ldb/include/ldb.h index 667c91b34d..065aa31682 100644 --- a/source4/lib/ldb/include/ldb.h +++ b/source4/lib/ldb/include/ldb.h @@ -1854,6 +1854,11 @@ struct ldb_message *ldb_msg_copy(TALLOC_CTX *mem_ctx, struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb, const struct ldb_message *msg); +int ldb_msg_normalize(struct ldb_context *ldb, + TALLOC_CTX *mem_ctx, + const struct ldb_message *msg, + struct ldb_message **_msg_out); + struct ldb_message *ldb_msg_diff(struct ldb_context *ldb, struct ldb_message *msg1, -- cgit From 2ad701911e2bd5d4cdc5d5db64449f3cc01df3cd Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Fri, 16 Jul 2010 14:01:49 +0300 Subject: s4-dsdb: use ldb_msg_normalize() in source4/lib/ldb/common/ldb.c Signed-off-by: Andrew Bartlett --- source4/lib/ldb/common/ldb.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'source4/lib') diff --git a/source4/lib/ldb/common/ldb.c b/source4/lib/ldb/common/ldb.c index 877f283491..3a0ca464a4 100644 --- a/source4/lib/ldb/common/ldb.c +++ b/source4/lib/ldb/common/ldb.c @@ -795,15 +795,17 @@ int ldb_request(struct ldb_context *ldb, struct ldb_request *req) ret = module->ops->search(module, req); break; case LDB_ADD: - /* we have to canonicalise here, as so many places + /* + * we have to normalize here, as so many places * in modules and backends assume we don't have two - * elements with the same name */ - req->op.add.message = ldb_msg_canonicalize(ldb, req->op.add.message); - if (!req->op.add.message) { + * elements with the same name + */ + ret = ldb_msg_normalize(ldb, req, req->op.add.message, + discard_const(&req->op.add.message)); + if (ret != LDB_SUCCESS) { ldb_oom(ldb); return LDB_ERR_OPERATIONS_ERROR; } - talloc_steal(req, req->op.add.message); FIRST_OP(ldb, add); ret = module->ops->add(module, req); break; -- cgit From d71b20e8dc9d8e8366ffb5147c84586f5d71416e Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Fri, 16 Jul 2010 14:03:53 +0300 Subject: s4-dsdb: use ldb_msg_normalize() in ldbadd-process_file() Signed-off-by: Andrew Bartlett --- source4/lib/ldb/tools/ldbadd.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'source4/lib') diff --git a/source4/lib/ldb/tools/ldbadd.c b/source4/lib/ldb/tools/ldbadd.c index e057b873a8..fd05d8fa7f 100644 --- a/source4/lib/ldb/tools/ldbadd.c +++ b/source4/lib/ldb/tools/ldbadd.c @@ -68,7 +68,15 @@ static int process_file(struct ldb_context *ldb, FILE *f, unsigned int *count) break; } - ldif->msg = ldb_msg_canonicalize(ldb, ldif->msg); + ret = ldb_msg_normalize(ldb, ldif, ldif->msg, &ldif->msg); + if (ret != LDB_SUCCESS) { + fprintf(stderr, + "ERR: Message canonicalize failed - %s\n", + ldb_strerror(ret)); + failures++; + ldb_ldif_read_free(ldb, ldif); + continue; + } ret = ldb_add_ctrl(ldb, ldif->msg,req_ctrls); if (ret != LDB_SUCCESS) { -- cgit From 1e20dbd8127bcecda8e4a656d326b391cc5c8e8d Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Fri, 16 Jul 2010 14:16:38 +0300 Subject: s4-test: Use ldb_msg_normalize() in sqlite3 backend Signed-off-by: Andrew Bartlett --- source4/lib/ldb/ldb_sqlite3/ldb_sqlite3.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'source4/lib') diff --git a/source4/lib/ldb/ldb_sqlite3/ldb_sqlite3.c b/source4/lib/ldb/ldb_sqlite3/ldb_sqlite3.c index 12af9c5450..2225327bbe 100644 --- a/source4/lib/ldb/ldb_sqlite3/ldb_sqlite3.c +++ b/source4/lib/ldb/ldb_sqlite3/ldb_sqlite3.c @@ -687,14 +687,21 @@ static int lsqlite3_search_callback(void *result, int col_num, char **cols, char /* call the async callback for the last entry * except the first time */ if (ac->current_eid != 0) { - msg = ldb_msg_canonicalize(ldb, msg); - if (!msg) return SQLITE_ABORT; + ret = ldb_msg_normalize(ldb, ac->req, msg, &msg); + if (ret != LDB_SUCCESS) { + return SQLITE_ABORT; + } ret = ldb_module_send_entry(ac->req, msg, NULL); if (ret != LDB_SUCCESS) { ac->callback_failed = true; + /* free msg object */ + TALLOC_FREE(msg); return SQLITE_ABORT; } + + /* free msg object */ + TALLOC_FREE(msg); } /* start over */ @@ -960,8 +967,10 @@ int lsql_search(struct lsql_context *ctx) /* complete the last message if any */ if (ctx->ares) { - ctx->ares->message = ldb_msg_canonicalize(ldb, ctx->ares->message); - if (ctx->ares->message == NULL) { + ret = ldb_msg_normalize(ldb, ctx->ares, + ctx->ares->message, + &ctx->ares->message); + if (ret != LDB_SUCCESS) { return LDB_ERR_OPERATIONS_ERROR; } -- cgit From 31aeb841c9823574cb6f13986f4da34d00bb40a1 Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Fri, 16 Jul 2010 14:18:14 +0300 Subject: s4-dsdb: use ldb_msg_normalize() in ldb_msg_difference() Signed-off-by: Andrew Bartlett --- source4/lib/ldb/common/ldb_msg.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'source4/lib') diff --git a/source4/lib/ldb/common/ldb_msg.c b/source4/lib/ldb/common/ldb_msg.c index a643004605..8cf2584413 100644 --- a/source4/lib/ldb/common/ldb_msg.c +++ b/source4/lib/ldb/common/ldb_msg.c @@ -705,15 +705,17 @@ int ldb_msg_difference(struct ldb_context *ldb, mod->num_elements = 0; mod->elements = NULL; - /* canonicalize msg2 so we have no repeated elements */ - msg2 = ldb_msg_canonicalize(ldb, msg2); - if (msg2 == NULL) { + /* + * Canonicalize *msg2 so we have no repeated elements + * Resulting message is allocated in *mod's mem context, + * as we are going to move some elements from *msg2 to + * *mod object later + */ + ldb_res = ldb_msg_normalize(ldb, mod, msg2, &msg2); + if (ldb_res != LDB_SUCCESS) { goto failed; } - /* steal msg2 into mod context as it is allocated in ldb's context */ - talloc_steal(mod, msg2); - /* look in msg2 to find elements that need to be added or modified */ for (i=0;inum_elements;i++) { el = ldb_msg_find_element(msg1, msg2->elements[i].name); -- cgit From 11a44ce6f885de1c1f78c791cbe85a915934ae8a Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Fri, 16 Jul 2010 14:18:49 +0300 Subject: ldb: Mark _DEPRECATED_ ldb_msg_diff() and ldb_msg_canonicalize() functions They are not quite safe to use (requires caller to steal resulting message in own context) and may lead to holding memory for too long. Signed-off-by: Andrew Bartlett --- source4/lib/ldb/include/ldb.h | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'source4/lib') diff --git a/source4/lib/ldb/include/ldb.h b/source4/lib/ldb/include/ldb.h index 065aa31682..b3d874e36f 100644 --- a/source4/lib/ldb/include/ldb.h +++ b/source4/lib/ldb/include/ldb.h @@ -86,6 +86,14 @@ struct ldb_val { #ifndef PRINTF_ATTRIBUTE #define PRINTF_ATTRIBUTE(a,b) #endif + +#ifndef _DEPRECATED_ +#if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 ) +#define _DEPRECATED_ __attribute__ ((deprecated)) +#else +#define _DEPRECATED_ +#endif +#endif /*! \endcond */ /* opaque ldb_dn structures, see ldb_dn.c for internals */ @@ -1851,8 +1859,17 @@ struct ldb_message *ldb_msg_copy_shallow(TALLOC_CTX *mem_ctx, struct ldb_message *ldb_msg_copy(TALLOC_CTX *mem_ctx, const struct ldb_message *msg); +/* + * ldb_msg_canonicalize() is now depreciated + * Please use ldb_msg_normalize() instead + * + * NOTE: Returned ldb_message object is allocated + * into *ldb's context. Callers are recommended + * to steal the returned object into a TALLOC_CTX + * with short lifetime. + */ struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb, - const struct ldb_message *msg); + const struct ldb_message *msg) _DEPRECATED_; int ldb_msg_normalize(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, @@ -1860,9 +1877,18 @@ int ldb_msg_normalize(struct ldb_context *ldb, struct ldb_message **_msg_out); +/* + * ldb_msg_diff() is now depreciated + * Please use ldb_msg_difference() instead + * + * NOTE: Returned ldb_message object is allocated + * into *ldb's context. Callers are recommended + * to steal the returned object into a TALLOC_CTX + * with short lifetime. + */ struct ldb_message *ldb_msg_diff(struct ldb_context *ldb, struct ldb_message *msg1, - struct ldb_message *msg2); + struct ldb_message *msg2) _DEPRECATED_; /** * return a ldb_message representing the differences between msg1 and msg2. -- cgit From 35b1e00ba330d5f90f121f2af384ff416dd4a62b Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Fri, 16 Jul 2010 14:19:07 +0300 Subject: s4: Remove trailing whitespaces Signed-off-by: Andrew Bartlett --- source4/lib/ldb/tools/ldbadd.c | 8 ++++---- source4/lib/ldb/tools/ldbedit.c | 18 +++++++++--------- 2 files changed, 13 insertions(+), 13 deletions(-) (limited to 'source4/lib') diff --git a/source4/lib/ldb/tools/ldbadd.c b/source4/lib/ldb/tools/ldbadd.c index fd05d8fa7f..42470656fb 100644 --- a/source4/lib/ldb/tools/ldbadd.c +++ b/source4/lib/ldb/tools/ldbadd.c @@ -1,4 +1,4 @@ -/* +/* ldb database library Copyright (C) Andrew Tridgell 2004 @@ -6,7 +6,7 @@ ** NOTE! The following LGPL license applies to the ldb ** library. This does NOT imply that all of Samba is released ** under the LGPL - + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either @@ -40,7 +40,7 @@ static struct ldb_cmdline *options; static void usage(void) { - printf("Usage: ldbadd \n"); + printf("Usage: ldbadd \n"); printf("Adds records to a ldb, reading ldif the specified list of files\n\n"); ldb_cmdline_help("ldbadd", stdout); exit(1); @@ -142,6 +142,6 @@ int main(int argc, const char **argv) talloc_free(mem_ctx); printf("Added %d records with %d failures\n", count, failures); - + return ret; } diff --git a/source4/lib/ldb/tools/ldbedit.c b/source4/lib/ldb/tools/ldbedit.c index bb23f56377..4deeb30ee9 100644 --- a/source4/lib/ldb/tools/ldbedit.c +++ b/source4/lib/ldb/tools/ldbedit.c @@ -1,4 +1,4 @@ -/* +/* ldb database library Copyright (C) Andrew Tridgell 2004 @@ -6,7 +6,7 @@ ** NOTE! The following LGPL license applies to the ldb ** library. This does NOT imply that all of Samba is released ** under the LGPL - + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either @@ -45,10 +45,10 @@ static struct ldb_cmdline *options; /* - debug routine + debug routine */ -static void ldif_write_msg(struct ldb_context *ldb, - FILE *f, +static void ldif_write_msg(struct ldb_context *ldb, + FILE *f, enum ldb_changetype changetype, struct ldb_message *msg) { @@ -62,7 +62,7 @@ static void ldif_write_msg(struct ldb_context *ldb, modify a database record so msg1 becomes msg2 returns the number of modified elements */ -static int modify_record(struct ldb_context *ldb, +static int modify_record(struct ldb_context *ldb, struct ldb_message *msg1, struct ldb_message *msg2, struct ldb_control **req_ctrls) @@ -85,7 +85,7 @@ static int modify_record(struct ldb_context *ldb, } if (ldb_modify_ctrl(ldb, mod, req_ctrls) != 0) { - fprintf(stderr, "failed to modify %s - %s\n", + fprintf(stderr, "failed to modify %s - %s\n", ldb_dn_get_linearized(msg1->dn), ldb_errstring(ldb)); ret = -1; goto done; @@ -188,7 +188,7 @@ static int merge_edits(struct ldb_context *ldb, /* save a set of messages as ldif to a file */ -static int save_ldif(struct ldb_context *ldb, +static int save_ldif(struct ldb_context *ldb, FILE *f, struct ldb_message **msgs, unsigned int count) { unsigned int i; @@ -309,7 +309,7 @@ int main(int argc, const char **argv) options = ldb_cmdline_process(ldb, argc, argv, usage); /* the check for '=' is for compatibility with ldapsearch */ - if (options->argc > 0 && + if (options->argc > 0 && strchr(options->argv[0], '=')) { expression = options->argv[0]; options->argv++; -- cgit