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, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Component: ldbsearch
30 * Description: utility for ldb search - modelled on ldapsearch
32 * Author: Andrew Tridgell
35 #include "ldb_includes.h"
36 #include "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 return ldb_dn_compare((*el1)->dn, (*el2)->dn);
59 struct search_context {
60 struct ldb_control **req_ctrls;
64 struct ldb_message **store;
74 static int store_message(struct ldb_message *msg, struct search_context *sctx) {
76 sctx->store = talloc_realloc(sctx, sctx->store, struct ldb_message *, sctx->num_stored + 2);
78 fprintf(stderr, "talloc_realloc failed while storing messages\n");
82 sctx->store[sctx->num_stored] = talloc_move(sctx->store, &msg);
84 sctx->store[sctx->num_stored] = NULL;
89 static int store_referral(char *referral, struct search_context *sctx) {
91 sctx->refs_store = talloc_realloc(sctx, sctx->refs_store, char *, sctx->refs + 2);
92 if (!sctx->refs_store) {
93 fprintf(stderr, "talloc_realloc failed while storing referrals\n");
97 sctx->refs_store[sctx->refs] = talloc_move(sctx->refs_store, &referral);
99 sctx->refs_store[sctx->refs] = NULL;
104 static int display_message(struct ldb_context *ldb, struct ldb_message *msg, struct search_context *sctx) {
105 struct ldb_ldif ldif;
108 printf("# record %d\n", sctx->entries);
110 ldif.changetype = LDB_CHANGETYPE_NONE;
115 * Ensure attributes are always returned in the same
116 * order. For testing, this makes comparison of old
117 * vs. new much easier.
119 ldb_msg_sort_elements(ldif.msg);
122 ldb_ldif_write_file(ldb, stdout, &ldif);
127 static int display_referral(char *referral, struct search_context *sctx)
131 printf("# Referral\nref: %s\n\n", referral);
136 static int search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
138 struct search_context *sctx = talloc_get_type(context, struct search_context);
141 switch (ares->type) {
143 case LDB_REPLY_ENTRY:
145 ret = store_message(ares->message, sctx);
147 ret = display_message(ldb, ares->message, sctx);
151 case LDB_REPLY_REFERRAL:
153 ret = store_referral(ares->referral, sctx);
155 ret = display_referral(ares->referral, sctx);
160 if (ares->controls) {
161 if (handle_controls_reply(ares->controls, sctx->req_ctrls) == 1)
168 fprintf(stderr, "unknown Reply Type\n");
169 return LDB_ERR_OTHER;
172 if (talloc_free(ares) == -1) {
173 fprintf(stderr, "talloc_free failed\n");
175 return LDB_ERR_OPERATIONS_ERROR;
179 return LDB_ERR_OPERATIONS_ERROR;
185 static int do_search(struct ldb_context *ldb,
186 struct ldb_dn *basedn,
187 struct ldb_cmdline *options,
188 const char *expression,
189 const char * const *attrs)
191 struct ldb_request *req;
192 struct search_context *sctx;
195 req = talloc(ldb, struct ldb_request);
198 sctx = talloc(req, struct search_context);
199 if (!sctx) return -1;
201 sctx->sort = options->sorted;
202 sctx->num_stored = 0;
204 sctx->req_ctrls = ldb_parse_control_strings(ldb, sctx, (const char **)options->controls);
205 if (options->controls != NULL && sctx->req_ctrls== NULL) {
206 printf("parsing controls failed: %s\n", ldb_errstring(ldb));
212 if (basedn == NULL) {
213 basedn = ldb_get_default_basedn(ldb);
216 req->operation = LDB_SEARCH;
217 req->op.search.base = basedn;
218 req->op.search.scope = options->scope;
219 req->op.search.tree = ldb_parse_tree(req, expression);
220 if (req->op.search.tree == NULL) return -1;
221 req->op.search.attrs = attrs;
222 req->controls = sctx->req_ctrls;
224 req->callback = &search_callback;
225 ldb_set_timeout(ldb, req, 0); /* TODO: make this settable by command line */
230 ret = ldb_request(ldb, req);
231 if (ret != LDB_SUCCESS) {
232 printf("search failed - %s\n", ldb_errstring(ldb));
236 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
237 if (ret != LDB_SUCCESS) {
238 printf("search error - %s\n", ldb_errstring(ldb));
245 if (sctx->sort && sctx->num_stored != 0) {
248 ldb_qsort(sctx->store, ret, sizeof(struct ldb_message *),
249 ldb, (ldb_qsort_cmp_fn_t)do_compare_msg);
252 fprintf(stderr, "An error occurred while sorting messages\n");
256 for (i = 0; i < sctx->num_stored; i++) {
257 display_message(ldb, sctx->store[i], sctx);
260 for (i = 0; i < sctx->refs; i++) {
261 display_referral(sctx->refs_store[i], sctx);
265 printf("# returned %d records\n# %d entries\n# %d referrals\n",
266 sctx->entries + sctx->refs, sctx->entries, sctx->refs);
273 int main(int argc, const char **argv)
275 struct ldb_context *ldb;
276 struct ldb_dn *basedn = NULL;
277 const char * const * attrs = NULL;
278 struct ldb_cmdline *options;
280 const char *expression = "(|(objectClass=*)(distinguishedName=*))";
284 ldb = ldb_init(NULL);
286 options = ldb_cmdline_process(ldb, argc, argv, usage);
288 /* the check for '=' is for compatibility with ldapsearch */
289 if (!options->interactive &&
291 strchr(options->argv[0], '=')) {
292 expression = options->argv[0];
297 if (options->argc > 0) {
298 attrs = (const char * const *)(options->argv);
301 if (options->basedn != NULL) {
302 basedn = ldb_dn_new(ldb, ldb, options->basedn);
303 if ( ! ldb_dn_validate(basedn)) {
304 fprintf(stderr, "Invalid Base DN format\n");
309 if (options->interactive) {
311 while (fgets(line, sizeof(line), stdin)) {
312 if (do_search(ldb, basedn, options, line, attrs) == -1) {
317 ret = do_search(ldb, basedn, options, expression, attrs);