diff options
Diffstat (limited to 'source4/lib/ldb/tools/cmdline.c')
-rw-r--r-- | source4/lib/ldb/tools/cmdline.c | 198 |
1 files changed, 126 insertions, 72 deletions
diff --git a/source4/lib/ldb/tools/cmdline.c b/source4/lib/ldb/tools/cmdline.c index 73bf2a93a7..b2be54ebf9 100644 --- a/source4/lib/ldb/tools/cmdline.c +++ b/source4/lib/ldb/tools/cmdline.c @@ -21,23 +21,18 @@ License along with this library; if not, see <http://www.gnu.org/licenses/>. */ -#include "ldb_includes.h" +#include "replace.h" +#include "system/filesys.h" +#include "system/time.h" #include "ldb.h" +#include "ldb_module.h" #include "tools/cmdline.h" -#if (_SAMBA_BUILD_ >= 4) -#include "includes.h" -#include "lib/cmdline/popt_common.h" -#include "lib/ldb-samba/ldif_handlers.h" -#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[] = { +enum ldb_cmdline_options { CMDLINE_RELAX=1 }; + +static struct poptOption builtin_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" }, @@ -53,51 +48,65 @@ static struct poptOption popt_options[] = { { "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 }, -#if (_SAMBA_BUILD_ >= 4) - POPT_COMMON_SAMBA - POPT_COMMON_CREDENTIALS - POPT_COMMON_CONNECTION - POPT_COMMON_VERSION -#endif + { "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 }, + { "show-deactivated-link", 0, POPT_ARG_NONE, NULL, 'd', "show deactivated links", NULL }, + { "reveal", 0, POPT_ARG_NONE, NULL, 'r', "reveal ldb internals", NULL }, + { "relax", 0, POPT_ARG_NONE, NULL, CMDLINE_RELAX, "pass relax control", 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 }, { NULL } }; -void ldb_cmdline_help(const char *cmdname, FILE *f) +void ldb_cmdline_help(struct ldb_context *ldb, const char *cmdname, FILE *f) { poptContext pc; - pc = poptGetContext(cmdname, 0, NULL, popt_options, + struct poptOption **popt_options = ldb_module_popt_options(ldb); + 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) +{ + unsigned 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, - void (*usage)(void)) + void (*usage)(struct ldb_context *)) { struct ldb_cmdline *ret=NULL; poptContext pc; -#if (_SAMBA_BUILD_ >= 4) - int r; -#endif int num_options = 0; int opt; int flags = 0; + int rc; + struct poptOption **popt_options; -#if (_SAMBA_BUILD_ >= 4) - r = ldb_register_samba_handlers(ldb); - if (r != 0) { - goto failed; - } - -#endif + /* make the ldb utilities line buffered */ + setlinebuf(stdout); ret = talloc_zero(ldb, struct ldb_cmdline); if (ret == NULL) { @@ -121,7 +130,16 @@ struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, options.scope = LDB_SCOPE_DEFAULT; - pc = poptGetContext(argv[0], argc, argv, popt_options, + popt_options = ldb_module_popt_options(ldb); + (*popt_options) = builtin_popt_options; + + rc = ldb_modules_hook(ldb, LDB_MODULE_HOOK_CMDLINE_OPTIONS); + if (rc != LDB_SUCCESS) { + fprintf(stderr, "ldb: failed to run command line hooks : %s\n", ldb_strerror(rc)); + goto failed; + } + + pc = poptGetContext(argv[0], argc, argv, *popt_options, POPT_CONTEXT_KEEP_FIRST); while((opt = poptGetNextOpt(pc)) != -1) { @@ -159,36 +177,79 @@ struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, case 'c': { const char *cs = poptGetOptArg(pc); - const char *p, *q; - int cc; - - for (p = cs, cc = 1; (q = strchr(p, ',')); cc++, p = q + 1) ; + const char *p; - 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:0")) { + fprintf(stderr, __location__ ": out of memory\n"); + goto failed; + } + break; + case 'd': + if (!add_control(ret, "show_deactivated_link:0")) { + 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 CMDLINE_RELAX: + if (!add_control(ret, "relax: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)); - if (usage) usage(); + if (usage) usage(ldb); goto failed; } } @@ -205,7 +266,7 @@ struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, /* all utils need some option */ if (ret->url == NULL) { fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n"); - if (usage) usage(); + if (usage) usage(ldb); goto failed; } @@ -225,27 +286,14 @@ struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, flags |= LDB_FLG_ENABLE_TRACING; } -#if (_SAMBA_BUILD_ >= 4) - /* Must be after we have processed command line options */ - gensec_init(cmdline_lp_ctx); - - if (ldb_set_opaque(ldb, "sessionInfo", system_session(ldb, 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 - if (options.modules_path != NULL) { ldb_set_modules_dir(ldb, options.modules_path); - } else if (getenv("LDB_MODULES_PATH") != NULL) { - ldb_set_modules_dir(ldb, getenv("LDB_MODULES_PATH")); + } + + rc = ldb_modules_hook(ldb, LDB_MODULE_HOOK_CMDLINE_PRECONNECT); + if (rc != LDB_SUCCESS) { + fprintf(stderr, "ldb: failed to run preconnect hooks : %s\n", ldb_strerror(rc)); + goto failed; } /* now connect to the ldb */ @@ -255,11 +303,17 @@ struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, goto failed; } + rc = ldb_modules_hook(ldb, LDB_MODULE_HOOK_CMDLINE_POSTCONNECT); + if (rc != LDB_SUCCESS) { + fprintf(stderr, "ldb: failed to run post connect hooks : %s\n", ldb_strerror(rc)); + goto failed; + } + return ret; failed: talloc_free(ret); - exit(1); + exit(LDB_ERR_OPERATIONS_ERROR); return NULL; } @@ -273,8 +327,8 @@ failed: */ int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request) { - int i, j; - int ret = 0; + unsigned int i, j; + int ret = 0; if (reply == NULL || request == NULL) return -1; |