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
37 static void usage(void)
39 printf("Usage: ldbsearch <options> <expression> <attrs...>\n");
41 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
42 printf(" -s base|sub|one choose search scope\n");
43 printf(" -b basedn choose baseDN\n");
44 printf(" -i read search expressions from stdin\n");
48 static void do_search(struct ldb_context *ldb,
51 const char *expression,
52 const char * const *attrs)
55 struct ldb_message **msgs;
57 ret = ldb_search(ldb, basedn, scope, expression, attrs, &msgs);
59 printf("search failed - %s\n", ldb_errstring(ldb));
63 printf("# returned %d records\n", ret);
67 printf("# record %d\n", i+1);
69 ldif.changetype = LDB_CHANGETYPE_NONE;
72 ldif_write_file(stdout, &ldif);
76 ret = ldb_search_free(ldb, msgs);
78 fprintf(stderr, "search_free failed\n");
84 int main(int argc, char * const argv[])
86 struct ldb_context *ldb;
87 const char * const * attrs = NULL;
89 const char *basedn = NULL;
91 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
94 ldb_url = getenv("LDB_URL");
96 while ((opt = getopt(argc, argv, "b:H:s:hi")) != EOF) {
107 if (strcmp(optarg, "base") == 0) {
108 scope = LDB_SCOPE_BASE;
109 } else if (strcmp(optarg, "sub") == 0) {
110 scope = LDB_SCOPE_SUBTREE;
111 } else if (strcmp(optarg, "one") == 0) {
112 scope = LDB_SCOPE_ONELEVEL;
128 fprintf(stderr, "You must specify a ldb URL\n\n");
135 if (argc < 1 && !interactive) {
144 ldb = ldb_connect(ldb_url, 0, NULL);
146 perror("ldb_connect");
152 while (fgets(line, sizeof(line), stdin)) {
153 do_search(ldb, basedn, scope, line, attrs);
156 do_search(ldb, basedn, scope, argv[0], attrs);