4
Copyright (C) Andrew Tridgell 2004
6
** NOTE! The following LGPL license applies to the ldb
7
** library. This does NOT imply that all of Samba is released
10
This library is free software; you can redistribute it and/or
11
modify it under the terms of the GNU Lesser General Public
12
License as published by the Free Software Foundation; either
13
version 3 of the License, or (at your option) any later version.
15
This library is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
Lesser General Public License for more details.
20
You should have received a copy of the GNU Lesser General Public
21
License along with this library; if not, see <http://www.gnu.org/licenses/>.
27
* Component: ldbsearch
29
* Description: utility for ldb search - modelled on ldapsearch
31
* Author: Andrew Tridgell
35
#include "ldb/include/includes.h"
36
#include "ldb/tools/cmdline.h"
38
static void usage(void)
40
printf("Usage: ldbsearch <options> <expression> <attrs...>\n");
42
printf(" -H ldb_url choose the database (or $LDB_URL)\n");
43
printf(" -s base|sub|one choose search scope\n");
44
printf(" -b basedn choose baseDN\n");
45
printf(" -i read search expressions from stdin\n");
46
printf(" -S sort returned attributes\n");
47
printf(" -o options pass options like modules to activate\n");
48
printf(" e.g: -o modules:timestamps\n");
52
static int do_compare_msg(struct ldb_message **el1,
53
struct ldb_message **el2,
56
struct ldb_context *ldb = talloc_get_type(opaque, struct ldb_context);
57
return ldb_dn_compare(ldb, (*el1)->dn, (*el2)->dn);
60
struct search_context {
61
struct ldb_control **req_ctrls;
65
struct ldb_message **store;
75
static int store_message(struct ldb_message *msg, struct search_context *sctx) {
77
sctx->store = talloc_realloc(sctx, sctx->store, struct ldb_message *, sctx->num_stored + 2);
79
fprintf(stderr, "talloc_realloc failed while storing messages\n");
83
sctx->store[sctx->num_stored] = talloc_move(sctx->store, &msg);
85
sctx->store[sctx->num_stored] = NULL;
90
static int store_referral(char *referral, struct search_context *sctx) {
92
sctx->refs_store = talloc_realloc(sctx, sctx->refs_store, char *, sctx->refs + 2);
93
if (!sctx->refs_store) {
94
fprintf(stderr, "talloc_realloc failed while storing referrals\n");
98
sctx->refs_store[sctx->refs] = talloc_move(sctx->refs_store, &referral);
100
sctx->refs_store[sctx->refs] = NULL;
105
static int display_message(struct ldb_context *ldb, struct ldb_message *msg, struct search_context *sctx) {
106
struct ldb_ldif ldif;
109
printf("# record %d\n", sctx->entries);
111
ldif.changetype = LDB_CHANGETYPE_NONE;
116
* Ensure attributes are always returned in the same
117
* order. For testing, this makes comparison of old
118
* vs. new much easier.
120
ldb_msg_sort_elements(ldif.msg);
123
ldb_ldif_write_file(ldb, stdout, &ldif);
128
static int display_referral(char *referral, struct search_context *sctx)
132
printf("# Referral\nref: %s\n\n", referral);
137
static int search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
139
struct search_context *sctx = talloc_get_type(context, struct search_context);
142
switch (ares->type) {
144
case LDB_REPLY_ENTRY:
146
ret = store_message(ares->message, sctx);
148
ret = display_message(ldb, ares->message, sctx);
152
case LDB_REPLY_REFERRAL:
154
ret = store_referral(ares->referral, sctx);
156
ret = display_referral(ares->referral, sctx);
161
if (ares->controls) {
162
if (handle_controls_reply(ares->controls, sctx->req_ctrls) == 1)
169
fprintf(stderr, "unknown Reply Type\n");
170
return LDB_ERR_OTHER;
173
if (talloc_free(ares) == -1) {
174
fprintf(stderr, "talloc_free failed\n");
176
return LDB_ERR_OPERATIONS_ERROR;
180
return LDB_ERR_OPERATIONS_ERROR;
186
static int do_search(struct ldb_context *ldb,
187
const struct ldb_dn *basedn,
188
struct ldb_cmdline *options,
189
const char *expression,
190
const char * const *attrs)
192
struct ldb_request *req;
193
struct search_context *sctx;
196
req = talloc(ldb, struct ldb_request);
199
sctx = talloc(req, struct search_context);
200
if (!sctx) return -1;
202
sctx->sort = options->sorted;
203
sctx->num_stored = 0;
205
sctx->req_ctrls = parse_controls(ldb, options->controls);
206
if (options->controls != NULL && sctx->req_ctrls== NULL) return -1;
210
if (basedn == NULL) {
211
basedn = ldb_get_default_basedn(ldb);
214
req->operation = LDB_SEARCH;
215
req->op.search.base = basedn;
216
req->op.search.scope = options->scope;
217
req->op.search.tree = ldb_parse_tree(req, expression);
218
if (req->op.search.tree == NULL) return -1;
219
req->op.search.attrs = attrs;
220
req->controls = sctx->req_ctrls;
222
req->callback = &search_callback;
223
ldb_set_timeout(ldb, req, 0); /* TODO: make this settable by command line */
228
ret = ldb_request(ldb, req);
229
if (ret != LDB_SUCCESS) {
230
printf("search failed - %s\n", ldb_errstring(ldb));
234
ret = ldb_wait(req->handle, LDB_WAIT_ALL);
235
if (ret != LDB_SUCCESS) {
236
printf("search error - %s\n", ldb_errstring(ldb));
243
if (sctx->sort && sctx->num_stored != 0) {
246
ldb_qsort(sctx->store, ret, sizeof(struct ldb_message *),
247
ldb, (ldb_qsort_cmp_fn_t)do_compare_msg);
250
fprintf(stderr, "An error occurred while sorting messages\n");
254
for (i = 0; i < sctx->num_stored; i++) {
255
display_message(ldb, sctx->store[i], sctx);
258
for (i = 0; i < sctx->refs; i++) {
259
display_referral(sctx->refs_store[i], sctx);
263
printf("# returned %d records\n# %d entries\n# %d referrals\n",
264
sctx->entries + sctx->refs, sctx->entries, sctx->refs);
271
int main(int argc, const char **argv)
273
struct ldb_context *ldb;
274
struct ldb_dn *basedn = NULL;
275
const char * const * attrs = NULL;
276
struct ldb_cmdline *options;
278
const char *expression = "(|(objectClass=*)(distinguishedName=*))";
282
ldb = ldb_init(NULL);
284
options = ldb_cmdline_process(ldb, argc, argv, usage);
286
/* the check for '=' is for compatibility with ldapsearch */
287
if (!options->interactive &&
289
strchr(options->argv[0], '=')) {
290
expression = options->argv[0];
295
if (options->argc > 0) {
296
attrs = (const char * const *)(options->argv);
299
if (options->basedn != NULL) {
300
basedn = ldb_dn_explode(ldb, options->basedn);
301
if (basedn == NULL) {
302
fprintf(stderr, "Invalid Base DN format\n");
307
if (options->interactive) {
309
while (fgets(line, sizeof(line), stdin)) {
310
if (do_search(ldb, basedn, options, line, attrs) == -1) {
315
ret = do_search(ldb, basedn, options, expression, attrs);