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