01df8ff4e536d41fafdb73cd9f3c6fdd268ec0e0
[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 "ldb_private.h"
26 #include "ldb.h"
27 #include "ldb_module.h"
28 #include "tools/cmdline.h"
29
30 static struct ldb_cmdline options; /* needs to be static for older compilers */
31
32 enum ldb_cmdline_options { CMDLINE_RELAX=1 };
33
34 static struct poptOption builtin_popt_options[] = {
35         POPT_AUTOHELP
36         { "url",       'H', POPT_ARG_STRING, &options.url, 0, "database URL", "URL" },
37         { "basedn",    'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
38         { "editor",    'e', POPT_ARG_STRING, &options.editor, 0, "external editor", "PROGRAM" },
39         { "scope",     's', POPT_ARG_STRING, NULL, 's', "search scope", "SCOPE" },
40         { "verbose",   'v', POPT_ARG_NONE, NULL, 'v', "increase verbosity", NULL },
41         { "trace",     0,   POPT_ARG_NONE, &options.tracing, 0, "enable tracing", NULL },
42         { "interactive", 'i', POPT_ARG_NONE, &options.interactive, 0, "input from stdin", NULL },
43         { "recursive", 'r', POPT_ARG_NONE, &options.recursive, 0, "recursive delete", NULL },
44         { "modules-path", 0, POPT_ARG_STRING, &options.modules_path, 0, "modules path", "PATH" },
45         { "num-searches", 0, POPT_ARG_INT, &options.num_searches, 0, "number of test searches", NULL },
46         { "num-records", 0, POPT_ARG_INT, &options.num_records, 0, "number of test records", NULL },
47         { "all", 'a',    POPT_ARG_NONE, &options.all_records, 0, "(|(objectClass=*)(distinguishedName=*))", NULL },
48         { "nosync", 0,   POPT_ARG_NONE, &options.nosync, 0, "non-synchronous transactions", NULL },
49         { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
50         { NULL,    'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
51         { "controls", 0, POPT_ARG_STRING, NULL, 'c', "controls", NULL },
52         { "show-binary", 0, POPT_ARG_NONE, &options.show_binary, 0, "display binary LDIF", NULL },
53         { "paged", 0, POPT_ARG_NONE, NULL, 'P', "use a paged search", NULL },
54         { "show-deleted", 0, POPT_ARG_NONE, NULL, 'D', "show deleted objects", NULL },
55         { "show-recycled", 0, POPT_ARG_NONE, NULL, 'R', "show recycled objects", NULL },
56         { "show-deactivated-link", 0, POPT_ARG_NONE, NULL, 'd', "show deactivated links", NULL },
57         { "reveal", 0, POPT_ARG_NONE, NULL, 'r', "reveal ldb internals", NULL },
58         { "relax", 0, POPT_ARG_NONE, NULL, CMDLINE_RELAX, "pass relax control", NULL },
59         { "cross-ncs", 0, POPT_ARG_NONE, NULL, 'N', "search across NC boundaries", NULL },
60         { "extended-dn", 0, POPT_ARG_NONE, NULL, 'E', "show extended DNs", NULL },
61         { NULL }
62 };
63
64 void ldb_cmdline_help(struct ldb_context *ldb, const char *cmdname, FILE *f)
65 {
66         poptContext pc;
67         pc = poptGetContext(cmdname, 0, NULL, ldb->popt_options,
68                             POPT_CONTEXT_KEEP_FIRST);
69         poptPrintHelp(pc, f, 0);
70 }
71
72 /*
73   add a control to the options structure
74  */
75 static bool add_control(TALLOC_CTX *mem_ctx, const char *control)
76 {
77         unsigned int i;
78
79         /* count how many controls we already have */
80         for (i=0; options.controls && options.controls[i]; i++) ;
81
82         options.controls = talloc_realloc(mem_ctx, options.controls, const char *, i + 2);
83         if (options.controls == NULL) {
84                 return false;
85         }
86         options.controls[i] = control;
87         options.controls[i+1] = NULL;
88         return true;
89 }
90
91 /**
92   process command line options
93 */
94 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, 
95                                         int argc, const char **argv,
96                                         void (*usage)(struct ldb_context *))
97 {
98         struct ldb_cmdline *ret=NULL;
99         poptContext pc;
100         int num_options = 0;
101         int opt;
102         int flags = 0;
103         int rc;
104
105         /* make the ldb utilities line buffered */
106         setlinebuf(stdout);
107
108         ret = talloc_zero(ldb, struct ldb_cmdline);
109         if (ret == NULL) {
110                 fprintf(stderr, "Out of memory!\n");
111                 goto failed;
112         }
113
114         options = *ret;
115         
116         /* pull in URL */
117         options.url = getenv("LDB_URL");
118
119         /* and editor (used by ldbedit) */
120         options.editor = getenv("VISUAL");
121         if (!options.editor) {
122                 options.editor = getenv("EDITOR");
123         }
124         if (!options.editor) {
125                 options.editor = "vi";
126         }
127
128         options.scope = LDB_SCOPE_DEFAULT;
129
130         ldb->popt_options = builtin_popt_options;
131
132         rc = ldb_modules_hook(ldb, LDB_MODULE_HOOK_CMDLINE_OPTIONS);
133         if (rc != LDB_SUCCESS) {
134                 fprintf(stderr, "ldb: failed to run command line hooks : %s\n", ldb_strerror(rc));
135                 goto failed;
136         }
137
138         pc = poptGetContext(argv[0], argc, argv, ldb->popt_options,
139                             POPT_CONTEXT_KEEP_FIRST);
140
141         while((opt = poptGetNextOpt(pc)) != -1) {
142                 switch (opt) {
143                 case 's': {
144                         const char *arg = poptGetOptArg(pc);
145                         if (strcmp(arg, "base") == 0) {
146                                 options.scope = LDB_SCOPE_BASE;
147                         } else if (strcmp(arg, "sub") == 0) {
148                                 options.scope = LDB_SCOPE_SUBTREE;
149                         } else if (strcmp(arg, "one") == 0) {
150                                 options.scope = LDB_SCOPE_ONELEVEL;
151                         } else {
152                                 fprintf(stderr, "Invalid scope '%s'\n", arg);
153                                 goto failed;
154                         }
155                         break;
156                 }
157
158                 case 'v':
159                         options.verbose++;
160                         break;
161
162                 case 'o':
163                         options.options = talloc_realloc(ret, options.options, 
164                                                          const char *, num_options+3);
165                         if (options.options == NULL) {
166                                 fprintf(stderr, "Out of memory!\n");
167                                 goto failed;
168                         }
169                         options.options[num_options] = poptGetOptArg(pc);
170                         options.options[num_options+1] = NULL;
171                         num_options++;
172                         break;
173
174                 case 'c': {
175                         const char *cs = poptGetOptArg(pc);
176                         const char *p;
177
178                         for (p = cs; p != NULL; ) {
179                                 const char *t, *c;
180
181                                 t = strchr(p, ',');
182                                 if (t == NULL) {
183                                         c = talloc_strdup(options.controls, p);
184                                         p = NULL;
185                                 } else {
186                                         c = talloc_strndup(options.controls, p, t-p);
187                                         p = t + 1;
188                                 }
189                                 if (c == NULL || !add_control(ret, c)) {
190                                         fprintf(stderr, __location__ ": out of memory\n");
191                                         goto failed;
192                                 }
193                         }
194
195                         break;    
196                 }
197                 case 'P':
198                         if (!add_control(ret, "paged_results:1:1024")) {
199                                 fprintf(stderr, __location__ ": out of memory\n");
200                                 goto failed;
201                         }
202                         break;
203                 case 'D':
204                         if (!add_control(ret, "show_deleted:1")) {
205                                 fprintf(stderr, __location__ ": out of memory\n");
206                                 goto failed;
207                         }
208                         break;
209                 case 'R':
210                         if (!add_control(ret, "show_recycled:0")) {
211                                 fprintf(stderr, __location__ ": out of memory\n");
212                                 goto failed;
213                         }
214                         break;
215                 case 'd':
216                         if (!add_control(ret, "show_deactivated_link:0")) {
217                                 fprintf(stderr, __location__ ": out of memory\n");
218                                 goto failed;
219                         }
220                         break;
221                 case 'r':
222                         if (!add_control(ret, "reveal_internals:0")) {
223                                 fprintf(stderr, __location__ ": out of memory\n");
224                                 goto failed;
225                         }
226                         break;
227                 case CMDLINE_RELAX:
228                         if (!add_control(ret, "relax:0")) {
229                                 fprintf(stderr, __location__ ": out of memory\n");
230                                 goto failed;
231                         }
232                         break;
233                 case 'N':
234                         if (!add_control(ret, "search_options:1:2")) {
235                                 fprintf(stderr, __location__ ": out of memory\n");
236                                 goto failed;
237                         }
238                         break;
239                 case 'E':
240                         if (!add_control(ret, "extended_dn:1:1")) {
241                                 fprintf(stderr, __location__ ": out of memory\n");
242                                 goto failed;
243                         }
244                         break;
245                 default:
246                         fprintf(stderr, "Invalid option %s: %s\n", 
247                                 poptBadOption(pc, 0), poptStrerror(opt));
248                         if (usage) usage(ldb);
249                         goto failed;
250                 }
251         }
252
253         /* setup the remaining options for the main program to use */
254         options.argv = poptGetArgs(pc);
255         if (options.argv) {
256                 options.argv++;
257                 while (options.argv[options.argc]) options.argc++;
258         }
259
260         *ret = options;
261
262         /* all utils need some option */
263         if (ret->url == NULL) {
264                 fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
265                 if (usage) usage(ldb);
266                 goto failed;
267         }
268
269         if (strcmp(ret->url, "NONE") == 0) {
270                 return ret;
271         }
272
273         if (options.nosync) {
274                 flags |= LDB_FLG_NOSYNC;
275         }
276
277         if (options.show_binary) {
278                 flags |= LDB_FLG_SHOW_BINARY;
279         }
280
281         if (options.tracing) {
282                 flags |= LDB_FLG_ENABLE_TRACING;
283         }
284
285         if (options.modules_path != NULL) {
286                 ldb_set_modules_dir(ldb, options.modules_path);
287         }
288
289         rc = ldb_modules_hook(ldb, LDB_MODULE_HOOK_CMDLINE_PRECONNECT);
290         if (rc != LDB_SUCCESS) {
291                 fprintf(stderr, "ldb: failed to run preconnect hooks : %s\n", ldb_strerror(rc));
292                 goto failed;
293         }
294
295         /* now connect to the ldb */
296         if (ldb_connect(ldb, ret->url, flags, ret->options) != 0) {
297                 fprintf(stderr, "Failed to connect to %s - %s\n", 
298                         ret->url, ldb_errstring(ldb));
299                 goto failed;
300         }
301
302         rc = ldb_modules_hook(ldb, LDB_MODULE_HOOK_CMDLINE_POSTCONNECT);
303         if (rc != LDB_SUCCESS) {
304                 fprintf(stderr, "ldb: failed to run post connect hooks : %s\n", ldb_strerror(rc));
305                 goto failed;
306         }
307
308         return ret;
309
310 failed:
311         talloc_free(ret);
312         exit(1);
313         return NULL;
314 }
315
316 /* this function check controls reply and determines if more
317  * processing is needed setting up the request controls correctly
318  *
319  * returns:
320  *      -1 error
321  *      0 all ok
322  *      1 all ok, more processing required
323  */
324 int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request)
325 {
326         unsigned int i, j;
327         int ret = 0;
328
329         if (reply == NULL || request == NULL) return -1;
330         
331         for (i = 0; reply[i]; i++) {
332                 if (strcmp(LDB_CONTROL_VLV_RESP_OID, reply[i]->oid) == 0) {
333                         struct ldb_vlv_resp_control *rep_control;
334
335                         rep_control = talloc_get_type(reply[i]->data, struct ldb_vlv_resp_control);
336                         
337                         /* check we have a matching control in the request */
338                         for (j = 0; request[j]; j++) {
339                                 if (strcmp(LDB_CONTROL_VLV_REQ_OID, request[j]->oid) == 0)
340                                         break;
341                         }
342                         if (! request[j]) {
343                                 fprintf(stderr, "Warning VLV reply received but no request have been made\n");
344                                 continue;
345                         }
346
347                         /* check the result */
348                         if (rep_control->vlv_result != 0) {
349                                 fprintf(stderr, "Warning: VLV not performed with error: %d\n", rep_control->vlv_result);
350                         } else {
351                                 fprintf(stderr, "VLV Info: target position = %d, content count = %d\n", rep_control->targetPosition, rep_control->contentCount);
352                         }
353
354                         continue;
355                 }
356
357                 if (strcmp(LDB_CONTROL_ASQ_OID, reply[i]->oid) == 0) {
358                         struct ldb_asq_control *rep_control;
359
360                         rep_control = talloc_get_type(reply[i]->data, struct ldb_asq_control);
361
362                         /* check the result */
363                         if (rep_control->result != 0) {
364                                 fprintf(stderr, "Warning: ASQ not performed with error: %d\n", rep_control->result);
365                         }
366
367                         continue;
368                 }
369
370                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, reply[i]->oid) == 0) {
371                         struct ldb_paged_control *rep_control, *req_control;
372
373                         rep_control = talloc_get_type(reply[i]->data, struct ldb_paged_control);
374                         if (rep_control->cookie_len == 0) /* we are done */
375                                 break;
376
377                         /* more processing required */
378                         /* let's fill in the request control with the new cookie */
379
380                         for (j = 0; request[j]; j++) {
381                                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, request[j]->oid) == 0)
382                                         break;
383                         }
384                         /* if there's a reply control we must find a request
385                          * control matching it */
386                         if (! request[j]) return -1;
387
388                         req_control = talloc_get_type(request[j]->data, struct ldb_paged_control);
389
390                         if (req_control->cookie)
391                                 talloc_free(req_control->cookie);
392                         req_control->cookie = (char *)talloc_memdup(
393                                 req_control, rep_control->cookie,
394                                 rep_control->cookie_len);
395                         req_control->cookie_len = rep_control->cookie_len;
396
397                         ret = 1;
398
399                         continue;
400                 }
401
402                 if (strcmp(LDB_CONTROL_SORT_RESP_OID, reply[i]->oid) == 0) {
403                         struct ldb_sort_resp_control *rep_control;
404
405                         rep_control = talloc_get_type(reply[i]->data, struct ldb_sort_resp_control);
406
407                         /* check we have a matching control in the request */
408                         for (j = 0; request[j]; j++) {
409                                 if (strcmp(LDB_CONTROL_SERVER_SORT_OID, request[j]->oid) == 0)
410                                         break;
411                         }
412                         if (! request[j]) {
413                                 fprintf(stderr, "Warning Server Sort reply received but no request found\n");
414                                 continue;
415                         }
416
417                         /* check the result */
418                         if (rep_control->result != 0) {
419                                 fprintf(stderr, "Warning: Sorting not performed with error: %d\n", rep_control->result);
420                         }
421
422                         continue;
423                 }
424
425                 if (strcmp(LDB_CONTROL_DIRSYNC_OID, reply[i]->oid) == 0) {
426                         struct ldb_dirsync_control *rep_control, *req_control;
427                         char *cookie;
428
429                         rep_control = talloc_get_type(reply[i]->data, struct ldb_dirsync_control);
430                         if (rep_control->cookie_len == 0) /* we are done */
431                                 break;
432
433                         /* more processing required */
434                         /* let's fill in the request control with the new cookie */
435
436                         for (j = 0; request[j]; j++) {
437                                 if (strcmp(LDB_CONTROL_DIRSYNC_OID, request[j]->oid) == 0)
438                                         break;
439                         }
440                         /* if there's a reply control we must find a request
441                          * control matching it */
442                         if (! request[j]) return -1;
443
444                         req_control = talloc_get_type(request[j]->data, struct ldb_dirsync_control);
445
446                         if (req_control->cookie)
447                                 talloc_free(req_control->cookie);
448                         req_control->cookie = (char *)talloc_memdup(
449                                 req_control, rep_control->cookie,
450                                 rep_control->cookie_len);
451                         req_control->cookie_len = rep_control->cookie_len;
452
453                         cookie = ldb_base64_encode(req_control, rep_control->cookie, rep_control->cookie_len);
454                         printf("# DIRSYNC cookie returned was:\n# %s\n", cookie);
455
456                         continue;
457                 }
458
459                 /* no controls matched, throw a warning */
460                 fprintf(stderr, "Unknown reply control oid: %s\n", reply[i]->oid);
461         }
462
463         return ret;
464 }
465