From 5c9590587197dcb95007fdc54318187d5716c7c6 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Tue, 8 Nov 2005 00:11:45 +0000 Subject: r11567: Ldb API change patch. This patch changes the way lsb_search is called and the meaning of the returned integer. The last argument of ldb_search is changed from struct ldb_message to struct ldb_result which contains a pointer to a struct ldb_message list and a count of the number of messages. The return is not the count of messages anymore but instead it is an ldb error value. I tryed to keep the patch as tiny as possible bu as you can guess I had to change a good amount of places. I also tried to double check all my changes being sure that the calling functions would still behave as before. But this patch is big enough that I fear some bug may have been introduced anyway even if it passes the test suite. So if you are currently working on any file being touched please give it a deep look and blame me for any error. Simo. (This used to be commit 22c8c97e6fb466b41859e090e959d7f1134be780) --- source4/ldap_server/ldap_rootdse.c | 38 +++++----- source4/ldap_server/ldap_simple_ldb.c | 126 +++++++++++++++++++--------------- 2 files changed, 88 insertions(+), 76 deletions(-) (limited to 'source4/ldap_server') diff --git a/source4/ldap_server/ldap_rootdse.c b/source4/ldap_server/ldap_rootdse.c index 81a9626f14..d4e6003e5f 100644 --- a/source4/ldap_server/ldap_rootdse.c +++ b/source4/ldap_server/ldap_rootdse.c @@ -22,6 +22,7 @@ #include "ldap_server/ldap_server.h" #include "system/time.h" #include "lib/ldb/include/ldb.h" +#include "lib/ldb/include/ldb_errors.h" #define ATTR_BLOB_CONST(val) data_blob_talloc(mem_ctx, val, sizeof(val)-1) @@ -267,12 +268,12 @@ static NTSTATUS rootdse_Search(struct ldapsrv_partition *partition, struct ldaps void *local_ctx; struct ldap_SearchResEntry *ent; struct ldap_Result *done; - struct ldb_message **res = NULL; + struct ldb_result *res = NULL; int result = LDAP_SUCCESS; struct ldapsrv_reply *ent_r, *done_r; struct ldb_context *ldb; const char *errstr = NULL; - int count, j; + int ret, j; const char **attrs = NULL; if (r->scope != LDAP_SEARCH_SCOPE_BASE) { @@ -295,11 +296,10 @@ static NTSTATUS rootdse_Search(struct ldapsrv_partition *partition, struct ldaps attrs[j] = NULL; } - count = ldb_search(ldb, ldb_dn_explode(local_ctx, "cn=rootDSE"), 0, - NULL, attrs, &res); + ret = ldb_search(ldb, ldb_dn_explode(local_ctx, "cn=rootDSE"), 0, NULL, attrs, &res); talloc_steal(local_ctx, res); - if (count == 1) { + if (ret == LDB_SUCCESS && res->count == 1) { ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultEntry); NT_STATUS_HAVE_NO_MEMORY(ent_r); @@ -307,11 +307,11 @@ static NTSTATUS rootdse_Search(struct ldapsrv_partition *partition, struct ldaps ent->dn = ""; ent->num_attributes = 0; ent->attributes = NULL; - if (res[0]->num_elements == 0) { + if (res->msgs[0]->num_elements == 0) { goto queue_reply; } - ent->num_attributes = res[0]->num_elements; - ent->attributes = talloc_steal(ent_r, res[0]->elements); + ent->num_attributes = res->msgs[0]->num_elements; + ent->attributes = talloc_steal(ent_r, res->msgs[0]->elements); for (j=0; j < ent->num_attributes; j++) { if (ent->attributes[j].num_values == 1 && @@ -330,22 +330,22 @@ queue_reply: done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone); NT_STATUS_HAVE_NO_MEMORY(done_r); - if (count == 1) { - DEBUG(10,("rootdse_Search: results: [%d]\n",count)); - result = LDAP_SUCCESS; - errstr = NULL; - } else if (count == 0) { + if (ret != LDB_SUCCESS) { + DEBUG(10,("rootdse_Search: error\n")); + result = LDAP_OTHER; + errstr = ldb_errstring(ldb); + } else if (res->count == 0) { DEBUG(10,("rootdse_Search: no results\n")); result = LDAP_NO_SUCH_OBJECT; errstr = ldb_errstring(ldb); - } else if (count > 1) { - DEBUG(10,("rootdse_Search: too many results[%d]\n", count)); + } else if (res->count == 1) { + DEBUG(10,("rootdse_Search: results: [%d]\n", res->count)); + result = LDAP_SUCCESS; + errstr = NULL; + } else if (res->count > 1) { + DEBUG(10,("rootdse_Search: too many results[%d]\n", res->count)); result = LDAP_OTHER; errstr = "internal error"; - } else if (count == -1) { - DEBUG(10,("rootdse_Search: error\n")); - result = LDAP_OTHER; - errstr = ldb_errstring(ldb); } done = &done_r->msg->r.SearchResultDone; diff --git a/source4/ldap_server/ldap_simple_ldb.c b/source4/ldap_server/ldap_simple_ldb.c index b4e4cf8078..481db6052d 100644 --- a/source4/ldap_server/ldap_simple_ldb.c +++ b/source4/ldap_server/ldap_simple_ldb.c @@ -22,6 +22,7 @@ #include "includes.h" #include "ldap_server/ldap_server.h" #include "lib/ldb/include/ldb.h" +#include "lib/ldb/include/ldb_errors.h" #include "auth/auth.h" #include "db_wrap.h" @@ -113,12 +114,13 @@ static NTSTATUS sldb_Search(struct ldapsrv_partition *partition, struct ldapsrv_ struct ldapsrv_reply *ent_r, *done_r; int result = LDAP_SUCCESS; struct ldb_context *samdb; - struct ldb_message **res = NULL; - int i, j, y, count = 0; + struct ldb_result *res = NULL; + int i, j, y, ret; int success_limit = 1; enum ldb_scope scope = LDB_SCOPE_DEFAULT; const char **attrs = NULL; const char *errstr = NULL; + struct ldb_request lreq; local_ctx = talloc_named(call, 0, "sldb_Search local memory context"); NT_STATUS_HAVE_NO_MEMORY(local_ctx); @@ -160,71 +162,81 @@ static NTSTATUS sldb_Search(struct ldapsrv_partition *partition, struct ldapsrv_ attrs[i] = NULL; } - DEBUG(5,("ldb_search_bytree dn=%s filter=%s\n", + DEBUG(5,("ldb_request dn=%s filter=%s\n", r->basedn, ldb_filter_from_tree(call, r->tree))); - count = ldb_search_bytree(samdb, basedn, scope, r->tree, attrs, &res); - talloc_steal(samdb, res); + ZERO_STRUCT(lreq); + lreq.operation = LDB_REQ_SEARCH; + lreq.op.search.base = basedn; + lreq.op.search.scope = scope; + lreq.op.search.tree = r->tree; + lreq.op.search.attrs = attrs; + lreq.op.search.res = &res; - for (i=0; i < count; i++) { - ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultEntry); - NT_STATUS_HAVE_NO_MEMORY(ent_r); + ret = ldb_request(samdb, &lreq); + talloc_steal(samdb, res); - ent = &ent_r->msg->r.SearchResultEntry; - ent->dn = ldb_dn_linearize(ent_r, res[i]->dn); - ent->num_attributes = 0; - ent->attributes = NULL; - if (res[i]->num_elements == 0) { - goto queue_reply; - } - ent->num_attributes = res[i]->num_elements; - ent->attributes = talloc_array(ent_r, struct ldb_message_element, ent->num_attributes); - NT_STATUS_HAVE_NO_MEMORY(ent->attributes); - for (j=0; j < ent->num_attributes; j++) { - ent->attributes[j].name = talloc_steal(ent->attributes, res[i]->elements[j].name); - ent->attributes[j].num_values = 0; - ent->attributes[j].values = NULL; - if (r->attributesonly && (res[i]->elements[j].num_values == 0)) { - continue; + if (ret == LDB_SUCCESS) { + for (i = 0; i < res->count; i++) { + ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultEntry); + NT_STATUS_HAVE_NO_MEMORY(ent_r); + + ent = &ent_r->msg->r.SearchResultEntry; + ent->dn = ldb_dn_linearize(ent_r, res->msgs[i]->dn); + ent->num_attributes = 0; + ent->attributes = NULL; + if (res->msgs[i]->num_elements == 0) { + goto queue_reply; } - ent->attributes[j].num_values = res[i]->elements[j].num_values; - ent->attributes[j].values = talloc_array(ent->attributes, - DATA_BLOB, ent->attributes[j].num_values); - NT_STATUS_HAVE_NO_MEMORY(ent->attributes[j].values); - for (y=0; y < ent->attributes[j].num_values; y++) { - ent->attributes[j].values[y].length = res[i]->elements[j].values[y].length; - ent->attributes[j].values[y].data = talloc_steal(ent->attributes[j].values, - res[i]->elements[j].values[y].data); + ent->num_attributes = res->msgs[i]->num_elements; + ent->attributes = talloc_array(ent_r, struct ldb_message_element, ent->num_attributes); + NT_STATUS_HAVE_NO_MEMORY(ent->attributes); + for (j=0; j < ent->num_attributes; j++) { + ent->attributes[j].name = talloc_steal(ent->attributes, res->msgs[i]->elements[j].name); + ent->attributes[j].num_values = 0; + ent->attributes[j].values = NULL; + if (r->attributesonly && (res->msgs[i]->elements[j].num_values == 0)) { + continue; + } + ent->attributes[j].num_values = res->msgs[i]->elements[j].num_values; + ent->attributes[j].values = talloc_array(ent->attributes, + DATA_BLOB, ent->attributes[j].num_values); + NT_STATUS_HAVE_NO_MEMORY(ent->attributes[j].values); + for (y=0; y < ent->attributes[j].num_values; y++) { + ent->attributes[j].values[y].length = res->msgs[i]->elements[j].values[y].length; + ent->attributes[j].values[y].data = talloc_steal(ent->attributes[j].values, + res->msgs[i]->elements[j].values[y].data); + } } - } queue_reply: - ldapsrv_queue_reply(call, ent_r); + ldapsrv_queue_reply(call, ent_r); + } } reply: done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone); NT_STATUS_HAVE_NO_MEMORY(done_r); - if (result == LDAP_SUCCESS) { - if (count >= success_limit) { - DEBUG(10,("sldb_Search: results: [%d]\n",count)); + if (ret == LDB_SUCCESS) { + if (res->count >= success_limit) { + DEBUG(10,("sldb_Search: results: [%d]\n", res->count)); result = LDAP_SUCCESS; errstr = NULL; - } else if (count == 0) { + } else if (res->count == 0) { DEBUG(10,("sldb_Search: no results\n")); result = LDAP_NO_SUCH_OBJECT; errstr = ldb_errstring(samdb); - } else if (count == -1) { - DEBUG(10,("sldb_Search: error\n")); - result = LDAP_OTHER; - errstr = ldb_errstring(samdb); } + } else { + DEBUG(10,("sldb_Search: error\n")); + result = ret; + errstr = ldb_errstring(samdb); } done = &done_r->msg->r.SearchResultDone; done->dn = NULL; done->resultcode = result; - done->errormessage = (errstr?talloc_strdup(done_r,errstr):NULL); + done->errormessage = (errstr?talloc_strdup(done_r, errstr):NULL); done->referral = NULL; talloc_free(local_ctx); @@ -476,11 +488,11 @@ static NTSTATUS sldb_Compare(struct ldapsrv_partition *partition, struct ldapsrv struct ldapsrv_reply *compare_r; int result = LDAP_SUCCESS; struct ldb_context *samdb; - struct ldb_message **res = NULL; + struct ldb_result *res = NULL; const char *attrs[1]; const char *errstr = NULL; const char *filter = NULL; - int count; + int ret; local_ctx = talloc_named(call, 0, "sldb_Compare local_memory_context"); NT_STATUS_HAVE_NO_MEMORY(local_ctx); @@ -504,24 +516,24 @@ reply: NT_STATUS_HAVE_NO_MEMORY(compare_r); if (result == LDAP_SUCCESS) { - count = ldb_search(samdb, dn, LDB_SCOPE_BASE, filter, attrs, &res); + ret = ldb_search(samdb, dn, LDB_SCOPE_BASE, filter, attrs, &res); talloc_steal(samdb, res); - if (count == 1) { - DEBUG(10,("sldb_Compare: matched\n")); - result = LDAP_COMPARE_TRUE; - errstr = NULL; - } else if (count == 0) { + if (ret != LDB_SUCCESS) { + result = LDAP_OTHER; + errstr = ldb_errstring(samdb); + DEBUG(10,("sldb_Compare: error: %s\n", errstr)); + } else if (res->count == 0) { DEBUG(10,("sldb_Compare: doesn't matched\n")); result = LDAP_COMPARE_FALSE; errstr = NULL; - } else if (count > 1) { + } else if (res->count == 1) { + DEBUG(10,("sldb_Compare: matched\n")); + result = LDAP_COMPARE_TRUE; + errstr = NULL; + } else if (res->count > 1) { result = LDAP_OTHER; errstr = "too many objects match"; - DEBUG(10,("sldb_Compare: %d results: %s\n", count, errstr)); - } else if (count == -1) { - result = LDAP_OTHER; - errstr = ldb_errstring(samdb); - DEBUG(10,("sldb_Compare: error: %s\n", errstr)); + DEBUG(10,("sldb_Compare: %d results: %s\n", res->count, errstr)); } } -- cgit