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/includes.h"
37 #include "ldb/tools/cmdline.h"
39 static void usage(void)
41 printf("Usage: ldbsearch <options> <expression> <attrs...>\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");
53 static int do_compare_msg(struct ldb_message **el1,
54 struct ldb_message **el2,
57 struct ldb_context *ldb = talloc_get_type(opaque, struct ldb_context);
58 return ldb_dn_compare(ldb, (*el1)->dn, (*el2)->dn);
61 static struct ldb_control **parse_controls(void *mem_ctx, char **control_strings)
64 struct ldb_control **ctrl;
66 if (control_strings == NULL || control_strings[0] == NULL)
69 for (i = 0; control_strings[i]; i++);
71 ctrl = talloc_array(mem_ctx, struct ldb_control *, i + 1);
73 for (i = 0; control_strings[i]; i++) {
74 if (strncmp(control_strings[i], "dirsync:", 8) == 0) {
75 struct ldb_dirsync_control *control;
78 int crit, flags, max_attrs, ret;
81 p = &(control_strings[i][8]);
82 ret = sscanf(p, "%d:%d:%d:%1023[^$]", &crit, &flags, &max_attrs, cookie);
84 if ((ret < 3) || (crit < 0) || (crit > 1) || (flags < 0) || (max_attrs < 0)) {
85 fprintf(stderr, "invalid dirsync control syntax\n");
89 /* w2k3 seems to ignore the parameter,
90 * but w2k sends a wrong cookie when this value is to small
91 * this would cause looping forever, while getting
92 * the same data and same cookie forever
94 if (max_attrs == 0) max_attrs = 0x0FFFFFFF;
96 ctrl[i] = talloc(ctrl, struct ldb_control);
97 ctrl[i]->oid = LDB_CONTROL_DIRSYNC_OID;
98 ctrl[i]->critical = crit;
99 control = talloc(ctrl[i], struct ldb_dirsync_control);
100 control->flags = flags;
101 control->max_attributes = max_attrs;
103 control->cookie_len = ldb_base64_decode(cookie);
104 control->cookie = talloc_memdup(control, cookie, control->cookie_len);
106 control->cookie = NULL;
107 control->cookie_len = 0;
109 ctrl[i]->data = control;
114 if (strncmp(control_strings[i], "asq:", 4) == 0) {
115 struct ldb_asq_control *control;
121 p = &(control_strings[i][4]);
122 ret = sscanf(p, "%d:%255[^$]", &crit, attr);
123 if ((ret != 2) || (crit < 0) || (crit > 1) || (attr[0] == '\0')) {
124 fprintf(stderr, "invalid asq control syntax\n");
128 ctrl[i] = talloc(ctrl, struct ldb_control);
129 ctrl[i]->oid = LDB_CONTROL_ASQ_OID;
130 ctrl[i]->critical = crit;
131 control = talloc(ctrl[i], struct ldb_asq_control);
132 control->request = 1;
133 control->source_attribute = talloc_strdup(control, attr);
134 control->src_attr_len = strlen(attr);
135 ctrl[i]->data = control;
140 if (strncmp(control_strings[i], "extended_dn:", 12) == 0) {
141 struct ldb_extended_dn_control *control;
145 p = &(control_strings[i][12]);
146 ret = sscanf(p, "%d:%d", &crit, &type);
147 if ((ret != 2) || (crit < 0) || (crit > 1) || (type < 0) || (type > 1)) {
148 fprintf(stderr, "invalid extended_dn control syntax\n");
152 ctrl[i] = talloc(ctrl, struct ldb_control);
153 ctrl[i]->oid = LDB_CONTROL_EXTENDED_DN_OID;
154 ctrl[i]->critical = crit;
155 control = talloc(ctrl[i], struct ldb_extended_dn_control);
156 control->type = type;
157 ctrl[i]->data = control;
162 if (strncmp(control_strings[i], "paged_results:", 14) == 0) {
163 struct ldb_paged_control *control;
167 p = &(control_strings[i][14]);
168 ret = sscanf(p, "%d:%d", &crit, &size);
170 if ((ret != 2) || (crit < 0) || (crit > 1) || (size < 0)) {
171 fprintf(stderr, "invalid paged_results control syntax\n");
175 ctrl[i] = talloc(ctrl, struct ldb_control);
176 ctrl[i]->oid = LDB_CONTROL_PAGED_RESULTS_OID;
177 ctrl[i]->critical = crit;
178 control = talloc(ctrl[i], struct ldb_paged_control);
179 control->size = size;
180 control->cookie = NULL;
181 control->cookie_len = 0;
182 ctrl[i]->data = control;
187 if (strncmp(control_strings[i], "server_sort:", 12) == 0) {
188 struct ldb_server_sort_control **control;
196 p = &(control_strings[i][12]);
197 ret = sscanf(p, "%d:%d:%255[^:]:%127[^:]", &crit, &rev, attr, rule);
198 if ((ret < 3) || (crit < 0) || (crit > 1) || (rev < 0 ) || (rev > 1) ||attr[0] == '\0') {
199 fprintf(stderr, "invalid server_sort control syntax\n");
202 ctrl[i] = talloc(ctrl, struct ldb_control);
203 ctrl[i]->oid = LDB_CONTROL_SERVER_SORT_OID;
204 ctrl[i]->critical = crit;
205 control = talloc_array(ctrl[i], struct ldb_server_sort_control *, 2);
206 control[0] = talloc(control, struct ldb_server_sort_control);
207 control[0]->attributeName = talloc_strdup(control, attr);
209 control[0]->orderingRule = talloc_strdup(control, rule);
211 control[0]->orderingRule = NULL;
212 control[0]->reverse = rev;
214 ctrl[i]->data = control;
219 /* no controls matched, throw an error */
220 fprintf(stderr, "Invalid control name\n");
229 /* this function check controls reply and determines if more
230 * processing is needed setting up the request controls correctly
235 * 1 all ok, more processing required
237 static int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request)
242 if (reply == NULL || request == NULL) return -1;
244 for (i = 0; reply[i]; i++) {
245 if (strcmp(LDB_CONTROL_ASQ_OID, reply[i]->oid) == 0) {
246 struct ldb_asq_control *rep_control;
248 rep_control = talloc_get_type(reply[i]->data, struct ldb_asq_control);
250 /* check the result */
251 if (rep_control->result != 0) {
252 fprintf(stderr, "Warning: ASQ not performed with error: %d\n", rep_control->result);
257 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, reply[i]->oid) == 0) {
258 struct ldb_paged_control *rep_control, *req_control;
260 rep_control = talloc_get_type(reply[i]->data, struct ldb_paged_control);
261 if (rep_control->cookie_len == 0) /* we are done */
264 /* more processing required */
265 /* let's fill in the request control with the new cookie */
267 for (j = 0; request[j]; j++) {
268 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, request[j]->oid) == 0)
271 /* if there's a reply control we must find a request
272 * control matching it */
273 if (! request[j]) return -1;
275 req_control = talloc_get_type(request[j]->data, struct ldb_paged_control);
277 if (req_control->cookie)
278 talloc_free(req_control->cookie);
279 req_control->cookie = talloc_memdup(req_control,
281 rep_control->cookie_len);
282 req_control->cookie_len = rep_control->cookie_len;
289 if (strcmp(LDB_CONTROL_SORT_RESP_OID, reply[i]->oid) == 0) {
290 struct ldb_sort_resp_control *rep_control;
292 rep_control = talloc_get_type(reply[i]->data, struct ldb_sort_resp_control);
294 /* check we have a matching control in the request */
295 for (j = 0; request[j]; j++) {
296 if (strcmp(LDB_CONTROL_SERVER_SORT_OID, request[j]->oid) == 0)
300 fprintf(stderr, "Warning Server Sort reply received but no request found\n");
304 /* check the result */
305 if (rep_control->result != 0) {
306 fprintf(stderr, "Warning: Sorting not performed with error: %d\n", rep_control->result);
312 if (strcmp(LDB_CONTROL_DIRSYNC_OID, reply[i]->oid) == 0) {
313 struct ldb_dirsync_control *rep_control, *req_control;
316 rep_control = talloc_get_type(reply[i]->data, struct ldb_dirsync_control);
317 if (rep_control->cookie_len == 0) /* we are done */
320 /* more processing required */
321 /* let's fill in the request control with the new cookie */
323 for (j = 0; request[j]; j++) {
324 if (strcmp(LDB_CONTROL_DIRSYNC_OID, request[j]->oid) == 0)
327 /* if there's a reply control we must find a request
328 * control matching it */
329 if (! request[j]) return -1;
331 req_control = talloc_get_type(request[j]->data, struct ldb_dirsync_control);
333 if (req_control->cookie)
334 talloc_free(req_control->cookie);
335 req_control->cookie = talloc_memdup(req_control,
337 rep_control->cookie_len);
338 req_control->cookie_len = rep_control->cookie_len;
340 cookie = ldb_base64_encode(req_control, rep_control->cookie, rep_control->cookie_len);
341 printf("# DIRSYNC cookie returned was:\n# %s\n", cookie);
348 /* no controls matched, throw a warning */
349 fprintf(stderr, "Unknown reply control oid: %s\n", reply[i]->oid);
356 static int do_search(struct ldb_context *ldb,
357 const struct ldb_dn *basedn,
358 struct ldb_cmdline *options,
359 const char *expression,
360 const char * const *attrs)
365 struct ldb_request req;
366 struct ldb_result *result = NULL;
368 req.operation = LDB_REQ_SEARCH;
369 req.op.search.base = basedn;
370 req.op.search.scope = options->scope;
371 req.op.search.tree = ldb_parse_tree(ldb, expression);
372 req.op.search.attrs = attrs;
373 req.op.search.res = NULL;
374 req.controls = parse_controls(ldb, options->controls);
375 if (options->controls != NULL && req.controls == NULL) return -1;
381 ret = ldb_request(ldb, &req);
382 if (ret != LDB_SUCCESS) {
383 printf("search failed - %s\n", ldb_errstring(ldb));
384 if (req.op.search.res && req.op.search.res->controls) {
386 /* TODO: handle_control */
391 result = req.op.search.res;
392 printf("# returned %d records\n", result->count);
394 if (options->sorted) {
395 ldb_qsort(result->msgs, ret, sizeof(struct ldb_message *),
396 ldb, (ldb_qsort_cmp_fn_t)do_compare_msg);
399 for (i = 0; i < result->count; i++, total++) {
400 struct ldb_ldif ldif;
401 printf("# record %d\n", total + 1);
403 ldif.changetype = LDB_CHANGETYPE_NONE;
404 ldif.msg = result->msgs[i];
406 if (options->sorted) {
408 * Ensure attributes are always returned in the same
409 * order. For testing, this makes comparison of old
410 * vs. new much easier.
412 ldb_msg_sort_elements(ldif.msg);
415 ldb_ldif_write_file(ldb, stdout, &ldif);
418 if (result->controls) {
419 if (handle_controls_reply(result->controls, req.controls) == 1)
424 ret = talloc_free(result);
426 fprintf(stderr, "talloc_free failed\n");
431 req.op.search.res = NULL;
438 int main(int argc, const char **argv)
440 struct ldb_context *ldb;
441 struct ldb_dn *basedn = NULL;
442 const char * const * attrs = NULL;
443 struct ldb_cmdline *options;
445 const char *expression = "(|(objectClass=*)(distinguishedName=*))";
447 ldb = ldb_init(NULL);
449 options = ldb_cmdline_process(ldb, argc, argv, usage);
451 /* the check for '=' is for compatibility with ldapsearch */
452 if (!options->interactive &&
454 strchr(options->argv[0], '=')) {
455 expression = options->argv[0];
460 if (options->argc > 0) {
461 attrs = (const char * const *)(options->argv);
464 if (options->basedn != NULL) {
465 basedn = ldb_dn_explode(ldb, options->basedn);
466 if (basedn == NULL) {
467 fprintf(stderr, "Invalid Base DN format\n");
472 if (options->interactive) {
474 while (fgets(line, sizeof(line), stdin)) {
475 if (do_search(ldb, basedn, options, line, attrs) == -1) {
480 ret = do_search(ldb, basedn, options, expression, attrs);