c5391cce8ff2620b40e5c1ab2350a84220177221
[kai/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 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         { NULL }
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 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, 
97                                         int argc, const char **argv,
98                                         void (*usage)(struct ldb_context *))
99 {
100         struct ldb_cmdline *ret=NULL;
101         poptContext pc;
102         int num_options = 0;
103         int opt;
104         int flags = 0;
105         int rc;
106         struct poptOption **popt_options;
107
108         /* make the ldb utilities line buffered */
109         setlinebuf(stdout);
110
111         ret = talloc_zero(ldb, struct ldb_cmdline);
112         if (ret == NULL) {
113                 fprintf(stderr, "Out of memory!\n");
114                 goto failed;
115         }
116
117         options = *ret;
118         
119         /* pull in URL */
120         options.url = getenv("LDB_URL");
121
122         /* and editor (used by ldbedit) */
123         options.editor = getenv("VISUAL");
124         if (!options.editor) {
125                 options.editor = getenv("EDITOR");
126         }
127         if (!options.editor) {
128                 options.editor = "vi";
129         }
130
131         options.scope = LDB_SCOPE_DEFAULT;
132
133         popt_options = ldb_module_popt_options(ldb);
134         (*popt_options) = builtin_popt_options;
135
136         rc = ldb_modules_hook(ldb, LDB_MODULE_HOOK_CMDLINE_OPTIONS);
137         if (rc != LDB_SUCCESS) {
138                 fprintf(stderr, "ldb: failed to run command line hooks : %s\n", ldb_strerror(rc));
139                 goto failed;
140         }
141
142         pc = poptGetContext(argv[0], argc, argv, *popt_options,
143                             POPT_CONTEXT_KEEP_FIRST);
144
145         while((opt = poptGetNextOpt(pc)) != -1) {
146                 switch (opt) {
147                 case 's': {
148                         const char *arg = poptGetOptArg(pc);
149                         if (strcmp(arg, "base") == 0) {
150                                 options.scope = LDB_SCOPE_BASE;
151                         } else if (strcmp(arg, "sub") == 0) {
152                                 options.scope = LDB_SCOPE_SUBTREE;
153                         } else if (strcmp(arg, "one") == 0) {
154                                 options.scope = LDB_SCOPE_ONELEVEL;
155                         } else {
156                                 fprintf(stderr, "Invalid scope '%s'\n", arg);
157                                 goto failed;
158                         }
159                         break;
160                 }
161
162                 case 'v':
163                         options.verbose++;
164                         break;
165
166                 case 'o':
167                         options.options = talloc_realloc(ret, options.options, 
168                                                          const char *, num_options+3);
169                         if (options.options == NULL) {
170                                 fprintf(stderr, "Out of memory!\n");
171                                 goto failed;
172                         }
173                         options.options[num_options] = poptGetOptArg(pc);
174                         options.options[num_options+1] = NULL;
175                         num_options++;
176                         break;
177
178                 case 'c': {
179                         const char *cs = poptGetOptArg(pc);
180                         const char *p;
181
182                         for (p = cs; p != NULL; ) {
183                                 const char *t, *c;
184
185                                 t = strchr(p, ',');
186                                 if (t == NULL) {
187                                         c = talloc_strdup(options.controls, p);
188                                         p = NULL;
189                                 } else {
190                                         c = talloc_strndup(options.controls, p, t-p);
191                                         p = t + 1;
192                                 }
193                                 if (c == NULL || !add_control(ret, c)) {
194                                         fprintf(stderr, __location__ ": out of memory\n");
195                                         goto failed;
196                                 }
197                         }
198
199                         break;    
200                 }
201                 case 'P':
202                         if (!add_control(ret, "paged_results:1:1024")) {
203                                 fprintf(stderr, __location__ ": out of memory\n");
204                                 goto failed;
205                         }
206                         break;
207                 case 'D':
208                         if (!add_control(ret, "show_deleted:1")) {
209                                 fprintf(stderr, __location__ ": out of memory\n");
210                                 goto failed;
211                         }
212                         break;
213                 case 'R':
214                         if (!add_control(ret, "show_recycled:0")) {
215                                 fprintf(stderr, __location__ ": out of memory\n");
216                                 goto failed;
217                         }
218                         break;
219                 case 'd':
220                         if (!add_control(ret, "show_deactivated_link:0")) {
221                                 fprintf(stderr, __location__ ": out of memory\n");
222                                 goto failed;
223                         }
224                         break;
225                 case 'r':
226                         if (!add_control(ret, "reveal_internals:0")) {
227                                 fprintf(stderr, __location__ ": out of memory\n");
228                                 goto failed;
229                         }
230                         break;
231                 case CMDLINE_RELAX:
232                         if (!add_control(ret, "relax:0")) {
233                                 fprintf(stderr, __location__ ": out of memory\n");
234                                 goto failed;
235                         }
236                         break;
237                 case 'N':
238                         if (!add_control(ret, "search_options:1:2")) {
239                                 fprintf(stderr, __location__ ": out of memory\n");
240                                 goto failed;
241                         }
242                         break;
243                 case 'E':
244                         if (!add_control(ret, "extended_dn:1:1")) {
245                                 fprintf(stderr, __location__ ": out of memory\n");
246                                 goto failed;
247                         }
248                         break;
249                 default:
250                         fprintf(stderr, "Invalid option %s: %s\n", 
251                                 poptBadOption(pc, 0), poptStrerror(opt));
252                         if (usage) usage(ldb);
253                         goto failed;
254                 }
255         }
256
257         /* setup the remaining options for the main program to use */
258         options.argv = poptGetArgs(pc);
259         if (options.argv) {
260                 options.argv++;
261                 while (options.argv[options.argc]) options.argc++;
262         }
263
264         *ret = options;
265
266         /* all utils need some option */
267         if (ret->url == NULL) {
268                 fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
269                 if (usage) usage(ldb);
270                 goto failed;
271         }
272
273         if (strcmp(ret->url, "NONE") == 0) {
274                 return ret;
275         }
276
277         if (options.nosync) {
278                 flags |= LDB_FLG_NOSYNC;
279         }
280
281         if (options.show_binary) {
282                 flags |= LDB_FLG_SHOW_BINARY;
283         }
284
285         if (options.tracing) {
286                 flags |= LDB_FLG_ENABLE_TRACING;
287         }
288
289         if (options.modules_path != NULL) {
290                 ldb_set_modules_dir(ldb, options.modules_path);
291         }
292
293         rc = ldb_modules_hook(ldb, LDB_MODULE_HOOK_CMDLINE_PRECONNECT);
294         if (rc != LDB_SUCCESS) {
295                 fprintf(stderr, "ldb: failed to run preconnect hooks : %s\n", ldb_strerror(rc));
296                 goto failed;
297         }
298
299         /* now connect to the ldb */
300         if (ldb_connect(ldb, ret->url, flags, ret->options) != 0) {
301                 fprintf(stderr, "Failed to connect to %s - %s\n", 
302                         ret->url, ldb_errstring(ldb));
303                 goto failed;
304         }
305
306         rc = ldb_modules_hook(ldb, LDB_MODULE_HOOK_CMDLINE_POSTCONNECT);
307         if (rc != LDB_SUCCESS) {
308                 fprintf(stderr, "ldb: failed to run post connect hooks : %s\n", ldb_strerror(rc));
309                 goto failed;
310         }
311
312         return ret;
313
314 failed:
315         talloc_free(ret);
316         exit(1);
317         return NULL;
318 }
319
320 /* this function check controls reply and determines if more
321  * processing is needed setting up the request controls correctly
322  *
323  * returns:
324  *      -1 error
325  *      0 all ok
326  *      1 all ok, more processing required
327  */
328 int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request)
329 {
330         unsigned int i, j;
331         int ret = 0;
332
333         if (reply == NULL || request == NULL) return -1;
334         
335         for (i = 0; reply[i]; i++) {
336                 if (strcmp(LDB_CONTROL_VLV_RESP_OID, reply[i]->oid) == 0) {
337                         struct ldb_vlv_resp_control *rep_control;
338
339                         rep_control = talloc_get_type(reply[i]->data, struct ldb_vlv_resp_control);
340                         
341                         /* check we have a matching control in the request */
342                         for (j = 0; request[j]; j++) {
343                                 if (strcmp(LDB_CONTROL_VLV_REQ_OID, request[j]->oid) == 0)
344                                         break;
345                         }
346                         if (! request[j]) {
347                                 fprintf(stderr, "Warning VLV reply received but no request have been made\n");
348                                 continue;
349                         }
350
351                         /* check the result */
352                         if (rep_control->vlv_result != 0) {
353                                 fprintf(stderr, "Warning: VLV not performed with error: %d\n", rep_control->vlv_result);
354                         } else {
355                                 fprintf(stderr, "VLV Info: target position = %d, content count = %d\n", rep_control->targetPosition, rep_control->contentCount);
356                         }
357
358                         continue;
359                 }
360
361                 if (strcmp(LDB_CONTROL_ASQ_OID, reply[i]->oid) == 0) {
362                         struct ldb_asq_control *rep_control;
363
364                         rep_control = talloc_get_type(reply[i]->data, struct ldb_asq_control);
365
366                         /* check the result */
367                         if (rep_control->result != 0) {
368                                 fprintf(stderr, "Warning: ASQ not performed with error: %d\n", rep_control->result);
369                         }
370
371                         continue;
372                 }
373
374                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, reply[i]->oid) == 0) {
375                         struct ldb_paged_control *rep_control, *req_control;
376
377                         rep_control = talloc_get_type(reply[i]->data, struct ldb_paged_control);
378                         if (rep_control->cookie_len == 0) /* we are done */
379                                 break;
380
381                         /* more processing required */
382                         /* let's fill in the request control with the new cookie */
383
384                         for (j = 0; request[j]; j++) {
385                                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, request[j]->oid) == 0)
386                                         break;
387                         }
388                         /* if there's a reply control we must find a request
389                          * control matching it */
390                         if (! request[j]) return -1;
391
392                         req_control = talloc_get_type(request[j]->data, struct ldb_paged_control);
393
394                         if (req_control->cookie)
395                                 talloc_free(req_control->cookie);
396                         req_control->cookie = (char *)talloc_memdup(
397                                 req_control, rep_control->cookie,
398                                 rep_control->cookie_len);
399                         req_control->cookie_len = rep_control->cookie_len;
400
401                         ret = 1;
402
403                         continue;
404                 }
405
406                 if (strcmp(LDB_CONTROL_SORT_RESP_OID, reply[i]->oid) == 0) {
407                         struct ldb_sort_resp_control *rep_control;
408
409                         rep_control = talloc_get_type(reply[i]->data, struct ldb_sort_resp_control);
410
411                         /* check we have a matching control in the request */
412                         for (j = 0; request[j]; j++) {
413                                 if (strcmp(LDB_CONTROL_SERVER_SORT_OID, request[j]->oid) == 0)
414                                         break;
415                         }
416                         if (! request[j]) {
417                                 fprintf(stderr, "Warning Server Sort reply received but no request found\n");
418                                 continue;
419                         }
420
421                         /* check the result */
422                         if (rep_control->result != 0) {
423                                 fprintf(stderr, "Warning: Sorting not performed with error: %d\n", rep_control->result);
424                         }
425
426                         continue;
427                 }
428
429                 if (strcmp(LDB_CONTROL_DIRSYNC_OID, reply[i]->oid) == 0) {
430                         struct ldb_dirsync_control *rep_control, *req_control;
431                         char *cookie;
432
433                         rep_control = talloc_get_type(reply[i]->data, struct ldb_dirsync_control);
434                         if (rep_control->cookie_len == 0) /* we are done */
435                                 break;
436
437                         /* more processing required */
438                         /* let's fill in the request control with the new cookie */
439
440                         for (j = 0; request[j]; j++) {
441                                 if (strcmp(LDB_CONTROL_DIRSYNC_OID, request[j]->oid) == 0)
442                                         break;
443                         }
444                         /* if there's a reply control we must find a request
445                          * control matching it */
446                         if (! request[j]) return -1;
447
448                         req_control = talloc_get_type(request[j]->data, struct ldb_dirsync_control);
449
450                         if (req_control->cookie)
451                                 talloc_free(req_control->cookie);
452                         req_control->cookie = (char *)talloc_memdup(
453                                 req_control, rep_control->cookie,
454                                 rep_control->cookie_len);
455                         req_control->cookie_len = rep_control->cookie_len;
456
457                         cookie = ldb_base64_encode(req_control, rep_control->cookie, rep_control->cookie_len);
458                         printf("# DIRSYNC cookie returned was:\n# %s\n", cookie);
459
460                         continue;
461                 }
462
463                 /* no controls matched, throw a warning */
464                 fprintf(stderr, "Unknown reply control oid: %s\n", reply[i]->oid);
465         }
466
467         return ret;
468 }
469