ldb-samba: Handle generic mdb:// url scheme in ldb_relative_path()
[metze/samba/wip.git] / lib / ldb-samba / ldb_wrap.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    LDB wrap functions
5
6    Copyright (C) Andrew Tridgell 2004-2009
7
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.
12
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.
17
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/>.
20 */
21
22 /*
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
27 */
28
29 #include "includes.h"
30 #include "lib/events/events.h"
31 #include <ldb.h>
32 #include <ldb_errors.h>
33 #include "lib/ldb-samba/ldif_handlers.h"
34 #include "ldb_wrap.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "param/param.h"
37 #include "../lib/util/dlinklist.h"
38 #include "lib/util/util_paths.h"
39 #include <tdb.h>
40
41 #undef DBGC_CLASS
42 #define DBGC_CLASS DBGC_LDB
43
44 /*
45   this is used to catch debug messages from ldb
46 */
47 static void ldb_wrap_debug(void *context, enum ldb_debug_level level,
48                            const char *fmt, va_list ap)  PRINTF_ATTRIBUTE(3,0);
49
50 static void ldb_wrap_debug(void *context, enum ldb_debug_level level,
51                            const char *fmt, va_list ap)
52 {
53         int samba_level = -1;
54         switch (level) {
55         case LDB_DEBUG_FATAL:
56                 samba_level = 0;
57                 break;
58         case LDB_DEBUG_ERROR:
59                 samba_level = 1;
60                 break;
61         case LDB_DEBUG_WARNING:
62                 samba_level = 2;
63                 break;
64         case LDB_DEBUG_TRACE:
65                 samba_level = 10;
66                 break;
67
68         };
69         if (CHECK_DEBUGLVL(samba_level)) {
70                 char *s = NULL;
71                 int ret;
72
73                 ret = vasprintf(&s, fmt, ap);
74                 if (ret == -1) {
75                         return;
76                 }
77                 DEBUG(samba_level, ("ldb: %s\n", s));
78                 free(s);
79         }
80 }
81
82
83 /*
84   connecting to a ldb can be a relatively expensive operation because
85   of the schema and partition loads. We keep a list of open ldb
86   contexts here, and try to re-use when possible.
87
88   This means callers of ldb_wrap_connect() must use talloc_unlink() or
89   the free of a parent to destroy the context
90  */
91 static struct ldb_wrap {
92         struct ldb_wrap *next, *prev;
93         struct ldb_wrap_context {
94                 /* the context is what we use to tell if two ldb
95                  * connections are exactly equivalent
96                  */
97                 pid_t pid; /* We want to re-open in a new PID due to
98                             * the LMDB backend */
99                 const char *url;
100                 struct tevent_context *ev;
101                 struct loadparm_context *lp_ctx;
102                 struct auth_session_info *session_info;
103                 struct cli_credentials *credentials;
104                 unsigned int flags;
105         } context;
106         struct ldb_context *ldb;
107 } *ldb_wrap_list;
108
109 /*
110    free a ldb_wrap structure
111  */
112 static int ldb_wrap_destructor(struct ldb_wrap *w)
113 {
114         DLIST_REMOVE(ldb_wrap_list, w);
115         return 0;
116 }
117
118 /*
119  * The casefolder for s4's LDB databases - Unicode-safe
120  */
121 char *wrap_casefold(void *context, void *mem_ctx, const char *s, size_t n)
122 {
123         return strupper_talloc_n(mem_ctx, s, n);
124 }
125
126
127  struct ldb_context *samba_ldb_init(TALLOC_CTX *mem_ctx,
128                                     struct tevent_context *ev,
129                                     struct loadparm_context *lp_ctx,
130                                     struct auth_session_info *session_info,
131                                     struct cli_credentials *credentials)
132 {
133         struct ldb_context *ldb;
134         int ret;
135
136         ldb = ldb_init(mem_ctx, ev);
137         if (ldb == NULL) {
138                 return NULL;
139         }
140
141         ldb_set_modules_dir(ldb, modules_path(ldb, "ldb"));
142
143         ldb_set_debug(ldb, ldb_wrap_debug, NULL);
144
145         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
146
147         if (session_info) {
148                 if (ldb_set_opaque(ldb, "sessionInfo", session_info)) {
149                         talloc_free(ldb);
150                         return NULL;
151                 }
152         }
153
154         if (credentials) {
155                 if (ldb_set_opaque(ldb, "credentials", credentials)) {
156                         talloc_free(ldb);
157                         return NULL;
158                 }
159         }
160
161         if (ldb_set_opaque(ldb, "loadparm", lp_ctx)) {
162                 talloc_free(ldb);
163                 return NULL;
164         }
165
166         /* This must be done before we load the schema, as these
167          * handlers for objectSid and objectGUID etc must take
168          * precedence over the 'binary attribute' declaration in the
169          * schema */
170         ret = ldb_register_samba_handlers(ldb);
171         if (ret != LDB_SUCCESS) {
172                 talloc_free(ldb);
173                 return NULL;
174         }
175
176         /* we usually want Samba databases to be private. If we later
177            find we need one public, we will need to add a parameter to
178            ldb_wrap_connect() */
179         ldb_set_create_perms(ldb, 0600);
180
181         return ldb;
182 }
183
184  struct ldb_context *ldb_wrap_find(const char *url,
185                                    struct tevent_context *ev,
186                                    struct loadparm_context *lp_ctx,
187                                    struct auth_session_info *session_info,
188                                    struct cli_credentials *credentials,
189                                    unsigned int flags)
190 {
191         pid_t pid = getpid();
192         struct ldb_wrap *w;
193         /* see if we can re-use an existing ldb */
194         for (w=ldb_wrap_list; w; w=w->next) {
195                 if (w->context.pid == pid &&
196                     w->context.ev == ev &&
197                     w->context.lp_ctx == lp_ctx &&
198                     w->context.session_info == session_info &&
199                     w->context.credentials == credentials &&
200                     w->context.flags == flags &&
201                     (w->context.url == url || strcmp(w->context.url, url) == 0))
202                         return w->ldb;
203         }
204
205         return NULL;
206 }
207
208 int samba_ldb_connect(struct ldb_context *ldb, struct loadparm_context *lp_ctx,
209                       const char *url, unsigned int flags)
210 {
211         int ret;
212         char *real_url = NULL;
213
214         /* allow admins to force non-sync ldb for all databases */
215         if (lpcfg_parm_bool(lp_ctx, NULL, "ldb", "nosync", false)) {
216                 flags |= LDB_FLG_NOSYNC;
217         }
218
219         if (DEBUGLVL(10)) {
220                 flags |= LDB_FLG_ENABLE_TRACING;
221         }
222
223         real_url = lpcfg_private_path(ldb, lp_ctx, url);
224         if (real_url == NULL) {
225                 return LDB_ERR_OPERATIONS_ERROR;
226         }
227
228         ret = ldb_connect(ldb, real_url, flags, NULL);
229
230         if (ret != LDB_SUCCESS) {
231                 return ret;
232         }
233
234         /* setup for leak detection */
235         ldb_set_opaque(ldb, "wrap_url", real_url);
236
237         return LDB_SUCCESS;
238 }
239
240  bool ldb_wrap_add(const char *url, struct tevent_context *ev,
241                    struct loadparm_context *lp_ctx,
242                    struct auth_session_info *session_info,
243                    struct cli_credentials *credentials,
244                    unsigned int flags,
245                    struct ldb_context *ldb)
246 {
247         struct ldb_wrap *w;
248         struct ldb_wrap_context c;
249
250         /* add to the list of open ldb contexts */
251         w = talloc(ldb, struct ldb_wrap);
252         if (w == NULL) {
253                 return false;
254         }
255
256         c.pid          = getpid();
257         c.url          = url;
258         c.ev           = ev;
259         c.lp_ctx       = lp_ctx;
260         c.session_info = session_info;
261         c.credentials  = credentials;
262         c.flags        = flags;
263
264         w->context = c;
265         w->context.url = talloc_strdup(w, url);
266         if (w->context.url == NULL) {
267                 return false;
268         }
269
270         if (session_info) {
271                 /* take a reference to the session_info, as it is
272                  * possible for the ldb to live longer than the
273                  * session_info. This happens when a DRS DsBind call
274                  * reuses a handle, but the original connection is
275                  * shutdown. The token for the new connection is still
276                  * valid, so we need the session_info to remain valid for
277                  * ldb modules to use
278                  */
279                 if (talloc_reference(w, session_info) == NULL) {
280                         return false;
281                 }
282         }
283
284         w->ldb = ldb;
285
286         DLIST_ADD(ldb_wrap_list, w);
287
288         talloc_set_destructor(w, ldb_wrap_destructor);
289
290         return true;
291 }
292
293
294 /*
295   wrapped connection to a ldb database
296   to close just talloc_free() the returned ldb_context
297
298   TODO:  We need an error_string parameter
299  */
300  struct ldb_context *ldb_wrap_connect(TALLOC_CTX *mem_ctx,
301                                       struct tevent_context *ev,
302                                       struct loadparm_context *lp_ctx,
303                                       const char *url,
304                                       struct auth_session_info *session_info,
305                                       struct cli_credentials *credentials,
306                                       unsigned int flags)
307 {
308         struct ldb_context *ldb;
309         int ret;
310
311         /*
312          * Unlike samdb_connect_url() do not try and cache the LDB
313          * handle, get a new one each time.  Only sam.ldb is
314          * punitively expensive to open and helpful caches like this
315          * cause challenges (such as if the value for 'private dir'
316          * changes).
317          */
318
319         ldb = samba_ldb_init(mem_ctx, ev, lp_ctx, session_info, credentials);
320
321         if (ldb == NULL)
322                 return NULL;
323
324         ret = samba_ldb_connect(ldb, lp_ctx, url, flags);
325         if (ret != LDB_SUCCESS) {
326                 talloc_free(ldb);
327                 return NULL;
328         }
329
330         DEBUG(3,("ldb_wrap open of %s\n", url));
331
332         return ldb;
333 }
334
335 /*
336   call tdb_reopen_all() in case there is a TDB open so we are
337   not blocked from re-opening it inside ldb_tdb.
338 */
339  void ldb_wrap_fork_hook(void)
340 {
341         if (tdb_reopen_all(1) != 0) {
342                 smb_panic("tdb_reopen_all failed\n");
343         }
344 }
345
346  char *ldb_relative_path(struct ldb_context *ldb,
347                          TALLOC_CTX *mem_ctx,
348                          const char *name)
349 {
350         const char *base_url =
351                 (const char *)ldb_get_opaque(ldb, "ldb_url");
352         char *path, *p, *full_name;
353         if (name == NULL) {
354                 return NULL;
355         }
356         if (strncmp("tdb://", base_url, 6) == 0) {
357                 base_url = base_url+6;
358         } else if (strncmp("mdb://", base_url, 6) == 0) {
359                 base_url = base_url+6;
360         } else if (strncmp("ldb://", base_url, 6) == 0) {
361                 base_url = base_url+6;
362         }
363         path = talloc_strdup(mem_ctx, base_url);
364         if (path == NULL) {
365                 return NULL;
366         }
367         if ( (p = strrchr(path, '/')) != NULL) {
368                 p[0] = '\0';
369                 full_name = talloc_asprintf(mem_ctx, "%s/%s", path, name);
370         } else {
371                 full_name = talloc_asprintf(mem_ctx, "./%s", name);
372         }
373         talloc_free(path);
374         return full_name;
375 }