Merge branch 'v4-0-test' of git://git.samba.org/samba into 4-0-local
[samba.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 3 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, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "ldb_includes.h"
25 #include "tools/cmdline.h"
26
27 #if (_SAMBA_BUILD_ >= 4)
28 #include "includes.h"
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"
33 #include "ldb_wrap.h"
34 #include "param/param.h"
35 #endif
36
37
38
39 /**
40   process command line options
41 */
42 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, 
43                                         int argc, const char **argv,
44                                         void (*usage)(void))
45 {
46         static struct ldb_cmdline options; /* needs to be static for older compilers */
47         struct ldb_cmdline *ret=NULL;
48         poptContext pc;
49 #if (_SAMBA_BUILD_ >= 4)
50         int r;
51 #endif
52         int num_options = 0;
53         int opt;
54         int flags = 0;
55
56         struct poptOption popt_options[] = {
57                 POPT_AUTOHELP
58                 { "url",       'H', POPT_ARG_STRING, &options.url, 0, "database URL", "URL" },
59                 { "basedn",    'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
60                 { "editor",    'e', POPT_ARG_STRING, &options.editor, 0, "external editor", "PROGRAM" },
61                 { "scope",     's', POPT_ARG_STRING, NULL, 's', "search scope", "SCOPE" },
62                 { "verbose",   'v', POPT_ARG_NONE, NULL, 'v', "increase verbosity", NULL },
63                 { "interactive", 'i', POPT_ARG_NONE, &options.interactive, 0, "input from stdin", NULL },
64                 { "recursive", 'r', POPT_ARG_NONE, &options.recursive, 0, "recursive delete", NULL },
65                 { "modules-path", 0, POPT_ARG_STRING, &options.modules_path, 0, "modules path", "PATH" },
66                 { "num-searches", 0, POPT_ARG_INT, &options.num_searches, 0, "number of test searches", NULL },
67                 { "num-records", 0, POPT_ARG_INT, &options.num_records, 0, "number of test records", NULL },
68                 { "all", 'a',    POPT_ARG_NONE, &options.all_records, 0, "(|(objectClass=*)(distinguishedName=*))", NULL },
69                 { "nosync", 0,   POPT_ARG_NONE, &options.nosync, 0, "non-synchronous transactions", NULL },
70                 { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
71                 { "input", 'I', POPT_ARG_STRING, &options.input, 0, "Input File", "Input" },
72                 { "output", 'O', POPT_ARG_STRING, &options.output, 0, "Output File", "Output" },
73                 { NULL,    'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
74                 { "controls", 0, POPT_ARG_STRING, NULL, 'c', "controls", NULL },
75 #if (_SAMBA_BUILD_ >= 4)
76                 POPT_COMMON_SAMBA
77                 POPT_COMMON_CREDENTIALS
78                 POPT_COMMON_VERSION
79 #endif
80                 { NULL }
81         };
82
83 #if (_SAMBA_BUILD_ >= 4)
84         r = ldb_register_samba_handlers(ldb);
85         if (r != 0) {
86                 goto failed;
87         }
88
89 #endif
90
91         ret = talloc_zero(ldb, struct ldb_cmdline);
92         if (ret == NULL) {
93                 ldb_oom(ldb);
94                 goto failed;
95         }
96
97         options = *ret;
98         
99         /* pull in URL */
100         options.url = getenv("LDB_URL");
101
102         /* and editor (used by ldbedit) */
103         options.editor = getenv("VISUAL");
104         if (!options.editor) {
105                 options.editor = getenv("EDITOR");
106         }
107         if (!options.editor) {
108                 options.editor = "vi";
109         }
110
111         options.scope = LDB_SCOPE_DEFAULT;
112
113         pc = poptGetContext(argv[0], argc, argv, popt_options, 
114                             POPT_CONTEXT_KEEP_FIRST);
115
116         while((opt = poptGetNextOpt(pc)) != -1) {
117                 switch (opt) {
118                 case 's': {
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;
126                         } else {
127                                 fprintf(stderr, "Invalid scope '%s'\n", arg);
128                                 goto failed;
129                         }
130                         break;
131                 }
132
133                 case 'v':
134                         options.verbose++;
135                         break;
136
137                 case 'o':
138                         options.options = talloc_realloc(ret, options.options, 
139                                                          const char *, num_options+3);
140                         if (options.options == NULL) {
141                                 ldb_oom(ldb);
142                                 goto failed;
143                         }
144                         options.options[num_options] = poptGetOptArg(pc);
145                         options.options[num_options+1] = NULL;
146                         num_options++;
147                         break;
148
149                 case 'c': {
150                         const char *cs = poptGetOptArg(pc);
151                         const char *p, *q;
152                         int cc;
153
154                         for (p = cs, cc = 1; (q = strchr(p, ',')); cc++, p = q + 1) ;
155
156                         options.controls = talloc_array(ret, char *, cc + 1);
157                         if (options.controls == NULL) {
158                                 ldb_oom(ldb);
159                                 goto failed;
160                         }
161                         for (p = cs, cc = 0; p != NULL; cc++) {
162                                 const char *t;
163
164                                 t = strchr(p, ',');
165                                 if (t == NULL) {
166                                         options.controls[cc] = talloc_strdup(options.controls, p);
167                                         p = NULL;
168                                 } else {
169                                         options.controls[cc] = talloc_strndup(options.controls, p, t-p);
170                                         p = t + 1;
171                                 }
172                         }
173                         options.controls[cc] = NULL;
174
175                         break;    
176                 }
177                 default:
178                         fprintf(stderr, "Invalid option %s: %s\n", 
179                                 poptBadOption(pc, 0), poptStrerror(opt));
180                         if (usage) usage();
181                         goto failed;
182                 }
183         }
184
185         /* setup the remaining options for the main program to use */
186         options.argv = poptGetArgs(pc);
187         if (options.argv) {
188                 options.argv++;
189                 while (options.argv[options.argc]) options.argc++;
190         }
191
192         *ret = options;
193
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");
197                 if (usage) usage();
198                 goto failed;
199         }
200
201         if (strcmp(ret->url, "NONE") == 0) {
202                 return ret;
203         }
204
205         if (options.nosync) {
206                 flags |= LDB_FLG_NOSYNC;
207         }
208
209 #if (_SAMBA_BUILD_ >= 4)
210         /* Must be after we have processed command line options */
211         gensec_init(cmdline_lp_ctx); 
212         
213         if (ldb_set_opaque(ldb, "sessionInfo", system_session(ldb, cmdline_lp_ctx))) {
214                 goto failed;
215         }
216         if (ldb_set_opaque(ldb, "credentials", cmdline_credentials)) {
217                 goto failed;
218         }
219         if (ldb_set_opaque(ldb, "loadparm", cmdline_lp_ctx)) {
220                 goto failed;
221         }
222
223         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
224 #endif
225
226         if (options.modules_path != NULL) {
227                 ldb_set_modules_dir(ldb, options.modules_path);
228         } else if (getenv("LDB_MODULES_PATH") != NULL) {
229                 ldb_set_modules_dir(ldb, getenv("LDB_MODULES_PATH"));
230         }
231
232         /* now connect to the ldb */
233         if (ldb_connect(ldb, ret->url, flags, ret->options) != 0) {
234                 fprintf(stderr, "Failed to connect to %s - %s\n", 
235                         ret->url, ldb_errstring(ldb));
236                 goto failed;
237         }
238
239         return ret;
240
241 failed:
242         talloc_free(ret);
243         exit(1);
244         return NULL;
245 }
246
247 /* this function check controls reply and determines if more
248  * processing is needed setting up the request controls correctly
249  *
250  * returns:
251  *      -1 error
252  *      0 all ok
253  *      1 all ok, more processing required
254  */
255 int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request)
256 {
257         int i, j;
258         int ret = 0;
259
260         if (reply == NULL || request == NULL) return -1;
261         
262         for (i = 0; reply[i]; i++) {
263                 if (strcmp(LDB_CONTROL_VLV_RESP_OID, reply[i]->oid) == 0) {
264                         struct ldb_vlv_resp_control *rep_control;
265
266                         rep_control = talloc_get_type(reply[i]->data, struct ldb_vlv_resp_control);
267                         
268                         /* check we have a matching control in the request */
269                         for (j = 0; request[j]; j++) {
270                                 if (strcmp(LDB_CONTROL_VLV_REQ_OID, request[j]->oid) == 0)
271                                         break;
272                         }
273                         if (! request[j]) {
274                                 fprintf(stderr, "Warning VLV reply received but no request have been made\n");
275                                 continue;
276                         }
277
278                         /* check the result */
279                         if (rep_control->vlv_result != 0) {
280                                 fprintf(stderr, "Warning: VLV not performed with error: %d\n", rep_control->vlv_result);
281                         } else {
282                                 fprintf(stderr, "VLV Info: target position = %d, content count = %d\n", rep_control->targetPosition, rep_control->contentCount);
283                         }
284
285                         continue;
286                 }
287
288                 if (strcmp(LDB_CONTROL_ASQ_OID, reply[i]->oid) == 0) {
289                         struct ldb_asq_control *rep_control;
290
291                         rep_control = talloc_get_type(reply[i]->data, struct ldb_asq_control);
292
293                         /* check the result */
294                         if (rep_control->result != 0) {
295                                 fprintf(stderr, "Warning: ASQ not performed with error: %d\n", rep_control->result);
296                         }
297
298                         continue;
299                 }
300
301                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, reply[i]->oid) == 0) {
302                         struct ldb_paged_control *rep_control, *req_control;
303
304                         rep_control = talloc_get_type(reply[i]->data, struct ldb_paged_control);
305                         if (rep_control->cookie_len == 0) /* we are done */
306                                 break;
307
308                         /* more processing required */
309                         /* let's fill in the request control with the new cookie */
310
311                         for (j = 0; request[j]; j++) {
312                                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, request[j]->oid) == 0)
313                                         break;
314                         }
315                         /* if there's a reply control we must find a request
316                          * control matching it */
317                         if (! request[j]) return -1;
318
319                         req_control = talloc_get_type(request[j]->data, struct ldb_paged_control);
320
321                         if (req_control->cookie)
322                                 talloc_free(req_control->cookie);
323                         req_control->cookie = (char *)talloc_memdup(
324                                 req_control, rep_control->cookie,
325                                 rep_control->cookie_len);
326                         req_control->cookie_len = rep_control->cookie_len;
327
328                         ret = 1;
329
330                         continue;
331                 }
332
333                 if (strcmp(LDB_CONTROL_SORT_RESP_OID, reply[i]->oid) == 0) {
334                         struct ldb_sort_resp_control *rep_control;
335
336                         rep_control = talloc_get_type(reply[i]->data, struct ldb_sort_resp_control);
337
338                         /* check we have a matching control in the request */
339                         for (j = 0; request[j]; j++) {
340                                 if (strcmp(LDB_CONTROL_SERVER_SORT_OID, request[j]->oid) == 0)
341                                         break;
342                         }
343                         if (! request[j]) {
344                                 fprintf(stderr, "Warning Server Sort reply received but no request found\n");
345                                 continue;
346                         }
347
348                         /* check the result */
349                         if (rep_control->result != 0) {
350                                 fprintf(stderr, "Warning: Sorting not performed with error: %d\n", rep_control->result);
351                         }
352
353                         continue;
354                 }
355
356                 if (strcmp(LDB_CONTROL_DIRSYNC_OID, reply[i]->oid) == 0) {
357                         struct ldb_dirsync_control *rep_control, *req_control;
358                         char *cookie;
359
360                         rep_control = talloc_get_type(reply[i]->data, struct ldb_dirsync_control);
361                         if (rep_control->cookie_len == 0) /* we are done */
362                                 break;
363
364                         /* more processing required */
365                         /* let's fill in the request control with the new cookie */
366
367                         for (j = 0; request[j]; j++) {
368                                 if (strcmp(LDB_CONTROL_DIRSYNC_OID, request[j]->oid) == 0)
369                                         break;
370                         }
371                         /* if there's a reply control we must find a request
372                          * control matching it */
373                         if (! request[j]) return -1;
374
375                         req_control = talloc_get_type(request[j]->data, struct ldb_dirsync_control);
376
377                         if (req_control->cookie)
378                                 talloc_free(req_control->cookie);
379                         req_control->cookie = (char *)talloc_memdup(
380                                 req_control, rep_control->cookie,
381                                 rep_control->cookie_len);
382                         req_control->cookie_len = rep_control->cookie_len;
383
384                         cookie = ldb_base64_encode(req_control, rep_control->cookie, rep_control->cookie_len);
385                         printf("# DIRSYNC cookie returned was:\n# %s\n", cookie);
386
387                         continue;
388                 }
389
390                 /* no controls matched, throw a warning */
391                 fprintf(stderr, "Unknown reply control oid: %s\n", reply[i]->oid);
392         }
393
394         return ret;
395 }
396