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 3 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, see <http://www.gnu.org/licenses/>.
25 #include "system/filesys.h"
26 #include "system/time.h"
28 #include "ldb_module.h"
29 #include "tools/cmdline.h"
31 static struct ldb_cmdline options
; /* needs to be static for older compilers */
33 enum ldb_cmdline_options
{ CMDLINE_RELAX
=1 };
35 static struct poptOption builtin_popt_options
[] = {
40 .argInfo
= POPT_ARG_STRING
,
43 .descrip
= "database URL",
49 .argInfo
= POPT_ARG_STRING
,
50 .arg
= &options
.basedn
,
58 .argInfo
= POPT_ARG_STRING
,
59 .arg
= &options
.editor
,
61 .descrip
= "external editor",
62 .argDescrip
= "PROGRAM"
67 .argInfo
= POPT_ARG_STRING
,
70 .descrip
= "search scope",
74 .longName
= "verbose",
76 .argInfo
= POPT_ARG_NONE
,
79 .descrip
= "increase verbosity",
85 .argInfo
= POPT_ARG_NONE
,
86 .arg
= &options
.tracing
,
88 .descrip
= "enable tracing",
92 .longName
= "interactive",
94 .argInfo
= POPT_ARG_NONE
,
95 .arg
= &options
.interactive
,
97 .descrip
= "input from stdin",
101 .longName
= "recursive",
103 .argInfo
= POPT_ARG_NONE
,
104 .arg
= &options
.recursive
,
106 .descrip
= "recursive delete",
110 .longName
= "modules-path",
112 .argInfo
= POPT_ARG_STRING
,
113 .arg
= &options
.modules_path
,
115 .descrip
= "modules path",
119 .longName
= "num-searches",
121 .argInfo
= POPT_ARG_INT
,
122 .arg
= &options
.num_searches
,
124 .descrip
= "number of test searches",
128 .longName
= "num-records",
130 .argInfo
= POPT_ARG_INT
,
131 .arg
= &options
.num_records
,
133 .descrip
= "number of test records",
139 .argInfo
= POPT_ARG_NONE
,
140 .arg
= &options
.all_records
,
142 .descrip
= "(|(objectClass=*)(distinguishedName=*))",
146 .longName
= "nosync",
148 .argInfo
= POPT_ARG_NONE
,
149 .arg
= &options
.nosync
,
151 .descrip
= "non-synchronous transactions",
155 .longName
= "sorted",
157 .argInfo
= POPT_ARG_NONE
,
158 .arg
= &options
.sorted
,
160 .descrip
= "sort attributes",
166 .argInfo
= POPT_ARG_STRING
,
169 .descrip
= "ldb_connect option",
170 .argDescrip
= "OPTION"
173 .longName
= "controls",
175 .argInfo
= POPT_ARG_STRING
,
178 .descrip
= "controls",
182 .longName
= "show-binary",
184 .argInfo
= POPT_ARG_NONE
,
185 .arg
= &options
.show_binary
,
187 .descrip
= "display binary LDIF",
193 .argInfo
= POPT_ARG_NONE
,
196 .descrip
= "use a paged search",
200 .longName
= "show-deleted",
202 .argInfo
= POPT_ARG_NONE
,
205 .descrip
= "show deleted objects",
209 .longName
= "show-recycled",
211 .argInfo
= POPT_ARG_NONE
,
214 .descrip
= "show recycled objects",
218 .longName
= "show-deactivated-link",
220 .argInfo
= POPT_ARG_NONE
,
223 .descrip
= "show deactivated links",
227 .longName
= "reveal",
229 .argInfo
= POPT_ARG_NONE
,
232 .descrip
= "reveal ldb internals",
238 .argInfo
= POPT_ARG_NONE
,
240 .val
= CMDLINE_RELAX
,
241 .descrip
= "pass relax control",
245 .longName
= "cross-ncs",
247 .argInfo
= POPT_ARG_NONE
,
250 .descrip
= "search across NC boundaries",
254 .longName
= "extended-dn",
256 .argInfo
= POPT_ARG_NONE
,
259 .descrip
= "show extended DNs",
265 void ldb_cmdline_help(struct ldb_context
*ldb
, const char *cmdname
, FILE *f
)
268 struct poptOption
**popt_options
= ldb_module_popt_options(ldb
);
269 pc
= poptGetContext(cmdname
, 0, NULL
, *popt_options
,
270 POPT_CONTEXT_KEEP_FIRST
);
271 poptPrintHelp(pc
, f
, 0);
275 add a control to the options structure
277 static bool add_control(TALLOC_CTX
*mem_ctx
, const char *control
)
281 /* count how many controls we already have */
282 for (i
=0; options
.controls
&& options
.controls
[i
]; i
++) ;
284 options
.controls
= talloc_realloc(mem_ctx
, options
.controls
, const char *, i
+ 2);
285 if (options
.controls
== NULL
) {
288 options
.controls
[i
] = control
;
289 options
.controls
[i
+1] = NULL
;
294 process command line options
296 static struct ldb_cmdline
*ldb_cmdline_process_internal(struct ldb_context
*ldb
,
297 int argc
, const char **argv
,
298 void (*usage
)(struct ldb_context
*),
302 struct ldb_cmdline
*ret
=NULL
;
306 unsigned int flags
= 0;
308 struct poptOption
**popt_options
;
310 /* make the ldb utilities line buffered */
313 ret
= talloc_zero(ldb
, struct ldb_cmdline
);
315 fprintf(stderr
, "Out of memory!\n");
322 options
.url
= getenv("LDB_URL");
324 /* and editor (used by ldbedit) */
325 options
.editor
= getenv("VISUAL");
326 if (!options
.editor
) {
327 options
.editor
= getenv("EDITOR");
329 if (!options
.editor
) {
330 options
.editor
= "vi";
333 options
.scope
= LDB_SCOPE_DEFAULT
;
335 popt_options
= ldb_module_popt_options(ldb
);
336 (*popt_options
) = builtin_popt_options
;
338 rc
= ldb_modules_hook(ldb
, LDB_MODULE_HOOK_CMDLINE_OPTIONS
);
339 if (rc
!= LDB_SUCCESS
) {
340 fprintf(stderr
, "ldb: failed to run command line hooks : %s\n", ldb_strerror(rc
));
344 pc
= poptGetContext(argv
[0], argc
, argv
, *popt_options
,
345 POPT_CONTEXT_KEEP_FIRST
);
347 while((opt
= poptGetNextOpt(pc
)) != -1) {
350 const char *arg
= poptGetOptArg(pc
);
351 if (strcmp(arg
, "base") == 0) {
352 options
.scope
= LDB_SCOPE_BASE
;
353 } else if (strcmp(arg
, "sub") == 0) {
354 options
.scope
= LDB_SCOPE_SUBTREE
;
355 } else if (strcmp(arg
, "one") == 0) {
356 options
.scope
= LDB_SCOPE_ONELEVEL
;
358 fprintf(stderr
, "Invalid scope '%s'\n", arg
);
369 options
.options
= talloc_realloc(ret
, options
.options
,
370 const char *, num_options
+3);
371 if (options
.options
== NULL
) {
372 fprintf(stderr
, "Out of memory!\n");
375 options
.options
[num_options
] = poptGetOptArg(pc
);
376 options
.options
[num_options
+1] = NULL
;
381 const char *cs
= poptGetOptArg(pc
);
384 for (p
= cs
; p
!= NULL
; ) {
389 c
= talloc_strdup(options
.controls
, p
);
392 c
= talloc_strndup(options
.controls
, p
, t
-p
);
395 if (c
== NULL
|| !add_control(ret
, c
)) {
396 fprintf(stderr
, __location__
": out of memory\n");
404 if (!add_control(ret
, "paged_results:1:1024")) {
405 fprintf(stderr
, __location__
": out of memory\n");
410 if (!add_control(ret
, "show_deleted:1")) {
411 fprintf(stderr
, __location__
": out of memory\n");
416 if (!add_control(ret
, "show_recycled:0")) {
417 fprintf(stderr
, __location__
": out of memory\n");
422 if (!add_control(ret
, "show_deactivated_link:0")) {
423 fprintf(stderr
, __location__
": out of memory\n");
428 if (!add_control(ret
, "reveal_internals:0")) {
429 fprintf(stderr
, __location__
": out of memory\n");
434 if (!add_control(ret
, "relax:0")) {
435 fprintf(stderr
, __location__
": out of memory\n");
440 if (!add_control(ret
, "search_options:1:2")) {
441 fprintf(stderr
, __location__
": out of memory\n");
446 if (!add_control(ret
, "extended_dn:1:1")) {
447 fprintf(stderr
, __location__
": out of memory\n");
452 fprintf(stderr
, "Invalid option %s: %s\n",
453 poptBadOption(pc
, 0), poptStrerror(opt
));
454 if (usage
) usage(ldb
);
459 /* setup the remaining options for the main program to use */
460 options
.argv
= poptGetArgs(pc
);
463 while (options
.argv
[options
.argc
]) options
.argc
++;
468 /* all utils need some option */
469 if (ret
->url
== NULL
) {
470 fprintf(stderr
, "You must supply a url with -H or with $LDB_URL\n");
471 if (usage
) usage(ldb
);
475 if (strcmp(ret
->url
, "NONE") == 0) {
479 if (options
.nosync
) {
480 flags
|= LDB_FLG_NOSYNC
;
484 flags
|= LDB_FLG_DONT_CREATE_DB
;
486 if (options
.show_binary
) {
487 flags
|= LDB_FLG_SHOW_BINARY
;
491 if (options
.tracing
) {
492 flags
|= LDB_FLG_ENABLE_TRACING
;
495 if (options
.modules_path
!= NULL
) {
496 ldb_set_modules_dir(ldb
, options
.modules_path
);
499 rc
= ldb_modules_hook(ldb
, LDB_MODULE_HOOK_CMDLINE_PRECONNECT
);
500 if (rc
!= LDB_SUCCESS
) {
501 fprintf(stderr
, "ldb: failed to run preconnect hooks : %s\n", ldb_strerror(rc
));
505 /* now connect to the ldb */
506 if (ldb_connect(ldb
, ret
->url
, flags
, ret
->options
) != LDB_SUCCESS
) {
507 fprintf(stderr
, "Failed to connect to %s - %s\n",
508 ret
->url
, ldb_errstring(ldb
));
512 rc
= ldb_modules_hook(ldb
, LDB_MODULE_HOOK_CMDLINE_POSTCONNECT
);
513 if (rc
!= LDB_SUCCESS
) {
514 fprintf(stderr
, "ldb: failed to run post connect hooks : %s\n", ldb_strerror(rc
));
522 exit(LDB_ERR_OPERATIONS_ERROR
);
526 struct ldb_cmdline
*ldb_cmdline_process_search(struct ldb_context
*ldb
,
527 int argc
, const char **argv
,
528 void (*usage
)(struct ldb_context
*))
530 return ldb_cmdline_process_internal(ldb
, argc
, argv
, usage
, true, true);
533 struct ldb_cmdline
*ldb_cmdline_process_edit(struct ldb_context
*ldb
,
534 int argc
, const char **argv
,
535 void (*usage
)(struct ldb_context
*))
537 return ldb_cmdline_process_internal(ldb
, argc
, argv
, usage
, false, true);
540 struct ldb_cmdline
*ldb_cmdline_process(struct ldb_context
*ldb
,
541 int argc
, const char **argv
,
542 void (*usage
)(struct ldb_context
*))
544 return ldb_cmdline_process_internal(ldb
, argc
, argv
, usage
, false, false);
547 /* this function check controls reply and determines if more
548 * processing is needed setting up the request controls correctly
553 * 1 all ok, more processing required
555 int handle_controls_reply(struct ldb_control
**reply
, struct ldb_control
**request
)
560 if (reply
== NULL
|| request
== NULL
) return -1;
562 for (i
= 0; reply
[i
]; i
++) {
563 if (strcmp(LDB_CONTROL_VLV_RESP_OID
, reply
[i
]->oid
) == 0) {
564 struct ldb_vlv_resp_control
*rep_control
;
566 rep_control
= talloc_get_type(reply
[i
]->data
, struct ldb_vlv_resp_control
);
567 if (rep_control
== NULL
) {
569 "Warning VLV reply OID received "
570 "with no VLV data\n");
574 /* check we have a matching control in the request */
575 for (j
= 0; request
[j
]; j
++) {
576 if (strcmp(LDB_CONTROL_VLV_REQ_OID
, request
[j
]->oid
) == 0)
580 fprintf(stderr
, "Warning VLV reply received but no request have been made\n");
584 /* check the result */
585 if (rep_control
->vlv_result
!= 0) {
586 fprintf(stderr
, "Warning: VLV not performed with error: %d\n", rep_control
->vlv_result
);
588 fprintf(stderr
, "VLV Info: target position = %d, content count = %d\n", rep_control
->targetPosition
, rep_control
->contentCount
);
594 if (strcmp(LDB_CONTROL_ASQ_OID
, reply
[i
]->oid
) == 0) {
595 struct ldb_asq_control
*rep_control
;
597 rep_control
= talloc_get_type(reply
[i
]->data
, struct ldb_asq_control
);
598 if (rep_control
== NULL
) {
600 "Warning ASQ reply OID received "
601 "with no ASQ data\n");
605 /* check the result */
606 if (rep_control
->result
!= 0) {
607 fprintf(stderr
, "Warning: ASQ not performed with error: %d\n", rep_control
->result
);
613 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID
, reply
[i
]->oid
) == 0) {
614 struct ldb_paged_control
*rep_control
, *req_control
;
616 rep_control
= talloc_get_type(reply
[i
]->data
, struct ldb_paged_control
);
617 if (rep_control
== NULL
) {
619 "Warning PAGED_RESULTS reply OID "
620 "received with no data\n");
624 if (rep_control
->cookie_len
== 0) { /* we are done */
628 /* more processing required */
629 /* let's fill in the request control with the new cookie */
631 for (j
= 0; request
[j
]; j
++) {
632 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID
, request
[j
]->oid
) == 0)
635 /* if there's a reply control we must find a request
636 * control matching it */
637 if (! request
[j
]) return -1;
639 req_control
= talloc_get_type(request
[j
]->data
, struct ldb_paged_control
);
641 if (req_control
->cookie
)
642 talloc_free(req_control
->cookie
);
643 req_control
->cookie
= (char *)talloc_memdup(
644 req_control
, rep_control
->cookie
,
645 rep_control
->cookie_len
);
646 req_control
->cookie_len
= rep_control
->cookie_len
;
653 if (strcmp(LDB_CONTROL_SORT_RESP_OID
, reply
[i
]->oid
) == 0) {
654 struct ldb_sort_resp_control
*rep_control
;
656 rep_control
= talloc_get_type(reply
[i
]->data
, struct ldb_sort_resp_control
);
657 if (rep_control
== NULL
) {
659 "Warning SORT reply OID "
660 "received with no data\n");
664 /* check we have a matching control in the request */
665 for (j
= 0; request
[j
]; j
++) {
666 if (strcmp(LDB_CONTROL_SERVER_SORT_OID
, request
[j
]->oid
) == 0)
670 fprintf(stderr
, "Warning Server Sort reply received but no request found\n");
674 /* check the result */
675 if (rep_control
->result
!= 0) {
676 fprintf(stderr
, "Warning: Sorting not performed with error: %d\n", rep_control
->result
);
682 if (strcmp(LDB_CONTROL_DIRSYNC_OID
, reply
[i
]->oid
) == 0) {
683 struct ldb_dirsync_control
*rep_control
, *req_control
;
686 rep_control
= talloc_get_type(reply
[i
]->data
, struct ldb_dirsync_control
);
687 if (rep_control
== NULL
) {
689 "Warning DIRSYNC reply OID "
690 "received with no data\n");
693 if (rep_control
->cookie_len
== 0) /* we are done */
696 /* more processing required */
697 /* let's fill in the request control with the new cookie */
699 for (j
= 0; request
[j
]; j
++) {
700 if (strcmp(LDB_CONTROL_DIRSYNC_OID
, request
[j
]->oid
) == 0)
703 /* if there's a reply control we must find a request
704 * control matching it */
705 if (! request
[j
]) return -1;
707 req_control
= talloc_get_type(request
[j
]->data
, struct ldb_dirsync_control
);
709 if (req_control
->cookie
)
710 talloc_free(req_control
->cookie
);
711 req_control
->cookie
= (char *)talloc_memdup(
712 req_control
, rep_control
->cookie
,
713 rep_control
->cookie_len
);
714 req_control
->cookie_len
= rep_control
->cookie_len
;
716 cookie
= ldb_base64_encode(req_control
, rep_control
->cookie
, rep_control
->cookie_len
);
717 printf("# DIRSYNC cookie returned was:\n# %s\n", cookie
);
721 if (strcmp(LDB_CONTROL_DIRSYNC_EX_OID
, reply
[i
]->oid
) == 0) {
722 struct ldb_dirsync_control
*rep_control
, *req_control
;
725 rep_control
= talloc_get_type(reply
[i
]->data
, struct ldb_dirsync_control
);
726 if (rep_control
== NULL
) {
728 "Warning DIRSYNC_EX reply OID "
729 "received with no data\n");
732 if (rep_control
->cookie_len
== 0) /* we are done */
735 /* more processing required */
736 /* let's fill in the request control with the new cookie */
738 for (j
= 0; request
[j
]; j
++) {
739 if (strcmp(LDB_CONTROL_DIRSYNC_EX_OID
, request
[j
]->oid
) == 0)
742 /* if there's a reply control we must find a request
743 * control matching it */
744 if (! request
[j
]) return -1;
746 req_control
= talloc_get_type(request
[j
]->data
, struct ldb_dirsync_control
);
748 if (req_control
->cookie
)
749 talloc_free(req_control
->cookie
);
750 req_control
->cookie
= (char *)talloc_memdup(
751 req_control
, rep_control
->cookie
,
752 rep_control
->cookie_len
);
753 req_control
->cookie_len
= rep_control
->cookie_len
;
755 cookie
= ldb_base64_encode(req_control
, rep_control
->cookie
, rep_control
->cookie_len
);
756 printf("# DIRSYNC_EX cookie returned was:\n# %s\n", cookie
);
761 /* no controls matched, throw a warning */
762 fprintf(stderr
, "Unknown reply control oid: %s\n", reply
[i
]->oid
);