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