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 int 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 ldb_ldif_write_file(ldb, stdout, &ldif);
76 ret = ldb_search_free(ldb, msgs);
78 fprintf(stderr, "search_free failed\n");
86 int main(int argc, char * const argv[])
88 struct ldb_context *ldb;
89 const char * const * attrs = NULL;
91 const char *basedn = NULL;
92 const char **options = NULL;
94 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
95 int interactive = 0, ret=0;
97 ldb_url = getenv("LDB_URL");
100 while ((opt = getopt(argc, argv, "b:H:s:o:hi")) != EOF) {
111 if (strcmp(optarg, "base") == 0) {
112 scope = LDB_SCOPE_BASE;
113 } else if (strcmp(optarg, "sub") == 0) {
114 scope = LDB_SCOPE_SUBTREE;
115 } else if (strcmp(optarg, "one") == 0) {
116 scope = LDB_SCOPE_ONELEVEL;
126 if (options == NULL) {
127 options = (const char **)malloc(sizeof(char *) * (ldbopts + 1));
129 options = (const char **)realloc(options, sizeof(char *) * (ldbopts + 1));
130 if (options == NULL) {
131 fprintf(stderr, "Out of memory!\n");
135 options[ldbopts - 1] = optarg;
136 options[ldbopts] = NULL;
147 fprintf(stderr, "You must specify a ldb URL\n\n");
154 if (argc < 1 && !interactive) {
160 attrs = (const char * const *)(argv+1);
163 ldb = ldb_connect(ldb_url, 0, options);
165 perror("ldb_connect");
169 ldb_set_debug_stderr(ldb);
173 while (fgets(line, sizeof(line), stdin)) {
174 if (do_search(ldb, basedn, scope, line, attrs) == -1) {
179 ret = do_search(ldb, basedn, scope, argv[0], attrs);