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/>.
24 #include "ldb_includes.h"
26 #include "tools/cmdline.h"
28 #if (_SAMBA_BUILD_ >= 4)
30 #include "lib/cmdline/popt_common.h"
31 #include "lib/ldb-samba/ldif_handlers.h"
32 #include "auth/gensec/gensec.h"
33 #include "auth/auth.h"
35 #include "param/param.h"
38 static struct ldb_cmdline options; /* needs to be static for older compilers */
40 static struct poptOption popt_options[] = {
42 { "url", 'H', POPT_ARG_STRING, &options.url, 0, "database URL", "URL" },
43 { "basedn", 'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
44 { "editor", 'e', POPT_ARG_STRING, &options.editor, 0, "external editor", "PROGRAM" },
45 { "scope", 's', POPT_ARG_STRING, NULL, 's', "search scope", "SCOPE" },
46 { "verbose", 'v', POPT_ARG_NONE, NULL, 'v', "increase verbosity", NULL },
47 { "trace", 0, POPT_ARG_NONE, &options.tracing, 0, "enable tracing", NULL },
48 { "interactive", 'i', POPT_ARG_NONE, &options.interactive, 0, "input from stdin", NULL },
49 { "recursive", 'r', POPT_ARG_NONE, &options.recursive, 0, "recursive delete", NULL },
50 { "modules-path", 0, POPT_ARG_STRING, &options.modules_path, 0, "modules path", "PATH" },
51 { "num-searches", 0, POPT_ARG_INT, &options.num_searches, 0, "number of test searches", NULL },
52 { "num-records", 0, POPT_ARG_INT, &options.num_records, 0, "number of test records", NULL },
53 { "all", 'a', POPT_ARG_NONE, &options.all_records, 0, "(|(objectClass=*)(distinguishedName=*))", NULL },
54 { "nosync", 0, POPT_ARG_NONE, &options.nosync, 0, "non-synchronous transactions", NULL },
55 { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
56 { "input", 'I', POPT_ARG_STRING, &options.input, 0, "Input File", "Input" },
57 { "output", 'O', POPT_ARG_STRING, &options.output, 0, "Output File", "Output" },
58 { NULL, 'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
59 { "controls", 0, POPT_ARG_STRING, NULL, 'c', "controls", NULL },
60 { "show-binary", 0, POPT_ARG_NONE, &options.show_binary, 0, "display binary LDIF", NULL },
61 #if (_SAMBA_BUILD_ >= 4)
63 POPT_COMMON_CREDENTIALS
64 POPT_COMMON_CONNECTION
70 void ldb_cmdline_help(const char *cmdname, FILE *f)
73 pc = poptGetContext(cmdname, 0, NULL, popt_options,
74 POPT_CONTEXT_KEEP_FIRST);
75 poptPrintHelp(pc, f, 0);
79 process command line options
81 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb,
82 int argc, const char **argv,
85 struct ldb_cmdline *ret=NULL;
87 #if (_SAMBA_BUILD_ >= 4)
94 #if (_SAMBA_BUILD_ >= 4)
95 r = ldb_register_samba_handlers(ldb);
102 ret = talloc_zero(ldb, struct ldb_cmdline);
104 fprintf(stderr, "Out of memory!\n");
111 options.url = getenv("LDB_URL");
113 /* and editor (used by ldbedit) */
114 options.editor = getenv("VISUAL");
115 if (!options.editor) {
116 options.editor = getenv("EDITOR");
118 if (!options.editor) {
119 options.editor = "vi";
122 options.scope = LDB_SCOPE_DEFAULT;
124 pc = poptGetContext(argv[0], argc, argv, popt_options,
125 POPT_CONTEXT_KEEP_FIRST);
127 while((opt = poptGetNextOpt(pc)) != -1) {
130 const char *arg = poptGetOptArg(pc);
131 if (strcmp(arg, "base") == 0) {
132 options.scope = LDB_SCOPE_BASE;
133 } else if (strcmp(arg, "sub") == 0) {
134 options.scope = LDB_SCOPE_SUBTREE;
135 } else if (strcmp(arg, "one") == 0) {
136 options.scope = LDB_SCOPE_ONELEVEL;
138 fprintf(stderr, "Invalid scope '%s'\n", arg);
149 options.options = talloc_realloc(ret, options.options,
150 const char *, num_options+3);
151 if (options.options == NULL) {
152 fprintf(stderr, "Out of memory!\n");
155 options.options[num_options] = poptGetOptArg(pc);
156 options.options[num_options+1] = NULL;
161 const char *cs = poptGetOptArg(pc);
165 for (p = cs, cc = 1; (q = strchr(p, ',')); cc++, p = q + 1) ;
167 options.controls = talloc_array(ret, char *, cc + 1);
168 if (options.controls == NULL) {
169 fprintf(stderr, "Out of memory!\n");
172 for (p = cs, cc = 0; p != NULL; cc++) {
177 options.controls[cc] = talloc_strdup(options.controls, p);
180 options.controls[cc] = talloc_strndup(options.controls, p, t-p);
184 options.controls[cc] = NULL;
189 fprintf(stderr, "Invalid option %s: %s\n",
190 poptBadOption(pc, 0), poptStrerror(opt));
196 /* setup the remaining options for the main program to use */
197 options.argv = poptGetArgs(pc);
200 while (options.argv[options.argc]) options.argc++;
205 /* all utils need some option */
206 if (ret->url == NULL) {
207 fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
212 if (strcmp(ret->url, "NONE") == 0) {
216 if (options.nosync) {
217 flags |= LDB_FLG_NOSYNC;
220 if (options.show_binary) {
221 flags |= LDB_FLG_SHOW_BINARY;
224 if (options.tracing) {
225 flags |= LDB_FLG_ENABLE_TRACING;
228 #if (_SAMBA_BUILD_ >= 4)
229 /* Must be after we have processed command line options */
230 gensec_init(cmdline_lp_ctx);
232 if (ldb_set_opaque(ldb, "sessionInfo", system_session(cmdline_lp_ctx))) {
235 if (ldb_set_opaque(ldb, "credentials", cmdline_credentials)) {
238 if (ldb_set_opaque(ldb, "loadparm", cmdline_lp_ctx)) {
242 ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
245 if (options.modules_path != NULL) {
246 ldb_set_modules_dir(ldb, options.modules_path);
247 } else if (getenv("LDB_MODULES_PATH") != NULL) {
248 ldb_set_modules_dir(ldb, getenv("LDB_MODULES_PATH"));
251 /* now connect to the ldb */
252 if (ldb_connect(ldb, ret->url, flags, ret->options) != 0) {
253 fprintf(stderr, "Failed to connect to %s - %s\n",
254 ret->url, ldb_errstring(ldb));
266 /* this function check controls reply and determines if more
267 * processing is needed setting up the request controls correctly
272 * 1 all ok, more processing required
274 int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request)
279 if (reply == NULL || request == NULL) return -1;
281 for (i = 0; reply[i]; i++) {
282 if (strcmp(LDB_CONTROL_VLV_RESP_OID, reply[i]->oid) == 0) {
283 struct ldb_vlv_resp_control *rep_control;
285 rep_control = talloc_get_type(reply[i]->data, struct ldb_vlv_resp_control);
287 /* check we have a matching control in the request */
288 for (j = 0; request[j]; j++) {
289 if (strcmp(LDB_CONTROL_VLV_REQ_OID, request[j]->oid) == 0)
293 fprintf(stderr, "Warning VLV reply received but no request have been made\n");
297 /* check the result */
298 if (rep_control->vlv_result != 0) {
299 fprintf(stderr, "Warning: VLV not performed with error: %d\n", rep_control->vlv_result);
301 fprintf(stderr, "VLV Info: target position = %d, content count = %d\n", rep_control->targetPosition, rep_control->contentCount);
307 if (strcmp(LDB_CONTROL_ASQ_OID, reply[i]->oid) == 0) {
308 struct ldb_asq_control *rep_control;
310 rep_control = talloc_get_type(reply[i]->data, struct ldb_asq_control);
312 /* check the result */
313 if (rep_control->result != 0) {
314 fprintf(stderr, "Warning: ASQ not performed with error: %d\n", rep_control->result);
320 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, reply[i]->oid) == 0) {
321 struct ldb_paged_control *rep_control, *req_control;
323 rep_control = talloc_get_type(reply[i]->data, struct ldb_paged_control);
324 if (rep_control->cookie_len == 0) /* we are done */
327 /* more processing required */
328 /* let's fill in the request control with the new cookie */
330 for (j = 0; request[j]; j++) {
331 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, request[j]->oid) == 0)
334 /* if there's a reply control we must find a request
335 * control matching it */
336 if (! request[j]) return -1;
338 req_control = talloc_get_type(request[j]->data, struct ldb_paged_control);
340 if (req_control->cookie)
341 talloc_free(req_control->cookie);
342 req_control->cookie = (char *)talloc_memdup(
343 req_control, rep_control->cookie,
344 rep_control->cookie_len);
345 req_control->cookie_len = rep_control->cookie_len;
352 if (strcmp(LDB_CONTROL_SORT_RESP_OID, reply[i]->oid) == 0) {
353 struct ldb_sort_resp_control *rep_control;
355 rep_control = talloc_get_type(reply[i]->data, struct ldb_sort_resp_control);
357 /* check we have a matching control in the request */
358 for (j = 0; request[j]; j++) {
359 if (strcmp(LDB_CONTROL_SERVER_SORT_OID, request[j]->oid) == 0)
363 fprintf(stderr, "Warning Server Sort reply received but no request found\n");
367 /* check the result */
368 if (rep_control->result != 0) {
369 fprintf(stderr, "Warning: Sorting not performed with error: %d\n", rep_control->result);
375 if (strcmp(LDB_CONTROL_DIRSYNC_OID, reply[i]->oid) == 0) {
376 struct ldb_dirsync_control *rep_control, *req_control;
379 rep_control = talloc_get_type(reply[i]->data, struct ldb_dirsync_control);
380 if (rep_control->cookie_len == 0) /* we are done */
383 /* more processing required */
384 /* let's fill in the request control with the new cookie */
386 for (j = 0; request[j]; j++) {
387 if (strcmp(LDB_CONTROL_DIRSYNC_OID, request[j]->oid) == 0)
390 /* if there's a reply control we must find a request
391 * control matching it */
392 if (! request[j]) return -1;
394 req_control = talloc_get_type(request[j]->data, struct ldb_dirsync_control);
396 if (req_control->cookie)
397 talloc_free(req_control->cookie);
398 req_control->cookie = (char *)talloc_memdup(
399 req_control, rep_control->cookie,
400 rep_control->cookie_len);
401 req_control->cookie_len = rep_control->cookie_len;
403 cookie = ldb_base64_encode(req_control, rep_control->cookie, rep_control->cookie_len);
404 printf("# DIRSYNC cookie returned was:\n# %s\n", cookie);
409 /* no controls matched, throw a warning */
410 fprintf(stderr, "Unknown reply control oid: %s\n", reply[i]->oid);