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/ldb.h"
27 #include "ldb/include/ldb_private.h"
28 #include "ldb/tools/cmdline.h"
30 #include "lib/cmdline/popt_common.h"
34 process command line options
36 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc, const char **argv,
39 struct ldb_cmdline options, *ret=NULL;
46 struct poptOption popt_options[] = {
48 { "url", 'H', POPT_ARG_STRING, &options.url, 0, "database URL", "URL" },
49 { "basedn", 'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
50 { "editor", 'e', POPT_ARG_STRING, &options.editor, 0, "external editor", "PROGRAM" },
51 { "scope", 's', POPT_ARG_STRING, NULL, 's', "search scope", "SCOPE" },
52 { "verbose", 'v', POPT_ARG_NONE, NULL, 'v', "increase verbosity", NULL },
53 { "interactive", 'i', POPT_ARG_NONE, &options.interactive, 0, "input from stdin", NULL },
54 { "recursive", 'r', POPT_ARG_NONE, &options.recursive, 0, "recursive delete", NULL },
55 { "num-searches", 0, POPT_ARG_INT, &options.num_searches, 0, "number of test searches", NULL },
56 { "num-records", 0, POPT_ARG_INT, &options.num_records, 0, "number of test records", NULL },
57 { "all", 'a', POPT_ARG_NONE, &options.all_records, 0, "dn=*", NULL },
58 { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
59 { "sasl-mechanism", 0, POPT_ARG_STRING, &options.sasl_mechanism, 0, "choose SASL mechanism", "MECHANISM" },
60 { NULL, 'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
63 POPT_COMMON_CREDENTIALS
70 ldbsearch_init_subsystems;
71 r = ldb_register_samba_handlers(ldb);
77 ret = talloc_zero(ldb, struct ldb_cmdline);
86 options.url = getenv("LDB_URL");
88 /* and editor (used by ldbedit) */
89 options.editor = getenv("VISUAL");
90 if (!options.editor) {
91 options.editor = getenv("EDITOR");
93 if (!options.editor) {
94 options.editor = "vi";
97 options.scope = LDB_SCOPE_DEFAULT;
99 pc = poptGetContext(argv[0], argc, argv, popt_options,
100 POPT_CONTEXT_KEEP_FIRST);
102 while((opt = poptGetNextOpt(pc)) != -1) {
105 const char *arg = poptGetOptArg(pc);
106 if (strcmp(arg, "base") == 0) {
107 options.scope = LDB_SCOPE_BASE;
108 } else if (strcmp(arg, "sub") == 0) {
109 options.scope = LDB_SCOPE_SUBTREE;
110 } else if (strcmp(arg, "one") == 0) {
111 options.scope = LDB_SCOPE_ONELEVEL;
113 fprintf(stderr, "Invalid scope '%s'\n", arg);
124 options.options = talloc_realloc(ret, options.options,
125 const char *, num_options+2);
126 if (options.options == NULL) {
130 options.options[num_options++] = poptGetOptArg(pc);
131 options.options[num_options+1] = NULL;
135 fprintf(stderr, "Invalid option %s: %s\n",
136 poptBadOption(pc, 0), poptStrerror(opt));
142 /* setup the remaining options for the main program to use */
143 options.argv = poptGetArgs(pc);
146 while (options.argv[options.argc]) options.argc++;
151 /* all utils need some option */
152 if (ret->url == NULL) {
153 fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
158 if (ldb_connect(ldb, ret->url, 0, ret->options) != 0) {
159 fprintf(stderr, "Failed to connect to %s - %s\n",
160 ret->url, ldb_errstring(ldb));