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 2 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
36 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_private.h"
38 #include "ldb/tools/cmdline.h"
41 #include "system/filesys.h"
44 static void usage(void)
46 printf("Usage: ldbsearch <options> <expression> <attrs...>\n");
48 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
49 printf(" -s base|sub|one choose search scope\n");
50 printf(" -b basedn choose baseDN\n");
51 printf(" -i read search expressions from stdin\n");
52 printf(" -S sort returned attributes\n");
53 printf(" -o options pass options like modules to activate\n");
54 printf(" e.g: -o modules:timestamps\n");
58 static int do_compare_msg(struct ldb_message **el1,
59 struct ldb_message **el2)
61 return ldb_dn_cmp((*el1)->dn, (*el2)->dn);
64 static int do_search(struct ldb_context *ldb,
68 const char *expression,
69 const char * const *attrs)
72 struct ldb_message **msgs;
74 ret = ldb_search(ldb, basedn, scope, expression, attrs, &msgs);
76 printf("search failed - %s\n", ldb_errstring(ldb));
80 printf("# returned %d records\n", ret);
83 qsort(msgs, ret, sizeof(struct ldb_message *),
84 (comparison_fn_t)do_compare_msg);
89 printf("# record %d\n", i+1);
91 ldif.changetype = LDB_CHANGETYPE_NONE;
96 * Ensure attributes are always returned in the same
97 * order. For testing, this makes comparison of old
98 * vs. new much easier.
100 ldb_msg_sort_elements(ldif.msg);
103 ldb_ldif_write_file(ldb, stdout, &ldif);
107 ret = talloc_free(msgs);
109 fprintf(stderr, "talloc_free failed\n");
117 int main(int argc, const char **argv)
119 struct ldb_context *ldb;
120 const char * const * attrs = NULL;
121 struct ldb_cmdline *options;
124 ldb = ldb_init(NULL);
126 options = ldb_cmdline_process(ldb, argc, argv, usage);
128 if (options->argc < 1 && !options->interactive) {
133 if (options->argc > 1) {
134 attrs = (const char * const *)(options->argv+1);
137 ret = ldb_connect(ldb, options->url, LDB_FLG_RDONLY, options->options);
139 fprintf(stderr, "Failed to connect to %s - %s\n",
140 options->url, ldb_errstring(ldb));
145 if (options->interactive) {
147 while (fgets(line, sizeof(line), stdin)) {
148 if (do_search(ldb, options->basedn,
149 options->scope, options->sorted, line, attrs) == -1) {
154 ret = do_search(ldb, options->basedn, options->scope, options->sorted,
155 options->argv[0], attrs);