diff options
Diffstat (limited to 'source4/lib/ldb/tools')
-rw-r--r-- | source4/lib/ldb/tools/ldbadd.c | 78 | ||||
-rw-r--r-- | source4/lib/ldb/tools/ldbdel.c | 64 | ||||
-rw-r--r-- | source4/lib/ldb/tools/ldbedit.c | 103 | ||||
-rw-r--r-- | source4/lib/ldb/tools/ldbmodify.c | 59 | ||||
-rw-r--r-- | source4/lib/ldb/tools/ldbrename.c | 58 | ||||
-rw-r--r-- | source4/lib/ldb/tools/ldbsearch.c | 87 | ||||
-rw-r--r-- | source4/lib/ldb/tools/ldbtest.c | 152 |
7 files changed, 167 insertions, 434 deletions
diff --git a/source4/lib/ldb/tools/ldbadd.c b/source4/lib/ldb/tools/ldbadd.c index 818c33a11d..35a41527be 100644 --- a/source4/lib/ldb/tools/ldbadd.c +++ b/source4/lib/ldb/tools/ldbadd.c @@ -35,6 +35,7 @@ #include "includes.h" #include "ldb/include/ldb.h" #include "ldb/include/ldb_private.h" +#include "ldb/tools/cmdline.h" #ifdef _SAMBA_BUILD_ #include "system/filesys.h" @@ -86,69 +87,36 @@ static int process_file(struct ldb_context *ldb, FILE *f) - int main(int argc, char * const argv[]) + int main(int argc, const char **argv) { struct ldb_context *ldb; - int count=0; - const char *ldb_url; - const char **options = NULL; - int ldbopts; - int opt, i; - - ldb_url = getenv("LDB_URL"); - - ldbopts = 0; - while ((opt = getopt(argc, argv, "hH:o:")) != EOF) { - switch (opt) { - case 'H': - ldb_url = optarg; - break; - - case 'o': - options = ldb_options_parse(options, &ldbopts, optarg); - break; - - case 'h': - default: - usage(); - break; - } - } + int i, ret, count=0; + struct ldb_cmdline *options; - if (!ldb_url) { - fprintf(stderr, "You must specify an ldb URL\n\n"); - usage(); - } + ldb = ldb_init(NULL); - argc -= optind; - argv += optind; + options = ldb_cmdline_process(ldb, argc, argv, usage); - ldb = ldb_connect(ldb_url, 0, options); - - if (!ldb) { - perror("ldb_connect"); + ret = ldb_connect(ldb, options->url, 0, options->options); + if (ret != 0) { + fprintf(stderr, "Failed to connect to %s - %s\n", + options->url, ldb_errstring(ldb)); + talloc_free(ldb); exit(1); } - ldb_set_debug_stderr(ldb); - - if (argc == 0) { - usage(); - } - - for (i=0;i<argc;i++) { - FILE *f; - if (strcmp(argv[i],"-") == 0) { - f = stdin; - } else { - f = fopen(argv[i], "r"); - } - if (!f) { - perror(argv[i]); - exit(1); - } - count += process_file(ldb, f); - if (f != stdin) { + if (options->argc == 0) { + count += process_file(ldb, stdin); + } else { + for (i=0;i<options->argc;i++) { + const char *fname = options->argv[i]; + FILE *f; + f = fopen(fname, "r"); + if (!f) { + perror(fname); + exit(1); + } + count += process_file(ldb, f); fclose(f); } } diff --git a/source4/lib/ldb/tools/ldbdel.c b/source4/lib/ldb/tools/ldbdel.c index ec2e302b20..fcf1d26d53 100644 --- a/source4/lib/ldb/tools/ldbdel.c +++ b/source4/lib/ldb/tools/ldbdel.c @@ -35,6 +35,7 @@ #include "includes.h" #include "ldb/include/ldb.h" #include "ldb/include/ldb_private.h" +#include "ldb/tools/cmdline.h" #ifdef _SAMBA_BUILD_ #include "system/filesys.h" @@ -77,72 +78,41 @@ static void usage(void) exit(1); } - int main(int argc, char * const argv[]) + int main(int argc, const char **argv) { struct ldb_context *ldb; - const char **options = NULL; - int ldbopts; int ret, i; - const char *ldb_url; - int opt, recursive=0; - - ldb_url = getenv("LDB_URL"); - - ldbopts = 0; - while ((opt = getopt(argc, argv, "hH:ro:")) != EOF) { - switch (opt) { - case 'H': - ldb_url = optarg; - break; - - case 'r': - recursive=1; - break; - - case 'o': - options = ldb_options_parse(options, &ldbopts, optarg); - break; - - case 'h': - default: - usage(); - break; - } - } + struct ldb_cmdline *options; - if (!ldb_url) { - fprintf(stderr, "You must specify a ldb URL\n\n"); - usage(); - } + ldb = ldb_init(NULL); - argc -= optind; - argv += optind; + options = ldb_cmdline_process(ldb, argc, argv, usage); - if (argc < 1) { + if (options->argc < 1) { usage(); exit(1); } - ldb = ldb_connect(ldb_url, 0, options); - if (!ldb) { - perror("ldb_connect"); + ret = ldb_connect(ldb, options->url, 0, options->options); + if (ret != 0) { + fprintf(stderr, "Failed to connect to %s - %s\n", + options->url, ldb_errstring(ldb)); + talloc_free(ldb); exit(1); } - ldb_set_debug_stderr(ldb); - - for (i=0;i<argc;i++) { - if (recursive) { - ret = ldb_delete_recursive(ldb, argv[i]); + for (i=0;i<options->argc;i++) { + const char *dn = options->argv[i]; + if (options->recursive) { + ret = ldb_delete_recursive(ldb, dn); } else { - ret = ldb_delete(ldb, argv[i]); + ret = ldb_delete(ldb, dn); if (ret == 0) { printf("Deleted 1 record\n"); } } if (ret != 0) { - printf("delete of '%s' failed - %s\n", - argv[i], ldb_errstring(ldb)); + printf("delete of '%s' failed - %s\n", dn, ldb_errstring(ldb)); } } diff --git a/source4/lib/ldb/tools/ldbedit.c b/source4/lib/ldb/tools/ldbedit.c index 851eaeebdf..6c599ee2ec 100644 --- a/source4/lib/ldb/tools/ldbedit.c +++ b/source4/lib/ldb/tools/ldbedit.c @@ -35,6 +35,7 @@ #include "includes.h" #include "ldb/include/ldb.h" #include "ldb/include/ldb_private.h" +#include "ldb/tools/cmdline.h" #ifdef _SAMBA_BUILD_ #include "system/filesys.h" @@ -272,109 +273,45 @@ static void usage(void) exit(1); } - int main(int argc, char * const argv[]) + int main(int argc, const char **argv) { struct ldb_context *ldb; struct ldb_message **msgs; int ret; const char *expression = NULL; - const char *ldb_url; - const char *basedn = NULL; - const char **options = NULL; - int ldbopts; - int opt; - enum ldb_scope scope = LDB_SCOPE_SUBTREE; - const char *editor; const char * const * attrs = NULL; + struct ldb_cmdline *options; - ldb_url = getenv("LDB_URL"); + ldb = ldb_init(NULL); - /* build the editor command to run - - use the same editor priorities as vipw */ - editor = getenv("VISUAL"); - if (!editor) { - editor = getenv("EDITOR"); - } - if (!editor) { - editor = "vi"; - } - - ldbopts = 0; - while ((opt = getopt(argc, argv, "hab:e:H:s:vo:")) != EOF) { - switch (opt) { - case 'b': - basedn = optarg; - break; - - case 'H': - ldb_url = optarg; - break; - - case 's': - if (strcmp(optarg, "base") == 0) { - scope = LDB_SCOPE_BASE; - } else if (strcmp(optarg, "sub") == 0) { - scope = LDB_SCOPE_SUBTREE; - } else if (strcmp(optarg, "one") == 0) { - scope = LDB_SCOPE_ONELEVEL; - } - break; - - case 'e': - editor = optarg; - break; - - case 'a': - expression = "(|(objectclass=*)(dn=*))"; - break; - - case 'v': - verbose++; - break; - - case 'o': - options = ldb_options_parse(options, &ldbopts, optarg); - break; - - case 'h': - default: - usage(); - break; - } - } + options = ldb_cmdline_process(ldb, argc, argv, usage); - if (!ldb_url) { - fprintf(stderr, "You must specify a ldb URL\n\n"); - usage(); + if (options->all_records) { + expression = "(|(objectclass=*)(dn=*))"; } - argc -= optind; - argv += optind; - if (!expression) { - if (argc == 0) { + if (options->argc == 0) { usage(); } - expression = argv[0]; - argc--; - argv++; + expression = options->argv[0]; + options->argc--; + options->argv++; } - if (argc > 0) { - attrs = (const char * const *)argv; + if (options->argc > 0) { + attrs = (const char * const *)options->argv; } - ldb = ldb_connect(ldb_url, 0, options); - - if (!ldb) { - perror("ldb_connect"); + ret = ldb_connect(ldb, options->url, LDB_FLG_RDONLY, options->options); + if (ret != 0) { + fprintf(stderr, "Failed to connect to %s - %s\n", + options->url, ldb_errstring(ldb)); + talloc_free(ldb); exit(1); } - ldb_set_debug_stderr(ldb); - - ret = ldb_search(ldb, basedn, scope, expression, attrs, &msgs); - + ret = ldb_search(ldb, options->basedn, options->scope, expression, attrs, &msgs); if (ret == -1) { printf("search failed - %s\n", ldb_errstring(ldb)); exit(1); @@ -385,7 +322,7 @@ static void usage(void) return 0; } - do_edit(ldb, msgs, ret, editor); + do_edit(ldb, msgs, ret, options->editor); if (ret > 0) { ret = talloc_free(msgs); diff --git a/source4/lib/ldb/tools/ldbmodify.c b/source4/lib/ldb/tools/ldbmodify.c index 78baa0e36c..c54c573ab0 100644 --- a/source4/lib/ldb/tools/ldbmodify.c +++ b/source4/lib/ldb/tools/ldbmodify.c @@ -35,6 +35,7 @@ #include "includes.h" #include "ldb/include/ldb.h" #include "ldb/include/ldb_private.h" +#include "ldb/tools/cmdline.h" #ifdef _SAMBA_BUILD_ #include "system/filesys.h" @@ -88,66 +89,40 @@ static int process_file(struct ldb_context *ldb, FILE *f) return count; } - int main(int argc, char * const argv[]) + int main(int argc, const char **argv) { struct ldb_context *ldb; int count=0; - const char *ldb_url; - const char **options = NULL; - int ldbopts; - int opt, i; - - ldb_url = getenv("LDB_URL"); - - ldbopts = 0; - while ((opt = getopt(argc, argv, "hH:o:")) != EOF) { - switch (opt) { - case 'H': - ldb_url = optarg; - break; + int i, ret; + struct ldb_cmdline *options; - case 'o': - options = ldb_options_parse(options, &ldbopts, optarg); - break; + ldb = ldb_init(NULL); - case 'h': - default: - usage(); - break; - } - } + options = ldb_cmdline_process(ldb, argc, argv, usage); - if (!ldb_url) { - fprintf(stderr, "You must specify a ldb URL\n\n"); - usage(); - } - - argc -= optind; - argv += optind; - - ldb = ldb_connect(ldb_url, 0, options); - - if (!ldb) { - perror("ldb_connect"); + ret = ldb_connect(ldb, options->url, 0, options->options); + if (ret != 0) { + fprintf(stderr, "Failed to connect to %s - %s\n", + options->url, ldb_errstring(ldb)); + talloc_free(ldb); exit(1); } - ldb_set_debug_stderr(ldb); - - if (argc == 0) { + if (options->argc == 0) { usage(); exit(1); } - for (i=0;i<argc;i++) { + for (i=0;i<options->argc;i++) { + const char *fname = options->argv[i]; FILE *f; - if (strcmp(argv[i],"-") == 0) { + if (strcmp(fname,"-") == 0) { f = stdin; } else { - f = fopen(argv[i], "r"); + f = fopen(fname, "r"); } if (!f) { - perror(argv[i]); + perror(fname); exit(1); } count += process_file(ldb, f); diff --git a/source4/lib/ldb/tools/ldbrename.c b/source4/lib/ldb/tools/ldbrename.c index 273c792584..5566c3d7d7 100644 --- a/source4/lib/ldb/tools/ldbrename.c +++ b/source4/lib/ldb/tools/ldbrename.c @@ -37,6 +37,7 @@ #include "includes.h" #include "ldb/include/ldb.h" #include "ldb/include/ldb_private.h" +#include "ldb/tools/cmdline.h" #ifdef _SAMBA_BUILD_ #include "system/filesys.h" @@ -55,61 +56,38 @@ static void usage(void) } - int main(int argc, char * const argv[]) + int main(int argc, const char **argv) { struct ldb_context *ldb; - const char *ldb_url; - const char **options = NULL; - int ldbopts; - int opt, ret; - - ldb_url = getenv("LDB_URL"); - - ldbopts = 0; - while ((opt = getopt(argc, argv, "hH:o:")) != EOF) { - switch (opt) { - case 'H': - ldb_url = optarg; - break; - - case 'o': - options = ldb_options_parse(options, &ldbopts, optarg); - break; - - case 'h': - default: - usage(); - break; - } - } - - if (!ldb_url) { - fprintf(stderr, "You must specify a ldb URL\n\n"); - usage(); - } + int ret; + struct ldb_cmdline *options; + const char *dn1, *dn2; - argc -= optind; - argv += optind; + ldb = ldb_init(NULL); - ldb = ldb_connect(ldb_url, 0, options); + options = ldb_cmdline_process(ldb, argc, argv, usage); - if (!ldb) { - perror("ldb_connect"); + ret = ldb_connect(ldb, options->url, 0, options->options); + if (ret != 0) { + fprintf(stderr, "Failed to connect to %s - %s\n", + options->url, ldb_errstring(ldb)); + talloc_free(ldb); exit(1); } - ldb_set_debug_stderr(ldb); - - if (argc < 2) { + if (options->argc < 2) { usage(); } - ret = ldb_rename(ldb, argv[0], argv[1]); + dn1 = options->argv[0]; + dn2 = options->argv[1]; + + ret = ldb_rename(ldb, dn1, dn2); if (ret == 0) { printf("Renamed 1 record\n"); } else { printf("rename of '%s' to '%s' failed - %s\n", - argv[0], argv[1], ldb_errstring(ldb)); + dn1, dn2, ldb_errstring(ldb)); } talloc_free(ldb); diff --git a/source4/lib/ldb/tools/ldbsearch.c b/source4/lib/ldb/tools/ldbsearch.c index 4bf9db8d90..04f83ca366 100644 --- a/source4/lib/ldb/tools/ldbsearch.c +++ b/source4/lib/ldb/tools/ldbsearch.c @@ -35,6 +35,7 @@ #include "includes.h" #include "ldb/include/ldb.h" #include "ldb/include/ldb_private.h" +#include "ldb/tools/cmdline.h" #ifdef _SAMBA_BUILD_ #include "system/filesys.h" @@ -113,93 +114,45 @@ static int do_search(struct ldb_context *ldb, return 0; } - int main(int argc, char * const argv[]) + int main(int argc, const char **argv) { struct ldb_context *ldb; const char * const * attrs = NULL; - const char *ldb_url; - const char *basedn = NULL; - const char **options = NULL; - int opt, ldbopts; - enum ldb_scope scope = LDB_SCOPE_SUBTREE; - int interactive = 0, sort_attribs=0, ret=0; - - ldb_url = getenv("LDB_URL"); - - ldbopts = 0; - while ((opt = getopt(argc, argv, "b:H:s:o:hiS")) != EOF) { - switch (opt) { - case 'b': - basedn = optarg; - break; - - case 'H': - ldb_url = optarg; - break; - - case 's': - if (strcmp(optarg, "base") == 0) { - scope = LDB_SCOPE_BASE; - } else if (strcmp(optarg, "sub") == 0) { - scope = LDB_SCOPE_SUBTREE; - } else if (strcmp(optarg, "one") == 0) { - scope = LDB_SCOPE_ONELEVEL; - } - break; - - case 'i': - interactive = 1; - break; - - case 'S': - sort_attribs = 1; - break; + struct ldb_cmdline *options; + int ret; - case 'o': - options = ldb_options_parse(options, &ldbopts, optarg); - break; - - case 'h': - default: - usage(); - break; - } - } + ldb = ldb_init(NULL); - if (!ldb_url) { - fprintf(stderr, "You must specify a ldb URL\n\n"); - usage(); - } - - argc -= optind; - argv += optind; - - if (argc < 1 && !interactive) { + options = ldb_cmdline_process(ldb, argc, argv, usage); + + if (options->argc < 1 && !options->interactive) { usage(); exit(1); } - if (argc > 1) { - attrs = (const char * const *)(argv+1); + if (options->argc > 1) { + attrs = (const char * const *)(options->argv+1); } - ldb = ldb_connect(ldb_url, LDB_FLG_RDONLY, options); - if (!ldb) { - perror("ldb_connect"); + ret = ldb_connect(ldb, options->url, LDB_FLG_RDONLY, options->options); + if (ret != 0) { + fprintf(stderr, "Failed to connect to %s - %s\n", + options->url, ldb_errstring(ldb)); + talloc_free(ldb); exit(1); } - ldb_set_debug_stderr(ldb); - - if (interactive) { + if (options->interactive) { char line[1024]; while (fgets(line, sizeof(line), stdin)) { - if (do_search(ldb, basedn, scope, sort_attribs, line, attrs) == -1) { + if (do_search(ldb, options->basedn, + options->scope, options->sorted, line, attrs) == -1) { ret = -1; } } } else { - ret = do_search(ldb, basedn, scope, sort_attribs, argv[0], attrs); + ret = do_search(ldb, options->basedn, options->scope, options->sorted, + options->argv[0], attrs); } talloc_free(ldb); diff --git a/source4/lib/ldb/tools/ldbtest.c b/source4/lib/ldb/tools/ldbtest.c index fc1f3e3098..a7c9a3123a 100644 --- a/source4/lib/ldb/tools/ldbtest.c +++ b/source4/lib/ldb/tools/ldbtest.c @@ -35,16 +35,15 @@ #include "includes.h" #include "ldb/include/ldb.h" #include "ldb/include/ldb_private.h" +#include "ldb/tools/cmdline.h" #ifdef _SAMBA_BUILD_ #include "system/filesys.h" #include "system/time.h" #endif -static const char *ldb_url; -static const char *base_dn = "ou=Ldb Test,ou=People,o=University of Michigan,c=US"; - static struct timeval tp1,tp2; +static struct ldb_cmdline *options; static void _start_timer(void) { @@ -69,51 +68,51 @@ static void add_records(struct ldb_context *ldb, struct ldb_message_element el[6]; struct ldb_val vals[6][1]; char *name; - int j; + TALLOC_CTX *tmp_ctx = talloc_new(ldb); asprintf(&name, "Test%d", i); - asprintf(&msg.dn, "cn=%s,%s", name, basedn); + msg.dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", name, basedn); msg.num_elements = 6; msg.elements = el; el[0].flags = 0; - el[0].name = strdup("cn"); + el[0].name = talloc_strdup(tmp_ctx, "cn"); el[0].num_values = 1; el[0].values = vals[0]; vals[0][0].data = name; vals[0][0].length = strlen(name); el[1].flags = 0; - el[1].name = strdup("title"); + el[1].name = talloc_strdup(tmp_ctx, "title"); el[1].num_values = 1; el[1].values = vals[1]; - asprintf((char **)&vals[1][0].data, "The title of %s", name); + vals[1][0].data = talloc_asprintf(tmp_ctx, "The title of %s", name); vals[1][0].length = strlen(vals[1][0].data); el[2].flags = 0; - el[2].name = strdup("uid"); + el[2].name = talloc_strdup(tmp_ctx, "uid"); el[2].num_values = 1; el[2].values = vals[2]; - vals[2][0].data = ldb_casefold(ldb, name); + vals[2][0].data = ldb_casefold(tmp_ctx, name); vals[2][0].length = strlen(vals[2][0].data); el[3].flags = 0; - el[3].name = strdup("mail"); + el[3].name = talloc_strdup(tmp_ctx, "mail"); el[3].num_values = 1; el[3].values = vals[3]; - asprintf((char **)&vals[3][0].data, "%s@example.com", name); + vals[3][0].data = talloc_asprintf(tmp_ctx, "%s@example.com", name); vals[3][0].length = strlen(vals[3][0].data); el[4].flags = 0; - el[4].name = strdup("objectClass"); + el[4].name = talloc_strdup(tmp_ctx, "objectClass"); el[4].num_values = 1; el[4].values = vals[4]; - vals[4][0].data = strdup("OpenLDAPperson"); + vals[4][0].data = talloc_strdup(tmp_ctx, "OpenLDAPperson"); vals[4][0].length = strlen(vals[4][0].data); el[5].flags = 0; - el[5].name = strdup("sn"); + el[5].name = talloc_strdup(tmp_ctx, "sn"); el[5].num_values = 1; el[5].values = vals[5]; vals[5][0].data = name; @@ -129,15 +128,7 @@ static void add_records(struct ldb_context *ldb, printf("adding uid %s\r", name); fflush(stdout); - for (j=0;j<msg.num_elements;j++) { - free(el[j].name); - } - free(name); - free(msg.dn); - free(vals[1][0].data); - talloc_free(vals[2][0].data); - free(vals[3][0].data); - free(vals[4][0].data); + talloc_free(tmp_ctx); } printf("\n"); @@ -154,30 +145,30 @@ static void modify_records(struct ldb_context *ldb, struct ldb_message_element el[3]; struct ldb_val vals[3]; char *name; - int j; + TALLOC_CTX *tmp_ctx = talloc_new(ldb); - asprintf(&name, "Test%d", i); - asprintf(&msg.dn, "cn=%s,%s", name, basedn); + name = talloc_asprintf(tmp_ctx, "Test%d", i); + msg.dn = talloc_asprintf(tmp_ctx, "cn=%s,%s", name, basedn); msg.num_elements = 3; msg.elements = el; el[0].flags = LDB_FLAG_MOD_DELETE; - el[0].name = strdup("mail"); + el[0].name = talloc_strdup(tmp_ctx, "mail"); el[0].num_values = 0; el[1].flags = LDB_FLAG_MOD_ADD; - el[1].name = strdup("mail"); + el[1].name = talloc_strdup(tmp_ctx, "mail"); el[1].num_values = 1; el[1].values = &vals[1]; - asprintf((char **)&vals[1].data, "%s@other.example.com", name); + vals[1].data = talloc_asprintf(tmp_ctx, "%s@other.example.com", name); vals[1].length = strlen(vals[1].data); el[2].flags = LDB_FLAG_MOD_REPLACE; - el[2].name = strdup("mail"); + el[2].name = talloc_strdup(tmp_ctx, "mail"); el[2].num_values = 1; el[2].values = &vals[2]; - asprintf((char **)&vals[2].data, "%s@other2.example.com", name); + vals[2].data = talloc_asprintf(tmp_ctx, "%s@other2.example.com", name); vals[2].length = strlen(vals[2].data); if (ldb_modify(ldb, &msg) != 0) { @@ -188,13 +179,7 @@ static void modify_records(struct ldb_context *ldb, printf("Modifying uid %s\r", name); fflush(stdout); - for (j=0;j<msg.num_elements;j++) { - free(el[j].name); - } - free(name); - free(msg.dn); - free(vals[1].data); - free(vals[2].data); + talloc_free(tmp_ctx); } printf("\n"); @@ -235,7 +220,7 @@ static void search_uid(struct ldb_context *ldb, int nrecords, int nsearches) int ret; asprintf(&expr, "(uid=TEST%d)", uid); - ret = ldb_search(ldb, base_dn, LDB_SCOPE_SUBTREE, expr, NULL, &res); + ret = ldb_search(ldb, options->basedn, LDB_SCOPE_SUBTREE, expr, NULL, &res); if (uid < nrecords && ret != 1) { printf("Failed to find %s - %s\n", expr, ldb_errstring(ldb)); @@ -263,7 +248,7 @@ static void search_uid(struct ldb_context *ldb, int nrecords, int nsearches) static void start_test(struct ldb_context *ldb, int nrecords, int nsearches) { printf("Adding %d records\n", nrecords); - add_records(ldb, base_dn, nrecords); + add_records(ldb, options->basedn, nrecords); printf("Starting search on uid\n"); _start_timer(); @@ -271,10 +256,10 @@ static void start_test(struct ldb_context *ldb, int nrecords, int nsearches) printf("uid search took %.2f seconds\n", _end_timer()); printf("Modifying records\n"); - modify_records(ldb, base_dn, nrecords); + modify_records(ldb, options->basedn, nrecords); printf("Deleting records\n"); - delete_records(ldb, base_dn, nrecords); + delete_records(ldb, options->basedn, nrecords); } @@ -312,7 +297,7 @@ static void start_test_index(struct ldb_context **ldb) } memset(msg, 0, sizeof(*msg)); - asprintf(&msg->dn, "cn=%s,%s", "test", base_dn); + asprintf(&msg->dn, "cn=%s,%s", "test", options->basedn); ldb_msg_add_string(*ldb, msg, "cn", strdup("test")); ldb_msg_add_string(*ldb, msg, "sn", strdup("test")); ldb_msg_add_string(*ldb, msg, "uid", strdup("test")); @@ -328,14 +313,15 @@ static void start_test_index(struct ldb_context **ldb) exit(1); } - *ldb = ldb_connect(ldb_url, 0, NULL); - - if (!*ldb) { - perror("ldb_connect"); + (*ldb) = ldb_init(options); + + ret = ldb_connect(*ldb, options->url, 0, NULL); + if (ret != 0) { + printf("failed to connect to %s\n", options->url); exit(1); } - ret = ldb_search(*ldb, base_dn, LDB_SCOPE_SUBTREE, "uid=test", NULL, &res); + ret = ldb_search(*ldb, options->basedn, LDB_SCOPE_SUBTREE, "uid=test", NULL, &res); if (ret != 1) { printf("Should have found 1 record - found %d\n", ret); exit(1); @@ -363,71 +349,37 @@ static void usage(void) exit(1); } - int main(int argc, char * const argv[]) + int main(int argc, const char **argv) { + TALLOC_CTX *mem_ctx = talloc_new(NULL); struct ldb_context *ldb; - const char **options = NULL; - int ldbopts; - int opt; - int nrecords = 5000; - int nsearches = 2000; - - ldb_url = getenv("LDB_URL"); - - ldbopts = 0; - while ((opt = getopt(argc, argv, "hH:r:s:b:o:")) != EOF) { - switch (opt) { - case 'H': - ldb_url = optarg; - break; - - case 'r': - nrecords = atoi(optarg); - break; - - case 'b': - base_dn = optarg; - break; - - case 's': - nsearches = atoi(optarg); - break; - - case 'o': - options = ldb_options_parse(options, &ldbopts, optarg); - break; - - case 'h': - default: - usage(); - break; - } - } + int ret; - if (!ldb_url) { - fprintf(stderr, "You must specify a ldb URL\n\n"); - usage(); - } + ldb = ldb_init(mem_ctx); - argc -= optind; - argv += optind; + options = ldb_cmdline_process(ldb, argc, argv, usage); - ldb = ldb_connect(ldb_url, 0, options); + talloc_steal(mem_ctx, options); - if (!ldb) { - perror("ldb_connect"); - exit(1); + if (options->basedn == NULL) { + options->basedn = "ou=Ldb Test,ou=People,o=University of Michigan,c=US"; } - ldb_set_debug_stderr(ldb); + ret = ldb_connect(ldb, options->url, 0, options->options); + if (ret != 0) { + fprintf(stderr, "Failed to connect to %s - %s\n", + options->url, ldb_errstring(ldb)); + talloc_free(ldb); + exit(1); + } srandom(1); - start_test(ldb, nrecords, nsearches); + start_test(ldb, options->num_records, options->num_searches); start_test_index(&ldb); - talloc_free(ldb); + talloc_free(mem_ctx); return 0; } |