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(" -o options pass options like modules to activate\n");
52 printf(" e.g: -o modules:timestamps\n");
56 static int do_search(struct ldb_context *ldb,
59 const char *expression,
60 const char * const *attrs)
63 struct ldb_message **msgs;
65 ret = ldb_search(ldb, basedn, scope, expression, attrs, &msgs);
67 printf("search failed - %s\n", ldb_errstring(ldb));
71 printf("# returned %d records\n", ret);
75 printf("# record %d\n", i+1);
77 ldif.changetype = LDB_CHANGETYPE_NONE;
80 ldb_ldif_write_file(ldb, stdout, &ldif);
84 ret = ldb_search_free(ldb, msgs);
86 fprintf(stderr, "search_free failed\n");
94 int main(int argc, char * const argv[])
96 struct ldb_context *ldb;
97 const char * const * attrs = NULL;
99 const char *basedn = NULL;
100 const char **options = NULL;
102 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
103 int interactive = 0, ret=0;
105 ldb_url = getenv("LDB_URL");
108 while ((opt = getopt(argc, argv, "b:H:s:o:hi")) != EOF) {
119 if (strcmp(optarg, "base") == 0) {
120 scope = LDB_SCOPE_BASE;
121 } else if (strcmp(optarg, "sub") == 0) {
122 scope = LDB_SCOPE_SUBTREE;
123 } else if (strcmp(optarg, "one") == 0) {
124 scope = LDB_SCOPE_ONELEVEL;
133 options = ldb_options_parse(options, &ldbopts, optarg);
144 fprintf(stderr, "You must specify a ldb URL\n\n");
151 if (argc < 1 && !interactive) {
157 attrs = (const char * const *)(argv+1);
160 ldb = ldb_connect(ldb_url, LDB_FLG_RDONLY, options);
162 perror("ldb_connect");
166 ldb_set_debug_stderr(ldb);
170 while (fgets(line, sizeof(line), stdin)) {
171 if (do_search(ldb, basedn, scope, line, attrs) == -1) {
176 ret = do_search(ldb, basedn, scope, argv[0], attrs);