cde357a0882346984ac12e89f982d9363c18ecd8
[sfrench/samba-autobuild/.git] / source4 / lib / ldb / tools / cmdline.c
1 /* 
2    ldb database library - command line handling for ldb tools
3
4    Copyright (C) Andrew Tridgell  2005
5
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
9    
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.
14
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.
19
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
23 */
24
25 #include "includes.h"
26 #include "ldb/include/ldb.h"
27 #include "ldb/include/ldb_private.h"
28 #include "ldb/tools/cmdline.h"
29 #ifdef _SAMBA_BUILD_
30 #include "lib/cmdline/popt_common.h"
31 #endif
32
33 /*
34   process command line options
35 */
36 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc, const char **argv,
37                                         void (*usage)(void))
38 {
39         struct ldb_cmdline options, *ret=NULL;
40         poptContext pc;
41 #ifdef _SAMBA_BUILD_
42         int r;
43 #endif
44         int num_options = 0;
45         int opt;
46         struct poptOption popt_options[] = {
47                 POPT_AUTOHELP
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                 { "input", 'I', POPT_ARG_STRING, &options.input, 0, "Input File", "Input" },
61                 { "output", 'O', POPT_ARG_STRING, &options.output, 0, "Output File", "Output" },
62                 { NULL,    'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
63 #ifdef _SAMBA_BUILD_
64                 POPT_COMMON_SAMBA
65                 POPT_COMMON_CREDENTIALS
66                 POPT_COMMON_VERSION
67 #endif
68                 POPT_TABLEEND
69         };
70
71 #ifdef _SAMBA_BUILD_
72         ldbsearch_init_subsystems;
73         r = ldb_register_samba_handlers(ldb);
74         if (r != 0) {
75                 goto failed;
76         }
77 #endif
78
79         ret = talloc_zero(ldb, struct ldb_cmdline);
80         if (ret == NULL) {
81                 ldb_oom(ldb);
82                 goto failed;
83         }
84
85         options = *ret;
86         
87         /* pull in URL */
88         options.url = getenv("LDB_URL");
89
90         /* and editor (used by ldbedit) */
91         options.editor = getenv("VISUAL");
92         if (!options.editor) {
93                 options.editor = getenv("EDITOR");
94         }
95         if (!options.editor) {
96                 options.editor = "vi";
97         }
98
99         options.scope = LDB_SCOPE_DEFAULT;
100
101         pc = poptGetContext(argv[0], argc, argv, popt_options, 
102                             POPT_CONTEXT_KEEP_FIRST);
103
104         while((opt = poptGetNextOpt(pc)) != -1) {
105                 switch (opt) {
106                 case 's': {
107                         const char *arg = poptGetOptArg(pc);
108                         if (strcmp(arg, "base") == 0) {
109                                 options.scope = LDB_SCOPE_BASE;
110                         } else if (strcmp(arg, "sub") == 0) {
111                                 options.scope = LDB_SCOPE_SUBTREE;
112                         } else if (strcmp(arg, "one") == 0) {
113                                 options.scope = LDB_SCOPE_ONELEVEL;
114                         } else {
115                                 fprintf(stderr, "Invalid scope '%s'\n", arg);
116                                 goto failed;
117                         }
118                         break;
119                 }
120
121                 case 'v':
122                         options.verbose++;
123                         break;
124
125                 case 'o':
126                         options.options = talloc_realloc(ret, options.options, 
127                                                          const char *, num_options+2);
128                         if (options.options == NULL) {
129                                 ldb_oom(ldb);
130                                 goto failed;
131                         }
132                         options.options[num_options++] = poptGetOptArg(pc);
133                         options.options[num_options+1] = NULL;
134                         break;
135                         
136                 default:
137                         fprintf(stderr, "Invalid option %s: %s\n", 
138                                 poptBadOption(pc, 0), poptStrerror(opt));
139                         if (usage) usage();
140                         goto failed;
141                 }
142         }
143
144         /* setup the remaining options for the main program to use */
145         options.argv = poptGetArgs(pc);
146         if (options.argv) {
147                 options.argv++;
148                 while (options.argv[options.argc]) options.argc++;
149         }
150
151         *ret = options;
152
153         /* all utils need some option */
154         if (ret->url == NULL) {
155                 fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
156                 if (usage) usage();
157                 goto failed;
158         }
159
160         if (strcmp(ret->url, "NONE") != 0) {
161                 if (ldb_connect(ldb, ret->url, 0, ret->options) != 0) {
162                         fprintf(stderr, "Failed to connect to %s - %s\n", 
163                                 ret->url, ldb_errstring(ldb));
164                         goto failed;
165                 }
166         }
167
168         return ret;
169
170 failed:
171         talloc_free(ret);
172         exit(1);
173         return NULL;
174 }