s4-ldb: use a parent context in the ldb utils
[kamenim/samba.git] / source4 / 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 3 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, see <http://www.gnu.org/licenses/>.
22 */
23
24 /*
25  *  Name: ldb
26  *
27  *  Component: ldbsearch
28  *
29  *  Description: utility for ldb search - modelled on ldapsearch
30  *
31  *  Author: Andrew Tridgell
32  */
33
34 #include "ldb_includes.h"
35 #include "ldb.h"
36 #include "tools/cmdline.h"
37
38 static void usage(void)
39 {
40         printf("Usage: ldbsearch <options> <expression> <attrs...>\n");
41         ldb_cmdline_help("ldbsearch", stdout);
42         exit(1);
43 }
44
45 static int do_compare_msg(struct ldb_message **el1,
46                           struct ldb_message **el2,
47                           void *opaque)
48 {
49         return ldb_dn_compare((*el1)->dn, (*el2)->dn);
50 }
51
52 struct search_context {
53         struct ldb_context *ldb;
54         struct ldb_control **req_ctrls;
55
56         int sort;
57         unsigned int num_stored;
58         struct ldb_message **store;
59         unsigned int refs_stored;
60         char **refs_store;
61
62         unsigned int entries;
63         unsigned int refs;
64
65         unsigned int pending;
66         int status;
67 };
68
69 static int store_message(struct ldb_message *msg, struct search_context *sctx) {
70
71         sctx->store = talloc_realloc(sctx, sctx->store, struct ldb_message *, sctx->num_stored + 2);
72         if (!sctx->store) {
73                 fprintf(stderr, "talloc_realloc failed while storing messages\n");
74                 return -1;
75         }
76
77         sctx->store[sctx->num_stored] = talloc_move(sctx->store, &msg);
78         sctx->num_stored++;
79         sctx->store[sctx->num_stored] = NULL;
80
81         return 0;
82 }
83
84 static int store_referral(char *referral, struct search_context *sctx) {
85
86         sctx->refs_store = talloc_realloc(sctx, sctx->refs_store, char *, sctx->refs_stored + 2);
87         if (!sctx->refs_store) {
88                 fprintf(stderr, "talloc_realloc failed while storing referrals\n");
89                 return -1;
90         }
91
92         sctx->refs_store[sctx->refs_stored] = talloc_move(sctx->refs_store, &referral);
93         sctx->refs_stored++;
94         sctx->refs_store[sctx->refs_stored] = NULL;
95
96         return 0;
97 }
98
99 static int display_message(struct ldb_message *msg, struct search_context *sctx) {
100         struct ldb_ldif ldif;
101
102         sctx->entries++;
103         printf("# record %d\n", sctx->entries);
104
105         ldif.changetype = LDB_CHANGETYPE_NONE;
106         ldif.msg = msg;
107
108         if (sctx->sort) {
109         /*
110          * Ensure attributes are always returned in the same
111          * order.  For testing, this makes comparison of old
112          * vs. new much easier.
113          */
114                 ldb_msg_sort_elements(ldif.msg);
115         }
116
117         ldb_ldif_write_file(sctx->ldb, stdout, &ldif);
118
119         return 0;
120 }
121
122 static int display_referral(char *referral, struct search_context *sctx)
123 {
124
125         sctx->refs++;
126         printf("# Referral\nref: %s\n\n", referral);
127
128         return 0;
129 }
130
131 static int search_callback(struct ldb_request *req, struct ldb_reply *ares)
132 {
133         struct search_context *sctx;
134         int ret;
135
136         sctx = talloc_get_type(req->context, struct search_context);
137
138         if (!ares) {
139                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
140         }
141         if (ares->error != LDB_SUCCESS) {
142                 return ldb_request_done(req, ares->error);
143         }
144         
145         switch (ares->type) {
146         case LDB_REPLY_ENTRY:
147                 if (sctx->sort) {
148                         ret = store_message(ares->message, sctx);
149                 } else {
150                         ret = display_message(ares->message, sctx);
151                 }
152                 break;
153
154         case LDB_REPLY_REFERRAL:
155                 if (sctx->sort) {
156                         ret = store_referral(ares->referral, sctx);
157                 } else {
158                         ret = display_referral(ares->referral, sctx);
159                 }
160                 if (ret) {
161                         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
162                 }
163                 break;
164
165         case LDB_REPLY_DONE:
166                 if (ares->controls) {
167                         if (handle_controls_reply(ares->controls, sctx->req_ctrls) == 1)
168                                 sctx->pending = 1;
169                 }
170                 talloc_free(ares);
171                 return ldb_request_done(req, LDB_SUCCESS);
172         }
173
174         talloc_free(ares);
175         if (ret) {
176                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
177         }
178
179         return LDB_SUCCESS;
180 }
181
182 static int do_search(struct ldb_context *ldb,
183                      struct ldb_dn *basedn,
184                      struct ldb_cmdline *options,
185                      const char *expression,
186                      const char * const *attrs)
187 {
188         struct ldb_request *req;
189         struct search_context *sctx;
190         int ret;
191
192         req = NULL;
193         
194         sctx = talloc_zero(ldb, struct search_context);
195         if (!sctx) return -1;
196
197         sctx->ldb = ldb;
198         sctx->sort = options->sorted;
199         sctx->req_ctrls = ldb_parse_control_strings(ldb, sctx, (const char **)options->controls);
200         if (options->controls != NULL &&  sctx->req_ctrls== NULL) {
201                 printf("parsing controls failed: %s\n", ldb_errstring(ldb));
202                 return -1;
203         }
204
205         if (basedn == NULL) {
206                 basedn = ldb_get_default_basedn(ldb);
207         }
208
209 again:
210         /* free any previous requests */
211         if (req) talloc_free(req);
212
213         ret = ldb_build_search_req(&req, ldb, ldb,
214                                    basedn, options->scope,
215                                    expression, attrs,
216                                    sctx->req_ctrls,
217                                    sctx, search_callback,
218                                    NULL);
219         if (ret != LDB_SUCCESS) {
220                 talloc_free(sctx);
221                 printf("allocating request failed: %s\n", ldb_errstring(ldb));
222                 return -1;
223         }
224
225         sctx->pending = 0;
226
227         ret = ldb_request(ldb, req);
228         if (ret != LDB_SUCCESS) {
229                 printf("search failed - %s\n", ldb_errstring(ldb));
230                 return -1;
231         }
232
233         ret = ldb_wait(req->handle, LDB_WAIT_ALL);
234         if (ret != LDB_SUCCESS) {
235                 printf("search error - %s\n", ldb_errstring(ldb));
236                 return -1;
237         }
238
239         if (sctx->pending)
240                 goto again;
241
242         if (sctx->sort && (sctx->num_stored != 0 || sctx->refs != 0)) {
243                 unsigned int i;
244
245                 if (sctx->num_stored) {
246                         LDB_TYPESAFE_QSORT(sctx->store, sctx->num_stored, ldb, do_compare_msg);
247                 }
248                 for (i = 0; i < sctx->num_stored; i++) {
249                         display_message(sctx->store[i], sctx);
250                 }
251
252                 for (i = 0; i < sctx->refs_stored; i++) {
253                         display_referral(sctx->refs_store[i], sctx);
254                 }
255         }
256
257         printf("# returned %d records\n# %d entries\n# %d referrals\n",
258                 sctx->entries + sctx->refs, sctx->entries, sctx->refs);
259
260         talloc_free(sctx);
261         talloc_free(req);
262
263         return 0;
264 }
265
266 int main(int argc, const char **argv)
267 {
268         struct ldb_context *ldb;
269         struct ldb_dn *basedn = NULL;
270         const char * const * attrs = NULL;
271         struct ldb_cmdline *options;
272         int ret = -1;
273         const char *expression = "(|(objectClass=*)(distinguishedName=*))";
274         TALLOC_CTX *mem_ctx = talloc_new(NULL);
275
276         ldb = ldb_init(mem_ctx, NULL);
277         if (ldb == NULL) {
278                 return -1;
279         }
280
281         options = ldb_cmdline_process(ldb, argc, argv, usage);
282
283         /* the check for '=' is for compatibility with ldapsearch */
284         if (!options->interactive &&
285             options->argc > 0 && 
286             strchr(options->argv[0], '=')) {
287                 expression = options->argv[0];
288                 options->argv++;
289                 options->argc--;
290         }
291
292         if (options->argc > 0) {
293                 attrs = (const char * const *)(options->argv);
294         }
295
296         if (options->basedn != NULL) {
297                 basedn = ldb_dn_new(ldb, ldb, options->basedn);
298                 if ( ! ldb_dn_validate(basedn)) {
299                         fprintf(stderr, "Invalid Base DN format\n");
300                         exit(1);
301                 }
302         }
303
304         if (options->interactive) {
305                 char line[1024];
306                 while (fgets(line, sizeof(line), stdin)) {
307                         if (do_search(ldb, basedn, options, line, attrs) == -1) {
308                                 ret = -1;
309                         }
310                 }
311         } else {
312                 ret = do_search(ldb, basedn, options, expression, attrs);
313         }
314
315         talloc_free(mem_ctx);
316
317         return ret;
318 }