r7711: update callers of ldb_connect() for new syntax
authorAndrew Tridgell <tridge@samba.org>
Sat, 18 Jun 2005 07:44:36 +0000 (07:44 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:18:24 +0000 (13:18 -0500)
(This used to be commit f852661463624714ad8e7adc0547b2f07b8f9f6d)

source4/ldap_server/ldap_rootdse.c
source4/lib/db_wrap.c

index 2ceb14981be37273f2bb706c2340e2c866873673..39e299ece153ef408aa1a316ba85454d2c148b79 100644 (file)
        attr->values = blob;\
 } while(0)
 
-struct rootdse_db_context {
-       struct ldb_context *ldb;
-       struct rootdse_db_context **static_ptr;
-};
-
 /*
   this is used to catch debug messages from ldb
 */
@@ -55,60 +50,28 @@ static void rootdse_db_debug(void *context, enum ldb_debug_level level, const ch
 }
 
 
-/* destroy the last connection to the sam */
-static int rootdse_db_destructor(void *ctx)
-{
-       struct rootdse_db_context *rd_ctx = ctx;
-       talloc_free(rd_ctx->ldb);
-       *(rd_ctx->static_ptr) = NULL;
-       return 0;
-}                               
-
 /*
   connect to the SAM database
-  return an opaque context pointer on success, or NULL on failure
  */
-static void *rootdse_db_connect(TALLOC_CTX *mem_ctx)
+static struct ldb_context *rootdse_db_connect(TALLOC_CTX *mem_ctx)
 {
-       static struct rootdse_db_context *ctx;
        char *db_path;
-       /*
-         the way that unix fcntl locking works forces us to have a
-         static ldb handle here rather than a much more sensible
-         approach of having the ldb handle as part of the
-         ldap base structures. Otherwise we would try to open
-         the ldb more than once, and tdb would rightly refuse the
-         second open due to the broken nature of unix locking.
-       */
-       if (ctx != NULL) {
-               return talloc_reference(mem_ctx, ctx);
-       }
-
-       ctx = talloc(mem_ctx, struct rootdse_db_context);
-       if (ctx == NULL) {
-               errno = ENOMEM;
-               return NULL;
-       }
-
-       ctx->static_ptr = &ctx;
+       struct ldb_context *ldb;
 
-       db_path = talloc_asprintf(ctx, "tdb://%s", private_path(ctx, "rootdse.ldb"));
+       db_path = talloc_asprintf(mem_ctx, "tdb://%s", 
+                                 private_path(mem_ctx, "rootdse.ldb"));
        if (db_path == NULL) {
-               errno = ENOMEM;
                return NULL;
        }
 
-       DEBUG(10, ("opening %s\n", db_path));
-       ctx->ldb = ldb_connect(db_path, 0, NULL);
-       if (ctx->ldb == NULL) {
-               talloc_free(ctx);
+       ldb = ldb_wrap_connect(mem_ctx, db_path, 0, NULL);
+       if (ldb == NULL) {
                return NULL;
        }
 
-       talloc_set_destructor(ctx, rootdse_db_destructor);
-       ldb_set_debug(ctx->ldb, rootdse_db_debug, NULL);
+       ldb_set_debug(ldb, rootdse_db_debug, NULL);
 
-       return ctx;
+       return ldb;
 }
 
 
@@ -305,7 +268,7 @@ static NTSTATUS rootdse_Search(struct ldapsrv_partition *partition, struct ldaps
        struct ldb_message **res = NULL;
        int result = LDAP_SUCCESS;
        struct ldapsrv_reply *ent_r, *done_r;
-       struct  rootdse_db_context *rootdsedb;
+       struct ldb_context *ldb;
        const char *errstr = NULL;
        int count, j, y;
        const char **attrs = NULL;
@@ -317,11 +280,11 @@ static NTSTATUS rootdse_Search(struct ldapsrv_partition *partition, struct ldaps
        local_ctx = talloc_named(call, 0, "rootdse_Search local memory context");
        NT_STATUS_HAVE_NO_MEMORY(local_ctx);
 
-       rootdsedb = rootdse_db_connect(local_ctx);
-       NT_STATUS_HAVE_NO_MEMORY(rootdsedb);
+       ldb = rootdse_db_connect(local_ctx);
+       NT_STATUS_HAVE_NO_MEMORY(ldb);
 
        if (r->num_attributes >= 1) {
-               attrs = talloc_array(rootdsedb, const char *, r->num_attributes+1);
+               attrs = talloc_array(ldb, const char *, r->num_attributes+1);
                NT_STATUS_HAVE_NO_MEMORY(attrs);
 
                for (j=0; j < r->num_attributes; j++) {
@@ -331,8 +294,8 @@ static NTSTATUS rootdse_Search(struct ldapsrv_partition *partition, struct ldaps
                attrs[j] = NULL;
        }
 
-       count = ldb_search(rootdsedb->ldb, NULL, 0, "dn=cn=rootDSE", attrs, &res);
-       talloc_steal(rootdsedb, res);
+       count = ldb_search(ldb, NULL, 0, "dn=cn=rootDSE", attrs, &res);
+       talloc_steal(local_ctx, res);
 
        if (count == 1) {
                ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultEntry);
@@ -387,7 +350,7 @@ queue_reply:
        } else if (count == 0) {
                DEBUG(10,("rootdse_Search: no results\n"));
                result = LDAP_NO_SUCH_OBJECT;
-               errstr = ldb_errstring(rootdsedb->ldb);
+               errstr = ldb_errstring(ldb);
        } else if (count > 1) {
                DEBUG(10,("rootdse_Search: too many results[%d]\n", count));
                result = LDAP_OTHER; 
@@ -395,7 +358,7 @@ queue_reply:
        } else if (count == -1) {
                DEBUG(10,("rootdse_Search: error\n"));
                result = LDAP_OTHER;
-               errstr = ldb_errstring(rootdsedb->ldb);
+               errstr = ldb_errstring(ldb);
        }
 
        done = &done_r->msg->r.SearchResultDone;
index a3a9ee9b86dc199c4668821cd195234f0c7e6803..33c2af28515460218096bae2f58706b7f6a58f48 100644 (file)
@@ -75,12 +75,13 @@ static int ldb_wrap_destructor(void *ctx)
   to close just talloc_free() the returned ldb_context
  */
 struct ldb_context *ldb_wrap_connect(TALLOC_CTX *mem_ctx,
-                                 const char *url,
-                                 unsigned int flags,
-                                 const char *options[])
+                                    const char *url,
+                                    unsigned int flags,
+                                    const char *options[])
 {
        struct ldb_context *ldb;
        struct ldb_wrap *w;
+       int ret;
 
        for (w = ldb_list; w; w = w->next) {
                if (strcmp(url, w->url) == 0) {
@@ -88,10 +89,16 @@ struct ldb_context *ldb_wrap_connect(TALLOC_CTX *mem_ctx,
                }
        }
 
-       ldb = ldb_connect(url, flags, options);
+       ldb = ldb_init(talloc_autofree_context());
        if (ldb == NULL) {
                return NULL;
        }
+       
+       ret = ldb_connect(ldb, url, flags, options);
+       if (ret == -1) {
+               talloc_free(ldb);
+               return NULL;
+       }
 
        w = talloc(ldb, struct ldb_wrap);
        if (w == NULL) {