r12843: get special objects with ldbsearch -a too, to match ldbedit -a
[metze/samba/wip.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/includes.h"
27 #include "ldb/tools/cmdline.h"
28
29 #ifdef _SAMBA_BUILD_
30 #include "lib/cmdline/popt_common.h"
31 #include "auth/auth.h"
32 #endif
33
34 /*
35   process command line options
36 */
37 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc, const char **argv,
38                                         void (*usage)(void))
39 {
40         struct ldb_cmdline options, *ret=NULL;
41         poptContext pc;
42 #ifdef _SAMBA_BUILD_
43         int r;
44 #endif
45         int num_options = 0;
46         int opt;
47         struct poptOption popt_options[] = {
48                 POPT_AUTOHELP
49                 { "url",       'H', POPT_ARG_STRING, &options.url, 0, "database URL", "URL" },
50                 { "basedn",    'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
51                 { "editor",    'e', POPT_ARG_STRING, &options.editor, 0, "external editor", "PROGRAM" },
52                 { "scope",     's', POPT_ARG_STRING, NULL, 's', "search scope", "SCOPE" },
53                 { "verbose",   'v', POPT_ARG_NONE, NULL, 'v', "increase verbosity", NULL },
54                 { "interactive", 'i', POPT_ARG_NONE, &options.interactive, 0, "input from stdin", NULL },
55                 { "recursive", 'r', POPT_ARG_NONE, &options.recursive, 0, "recursive delete", NULL },
56                 { "num-searches", 0, POPT_ARG_INT, &options.num_searches, 0, "number of test searches", NULL },
57                 { "num-records", 0, POPT_ARG_INT, &options.num_records, 0, "number of test records", NULL },
58                 { "all", 'a',    POPT_ARG_NONE, &options.all_records, 0, "(|(objectClass=*)(distinguishedName=*))", NULL },
59                 { "nosync", 0,   POPT_ARG_NONE, &options.nosync, 0, "non-synchronous transactions", NULL },
60                 { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
61                 { "sasl-mechanism", 0, POPT_ARG_STRING, &options.sasl_mechanism, 0, "choose SASL mechanism", "MECHANISM" },
62                 { "input", 'I', POPT_ARG_STRING, &options.input, 0, "Input File", "Input" },
63                 { "output", 'O', POPT_ARG_STRING, &options.output, 0, "Output File", "Output" },
64                 { NULL,    'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
65                 { "controls", 0, POPT_ARG_STRING, NULL, 'c', "controls", NULL },
66 #ifdef _SAMBA_BUILD_
67                 POPT_COMMON_SAMBA
68                 POPT_COMMON_CREDENTIALS
69                 POPT_COMMON_VERSION
70 #endif
71                 POPT_TABLEEND
72         };
73
74 #ifdef _SAMBA_BUILD_
75         gensec_init(); 
76
77         r = ldb_register_samba_handlers(ldb);
78         if (r != 0) {
79                 goto failed;
80         }
81
82 #endif
83
84         ret = talloc_zero(ldb, struct ldb_cmdline);
85         if (ret == NULL) {
86                 ldb_oom(ldb);
87                 goto failed;
88         }
89
90         options = *ret;
91         
92         /* pull in URL */
93         options.url = getenv("LDB_URL");
94
95         /* and editor (used by ldbedit) */
96         options.editor = getenv("VISUAL");
97         if (!options.editor) {
98                 options.editor = getenv("EDITOR");
99         }
100         if (!options.editor) {
101                 options.editor = "vi";
102         }
103
104         options.scope = LDB_SCOPE_DEFAULT;
105
106         pc = poptGetContext(argv[0], argc, argv, popt_options, 
107                             POPT_CONTEXT_KEEP_FIRST);
108
109         while((opt = poptGetNextOpt(pc)) != -1) {
110                 switch (opt) {
111                 case 's': {
112                         const char *arg = poptGetOptArg(pc);
113                         if (strcmp(arg, "base") == 0) {
114                                 options.scope = LDB_SCOPE_BASE;
115                         } else if (strcmp(arg, "sub") == 0) {
116                                 options.scope = LDB_SCOPE_SUBTREE;
117                         } else if (strcmp(arg, "one") == 0) {
118                                 options.scope = LDB_SCOPE_ONELEVEL;
119                         } else {
120                                 fprintf(stderr, "Invalid scope '%s'\n", arg);
121                                 goto failed;
122                         }
123                         break;
124                 }
125
126                 case 'v':
127                         options.verbose++;
128                         break;
129
130                 case 'o':
131                         options.options = talloc_realloc(ret, options.options, 
132                                                          const char *, num_options+3);
133                         if (options.options == NULL) {
134                                 ldb_oom(ldb);
135                                 goto failed;
136                         }
137                         options.options[num_options] = poptGetOptArg(pc);
138                         options.options[num_options+1] = NULL;
139                         num_options++;
140                         break;
141
142                 case 'c': {
143                         const char *cs = poptGetOptArg(pc);
144                         const char *p, *q;
145                         int cc;
146
147                         for (p = cs, cc = 1; (q = strchr(p, ',')); cc++, p = q + 1) ;
148
149                         options.controls = talloc_array(ret, char *, cc + 1);
150                         if (options.controls == NULL) {
151                                 ldb_oom(ldb);
152                                 goto failed;
153                         }
154                         for (p = cs, cc = 0; p != NULL; cc++) {
155                                 const char *t;
156
157                                 t = strchr(p, ',');
158                                 if (t == NULL) {
159                                         options.controls[cc] = talloc_strdup(options.controls, p);
160                                         p = NULL;
161                                 } else {
162                                         options.controls[cc] = talloc_strndup(options.controls, p, t-p);
163                                         p = t + 1;
164                                 }
165                         }
166                         options.controls[cc] = NULL;
167
168                         break;    
169                 }
170                 default:
171                         fprintf(stderr, "Invalid option %s: %s\n", 
172                                 poptBadOption(pc, 0), poptStrerror(opt));
173                         if (usage) usage();
174                         goto failed;
175                 }
176         }
177
178         /* setup the remaining options for the main program to use */
179         options.argv = poptGetArgs(pc);
180         if (options.argv) {
181                 options.argv++;
182                 while (options.argv[options.argc]) options.argc++;
183         }
184
185         *ret = options;
186
187         /* all utils need some option */
188         if (ret->url == NULL) {
189                 fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
190                 if (usage) usage();
191                 goto failed;
192         }
193
194         if (strcmp(ret->url, "NONE") != 0) {
195                 int flags = 0;
196                 if (options.nosync) {
197                         flags |= LDB_FLG_NOSYNC;
198                 }
199
200 #ifdef _SAMBA_BUILD_
201                 if (ldb_set_opaque(ldb, "sessionInfo", system_session(ldb))) {
202                         goto failed;
203                 }
204                 if (ldb_set_opaque(ldb, "credentials", cmdline_credentials)) {
205                         goto failed;
206                 }
207 #endif
208                 if (ldb_connect(ldb, ret->url, flags, ret->options) != 0) {
209                         fprintf(stderr, "Failed to connect to %s - %s\n", 
210                                 ret->url, ldb_errstring(ldb));
211                         goto failed;
212                 }
213         }
214
215         return ret;
216
217 failed:
218         talloc_free(ret);
219         exit(1);
220         return NULL;
221 }