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_errors.h"
38 #include "ldb/include/ldb_private.h"
39 #include "ldb/tools/cmdline.h"
42 #include "system/filesys.h"
45 static void usage(void)
47 printf("Usage: ldbsearch <options> <expression> <attrs...>\n");
49 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
50 printf(" -s base|sub|one choose search scope\n");
51 printf(" -b basedn choose baseDN\n");
52 printf(" -i read search expressions from stdin\n");
53 printf(" -S sort returned attributes\n");
54 printf(" -o options pass options like modules to activate\n");
55 printf(" e.g: -o modules:timestamps\n");
59 struct ldb_context *ldbsearch_ldb;
61 static int do_compare_msg(struct ldb_message **el1,
62 struct ldb_message **el2)
64 return ldb_dn_compare(ldbsearch_ldb, (*el1)->dn, (*el2)->dn);
67 static int do_search(struct ldb_context *ldb,
68 const struct ldb_dn *basedn,
71 const char *expression,
72 const char * const *attrs)
75 struct ldb_result *result = NULL;
77 ret = ldb_search(ldb, basedn, scope, expression, attrs, &result);
78 if (ret != LDB_SUCCESS) {
79 printf("search failed - %s\n", ldb_errstring(ldb));
83 printf("# returned %d records\n", ret);
87 qsort(result->msgs, ret, sizeof(struct ldb_message *),
88 (comparison_fn_t)do_compare_msg);
91 for (i = 0; i < result->count; i++) {
93 printf("# record %d\n", i+1);
95 ldif.changetype = LDB_CHANGETYPE_NONE;
96 ldif.msg = result->msgs[i];
100 * Ensure attributes are always returned in the same
101 * order. For testing, this makes comparison of old
102 * vs. new much easier.
104 ldb_msg_sort_elements(ldif.msg);
107 ldb_ldif_write_file(ldb, stdout, &ldif);
111 ret = talloc_free(result);
113 fprintf(stderr, "talloc_free failed\n");
121 int main(int argc, const char **argv)
123 struct ldb_context *ldb;
124 struct ldb_dn *basedn = NULL;
125 const char * const * attrs = NULL;
126 struct ldb_cmdline *options;
128 const char *expression = "(objectclass=*)";
130 ldb = ldb_init(NULL);
132 options = ldb_cmdline_process(ldb, argc, argv, usage);
134 /* the check for '=' is for compatibility with ldapsearch */
135 if (!options->interactive &&
137 strchr(options->argv[0], '=')) {
138 expression = options->argv[0];
143 if (options->argc > 0) {
144 attrs = (const char * const *)(options->argv);
147 if (options->basedn != NULL) {
148 basedn = ldb_dn_explode(ldb, options->basedn);
149 if (basedn == NULL) {
150 fprintf(stderr, "Invalid Base DN format\n");
155 if (options->interactive) {
157 while (fgets(line, sizeof(line), stdin)) {
158 if (do_search(ldb, basedn,
159 options->scope, options->sorted, line, attrs) == -1) {
164 ret = do_search(ldb, basedn, options->scope, options->sorted,