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