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"
39 static void usage(void)
41 printf("Usage: ldbsearch <options> <expression> <attrs...>\n");
43 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
44 printf(" -s base|sub|one choose search scope\n");
45 printf(" -b basedn choose baseDN\n");
46 printf(" -i read search expressions from stdin\n");
47 printf(" -o options pass options like modules to activate\n");
48 printf(" e.g: -o modules:timestamps\n");
52 static int do_search(struct ldb_context *ldb,
55 const char *expression,
56 const char * const *attrs)
59 struct ldb_message **msgs;
61 ret = ldb_search(ldb, basedn, scope, expression, attrs, &msgs);
63 printf("search failed - %s\n", ldb_errstring(ldb));
67 printf("# returned %d records\n", ret);
71 printf("# record %d\n", i+1);
73 ldif.changetype = LDB_CHANGETYPE_NONE;
76 ldb_ldif_write_file(ldb, stdout, &ldif);
80 ret = ldb_search_free(ldb, msgs);
82 fprintf(stderr, "search_free failed\n");
90 int main(int argc, char * const argv[])
92 struct ldb_context *ldb;
93 const char * const * attrs = NULL;
95 const char *basedn = NULL;
96 const char **options = NULL;
98 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
99 int interactive = 0, ret=0;
101 ldb_url = getenv("LDB_URL");
104 while ((opt = getopt(argc, argv, "b:H:s:o:hi")) != EOF) {
115 if (strcmp(optarg, "base") == 0) {
116 scope = LDB_SCOPE_BASE;
117 } else if (strcmp(optarg, "sub") == 0) {
118 scope = LDB_SCOPE_SUBTREE;
119 } else if (strcmp(optarg, "one") == 0) {
120 scope = LDB_SCOPE_ONELEVEL;
129 options = ldb_options_parse(options, &ldbopts, optarg);
140 fprintf(stderr, "You must specify a ldb URL\n\n");
147 if (argc < 1 && !interactive) {
153 attrs = (const char * const *)(argv+1);
156 ldb = ldb_connect(ldb_url, 0, options);
158 perror("ldb_connect");
162 ldb_set_debug_stderr(ldb);
166 while (fgets(line, sizeof(line), stdin)) {
167 if (do_search(ldb, basedn, scope, line, attrs) == -1) {
172 ret = do_search(ldb, basedn, scope, argv[0], attrs);