2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2004-2009
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 the stupidity of the unix fcntl locking design forces us to never
24 allow a database file to be opened twice in the same process. These
25 wrappers provide convenient access to a tdb or ldb, taking advantage
26 of talloc destructors to ensure that only a single open is done
30 #include "lib/events/events.h"
32 #include <ldb_errors.h>
33 #include "lib/ldb-samba/ldif_handlers.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "param/param.h"
37 #include "../lib/util/dlinklist.h"
38 #include "../lib/tdb_compat/tdb_compat.h"
41 this is used to catch debug messages from ldb
43 static void ldb_wrap_debug(void *context, enum ldb_debug_level level,
44 const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
46 static void ldb_wrap_debug(void *context, enum ldb_debug_level level,
47 const char *fmt, va_list ap)
58 case LDB_DEBUG_WARNING:
66 vasprintf(&s, fmt, ap);
68 DEBUG(samba_level, ("ldb: %s\n", s));
74 connecting to a ldb can be a relatively expensive operation because
75 of the schema and partition loads. We keep a list of open ldb
76 contexts here, and try to re-use when possible.
78 This means callers of ldb_wrap_connect() must use talloc_unlink() or
79 the free of a parent to destroy the context
81 static struct ldb_wrap {
82 struct ldb_wrap *next, *prev;
83 struct ldb_wrap_context {
84 /* the context is what we use to tell if two ldb
85 * connections are exactly equivalent
88 struct tevent_context *ev;
89 struct loadparm_context *lp_ctx;
90 struct auth_session_info *session_info;
91 struct cli_credentials *credentials;
94 struct ldb_context *ldb;
98 free a ldb_wrap structure
100 static int ldb_wrap_destructor(struct ldb_wrap *w)
102 DLIST_REMOVE(ldb_wrap_list, w);
107 * The casefolder for s4's LDB databases - Unicode-safe
109 char *wrap_casefold(void *context, void *mem_ctx, const char *s, size_t n)
111 return strupper_talloc_n(mem_ctx, s, n);
115 struct ldb_context *samba_ldb_init(TALLOC_CTX *mem_ctx,
116 struct tevent_context *ev,
117 struct loadparm_context *lp_ctx,
118 struct auth_session_info *session_info,
119 struct cli_credentials *credentials)
121 struct ldb_context *ldb;
124 ldb = ldb_init(mem_ctx, ev);
129 ldb_set_modules_dir(ldb, modules_path(ldb, "ldb"));
131 ldb_set_debug(ldb, ldb_wrap_debug, NULL);
133 ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
136 if (ldb_set_opaque(ldb, "sessionInfo", session_info)) {
143 if (ldb_set_opaque(ldb, "credentials", credentials)) {
149 if (ldb_set_opaque(ldb, "loadparm", lp_ctx)) {
154 /* This must be done before we load the schema, as these
155 * handlers for objectSid and objectGUID etc must take
156 * precedence over the 'binary attribute' declaration in the
158 ret = ldb_register_samba_handlers(ldb);
159 if (ret != LDB_SUCCESS) {
164 /* we usually want Samba databases to be private. If we later
165 find we need one public, we will need to add a parameter to
166 ldb_wrap_connect() */
167 ldb_set_create_perms(ldb, 0600);
172 struct ldb_context *ldb_wrap_find(const char *url,
173 struct tevent_context *ev,
174 struct loadparm_context *lp_ctx,
175 struct auth_session_info *session_info,
176 struct cli_credentials *credentials,
180 /* see if we can re-use an existing ldb */
181 for (w=ldb_wrap_list; w; w=w->next) {
182 if (w->context.ev == ev &&
183 w->context.lp_ctx == lp_ctx &&
184 w->context.session_info == session_info &&
185 w->context.credentials == credentials &&
186 w->context.flags == flags &&
187 (w->context.url == url || strcmp(w->context.url, url) == 0))
194 int samba_ldb_connect(struct ldb_context *ldb, struct loadparm_context *lp_ctx,
195 const char *url, unsigned int flags)
198 char *real_url = NULL;
200 /* allow admins to force non-sync ldb for all databases */
201 if (lpcfg_parm_bool(lp_ctx, NULL, "ldb", "nosync", false)) {
202 flags |= LDB_FLG_NOSYNC;
206 flags |= LDB_FLG_ENABLE_TRACING;
209 real_url = lpcfg_private_path(ldb, lp_ctx, url);
210 if (real_url == NULL) {
211 return LDB_ERR_OPERATIONS_ERROR;
214 ret = ldb_connect(ldb, real_url, flags, NULL);
216 if (ret != LDB_SUCCESS) {
220 /* setup for leak detection */
221 ldb_set_opaque(ldb, "wrap_url", real_url);
226 bool ldb_wrap_add(const char *url, struct tevent_context *ev,
227 struct loadparm_context *lp_ctx,
228 struct auth_session_info *session_info,
229 struct cli_credentials *credentials,
231 struct ldb_context *ldb)
234 struct ldb_wrap_context c;
236 /* add to the list of open ldb contexts */
237 w = talloc(ldb, struct ldb_wrap);
245 c.session_info = session_info;
246 c.credentials = credentials;
250 w->context.url = talloc_strdup(w, url);
251 if (w->context.url == NULL) {
256 /* take a reference to the session_info, as it is
257 * possible for the ldb to live longer than the
258 * session_info. This happens when a DRS DsBind call
259 * reuses a handle, but the original connection is
260 * shutdown. The token for the new connection is still
261 * valid, so we need the session_info to remain valid for
264 if (talloc_reference(w, session_info) == NULL) {
271 DLIST_ADD(ldb_wrap_list, w);
273 talloc_set_destructor(w, ldb_wrap_destructor);
280 wrapped connection to a ldb database
281 to close just talloc_free() the returned ldb_context
283 TODO: We need an error_string parameter
285 struct ldb_context *ldb_wrap_connect(TALLOC_CTX *mem_ctx,
286 struct tevent_context *ev,
287 struct loadparm_context *lp_ctx,
289 struct auth_session_info *session_info,
290 struct cli_credentials *credentials,
293 struct ldb_context *ldb;
296 ldb = ldb_wrap_find(url, ev, lp_ctx, session_info, credentials, flags);
298 return talloc_reference(mem_ctx, ldb);
300 ldb = samba_ldb_init(mem_ctx, ev, lp_ctx, session_info, credentials);
305 ret = samba_ldb_connect(ldb, lp_ctx, url, flags);
306 if (ret != LDB_SUCCESS) {
311 if (!ldb_wrap_add(url, ev, lp_ctx, session_info, credentials, flags, ldb)) {
316 DEBUG(3,("ldb_wrap open of %s\n", url));
322 when we fork() we need to make sure that any open ldb contexts have
323 any open transactions cancelled
325 void ldb_wrap_fork_hook(void)
329 for (w=ldb_wrap_list; w; w=w->next) {
330 if (ldb_transaction_cancel_noerr(w->ldb) != LDB_SUCCESS) {
331 smb_panic("Failed to cancel child transactions\n");
335 if (tdb_reopen_all(1) != 0) {
336 smb_panic("tdb_reopen_all failed\n");
340 char *ldb_relative_path(struct ldb_context *ldb,
344 const char *base_url =
345 (const char *)ldb_get_opaque(ldb, "ldb_url");
346 char *path, *p, *full_name;
350 if (strncmp("tdb://", base_url, 6) == 0) {
351 base_url = base_url+6;
353 path = talloc_strdup(mem_ctx, base_url);
357 if ( (p = strrchr(path, '/')) != NULL) {
359 full_name = talloc_asprintf(mem_ctx, "%s/%s", path, name);
361 full_name = talloc_asprintf(mem_ctx, "./%s", name);