summaryrefslogtreecommitdiff
path: root/source4/lib
diff options
context:
space:
mode:
Diffstat (limited to 'source4/lib')
-rw-r--r--source4/lib/ldb/common/ldb.c41
-rw-r--r--source4/lib/ldb/include/ldb.h5
-rw-r--r--source4/lib/ldb/tools/ldbsearch.c4
3 files changed, 50 insertions, 0 deletions
diff --git a/source4/lib/ldb/common/ldb.c b/source4/lib/ldb/common/ldb.c
index ce42a36a59..f49fb2d955 100644
--- a/source4/lib/ldb/common/ldb.c
+++ b/source4/lib/ldb/common/ldb.c
@@ -529,6 +529,43 @@ error:
return LDB_ERR_OPERATIONS_ERROR;
}
+/*
+ try to autodetect a basedn if none specified. This fixes one of my
+ pet hates about ldapsearch, which is that you have to get a long,
+ complex basedn right to make any use of it.
+*/
+const struct ldb_dn *ldb_auto_basedn(struct ldb_context *ldb)
+{
+ TALLOC_CTX *tmp_ctx;
+ int ret;
+ static const char *attrs[] = { "defaultNamingContext", NULL };
+ struct ldb_result *res;
+ struct ldb_dn *basedn=NULL;
+
+ basedn = ldb_get_opaque(ldb, "auto_baseDN");
+ if (basedn) {
+ return basedn;
+ }
+
+ tmp_ctx = talloc_new(ldb);
+ ret = ldb_search(ldb, ldb_dn_new(tmp_ctx), LDB_SCOPE_BASE,
+ "(objectClass=*)", attrs, &res);
+ if (ret == LDB_SUCCESS && res->count == 1) {
+ basedn = ldb_msg_find_attr_as_dn(ldb, res->msgs[0], "defaultNamingContext");
+ }
+
+ if (basedn) {
+ ldb_set_opaque(ldb, "auto_baseDN", basedn);
+ }
+
+ talloc_free(tmp_ctx);
+ return basedn;
+}
+
+/*
+ note that ldb_search() will automatically replace a NULL 'base' value with the
+ defaultNamingContext from the rootDSE if available.
+*/
int ldb_search(struct ldb_context *ldb,
const struct ldb_dn *base,
enum ldb_scope scope,
@@ -547,6 +584,10 @@ int ldb_search(struct ldb_context *ldb,
return LDB_ERR_OPERATIONS_ERROR;
}
+ if (base == NULL) {
+ base = ldb_auto_basedn(ldb);
+ }
+
req->operation = LDB_SEARCH;
req->op.search.base = base;
req->op.search.scope = scope;
diff --git a/source4/lib/ldb/include/ldb.h b/source4/lib/ldb/include/ldb.h
index b07cdd964c..415eacbf61 100644
--- a/source4/lib/ldb/include/ldb.h
+++ b/source4/lib/ldb/include/ldb.h
@@ -822,6 +822,11 @@ struct ldb_context *ldb_init(void *mem_ctx);
*/
int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[]);
+/*
+ return an automatic baseDN from the defaultNamingContext of the rootDSE
+*/
+const struct ldb_dn *ldb_auto_basedn(struct ldb_context *ldb);
+
/**
Search the database
diff --git a/source4/lib/ldb/tools/ldbsearch.c b/source4/lib/ldb/tools/ldbsearch.c
index 6bbd9e2f74..f151164559 100644
--- a/source4/lib/ldb/tools/ldbsearch.c
+++ b/source4/lib/ldb/tools/ldbsearch.c
@@ -218,6 +218,10 @@ static int do_search(struct ldb_context *ldb,
sctx->entries = 0;
sctx->refs = 0;
+ if (basedn == NULL) {
+ basedn = ldb_auto_basedn(ldb);
+ }
+
req->operation = LDB_SEARCH;
req->op.search.base = basedn;
req->op.search.scope = options->scope;