s4-ldb: added a new "reveal" control
[kamenim/samba.git] / source4 / lib / ldb / tools / cmdline.c
index 01ef04f5d2d230737a1d53f51ee8cf76029d5240..5204215778957049c683906a412261e35f8a5028 100644 (file)
@@ -22,6 +22,7 @@
 */
 
 #include "ldb_includes.h"
+#include "ldb.h"
 #include "tools/cmdline.h"
 
 #if (_SAMBA_BUILD_ >= 4)
 #include "auth/gensec/gensec.h"
 #include "auth/auth.h"
 #include "ldb_wrap.h"
+#include "param/param.h"
 #endif
 
+static struct ldb_cmdline options; /* needs to be static for older compilers */
+
+static struct poptOption popt_options[] = {
+       POPT_AUTOHELP
+       { "url",       'H', POPT_ARG_STRING, &options.url, 0, "database URL", "URL" },
+       { "basedn",    'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
+       { "editor",    'e', POPT_ARG_STRING, &options.editor, 0, "external editor", "PROGRAM" },
+       { "scope",     's', POPT_ARG_STRING, NULL, 's', "search scope", "SCOPE" },
+       { "verbose",   'v', POPT_ARG_NONE, NULL, 'v', "increase verbosity", NULL },
+       { "trace",     0,   POPT_ARG_NONE, &options.tracing, 0, "enable tracing", NULL },
+       { "interactive", 'i', POPT_ARG_NONE, &options.interactive, 0, "input from stdin", NULL },
+       { "recursive", 'r', POPT_ARG_NONE, &options.recursive, 0, "recursive delete", NULL },
+       { "modules-path", 0, POPT_ARG_STRING, &options.modules_path, 0, "modules path", "PATH" },
+       { "num-searches", 0, POPT_ARG_INT, &options.num_searches, 0, "number of test searches", NULL },
+       { "num-records", 0, POPT_ARG_INT, &options.num_records, 0, "number of test records", NULL },
+       { "all", 'a',    POPT_ARG_NONE, &options.all_records, 0, "(|(objectClass=*)(distinguishedName=*))", NULL },
+       { "nosync", 0,   POPT_ARG_NONE, &options.nosync, 0, "non-synchronous transactions", NULL },
+       { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
+       { "input", 'I', POPT_ARG_STRING, &options.input, 0, "Input File", "Input" },
+       { "output", 'O', POPT_ARG_STRING, &options.output, 0, "Output File", "Output" },
+       { NULL,    'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
+       { "controls", 0, POPT_ARG_STRING, NULL, 'c', "controls", NULL },
+       { "show-binary", 0, POPT_ARG_NONE, &options.show_binary, 0, "display binary LDIF", NULL },
+       { "paged", 0, POPT_ARG_NONE, NULL, 'P', "use a paged search", NULL },
+       { "show-deleted", 0, POPT_ARG_NONE, NULL, 'D', "show deleted objects", NULL },
+       { "show-recycled", 0, POPT_ARG_NONE, NULL, 'R', "show recycled objects", NULL },
+       { "reveal", 0, POPT_ARG_NONE, NULL, 'r', "reveal ldb internals", NULL },
+       { "cross-ncs", 0, POPT_ARG_NONE, NULL, 'N', "search across NC boundaries", NULL },
+       { "extended-dn", 0, POPT_ARG_NONE, NULL, 'E', "show extended DNs", NULL },
+#if (_SAMBA_BUILD_ >= 4)
+       POPT_COMMON_SAMBA
+       POPT_COMMON_CREDENTIALS
+       POPT_COMMON_CONNECTION
+       POPT_COMMON_VERSION
+#endif
+       { NULL }
+};
 
+void ldb_cmdline_help(const char *cmdname, FILE *f)
+{
+       poptContext pc;
+       pc = poptGetContext(cmdname, 0, NULL, popt_options, 
+                           POPT_CONTEXT_KEEP_FIRST);
+       poptPrintHelp(pc, f, 0);
+}
 
 /*
+  add a control to the options structure
+ */
+static bool add_control(TALLOC_CTX *mem_ctx, const char *control)
+{
+       int i;
+
+       /* count how many controls we already have */
+       for (i=0; options.controls && options.controls[i]; i++) ;
+
+       options.controls = talloc_realloc(mem_ctx, options.controls, const char *, i + 2);
+       if (options.controls == NULL) {
+               return false;
+       }
+       options.controls[i] = control;
+       options.controls[i+1] = NULL;
+       return true;
+}
+
+/**
   process command line options
 */
-struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc, const char **argv,
+struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, 
+                                       int argc, const char **argv,
                                        void (*usage)(void))
 {
-       static struct ldb_cmdline options; /* needs to be static for older compilers */
        struct ldb_cmdline *ret=NULL;
        poptContext pc;
 #if (_SAMBA_BUILD_ >= 4)
@@ -51,35 +116,6 @@ struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc, const
        int opt;
        int flags = 0;
 
-       struct poptOption popt_options[] = {
-               POPT_AUTOHELP
-               { "url",       'H', POPT_ARG_STRING, &options.url, 0, "database URL", "URL" },
-               { "basedn",    'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
-               { "editor",    'e', POPT_ARG_STRING, &options.editor, 0, "external editor", "PROGRAM" },
-               { "scope",     's', POPT_ARG_STRING, NULL, 's', "search scope", "SCOPE" },
-               { "verbose",   'v', POPT_ARG_NONE, NULL, 'v', "increase verbosity", NULL },
-               { "interactive", 'i', POPT_ARG_NONE, &options.interactive, 0, "input from stdin", NULL },
-               { "recursive", 'r', POPT_ARG_NONE, &options.recursive, 0, "recursive delete", NULL },
-               { "modules-path", 0, POPT_ARG_STRING, &options.modules_path, 0, "modules path", "PATH" },
-               { "num-searches", 0, POPT_ARG_INT, &options.num_searches, 0, "number of test searches", NULL },
-               { "num-records", 0, POPT_ARG_INT, &options.num_records, 0, "number of test records", NULL },
-               { "all", 'a',    POPT_ARG_NONE, &options.all_records, 0, "(|(objectClass=*)(distinguishedName=*))", NULL },
-               { "nosync", 0,   POPT_ARG_NONE, &options.nosync, 0, "non-synchronous transactions", NULL },
-               { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
-               { "input", 'I', POPT_ARG_STRING, &options.input, 0, "Input File", "Input" },
-               { "output", 'O', POPT_ARG_STRING, &options.output, 0, "Output File", "Output" },
-               { NULL,    'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
-               { "controls", 0, POPT_ARG_STRING, NULL, 'c', "controls", NULL },
-#if (_SAMBA_BUILD_ >= 4)
-               POPT_COMMON_SAMBA
-               POPT_COMMON_CREDENTIALS
-               POPT_COMMON_VERSION
-#endif
-               { NULL }
-       };
-
-       ldb_global_init();
-
 #if (_SAMBA_BUILD_ >= 4)
        r = ldb_register_samba_handlers(ldb);
        if (r != 0) {
@@ -88,9 +124,12 @@ struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc, const
 
 #endif
 
+       /* make the ldb utilities line buffered */
+       setlinebuf(stdout);
+
        ret = talloc_zero(ldb, struct ldb_cmdline);
        if (ret == NULL) {
-               ldb_oom(ldb);
+               fprintf(stderr, "Out of memory!\n");
                goto failed;
        }
 
@@ -138,7 +177,7 @@ struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc, const
                        options.options = talloc_realloc(ret, options.options, 
                                                         const char *, num_options+3);
                        if (options.options == NULL) {
-                               ldb_oom(ldb);
+                               fprintf(stderr, "Out of memory!\n");
                                goto failed;
                        }
                        options.options[num_options] = poptGetOptArg(pc);
@@ -148,32 +187,63 @@ struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc, const
 
                case 'c': {
                        const char *cs = poptGetOptArg(pc);
-                       const char *p, *q;
-                       int cc;
+                       const char *p;
 
-                       for (p = cs, cc = 1; (q = strchr(p, ',')); cc++, p = q + 1) ;
-
-                       options.controls = talloc_array(ret, char *, cc + 1);
-                       if (options.controls == NULL) {
-                               ldb_oom(ldb);
-                               goto failed;
-                       }
-                       for (p = cs, cc = 0; p != NULL; cc++) {
-                               const char *t;
+                       for (p = cs; p != NULL; ) {
+                               const char *t, *c;
 
                                t = strchr(p, ',');
                                if (t == NULL) {
-                                       options.controls[cc] = talloc_strdup(options.controls, p);
+                                       c = talloc_strdup(options.controls, p);
                                        p = NULL;
                                } else {
-                                       options.controls[cc] = talloc_strndup(options.controls, p, t-p);
+                                       c = talloc_strndup(options.controls, p, t-p);
                                        p = t + 1;
                                }
+                               if (c == NULL || !add_control(ret, c)) {
+                                       fprintf(stderr, __location__ ": out of memory\n");
+                                       goto failed;
+                               }
                        }
-                       options.controls[cc] = NULL;
 
                        break;    
                }
+               case 'P':
+                       if (!add_control(ret, "paged_results:1:1024")) {
+                               fprintf(stderr, __location__ ": out of memory\n");
+                               goto failed;
+                       }
+                       break;
+               case 'D':
+                       if (!add_control(ret, "show_deleted:1")) {
+                               fprintf(stderr, __location__ ": out of memory\n");
+                               goto failed;
+                       }
+                       break;
+               case 'R':
+                       if (!add_control(ret, "show_recycled:1")) {
+                               fprintf(stderr, __location__ ": out of memory\n");
+                               goto failed;
+                       }
+                       break;
+               case 'r':
+                       if (!add_control(ret, "reveal_internals:0")) {
+                               fprintf(stderr, __location__ ": out of memory\n");
+                               goto failed;
+                       }
+                       break;
+               case 'N':
+                       if (!add_control(ret, "search_options:1:2")) {
+                               fprintf(stderr, __location__ ": out of memory\n");
+                               goto failed;
+                       }
+                       break;
+               case 'E':
+                       if (!add_control(ret, "extended_dn:1:1")) {
+                               fprintf(stderr, __location__ ": out of memory\n");
+                               goto failed;
+                       }
+                       break;
                default:
                        fprintf(stderr, "Invalid option %s: %s\n", 
                                poptBadOption(pc, 0), poptStrerror(opt));
@@ -206,16 +276,28 @@ struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc, const
                flags |= LDB_FLG_NOSYNC;
        }
 
+       if (options.show_binary) {
+               flags |= LDB_FLG_SHOW_BINARY;
+       }
+
+       if (options.tracing) {
+               flags |= LDB_FLG_ENABLE_TRACING;
+       }
+
 #if (_SAMBA_BUILD_ >= 4)
        /* Must be after we have processed command line options */
-       gensec_init(); 
+       gensec_init(cmdline_lp_ctx); 
        
-       if (ldb_set_opaque(ldb, "sessionInfo", system_session(ldb))) {
+       if (ldb_set_opaque(ldb, "sessionInfo", system_session(cmdline_lp_ctx))) {
                goto failed;
        }
        if (ldb_set_opaque(ldb, "credentials", cmdline_credentials)) {
                goto failed;
        }
+       if (ldb_set_opaque(ldb, "loadparm", cmdline_lp_ctx)) {
+               goto failed;
+       }
+
        ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
 #endif