svn commit: samba r17821 - in branches/SAMBA_4_0/source/lib/ldb:
common include tools
tridge at samba.org
tridge at samba.org
Fri Aug 25 06:41:39 GMT 2006
Author: tridge
Date: 2006-08-25 06:41:37 +0000 (Fri, 25 Aug 2006)
New Revision: 17821
WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=17821
Log:
changed ldb_search() and the ldbsearch command line utility to
automatically work out the basedn when basedn==NULL. The basedn is
fetched from the rootDSE defaultNamingContext value (if there is one)
This means we don't have to have the defaultNamingContext logic in
lots of places. It makes a lot of sense to me to have basedn==NULL
mean "use the default, as given by the database"
Note that explicitly specifing a basedn of '' is not the same thing,
and will not trigger this code
The baseDN is cached in a ldb opaque, so we only have to fetch it once
Modified:
branches/SAMBA_4_0/source/lib/ldb/common/ldb.c
branches/SAMBA_4_0/source/lib/ldb/include/ldb.h
branches/SAMBA_4_0/source/lib/ldb/tools/ldbsearch.c
Changeset:
Modified: branches/SAMBA_4_0/source/lib/ldb/common/ldb.c
===================================================================
--- branches/SAMBA_4_0/source/lib/ldb/common/ldb.c 2006-08-25 06:38:29 UTC (rev 17820)
+++ branches/SAMBA_4_0/source/lib/ldb/common/ldb.c 2006-08-25 06:41:37 UTC (rev 17821)
@@ -529,6 +529,43 @@
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 @@
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;
Modified: branches/SAMBA_4_0/source/lib/ldb/include/ldb.h
===================================================================
--- branches/SAMBA_4_0/source/lib/ldb/include/ldb.h 2006-08-25 06:38:29 UTC (rev 17820)
+++ branches/SAMBA_4_0/source/lib/ldb/include/ldb.h 2006-08-25 06:41:37 UTC (rev 17821)
@@ -822,6 +822,11 @@
*/
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
Modified: branches/SAMBA_4_0/source/lib/ldb/tools/ldbsearch.c
===================================================================
--- branches/SAMBA_4_0/source/lib/ldb/tools/ldbsearch.c 2006-08-25 06:38:29 UTC (rev 17820)
+++ branches/SAMBA_4_0/source/lib/ldb/tools/ldbsearch.c 2006-08-25 06:41:37 UTC (rev 17821)
@@ -218,6 +218,10 @@
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;
More information about the samba-cvs
mailing list