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], "asq:", 4) == 0) {
75 struct ldb_asq_control *control;
81 p = &(control_strings[i][4]);
82 ret = sscanf(p, "%d:%255[^$]", &crit, attr);
83 if ((ret != 2) || (crit < 0) || (crit > 1) || (attr[0] == '\0')) {
84 fprintf(stderr, "invalid asq control syntax\n");
88 ctrl[i] = talloc(ctrl, struct ldb_control);
89 ctrl[i]->oid = LDB_CONTROL_ASQ_OID;
90 ctrl[i]->critical = crit;
91 control = talloc(ctrl[i], struct ldb_asq_control);
93 control->source_attribute = talloc_strdup(control, attr);
94 control->src_attr_len = strlen(attr);
95 ctrl[i]->data = control;
100 if (strncmp(control_strings[i], "extended_dn:", 12) == 0) {
101 struct ldb_extended_dn_control *control;
105 p = &(control_strings[i][12]);
106 ret = sscanf(p, "%d:%d", &crit, &type);
107 if ((ret != 2) || (crit < 0) || (crit > 1) || (type < 0) || (type > 1)) {
108 fprintf(stderr, "invalid extended_dn control syntax\n");
112 ctrl[i] = talloc(ctrl, struct ldb_control);
113 ctrl[i]->oid = LDB_CONTROL_EXTENDED_DN_OID;
114 ctrl[i]->critical = crit;
115 control = talloc(ctrl[i], struct ldb_extended_dn_control);
116 control->type = type;
117 ctrl[i]->data = control;
122 if (strncmp(control_strings[i], "paged_results:", 14) == 0) {
123 struct ldb_paged_control *control;
127 p = &(control_strings[i][14]);
128 ret = sscanf(p, "%d:%d", &crit, &size);
130 if ((ret != 2) || (crit < 0) || (crit > 1) || (size < 0)) {
131 fprintf(stderr, "invalid paged_results control syntax\n");
135 ctrl[i] = talloc(ctrl, struct ldb_control);
136 ctrl[i]->oid = LDB_CONTROL_PAGED_RESULTS_OID;
137 ctrl[i]->critical = crit;
138 control = talloc(ctrl[i], struct ldb_paged_control);
139 control->size = size;
140 control->cookie = NULL;
141 control->cookie_len = 0;
142 ctrl[i]->data = control;
147 if (strncmp(control_strings[i], "server_sort:", 12) == 0) {
148 struct ldb_server_sort_control **control;
156 p = &(control_strings[i][12]);
157 ret = sscanf(p, "%d:%d:%255[^:]:%127[^:]", &crit, &rev, attr, rule);
158 if ((ret < 3) || (crit < 0) || (crit > 1) || (rev < 0 ) || (rev > 1) ||attr[0] == '\0') {
159 fprintf(stderr, "invalid server_sort control syntax\n");
162 ctrl[i] = talloc(ctrl, struct ldb_control);
163 ctrl[i]->oid = LDB_CONTROL_SERVER_SORT_OID;
164 ctrl[i]->critical = crit;
165 control = talloc_array(ctrl[i], struct ldb_server_sort_control *, 2);
166 control[0] = talloc(control, struct ldb_server_sort_control);
167 control[0]->attributeName = talloc_strdup(control, attr);
169 control[0]->orderingRule = talloc_strdup(control, rule);
171 control[0]->orderingRule = NULL;
172 control[0]->reverse = rev;
174 ctrl[i]->data = control;
179 /* no controls matched, throw an error */
180 fprintf(stderr, "Invalid control name\n");
189 /* this function check controls reply and determines if more
190 * processing is needed setting up the request controls correctly
195 * 1 all ok, more processing required
197 static int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request)
202 if (reply == NULL || request == NULL) return -1;
204 for (i = 0; reply[i]; i++) {
205 if (strcmp(LDB_CONTROL_ASQ_OID, reply[i]->oid) == 0) {
206 struct ldb_asq_control *rep_control;
208 rep_control = talloc_get_type(reply[i]->data, struct ldb_asq_control);
210 /* check the result */
211 if (rep_control->result != 0) {
212 fprintf(stderr, "Warning: ASQ not performed with error: %d\n", rep_control->result);
217 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, reply[i]->oid) == 0) {
218 struct ldb_paged_control *rep_control, *req_control;
220 rep_control = talloc_get_type(reply[i]->data, struct ldb_paged_control);
221 if (rep_control->cookie_len == 0) /* we are done */
224 /* more processing required */
225 /* let's fill in the request control with the new cookie */
227 for (j = 0; request[j]; j++) {
228 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, request[j]->oid) == 0)
231 /* if there's a reply control we must find a request
232 * control matching it */
233 if (! request[j]) return -1;
235 req_control = talloc_get_type(request[j]->data, struct ldb_paged_control);
237 if (req_control->cookie)
238 talloc_free(req_control->cookie);
239 req_control->cookie = talloc_memdup(req_control,
241 rep_control->cookie_len);
242 req_control->cookie_len = rep_control->cookie_len;
249 if (strcmp(LDB_CONTROL_SORT_RESP_OID, reply[i]->oid) == 0) {
250 struct ldb_sort_resp_control *rep_control;
252 rep_control = talloc_get_type(reply[i]->data, struct ldb_sort_resp_control);
254 /* check we have a matching control in the request */
255 for (j = 0; request[j]; j++) {
256 if (strcmp(LDB_CONTROL_SERVER_SORT_OID, request[j]->oid) == 0)
260 fprintf(stderr, "Warning Server Sort reply received but no request found\n");
264 /* check the result */
265 if (rep_control->result != 0) {
266 fprintf(stderr, "Warning: Sorting not performed with error: %d\n", rep_control->result);
272 /* no controls matched, throw a warning */
273 fprintf(stderr, "Unknown reply control oid: %s\n", reply[i]->oid);
280 static int do_search(struct ldb_context *ldb,
281 const struct ldb_dn *basedn,
282 struct ldb_cmdline *options,
283 const char *expression,
284 const char * const *attrs)
289 struct ldb_request req;
290 struct ldb_result *result = NULL;
292 req.operation = LDB_REQ_SEARCH;
293 req.op.search.base = basedn;
294 req.op.search.scope = options->scope;
295 req.op.search.tree = ldb_parse_tree(ldb, expression);
296 req.op.search.attrs = attrs;
297 req.op.search.res = NULL;
298 req.controls = parse_controls(ldb, options->controls);
299 if (options->controls != NULL && req.controls == NULL) return -1;
305 ret = ldb_request(ldb, &req);
306 if (ret != LDB_SUCCESS) {
307 printf("search failed - %s\n", ldb_errstring(ldb));
308 if (req.op.search.res && req.op.search.res->controls) {
309 /* TODO: handle_control */
315 result = req.op.search.res;
316 printf("# returned %d records\n", result->count);
318 if (options->sorted) {
319 ldb_qsort(result->msgs, ret, sizeof(struct ldb_message *),
320 ldb, (ldb_qsort_cmp_fn_t)do_compare_msg);
323 for (i = 0; i < result->count; i++, total++) {
324 struct ldb_ldif ldif;
325 printf("# record %d\n", total + 1);
327 ldif.changetype = LDB_CHANGETYPE_NONE;
328 ldif.msg = result->msgs[i];
330 if (options->sorted) {
332 * Ensure attributes are always returned in the same
333 * order. For testing, this makes comparison of old
334 * vs. new much easier.
336 ldb_msg_sort_elements(ldif.msg);
339 ldb_ldif_write_file(ldb, stdout, &ldif);
342 if (result->controls) {
343 if (handle_controls_reply(result->controls, req.controls) == 1)
348 ret = talloc_free(result);
350 fprintf(stderr, "talloc_free failed\n");
355 req.op.search.res = NULL;
362 int main(int argc, const char **argv)
364 struct ldb_context *ldb;
365 struct ldb_dn *basedn = NULL;
366 const char * const * attrs = NULL;
367 struct ldb_cmdline *options;
369 const char *expression = "(|(objectClass=*)(distinguishedName=*))";
371 ldb = ldb_init(NULL);
373 options = ldb_cmdline_process(ldb, argc, argv, usage);
375 /* the check for '=' is for compatibility with ldapsearch */
376 if (!options->interactive &&
378 strchr(options->argv[0], '=')) {
379 expression = options->argv[0];
384 if (options->argc > 0) {
385 attrs = (const char * const *)(options->argv);
388 if (options->basedn != NULL) {
389 basedn = ldb_dn_explode(ldb, options->basedn);
390 if (basedn == NULL) {
391 fprintf(stderr, "Invalid Base DN format\n");
396 if (options->interactive) {
398 while (fgets(line, sizeof(line), stdin)) {
399 if (do_search(ldb, basedn, options, line, attrs) == -1) {
404 ret = do_search(ldb, basedn, options, expression, attrs);