s4-ldb: make it much easier to use common ldb controls
authorAndrew Tridgell <tridge@samba.org>
Sat, 28 Nov 2009 06:35:36 +0000 (17:35 +1100)
committerAndrew Tridgell <tridge@samba.org>
Sat, 28 Nov 2009 10:58:04 +0000 (21:58 +1100)
source4/lib/ldb/tools/cmdline.c
source4/lib/ldb/tools/cmdline.h

index f0cd0a3a60c3b93bac0934dc24abfd4ceca652e4..522bbed0497bb9357da532226ae722f2b960e8ea 100644 (file)
@@ -58,6 +58,11 @@ static struct poptOption popt_options[] = {
        { 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 },
+       { "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
@@ -75,6 +80,25 @@ void ldb_cmdline_help(const char *cmdname, FILE *f)
        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
 */
@@ -162,32 +186,57 @@ struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb,
 
                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) {
-                               fprintf(stderr, "Out of memory!\n");
-                               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 '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")) {
+                               fprintf(stderr, __location__ ": out of memory\n");
+                               goto failed;
+                       }
+                       break;
                default:
                        fprintf(stderr, "Invalid option %s: %s\n", 
                                poptBadOption(pc, 0), poptStrerror(opt));
index 28061a5a7d6d2206bf2d508d839500549768dada..52e7eceabf6d31a0c6e11be1f8414350ddfa8449 100644 (file)
@@ -43,7 +43,7 @@ struct ldb_cmdline {
        const char *sasl_mechanism;
        const char *input;
        const char *output;
-       char **controls;
+       const char **controls;
        int show_binary;
        int tracing;
 };