ldb-samba: Add ldb_wrap_add, remove last schema reference from ldb_wrap.
authorJelmer Vernooij <jelmer@samba.org>
Sun, 10 Oct 2010 21:25:38 +0000 (23:25 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Sun, 10 Oct 2010 21:25:38 +0000 (23:25 +0200)
source4/dsdb/samdb/samdb.c
source4/lib/ldb-samba/ldb_wrap.c
source4/lib/ldb-samba/ldb_wrap.h
source4/smbd/server.c

index 8b0c0fec3f52d55f8d0de280ef9dad738d033a17..c2a1e9ec8010b60c81b88c2c20b928b90095da4b 100644 (file)
@@ -128,12 +128,27 @@ struct ldb_context *samdb_connect(TALLOC_CTX *mem_ctx,
 {
        struct ldb_context *ldb;
        struct dsdb_schema *schema;
-       ldb = ldb_wrap_connect(mem_ctx, ev_ctx, lp_ctx,
-                              lpcfg_sam_url(lp_ctx), session_info,
-                              samdb_credentials(ev_ctx, lp_ctx),
-                              flags);
+       const char *url;
+       struct cli_credentials *credentials;
+       int ret;
 
-       if (!ldb) {
+       url  = lpcfg_sam_url(lp_ctx);
+       credentials = samdb_credentials(ev_ctx, lp_ctx);
+
+       ldb = ldb_wrap_find(url, ev_ctx, lp_ctx, session_info, credentials, flags);
+       if (ldb != NULL)
+               return talloc_reference(mem_ctx, ldb);
+
+       ldb = samba_ldb_init(mem_ctx, ev_ctx, lp_ctx, session_info, credentials);
+
+       if (ldb == NULL)
+               return NULL;
+
+       dsdb_set_global_schema(ldb);
+
+       ret = samba_ldb_connect(ldb, lp_ctx, url, flags);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(ldb);
                return NULL;
        }
 
@@ -143,6 +158,11 @@ struct ldb_context *samdb_connect(TALLOC_CTX *mem_ctx,
                dsdb_make_schema_global(ldb, schema);
        }
 
+       if (!ldb_wrap_add(url, ev_ctx, lp_ctx, session_info, credentials, flags, ldb)) {
+               talloc_free(ldb);
+               return NULL;
+       }
+
        return ldb;
 }
 
index 5aff11229eac5697a0a4a96d06ba96413252296d..7ac94dea2c0829477842a2680b58cdda260937a3 100644 (file)
@@ -107,8 +107,7 @@ static int ldb_wrap_destructor(struct ldb_wrap *w)
                                                                  struct tevent_context *ev,
                                                                  struct loadparm_context *lp_ctx,
                                                                  struct auth_session_info *session_info,
-                                                                 struct cli_credentials *credentials,
-                                                                 int flags
+                                                                 struct cli_credentials *credentials
                                                                  )
 {
        struct ldb_context *ldb;
@@ -164,15 +163,6 @@ static int ldb_wrap_destructor(struct ldb_wrap *w)
                return NULL;
        }
 
-       /* allow admins to force non-sync ldb for all databases */
-       if (lpcfg_parm_bool(lp_ctx, NULL, "ldb", "nosync", false)) {
-               flags |= LDB_FLG_NOSYNC;
-       }
-
-       if (DEBUGLVL(10)) {
-               flags |= LDB_FLG_ENABLE_TRACING;
-       }
-
        /* we usually want Samba databases to be private. If we later
           find we need one public, we will need to add a parameter to
           ldb_wrap_connect() */
@@ -203,60 +193,51 @@ static int ldb_wrap_destructor(struct ldb_wrap *w)
        return NULL;
 }
 
