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"
40 #include "system/filesys.h"
43 static void usage(void)
45 printf("Usage: ldbsearch <options> <expression> <attrs...>\n");
47 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
48 printf(" -s base|sub|one choose search scope\n");
49 printf(" -b basedn choose baseDN\n");
50 printf(" -i read search expressions from stdin\n");
51 printf(" -S sort returned attributes\n");
52 printf(" -o options pass options like modules to activate\n");
53 printf(" e.g: -o modules:timestamps\n");
57 static int do_compare_msg(struct ldb_message **el1,
58 struct ldb_message **el2)
60 return ldb_dn_cmp((*el1)->dn, (*el2)->dn);
63 static int do_search(struct ldb_context *ldb,
67 const char *expression,
68 const char * const *attrs)
71 struct ldb_message **msgs;
73 ret = ldb_search(ldb, basedn, scope, expression, attrs, &msgs);
75 printf("search failed - %s\n", ldb_errstring(ldb));
79 printf("# returned %d records\n", ret);
82 qsort(msgs, ret, sizeof(struct ldb_message *),
83 (comparison_fn_t)do_compare_msg);
88 printf("# record %d\n", i+1);
90 ldif.changetype = LDB_CHANGETYPE_NONE;
95 * Ensure attributes are always returned in the same
96 * order. For testing, this makes comparison of old
97 * vs. new much easier.
99 ldb_msg_sort_elements(ldif.msg);
102 ldb_ldif_write_file(ldb, stdout, &ldif);
106 ret = talloc_free(msgs);
108 fprintf(stderr, "talloc_free failed\n");
116 int main(int argc, char * const argv[])
118 struct ldb_context *ldb;
119 const char * const * attrs = NULL;
121 const char *basedn = NULL;
122 const char **options = NULL;
124 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
125 int interactive = 0, sort_attribs=0, ret=0;
127 ldb_url = getenv("LDB_URL");
130 while ((opt = getopt(argc, argv, "b:H:s:o:hiS")) != EOF) {
141 if (strcmp(optarg, "base") == 0) {
142 scope = LDB_SCOPE_BASE;
143 } else if (strcmp(optarg, "sub") == 0) {
144 scope = LDB_SCOPE_SUBTREE;
145 } else if (strcmp(optarg, "one") == 0) {
146 scope = LDB_SCOPE_ONELEVEL;
159 options = ldb_options_parse(options, &ldbopts, optarg);
170 fprintf(stderr, "You must specify a ldb URL\n\n");
177 if (argc < 1 && !interactive) {
183 attrs = (const char * const *)(argv+1);
186 ldb = ldb_connect(ldb_url, LDB_FLG_RDONLY, options);
188 perror("ldb_connect");
192 ldb_set_debug_stderr(ldb);
196 while (fgets(line, sizeof(line), stdin)) {
197 if (do_search(ldb, basedn, scope, sort_attribs, line, attrs) == -1) {
202 ret = do_search(ldb, basedn, scope, sort_attribs, argv[0], attrs);