summaryrefslogtreecommitdiff
path: root/source4/lib/ldb/tools/ldbsearch.c
diff options
context:
space:
mode:
Diffstat (limited to 'source4/lib/ldb/tools/ldbsearch.c')
-rw-r--r--source4/lib/ldb/tools/ldbsearch.c69
1 files changed, 32 insertions, 37 deletions
diff --git a/source4/lib/ldb/tools/ldbsearch.c b/source4/lib/ldb/tools/ldbsearch.c
index 8f7ee1ce38..d10b9650da 100644
--- a/source4/lib/ldb/tools/ldbsearch.c
+++ b/source4/lib/ldb/tools/ldbsearch.c
@@ -31,15 +31,17 @@
* Author: Andrew Tridgell
*/
-#include "ldb_includes.h"
+#include "replace.h"
+#include "system/filesys.h"
+#include "system/time.h"
#include "ldb.h"
#include "tools/cmdline.h"
-static void usage(void)
+static void usage(struct ldb_context *ldb)
{
printf("Usage: ldbsearch <options> <expression> <attrs...>\n");
- ldb_cmdline_help("ldbsearch", stdout);
- exit(1);
+ ldb_cmdline_help(ldb, "ldbsearch", stdout);
+ exit(LDB_ERR_OPERATIONS_ERROR);
}
static int do_compare_msg(struct ldb_message **el1,
@@ -54,15 +56,15 @@ struct search_context {
struct ldb_control **req_ctrls;
int sort;
- int num_stored;
+ unsigned int num_stored;
struct ldb_message **store;
- int refs_stored;
+ unsigned int refs_stored;
char **refs_store;
- int entries;
- int refs;
+ unsigned int entries;
+ unsigned int refs;
- int pending;
+ unsigned int pending;
int status;
};
@@ -131,7 +133,7 @@ static int display_referral(char *referral, struct search_context *sctx)
static int search_callback(struct ldb_request *req, struct ldb_reply *ares)
{
struct search_context *sctx;
- int ret;
+ int ret = LDB_SUCCESS;
sctx = talloc_get_type(req->context, struct search_context);
@@ -172,7 +174,7 @@ static int search_callback(struct ldb_request *req, struct ldb_reply *ares)
}
talloc_free(ares);
- if (ret) {
+ if (ret != LDB_SUCCESS) {
return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
}
@@ -191,21 +193,16 @@ static int do_search(struct ldb_context *ldb,
req = NULL;
- sctx = talloc(ldb, struct search_context);
- if (!sctx) return -1;
+ sctx = talloc_zero(ldb, struct search_context);
+ if (!sctx) return LDB_ERR_OPERATIONS_ERROR;
sctx->ldb = ldb;
sctx->sort = options->sorted;
- sctx->num_stored = 0;
- sctx->refs_stored = 0;
- sctx->store = NULL;
sctx->req_ctrls = ldb_parse_control_strings(ldb, sctx, (const char **)options->controls);
if (options->controls != NULL && sctx->req_ctrls== NULL) {
printf("parsing controls failed: %s\n", ldb_errstring(ldb));
- return -1;
+ return LDB_ERR_OPERATIONS_ERROR;
}
- sctx->entries = 0;
- sctx->refs = 0;
if (basedn == NULL) {
basedn = ldb_get_default_basedn(ldb);
@@ -224,7 +221,7 @@ again:
if (ret != LDB_SUCCESS) {
talloc_free(sctx);
printf("allocating request failed: %s\n", ldb_errstring(ldb));
- return -1;
+ return ret;
}
sctx->pending = 0;
@@ -232,24 +229,23 @@ again:
ret = ldb_request(ldb, req);
if (ret != LDB_SUCCESS) {
printf("search failed - %s\n", ldb_errstring(ldb));
- return -1;
+ return ret;
}
ret = ldb_wait(req->handle, LDB_WAIT_ALL);
- if (ret != LDB_SUCCESS) {
+ if (ret != LDB_SUCCESS) {
printf("search error - %s\n", ldb_errstring(ldb));
- return -1;
+ return ret;
}
if (sctx->pending)
goto again;
if (sctx->sort && (sctx->num_stored != 0 || sctx->refs != 0)) {
- int i;
+ unsigned int i;
if (sctx->num_stored) {
- ldb_qsort(sctx->store, sctx->num_stored, sizeof(struct ldb_message *),
- ldb, (ldb_qsort_cmp_fn_t)do_compare_msg);
+ LDB_TYPESAFE_QSORT(sctx->store, sctx->num_stored, ldb, do_compare_msg);
}
for (i = 0; i < sctx->num_stored; i++) {
display_message(sctx->store[i], sctx);
@@ -260,13 +256,13 @@ again:
}
}
- printf("# returned %d records\n# %d entries\n# %d referrals\n",
+ printf("# returned %u records\n# %u entries\n# %u referrals\n",
sctx->entries + sctx->refs, sctx->entries, sctx->refs);
talloc_free(sctx);
talloc_free(req);
- return 0;
+ return LDB_SUCCESS;
}
int main(int argc, const char **argv)
@@ -277,10 +273,11 @@ int main(int argc, const char **argv)
struct ldb_cmdline *options;
int ret = -1;
const char *expression = "(|(objectClass=*)(distinguishedName=*))";
+ TALLOC_CTX *mem_ctx = talloc_new(NULL);
- ldb = ldb_init(NULL, NULL);
+ ldb = ldb_init(mem_ctx, NULL);
if (ldb == NULL) {
- return -1;
+ return LDB_ERR_OPERATIONS_ERROR;
}
options = ldb_cmdline_process(ldb, argc, argv, usage);
@@ -300,23 +297,21 @@ int main(int argc, const char **argv)
if (options->basedn != NULL) {
basedn = ldb_dn_new(ldb, ldb, options->basedn);
- if ( ! ldb_dn_validate(basedn)) {
- fprintf(stderr, "Invalid Base DN format\n");
- exit(1);
+ if (basedn == NULL) {
+ return LDB_ERR_OPERATIONS_ERROR;
}
}
if (options->interactive) {
char line[1024];
while (fgets(line, sizeof(line), stdin)) {
- if (do_search(ldb, basedn, options, line, attrs) == -1) {
- ret = -1;
- }
+ ret = do_search(ldb, basedn, options, line, attrs);
}
} else {
ret = do_search(ldb, basedn, options, expression, attrs);
}
- talloc_free(ldb);
+ talloc_free(mem_ctx);
+
return ret;
}