-/*
-  wrapped connection to a ldb database
-  to close just talloc_free() the returned ldb_context
-
-  TODO:  We need an error_string parameter
- */
- struct ldb_context *ldb_wrap_connect(TALLOC_CTX *mem_ctx,
-                                    struct tevent_context *ev,
-                                    struct loadparm_context *lp_ctx,
-                                    const char *url,
-                                    struct auth_session_info *session_info,
-                                    struct cli_credentials *credentials,
-                                    unsigned int flags)
+int samba_ldb_connect(struct ldb_context *ldb, struct loadparm_context *lp_ctx, const char *url, int flags)
 {
-       struct ldb_context *ldb;
        int ret;
        char *real_url = NULL;
-       struct ldb_wrap_context c;
-       struct ldb_wrap *w;
-
-       ldb = ldb_wrap_find(url, ev, lp_ctx, session_info, credentials, flags);
-       if (ldb != NULL)
-               return talloc_reference(mem_ctx, ldb);
-
-       ldb = samba_ldb_init(mem_ctx, ev, lp_ctx, session_info, credentials, flags);
 
-       if (ldb == NULL)
-               return NULL;
+       /* allow admins to force non-sync ldb for all databases */
+       if (lpcfg_parm_bool(lp_ctx, NULL, "ldb", "nosync", false)) {
+               flags |= LDB_FLG_NOSYNC;
+       }
 
-       if (lp_ctx != NULL && strcmp(lpcfg_sam_url(lp_ctx), url) == 0) {
-               dsdb_set_global_schema(ldb);
+       if (DEBUGLVL(10)) {
+               flags |= LDB_FLG_ENABLE_TRACING;
        }
 
        real_url = private_path(ldb, lp_ctx, url);
        if (real_url == NULL) {
-               talloc_free(ldb);
-               return NULL;
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       
        ret = ldb_connect(ldb, real_url, flags, NULL);
+
        if (ret != LDB_SUCCESS) {
-               talloc_free(ldb);
-               return NULL;
+               return ret;
        }
 
        /* setup for leak detection */
        ldb_set_opaque(ldb, "wrap_url", real_url);
-       
+
+       return LDB_SUCCESS;
+}
+
+ bool ldb_wrap_add(const char *url, struct tevent_context *ev,
+                                 struct loadparm_context *lp_ctx,
+                                 struct auth_session_info *session_info,
+                                 struct cli_credentials *credentials,
+                                 int flags,
+                                 struct ldb_context *ldb)
+{
+       struct ldb_wrap *w;
+       struct ldb_wrap_context c;
+
        /* add to the list of open ldb contexts */
        w = talloc(ldb, struct ldb_wrap);
        if (w == NULL) {
-               talloc_free(ldb);
-               return NULL;
+               return false;
        }
 
        c.url          = url;
@@ -269,8 +250,7 @@ static int ldb_wrap_destructor(struct ldb_wrap *w)
        w->context = c;
        w->context.url = talloc_strdup(w, url);
        if (w->context.url == NULL) {
-               talloc_free(ldb);
-               return NULL;
+               return false;
        }
 
        if (session_info) {
@@ -283,8 +263,7 @@ static int ldb_wrap_destructor(struct ldb_wrap *w)
                 * ldb modules to use
                 */
                if (talloc_reference(w, session_info) == NULL) {
-                       talloc_free(ldb);
-                       return NULL;
+                       return false;
                }
        }
 
@@ -292,10 +271,51 @@ static int ldb_wrap_destructor(struct ldb_wrap *w)
 
        DLIST_ADD(ldb_wrap_list, w);
 
-       DEBUG(3,("ldb_wrap open of %s\n", url));
-
        talloc_set_destructor(w, ldb_wrap_destructor);
 
+       return true;
+}
+
+
+/*
+  wrapped connection to a ldb database
+  to close just talloc_free() the returned ldb_context
+
+  TODO:  We need an error_string parameter
+ */
+ struct ldb_context *ldb_wrap_connect(TALLOC_CTX *mem_ctx,
+                                    struct tevent_context *ev,
+                                    struct loadparm_context *lp_ctx,
+                                    const char *url,
+                                    struct auth_session_info *session_info,
+                                    struct cli_credentials *credentials,
+                                    unsigned int flags)
+{
+       struct ldb_context *ldb;
+       int ret;
+
+       ldb = ldb_wrap_find(url, ev, lp_ctx, session_info, credentials, flags);
+       if (ldb != NULL)
+               return talloc_reference(mem_ctx, ldb);
+
+       ldb = samba_ldb_init(mem_ctx, ev, lp_ctx, session_info, credentials);
+
+       if (ldb == NULL)
+               return NULL;
+
+       ret = samba_ldb_connect(ldb, lp_ctx, url, flags);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(ldb);
+               return NULL;
+       }
+
+       if (!ldb_wrap_add(url, ev, lp_ctx, session_info, credentials, flags, ldb)) {
+               talloc_free(ldb);
+               return NULL;
+       }
+
+       DEBUG(3,("ldb_wrap open of %s\n", url));
+
        return ldb;
 }
 
index f5768028365fb31a2bf1f8ca8c66979b376fe6d7..a835f665fc012251cacb50eab975706f6f3d8bf5 100644 (file)
@@ -47,13 +47,18 @@ struct ldb_context *samba_ldb_init(TALLOC_CTX *mem_ctx,
                                                                  struct tevent_context *ev,
                                                                  struct loadparm_context *lp_ctx,
                                                                  struct auth_session_info *session_info,
-                                                                 struct cli_credentials *credentials,
-                                                                 int flags);
+                                                                 struct cli_credentials *credentials);
 struct ldb_context *ldb_wrap_find(const char *url,
                                                                  struct tevent_context *ev,
                                                                  struct loadparm_context *lp_ctx,
                                                                  struct auth_session_info *session_info,
                                                                  struct cli_credentials *credentials,
                                                                  int flags);
+bool ldb_wrap_add(const char *url, struct tevent_context *ev,
+                                 struct loadparm_context *lp_ctx,
+                                 struct auth_session_info *session_info,
+                                 struct cli_credentials *credentials,
+                                 int flags,
+                                 struct ldb_context *ldb);
 
 #endif /* _LDB_WRAP_H_ */
index 65364556fc042a70682e3836037452700d6b3e8c..2d837dd1fc86b8f01d65dea21fd5e676d6270242 100644 (file)
@@ -191,7 +191,7 @@ static void prime_ldb_databases(struct tevent_context *event_ctx)
        TALLOC_CTX *db_context;
        db_context = talloc_new(event_ctx);
 
-       samdb_connect(db_context, event_ctx, cmdline_lp_ctx, system_session(cmdline_lp_ctx));
+       samdb_connect(db_context, event_ctx, cmdline_lp_ctx, system_session(cmdline_lp_ctx), 0);
        privilege_connect(db_context, event_ctx, cmdline_lp_ctx);
 
        /* we deliberately leave these open, which allows them to be