r13609: Get in the initial work on making ldb async
[samba.git] / source / lib / ldb / tools / ldbsearch.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
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.
14
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.
19
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
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldbsearch
29  *
30  *  Description: utility for ldb search - modelled on ldapsearch
31  *
32  *  Author: Andrew Tridgell
33  */
34
35 #include "includes.h"
36 #include "ldb/include/includes.h"
37 #include "ldb/tools/cmdline.h"
38
39 static void usage(void)
40 {
41         printf("Usage: ldbsearch <options> <expression> <attrs...>\n");
42         printf("Options:\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("  -S               sort returned attributes\n");
48         printf("  -o options       pass options like modules to activate\n");
49         printf("              e.g: -o modules:timestamps\n");
50         exit(1);
51 }
52
53 static int do_compare_msg(struct ldb_message **el1,
54                           struct ldb_message **el2,
55                           void *opaque)
56 {
57         struct ldb_context *ldb = talloc_get_type(opaque, struct ldb_context);
58         return ldb_dn_compare(ldb, (*el1)->dn, (*el2)->dn);
59 }
60
61 static int do_search(struct ldb_context *ldb,
62                      const struct ldb_dn *basedn,
63                      struct ldb_cmdline *options,
64                      const char *expression,
65                      const char * const *attrs)
66 {
67         int ret, i, n;
68         int loop = 0;
69         int total = 0;
70         int refs = 0;
71         struct ldb_request req;
72         struct ldb_result *result = NULL;
73
74         req.operation = LDB_REQ_SEARCH;
75         req.op.search.base = basedn;
76         req.op.search.scope = options->scope;
77         req.op.search.tree = ldb_parse_tree(ldb, expression);
78         if (req.op.search.tree == NULL) return -1;
79         req.op.search.attrs = attrs;
80         req.op.search.res = NULL;
81         req.controls = parse_controls(ldb, options->controls);
82         if (options->controls != NULL && req.controls == NULL) return -1;
83         req.creds = NULL;
84
85         do {
86                 loop = 0;
87
88                 ret = ldb_request(ldb, &req);
89                 if (ret != LDB_SUCCESS) {
90                         printf("search failed - %s\n", ldb_errstring(ldb));
91                         if (req.op.search.res && req.op.search.res->controls) {
92                                 handle_controls_reply(req.op.search.res->controls, req.controls);
93                         }
94                         return -1;
95                 }
96
97                 result = req.op.search.res;
98
99                 if (options->sorted) {
100                         ldb_qsort(result->msgs, result->count, sizeof(struct ldb_message *),
101                                   ldb, (ldb_qsort_cmp_fn_t)do_compare_msg);
102                 }
103
104                 for (i = 0; i < result->count; i++, total++) {
105                         struct ldb_ldif ldif;
106                         printf("# record %d\n", total + 1);
107
108                         ldif.changetype = LDB_CHANGETYPE_NONE;
109                         ldif.msg = result->msgs[i];
110
111                         if (options->sorted) {
112                                 /*
113                                  * Ensure attributes are always returned in the same
114                                  * order.  For testing, this makes comparison of old
115                                  * vs. new much easier.
116                                  */
117                                 ldb_msg_sort_elements(ldif.msg);
118                         }
119         
120                         ldb_ldif_write_file(ldb, stdout, &ldif);
121                 }
122
123                 if (result->refs) {
124                         for(n = 0;result->refs[n]; n++, refs++) {
125                                 printf("# referral %d\nref: %s\n\n", refs + 1, result->refs[n]);
126                         }
127                 }
128                 
129                 if (result->controls) {
130                         if (handle_controls_reply(result->controls, req.controls) == 1)
131                                 loop = 1;
132                 }
133
134                 if (result) {
135                         ret = talloc_free(result);
136                         if (ret == -1) {
137                                 fprintf(stderr, "talloc_free failed\n");
138                                 exit(1);
139                         }
140                 }
141
142                 req.op.search.res = NULL;
143                 
144         } while(loop);
145
146         printf("# returned %d records\n# %d entries\n# %d referrals\n", total + refs, total, refs);
147
148         return 0;
149 }
150
151  int main(int argc, const char **argv)
152 {
153         struct ldb_context *ldb;
154         struct ldb_dn *basedn = NULL;
155         const char * const * attrs = NULL;
156         struct ldb_cmdline *options;
157         int ret = -1;
158         const char *expression = "(|(objectClass=*)(distinguishedName=*))";
159
160         ldb = ldb_init(NULL);
161
162         options = ldb_cmdline_process(ldb, argc, argv, usage);
163
164         /* the check for '=' is for compatibility with ldapsearch */
165         if (!options->interactive &&
166             options->argc > 0 && 
167             strchr(options->argv[0], '=')) {
168                 expression = options->argv[0];
169                 options->argv++;
170                 options->argc--;
171         }
172
173         if (options->argc > 0) {
174                 attrs = (const char * const *)(options->argv);
175         }
176
177         if (options->basedn != NULL) {
178                 basedn = ldb_dn_explode(ldb, options->basedn);
179                 if (basedn == NULL) {
180                         fprintf(stderr, "Invalid Base DN format\n");
181                         exit(1);
182                 }
183         }
184
185         if (options->interactive) {
186                 char line[1024];
187                 while (fgets(line, sizeof(line), stdin)) {
188                         if (do_search(ldb, basedn, options, line, attrs) == -1) {
189                                 ret = -1;
190                         }
191                 }
192         } else {
193                 ret = do_search(ldb, basedn, options, expression, attrs);
194         }
195
196         talloc_free(ldb);
197         return ret;
198 }