2 ldb database library - command line handling for ldb tools
4 Copyright (C) Andrew Tridgell 2005
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
26 #include "ldb/include/includes.h"
27 #include "ldb/tools/cmdline.h"
30 #include "lib/cmdline/popt_common.h"
31 #include "auth/auth.h"
35 process command line options
37 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc, const char **argv,
40 struct ldb_cmdline options, *ret=NULL;
47 struct poptOption popt_options[] = {
49 { "url", 'H', POPT_ARG_STRING, &options.url, 0, "database URL", "URL" },
50 { "basedn", 'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
51 { "editor", 'e', POPT_ARG_STRING, &options.editor, 0, "external editor", "PROGRAM" },
52 { "scope", 's', POPT_ARG_STRING, NULL, 's', "search scope", "SCOPE" },
53 { "verbose", 'v', POPT_ARG_NONE, NULL, 'v', "increase verbosity", NULL },
54 { "interactive", 'i', POPT_ARG_NONE, &options.interactive, 0, "input from stdin", NULL },
55 { "recursive", 'r', POPT_ARG_NONE, &options.recursive, 0, "recursive delete", NULL },
56 { "num-searches", 0, POPT_ARG_INT, &options.num_searches, 0, "number of test searches", NULL },
57 { "num-records", 0, POPT_ARG_INT, &options.num_records, 0, "number of test records", NULL },
58 { "all", 'a', POPT_ARG_NONE, &options.all_records, 0, "(|(objectClass=*)(distinguishedName=*))", NULL },
59 { "nosync", 0, POPT_ARG_NONE, &options.nosync, 0, "non-synchronous transactions", NULL },
60 { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
61 { "sasl-mechanism", 0, POPT_ARG_STRING, &options.sasl_mechanism, 0, "choose SASL mechanism", "MECHANISM" },
62 { "input", 'I', POPT_ARG_STRING, &options.input, 0, "Input File", "Input" },
63 { "output", 'O', POPT_ARG_STRING, &options.output, 0, "Output File", "Output" },
64 { NULL, 'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
65 { "controls", 0, POPT_ARG_STRING, NULL, 'c', "controls", NULL },
68 POPT_COMMON_CREDENTIALS
75 r = ldb_register_samba_handlers(ldb);
82 ret = talloc_zero(ldb, struct ldb_cmdline);
91 options.url = getenv("LDB_URL");
93 /* and editor (used by ldbedit) */
94 options.editor = getenv("VISUAL");
95 if (!options.editor) {
96 options.editor = getenv("EDITOR");
98 if (!options.editor) {
99 options.editor = "vi";
102 options.scope = LDB_SCOPE_DEFAULT;
104 pc = poptGetContext(argv[0], argc, argv, popt_options,
105 POPT_CONTEXT_KEEP_FIRST);
107 while((opt = poptGetNextOpt(pc)) != -1) {
110 const char *arg = poptGetOptArg(pc);
111 if (strcmp(arg, "base") == 0) {
112 options.scope = LDB_SCOPE_BASE;
113 } else if (strcmp(arg, "sub") == 0) {
114 options.scope = LDB_SCOPE_SUBTREE;
115 } else if (strcmp(arg, "one") == 0) {
116 options.scope = LDB_SCOPE_ONELEVEL;
118 fprintf(stderr, "Invalid scope '%s'\n", arg);
129 options.options = talloc_realloc(ret, options.options,
130 const char *, num_options+3);
131 if (options.options == NULL) {
135 options.options[num_options] = poptGetOptArg(pc);
136 options.options[num_options+1] = NULL;
141 const char *cs = poptGetOptArg(pc);
145 for (p = cs, cc = 1; (q = strchr(p, ',')); cc++, p = q + 1) ;
147 options.controls = talloc_array(ret, char *, cc + 1);
148 if (options.controls == NULL) {
152 for (p = cs, cc = 0; p != NULL; cc++) {
157 options.controls[cc] = talloc_strdup(options.controls, p);
160 options.controls[cc] = talloc_strndup(options.controls, p, t-p);
164 options.controls[cc] = NULL;
169 fprintf(stderr, "Invalid option %s: %s\n",
170 poptBadOption(pc, 0), poptStrerror(opt));
176 /* setup the remaining options for the main program to use */
177 options.argv = poptGetArgs(pc);
180 while (options.argv[options.argc]) options.argc++;
185 /* all utils need some option */
186 if (ret->url == NULL) {
187 fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
192 if (strcmp(ret->url, "NONE") != 0) {
194 if (options.nosync) {
195 flags |= LDB_FLG_NOSYNC;
199 /* Must be after we have processed command line options */
202 if (ldb_set_opaque(ldb, "sessionInfo", system_session(ldb))) {
205 if (ldb_set_opaque(ldb, "credentials", cmdline_credentials)) {
208 ldb_set_utf8_fns(ldb, NULL, wrap_caseless_cmp, wrap_casefold);
210 if (ldb_connect(ldb, ret->url, flags, ret->options) != 0) {
211 fprintf(stderr, "Failed to connect to %s - %s\n",
212 ret->url, ldb_errstring(ldb));
225 struct ldb_control **parse_controls(void *mem_ctx, char **control_strings)
228 struct ldb_control **ctrl;
230 if (control_strings == NULL || control_strings[0] == NULL)
233 for (i = 0; control_strings[i]; i++);
235 ctrl = talloc_array(mem_ctx, struct ldb_control *, i + 1);
237 for (i = 0; control_strings[i]; i++) {
238 if (strncmp(control_strings[i], "dirsync:", 8) == 0) {
239 struct ldb_dirsync_control *control;
242 int crit, flags, max_attrs, ret;
245 p = &(control_strings[i][8]);
246 ret = sscanf(p, "%d:%d:%d:%1023[^$]", &crit, &flags, &max_attrs, cookie);
248 if ((ret < 3) || (crit < 0) || (crit > 1) || (flags < 0) || (max_attrs < 0)) {
249 fprintf(stderr, "invalid dirsync control syntax\n");
253 /* w2k3 seems to ignore the parameter,
254 * but w2k sends a wrong cookie when this value is to small
255 * this would cause looping forever, while getting
256 * the same data and same cookie forever
258 if (max_attrs == 0) max_attrs = 0x0FFFFFFF;
260 ctrl[i] = talloc(ctrl, struct ldb_control);
261 ctrl[i]->oid = LDB_CONTROL_DIRSYNC_OID;
262 ctrl[i]->critical = crit;
263 control = talloc(ctrl[i], struct ldb_dirsync_control);
264 control->flags = flags;
265 control->max_attributes = max_attrs;
267 control->cookie_len = ldb_base64_decode(cookie);
268 control->cookie = talloc_memdup(control, cookie, control->cookie_len);
270 control->cookie = NULL;
271 control->cookie_len = 0;
273 ctrl[i]->data = control;
278 if (strncmp(control_strings[i], "asq:", 4) == 0) {
279 struct ldb_asq_control *control;
285 p = &(control_strings[i][4]);
286 ret = sscanf(p, "%d:%255[^$]", &crit, attr);
287 if ((ret != 2) || (crit < 0) || (crit > 1) || (attr[0] == '\0')) {
288 fprintf(stderr, "invalid asq control syntax\n");
292 ctrl[i] = talloc(ctrl, struct ldb_control);
293 ctrl[i]->oid = LDB_CONTROL_ASQ_OID;
294 ctrl[i]->critical = crit;
295 control = talloc(ctrl[i], struct ldb_asq_control);
296 control->request = 1;
297 control->source_attribute = talloc_strdup(control, attr);
298 control->src_attr_len = strlen(attr);
299 ctrl[i]->data = control;
304 if (strncmp(control_strings[i], "extended_dn:", 12) == 0) {
305 struct ldb_extended_dn_control *control;
309 p = &(control_strings[i][12]);
310 ret = sscanf(p, "%d:%d", &crit, &type);
311 if ((ret != 2) || (crit < 0) || (crit > 1) || (type < 0) || (type > 1)) {
312 fprintf(stderr, "invalid extended_dn control syntax\n");
316 ctrl[i] = talloc(ctrl, struct ldb_control);
317 ctrl[i]->oid = LDB_CONTROL_EXTENDED_DN_OID;
318 ctrl[i]->critical = crit;
319 control = talloc(ctrl[i], struct ldb_extended_dn_control);
320 control->type = type;
321 ctrl[i]->data = control;
326 if (strncmp(control_strings[i], "paged_results:", 14) == 0) {
327 struct ldb_paged_control *control;
331 p = &(control_strings[i][14]);
332 ret = sscanf(p, "%d:%d", &crit, &size);
334 if ((ret != 2) || (crit < 0) || (crit > 1) || (size < 0)) {
335 fprintf(stderr, "invalid paged_results control syntax\n");
339 ctrl[i] = talloc(ctrl, struct ldb_control);
340 ctrl[i]->oid = LDB_CONTROL_PAGED_RESULTS_OID;
341 ctrl[i]->critical = crit;
342 control = talloc(ctrl[i], struct ldb_paged_control);
343 control->size = size;
344 control->cookie = NULL;
345 control->cookie_len = 0;
346 ctrl[i]->data = control;
351 if (strncmp(control_strings[i], "server_sort:", 12) == 0) {
352 struct ldb_server_sort_control **control;
360 p = &(control_strings[i][12]);
361 ret = sscanf(p, "%d:%d:%255[^:]:%127[^:]", &crit, &rev, attr, rule);
362 if ((ret < 3) || (crit < 0) || (crit > 1) || (rev < 0 ) || (rev > 1) ||attr[0] == '\0') {
363 fprintf(stderr, "invalid server_sort control syntax\n");
366 ctrl[i] = talloc(ctrl, struct ldb_control);
367 ctrl[i]->oid = LDB_CONTROL_SERVER_SORT_OID;
368 ctrl[i]->critical = crit;
369 control = talloc_array(ctrl[i], struct ldb_server_sort_control *, 2);
370 control[0] = talloc(control, struct ldb_server_sort_control);
371 control[0]->attributeName = talloc_strdup(control, attr);
373 control[0]->orderingRule = talloc_strdup(control, rule);
375 control[0]->orderingRule = NULL;
376 control[0]->reverse = rev;
378 ctrl[i]->data = control;
383 if (strncmp(control_strings[i], "notification:", 13) == 0) {
387 p = &(control_strings[i][13]);
388 ret = sscanf(p, "%d", &crit);
389 if ((ret != 1) || (crit < 0) || (crit > 1)) {
390 fprintf(stderr, "invalid notification control syntax\n");
394 ctrl[i] = talloc(ctrl, struct ldb_control);
395 ctrl[i]->oid = LDB_CONTROL_NOTIFICATION_OID;
396 ctrl[i]->critical = crit;
397 ctrl[i]->data = NULL;
402 /* no controls matched, throw an error */
403 fprintf(stderr, "Invalid control name\n");
413 /* this function check controls reply and determines if more
414 * processing is needed setting up the request controls correctly
419 * 1 all ok, more processing required
421 int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request)
426 if (reply == NULL || request == NULL) return -1;
428 for (i = 0; reply[i]; i++) {
429 if (strcmp(LDB_CONTROL_ASQ_OID, reply[i]->oid) == 0) {
430 struct ldb_asq_control *rep_control;
432 rep_control = talloc_get_type(reply[i]->data, struct ldb_asq_control);
434 /* check the result */
435 if (rep_control->result != 0) {
436 fprintf(stderr, "Warning: ASQ not performed with error: %d\n", rep_control->result);
441 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, reply[i]->oid) == 0) {
442 struct ldb_paged_control *rep_control, *req_control;
444 rep_control = talloc_get_type(reply[i]->data, struct ldb_paged_control);
445 if (rep_control->cookie_len == 0) /* we are done */
448 /* more processing required */
449 /* let's fill in the request control with the new cookie */
451 for (j = 0; request[j]; j++) {
452 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, request[j]->oid) == 0)
455 /* if there's a reply control we must find a request
456 * control matching it */
457 if (! request[j]) return -1;
459 req_control = talloc_get_type(request[j]->data, struct ldb_paged_control);
461 if (req_control->cookie)
462 talloc_free(req_control->cookie);
463 req_control->cookie = talloc_memdup(req_control,
465 rep_control->cookie_len);
466 req_control->cookie_len = rep_control->cookie_len;
473 if (strcmp(LDB_CONTROL_SORT_RESP_OID, reply[i]->oid) == 0) {
474 struct ldb_sort_resp_control *rep_control;
476 rep_control = talloc_get_type(reply[i]->data, struct ldb_sort_resp_control);
478 /* check we have a matching control in the request */
479 for (j = 0; request[j]; j++) {
480 if (strcmp(LDB_CONTROL_SERVER_SORT_OID, request[j]->oid) == 0)
484 fprintf(stderr, "Warning Server Sort reply received but no request found\n");
488 /* check the result */
489 if (rep_control->result != 0) {
490 fprintf(stderr, "Warning: Sorting not performed with error: %d\n", rep_control->result);
496 if (strcmp(LDB_CONTROL_DIRSYNC_OID, reply[i]->oid) == 0) {
497 struct ldb_dirsync_control *rep_control, *req_control;
500 rep_control = talloc_get_type(reply[i]->data, struct ldb_dirsync_control);
501 if (rep_control->cookie_len == 0) /* we are done */
504 /* more processing required */
505 /* let's fill in the request control with the new cookie */
507 for (j = 0; request[j]; j++) {
508 if (strcmp(LDB_CONTROL_DIRSYNC_OID, request[j]->oid) == 0)
511 /* if there's a reply control we must find a request
512 * control matching it */
513 if (! request[j]) return -1;
515 req_control = talloc_get_type(request[j]->data, struct ldb_dirsync_control);
517 if (req_control->cookie)
518 talloc_free(req_control->cookie);
519 req_control->cookie = talloc_memdup(req_control,
521 rep_control->cookie_len);
522 req_control->cookie_len = rep_control->cookie_len;
524 cookie = ldb_base64_encode(req_control, rep_control->cookie, rep_control->cookie_len);
525 printf("# DIRSYNC cookie returned was:\n# %s\n", cookie);
534 /* no controls matched, throw a warning */
535 fprintf(stderr, "Unknown reply control oid: %s\n", reply[i]->oid);