ldb: Permit desactivation of autocomit for every ldb_xxx_ctrl function
[Samba/gbeck.git] / lib / ldb / tools / cmdline.c
blobff3c1cd41fe72a80004db59840b7b5e39a77414a
1 /*
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
8 ** under the LGPL
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 "replace.h"
25 #include "system/filesys.h"
26 #include "system/time.h"
27 #include "ldb.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[] = {
36 POPT_AUTOHELP
37 { "url", 'H', POPT_ARG_STRING, &options.url, 0, "database URL", "URL" },
38 { "basedn", 'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
39 { "editor", 'e', POPT_ARG_STRING, &options.editor, 0, "external editor", "PROGRAM" },
40 { "scope", 's', POPT_ARG_STRING, NULL, 's', "search scope", "SCOPE" },
41 { "verbose", 'v', POPT_ARG_NONE, NULL, 'v', "increase verbosity", NULL },
42 { "trace", 0, POPT_ARG_NONE, &options.tracing, 0, "enable tracing", NULL },
43 { "interactive", 'i', POPT_ARG_NONE, &options.interactive, 0, "input from stdin", NULL },
44 { "recursive", 'r', POPT_ARG_NONE, &options.recursive, 0, "recursive delete", NULL },
45 { "modules-path", 0, POPT_ARG_STRING, &options.modules_path, 0, "modules path", "PATH" },
46 { "num-searches", 0, POPT_ARG_INT, &options.num_searches, 0, "number of test searches", NULL },
47 { "num-records", 0, POPT_ARG_INT, &options.num_records, 0, "number of test records", NULL },
48 { "all", 'a', POPT_ARG_NONE, &options.all_records, 0, "(|(objectClass=*)(distinguishedName=*))", NULL },
49 { "nosync", 0, POPT_ARG_NONE, &options.nosync, 0, "non-synchronous transactions", NULL },
50 { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
51 { NULL, 'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
52 { "controls", 0, POPT_ARG_STRING, NULL, 'c', "controls", NULL },
53 { "show-binary", 0, POPT_ARG_NONE, &options.show_binary, 0, "display binary LDIF", NULL },
54 { "paged", 0, POPT_ARG_NONE, NULL, 'P', "use a paged search", NULL },
55 { "show-deleted", 0, POPT_ARG_NONE, NULL, 'D', "show deleted objects", NULL },
56 { "show-recycled", 0, POPT_ARG_NONE, NULL, 'R', "show recycled objects", NULL },
57 { "show-deactivated-link", 0, POPT_ARG_NONE, NULL, 'd', "show deactivated links", NULL },
58 { "reveal", 0, POPT_ARG_NONE, NULL, 'r', "reveal ldb internals", NULL },
59 { "relax", 0, POPT_ARG_NONE, NULL, CMDLINE_RELAX, "pass relax control", NULL },
60 { "cross-ncs", 0, POPT_ARG_NONE, NULL, 'N', "search across NC boundaries", NULL },
61 { "extended-dn", 0, POPT_ARG_NONE, NULL, 'E', "show extended DNs", NULL },
62 { "noautocommit", 0, POPT_ARG_NONE, &options.noautocommit, 1, "do not commit after each ldif message", NULL },
63 { NULL }
66 void ldb_cmdline_help(struct ldb_context *ldb, const char *cmdname, FILE *f)
68 poptContext pc;
69 struct poptOption **popt_options = ldb_module_popt_options(ldb);
70 pc = poptGetContext(cmdname, 0, NULL, *popt_options,
71 POPT_CONTEXT_KEEP_FIRST);
72 poptPrintHelp(pc, f, 0);
76 add a control to the options structure
78 static bool add_control(TALLOC_CTX *mem_ctx, const char *control)
80 unsigned int i;
82 /* count how many controls we already have */
83 for (i=0; options.controls && options.controls[i]; i++) ;
85 options.controls = talloc_realloc(mem_ctx, options.controls, const char *, i + 2);
86 if (options.controls == NULL) {
87 return false;
89 options.controls[i] = control;
90 options.controls[i+1] = NULL;
91 return true;
94 /**
95 process command line options
97 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb,
98 int argc, const char **argv,
99 void (*usage)(struct ldb_context *))
101 struct ldb_cmdline *ret=NULL;
102 poptContext pc;
103 int num_options = 0;
104 int opt;
105 unsigned int flags = 0;
106 int rc;
107 struct poptOption **popt_options;
109 /* make the ldb utilities line buffered */
110 setlinebuf(stdout);
112 ret = talloc_zero(ldb, struct ldb_cmdline);
113 if (ret == NULL) {
114 fprintf(stderr, "Out of memory!\n");
115 goto failed;
118 options = *ret;
120 /* pull in URL */
121 options.url = getenv("LDB_URL");
123 /* and editor (used by ldbedit) */
124 options.editor = getenv("VISUAL");
125 if (!options.editor) {
126 options.editor = getenv("EDITOR");
128 if (!options.editor) {
129 options.editor = "vi";
132 options.scope = LDB_SCOPE_DEFAULT;
134 popt_options = ldb_module_popt_options(ldb);
135 (*popt_options) = builtin_popt_options;
137 rc = ldb_modules_hook(ldb, LDB_MODULE_HOOK_CMDLINE_OPTIONS);
138 if (rc != LDB_SUCCESS) {
139 fprintf(stderr, "ldb: failed to run command line hooks : %s\n", ldb_strerror(rc));
140 goto failed;
143 pc = poptGetContext(argv[0], argc, argv, *popt_options,
144 POPT_CONTEXT_KEEP_FIRST);
146 while((opt = poptGetNextOpt(pc)) != -1) {
147 switch (opt) {
148 case 's': {
149 const char *arg = poptGetOptArg(pc);
150 if (strcmp(arg, "base") == 0) {
151 options.scope = LDB_SCOPE_BASE;
152 } else if (strcmp(arg, "sub") == 0) {
153 options.scope = LDB_SCOPE_SUBTREE;
154 } else if (strcmp(arg, "one") == 0) {
155 options.scope = LDB_SCOPE_ONELEVEL;
156 } else {
157 fprintf(stderr, "Invalid scope '%s'\n", arg);
158 goto failed;
160 break;
163 case 'v':
164 options.verbose++;
165 break;
167 case 'o':
168 options.options = talloc_realloc(ret, options.options,
169 const char *, num_options+3);
170 if (options.options == NULL) {
171 fprintf(stderr, "Out of memory!\n");
172 goto failed;
174 options.options[num_options] = poptGetOptArg(pc);
175 options.options[num_options+1] = NULL;
176 num_options++;
177 break;
179 case 'c': {
180 const char *cs = poptGetOptArg(pc);
181 const char *p;
183 for (p = cs; p != NULL; ) {
184 const char *t, *c;
186 t = strchr(p, ',');
187 if (t == NULL) {
188 c = talloc_strdup(options.controls, p);
189 p = NULL;
190 } else {
191 c = talloc_strndup(options.controls, p, t-p);
192 p = t + 1;
194 if (c == NULL || !add_control(ret, c)) {
195 fprintf(stderr, __location__ ": out of memory\n");
196 goto failed;
200 break;
202 case 'P':
203 if (!add_control(ret, "paged_results:1:1024")) {
204 fprintf(stderr, __location__ ": out of memory\n");
205 goto failed;
207 break;
208 case 'D':
209 if (!add_control(ret, "show_deleted:1")) {
210 fprintf(stderr, __location__ ": out of memory\n");
211 goto failed;
213 break;
214 case 'R':
215 if (!add_control(ret, "show_recycled:0")) {
216 fprintf(stderr, __location__ ": out of memory\n");
217 goto failed;
219 break;
220 case 'd':
221 if (!add_control(ret, "show_deactivated_link:0")) {
222 fprintf(stderr, __location__ ": out of memory\n");
223 goto failed;
225 break;
226 case 'r':
227 if (!add_control(ret, "reveal_internals:0")) {
228 fprintf(stderr, __location__ ": out of memory\n");
229 goto failed;
231 break;
232 case CMDLINE_RELAX:
233 if (!add_control(ret, "relax:0")) {
234 fprintf(stderr, __location__ ": out of memory\n");
235 goto failed;
237 break;
238 case 'N':
239 if (!add_control(ret, "search_options:1:2")) {
240 fprintf(stderr, __location__ ": out of memory\n");
241 goto failed;
243 break;
244 case 'E':
245 if (!add_control(ret, "extended_dn:1:1")) {
246 fprintf(stderr, __location__ ": out of memory\n");
247 goto failed;
249 break;
250 default:
251 fprintf(stderr, "Invalid option %s: %s\n",
252 poptBadOption(pc, 0), poptStrerror(opt));
253 if (usage) usage(ldb);
254 goto failed;
258 /* setup the remaining options for the main program to use */
259 options.argv = poptGetArgs(pc);
260 if (options.argv) {
261 options.argv++;
262 while (options.argv[options.argc]) options.argc++;
265 *ret = options;
267 /* all utils need some option */
268 if (ret->url == NULL) {
269 fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
270 if (usage) usage(ldb);
271 goto failed;
274 if (strcmp(ret->url, "NONE") == 0) {
275 return ret;
278 if (options.nosync) {
279 flags |= LDB_FLG_NOSYNC;
282 if (options.show_binary) {
283 flags |= LDB_FLG_SHOW_BINARY;
286 if (options.tracing) {
287 flags |= LDB_FLG_ENABLE_TRACING;
290 if (options.modules_path != NULL) {
291 ldb_set_modules_dir(ldb, options.modules_path);
294 rc = ldb_modules_hook(ldb, LDB_MODULE_HOOK_CMDLINE_PRECONNECT);
295 if (rc != LDB_SUCCESS) {
296 fprintf(stderr, "ldb: failed to run preconnect hooks : %s\n", ldb_strerror(rc));
297 goto failed;
300 /* now connect to the ldb */
301 if (ldb_connect(ldb, ret->url, flags, ret->options) != LDB_SUCCESS) {
302 fprintf(stderr, "Failed to connect to %s - %s\n",
303 ret->url, ldb_errstring(ldb));
304 goto failed;
307 rc = ldb_modules_hook(ldb, LDB_MODULE_HOOK_CMDLINE_POSTCONNECT);
308 if (rc != LDB_SUCCESS) {
309 fprintf(stderr, "ldb: failed to run post connect hooks : %s\n", ldb_strerror(rc));
310 goto failed;
313 return ret;
315 failed:
316 talloc_free(ret);
317 exit(LDB_ERR_OPERATIONS_ERROR);
318 return NULL;
321 /* this function check controls reply and determines if more
322 * processing is needed setting up the request controls correctly
324 * returns:
325 * -1 error
326 * 0 all ok
327 * 1 all ok, more processing required
329 int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request)
331 unsigned int i, j;
332 int ret = 0;
334 if (reply == NULL || request == NULL) return -1;
336 for (i = 0; reply[i]; i++) {
337 if (strcmp(LDB_CONTROL_VLV_RESP_OID, reply[i]->oid) == 0) {
338 struct ldb_vlv_resp_control *rep_control;
340 rep_control = talloc_get_type(reply[i]->data, struct ldb_vlv_resp_control);
342 /* check we have a matching control in the request */
343 for (j = 0; request[j]; j++) {
344 if (strcmp(LDB_CONTROL_VLV_REQ_OID, request[j]->oid) == 0)
345 break;
347 if (! request[j]) {
348 fprintf(stderr, "Warning VLV reply received but no request have been made\n");
349 continue;
352 /* check the result */
353 if (rep_control->vlv_result != 0) {
354 fprintf(stderr, "Warning: VLV not performed with error: %d\n", rep_control->vlv_result);
355 } else {
356 fprintf(stderr, "VLV Info: target position = %d, content count = %d\n", rep_control->targetPosition, rep_control->contentCount);
359 continue;
362 if (strcmp(LDB_CONTROL_ASQ_OID, reply[i]->oid) == 0) {
363 struct ldb_asq_control *rep_control;
365 rep_control = talloc_get_type(reply[i]->data, struct ldb_asq_control);
367 /* check the result */
368 if (rep_control->result != 0) {
369 fprintf(stderr, "Warning: ASQ not performed with error: %d\n", rep_control->result);
372 continue;
375 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, reply[i]->oid) == 0) {
376 struct ldb_paged_control *rep_control, *req_control;
378 rep_control = talloc_get_type(reply[i]->data, struct ldb_paged_control);
379 if (rep_control->cookie_len == 0) /* we are done */
380 break;
382 /* more processing required */
383 /* let's fill in the request control with the new cookie */
385 for (j = 0; request[j]; j++) {
386 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, request[j]->oid) == 0)
387 break;
389 /* if there's a reply control we must find a request
390 * control matching it */
391 if (! request[j]) return -1;
393 req_control = talloc_get_type(request[j]->data, struct ldb_paged_control);
395 if (req_control->cookie)
396 talloc_free(req_control->cookie);
397 req_control->cookie = (char *)talloc_memdup(
398 req_control, rep_control->cookie,
399 rep_control->cookie_len);
400 req_control->cookie_len = rep_control->cookie_len;
402 ret = 1;
404 continue;
407 if (strcmp(LDB_CONTROL_SORT_RESP_OID, reply[i]->oid) == 0) {
408 struct ldb_sort_resp_control *rep_control;
410 rep_control = talloc_get_type(reply[i]->data, struct ldb_sort_resp_control);
412 /* check we have a matching control in the request */
413 for (j = 0; request[j]; j++) {
414 if (strcmp(LDB_CONTROL_SERVER_SORT_OID, request[j]->oid) == 0)
415 break;
417 if (! request[j]) {
418 fprintf(stderr, "Warning Server Sort reply received but no request found\n");
419 continue;
422 /* check the result */
423 if (rep_control->result != 0) {
424 fprintf(stderr, "Warning: Sorting not performed with error: %d\n", rep_control->result);
427 continue;
430 if (strcmp(LDB_CONTROL_DIRSYNC_OID, reply[i]->oid) == 0) {
431 struct ldb_dirsync_control *rep_control, *req_control;
432 char *cookie;
434 rep_control = talloc_get_type(reply[i]->data, struct ldb_dirsync_control);
435 if (rep_control->cookie_len == 0) /* we are done */
436 break;
438 /* more processing required */
439 /* let's fill in the request control with the new cookie */
441 for (j = 0; request[j]; j++) {
442 if (strcmp(LDB_CONTROL_DIRSYNC_OID, request[j]->oid) == 0)
443 break;
445 /* if there's a reply control we must find a request
446 * control matching it */
447 if (! request[j]) return -1;
449 req_control = talloc_get_type(request[j]->data, struct ldb_dirsync_control);
451 if (req_control->cookie)
452 talloc_free(req_control->cookie);
453 req_control->cookie = (char *)talloc_memdup(
454 req_control, rep_control->cookie,
455 rep_control->cookie_len);
456 req_control->cookie_len = rep_control->cookie_len;
458 cookie = ldb_base64_encode(req_control, rep_control->cookie, rep_control->cookie_len);
459 printf("# DIRSYNC cookie returned was:\n# %s\n", cookie);
461 continue;
464 /* no controls matched, throw a warning */
465 fprintf(stderr, "Unknown reply control oid: %s\n", reply[i]->oid);
468 return ret;