c33786a1e43a8c0b83b8259c9972357e23ff33d6
[jra/samba/.git] / source4 / lib / db_wrap.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    database wrap functions
5
6    Copyright (C) Andrew Tridgell 2004
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/util/dlinklist.h"
31 #include "lib/events/events.h"
32 #include "lib/tdb/include/tdb.h"
33 #include "lib/ldb/include/ldb.h"
34 #include "lib/ldb/include/ldb_errors.h"
35 #include "lib/ldb-samba/ldif_handlers.h"
36 #include "db_wrap.h"
37 #include "dsdb/samdb/samdb.h"
38 #include "param/param.h"
39
40 static struct tdb_wrap *tdb_list;
41
42 /*
43   this is used to catch debug messages from ldb
44 */
45 static void ldb_wrap_debug(void *context, enum ldb_debug_level level, 
46                            const char *fmt, va_list ap)  PRINTF_ATTRIBUTE(3,0);
47
48 static void ldb_wrap_debug(void *context, enum ldb_debug_level level, 
49                            const char *fmt, va_list ap)
50 {
51         int samba_level;
52         char *s = NULL;
53         switch (level) {
54         case LDB_DEBUG_FATAL:
55                 samba_level = 0;
56                 break;
57         case LDB_DEBUG_ERROR:
58                 samba_level = 1;
59                 break;
60         case LDB_DEBUG_WARNING:
61                 samba_level = 2;
62                 break;
63         case LDB_DEBUG_TRACE:
64                 samba_level = 5;
65                 break;
66                 
67         };
68         vasprintf(&s, fmt, ap);
69         if (!s) return;
70         DEBUG(level, ("ldb: %s\n", s));
71         free(s);
72 }
73
74 char *wrap_casefold(void *context, void *mem_ctx, const char *s)
75 {
76         return strupper_talloc(mem_ctx, s);
77 }
78
79 /* check for memory leaks on the ldb context */
80 static int ldb_wrap_destructor(struct ldb_context *ldb)
81 {
82         size_t *startup_blocks = (size_t *)ldb_get_opaque(ldb, "startup_blocks");
83         if (startup_blocks &&
84             talloc_total_blocks(ldb) > *startup_blocks + 400) {
85                 DEBUG(0,("WARNING: probable memory leak in ldb %s - %lu blocks (startup %lu) %lu bytes\n",
86                          (char *)ldb_get_opaque(ldb, "wrap_url"), 
87                          (unsigned long)talloc_total_blocks(ldb), 
88                          (unsigned long)*startup_blocks,
89                          (unsigned long)talloc_total_size(ldb)));
90 #if 0
91                 talloc_report_full(ldb, stdout);
92                 call_backtrace();
93                 smb_panic("probable memory leak in ldb");
94 #endif
95         }
96         return 0;
97 }                                
98
99 /*
100   wrapped connection to a ldb database
101   to close just talloc_free() the returned ldb_context
102
103   TODO:  We need an error_string parameter
104  */
105 struct ldb_context *ldb_wrap_connect(TALLOC_CTX *mem_ctx,
106                                      struct loadparm_context *lp_ctx,
107                                      const char *url,
108                                      struct auth_session_info *session_info,
109                                      struct cli_credentials *credentials,
110                                      unsigned int flags,
111                                      const char *options[])
112 {
113         struct ldb_context *ldb;
114         int ret;
115         struct event_context *ev;
116         char *real_url = NULL;
117         size_t *startup_blocks;
118
119         ldb = ldb_init(mem_ctx);
120         if (ldb == NULL) {
121                 return NULL;
122         }
123
124         ldb_set_modules_dir(ldb, 
125                             talloc_asprintf(ldb, "%s/ldb", lp_modulesdir(lp_ctx)));
126
127         /* we want to use the existing event context if possible. This
128            relies on the fact that in smbd, everything is a child of
129            the main event_context */
130         ev = event_context_find(ldb);
131
132         if (ldb_set_opaque(ldb, "EventContext", ev)) {
133                 talloc_free(ldb);
134                 return NULL;
135         }
136
137         if (ldb_set_opaque(ldb, "sessionInfo", session_info)) {
138                 talloc_free(ldb);
139                 return NULL;
140         }
141
142         if (ldb_set_opaque(ldb, "credentials", credentials)) {
143                 talloc_free(ldb);
144                 return NULL;
145         }
146         
147         if (strcmp(lp_sam_url(lp_ctx), url) == 0) {
148                 dsdb_set_global_schema(ldb);
149         }
150
151         ret = ldb_register_samba_handlers(ldb);
152         if (ret == -1) {
153                 talloc_free(ldb);
154                 return NULL;
155         }
156
157         ldb_set_debug(ldb, ldb_wrap_debug, NULL);
158
159         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
160
161         real_url = private_path(ldb, lp_ctx, url);
162         if (real_url == NULL) {
163                 talloc_free(ldb);
164                 return NULL;
165         }
166
167         /* allow admins to force non-sync ldb for all databases */
168         if (lp_parm_bool(lp_ctx, NULL, "ldb", "nosync", false)) {
169                 flags |= LDB_FLG_NOSYNC;
170         }
171
172         /* we usually want Samba databases to be private. If we later
173            find we need one public, we will need to add a parameter to
174            ldb_wrap_connect() */
175         ldb_set_create_perms(ldb, 0600);
176         
177         ret = ldb_connect(ldb, real_url, flags, options);
178         if (ret != LDB_SUCCESS) {
179                 talloc_free(ldb);
180                 return NULL;
181         }
182
183         /* setup for leak detection */
184         ldb_set_opaque(ldb, "wrap_url", real_url);
185         startup_blocks = talloc(ldb, size_t);
186         *startup_blocks = talloc_total_blocks(ldb);
187         ldb_set_opaque(ldb, "startup_blocks", startup_blocks);
188         
189         talloc_set_destructor(ldb, ldb_wrap_destructor);
190
191         return ldb;
192 }
193
194
195 /*
196  Log tdb messages via DEBUG().
197 */
198 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, 
199                          const char *format, ...) PRINTF_ATTRIBUTE(3,4);
200
201 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, 
202                          const char *format, ...)
203 {
204         va_list ap;
205         char *ptr = NULL;
206         int debug_level;
207
208         va_start(ap, format);
209         vasprintf(&ptr, format, ap);
210         va_end(ap);
211         
212         switch (level) {
213         case TDB_DEBUG_FATAL:
214                 debug_level = 0;
215                 break;
216         case TDB_DEBUG_ERROR:
217                 debug_level = 1;
218                 break;
219         case TDB_DEBUG_WARNING:
220                 debug_level = 2;
221                 break;
222         case TDB_DEBUG_TRACE:
223                 debug_level = 5;
224                 break;
225         default:
226                 debug_level = 0;
227         }               
228
229         if (ptr != NULL) {
230                 const char *name = tdb_name(tdb);
231                 DEBUG(debug_level, ("tdb(%s): %s", name ? name : "unnamed", ptr));
232                 free(ptr);
233         }
234 }
235
236
237 /* destroy the last connection to a tdb */
238 static int tdb_wrap_destructor(struct tdb_wrap *w)
239 {
240         tdb_close(w->tdb);
241         DLIST_REMOVE(tdb_list, w);
242         return 0;
243 }                                
244
245 /*
246   wrapped connection to a tdb database
247   to close just talloc_free() the tdb_wrap pointer
248  */
249 struct tdb_wrap *tdb_wrap_open(TALLOC_CTX *mem_ctx,
250                                const char *name, int hash_size, int tdb_flags,
251                                int open_flags, mode_t mode)
252 {
253         struct tdb_wrap *w;
254         struct tdb_logging_context log_ctx;
255         log_ctx.log_fn = tdb_wrap_log;
256
257         for (w=tdb_list;w;w=w->next) {
258                 if (strcmp(name, w->name) == 0) {
259                         return talloc_reference(mem_ctx, w);
260                 }
261         }
262
263         w = talloc(mem_ctx, struct tdb_wrap);
264         if (w == NULL) {
265                 return NULL;
266         }
267
268         w->name = talloc_strdup(w, name);
269
270         w->tdb = tdb_open_ex(name, hash_size, tdb_flags, 
271                              open_flags, mode, &log_ctx, NULL);
272         if (w->tdb == NULL) {
273                 talloc_free(w);
274                 return NULL;
275         }
276
277         talloc_set_destructor(w, tdb_wrap_destructor);
278
279         DLIST_ADD(tdb_list, w);
280
281         return w;
282 }