s4-ldbwrap: ensure session_info in ldb opaque remains valid
[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   see if two database opens are equivalent
99  */
100 static bool ldb_wrap_same_context(const struct ldb_wrap_context *c1,
101                                   const struct ldb_wrap_context *c2)
102 {
103         return (c1->ev == c2->ev &&
104                 c1->lp_ctx == c2->lp_ctx &&
105                 c1->session_info == c2->session_info &&
106                 c1->credentials == c2->credentials &&
107                 c1->flags == c2->flags &&
108                 (c1->url == c2->url || strcmp(c1->url, c2->url) == 0));
109 }
110
111 /* 
112    free a ldb_wrap structure
113  */
114 static int ldb_wrap_destructor(struct ldb_wrap *w)
115 {
116         DLIST_REMOVE(ldb_wrap_list, w);
117         return 0;
118 }
119
120 /*
121   wrapped connection to a ldb database
122   to close just talloc_free() the returned ldb_context
123
124   TODO:  We need an error_string parameter
125  */
126  struct ldb_context *ldb_wrap_connect(TALLOC_CTX *mem_ctx,
127                                      struct tevent_context *ev,
128                                      struct loadparm_context *lp_ctx,
129                                      const char *url,
130                                      struct auth_session_info *session_info,
131                                      struct cli_credentials *credentials,
132                                      unsigned int flags)
133 {
134         struct ldb_context *ldb;
135         int ret;
136         char *real_url = NULL;
137         struct ldb_wrap *w;
138         struct ldb_wrap_context c;
139
140         c.url          = url;
141         c.ev           = ev;
142         c.lp_ctx       = lp_ctx;
143         c.session_info = session_info;
144         c.credentials  = credentials;
145         c.flags        = flags;
146
147         /* see if we can re-use an existing ldb */
148         for (w=ldb_wrap_list; w; w=w->next) {
149                 if (ldb_wrap_same_context(&c, &w->context)) {
150                         return talloc_reference(mem_ctx, w->ldb);
151                 }
152         }
153
154         /* we want to use the existing event context if possible. This
155            relies on the fact that in smbd, everything is a child of
156            the main event_context */
157         if (ev == NULL) {
158                 return NULL;
159         }
160
161         ldb = ldb_init(mem_ctx, ev);
162         if (ldb == NULL) {
163                 return NULL;
164         }
165
166         ldb_set_modules_dir(ldb,
167                             talloc_asprintf(ldb,
168                                             "%s/ldb",
169                                             lpcfg_modulesdir(lp_ctx)));
170
171         if (session_info) {
172                 if (ldb_set_opaque(ldb, "sessionInfo", session_info)) {
173                         talloc_free(ldb);
174                         return NULL;
175                 }
176         }
177
178         if (credentials) {
179                 if (ldb_set_opaque(ldb, "credentials", credentials)) {
180                         talloc_free(ldb);
181                         return NULL;
182                 }
183         }
184
185         if (ldb_set_opaque(ldb, "loadparm", lp_ctx)) {
186                 talloc_free(ldb);
187                 return NULL;
188         }
189
190         /* This must be done before we load the schema, as these
191          * handlers for objectSid and objectGUID etc must take
192          * precedence over the 'binary attribute' declaration in the
193          * schema */
194         ret = ldb_register_samba_handlers(ldb);
195         if (ret == -1) {
196                 talloc_free(ldb);
197                 return NULL;
198         }
199
200         if (lp_ctx != NULL && strcmp(lpcfg_sam_url(lp_ctx), url) == 0) {
201                 dsdb_set_global_schema(ldb);
202         }
203
204         ldb_set_debug(ldb, ldb_wrap_debug, NULL);
205
206         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
207
208         real_url = private_path(ldb, lp_ctx, url);
209         if (real_url == NULL) {
210                 talloc_free(ldb);
211                 return NULL;
212         }
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         /* we usually want Samba databases to be private. If we later
224            find we need one public, we will need to add a parameter to
225            ldb_wrap_connect() */
226         ldb_set_create_perms(ldb, 0600);
227         
228         ret = ldb_connect(ldb, real_url, flags, NULL);
229         if (ret != LDB_SUCCESS) {
230                 talloc_free(ldb);
231                 return NULL;
232         }
233
234         /* setup for leak detection */
235         ldb_set_opaque(ldb, "wrap_url", real_url);
236         
237         /* add to the list of open ldb contexts */
238         w = talloc(ldb, struct ldb_wrap);
239         if (w == NULL) {
240                 talloc_free(ldb);
241                 return NULL;
242         }
243
244         w->context = c;
245         w->context.url = talloc_strdup(w, url);
246         if (w->context.url == NULL) {
247                 talloc_free(ldb);
248                 return NULL;
249         }
250
251         if (session_info) {
252                 /* take a reference to the session_info, as it is
253                  * possible for the ldb to live longer than the
254                  * session_info. This happens when a DRS DsBind call
255                  * reuses a handle, but the original connection is
256                  * shutdown. The token for the new connection is still
257                  * valid, so we need the session_info to remain valid for
258                  * ldb modules to use
259                  */
260                 if (talloc_reference(w, session_info) == NULL) {
261                         talloc_free(ldb);
262                         return NULL;
263                 }
264         }
265
266         w->ldb = ldb;
267
268         DLIST_ADD(ldb_wrap_list, w);
269
270         /* make the resulting schema global */
271         if (lp_ctx != NULL && strcmp(lpcfg_sam_url(lp_ctx), url) == 0) {
272                 struct dsdb_schema *schema = dsdb_get_schema(ldb, NULL);
273                 if (schema) {
274                         dsdb_make_schema_global(ldb, schema);
275                 }
276         }
277
278         DEBUG(3,("ldb_wrap open of %s\n", url));
279
280         talloc_set_destructor(w, ldb_wrap_destructor);
281
282         return ldb;
283 }
284
285 /*
286   when we fork() we need to make sure that any open ldb contexts have
287   any open transactions cancelled
288  */
289  void ldb_wrap_fork_hook(void)
290 {
291         struct ldb_wrap *w;
292
293         for (w=ldb_wrap_list; w; w=w->next) {
294                 if (ldb_transaction_cancel_noerr(w->ldb) != LDB_SUCCESS) {
295                         smb_panic("Failed to cancel child transactions\n");
296                 }
297         }       
298
299         if (tdb_reopen_all(1) == -1) {
300                 smb_panic("tdb_reopen_all failed\n");
301         }
302 }
303