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"
25 #include "tools/cmdline.h"
27 #if (_SAMBA_BUILD_ >= 4)
29 #include "lib/cmdline/popt_common.h"
30 #include "lib/ldb/samba/ldif_handlers.h"
31 #include "auth/gensec/gensec.h"
32 #include "auth/auth.h"
39 process command line options
41 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc, const char **argv,
44 static struct ldb_cmdline options; /* needs to be static for older compilers */
45 struct ldb_cmdline *ret=NULL;
47 #if (_SAMBA_BUILD_ >= 4)
54 struct poptOption popt_options[] = {
56 { "url", 'H', POPT_ARG_STRING, &options.url, 0, "database URL", "URL" },
57 { "basedn", 'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
58 { "editor", 'e', POPT_ARG_STRING, &options.editor, 0, "external editor", "PROGRAM" },
59 { "scope", 's', POPT_ARG_STRING, NULL, 's', "search scope", "SCOPE" },
60 { "verbose", 'v', POPT_ARG_NONE, NULL, 'v', "increase verbosity", NULL },
61 { "interactive", 'i', POPT_ARG_NONE, &options.interactive, 0, "input from stdin", NULL },
62 { "recursive", 'r', POPT_ARG_NONE, &options.recursive, 0, "recursive delete", NULL },
63 { "num-searches", 0, POPT_ARG_INT, &options.num_searches, 0, "number of test searches", NULL },
64 { "num-records", 0, POPT_ARG_INT, &options.num_records, 0, "number of test records", NULL },
65 { "all", 'a', POPT_ARG_NONE, &options.all_records, 0, "(|(objectClass=*)(distinguishedName=*))", NULL },
66 { "nosync", 0, POPT_ARG_NONE, &options.nosync, 0, "non-synchronous transactions", NULL },
67 { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
68 { "sasl-mechanism", 0, POPT_ARG_STRING, &options.sasl_mechanism, 0, "choose SASL mechanism", "MECHANISM" },
69 { "input", 'I', POPT_ARG_STRING, &options.input, 0, "Input File", "Input" },
70 { "output", 'O', POPT_ARG_STRING, &options.output, 0, "Output File", "Output" },
71 { NULL, 'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
72 { "controls", 0, POPT_ARG_STRING, NULL, 'c', "controls", NULL },
73 #if (_SAMBA_BUILD_ >= 4)
75 POPT_COMMON_CREDENTIALS
83 #if (_SAMBA_BUILD_ >= 4)
84 r = ldb_register_samba_handlers(ldb);
91 ret = talloc_zero(ldb, struct ldb_cmdline);
100 options.url = getenv("LDB_URL");
102 /* and editor (used by ldbedit) */
103 options.editor = getenv("VISUAL");
104 if (!options.editor) {
105 options.editor = getenv("EDITOR");
107 if (!options.editor) {
108 options.editor = "vi";
111 options.scope = LDB_SCOPE_DEFAULT;
113 pc = poptGetContext(argv[0], argc, argv, popt_options,
114 POPT_CONTEXT_KEEP_FIRST);
116 while((opt = poptGetNextOpt(pc)) != -1) {
119 const char *arg = poptGetOptArg(pc);
120 if (strcmp(arg, "base") == 0) {
121 options.scope = LDB_SCOPE_BASE;
122 } else if (strcmp(arg, "sub") == 0) {
123 options.scope = LDB_SCOPE_SUBTREE;
124 } else if (strcmp(arg, "one") == 0) {
125 options.scope = LDB_SCOPE_ONELEVEL;
127 fprintf(stderr, "Invalid scope '%s'\n", arg);
138 options.options = talloc_realloc(ret, options.options,
139 const char *, num_options+3);
140 if (options.options == NULL) {
144 options.options[num_options] = poptGetOptArg(pc);
145 options.options[num_options+1] = NULL;
150 const char *cs = poptGetOptArg(pc);
154 for (p = cs, cc = 1; (q = strchr(p, ',')); cc++, p = q + 1) ;
156 options.controls = talloc_array(ret, char *, cc + 1);
157 if (options.controls == NULL) {
161 for (p = cs, cc = 0; p != NULL; cc++) {
166 options.controls[cc] = talloc_strdup(options.controls, p);
169 options.controls[cc] = talloc_strndup(options.controls, p, t-p);
173 options.controls[cc] = NULL;
178 fprintf(stderr, "Invalid option %s: %s\n",
179 poptBadOption(pc, 0), poptStrerror(opt));
185 /* setup the remaining options for the main program to use */
186 options.argv = poptGetArgs(pc);
189 while (options.argv[options.argc]) options.argc++;
194 /* all utils need some option */
195 if (ret->url == NULL) {
196 fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
201 if (strcmp(ret->url, "NONE") == 0) {
205 if (options.nosync) {
206 flags |= LDB_FLG_NOSYNC;
209 #if (_SAMBA_BUILD_ >= 4)
210 /* Must be after we have processed command line options */
213 if (ldb_set_opaque(ldb, "sessionInfo", system_session(ldb))) {
216 if (ldb_set_opaque(ldb, "credentials", cmdline_credentials)) {
219 ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
222 /* now connect to the ldb */
223 if (ldb_connect(ldb, ret->url, flags, ret->options) != 0) {
224 fprintf(stderr, "Failed to connect to %s - %s\n",
225 ret->url, ldb_errstring(ldb));
237 /* this function check controls reply and determines if more
238 * processing is needed setting up the request controls correctly
243 * 1 all ok, more processing required
245 int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request)
250 if (reply == NULL || request == NULL) return -1;
252 for (i = 0; reply[i]; i++) {
253 if (strcmp(LDB_CONTROL_VLV_RESP_OID, reply[i]->oid) == 0) {
254 struct ldb_vlv_resp_control *rep_control;
256 rep_control = talloc_get_type(reply[i]->data, struct ldb_vlv_resp_control);
258 /* check we have a matching control in the request */
259 for (j = 0; request[j]; j++) {
260 if (strcmp(LDB_CONTROL_VLV_REQ_OID, request[j]->oid) == 0)
264 fprintf(stderr, "Warning VLV reply received but no request have been made\n");
268 /* check the result */
269 if (rep_control->vlv_result != 0) {
270 fprintf(stderr, "Warning: VLV not performed with error: %d\n", rep_control->vlv_result);
272 fprintf(stderr, "VLV Info: target position = %d, content count = %d\n", rep_control->targetPosition, rep_control->contentCount);
278 if (strcmp(LDB_CONTROL_ASQ_OID, reply[i]->oid) == 0) {
279 struct ldb_asq_control *rep_control;
281 rep_control = talloc_get_type(reply[i]->data, struct ldb_asq_control);
283 /* check the result */
284 if (rep_control->result != 0) {
285 fprintf(stderr, "Warning: ASQ not performed with error: %d\n", rep_control->result);
291 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, reply[i]->oid) == 0) {
292 struct ldb_paged_control *rep_control, *req_control;
294 rep_control = talloc_get_type(reply[i]->data, struct ldb_paged_control);
295 if (rep_control->cookie_len == 0) /* we are done */
298 /* more processing required */
299 /* let's fill in the request control with the new cookie */
301 for (j = 0; request[j]; j++) {
302 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, request[j]->oid) == 0)
305 /* if there's a reply control we must find a request
306 * control matching it */
307 if (! request[j]) return -1;
309 req_control = talloc_get_type(request[j]->data, struct ldb_paged_control);
311 if (req_control->cookie)
312 talloc_free(req_control->cookie);
313 req_control->cookie = (char *)talloc_memdup(
314 req_control, rep_control->cookie,
315 rep_control->cookie_len);
316 req_control->cookie_len = rep_control->cookie_len;
323 if (strcmp(LDB_CONTROL_SORT_RESP_OID, reply[i]->oid) == 0) {
324 struct ldb_sort_resp_control *rep_control;
326 rep_control = talloc_get_type(reply[i]->data, struct ldb_sort_resp_control);
328 /* check we have a matching control in the request */
329 for (j = 0; request[j]; j++) {
330 if (strcmp(LDB_CONTROL_SERVER_SORT_OID, request[j]->oid) == 0)
334 fprintf(stderr, "Warning Server Sort reply received but no request found\n");
338 /* check the result */
339 if (rep_control->result != 0) {
340 fprintf(stderr, "Warning: Sorting not performed with error: %d\n", rep_control->result);
346 if (strcmp(LDB_CONTROL_DIRSYNC_OID, reply[i]->oid) == 0) {
347 struct ldb_dirsync_control *rep_control, *req_control;
350 rep_control = talloc_get_type(reply[i]->data, struct ldb_dirsync_control);
351 if (rep_control->cookie_len == 0) /* we are done */
354 /* more processing required */
355 /* let's fill in the request control with the new cookie */
357 for (j = 0; request[j]; j++) {
358 if (strcmp(LDB_CONTROL_DIRSYNC_OID, request[j]->oid) == 0)
361 /* if there's a reply control we must find a request
362 * control matching it */
363 if (! request[j]) return -1;
365 req_control = talloc_get_type(request[j]->data, struct ldb_dirsync_control);
367 if (req_control->cookie)
368 talloc_free(req_control->cookie);
369 req_control->cookie = (char *)talloc_memdup(
370 req_control, rep_control->cookie,
371 rep_control->cookie_len);
372 req_control->cookie_len = rep_control->cookie_len;
374 cookie = ldb_base64_encode(req_control, rep_control->cookie, rep_control->cookie_len);
375 printf("# DIRSYNC cookie returned was:\n# %s\n", cookie);
380 /* no controls matched, throw a warning */
381 fprintf(stderr, "Unknown reply control oid: %s\n", reply[i]->oid);