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