ldb_wrap: Remove ldb_transaction_cancel_noerr from ldb_wrap_fork_hook()
[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                 const char *url;
98                 struct tevent_context *ev;
99                 struct loadparm_context *lp_ctx;
100                 struct auth_session_info *session_info;
101                 struct cli_credentials *credentials;
102                 unsigned int flags;
103         } context;
104         struct ldb_context *ldb;
105 } *ldb_wrap_list;
106
107 /*
108    free a ldb_wrap structure
109  */
110 static int ldb_wrap_destructor(struct ldb_wrap *w)
111 {
112         DLIST_REMOVE(ldb_wrap_list, w);
113         return 0;
114 }
115
116 /*
117  * The casefolder for s4's LDB databases - Unicode-safe
118  */
119 char *wrap_casefold(void *context, void *mem_ctx, const char *s, size_t n)
120 {
121         return strupper_talloc_n(mem_ctx, s, n);
122 }
123
124
125  struct ldb_context *samba_ldb_init(TALLOC_CTX *mem_ctx,
126                                     struct tevent_context *ev,
127                                     struct loadparm_context *lp_ctx,
128                                     struct auth_session_info *session_info,
129                                     struct cli_credentials *credentials)
130 {
131         struct ldb_context *ldb;
132         int ret;
133
134         ldb = ldb_init(mem_ctx, ev);
135         if (ldb == NULL) {
136                 return NULL;
137         }
138
139         ldb_set_modules_dir(ldb, modules_path(ldb, "ldb"));
140
141         ldb_set_debug(ldb, ldb_wrap_debug, NULL);
142
143         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
144
145         if (session_info) {
146                 if (ldb_set_opaque(ldb, "sessionInfo", session_info)) {
147                         talloc_free(ldb);
148                         return NULL;
149                 }
150         }
151
152         if (credentials) {
153                 if (ldb_set_opaque(ldb, "credentials", credentials)) {
154                         talloc_free(ldb);
155                         return NULL;
156                 }
157         }
158
159         if (ldb_set_opaque(ldb, "loadparm", lp_ctx)) {
160                 talloc_free(ldb);
161                 return NULL;
162         }
163
164         /* This must be done before we load the schema, as these
165          * handlers for objectSid and objectGUID etc must take
166          * precedence over the 'binary attribute' declaration in the
167          * schema */
168         ret = ldb_register_samba_handlers(ldb);
169         if (ret != LDB_SUCCESS) {
170                 talloc_free(ldb);
171                 return NULL;
172         }
173
174         /* we usually want Samba databases to be private. If we later
175            find we need one public, we will need to add a parameter to
176            ldb_wrap_connect() */
177         ldb_set_create_perms(ldb, 0600);
178
179         return ldb;
180 }
181
182  struct ldb_context *ldb_wrap_find(const char *url,
183                                    struct tevent_context *ev,
184                                    struct loadparm_context *lp_ctx,
185                                    struct auth_session_info *session_info,
186                                    struct cli_credentials *credentials,
187                                    unsigned int flags)
188 {
189         struct ldb_wrap *w;
190         /* see if we can re-use an existing ldb */
191         for (w=ldb_wrap_list; w; w=w->next) {
192                 if (w->context.ev == ev &&
193                     w->context.lp_ctx == lp_ctx &&
194                     w->context.session_info == session_info &&
195                     w->context.credentials == credentials &&
196                     w->context.flags == flags &&
197                     (w->context.url == url || strcmp(w->context.url, url) == 0))
198                         return w->ldb;
199         }
200
201         return NULL;
202 }
203
204 int samba_ldb_connect(struct ldb_context *ldb, struct loadparm_context *lp_ctx,
205                       const char *url, unsigned int flags)
206 {
207         int ret;
208         char *real_url = NULL;
209
210         /* allow admins to force non-sync ldb for all databases */
211         if (lpcfg_parm_bool(lp_ctx, NULL, "ldb", "nosync", false)) {
212                 flags |= LDB_FLG_NOSYNC;
213         }
214
215         if (DEBUGLVL(10)) {
216                 flags |= LDB_FLG_ENABLE_TRACING;
217         }
218
219         real_url = lpcfg_private_path(ldb, lp_ctx, url);
220         if (real_url == NULL) {
221                 return LDB_ERR_OPERATIONS_ERROR;
222         }
223
224         ret = ldb_connect(ldb, real_url, flags, NULL);
225
226         if (ret != LDB_SUCCESS) {
227                 return ret;
228         }
229
230         /* setup for leak detection */
231         ldb_set_opaque(ldb, "wrap_url", real_url);
232
233         return LDB_SUCCESS;
234 }
235
236  bool ldb_wrap_add(const char *url, struct tevent_context *ev,
237                    struct loadparm_context *lp_ctx,
238                    struct auth_session_info *session_info,
239                    struct cli_credentials *credentials,
240                    unsigned int flags,
241                    struct ldb_context *ldb)
242 {
243         struct ldb_wrap *w;
244         struct ldb_wrap_context c;
245
246         /* add to the list of open ldb contexts */
247         w = talloc(ldb, struct ldb_wrap);
248         if (w == NULL) {
249                 return false;
250         }
251
252         c.url          = url;
253         c.ev           = ev;
254         c.lp_ctx       = lp_ctx;
255         c.session_info = session_info;
256         c.credentials  = credentials;
257         c.flags        = flags;
258
259         w->context = c;
260         w->context.url = talloc_strdup(w, url);
261         if (w->context.url == NULL) {
262                 return false;
263         }
264
265         if (session_info) {
266                 /* take a reference to the session_info, as it is
267                  * possible for the ldb to live longer than the
268                  * session_info. This happens when a DRS DsBind call
269                  * reuses a handle, but the original connection is
270                  * shutdown. The token for the new connection is still
271                  * valid, so we need the session_info to remain valid for
272                  * ldb modules to use
273                  */
274                 if (talloc_reference(w, session_info) == NULL) {
275                         return false;
276                 }
277         }
278
279         w->ldb = ldb;
280
281         DLIST_ADD(ldb_wrap_list, w);
282
283         talloc_set_destructor(w, ldb_wrap_destructor);
284
285         return true;
286 }
287
288
289 /*
290   wrapped connection to a ldb database
291   to close just talloc_free() the returned ldb_context
292
293   TODO:  We need an error_string parameter
294  */
295  struct ldb_context *ldb_wrap_connect(TALLOC_CTX *mem_ctx,
296                                       struct tevent_context *ev,
297                                       struct loadparm_context *lp_ctx,
298                                       const char *url,
299                                       struct auth_session_info *session_info,
300                                       struct cli_credentials *credentials,
301                                       unsigned int flags)
302 {
303         struct ldb_context *ldb;
304         int ret;
305
306         ldb = ldb_wrap_find(url, ev, lp_ctx, session_info, credentials, flags);
307         if (ldb != NULL)
308                 return talloc_reference(mem_ctx, ldb);
309
310         ldb = samba_ldb_init(mem_ctx, ev, lp_ctx, session_info, credentials);
311
312         if (ldb == NULL)
313                 return NULL;
314
315         ret = samba_ldb_connect(ldb, lp_ctx, url, flags);
316         if (ret != LDB_SUCCESS) {
317                 talloc_free(ldb);
318                 return NULL;
319         }
320
321         if (!ldb_wrap_add(url, ev, lp_ctx, session_info, credentials, flags, ldb)) {
322                 talloc_free(ldb);
323                 return NULL;
324         }
325
326         DEBUG(3,("ldb_wrap open of %s\n", url));
327
328         return ldb;
329 }
330
331 /*
332   call tdb_reopen_all() in case there is a TDB open so we are
333   not blocked from re-opening it inside ldb_tdb.
334 */
335  void ldb_wrap_fork_hook(void)
336 {
337         if (tdb_reopen_all(1) != 0) {
338                 smb_panic("tdb_reopen_all failed\n");
339         }
340 }
341
342  char *ldb_relative_path(struct ldb_context *ldb,
343                          TALLOC_CTX *mem_ctx,
344                          const char *name)
345 {
346         const char *base_url =
347                 (const char *)ldb_get_opaque(ldb, "ldb_url");
348         char *path, *p, *full_name;
349         if (name == NULL) {
350                 return NULL;
351         }
352         if (strncmp("tdb://", base_url, 6) == 0) {
353                 base_url = base_url+6;
354         }
355         path = talloc_strdup(mem_ctx, base_url);
356         if (path == NULL) {
357                 return NULL;
358         }
359         if ( (p = strrchr(path, '/')) != NULL) {
360                 p[0] = '\0';
361                 full_name = talloc_asprintf(mem_ctx, "%s/%s", path, name);
362         } else {
363                 full_name = talloc_asprintf(mem_ctx, "./%s", name);
364         }
365         talloc_free(path);
366         return full_name;
367 }